CPU Identification For OS Developers...
- Brynet-Inc
- Member
Member - Posts: 2426
- Joined: Tue Oct 17, 2006 9:29 pm
- Libera.chat IRC: brynet
- Location: Canada
- Contact:
CPU Identification For OS Developers...
Post by Brynet-Inc »
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..)
- Attachments
-
- cpudet-clean.c
- Latest cpudet code.
- (9.06 KiB) Downloaded 4049 times
- Habbit
Re:CPU Identification For OS Development..
Post by Habbit »
- piranha
Re:CPU Identification For OS Development..
Post by piranha »
All I had to do was adapt it for my prinf() function, then it worked without problems! ;D ;)
- Brynet-Inc
- Member
Member - Posts: 2426
- Joined: Tue Oct 17, 2006 9:29 pm
- Libera.chat IRC: brynet
- Location: Canada
- Contact:
Re:CPU Identification For OS Development..
Post by Brynet-Inc »
I'm very glad it worked out for you, :) Enjoy it.piranha wrote: Wow! That works great!
All I had to do was adapt it for my prinf() function, then it worked without problems! ;D ;)
- Candamir
Re:CPU Identification For OS Development..
Post by Candamir »
Candamir
- Shecks
Re:CPU Identification For OS Development..
Post by Shecks »
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
- Brynet-Inc
- Member
Member - Posts: 2426
- Joined: Tue Oct 17, 2006 9:29 pm
- Libera.chat IRC: brynet
- Location: Canada
- Contact:
Re:CPU Identification For OS Development..
Post by Brynet-Inc »
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 ::).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
- proxy
Re:CPU Identification For OS Developers...
Post by proxy »
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
- Brynet-Inc
- Member
Member - Posts: 2426
- Joined: Tue Oct 17, 2006 9:29 pm
- Libera.chat IRC: brynet
- Location: Canada
- Contact:
Re:CPU Identification For OS Developers...
Post by Brynet-Inc »
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.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
;)
Take care, And Thanks again.
- proxy
Re:CPU Identification For OS Developers...
Post by proxy »
every compiler worth using will warn about code like this:
Code: Select all
int f() {
}
proxy
- srg_13
Re:CPU Identification For OS Developers...
Post by srg_13 »
Thanks,
-Stephen
- Brynet-Inc
- Member
Member - Posts: 2426
- Joined: Tue Oct 17, 2006 9:29 pm
- Libera.chat IRC: brynet
- Location: Canada
- Contact:
Re:CPU Identification For OS Developers...
Post by Brynet-Inc »
Alright, Alright, I made some cleanup's, Hopefully this is acceptable :), Compiles with no warnings/errors with -Wall and -W.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.Code: Select all
int f() { }
proxy
Thanks Again ;)
- smiddy
Re:CPU Identification For OS Developers...
Post by smiddy »
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)
- oswizard
Re:CPU Identification For OS Developers...
Post by oswizard »
- Brynet-Inc
- Member
Member - Posts: 2426
- Joined: Tue Oct 17, 2006 9:29 pm
- Libera.chat IRC: brynet
- Location: Canada
- Contact:
Re:CPU Identification For OS Developers...
Post by Brynet-Inc »
Definitely not meant for usage in MSVC pal, Why anyone would want to use MSVC for OS Development is beyond me.. ::)