Page 1 of 1
Testing Processor Mode
Posted: Fri Feb 22, 2008 7:27 am
by kg4mxz
So I think I have successfully switched to Protected Mode, but my printf function still works, therefore I have some doubt as to if it really worked. Is there some function I can call that will definitively fail if I run it in PMode? I have searched google, osdev wiki, osdev forum, and have tried (and failed ) to decipher the intel manuals.
Thanks in Advance,
Robert
Posted: Fri Feb 22, 2008 7:38 am
by cyr1x
Just call an interrupt, e.g.
If the PC reboots(triple faults) you're in ProtectedMode.
If not, you're in RealMode.
Well this works only as long as you haven't any IDT for ProtectedMode set up.
Posted: Fri Feb 22, 2008 7:52 am
by Dex
Also (for a simple test) if your on a real PC and your using floppy to boot from, the floppy light stays on one you enter pmode.
Posted: Fri Feb 22, 2008 8:02 am
by kg4mxz
I was able to run sti after I entered PMode, so my Kernel does catch interrupts...
Not looking like its doing anything when I call int 0x10
Heres the Assembler Code:
Code: Select all
[BITS 32]
[GLOBAL ProtectedMode]
[EXTERN main_p]
ProtectedMode:
cli ; clear interrupts
mov eax, cr0 ; set bit 0 in cr0--enter pmode
or eax, 1
mov cr0, eax
jmp 08h:Stage3 ; far jump to fix CS. Remember that the code selector is 0x8!
Stage3:
;-------------------------------;
; Set registers ;
;-------------------------------;
mov ax, 0x10 ; set data segments to data selector (0x10)
mov ds, ax
mov ss, ax
mov es, ax
mov esp, 90000h ; stack begins from 90000h
sti
call main_p
Most of the Kernel is in C with occasional jumps to Assembler and back.
Here is what Bochs says:
Code: Select all
01123558511d[CPU0 ] Enter Protected Mode
01123558511d[CPU0 ] Protected Mode Activated
01123815139d[CPU0 ] Enter Real Mode
01123815139d[CPU0 ] Real Mode Activated
01123815149d[CPU0 ] interrupt(): vector = 8, INT = 0, EXT = 1
01123815165d[CPU0 ] interrupt(): vector = 28, INT = 1, EXT = 0
01123815177d[CPU0 ] interrupt(): vector = 18, INT = 1, EXT = 0
01123815191d[CPU0 ] Enter Protected Mode
01123815191d[CPU0 ] Protected Mode Activated
01123815236d[CPU0 ] Enter Real Mode
01123815236d[CPU0 ] Real Mode Activated
01123815249d[CPU0 ] interrupt(): vector = 21, INT = 1, EXT = 0
01123815356d[CPU0 ] Enter Protected Mode
01123815356d[CPU0 ] Protected Mode Activated
01123815400d[CPU0 ] Enter Real Mode
01123815400d[CPU0 ] Real Mode Activated
Well, it triple faults in VMWare right off the bat... back to the drawing board...
Posted: Fri Feb 22, 2008 10:57 am
by Brynet-Inc
You could test to see if bit 0 in the cr0 register is set...
Code: Select all
if (cr0 & 1) {
......
}
....
mov %cr0,%eax
and $0x1,%eax
test %eax,%eax
Have fun..
Posted: Fri Feb 22, 2008 3:48 pm
by nick8325
ProtectedMode will run when the processor is in real mode, so you shouldn't have [BITS 32] at the top of the file. Try changing that to [BITS 16], and putting [BITS 32] just above Stage3.
Posted: Tue Feb 26, 2008 9:27 pm
by kg4mxz
So after starting from scratch, I think I have this working, although bochs reports that the processor keeps switching between real and protected mode indefinatly...
Code: Select all
00864717350d[CPU0 ] Enter Protected Mode
00864717350d[CPU0 ] Protected Mode Activated
00864717415d[CPU0 ] Enter Real Mode
00864717415d[CPU0 ] Real Mode Activated
00864717438d[CPU0 ] interrupt(): vector = 16, INT = 1, EXT = 0
00864717439d[CPU0 ] interrupt(): vector = 109, INT = 1, EXT = 0
00864717468d[CPU0 ] interrupt(): vector = 16, INT = 1, EXT = 0
00864717469d[CPU0 ] interrupt(): vector = 109, INT = 1, EXT = 0
00864717562d[CPU0 ] interrupt(): vector = 16, INT = 1, EXT = 0
00864717563d[CPU0 ] interrupt(): vector = 109, INT = 1, EXT = 0
on and on and on... any ideas?
Posted: Wed Feb 27, 2008 1:41 am
by codemastersnake
To Check whether you are in protected mode or not you can always check the PM bit in the cr0 register.
By this method you can use it as simpl funtions to check the Pm w/o have to crash you kernel and making your OS user have to reboot the system