initrd module

This commit is contained in:
rami 2024-07-03 15:42:01 -04:00
parent c35191e819
commit 3fd90c7483
8 changed files with 47 additions and 23 deletions

View File

@ -34,7 +34,8 @@ $(BUILDDIR)/%.o: */%.c
kernel: $(BUILDDIR)/$(KIMG) kernel: $(BUILDDIR)/$(KIMG)
$(BUILDDIR)/$(ISO): kernel $(BUILDDIR)/$(ISO): kernel
rm -f $(BUILDDIR)/$(ISO) rm -f $(BUILDDIR)/$(ISO) boot/initrd
nasm -fbin user/userinit.asm -o boot/initrd
cp $(BUILDDIR)/$(KIMG) boot/$(KIMG) cp $(BUILDDIR)/$(KIMG) boot/$(KIMG)
grub-mkrescue -o $(BUILDDIR)/$(ISO) . grub-mkrescue -o $(BUILDDIR)/$(ISO) .
iso: $(BUILDDIR)/$(ISO) iso: $(BUILDDIR)/$(ISO)
@ -42,4 +43,4 @@ iso: $(BUILDDIR)/$(ISO)
qemu: iso qemu: iso
qemu-system-i386 $(QEMUFLAGS) qemu-system-i386 $(QEMUFLAGS)
clean: clean:
rm -f build/* boot/*.bin rm -f build/* boot/*.bin boot/initrd

View File

@ -2,4 +2,5 @@ default=0
timeout=0 timeout=0
menuentry "Hazel" { menuentry "Hazel" {
multiboot /boot/kernel.bin multiboot /boot/kernel.bin
module /boot/initrd
} }

1
boot/initrd Normal file
View File

@ -0,0 +1 @@
<EFBFBD>ュ゙<EFBFBD>

View File

@ -29,10 +29,8 @@ typedef struct {
struct multiboot_mmap_entry struct multiboot_mmap_entry
{ {
uint32_t size; uint32_t size;
uint32_t addr_low; uint64_t addr;
uint32_t addr_high; uint64_t len;
uint32_t len_low;
uint32_t len_high;
#define MULTIBOOT_MEMORY_AVAILABLE 1 #define MULTIBOOT_MEMORY_AVAILABLE 1
#define MULTIBOOT_MEMORY_RESERVED 2 #define MULTIBOOT_MEMORY_RESERVED 2
#define MULTIBOOT_MEMORY_ACPI_RECLAIMABLE 3 #define MULTIBOOT_MEMORY_ACPI_RECLAIMABLE 3
@ -42,5 +40,19 @@ struct multiboot_mmap_entry
} __attribute__((packed)); } __attribute__((packed));
typedef struct multiboot_mmap_entry multi_mmap_t; typedef struct multiboot_mmap_entry multi_mmap_t;
struct multiboot_mod_list
{
/* the memory used goes from bytes mod_start to mod_end-1 inclusive */
uint32_t mod_start;
uint32_t mod_end;
/* Module command line */
uint32_t cmdline;
/* padding to take it to 16 bytes (must be zero) */
uint32_t pad;
};
typedef struct multiboot_mod_list multi_mod_t;
#endif #endif

View File

@ -71,8 +71,7 @@ jmp_user_mode:
push eax push eax
pushf pushf
push (3*8) | 3 push (3*8) | 3
extern test_entry push edx
push test_entry
iret iret

View File

@ -32,6 +32,7 @@ void sleep(int delay) {
} }
void kernel(multiboot_info_t *info) { void kernel(multiboot_info_t *info) {
if (!CHECK_FLAG(info->flags, 3)) goto halt; // Modules
if (!CHECK_FLAG(info->flags, 6)) goto halt; // Memory map if (!CHECK_FLAG(info->flags, 6)) goto halt; // Memory map
if (!CHECK_FLAG(info->flags, 12)) goto halt; // VBE data if (!CHECK_FLAG(info->flags, 12)) goto halt; // VBE data
ctx.multi_mmap = (multi_mmap_t *)info->memmapaddress; ctx.multi_mmap = (multi_mmap_t *)info->memmapaddress;
@ -58,7 +59,10 @@ void kernel(multiboot_info_t *info) {
ctx.ticks = 0; ctx.ticks = 0;
//pit_init(); //pit_init();
jmp_user_mode(0x5000, (uint32_t)test_entry); uint32_t stack = (uint32_t)mmap_find_first_free_block();
multi_mod_t *init = (multi_mod_t *)info->moduleaddress;
LOG("Switching to ring 3... EIP: 0x%08X ESP: 0x%08X", init->mod_start, stack+0x1000);
jmp_user_mode(stack+0x1000, init->mod_start);
halt: halt:
for (;;) {} for (;;) {}

View File

@ -9,10 +9,10 @@ void *mmap_block_to_physical(int block) {
for (uint32_t i = 0; i < ctx.multi_mmap_size; i++) { for (uint32_t i = 0; i < ctx.multi_mmap_size; i++) {
struct multiboot_mmap_entry entry = ctx.multi_mmap[i]; struct multiboot_mmap_entry entry = ctx.multi_mmap[i];
if (entry.type != MULTIBOOT_MEMORY_AVAILABLE) continue; if (entry.type != MULTIBOOT_MEMORY_AVAILABLE || entry.len > UINT32_MAX || entry.addr > UINT32_MAX) continue;
uint32_t len = (uint32_t)(entry.len_low | entry.len_high); uint32_t len = (uint32_t)(entry.len);
uint32_t addr = (uint32_t)(entry.addr_low | entry.addr_high); uint32_t addr = (uint32_t)(entry.addr);
if (len > offset) if (len > offset)
return (void *)(addr + offset); return (void *)(addr + offset);
} }
@ -40,10 +40,11 @@ uint8_t mmap_init(void) {
uint64_t available_bytes = 0; uint64_t available_bytes = 0;
for (uint32_t i = 0; i < ctx.multi_mmap_size; i++) { for (uint32_t i = 0; i < ctx.multi_mmap_size; i++) {
struct multiboot_mmap_entry entry = ctx.multi_mmap[i]; struct multiboot_mmap_entry entry = ctx.multi_mmap[i];
if (entry.type != MULTIBOOT_MEMORY_AVAILABLE) continue; if (entry.type != MULTIBOOT_MEMORY_AVAILABLE || entry.len > UINT32_MAX || entry.addr > UINT32_MAX) continue;
uint32_t len = (uint32_t)(entry.len_low | entry.len_high); //uint32_t len = (uint32_t)(entry.len_low | entry.len_high);
available_bytes += len; available_bytes += entry.len;
LOG("Entry: Addr: %016llX -- Len: %016llX\n", (long long)entry.addr, (long long)entry.len);
} }
ctx.mmap_size = available_bytes / BLOCK_SIZE / 8; ctx.mmap_size = available_bytes / BLOCK_SIZE / 8;
@ -52,10 +53,10 @@ uint8_t mmap_init(void) {
int index = -1; int index = -1;
for (uint32_t i = 0; i < ctx.multi_mmap_size; i++) { for (uint32_t i = 0; i < ctx.multi_mmap_size; i++) {
struct multiboot_mmap_entry entry = ctx.multi_mmap[i]; struct multiboot_mmap_entry entry = ctx.multi_mmap[i];
if (entry.type != MULTIBOOT_MEMORY_AVAILABLE) continue; if (entry.type != MULTIBOOT_MEMORY_AVAILABLE || entry.len > UINT32_MAX || entry.addr > UINT32_MAX) continue;
uint32_t len = (uint32_t)(entry.len_low | entry.len_high); //uint32_t len = (uint32_t)(entry.len_low | entry.len_high);
if (len > ctx.mmap_size) { if (entry.len > ctx.mmap_size) {
index = i; index = i;
break; break;
} }
@ -65,8 +66,8 @@ uint8_t mmap_init(void) {
// Create map // Create map
struct multiboot_mmap_entry entry = ctx.multi_mmap[index]; struct multiboot_mmap_entry entry = ctx.multi_mmap[index];
uint32_t addr = (uint32_t)(entry.addr_low | entry.addr_high); //uint32_t addr = (uint32_t)(entry.addr_low | entry.addr_high);
ctx.mmap = (uint32_t *)addr; ctx.mmap = (uint32_t *)(uint32_t)(entry.addr & 0xffffffff);
// Zero the map // Zero the map
memset(ctx.mmap, 0, ctx.mmap_size); memset(ctx.mmap, 0, ctx.mmap_size);
// Reserve the blocks that hold the memory map + 1 // Reserve the blocks that hold the memory map + 1

5
user/userinit.asm Normal file
View File

@ -0,0 +1,5 @@
[bits 32]
global _start
_start:
mov eax, 0xDEADBEEF
jmp $