Tcl Source Code

View Ticket
Login
Ticket UUID: 3601804
Title: platformCPUID segmentation fault on Darwin
Type: Bug Created on: 2013-01-22 19:49:45
Submitter: pooryorick Assigned to: nijtmans
Subsystem: 38. Init - Library - Autoload Severity:
Priority: 5 Medium Last modified: 2013-04-15 17:07:16
Status: Closed Closed by: nijtmans
Resolution: Fixed Closed on: 2013-04-15 10:07:16
Version: current: 8.6.0
Description:
platform-3.1 results in segmentation fault.  the CPUID instruction is documented to clobber eax, ebx, ecx, and edx.  The code clobbers edi, so chaning the list of clobbers in the assembly code in TclWinCPUID may be a good fix for the problem.  It cured the segmentation fault in my case:

"edi","ebx","ecx","edx"

"eax" does not need to be listed, since the code expects it to be clobbered.
User Comments:
dgp added on 2013-01-23 02:54:24:
Passing to test originator.

nijtmans added on 2013-01-23 05:00:30:
Good analysis, Thanks! However, ecx and edx are
already output registers, so they cannot be mentioned
twice. gcc will complain with "can’t find a register in class ‘DREG’ while reloading ‘asm’"

However, "ebx" was indeed missing as clobber register.

Fixed now in core-8-4-branch, core-8-5-branch and trunk.

nijtmans added on 2013-01-25 04:35:34:
Had to revert the change on all branches: on i386,
ebx is the PIC register, which cannot be used
in assembler. The asm code already saves
ebx before the cpuid, and restores it afterwards,
so in reality ebx is not clobbered at all: The
original code was correct......

Poor, what platform are you using?

nijtmans added on 2013-01-25 04:49:30:
>Poor, what platform are you using?

Just read from the title that it was Darwin..... What version?

nijtmans added on 2013-01-25 05:04:14:
Found some other cpuid example code for Darwin. Committed a version now that doesn't use the edi register any more.

Does that help?

pooryorick added on 2013-01-26 02:39:14:
This host is Mac OS X 10.6.8 (Darwin 10.8.0).  Still getting a segmentation fault with your latest changes.

andreas_kupries added on 2013-01-26 02:53:54:
Where and why do we need this CPUID stuff in the Tcl core ?

nijtmans added on 2013-01-26 22:08:45:
The CPUID stuff came in the core for Cygwin, but
actually it's processor-specific, not OS-specific. So it
should simply work fine on any Intel or compatible
processor, even on Darwin and Solaris.

My big question is: Why is pooryorick the only
one getting a segfault? This code is already in
for more than half a year, and nobody else
noticed a segmentation fault on Darwin
in any of the past releases.

This sounds to me like a "Works for Me", but
I'm willing to give it some more time until
someone comes up with a bright idea
what's going on here. My best guess would
be a buggy compiler.

Regards,
       Jan Nijtmans

pooryorick added on 2013-01-26 23:14:15:
I've found that using the 64-bit register names rbx and rsi with Jan's latest changes resolves  the segmentation fault problem.  I think the same would probably be true with the original code.

pooryorick added on 2013-01-26 23:43:52:
Here is an option that specifies fewer registers:

        __asm__ __volatile__(
                "pushq %%rbx     \n\t" /* save %rbx */
                "cpuid            \n\t"
                "movl %%ebx ,%[rbx]  \n\t" /* write the result into output var */
                "popq %%rbx \n\t"
                : "=a"(regs[0]), [rbx] "=r"(regs[1]), "=c"(regs[2]), "=d"(regs[3])
                : "a"(index));

nijtmans added on 2013-01-27 00:00:21:
So, that's the point:  the problem only occurs on 64-bit Darwin. This
means that we cannot have the same assembler-code in 32-bit
and 64-bit machines.

Should be fixed now on all branches.

Thanks!

pooryorick added on 2013-01-27 01:37:26:
I think configure.in needs to be updated as well.

msofer added on 2013-01-27 01:41:03:
Shouldn't this code be ifdef'ed out of the core for all non-cygwin builds? Why is it needed at all?

nijtmans added on 2013-01-27 04:03:43:
> Why is it needed at all?
It could be needed by any code which wants to detect what
kind of processor the system has. Currently, only "clock" is
using it on Windows, but any platform could use it, provided
the processor is Intel-compatible. See also TIP #377, which
was rejected at that time, but it could easily be implemented
for Intel based on cpuid.
> I think configure.in needs to be updated as well.
No, the problem here can only be detected at run-time,
and "configure" doesn't run the code. So there is
no way configure could detect this.
>Shouldn't this code be ifdef'ed out ...
Yes, it should be ifdefed out if the processor is not
Intel, that's exactly what "configure" does.

The problem that pouryorick found has no
relation with cpuid, it's the code that gcc
generates around it which is problematic.
The compiler option -fPIC needs a special
register, and when cpuid uses the same
register, that's the problem that needs to
be worked around. Without -fPIC it would
work fine without any of those tricks.

So, I' glad that this is fixed now. Case closed.

nijtmans added on 2013-04-15 17:07:16:

allow_comments - 1