summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordzwdz2022-08-15 23:03:18 +0200
committerdzwdz2022-08-15 23:03:18 +0200
commit63fd7ce362c7f1d59365045f19cf1ca87ffe2db9 (patch)
treed991957f52e74afebe88a1ec88ec0d371728160b /src
parent3e80780859c4cbe5223ca4329bfd6176f5c2c879 (diff)
kernel: combine kmain_early and kmain
Diffstat (limited to 'src')
-rw-r--r--src/kernel/arch/amd64/boot.c70
-rw-r--r--src/kernel/arch/generic.h2
-rw-r--r--src/kernel/main.c20
-rw-r--r--src/kernel/main.h17
-rw-r--r--src/kernel/mem/alloc.c31
-rw-r--r--src/kernel/mem/alloc.h4
6 files changed, 59 insertions, 85 deletions
diff --git a/src/kernel/arch/amd64/boot.c b/src/kernel/arch/amd64/boot.c
index a03c21d..e92b040 100644
--- a/src/kernel/arch/amd64/boot.c
+++ b/src/kernel/arch/amd64/boot.c
@@ -1,3 +1,4 @@
+#include <kernel/arch/amd64/3rdparty/multiboot2.h>
#include <kernel/arch/amd64/boot.h>
#include <kernel/arch/amd64/driver/fsroot.h>
#include <kernel/arch/amd64/driver/pata.h>
@@ -6,12 +7,11 @@
#include <kernel/arch/amd64/driver/video.h>
#include <kernel/arch/amd64/interrupts/idt.h>
#include <kernel/arch/amd64/interrupts/irq.h>
-#include <kernel/arch/amd64/3rdparty/multiboot2.h>
#include <kernel/arch/amd64/tty/tty.h>
#include <kernel/arch/generic.h>
-#include <kernel/main.h>
#include <kernel/mem/alloc.h>
#include <kernel/panic.h>
+#include <kernel/proc.h>
static void *mbi_tag(void *mbi, uint32_t type) {
struct multiboot_tag *tag;
@@ -27,8 +27,10 @@ static void *mbi_tag(void *mbi, uint32_t type) {
}
void kmain_early(void *mbi) {
- struct kmain_info info;
struct fb_info vid;
+ struct {
+ void *addr; size_t len;
+ } init;
tty_init();
kprintf("idt...");
@@ -36,44 +38,50 @@ void kmain_early(void *mbi) {
kprintf("irq...");
irq_init();
- struct multiboot_tag_basic_meminfo *meminfo;
- meminfo = mbi_tag(mbi, MULTIBOOT_TAG_TYPE_BASIC_MEMINFO);
- info.memtop = (void*)(long)(meminfo->mem_upper * 1024);
-
- struct multiboot_tag_framebuffer *framebuf;
- framebuf = mbi_tag(mbi, MULTIBOOT_TAG_TYPE_FRAMEBUFFER);
- vid.b = (void*)framebuf->common.framebuffer_addr;
- vid.pitch = framebuf->common.framebuffer_pitch;
- vid.width = framebuf->common.framebuffer_width;
- vid.height = framebuf->common.framebuffer_height;
- vid.bpp = framebuf->common.framebuffer_bpp;
- vid.size = vid.pitch * vid.height;
- // TODO framebuffer doesn't need to be part of kmain_info
- info.fb.at = vid.b;
- info.fb.size = vid.size;
+ {
+ kprintf("mem...\n");
+ struct multiboot_tag_basic_meminfo *meminfo;
+ meminfo = mbi_tag(mbi, MULTIBOOT_TAG_TYPE_BASIC_MEMINFO);
+ mem_init((void*)(long)(meminfo->mem_upper * 1024));
+ }
- struct multiboot_tag_module *init;
- init = mbi_tag(mbi, MULTIBOOT_TAG_TYPE_MODULE);
- info.init.at = (void*)(long)init->mod_start;
- info.init.size = init->mod_end - init->mod_start;
+ {
+ struct multiboot_tag_framebuffer *framebuf;
+ framebuf = mbi_tag(mbi, MULTIBOOT_TAG_TYPE_FRAMEBUFFER);
+ vid.b = (void*)framebuf->common.framebuffer_addr;
+ vid.pitch = framebuf->common.framebuffer_pitch;
+ vid.width = framebuf->common.framebuffer_width;
+ vid.height = framebuf->common.framebuffer_height;
+ vid.bpp = framebuf->common.framebuffer_bpp;
+ vid.size = vid.pitch * vid.height;
+ mem_reserve(vid.b, vid.size);
+ kprintf("framebuffer at 0x%x, %ux%u bpp %u\n", vid.b, vid.width, vid.height, vid.bpp);
+ }
- kprintf("mem...\n");
- mem_init(&info);
+ {
+ struct multiboot_tag_module *mod;
+ mod = mbi_tag(mbi, MULTIBOOT_TAG_TYPE_MODULE);
+ init.addr = (void*)(long)mod->mod_start;
+ init.len = mod->mod_end - mod->mod_start;
+ mem_reserve(init.addr, init.len);
+ }
kprintf("rootfs...\n");
vfs_root_init();
ps2_init();
serial_init();
+ video_init(vid);
kprintf("ata...\n");
pata_init();
- kprintf("kernel %8x -> %8x\n", 0, &_bss_end);
- kprintf("init %8x -> %8x\n", info.init.at, info.init.at + info.init.size);
- kprintf("video %8x -> %8x\n", vid.b, vid.b + vid.size);
- kprintf("limit %8x\n", info.memtop);
- kprintf("framebuffer at 0x%x, %ux%u bpp %u\n", vid.b, vid.width, vid.height, vid.bpp);
- video_init(vid);
+ kprintf("running init...\n");
+ process_seed(init.addr, init.len);
+ process_switch_any();
+}
- kmain(info);
+void shutdown(void) {
+ kprintf("shutting off\n");
+ mem_debugprint();
+ cpu_shutdown();
}
diff --git a/src/kernel/arch/generic.h b/src/kernel/arch/generic.h
index 0b7b2f2..b213962 100644
--- a/src/kernel/arch/generic.h
+++ b/src/kernel/arch/generic.h
@@ -20,6 +20,8 @@ __attribute__((noreturn))
void cpu_halt(void);
__attribute__((noreturn))
void cpu_shutdown(void);
+__attribute__((noreturn))
+void shutdown(void);
/** on x86: waits for an IRQ */
void cpu_pause(void);
diff --git a/src/kernel/main.c b/src/kernel/main.c
deleted file mode 100644
index ef7acee..0000000
--- a/src/kernel/main.c
+++ /dev/null
@@ -1,20 +0,0 @@
-#include <kernel/arch/generic.h>
-#include <kernel/main.h>
-#include <kernel/mem/alloc.h>
-#include <kernel/proc.h>
-#include <kernel/util.h>
-#include <stdint.h>
-
-void kmain(struct kmain_info info) {
- kprintf("loading init...\n");
- process_seed(info.init.at, info.init.size);
-
- kprintf("switching...\n");
- process_switch_any();
-}
-
-void shutdown(void) {
- kprintf("shutting off\n");
- mem_debugprint();
- cpu_shutdown();
-}
diff --git a/src/kernel/main.h b/src/kernel/main.h
deleted file mode 100644
index 45fd85e..0000000
--- a/src/kernel/main.h
+++ /dev/null
@@ -1,17 +0,0 @@
-#pragma once
-#include <stddef.h>
-
-struct kmain_info {
- struct {
- void *at; /* page aligned */
- size_t size;
- } init; /* a boot module loaded by GRUB, containing the initrd driver */
- struct {
- void *at;
- size_t size;
- } fb; /* the framebuffer */
- void *memtop;
-};
-
-void kmain(struct kmain_info);
-void shutdown(void);
diff --git a/src/kernel/mem/alloc.c b/src/kernel/mem/alloc.c
index 7270021..3ed49d6 100644
--- a/src/kernel/mem/alloc.c
+++ b/src/kernel/mem/alloc.c
@@ -36,29 +36,30 @@ static void bitmap_set(size_t i, bool v) {
else pbitmap[b] &= ~m;
}
-static void bitmap_mark(void *start, size_t len) {
+void mem_init(void *memtop) {
+ kprintf("memory %8x -> %8x\n", &_bss_end, memtop);
+ size_t pageamt = ((uintptr_t)memtop - (uintptr_t)pbitmap_start) / PAGE_SIZE;
+ pbitmap_len = pageamt / 8;
+ memset(pbitmap, 0, pbitmap_len);
+ mem_reserve(pbitmap, pbitmap_len);
+}
+
+void mem_reserve(void *addr, size_t len) {
+ kprintf("reserved %8x -> %8x\n", addr, addr + len);
+
/* align to the previous page */
- size_t off = (uintptr_t)start & PAGE_MASK;
- start -= off;
+ size_t off = (uintptr_t)addr & PAGE_MASK;
+ addr -= off;
len += off;
- size_t first = ((uintptr_t)start - (uintptr_t)pbitmap_start) / PAGE_SIZE;
+ size_t first = ((uintptr_t)addr - (uintptr_t)pbitmap_start) / PAGE_SIZE;
for (size_t i = 0; i * PAGE_SIZE < len; i++) {
if (first + i >= pbitmap_len) break;
+ if (bitmap_get(first + i))
+ panic_invalid_state();
bitmap_set(first + i, true);
}
}
-void mem_init(struct kmain_info *info) {
- size_t pageamt = ((uintptr_t)info->memtop - (uintptr_t)pbitmap_start) / PAGE_SIZE;
- pbitmap_len = pageamt / 8;
- memset(pbitmap, 0, pbitmap_len);
- bitmap_mark(pbitmap, pbitmap_len);
-
- // TODO make boot.c handle this instead
- bitmap_mark(info->init.at, info->init.size);
- bitmap_mark(info->fb.at, info->fb.size);
-}
-
void mem_debugprint(void) {
size_t total = 0;
kprintf("current kmallocs:\n");
diff --git a/src/kernel/mem/alloc.h b/src/kernel/mem/alloc.h
index 14c4361..fa2798a 100644
--- a/src/kernel/mem/alloc.h
+++ b/src/kernel/mem/alloc.h
@@ -1,11 +1,11 @@
#pragma once
#include <kernel/arch/generic.h>
-#include <kernel/main.h>
#include <stddef.h>
extern struct malloc_hdr *malloc_last;
-void mem_init(struct kmain_info *);
+void mem_init(void *memtop);
+void mem_reserve(void *addr, size_t len);
void mem_debugprint(void);
// allocates `pages` consecutive pages