Page 1 of 4
CPU Identification For OS Developers...
Posted: Tue Sep 26, 2006 8:28 am
by Brynet-Inc
Recently when working on a OS-Dev project I wrote code for detecting both AMD and Intel 32bit x86 CPU's, The code is written in C(with one line of inline asm..) and compiles in GCC with with OS-Dev projects in mind.
Features
Detects basically every x86 cpu supporting the CPUID instruction.
Supports 386/486 CPU's with CPUID (There are a few..)
Supports The following CPU's..
Intel 486/586/686 and compatible AMD CPU's..
Also supports Pentium Pro/Pentium 1/2/3/4 and later AMD models including Athlon,Duron,Athlon MP/Mobile, K6, K6-2, K6-III, and early K5's..
Example Output on an Athlon XP 2600+
AMD-specific functions
Family: 6 Model: 10 [Duron/Athlon model 10]
Detected Processor Name: AMD Athlon(TM) XP 2600+
Only has 1 external dependency! requires a fairly(ish) compliant printf, Decimal and String support like %s and %d and this is truly all you need!
Comments/Questions? Death Threats? Post them here
(All under the BSD license so feel free to use this in your closed source OS also..)
Re:CPU Identification For OS Development..
Posted: Tue Sep 26, 2006 4:51 pm
by Habbit
Well, I haven't had time to test it, but I must say it looks like quite a good and thorough job. Keep it up!
Re:CPU Identification For OS Development..
Posted: Tue Sep 26, 2006 7:18 pm
by piranha
Wow! That works great!
All I had to do was adapt it for my prinf() function, then it worked without problems! ;D
Re:CPU Identification For OS Development..
Posted: Tue Sep 26, 2006 7:22 pm
by Brynet-Inc
piranha wrote:
Wow! That works great!
All I had to do was adapt it for my prinf() function, then it worked without problems! ;D
I'm very glad it worked out for you,
Enjoy it.
Re:CPU Identification For OS Development..
Posted: Wed Sep 27, 2006 6:09 pm
by Candamir
Looks like a fine piece of code. Nice job!
Candamir
Re:CPU Identification For OS Development..
Posted: Wed Sep 27, 2006 6:30 pm
by Shecks
Hi,
First off, nice piece of code.
There might be a little potential bug there though. I notice in detect_cpu() you declare an unsigned long 'maxi' and pass it to both do_intel() and do_amd() but it is never initailised (unless i've missed something) . Both sub functions test maxi for >= 1 but it is never explicitly set?
Is this a typo? or am i loosing the plot :-\
Shecks
Re:CPU Identification For OS Development..
Posted: Wed Sep 27, 2006 6:50 pm
by Brynet-Inc
Shecks wrote:
Hi,
First off, nice piece of code.
There might be a little potential bug there though. I notice in detect_cpu() you declare an unsigned long 'maxi' and pass it to both do_intel() and do_amd() but it is never initailised (unless i've missed something) . Both sub functions test maxi for >= 1 but it is never explicitly set?
Is this a typo? or am i loosing the plot :-\
Shecks
Yes, Sorry, Thanks for pointing this out, The codebase used to be much larger and that variable was actually used for something at one time ::).
Re:CPU Identification For OS Developers...
Posted: Thu Sep 28, 2006 4:56 pm
by proxy
generally a good piece of code, and a solid example of how to properly detect CPU types and such.
Now here's come the nit-picking
Control reaches the end of several functions defined to return int.
A few funtions are defined to return int for no apparent reason (some of these overlap with the ones which reach the end with no return!)
some functions (int do_intel and int do_amd) are used before the defintion in the file, which is technically ok in c89, but produces an avoidable warning.
finally, the integer constant 0x80000002 is unsigned but being used in a for loop where j is signed, thus you get an avoidable warning involving comparison of signed/unsigned comparison mis-match.
like i said, good code overall, just had to get out my pedantic side for a bit
good job
proxy
Re:CPU Identification For OS Developers...
Posted: Thu Sep 28, 2006 6:07 pm
by Brynet-Inc
proxy wrote:
generally a good piece of code, and a solid example of how to properly detect CPU types and such.
Now here's come the nit-picking
Control reaches the end of several functions defined to return int.
A few funtions are defined to return int for no apparent reason (some of these overlap with the ones which reach the end with no return!)
some functions (int do_intel and int do_amd) are used before the defintion in the file, which is technically ok in c89, but produces an avoidable warning.
finally, the integer constant 0x80000002 is unsigned but being used in a for loop where j is signed, thus you get an avoidable warning involving comparison of signed/unsigned comparison mis-match.
like i said, good code overall, just had to get out my pedantic side for a bit
good job
proxy
Thanks for the criticism and the compliments
, I'm generally a bad at keeping with whats standard C89 and C99, But from my POV I don't get any errors, or warnings, So I'm happy as a clam.
Take care, And Thanks again.
Re:CPU Identification For OS Developers...
Posted: Thu Sep 28, 2006 7:34 pm
by proxy
if you are not getting warnings in with this code, then you are not compiling with a reasonable warning level. gcc you shoudl be using "-Wall" (preferably "-W -Wall" if you wanna be extra strict), and in msvc you should be using something like warning level 3 for decent useful warnings.
every compiler worth using will warn about code like this:
because it is declared as returning an int, but there is no actual return statement.
proxy
Re:CPU Identification For OS Developers...
Posted: Thu Sep 28, 2006 7:59 pm
by srg_13
Looks pretty cool. I'll have to try it out!
Thanks,
-Stephen
Re:CPU Identification For OS Developers...
Posted: Fri Sep 29, 2006 8:08 am
by Brynet-Inc
proxy wrote:
if you are not getting warnings in with this code, then you are not compiling with a reasonable warning level. gcc you shoudl be using "-Wall" (preferably "-W -Wall" if you wanna be extra strict), and in msvc you should be using something like warning level 3 for decent useful warnings.
every compiler worth using will warn about code like this:
because it is declared as returning an int, but there is no actual return statement.
proxy
Alright, Alright, I made some cleanup's, Hopefully this is acceptable
, Compiles with no warnings/errors with -Wall and -W.
Thanks Again
Re:CPU Identification For OS Developers...
Posted: Fri Sep 29, 2006 8:18 pm
by smiddy
Here's what I get with C++ 6.0:
Code: Select all
Deleting intermediate files and output files for project 'cpudet-fix - Win32 Debug'.
--------------------Configuration: cpudet-fix - Win32 Debug--------------------
Compiling...
cpudet-fix.c
c:\smiddyos\cpuid c\cpudet-fix.c(55) : warning C4013: 'asm' undefined; assuming extern returning int
c:\smiddyos\cpuid c\cpudet-fix.c(55) : error C2143: syntax error : missing ')' before ':'
c:\smiddyos\cpuid c\cpudet-fix.c(65) : warning C4013: 'printf' undefined; assuming extern returning int
c:\smiddyos\cpuid c\cpudet-fix.c(75) : error C2143: syntax error : missing ';' before 'type'
c:\smiddyos\cpuid c\cpudet-fix.c(76) : error C2143: syntax error : missing ';' before 'type'
c:\smiddyos\cpuid c\cpudet-fix.c(77) : error C2143: syntax error : missing ';' before 'type'
c:\smiddyos\cpuid c\cpudet-fix.c(78) : error C2143: syntax error : missing ')' before ':'
c:\smiddyos\cpuid c\cpudet-fix.c(79) : error C2065: 'stepping' : undeclared identifier
c:\smiddyos\cpuid c\cpudet-fix.c(79) : error C2065: 'eax' : undeclared identifier
c:\smiddyos\cpuid c\cpudet-fix.c(80) : error C2065: 'model' : undeclared identifier
c:\smiddyos\cpuid c\cpudet-fix.c(81) : error C2065: 'family' : undeclared identifier
c:\smiddyos\cpuid c\cpudet-fix.c(82) : error C2065: 'type' : undeclared identifier
c:\smiddyos\cpuid c\cpudet-fix.c(83) : error C2065: 'reserved' : undeclared identifier
c:\smiddyos\cpuid c\cpudet-fix.c(122) : error C2065: 'extended_family' : undeclared identifier
c:\smiddyos\cpuid c\cpudet-fix.c(228) : error C2143: syntax error : missing ';' before 'type'
c:\smiddyos\cpuid c\cpudet-fix.c(229) : error C2143: syntax error : missing ';' before 'type'
c:\smiddyos\cpuid c\cpudet-fix.c(230) : error C2143: syntax error : missing ')' before ':'
c:\smiddyos\cpuid c\cpudet-fix.c(287) : error C2143: syntax error : missing ')' before ':'
c:\smiddyos\cpuid c\cpudet-fix.c(299) : error C2143: syntax error : missing ')' before ':'
Error executing cl.exe.
cpudet-fix.exe - 17 error(s), 2 warning(s)
Re:CPU Identification For OS Developers...
Posted: Fri Sep 29, 2006 8:44 pm
by oswizard
The code uses GCC-specific assembly language syntax. You will probably need to redefine the asm macro.
Re:CPU Identification For OS Developers...
Posted: Fri Sep 29, 2006 9:31 pm
by Brynet-Inc
Yeah smiddy, I use OpenBSD and if you can see from my signature I do not care for Windows, This is for OSdev projects..It should compile safely in MinGW/GCC if you absolutely have to compile it in Windows, But I never recommend using Windows for anything at all, ;D
Definitely not meant for usage in MSVC pal, Why anyone would want to use MSVC for OS Development is beyond me.. ::)