At the risk of being called a newbie, I decided to post here about my code that switches to realmode. I've tried to follow the Intel Manuals as good as possible, but my code keeps breaking. I'm fairly new to doing the switches so I thought it'd be better to let you guys take a look at it than stay stuck with it.
Here it goes:
Code: Select all
[BITS 32]
[ORG 0x6000]
RegsAX dw 0 ; |
RegsBX dw 0 ; | > These will be used later, they are ignored at this time.
RegsCX dw 0 ; | >
RegsDX dw 0 ; |
; This should be at address 0x6008 (which I'm calling).
Entry:
; Step 1
cli
; Step 2: Turn paging off...
mov eax, CR0
and eax, 0x7FFFFFFF
mov CR0, eax
mov eax, 0
mov CR3, eax
; Step 3
jmp 0x08:ProtectedMode16
[BITS 16]
ProtectedMode16:
; Step 4 - 5
mov ax, 0x10
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ss, ax
; Step 6
lidt [RealModeIDT]
; Step 7: Turn protected mode off...
mov eax, CR0
and eax, 0xFFFFFFFE
mov CR0, eax
; Step 8
jmp 0x00:RealMode
RealMode:
; Step 9
xor eax, eax
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ss, ax
; Step 10
sti
; Perform a BIOS interrupt.
mov ax, 13h
int 10h
cli
; Code to switch back is unfinished, but not really necessary at this time.
jmp $
RealModeIDT:
dw 0x03FF
dd 0x0000
Code: Select all
00017728222e[CPU0 ] SetCR0: GP(0) when attempt to set CR0.NW with CR0.CD cleared !
00017728222e[CPU0 ] interrupt(): gate descriptor is not valid sys seg (vector=0x0d)
00017728222e[CPU0 ] interrupt(): gate descriptor is not valid sys seg (vector=0x08)
Code: Select all
[CPU0 ] CS.d_b = 32 bit
[CPU0 ] SS.d_b = 32 bit
I know there will probably be some serious flaws in my code, but I guess everybody has to learn sometime.
PS: The realmode switching program is assembled as pure binary (-f bin) with NASM and passed to the kernel as a module with GRUB. I then clear 4096 bytes (0x1000 = size of one page) starting at 0x6000 and copy the module to its origin (0x6000). Then, finally, I call the module at 0x6008 (skipping the 4 words). GRUB modules, the kernel, and everything below 1 MB is identity mapped in my kernel.
Thanks in advance for your help,
Creature