[OSDev] How to switch to long mode in x86 CPUs?
Sujet : [OSDev] How to switch to long mode in x86 CPUs?
De : rakinar2 (at) *nospam* onesoftnet.eu.org (Ar Rakin)
Groupes : comp.lang.cDate : 27. Feb 2025, 16:57:01
Autres entêtes
Organisation : OSN Developers
Message-ID : <871pvje5yq.fsf@onesoftnet.eu.org>
User-Agent : Gnus/5.13 (Gnus v5.13)
Hello there,
I am trying to develop my own, simple operating system to learn more
about how kernels work and low level stuff like that. However, I am
stuck at setting up paging while switching long mode (64-bit protected
mode) in x86 processors.
The assembly code I currently have:
#define PG_START 0x000000000
#define MSR_EFER 0xc0000080
.section .bss, "aw", @nobits
.align 4096
pml4_tbl:
.skip 4096
pdpt_tbl:
.skip 4096
.text
.globl _mboot_start
_mboot_start:
/* GRUB executes this code in 32-bit protected mode. */
/* Write (pdpt_tbl | 0x3) to the first 8 bytes of pml4_tbl */
movl $pdpt_tbl, %eax
orl $0x3, %eax
movl $pml4_tbl, %edi
movl %eax, (%edi)
xorl %eax, %eax
movl %eax, 4(%edi)
movl $pdpt_tbl, %edi
movl $PG_START, %eax
/* 0x83 = 0b10000011; flags: present, writable, upervisor-only,
1GB huge page */
movl $0x83, (%edi)
movl %eax, 4(%edi)
/* Enable Physical Address Extension (PAE) */
movl %cr4, %eax
btsl $5, %eax
movl %eax, %cr4
/* Load the address of the PML4 table into %cr3 */
movl $pml4_tbl, %edi
movl %edi, %cr3
/* Enable long mode */
movl $MSR_EFER, %ecx
rdmsr
btsl $8, %eax
wrmsr
/* Enable paging */
movl %cr0, %eax
btsl $31, %eax
movl %eax, %cr0
/* Jump to 64-bit code */
ljmpl $0x08, $long_mode_entry
.loop:
hlt
jmp .loop
long_mode_entry:
.code64
xorw %ax, %ax
movw %ax, %ds
movw %ax, %es
movw %ax, %fs
movw %ax, %gs
movw %ax, %ss
callq kmain
callq kabort
I am not sure what is wrong, but when I run my kernel in
qemu-system-x86_64, it causes a triple fault when trying to jump to the
long mode code. After a lot of debugging, I am sure that the issue is
with paging, because removing the ljmpl and paging instructions do not
cause any further errors and the kernel runs fine in 32-bit mode.
If anyone knows what is wrong with this code, please let me know. Any
help will be appreciated!
--
Ar Rakin
Haut de la page
Les messages affichés proviennent d'usenet.
NewsPortal