[OSDev] How to switch to long mode in x86 CPUs?

Liste des GroupesRevenir à cl c 
Sujet : [OSDev] How to switch to long mode in x86 CPUs?
De : rakinar2 (at) *nospam* onesoftnet.eu.org (Ar Rakin)
Groupes : comp.lang.c
Date : 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

Date Sujet#  Auteur
27 Feb 25 * [OSDev] How to switch to long mode in x86 CPUs?29Ar Rakin
27 Feb 25 +* Re: [OSDev] How to switch to long mode in x86 CPUs?14Keith Thompson
28 Feb 25 i`* Re: [OSDev] How to switch to long mode in x86 CPUs?13Ar Rakin
28 Feb 25 i +- Re: [OSDev] How to switch to long mode in x86 CPUs?1Waldek Hebisch
28 Feb 25 i `* OT: (Was: How to switch to long mode in x86 CPUs?)11Salvador Mirzo
28 Feb 25 i  +* Re: OT: USENET9Salvador Mirzo
1 Mar 25 i  i+* Re: OT: USENET7Richard Harnden
1 Mar 25 i  ii+* Re: OT: USENET2Ar Rakin
5 Mar 25 i  iii`- Re: OT: USENET1candycanearter07
2 Mar 25 i  ii`* Re: OT: USENET4Salvador Mirzo
2 Mar 25 i  ii `* Re: OT: USENET3Ar Rakin
2 Mar 25 i  ii  +- Re: OT: USENET1yeti
9 Mar 25 i  ii  `- Re: OT: USENET1Salvador Mirzo
1 Mar 25 i  i`- Re: OT: USENET1Ar Rakin
1 Mar 25 i  `- Re: OT:1Ar Rakin
28 Feb 25 +* Re: [OSDev] How to switch to long mode in x86 CPUs?12David Brown
28 Feb 25 i+* Re: [OSDev] How to switch to long mode in x86 CPUs?2Ar Rakin
1 Mar 25 ii`- Re: [OSDev] How to switch to long mode in x86 CPUs?1Dan Cross
1 Mar 25 i`* Re: [OSDev] How to switch to long mode in x86 CPUs?9Paul Edwards
2 Mar 25 i +* Re: [OSDev] How to switch to long mode in x86 CPUs?3Salvador Mirzo
2 Mar 25 i i+- Re: PC BIOS (was [OSDev] How to switch to long mode in x86 CPUs?)1Dan Cross
2 Mar 25 i i`- Re: [OSDev] How to switch to long mode in x86 CPUs?1Dan Cross
2 Mar 25 i `* Re: [OSDev] How to switch to long mode in x86 CPUs?5Paul Edwards
2 Mar 25 i  `* Re: [OSDev] How to switch to long mode in x86 CPUs?4bart
4 Mar 25 i   `* Re: [OSDev] How to switch to long mode in x86 CPUs?3Paul Edwards
4 Mar 25 i    `* Re: [OSDev] How to switch to long mode in x86 CPUs?2bart
5 Mar 25 i     `- Re: [OSDev] How to switch to long mode in x86 CPUs?1Paul Edwards
1 Mar 25 +- Re: [OSDev] How to switch to long mode in x86 CPUs?1Dan Cross
2 Mar 25 `- Re: [OSDev] How to switch to long mode in x86 CPUs?1Andy Valencia

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal