CPU Identification For OS Developers...
- Brynet-Inc
- Member
- Posts: 2426
- Joined: Tue Oct 17, 2006 9:29 pm
- Libera.chat IRC: brynet
- Location: Canada
- Contact:
CPU Identification For OS Developers...
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..)
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 3815 times
Last edited by Brynet-Inc on Tue Apr 10, 2007 5:27 pm, edited 7 times in total.
Re:CPU Identification For OS Development..
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..
Wow! That works great!
All I had to do was adapt it for my prinf() function, then it worked without problems! ;D
All I had to do was adapt it for my prinf() function, then it worked without problems! ;D
- Brynet-Inc
- Member
- Posts: 2426
- Joined: Tue Oct 17, 2006 9:29 pm
- Libera.chat IRC: brynet
- Location: Canada
- Contact:
Re:CPU Identification For OS Development..
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
Re:CPU Identification For OS Development..
Looks like a fine piece of code. Nice job!
Candamir
Candamir
Re:CPU Identification For OS Development..
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
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
- Posts: 2426
- Joined: Tue Oct 17, 2006 9:29 pm
- Libera.chat IRC: brynet
- Location: Canada
- Contact:
Re:CPU Identification For OS Development..
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
Re:CPU Identification For OS Developers...
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
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
- Posts: 2426
- Joined: Tue Oct 17, 2006 9:29 pm
- Libera.chat IRC: brynet
- Location: Canada
- Contact:
Re:CPU Identification For OS Developers...
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.
Last edited by Brynet-Inc on Sun Oct 22, 2006 12:56 am, edited 1 time in total.
Re:CPU Identification For OS Developers...
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
every compiler worth using will warn about code like this:
Code: Select all
int f() {
}
proxy
Re:CPU Identification For OS Developers...
Looks pretty cool. I'll have to try it out!
Thanks,
-Stephen
Thanks,
-Stephen
- Brynet-Inc
- Member
- Posts: 2426
- Joined: Tue Oct 17, 2006 9:29 pm
- Libera.chat IRC: brynet
- Location: Canada
- Contact:
Re:CPU Identification For OS Developers...
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
Re:CPU Identification For OS Developers...
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...
The code uses GCC-specific assembly language syntax. You will probably need to redefine the asm macro.
- Brynet-Inc
- Member
- Posts: 2426
- Joined: Tue Oct 17, 2006 9:29 pm
- Libera.chat IRC: brynet
- Location: Canada
- Contact:
Re:CPU Identification For OS Developers...
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.. ::)
Definitely not meant for usage in MSVC pal, Why anyone would want to use MSVC for OS Development is beyond me.. ::)