Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 30 Jul 2013 12:16:15 -0400
From:      John Baldwin <jhb@freebsd.org>
To:        Matthew Fleming <mdf@freebsd.org>
Cc:        "svn-src-head@freebsd.org" <svn-src-head@freebsd.org>, "svn-src-all@freebsd.org" <svn-src-all@freebsd.org>, "src-committers@freebsd.org" <src-committers@freebsd.org>, Dimitry Andric <dim@freebsd.org>
Subject:   Re: svn commit: r253802 - head/contrib/llvm/tools/clang/lib/Headers
Message-ID:  <201307301216.15235.jhb@freebsd.org>
In-Reply-To: <CAMBSHm8xDvWsKJiLAzPF8azMKLHq3WWWQ3X1d6-6nJSAppux9Q@mail.gmail.com>
References:  <201307301233.r6UCXLT8012177@svn.freebsd.org> <CAMBSHm8xDvWsKJiLAzPF8azMKLHq3WWWQ3X1d6-6nJSAppux9Q@mail.gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On Tuesday, July 30, 2013 10:09:35 am Matthew Fleming wrote:
> On Tue, Jul 30, 2013 at 5:33 AM, Dimitry Andric <dim@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



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