32-bit syscalls from 64-bit process on x86-64?
Quoted 7 lines · Andi Kleen
Quoted 4 lines
#define TOLM \ "pushl %%cs\n" \ "pushl $91f\n" \ "ljmpl $0x33,$90f\n" \It's useless, there is nothing in the kernel code that checks the 32bit segment.
??? Processor checks for 32bit/64bit segment. It is impossible to load upper 32bit of all registers with non-zero value or call 64bit syscall entry point from 32bit mode. As x86-64 kernel offers 64bit interface through syscall only, only way how to issue 64bit system call is using syscall instruction in 64bit code.
Or are you trying to say that these samples do not work and you cannot call 64bit entry point from 32bit app, or vice versa? Then I'm afraid that you are not completely right, as these samples do work...
Petr Vandrovec
Quoted 6 lines · Petr Vandrovec
Quoted 5 lines · Andi Kleen
Quoted 4 lines
#define TOLM \ "pushl %%cs\n" \ "pushl $91f\n" \ "ljmpl $0x33,$90f\n" \
Quoted 8 lines
Quoted 2 lines
It's useless, there is nothing in the kernel code that checks the 32bit segment.
??? Processor checks for 32bit/64bit segment. It is impossible to load upper 32bit of all registers with non-zero value or call 64bit syscall entry point from 32bit mode. As x86-64 kernel offers 64bit interface through syscall only, only way how to issue 64bit system call is using syscall instruction in 64bit code.
Ah sorry. I misread the intention of your code. I thought you wanted to do it the other way round - 32bit syscall from 64bit code. I just wanted to point out that you can do it directly without changing the code segment, as long as you use int $0x80.
Quoted 1 line
From 64bit-from-32bit the lcall is needed agreed. However as a
warning it will not work for all calls since a few check a bit in task_struct that says if the process is 32bit or 64bit (rather rare though, most prominent is signal handling)
Quoted 4 lines
Or are you trying to say that these samples do not work and you cannot call 64bit entry point from 32bit app, or vice versa? Then I'm afraid that you are not completely right, as these samples do work...
I haven't ever tried it, but I see no reason it cannot work.
-Andi
Quoted 4 lines · Andi Kleen
From 64bit-from-32bit the lcall is needed agreed. However as a warning it will not work for all calls since a few check a bit in
task_structthat says if the process is 32bit or 64bit (rather rare though, most prominent is signal handling)
When delivering a signal to a 64-bit process (ie, without TIF_IA32 set), do you think it should always set cs to be USER_CS? At the moment, if cs is something else (ie, USER32_CS), it tries to deliver the signal with that current...
J
Quoted 10 lines · Jeremy Fitzhardinge
Quoted 4 lines · Andi Kleen
From 64bit-from-32bit the lcall is needed agreed. However as a warning it will not work for all calls since a few check a bit in
task_structthat says if the process is 32bit or 64bit (rather rare though, most prominent is signal handling)When delivering a signal to a 64-bit process (ie, without
TIF_IA32set), do you think it should always set cs to beUSER_CS? At the moment, if cs is something else (ie,USER32_CS), it tries to deliver the signal with that current...
Hmm, in theory you could handle a 64bit signal frame from 32bit code (just may need an assembly stub if you want the arguments). But it would be quite ugly agreed.
Perhaps it should force __USER_CS yes in this case, agreed.
There is a small risk of breaking someone, but it's very small.
I can do that change if you want.
BTW the long term plan is to get rid of the special cases to make it easierto use the 32bit kernel ABI from a 64bit program. This means signal handling will likely just check the code segment at some point to decide if it should set up 32bit or 64bit frames and we'll probably do similar things with the other cases (except exec which needs to stay this way)
If you're interested in this I guess that could be done sooner with some patch submissions (hint hint ;)
-Andi
Quoted 3 lines · Andi Kleen
Hmm, in theory you could handle a 64bit signal frame from 32bit code (just may need an assembly stub if you want the arguments). But it would be quite ugly agreed.
Yes. I've tried this out, and it works OK, but it isn't pleasing. One of the main problems is that the stack is likely to be above 4G, so %esp has no useful value, and when you switch to 64-bit mode, the top 32-bits of %rsp become undefined.
Quoted 3 lines
Perhaps it should force
__USER_CSyes in this case, agreed.There is a small risk of breaking someone, but it's very small.
Well, if they've got code which is already switching between 32 and 64 bit segments, then they need to cope with either cs being current at delivery time.
Quoted 8 lines
I can do that change if you want.
BTW the long term plan is to get rid of the special cases to make it easierto use the 32bit kernel ABI from a 64bit program. This means signal handling will likely just check the code segment at some point to decide if it should set up 32bit or 64bit frames and we'll probably do similar things with the other cases (except exec which needs to stay this way)
At syscall time, rather than delivery time, I assume. Hm, I'd prefer it if it didn't look at the current segment, but at the syscall path. Ie, installing a handler with __NR_rt_sigaction via int 0x80 (or 32-bit syscall/sysenter) should set up a 32-bit frame on delivery, but if the handler was installed with the 64-bit syscall, it should be called with a 64-bit frame.
Quoted 2 lines
If you're interested in this I guess that could be done sooner with some patch submissions (hint hint ;)
I'll take a look.
J
Quoted 9 lines · Jeremy Fitzhardinge
Quoted 3 lines · Andi Kleen
Hmm, in theory you could handle a 64bit signal frame from 32bit code (just may need an assembly stub if you want the arguments). But it would be quite ugly agreed.
Yes. I've tried this out, and it works OK, but it isn't pleasing. One of the main problems is that the stack is likely to be above 4G, so %esp has no useful value, and when you switch to 64-bit mode, the top 32-bits of %rsp become undefined.
They seem to stay at the previous value in the current CPUs, but it's undefined behaviour yes.
Quoted 8 lines
Quoted 4 lines
Perhaps it should force
__USER_CSyes in this case, agreed.There is a small risk of breaking someone, but it's very small.
Well, if they've got code which is already switching between 32 and 64 bit segments, then they need to cope with either cs being current at delivery time.
Can you cope with that in valgrind? If I change it there will be kernels with both behaviour around for a long time.
Quoted 10 lines
Quoted 9 lines
I can do that change if you want.
BTW the long term plan is to get rid of the special cases to make it easierto use the 32bit kernel ABI from a 64bit program. This means signal handling will likely just check the code segment at some point to decide if it should set up 32bit or 64bit frames and we'll probably do similar things with the other cases (except exec which needs to stay this way)
Quoted 6 lines
At syscall time, rather than delivery time, I assume. Hm, I'd prefer it if it didn't look at the current segment, but at the syscall path. Ie, installing a handler with
__NR_rt_sigactionvia int0x80(or 32-bit syscall/sysenter) should set up a 32-bit frame on delivery, but if the handler was installed with the 64-bit syscall, it should be called with a 64-bit frame.
That would probably add some complications to the syscall exit path:
there is some code sharing between 32bit and 64bit, in particular 64bit execve uses the IRET path to restore all registers and there can be a signal after that.
The only case that wouldn't be handled by checking the segment would be that if someone defines a 64bit LDT code segment and uses it, it will not work. But that won't work anyways from 64bit because SYSRET always forces __USER_CS. If you want to do any 64bit system calls you cannot use such custom segment anyways.
So i don't see any drawbacks in just checking the segment for this.
-Andi