Protected Mode Boot Sector Help

From: Ryan Sommers (ryans_at_gamersimpact.com)
Date: 04/25/04


Date: Sat, 24 Apr 2004 23:20:44 +0000 (UTC)

I'm attempting to write a bootsector that enables the A20 line and
then puts the processor into protected mode. I've been stewing over
this code the past few days trying to find out why it's failing. I'm
using Bochs on FreeBSD to test. Any help would be greatly appreciated.
It seems to be failing after I enable the PE bit in the cr0 register.
Here is the ASM code:

        .set SEL_CODE, 0x8
        .set SEL_LINEAR, 0x10
.org 0x000
.code16
        ljmp $0x7C0, $start # far jump to load the CS with known value
                                        # some BIOS' start at 07c0:0000 and others
                                        # at 0000:7c00
        .globl start
start:
        movw $0x7C0, %ax # load known address
        movw %ax, %ds # into the segment
        movw %ax, %es # registers.
        movw %ax, %fs
        movw %ax, %gs

        xorw %ax, %ax # setup
        movw %ax, %ss # ... the
        movw $0xffff, %sp # ... stack

        movw $bootmessage, %si # put something
        call putstr # on the screen
        call gateA20 # enable the A20 line

        pushw $0x2
        popf
        lgdt (gdt_ptr+0x7C00) # Initialize GDTR, need a linear address
                                        # not a segmented one.
        movl %cr0, %eax
        orb $0x1, %al
        movl %eax, %cr0
        ljmp $SEL_CODE, $pmode # long jump to reload %cs

gateA20:
        push %si
        push %ax
        movw $A20Message, %si
        call putstr
        inb $0x92, %al
        orb $0x02, %al
        outb %al, $0x92
        pop %ax
        pop %si
        ret

putstr:
        push %ax
        push %bx
        movb $0x0e, %ah
        movw $0x0007, %bx
putstr.0:
        lodsb
        testb %al, %al
        jz putstr.1
        int $0x10
        jmp putstr.0
putstr.1:
        pop %bx
        pop %ax
        ret

.code32
pmode:
        jmp .

        
# Enter protected mode
        
bootmessage: .asciz "SOS Bootloader (v.01)\n"
A20Message: .asciz "Enabling A20 line...\n"
pmodemsg: .asciz "Jumping to PMode...\n"

# GDT
# Segment Descriptor Layout:
#
# 31 24 23 22 21 20 19 16 15 14 13 12 11 8 7
    0 Byte
# _________________________________________________________________________
# | | |D | |A | Seg | | D | | |
     |
# | Base 31:24 |G |/ |O |V | Limit |P | P |S | Type | Base
23:16| 4
# | | |B | |L | 19:16 | | L | | |
     |
# |___________________|__|__|__|__|________|__|_____|__|________|___________|
#
# 31 16 15
    0
# _________________________________________________________________________
# | |
     |
# | Base Address 15:00 | Segment Limit 15:00
     | 0
# |________________________________________|________________________________|

.org 0x0120
gdt_ptr: .word gdt_end-gdt-1
                .word (gdt+0x7C00)
                .word 0x0
                .word 0x0 # align the GDT on an 8-byte boundary
.org 0x130
gdt:
gdt.0: # Null selector
        .word 0 # [Off. 0] Segment Limit (15:00)
        .word 0 # [Off. 2] Base Address (15:00)
        .byte 0 # [Off. 4] Base Address (23:16)
        .byte 0 # [Off. 5] Flags
        .byte 0 # [Off. 6] Seg Limit & Flags
        .byte 0 # [Off. 7] Base Address (31:24)
        
gdt.1: # Code (0x7C00 base)
        .word 0xFFFF # [Off. 0] Segment Limit (15:00)
        .word 0x7C00 # [Off. 2] Base Address (15:00)
        .byte 0x00 # [Off. 4] Base Address (23:16)
        .byte 0x9A # [Off. 5] Flags
        .byte 0xCF # [Off. 6] Seg Limit & Flags
        .byte 0x00 # [Off. 7] Base Address (31:24)
        
gdt.2: # Linear (0x0 base)
        .word 0xFFFF # [Off. 0] Segment Limit (15:00)
        .word 0x0 # [Off. 2] Base Address (15:00)
        .byte 0x0 # [Off. 4] Base Address (23:16)
        .byte 0x92 # [Off. 5] Flags
        .byte 0xCF # [Off. 6] Seg Limit & Flags
        .byte 0x0 # [Off. 7] Base Address (31:24)
gdt_end:

.org 0x01fe
.word 0xaa55



Relevant Pages

  • Re: [pm] Strange cleanups in -test8 kernel/acpi/wakeup.S
    ... understand your generic plea for review, I fail to see how it would help ... > registers are in gdt ... you must reload the GDT in protected mode ... what you killed were debugging hacks. ...
    (Linux-Kernel)
  • Re: [pm] Strange cleanups in -test8 kernel/acpi/wakeup.S
    ... >> loading gdt. ... you must reload the GDT in protected mode ... what you killed were debugging hacks. ... To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/ ...
    (Linux-Kernel)
  • Re: Protected Mode:
    ... I have a valid gdt and idt setup, however when I try to execute my ... is still trying to execute the instructions as 16-bit instructions. ... The 16-bit execution causes some side effects which I can track, ... dissassembles in real or protected mode), ...
    (comp.arch.embedded)
  • Re: [pm] Strange cleanups in -test8 kernel/acpi/wakeup.S
    ... Patrick Mochel wrote: ... >understand your generic plea for review, I fail to see how it would help ... >>registers are in gdt ... you must reload the GDT in protected mode ...
    (Linux-Kernel)