Results 1 to 4 of 4
  1. #1
    t7ancients's Avatar
    Join Date
    Dec 2008
    Gender
    male
    Location
    New York
    Posts
    381
    Reputation
    28
    Thanks
    68
    My Mood
    Twisted

    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.

  2. #2
    .::SCHiM::.'s Avatar
    Join Date
    Sep 2010
    Gender
    male
    Posts
    733
    Reputation
    180
    Thanks
    880
    My Mood
    Twisted
    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
    Last edited by .::SCHiM::.; 07-03-2012 at 06:21 AM.

    I'm SCHiM

    Morals derive from the instinct to survive. Moral behavior is survival behavior above the individual level.

    Polymorphic engine
    Interprocess callback class
    SIN
    Infinite-precision arithmetic
    Hooking dynamic linkage
    (sloppy)Kernel mode Disassembler!!!

    Semi debugger




  3. The Following 2 Users Say Thank You to .::SCHiM::. For This Useful Post:

    Hassan (07-09-2012),t7ancients (07-03-2012)

  4. #3
    t7ancients's Avatar
    Join Date
    Dec 2008
    Gender
    male
    Location
    New York
    Posts
    381
    Reputation
    28
    Thanks
    68
    My Mood
    Twisted
    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?

  5. #4
    .::SCHiM::.'s Avatar
    Join Date
    Sep 2010
    Gender
    male
    Posts
    733
    Reputation
    180
    Thanks
    880
    My Mood
    Twisted
    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.

    I'm SCHiM

    Morals derive from the instinct to survive. Moral behavior is survival behavior above the individual level.

    Polymorphic engine
    Interprocess callback class
    SIN
    Infinite-precision arithmetic
    Hooking dynamic linkage
    (sloppy)Kernel mode Disassembler!!!

    Semi debugger




  6. The Following 2 Users Say Thank You to .::SCHiM::. For This Useful Post:

    LEGiiTxCHAOTiiC (07-19-2012),t7ancients (07-04-2012)

Similar Threads

  1. Replies: 16
    Last Post: 07-20-2007, 09:32 AM
  2. read this bit of fun...
    By prox32 in forum WarRock - International Hacks
    Replies: 2
    Last Post: 05-19-2007, 12:14 PM
  3. bit of help plz....
    By prox32 in forum WarRock - International Hacks
    Replies: 8
    Last Post: 04-24-2007, 04:12 PM
  4. MPGH a bit slow?
    By Dave84311 in forum General
    Replies: 7
    Last Post: 04-04-2007, 11:23 AM
  5. Replies: 6
    Last Post: 07-20-2006, 05:19 AM