Search code examples
assemblyx86-64bare-metal

How to enter 64-bit mode on a x86_64


I've found a nice bit of assembly in xv6 https://github.com/chrisdew/xv6/blob/master/bootasm.S which shows me how to move from 16 bit to 32 bit protected mode.

Does anyone know of a similar example for entering the 64-bit mode? (Either through or without using 32 bit mode.)


Solution

  • In order to enable 64 bit capabillities, you must switch the CPU to Long Mode.

    To enter Long Mode on a 64-bit x86 processor (x86-64):

    If paging is enabled, disable paging.
    If CR4.PAE is not already set, set it.
    Set IA32_EFER.LME = 1.
    Load CR3 with a valid PML4 table.
    Enable paging.
    At this point you will be in compatibility mode. A far jump may be executed to switch to long mode. However, the offset must not exceed 32-bit.