From 8e79ceaa7d3995df5a5dcf0ffdbd52ebe4c52163 Mon Sep 17 00:00:00 2001 From: Aleksa Vučković Date: Sun, 9 Jan 2022 18:50:48 +0100 Subject: Indentation --- src/as/boot.s | 32 +-- src/as/crti.s | 12 +- src/as/crtn.s | 12 +- src/as/paging.s | 4 +- src/c/gdt.c | 10 +- src/c/heap.c | 265 ++++++++++--------- src/c/idt.c | 3 +- src/c/keyboard.c | 150 ++++++----- src/c/keymap.c | 680 ++++++++++++++++++++++++------------------------ src/c/paging.c | 18 +- src/c/stack_protector.c | 14 +- src/c/stdio.c | 28 +- src/c/string.c | 48 ++-- src/c/timer.c | 6 +- src/c/tty.c | 70 ++--- src/c/vga.c | 25 +- src/include/stdio.h | 9 +- src/include/stdlib.h | 8 +- src/include/string.h | 8 +- src/include/sys/types.h | 2 + src/include/unistd.h | 9 +- src/linker.ld | 70 ++--- 22 files changed, 740 insertions(+), 743 deletions(-) (limited to 'src') diff --git a/src/as/boot.s b/src/as/boot.s index 68e5947..907cd3c 100644 --- a/src/as/boot.s +++ b/src/as/boot.s @@ -3,7 +3,7 @@ .set FLAGS, ALIGN | MEMINFO .set MAGIC, 0x1BADB002 .set CHECKSUM, -(MAGIC + FLAGS) - + .section .multiboot .align 4 .long MAGIC @@ -51,20 +51,20 @@ stack_top: .section .text .type _start, @function _start: - call init_gdt_table - ljmp $CODE_SEGMENT, $code - + call init_gdt_table + ljmp $CODE_SEGMENT, $code + code: - movw $DATA_SEGMENT, %ax - movw %ax, %ds - movw %ax, %es - movw %ax, %fs - movw %ax, %gs - movw %ax, %ss - movl $stack_top, %esp - cli - call _init - call kernel_main - hlt - + movw $DATA_SEGMENT, %ax + movw %ax, %ds + movw %ax, %es + movw %ax, %fs + movw %ax, %gs + movw %ax, %ss + movl $stack_top, %esp + cli + call _init + call kernel_main + hlt + .size _start, . - _start diff --git a/src/as/crti.s b/src/as/crti.s index 30dd4ea..5894e2d 100644 --- a/src/as/crti.s +++ b/src/as/crti.s @@ -3,14 +3,14 @@ .global _init .type _init, @function _init: - push %ebp - movl %esp, %ebp - /* gcc will nicely put the contents of crtbegin.o's .init section here. */ + push %ebp + movl %esp, %ebp + /* gcc will nicely put the contents of crtbegin.o's .init section here. */ .section .fini .global _fini .type _fini, @function _fini: - push %ebp - movl %esp, %ebp - /* gcc will nicely put the contents of crtbegin.o's .fini section here. */ + push %ebp + movl %esp, %ebp + /* gcc will nicely put the contents of crtbegin.o's .fini section here. */ diff --git a/src/as/crtn.s b/src/as/crtn.s index 1da795d..0e1c314 100644 --- a/src/as/crtn.s +++ b/src/as/crtn.s @@ -1,10 +1,10 @@ /* x86 crtn.s */ .section .init - /* gcc will nicely put the contents of crtend.o's .init section here. */ - popl %ebp - ret + /* gcc will nicely put the contents of crtend.o's .init section here. */ + popl %ebp + ret .section .fini - /* gcc will nicely put the contents of crtend.o's .fini section here. */ - popl %ebp - ret + /* gcc will nicely put the contents of crtend.o's .fini section here. */ + popl %ebp + ret diff --git a/src/as/paging.s b/src/as/paging.s index 2dd52d3..e1cf42a 100644 --- a/src/as/paging.s +++ b/src/as/paging.s @@ -1,4 +1,4 @@ -.globl loadPageDirectory +.global loadPageDirectory loadPageDirectory: push %ebp mov %esp, %ebp @@ -9,7 +9,7 @@ loadPageDirectory: ret .text -.globl enablePaging +.global enablePaging enablePaging: push %ebp mov %esp, %ebp diff --git a/src/c/gdt.c b/src/c/gdt.c index a9a745c..e3d2b4c 100644 --- a/src/c/gdt.c +++ b/src/c/gdt.c @@ -36,11 +36,11 @@ void init_gdt_table() gdtp.size=sizeof(gdt)-1; gdtp.offset=(uint32_t)&gdt; - init_gdt_entry(0,0,0,0,0); // null segment - init_gdt_entry(1,0xffffffff,0,0b10011010,0b11001111); // code segment - init_gdt_entry(2,0xffffffff,0,0b10010010,0b11001111); // data segment - init_gdt_entry(3,0xffffffff,0,0b11111010,0b11001111); // user mode code segment - init_gdt_entry(4,0xffffffff,0,0b11110010,0b11001111); // user mode data segment + init_gdt_entry(0,0,0,0,0); // null segment + init_gdt_entry(1,0xffffffff,0,0b10011010,0b11001111); // code segment + init_gdt_entry(2,0xffffffff,0,0b10010010,0b11001111); // data segment + init_gdt_entry(3,0xffffffff,0,0b11111010,0b11001111); // user mode code segment + init_gdt_entry(4,0xffffffff,0,0b11110010,0b11001111); // user mode data segment load_gdt(&gdtp); } diff --git a/src/c/heap.c b/src/c/heap.c index 3e1ba24..99d0a37 100644 --- a/src/c/heap.c +++ b/src/c/heap.c @@ -1,151 +1,146 @@ #include typedef struct _KHEAPBLOCKBM { - struct _KHEAPBLOCKBM *next; - uint32_t size; - uint32_t used; - uint32_t bsize; - uint32_t lfb; + struct _KHEAPBLOCKBM *next; + uint32_t size; + uint32_t used; + uint32_t bsize; + uint32_t lfb; } KHEAPBLOCKBM; - + typedef struct _KHEAPBM { - KHEAPBLOCKBM *fblock; + KHEAPBLOCKBM *fblock; } KHEAPBM; - + void k_heapBMInit(KHEAPBM *heap) { - heap->fblock = 0; + heap->fblock = 0; } - + int k_heapBMAddBlock(KHEAPBM *heap, uintptr_t addr, uint32_t size, uint32_t bsize) { - KHEAPBLOCKBM *b; - uint32_t bcnt; - uint32_t x; - uint8_t *bm; - - b = (KHEAPBLOCKBM*)addr; - b->size = size - sizeof(KHEAPBLOCKBM); - b->bsize = bsize; - - b->next = heap->fblock; - heap->fblock = b; - - bcnt = b->size / b->bsize; - bm = (uint8_t*)&b[1]; - - /* clear bitmap */ - for (x = 0; x < bcnt; ++x) { - bm[x] = 0; - } - - /* reserve room for bitmap */ - bcnt = (bcnt / bsize) * bsize < bcnt ? bcnt / bsize + 1 : bcnt / bsize; - for (x = 0; x < bcnt; ++x) { - bm[x] = 5; - } - - b->lfb = bcnt - 1; - - b->used = bcnt; - - return 1; + KHEAPBLOCKBM *b; + uint32_t bcnt; + uint32_t x; + uint8_t *bm; + + b = (KHEAPBLOCKBM*)addr; + b->size = size - sizeof(KHEAPBLOCKBM); + b->bsize = bsize; + + b->next = heap->fblock; + heap->fblock = b; + + bcnt = b->size / b->bsize; + bm = (uint8_t*)&b[1]; + + /* clear bitmap */ + for (x = 0; x < bcnt; ++x) { + bm[x] = 0; + } + + /* reserve room for bitmap */ + bcnt = (bcnt / bsize) * bsize < bcnt ? bcnt / bsize + 1 : bcnt / bsize; + for (x = 0; x < bcnt; ++x) { + bm[x] = 5; + } + + b->lfb = bcnt - 1; + b->used = bcnt; + return 1; } - + static uint8_t k_heapBMGetNID(uint8_t a, uint8_t b) { - uint8_t c; - for (c = a + 1; c == b || c == 0; ++c); - return c; + uint8_t c; + for (c = a + 1; c == b || c == 0; ++c); + return c; } - + void *k_heapBMAlloc(KHEAPBM *heap, uint32_t size) { - KHEAPBLOCKBM *b; - uint8_t *bm; - uint32_t bcnt; - uint32_t x, y, z; - uint32_t bneed; - uint8_t nid; - - /* iterate blocks */ - for (b = heap->fblock; b; b = b->next) { - //printf("size:%d,used:%d,bsize:%d,lfb:%d\n",b->size,b->used,b->bsize,b->lfb); - /* check if block has enough room */ - if (b->size - (b->used * b->bsize) >= size) { - - bcnt = b->size / b->bsize; - bneed = (size / b->bsize) * b->bsize < size ? size / b->bsize + 1 : size / b->bsize; - bm = (uint8_t*)&b[1]; - //printf("bcnt:%d,bneed:%d,bm:%d\n",bcnt,bneed,bm); - - for (x = (b->lfb + 1 >= bcnt ? 0 : b->lfb + 1); x != b->lfb; ++x) { - /* just wrap around */ - if (x >= bcnt) { - x = 0; - } - - if (bm[x] == 0) { - /* count free blocks */ - for (y = 0; bm[x + y] == 0 && y < bneed && (x + y) < bcnt; ++y); - - /* we have enough, now allocate them */ - if (y == bneed) { - /* find ID that does not match left or right */ - nid = k_heapBMGetNID(bm[x - 1], bm[x + y]); - - /* allocate by setting id */ - for (z = 0; z < y; ++z) { - bm[x + z] = nid; - } - - /* optimization */ - b->lfb = (x + bneed) - 2; - - /* count used blocks NOT bytes */ - b->used += y; - - return (void*)(x * b->bsize + (uintptr_t)&b[1]); - } - - /* x will be incremented by one ONCE more in our FOR loop */ - x += (y - 1); - continue; - } - } - } - } - - return 0; + KHEAPBLOCKBM *b; + uint8_t *bm; + uint32_t bcnt; + uint32_t x, y, z; + uint32_t bneed; + uint8_t nid; + + /* iterate blocks */ + for (b = heap->fblock; b; b = b->next) { + /* check if block has enough room */ + if (b->size - (b->used * b->bsize) >= size) { + + bcnt = b->size / b->bsize; + bneed = (size / b->bsize) * b->bsize < size ? size / b->bsize + 1 : size / b->bsize; + bm = (uint8_t*)&b[1]; + + for (x = (b->lfb + 1 >= bcnt ? 0 : b->lfb + 1); x != b->lfb; ++x) { + /* just wrap around */ + if (x >= bcnt) { + x = 0; + } + + if (bm[x] == 0) { + /* count free blocks */ + for (y = 0; bm[x + y] == 0 && y < bneed && (x + y) < bcnt; ++y); + + /* we have enough, now allocate them */ + if (y == bneed) { + /* find ID that does not match left or right */ + nid = k_heapBMGetNID(bm[x - 1], bm[x + y]); + + /* allocate by setting id */ + for (z = 0; z < y; ++z) { + bm[x + z] = nid; + } + + /* optimization */ + b->lfb = (x + bneed) - 2; + + /* count used blocks NOT bytes */ + b->used += y; + + return (void*)(x * b->bsize + (uintptr_t)&b[1]); + } + + /* x will be incremented by one ONCE more in our FOR loop */ + x += (y - 1); + continue; + } + } + } + } + return 0; } - + void k_heapBMFree(KHEAPBM *heap, void *ptr) { - KHEAPBLOCKBM *b; - uintptr_t ptroff; - uint32_t bi, x; - uint8_t *bm; - uint8_t id; - uint32_t max; - - for (b = heap->fblock; b; b = b->next) { - if ((uintptr_t)ptr > (uintptr_t)b && (uintptr_t)ptr < (uintptr_t)b + sizeof(KHEAPBLOCKBM) + b->size) { - /* found block */ - ptroff = (uintptr_t)ptr - (uintptr_t)&b[1]; /* get offset to get block */ - /* block offset in BM */ - bi = ptroff / b->bsize; - /* .. */ - bm = (uint8_t*)&b[1]; - /* clear allocation */ - id = bm[bi]; - /* oddly.. GCC did not optimize this */ - max = b->size / b->bsize; - for (x = bi; bm[x] == id && x < max; ++x) { - bm[x] = 0; - } - /* update free block count */ - b->used -= x - bi; - return; - } - } - - /* this error needs to be raised or reported somehow */ - return; + KHEAPBLOCKBM *b; + uintptr_t ptroff; + uint32_t bi, x; + uint8_t *bm; + uint8_t id; + uint32_t max; + + for (b = heap->fblock; b; b = b->next) { + if ((uintptr_t)ptr > (uintptr_t)b && (uintptr_t)ptr < (uintptr_t)b + sizeof(KHEAPBLOCKBM) + b->size) { + /* found block */ + ptroff = (uintptr_t)ptr - (uintptr_t)&b[1]; /* get offset to get block */ + /* block offset in BM */ + bi = ptroff / b->bsize; + /* .. */ + bm = (uint8_t*)&b[1]; + /* clear allocation */ + id = bm[bi]; + /* oddly.. GCC did not optimize this */ + max = b->size / b->bsize; + for (x = bi; bm[x] == id && x < max; ++x) { + bm[x] = 0; + } + /* update free block count */ + b->used -= x - bi; + return; + } + } + + /* this error needs to be raised or reported somehow */ + return; } KHEAPBM kheap; @@ -154,15 +149,17 @@ void kheapinit() { k_heapBMInit(&kheap); } + int kheapaddblock(uintptr_t addr,uint32_t size,uint32_t bsize) { return k_heapBMAddBlock(&kheap,addr,size,bsize); } + void *kmalloc(uint32_t size) { return k_heapBMAlloc(&kheap,size); - } + void kfree(void *ptr) { k_heapBMFree(&kheap,ptr); diff --git a/src/c/idt.c b/src/c/idt.c index 005a05c..843c9da 100644 --- a/src/c/idt.c +++ b/src/c/idt.c @@ -42,6 +42,7 @@ void init_idt_entry(size_t num, uint32_t offset, uint16_t selector, uint8_t type idt[num].type_attr=type_attr; idt[num].offset2=(offset & 0xffff0000)>>16; } + void add_idt_entry(size_t num,uint32_t offset) { init_idt_entry(num,offset,KERNEL_CODE,INTERRUPT_GATE_32); @@ -59,7 +60,6 @@ void init_pic() ioport_out(PIC2_DATA_PORT, 0x01); ioport_out(PIC1_DATA_PORT, 0xff); ioport_out(PIC2_DATA_PORT, 0xff); - ioport_out(PIC1_DATA_PORT, 0xFC); } @@ -104,6 +104,5 @@ void init_idt_table() idtp.size=sizeof(struct idt_entry)*256-1; idtp.offset=(uint32_t)&idt; - load_idt(&idtp); } diff --git a/src/c/keyboard.c b/src/c/keyboard.c index d082ec5..2218f09 100644 --- a/src/c/keyboard.c +++ b/src/c/keyboard.c @@ -28,6 +28,10 @@ void us_en_shift(char keymap[]); char charcode[256]; char shift_charcode[256]; bool ispressed[128]; +#define lshift 0x2A +#define rshift 0x36 +#define lctrl 0x1D +#define rctrl 0x1D void init_keyboard() { @@ -56,15 +60,15 @@ void enter() printf("\n"); if(buffer_index>0) { - tty(buffer[buffer_current]); - buffer_size[buffer_current]=buffer_index; - if(buffer_current==buffer_all) buffer_current=(++buffer_all); - else - { - for(size_t i=0;i0) { - buffer_size[buffer_current]=buffer_index; - for(size_t i=0;i #include - + #if UINT32_MAX == UINTPTR_MAX #define STACK_CHK_GUARD 0xe2dee396 #else #define STACK_CHK_GUARD 0x595e9fbd94fda766 #endif - + uintptr_t __stack_chk_guard = STACK_CHK_GUARD; - -//__attribute__((noreturn)) void __stack_chk_fail(void) { -#if __STDC_HOSTED__ - printf("Stack smashing detected\n"); - abort(); -#elif __is_myos_kernel - printf("Stack smashing detected\n"); - panic("Stack smashing detected"); -#endif + printf("Stack smashing detected\n"); } diff --git a/src/c/stdio.c b/src/c/stdio.c index 36fd140..3a51e9a 100644 --- a/src/c/stdio.c +++ b/src/c/stdio.c @@ -17,20 +17,20 @@ void printf(char *str, ...) for(size_t i=0;str[i]!='\0';i++) { - if(str[i]=='%') - { - i++; - if(str[i]=='c') terminal_putchar((char)va_arg(list,int)); - else if(str[i]=='s') terminal_writestring(va_arg(list,char*)); - else if(str[i]=='d') terminal_writeint(va_arg(list,int)); - else if(str[i]=='f') terminal_writefloat(va_arg(list,double)); - else - { - terminal_writestring("wrong format using print function\n"); - return; - } - } - else terminal_putchar(str[i]); + if(str[i]=='%') + { + i++; + if(str[i]=='c') terminal_putchar((char)va_arg(list,int)); + else if(str[i]=='s') terminal_writestring(va_arg(list,char*)); + else if(str[i]=='d') terminal_writeint(va_arg(list,int)); + else if(str[i]=='f') terminal_writefloat(va_arg(list,double)); + else + { + terminal_writestring("wrong format using print function\n"); + return; + } + } + else terminal_putchar(str[i]); } va_end(list); diff --git a/src/c/string.c b/src/c/string.c index 6a3a96b..ce33528 100644 --- a/src/c/string.c +++ b/src/c/string.c @@ -36,9 +36,9 @@ void stringrev(char *str) for(size_t j=0;j'9') return num; - num*=10; - num+=str[i]-'0'; + if(str[i]<'0'||str[i]>'9') return num; + num*=10; + num+=str[i]-'0'; } return num; @@ -61,11 +61,11 @@ void itos(uint32_t num,char *str) if(num==0) stringcpy(str,"0"); else { - size_t i=0; - for(;num>0;num/=10,i++) str[i]='0'+num%10; - str[i]='\0'; + size_t i=0; + for(;num>0;num/=10,i++) str[i]='0'+num%10; + str[i]='\0'; - stringrev(str); + stringrev(str); } } @@ -79,16 +79,16 @@ double stof(const char *str) for(;str[i]!='\0';i++) { - if(str[i]=='.') - { - if(point) return num/pow; - point=1; - continue; - } - if(str[i]<'0'||str[i]>'9') return num/pow; - num*=10; - num+=str[i]-'0'; - if(point) pow*=10; + if(str[i]=='.') + { + if(point) return num/pow; + point=1; + continue; + } + if(str[i]<'0'||str[i]>'9') return num/pow; + num*=10; + num+=str[i]-'0'; + if(point) pow*=10; } return num/pow; @@ -105,9 +105,9 @@ void ftos(double num, char *str) for(size_t i=0;i #include + #define SEEK_SET 0 typedef struct { int unused; } FILE; -#ifdef __cplusplus -extern "C" { -#endif + extern FILE* stderr; #define stderr stderr int fclose(FILE*); @@ -22,7 +22,4 @@ int vfprintf(FILE*, const char*, va_list); void printf(char *str, ...); -#ifdef __cplusplus -} -#endif #endif diff --git a/src/include/stdlib.h b/src/include/stdlib.h index a0afe38..77ffa67 100644 --- a/src/include/stdlib.h +++ b/src/include/stdlib.h @@ -1,15 +1,11 @@ #ifndef _STDLIB_H #define _STDLIB_H -#ifdef __cplusplus -extern "C" { -#endif + void abort(void); int atexit(void (*)(void)); int atoi(const char*); void free(void*); char* getenv(const char*); void* malloc(size_t); -#ifdef __cplusplus -} -#endif + #endif diff --git a/src/include/string.h b/src/include/string.h index bef5854..78fa956 100644 --- a/src/include/string.h +++ b/src/include/string.h @@ -1,16 +1,12 @@ #ifndef _STRING_H #define _STRING_H + #include -#ifdef __cplusplus -extern "C" { -#endif + void* memcpy(void*, const void*, size_t); void* memset(void*, int, size_t); char* strcpy(char*, const char*); size_t strlen(const char*); -#ifdef __cplusplus -} -#endif #include diff --git a/src/include/sys/types.h b/src/include/sys/types.h index 4f55189..34c1b65 100644 --- a/src/include/sys/types.h +++ b/src/include/sys/types.h @@ -1,4 +1,6 @@ #ifndef _SYS_TYPES_H #define _SYS_TYPES_H + typedef int pid_t; + #endif diff --git a/src/include/unistd.h b/src/include/unistd.h index a6bfee0..abe7f3c 100644 --- a/src/include/unistd.h +++ b/src/include/unistd.h @@ -1,14 +1,11 @@ #ifndef _UNISTD_H #define _UNISTD_H + #include -#ifdef __cplusplus -extern "C" { -#endif + int execv(const char*, char* const[]); int execve(const char*, char* const[], char* const[]); int execvp(const char*, char* const[]); pid_t fork(void); -#ifdef __cplusplus -} -#endif + #endif diff --git a/src/linker.ld b/src/linker.ld index 38ddd94..d8e7f4b 100644 --- a/src/linker.ld +++ b/src/linker.ld @@ -1,43 +1,43 @@ /* The bootloader will look at this image and start execution at the symbol designated as the entry point. */ ENTRY(_start) - + /* Tell where the various sections of the object files will be put in the final kernel image. */ SECTIONS { - /* Begin putting sections at 1 MiB, a conventional place for kernels to be - loaded at by the bootloader. */ - . = 1M; - - /* First put the multiboot header, as it is required to be put very early - early in the image or the bootloader won't recognize the file format. - Next we'll put the .text section. */ - .text BLOCK(4K) : ALIGN(4K) - { - *(.multiboot) - *(.text) - } - - /* Read-only data. */ - .rodata BLOCK(4K) : ALIGN(4K) - { - *(.rodata) - } - - /* Read-write data (initialized) */ - .data BLOCK(4K) : ALIGN(4K) - { - *(.data) - } - - /* Read-write data (uninitialized) and stack */ - .bss BLOCK(4K) : ALIGN(4K) - { - *(COMMON) - *(.bss) - } - - /* The compiler may produce other sections, by default it will put them in - a segment with the same name. Simply add stuff here as needed. */ + /* Begin putting sections at 1 MiB, a conventional place for kernels to be + loaded at by the bootloader. */ + . = 1M; + + /* First put the multiboot header, as it is required to be put very early + early in the image or the bootloader won't recognize the file format. + Next we'll put the .text section. */ + .text BLOCK(4K) : ALIGN(4K) + { + *(.multiboot) + *(.text) + } + + /* Read-only data. */ + .rodata BLOCK(4K) : ALIGN(4K) + { + *(.rodata) + } + + /* Read-write data (initialized) */ + .data BLOCK(4K) : ALIGN(4K) + { + *(.data) + } + + /* Read-write data (uninitialized) and stack */ + .bss BLOCK(4K) : ALIGN(4K) + { + *(COMMON) + *(.bss) + } + + /* The compiler may produce other sections, by default it will put them in + a segment with the same name. Simply add stuff here as needed. */ } -- cgit v1.2.3