diff options
author | Marvin Borner | 2020-02-22 17:22:17 +0100 |
---|---|---|
committer | Marvin Borner | 2020-02-22 17:22:17 +0100 |
commit | add6efeb22ffb7695d5c9addcef073fc653f700e (patch) | |
tree | 147c99f2f3e037c877a34468f1494c412ec53416 | |
parent | 86ef6a779a42cf5701632ccb82714a006bae4ee9 (diff) |
Well basically nothing really works I guess
I've worked quite a while on several small things which I didn't commit but I'm going away for a week (holiday) soooo I synced these useless and dumb files :)
-rw-r--r-- | src/kernel/acpi/acpi.c | 5 | ||||
-rw-r--r-- | src/kernel/acpi/acpi.h | 30 | ||||
-rw-r--r-- | src/kernel/fs/elf.h | 53 | ||||
-rw-r--r-- | src/kernel/fs/elfloader.c | 64 | ||||
-rw-r--r-- | src/kernel/fs/load.c | 44 | ||||
-rw-r--r-- | src/kernel/fs/load.h | 16 | ||||
-rw-r--r-- | src/kernel/kernel.c | 12 | ||||
-rw-r--r-- | src/kernel/timer/timer.c | 2 |
8 files changed, 218 insertions, 8 deletions
diff --git a/src/kernel/acpi/acpi.c b/src/kernel/acpi/acpi.c index 60ed4f9..0122450 100644 --- a/src/kernel/acpi/acpi.c +++ b/src/kernel/acpi/acpi.c @@ -162,7 +162,10 @@ int acpi_install() } if (memcmp((unsigned int *) *ptr, "HPET", 4) == 0) { hpet = (struct HPET *) *ptr; - // serial_printf("%d", hpet->base_address); + serial_printf("%c%c%c%c", hpet->signature[0], hpet->signature[1], hpet->signature[2], + hpet->signature[3]); + serial_printf("%d", hpet->legacy_replacement); + serial_printf("%d", hpet->address.address); } ptr++; } // Else: no valid FADT present diff --git a/src/kernel/acpi/acpi.h b/src/kernel/acpi/acpi.h index 43cb61e..8544090 100644 --- a/src/kernel/acpi/acpi.h +++ b/src/kernel/acpi/acpi.h @@ -43,11 +43,35 @@ struct FADT { char century; }; +struct address_structure { + uint8_t address_space_id; + uint8_t register_bit_width; + uint8_t register_bit_offset; + uint8_t reserved; + uint64_t address; +} __attribute__((packed)); + struct HPET { char signature[4]; - char unneeded[36]; - char base_address[12]; -}; + uint32_t length; + uint8_t revision; + uint8_t checksum; + char oemid[6]; + uint64_t oem_tableid; + uint32_t oem_revision; + uint32_t creator_id; + uint32_t creator_revision; + uint8_t hardware_rev_id; + uint8_t comparator_count:5; + uint8_t counter_size:1; + uint8_t reserved:1; + uint8_t legacy_replacement:1; + uint16_t pci_vendor_id; + struct address_structure address; + uint8_t hpet_number; + uint16_t minimum_tick; + uint8_t page_protection; +} __attribute__((packed)); struct FADT *fadt; diff --git a/src/kernel/fs/elf.h b/src/kernel/fs/elf.h new file mode 100644 index 0000000..9be6b4d --- /dev/null +++ b/src/kernel/fs/elf.h @@ -0,0 +1,53 @@ +#ifndef MELVIX_ELF_H +#define MELVIX_ELF_H + +#include <stdint.h> + +typedef struct { + uint32_t sig; +} elf_priv_data; + +typedef struct { + unsigned char e_ident[16]; + uint16_t e_type; + uint16_t e_machine; + uint32_t e_version; + uint32_t e_entry; + uint32_t e_phoff; + uint32_t e_shoff; + uint32_t e_flags; + uint16_t e_ehsize; + uint16_t e_phentsize; + uint16_t e_phnum; + uint16_t e_shentsize; + uint16_t e_shnum; + uint16_t e_shstrndx; +} elf_header_t; + +typedef struct { + uint32_t sh_name; + uint32_t sh_type; + uint32_t sh_flags; + uint32_t sh_addr; + uint32_t sh_offset; + uint32_t sh_size; + uint32_t sh_link; + uint32_t sh_info; + uint32_t sh_addralign; + uint32_t sh_entsize; +} elf_section_header_t; + +typedef struct { + uint32_t p_type; + uint32_t p_offset; + uint32_t p_vaddr; + uint32_t p_paddr; + uint32_t p_filesz; + uint32_t p_memsz; + uint32_t p_flags; + uint32_t p_align; +} elf_program_header_t; + +void elf_init(); + +#endif diff --git a/src/kernel/fs/elfloader.c b/src/kernel/fs/elfloader.c new file mode 100644 index 0000000..5b81a5e --- /dev/null +++ b/src/kernel/fs/elfloader.c @@ -0,0 +1,64 @@ +#include <kernel/fs/elf.h> +#include <kernel/lib/stdio.h> +#include <kernel/memory/alloc.h> +#include <kernel/lib/lib.h> +#include <kernel/memory/paging.h> +#include "load.h" + +elf_priv_data *elf_probe(uint8_t *buf) +{ + elf_header_t *header = (elf_header_t *) buf; + /* The first four bytes are 0x7f and 'ELF' */ + if (header->e_ident[0] == 0x7f && + header->e_ident[1] == 'E' && header->e_ident[2] == 'L' && header->e_ident[3] == 'F') { + /* Valid ELF! */ + serial_printf("Buffer is valid ELF file!"); + return (void *) 1; + } + return 0; +} + +uint8_t elf_start(uint8_t *buf) +{ + elf_header_t *header = (elf_header_t *) buf; + serial_printf("Type: %s%s%s", + header->e_ident[4] == 1 ? "32bit " : "64 bit", + header->e_ident[5] == 1 ? "Little Endian " : "Big endian ", + header->e_ident[6] == 1 ? "True ELF " : "buggy ELF "); + if (header->e_type != 2) { + serial_printf("File is not executable!"); + return 0; + } + /* Map the virtual space */ + uint32_t phys_loc = loader_get_unused_load_location(); + uint32_t cr3 = kmalloc(4096); + /* Find first program header and loop through them */ + elf_program_header_t *ph = (elf_program_header_t *) (buf + header->e_phoff); + for (int i = 0; i < header->e_phnum; i++, ph++) { + switch (ph->p_type) { + case 0: /* NULL */ + break; + case 1: /* LOAD */ + serial_printf("LOAD: offset 0x%x vaddr 0x%x paddr 0x%x filesz 0x%x memsz 0x%x", + ph->p_offset, ph->p_vaddr, ph->p_paddr, ph->p_filesz, ph->p_memsz); + paging_map(phys_loc, ph->p_vaddr, PT_PRESENT | PT_RW | PT_USED); + memcpy(ph->p_vaddr, buf + ph->p_offset, ph->p_filesz); + break; + default: /* @TODO add more */ + serial_printf("Unsupported p_type! Bail out!"); + return 0; + } + } + + return 0;//START("elf32", header->e_entry); +} + +void elf_init() +{ + loader_t *elfloader = (loader_t *) kmalloc(sizeof(loader_t)); + elfloader->name = "ELF32"; + elfloader->probe = (void *) elf_probe; + elfloader->start = (void *) elf_start; + register_loader(elfloader); + // _kill(); +}
\ No newline at end of file diff --git a/src/kernel/fs/load.c b/src/kernel/fs/load.c index cd422d0..5a476b5 100644 --- a/src/kernel/fs/load.c +++ b/src/kernel/fs/load.c @@ -5,6 +5,8 @@ #include <kernel/system.h> #include <kernel/fs/iso9660/iso9660.h> #include <kernel/memory/alloc.h> +#include <kernel/lib/stdio.h> +#include <kernel/lib/lib.h> void load_binaries() { @@ -31,4 +33,46 @@ void load_binaries() kfree(user_e); } vga_log("Successfully loaded binaries"); +} + +#define MAX_LOADERS 16 + +loader_t **loaders = 0; +uint8_t last_loader = 0; + +uint32_t last_load_loc = 0x400000; + +void loader_init() +{ + serial_printf("Setting up loader"); + loaders = (loader_t **) kmalloc(MAX_LOADERS * sizeof(uint32_t)); + memset(loaders, 0, MAX_LOADERS * sizeof(uint32_t)); + // _kill(); +} + +void register_loader(loader_t *load) +{ + if (last_loader + 1 > MAX_LOADERS) return; + if (!load) return; + loaders[last_loader] = load; + last_loader++; + serial_printf("Registered %s loader as id %d", load->name, last_loader - 1); +} + +uint32_t loader_get_unused_load_location() +{ + last_load_loc += 0x400000; + return last_load_loc; +} + +uint8_t exec_start(uint8_t *buf) +{ + for (int i = 0; i < MAX_LOADERS; i++) { + if (!loaders[i]) break; + void *priv = loaders[i]->probe(buf); + if (priv) { + return loaders[i]->start(buf, priv); + } + } + return 0; }
\ No newline at end of file diff --git a/src/kernel/fs/load.h b/src/kernel/fs/load.h index 7a0ec06..4ba545f 100644 --- a/src/kernel/fs/load.h +++ b/src/kernel/fs/load.h @@ -16,4 +16,20 @@ struct font { void load_binaries(); +typedef struct { + char *name; + + void *(*probe)(uint8_t *buf); + + uint8_t (*start)(uint8_t *buf, void *priv); +} loader_t; + +void loader_init(); + +void register_loader(loader_t *load); + +uint8_t exec_start(uint8_t *buf); + +uint32_t loader_get_unused_load_location(); + #endif diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index bd6fac1..ec98a88 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -12,6 +12,8 @@ #include <kernel/net/network.h> #include <kernel/tasks/task.h> #include <kernel/fs/load.h> +#include <kernel/fs/elf.h> +#include <kernel/lib/stdio.h> void kernel_main(uint32_t initial_stack) { @@ -40,7 +42,7 @@ void kernel_main(uint32_t initial_stack) network_install(); asm ("sti"); - tasking_install(); + // tasking_install(); // Get hardware information // get_smbios(); @@ -55,8 +57,12 @@ void kernel_main(uint32_t initial_stack) install_melvix(); #endif - syscalls_install(); - exec(userspace); + loader_init(); + elf_init(); + exec_start((uint8_t *) userspace); + +// syscalls_install(); +// exec(userspace); panic("This should NOT happen!"); diff --git a/src/kernel/timer/timer.c b/src/kernel/timer/timer.c index 064d807..9bf6014 100644 --- a/src/kernel/timer/timer.c +++ b/src/kernel/timer/timer.c @@ -17,7 +17,7 @@ void timer_phase(int hz) void timer_handler(struct regs *r) { timer_ticks++; - switch_task(); + // switch_task(); } // "Delay" function with CPU sleep |