svn commit: r253802 - head/contrib/llvm/tools/clang/lib/Headers

John Baldwin jhb at freebsd.org
Tue Jul 30 16:18:46 UTC 2013


On Tuesday, July 30, 2013 10:09:35 am Matthew Fleming wrote:
> On Tue, Jul 30, 2013 at 5:33 AM, Dimitry Andric <dim at freebsd.org> wrote:
> 
> > Author: dim
> > Date: Tue Jul 30 12:33:21 2013
> > New Revision: 253802
> > URL: http://svnweb.freebsd.org/changeset/base/253802
> >
> > Log:
> >   Pull in r186696 from upstream clang trunk:
> >
> >     This patch implements __get_cpuid_max() as an inline and __cpuid()
> >     and __cpuid_count() as macros to be compatible with GCC's cpuid.h.
> >     It also adds bit_<foo> constants for the various feature bits as
> >     described in version 039 (May 2011) of Intel's SDM Volume 2 in the
> >     description of the CPUID instruction.  The list of bit_<foo>
> >     constants is a bit exhaustive (GCC doesn't do near this many).  More
> >     bits could be added from a newer version of SDM if desired.
> >
> >     Patch by John Baldwin!
> >
> >   This should fix several ports which depend on this functionality being
> >   available.
> >
> >   MFC after:    1 week
> >
> > Modified:
> >   head/contrib/llvm/tools/clang/lib/Headers/cpuid.h
> >
> > Modified: head/contrib/llvm/tools/clang/lib/Headers/cpuid.h
> >
> > 
==============================================================================
> > --- head/contrib/llvm/tools/clang/lib/Headers/cpuid.h   Tue Jul 30
> > 12:17:45 2013        (r253801)
> > +++ head/contrib/llvm/tools/clang/lib/Headers/cpuid.h   Tue Jul 30
> > 12:33:21 2013        (r253802)
> > +/* PIC on i386 uses %ebx, so preserve it. */
> > +#if __i386__
> > +#define __cpuid(__level, __eax, __ebx, __ecx, __edx) \
> > +    __asm("  pushl  %%ebx\n" \
> > +          "  cpuid\n" \
> > +          "  mov    %%ebx,%1\n" \
> > +          "  popl   %%ebx" \
> > +        : "=a"(__eax), "=r" (__ebx), "=c"(__ecx), "=d"(__edx) \
> > +        : "0"(__level))
> > +
> > +#define __cpuid_count(__level, __count, __eax, __ebx, __ecx, __edx) \
> > +    __asm("  pushl  %%ebx\n" \
> > +          "  cpuid\n" \
> > +          "  mov    %%ebx,%1\n" \
> > +          "  popl   %%ebx" \
> > +        : "=a"(__eax), "=r" (__ebx), "=c"(__ecx), "=d"(__edx) \
> > +        : "0"(__level), "2"(__count))
> > +#else
> > +#define __cpuid(__level, __eax, __ebx, __ecx, __edx) \
> > +    __asm("cpuid" : "=a"(__eax), "=b" (__ebx), "=c"(__ecx), "=d"(__edx) \
> > +                  : "0"(__level))
> > +
> > +#define __cpuid_count(__level, __count, __eax, __ebx, __ecx, __edx) \
> > +    __asm("cpuid" : "=a"(__eax), "=b" (__ebx), "=c"(__ecx), "=d"(__edx) \
> > +                  : "0"(__level), "2"(__count))
> > +#endif
> >
> 
> PIC mode on amd64 also uses %ebx.  The difference is that FreeBSD makefiles
> set -fPIC for i386 kernel compile but not amd64.  Locally we use -fPIC for
> amd64 (it was added 6 years ago to our environment because it gave better
> kernel debugging).

Note that this is used in userland and the kernel.

> Anyways, is there some way to detect PIC mode and use that to decide
> whether to use %ebx for the cpuid instruction, rather than using i386?

Does clang supply a reliable #define to indicate that PIC is in use?  If not,
then this should use the PIC path always to be safe.

-- 
John Baldwin


More information about the svn-src-head mailing list