From 41648228624a3258bbcd764f31e6da506cf92ad0 Mon Sep 17 00:00:00 2001 From: Yanhui Zhao Date: Wed, 6 Nov 2019 09:08:28 -0800 Subject: [PATCH] Update typedef, add ASSERT, and fix heap --- gdt.c | 2 +- idt.c | 2 +- include/ordered_array.h | 4 +- include/paging.h | 2 +- include/system.h | 30 ++++++++------ irq.c | 4 +- isrs.c | 2 +- kheap.c | 59 ++++++++++++++++++--------- main.c | 42 +++++++++++++------ ordered_array.c | 2 +- paging.c | 23 +++++------ scrn.c | 90 ++++++++++++++++++++++++++++++++++++----- timer.c | 6 +-- 13 files changed, 189 insertions(+), 79 deletions(-) diff --git a/gdt.c b/gdt.c index 27094c4..4bf4826 100644 --- a/gdt.c +++ b/gdt.c @@ -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; diff --git a/idt.c b/idt.c index 0008d34..e7bb705 100644 --- a/idt.c +++ b/idt.c @@ -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; diff --git a/include/ordered_array.h b/include/ordered_array.h index 8d6f0e4..1e56c1e 100644 --- a/include/ordered_array.h +++ b/include/ordered_array.h @@ -3,7 +3,7 @@ #include -typedef int (*less_than_predictate_t)(type_t, type_t); +typedef int32_t (*less_than_predictate_t)(type_t, type_t); typedef struct { @@ -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); diff --git a/include/paging.h b/include/paging.h index eb9e179..fbe0e54 100644 --- a/include/paging.h +++ b/include/paging.h @@ -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); diff --git a/include/system.h b/include/system.h index 51fcc86..fe1043b 100644 --- a/include/system.h +++ b/include/system.h @@ -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(); @@ -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); diff --git a/irq.c b/irq.c index 5732197..517332b 100644 --- a/irq.c +++ b/irq.c @@ -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; } diff --git a/isrs.c b/isrs.c index 71bba99..2cb2be7 100644 --- a/isrs.c +++ b/isrs.c @@ -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; } diff --git a/kheap.c b/kheap.c index 7e399e7..b27e3f0 100644 --- a/kheap.c +++ b/kheap.c @@ -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) @@ -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) @@ -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) @@ -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; } @@ -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; @@ -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) @@ -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; @@ -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) { @@ -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) @@ -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 { @@ -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; @@ -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); } diff --git a/main.c b/main.c index aabc95e..5b9a4b8 100644 --- a/main.c +++ b/main.c @@ -2,7 +2,7 @@ #include #include -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; @@ -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) { @@ -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) { @@ -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; @@ -54,7 +54,7 @@ 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"); @@ -62,6 +62,22 @@ void panic(const char *message, const char *file) 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(;;); } @@ -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); diff --git a/ordered_array.c b/ordered_array.c index 1696b54..bdd0ca4 100644 --- a/ordered_array.c +++ b/ordered_array.c @@ -1,7 +1,7 @@ #include #include -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; } diff --git a/paging.c b/paging.c index b655bc9..d3c81cf 100644 --- a/paging.c +++ b/paging.c @@ -57,7 +57,7 @@ static uint32_t first_frame() } } -void alloc_frame(page_t *page, int is_kernel, int is_writeable) +void alloc_frame(page_t *page, int32_t is_kernel, int32_t is_writeable) { if (page->frame != 0) { @@ -105,7 +105,7 @@ void init_paging() memset(kernel_directory, 0, sizeof(page_directory_t)); current_directory = kernel_directory; - int i = 0; + int32_t i = 0; for (i = KHEAP_START; i < KHEAP_START + KHEAP_INITIAL_SIZE; i += 0x1000) get_page(i, 1, kernel_directory); @@ -136,7 +136,7 @@ void switch_page_directory(page_directory_t *dir) __asm__ __volatile__("mov %0, %%cr0":: "r"(cr0)); } -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) { address /= 0x1000; uint32_t table_idx = address / 1024; @@ -160,17 +160,16 @@ page_t *get_page(uint32_t address, int make, page_directory_t *dir) void page_fault_handler(register_t regs) { - puts("Calling page fault handler"); uint32_t faulting_address; __asm__ __volatile__("mov %%cr2, %0" : "=r" (faulting_address)); - int present = !(regs.err_code & 0x1); - int rw = regs.err_code & 0x2; - int us = regs.err_code & 0x4; - int reserved = regs.err_code & 0x8; - int id = regs.err_code & 0x10; + int32_t present = !(regs.err_code & 0x1); + int32_t rw = regs.err_code & 0x2; + int32_t us = regs.err_code & 0x4; + int32_t reserved = regs.err_code & 0x8; + int32_t id = regs.err_code & 0x10; - puts("Page fault! ("); + puts("Page fault! ( "); if (present) puts("present "); if (rw) @@ -179,8 +178,8 @@ void page_fault_handler(register_t regs) puts("user-mode "); if (reserved) puts("reserved "); - puts(") at 0x"); - // puts(faulting_address); + puts(") at "); + write_hex(faulting_address); puts("\n"); PANIC("Page fault"); } \ No newline at end of file diff --git a/scrn.c b/scrn.c index cbcbba2..d946529 100644 --- a/scrn.c +++ b/scrn.c @@ -1,13 +1,13 @@ #include uint16_t *textmemptr; -int attrib = 0x0F; -int csr_x = 0; -int csr_y = 0; +int32_t attrib = 0x0F; +int32_t csr_x = 0; +int32_t csr_y = 0; void scroll(void) { - unsigned blank, temp; + uint16_t blank, temp; blank = 0x20 | (attrib << 8); @@ -22,7 +22,7 @@ void scroll(void) void move_csr(void) { - unsigned temp; + uint16_t temp; temp = csr_y * 80 + csr_x; outportb(0x3D4, 14); @@ -33,8 +33,8 @@ void move_csr(void) void cls() { - unsigned blank; - int i; + uint16_t blank; + int32_t i; blank = 0x20 | (attrib << 8); @@ -46,10 +46,10 @@ void cls() move_csr(); } -void putch(uint8_t c) +void putch(char c) { uint16_t *where; - unsigned att = attrib << 8; + uint16_t att = attrib << 8; if (c == 0x08) { @@ -86,13 +86,81 @@ void putch(uint8_t c) move_csr(); } -void puts(uint8_t *text) +void puts(char *text) { - int i; + int32_t i; for (i = 0; i < strlen(text); i++) putch(text[i]); } +void write_hex(uint32_t n) +{ + int32_t tmp; + puts("0x"); + + char noZeroes = 1; + + int32_t i; + for (i = 28; i > 0; i -= 4) + { + tmp = (n >> i) & 0xF; + if (tmp == 0 && noZeroes != 0) + { + continue; + } + + if (tmp >= 0xA) + { + noZeroes = 0; + putch(tmp - 0xA + 'a'); + } + else + { + noZeroes = 0; + putch(tmp + '0'); + } + } + + tmp = n & 0xF; + if (tmp >= 0xA) + { + putch(tmp - 0xA + 'a'); + } + else + { + putch(tmp + '0'); + } +} + +void write_dec_str(uint32_t n) +{ + if (n == 0) + { + puts('0'); + return; + } + + int32_t acc = n; + char c[32]; + int32_t i = 0; + while (acc > 0) + { + c[i] = '0' + acc % 10; + acc /= 10; + i++; + } + c[i] = 0; + + char c2[32]; + c2[i--] = 0; + int32_t j = 0; + while (i >= 0) + { + c2[i--] = c[j++]; + } + puts(c2); +} + void settextcolor(uint8_t forecolor, uint8_t backcolor) { attrib = (backcolor << 4) | (forecolor & 0x0F); diff --git a/timer.c b/timer.c index 626dc97..463ba92 100644 --- a/timer.c +++ b/timer.c @@ -13,7 +13,7 @@ void timer_handler(register_t *r) void timer_install(uint32_t frequency) { - irq_install_handler(0, timer_handler); + // irq_install_handler(0, timer_handler); uint32_t divisor = 1193180 / frequency; outportb(0x43, 0x36); @@ -25,9 +25,9 @@ void timer_install(uint32_t frequency) } -void timer_wait(int ticks) +void timer_wait(int32_t ticks) { - unsigned long eticks; + uint64_t eticks; eticks = timer_ticks + ticks; while (timer_ticks < eticks)