From 2dbf529c33aa3e24beff944758d586bb0608c1be Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Tue, 15 Apr 2025 22:20:59 -0400 Subject: expand memory manager work with userspace (more then one ctx) --- kernel/drivers/acpi.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'kernel/drivers') diff --git a/kernel/drivers/acpi.c b/kernel/drivers/acpi.c index b4bb805..bd9fe54 100644 --- a/kernel/drivers/acpi.c +++ b/kernel/drivers/acpi.c @@ -287,10 +287,10 @@ static void acpi_load_table(uint64_t addr) struct acpi_header *temp, *mapped; uint32_t length; temp = (struct acpi_header *)(uintptr_t)addr; - mapped = kmapaddr(temp, sizeof(struct acpi_header)); + mapped = kmapaddr(temp, NULL, sizeof(struct acpi_header), 0); length = mapped->length; kunmapaddr(mapped); - mapped = kmapaddr(temp, length); + mapped = kmapaddr(temp, NULL, length, 0); if (!checksum((uint8_t *)mapped, mapped->length)) { kunmapaddr(mapped); return; -- cgit v1.2.3-freya From 730aafc1758e4096853c2491345901a296b4cc3e Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Wed, 16 Apr 2025 16:48:56 -0400 Subject: remove all printing to report functions --- kernel/drivers/acpi.c | 37 +++++++++++++++++++++++++++---- kernel/drivers/pci.c | 4 ++++ kernel/include/comus/drivers/acpi.h | 5 +++++ kernel/include/comus/drivers/pci.h | 5 +++++ kernel/include/comus/memory.h | 6 ++++++ kernel/main.c | 14 ++++++++++++ kernel/mboot/mmap.c | 15 +------------ kernel/memory/physalloc.c | 43 +++++++++++++++++++++++++++++++------ 8 files changed, 105 insertions(+), 24 deletions(-) (limited to 'kernel/drivers') diff --git a/kernel/drivers/acpi.c b/kernel/drivers/acpi.c index bd9fe54..41f7ba1 100644 --- a/kernel/drivers/acpi.c +++ b/kernel/drivers/acpi.c @@ -1,7 +1,9 @@ +#include "lib/kio.h" #include #include #include #include +#include struct acpi_header { uint32_t signature; @@ -295,7 +297,6 @@ static void acpi_load_table(uint64_t addr) kunmapaddr(mapped); return; } - kprintf("%.*s: %#016lx\n", 4, (char *)&mapped->signature, (size_t)temp); acpi_handle_table(mapped); } @@ -311,20 +312,48 @@ void acpi_init(void *rootsdp) } if (rsdp->revision == 0) { state.version = 0; - kprintf("ACPI 1.0\n"); acpi_load_table(rsdp->rsdt_addr); } else if (rsdp->revision == 2) { state.version = 2; struct xsdp *xsdp = (struct xsdp *)rsdp; - kprintf("ACPI 2.0\n"); acpi_load_table(xsdp->xsdt_addr); } else { panic("invalid acpi rev: %d\n", rsdp->revision); } - kprintf("\n"); outb(state.fadt->smi_command_port, state.fadt->acpi_enable); } +void acpi_report(void) +{ + if (state.version == 0) { + kprintf("ACPI 1.0\n"); + kprintf("%.*s: %#016lx\n", 4, (char *) &state.sdt.rsdt->h.signature, + (uintptr_t) state.sdt.rsdt); + } else { + kprintf("ACPI 2.0\n"); + kprintf("%.*s: %#016lx\n", 4, (char *) &state.sdt.xsdt->h.signature, + (uintptr_t) state.sdt.xsdt); + } + + if (state.fadt) + kprintf("%.*s: %#016lx\n", 4, (char *) &state.fadt->h.signature, + (uintptr_t) state.fadt); + if (state.dsdt) + kprintf("%.*s: %#016lx\n", 4, (char *) &state.dsdt->h.signature, + (uintptr_t) state.dsdt); + if (state.apic) + kprintf("%.*s: %#016lx\n", 4, (char *) &state.apic->h.signature, + (uintptr_t) state.apic); + if (state.hept) + kprintf("%.*s: %#016lx\n", 4, (char *) &state.hept->h.signature, + (uintptr_t) state.hept); + if (state.waet) + kprintf("%.*s: %#016lx\n", 4, (char *) &state.waet->h.signature, + (uintptr_t) state.waet); + + kprintf("\n"); +} + void acpi_shutdown(void) { outw((unsigned int)state.fadt->pm1_a_control_block, diff --git a/kernel/drivers/pci.c b/kernel/drivers/pci.c index e84ca11..ce2583f 100644 --- a/kernel/drivers/pci.c +++ b/kernel/drivers/pci.c @@ -134,6 +134,10 @@ void pci_init(void) } } } +} + +void pci_report(void) +{ kprintf("PCI DEVICES\n"); for (size_t i = 0; i < pci_table_next; i++) { print_device(&pci_table[i]); diff --git a/kernel/include/comus/drivers/acpi.h b/kernel/include/comus/drivers/acpi.h index bb11860..f4948f7 100644 --- a/kernel/include/comus/drivers/acpi.h +++ b/kernel/include/comus/drivers/acpi.h @@ -14,6 +14,11 @@ */ void acpi_init(void *rsdp); +/** + * Report ACPI tables + */ +void acpi_report(void); + /** * Shutdowns down the system */ diff --git a/kernel/include/comus/drivers/pci.h b/kernel/include/comus/drivers/pci.h index 34bcdda..b06aca9 100644 --- a/kernel/include/comus/drivers/pci.h +++ b/kernel/include/comus/drivers/pci.h @@ -56,6 +56,11 @@ struct pci_device { */ void pci_init(void); +/** + * Report all PCI devices + */ +void pci_report(void); + bool pci_findby_class(struct pci_device *dest, uint8_t class, uint8_t subclass, size_t *offset); bool pci_findby_id(struct pci_device *dest, uint16_t device, uint16_t vendor, diff --git a/kernel/include/comus/memory.h b/kernel/include/comus/memory.h index dec872d..ceca4aa 100644 --- a/kernel/include/comus/memory.h +++ b/kernel/include/comus/memory.h @@ -29,6 +29,7 @@ struct memory_segment { uint64_t addr; uint64_t len; + uint32_t type; }; struct memory_map { @@ -59,6 +60,11 @@ uint64_t memory_free(void); */ uint64_t memory_used(void); +/** + * Reports system memory usage and map + */ +void memory_report(void); + /** * Allocate a new memory context * diff --git a/kernel/main.c b/kernel/main.c index 4f91d95..f600320 100644 --- a/kernel/main.c +++ b/kernel/main.c @@ -1,10 +1,21 @@ +#include "lib/kio.h" #include #include #include #include +#include +#include #include #include +void kreport(void) +{ + cpu_report(); + memory_report(); + acpi_report(); + pci_report(); +} + void main(long magic, volatile void *mboot) { // initalize idt and pic @@ -22,6 +33,9 @@ void main(long magic, volatile void *mboot) // load file systems fs_init(); + // report system state + kreport(); + // halt kprintf("halting...\n"); } diff --git a/kernel/mboot/mmap.c b/kernel/mboot/mmap.c index e93421d..8959c49 100644 --- a/kernel/mboot/mmap.c +++ b/kernel/mboot/mmap.c @@ -20,11 +20,6 @@ struct multiboot_tag_mmap { struct multiboot_mmap_entry entries[]; }; -static const char *segment_type[] = { "Reserved", "Free", - "Reserved", "ACPI Reserved", - "Hibernation", "Defective", - "Unknown" }; - int mboot_get_mmap(struct memory_map *res) { void *tag = locate_mboot_table(MULTIBOOT_TAG_TYPE_MMAP); @@ -35,22 +30,14 @@ int mboot_get_mmap(struct memory_map *res) int idx = 0; uintptr_t i = (uintptr_t)mmap->entries; - kprintf("MEMORY MAP\n"); char buf[20]; for (; i < (uintptr_t)mmap->entries + mmap->size; i += mmap->entry_size, idx++) { struct multiboot_mmap_entry *seg = (struct multiboot_mmap_entry *)i; const char *type = NULL; - if (seg->type > 6) - type = segment_type[6]; - else - type = segment_type[seg->type]; - kprintf("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; res->entries[idx].len = seg->len; + res->entries[idx].type = seg->type; } res->entry_count = idx; diff --git a/kernel/memory/physalloc.c b/kernel/memory/physalloc.c index 7083c21..328502f 100644 --- a/kernel/memory/physalloc.c +++ b/kernel/memory/physalloc.c @@ -1,3 +1,4 @@ +#include "lib/kio.h" #include #include #include @@ -16,8 +17,14 @@ static uint64_t total_memory; static uint64_t free_memory; static uint64_t page_count; static uint64_t segment_count; +struct memory_map phys_mmap; struct memory_segment *page_start; +static const char *segment_type_str[] = { "Reserved", "Free", + "Reserved", "ACPI Reserved", + "Hibernation", "Defective", + "Unknown" }; + static int n_pages(const struct memory_segment *m) { return m->len / PAGE_SIZE; @@ -115,6 +122,10 @@ void free_phys_pages(void *ptr, int pages) static bool segment_invalid(const struct memory_segment *segment) { + if (segment->len < 1) + return true; + if (segment->type != 1) + return true; if (segment->addr < kaddr(kernel_start)) return true; if (segment->addr + segment->len < memory_start) @@ -161,6 +172,7 @@ void physalloc_init(struct memory_map *map) free_memory = 0; page_count = 0; page_start = NULL; + phys_mmap = *map; segment_count = 0; @@ -209,12 +221,6 @@ void physalloc_init(struct memory_map *map) total_memory = page_count * PAGE_SIZE; page_count -= bitmap_pages; free_memory = page_count * PAGE_SIZE; - - char buf[20]; - kprintf("\nMEMORY USAGE\n"); - kprintf("mem total: %s\n", btoa(memory_total(), buf)); - kprintf("mem free: %s\n", btoa(memory_free(), buf)); - kprintf("mem used: %s\n\n", btoa(memory_used(), buf)); } uint64_t memory_total(void) @@ -231,3 +237,28 @@ uint64_t memory_used(void) { return total_memory - free_memory; } + +void memory_report(void) +{ + char buf[20]; + + kprintf("MEMORY MAP\n"); + for (uint32_t i = 0; i < phys_mmap.entry_count; i++) { + struct memory_segment *seg; + const char *type_str; + + seg = &phys_mmap.entries[i]; + if (seg->type > 6) + type_str = segment_type_str[6]; + else + type_str = segment_type_str[seg->type]; + + kprintf("ADDR: %16p LEN: %4s TYPE: %s (%d)\n", (void *)seg->addr, + btoa(seg->len, buf), type_str, seg->type); + } + + kprintf("\nMEMORY USAGE\n"); + kprintf("mem total: %s\n", btoa(memory_total(), buf)); + kprintf("mem free: %s\n", btoa(memory_free(), buf)); + kprintf("mem used: %s\n\n", btoa(memory_used(), buf)); +} -- cgit v1.2.3-freya From 0ddc618b1d60ef6729326aa4e45b8280fcf51775 Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Wed, 16 Apr 2025 16:51:25 -0400 Subject: fmt --- kernel/cpu/cpu.c | 130 ++++++++++++++++++++++++---------------------- kernel/drivers/acpi.c | 28 +++++----- kernel/memory/physalloc.c | 6 +-- 3 files changed, 84 insertions(+), 80 deletions(-) (limited to 'kernel/drivers') diff --git a/kernel/cpu/cpu.c b/kernel/cpu/cpu.c index 3835b68..136a1d8 100644 --- a/kernel/cpu/cpu.c +++ b/kernel/cpu/cpu.c @@ -18,19 +18,19 @@ static inline void sse_init(void) { size_t cr0, cr4; __asm__ volatile("mov %%cr0, %0" : "=r"(cr0)); - cr0 &= ~0x4; // clear coprocessor emulation - cr0 |= 0x2; // set coprocessor monitoring - __asm__ volatile("mov %0, %%cr0" :: "r"(cr0)); + cr0 &= ~0x4; // clear coprocessor emulation + cr0 |= 0x2; // set coprocessor monitoring + __asm__ volatile("mov %0, %%cr0" ::"r"(cr0)); __asm__ volatile("mov %%cr4, %0" : "=r"(cr4)); cr4 |= 1 << 9; // set CR4.OSFXSR cr4 |= 1 << 10; // set CR4.OSXMMEXCPT - __asm__ volatile("mov %0, %%cr4" :: "r"(cr4)); + __asm__ volatile("mov %0, %%cr4" ::"r"(cr4)); } static inline void fxsave_init(void) { static char fxsave_region[512] __attribute__((aligned(16))); - __asm__ volatile("fxsave %0" :: "m"(fxsave_region)); + __asm__ volatile("fxsave %0" ::"m"(fxsave_region)); } static inline void xsave_init(void) @@ -38,23 +38,21 @@ static inline void xsave_init(void) size_t cr4; __asm__ volatile("mov %%cr4, %0" : "=r"(cr4)); cr4 |= 1 << 18; // set CR4.OSXSAVE - __asm__ volatile("mov %0, %%cr4" :: "r"(cr4)); + __asm__ volatile("mov %0, %%cr4" ::"r"(cr4)); } static inline void avx_init(void) { - __asm__ volatile( - "pushq %rax;" - "pushq %rcx;" - "pushq %rdx;" - "xorq %rcx, %rcx;" - "xgetbv;" - "orq $7, %rax;" - "xsetbv;" - "popq %rdx;" - "popq %rcx;" - "popq %rax;" - ); + __asm__ volatile("pushq %rax;" + "pushq %rcx;" + "pushq %rdx;" + "xorq %rcx, %rcx;" + "xgetbv;" + "orq $7, %rax;" + "xsetbv;" + "popq %rdx;" + "popq %rcx;" + "popq %rax;"); } void cpu_init(void) @@ -119,30 +117,37 @@ void cpu_report(void) kputs("\n\n"); } -static inline void cpuid(uint32_t leaf, uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx) +static inline void cpuid(uint32_t leaf, uint32_t *eax, uint32_t *ebx, + uint32_t *ecx, uint32_t *edx) { - __asm__ volatile("cpuid" - : "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx) - : "0" (leaf)); + __asm__ volatile("cpuid" + : "=a"(*eax), "=b"(*ebx), "=c"(*ecx), "=d"(*edx) + : "0"(leaf)); } -static inline void cpuid_count(uint32_t leaf, uint32_t subleaf, uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx) +static inline void cpuid_count(uint32_t leaf, uint32_t subleaf, uint32_t *eax, + uint32_t *ebx, uint32_t *ecx, uint32_t *edx) { - __asm__ volatile("cpuid" - : "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx) - : "0" (leaf), "2" (subleaf)); + __asm__ volatile("cpuid" + : "=a"(*eax), "=b"(*ebx), "=c"(*ecx), "=d"(*edx) + : "0"(leaf), "2"(subleaf)); } void cpu_vendor(char vendor[12]) { uint32_t ignore; - cpuid(0, &ignore, (uint32_t *)(vendor + 0), (uint32_t *)(vendor + 8), (uint32_t *)(vendor + 4)); + cpuid(0, &ignore, (uint32_t *)(vendor + 0), (uint32_t *)(vendor + 8), + (uint32_t *)(vendor + 4)); } -void cpu_name(char name[48]) { - cpuid(0x80000002, (uint32_t *)(name + 0), (uint32_t *)(name + 4), (uint32_t *)(name + 8), (uint32_t *)(name + 12)); - cpuid(0x80000003, (uint32_t *)(name + 16), (uint32_t *)(name + 20), (uint32_t *)(name + 24), (uint32_t *)(name + 28)); - cpuid(0x80000004, (uint32_t *)(name + 32), (uint32_t *)(name + 36), (uint32_t *)(name + 40), (uint32_t *)(name + 44)); +void cpu_name(char name[48]) +{ + cpuid(0x80000002, (uint32_t *)(name + 0), (uint32_t *)(name + 4), + (uint32_t *)(name + 8), (uint32_t *)(name + 12)); + cpuid(0x80000003, (uint32_t *)(name + 16), (uint32_t *)(name + 20), + (uint32_t *)(name + 24), (uint32_t *)(name + 28)); + cpuid(0x80000004, (uint32_t *)(name + 32), (uint32_t *)(name + 36), + (uint32_t *)(name + 40), (uint32_t *)(name + 44)); } void cpu_feats(struct cpu_feat *feats) @@ -156,19 +161,19 @@ void cpu_feats(struct cpu_feat *feats) cpuid(1, &ignore, &ignore, &ecx_1, &edx_1); cpuid_count(7, 0, &ignore, &ebx_7, &ignore, &ignore); - feats->fpu = edx_1 & (1<<0) ? 1 : 0; - feats->mmx = edx_1 & (1<<23) ? 1 : 0; - feats->sse = edx_1 & (1<<25) ? 1 : 0; - feats->sse2 = edx_1 & (1<<26) ? 1 : 0; - feats->sse3 = ecx_1 & (1<<0) ? 1 : 0; - feats->ssse3 = ecx_1 & (1<<9) ? 1 : 0; - feats->sse41 = ecx_1 & (1<<19) ? 1 : 0; - feats->sse42 = ecx_1 & (1<<20) ? 1 : 0; - feats->sse4a = ecx_1 & (1<<6) ? 1 : 0; - feats->avx = ecx_1 & (1<<28) ? 1 : 0; - feats->xsave = ecx_1 & (1<<26) ? 1 : 0; - feats->avx2 = ebx_7 & (1<<5) ? 1 : 0; - feats->avx512 = ebx_7 & (7<<16) ? 1 : 0; + feats->fpu = edx_1 & (1 << 0) ? 1 : 0; + feats->mmx = edx_1 & (1 << 23) ? 1 : 0; + feats->sse = edx_1 & (1 << 25) ? 1 : 0; + feats->sse2 = edx_1 & (1 << 26) ? 1 : 0; + feats->sse3 = ecx_1 & (1 << 0) ? 1 : 0; + feats->ssse3 = ecx_1 & (1 << 9) ? 1 : 0; + feats->sse41 = ecx_1 & (1 << 19) ? 1 : 0; + feats->sse42 = ecx_1 & (1 << 20) ? 1 : 0; + feats->sse4a = ecx_1 & (1 << 6) ? 1 : 0; + feats->avx = ecx_1 & (1 << 28) ? 1 : 0; + feats->xsave = ecx_1 & (1 << 26) ? 1 : 0; + feats->avx2 = ebx_7 & (1 << 5) ? 1 : 0; + feats->avx512 = ebx_7 & (7 << 16) ? 1 : 0; } void cpu_print_regs(struct cpu_regs *regs) @@ -192,42 +197,41 @@ void cpu_print_regs(struct cpu_regs *regs) kprintf("rip: %#016lx (%lu)\n", regs->rip, regs->rip); kprintf("rflags: %#016lx (%lu)\n", regs->rflags, regs->rflags); kputs("rflags: "); - if (regs->rflags & (1<<0)) + if (regs->rflags & (1 << 0)) kputs("CF "); - if (regs->rflags & (1<<2)) + if (regs->rflags & (1 << 2)) kputs("PF "); - if (regs->rflags & (1<<4)) + if (regs->rflags & (1 << 4)) kputs("AF "); - if (regs->rflags & (1<<6)) + if (regs->rflags & (1 << 6)) kputs("ZF "); - if (regs->rflags & (1<<7)) + if (regs->rflags & (1 << 7)) kputs("SF "); - if (regs->rflags & (1<<8)) + if (regs->rflags & (1 << 8)) kputs("TF "); - if (regs->rflags & (1<<9)) + if (regs->rflags & (1 << 9)) kputs("IF "); - if (regs->rflags & (1<<10)) + if (regs->rflags & (1 << 10)) kputs("DF "); - if (regs->rflags & (1<<11)) + if (regs->rflags & (1 << 11)) kputs("OF "); - if (regs->rflags & (3<<12)) + if (regs->rflags & (3 << 12)) kputs("IOPL "); - if (regs->rflags & (1<<14)) + if (regs->rflags & (1 << 14)) kputs("NT "); - if (regs->rflags & (1<<15)) + if (regs->rflags & (1 << 15)) kputs("MD "); - if (regs->rflags & (1<<16)) + if (regs->rflags & (1 << 16)) kputs("RF "); - if (regs->rflags & (1<<17)) + if (regs->rflags & (1 << 17)) kputs("VM "); - if (regs->rflags & (1<<18)) + if (regs->rflags & (1 << 18)) kputs("AC "); - if (regs->rflags & (1<<19)) + if (regs->rflags & (1 << 19)) kputs("VIF "); - if (regs->rflags & (1<<20)) + if (regs->rflags & (1 << 20)) kputs("VIP "); - if (regs->rflags & (1<<21)) + if (regs->rflags & (1 << 21)) kputs("ID "); kputs("\n"); } - diff --git a/kernel/drivers/acpi.c b/kernel/drivers/acpi.c index 41f7ba1..b4b219a 100644 --- a/kernel/drivers/acpi.c +++ b/kernel/drivers/acpi.c @@ -327,29 +327,29 @@ void acpi_report(void) { if (state.version == 0) { kprintf("ACPI 1.0\n"); - kprintf("%.*s: %#016lx\n", 4, (char *) &state.sdt.rsdt->h.signature, - (uintptr_t) state.sdt.rsdt); + kprintf("%.*s: %#016lx\n", 4, (char *)&state.sdt.rsdt->h.signature, + (uintptr_t)state.sdt.rsdt); } else { kprintf("ACPI 2.0\n"); - kprintf("%.*s: %#016lx\n", 4, (char *) &state.sdt.xsdt->h.signature, - (uintptr_t) state.sdt.xsdt); + kprintf("%.*s: %#016lx\n", 4, (char *)&state.sdt.xsdt->h.signature, + (uintptr_t)state.sdt.xsdt); } if (state.fadt) - kprintf("%.*s: %#016lx\n", 4, (char *) &state.fadt->h.signature, - (uintptr_t) state.fadt); + kprintf("%.*s: %#016lx\n", 4, (char *)&state.fadt->h.signature, + (uintptr_t)state.fadt); if (state.dsdt) - kprintf("%.*s: %#016lx\n", 4, (char *) &state.dsdt->h.signature, - (uintptr_t) state.dsdt); + kprintf("%.*s: %#016lx\n", 4, (char *)&state.dsdt->h.signature, + (uintptr_t)state.dsdt); if (state.apic) - kprintf("%.*s: %#016lx\n", 4, (char *) &state.apic->h.signature, - (uintptr_t) state.apic); + kprintf("%.*s: %#016lx\n", 4, (char *)&state.apic->h.signature, + (uintptr_t)state.apic); if (state.hept) - kprintf("%.*s: %#016lx\n", 4, (char *) &state.hept->h.signature, - (uintptr_t) state.hept); + kprintf("%.*s: %#016lx\n", 4, (char *)&state.hept->h.signature, + (uintptr_t)state.hept); if (state.waet) - kprintf("%.*s: %#016lx\n", 4, (char *) &state.waet->h.signature, - (uintptr_t) state.waet); + kprintf("%.*s: %#016lx\n", 4, (char *)&state.waet->h.signature, + (uintptr_t)state.waet); kprintf("\n"); } diff --git a/kernel/memory/physalloc.c b/kernel/memory/physalloc.c index 328502f..676e68e 100644 --- a/kernel/memory/physalloc.c +++ b/kernel/memory/physalloc.c @@ -21,9 +21,9 @@ struct memory_map phys_mmap; struct memory_segment *page_start; static const char *segment_type_str[] = { "Reserved", "Free", - "Reserved", "ACPI Reserved", - "Hibernation", "Defective", - "Unknown" }; + "Reserved", "ACPI Reserved", + "Hibernation", "Defective", + "Unknown" }; static int n_pages(const struct memory_segment *m) { -- cgit v1.2.3-freya From f3e6838b44a5b37ce7664db5b8662e3d02e5f539 Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Thu, 17 Apr 2025 00:51:46 -0400 Subject: font rending in framebuffer yay!! --- build.zig | 9 +- font/default8x16.psfu | Bin 0 -> 4969 bytes kernel/drivers.c | 14 +++ kernel/drivers/drivers.c | 12 --- kernel/drivers/gpu.c | 119 ++++++++++++++++++++++ kernel/drivers/gpu/bochs.c | 98 ++++++++++++++++++ kernel/drivers/term.c | 105 ------------------- kernel/drivers/vga.c | 25 +++++ kernel/font.S | 7 ++ kernel/include/comus/drivers/gpu.h | 50 +++++++++ kernel/include/comus/drivers/gpu/bochs.h | 40 ++++++++ kernel/include/comus/drivers/term.h | 37 ------- kernel/include/comus/drivers/vga.h | 22 ++++ kernel/include/comus/limits.h | 4 + kernel/include/comus/term.h | 85 ++++++++++++++++ kernel/include/lib/kctype.h | 5 + kernel/lib/isprint.c | 6 ++ kernel/lib/kprintf.c | 2 +- kernel/main.c | 2 + kernel/term.c | 169 +++++++++++++++++++++++++++++++ 20 files changed, 654 insertions(+), 157 deletions(-) create mode 100644 font/default8x16.psfu create mode 100644 kernel/drivers.c delete mode 100644 kernel/drivers/drivers.c create mode 100644 kernel/drivers/gpu.c create mode 100644 kernel/drivers/gpu/bochs.c delete mode 100644 kernel/drivers/term.c create mode 100644 kernel/drivers/vga.c create mode 100644 kernel/font.S create mode 100644 kernel/include/comus/drivers/gpu.h create mode 100644 kernel/include/comus/drivers/gpu/bochs.h delete mode 100644 kernel/include/comus/drivers/term.h create mode 100644 kernel/include/comus/drivers/vga.h create mode 100644 kernel/include/comus/term.h create mode 100644 kernel/lib/isprint.c create mode 100644 kernel/term.c (limited to 'kernel/drivers') diff --git a/build.zig b/build.zig index da70ba1..cee2138 100644 --- a/build.zig +++ b/build.zig @@ -27,12 +27,15 @@ const kernel_src = &[_][]const u8{ "kernel/cpu/idt.c", "kernel/cpu/idt.S", "kernel/cpu/pic.c", + "kernel/drivers.c", "kernel/drivers/acpi.c", "kernel/drivers/clock.c", - "kernel/drivers/drivers.c", + "kernel/drivers/gpu.c", + "kernel/drivers/gpu/bochs.c", "kernel/drivers/pci.c", - "kernel/drivers/term.c", "kernel/drivers/uart.c", + "kernel/drivers/vga.c", + "kernel/font.S", "kernel/fs/fs.c", "kernel/lib/atox.c", "kernel/lib/backtrace.c", @@ -40,6 +43,7 @@ const kernel_src = &[_][]const u8{ "kernel/lib/btoa.c", "kernel/lib/ctoi.c", "kernel/lib/isdigit.c", + "kernel/lib/isprint.c", "kernel/lib/isspace.c", "kernel/lib/itoc.c", "kernel/lib/kalloc.c", @@ -71,6 +75,7 @@ const kernel_src = &[_][]const u8{ "kernel/memory/physalloc.c", "kernel/memory/virtalloc.c", "kernel/procs/procs.c", + "kernel/term.c", }; const Prog = struct { diff --git a/font/default8x16.psfu b/font/default8x16.psfu new file mode 100644 index 0000000..e789b77 Binary files /dev/null and b/font/default8x16.psfu differ diff --git a/kernel/drivers.c b/kernel/drivers.c new file mode 100644 index 0000000..04145db --- /dev/null +++ b/kernel/drivers.c @@ -0,0 +1,14 @@ +#include +#include +#include +#include +#include +#include + +void drivers_init(void) +{ + uart_init(); + pci_init(); + acpi_init(mboot_get_rsdp()); + gpu_init(); +} diff --git a/kernel/drivers/drivers.c b/kernel/drivers/drivers.c deleted file mode 100644 index 4e8f175..0000000 --- a/kernel/drivers/drivers.c +++ /dev/null @@ -1,12 +0,0 @@ -#include -#include -#include -#include -#include - -void drivers_init(void) -{ - uart_init(); - pci_init(); - acpi_init(mboot_get_rsdp()); -} diff --git a/kernel/drivers/gpu.c b/kernel/drivers/gpu.c new file mode 100644 index 0000000..7ecdada --- /dev/null +++ b/kernel/drivers/gpu.c @@ -0,0 +1,119 @@ +#include +#include +#include +#include +#include +#include + +enum gpu_type { + GPU_UNSET = 0, + GPU_BOCHS = 1, + N_GPU_TYPE, +}; + +static enum gpu_type type = GPU_UNSET; + +static const char *gpu_type_str[N_GPU_TYPE] = { + "Unknown", "Bochs" +}; + +struct psf2_font { + uint8_t magic[4]; + uint32_t version; + uint32_t header_size; + uint32_t flags; + uint32_t length; + uint32_t glyph_size; + uint32_t height; + uint32_t width; + uint8_t data[]; +}; + +extern struct psf2_font en_font; + +int gpu_init(void) +{ + if (bochs_init() == SUCCESS) + type = GPU_BOCHS; + + if (type != GPU_UNSET) { + uint16_t width = gpu_width() / en_font.width; + uint16_t height = gpu_height() / en_font.height; + term_switch_handler(width, height, gpu_draw_char); + } + + return type != GPU_UNSET; +} + +uint32_t gpu_width(void) +{ + switch (type) { + case GPU_BOCHS: + return bochs_width(); + default: + return 0; + } +} + +uint32_t gpu_height(void) +{ + switch (type) { + case GPU_BOCHS: + return bochs_height(); + default: + return 0; + } +} + +uint8_t gpu_bit_depth(void) +{ + switch (type) { + case GPU_BOCHS: + return bochs_bit_depth(); + default: + return 0; + } +} + +void gpu_set_pixel(uint32_t x, uint32_t y, uint32_t r, uint32_t g, uint32_t b) +{ + switch (type) { + case GPU_BOCHS: + bochs_set_pixel(x, y, r, g, b); + break; + default: + break; + } +} + +void gpu_draw_char(char c, uint16_t cx, uint16_t cy) +{ + uint32_t sx = en_font.width * cx; + uint32_t sy = en_font.height * cy; + + uint8_t *glyph = &en_font.data[en_font.glyph_size * c]; + + for (uint32_t i = 0; i < en_font.width; i++) { + for (uint32_t j = 0; j < en_font.height; j++) { + uint32_t x = sx + (en_font.width - i - 1); + uint32_t y = sy + j; + uint32_t bitoff = i + j * en_font.width; + uint32_t byteoff = bitoff / 8; + uint32_t bitshift = bitoff % 8; + + uint32_t color = ((glyph[byteoff] >> bitshift) & 0x1) * 255; + gpu_set_pixel(x, y, color, color, color); + } + } +} + +void gpu_report(void) +{ + if (type == GPU_UNSET) + return; + + kprintf("GPU (%s)\n", gpu_type_str[type]); + kprintf("Width: %d\n", gpu_width()); + kprintf("Height: %d\n", gpu_height()); + kprintf("Bit depth: %d\n\n", gpu_bit_depth()); +} diff --git a/kernel/drivers/gpu/bochs.c b/kernel/drivers/gpu/bochs.c new file mode 100644 index 0000000..5b0f1fc --- /dev/null +++ b/kernel/drivers/gpu/bochs.c @@ -0,0 +1,98 @@ +#include +#include +#include +#include +#include + +#define INDEX 0x1CE +#define DATA 0x1CF + +#define INDEX_ID 0 +#define INDEX_XRES 1 +#define INDEX_YRES 2 +#define INDEX_BPP 3 +#define INDEX_ENABLE 4 +#define INDEX_BANK 5 +#define INDEX_VIRT_WIDTH 6 +#define INDEX_VIRT_HEIGHT 7 +#define INDEX_X_OFFSET 8 +#define INDEX_Y_OFFSET 9 + +#define DATA_DISP_DISABLE 0x00 +#define DATA_DISP_ENABLE 0x01 +#define DATA_LFB_ENABLE 0x40 +#define DATA_NO_CLEAR_MEM 0x80 + +#define BOCHS_PCI_VENDOR 0x1234 +#define BOCHS_PCI_DEVICE 0x1111 + +#define BOCHS_WIDTH 1024 +#define BOCHS_HEIGHT 768 +#define BOCHS_BIT_DEPTH 32 + +static volatile uint32_t *framebuffer; +static uint16_t width = BOCHS_WIDTH; +static uint16_t height = BOCHS_HEIGHT; +static uint8_t bit_depth = BOCHS_BIT_DEPTH; + +static void write(uint16_t index, uint16_t data) { + outw(INDEX, index); + outw(DATA, data); +} + +static uint16_t read(uint16_t value) { + outw(INDEX, value); + return inw(DATA); +} + +static int is_available(void) { + return (read(INDEX_ID) == 0xB0C5); +} + +static void set_mode(uint16_t width, uint16_t height, uint16_t bit_depth, int lfb, int clear) { + write(INDEX_ENABLE, DATA_DISP_DISABLE); + write(INDEX_XRES, width); + write(INDEX_YRES, height); + write(INDEX_BPP, bit_depth); + write(INDEX_ENABLE, DATA_DISP_ENABLE | + (lfb ? DATA_LFB_ENABLE : 0) | + (clear ? 0 : DATA_NO_CLEAR_MEM)); +} + +int bochs_init(void) { + struct pci_device bochs = {0}; + bool found = pci_findby_id(&bochs, BOCHS_PCI_DEVICE, BOCHS_PCI_VENDOR, NULL); + if (!found) + return 1; + + set_mode(width, height, bit_depth, true, true); + if (!is_available()) + return 1; + + uint32_t bar0 = pci_rcfg_d(bochs, PCI_BAR0_D); + uint32_t *addr = (uint32_t *) (uintptr_t) bar0; + framebuffer = kmapaddr(addr, NULL, width * height * bit_depth, F_WRITEABLE); + + return 0; +} + +uint32_t bochs_width(void) +{ + return width; +} + +uint32_t bochs_height(void) +{ + return height; +} + +uint8_t bochs_bit_depth(void) +{ + return bit_depth; +} + +void bochs_set_pixel(uint32_t x, uint32_t y, uint32_t r, uint32_t g, uint32_t b) +{ + uint32_t index = x + y * width; + framebuffer[index] = (b << 0) | (g << 8) | (r << 16); +} diff --git a/kernel/drivers/term.c b/kernel/drivers/term.c deleted file mode 100644 index d1cd553..0000000 --- a/kernel/drivers/term.c +++ /dev/null @@ -1,105 +0,0 @@ -#include -#include -#include -#include - -#define VGA_ADDR 0xB8000 - -static const uint8_t width = 80; -static const uint8_t height = 25; -static volatile uint16_t *buffer = (uint16_t *)VGA_ADDR; - -// position -static uint32_t x = 0, y = 0; - -// color -static uint8_t fg = 15, bg = 0; - -// blank color -const uint16_t blank = (uint16_t)0 | 0 << 12 | 15 << 8; - -void term_clear_line(int line) -{ - if (line < 0 || line >= height) - return; - for (uint8_t x = 0; x < width; x++) { - const size_t index = line * width + x; - buffer[index] = blank; - } -} - -void term_clear(void) -{ - for (uint8_t y = 0; y < height; y++) - term_clear_line(y); -} - -void term_scroll(int lines) -{ - cli(); - y -= lines; - if (!lines) - return; - if (lines >= height || lines <= -height) { - term_clear(); - } else if (lines > 0) { - memmovev(buffer, buffer + lines * width, 2 * (height - lines) * width); - term_clear_line(height - lines); - } else { - memmovev(buffer + lines * width, buffer + lines, - (height + lines) * width); - } - sti(); -} - -void term_out(char c) -{ - if (buffer == NULL) - return; - - // handle special chars - switch (c) { - case '\n': - x = 0; - y++; - break; - case '\t': - x += 4; - break; - case '\v': - case '\f': - y++; - break; - case '\r': - x = 0; - break; - default: { - const size_t index = y * width + x; - buffer[index] = c | bg << 12 | fg << 8; - x++; - } - } - - // wrap cursor if needed - if (x >= width) { - x = 0; - y++; - } - if (y >= height) { - term_scroll(y - (height - 1)); - y = height - 1; - } - - // set cursor position on screen - const uint16_t pos = y * width + x; - outb(0x3D4, 0x0F); - outb(0x3D5, (uint8_t)(pos & 0xFF)); - outb(0x3D4, 0x0E); - outb(0x3D5, (uint8_t)((pos >> 8) & 0xFF)); -} - -void term_out_str(const char *str) -{ - while (*str) - term_out(*str++); -} diff --git a/kernel/drivers/vga.c b/kernel/drivers/vga.c new file mode 100644 index 0000000..d73fa9f --- /dev/null +++ b/kernel/drivers/vga.c @@ -0,0 +1,25 @@ +#include +#include +#include +#include +#include + +#define VGA_ADDR 0xB8000 +static volatile uint16_t *buffer = (uint16_t *)VGA_ADDR; + +// color +static uint8_t fg = 15, bg = 0; + +void vga_draw_char(char c, uint16_t x, uint16_t y) +{ + // output character + const size_t index = y * VGA_WIDTH + x; + buffer[index] = c | bg << 12 | fg << 8; + + // set cursor position on screen + const uint16_t pos = y * VGA_HEIGHT + x; + outb(0x3D4, 0x0F); + outb(0x3D5, (uint8_t)(pos & 0xFF)); + outb(0x3D4, 0x0E); + outb(0x3D5, (uint8_t)((pos >> 8) & 0xFF)); +} diff --git a/kernel/font.S b/kernel/font.S new file mode 100644 index 0000000..f125e90 --- /dev/null +++ b/kernel/font.S @@ -0,0 +1,7 @@ + .globl en_font + + .section .rodata + + .align 8 +en_font: + .incbin "../../font/default8x16.psfu" diff --git a/kernel/include/comus/drivers/gpu.h b/kernel/include/comus/drivers/gpu.h new file mode 100644 index 0000000..d6b4266 --- /dev/null +++ b/kernel/include/comus/drivers/gpu.h @@ -0,0 +1,50 @@ +/** + * @file gpu.h + * + * @author Freya Murphy + * + * Graphics Driver Initalization + */ + +#ifndef GPU_H_ +#define GPU_H_ + +#include + +/** + * Loads any gpu graphics driver + * @returns 0 on success + */ +int gpu_init(void); + +/** + * @returns the width of the framebuffer + */ +uint32_t gpu_width(void); + +/** + * @returns the height of the framebuffer + */ +uint32_t gpu_height(void); + +/** + * @returns the bit depth of the framebuffer + */ +uint8_t gpu_bit_depth(void); + +/** + * Sets the pixel at a given position + */ +void gpu_set_pixel(uint32_t x, uint32_t y, uint32_t r, uint32_t g, uint32_t b); + +/** + * Draw a character on the gpu at a given (terminal) position + */ +void gpu_draw_char(char c, uint16_t x, uint16_t y); + +/** + * Reports the state of the gpu + */ +void gpu_report(void); + +#endif /* gpu.h */ diff --git a/kernel/include/comus/drivers/gpu/bochs.h b/kernel/include/comus/drivers/gpu/bochs.h new file mode 100644 index 0000000..5933a3a --- /dev/null +++ b/kernel/include/comus/drivers/gpu/bochs.h @@ -0,0 +1,40 @@ +/** + * @file bochs.h + * + * @author Freya Murphy + * + * Bochs Graphics Driver + */ + +#ifndef BOCHS_H_ +#define BOCHS_H_ + +#include + +/** + * Loads the bochs graphics driver + * @returns 0 on success + */ +int bochs_init(void); + +/** + * @returns the width of the framebuffer + */ +uint32_t bochs_width(void); + +/** + * @returns the height of the framebuffer + */ +uint32_t bochs_height(void); + +/** + * @returns the bit depth of the framebuffer + */ +uint8_t bochs_bit_depth(void); + +/** + * Sets the pixel at a given position + */ +void bochs_set_pixel(uint32_t x, uint32_t y, uint32_t r, uint32_t g, uint32_t b); + +#endif /* bochs.h */ diff --git a/kernel/include/comus/drivers/term.h b/kernel/include/comus/drivers/term.h deleted file mode 100644 index ad55bfd..0000000 --- a/kernel/include/comus/drivers/term.h +++ /dev/null @@ -1,37 +0,0 @@ -/** - * @file tty.h - * - * @author Freya Murphy - * - * Terminal output - */ - -#ifndef TERM_H_ -#define TERM_H_ - -/** - * Output a character to the terminal - */ -void term_out(char c); - -/** - * Output a string to the terminal - */ -void term_out_str(const char *str); - -/** - * Clear all lines on the terminal - */ -void term_clear(void); - -/** - * Clear a specifc line on the terminal terminal - */ -void term_clear_line(int line); - -/** - * Scroll terminal - */ -void term_scroll(int lines); - -#endif /* tty.h */ diff --git a/kernel/include/comus/drivers/vga.h b/kernel/include/comus/drivers/vga.h new file mode 100644 index 0000000..71906f6 --- /dev/null +++ b/kernel/include/comus/drivers/vga.h @@ -0,0 +1,22 @@ +/** + * @file vga.h + * + * @author Freya Murphy + * + * VGA Text Mode Functions + */ + +#ifndef VGA_H_ +#define VGA_H_ + +#define VGA_WIDTH 80 +#define VGA_HEIGHT 25 + +#include + +/** + * Draw a character to the vga text mode at a given position + */ +void vga_draw_char(char c, uint16_t x, uint16_t y); + +#endif /* vga.h */ diff --git a/kernel/include/comus/limits.h b/kernel/include/comus/limits.h index 173d013..2942b89 100644 --- a/kernel/include/comus/limits.h +++ b/kernel/include/comus/limits.h @@ -8,3 +8,7 @@ /// max number of processes #define N_PROCS 256 + +/// length of terminal buffer +#define TERM_MAX_WIDTH 480 +#define TERM_MAX_HEIGHT 270 diff --git a/kernel/include/comus/term.h b/kernel/include/comus/term.h new file mode 100644 index 0000000..2d39915 --- /dev/null +++ b/kernel/include/comus/term.h @@ -0,0 +1,85 @@ +/** + * @file term.h + * + * @author Freya Murphy + * + * Terminal output + */ + +#ifndef TERM_H_ +#define TERM_H_ + +#include + +/// vtable structure for terminal handlers +/// +/// required: +/// out_at - put out char at position +/// scroll - scroll the terminal count lines +/// +/// optional: +/// clear - clear terminal +/// clear_line - clear a specific line on the terminal +/// +struct terminal { + uint16_t width; + uint16_t height; + void (*draw_char)(char c, uint16_t x, uint16_t y); +}; + +/** + * Get the character width of the terminal + */ +uint16_t term_width(void); + +/** + * Get the character height of the terminal + */ +uint16_t term_height(void); + +/** + * Output a character to the terminal + */ +void term_out(char c); + +/** + * Output a character to the terminal at a given position + */ +void term_out_at(char c, uint16_t x, uint16_t y); + +/** + * Output a string to the terminal + */ +void term_out_str(const char *str); + +/** + * Output a string to the terminal at a given position + */ +void term_out_str_at(const char *str, uint16_t x, uint16_t y); + +/** + * Clear all lines on the terminal + */ +void term_clear(void); + +/** + * Clear a specifc line on the terminal terminal + */ +void term_clear_line(uint16_t line); + +/** + * Redraw terminal + */ +void term_redraw(void); + +/** + * Scroll terminal + */ +void term_scroll(uint16_t lines); + +/** + * Switch terminal handler + */ +void term_switch_handler(uint16_t width, uint16_t height, void (*draw_char)(char c, uint16_t x, uint16_t y)); + +#endif /* term.h */ diff --git a/kernel/include/lib/kctype.h b/kernel/include/lib/kctype.h index 6d090c6..aee1bb9 100644 --- a/kernel/include/lib/kctype.h +++ b/kernel/include/lib/kctype.h @@ -19,4 +19,9 @@ int isspace(int c); */ int isdigit(int c); +/** + * @returns if a character is ascii printable + */ +int isprint(int c); + #endif /* kctype.h */ diff --git a/kernel/lib/isprint.c b/kernel/lib/isprint.c new file mode 100644 index 0000000..e5582dc --- /dev/null +++ b/kernel/lib/isprint.c @@ -0,0 +1,6 @@ +#include + +int isprint(int c) +{ + return ((unsigned)(c - 0x20) <= (0x7e - 0x20)); +} diff --git a/kernel/lib/kprintf.c b/kernel/lib/kprintf.c index 269318f..452e45c 100644 --- a/kernel/lib/kprintf.c +++ b/kernel/lib/kprintf.c @@ -1,7 +1,7 @@ #include "lib/kio.h" #include +#include #include -#include #define PRINTF_NUMERIC_BUF_LEN 50 diff --git a/kernel/main.c b/kernel/main.c index f600320..99954d6 100644 --- a/kernel/main.c +++ b/kernel/main.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -14,6 +15,7 @@ void kreport(void) memory_report(); acpi_report(); pci_report(); + gpu_report(); } void main(long magic, volatile void *mboot) diff --git a/kernel/term.c b/kernel/term.c new file mode 100644 index 0000000..e40e0fe --- /dev/null +++ b/kernel/term.c @@ -0,0 +1,169 @@ +#include "lib/kio.h" +#include +#include +#include +#include +#include +#include +#include + +// draw function +static void (*draw_char)(char c, uint16_t x, uint16_t y) = vga_draw_char; + +// terminal data +static char buffer[TERM_MAX_WIDTH * TERM_MAX_HEIGHT]; +static uint16_t buffer_line = UINT16_MAX; +static uint16_t width = VGA_WIDTH; +static uint16_t height = VGA_HEIGHT; +static uint16_t x = 0; +static uint16_t y = 0; + +#define BUFY(y) ((buffer_line + (y)) % TERM_MAX_HEIGHT) +#define BUFIDX(x,y) ((x) + (BUFY(y) * TERM_MAX_WIDTH)) + +static void buffer_check(void) +{ + if (buffer_line == UINT16_MAX) { + buffer_line = 0; + memset(buffer, 0, sizeof(buffer)); + } +} + +static inline bool printable(char c) +{ + switch (c) { + case '\0': + case '\n': + case '\t': + case '\v': + case '\f': + case '\r': + return false; + default: + return true; + } +} + +static inline void term_move_cur(char c) +{ + if (c == '\0') + return; + + switch (c) { + case '\n': + x = 0; + y++; + break; + case '\t': + x += 4; + break; + case '\v': + case '\f': + y++; + break; + case '\r': + x = 0; + break; + default: + x++; + } + + if (x >= width) { + x = 0; + y++; + } + if (y >= height) { + term_scroll(y - (height - 1)); + y = height - 1; + } +} + +uint16_t term_width(void) +{ + return width; +} + +uint16_t term_height(void) +{ + return height; +} + +void term_out(char c) +{ + term_out_at(c, x, y); + term_move_cur(c); +} + +void term_out_at(char c, uint16_t x, uint16_t y) +{ + buffer_check(); + + if (!printable(c)) + return; + + buffer[BUFIDX(x, y)] = c; + draw_char(c, x, y); +} + +void term_out_str(const char *str) +{ + char c; + while (c = *(str++), c) { + term_out_at(c, x, y); + term_move_cur(c); + } +} + +void term_out_str_at(const char *str, uint16_t x, uint16_t y) +{ + char c; + while (c = *(str++), c) + term_out_at(c, x, y); +} + +void term_clear(void) +{ + for (uint16_t y = 0; y < height; y++) + term_clear_line(y); +} + +void term_clear_line(uint16_t line) +{ + buffer_check(); + + if (line > height) + return; + + for (uint16_t x = 0; x < width; x++) { + buffer[BUFIDX(x, line)] = 0; + draw_char(0, x, line); + } +} + +void term_redraw(void) +{ + buffer_check(); + + for (uint16_t j = 0; j < height; j++) { + for (uint16_t i = 0; i < width; i++) { + char c = buffer[BUFIDX(i, j)]; + draw_char(c, i, j); + } + } +} + +void term_scroll(uint16_t lines) +{ + if (!lines) + return; + buffer_line += lines; + term_redraw(); +} + +void term_switch_handler(uint16_t w, uint16_t h, void (*fn)(char c, uint16_t x, uint16_t y)) +{ + draw_char = fn; + width = w % TERM_MAX_WIDTH; + height = h % TERM_MAX_HEIGHT; + term_redraw(); +} -- cgit v1.2.3-freya From 485c33eb10b4d877632676ee1bd8d8bdef821cfe Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Thu, 17 Apr 2025 09:26:49 -0400 Subject: fmt --- kernel/drivers/gpu.c | 4 +- kernel/drivers/gpu/bochs.c | 70 +++++++++++++++++--------------- kernel/include/comus/drivers/gpu/bochs.h | 3 +- kernel/include/comus/term.h | 3 +- kernel/term.c | 5 ++- 5 files changed, 46 insertions(+), 39 deletions(-) (limited to 'kernel/drivers') diff --git a/kernel/drivers/gpu.c b/kernel/drivers/gpu.c index 7ecdada..730fa42 100644 --- a/kernel/drivers/gpu.c +++ b/kernel/drivers/gpu.c @@ -13,9 +13,7 @@ enum gpu_type { static enum gpu_type type = GPU_UNSET; -static const char *gpu_type_str[N_GPU_TYPE] = { - "Unknown", "Bochs" -}; +static const char *gpu_type_str[N_GPU_TYPE] = { "Unknown", "Bochs" }; struct psf2_font { uint8_t magic[4]; diff --git a/kernel/drivers/gpu/bochs.c b/kernel/drivers/gpu/bochs.c index 5b0f1fc..a100908 100644 --- a/kernel/drivers/gpu/bochs.c +++ b/kernel/drivers/gpu/bochs.c @@ -4,27 +4,27 @@ #include #include -#define INDEX 0x1CE -#define DATA 0x1CF - -#define INDEX_ID 0 -#define INDEX_XRES 1 -#define INDEX_YRES 2 -#define INDEX_BPP 3 -#define INDEX_ENABLE 4 -#define INDEX_BANK 5 -#define INDEX_VIRT_WIDTH 6 -#define INDEX_VIRT_HEIGHT 7 -#define INDEX_X_OFFSET 8 -#define INDEX_Y_OFFSET 9 - -#define DATA_DISP_DISABLE 0x00 -#define DATA_DISP_ENABLE 0x01 -#define DATA_LFB_ENABLE 0x40 -#define DATA_NO_CLEAR_MEM 0x80 - -#define BOCHS_PCI_VENDOR 0x1234 -#define BOCHS_PCI_DEVICE 0x1111 +#define INDEX 0x1CE +#define DATA 0x1CF + +#define INDEX_ID 0 +#define INDEX_XRES 1 +#define INDEX_YRES 2 +#define INDEX_BPP 3 +#define INDEX_ENABLE 4 +#define INDEX_BANK 5 +#define INDEX_VIRT_WIDTH 6 +#define INDEX_VIRT_HEIGHT 7 +#define INDEX_X_OFFSET 8 +#define INDEX_Y_OFFSET 9 + +#define DATA_DISP_DISABLE 0x00 +#define DATA_DISP_ENABLE 0x01 +#define DATA_LFB_ENABLE 0x40 +#define DATA_NO_CLEAR_MEM 0x80 + +#define BOCHS_PCI_VENDOR 0x1234 +#define BOCHS_PCI_DEVICE 0x1111 #define BOCHS_WIDTH 1024 #define BOCHS_HEIGHT 768 @@ -35,33 +35,39 @@ static uint16_t width = BOCHS_WIDTH; static uint16_t height = BOCHS_HEIGHT; static uint8_t bit_depth = BOCHS_BIT_DEPTH; -static void write(uint16_t index, uint16_t data) { +static void write(uint16_t index, uint16_t data) +{ outw(INDEX, index); outw(DATA, data); } -static uint16_t read(uint16_t value) { +static uint16_t read(uint16_t value) +{ outw(INDEX, value); return inw(DATA); } -static int is_available(void) { +static int is_available(void) +{ return (read(INDEX_ID) == 0xB0C5); } -static void set_mode(uint16_t width, uint16_t height, uint16_t bit_depth, int lfb, int clear) { +static void set_mode(uint16_t width, uint16_t height, uint16_t bit_depth, + int lfb, int clear) +{ write(INDEX_ENABLE, DATA_DISP_DISABLE); write(INDEX_XRES, width); write(INDEX_YRES, height); write(INDEX_BPP, bit_depth); - write(INDEX_ENABLE, DATA_DISP_ENABLE | - (lfb ? DATA_LFB_ENABLE : 0) | - (clear ? 0 : DATA_NO_CLEAR_MEM)); + write(INDEX_ENABLE, DATA_DISP_ENABLE | (lfb ? DATA_LFB_ENABLE : 0) | + (clear ? 0 : DATA_NO_CLEAR_MEM)); } -int bochs_init(void) { - struct pci_device bochs = {0}; - bool found = pci_findby_id(&bochs, BOCHS_PCI_DEVICE, BOCHS_PCI_VENDOR, NULL); +int bochs_init(void) +{ + struct pci_device bochs = { 0 }; + bool found = + pci_findby_id(&bochs, BOCHS_PCI_DEVICE, BOCHS_PCI_VENDOR, NULL); if (!found) return 1; @@ -70,7 +76,7 @@ int bochs_init(void) { return 1; uint32_t bar0 = pci_rcfg_d(bochs, PCI_BAR0_D); - uint32_t *addr = (uint32_t *) (uintptr_t) bar0; + uint32_t *addr = (uint32_t *)(uintptr_t)bar0; framebuffer = kmapaddr(addr, NULL, width * height * bit_depth, F_WRITEABLE); return 0; diff --git a/kernel/include/comus/drivers/gpu/bochs.h b/kernel/include/comus/drivers/gpu/bochs.h index 5933a3a..31aa740 100644 --- a/kernel/include/comus/drivers/gpu/bochs.h +++ b/kernel/include/comus/drivers/gpu/bochs.h @@ -35,6 +35,7 @@ uint8_t bochs_bit_depth(void); /** * Sets the pixel at a given position */ -void bochs_set_pixel(uint32_t x, uint32_t y, uint32_t r, uint32_t g, uint32_t b); +void bochs_set_pixel(uint32_t x, uint32_t y, uint32_t r, uint32_t g, + uint32_t b); #endif /* bochs.h */ diff --git a/kernel/include/comus/term.h b/kernel/include/comus/term.h index 2d39915..6ac77e4 100644 --- a/kernel/include/comus/term.h +++ b/kernel/include/comus/term.h @@ -80,6 +80,7 @@ void term_scroll(uint16_t lines); /** * Switch terminal handler */ -void term_switch_handler(uint16_t width, uint16_t height, void (*draw_char)(char c, uint16_t x, uint16_t y)); +void term_switch_handler(uint16_t width, uint16_t height, + void (*draw_char)(char c, uint16_t x, uint16_t y)); #endif /* term.h */ diff --git a/kernel/term.c b/kernel/term.c index e40e0fe..101805f 100644 --- a/kernel/term.c +++ b/kernel/term.c @@ -19,7 +19,7 @@ static uint16_t x = 0; static uint16_t y = 0; #define BUFY(y) ((buffer_line + (y)) % TERM_MAX_HEIGHT) -#define BUFIDX(x,y) ((x) + (BUFY(y) * TERM_MAX_WIDTH)) +#define BUFIDX(x, y) ((x) + (BUFY(y) * TERM_MAX_WIDTH)) static void buffer_check(void) { @@ -160,7 +160,8 @@ void term_scroll(uint16_t lines) term_redraw(); } -void term_switch_handler(uint16_t w, uint16_t h, void (*fn)(char c, uint16_t x, uint16_t y)) +void term_switch_handler(uint16_t w, uint16_t h, + void (*fn)(char c, uint16_t x, uint16_t y)) { draw_char = fn; width = w % TERM_MAX_WIDTH; -- cgit v1.2.3-freya