Drivers and the Kernel
Com
pu
ter C
en
ter, C
S, N
CT
U
2
Roles of the Kernel – (1)
Components of a UNIX System
• User-level programs
• Kernel
• Hardware
Kernel manages the resources
• CPU – process management
• Memory – memory management
• I/O devices – device management
Kernel provides method for
• Inter-process communication (IPC)
Synchronization, communication
• Processes to request resources
System calls
Com
pu
ter C
en
ter, C
S, N
CT
U
3
Roles of the Kernel – (2)
Com
pu
ter C
en
ter, C
S, N
CT
U
4
Kernel-wide Design Approaches
Monolithic kernels
• Kernel space, shared memory, fast, …, need reboot
• Eg. Linux, *BSD, MS-DOS, MS Windows 9x series…
Microkernels
• User space, message passing, slow, …, as services
• Eg. Mac OS X, BeOS, …
Hybrid
• Monolithic + Micro
• Eg. MS Windows NT series, DragonFly BSD, …
Nanokernels
Exokernels
Com
pu
ter C
en
ter, C
S, N
CT
U
5
Why Build a Custom Kernel
FreeBSD kernel
• Monolithic modular
• The functionality cannot be dynamically loadable
Building a custom kernel
• To be an advanced BSD user
• Time-consuming task
Customized kernel vs. GENERIC
• Fast boot time
• Lower memory usage
• Additional hardware support
• Fine-tune system performance
Such as adjusting important system parameters
Com
pu
ter C
en
ter, C
S, N
CT
U
6
Finding the System Hardware
Before venturing into kernel configuration
• Get an inventory of the machine’s hardware
• Microsoft's Device Manager
dmesg
• cat /var/run/dmesg.boot
pciconf -lv
• man 4 ath
• man -k Atheros
psm0: <PS/2 Mouse> irq 12 on atkbdc0
psm0: [GIANT-LOCKED]
psm0: [ITHREAD] psm0: model Generic PS/2 mouse, device ID 0
ath0@pci0:3:0:0: class=0x020000 card=0x058a1014 chip=0x1014168c
vendor = 'Atheros Communications Inc.'
device = 'AR5212 Atheros AR5212 802.11abg wireless'
class = network subclass = ethernet
Com
pu
ter C
en
ter, C
S, N
CT
U
7
Kernel Drivers and Modules
Before building a custom kernel
• Load kernel modules to get the specific hardware support
Kernel modules
• kldstat
• kldload
/boot/kernel/*.ko
Modify /boot/loader.conf to load modules in boot time automatically
– if_ath_load="YES"
• kldunload
zfs [/home/liuyh] -liuyh- kldstat Id Refs Address Size Name 1 62 0xffffffff80100000 6a2dc8 kernel 2 1 0xffffffff807a3000 1abd08 zfs.ko 3 2 0xffffffff8094f000 3a68 opensolaris.ko 4 4 0xffffffff80953000 1f348 krpc.ko 5 1 0xffffffff80973000 bc28 geom_label.ko
Com
pu
ter C
en
ter, C
S, N
CT
U
8
Building and Installing a Custom Kernel – (1)
Obtain full FreeBSD source tree
• sysinstall
Configure Distributions src All
• csup
csup -h cvsup.tw.freebsd.org /usr/share/examples/cvsup/standard-supfile
Customized kernel configuration file
• Sample files
/usr/src/sys/<ARCH>/conf
– GENERIC, NOTES, LINT (generated by “make LINT”)
– /usr/src/sys/conf/NOTES for architecture independent options
• Customized file
cp GENERIC /usr/local/etc/MYKERNEL
ln -s /usr/local/etc/MYKERNEL /usr/src/sys/<ARCH>/conf/
Com
pu
ter C
en
ter, C
S, N
CT
U
9
Building and Installing a Custom Kernel – (2)
Building a kernel
• cd /usr/src
• make buildkernel KERNCONF=MYKERNEL
• make installkernel KERNCONF=MYKERNEL
kernel = buildkernel + installkernel
• MODULES_OVERRIDE
Set to a list of modules to build instead of all of them
• NO_MODULES
Set to not build modules with the kernel
• WITHOUT_MODULES
Set to a list of modules to exclude from the build
Kernel locations
• /boot/kernel/kernel
• /boot/kernel /boot/kernel.old
make.conf(5)
Com
pu
ter C
en
ter, C
S, N
CT
U
10
The Configuration File
Each line contains a keyword and one or more arguments • http://www.freebsd.org/doc/en_US.ISO8859-1/books/handbook/kernelconfig-config.html
Keyword Function Example
machine Sets the machine type i386 or amd64
cpu Sets the CPU type I686_CPU or HAMMER
ident Sets the name of the kernel MYKERNEL
options Sets various comiple-time options INET or INET6
device Declares devices fxp
include Include another kernel config file GENERIC
nooptions Unset options MD_ROOT
nodevice Unset devices fdc
Com
pu
ter C
en
ter, C
S, N
CT
U
11
If Something Goes Wrong
Config failed
• Make sure the keyword is typed correctly
Make failed
• Look over your configuration
• Send mail to the FreeBSD general questions mailing list with kernel configuration
Boot failed
• ok mode
boot kernel.old
unload kernel && load /boot/kernel.old/kernel && boot
• nextboot(8)
ps(1) failed
• Recompile and install a world
Com
pu
ter C
en
ter, C
S, N
CT
U
12
Ok mode
Com
pu
ter C
en
ter, C
S, N
CT
U
13
Tuning the FreeBSD Kernel
sysctl command
• Dynamically set or get kernel parameters
• All changes made by sysctl will be lost across reboot
• Use sysctl to tune the kernel and test it, then recompile the kernel
• Format:
% sysctl [options] name[=value] …
Ex:
% sysctl –a list all kernel variables
% sysctl –d kern.maxfiles print the description of the variable
% sysctl kern.maxfiles print the value of the variable
% sudo sysctl kern.maxfiles=2048
Com
pu
ter C
en
ter, C
S, N
CT
U
14
Reference
References
• http://www.freebsd.org/doc/en/books/handbook/kernelconfig-
config.html
• /usr/src/sys/<ARCH>/conf
NOTES
LINT
GENERIC
Further reading
• http://www.freebsd.org/doc/handbook/configtuning-kernel-
limits.html
• http://www.freebsd.org/doc/en/books/developers-
handbook/kerneldebug.html