OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Dec 13, 2018 9:09 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 19 posts ]  Go to page Previous  1, 2
Author Message
 Post subject: Re: here are cpuid with raw output
PostPosted: Mon Mar 12, 2018 1:30 pm 
Offline
Member
Member

Joined: Wed Nov 18, 2015 3:04 pm
Posts: 337
Location: San Jose San Francisco Bay Area
Octocontrabass wrote:
ggodw000 wrote:
I wondered if there is a way to fit all the series of instructions in one asm() call making it atomic.

Of course there is. You can put as many instructions in a single asm() call as you want.

But, for CPUID, you don't need more than one instruction. If you use the correct input and output constraints, GCC will automatically generate the instructions to load EAX before CPUID and read the outputs afterwards.

In fact, I already showed you one possible way to write it.
Code:
asm( "cpuid" : "=a"(eax_value), "=b"(ebx_value), "=c"(ecx_value), "=d"(edx_value) : "a"(leaf) );


Reviving the long lost thread, looking at extended asm section of this doc: https://gcc.gnu.org/onlinedocs/gcc/Exte ... tended-Asm, it looks like it fits into the call convention:
Code:
asm [volatile] ( AssemblerTemplate
                 : OutputOperands
                 [ : InputOperands
                 [ : Clobbers ] ])

With that, it appears cpuid is
Code:
assembler template
, output operands are
Code:
"=a"(eax_value), "=b"(ebx_value), "=c"(ecx_value), "=d"(edx_value)
: and inputs operands are "
Code:
a"(leaf)
, correct?
Once executed, the variable (possibly long int) a, b, c and d will hold the result of CPUID which can safely be used.
thx.

_________________
key takeaway after spending yrs on sw industry: big issue small because everyone jumps on it and fixes it. small issue is big since everyone ignores and it causes catastrophy later. #devilisinthedetails


Top
 Profile  
 
 Post subject: Re: here are cpuid with raw output
PostPosted: Mon Mar 12, 2018 1:43 pm 
Offline
Member
Member

Joined: Wed Nov 18, 2015 3:04 pm
Posts: 337
Location: San Jose San Francisco Bay Area
Oh wait, what is a, b, c and d in this case?
I had to declare all of 'em for the compilation to work.
I can see eax_value through edx_value will hold the return values.
Code:
    int a, b, c, d;
    int eax_value, ebx_value, ecx_value, edx_value, leaf;

        printf("\neax_value: %08x", eax_value);
        printf("\nebx_value: %08x", ebx_value);
        printf("\necx_value: %08x", ecx_value);
        printf("\nedx_value: %08x", edx_value);


result:
Code:
Issuing CPUID 0
eax_value: 00000014
ebx_value: 756e6547
ecx_value: 6c65746e
edx_value: 49656e69
----------------------------
Issuing CPUID 1
eax_value: 000406f1
ebx_value: 01040800
ecx_value: 9ed83203
edx_value: 1fabfbff
----------------------------
Issuing CPUID 2
eax_value: 76036301
ebx_value: 00f0b5ff
ecx_value: 00000000
edx_value: 00c30000
----------------------------
Issuing CPUID 3
eax_value: 00000000
ebx_value: 00000000
ecx_value: 00000000
edx_value: 00000000
Done.

_________________
key takeaway after spending yrs on sw industry: big issue small because everyone jumps on it and fixes it. small issue is big since everyone ignores and it causes catastrophy later. #devilisinthedetails


Top
 Profile  
 
 Post subject: Re: here are cpuid with raw output
PostPosted: Tue Mar 13, 2018 10:17 am 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 1361
ggodw000 wrote:
Oh wait, what is a, b, c and d in this case?

The strings "=a", "=b", "=c", "=d", and "a" in the inline assembly are the constraints. The "=" portion is a modifier that indicates an operand that will be written to by the assembly code. The four letters "a", "b", "c", and "d" are specific to the target CPU, and on x86 they indicate that the operand may be placed in EAX, EBX, ECX, or EDX respectively.

ggodw000 wrote:
I had to declare all of 'em for the compilation to work.
Code:
    int a, b, c, d;

You don't have to declare those. Only the expression in the parentheses needs to be declared in the surrounding C code.


Top
 Profile  
 
 Post subject: Re: here are cpuid with raw output
PostPosted: Tue Mar 13, 2018 4:55 pm 
Offline
Member
Member

Joined: Wed Nov 18, 2015 3:04 pm
Posts: 337
Location: San Jose San Francisco Bay Area
Octocontrabass wrote:
ggodw000 wrote:
Oh wait, what is a, b, c and d in this case?

The strings "=a", "=b", "=c", "=d", and "a" in the inline assembly are the constraints. The "=" portion is a modifier that indicates an operand that will be written to by the assembly code. The four letters "a", "b", "c", and "d" are specific to the target CPU, and on x86 they indicate that the operand may be placed in EAX, EBX, ECX, or EDX respectively.

ggodw000 wrote:
I had to declare all of 'em for the compilation to work.
Code:
    int a, b, c, d;

You don't have to declare those. Only the expression in the parentheses needs to be declared in the surrounding C code.

yes that was not necessary.

_________________
key takeaway after spending yrs on sw industry: big issue small because everyone jumps on it and fixes it. small issue is big since everyone ignores and it causes catastrophy later. #devilisinthedetails


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 19 posts ]  Go to page Previous  1, 2

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group