Skip to content

Commit

Permalink
Update typedef, add ASSERT, and fix heap
Browse files Browse the repository at this point in the history
  • Loading branch information
yzz127 committed Nov 6, 2019
1 parent 1e68156 commit 4164822
Show file tree
Hide file tree
Showing 13 changed files with 189 additions and 79 deletions.
2 changes: 1 addition & 1 deletion gdt.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ gdt_ptr_t gp;

extern void gdt_flush();

void gdt_set_gate(int num, unsigned long base, unsigned long limit, uint8_t access, uint8_t gran)
void gdt_set_gate(int32_t num, uint32_t base, uint32_t limit, uint8_t access, uint8_t gran)
{
gdt[num].base_low = (base & 0xFFFF);
gdt[num].base_middle = (base >> 16) & 0xFF;
Expand Down
2 changes: 1 addition & 1 deletion idt.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ idt_ptr_t idtp;

extern void idt_load();

void idt_set_gate(uint8_t num, unsigned long base, uint16_t sel, uint8_t flags)
void idt_set_gate(uint8_t num, uint32_t base, uint16_t sel, uint8_t flags)
{
idt[num].base_lo = base & 0xFFFF;
idt[num].base_hi = (base >> 16) & 0xFFFF;
Expand Down
4 changes: 2 additions & 2 deletions include/ordered_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <system.h>

typedef int (*less_than_predictate_t)(type_t, type_t);
typedef int32_t (*less_than_predictate_t)(type_t, type_t);

typedef struct
{
Expand All @@ -13,7 +13,7 @@ typedef struct
less_than_predictate_t less_than;
} ordered_array_t;

int standard_less_than_predicate(type_t a, type_t b);
int32_t standard_less_than_predicate(type_t a, type_t b);

ordered_array_t create_ordered_array(uint32_t max_size, less_than_predictate_t less_than);
ordered_array_t place_ordered_array(void * addr, uint32_t max_size, less_than_predictate_t less_than);
Expand Down
2 changes: 1 addition & 1 deletion include/paging.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void init_paging();

void switch_page_directory(page_directory_t *new);

page_t *get_page(uint32_t address, int make, page_directory_t *dir);
page_t *get_page(uint32_t address, int32_t make, page_directory_t *dir);

void page_fault_handler(register_t regs);

Expand Down
30 changes: 17 additions & 13 deletions include/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,36 @@

#define NULL 0

typedef signed char int8_t;
typedef char int8_t;
typedef short int16_t;
typedef long int32_t;
typedef int int32_t;
typedef long long int64_t;

typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned long uint32_t;
typedef unsigned int uint32_t;
typedef unsigned long long uint64_t;

typedef void * type_t;

/* MAIN.C */
extern uint8_t *memcpy(uint8_t *dest, const uint8_t *src, int count);
extern uint8_t *memset(uint8_t *dest, uint8_t val, int count);
extern uint16_t *memsetw(uint16_t *dest, uint16_t val, int count);
extern uint8_t *memcpy(uint8_t *dest, const uint8_t *src, int32_t count);
extern uint8_t *memset(uint8_t *dest, uint8_t val, int32_t count);
extern uint16_t *memsetw(uint16_t *dest, uint16_t val, int32_t count);
extern int strlen(const char *str);
extern uint8_t inportb(uint16_t _port);
extern void outportb(uint16_t _port, uint8_t _data);
extern void panic(const char *message, const char *file);
extern void panic(const char *message, const char *file, uint32_t line);
extern void panic_assert(const char *file, uint32_t line, const char *desc);

#define PANIC(msg) panic(msg, __FILE__);
#define PANIC(msg) panic(msg, __FILE__, __LINE__);
#define ASSERT(b) ((b) ? (void)0 : panic_assert(__FILE__, __LINE__, #b));

extern void cls();
extern void putch(uint8_t c);
extern void puts(uint8_t *str);
extern void putch(char c);
extern void puts(char *str);
void write_hex(uint32_t n);
void write_dec_str(uint32_t n);
extern void settextcolor(uint8_t forecolor, uint8_t backcolor);
extern void init_video();

Expand All @@ -44,11 +48,11 @@ typedef struct regs
uint32_t eip, cs, eflags, useresp, ss;
} register_t;

void irq_install_handler(int irq, void (*handler)(register_t *r));
void irq_uninstall_handler(int irq);
void irq_install_handler(int32_t irq, void (*handler)(register_t *r));
void irq_uninstall_handler(int32_t irq);
void irq_install();

void irs_install_handler(int irs, void (*handler)(register_t *r));
void irs_install_handler(int32_t irs, void (*handler)(register_t *r));

void timer_install(uint32_t frequency);

Expand Down
4 changes: 2 additions & 2 deletions irq.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ void *irq_routines[16] =
0, 0, 0, 0, 0, 0, 0, 0
};

void irq_install_handler(int irq, void (*handler)(register_t *r))
void irq_install_handler(int32_t irq, void (*handler)(register_t *r))
{
irq_routines[irq] = handler;
}

void irq_uninstall_handler(int irq)
void irq_uninstall_handler(int32_t irq)
{
irq_routines[irq] = 0;
}
Expand Down
2 changes: 1 addition & 1 deletion isrs.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ void *irs_routines[32] =
0, 0, 0, 0, 0, 0, 0, 0
};

void irs_install_handler(int irs, void (*handler)(register_t *r))
void irs_install_handler(int32_t irs, void (*handler)(register_t *r))
{
irs_routines[irs] = handler;
}
Expand Down
59 changes: 41 additions & 18 deletions kheap.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,33 @@ uint32_t start_address = (uint32_t)&end;
extern page_directory_t *kernel_directory;
heap_t *kheap = 0;

uint32_t kmalloc_internel(uint32_t size, int align, uint32_t *phys)
uint32_t kmalloc_internel(uint32_t size, int32_t align, uint32_t *phys)
{
if (align == 1 && (start_address & 0xFFFFF000))
if (kheap != 0)
{
start_address &= 0xFFFFF000;
start_address += 0x1000;
void *addr = alloc(size, (uint8_t)align, kheap);
if (phys != 0)
{
page_t *page = get_page((uint32_t)addr, 0, kernel_directory);
*phys = page->frame * 0x1000 + (uint32_t)addr & 0xFFF;
}
return (uint32_t)addr;
}
if (phys)
else
{
*phys = start_address;
if (align == 1 && (start_address & 0xFFFFF000))
{
start_address &= 0xFFFFF000;
start_address += 0x1000;
}
if (phys)
{
*phys = start_address;
}
uint32_t tmp = start_address;
start_address += size;
return tmp;
}
uint32_t tmp = start_address;
start_address += size;
return tmp;
}

void kfree(void *p)
Expand All @@ -47,8 +60,7 @@ uint32_t kmalloc(uint32_t size)
return kmalloc_internel(size, 0, NULL);
}


static int find_smallest_hole(uint32_t size, uint8_t page_align, heap_t *heap)
static int32_t find_smallest_hole(uint32_t size, uint8_t page_align, heap_t *heap)
{
uint32_t iterator = 0;
while (iterator < heap->index.size)
Expand All @@ -57,11 +69,11 @@ static int find_smallest_hole(uint32_t size, uint8_t page_align, heap_t *heap)
if (page_align > 0)
{
uint32_t location = (uint32_t)header;
int offset = 0;
int32_t offset = 0;
if ((location + sizeof(header_t)) & 0xFFFFF000 != 0)
offset = 0x1000 - (location + sizeof(header_t)) % 0x1000;
int hole_size = (int)header->size - offset;
if (hole_size >= (int)size)
int32_t hole_size = (int32_t)header->size - offset;
if (hole_size >= (int32_t)size)
break;
}
else if (header->size >= size)
Expand All @@ -74,7 +86,7 @@ static int find_smallest_hole(uint32_t size, uint8_t page_align, heap_t *heap)
return iterator;
}

static int header_t_less_than(void *a, void *b)
static int32_t header_t_less_than(void *a, void *b)
{
return (((header_t *)a)->size < ((header_t *)b)->size)? 1 : 0;
}
Expand All @@ -83,6 +95,9 @@ heap_t *create_heap(uint32_t start, uint32_t end_addr, uint32_t max, uint8_t sup
{
heap_t *heap = (heap_t *)kmalloc(sizeof(heap_t));

ASSERT(start % 0x1000 == 0);
ASSERT(end_addr % 0x1000 == 0);

heap->index = place_ordered_array((void *)start, HEAP_INDEX_SIZE, &header_t_less_than);
start += sizeof(type_t) * HEAP_INDEX_SIZE;

Expand Down Expand Up @@ -115,6 +130,8 @@ static void expand(uint32_t new_size, heap_t *heap)
new_size += 0x1000;
}

ASSERT(heap->start_address + new_size <= heap->max_address);

uint32_t old_size = heap->end_address - heap->start_address;
uint32_t i = old_size;
while (i < new_size)
Expand All @@ -127,6 +144,8 @@ static void expand(uint32_t new_size, heap_t *heap)

static uint32_t contract(uint32_t new_size, heap_t *heap)
{
ASSERT(new_size < heap->end_address - heap->start_address);

if (new_size & 0x1000)
{
new_size &= 0x1000;
Expand All @@ -150,7 +169,7 @@ static uint32_t contract(uint32_t new_size, heap_t *heap)
void *alloc(uint32_t size, uint8_t page_align, heap_t *heap)
{
uint32_t new_size = size + sizeof(header_t) + sizeof(footer_t);
int iterator = find_smallest_hole(new_size, page_align, heap);
int32_t iterator = find_smallest_hole(new_size, page_align, heap);

if (iterator == -1)
{
Expand Down Expand Up @@ -204,7 +223,7 @@ void *alloc(uint32_t size, uint8_t page_align, heap_t *heap)
if (orig_hole_size - new_size < sizeof(header_t) + sizeof(footer_t));
{
size += orig_hole_size - new_size;
new_size = orig_hole_pos;
new_size = orig_hole_size;
}

if (page_align && orig_hole_pos & 0xFFFFF000)
Expand All @@ -218,7 +237,7 @@ void *alloc(uint32_t size, uint8_t page_align, heap_t *heap)
hole_footer->magic = HEAP_MAGIC;
hole_footer->header = hole_header;
orig_hole_pos = new_location;
orig_hole_size = orig_hole_size -hole_header->size;
orig_hole_size = orig_hole_size - hole_header->size;
}
else
{
Expand Down Expand Up @@ -260,6 +279,9 @@ void free(void *p, heap_t *heap)
header_t *header = (header_t *)((uint32_t)p - sizeof(header_t));
footer_t *footer = (footer_t *)((uint32_t)header + header->size - sizeof(footer_t));

ASSERT(header->magic == HEAP_MAGIC);
ASSERT(footer->magic == HEAP_MAGIC);

header->is_hole = 1;

char do_add = 1;
Expand All @@ -283,6 +305,7 @@ void free(void *p, heap_t *heap)
uint32_t iterator = 0;
while ((iterator < heap->index.size) && (lookup_ordered_array(iterator, &heap->index) != (void *)test_header))
iterator++;
ASSERT(iterator < heap->index.size);
remove_ordered_array(iterator, &heap->index);
}

Expand Down
42 changes: 29 additions & 13 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <paging.h>
#include <kheap.h>

uint8_t *memcpy(uint8_t *dest, const uint8_t *src, int count)
uint8_t *memcpy(uint8_t *dest, const uint8_t *src, int32_t count)
{
const char *sp = (const char *)src;
char *dp = (char *)dest;
Expand All @@ -14,7 +14,7 @@ uint8_t *memcpy(uint8_t *dest, const uint8_t *src, int count)

}

uint8_t *memset(uint8_t *dest, uint8_t val, int count)
uint8_t *memset(uint8_t *dest, uint8_t val, int32_t count)
{
char *temp = (char *)dest;
while (count > 0) {
Expand All @@ -24,7 +24,7 @@ uint8_t *memset(uint8_t *dest, uint8_t val, int count)
return dest;
}

uint16_t *memsetw(uint16_t *dest, uint16_t val, int count)
uint16_t *memsetw(uint16_t *dest, uint16_t val, int32_t count)
{
uint16_t *temp = (uint16_t *)dest;
while (count > 0) {
Expand All @@ -34,9 +34,9 @@ uint16_t *memsetw(uint16_t *dest, uint16_t val, int count)
return dest;
}

int strlen(const char *str)
int32_t strlen(const char *str)
{
int retval;
int32_t retval;
for (retval = 0; *str != '\0'; str++)
retval++;
return retval;
Expand All @@ -54,14 +54,30 @@ void outportb(uint16_t _port, uint8_t _data)
__asm__ __volatile__("outb %1, %0" : : "dN" (_port), "a" (_data));
}

void panic(const char *message, const char *file)
extern void panic(const char *message, const char *file, uint32_t line)
{
__asm__ __volatile__("cli");

puts("PANIC(");
puts(message);
puts(") at ");
puts(file);
puts(":");
write_dec_str(line);
puts("\n");
for(;;);
}

extern void panic_assert(const char *file, uint32_t line, const char *desc)
{
asm volatile("cli");

puts("ASSERTION-FAILED(");
puts(desc);
puts(") at ");
puts(file);
puts(":");
write_dec_str(line);
puts("\n");
for(;;);
}
Expand All @@ -80,24 +96,24 @@ void main()
__asm__ __volatile__("sti");
puts("System Start!\n");

//uint32_t *ptr = (uint32_t *)0xA0000000;
//uint32_t do_page_fault = *ptr;
//puts(do_page_fault);
// uint32_t *ptr = (uint32_t *)0xA0000000;
// uint32_t do_page_fault = *ptr;
// puts(do_page_fault);

uint32_t b = kmalloc(8);
uint32_t c = kmalloc(8);
puts("a: ");
puts(a);
write_hex(a);
puts(", b: ");
puts(b);
write_hex(b);
puts("\nc: ");
puts(c);
write_hex(c);

kfree(c);
kfree(b);
uint32_t d = kmalloc(12);
puts(", d: ");
puts(d);
write_hex(d);

// int i = 8 / 0;
// puts(i);
Expand Down
2 changes: 1 addition & 1 deletion ordered_array.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <system.h>
#include <ordered_array.h>

int standard_less_than_predicate(type_t a, type_t b)
int32_t standard_less_than_predicate(type_t a, type_t b)
{
return (a < b)? 1 : 0;
}
Expand Down
Loading

0 comments on commit 4164822

Please sign in to comment.