Drivers and the Kernel
Computer Center, CS, NCTU 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
Computer Center, CS, NCTU 3 Roles of the Kernel – (2)
Computer Center, CS, NCTU 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
Computer Center, CS, NCTU 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
Computer Center, CS, NCTU 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: irq 12 on atkbdc0 psm0: [GIANT-LOCKED] psm0: [ITHREAD] psm0: model Generic PS/2 mouse, device ID 0 class=0x card=0x058a1014 chip=0x c vendor = 'Atheros Communications Inc.' device = 'AR5212 Atheros AR abg wireless' class = network subclass = ethernet
Computer Center, CS, NCTU 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 xffffffff a2dc8 kernel 2 1 0xffffffff807a3000 1abd08 zfs.ko 3 2 0xffffffff8094f000 3a68 opensolaris.ko 4 4 0xffffffff f348 krpc.ko 5 1 0xffffffff bc28 geom_label.ko
Computer Center, CS, NCTU 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/ /conf –GENERIC, NOTES, LINT (generated by “ make LINT ” ) –/usr/src/sys/conf/NOTESfor architecture independent options Customized file cp GENERIC /usr/local/etc/MYKERNEL ln -s /usr/local/etc/MYKERNEL /usr/src/sys/ /conf/
Computer Center, CS, NCTU 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)
Computer Center, CS, NCTU 10 The Configuration File Each line contains a keyword and one or more arguments KeywordFunctionExample machineSets the machine typei386 or amd64 cpuSets the CPU type I686_CPU or HAMMER identSets the name of the kernelMYKERNEL optionsSets various comiple-time optionsINET or INET6 deviceDeclares devicesfxp includeInclude another kernel config fileGENERIC nooptionsUnset optionsMD_ROOT nodeviceUnset devicesfdc
Computer Center, CS, NCTU 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
Computer Center, CS, NCTU 12 Ok mode
Computer Center, CS, NCTU 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.maxfilesprint the description of the variable % sysctl kern.maxfilesprint the value of the variable % sudo sysctl kern.maxfiles=2048
Computer Center, CS, NCTU 14 Reference References config.htmlhttp:// config.html /usr/src/sys/ /conf NOTES LINT GENERIC Further reading limits.htmlhttp:// limits.html handbook/kerneldebug.htmlhttp:// handbook/kerneldebug.html