Can I take a Linux kernel and use it with, say, FreeBSD and vice versa (FreeBSD kernel in, say, a Debian)? Is there a universal answer? What are the limitations? What are the obstructions?
Linux – Are different Linux/Unix kernels interchangeable
Architecturebsdkernellinux
Related Solutions
There is no unique thing named "the Unix kernel". There are multiple descendants of the original Unix kernel source code trunk that forked branches from it at different stages and that have evolved separately according to their own needs.
The mainstream ones these days are found in Operating Systems created either from System V source code: AIX, HPUX, Solaris or from BSD source code, OpenBSD, FreeBSD and Mac OS/X.
All of these kernels have their particular strengths and weaknesses, just like Linux and other "from scratch" Unix like kernels (minix, Gnu hurd, ...).
Here is a non exhaustive list of the areas where differences can be observed, in no particular order:
- CPU architecture support
- Availability of drivers
- File systems supported
- Virtualization capabilities
- Scheduling features, (alternate scheduling classes, real-time, ...)
- Modularity
- Observability
- Tunability
- Reliability
- Performance
- Scalability
- API stability between versions
- Open/Close source, license used
- Security (eg: privilege granularity)
- Memory management
From the operating system's perspective, user space is the execution of code from a process, and kernel space is the execution of code from the kernel. A transition from user space to kernel space is the entry into a system call, and a transition from kernel space to user space is the return from a system call.
From the processor's perspective, user space and kernel space are privilege levels. “Ring 3” and “ring 0” are the names used on Intel x86 processors (there are rings 1 and 2, but most unices don't use them, because there isn't a lot of use for them). Other CPU types use different names, for example user mode and privileged (or system) modes on ARM.
Transition between modes is done by instructions that both change the processor mode and jump to a different address; the detail of these instructions is very much dependent on the CPU type. In addition to switching the processor mode and jumping to a different address, the mode transition instructions typically perform some other tasks such as swapping the value of a few registers (again, this is very CPU-dependent).
A transition from user mode to kernel mode has to jump to a fixed address, which has been set by the kernel. In order to maintain the security of the system, code in user mode cannot be allowed to switch the processor to kernel mode except to invoke a piece of code (the system call entry point) which has been specially crafted to work securely no matter what the user mode code has been doing. The system call entry point first saves register values into a designated memory area, and performs any other necessary bookkeeping, then it reads the system call parameters and dispatches the system call to the appropriate function. Depending on the processor type, the bookkeeping may involve setting the MMU tables appropriately. The kernel may decide at any point to pause the calling process and schedule another process.
A transition from kernel mode to user mode can happen anywhere the kernel deems fit. The kernel restores the saved registers, the MMU configuration and anything else that is necessary, and writes the system call's return value, and finally jumps back to the instruction in the process after the system call.
Best Answer
No, kernels from different implementations of Unix-style operating systems are not interchangeable, notably because they all present different interfaces to the rest of the system (user space) — their system calls (including
ioctl
specifics), the various virtual file systems they use...What is interchangeable to some extent, at the source level, is the combination of the kernel and the C library, or rather, the user-level APIs that the kernel and libraries expose (essentially, the view at the layer described by POSIX, without considering whether it is actually POSIX). Examples of this include Debian GNU/kFreeBSD, which builds a Debian system on top of a FreeBSD kernel, and Debian GNU/Hurd, which builds a Debian system on top of the Hurd.
This isn’t quite at the level of kernel interchangeability, but there have been attempts to standardise a common application binary interface, to allow binaries to be used on various systems without needing recompilation. One example is the Intel Binary Compatibility Standard, which allows binaries conforming to it to run on any Unix system implementing it, including older versions of Linux with the iBCS 2 layer. I used this in the late 90s to run WordPerfect on Linux.
See also How to build a FreeBSD chroot inside of Linux.