i486 emu in mainline?
These days gcc uses i486+ only instruction by default in libstdc++ so most modern distros wouldn't work on i386 cpus anymore. To make it work again Debian merged Willy Tarreau's patch to trap those and emulate them on real i386 cpus. The patch is extremely non-invasive and would certainly be usefull for mainline. Any reason not to include it?
arch/i386/Kconfig +36 -0
@@ -330,6 +330,41 @@ This is really intended for distributors who need more generic optimizations. +config X86_EMU486 + bool "486 emulation" + help + When used on a 386, Linux can emulate 3 instructions from the 486 + set. This allows user space programs compiled for 486 to run on a + 386 without crashing with a SIGILL. As any emulation, performance + will be very low, but since these instruction are not often used, + this might not hurt. The emulated instructions are: + - bswap (does the same as htonl()) + - cmpxchg (used in multi-threading, mutex locking) + - xadd (rarely used) + + Note that this can also allow Step-A 486's to correctly run + multi-thread applications since cmpxchg has a wrong opcode on this + early CPU. + + Don't use this to enable multi-threading on an SMP machine, the lock + atomicity can't be guaranted! + + Although it's highly preferable that you only execute programs + targetted for your CPU, it may happen that, consecutively to a + hardware replacement, or during rescue of a damaged system, you have + to execute such programs on an inadapted processor. In this case, + this option will help you get your programs working, even if they + will be slower. + + It is recommended that you say N here in any case, except for the + kernels that you will use on your rescue disks. + + This option should not be left on by default, because it means that + you execute a program not targetted for your CPU. You should + recompile your applications whenever possible. + + If you are not sure, say N. + endif #
@@ -438,6 +473,7 @@ config SMP bool "Symmetric multi-processing support" + depends on !X86_EMU486 ---help--- This enables support for systems with more than one CPU. If you have a system with only one CPU, like most personal computers, say N. If
arch/i386/kernel/Makefile +1 -0
@@ -28,6 +28,7 @@ obj-$(CONFIG_MODULES) += module.o obj-y += sysenter.o vsyscall.o obj-$(CONFIG_ACPI_SRAT) += srat.o +obj-$(CONFIG_X86_EMU486) += emu.o obj-$(CONFIG_HPET_TIMER) += time_hpet.o obj-$(CONFIG_EFI) += efi.o efi_stub.o obj-$(CONFIG_EARLY_PRINTK) += early_printk.o
arch/i386/kernel/emu.c +434 -0
434 lines not on this page · raw message
arch/i386/kernel/entry.S +4 -0
@@ -562,7 +562,11 @@ ENTRY(invalid_op) pushl $0 +#ifdef CONFIG_X86_EMU486 + pushl $do_emu +#else pushl $do_invalid_op +#endif jmp error_code ENTRY(coprocessor_segment_overrun)
Hi,
On: Sun, 23 May 2004 01:40:59 +0200,
Quoted 5 lines · Christoph Hellwig
These days gcc uses i486+ only instruction by default in libstdc++ so most modern distros wouldn't work on i386 cpus anymore. To make it work again Debian merged Willy Tarreau's patch to trap those and emulate them on real i386 cpus. The patch is extremely non-invasive and would certainly be usefull for mainline. Any reason not to include it?
Oh cool! Given the fact with libstdc++ and sparc32 kernel also always contain code to emulatre the mul/div instructions missing in the v7 hardware (just foudn this SIGILL trap by accident recently ..) the comment might be a bit too strict. Maybe something like written in the MATH_EMULATION config:
"If you are not sure, say Y; apart from resulting in a 66 KB bigger kernel, it won't hurt."
Sincerely yours,
René Rebe
- ROCK Linux stable release maintainer
Signature · 3 lines
René Rebe - Europe/Germany/Berlin [email protected] [email protected] http://www.rocklinux.org http://www.rocklinux-consulting.de
Quoted 6 lines · Christoph Hellwig
These days gcc uses i486+ only instruction by default in libstdc++ so most modern distros wouldn't work on i386 cpus anymore. To make it work again Debian merged Willy Tarreau's patch to trap those and emulate them on real i386 cpus. The patch is extremely non-invasive and would certainly be usefull for mainline. Any reason not to include it?
on first look it seems to be missing a bunch of get_user() calls and does direct access instead....
Hi Arjan,
Quoted 2 lines · Arjan van de Ven
on first look it seems to be missing a bunch of
get_user()calls and does direct access instead....
It was intentional for speed purpose. The areas are checked once with verify_area() when we need to access memory, then data is copied directly from/to memory. I don't think there's any risk, but I can be wrong.
Regards,
Willy
Quoted 9 lines · Willy Tarreau
Hi Arjan,
Quoted 2 lines · Arjan van de Ven
on first look it seems to be missing a bunch of
get_user()calls and does direct access instead....It was intentional for speed purpose. The areas are checked once with
verify_area()when we need to access memory, then data is copied directly from/to memory. I don't think there's any risk, but I can be wrong.
it's an oopsable offence; nothing is making sure the memory is actually present for example.
Quoted 7 lines · Arjan van de Ven
Quoted 6 lines · Willy Tarreau
Hi Arjan,
Quoted 2 lines · Arjan van de Ven
on first look it seems to be missing a bunch of
get_user()calls and does direct access instead....
Quoted 6 lines
Quoted 3 lines
It was intentional for speed purpose. The areas are checked once with
verify_area()when we need to access memory, then data is copied directly from/to memory. I don't think there's any risk, but I can be wrong.it's an oopsable offence; nothing is making sure the memory is actually present for example.
You mean like when a user does a malloc() and the memory is not physically allocated because not used yet ? or even in case memory has been swapped out ? I believe I begin to understand, but the corner case is not really clear to me. It yet seems strange to me that the user can reference memory areas that the kernel cannot access. I'm certainly mistaken somewhere, but I don't know where. In fact, if you could give me a simple example which puts my original code at fault, it would really help me. Then I'll change the code as suggested by Andrew but at least I would understand what I do.
Thanks in advance.
Willy
Quoted 12 lines · Willy Tarreau
Quoted 11 lines · Arjan van de Ven
Quoted 9 lines · Willy Tarreau
Hi Arjan,
Quoted 2 lines · Arjan van de Ven
on first look it seems to be missing a bunch of
get_user()calls and does direct access instead....It was intentional for speed purpose. The areas are checked once with
verify_area()when we need to access memory, then data is copied directly from/to memory. I don't think there's any risk, but I can be wrong.
Quoted 6 lines
Quoted 2 lines
it's an oopsable offence; nothing is making sure the memory is actually present for example.
You mean like when a user does a
malloc()and the memory is not physically allocated because not used yet ? or even in case memory has been swapped out ?
Well both. Or even a stray invalid userspace pointer.
copy_from_user/get_user will catch the trap the cpu will cause in such a case, and the VM will swap in the page, allocate a new page in case of the malloc-but-never-used-yet or throw a segmentation fault if the userspace pointer is broken.
If there is no protection (eg there is no exception handler defined) then the kernel will throw an oops since it's an uncaught/unhandled kernel mode exception. Obviously copy_from_user/get_user and co have such exception handlers defined and will do the expected (right) thing. Direct access does not.
Quoted 6 lines · Willy Tarreau
You mean like when a user does a
malloc()and the memory is not physically allocated because not used yet ? or even in case memory has been swapped out ? I believe I begin to understand, but the corner case is not really clear to me. It yet seems strange to me that the user can reference memory areas that the kernel cannot access.
The user referencing non-present memory is not a problem, because:
1. It is always pssoible to block the user process and let it wait (a long time) for swapping to happen. That might not be an option for the kernel - it can't wait while holding a spinlock, for example.
2. It is always ok to kill the user process if it uses memory it doesn't have. It is not ok to "kill" the kernel. The indirect ways of using user memory from the kernel side ensures that the normal mechanisms (swap-wait or kill) applies to the process owning the memory. Direct reference from kernel does not invoke normal page fault mechanisms when memory goes wrong.
Helge Hafting
Quoted 8 lines · Willy Tarreau
Quoted 2 lines · Arjan van de Ven
on first look it seems to be missing a bunch of
get_user()calls and does direct access instead....It was intentional for speed purpose. The areas are checked once with
verify_area()when we need to access memory, then data is copied directly from/to memory. I don't think there's any risk, but I can be wrong.
verify_area() simply checks that the address is a legal one for a userspace access (it's not a chunk of kernel memory). But the kernel can still take a pagefault when accessing the address, so you need to use the uaccess functions which will handle the fault appropriately.
That's put_user(), get_user(), copy_*_user(), etc. Those functions internally perform verify_area(), so if you've already done a verify_area() you can use __put_user(), __get_user(), etc which skip the verify_area() but which still know how to deal with user address faults.
Quoted 11 lines · Willy Tarreau
Hi Arjan,
Quoted 2 lines · Arjan van de Ven
on first look it seems to be missing a bunch of
get_user()calls and does direct access instead....It was intentional for speed purpose. The areas are checked once with
verify_area()when we need to access memory, then data is copied directly from/to memory. I don't think there's any risk, but I can be wrong.
Which will break with 4G/4G. You must use at least __get_user().
--
Brian Gerst
Quoted 6 lines · Brian Gerst
Quoted 4 lines
It was intentional for speed purpose. The areas are checked once with
verify_area()when we need to access memory, then data is copied directly from/to memory. I don't think there's any risk, but I can be wrong.Which will break with 4G/4G. You must use at least
__get_user().
A 386 with a 4G/4G split, I'd like to see that.
Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txtHi!
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
It was intentional for speed purpose. The areas are checked once with
verify_area()when we need to access memory, then data is copied directly from/to memory. I don't think there's any risk, but I can be wrong.Which will break with 4G/4G. You must use at least
__get_user().A 386 with a 4G/4G split, I'd like to see that.
I believe you could...
1) some 586 class machines do not know 486 opcodes
2) this is for rescue kernel. If you want to be able to rescue 64GB machine *and* rescue 486 too, this+4G/4G is right choice ;-)
Signature · 1 line
64 bytes from 195.113.31.123: icmp_seq=28 ttl=51 time=448769.1 ms
Hi Christoph,
Quoted 5 lines · Christoph Hellwig
These days gcc uses i486+ only instruction by default in libstdc++ so most modern distros wouldn't work on i386 cpus anymore. To make it work again Debian merged Willy Tarreau's patch to trap those and emulate them on real i386 cpus. The patch is extremely non-invasive and would certainly be usefull for mainline. Any reason not to include it?
Well, I have mixed feelings about this because :
- I don't know which version they based their port on. The version I published 2 years ago included CMOV emulation, and Denis Vlasenko found several bugs in it which I then fixed. Since the Debian port doesn't include CMOV, I wonder whether it includes those bugs or not (I'll have to diff the patches).
- The code is ugly in some areas, and someone will kill me if this goes into mainline.
- There are people (like Alan) who think that this should not go into mainline because this is a distro problem and nothing else. He says that only i386 packages should be installed on an i386 machine. He's perfectly right about this. I found it interesting for people like me who boot kernels on random machines, try to recover hard disk contents or other things using lots of dirty tools, and sometimes get hit by the "illegal instruction" trap. It also allowed me to run a pppd compiled with i586 glibc on my i386 firewall, but obviously this is just the easy way and not the right way to go.
- I couldn't emulate locks, so this will break on SMP systems, and so will it if you need to access some memory share with an external microcontroller or something like that.
- I've always wondered if this feature would not be exploitable to access unauthorized information. Eg: code an invalid opcode which would get emulated and references a memory area outside the user space. I put some verify_area() where I thought appropriate, but I might have left some caveats... Morten Welinder once insisted on the fact that each byte should be read once and only once so as to ensure that the user doesn't change the instruction while it is being emulated. I think it's already the case. He also said that I didn't take care of the segment selectors (such as SS) which some programs use perfectly legally (eg Wine). I don't know how to do that.
- Denis Vlasenko suggested that we print some messages on the console when a program triggers the code, so as not to let the user think that his machine is slow as hell. But for this we would need not to flood the console (eg: once for each prog) but we don't want either to store anything in the task structure about the message having been displayed, so for now there's nothing. In fact, the only message which is displayed (in the most recent version) is about a warning about a LOCK prefix on an SMP kernel. But I didn't find it right here, so I suspect this is based on ancient code.
- why not include the CMOV emulation while we're at it ? There are so many people using VIA EDEN chips who think it's i686 compatible that they may get hit too. IIRC, the chip only executes CMOV on registers, but very slowly (a few tens of cycles), while register to memory accesses generate a trap.
Other than that, I'm happy that someone found it useful, and happy too that someone did the 2.6 port :-)
Regards,
Willy
Quoted 4 lines · Willy Tarreau
being emulated. I think it's already the case. He also said that I didn't take care of the segment selectors (such as SS) which some programs use perfectly legally (eg Wine). I don't know how to do that.
You have to parse all the valid header bytes (the opcode prefixes) that change segment, cause repeats and change sizes. DOSemu has a worked example of this particular set of horrors.
Quoted 5 lines
- why not include the CMOV emulation while we're at it ? There are so many people using VIA EDEN chips who think it's i686 compatible that they may get hit too. IIRC, the chip only executes CMOV on registers, but very slowly (a few tens of cycles), while register to memory accesses generate a trap.
gcc generates a lot of cmov on i686 so many that people I've talked with on the compiler side also feel cmov emulation isnt useful. Newer Eden btw has cmov.
Quoted 2 lines
Other than that, I'm happy that someone found it useful, and happy too that someone did the 2.6 port :-)
Is there a reason btw it can't be done with LD_PRELOAD ?
Quoted 9 lines · Alan Cox
Quoted 4 lines · Willy Tarreau
being emulated. I think it's already the case. He also said that I didn't take care of the segment selectors (such as SS) which some programs use perfectly legally (eg Wine). I don't know how to do that.
You have to parse all the valid header bytes (the opcode prefixes) that change segment, cause repeats and change sizes. DOSemu has a worked example of this particular set of horrors.
It's already what it does, but I don't use the segment base in the TSS nor check the segment limit. It will take me quite some time to understand how all this is implemented in the kernel and unfortunately I don't have such time now.
Quoted 9 lines
Quoted 5 lines
- why not include the CMOV emulation while we're at it ? There are so many people using VIA EDEN chips who think it's i686 compatible that they may get hit too. IIRC, the chip only executes CMOV on registers, but very slowly (a few tens of cycles), while register to memory accesses generate a trap.
gcc generates a lot of cmov on i686 so many that people I've talked with on the compiler side also feel cmov emulation isnt useful. Newer Eden btw has cmov.
agreed, but there are some programs which don't need speed at all, hence the reason for my first implementation of cmov (which burnt about 400 cycles IIRC).
Quoted 4 lines
Quoted 2 lines
Other than that, I'm happy that someone found it useful, and happy too that someone did the 2.6 port :-)
Is there a reason btw it can't be done with
LD_PRELOAD?
Well, this is an interesting question. I don't know how to do it this way (how can a program know exactly where the trap occured, etc... I don't know how to program this). Other than that, LD_PRELOAD will not work against setuid binaries. But if it does for the rest, I think it can become an elegant approach.
Thanks for your insights,
Willy
Quoted 7 lines
Quoted 1 line
Is there a reason btw it can't be done with
LD_PRELOAD?Well, this is an interesting question. I don't know how to do it this way (how can a program know exactly where the trap occured, etc... I don't know how to program this). Other than that,
LD_PRELOADwill not work against setuid binaries. But if it does for the rest, I think it can become an elegant approach.
setuid binaries can still use /etc/ld.preload or whatever the file is called just not environment.
Someone actually did a libmmx long ago that used preload, hooked SIGILL and the signal handlers and used that to provide mmx on an mmx free cpu
On Sun, 2004-05-23 09:15:12 -0400, Alan Cox <[email protected]> wrote in message <[email protected]>:
Quoted 10 lines
Quoted 7 lines
Quoted 1 line
Is there a reason btw it can't be done with
LD_PRELOAD?Well, this is an interesting question. I don't know how to do it this way (how can a program know exactly where the trap occured, etc... I don't know how to program this). Other than that,
LD_PRELOADwill not work against setuid binaries. But if it does for the rest, I think it can become an elegant approach.setuid binaries can still use
/etc/ld.preloador whatever the file is called just not environment.
Being an old hardware user and tester (I still have a i386 and i486SLC where I do testing on!), I strongly support the inclusion of the emulator. I think that it should even be extended/fixed to catch the remaining opcode(s).
Quoted 2 lines
Someone actually did a libmmx long ago that used preload, hooked SIGILL and the signal handlers and used that to provide mmx on an mmx free cpu
There are some application that register signal handling functions IIRC for SIGILL, SIGSEGV and the like to do internal error trapping on their own (not only OOo comes to mind). These would probably be f*cked up if they didn't call the LD_PRELOADed signal handler...
MfG, JBG
Signature · 4 lines
Jan-Benedict Glaw [email protected] . +49-172-7608481 "Eine Freie Meinung in einem Freien Kopf | Gegen Zensur | Gegen Krieg fuer einen Freien Staat voll Freier Bürger" | im Internet! | im Irak! ret = do_actions((curr | FREE_SPEECH) & ~(NEW_COPYRIGHT_LAW | DRM | TCPA));
Quoted 4 lines · Jan-Benedict Glaw
There are some application that register signal handling functions IIRC for SIGILL, SIGSEGV and the like to do internal error trapping on their own (not only OOo comes to mind). These would probably be f*cked up if they didn't call the
LD_PRELOADedsignal handler...
No. The LD_PRELOAD also hooks the signal setting functions. This really is not rocket science at all.
On Mon, 2004-05-24 13:41:56 -0400, Alan Cox <[email protected]> wrote in message <[email protected]>:
Quoted 8 lines · Jan-Benedict Glaw
Quoted 4 lines
There are some application that register signal handling functions IIRC for SIGILL, SIGSEGV and the like to do internal error trapping on their own (not only OOo comes to mind). These would probably be f*cked up if they didn't call the
LD_PRELOADedsignal handler...No. The
LD_PRELOADalso hooks the signal setting functions. This really is not rocket science at all.
But works only on dynamically linkes executables, and only on those that don't do system calls on their own, right?
MfG, JBG
Signature · 4 lines
Jan-Benedict Glaw [email protected] . +49-172-7608481 "Eine Freie Meinung in einem Freien Kopf | Gegen Zensur | Gegen Krieg fuer einen Freien Staat voll Freier Bürger" | im Internet! | im Irak! ret = do_actions((curr | FREE_SPEECH) & ~(NEW_COPYRIGHT_LAW | DRM | TCPA));
Quoted 5 lines · Jan-Benedict Glaw
Quoted 2 lines
No. The
LD_PRELOADalso hooks the signal setting functions. This really is not rocket science at all.But works only on dynamically linkes executables, and only on those that don't do system calls on their own, right?
If it is static linked to glibc or libc5 then you have the source or the bits to relink it.
You can build an app to deliberately break software emulation, but that goes for kernel mode too and isn't the problem you *actually* want to solve
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 10 lines · Joseph Fannin
Quoted 6 lines · Willy Tarreau
Quoted 5 lines · Christoph Hellwig
These days gcc uses i486+ only instruction by default in libstdc++ so most modern distros wouldn't work on i386 cpus anymore. To make it work again Debian merged Willy Tarreau's patch to trap those and emulate them on real i386 cpus. The patch is extremely non-invasive and would certainly be usefull for mainline. Any reason not to include it?
Quoted 6 lines
Quoted 3 lines
- I couldn't emulate locks, so this will break on SMP systems, and so will it if you need to access some memory share with an external microcontroller or something like that.
Does this mean that programs that use the NPTL will work on
non-SMP 386s?
I have no idea. Why would NPTL not work on i386 ?
Willy
Quoted 12 lines · Willy Tarreau
Quoted 10 lines · Joseph Fannin
Quoted 7 lines · Willy Tarreau
Quoted 5 lines · Christoph Hellwig
These days gcc uses i486+ only instruction by default in libstdc++ so most modern distros wouldn't work on i386 cpus anymore. To make it work again Debian merged Willy Tarreau's patch to trap those and emulate them on real i386 cpus. The patch is extremely non-invasive and would certainly be usefull for mainline. Any reason not to include it?
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
- I couldn't emulate locks, so this will break on SMP systems, and so will it if you need to access some memory share with an external microcontroller or something like that.
Does this mean that programs that use the NPTL will work on
non-SMP 386s?I have no idea. Why would NPTL not work on i386 ?
i386 doesn't have the necessary instructions.
It's my understanding that the following is not being maintained:
http://sources.redhat.com/ml/libc-hacker/2004-05/msg00019.html
Pádraig.
Quoted 4 lines · Willy Tarreau
- There are people (like Alan) who think that this should not go into mainline because this is a distro problem and nothing else. He says that only i386 packages should be installed on an i386 machine. He's perfectly right about this. I found it interesting for people like
I disagree.
I want to add "old Alpha" emulation code, so that older Alphas can run binaries built on the newer alphas.
Jeff
Quoted 10 lines · Jeff Garzik
Quoted 4 lines
- There are people (like Alan) who think that this should not go into mainline because this is a distro problem and nothing else. He says that only i386 packages should be installed on an i386 machine. He's perfectly right about this. I found it interesting for people like
I disagree.
I want to add "old Alpha" emulation code, so that older Alphas can run binaries built on the newer alphas.
Well it always depends on the platform. cmov emulation isnt useful because i686 gcc generates too many for it to be of value. For alpha it depends on the commonness of the emulated instructions and the emulation cost.
Either way it is a user space problem in almost all situations. See
http://www-sop.inria.fr/geometrica/team/Sylvain.Pion/progs/mmx-emu/
Quoted 6 lines · Christoph Hellwig
arch/i386/kernel/emu.c +6
-0+/* we'll verify if this is a BSWAP opcode, main source of SIGILL on 386's */ +if ((*eip &0xF8 ) ==0xC8 ) {/* BSWAP */ +u8 reg; + + reg = *eip++ &0x07 ; + src =reg_address (regs,1 , reg);
arch/i386/kernel/emu.c line 246
Quoted 10 lines
arch/i386/kernel/emu.c +10
-0+ + __asm__ __volatile__ ( +"xchgb %%al, %%ah\n\t" +"roll $16, %%eax\n\t" +"xchgb %%al, %%ah\n\t" + :"=a" (*(u32 *)src) + :"a" (*(u32 *)src)); + regs->eip = (u32 )eip; + goto out; + }
You've forgotten about the 16-bit variant here -- the emulation is wrong
with PREFIX_D32. This may not matter much in practice, though.
Signature · 3 lines
+ Maciej W. Rozycki, Technical University of Gdansk, Poland + +--------------------------------------------------------------+ + e-mail: [email protected], PGP key available +
Christoph Hellwig <[email protected]> ha scritto:
arch/i386/Kconfig 2004-05-10 22:21:08.000000000 +1000 line 335
Quoted 9 lines
arch/i386/Kconfig 2004-05-10 22:21:08.000000000 +1000 renamed from arch/i386/Kconfig 2004-05-10 19:47:45.000000000 +1000 +3
-0@@ -330,6 +330,41 @@ This is really intended for distributors who need more generic optimizations. +config X86_EMU486 + bool "486 emulation" + help
Hi,
what about adding an explicit dependancy from M386/M486? A kernel compiled for 486 or higher won't boot on 386 anyway so the emulation code would be useless.
arch/i386/Kconfig +1 -0
@@ -332,6 +332,7 @@ config X86_EMU486 bool "486 emulation" + depends on M386 || M486 help When used on a 386, Linux can emulate 3 instructions from the 486 set. This allows user space programs compiled for 486 to run on a
(just in case ;) )
Luca
Signature · 5 lines
Home: http://kronoz.cjb.net Windows /win'dohz/ n. : thirty-two bit extension and graphical shell to a sixteen bit patch to an eight bit operating system originally coded for a four bit microprocessor which was written by a two-bit company that can't stand a bit of competition.