Testing Processor Mode

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
kg4mxz
Posts: 7
Joined: Wed Feb 20, 2008 6:54 pm

Testing Processor Mode

Post 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
cyr1x
Member
Member
Posts: 207
Joined: Tue Aug 21, 2007 1:41 am
Location: Germany

Post by cyr1x »

Just call an interrupt, e.g.

Code: Select all

 int 0x10 
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.
User avatar
Dex
Member
Member
Posts: 1444
Joined: Fri Jan 27, 2006 12:00 am
Contact:

Post 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.
kg4mxz
Posts: 7
Joined: Wed Feb 20, 2008 6:54 pm

Post 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...
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Post 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..
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
nick8325
Member
Member
Posts: 200
Joined: Wed Oct 18, 2006 5:49 am

Post 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.
kg4mxz
Posts: 7
Joined: Wed Feb 20, 2008 6:54 pm

Post 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?
User avatar
codemastersnake
Member
Member
Posts: 148
Joined: Sun Nov 07, 2004 12:00 am
Contact:

Post 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 ;)
Post Reply