Drivers and the Kernel

lwhsu (2019, CC-BY) ? (?-2018) interpret – UNIX Kernel Kernel Shell and UNIX Introduction Introduction

Computer Center, CS, NCTU 2 time time structure of the kernel - Design and Implementation of the FreeBSD Operating System, The, 2nd Edition 2ndThe, System, OperatingFreeBSD of the Implementation and Design - Run Figure 3.1 Figure

Computer Center, CS, NCTU 3 Time Time sharing, memory protect – level abstractions level level programs level level interface level - - - File system management Filesystem management Memory I/O management drivers Process managements Process ➢ ➢ ➢ ➢ ➢ Low User Kernel Hardware High • • • • • Two roles of kernel (OS) of kernel Two roles Components of a UNIX of a UNIX SystemComponents ❑ ❑ Roles ofKernel

Computer Center, CS, NCTU 4 Design and Implementation of the FreeBSD Operating System, The, 2nd Edition 2ndThe, System, OperatingFreeBSD of the Implementation and Design - Kernel I/O structure Figure 7.1 Figure

Computer Center, CS, NCTU 5 https://en.wikipedia.org/wiki/Microkernel Kernel Types

Computer Center, CS, NCTU 6 ), modulized … … efficient less e.g., UNIX) – – modulized defined interfaces - Moreintegrated… kernel Message passing passing Message 龐大的 → e.g., I/O management in the user user in levelthe management I/O e.g., Concept of beingConcept essential functionalities; provides only levelintouser functionsother sophisticatedPut increase scalability and less difficult in maintenance and difficult less scalabilityincrease to communicate?How added via well • • derived systemderived - Completely Completely modular kernel necessary Loadmodule it whenis needed Much the of kernel's functionality is contained in modules Provide only necessarily, compact small and functionalities Other functions is Whole functionalities in one kernel ➢ ➢ ➢ ➢ ➢ ➢ Solaris BSD/Linux Microkernel kernel( Monolithic • • • • Modern OS Modern Two extreme types Twoextreme Monolithic kernel developing towards micro kernel (being (being more micro kernelkerneltowards Monolithicdeveloping problem passing) (message but without IPC ❑ ❑ Kernel Types

Computer Center, CS, NCTU 7 or vmlinuz vmlinuz vmunix Kernel file Kernel 4.x) (< /kernel 5.x) (>= /boot/kernel/kernel / /boot/ /kernel/unix / - /usr/kvm/sys Build Directory Build /usr/src/sys /usr/src/linux Solaris SunOS Linux System FreeBSD Build directory and locationand directoryBuild ❑ Kernel related directory

Computer Center, CS, NCTU 8 emory usage emory m → Generic: with with devices…, various Generic: supported functions image kernel of kernelimplementation often big and common and big often securityhole adjusting important systemparametersadjusting important tune system performance system tune - Such as Such Purge unnecessary kernel devices and kernelunnecessarydevicesPurge options youwant thatAdd functionalities Remedy • • • • Adding device drivers device Adding time boot Fast usage memory Lower Fine OS patch OS The native kernel is is kernel The native situation site match to kernel Tailoring ❑ ❑ ❑ ❑ ❑ ❑ ❑ Why Why configure the kernel?

Computer Center, CS, NCTU 9 , sparc64 powerpc , building.html - mips , riscv LINT file: lists all all lists options file: LINT represents one of amd64, arm, of amd64, one represents i386, arm64, conf KERNCONF=SABSD KERNCONF=SABSD ; /sys//conf/ src / src buildkernel installkernel / /sys /sys// usr usr src src For example, For example, save a configuration file named as SABSD GENERIC LINT (generated `make by LINT` under this directory / / ➢ ➢ ➢ usr usr Edit / Edit cd % / make % make % / / • • • • • • Steps to build a new kernel a new build to Steps Kernel source Kernel file configuration Kernel https://www.freebsd.org/doc/en/books/handbook/kernelconfig ❑ ❑ ❑ Building FreeBSD Kernel a

Computer Center, CS, NCTU 10 OptionSettings? Device Drivers? WhatChoose? to What toLoad? ❑ ❑ ❑ ❑ To BuildTo a FreeBSD Kernel…

Computer Center, CS, NCTU 11 Listing devices from dmesg devices Listing Listing devices M$ windows from devices Listing 12 on atkbdc0 atkbdc0 on 12 irq LOCKED] LOCKED] dmesg.boot - Device ManagerDevice display the system message buffer message system the display - (8) Get an inventory of the machine’s hardware machine’s the of inventory an Get Microsoft's dmesg /var/run/ cat psm0: psm0: psm0: [GIANT 0 device ID PS/2 mouse, Generic psm0: model psm0: [ITHREAD] • • • • dmesg Before venturing into kernel configuration kernelinto venturingBefore ❑ ❑ Finding system the hardware (1)

Computer Center, CS, NCTU 12 =0x00 =0x01 =0x00 =0x00 =0x00 =0x00 =0x00 hdr hdr hdr hdr hdr hdr hdr em l & man & l man man [device] - Atheros – & man & k - man man Find drivers from company name drivers from companyFind attacheddevicesallList ➢ ➢ ➢ man man pciconf • • pciconf ❑ Finding system the hardware (2) em0@pci0:3:0:0: class=0x020000 card=0x00008086 chip=0x10d38086 rev=0x00 em1@pci0:2:0:0: class=0x020000 card=0x00008086 chip=0x10d38086rev=0x00 ehci1@pci0:0:29:7: class=0x0c0320 card=0x3a3a8086 chip=0x3a3a8086 rev=0x00 pcib10@pci0:0:30:0: class=0x060401 card=0x244e8086 chip=0x244e8086rev=0x90 isab0@pci0:0:31:0: class=0x060100 card=0x3a168086 chip=0x3a168086rev=0x00 ahci0@pci0:0:31:2: class=0x010601 card=0x3a228086 chip=0x3a228086 rev=0x00 none8@pci0:0:31:3: class=0x0c0500 card=0x3a308086 chip=0x3a308086 rev=0x00

Computer Center, CS, NCTU 13 =0x00 =0x00 =0x00 hdr hdr hdr AC]' - V' - [Wireless CNVi LP - PCIe SSD' PCIe May not support by GENERC because of size, license, or… of size, license, because GENERC support by not May NVMe lv - pciconf • pciconf class = mass storage = mass class = NVM subclass subclass = = ethernet subclass Technology' = 'Micron/Crucial vendor = 'P1 device vendor = 'Intel Corporation' = 'Intel vendor I219 (6) Connection 'Ethernet = device = network class vendor = 'Intel Corporation' = 'Intel vendor Point = 'Cannon device = network class ❑ Finding system the hardware (3) nvme0@pci0:109:0:0: class=0x010802 card=0x2263c0a9 chip=0x2263c0a9 rev=0x03 rev=0x03 chip=0x2263c0a9 card=0x2263c0a9 class=0x010802 nvme0@pci0:109:0:0: em0@pci0:0:31:6: class=0x020000 card=0x20748086 chip=0x15be8086 rev=0x30 rev=0x30 chip=0x15be8086 card=0x20748086 class=0x020000 em0@pci0:0:31:6: none3@pci0:0:20:3: class=0x028000 card=0x00348086 chip=0x9df08086 rev=0x30 rev=0x30 chip=0x9df08086 card=0x00348086 class=0x028000 none3@pci0:0:20:3:

Computer Center, CS, NCTU 14 [device] man man • Man page for devices Man page ❑ Finding system the hardware (4)

Computer Center, CS, NCTU 15 em (1) debug symbols config.html , - gdb I586_CPUor HAMMER SABSD 0 INET6 INET, fxp Example i386 or amd64 amd64/conf/GENERIC The explanations on explanations The and devices… options Uniform Memory Architecture support – - InterNETworking time options - s table sizes ’ g Build # kernel with - Sets various comiple Declares devices Function Sets the machine type Sets the CPU type Sets the name of the kernel Sets the kernel em HAMMER DEBUG= Configuration Configuration file Keyword + arguments Keyword options options SCHED_ULE ULE # scheduler options NUMA # Non options PREEMPTION # Enable kernel thread preemption options INET # device cpu ident GENERIC makeoptions • )options Each line is a control phrase controlis a line Each device ident maxusers (no Keyword machine cpu https://www.freebsd.org/doc/en/books/handbook/kernelconfig ❑ Building Building Kernel a FreeBSD

Computer Center, CS, NCTU 16 kernel.old /kernel 4.x) your first! GENERIC /boot/kernel your cp Freebsd ( /kernel kernel.old Old kernel is automatically to moved is automatically Old kernel new the kernel making you’re when Your last chance to prevent module module prevent to chance last Your survive!! missing…to kernel.bad kernel.old kernel.old Or just simply simply Or just , / , /boot/ unload kernel; loadunloadkernel; modules loadkernel ➢ ➢ kernel.GENERIC ok mode ! mode ok /boot/kernel mv Put in the /boot directory /boot the in Put /boot/ /boot/GENERIC/kernel, / • • • • • If something goes wrong goes If something Kernel file locations file Kernel ❑ ❑ Kernel backup

Computer Center, CS, NCTU 17 Or “enable ok mode.. in the modules” Or “enable Ok modeOk

Computer Center, CS, NCTU 18 … sysctl.conf / etc … print the value of printthevaluethevariable print the description of the variableof printthedescription =2048 will be lost across across rebootwillbe lost list all kernelvariablesalllist sysctl kern.maxfiles kern.maxfiles to tune the kernel and test it, then recompile thekernelit,and thenrecompile thekernel to tune test a d - - kern.maxfiles [options] name[=value] name[=value] [options] sysctl sysctl sysctl sysctl sysctl sudo sysctl command The other way way into write Theother settings / to your is % % % % Dynamically set set or get kernelDynamically parameters made All changesby Use Format: % Ex: • • • • tuning(7) sysctl ❑ ❑ Tuning KernelFreeBSD the

Computer Center, CS, NCTU 19 kldstat zfs.ko opensolaris.ko krpc.ko if_le.ko - kld chiahung - (8) if_fxp ko kldunload [/boot/kernel] [/boot/kernel] kldload 3 2 0xc09e8000 3d5c 3 2 0xc09e8000 16b84 4 2 0xc09ec000 8c48 5 1 0xc0a03000 1 15 0xc0400000 4abd60 kernel 4abd60 1 15 0xc0400000 13b0fc 2 1 0xc08ac000 (8), (8), zfs Name Size Id Refs Address E.g., ➢ kldload /boot/kernel/*. 4.x) FreeBSD ( /modules • • • Examples in share/examples/ Examples Load/unload kernel modules kernel Load/unload kldstat Kernel module location module Kernel ❑ ❑ ❑ ❑ Kernel modules

Computer Center, CS, NCTU 20 loader.conf ko or oading Device a Module l for a device a for l - name] [module Recompile the kernel Recompile /boot/ [module in name]_enable="YES" Add a) b) pciconf BSD in name module for name vendor man /boot/kernel/*. in name the kldload by permanently Setup 5. 2. 3. 4. 1. Loading a device module device a Loading ❑ Procedure L of

Computer Center, CS, NCTU 21 } https://kernel.org linux / src / initramfs.img,System.map,vmlinuz usr N j modules modules_install menuconfig – Kernel package Kernel make make install make /boot/{ Check Install kernel toolchain kernel Install from code sourceGet / to Extract make make • • • • • • • • • • Check the distribution specified method specified the distribution Check General procedure General ❑ ❑ BuildingKernel Linux

Computer Center, CS, NCTU 22 - common - handbook/ch - conf official - team.pages.debian.net/kernel machine dependent kernel configuration notes. configuration kernel dependent machine common - - → /sys// src / https://wiki.centos.org/HowTos/Custom_Kernel https://kernel tasks.html#s https://wiki.ubuntu.com/Kernel/BuildYourOwnKernel https://wiki.archlinux.org/index.php/Kernel/Arch_Build_System NOTES LINT GENERIC config.html - • • • • • • • usr “building kernel” of Linux distributions documents distributions of Linux kernel” “building / http://www.freebsd.org/doc/en/books/handbook/kernelconfi g ❑ ❑ ❑ Reference

Computer Center, CS, NCTU 23