Hello. On x86 (32/64) gcc uses FS register for TLS. When writing a signal handler, a special care must be taken: FS register must be restored by hands, together with all other segment registers, because linux unfortunately does not do that (except cs and ds). Then signal handler can access the thread-local data safely... except that it can't. After calling the function that restores the segment registers, I need to specify that FS was clobbered, which is currently not possible with the inline asm. As a result, the following code is generated: 0x00000000004438c0 <+0>: push %r14 0x00000000004438c2 <+2>: mov %edi,%r14d 0x00000000004438c5 <+5>: push %r13 0x00000000004438c7 <+7>: mov %rdx,%r13 0x00000000004438ca <+10>: push %r12 0x00000000004438cc <+12>: lea 0x28(%rdx),%r12 0x00000000004438d0 <+16>: push %rbp 0x00000000004438d1 <+17>: mov %r12,%rdi 0x00000000004438d4 <+20>: mov %fs:0x0,%rbp 0x00000000004438dd <+29>: push %rbx 0x00000000004438de <+30>: add $0xffffffffffffff80,%rsp 0x00000000004438e2 <+34>: callq 0x4441b0 <init_handler> 0x00000000004438e7 <+39>: mov 0x35070a(%rip),%rbx # 0x793ff8 0x00000000004438ee <+46>: mov 0x0(%rbp,%rbx,1),%eax <-- STACK FAULT init_handler() is a function that restore the segregs - it is called first in a function: --- { init_handler(scp); // asm volatile("":::"fs"); fault_cnt++; --- fault_cnt is a thread-local var: extern volatile __thread int fault_cnt; As you can see, gcc doesn't move the access to fault_cnt below init_handler(), but, unfortunately, it moved the address calculation. To prevent this, I need to clobber the FS, but it doesn't seem to be possible. Please let me know if there are any work-arounds to that. It seems to be a bug that gcc does not allow clobbering the segregs that it use.
Please note that the shown code was generated with gcc-4.8.2, but inability to clobber segreg with inline asm was tested with gcc-5.1.1. A bit of dissonance here, I can't do both tests with similar gcc. Is there some magic -mno-xxx option that will prevent gcc from moving the addr caclulus too far away from the access to the volatile var?
Can you better describe your use case because this is not obvious what you are trying to achieve here.
The signal handler needs to do the following things: 1. Restore segment registers (init_handler() func) 2. Increment thread-local var (fault_cnt) 3. Do stuff including reads of fault_cnt var Being thread-local, fault_cnt var is being accessed with the help of FS, but FS is only valid after the call to init_handler(). Since fault_cnt is marked as volatile, gcc does not move the access to it above the init_handler() call, so that part is fine. What is NOT fine is this: 0x00000000004438d4 <+20>: mov %fs:0x0,%rbp <-- (bad) TLS access 0x00000000004438dd <+29>: push %rbx 0x00000000004438de <+30>: add $0xffffffffffffff80,%rsp 0x00000000004438e2 <+34>: callq 0x4441b0 <init_handler> <-- FS now valid 0x00000000004438e7 <+39>: mov 0x35070a(%rip),%rbx # 0x793ff8 0x00000000004438ee <+46>: mov 0x0(%rbp,%rbx,1),%eax <-- STACK FAULT As you can see, gcc moved some TLS access above init_handler(), which results in a stack fault later when trying to read from fault_cnt. To prevent moving any TLS access around init_handler(), I need to do the following: asm volatile("":::"fs"); but this gives a compilation error. Is the explanation clear?
(In reply to Stas Sergeev from comment #3) > The signal handler needs to do the following things: > 1. Restore segment registers (init_handler() func) Why are you doing this, that is the question I am trying to understand here. What signal handler is happening? Because you can't do this under Linux at all. I am also trying to understand if you are writing a kernel/userspace why you can't write this part in pure assembly function instead of using GCC here to do this work.
(In reply to Andrew Pinski from comment #4) > (In reply to Stas Sergeev from comment #3) > > The signal handler needs to do the following things: > > 1. Restore segment registers (init_handler() func) > > Why are you doing this, that is the question I am trying to understand here. > What signal handler is happening? Ah, OK. This is a jit compiler. It uses segment registers at will, and it uses a memory protection too, so the signal is SIGSEGV. > Because you can't do this under Linux at > all. What exactly do you mean? > I am also trying to understand if you are writing a kernel/userspace why you > can't write this part in pure assembly function instead of using GCC here to > do this work. How am I supposed to access TLS in asm? init_handler() itself is mostly in asm though, yes. But the main sighandler is a huge convoluted func.
(In reply to Stas Sergeev from comment #5) > (In reply to Andrew Pinski from comment #4) > > (In reply to Stas Sergeev from comment #3) > > > The signal handler needs to do the following things: > > > 1. Restore segment registers (init_handler() func) > > > > Why are you doing this, that is the question I am trying to understand here. > > What signal handler is happening? > Ah, OK. > This is a jit compiler. > It uses segment registers at will, and it uses > a memory protection too, so the signal is SIGSEGV. That seems bogus usage really. And very unportable. Don't use segment registers at all. > > > Because you can't do this under Linux at > > all. > What exactly do you mean? > > > I am also trying to understand if you are writing a kernel/userspace why you > > can't write this part in pure assembly function instead of using GCC here to > > do this work. > How am I supposed to access TLS in asm? > init_handler() itself is mostly in asm though, yes. > But the main sighandler is a huge convoluted func.
(In reply to Andrew Pinski from comment #6) > (In reply to Stas Sergeev from comment #5) > > (In reply to Andrew Pinski from comment #4) > > > (In reply to Stas Sergeev from comment #3) > > > > The signal handler needs to do the following things: > > > > 1. Restore segment registers (init_handler() func) > > > > > > Why are you doing this, that is the question I am trying to understand here. > > > What signal handler is happening? > > Ah, OK. > > This is a jit compiler. > > It uses segment registers at will, and it uses > > a memory protection too, so the signal is SIGSEGV. > > That seems bogus usage really. And very unportable. Don't use segment > registers at all. Well, I can't, sorry. :) Please note that clang is getting this fixed already it seems: http://lists.cs.uiuc.edu/pipermail/cfe-commits/Week-of-Mon-20140714/110476.html http://lists.cs.uiuc.edu/pipermail/cfe-commits/Week-of-Mon-20140714/110481.html (not sure, just googled) Really, no even a -mno-xxx option to disable this optimization?
Note that O0 and O1 are fine. Only O2 gives the problem. So there might be a simple solution somewhere!
(In reply to Stas Sergeev from comment #7) > (In reply to Andrew Pinski from comment #6) > > (In reply to Stas Sergeev from comment #5) > > > (In reply to Andrew Pinski from comment #4) > > > > (In reply to Stas Sergeev from comment #3) > > > > > The signal handler needs to do the following things: > > > > > 1. Restore segment registers (init_handler() func) > > > > > > > > Why are you doing this, that is the question I am trying to understand here. > > > > What signal handler is happening? > > > Ah, OK. > > > This is a jit compiler. > > > It uses segment registers at will, and it uses > > > a memory protection too, so the signal is SIGSEGV. > > > > That seems bogus usage really. And very unportable. Don't use segment > > registers at all. > Well, I can't, sorry. :) > Please note that clang is getting this fixed already it seems: Clang is not GCC. Really you should not be using the fs segment register for your own use as it is part of the ABI that is the TLS segment. Any other use it of it is outside of the ABI and will break everything else. Why can't you not use fs segment?
Also why can't you write your segfault handler in assembly when you fixup the fs segment and then call the C functions?
(In reply to Andrew Pinski from comment #9) > Clang is not GCC. Really you should not be using the fs segment register > for your own use as it is part of the ABI that is the TLS segment. Any > other use it of it is outside of the ABI and will break everything else. > Why can't you not use fs segment? Alien code in place (think of wine/dosemu). > Also why can't you write your segfault handler in assembly when you fixup > the fs segment and then call the C functions? In principle - yes, but in practice - looking into a source - too much work. It is large, and it also uses a syscalls: sys_prctl(ARCH_SET_FS, eflags_fs_gs.fsbase); Do syscalls in asm too? :( Of course this all is possible, but... Should I really resort to always using -O1 till that also break? :( Let me ask you from the other side: why not simply to allow clobber also segregs? Improvement for everyone IMHO. clang did that for chromium it seems - likely a jit either. IMHO gcc should allow writing the specific things like signal handlers, interrupt handlers in embedded systems, etc - they all have suprises and deviations from ABI, yet gcc usually has an extensions to cover all these needs. Why to suddenly change that good practice?
Note that on x86_64 you can't set FS without a syscall. I would really hate doing that in asm it seems.
(In reply to Stas Sergeev from comment #11) > (In reply to Andrew Pinski from comment #9) > > Clang is not GCC. Really you should not be using the fs segment register > > for your own use as it is part of the ABI that is the TLS segment. Any > > other use it of it is outside of the ABI and will break everything else. > > Why can't you not use fs segment? > Alien code in place (think of wine/dosemu). Those all have pure assembly code to handle this case. That is they have wrapper code which handles the first part in pure C and then pass it on to the other code. > > > Also why can't you write your segfault handler in assembly when you fixup > > the fs segment and then call the C functions? > In principle - yes, but in practice - looking into a source - > too much work. It is large, and it also uses a syscalls: > sys_prctl(ARCH_SET_FS, eflags_fs_gs.fsbase); > Do syscalls in asm too? :( > Of course this all is possible, but... I don't see why you need to do the first part in assembly code and then pass it on to C code. > > Should I really resort to always using -O1 till that also break? :( > Let me ask you from the other side: why not simply to > allow clobber also segregs? Improvement for everyone IMHO. > clang did that for chromium it seems - likely a jit either. > IMHO gcc should allow writing the specific things like > signal handlers, interrupt handlers in embedded systems, > etc - they all have suprises and deviations from ABI, yet > gcc usually has an extensions to cover all these needs. Why > to suddenly change that good practice? Or why do you need to use the fs segment when it is part of the x86 Linux ABI? It seems like you are outside of C/ABI at this stage and need pure assembly to fix up your code. Again this is outside of the x86 Linux ABI and GCC should not handle this at all.
(In reply to Stas Sergeev from comment #12) > Note that on x86_64 you can't set FS without a syscall. > I would really hate doing that in asm it seems. Yes because you are setting the TLS segment. Again why are you abusing the fs segment here not to be the TLS segment?
(In reply to Andrew Pinski from comment #13) > Those all have pure assembly code to handle this case. No, they actually simply don't use TLS in a handler. That's something I'll be looking into too to save me from all that pain, but its not like they write a part of sighandler in asm and do the syscalls in asm. > That is they have > wrapper code which handles the first part in pure C Guess you meant "pure asm" here. > and then pass it on to > the other code. If you have such an evidence - the URL would be nice. From what I see they avoid the troubles simply by not using TLS. > I don't see why you need to do the first part in assembly code and then pass > it on to C code. Do you suggest syscalls in asm? int80h? sysenter? > Or why do you need to use the fs segment when it is part of the x86 Linux > ABI? It seems like you are outside of C/ABI at this stage and need pure > assembly to fix up your code. Again this is outside of the x86 Linux ABI > and GCC should not handle this at all. gcc handles many many things that are outside the ABI imho. It has many function attributes for all the weird cases... except this one of course. :) > Yes because you are setting the TLS segment. Again why are you abusing > the fs segment here not to be the TLS segment? IMHO linux itself should have been restoring FS. It doesn't, and as such, the signal handlers are violating the ABI. gcc can kindly fulfill the gap with just a small patch, as clang did, or even just by telling me the magic -mno-xxx option, or force the users to suffer.
Actually, Andrew, do you think this is even TLS specific? No segregs are restored at all (not even DS, I was wrong saying it is), so is it guaranteed, if FS is restored by some other means, that gcc will not start using some "movs" instruction before init_handler(), or some ds-prefixed access? So, my guess is this: - If this is only TLS-specific and gcc really really doesn't want to implement a nice possibility of clobber segregs, then I'll probably start asking linux kernel people about a possible solution. After all, restoring TLS is their task. - BUT, if this is not TLS-specific but is unsafe with ANY segreg, then I think gcc should just allow clobber.
I'll chime in, possibly uselessly. Before Linux 4.1, only CS was saved. (Unless you go *way* back.) In 4.1 or newer, SS is saved, too. In 64-bit code, DS and ES have no effect*, so I don't think it should affect code gen. FS and GS are weird, and they may get weirder when WRGSBASE and friends are enabled, which will happen at some point. Anyway, I tend to agree with Andrew here, I think: if you want to fiddle with FS and GS, write a little asm wrapper around the C code. * Except in an odd case on AMD processors that is mostly invisible to userspace. On new enough kernels, it's completely invisible to userspace, and I don't think it was ever visible without long jumps or such.
(In reply to Andy Lutomirski from comment #17) > I'll chime in, possibly uselessly. > > Before Linux 4.1, only CS was saved. (Unless you go *way* back.) In 4.1 > or newer, SS is saved, too. > > In 64-bit code, DS and ES have no effect*, so I don't think it should affect > code gen. OK, so the problem is TLS-specific, and is therefore have less relevance to gcc. Indeed it then will be very good if the kernel can take care of TLS. My point was just that for gcc it may not be a big problem to implement a segreg clobbering, if the kernel devs don't want to restore TLS. > Anyway, I tend to agree with Andrew here, I think: if you want to fiddle > with FS and GS, write a little asm wrapper around the C code. Yes but what kind of wrapper? Should it do also the syscall to set the needed fs base? At one point I may start pestering glibc guys to provide such a wrapper, if everyone else is unhelpful. :)
Various work-arounds appeared since, like __attribute__((optimize("no-stack-protector"))). And even though I still can't tell gcc that I clobbered the segregs, I can make it not to use them behind my back, which is sufficient for my needs.