Mac OSX Kernel(XNU)
Download
Report
Transcript Mac OSX Kernel(XNU)
Mac OSX
Kernel(XNU)
Presented By:
K Avinash Reddy 2011251
K Srinath Reddy 2011252
V Anish 2011264
N Manikanta Reddy 2011255
The
kernel
is
the
essential center of a
computer
operating
system, the core that
provides basic services
for all other parts of the
operating system.
Monolithic kernel: On traditional Monolithic
kernels like unix,bsd linux, file system framework,
security components, user mode interface,
network stack and device drivers, besides those
things kernel mode things are also in kernel mode.
Micro kernel: On a microkernel, the kernel user
boundary is moved down so that only necessary
parts are in kernel mode like scheduler, memory
manager, ipc and low level hardware access.
Components like file system and networking are
implemented in their own address spaces in user
mode.
Monolithic
Kernel
Micro Kernel
App
User
mode
Kernel
mode
App
App
libc
API
VFS
dev
net
sec
HW IPC MM sched
Hardware
Micro kernel has an advantage that a crash in a driver or
a file system logic or a network protocol doesn’t have to
necessarily bring the system down, In many cases, the
faulty component can be restarted like a user
application.
Another advantage of micro kernel is the security. If
malicious code runs in the context of any component
like a network driver, it doesn’t have full control over
the system.
Also dividing the kernel into components makes it more
maintainable
The problem with this is the address space switches
invalidate TLB(Translation Look aside Buffer) which is
the cache for virtual memory mapping consequently
virtual to address space lookups become lower after a
switch until the TLB is repopulated. Leads to
performance problems.
Mach
• memory
management
• scheduling
• inter process
communication
•Mach manages mapping from virtual address spaces to physical address
spaces.
•Tasks are address free and policy free(i.e. address pages are set of
pages(no user id, no working directory, no command line).
•Mach allows one or more threads per task and does scheduling.
•Mach also does sending messages between tasks this is inter process
communication
•IPC is the distinguishing feature to other kernels.
Mach IPC
Mach IPC
•
A task can have any number of ports
•
Blue- sender ports
•
Red- receiver ports
•
•
Mach sends messages asynchronusly so recepient will be
busy and need not accept message immediately because
kernel buffers them i.e. puts them in queue.
Recepient will pick oldest message one first and next one
•
Most common use case for IPC is RPC(Remote Procedure
Call) i.e. a call from app to BST server
•
Unfortunately it is complicated to use mach IPC for RPC
•
So we use MIG
Mach Interface Generator
(MIG)
• Mach simplifies our pc.
• Effectively a function in task 1 can call function in task 2.
• MIG generates code from interface definition file.
Drivers
• If you want create a new
driver in windows, usually take
the closest match and duplicate
it into another file and make
respective changes in the new file.
• This creates duplicate code and
which will always duplicates the
bugs also.
Drivers
• A simple solution for the above problem is to
unify the c files and mark differences with #ifdef
during runtime.
• Obviously solution is neither readable and runtime
if’s have a performance impact.
I/O Kit
• A generic driver is then
used as a base class and
more specific driver inherits
all the code from this class
and overwrites all the
Functions that need different
implementation
• I/O kit offers many more
powerful features.
Booting
PC/BIOS
On a PC booting works through the framework called
BIOS (basic input output system) which is 16bit firmware
which can only address 1MB of memory
The only thing that BIOS does is read the first sector,
MBR (master boot record) and load into the lower 30kb
of ram
BIOS is not aware of any partitions or file systems, boot
sector takes care of these things
Booting
PC/BIOS
MBR
BIOS
1 MB
Video
640KB
RAM
0 MB
Booting
Mac (EFI)
The booting on mac is very different
The intel – mac firmware is called EFI(extensible
firmware interface) where as power pc – mac was open
firmware, it’s a different system but with a similar set
of features
In contrast to BIOS, EFI is 32 bit code and it understands
EFI partitioning scheme called ‘GPT’(GUID Partition
Table) GUID: (Globally Unique Identifiers)
It also understands APM(Apple Partition Map) file system
used in power pc-macs and also can read HFS (
Hierarchical file system)
It detects which partitions are bootable by looking into
the file system
Booting
(EFI)
The driver kexts are loaded by going through all the
objects in system library extensions and they are
matched against the detected hardware
In case neither the kernel, nor the hardware, drivers
are changed from the last boot, the kernel cache is
loaded instead , which is the kernel and a set of
necessary kexts that are pre linked in to a flat image
The kernel file is called mach_kernel
Booting
EFI
GPT
/System/Library
CoreServices/boot.efi
EFI
4 GB
HFS+
Video
mach_kernel
HFS+
2 GB
RAM
0 GB
Mach-O
If we look at file type of an executable like a kernel file
or a library, you will see that instead of virtually every
other UNIX like system Mac OS X doesn’t use
elf(Executable and linking format) but Mach–O
Eg: file libsystem.dylib : Mach–O
libsystem.dylib is a universal binary with 4
architectures 1) 32bit-ppc 2) 64bit-ppc 3) 32bit-i386
4) 64bit-i386
One advantage of this is no separate directories for 32
bit and 64bit are needed like in windows/ linux
Mach-O
Every single binary in Mac OS including the kernel can
run on both power pc and intel.
So the same hard disk can be used to boot computers
with totally incompatible architectures.
3/1 Switch
Intel kernel of Mac OS X has some distinguishing
features from other kernels.
One of them is the split of virtual address space in to
the user and kernel part.
On most OS, user mode occupies the lower 3GB and the
kernel mode the top 1GB. When the kernel switches
between tasks, it replaces the lower 3GB with pages of
another task
3/1 Switch
4 GB
kernel
3GB
user
0 GB
Windows/Linux
4/4 Switch
Instead 3/1, Mac OS X uses 4/4 switch, so both the
kernel and the user get 4GB each. Consequently only
one of them can be mapped at a time.
A tiny switcher is used to change the page table from
kernel to user.
This design is chosen to be able to map more devices
including large graphic cards in to the kernel address
space
Also it gives 4GB space for user tasks
There will be performance issues because of the
switching of whole page table and also TLB flush
4/4 Switch
4 GB
switcher
user
Kernel
0 GB
Mac OS X Intel
WHAT MAKES XNU
GREAT?
dtrace
dtrace-It is a frame work to get statistical data from the kernel with
near zero speed impact.
Traditionally user must write separate kernel code and a logging code
to get the data which results in decrease of System performance.
On a running system it can just rewrite the kernel code with out an
extra logging code thus it improves performance.
It is useful to get which system call does the application and how
often and no of memory allocations for a driver etc.
Kernel Cache
Linux has a similar system in which main root directories are not
loaded immediately.
this cache contains all kernel extensions that may be needed to boot
a mac with any hardware configuration.
Here the boot loader just loads a single file.
When a change is made to the kext system automatically updates the
kernel cache.
Separation between
Mach,BSD ,I/O kit
In Unix every system call can call every other system call.
Though it does not have advantages of microkernel, in XNU strict
layering is done.
5.POSIX Conformance
Portable Operating system interface-is a family of standards specified
by the ieee for maintaining compatibility between operating systems.
Mach message api
It handles security queuing and even data conversion.
It is handled by the kernel itself not by a separate library.
I/O kit
Is a modern objected oriented driver infrastructure that supports
inheritance and driver stacking.
Supports automatic matching and loading.
Stable KEXT ABI
Linux driver module typically doesn’t load in to another kernel
division.
While this has the advantage that linux can change the driver APIs.
This makes the third parties difficult to provide the users with
drivers.
But in xnu they can just download the drivers and install them
Universal libraries
Can boot on any type architecture.
Only single type of os.
AWESOME
MAC OS is awesome
That’s a fact.