diff --git a/include/string.h b/include/string.h index 1977ad2..f966981 100644 --- a/include/string.h +++ b/include/string.h @@ -60,7 +60,7 @@ extern void *memset(void *restrict dest, int c, size_t n); * @returns a pointer to dest */ extern volatile void *memcpyv(volatile void *restrict dest, - const volatile void *restrict src, size_t n); + const volatile void *restrict src, size_t n); /** * Copy the first n bytes from memory area src to memory area dest. The memory @@ -72,7 +72,7 @@ extern volatile void *memcpyv(volatile void *restrict dest, * @returns a pointer to dest */ extern volatile void *memmovev(volatile void *restrict dest, - const volatile void *restrict src, size_t n); + const volatile void *restrict src, size_t n); /** * Fill the first n bytes of the memory region dest with the constant byte c. diff --git a/kernel/cpu/cpu.c b/kernel/cpu/cpu.c index 8f37402..a77baac 100644 --- a/kernel/cpu/cpu.c +++ b/kernel/cpu/cpu.c @@ -3,7 +3,8 @@ #include "pic.h" #include "idt.h" -void cpu_init(void) { +void cpu_init(void) +{ pic_remap(); idt_init(); fpu_init(); diff --git a/kernel/cpu/fpu.c b/kernel/cpu/fpu.c index f2674a7..cafe6e5 100644 --- a/kernel/cpu/fpu.c +++ b/kernel/cpu/fpu.c @@ -2,11 +2,12 @@ #include "fpu.h" -void fpu_init(void) { +void fpu_init(void) +{ size_t cr4; uint16_t cw = 0x37F; - __asm__ volatile ("mov %%cr4, %0" : "=r"(cr4)); + __asm__ volatile("mov %%cr4, %0" : "=r"(cr4)); cr4 |= 0x200; - __asm__ volatile ("mov %0, %%cr4" :: "r"(cr4)); - __asm__ volatile("fldcw %0" :: "m"(cw)); + __asm__ volatile("mov %0, %%cr4" ::"r"(cr4)); + __asm__ volatile("fldcw %0" ::"m"(cw)); } diff --git a/kernel/cpu/idt.c b/kernel/cpu/idt.c index af12fff..3bdbe8d 100644 --- a/kernel/cpu/idt.c +++ b/kernel/cpu/idt.c @@ -9,12 +9,12 @@ #define IDT_SIZE 256 struct idt_entry { - uint16_t isr_low; // low 16 bits of isr - uint16_t kernel_cs; // kernel segment selector - uint8_t ist; // interrupt stack table - uint8_t flags; // gate type, privilege level, present bit - uint16_t isr_mid; // middle 16 bits of isr - uint32_t isr_high; // high 32 bits of isr + uint16_t isr_low; // low 16 bits of isr + uint16_t kernel_cs; // kernel segment selector + uint8_t ist; // interrupt stack table + uint8_t flags; // gate type, privilege level, present bit + uint16_t isr_mid; // middle 16 bits of isr + uint32_t isr_high; // high 32 bits of isr uint32_t reserved; } __attribute__((packed)); @@ -24,7 +24,7 @@ struct idtr { } __attribute__((packed)); // interrupt gate -#define GATE_64BIT_INT 0x0E +#define GATE_64BIT_INT 0x0E // trap gate #define GATE_64BIT_TRAP 0x0F @@ -37,15 +37,15 @@ struct idtr { // interrupt is present in IDT #define PRESENT 0x80 -__attribute__((aligned(0x10))) -static struct idt_entry idt[256]; +__attribute__((aligned(0x10))) static struct idt_entry idt[256]; static struct idtr idtr; // from idt.S extern void *isr_stub_table[]; // initialize and load the IDT -void idt_init(void) { +void idt_init(void) +{ // initialize idtr idtr.address = (uint64_t)&idt; idtr.size = (uint16_t)sizeof(struct idt_entry) * IDT_SIZE - 1; @@ -67,10 +67,11 @@ void idt_init(void) { entry->reserved = 0; } - __asm__ volatile ("lidt %0" : : "m"(idtr)); + __asm__ volatile("lidt %0" : : "m"(idtr)); } -static void isr_print_regs(struct isr_regs *regs) { +static void isr_print_regs(struct isr_regs *regs) +{ printf("rax: %#016lx (%lu)\n", regs->rax, regs->rax); printf("rbx: %#016lx (%lu)\n", regs->rbx, regs->rbx); printf("rcx: %#016lx (%lu)\n", regs->rcx, regs->rcx); @@ -79,8 +80,8 @@ static void isr_print_regs(struct isr_regs *regs) { printf("rdi: %#016lx (%lu)\n", regs->rdi, regs->rdi); printf("rsp: %#016lx (%lu)\n", regs->rsp, regs->rsp); printf("rbp: %#016lx (%lu)\n", regs->rbp, regs->rbp); - printf("r8 : %#016lx (%lu)\n", regs->r8 , regs->r8 ); - printf("r9 : %#016lx (%lu)\n", regs->r9 , regs->r9 ); + printf("r8 : %#016lx (%lu)\n", regs->r8, regs->r8); + printf("r9 : %#016lx (%lu)\n", regs->r9, regs->r9); printf("r10: %#016lx (%lu)\n", regs->r10, regs->r10); printf("r11: %#016lx (%lu)\n", regs->r11, regs->r11); printf("r12: %#016lx (%lu)\n", regs->r12, regs->r12); @@ -91,30 +92,48 @@ static void isr_print_regs(struct isr_regs *regs) { printf("rflags: %#016lx (%lu)\n", regs->rflags, regs->rflags); struct rflags *rflags = (struct rflags *)regs->rflags; puts("rflags: "); - if (rflags->cf) puts("CF "); - if (rflags->pf) puts("PF "); - if (rflags->af) puts("AF "); - if (rflags->zf) puts("ZF "); - if (rflags->sf) puts("SF "); - if (rflags->tf) puts("TF "); - if (rflags->if_) puts("IF "); - if (rflags->df) puts("DF "); - if (rflags->of) puts("OF "); - if (rflags->iopl) puts("IOPL "); - if (rflags->nt) puts("NT "); - if (rflags->md) puts("MD "); - if (rflags->rf) puts("RF "); - if (rflags->vm) puts("VM "); - if (rflags->ac) puts("AC "); - if (rflags->vif) puts("VIF "); - if (rflags->vip) puts("VIP "); - if (rflags->id) puts("ID "); + if (rflags->cf) + puts("CF "); + if (rflags->pf) + puts("PF "); + if (rflags->af) + puts("AF "); + if (rflags->zf) + puts("ZF "); + if (rflags->sf) + puts("SF "); + if (rflags->tf) + puts("TF "); + if (rflags->if_) + puts("IF "); + if (rflags->df) + puts("DF "); + if (rflags->of) + puts("OF "); + if (rflags->iopl) + puts("IOPL "); + if (rflags->nt) + puts("NT "); + if (rflags->md) + puts("MD "); + if (rflags->rf) + puts("RF "); + if (rflags->vm) + puts("VM "); + if (rflags->ac) + puts("AC "); + if (rflags->vif) + puts("VIF "); + if (rflags->vip) + puts("VIP "); + if (rflags->id) + puts("ID "); puts("\n"); } -#define EX_DEBUG 0x01 -#define EX_BREAKPOINT 0x03 -#define EX_PAGE_FAULT 0x0e +#define EX_DEBUG 0x01 +#define EX_BREAKPOINT 0x03 +#define EX_PAGE_FAULT 0x0e // Intel manual vol 3 ch 6.3.1 char *EXCEPTIONS[] = { @@ -152,13 +171,15 @@ char *EXCEPTIONS[] = { "Reserved", }; -void idt_exception_handler(uint64_t exception, uint64_t code, struct isr_regs *state) { +void idt_exception_handler(uint64_t exception, uint64_t code, + struct isr_regs *state) +{ uint64_t cr2; switch (exception) { case EX_PAGE_FAULT: // page faults store the offending address in cr2 - __asm__ volatile ("mov %%cr2, %0" : "=r"(cr2)); + __asm__ volatile("mov %%cr2, %0" : "=r"(cr2)); if (!load_page((void *)cr2)) return; } @@ -182,13 +203,15 @@ void idt_exception_handler(uint64_t exception, uint64_t code, struct isr_regs *s } } -void idt_pic_eoi(uint8_t exception) { +void idt_pic_eoi(uint8_t exception) +{ pic_eoi(exception - PIC_REMAP_OFFSET); } int counter = 0; -void idt_pic_timer(void) { +void idt_pic_timer(void) +{ // print a message once we know the timer works // but avoid spamming the logs if (counter == 3) { @@ -199,6 +222,10 @@ void idt_pic_timer(void) { } } -void idt_pic_keyboard(void) {} +void idt_pic_keyboard(void) +{ +} -void idt_pic_mouse(void) {} +void idt_pic_mouse(void) +{ +} diff --git a/kernel/cpu/idt.h b/kernel/cpu/idt.h index 46ef4a3..7f1d9e6 100644 --- a/kernel/cpu/idt.h +++ b/kernel/cpu/idt.h @@ -36,33 +36,13 @@ struct isr_regs { }; struct rflags { - uint64_t cf : 1, - : 1, - pf : 1, - : 1, - af : 1, - : 1, - zf : 1, - sf : 1, + uint64_t cf : 1, : 1, pf : 1, : 1, af : 1, : 1, zf : 1, sf : 1, - tf : 1, - if_ : 1, - df : 1, - of : 1, - iopl : 2, - nt : 1, - md : 1, + tf : 1, if_ : 1, df : 1, of : 1, iopl : 2, nt : 1, md : 1, - rf : 1, - vm : 1, - ac : 1, - vif : 1, - vip : 1, - id : 1, - : 42; + rf : 1, vm : 1, ac : 1, vif : 1, vip : 1, id : 1, : 42; }; void idt_init(void); #endif /* idt.h */ - diff --git a/kernel/cpu/pic.c b/kernel/cpu/pic.c index 7065a03..c5c41cc 100644 --- a/kernel/cpu/pic.c +++ b/kernel/cpu/pic.c @@ -2,56 +2,63 @@ #include "pic.h" -#define PIC1 0x20 /* IO base address for master PIC */ -#define PIC2 0xA0 /* IO base address for slave PIC */ -#define PIC1_COMMAND PIC1 -#define PIC1_DATA (PIC1+1) -#define PIC2_COMMAND PIC2 -#define PIC2_DATA (PIC2+1) +#define PIC1 0x20 /* IO base address for master PIC */ +#define PIC2 0xA0 /* IO base address for slave PIC */ +#define PIC1_COMMAND PIC1 +#define PIC1_DATA (PIC1 + 1) +#define PIC2_COMMAND PIC2 +#define PIC2_DATA (PIC2 + 1) -#define PIC_EOI 0x20 /* End-of-interrupt command code */ +#define PIC_EOI 0x20 /* End-of-interrupt command code */ -#define ICW1_ICW4 0x01 /* Indicates that ICW4 will be present */ -#define ICW1_SINGLE 0x02 /* Single (cascade) mode */ -#define ICW1_INTERVAL4 0x04 /* Call address interval 4 (8) */ -#define ICW1_LEVEL 0x08 /* Level triggered (edge) mode */ -#define ICW1_INIT 0x10 /* Initialization - required! */ +#define ICW1_ICW4 0x01 /* Indicates that ICW4 will be present */ +#define ICW1_SINGLE 0x02 /* Single (cascade) mode */ +#define ICW1_INTERVAL4 0x04 /* Call address interval 4 (8) */ +#define ICW1_LEVEL 0x08 /* Level triggered (edge) mode */ +#define ICW1_INIT 0x10 /* Initialization - required! */ -#define ICW4_8086 0x01 /* 8086/88 (MCS-80/85) mode */ -#define ICW4_AUTO 0x02 /* Auto (normal) EOI */ -#define ICW4_BUF_SLAVE 0x08 /* Buffered mode/slave */ -#define ICW4_BUF_MASTER 0x0C /* Buffered mode/master */ -#define ICW4_SFNM 0x10 /* Special fully nested (not) */ +#define ICW4_8086 0x01 /* 8086/88 (MCS-80/85) mode */ +#define ICW4_AUTO 0x02 /* Auto (normal) EOI */ +#define ICW4_BUF_SLAVE 0x08 /* Buffered mode/slave */ +#define ICW4_BUF_MASTER 0x0C /* Buffered mode/master */ +#define ICW4_SFNM 0x10 /* Special fully nested (not) */ -void pic_remap(void) { +void pic_remap(void) +{ uint8_t a1, a2; - a1 = inb(PIC1_DATA); // save masks + a1 = inb(PIC1_DATA); // save masks a2 = inb(PIC2_DATA); - outb(PIC1_COMMAND, ICW1_INIT | ICW1_ICW4); // starts the initialization sequence (in cascade mode) + outb(PIC1_COMMAND, + ICW1_INIT | + ICW1_ICW4); // starts the initialization sequence (in cascade mode) io_wait(); outb(PIC2_COMMAND, ICW1_INIT | ICW1_ICW4); io_wait(); - outb(PIC1_DATA, PIC_REMAP_OFFSET); // ICW2: Master PIC vector offset + outb(PIC1_DATA, PIC_REMAP_OFFSET); // ICW2: Master PIC vector offset io_wait(); - outb(PIC2_DATA, PIC_REMAP_OFFSET + 8); // ICW2: Slave PIC vector offset + outb(PIC2_DATA, PIC_REMAP_OFFSET + 8); // ICW2: Slave PIC vector offset io_wait(); - outb(PIC1_DATA, 4); // ICW3: tell Master PIC that there is a slave PIC at IRQ2 (0000 0100) + outb( + PIC1_DATA, + 4); // ICW3: tell Master PIC that there is a slave PIC at IRQ2 (0000 0100) io_wait(); - outb(PIC2_DATA, 2); // ICW3: tell Slave PIC its cascade identity (0000 0010) + outb(PIC2_DATA, 2); // ICW3: tell Slave PIC its cascade identity (0000 0010) io_wait(); - outb(PIC1_DATA, ICW4_8086); // ICW4: have the PICs use 8086 mode (and not 8080 mode) + outb(PIC1_DATA, + ICW4_8086); // ICW4: have the PICs use 8086 mode (and not 8080 mode) io_wait(); outb(PIC2_DATA, ICW4_8086); io_wait(); - outb(PIC1_DATA, a1); // restore saved masks. + outb(PIC1_DATA, a1); // restore saved masks. outb(PIC2_DATA, a2); } -void pic_mask(int irq) { +void pic_mask(int irq) +{ uint16_t port; uint8_t mask; if (irq < 8) { @@ -64,7 +71,8 @@ void pic_mask(int irq) { outb(port, mask); } -void pic_unmask(int irq) { +void pic_unmask(int irq) +{ uint16_t port; uint8_t mask; if (irq < 8) { @@ -77,12 +85,14 @@ void pic_unmask(int irq) { outb(port, mask); } -void pic_disable(void) { +void pic_disable(void) +{ outb(PIC1_DATA, 0xff); outb(PIC2_DATA, 0xff); } -void pic_eoi(int irq) { +void pic_eoi(int irq) +{ if (irq >= 8) { outb(PIC2_COMMAND, PIC_EOI); } diff --git a/kernel/io/io.c b/kernel/io/io.c index 11acfdb..489148d 100644 --- a/kernel/io/io.c +++ b/kernel/io/io.c @@ -3,14 +3,17 @@ #include <comus/asm.h> #define PORT 0x3F8 -static void serial_out(uint8_t ch) { +static void serial_out(uint8_t ch) +{ // wait for output to be free - while ((inb(PORT + 5) & 0x20) == 0); + while ((inb(PORT + 5) & 0x20) == 0) + ; outb(PORT, ch); } -void fputc(FILE *stream, char c) { - (void) stream; +void fputc(FILE *stream, char c) +{ + (void)stream; serial_out(c); } diff --git a/kernel/io/panic.c b/kernel/io/panic.c index 948542e..403418f 100644 --- a/kernel/io/panic.c +++ b/kernel/io/panic.c @@ -2,8 +2,8 @@ #include <stdarg.h> #include <comus/asm.h> -__attribute__((noreturn)) -void panic(const char *format, ...) { +__attribute__((noreturn)) void panic(const char *format, ...) +{ cli(); va_list list; va_start(list, format); diff --git a/kernel/mboot/mboot.c b/kernel/mboot/mboot.c index 08a0f37..9156c3c 100644 --- a/kernel/mboot/mboot.c +++ b/kernel/mboot/mboot.c @@ -3,13 +3,13 @@ void *locate_mboot_table(volatile void *mboot, uint32_t type) { - struct mboot_info *info = (struct mboot_info *) mboot; - const char *mboot_end = ((char *) info) + info->total_size; + struct mboot_info *info = (struct mboot_info *)mboot; + const char *mboot_end = ((char *)info) + info->total_size; char *tag_ptr = info->tags; while (tag_ptr < mboot_end) { - struct mboot_tag *tag = (struct mboot_tag *) tag_ptr; + struct mboot_tag *tag = (struct mboot_tag *)tag_ptr; if (tag->type == type) return tag; diff --git a/kernel/mboot/mboot.h b/kernel/mboot/mboot.h index 39a961d..db85de1 100644 --- a/kernel/mboot/mboot.h +++ b/kernel/mboot/mboot.h @@ -11,14 +11,14 @@ #include <lib.h> -#define MBOOT_HEADER_MAGIC 0x36D76289 +#define MBOOT_HEADER_MAGIC 0x36D76289 -#define MBOOT_CMDLINE 1 -#define MBOOT_MEMORY_MAP 6 -#define MBOOT_FRAMEBUFFER 8 -#define MBOOT_ELF_SYMBOLS 9 -#define MBOOT_OLD_RSDP 14 -#define MBOOT_NEW_RSDP 15 +#define MBOOT_CMDLINE 1 +#define MBOOT_MEMORY_MAP 6 +#define MBOOT_FRAMEBUFFER 8 +#define MBOOT_ELF_SYMBOLS 9 +#define MBOOT_OLD_RSDP 14 +#define MBOOT_NEW_RSDP 15 struct mboot_info { uint32_t total_size; @@ -70,7 +70,6 @@ struct mboot_tag_mmap { struct mboot_mmap_entry entries[]; }; - struct mboot_tag_old_rsdp { uint32_t type; uint32_t size; diff --git a/kernel/mboot/mmap.c b/kernel/mboot/mmap.c index c17d510..ff38771 100644 --- a/kernel/mboot/mmap.c +++ b/kernel/mboot/mmap.c @@ -5,41 +5,30 @@ #include <stdint.h> #include <stdio.h> -static const char *segment_type[] = { - "Reserved", - "Free", - "Reserved", - "ACPI Reserved", - "Hibernation", - "Defective", - "Unknown" -}; +static const char *segment_type[] = { "Reserved", "Free", + "Reserved", "ACPI Reserved", + "Hibernation", "Defective", + "Unknown" }; void mboot_load_mmap(volatile void *mboot, struct memory_map *res) { void *tag = locate_mboot_table(mboot, MBOOT_MEMORY_MAP); - struct mboot_tag_mmap *mmap = (struct mboot_tag_mmap *) tag; + struct mboot_tag_mmap *mmap = (struct mboot_tag_mmap *)tag; int idx = 0; uintptr_t i = (uintptr_t)mmap->entries; printf("MEMORY MAP\n"); char buf[20]; - for (; - i < (uintptr_t)mmap->entries + mmap->size; - i += mmap->entry_size, idx++ - ) { - struct mboot_mmap_entry *seg = (struct mboot_mmap_entry *) i; + for (; i < (uintptr_t)mmap->entries + mmap->size; + i += mmap->entry_size, idx++) { + struct mboot_mmap_entry *seg = (struct mboot_mmap_entry *)i; const char *type = NULL; if (seg->type > 6) type = segment_type[6]; else type = segment_type[seg->type]; - printf("ADDR: %16p LEN: %4s TYPE: %s (%d)\n", - (void *)seg->addr, - btoa(seg->len, buf), - type, - seg->type - ); + printf("ADDR: %16p LEN: %4s TYPE: %s (%d)\n", (void *)seg->addr, + btoa(seg->len, buf), type, seg->type); if (seg->type != 1 || seg->len < 1) continue; res->entries[idx].addr = seg->addr; diff --git a/lib/memcpyv.c b/lib/memcpyv.c index a4dcd22..610daf2 100644 --- a/lib/memcpyv.c +++ b/lib/memcpyv.c @@ -1,7 +1,7 @@ #include <string.h> -volatile void *memcpyv(volatile void *restrict dest, const volatile void *restrict src, - size_t n) +volatile void *memcpyv(volatile void *restrict dest, + const volatile void *restrict src, size_t n) { volatile char *d = dest; volatile const char *s = src; diff --git a/lib/printf.c b/lib/printf.c index d879c29..4a85956 100644 --- a/lib/printf.c +++ b/lib/printf.c @@ -286,10 +286,9 @@ static int printf_lltoa(char *buf, options_t *opts, bool is_neg, } } - // print zeros if needed if (opts->width_set && len < opts->width && opts->zero) { - while(len++ < opts->width) + while (len++ < opts->width) *(buf++) = '0'; } @@ -351,7 +350,8 @@ static void handle_char_specifier(context_t *ctx, data_t c) printf_putc(ctx, c.c); } -static void handle_string_specifier(context_t *ctx, options_t *opts, data_t data) +static void handle_string_specifier(context_t *ctx, options_t *opts, + data_t data) { char *str = data.str; int str_len = 0; @@ -460,7 +460,7 @@ static void do_printf(context_t *ctx, va_list args) // end int case 's': // read string - data.str = va_arg(args, void*); + data.str = va_arg(args, void *); break; // end string case 'c': @@ -585,15 +585,18 @@ void vfprintf(FILE *stream, const char *format, va_list args) do_printf(&ctx, args); } -void putc(char c) { +void putc(char c) +{ fputc(stdout, c); } -void puts(const char *str) { +void puts(const char *str) +{ fputs(stdout, str); } -void fputs(FILE *stream, const char *s) { +void fputs(FILE *stream, const char *s) +{ while (*s) fputc(stream, *s++); }