boot: Don't jump back to protected mode in EFI loader

This commit is contained in:
K. Lange 2023-01-05 15:39:44 +09:00
parent 5053857c88
commit a4a73a0835
2 changed files with 15 additions and 34 deletions

View File

@ -264,39 +264,9 @@ static void finish_boot(void) {
}
}
uint64_t foobar = ((uint32_t)(uintptr_t)&do_the_nasty) | (0x10L << 32L);
uint32_t * foo = (uint32_t *)0x7c00;
foo[0] = MULTIBOOT_EAX_MAGIC;
foo[1] = (uintptr_t)finalHeader;
foo[2] = _xmain;
/* Jump to entry with register arguments */
__asm__ __volatile__ (
"push %0\n"
"lretl\n"
: : "g"(foobar));
__asm__ (
"do_the_nasty:\n"
"cli\n"
".code32\n"
"mov %cr0, %eax\n"
"and $0x7FFeFFFF, %eax\n"
"mov %eax, %cr0\n"
"mov $0xc0000080, %ecx\n"
"rdmsr\n"
"and $0xfffffeff, %eax\n"
"wrmsr\n"
"mov $0x640, %eax\n"
"mov %eax, %cr4\n"
"mov 0x7c00, %eax\n"
"mov 0x7c04, %ebx\n"
"mov 0x7c08, %ecx\n"
"jmp *%ecx\n"
"target: jmp target\n"
".code64\n"
);
"jmp %0" :: "r"(_xmain), "a"(MULTIBOOT_EAX_MAGIC), "b"(finalHeader));
__builtin_unreachable();
}

View File

@ -151,7 +151,6 @@ jmp_to_long:
/* Set up initial page region, which was zero'd for us by the loader */
mov $init_page_region, %edi
mov %edi, %cr3
/* PML4[0] = &PDP[0] | (PRESENT, WRITABLE, USER) */
mov $0x1007, %eax
@ -179,6 +178,9 @@ jmp_to_long:
add $8, %edi
loop .set_entry
mov $init_page_region, %edi
mov %edi, %cr3
/* Enable PAE */
mov %cr4, %eax
or $32, %eax
@ -190,12 +192,19 @@ jmp_to_long:
or $256, %eax
wrmsr
/* Set PG */
/* Check PG */
mov %cr0, %eax
/* If paging was enabled, assume we were already in long mode (eg. booted by EFI) */
test $0x80000000, %eax
jnz .continue
/* Otherwise enable paging */
or $0x80000000, %eax
mov %eax, %cr0
lgdt gdtr
ljmp $0x08,$realm64
.align 8
@ -236,6 +245,8 @@ realm64:
mov %ax, %gs
mov %ax, %ss
.continue:
cli
pop %rdi
pop %rsi
pop %rdx