Help64-bit bootloader?

Posts 14 of 4 · Page 1 of 1
64-bit bootloader?
I want to write a bootloader for AMD64 using NASM. I would do this by using 'bits 64' and by outputting a flat binary, right? If someone could, could you write me up the basics in x64 assembly? I know a bit about assembly and C/C++ but don't really know how to start writing an operating system.

@.::SCHiM::.


I appreciate your help guys. @_@
I have read a bit on OSDev but they don't touch on 64-bit stuff as much as I'd like. A good working example would be absolutely amazing.
You've got it wrong sorry

The processor always starts in 16 bit mode, for backwards compatibly. In 16bit you don't have the full 4gb address space of 32bit or the 2^64 space of x64. The memory addressing in x16 is very different too. To access an address you need to multiply the segment selector by 16d (ROL 1) and add the offset. For example to jump to address 0x1000 you could do this:

jmp 0:1000

But this will also produce the same effect:

jmp 100:0 ; 100:0 == 0:1000 while 10:100 = 200

Once you've got your 16bit boot loader up and running, you can switch to x86. When x86 is up and running you switch (for the last time) to x64, you need to be in protected mode first, since certain functions of the processor can't be accessed in realmode and you need to functions to make the switch. The problem is that any paging related exceptions that occur during the switch to x64 must be handled by a handler in the IDT, if you have no handler yet your processor will triple fault.

So what you need to do is:

[BOOT]
Change the a21 line for extra memory
Load kernel
Switch to x86
Fix IDT
Enable paging
Switch to x64
relocate kernel
run kernel

And that's it!

You can of course use grub to load your kernel. Everyone uses grub
So a bootloader will be a combination of 16-bit, x86, and x64 code? Do I make a multi-stage bootloader, like separate binary files, and have the x16 load the x86 and so on?
Yea, but nasm can all do it in only one file. Consider this:

Code:
bits 16
org 7c0:0

xor ax, ax
mov ds, ax
mov cs, ax          ; segments to 0, I prefer this because it makes addressing that much easier (it's more like flat model now)
mov ss, ax

mov ax, 0x1fff
mov sp, ax            ; stack starts at 1fff

...
...

; fill 510 bytes with code, junk and zeros

db  0x55, 0xaa        ; (i forgot the exact signature )

jump far 0:JMP_x32

JMP_x32:                       

bits 32                            ;; here starts the 32 bit code part      

...
...
...

mov eax, cr0
or eax, PAE_BIT_LOC
mov cr0, eax


bits 64 ; yay 64 mode!!! :D
Obviously this is more pseudo code than anything, but nasm can handle changing modes in the same file.
Posts 14 of 4 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?