Subject: Re: misc kern questions
To: Travis H. <solinym@gmail.com>
From: Hubert Feyrer <hubert@feyrer.de>
List: tech-kern
Date: 08/25/2006 12:15:26
On Thu, 24 Aug 2006, Travis H. wrote:
> Can anyone describe succinctly how, say, Linux binary emulation works?
> URLs welcome.  Calls to RTFM just as welcome.  I'm hoping for
> something very low-level, like explaining that int 0x80 maps does
> this-or-that, whereas NetBSD natively uses int 0x81, or whatever.

NetBSD doesn't use BIOS interrupts (usually), it calls its own device 
drivers.

As for how things happen, look at the list that I posted yesterday 
(http://www.feyrer.de/BT/): The kernel knows under what emulation (native, 
linux, ...) a binary runs as it looked at the exact ELF type when starting 
the binary, and remembered that one, and then chooses a system call table 
appropriate for that binary. The idea of the native syscall table should 
be obvious. The 'emul' syscall tables either map emulated syscalls 
directly to native ones, or to "wrapper" functions that adjust semantical 
differences between the systems (before usually calling native syscalls 
again). See src/sys/compat/linux/arch/i386/syscalls.master for an example 
- you will see calls to many linux_*() functions, but also to some native 
ones like sys_read(). The implementation of those linux_*() functions is 
in the same directory and in src/sys/compat/linux/common.

Different register handling between NetBSD and Linux is handled by things 
like linux_syscall_plain() in src/sys/arch/i386/i386/linux_syscall.c.
(I'm actually not sure how this hooks into the overall syscall framework, 
but I guess that can be found out :)


Some URLs to read - the majority are not specific to i386, but go into 
much detail and give an in-depth overview of how things work:

Implementing Linux emulation on NetBSD:
http://os.newsforge.com/os/04/05/10/1437236.shtml?tid=8&tid=82&tid=94

Linux compatibility on BSD for the PPC platform (5 parts):
http://www.onlamp.com/pub/a/onlamp/2001/05/10/linux_bsd.html
http://www.onlamp.com/pub/a/onlamp/2001/05/17/linux_bsd.html
http://www.onlamp.com/pub/a/onlamp/2001/06/07/linux_bsd.html
http://www.onlamp.com/pub/a/onlamp/2001/06/21/linux_bsd.html
http://www.onlamp.com/pub/a/onlamp/2001/08/09/linux_bsd.html

Irix binary compatibility (6 parts):
http://www.onlamp.com/pub/a/bsd/2002/08/08/irix.html
http://www.onlamp.com/pub/a/bsd/2002/08/29/irix.html
http://www.onlamp.com/pub/a/bsd/2002/09/12/irix.html
http://www.onlamp.com/pub/a/bsd/2002/10/10/irix.html
http://www.onlamp.com/pub/a/bsd/2002/12/19/irix.html
http://www.onlamp.com/pub/a/bsd/2003/04/03/irix.html


> Do the Linux syscalls actually implement the entire syscall, or do
> they rearrange arguments and call the native syscall?

See above.


> Does NetBSD use
> any Linux code?

This is prohibited by the Linux license - NetBSD doesn't allow GPL in the 
kernel.


> IIRC Linux keeps most of the args in registers, and
> BSD copies them off the stack; how do we support both, just
> __asmlinkage__ the Linux syscalls and not the native?
>
> I assume that emulating Linux LKMs is not possible; does VMWare run
> natively?  How about Xen?
>