Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 30 Oct 2013 14:33:01 +0100
From:      Tijl Coosemans <tijl@coosemans.org>
To:        Bruce Cran <bruce@cran.org.uk>
Cc:        FreeBSD Questions <freebsd-questions@freebsd.org>
Subject:   Re: Code segfaults with clang -O1, works with gcc49 and clang -O2
Message-ID:  <20131030143301.623be910@kalimero.tijl.coosemans.org>
In-Reply-To: <5270D99C.6070901@cran.org.uk>
References:  <5270D99C.6070901@cran.org.uk>

next in thread | previous in thread | raw e-mail | index | archive | help
On Wed, 30 Oct 2013 10:04:12 +0000 Bruce Cran wrote:
> The following code works (on FreeBSD 11-CURRENT) with gcc49 or clang -O2 
> or higher, but segfaults in do_cpuid() with clang -O1 or lower. Is there 
> a bug in the asm statement, or is clang doing something wrong?
> 
> #include <stdio.h>
> 
> static inline void do_cpuid(unsigned int *eax, unsigned int *ebx,
>                              unsigned int *ecx, unsigned int *edx)
> {
>          asm volatile("cpuid"
>                  : "=a" (*eax), "=b" (*ebx), "=r" (*ecx), "=d" (*edx)

Should be: "=c" (*ecx)

But you can also use the '+' modifier and remove the input operands:

: "+a" (*eax), "=b" (*ebx), "+c" (*ecx), "=d" (*edx)
:
: "memory"


>                  : "0" (*eax), "2" (*ecx)
>                  : "memory");
> }
> 
> int main(int argc, char **argv)
> {
>      unsigned int a = 0, b = 0, c = 0, d = 0;
>      do_cpuid(&a, &b, &c, &d);
>      printf("%d %d %d %d\n", a, b, c, d);
>      return 0;
> }
> 
> 




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20131030143301.623be910>