Protected mode checks

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
Jabus
Member
Member
Posts: 39
Joined: Sun Jan 07, 2007 7:54 am

Protected mode checks

Post by Jabus »

I am currently changing from real mode to protected mode. Yet most sample code I look at has all these checks to makes usre the processor is protected mode compatable. Is this really neccessary or can is it bad to assume that the majority of processors support protected mode?
User avatar
JAAman
Member
Member
Posts: 879
Joined: Wed Oct 27, 2004 11:00 pm
Location: WA

Post by JAAman »

the only thing really necessary to ensure support of PMode is to make sure its at least a 386 CPU -- and your not likely to find one thats not

there are a couple problems with a few early 386s in PMode, but its not common (some were recalled, then rereleased as "16bit only" chips -- but there is no way to identify them by software)

MS bootcode doesnt bother to check, and just assumes you have at least a 386, however i prefer to be more cautious -- the 386 check is quite simple:

Code: Select all

MOV AX, 0xF000           ; set all the high bits to 1
PUSHF                    ; save state of flags register before modifying
PUSH AX
POPF                     ; move AX into FLAGS
PUSHF
POP AX                   ; move it back -- anything in flags forced to a specific value is changed
POPF                     ; restore previous flags
AND AH,0xF0              ; mask out the bottom part, and move it into flags for checking
JS no386                 ; the sign bit is only set on 8086/8
JZ no386                 ; bits 12:15 0 on start, and cannot be changed in RMode on 286, but can on 386



:386code                 ; if you get here you have a 386
standard disclaimer:
this code is public domain, and can be used by anyone for any reason, and modified in any way -- im not responsible for any problems with this code


most sample code, probably does more checking for other chips:
to detect 486: test to see if you can modify the AC bit in FLAGS
to detect CPUID (in later 486s and all P5 and above): can you modify the ID bit in FLAGS
all detection of more advanced chips should start with CPUID -- which is quite complicated (find more about CPUID in the intel manual, volume 2a, chapter 3: CPUID, and then specific results information from the manufacter of your particular CPU)


more information about detecting older CPUs can be found in intel manuals, but for some reason i cant find where :( sorry
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 »

If you were aiming to support later 486 models and higher.. Something like this could be adequate-ish.

Code: Select all

int main(void) 
{
	unsigned long eax, ecx;
	__asm__ __volatile__("pushf; pop %0; mov %0, %1; xor $0x200000, %0; push %0; popf; pushf; pop %0" : "=a" (eax), "=c" (ecx) : : "cc");
	if(eax == ecx)
	{
		printf("CPUID Not Supported!\n");
		return -1;
	} else {
		printf("CPUID Supported Detected!\n");
		return 0;
	}
}
But obviously this would still fail to detect some early 32bit compatible x86 CPU's.

Use JAAman's code if it works for you.
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
Post Reply