Kernel Newbies archive on lore.kernel.org
 help / color / Atom feed
* How to comprehend this code snippet: __asm__ __volatile__("rdtsc" : "=A"(t))?
@ 2020-07-16 12:20 孙世龙 sunshilong
  2020-07-16 12:52 ` Jeffrey Walton
  0 siblings, 1 reply; 4+ messages in thread
From: 孙世龙 sunshilong @ 2020-07-16 12:20 UTC (permalink / raw)
  To: kernelnewbies

Hi, list

Here is the code snippet:
#define ipipe_read_tsc(t)              \
__asm__ __volatile__("rdtsc" : "=A"(t))

I found that the rdtsc (Read Time-Stamp Counter) instruction is used
to determine how many CPU ticks took place since the processor was
reset.

But what does
"=A"(t)
mean?

Thank you for your attention to this matter.

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: How to comprehend this code snippet: __asm__ __volatile__("rdtsc" : "=A"(t))?
  2020-07-16 12:20 How to comprehend this code snippet: __asm__ __volatile__("rdtsc" : "=A"(t))? 孙世龙 sunshilong
@ 2020-07-16 12:52 ` Jeffrey Walton
  2020-07-16 13:24   ` 孙世龙 sunshilong
  0 siblings, 1 reply; 4+ messages in thread
From: Jeffrey Walton @ 2020-07-16 12:52 UTC (permalink / raw)
  To: 孙世龙 sunshilong; +Cc: kernelnewbies

On Thu, Jul 16, 2020 at 8:22 AM 孙世龙 sunshilong <sunshilong369@gmail.com> wrote:
>
> Here is the code snippet:
> #define ipipe_read_tsc(t)              \
> __asm__ __volatile__("rdtsc" : "=A"(t))

I hope that is i386 only, and not x86_64.

> I found that the rdtsc (Read Time-Stamp Counter) instruction is used
> to determine how many CPU ticks took place since the processor was
> reset.
>
> But what does
> "=A"(t)
> mean?

The '=A' is a GCC machine constraint for i386 and an output operand.
The 'A' is the constraint EAX:RDX register pair. The '=' means it is
being written to.

Also see https://gcc.gnu.org/onlinedocs/gcc/Machine-Constraints.html
and https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html.

Also read the note in the Machine Constraints for:

    unsigned long long rdtsc (void)
    {
      unsigned long long tick;
      __asm__ __volatile__("rdtsc":"=A"(tick));
      return tick;
    }

The manual says the pattern is wrong for x86_64.

Jeff

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: How to comprehend this code snippet: __asm__ __volatile__("rdtsc" : "=A"(t))?
  2020-07-16 12:52 ` Jeffrey Walton
@ 2020-07-16 13:24   ` 孙世龙 sunshilong
  2020-07-16 13:42     ` Jeffrey Walton
  0 siblings, 1 reply; 4+ messages in thread
From: 孙世龙 sunshilong @ 2020-07-16 13:24 UTC (permalink / raw)
  To: noloader; +Cc: kernelnewbies

Hi, Jeff

Thank you for taking the time to respond to my question.
Thanks to your help, I have a deeper understanding of this matter.

>Also read the note in the Machine Constraints for:
>   unsigned long long rdtsc (void)
>    {
>      unsigned long long tick;
>     __asm__ __volatile__("rdtsc":"=A"(tick));
>      return tick;
>    }
>The manual says the pattern is wrong for x86_64.
Thank you for your notification. I check it and find it's for X86_32.
I didn't notice such differences before.
Here is the full related code snippet:

#ifdef CONFIG_X86_32
#define ipipe_read_tsc(t)              \
   __asm__ __volatile__("rdtsc" : "=A"(t))
#else  /* X86_64 */
#define ipipe_read_tsc(t)  do {        \
   unsigned int __a,__d;           \
   asm volatile("rdtsc" : "=a" (__a), "=d" (__d)); \
   (t) = ((unsigned long)__a) | (((unsigned long)__d)<<32); \
} while(0)
#endif

>The 'A' is the constraint EAX:RDX register pair.
>Also see https://gcc.gnu.org/onlinedocs/gcc/Machine-Constraints.html
>and https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html.
Thanks to your attached document, I find a lot of useful information.
BTW,  What does "The A register", "The B register" and etc mean?
I googled but didn't get any useful information.
Could you please give me a few brief explanations or suggest some
documents for me to go through?

Thank you for your attention to this matter.
Looking forward to hearing from you.
Best Wishes.
sunshilong

On Thu, Jul 16, 2020 at 8:52 PM Jeffrey Walton <noloader@gmail.com> wrote:
>
> On Thu, Jul 16, 2020 at 8:22 AM 孙世龙 sunshilong <sunshilong369@gmail.com> wrote:
> >
> > Here is the code snippet:
> > #define ipipe_read_tsc(t)              \
> > __asm__ __volatile__("rdtsc" : "=A"(t))
>
> I hope that is i386 only, and not x86_64.
>
> > I found that the rdtsc (Read Time-Stamp Counter) instruction is used
> > to determine how many CPU ticks took place since the processor was
> > reset.
> >
> > But what does
> > "=A"(t)
> > mean?
>
> The '=A' is a GCC machine constraint for i386 and an output operand.
> The 'A' is the constraint EAX:RDX register pair. The '=' means it is
> being written to.
>
> Also see https://gcc.gnu.org/onlinedocs/gcc/Machine-Constraints.html
> and https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html.
>
> Also read the note in the Machine Constraints for:
>
>     unsigned long long rdtsc (void)
>     {
>       unsigned long long tick;
>       __asm__ __volatile__("rdtsc":"=A"(tick));
>       return tick;
>     }
>
> The manual says the pattern is wrong for x86_64.
>
> Jeff

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: How to comprehend this code snippet: __asm__ __volatile__("rdtsc" : "=A"(t))?
  2020-07-16 13:24   ` 孙世龙 sunshilong
@ 2020-07-16 13:42     ` Jeffrey Walton
  0 siblings, 0 replies; 4+ messages in thread
From: Jeffrey Walton @ 2020-07-16 13:42 UTC (permalink / raw)
  To: 孙世龙 sunshilong; +Cc: kernelnewbies

On Thu, Jul 16, 2020 at 9:25 AM 孙世龙 sunshilong <sunshilong369@gmail.com> wrote:
>
> >The 'A' is the constraint EAX:RDX register pair.
> >Also see https://gcc.gnu.org/onlinedocs/gcc/Machine-Constraints.html
> >and https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html.
> Thanks to your attached document, I find a lot of useful information.
> BTW,  What does "The A register", "The B register" and etc mean?
> I googled but didn't get any useful information.
> Could you please give me a few brief explanations or suggest some
> documents for me to go through?

See the docs for the various constraints. Also see
https://gcc.gnu.org/onlinedocs/gcc/Constraints.html#Constraints.

Jeff

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, back to index

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-16 12:20 How to comprehend this code snippet: __asm__ __volatile__("rdtsc" : "=A"(t))? 孙世龙 sunshilong
2020-07-16 12:52 ` Jeffrey Walton
2020-07-16 13:24   ` 孙世龙 sunshilong
2020-07-16 13:42     ` Jeffrey Walton

Kernel Newbies archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/kernelnewbies/0 kernelnewbies/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 kernelnewbies kernelnewbies/ https://lore.kernel.org/kernelnewbies \
		kernelnewbies@kernelnewbies.org
	public-inbox-index kernelnewbies

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernelnewbies.kernelnewbies


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git