972
edits
[unchecked revision] | [unchecked revision] |
m (Bot: Replace deprecated source tag with syntaxhighlight) |
|||
(12 intermediate revisions by 10 users not shown) | |||
Line 1:
For some reason, you might want to switch to 64 bit mode in your bootloader without entering [[protected mode]]. The following article practically described how to do so, mainly demonstrating it by example assembly code. Additionally, some may find it easier to switch directly to long mode on additional CPUs as they are initialized for [[Symmetric Multiprocessing]].
== Switching to Long Mode ==
Line 5:
The following can be used to switch directly to Long Mode. The code is well documented through comments, and nearly explains almost everything it does.
<
%define PAGE_PRESENT (1 << 0)
%define PAGE_WRITE (1 << 1)
Line 22:
; es:edi Should point to a valid page-aligned 16KiB buffer, for the PML4, PDPT, PD and a PT.
; ss:esp Should point to memory that can be used as a small (1
SwitchToLongMode:
Line 30:
mov ecx, 0x1000
xor eax, eax
cld▼
rep stosd
pop di ; Get DI back.
Line 106 ⟶ 107:
.Code:
dq
dq
ALIGN 4
Line 124 ⟶ 125:
mov fs, ax
mov gs, ax
mov ss, ax▼
; Blank out the screen to a blue color.
mov edi, 0xB8000
mov rcx, 500 ; Since we are clearing
mov rax, 0x1F201F201F201F20 ; Set the value to set the screen to: Blue background, white foreground, blank spaces.
rep stosq ; Clear the entire screen.
Line 144 ⟶ 146:
jmp Main.Long ; You should replace this jump to wherever you want to jump to.
</syntaxhighlight>
=== Notes about the above code ===
Line 155 ⟶ 157:
The following example bootsector has been provided for completeness. It checks whether the CPU supports x86_64 or not. It then calls SwitchToLongMode to switch to long mode.
<
%define FREE_SPACE 0x9000
Line 167 ⟶ 169:
; Do a far jump to fix this issue, and reload CS to 0x0000.
.FlushCS:
xor ax, ax▼
▲ cld
; Set up segment registers.▼
; Set up stack so that it starts below Main.▼
mov sp, Main
▲ ; Set up segment registers.
▲ xor ax, ax
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
cld
▲ mov ss, ax
▲ ; Set up stack so that it starts below Main.
▲ mov sp, Main
call CheckCPU ; Check whether we support Long Mode or not.
Line 282 ⟶ 281:
times 510 - ($-$$) db 0
dw 0xAA55
</syntaxhighlight>
The above code can be compiled (and tested using QEMU) by the following:
<
nasm -fbin Main.asm -o LongModeDirectly
qemu-system-x86_64 -hda LongModeDirectly
</syntaxhighlight>
=== Notes about the above bootsector ===
# The above bootsector has been written just for completeness.▼
# The above bootsector can't be used as an actual bootsector as it doesn't:▼
# Enable [[A20]]▼
# Detect memory▼
# Load required files from disk using BIOS▼
== See Also ==▼
[http://www.osdev.org/phpBB2/viewtopic.php?t=11093 Related forum thread]▼
===Forum Threads===
▲* [http://www.osdev.org/phpBB2/viewtopic.php?t=11093 Related forum thread]
|