ZealBooter: De-hardcode kernel load address and boot stack

This commit is contained in:
mintsuki 2022-08-21 22:21:01 +02:00
parent 1510f65468
commit 375a487087
3 changed files with 28 additions and 9 deletions

View file

@ -82,8 +82,6 @@ SYS_SEMAS:: DU8 SEMA_SEMAS_NUM * DEFAULT_CACHE_LINE_WIDTH DUP(0);
DU64 0xf6ceba7d4b74179a; DU64 0xf6ceba7d4b74179a;
CORE0_32BIT_INIT:: //Entry point for $LK,"BootRAM",A="MN:BootRAM"$. CORE0_32BIT_INIT:: //Entry point for $LK,"BootRAM",A="MN:BootRAM"$.
CLD
PUSH U32 RFLAGG_START PUSH U32 RFLAGG_START
POPFD POPFD
MOV EAX, SYS_START_CR0 MOV EAX, SYS_START_CR0
@ -100,7 +98,7 @@ CORE0_32BIT_INIT:: //Entry point for $LK,"BootRAM",A="MN:BootRAM"$.
MOV FS, AX MOV FS, AX
MOV GS, AX MOV GS, AX
MOV SS, AX MOV SS, AX
MOV ESP, BOOT_RAM_LIMIT //Tmp Stack //MOV ESP, BOOT_RAM_LIMIT //Tmp Stack
//Patch abs addresses //Patch abs addresses
MOV ECX, U32 CPatchTableAbsAddr.abs_address_count[ESI] MOV ECX, U32 CPatchTableAbsAddr.abs_address_count[ESI]

View file

@ -2,8 +2,6 @@
.global trampoline .global trampoline
trampoline: trampoline:
mov $0x6000, %esp
lgdt (%rcx) lgdt (%rcx)
pushq $0x30 pushq $0x30

View file

@ -166,7 +166,27 @@ void _start(void) {
struct limine_file *kernel = module_request.response->modules[0]; struct limine_file *kernel = module_request.response->modules[0];
struct CKernel *CKernel = kernel->address; struct CKernel *CKernel = kernel->address;
uintptr_t final_address = 0x7c00; size_t trampoline_size = (uintptr_t)trampoline_end - (uintptr_t)trampoline;
size_t boot_stack_size = 32768;
uintptr_t final_address = (uintptr_t)-1;
for (size_t i = 0; i < memmap_request.response->entry_count; i++) {
struct limine_memmap_entry *entry = memmap_request.response->entries[i];
if (entry->type != LIMINE_MEMMAP_USABLE) {
continue;
}
if (entry->length >= ALIGN_UP(kernel->size + trampoline_size, 16) + boot_stack_size) {
final_address = entry->base;
break;
}
}
if (final_address == (uintptr_t)-1) {
// TODO: Panic. Show something?
for (;;);
}
struct limine_framebuffer *fb = framebuffer_request.response->framebuffers[0]; struct limine_framebuffer *fb = framebuffer_request.response->framebuffers[0];
CKernel->sys_vbe_mode.pitch = fb->pitch; CKernel->sys_vbe_mode.pitch = fb->pitch;
@ -241,17 +261,20 @@ void _start(void) {
void *sys_gdt_ptr = (void *)&CKernel->sys_gdt_ptr - (uintptr_t)kernel->address; void *sys_gdt_ptr = (void *)&CKernel->sys_gdt_ptr - (uintptr_t)kernel->address;
sys_gdt_ptr += final_address; sys_gdt_ptr += final_address;
void *trampoline_phys = (void *)final_address - ((uintptr_t)trampoline_end - (uintptr_t)trampoline); void *trampoline_phys = (void *)final_address + kernel->size;
memmove(trampoline_phys, trampoline, ((uintptr_t)trampoline_end - (uintptr_t)trampoline)); uintptr_t boot_stack = ALIGN_UP(final_address + kernel->size + trampoline_size, 16) + boot_stack_size;
memmove(trampoline_phys, trampoline, trampoline_size);
memmove((void *)final_address, CKernel, kernel->size); memmove((void *)final_address, CKernel, kernel->size);
asm volatile ( asm volatile (
"mov %5, %%rsp;"
"jmp *%0" "jmp *%0"
: :
: "a"(trampoline_phys), "b"(CORE0_32BIT_INIT), : "a"(trampoline_phys), "b"(CORE0_32BIT_INIT),
"c"(sys_gdt_ptr), "S"(CKernel->boot_patch_table_base), "c"(sys_gdt_ptr), "S"(CKernel->boot_patch_table_base),
"D"(CKernel->boot_base) "D"(CKernel->boot_base), "r"(boot_stack)
: "memory"); : "memory");
__builtin_unreachable(); __builtin_unreachable();