diff --git a/docs/STYLE.md b/docs/STYLE.md index 7055ccc..c463c98 100644 --- a/docs/STYLE.md +++ b/docs/STYLE.md @@ -26,6 +26,8 @@ if (something) { } ``` +Having no braces for a single statement structure is fine. + Functions should have their opening brace on a separate line, and the same goes for the closing brace: ```c @@ -59,7 +61,7 @@ Global variables need to have descriptive names. Local variables can be kept sho ## Typedefs -Structures should not be `typedef`'d. +Structures should not be `typedef`'d. However using `typedef` for an enumeration is fine. ## Functions diff --git a/src/boot/boot.c b/src/boot/boot.c index f10c4e1..20abcd1 100644 --- a/src/boot/boot.c +++ b/src/boot/boot.c @@ -6,28 +6,24 @@ #include -// Framebuffer request __attribute__((used, section(".limine_requests"))) volatile struct limine_framebuffer_request framebuffer_request = { .id = LIMINE_FRAMEBUFFER_REQUEST, .revision = 0 }; -// Memory map request __attribute__((used, section(".limine_requests"))) volatile struct limine_memmap_request memmap_request = { .id = LIMINE_MEMMAP_REQUEST, .revision = 0 }; -// Higher Half Direct Map __attribute__((used, section(".limine_requests"))) volatile struct limine_hhdm_request hhdm_request = { .id = LIMINE_HHDM_REQUEST, .revision = 0 }; -// Executable Address/Kernel Address (find base phys/virt address of kernel) __attribute__((used, section(".limine_requests"))) volatile struct limine_kernel_address_request kerneladdr_request = { .id = LIMINE_KERNEL_ADDRESS_REQUEST, diff --git a/src/config.h b/src/config.h index d243f93..61cd7a7 100644 --- a/src/config.h +++ b/src/config.h @@ -28,6 +28,10 @@ // 2 MB should be enough (as of now, the whole kernel ELF is around 75kb) #define KERNEL_SIZE 0x200000 #define KERNEL_STACK_SIZE 65536 +#define KERNEL_IDT_ENTRIES 33 + +/* paging */ +#define PAGING_MAX_PHYS 0x100000000 /* heap */ #define KHEAP_SIZE (32*1024*1024) diff --git a/src/debug/misc.c b/src/debug/misc.c index eb7392e..19c076f 100644 --- a/src/debug/misc.c +++ b/src/debug/misc.c @@ -1,3 +1,9 @@ +/* + * @author xamidev + * @brief Miscellaneous debug features + * @license GPL-3.0-only + */ + #include #include "limine.h" #include "string/string.h" @@ -9,12 +15,10 @@ void memmap_display(struct limine_memmap_response* response) { DEBUG("Got memory map from Limine: revision %u, %u entries", response->revision, response->entry_count); - for (size_t i=0; ientry_count; i++) - { + for (size_t i=0; ientry_count; i++) { struct limine_memmap_entry* entry = response->entries[i]; char type[32] = {0}; - switch(entry->type) - { + switch(entry->type) { case LIMINE_MEMMAP_USABLE: strcpy(type, "USABLE"); break; diff --git a/src/debug/panic.c b/src/debug/panic.c index f770ac7..e1cf694 100644 --- a/src/debug/panic.c +++ b/src/debug/panic.c @@ -1,3 +1,9 @@ +/* + * @author xamidev + * @brief Kernel panic + * @license GPL-3.0-only + */ + #include #include "idt/idt.h" #include "io/serial/serial.h" @@ -8,8 +14,7 @@ extern struct init_status init; void panic(struct cpu_status_t* ctx, const char* str) { CLEAR_INTERRUPTS; - if (ctx == NULL) - { + if (ctx == NULL) { DEBUG("\x1b[38;5;231m\x1b[48;5;196mKernel panic!!!\x1b[0m Something went horribly wrong! (no cpu ctx)"); fctprintf((void*)&skputc, 0, "\x1b[38;5;231m\x1b[48;5;27m"); DIE_DEBUG(str); @@ -18,8 +23,7 @@ void panic(struct cpu_status_t* ctx, const char* str) skputc('\n'); DEBUG("\x1b[38;5;231m\x1b[48;5;196mend Kernel panic - halting...\x1b[0m"); - if (init.terminal) - { + if (init.terminal) { printf("\r\n\x1b[38;5;231m\x1b[48;5;196mKernel panic!!!\x1b[48;5;232m Something went horribly wrong! (no cpu ctx)"); printf("\r\n%s\r\n\x1b[38;5;231mend Kernel panic - halting...\x1b[0m", str); } @@ -32,8 +36,7 @@ void panic(struct cpu_status_t* ctx, const char* str) ctx->vector_number, ctx->error_code, ctx->rax, ctx->rbx, ctx->rcx, ctx->rdx, ctx->rsi, ctx->rdi, ctx->r8, ctx->r9, ctx->r10, ctx->r11, ctx->r12, ctx->r13, ctx->r14, ctx->r15, ctx->iret_flags); - if (init.terminal) - { + if (init.terminal) { printf("\r\n\x1b[38;5;231m\x1b[48;5;196mKernel panic!!!\x1b[48;5;232mat rip=%p\r\nSomething went horribly wrong! (%s) vect=0x%.2x errcode=0x%x\n\rrax=%p rbx=%p rcx=%p rdx=%p\n\rrsi=%p rdi=%p r8=%p r9=%p\n\rr10=%p r11=%p r12=%p r13=%p\n\rr14=%p r15=%p\n\n\rflags=%p\n\rHalting...\x1b[0m", ctx->iret_rip, str, diff --git a/src/debug/stacktrace.c b/src/debug/stacktrace.c index d71ca3e..f1bca8e 100644 --- a/src/debug/stacktrace.c +++ b/src/debug/stacktrace.c @@ -1,3 +1,9 @@ +/* + * @author xamidev + * @brief Stack trace tools + * @license GPL-3.0-only + */ + #include #include "kernel.h" @@ -6,45 +12,39 @@ extern struct init_status init; void debug_stack_trace(unsigned int max_frames) { DEBUG("*** begin stack trace ***"); - if (init.terminal) - { + if (init.terminal) { printf("\r\n*** begin stack trace ***\r\n"); } // Thanks GCC :) uintptr_t* rbp = (uintptr_t*)__builtin_frame_address(0); - for (unsigned int frame=0; frame (%s+0x%x)", frame, (void*)rip, name, offset); - if (init.terminal) - { + if (init.terminal) { printf("[%u] <0x%p> (%s+0x%x)\r\n", frame, (void*)rip, name, offset); } uintptr_t* next_rbp = (uintptr_t*)rbp[0]; - // invalid rbp or we're at the end - if (next_rbp <= rbp || next_rbp == NULL) - { + // Invalid rbp or we're at the end + if (next_rbp <= rbp || next_rbp == NULL) { break; } rbp = next_rbp; } - if (init.terminal) - { + if (init.terminal) { printf("*** end stack trace ***"); } DEBUG("*** end stack trace ***"); } -typedef struct -{ +typedef struct { uint64_t addr; const char *name; } __attribute__((packed)) kernel_symbol_t; @@ -55,8 +55,7 @@ __attribute__((weak)) extern uint64_t symbol_count; // binary search const char* debug_find_symbol(uintptr_t rip, uintptr_t* offset) { - if (!symbol_table || symbol_count == 0) - { + if (!symbol_table || symbol_count == 0) { if (offset) *offset = 0; return "???"; } @@ -64,11 +63,9 @@ const char* debug_find_symbol(uintptr_t rip, uintptr_t* offset) int low = 0, high = (int)symbol_count - 1; int best = -1; - while (low <= high) - { + while (low <= high) { int mid = (low + high) / 2; - if (symbol_table[mid].addr <= rip) - { + if (symbol_table[mid].addr <= rip) { best = mid; low = mid + 1; } else { @@ -76,15 +73,15 @@ const char* debug_find_symbol(uintptr_t rip, uintptr_t* offset) } } - if (best != -1) - { - if (offset) - { + if (best != -1) { + if (offset) { *offset = rip - symbol_table[best].addr; } return symbol_table[best].name; } - if (offset) *offset = 0; + if (offset) { + *offset = 0; + } return "unknown"; } \ No newline at end of file diff --git a/src/idt/idt.c b/src/idt/idt.c index 9203c54..ee1fa5c 100644 --- a/src/idt/idt.c +++ b/src/idt/idt.c @@ -53,8 +53,7 @@ void idt_load(void* idt_addr) void idt_init() { // Hardcoded... - for (size_t i=0; i<=33; i++) - { + for (size_t i=0; i<=KERNEL_IDT_ENTRIES; i++) { // Each vector handler is 16-byte aligned, so *16 = address of that handler idt_set_entry(i, vector_0_handler + (i*16), 0); } @@ -98,8 +97,7 @@ static void gp_fault_handler(struct cpu_status_t* ctx) (ctx->error_code == 0) ? "NOT_SEGMENT_RELATED" : "SEGMENT_RELATED"); // Segment-related - if (ctx->error_code != 0) - { + if (ctx->error_code != 0) { bool is_external = CHECK_BIT(ctx->error_code, 0); // is it IDT, GDT, LDT? uint8_t table = ctx->error_code & 0x6; // 0b110 (isolate table) @@ -118,13 +116,11 @@ static void gp_fault_handler(struct cpu_status_t* ctx) struct cpu_status_t* interrupt_dispatch(struct cpu_status_t* context) { - if (context == NULL) - { + if (context == NULL) { panic(NULL, "Interrupt dispatch recieved NULL context!"); } - switch(context->vector_number) - { + switch(context->vector_number) { case 0: panic(context, "Divide Error"); break; @@ -197,15 +193,13 @@ struct cpu_status_t* interrupt_dispatch(struct cpu_status_t* context) // Send an EOI so that we can continue having interrupts outb(0x20, 0x20); - if (ticks % SCHEDULER_QUANTUM == 0) - { + if (ticks % SCHEDULER_QUANTUM == 0) { return scheduler_schedule(context); } break; - case 33: - DEBUG("Keyboard Interrupt"); + case 33: // Keyboard Interrupt keyboard_handler(); outb(0x20, 0x20); break; diff --git a/src/idt/idt.h b/src/idt/idt.h index 898e49c..fab7853 100644 --- a/src/idt/idt.h +++ b/src/idt/idt.h @@ -11,8 +11,7 @@ void idt_init(); -struct interrupt_descriptor -{ +struct interrupt_descriptor { uint16_t address_low; uint16_t selector; uint8_t ist; @@ -22,8 +21,7 @@ struct interrupt_descriptor uint32_t reserved; } __attribute__((packed)); -struct idtr -{ +struct idtr { uint16_t limit; uint64_t base; } __attribute__((packed)); @@ -31,8 +29,7 @@ struct idtr // All general-purpose registers (except rsp) as stored on the stack, // plus the values we pushed (vector number, error code) and the iret frame // In reverse order because the stack grows downwards. -struct cpu_status_t -{ +struct cpu_status_t { uint64_t r15; uint64_t r14; uint64_t r13; diff --git a/src/io/kbd/ps2.c b/src/io/kbd/ps2.c index fdd1946..b28b31b 100644 --- a/src/io/kbd/ps2.c +++ b/src/io/kbd/ps2.c @@ -161,11 +161,9 @@ void keyboard_handler() unsigned char scancode = inb(0x60); // Key release (bit 7 set) - if (scancode & 0x80) - { + if (scancode & 0x80) { unsigned char code = scancode & 0x7F; - switch (code) - { + switch (code) { // Clear the corresponding bit if corresponding key is released case LEFT_SHIFT_PRESSED: case RIGHT_SHIFT_PRESSED: @@ -179,12 +177,9 @@ void keyboard_handler() break; } return; - } - else - { + } else { // Key press - switch (scancode) - { + switch (scancode) { // Set bits for corresponding special key press case LEFT_SHIFT_PRESSED: case RIGHT_SHIFT_PRESSED: @@ -200,15 +195,12 @@ void keyboard_handler() default: { // Avoiding buffer overflow from extended keys lol - if (scancode < 128) - { + if (scancode < 128) { // Should we get a SHIFTED char or a regular one? unsigned char c = (key_status & SHIFT_PRESSED_BIT) ? keymap_shifted[scancode] : keymap[scancode]; - if (c) - { - if (c == '\n') - { + if (c) { + if (c == '\n') { _putchar('\r'); } // Should probably have a keyboard buffer here... instead of this @@ -225,8 +217,7 @@ void keyboard_init(unsigned char layout) // Here we might go and select PS/2, USB, or other... (once we implement multiple keyboard protocols) // Keyboard layout selection - switch (layout) - { + switch (layout) { case US: keymap = kbdus; keymap_shifted = kbdus_shifted; @@ -242,8 +233,7 @@ void keyboard_init(unsigned char layout) } // Flush keyboard buffer - while (inb(0x64) & 1) - { + while (inb(0x64) & 1) { inb(0x60); } diff --git a/src/io/kbd/ps2.h b/src/io/kbd/ps2.h index 031fd91..a1d4860 100644 --- a/src/io/kbd/ps2.h +++ b/src/io/kbd/ps2.h @@ -13,15 +13,13 @@ void keyboard_handler(); #define ALT_PRESSED_BIT 0b00000010 #define CTRL_PRESSED_BIT 0b00000100 -enum SpecialKeys -{ +enum SpecialKeys { SHIFT = 255, ALT = 254, CTRL = 253 }; -enum SpecialScancodes -{ +enum SpecialScancodes { LEFT_SHIFT_PRESSED = 0x2A, LEFT_SHIFT_RELEASED = 0xAA, RIGHT_SHIFT_PRESSED = 0x36, @@ -32,8 +30,7 @@ enum SpecialScancodes ALT_RELEASED = 0xB8 }; -enum KeyboardLayout -{ +enum KeyboardLayout { US, FR }; diff --git a/src/io/serial/serial.c b/src/io/serial/serial.c index 8c8c9ac..fbf06c8 100644 --- a/src/io/serial/serial.c +++ b/src/io/serial/serial.c @@ -21,9 +21,6 @@ unsigned char inb(int port) return data; } -// COM1 -#define PORT 0x3F8 - int serial_init() { outb(PORT + 1, 0x00); // Disable all interrupts @@ -36,8 +33,7 @@ int serial_init() outb(PORT + 4, 0x1E); // Set in loopback mode, test the serial chip outb(PORT + 0, 0xAE); // Test serial chip (send byte 0xAE and check if serial returns same byte) - if (inb(PORT) != 0xAE) - { + if (inb(PORT) != 0xAE) { return -EIO; } @@ -66,8 +62,7 @@ void skputc(char c) void skputs(const char* str) { unsigned int i=0; - while (str[i]) - { + while (str[i]) { skputc(str[i]); i++; } diff --git a/src/io/serial/serial.h b/src/io/serial/serial.h index dc29d14..b60c488 100644 --- a/src/io/serial/serial.h +++ b/src/io/serial/serial.h @@ -7,6 +7,9 @@ #ifndef SERIAL_H #define SERIAL_H +// COM1 +#define PORT 0x3F8 + void outb(int port, unsigned char data); unsigned char inb(int port); diff --git a/src/io/term/term.c b/src/io/term/term.c index 89e5df9..cf90125 100644 --- a/src/io/term/term.c +++ b/src/io/term/term.c @@ -34,8 +34,7 @@ void _putchar(char character) void kputs(const char* str) { size_t i=0; - while (str[i] != 0) - { + while (str[i] != 0) { _putchar(str[i]); i++; } diff --git a/src/kernel.h b/src/kernel.h index f7dd570..0b276cc 100644 --- a/src/kernel.h +++ b/src/kernel.h @@ -7,8 +7,7 @@ #ifndef KERNEL_H #define KERNEL_H -enum ErrorCodes -{ +enum ErrorCodes { ENOMEM, EIO }; @@ -47,8 +46,7 @@ void boot_mem_display(); #define assert(check) do { if(!(check)) hcf(); } while(0) -struct boot_context -{ +struct boot_context { struct limine_framebuffer* fb; struct limine_memmap_response* mmap; struct limine_hhdm_response* hhdm; @@ -56,8 +54,7 @@ struct boot_context }; // Are these modules initialized yet? -struct init_status -{ +struct init_status { bool terminal; bool serial; bool keyboard; diff --git a/src/kmain.c b/src/kmain.c index 920639d..4ccd31b 100644 --- a/src/kmain.c +++ b/src/kmain.c @@ -65,8 +65,7 @@ void pedicel_main(void* arg) void idle_main(void* arg) { - for (;;) - { + for (;;) { asm("hlt"); } } @@ -89,10 +88,10 @@ void kmain() boot_ctx.kaddr = kerneladdr_request.response ? kerneladdr_request.response : NULL; boot_mem_display(); - pmm_init(boot_ctx.mmap, boot_ctx.hhdm); + pmm_init(boot_ctx); // Remap kernel , HHDM and framebuffer - paging_init(boot_ctx.kaddr, boot_ctx.fb); + paging_init(boot_ctx); kheap_init(); keyboard_init(FR); diff --git a/src/mem/gdt/gdt.h b/src/mem/gdt/gdt.h index a122387..33f744e 100644 --- a/src/mem/gdt/gdt.h +++ b/src/mem/gdt/gdt.h @@ -21,8 +21,7 @@ #define USER_CODE_SEGMENT 0x18 #define USER_DATA_SEGMENT 0x20 -struct GDTR -{ +struct GDTR { uint16_t limit; uint64_t address; } __attribute__((packed)); diff --git a/src/mem/heap/kheap.c b/src/mem/heap/kheap.c index b97f344..5393614 100644 --- a/src/mem/heap/kheap.c +++ b/src/mem/heap/kheap.c @@ -33,11 +33,9 @@ void kheap_init() uintptr_t current_addr = kheap_start; // Map/alloc enough pages for heap (KHEAP_SIZE) - for (size_t i=0; ifree && curr->size >= size) - { + if (curr->free && curr->size >= size) { // We split the block if it is big enough - if (curr->size >= size + sizeof(struct heap_block_t) + 16) - { - //struct heap_block_t* new_block = (struct heap_block_t*)((uintptr_t)curr + sizeof(struct heap_block_t) + size); + if (curr->size >= size + sizeof(struct heap_block_t) + 16) { struct heap_block_t* split = (struct heap_block_t*)((uintptr_t)curr + sizeof(struct heap_block_t) + size); split->size = curr->size - size - sizeof(struct heap_block_t); @@ -109,10 +103,8 @@ void kfree(void* ptr) // merge adjacent free blocks (coalescing) struct heap_block_t* curr = head; - while (curr && curr->next) - { - if (curr->free && curr->next->free) - { + while (curr && curr->next) { + if (curr->free && curr->next->free) { curr->size += sizeof(*curr) + curr->next->size; curr->next = curr->next->next; continue; diff --git a/src/mem/heap/kheap.h b/src/mem/heap/kheap.h index e59b204..52b9315 100644 --- a/src/mem/heap/kheap.h +++ b/src/mem/heap/kheap.h @@ -16,8 +16,7 @@ #include #include -struct heap_block_t -{ +struct heap_block_t { size_t size; bool free; // 1byte uint8_t reserved[7]; // (7+1 = 8 bytes) diff --git a/src/mem/misc/utils.c b/src/mem/misc/utils.c index bdd5f07..d43ec16 100644 --- a/src/mem/misc/utils.c +++ b/src/mem/misc/utils.c @@ -21,8 +21,7 @@ void* memcpy(void* restrict dest, const void* restrict src, size_t n) uint8_t* restrict pdest = (uint8_t* restrict)dest; const uint8_t* restrict psrc = (const uint8_t* restrict)src; - for (size_t i=0; i dest) - { - for (size_t i=0; i dest) { + for (size_t i=0; i0; i--) - { + } else if (src < dest) { + for (size_t i=n; i>0; i--) { pdest[i-1] = psrc[i-1]; } } @@ -67,10 +61,8 @@ int memcmp(const void* s1, const void* s2, size_t n) const uint8_t* p1 = (const uint8_t*)s1; const uint8_t* p2 = (const uint8_t*)s2; - for (size_t i=0; ientry_count; i++) - { + for (uint64_t i=0; ientry_count; i++) { struct limine_memmap_entry* entry = boot_ctx.mmap->entries[i]; - if (entry->length == 0) - { + if (entry->length == 0) { continue; } uint64_t top = entry->base + entry->length; - if (top > max_phys) - { + if (top > max_phys) { max_phys = top; } } // 4GB - if (max_phys > 0x100000000) - { - DEBUG("WARNING: max_phys capped to 4GB (0x100000000) (from max_phys=%p)", max_phys); - max_phys = 0x100000000; + if (max_phys > PAGING_MAX_PHYS) { + DEBUG("WARNING: max_phys capped to 4GB (%x) (from max_phys=%p)", PAGING_MAX_PHYS, max_phys); + max_phys = PAGING_MAX_PHYS; } - // HHDM map up to max_phys or 4GB, whichever is smaller, using given offset - for (uint64_t i=0; i #include #include "mem/heap/kheap.h" +#include -void paging_init(); +void paging_init(struct boot_context boot_ctx); void paging_map_page(uint64_t* root_table, uint64_t virt, uint64_t phys, uint64_t flags); // To swap root page tables diff --git a/src/mem/paging/pmm.c b/src/mem/paging/pmm.c index 472ccfa..db3a5e9 100644 --- a/src/mem/paging/pmm.c +++ b/src/mem/paging/pmm.c @@ -38,12 +38,10 @@ static void pmm_find_biggest_usable_region(struct limine_memmap_response* memmap uint64_t offset = hhdm->offset; DEBUG("Usable Memory:"); - for (size_t i=0; ientry_count; i++) - { + for (size_t i=0; ientry_count; i++) { struct limine_memmap_entry* entry = memmap->entries[i]; - if (entry->type == LIMINE_MEMMAP_USABLE) - { + if (entry->type == LIMINE_MEMMAP_USABLE) { DEBUG("0x%p-0x%p mapped at 0x%p-0x%p", entry->base, entry->base+entry->length, entry->base+offset, entry->base+entry->length+offset); if (entry->length > length_max) @@ -66,8 +64,7 @@ static uintptr_t g_freelist = 0; uintptr_t pmm_alloc() { - if (!g_freelist) - { + if (!g_freelist) { panic(NULL, "PMM is out of memory!"); } uintptr_t addr = g_freelist; @@ -89,19 +86,17 @@ static void pmm_init_freelist() uint64_t end = ALIGN_DOWN(biggest_entry->base + biggest_entry->length, PAGE_SIZE); uint64_t page_count=0; - for (uint64_t addr = base; addr < end; addr += PAGE_SIZE) - { + for (uint64_t addr = base; addr < end; addr += PAGE_SIZE) { pmm_free(addr); - //DEBUG("page %u lives at phys 0x%p (virt 0x%p)", page_count, addr, PHYS_TO_VIRT(addr)); page_count++; } DEBUG("%u frames in freelist, available for use (%u bytes)", page_count, page_count*PAGE_SIZE); } -void pmm_init(struct limine_memmap_response* memmap, struct limine_hhdm_response* hhdm) +void pmm_init(struct boot_context boot_ctx) { - hhdm_off = hhdm->offset; - pmm_find_biggest_usable_region(memmap, hhdm); + hhdm_off = boot_ctx.hhdm->offset; + pmm_find_biggest_usable_region(boot_ctx.mmap, boot_ctx.hhdm); // Now we have biggest USABLE region, // so to populate the free list we just iterate through it diff --git a/src/mem/paging/pmm.h b/src/mem/paging/pmm.h index a166727..90e2f54 100644 --- a/src/mem/paging/pmm.h +++ b/src/mem/paging/pmm.h @@ -8,8 +8,9 @@ #define PAGING_PMM_H #include +#include -void pmm_init(struct limine_memmap_response* memmap, struct limine_hhdm_response* hhdm); +void pmm_init(struct boot_context boot_ctx); void pmm_free(uintptr_t addr); uintptr_t pmm_alloc(); diff --git a/src/mem/paging/vmm.h b/src/mem/paging/vmm.h index b70e1a8..368a1f9 100644 --- a/src/mem/paging/vmm.h +++ b/src/mem/paging/vmm.h @@ -16,8 +16,7 @@ Flags here aren't x86 flags, they are platform-agnostic kernel-defined flags. */ -struct vm_object -{ +struct vm_object { uintptr_t base; size_t length; size_t flags; diff --git a/src/sched/process.c b/src/sched/process.c index 484f7dc..c4e879d 100644 --- a/src/sched/process.c +++ b/src/sched/process.c @@ -34,8 +34,7 @@ void process_display_list(struct process_t* processes_list) { int process_view_id = 0; struct process_t* tmp = processes_list; - while (tmp != NULL) - { + while (tmp != NULL) { DEBUG("{%d: %p} -> ", process_view_id, tmp); tmp = tmp->next; process_view_id++; @@ -47,7 +46,6 @@ struct process_t* process_create(char* name, void(*function)(void*), void* arg) { CLEAR_INTERRUPTS; struct process_t* proc = (struct process_t*)kmalloc(sizeof(struct process_t)); - struct cpu_status_t* ctx = (struct cpu_status_t*)kmalloc(sizeof(struct cpu_status_t)); // No more memory? @@ -88,29 +86,25 @@ void process_add(struct process_t** processes_list, struct process_t* process) if (!process) return; process->next = NULL; - if (*processes_list == NULL) - { + if (*processes_list == NULL) { // List is empty *processes_list = process; return; } struct process_t* tmp = *processes_list; - while (tmp->next != NULL) - { + while (tmp->next != NULL) { tmp = tmp->next; } // We're at last process before NULL tmp->next = process; - // process->next = NULL; } void process_delete(struct process_t** processes_list, struct process_t* process) { if (!processes_list || !*processes_list || !process) return; - if (*processes_list == process) - { + if (*processes_list == process) { // process to delete is at head *processes_list = process->next; process->next = NULL; @@ -119,13 +113,11 @@ void process_delete(struct process_t** processes_list, struct process_t* process } struct process_t* tmp = *processes_list; - while (tmp->next && tmp->next != process) - { + while (tmp->next && tmp->next != process) { tmp = tmp->next; } - if (tmp->next == NULL) - { + if (tmp->next == NULL) { // Didn't find the process return; } @@ -148,15 +140,12 @@ void process_exit() { DEBUG("Exiting from process '%s'", current_process->name); CLEAR_INTERRUPTS; - if (current_process) - { + if (current_process) { current_process->status = DEAD; } SET_INTERRUPTS; - //outb(0x20, 0x20); - for (;;) - { + for (;;) { asm("hlt"); } } \ No newline at end of file diff --git a/src/sched/process.h b/src/sched/process.h index 5209e65..cb080b3 100644 --- a/src/sched/process.h +++ b/src/sched/process.h @@ -11,15 +11,13 @@ #include "config.h" #include -typedef enum -{ +typedef enum { READY, RUNNING, DEAD } status_t; -struct process_t -{ +struct process_t { size_t pid; char name[PROCESS_NAME_MAX]; diff --git a/src/sched/scheduler.c b/src/sched/scheduler.c index 528e769..1c9dce0 100644 --- a/src/sched/scheduler.c +++ b/src/sched/scheduler.c @@ -22,13 +22,11 @@ void scheduler_init() struct cpu_status_t* scheduler_schedule(struct cpu_status_t* context) { - if (context == NULL) - { + if (context == NULL) { panic(NULL, "Scheduler called with NULL context"); } - if (current_process == NULL) - { + if (current_process == NULL) { // If no more processes, then set IDLE as the current process, that's it. current_process = idle_proc; } @@ -38,21 +36,17 @@ struct cpu_status_t* scheduler_schedule(struct cpu_status_t* context) for (;;) { struct process_t* prev_process = current_process; - if (current_process->next != NULL) - { + if (current_process->next != NULL) { current_process = current_process->next; - } else - { + } else { current_process = processes_list; } - if (current_process != NULL && current_process->status == DEAD) - { + if (current_process != NULL && current_process->status == DEAD) { process_delete(&prev_process, current_process); current_process = NULL; return idle_proc->context; - } else - { + } else { current_process->status = RUNNING; break; } diff --git a/src/string/string.c b/src/string/string.c index f95f000..b21a650 100644 --- a/src/string/string.c +++ b/src/string/string.c @@ -14,12 +14,14 @@ char* strcpy(char *dest, const char *src) } // https://stackoverflow.com/questions/2488563/strcat-implementation -char *strcat(char *dest, const char *src){ +char *strcat(char *dest, const char *src) +{ size_t i,j; - for (i = 0; dest[i] != '\0'; i++) - ; + for (i = 0; dest[i] != '\0'; i++); + for (j = 0; src[j] != '\0'; j++) dest[i+j] = src[j]; + dest[i+j] = '\0'; return dest; } diff --git a/src/time/timer.c b/src/time/timer.c index ab1cc24..5c591ef 100644 --- a/src/time/timer.c +++ b/src/time/timer.c @@ -78,8 +78,7 @@ void pit_init() void timer_wait(uint64_t wait_ticks) { uint64_t then = ticks + wait_ticks; - while (ticks < then) - { + while (ticks < then) { asm("hlt"); }; }