From 5ba212fe7123d454414f64d42ec7ff0822458c13 Mon Sep 17 00:00:00 2001 From: Aleksa Vuckovic Date: Sat, 25 Feb 2023 07:26:22 +0100 Subject: ANSI C: // -> /* */ --- kernel/src/apic/apic.c | 32 ++++++++++++++++---------------- kernel/src/apic/ioapic.c | 16 ++++++++-------- kernel/src/apic/madt.c | 26 +++++++++++++------------- kernel/src/boot/boot.S | 12 ++++++------ kernel/src/boot/header.S | 12 ++++++------ kernel/src/boot/multiboot2.c | 18 ++++++++---------- kernel/src/check/panic.c | 2 +- kernel/src/check/ubsan.c | 14 ++++++++------ kernel/src/cpu/gdt.c | 10 +++++----- kernel/src/cpu/idt.c | 4 ++-- kernel/src/cpu/pic.c | 12 ++++++------ kernel/src/devices/keyboard.c | 17 ++++++++++------- kernel/src/devices/serial.c | 15 ++++++++------- kernel/src/fs/ext2.c | 42 +++++++++++++++++++++--------------------- kernel/src/main.c | 4 ++-- kernel/src/mem/heap.c | 26 +++++++++++++------------- kernel/src/mem/paging.c | 12 ++++++------ kernel/src/misc/graphics.c | 2 -- kernel/src/scheduler/ap_init.S | 12 ++++++------ kernel/src/sys/userspace.c | 4 ++-- 20 files changed, 147 insertions(+), 145 deletions(-) (limited to 'kernel/src') diff --git a/kernel/src/apic/apic.c b/kernel/src/apic/apic.c index 2b650b0..f543c82 100644 --- a/kernel/src/apic/apic.c +++ b/kernel/src/apic/apic.c @@ -30,69 +30,69 @@ void init_ap_cpus() map_addr(lapic_addr, lapic_addr, FLAG_PRESENT); for (size_t i = 0; i < numcores; i++) { - // do not start BSP, that's already running this code + /* do not start BSP, that's already running this code */ if (cpu_apic_ids[i] == bspid) continue; - // send INIT IPI + /* send INIT IPI */ - // clear APIC errors + /* clear APIC errors */ *((__volatile__ uint32_t *)(lapic_addr + 0x280)) = 0; - // select AP + /* select AP */ *((__volatile__ uint32_t *)(lapic_addr + 0x310)) = (*((__volatile__ uint32_t *)(lapic_addr + 0x310)) & 0x00ffffff) | ((uint32_t)cpu_apic_ids[i] << 24); - // trigger INIT IPI + /* trigger INIT IPI */ *((__volatile__ uint32_t *)(lapic_addr + 0x300)) = (*((__volatile__ uint32_t *)(lapic_addr + 0x300)) & 0xfff00000) | 0x00C500; - // wait for delivery + /* wait for delivery */ do { __asm__ __volatile__("pause" : : : "memory"); } while (*((__volatile__ uint32_t *)(uint64_t)(lapic_addr + 0x300)) & (1 << 12)); - // select AP + /* select AP */ *((__volatile__ uint32_t *)(lapic_addr + 0x310)) = (*((__volatile__ uint32_t *)(lapic_addr + 0x310)) & 0x00ffffff) | ((uint32_t)cpu_apic_ids[i] << 24); - // deassert + /* deassert */ *((__volatile__ uint32_t *)(lapic_addr + 0x300)) = (*((__volatile__ uint32_t *)(lapic_addr + 0x300)) & 0xfff00000) | 0x008500; - // wait for delivery + /* wait for delivery */ do { __asm__ __volatile__("pause" : : : "memory"); } while (*((__volatile__ uint32_t *)(uint64_t)(lapic_addr + 0x300)) & (1 << 12)); - // wait 10 msec + /* wait 10 msec */ wait(10); - // send STARTUP IPI (twice) + /* send STARTUP IPI (twice) */ for (size_t j = 0; j < 2; j++) { - // clear APIC errors + /* clear APIC errors */ *((__volatile__ uint32_t *)(lapic_addr + 0x280)) = 0; - // select AP + /* select AP */ *((__volatile__ uint32_t *)(lapic_addr + 0x310)) = (*((__volatile__ uint32_t *)(lapic_addr + 0x310)) & 0x00ffffff) | ((uint32_t)cpu_apic_ids[i] << 24); - // trigger STARTUP IPI for 0800:0000 + /* trigger STARTUP IPI for 0800:0000 */ *((__volatile__ uint32_t *)(lapic_addr + 0x300)) = (*((__volatile__ uint32_t *)(lapic_addr + 0x300)) & 0xfff0f800) | 0x000608; - // wait 200 usec + /* wait 200 usec */ wait(1); - // wait for delivery + /* wait for delivery */ do { __asm__ __volatile__("pause" : : : "memory"); } while (*((__volatile__ uint32_t diff --git a/kernel/src/apic/ioapic.c b/kernel/src/apic/ioapic.c index cfda8be..7fe005b 100644 --- a/kernel/src/apic/ioapic.c +++ b/kernel/src/apic/ioapic.c @@ -32,23 +32,23 @@ void ioapic_set_irq(uint8_t irq, uint64_t apic_id, uint8_t vector) const uint32_t high_index = (uint32_t)0x10 + irq * 2 + 1; uint32_t high = ioapic_read((uint8_t)high_index); - // set APIC ID + /* set APIC ID */ high &= (uint32_t)~0xff000000; high |= (uint32_t)apic_id << 24; ioapic_write((uint8_t)high_index, high); uint32_t low = ioapic_read((uint8_t)low_index); - // unmask the IRQ + /* unmask the IRQ */ low &= (uint32_t) ~(1 << 16); - // set to physical delivery mode + /* set to physical delivery mode */ low &= (uint32_t) ~(1 << 11); - // set to fixed delivery mode + /* set to fixed delivery mode */ low &= (uint32_t)~0x700; - // set delivery vector + /* set delivery vector */ low &= (uint32_t)~0xff; low |= vector; @@ -62,9 +62,9 @@ void apic_remap_interrupts() uint8_t bspid = curr_cpu_apic_id(); - // irq is 2 because of gsi remap - ioapic_set_irq(0x2, bspid, 0x20); // timer - ioapic_set_irq(0x1, bspid, 0x21); // keyboard + /* irq is 2 because of gsi remap */ + ioapic_set_irq(0x2, bspid, 0x20); /* timer */ + ioapic_set_irq(0x1, bspid, 0x21); /* keyboard */ write_msr(APIC_BASE_MSR, read_msr(APIC_BASE_MSR) | (1 << 11)); *((__volatile__ uint32_t *)(lapic_addr + 0xF0)) = diff --git a/kernel/src/apic/madt.c b/kernel/src/apic/madt.c index 5a6e914..3a152aa 100644 --- a/kernel/src/apic/madt.c +++ b/kernel/src/apic/madt.c @@ -29,7 +29,7 @@ void parse_madt() kfree(m); if (type == 0) { - // Processor Local APIC + /* Processor Local APIC */ struct MADT_cpu_local_apic *cpu = (struct MADT_cpu_local_apic *)kalloc( sizeof(struct MADT_cpu_local_apic)); @@ -43,10 +43,10 @@ void parse_madt() numcores++; } - // printf("found cpu: acpi_id: 0x%x, apic_id: 0x%x, flags: 0x%x\n", cpu->acpi_id, cpu->apic_id, cpu->flags); + /* printf("found cpu: acpi_id: 0x%x, apic_id: 0x%x, flags: 0x%x\n", cpu->acpi_id, cpu->apic_id, cpu->flags); */ kfree(cpu); } else if (type == 1) { - // I/O APIC + /* I/O APIC */ struct MADT_io_apic *io = (struct MADT_io_apic *)kalloc( sizeof(struct MADT_io_apic)); memcpy(io, @@ -56,10 +56,10 @@ void parse_madt() ioapic_addr = io->io_apic_addr; - // printf("found io: apic_id: 0x%x, addr: 0x%x, int_base: 0x%x\n", io->apic_id, io->io_apic_addr, io->int_base); + /* printf("found io: apic_id: 0x%x, addr: 0x%x, int_base: 0x%x\n", io->apic_id, io->io_apic_addr, io->int_base); */ kfree(io); } else if (type == 2) { - // IO/APIC Interrupt Source Override + /* IO/APIC Interrupt Source Override */ struct MADT_io_apic_int *io_apic_int = (struct MADT_io_apic_int *)kalloc( sizeof(struct MADT_io_apic_int)); @@ -68,13 +68,13 @@ void parse_madt() (uint64_t)curr_size), sizeof(struct MADT_io_apic_int)); - // printf("found io_apic_int: bus: 0x%x, irq_source: 0x%x, global_sys_int: 0x%x, flags: 0x%x\n", io_apic_int->bus_source, io_apic_int->irq_source, io_apic_int->global_sys_int, io_apic_int->flags); + /* printf("found io_apic_int: bus: 0x%x, irq_source: 0x%x, global_sys_int: 0x%x, flags: 0x%x\n", io_apic_int->bus_source, io_apic_int->irq_source, io_apic_int->global_sys_int, io_apic_int->flags); */ kfree(io_apic_int); } else if (type == 3) { - // IO/APIC Non-maskable interrupt source + /* IO/APIC Non-maskable interrupt source */ printf("MADT entry of type %d\n", type); } else if (type == 4) { - // Local APIC Non-maskable interrupts + /* Local APIC Non-maskable interrupts */ struct MADT_lapic_nmi *lapic_nmi = (struct MADT_lapic_nmi *)kalloc( sizeof(struct MADT_lapic_nmi)); @@ -83,10 +83,10 @@ void parse_madt() (uint64_t)curr_size), sizeof(struct MADT_lapic_nmi)); - // printf("found lapic_nmi: acpi_cpu_id: 0x%x, flags: 0x%x, lint: 0x%x\n", lapic_nmi->acpi_cpu_id, lapic_nmi->flags, lapic_nmi->lint); + /* printf("found lapic_nmi: acpi_cpu_id: 0x%x, flags: 0x%x, lint: 0x%x\n", lapic_nmi->acpi_cpu_id, lapic_nmi->flags, lapic_nmi->lint); */ kfree(lapic_nmi); } else if (type == 5) { - // Local APIC Address Override + /* Local APIC Address Override */ struct MADT_lapic_addr *lapic_addr_ovr = (struct MADT_lapic_addr *)kalloc( sizeof(struct MADT_lapic_addr)); @@ -95,13 +95,13 @@ void parse_madt() (uint64_t)curr_size), sizeof(struct MADT_lapic_addr)); - // printf("found lapic: addr: 0x%x\n", lapic_addr_ovr->phys_addr); + /* printf("found lapic: addr: 0x%x\n", lapic_addr_ovr->phys_addr); */ kfree(lapic_addr_ovr); } else if (type == 9) { - // Processor Local x2APIC + /* Processor Local x2APIC */ printf("MADT entry of type %d\n", type); } else { - // ERROR + /* ERROR */ printf("ERROR: MADT entry of type %d\n", type); } curr_size += len; diff --git a/kernel/src/boot/boot.S b/kernel/src/boot/boot.S index f4cb290..1dc2320 100644 --- a/kernel/src/boot/boot.S +++ b/kernel/src/boot/boot.S @@ -21,7 +21,7 @@ _start: ljmp $0x08, $begin_long_mode setup_page_tables: -// first 2mb +/* first 2mb */ mov $pt_lvl3, %eax or $0x3, %eax mov %eax, pt_lvl4 @@ -42,7 +42,7 @@ setup_page_tables: cmp $25, %ecx jne 1b -// first 2mb in hh +/* first 2mb in hh */ mov $pt_lvl3_hh, %eax or $0x3, %eax mov %eax, pt_lvl4 + 4096 - 8 @@ -67,22 +67,22 @@ setup_page_tables: .global enable_paging enable_paging: - // enable PAE + /* enable PAE */ mov %cr4, %edx or $1<<5 ,%edx mov %edx, %cr4 - // set LME (long mode enable) + /* set LME (long mode enable) */ mov $0xC0000080, %ecx rdmsr or $1<<8, %eax wrmsr - // pt_lvl4 + /* pt_lvl4 */ mov $pt_lvl4, %eax mov %eax, %cr3 - // enable paging (+ protected mode if not already enabled) + /* enable paging (+ protected mode if not already enabled) */ mov %cr0, %eax or $1<<31 + 1<<0, %eax mov %eax, %cr0 diff --git a/kernel/src/boot/header.S b/kernel/src/boot/header.S index bbcb9be..a608dcc 100644 --- a/kernel/src/boot/header.S +++ b/kernel/src/boot/header.S @@ -1,12 +1,12 @@ -// multiboot tags +/* multiboot tags */ .set TAG_END, 0 .set TAG_FRAMEBUFFER, 5 -// multiboot flags +/* multiboot flags */ .set TAG_REQUIRED, 0 .set TAG_OPTIONAL, 1 -# multiboot2 header constants +/* multiboot2 header constants */ .set MAGIC, 0xe85250d6 .set ARCH, 0 .set HEADER_LENGTH, (header_end - header_start) @@ -15,14 +15,14 @@ .section .multiboot_header, "a" .align 4 header_start: - # magic + /* magic */ .align 8 .long MAGIC .long ARCH .long HEADER_LENGTH .long CHECKSUM - # framebuffer + /* framebuffer */ .align 8 .word TAG_FRAMEBUFFER .word TAG_REQUIRED @@ -31,7 +31,7 @@ header_start: .long 768 .long 32 - # end tag + /* end tag */ .align 8 .word TAG_END .word TAG_REQUIRED diff --git a/kernel/src/boot/multiboot2.c b/kernel/src/boot/multiboot2.c index 8a7d0d6..1958ac1 100644 --- a/kernel/src/boot/multiboot2.c +++ b/kernel/src/boot/multiboot2.c @@ -8,8 +8,6 @@ #include #include -/* https://www.gnu.org/software/grub/manual/multiboot2/html_node/Boot-information-format.html */ - mb2_tag_module *ext2_module; mmap_t mmap; @@ -24,7 +22,7 @@ void init_fb(mb2_tag_fb *tag_fb) main_fb.char_col = WHITE; main_fb.bg_col = BLACK; - // identity map framebuffer address + /* identity map framebuffer address */ map_addr(main_fb.addr, main_fb.addr, FLAG_PRESENT | FLAG_WRITABLE); map_addr(main_fb.addr + PAGE_SIZE, main_fb.addr + PAGE_SIZE, FLAG_PRESENT | FLAG_WRITABLE); @@ -34,7 +32,7 @@ void init_mmap(mb2_tag_mmap *tag_mmap) { INIT_LIST(mmap.list) - // get data and store it into list + /* get data and store it into list */ for (size_t i = sizeof(mb2_tag_mmap); i < tag_mmap->size; i += sizeof(mb2_tag_mmap_entry)) { mmap_t *curr_mmap_entry = (mmap_t *)kalloc(sizeof(mmap_t)); @@ -46,7 +44,7 @@ void init_mmap(mb2_tag_mmap *tag_mmap) void init_module(mb2_tag_module *tag_module) { - // name is utf-8 encoded string! + /* name is utf-8 encoded string! */ uint32_t name_size = tag_module->size - sizeof(tag_module) + sizeof(char *); tag_module->name = (char *)kalloc(name_size); @@ -58,21 +56,21 @@ void init_module(mb2_tag_module *tag_module) void read_mb2(mb2_tag_header *multiboot_bootinfo, uint32_t multiboot_magic) { if (multiboot_magic != MB2_MAGIC) { - // not loaded by multiboot2 bootloader + /* not loaded by multiboot2 bootloader */ __asm__ __volatile__("hlt;"); } - // we will store framebuffer information here + /* we will store framebuffer information here */ static mb2_tag_fb *tag_fb; static mb2_tag_mmap *tag_mmap; - // skip first 8 bytes (total_size + reserved) + /* skip first 8 bytes (total_size + reserved) */ mb2_tag_header *tag_header = (mb2_tag_header *)((uint64_t)multiboot_bootinfo + 8 + KERNEL_VMA); while (tag_header->type != MB2_TAG_END) { - // process tag_type + /* process tag_type */ switch (tag_header->type) { case MB2_TAG_FB: tag_fb = (mb2_tag_fb *)tag_header; @@ -87,7 +85,7 @@ void read_mb2(mb2_tag_header *multiboot_bootinfo, uint32_t multiboot_magic) break; } - // next mb2_tag + /* next mb2_tag */ tag_header += tag_header->size / 8 + ((tag_header->size % 8) > 0); } diff --git a/kernel/src/check/panic.c b/kernel/src/check/panic.c index 7cfaa56..66a73c7 100644 --- a/kernel/src/check/panic.c +++ b/kernel/src/check/panic.c @@ -9,7 +9,7 @@ __attribute__((noreturn)) void panic(const char *s, ...) { set_color(&main_fb, RED, BLACK); printf("KERNEL PANIC\n"); - // set_color(&main_fb, WHITE, BLACK); + /* set_color(&main_fb, WHITE, BLACK); */ va_list list; va_start(list, s); vprintf(s, list); diff --git a/kernel/src/check/ubsan.c b/kernel/src/check/ubsan.c index dc6c4b4..eb7721e 100644 --- a/kernel/src/check/ubsan.c +++ b/kernel/src/check/ubsan.c @@ -247,10 +247,11 @@ void __ubsan_handle_vla_bound_not_positive(void *data_raw, void *bound_raw) ABORT_VARIANT_VP_VP(vla_bound_not_positive); struct ubsan_float_cast_overflow_data { - // TODO: Remove this GCC 5.x compatibility after switching to GCC 6.x. The - // GCC developers accidentally forgot the source location. Their - // libubsan probes to see if it looks like a path, but we don't need - // to maintain compatibility with multiple gcc releases. See below. + /* TODO: Remove this GCC 5.x compatibility after switching to GCC 6.x. The + * GCC developers accidentally forgot the source location. Their + * libubsan probes to see if it looks like a path, but we don't need + * to maintain compatibility with multiple gcc releases. See below. + */ #if !(defined(__GNUC__) && __GNUC__ < 6) struct ubsan_source_location location; #endif @@ -324,8 +325,9 @@ struct ubsan_nonnull_arg_data { struct ubsan_source_location attr_location; }; -// TODO: GCC's libubsan does not have the second parameter, but its builtin -// somehow has it and conflict if we don't match it. +/* TODO: GCC's libubsan does not have the second parameter, but its builtin + * somehow has it and conflict if we don't match it. + */ void __ubsan_handle_nonnull_arg(void *data_raw, intptr_t index_raw) { struct ubsan_nonnull_arg_data *data = diff --git a/kernel/src/cpu/gdt.c b/kernel/src/cpu/gdt.c index 7460474..43ca332 100644 --- a/kernel/src/cpu/gdt.c +++ b/kernel/src/cpu/gdt.c @@ -29,7 +29,7 @@ void add_gdt_tss(uint32_t num, uint64_t offset, uint32_t limit, uint8_t access, void reload_gdt() { __asm__ __volatile__( - // reload segment registers + /* reload segment registers */ "mov $0x10, %ax;" "mov %ax, %ds;" "mov %ax, %es;" @@ -50,10 +50,10 @@ void init_gdt() gdt_pointer.size = sizeof(gdt) - 1; add_gdt_entry(0, 0, 0, 0, 0); - add_gdt_entry(1, 0, 0xfffff, 0x9a, 0xa); // code ring0 - add_gdt_entry(2, 0, 0xfffff, 0x92, 0xc); // data ring0 - add_gdt_entry(3, 0, 0xfffff, 0xf2, 0xc); // data ring3 - add_gdt_entry(4, 0, 0xfffff, 0xfa, 0xa); // code ring3 + add_gdt_entry(1, 0, 0xfffff, 0x9a, 0xa); /* code ring0 */ + add_gdt_entry(2, 0, 0xfffff, 0x92, 0xc); /* data ring0 */ + add_gdt_entry(3, 0, 0xfffff, 0xf2, 0xc); /* data ring3 */ + add_gdt_entry(4, 0, 0xfffff, 0xfa, 0xa); /* code ring3 */ add_gdt_tss(5, (uint64_t)&tss, sizeof(tss_type), 0x89, 0); load_gdt(&gdt_pointer); diff --git a/kernel/src/cpu/idt.c b/kernel/src/cpu/idt.c index 9134d84..bc3e143 100644 --- a/kernel/src/cpu/idt.c +++ b/kernel/src/cpu/idt.c @@ -33,7 +33,7 @@ void add_to_idt(uint16_t num, uint64_t offset, uint16_t selector, uint8_t type) void init_idt_table(void) { - // exceptions + /* exceptions */ add_to_idt(0, (uint64_t)isr0, GDT_CODE_SEG, TRAP_GATE); add_to_idt(1, (uint64_t)isr1, GDT_CODE_SEG, TRAP_GATE); add_to_idt(2, (uint64_t)isr2, GDT_CODE_SEG, TRAP_GATE); @@ -67,7 +67,7 @@ void init_idt_table(void) add_to_idt(30, (uint64_t)isr30, GDT_CODE_SEG, TRAP_GATE); add_to_idt(31, (uint64_t)isr31, GDT_CODE_SEG, TRAP_GATE); - // interrupts + /* interrupts */ add_to_idt(32, (uint64_t)irq0, GDT_CODE_SEG, INTERRUPT_GATE); add_to_idt(33, (uint64_t)irq1, GDT_CODE_SEG, INTERRUPT_GATE); for (size_t i = 34; i < 256; i++) { diff --git a/kernel/src/cpu/pic.c b/kernel/src/cpu/pic.c index cfcc4d5..222fc0f 100644 --- a/kernel/src/cpu/pic.c +++ b/kernel/src/cpu/pic.c @@ -3,20 +3,20 @@ void remap_pic(void) { - // starts the initialization sequence (in cascade mode) + /* starts the initialization sequence (in cascade mode) */ outb(PIC1_COMMAND, ICW1_INIT | ICW1_ICW4); outb(PIC2_COMMAND, ICW1_INIT | ICW1_ICW4); - outb(PIC1_DATA, 0xe0); // ICW2: Master PIC vector offset - outb(PIC2_DATA, 0xe8); // ICW2: Slave PIC vector offset + outb(PIC1_DATA, 0xe0); /* ICW2: Master PIC vector offset */ + outb(PIC2_DATA, 0xe8); /* ICW2: Slave PIC vector offset */ outb(PIC1_DATA, - 4); // ICW3: tell Master PIC that there is a slave PIC at IRQ2 (0000 0100) + 4); /* ICW3: tell Master PIC that there is a slave PIC at IRQ2 (0000 0100) */ outb(PIC2_DATA, - 2); // ICW3: tell Slave PIC its cascade identity (0000 0010) + 2); /* ICW3: tell Slave PIC its cascade identity (0000 0010) */ outb(PIC1_DATA, ICW4_8086); outb(PIC2_DATA, ICW4_8086); - // mask all interrupts + /* mask all interrupts */ outb(PIC1_DATA, 0xff); outb(PIC2_DATA, 0xff); } diff --git a/kernel/src/devices/keyboard.c b/kernel/src/devices/keyboard.c index 67c2f19..d631aac 100644 --- a/kernel/src/devices/keyboard.c +++ b/kernel/src/devices/keyboard.c @@ -17,13 +17,16 @@ stdbuff *keyboard_buffer; void init_keyboard() { - // outb(KEYBOARD_CMD_PORT, 0xF3); - // io_wait(); - // outb(KEYBOARD_DATA_PORT, 0x00); - // io_wait(); - // while (!(inb(KEYBOARD_STATUS_PORT) & 1)) {} - // if (inb(KEYBOARD_DATA_PORT) == 0xFA) - // printf("[keyboard init]\n"); + /* + outb(KEYBOARD_CMD_PORT, 0xF3); + io_wait(); + outb(KEYBOARD_DATA_PORT, 0x00); + io_wait(); + while (!(inb(KEYBOARD_STATUS_PORT) & 1)) + ; + if (inb(KEYBOARD_DATA_PORT) == 0xFA) + printf("[keyboard init]\n"); + */ } void keyboard_handler() diff --git a/kernel/src/devices/serial.c b/kernel/src/devices/serial.c index 2d2da51..006f25a 100644 --- a/kernel/src/devices/serial.c +++ b/kernel/src/devices/serial.c @@ -3,13 +3,14 @@ void init_serial() { - outb(PORT + 1, 0x00); // Disable all interrupts - outb(PORT + 3, 0x80); // Enable DLAB (set baud rate divisor) - outb(PORT + 0, 0x03); // Set divisor to 3 (lo byte) 38400 baud - outb(PORT + 1, 0x00); // (hi byte) - outb(PORT + 3, 0x03); // 8 bits, no parity, one stop bit - outb(PORT + 2, 0xC7); // Enable FIFO, clear them, with 14-byte threshold - outb(PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set + outb(PORT + 1, 0x00); /* Disable all interrupts */ + outb(PORT + 3, 0x80); /* Enable DLAB (set baud rate divisor) */ + outb(PORT + 0, 0x03); /* Set divisor to 3 (lo byte) 38400 baud */ + outb(PORT + 1, 0x00); /* (hi byte) */ + outb(PORT + 3, 0x03); /* 8 bits, no parity, one stop bit */ + outb(PORT + 2, + 0xC7); /* Enable FIFO, clear them, with 14-byte threshold */ + outb(PORT + 4, 0x0B); /* IRQs enabled, RTS/DSR set */ } uint8_t serial_received() diff --git a/kernel/src/fs/ext2.c b/kernel/src/fs/ext2.c index 7750a50..0420a08 100644 --- a/kernel/src/fs/ext2.c +++ b/kernel/src/fs/ext2.c @@ -71,45 +71,45 @@ dentry_list_t *directory_to_entries(uint32_t inode) uint32_t bg_desc = (inode - 1) / ext2_superblock->inodes_per_group; uint32_t inode_index = (inode - 1) % ext2_superblock->inodes_per_group; - // block group descriptor + /* block group descriptor */ ext2_bg_desc_t *ext2_bg_desc; ext2_bg_desc = (ext2_bg_desc_t *)kalloc(sizeof(ext2_bg_desc_t)); read_bg_desc(bg_desc, ext2_bg_desc); - // inode table + /* inode table */ ext2_inode_t *ext2_inode; ext2_inode = (ext2_inode_t *)kalloc(sizeof(ext2_inode_t)); read_inode(ext2_bg_desc->inode_block_address, inode_index, ext2_inode); - // if it is not directory + /* if it is not directory */ if (!(ext2_inode->type_perms & TYPE_DIR)) return dentry_list; - // read inode contents + /* read inode contents */ for (size_t i = 0; i < 12; i++) { if (ext2_inode->dbp[i] == 0) break; - // get block + /* get block */ char block[BLOCK_SIZE]; read_block(ext2_inode->dbp[i], block); - // parse block + /* parse block */ for (size_t block_offset = 0; block_offset < BLOCK_SIZE;) { - // get dentry header + /* get dentry header */ dentry_list_t *ext2_dentry_list = (dentry_list_t *)kalloc(sizeof(dentry_list_t)); memcpy(&ext2_dentry_list->ext2_dentry, (char *)block + block_offset, sizeof(ext2_dentry_t) - sizeof(char *)); - // dentry is unused + /* dentry is unused */ if (ext2_dentry_list->ext2_dentry.inode == 0) { kfree(ext2_dentry_list); continue; } - // get dentry name + /* get dentry name */ ext2_dentry_list->ext2_dentry.name = (char *)kalloc( ext2_dentry_list->ext2_dentry.name_length_lower + 1); @@ -121,11 +121,11 @@ dentry_list_t *directory_to_entries(uint32_t inode) .name[ext2_dentry_list->ext2_dentry .name_length_lower] = '\0'; - // put dentry in list + /* put dentry in list */ add_to_list(&ext2_dentry_list->list, &dentry_list->list, dentry_list->list.next); - // offset + /* offset */ block_offset += ext2_dentry_list->ext2_dentry.size; } } @@ -145,12 +145,12 @@ char *files_to_buffer(uint32_t inode) uint32_t bg_desc = (inode - 1) / ext2_superblock->inodes_per_group; uint32_t inode_index = (inode - 1) % ext2_superblock->inodes_per_group; - // block group descriptor + /* block group descriptor */ ext2_bg_desc_t *ext2_bg_desc; ext2_bg_desc = (ext2_bg_desc_t *)kalloc(sizeof(ext2_bg_desc_t)); read_bg_desc(bg_desc, ext2_bg_desc); - // inode table + /* inode table */ ext2_inode_t *ext2_inode; ext2_inode = (ext2_inode_t *)kalloc(sizeof(ext2_inode_t)); read_inode(ext2_bg_desc->inode_block_address, inode_index, ext2_inode); @@ -192,7 +192,7 @@ path_t *path_to_list(const char *path) size_t n = strlen(path); for (i = 0, j = 0; i <= n; i++) { if (i == n || path[i] == '/') { - // add data before slash + /* add data before slash */ if (i != j) { path_t *curr_path = (path_t *)kalloc(sizeof(path_t)); @@ -204,7 +204,7 @@ path_t *path_to_list(const char *path) ÷d_path->list, divided_path->list.next); } - // add slash + /* add slash */ if (i != n) { path_t *curr_path = (path_t *)kalloc(sizeof(path_t)); @@ -228,22 +228,22 @@ uint32_t path_to_inode(const char *path) uint32_t inode = 0; path_t *divided_path = path_to_list(path); - // first entry is / + /* first entry is / */ path_t *curr_path = list_prev_entry(divided_path, list); curr_path = list_prev_entry(curr_path, list); inode = 2; while (curr_path != divided_path) { - // list of dentry + /* list of dentry */ dentry_list_t *dentry_list = directory_to_entries(inode); - // check if inode is actually a dir + /* check if inode is actually a dir */ if (list_is_empty((&dentry_list->list))) { printf("not a directory\n"); return 0; } - // iterate through all direntries + /* iterate through all direntries */ uint8_t ind = 1; dentry_list_t *curr_dir; list_for_each_entry_prev(curr_dir, (&dentry_list->list), list) { @@ -255,13 +255,13 @@ uint32_t path_to_inode(const char *path) } } - // if dir not found error + /* if dir not found error */ if (ind) { printf("file/dir not found\n"); return 0; } - // next dir + /* next dir */ curr_path = list_prev_entry(curr_path, list); if (curr_path != divided_path) curr_path = list_prev_entry(curr_path, list); diff --git a/kernel/src/main.c b/kernel/src/main.c index 037c43e..126c69e 100644 --- a/kernel/src/main.c +++ b/kernel/src/main.c @@ -28,7 +28,7 @@ int kernel_main(mb2_tag_header *multiboot_bootinfo, uint32_t multiboot_magic); int kernel_main(mb2_tag_header *multiboot_bootinfo, uint32_t multiboot_magic) { init_serial(); - // serial is enabled from this point + /* serial is enabled from this point */ init_gdt(); init_paging(); init_heap(); @@ -36,7 +36,7 @@ int kernel_main(mb2_tag_header *multiboot_bootinfo, uint32_t multiboot_magic) clear_screen(main_fb); init_mutex(&stdio_lock); init_mutex(&serial_stdio_lock); - // framebuffer is enabled from this point + /* framebuffer is enabled from this point */ init_pmm(); memory_usage(); init_keyboard(); diff --git a/kernel/src/mem/heap.c b/kernel/src/mem/heap.c index 537a2fe..0e703ab 100644 --- a/kernel/src/mem/heap.c +++ b/kernel/src/mem/heap.c @@ -17,20 +17,20 @@ void kheap_add_block(kheap_t *kheap, uint64_t addr, uint32_t size, { kheapblock_t *kheapblock; - // store size & bsize into kheapblock + /* store size & bsize into kheapblock */ kheapblock = (kheapblock_t *)addr; kheapblock->size = size - (uint32_t)sizeof(kheapblock_t); kheapblock->bsize = bsize; - // add kheapblock to kheap + /* add kheapblock to kheap */ kheapblock->next = kheap->fblock; kheap->fblock = kheapblock; - // block count & bitmap + /* block count & bitmap */ uint32_t bcnt = kheapblock->size / kheapblock->bsize; uint8_t *bm = (uint8_t *)&kheapblock[1]; - // clear bitmap + /* clear bitmap */ for (size_t i = 0; i < bcnt; i++) { bm[i] = 0; } @@ -48,7 +48,7 @@ void *kheap_alloc(kheap_t *kheap, uint32_t size) { kheapblock_t *kheapblock; - // find kheapblock that has enough space + /* find kheapblock that has enough space */ for (kheapblock = kheap->fblock; kheapblock; kheapblock = kheapblock->next) { if (kheapblock->size - (kheapblock->used * kheapblock->bsize) < @@ -56,7 +56,7 @@ void *kheap_alloc(kheap_t *kheap, uint32_t size) continue; } - // use heap with bsize 4096 just for that block size + /* use heap with bsize 4096 just for that block size */ bool ind1 = ((size % 4096) == 0); bool ind2 = (kheapblock->bsize == 4096); if (ind1 + ind2 == 1) { @@ -67,13 +67,13 @@ void *kheap_alloc(kheap_t *kheap, uint32_t size) uint32_t bneed = upper_div(size, kheapblock->bsize); uint8_t *bm = (uint8_t *)&kheapblock[1]; - // find empty block + /* find empty block */ for (size_t i = 0; i < bcnt; i++) { if (bm[i] != 0) { continue; } - // find bneed consecutive empty blocks + /* find bneed consecutive empty blocks */ size_t j; for (j = 0; bm[i + j] == 0 && j < bneed && i + j < bcnt; j++) @@ -83,12 +83,12 @@ void *kheap_alloc(kheap_t *kheap, uint32_t size) continue; } - // using id for the block that is different from previous or next block + /* using id for the block that is different from previous or next block */ uint8_t idp = bm[i - 1], idn = bm[i + j], id; for (id = idp + 1; id == idn || id == 0; id++) ; - // mark blocks as used + /* mark blocks as used */ for (j = 0; j < bneed; j++) { bm[i + j] = id; } @@ -112,9 +112,9 @@ void kheap_free(kheap_t *kheap, void *pointer) (uintptr_t)(pointer) < (uintptr_t)kheapblock + sizeof(kheapblock_t) + kheapblock->size) { - // found block + /* found block */ - // get index of bitmap entry + /* get index of bitmap entry */ uintptr_t pointer_offset = (uintptr_t)pointer - (uintptr_t)&kheapblock[0]; uint32_t bi = @@ -123,7 +123,7 @@ void kheap_free(kheap_t *kheap, void *pointer) uint8_t id = bm[bi]; uint32_t max = kheapblock->size / kheapblock->bsize; - // set blocks as free + /* set blocks as free */ size_t i; for (i = bi; bm[i] == id && i < max; i++) { bm[i] = 0; diff --git a/kernel/src/mem/paging.c b/kernel/src/mem/paging.c index aa61c5d..87097cd 100644 --- a/kernel/src/mem/paging.c +++ b/kernel/src/mem/paging.c @@ -16,11 +16,11 @@ void map_addr(uint64_t virt, uint64_t phys, uint32_t flags) virt -= virt % PAGE_SIZE; phys -= phys % PAGE_SIZE; - // i is in range [0, 511] - size_t pt_lvl4_i = (virt >> 39) % 0x200; // 512gb entry - size_t pt_lvl3_i = (virt >> 30) % 0x200; // 1gb entry - size_t pt_lvl2_i = (virt >> 21) % 0x200; // 2mb entry - // size_t pt_lvl1_i = (virt >> 12) % 0x200; // 4kb entry + /* i is in range [0, 511] */ + size_t pt_lvl4_i = (virt >> 39) % 0x200; /* 512gb entry */ + size_t pt_lvl3_i = (virt >> 30) % 0x200; /* 1gb entry */ + size_t pt_lvl2_i = (virt >> 21) % 0x200; /* 2mb entry */ + /* size_t pt_lvl1_i = (virt >> 12) % 0x200; 4kb entry */ uint64_t *pt_lvl3 = (uint64_t *)(page_table_lvl4[pt_lvl4_i] + KERNEL_VMA); @@ -53,7 +53,7 @@ void init_paging(void) (FLAG_PRESENT | FLAG_WRITABLE | FLAG_USER); page_table_lvl3[510] = (uint64_t)page_table_lvl2 - KERNEL_VMA + (FLAG_PRESENT | FLAG_WRITABLE | FLAG_USER); - // 16mb kernel + 32mb heap + 2mb (32kb stack * 64 threads) = first 50mb + /* 16mb kernel + 32mb heap + 2mb (32kb stack * 64 threads) = first 50mb */ for (size_t i = 0; i < 25; i++) { page_table_lvl2[i] = (uint64_t)0x0 + PAGE_SIZE * i + diff --git a/kernel/src/misc/graphics.c b/kernel/src/misc/graphics.c index f595146..8f2dd05 100644 --- a/kernel/src/misc/graphics.c +++ b/kernel/src/misc/graphics.c @@ -36,8 +36,6 @@ void clear_screen(fb_t fb) fb.y = 0; } -/* https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm */ - void fb_draw_line_low(fb_t fb, int32_t x0, int32_t y0, int32_t x1, int32_t y1, uint32_t col) { diff --git a/kernel/src/scheduler/ap_init.S b/kernel/src/scheduler/ap_init.S index ac758c8..c6f4dfb 100644 --- a/kernel/src/scheduler/ap_init.S +++ b/kernel/src/scheduler/ap_init.S @@ -11,9 +11,9 @@ ap_trampoline: .align 16 gdt32: .long 0, 0 - .long 0x0000FFFF, 0x00CF9A00 # flat code - .long 0x0000FFFF, 0x008F9200 # flat data - .long 0x00000068, 0x00CF8900 # tss + .long 0x0000FFFF, 0x00CF9A00 /* flat code */ + .long 0x0000FFFF, 0x008F9200 /* flat data */ + .long 0x00000068, 0x00CF8900 /* tss */ gdt32p: .word gdt32p - gdt32 - 1 .long 0x8010 @@ -34,14 +34,14 @@ _start32: movw $16, %ax movw %ax, %ds movw %ax, %ss - # get our Local APIC ID + /* get our Local APIC ID */ mov $1, %eax cpuid shrl $24, %ebx movl %ebx, %edi - # set up 32k stack, one for each core. It is important that all core must have its own stack + /* set up 32k stack, one for each core. It is important that all core must have its own stack */ movl $stack_top, %esp movl %ebx, %ecx 22: @@ -53,7 +53,7 @@ _start32: call enable_paging lgdt gdtp - # jump into C code (should never return) + /* jump into C code (should never return) */ ljmp $8, $_start64 .set KERNEL_VMA, 0xffffffff80000000 diff --git a/kernel/src/sys/userspace.c b/kernel/src/sys/userspace.c index 7091a3f..2cd3591 100644 --- a/kernel/src/sys/userspace.c +++ b/kernel/src/sys/userspace.c @@ -5,9 +5,9 @@ void begin_userspace() { - // read + /* read */ __asm__ __volatile__("mov $0x0, %rax; syscall;"); - // write + /* write */ __asm__ __volatile__("mov $0x1, %rax; syscall;"); while (true) { -- cgit v1.2.3