I don't even know what to write at this point
This commit is contained in:
parent
df0ebbbbcd
commit
72a46bc7ce
12 changed files with 235 additions and 96 deletions
|
@ -20,7 +20,7 @@
|
|||
#include <vfs/vfs.h>
|
||||
#include <mm/mman.h>
|
||||
#include <mm/vmm.h>
|
||||
#include <loader/elf.h>
|
||||
#include <proto/aurix.h>
|
||||
#include <print.h>
|
||||
|
||||
void axboot_init()
|
||||
|
@ -31,22 +31,7 @@ void axboot_init()
|
|||
while (1);
|
||||
}
|
||||
|
||||
// read kernel -> test read
|
||||
char *kbuf = NULL;
|
||||
vfs_read("\\System\\axkrnl", &kbuf);
|
||||
|
||||
// TODO: Do something with the kernel :p
|
||||
uintptr_t *pm = create_pagemap();
|
||||
if (!pm) {
|
||||
debug("axboot_init(): Failed to create kernel pagemap! Halting...\n");
|
||||
// TODO: Halt
|
||||
while (1);
|
||||
}
|
||||
|
||||
void *kernel_entry = (void *)elf_load(kbuf, pm);
|
||||
(void)kernel_entry;
|
||||
|
||||
mem_free(kbuf);
|
||||
aurix_load("\\System\\axkrnl");
|
||||
|
||||
while (1);
|
||||
}
|
|
@ -28,7 +28,61 @@
|
|||
/* https://github.com/KevinAlavik/nekonix/blob/main/kernel/src/proc/elf.c */
|
||||
/* Thanks, Kevin <3 */
|
||||
|
||||
uint64_t elf_load(char *data, uintptr_t *pagemap)
|
||||
uintptr_t elf32_load(char *data, pagetable *pagemap)
|
||||
{
|
||||
(void)data;
|
||||
(void)pagemap;
|
||||
return 0;
|
||||
}
|
||||
|
||||
uintptr_t elf64_load(char *data, pagetable *pagemap)
|
||||
{
|
||||
struct elf_header *header = (struct elf_header *)data;
|
||||
struct elf_program_header *ph = (struct elf_program_header *)((uint8_t *)data + header->e_phoff);
|
||||
|
||||
uint64_t lowest = UINT64_MAX;
|
||||
uint64_t max_align = 0;
|
||||
|
||||
for (uint16_t i = 0; i < header->e_phnum; i++) {
|
||||
if (ph[i].p_type != PT_LOAD)
|
||||
continue;
|
||||
|
||||
if (ph[i].p_align > max_align) {
|
||||
max_align = ph[i].p_align;
|
||||
}
|
||||
}
|
||||
|
||||
for (uint16_t i = 0; i < header->e_phnum; i++) {
|
||||
if (ph[i].p_type != PT_LOAD)
|
||||
continue;
|
||||
|
||||
if ((ph[i].p_vaddr & (~(max_align - 1))) < lowest) {
|
||||
lowest = ph[i].p_vaddr & ~(max_align - 1);
|
||||
}
|
||||
|
||||
uint64_t flags = VMM_PRESENT;
|
||||
if (ph[i].p_flags & PF_W)
|
||||
flags |= VMM_WRITABLE;
|
||||
if (!(ph[i].p_flags & PF_X))
|
||||
flags |= VMM_NX;
|
||||
|
||||
debug("elf64_load(): phys=0x%llx, virt=0x%llx, size=%lu\n", ph[i].p_paddr, ph[i].p_vaddr, ph[i].p_filesz);
|
||||
|
||||
uint64_t phys = (uint64_t)mem_alloc(ph[i].p_memsz);
|
||||
if (!phys) {
|
||||
debug("elf64_load(): Out of memory\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
map_page(pagemap, ph[i].p_vaddr, phys, flags);
|
||||
memcpy((void*)ph[i].p_vaddr - lowest, data + ph[i].p_offset, ph[i].p_filesz);
|
||||
}
|
||||
|
||||
debug("elf64_load(): ELF loaded successfully, entry: 0x%llx\n", header->e_entry);
|
||||
return (uintptr_t)((uint8_t *)data + (header->e_entry - lowest));
|
||||
}
|
||||
|
||||
uintptr_t elf_load(char *data, pagetable *pagemap)
|
||||
{
|
||||
struct elf_header *header = (struct elf_header *)data;
|
||||
|
||||
|
@ -42,44 +96,14 @@ uint64_t elf_load(char *data, uintptr_t *pagemap)
|
|||
return 0;
|
||||
}
|
||||
|
||||
struct elf_program_header *ph = (struct elf_program_header *)(data + header->e_phoff);
|
||||
|
||||
for (uint16_t i = 0; i < header->e_phnum; i++) {
|
||||
if (ph[i].p_type != PT_LOAD)
|
||||
continue;
|
||||
|
||||
uint64_t vaddr_start = ALIGN_DOWN(ph[i].p_vaddr, PAGE_SIZE);
|
||||
uint64_t vaddr_end = ALIGN_UP(ph[i].p_vaddr + ph[i].p_memsz, PAGE_SIZE);
|
||||
uint64_t offset = ph[i].p_offset;
|
||||
|
||||
uint64_t flags = VMM_PRESENT;
|
||||
if (ph[i].p_flags & PF_W)
|
||||
flags |= VMM_WRITABLE;
|
||||
if (!(ph[i].p_flags & PF_X))
|
||||
flags |= VMM_NX;
|
||||
|
||||
for (uint64_t addr = vaddr_start; addr < vaddr_end; addr += PAGE_SIZE) {
|
||||
uint64_t phys = (uint64_t)mem_alloc(PAGE_SIZE);
|
||||
if (!phys) {
|
||||
debug("Out of physical memory");
|
||||
return 0;
|
||||
}
|
||||
|
||||
map_page(pagemap, addr, phys, flags);
|
||||
|
||||
uint64_t file_offset = offset + (addr - vaddr_start);
|
||||
if (file_offset < offset + ph[i].p_filesz) {
|
||||
uint64_t to_copy = PAGE_SIZE;
|
||||
if (file_offset + PAGE_SIZE > offset + ph[i].p_filesz)
|
||||
to_copy = offset + ph[i].p_filesz - file_offset;
|
||||
|
||||
memcpy((void *)phys, data + file_offset, to_copy);
|
||||
} else {
|
||||
memset((void *)phys, 0, PAGE_SIZE);
|
||||
}
|
||||
}
|
||||
if (header->e_machine == 20 ||
|
||||
header->e_machine == 3 ||
|
||||
header->e_machine == 40) {
|
||||
return elf32_load(data, pagemap);
|
||||
} else if (header->e_machine == 62) {
|
||||
return elf64_load(data, pagemap);
|
||||
}
|
||||
|
||||
debug("ELF loaded successfully, entry: 0x%lx", header->e_entry);
|
||||
return header->e_entry;
|
||||
debug("Unsupported ELF machine: %u", header->e_machine);
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -21,7 +21,7 @@
|
|||
#define NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS 1
|
||||
#define NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS 0
|
||||
#define NANOPRINTF_USE_FLOAT_FORMAT_SPECIFIERS 0
|
||||
#define NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS 0
|
||||
#define NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS 1
|
||||
#define NANOPRINTF_USE_BINARY_FORMAT_SPECIFIERS 0
|
||||
#define NANOPRINTF_USE_WRITEBACK_FORMAT_SPECIFIERS 0
|
||||
#include <nanoprintf.h>
|
||||
|
|
69
boot/common/proto/aurix.c
Normal file
69
boot/common/proto/aurix.c
Normal file
|
@ -0,0 +1,69 @@
|
|||
/*********************************************************************************/
|
||||
/* Module Name: aurix.c */
|
||||
/* Project: AurixOS */
|
||||
/* */
|
||||
/* Copyright (c) 2024-2025 Jozef Nagy */
|
||||
/* */
|
||||
/* This source is subject to the MIT License. */
|
||||
/* See License.txt in the root of this repository. */
|
||||
/* All other rights reserved. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
|
||||
/* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
|
||||
/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
|
||||
/* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
|
||||
/* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
|
||||
/* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
|
||||
/* SOFTWARE. */
|
||||
/*********************************************************************************/
|
||||
|
||||
#include <proto/aurix.h>
|
||||
#include <loader/elf.h>
|
||||
#include <mm/mman.h>
|
||||
#include <mm/vmm.h>
|
||||
#include <vfs/vfs.h>
|
||||
#include <print.h>
|
||||
|
||||
extern __attribute__((noreturn)) void aurix_handoff(void *pagemap, void *stack, uint64_t entry, void *params);
|
||||
extern char _aurix_handoff_start[], _aurix_handoff_end[];
|
||||
|
||||
void aurix_load(char *kernel)
|
||||
{
|
||||
// read kernel -> test read
|
||||
char *kbuf = NULL;
|
||||
vfs_read(kernel, &kbuf);
|
||||
|
||||
// TODO: Do something with the kernel :p
|
||||
pagetable *pm = create_pagemap();
|
||||
if (!pm) {
|
||||
debug("aurix_load(): Failed to create kernel pagemap! Halting...\n");
|
||||
// TODO: Halt
|
||||
while (1);
|
||||
}
|
||||
|
||||
map_pages(pm, (uintptr_t)_aurix_handoff_start, (uintptr_t)_aurix_handoff_start, (uint64_t)_aurix_handoff_end - (uint64_t)_aurix_handoff_start, VMM_PRESENT | VMM_USER | VMM_WRITABLE);
|
||||
|
||||
void *stack = mem_alloc(16*1024); // 16 KiB stack should be well more than enough
|
||||
if (!stack) {
|
||||
debug("aurix_load(): Failed to allocate stack! Halting...\n");
|
||||
while (1);
|
||||
}
|
||||
|
||||
void *kernel_entry = (void *)elf_load(kbuf, pm);
|
||||
if (!kernel_entry) {
|
||||
debug("aurix_load(): Failed to load '%s'! Halting...\n", kernel);
|
||||
mem_free(kbuf);
|
||||
while (1);
|
||||
}
|
||||
|
||||
void *parameters = NULL;
|
||||
|
||||
debug("aurix_load(): Handoff state: pm=0x%llx, stack=0x%llx, kernel_entry=0x%llx\n", pm, stack, kernel_entry);
|
||||
|
||||
aurix_handoff(pm, (void *)((uint8_t)stack - 16*1024), (uint64_t)kernel_entry, (void *)parameters);
|
||||
|
||||
// __asm__ volatile("movq %[pml4], %%cr3\n" :: [pml4]"r"(pm) : "memory");
|
||||
// __asm__ volatile("callq *%[entry]\n"
|
||||
// :: [entry]"r"(kernel_entry));
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue