diff --git a/Makefile b/Makefile index a6494ce..0198c9c 100644 --- a/Makefile +++ b/Makefile @@ -106,6 +106,7 @@ override HEADER_DEPS := $(addprefix obj/,$(CFILES:.c=.c.d) $(ASFILES:.S=.S.d)) .PHONY: all all: bin/$(OUTPUT) sh limine.sh + sh run.sh # Include header dependencies. -include $(HEADER_DEPS) diff --git a/bin/os b/bin/os index 635d259..580a4cf 100755 Binary files a/bin/os and b/bin/os differ diff --git a/image.hdd b/image.hdd index d999482..05858a1 100644 Binary files a/image.hdd and b/image.hdd differ diff --git a/limine.sh b/limine.sh index c386731..0e26ae1 100644 --- a/limine.sh +++ b/limine.sh @@ -5,9 +5,9 @@ dd if=/dev/zero bs=1M count=0 seek=64 of=image.hdd sgdisk image.hdd -n 1:2048 -t 1:ef00 -m 1 -rm -rf limine +#rm -rf limine # If first time: Download the latest Limine binary release for the 10.x branch. -git clone https://codeberg.org/Limine/Limine.git limine --branch=v10.x-binary --depth=1 +#git clone https://codeberg.org/Limine/Limine.git limine --branch=v10.x-binary --depth=1 # Build "limine" utility. make -C limine diff --git a/obj/src/int.c.d b/obj/src/int.c.d index 6d7ae4b..21bcf73 100644 --- a/obj/src/int.c.d +++ b/obj/src/int.c.d @@ -1,2 +1,3 @@ -obj/src/int.c.o: src/int.c src/common.h +obj/src/int.c.o: src/int.c src/print.h src/common.h +src/print.h: src/common.h: diff --git a/obj/src/int.c.o b/obj/src/int.c.o index f0a1663..1c2c7ea 100644 Binary files a/obj/src/int.c.o and b/obj/src/int.c.o differ diff --git a/obj/src/main.c.d b/obj/src/main.c.d index b94abe3..eb63212 100644 --- a/obj/src/main.c.d +++ b/obj/src/main.c.d @@ -1,5 +1,6 @@ -obj/src/main.c.o: src/main.c src/../include/limine.h src/common.h \ - src/print.h +obj/src/main.c.o: src/main.c src/../include/limine.h src/acpi.h \ + src/common.h src/print.h src/../include/limine.h: +src/acpi.h: src/common.h: src/print.h: diff --git a/obj/src/main.c.o b/obj/src/main.c.o index 5054645..24544ff 100644 Binary files a/obj/src/main.c.o and b/obj/src/main.c.o differ diff --git a/run.sh b/run.sh new file mode 100644 index 0000000..dbed59c --- /dev/null +++ b/run.sh @@ -0,0 +1 @@ +qemu-system-x86_64 image.hdd diff --git a/src/acpi.c b/src/acpi.c index 1c2992b..7cf267c 100644 --- a/src/acpi.c +++ b/src/acpi.c @@ -1,46 +1,26 @@ #include "common.h" +#include "print.h" -// root system description pointer, points -pstruct acpi_rsdp_header { - u8 signature[8]; - u8 checksum; - u8 OEMID[6]; - u8 revision; - u32 addr32_depr; - // v2 - u32 len; - u64 addr; - u8 extended_checksum; - u8 resv[3]; -}; +#include "../include/limine.h" +// credit to dreamportdev's OSDev-Notes and the OSDev Wiki for table layouts + +#include "acpi.h" #define RSDT_SIGNATURE "RSDT" #define XSDT_SIGNATURE "XSDT" -struct acpi_sdt_header { - u8 signature[4]; - u32 len; - u8 revision; - u8 checksum; - u8 OEMID[6]; - u8 OEM_table_id[8]; - u32 OEM_revision; - u32 creator_id; - u32 creator_revision; -}; +bool rsdp_validate(const u8* data) { + if(memcmp(data, (const u8*)RSDP_SIGNATURE, 8) != 0) { + /* to debug in memcmp is broken somehow */ + if(data[0] == 'R' && data[1] == 'S' && data[2] == 'D' && data[3] == 'T' && data[4] == ' ') { + printn((const char*)data, 8); + } + return false; + } -struct apci_rsdt { - struct acpi_sdt_header header; - u32 addrs[]; -}; - -struct xsdt { - struct acpi_rsdp_header header; - u64 addrs[]; -}; - - -bool rsdp_validate(u8* data, size_t len) { + struct acpi_rsdp_header* head = (struct acpi_rsdp_header*)data; + size_t len = sizeof(struct acpi_rsdp_header); + len -= (head->revision == 2) * sizeof(u32) + sizeof(u64) + sizeof(u8) + sizeof(u8[3]); u32 sum_to_zero = 0; for(size_t i = 0; i < len; i++) { sum_to_zero += data[i]; @@ -49,4 +29,13 @@ bool rsdp_validate(u8* data, size_t len) { return (sum_to_zero & 0xFF) == 0; } +static bool use_xsdt = false; +static struct acpi_rsdt* rsdt; +static struct acpi_xsdt* xsdt; + +struct acpi_sdt_header* acpi_get_header(size_t n) { + return (struct acpi_sdt_header*) (use_xsdt? xsdt->addrs[n] : (u64)rsdt->addrs[n]); +} + +struct acpi_rsdp_header* rsdp; diff --git a/src/acpi.h b/src/acpi.h new file mode 100644 index 0000000..5cb5127 --- /dev/null +++ b/src/acpi.h @@ -0,0 +1,46 @@ +#pragma once + +#include "common.h" + + +#define RSDP_SIGNATURE "RSDT PTR " + + +pstruct acpi_rsdp_header { + u8 signature[8]; + u8 checksum; + u8 OEMID[6]; + u8 revision; + u32 addr32_depr; + // v2 + u32 len; + u64 addr; + u8 extended_checksum; + u8 resv[3]; +}; + +struct acpi_sdt_header { + u8 signature[4]; + u32 len; + u8 revision; + u8 checksum; + u8 OEMID[6]; + u8 OEM_table_id[8]; + u32 OEM_revision; + u32 creator_id; + u32 creator_revision; +}; + +struct acpi_rsdt { + // signature == "RSDT" + struct acpi_sdt_header header; + u32 addrs[]; +}; + +struct acpi_xsdt { + // signature == "XSDT" + struct acpi_rsdp_header header; + u64 addrs[]; +}; + +extern struct acpi_rsdp_header* rsdp; diff --git a/src/common.h b/src/common.h index 1ff02c0..ec1c474 100644 --- a/src/common.h +++ b/src/common.h @@ -15,3 +15,18 @@ typedef int16_t i16; typedef int32_t i32; typedef int64_t i64; +static inline void INT(int n) { + asm volatile ( + "int %0" + : + : "i"(n) + ); +} + + +void* memcpy(void* restrict dst, const void* src, size_t n); +void* memset(void* s, int c, size_t n); +void* memmove(void* dst, const void* src, size_t n); +int memcmp(const void* s1, const void* s2, size_t n); + +extern u64 hhdm_offset; diff --git a/src/int.c b/src/int.c index 3620f31..5364253 100644 --- a/src/int.c +++ b/src/int.c @@ -1,7 +1,14 @@ #include +// for debugging +#include "print.h" + #include "common.h" + +// for trap debugging +u64 dbg_var = 0; + pstruct int_desc { u16 addr_low; u16 selector; @@ -22,6 +29,8 @@ enum INT_DESC_FLAGS { #define CODE64_SELECTOR 0x28 +static int cnt = 0; + struct int_desc idt[256]; void set_idt(u8 vect, void* handler, u8 dpl) { @@ -29,7 +38,7 @@ void set_idt(u8 vect, void* handler, u8 dpl) { u64 addr = (u64) handler; ent->addr_low = addr & 0xFFFF; ent->addr_mid = (addr >> 16) & 0xFFFF; - ent->addr_mid = addr >> 32; + ent->addr_high = addr >> 32; ent->selector = CODE64_SELECTOR; ent->flags = ID_GATE_INT | dpl*ID_DPL | ID_PRESENT; @@ -68,12 +77,16 @@ struct cpu_ctx { }; struct cpu_ctx* interrupt_dispatch(struct cpu_ctx* ctx) { - draw(ctx->vect); - + print("\n[IRQ "); + print8(ctx->vect); + print("] with message: "); + print64(dbg_var); + print("\n"); + while(1); return ctx; } - +__attribute__((naked)) void int_stub(void) { __asm__ volatile ( "push %%rax\n" @@ -108,7 +121,7 @@ void int_stub(void) { "pop %%rbx\n" "pop %%rax\n" "add $16, %%rsp\n" - "iret\n" + "iretq\n" : : : "memory" diff --git a/src/main.c b/src/main.c index 5100806..88d3396 100644 --- a/src/main.c +++ b/src/main.c @@ -2,8 +2,9 @@ #include #include - #include "../include/limine.h" + +#include "acpi.h" #include "common.h" #include "print.h" @@ -18,6 +19,27 @@ static volatile struct limine_framebuffer_request fb_req = { .revision = 0 }; + +__attribute__((used, section(".limine_requests"))) +static volatile struct limine_memmap_request mem_req = { + .id = LIMINE_MEMMAP_REQUEST_ID, + .revision = 0, +}; + +__attribute__((used, section(".limine_requests"))) +static volatile struct limine_hhdm_request hhdm_req = { + .id = LIMINE_HHDM_REQUEST_ID, + .revision = 0 +}; + +__attribute__((used, section(".limine_requests"))) +static volatile struct limine_rsdp_request rsdp_req = { + .id = LIMINE_RSDP_REQUEST_ID, + .revision = 0 +}; + +u64 hhdm_offset; + __attribute__((used, section(".limine_requests_start"))) static volatile u64 limine_requests_start_marker[] = LIMINE_REQUESTS_START_MARKER; @@ -99,6 +121,53 @@ void draw(u8 vect) { extern void init_idt(void); +extern u32 fg; +extern u32 bg; +void print_memmap_entry(struct limine_memmap_entry* entry) { + u32 savefg = fg; + u32 savebg = bg; + static const char* type_strs[] = { + "Usable", + "Reserved", + "ACPI Reclaimable", + "ACPI NVS", + "BAD!!!", + "Bootloader Reclaimable", + "Executable and Modules", + "Framebuffer", + "ACPI Tables", + }; + + static u32 type_colors[] = { + 0x00AF00, + 0xAF1F00, + 0x7f7f00, + 0x7f7f00, + 0xFF0000, + 0x007f7f, + 0x408000, + 0x0000FF, + 0x00FF00 + }; + + bg = type_colors[entry->type]; + fg = ~bg; + + u64 addr = entry->base; + u64 end = addr + entry->length; + print("["); + print(type_strs[entry->type]); + print("]"); + print64(addr); + print(" - "); + print64(end); + print("\n"); + fg = savefg; + bg = savebg; +} + + + void kmain() { if(LIMINE_BASE_REVISION_SUPPORTED(limine_base_revision) == false) { hang(); @@ -110,12 +179,39 @@ void kmain() { fb = fb_req.response->framebuffers[0]; + print_init(fb); init_idt(); - print_init(fb); + if(!mem_req.response) { + print("Failed to get memory map :(\n)"); + hang(); + } - print("TEST THIS\nMessage for me please Mr Kernel"); + if(!hhdm_req.response) { + print("Failed to get higher half direct mapping\n."); + hang(); + } + hhdm_offset = hhdm_req.response->offset; + + if(!rsdp_req.response) { + print ("Failed to find rsdp\n."); + hang(); + } + + rsdp = (struct acpi_rsdp_header*)(rsdp_req.response->address); + + for(size_t i = 0; i < mem_req.response->entry_count; i++) { + print_memmap_entry(mem_req.response->entries[i]); + } + + print("Found RSDP @ 0x"); + print64((u64)rsdp); + put('\n'); + + print("With signature: "); + printn((char*)rsdp->signature, sizeof(rsdp->signature)); + hang(); } diff --git a/src/print.c b/src/print.c index fe8251d..4c9a70f 100644 --- a/src/print.c +++ b/src/print.c @@ -4,7 +4,7 @@ #include "../include/limine.h" u32 bg = 0x00000000; -u32 fg = 0x00030200; +u32 fg = (0x007F1754 << 1); static size_t row; static size_t col; static size_t term_width, term_height; @@ -71,7 +71,7 @@ void put(int ch) { for (size_t x = 0; x < TERM_WIDTH; x++) { size_t off = (py + y) * fb_width + (px + x); bool padding = (y == 0 || y == TERM_HEIGHT-1 || x == 0 || x == TERM_WIDTH-1); - fb_scr[off] = ((~glyph & 1) | padding )? bg : (x^y * off + y*term_width) * fg; + fb_scr[off] = ((~glyph & 1) | padding )? bg : fg; glyph >>= !padding; } } @@ -85,3 +85,31 @@ void print(const char* msg) { put(*(msg++)); } } + +void printn(const char* msg, size_t len) { + while(len-->0) { + put(*(msg++)); + } +} + +static u8 digits[] = "0123456789ABCDEF"; + +void print8(u8 n) { + put(digits[n>>4]); + put(digits[n&0xf]); +} + +void print16(u16 n) { + print8(n >> 8); + print8(n & 0xFF); +} + +void print32(u32 n) { + print16(n >> 16); + print16(n & 0xFFFF); +} + +void print64(u64 n) { + print32(n >> 32); + print32(n & 0xFFFFFFFF); +} diff --git a/src/print.h b/src/print.h index d7dd1c2..eee3faf 100644 --- a/src/print.h +++ b/src/print.h @@ -1,10 +1,16 @@ #pragma once +#include "common.h" + struct limine_framebuffer; void put(int ch); void print(const char* msg); - +void printn(const char* msg, size_t len); +void print64(u64 n); +void print32(u32 n); +void print16(u16 n); +void print8(u8 n); void print_init(struct limine_framebuffer* fb);