Switch to PMode Problem
Posted: Thu Jan 02, 2003 2:35 pm
Hi
I'm having trouble with my code that switches to PMode and I'm not sure what I'm doing wrong.
Here is the code:
; Segment Descriptor Defenitions
; 0x00cf9a000000ffff Supervisor code from 0x00000000 to 4GB
; 0x00cf92000000ffff Supervisor data from 0x00000000 to 4GB
; 0x00cffa000000ffff User code from 0x00000000 to 4GB
; 0x00cff2000000ffff User data from 0x00000000 to 4GB
_switch_to_pmode:
cli
; Clearing the first 128KB of RAM with 00s
; ready for the GDT and IDT to be placed into it
; Not the best method, but should work ok
mov cx, 0xFFFF ; Want to clear room for 2KB RAM
.Lower_64K
mov ax, 0
mov es, ax ; Want to clear segement 0
; NOTE!! Interrupts MUST be disabled or
; a crash is certain!!
mov ax, 0
mov bx, cx
mov [es:bx], al
loop .Lower_64K
mov cx, 0xFFFF ; Want to clear room for 2KB RAM
.Upper_64K
mov ax, 0
mov es, ax ; Want to clear segement 0
; NOTE!! Interrupts MUST be disabled or
; a crash is certain!!
mov ax, 1
mov bx, cx
mov [es:bx], al
; loop .Upper_64K
;.setup_idt
;lidt [idtr_details]
.setup_gdt
xor ax, ax
mov es, ax
;------------------------------------
; First Setup the Segment Descriptors
;------------------------------------
; First 0x00 - The Null Selector
mov dword [es:GDT_LOCATION] , 0x00000000
mov dword [es:GDT_LOCATION+4], 0x00000000
; Now 0x08 - the Supervisor mode code segment
mov dword [es:GDT_LOCATION+8] , 0x0000ffff
mov dword [es:GDT_LOCATION+12], 0x00cf9a00
; Now 0x10 - the Supervisor mode data segment
mov dword [es:GDT_LOCATION+16], 0x0000ffff
mov dword [es:GDT_LOCATION+20], 0x00cf9200
; Now 0x18 - the User mode code segment
mov dword [es:GDT_LOCATION+24], 0x0000ffff
mov dword [es:GDT_LOCATION+28], 0x00cffa00
; Now 0x20 - the User mode data segment
mov dword [es:GDT_LOCATION+32], 0x0000ffff
mov dword [es:GDT_LOCATION+36], 0x00cff200
; Finally load the GDT into the GDTR
lgdt [gdtr_details]
;--------------------------------
; Now To switch to Protected Mode
;--------------------------------
mov eax, cr0 ; Read in CR0 Register
or eax, 1 ; Toggle PE Bit
mov cr0, eax ; Save to CR0
[Bits 32]
; That's it, we're in Protected Mode!
; Next, Flush the Prefetch Que
Jmp short .flush
.flush
;-------------------------------
; Now to setup Segment Selectors
;-------------------------------
mov ax, 0x10
mov ds, ax ; Setup data selectors with 0x10 + TI=0b + RPL=00b
mov es, ax
mov fs, ax
mov gs, ax
mov ss, ax
; To change cs, jump to new location
mov ax, 0x08
push ax
push word .ToHere
retf
.ToHere mov byte [ds:0xb8000], 'A'
Jmp $
;pop eax
;retn
; Data #######################################################################
; As this is a tiny mode program, all data is still in the code segment
gdtr_details dw 0xFFFF
dd 0x0800
This program is supposed to first clear the first 128KB RAM with 0s ready for my tables to go in. Then I put in the idt (will be done at a later date) and the GDT. After that it is switched into pmode and the selectors are set up. Finally, there is a line that prints the char A to the top left had of the screen to test that it has all worked.
My problem is that I can't get this to work and I don't know that the problem is with it.
Thanks for your help
srg
I'm having trouble with my code that switches to PMode and I'm not sure what I'm doing wrong.
Here is the code:
; Segment Descriptor Defenitions
; 0x00cf9a000000ffff Supervisor code from 0x00000000 to 4GB
; 0x00cf92000000ffff Supervisor data from 0x00000000 to 4GB
; 0x00cffa000000ffff User code from 0x00000000 to 4GB
; 0x00cff2000000ffff User data from 0x00000000 to 4GB
_switch_to_pmode:
cli
; Clearing the first 128KB of RAM with 00s
; ready for the GDT and IDT to be placed into it
; Not the best method, but should work ok
mov cx, 0xFFFF ; Want to clear room for 2KB RAM
.Lower_64K
mov ax, 0
mov es, ax ; Want to clear segement 0
; NOTE!! Interrupts MUST be disabled or
; a crash is certain!!
mov ax, 0
mov bx, cx
mov [es:bx], al
loop .Lower_64K
mov cx, 0xFFFF ; Want to clear room for 2KB RAM
.Upper_64K
mov ax, 0
mov es, ax ; Want to clear segement 0
; NOTE!! Interrupts MUST be disabled or
; a crash is certain!!
mov ax, 1
mov bx, cx
mov [es:bx], al
; loop .Upper_64K
;.setup_idt
;lidt [idtr_details]
.setup_gdt
xor ax, ax
mov es, ax
;------------------------------------
; First Setup the Segment Descriptors
;------------------------------------
; First 0x00 - The Null Selector
mov dword [es:GDT_LOCATION] , 0x00000000
mov dword [es:GDT_LOCATION+4], 0x00000000
; Now 0x08 - the Supervisor mode code segment
mov dword [es:GDT_LOCATION+8] , 0x0000ffff
mov dword [es:GDT_LOCATION+12], 0x00cf9a00
; Now 0x10 - the Supervisor mode data segment
mov dword [es:GDT_LOCATION+16], 0x0000ffff
mov dword [es:GDT_LOCATION+20], 0x00cf9200
; Now 0x18 - the User mode code segment
mov dword [es:GDT_LOCATION+24], 0x0000ffff
mov dword [es:GDT_LOCATION+28], 0x00cffa00
; Now 0x20 - the User mode data segment
mov dword [es:GDT_LOCATION+32], 0x0000ffff
mov dword [es:GDT_LOCATION+36], 0x00cff200
; Finally load the GDT into the GDTR
lgdt [gdtr_details]
;--------------------------------
; Now To switch to Protected Mode
;--------------------------------
mov eax, cr0 ; Read in CR0 Register
or eax, 1 ; Toggle PE Bit
mov cr0, eax ; Save to CR0
[Bits 32]
; That's it, we're in Protected Mode!
; Next, Flush the Prefetch Que
Jmp short .flush
.flush
;-------------------------------
; Now to setup Segment Selectors
;-------------------------------
mov ax, 0x10
mov ds, ax ; Setup data selectors with 0x10 + TI=0b + RPL=00b
mov es, ax
mov fs, ax
mov gs, ax
mov ss, ax
; To change cs, jump to new location
mov ax, 0x08
push ax
push word .ToHere
retf
.ToHere mov byte [ds:0xb8000], 'A'
Jmp $
;pop eax
;retn
; Data #######################################################################
; As this is a tiny mode program, all data is still in the code segment
gdtr_details dw 0xFFFF
dd 0x0800
This program is supposed to first clear the first 128KB RAM with 0s ready for my tables to go in. Then I put in the idt (will be done at a later date) and the GDT. After that it is switched into pmode and the selectors are set up. Finally, there is a line that prints the char A to the top left had of the screen to test that it has all worked.
My problem is that I can't get this to work and I don't know that the problem is with it.
Thanks for your help
srg