From e4717813f3a406ac0acebb797bded7319d7ee7ca Mon Sep 17 00:00:00 2001 From: Yanhui Zhao Date: Wed, 30 Oct 2019 21:32:25 -0700 Subject: [PATCH] update unsigned data type with typedef uint*_t type --- gdt.c | 18 +++++------ idt.c | 16 +++++----- include/system.h | 40 +++++++++++++++---------- isrs.c | 2 +- kb.c | 4 +-- kheap.c | 12 ++++---- main.c | 14 ++++----- paging.c | 77 ++++++++++++++++++++++++++++++++++++------------ paging.h | 20 ++++++------- scrn.c | 12 ++++---- timer.c | 8 ++--- 11 files changed, 136 insertions(+), 87 deletions(-) diff --git a/gdt.c b/gdt.c index 353aa17..27094c4 100644 --- a/gdt.c +++ b/gdt.c @@ -2,18 +2,18 @@ typedef struct gdt_entry { - unsigned short limit_low; - unsigned short base_low; - unsigned char base_middle; - unsigned char access; - unsigned char granularity; - unsigned char base_high; + uint16_t limit_low; + uint16_t base_low; + uint8_t base_middle; + uint8_t access; + uint8_t granularity; + uint8_t base_high; } __attribute__((packed)) gdt_entry_t; typedef struct gdt_ptr { - unsigned short limit; - unsigned int base; + uint16_t limit; + uint32_t base; } __attribute__ ((packed)) gdt_ptr_t; gdt_entry_t gdt[5]; @@ -21,7 +21,7 @@ gdt_ptr_t gp; extern void gdt_flush(); -void gdt_set_gate(int num, unsigned long base, unsigned long limit, unsigned char access, unsigned char gran) +void gdt_set_gate(int num, unsigned long base, unsigned long 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 cee58d0..0008d34 100644 --- a/idt.c +++ b/idt.c @@ -2,17 +2,17 @@ typedef struct idt_entry { - unsigned short base_lo; - unsigned short sel; - unsigned char always0; - unsigned char flags; - unsigned short base_hi; + uint16_t base_lo; + uint16_t sel; + uint8_t always0; + uint8_t flags; + uint16_t base_hi; } __attribute__ ((packed)) idt_entry_t; typedef struct idt_ptr { - unsigned short limit; - unsigned int base; + uint16_t limit; + uint32_t base; } __attribute__ ((packed)) idt_ptr_t; idt_entry_t idt[256]; @@ -20,7 +20,7 @@ idt_ptr_t idtp; extern void idt_load(); -void idt_set_gate(unsigned char num, unsigned long base, unsigned short sel, unsigned char flags) +void idt_set_gate(uint8_t num, unsigned long base, uint16_t sel, uint8_t flags) { idt[num].base_lo = base & 0xFFFF; idt[num].base_hi = (base >> 16) & 0xFFFF; diff --git a/include/system.h b/include/system.h index c030d84..f7677f6 100644 --- a/include/system.h +++ b/include/system.h @@ -1,18 +1,30 @@ #ifndef __SYSTEM_H #define __SYSTEM_H +#define NULL 0 + +typedef signed char int8_t; +typedef short int16_t; +typedef long int32_t; +typedef long long int64_t; + +typedef unsigned char uint8_t; +typedef unsigned short uint16_t; +typedef unsigned long uint32_t; +typedef unsigned long long uint64_t; + /* MAIN.C */ -extern unsigned char *memecpy(unsigned char *dest, const unsigned char *src, int count); -extern unsigned char *emeset(unsigned char *dest, unsigned char val, int count); -extern unsigned short *memsetw(unsigned short *dest, unsigned short val, int count); +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 int strlen(const char *str); -extern unsigned char inportb(unsigned short _port); -extern void outportb(unsigned short _port, unsigned char _data); +extern uint8_t inportb(uint16_t _port); +extern void outportb(uint16_t _port, uint8_t _data); extern void cls(); -extern void putch(unsigned char c); -extern void puts(unsigned char *str); -extern void settextcolor(unsigned char forecolor, unsigned char backcolor); +extern void putch(uint8_t c); +extern void puts(uint8_t *str); +extern void settextcolor(uint8_t forecolor, uint8_t backcolor); extern void init_video(); extern void gdt_install(); @@ -21,20 +33,18 @@ extern void isrs_install(); typedef struct regs { - unsigned int gs, fs, es, ds; - unsigned int edi, esi, ebp, esp, ebx, edx, ecx, eax; - unsigned int int_no, err_code; - unsigned int eip, cs, eflags, useresp, ss; + uint32_t gs, fs, es, ds; + uint32_t edi, esi, ebp, esp, ebx, edx, ecx, eax; + uint32_t int_no, err_code; + 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(); -void timer_install(unsigned int frequency); +void timer_install(uint32_t frequency); void keyborad_install(); -#define NULL 0 - #endif \ No newline at end of file diff --git a/isrs.c b/isrs.c index fed9172..d91773f 100644 --- a/isrs.c +++ b/isrs.c @@ -69,7 +69,7 @@ void isrs_install() idt_set_gate(31, (unsigned)_isr31, 0x08, 0x8E); } -unsigned char *exception_messages[] = +uint8_t *exception_messages[] = { "Division By Zero", "Debug", diff --git a/kb.c b/kb.c index 0a7de46..0885390 100644 --- a/kb.c +++ b/kb.c @@ -1,6 +1,6 @@ #include -unsigned char kbdus[128] = +uint8_t kbdus[128] = { 0, 27, '1', '2', '3', '4', '5', '6', '7', '8', /* 9 */ '9', '0', '-', '=', '\b', /* Backspace */ @@ -42,7 +42,7 @@ unsigned char kbdus[128] = void keyboard_handler(register_t *r) { - unsigned char scancode; + uint8_t scancode; scancode = inportb(0x60); diff --git a/kheap.c b/kheap.c index b692cb1..675470c 100644 --- a/kheap.c +++ b/kheap.c @@ -1,6 +1,6 @@ #include -unsigned int kmalloc_internel(unsigned int size, int align, unsigned int *phys) +uint32_t kmalloc_internel(uint32_t size, int align, uint32_t *phys) { if (align == 1 && (start_address & 0xFFFFF000)) { @@ -11,26 +11,26 @@ unsigned int kmalloc_internel(unsigned int size, int align, unsigned int *phys) { *phys = start_address; } - unsigned int tmp = start_address; + uint32_t tmp = start_address; start_address += size; return tmp; } -unsigned int kmalloc_a(unsigned int size) { +uint32_t kmalloc_a(uint32_t size) { return kmalloc_internel(size, 1, NULL); } -unsigned int kmalloc_p(unsigned int size, unsigned int *phys) +uint32_t kmalloc_p(uint32_t size, uint32_t *phys) { return kmalloc_internel(size, 0, phys); } -unsigned int kmalloc_ap(unsigned int size, unsigned int *phys) +uint32_t kmalloc_ap(uint32_t size, uint32_t *phys) { return kmalloc_internel(size, 1, phys); } -unsigned int kmalloc(unsigned int size) +uint32_t kmalloc(uint32_t size) { return kmalloc_internel(size, 0, NULL); } diff --git a/main.c b/main.c index b31c20f..d4388c7 100644 --- a/main.c +++ b/main.c @@ -1,6 +1,6 @@ #include -unsigned char *memcpy(unsigned char *dest, const unsigned char *src, int count) +uint8_t *memcpy(uint8_t *dest, const uint8_t *src, int count) { const char *sp = (const char *)src; char *dp = (char *)dest; @@ -12,7 +12,7 @@ unsigned char *memcpy(unsigned char *dest, const unsigned char *src, int count) } -unsigned char *memset(unsigned char *dest, unsigned char val, int count) +uint8_t *memset(uint8_t *dest, uint8_t val, int count) { char *temp = (char *)dest; while (count > 0) { @@ -22,9 +22,9 @@ unsigned char *memset(unsigned char *dest, unsigned char val, int count) return dest; } -unsigned short *memsetw(unsigned short *dest, unsigned short val, int count) +uint16_t *memsetw(uint16_t *dest, uint16_t val, int count) { - unsigned short *temp = (unsigned short *)dest; + uint16_t *temp = (uint16_t *)dest; while (count > 0) { *temp++ = val; count--; @@ -40,14 +40,14 @@ int strlen(const char *str) return retval; } -unsigned char inportb(unsigned short _port) +uint8_t inportb(uint16_t _port) { - unsigned char rv; + uint8_t rv; __asm__ __volatile__("inb %1, %0" : "=a" (rv) : "dN" (_port)); return rv; } -void outportb(unsigned short _port, unsigned char _data) +void outportb(uint16_t _port, uint8_t _data) { __asm__ __volatile__("outb %1, %0" : : "dN" (_port), "a" (_data)); } diff --git a/paging.c b/paging.c index fcfcd38..77f75c5 100644 --- a/paging.c +++ b/paging.c @@ -1,47 +1,47 @@ #include -unsigned int *frames; -unsigned int nframes; +uint32_t *frames; +uint32_t nframes; -extern unsigned int start_address; +extern uint32_t start_address; #define INDEX_FROM_BIT(a) (a / (8 * 4)) #define OFFSET_FROM_BIT(a) (a % (8 * 4)) -static void set_frame(unsigned int frame_addr) +static void set_frame(uint32_t frame_addr) { - unsigned int frame = frame_addr / 0x1000; - unsigned int idx = INDEX_FROM_BIT(frame); - unsigned int off = OFFSET_FROM_BIT(frame); + uint32_t frame = frame_addr / 0x1000; + uint32_t idx = INDEX_FROM_BIT(frame); + uint32_t off = OFFSET_FROM_BIT(frame); frames[idx] |= (0x1 << off); } static void clear_frame(unsigned frame_addr) { - unsigned int frame = frame_addr / 0x1000; - unsigned int idx = INDEX_FROM_BIT(frame); - unsigned int off = OFFSET_FROM_BIT(frame); + uint32_t frame = frame_addr / 0x1000; + uint32_t idx = INDEX_FROM_BIT(frame); + uint32_t off = OFFSET_FROM_BIT(frame); frames[idx] &= ~(0x1 << off); } static void test_frame(unsigned frame_addr) { - unsigned int frame = frame_addr / 0x1000; - unsigned int idx = INDEX_FROM_BIT(frame); - unsigned int off = OFFSET_FROM_BIT(frame); + uint32_t frame = frame_addr / 0x1000; + uint32_t idx = INDEX_FROM_BIT(frame); + uint32_t off = OFFSET_FROM_BIT(frame); return (frames[idx] & (0x1 << off)); } -static unsigned int first_frame() +static uint32_t first_frame() { - unsigned int i, j; + uint32_t i, j; for (i = 0; i < INDEX_FROM_BIT(nframes); i++) { if (frames[i] != 0xFFFFFFFF) { for (j = 0; j < 32; j++) { - unsigned int toTest = 0x1 << j; + uint32_t toTest = 0x1 << j; if (!(frames[i] & toTest)) { return i * 4 * 8 + j; @@ -59,8 +59,8 @@ void alloc_frame(page_t *page, int is_kernel, int is_writeable) } else { - unsigned int idx = first_frame(); - if (idx == (unsigned int)-1) + uint32_t idx = first_frame(); + if (idx == (uint32_t)-1) { PANIC("No free frames!"); } @@ -75,7 +75,7 @@ void alloc_frame(page_t *page, int is_kernel, int is_writeable) void free_frame(page_t *page) { - unsigned int frame; + uint32_t frame; if (!(frame = page->frame)) { return; @@ -85,4 +85,43 @@ void free_frame(page_t *page) clear_frame(frame); page->frame = 0x0; } +} + +void init_paging() +{ + uint32_t mem_end_page = 0x1000000; // The size of the physical memory is assumed to be 16MB + + nframes = mem_end_page / 0x1000; + frames = (uint32_t *)kmalloc(INDEX_FROM_BIT(nframes)); + memset(frames, 0, INDEX_FROM_BIT(nframes)); + + kernel_directory = (page_directory_t *)kmalloc_a(sizeof(page_directory_t)); + memset(kernel_directory, 0, sizeof(page_directory_t)); + current_directory = kernel_directory; + + int i = 0; + while (i < start_address) + { + alloc_frame(get_page(i, 1, kernel_directory), 0, 0); + i += 0x1000; + } + + irq_install_handler(14, page_fault); + + swict_page_directory(kernel_directory); +} + +void switch_page_directory(page_directory_t *dir) +{ + current_directory = dir; + asm volatile("mov %0, %%cr3":: "r"(&dir->tablesPhysical)); + uint32_t cr0; + asm volatile("mov %%cr0, %0": "=r"(cr0)); + cr0 |= 0x80000000; // Enable paging + asm volatile("mov %0, %%cr0":: "r"(cr0)); +} + +page_t *get_page(uint32_t address, int make, page_directory_t *dir) +{ + } \ No newline at end of file diff --git a/paging.h b/paging.h index f0a565f..4cf4886 100644 --- a/paging.h +++ b/paging.h @@ -5,13 +5,13 @@ typedef struct page { - unsigned int present : 1; // Page present - unsigned int rw : 1; // 0 for read-only, 1 for read-write - unsigned int user : 1; // 0 for user-mode, 1 for kernel-mode - unsigned int accessed : 1; // Check page has been accessed since last refresh - unsigned int dirty : 1; // Check page has been written since last refresh - unsigned int unused : 7; // Unused and reserved bits - unsigned int frame : 20; // Frame address + uint32_t present : 1; // Page present + uint32_t rw : 1; // 0 for read-only, 1 for read-write + uint32_t user : 1; // 0 for user-mode, 1 for kernel-mode + uint32_t accessed : 1; // Check page has been accessed since last refresh + uint32_t dirty : 1; // Check page has been written since last refresh + uint32_t unused : 7; // Unused and reserved bits + uint32_t frame : 20; // Frame address } page_t; typedef struct page_table @@ -22,15 +22,15 @@ typedef struct page_table typedef struct page_directory { page_table_t *tables[1024]; - unsigned int tablesPhysical[1024]; - unsigned int physicalAddr; + uint32_t tablesPhysical[1024]; + uint32_t physicalAddr; } page_directory_t; void init_paging(); void switch_page_directory(page_directory_t *new); -struct page *get_page(unsigned int address, int make, page_directory_t *dir); +struct page *get_page(uint32_t address, int make, page_directory_t *dir); void page_fault(register_t regs); diff --git a/scrn.c b/scrn.c index 532d125..cbcbba2 100644 --- a/scrn.c +++ b/scrn.c @@ -1,6 +1,6 @@ #include -unsigned short *textmemptr; +uint16_t *textmemptr; int attrib = 0x0F; int csr_x = 0; int csr_y = 0; @@ -46,9 +46,9 @@ void cls() move_csr(); } -void putch(unsigned char c) +void putch(uint8_t c) { - unsigned short *where; + uint16_t *where; unsigned att = attrib << 8; if (c == 0x08) @@ -86,20 +86,20 @@ void putch(unsigned char c) move_csr(); } -void puts(unsigned char *text) +void puts(uint8_t *text) { int i; for (i = 0; i < strlen(text); i++) putch(text[i]); } -void settextcolor(unsigned char forecolor, unsigned char backcolor) +void settextcolor(uint8_t forecolor, uint8_t backcolor) { attrib = (backcolor << 4) | (forecolor & 0x0F); } void init_video(void) { - textmemptr = (unsigned short *)0xB8000; + textmemptr = (uint16_t *)0xB8000; cls(); } \ No newline at end of file diff --git a/timer.c b/timer.c index f6ae066..626dc97 100644 --- a/timer.c +++ b/timer.c @@ -11,14 +11,14 @@ void timer_handler(register_t *r) } } -void timer_install(unsigned int frequency) +void timer_install(uint32_t frequency) { irq_install_handler(0, timer_handler); - unsigned int divisor = 1193180 / frequency; + uint32_t divisor = 1193180 / frequency; outportb(0x43, 0x36); - unsigned char l = (unsigned char)(divisor & 0xFF); - unsigned char h = (unsigned char)((divisor >> 8) & 0xFF); + uint8_t l = (uint8_t)(divisor & 0xFF); + uint8_t h = (uint8_t)((divisor >> 8) & 0xFF); outportb(0x40, l); outportb(0x40, h);