diff options
author | Lizzy Fleckenstein <lizzy@vlhl.dev> | 2024-04-11 20:58:38 +0200 |
---|---|---|
committer | Lizzy Fleckenstein <lizzy@vlhl.dev> | 2024-04-11 21:05:47 +0200 |
commit | 8b90c1f407b4f4aa3802858e23aa90d7dfbe17ad (patch) | |
tree | 4080e975e33df6f3c57ff5f3486f97923c9bbbe3 /stage3 | |
parent | a6669e496e46ef89673103b3330226c7d0201a1a (diff) | |
download | cuddles-8b90c1f407b4f4aa3802858e23aa90d7dfbe17ad.tar.xz |
bootinfo struct
Diffstat (limited to 'stage3')
-rw-r--r-- | stage3/bootinfo.h | 32 | ||||
-rw-r--r-- | stage3/cheese3d.c | 4 | ||||
-rw-r--r-- | stage3/font.c | 15 | ||||
-rw-r--r-- | stage3/fs.c | 3 | ||||
-rw-r--r-- | stage3/gfx.c | 15 | ||||
-rw-r--r-- | stage3/gfx.h | 7 | ||||
-rw-r--r-- | stage3/init.c | 8 | ||||
-rw-r--r-- | stage3/main.c | 75 | ||||
-rw-r--r-- | stage3/shell.c | 3 |
9 files changed, 99 insertions, 63 deletions
diff --git a/stage3/bootinfo.h b/stage3/bootinfo.h new file mode 100644 index 0000000..825f513 --- /dev/null +++ b/stage3/bootinfo.h @@ -0,0 +1,32 @@ +#ifndef BOOTINFO_H +#define BOOTINFO_H + +#include "def.h" + +typedef enum { + MEM_USABLE = 1, + MEM_RESERVED = 2, + MEM_ACPI_RECLAIMABLE = 3, + MEM_ACPI_NVS = 4, + MEM_BAD = 5, +} mem_region_type; + +typedef struct __attribute__((packed)) { + void *start; + usize size; + u32 type; + u32 acpi_attrs; +} mem_region; + +MKVEC(mem_region) + +extern struct __attribute__((packed)) bootinfo { + u64 ksize; + u16 gfx_pitch; + u16 gfx_width; + u16 gfx_height; + void *gfx_framebuffer; + slice_mem_region mmap; +} *bootinfo; + +#endif diff --git a/stage3/cheese3d.c b/stage3/cheese3d.c index 4d58a35..0c73f90 100644 --- a/stage3/cheese3d.c +++ b/stage3/cheese3d.c @@ -1,7 +1,7 @@ #include "heap.h" #include "math3d.h" #include "cheese3d.h" -#include "gfx.h" +#include "bootinfo.h" #include "memory.h" cheese3d_ctx cheese3d_create(void *target, u32 width, u32 height, u32 pitch, u32 bgcolor) @@ -19,7 +19,7 @@ cheese3d_ctx cheese3d_create(void *target, u32 width, u32 height, u32 pitch, u32 cheese3d_ctx cheese3d_create_default(u32 bgcolor) { - return cheese3d_create((void *) (u64) gfx_info->framebuffer, gfx_info->width, gfx_info->height, gfx_info->pitch, bgcolor); + return cheese3d_create(bootinfo->gfx_framebuffer, bootinfo->gfx_width, bootinfo->gfx_height, bootinfo->gfx_pitch, bgcolor); } void cheese3d_destroy(cheese3d_ctx ctx) diff --git a/stage3/font.c b/stage3/font.c index f0b8d93..4c99137 100644 --- a/stage3/font.c +++ b/stage3/font.c @@ -4,6 +4,7 @@ #include "heap.h" #include "memory.h" #include "math.h" +#include "bootinfo.h" #include "font_builtin.c" @@ -34,8 +35,8 @@ void font_set_size(u16 size) outer_width = CHAR_WIDTH * font_size; outer_height = CHAR_HEIGHT * font_size; - screen_width = gfx_info->width / outer_width; - screen_height = gfx_info->height / outer_height; + screen_width = bootinfo->gfx_width / outer_width; + screen_height = bootinfo->gfx_height / outer_height; } void font_set_cursor(term_pos new_cursor) @@ -95,7 +96,7 @@ void font_load_classic() void font_clear_screen() { cursor_x = cursor_y = 0; - gfx_set_area(0, 0, gfx_info->width, gfx_info->height, 0xFF000000); + gfx_set_area(0, 0, bootinfo->gfx_width, bootinfo->gfx_height, 0xFF000000); } static void render_char(u8 c) @@ -127,11 +128,11 @@ static void update_cursor() while (cursor_y >= screen_height) { cursor_y--; - lmemcpy((void *) (u64) gfx_info->framebuffer, - (void *) (u64) gfx_info->framebuffer + gfx_info->pitch * outer_height, - gfx_info->pitch * (gfx_info->height - outer_height)); + lmemcpy(bootinfo->gfx_framebuffer, + bootinfo->gfx_framebuffer + bootinfo->gfx_pitch * outer_height, + bootinfo->gfx_pitch * (bootinfo->gfx_height - outer_height)); - gfx_set_area(0, gfx_info->height-outer_height, gfx_info->width, outer_height, 0xFF000000); + gfx_set_area(0, bootinfo->gfx_height-outer_height, bootinfo->gfx_width, outer_height, 0xFF000000); } gfx_set_area(cursor_x * outer_width, cursor_y * outer_height, diff --git a/stage3/fs.c b/stage3/fs.c index d96d014..341a478 100644 --- a/stage3/fs.c +++ b/stage3/fs.c @@ -3,12 +3,13 @@ #include "string.h" #include "memory.h" #include "heap.h" +#include "bootinfo.h" #define FS_WALKER(X) bool (X)(str filename, u64 lba, usize fsize, usize fsect, void *varg) void fs_walk(FS_WALKER(*fun), void *arg) { - u64 lba = (*(u32 *) (0x1000-10-8))/512; + u64 lba = bootinfo->ksize/512; for (;;) { u8 *info = ata_read_full(lba, 1); diff --git a/stage3/gfx.c b/stage3/gfx.c index 7fe8df7..4c9fe4c 100644 --- a/stage3/gfx.c +++ b/stage3/gfx.c @@ -1,7 +1,6 @@ #include "gfx.h" #include "memory.h" - -struct GfxInfo *gfx_info = (void *) (0x1000-10); +#include "bootinfo.h" // byteswap u32 make_color(color col) @@ -15,17 +14,17 @@ u32 make_color(color col) void gfx_set_pixel(u16 x, u16 y, u32 col) { - u32 *out = (u32 *) (u64) (gfx_info->framebuffer + y * gfx_info->pitch + x * sizeof col); + u32 *out = bootinfo->gfx_framebuffer + y * bootinfo->gfx_pitch + x * sizeof col; *out = col; BARRIER_VAR(out); } void gfx_set_area(u16 x, u16 y, u16 w, u16 h, u32 col) { - void *cbeg = (void *) (u64) (gfx_info->framebuffer + y * gfx_info->pitch + x * sizeof col); - void *cend = cbeg + h * gfx_info->pitch; + void *cbeg = bootinfo->gfx_framebuffer + y * bootinfo->gfx_pitch + x * sizeof col; + void *cend = cbeg + h * bootinfo->gfx_pitch; - for (; cbeg < cend; cbeg += gfx_info->pitch) { + for (; cbeg < cend; cbeg += bootinfo->gfx_pitch) { u32 *rbeg = cbeg; u32 *rend = rbeg + w; @@ -38,8 +37,8 @@ void gfx_set_area(u16 x, u16 y, u16 w, u16 h, u32 col) void gfx_draw_img(u16 x, u16 y, u16 w, u16 h, u32 *img) { - void *cbeg = (void *) (u64) (gfx_info->framebuffer + y * gfx_info->pitch + x * sizeof(color)); - for (u16 yi = 0; yi < h; cbeg += gfx_info->pitch, yi++) + void *cbeg = bootinfo->gfx_framebuffer + y * bootinfo->gfx_pitch + x * sizeof(color); + for (u16 yi = 0; yi < h; cbeg += bootinfo->gfx_pitch, yi++) lmemcpy(cbeg, img + yi * w, w * sizeof(color)); BARRIER_VAR(cbeg); diff --git a/stage3/gfx.h b/stage3/gfx.h index 5d9c8b3..d81f54b 100644 --- a/stage3/gfx.h +++ b/stage3/gfx.h @@ -3,13 +3,6 @@ #include "def.h" -extern struct __attribute__((packed)) GfxInfo { - u16 pitch; - u16 width; - u16 height; - u32 framebuffer; -} *gfx_info; - typedef struct __attribute__((packed)) { u8 r, g, b, a; } color; diff --git a/stage3/init.c b/stage3/init.c index c65f08b..b49ba94 100644 --- a/stage3/init.c +++ b/stage3/init.c @@ -1,7 +1,9 @@ #include "def.h" -void kmain(); +#include "bootinfo.h" -void _start() +void kmain(struct bootinfo *info); + +void _start(struct bootinfo *info) { // enable SSE. long mode demands it is present u64 cr0; @@ -15,5 +17,5 @@ void _start() u16 fpu_cw = 0x37a; asm volatile("fldcw %0"::"m"(fpu_cw)); - kmain(); + kmain(info); } diff --git a/stage3/main.c b/stage3/main.c index e9b5717..b4ce692 100644 --- a/stage3/main.c +++ b/stage3/main.c @@ -1,5 +1,4 @@ #include "def.h" -#include "gfx.h" #include "halt.h" #include "heap.h" #include "font.h" @@ -12,21 +11,13 @@ #include "thread.h" #include "shell.h" #include "fs.h" -#include "gfx.h" #include "clock.h" #include "rng.h" #include "debug.h" +#include "bootinfo.h" -typedef enum { - MEM_USABLE = 1, - MEM_RESERVED = 2, -} mem_region_type; - -typedef struct __attribute__((packed)) { - void *start; - usize size; - usize type; -} mem_region; +#define T mem_region +#include "vec.h" char keymap[256] = { '\0' }; @@ -68,8 +59,12 @@ void keyboard_handler() } } -void kmain() +struct bootinfo *bootinfo; + +void kmain(struct bootinfo *info) { + bootinfo = info; + // PML3 for (u64 page = 0; page < 512*512; page++) ((u64 *) 0x200000)[page] = (page << 30) | 0b10000011; // bit 7 is for huge pages @@ -78,25 +73,29 @@ void kmain() for (u64 tbl = 0; tbl < 512; tbl++) ((u64 *) 0x1000)[tbl] = (0x200000 + tbl * 0x1000) | 0b11; -#define MMAP for (mem_region *mreg = (void *) 0x500; mreg->start != nil; mreg++) - - // heap init + // heap init and fill heap_init(); - MMAP { + ITER(bootinfo->mmap) { + mem_region *r = &bootinfo->mmap.data[i]; + if (r->type != MEM_USABLE || r->size == 0) + continue; + // remove anything between 0x100000 and 0x400000. it is used for kernel and page tables - usize start = (usize) mreg->start; + usize start = (usize) r->start; + usize size = r->size; + + if (start < 0x100000) + continue; + if (start >= 0x100000 && start < 0x400000) { - if (start + mreg->size <= 0x400000) { - mreg->size = 0; // kill it - } else { - mreg->size = start + mreg->size - 0x400000; - mreg->start = (void *) 0x400000; - } + if (start + size <= 0x400000) + continue; // skip + + size = start + size - 0x400000; + start = 0x400000; } - // add to heap - if (mreg->type == MEM_USABLE) // usable - heap_add(mreg->start, mreg->size); + heap_add((void *) start, size); } // font init @@ -108,20 +107,28 @@ void kmain() print(S("welcome to cuddles\n")); // memory map - print(S("heap memory:\n")); - MMAP { - print_num_pad((u64) mreg->start, 16, 16, ' '); + print(S("memory map:\n")); + ITER(bootinfo->mmap) { + mem_region *r = &bootinfo->mmap.data[i]; + print_num_pad((u64) r->start, 16, 16, ' '); print(S(" | ")); - print_num_pad((u64) mreg->start + mreg->size, 16, 16, ' '); + print_num_pad((u64) r->start + r->size, 16, 16, ' '); print(S(" | ")); - print_dec(mreg->type); + switch (r->type) { + case MEM_USABLE: print(S("usable")); break; + case MEM_RESERVED: print(S("reserved")); break; + case MEM_ACPI_RECLAIMABLE: print(S("acpi reclaimable")); break; + case MEM_ACPI_NVS: print(S("acpi nvs")); break; + case MEM_BAD: print(S("bad")); break; + default: print_dec(r->type); break; + } print(S("\n")); } print(S("gfx framebuffer at ")); - print_hex(gfx_info->framebuffer); + print_hex((u64) bootinfo->gfx_framebuffer); print(S("-")); - print_hex((u64) gfx_info->framebuffer + gfx_info->pitch * gfx_info->height); + print_hex((u64) bootinfo->gfx_framebuffer + bootinfo->gfx_pitch * bootinfo->gfx_height); print(S("\n")); u32 vendor[4]; diff --git a/stage3/shell.c b/stage3/shell.c index cf628ab..9fd0f93 100644 --- a/stage3/shell.c +++ b/stage3/shell.c @@ -12,6 +12,7 @@ #include "thread.h" #include "rng.h" #include "cheese_demo.h" +#include "bootinfo.h" static void cmd_echo(str arg) { @@ -82,7 +83,7 @@ static void cmd_img(str arg) if (f.len != 2 * sizeof(u32) + width * height * sizeof(color)) print(S("img: invalid file size\n")); else - gfx_draw_img(gfx_info->width-width, 0, width, height, + gfx_draw_img(bootinfo->gfx_width-width, 0, width, height, (void *) (f.data + 2 * sizeof(u32))); } |