1.\" 2.\" SPDX-License-Identifier: BSD-2-Clause 3.\" 4.\" Copyright (c) 2023 The FreeBSD Foundation 5.\" 6.\" This manual page was written by Mitchell Horne <mhorne@FreeBSD.org> under 7.\" sponsorship from the FreeBSD Foundation. 8.\" 9.Dd August 2, 2023 10.Dt INTRO 9 11.Os 12.Sh NAME 13.Nm intro 14.Nd "introduction to kernel programming interfaces" 15.Sh DESCRIPTION 16Welcome to the 17.Fx 18kernel documentation. 19Outside of the source code itself, this set of 20.Xr man 1 21pages is the primary resource for information on usage of the numerous 22programming interfaces available within the kernel. 23In some cases, it is also a source of truth for the implementation details 24and/or design decisions behind a particular subsystem or piece of code. 25.Pp 26The intended audience of this documentation is developers, and the primary 27authors are also developers. 28It is written assuming a certain familiarity with common programming or 29OS-level concepts and practices. 30However, this documentation should also attempt to provide enough background 31information that readers approaching a particular subsystem or interface for 32the first time will be able to understand. 33.Pp 34To further set expectations, we acknowledge that kernel documentation, like the 35source code itself, is forever a work-in-progress. 36There will be large sections of the codebase whose documentation is subtly or 37severely outdated, or missing altogether. 38This documentation is a supplement to the source code, and can not always be 39taken at face value. 40.Pp 41At its best, section 9 documentation will provide a description of a particular 42piece of code that, paired with its implementation, fully informs the reader of 43the intended and realized effects. 44.Pp 45.Xr man 1 46pages in this section most frequently describe functions, but may also 47describe types, global variables, macros, or high-level concepts. 48.Sh CODING GUIDELINES 49Code written for the 50.Fx 51kernel is expected to conform to the established style and coding conventions. 52Please see 53.Xr style 9 54for a detailed set of rules and guidelines. 55.Sh OVERVIEW 56Below is presented various subsystems. 57.Ss Data Structures 58There are implementations for many well-known data structures available in the 59kernel. 60.Bl -tag -width "Xr bitstring 3" 61.It Xr bitstring 3 62Simple bitmap implementation. 63.It Xr counter 9 64An SMP-safe general-purpose counter implementation. 65.It Xr hash 9 66Hash map implementation. 67.It Xr nv 9 68Name/value pairs. 69.It Xr queue 3 70Singly-linked and doubly-linked lists, and queues. 71.It Xr refcount 9 72An SMP-safe implementation of reference counts. 73.It Xr sbuf 9 74Dynamic string composition. 75.It Xr sglist 9 76A scatter/gather list implementation. 77.El 78.Ss Utility Functions 79Functions or facilities of general usefulness or convenience. 80See also the 81.Sx Testing and Debugging Tools 82or 83.Sx Miscellaneous 84sub-sections below. 85.Pp 86Formatted output and logging functions are described by 87.Xr printf 9 . 88.Pp 89Endian-swapping functions: 90.Xr byteorder 9 . 91.Pp 92Data output in hexadecimal format: 93.Xr hexdump 9 . 94.Pp 95A rich set of macros for declaring 96.Xr sysctl 8 97variables and functions is described by 98.Xr sysctl 9 . 99.Pp 100Non-recoverable errors in the kernel should trigger a 101.Xr panic 9 . 102Run-time assertions can be verified using the 103.Xr KASSERT 9 104macros. 105Compile-time assertions should use 106.Fn _Static_assert . 107.Pp 108The SYSINIT framework provides macros for declaring functions that will be 109executed during start-up and shutdown; see 110.Xr SYSINIT 9 . 111.Pp 112Deprecation messages may be emitted with 113.Xr gone_in 9 . 114.Pp 115A unit number facility is provided by 116.Xr unr 9 . 117.Ss Synchronization Primitives 118The 119.Xr locking 9 120man page gives an overview of the various types of locks available in the 121kernel and advice on their usage. 122.Pp 123Atomic primitives are described by 124.Xr atomic 9 . 125.Pp 126The 127.Xr epoch 9 128and 129.Xr smr 9 130facilities are used to create lock-free data structures. 131There is also 132.Xr seqc 9 . 133.Ss Memory Management 134Dynamic memory allocations inside the kernel are generally done using 135.Xr malloc 9 . 136Frequently allocated objects may prefer to use 137.Xr uma 9 . 138.Pp 139.\" MHTODO: It would be useful to have a vm_page(9) or similar 140.\" high-level page which points to the following contents instead. 141Much of the virtual memory system operates on 142.Vt vm_page_t 143structures. 144The following functions are documented: 145.Bd -ragged -offset indent 146.Xr vm_page_advise 9 , 147.Xr vm_page_alloc 9 , 148.Xr vm_page_bits 9 , 149.Xr vm_page_aflag 9 , 150.Xr vm_page_alloc 9 , 151.Xr vm_page_bits 9 , 152.Xr vm_page_busy 9 , 153.Xr vm_page_deactivate 9 , 154.Xr vm_page_free 9 , 155.Xr vm_page_grab 9 , 156.Xr vm_page_insert 9 , 157.Xr vm_page_lookup 9 , 158.Xr vm_page_rename 9 , 159.Xr vm_page_sbusy 9 , 160.Xr vm_page_wire 9 161.Ed 162.Pp 163Virtual address space maps are managed with the 164.Xr vm_map 9 165API. 166.Pp 167The machine-dependent portion of the virtual memory stack is the 168.Xr pmap 9 169module. 170.Pp 171Allocation policies for NUMA memory domains are managed with the 172.Xr domainset 9 173API. 174.Ss File Systems 175The kernel interface for file systems is 176.Xr VFS 9 . 177File system implementations register themselves with 178.Xr vfsconf 9 . 179.Pp 180The abstract and filesystem-independent representation of a file, directory, or 181other file-like entity within the kernel is the 182.Xr vnode 9 . 183.Pp 184The implementation of access control lists for filesystems is described by 185.Xr acl 9 . 186Also 187.Xr vaccess 9 . 188.Ss I/O and Storage 189.\" TODO: This page needs to be rewritten before it can be included here. 190.\" The buffer cache is described by: 191.\" .Xr buf 9 192.\" .Pp 193The GEOM framework represents I/O requests using the 194.Xr bio 9 195structure. 196.Pp 197Disk drivers connect themselves to GEOM using the 198.Xr disk 9 199API. 200.Pp 201The 202.Xr devstat 9 203facility provides an interface for recording device statistics in disk drivers. 204.Ss Networking 205Much of the networking stack uses the 206.Xr mbuf 9 , 207a flexible memory management unit commonly used to store network packets. 208.Pp 209Network interfaces are implemented using the 210.Xr ifnet 9 211API, which has functions for drivers and consumers. 212.Pp 213A framework for managing packet output queues is described by 214.Xr altq 9 . 215.Pp 216To receive incoming packets, network protocols register themselves with 217.Xr netisr 9 . 218.Pp 219Virtualization of the network stack is provided by 220.Xr VNET 9 . 221.Pp 222The front-end for interfacing with network sockets from within the kernel is 223described by 224.Xr socket 9 . 225The back-end interface for socket implementations is 226.Xr domain 9 . 227.Pp 228The low-level packet filter interface is described by 229.Xr pfil 9 . 230.Pp 231The 232.Xr bpf 9 233interface provides a mechanism to redirect packets to userspace. 234.Pp 235The subsystem for IEEE 802.11 wireless networking is described by 236.Xr ieee80211 9 . 237.Pp 238A framework for modular TCP implementations is described by 239.Xr tcp_functions 9 . 240.Pp 241A framework for modular congestion control algorithms is described by 242.Xr mod_cc 9 . 243.Ss Device Drivers 244.\" TODO: a bus(9) or newbus(9) page, as well as updates to existing pages 245.\" would be helpful in laying out the high-level concepts of FreeBSD's device 246.\" structure, and explaining the organization of existing documentation. 247Consult the 248.Xr device 9 249and 250.Xr driver 9 251pages first. 252.Pp 253Most drivers act as devices, and provide a set of methods implementing the 254device interface. 255This includes methods such as 256.Xr DEVICE_PROBE 9 , 257.Xr DEVICE_ATTACH 9 , 258and 259.Xr DEVICE_DETACH 9 . 260.Pp 261In addition to devices, there are buses. 262Buses may have children, in the form of devices or other buses. 263Bus drivers will implement additional methods, such as 264.Xr BUS_ADD_CHILD 9 , 265.Xr BUS_READ_IVAR 9 , 266or 267.Xr BUS_RESCAN 9 . 268.Pp 269Buses often perform resource accounting on behalf of their children. 270For this there is the 271.Xr rman 9 272API. 273.Pp 274Drivers can request and manage their resources (e.g. memory-space or IRQ 275number) from their parent using the following sets of functions: 276.Bd -ragged -offset indent 277.Xr bus_alloc_resource 9 , 278.Xr bus_adjust_resource 9 , 279.Xr bus_get_resource 9 , 280.Xr bus_map_resource 9 , 281.Xr bus_release_resource 9 , 282.Xr bus_set_resource 9 283.Ed 284.Pp 285Direct Memory Access (DMA) is handled using the 286.Xr busdma 9 287framework. 288.Pp 289Functions for accessing bus space (i.e. read/write) are provided by 290.Xr bus_space 9 . 291.Ss Clocks and Timekeeping 292The kernel clock frequency and overall system time model is described by 293.Xr hz 9 . 294.Pp 295A few global time variables, such as system up-time, are described by 296.Xr time 9 . 297.Pp 298Raw CPU cycles are provided by 299.Xr get_cyclecount 9 . 300.Ss Userspace Memory Access 301Direct read/write access of userspace memory from the kernel is not permitted, 302and memory transactions that cross the kernel/user boundary must go through one 303of several interfaces built for this task. 304.Pp 305Most device drivers use the 306.Xr uiomove 9 307set of routines. 308.Pp 309Simpler primitives for reading or writing smaller chunks of memory are 310described by 311.Xr casuword 9 , 312.Xr copy 9 , 313.Xr fetch 9 , 314and 315.Xr store 9 . 316.Ss Kernel Threads, Tasks, and Callbacks 317Kernel threads and processes are created using the 318.Xr kthread 9 319and 320.Xr kproc 9 321interfaces, respectively. 322.Pp 323Where dedicated kernel threads are too heavyweight, there is also the 324.Xr taskqueue 9 325interface. 326.Pp 327For low-latency callback handling, the 328.Xr callout 9 329framework should be used. 330.Pp 331Dynamic handlers for pre-defined event hooks are registered and invoked using 332the 333.Xr EVENTHANDLER 9 334API. 335.Ss Thread Switching and Scheduling 336The machine-independent interface to a context switch is 337.Xr mi_switch 9 . 338.Pp 339To prevent preemption, use a 340.Xr critical 9 341section. 342.Pp 343To voluntarily yield the processor, use 344.Xr kern_yield 9 . 345.Pp 346The various functions which will deliberately put a thread to sleep are 347described by 348.Xr sleep 9 . 349Sleeping threads are removed from the scheduler and placed on a 350.Xr sleepqueue 9 . 351.\" TODO: This page is outdated and can't be included here yet. 352.\".Pp 353.\"The thread scheduler interface is described by 354.\".Xr scheduler 9 . 355.Ss Processes and Signals 356To locate a process or process group by its identifier, use 357.Xr pfind 9 358and 359.Xr pgfind 9 . 360Alternatively, the 361.Xr pget 9 362function provides additional search specificity. 363.Pp 364The "hold count" of a process can be manipulated with 365.Xr PHOLD 9 . 366.Pp 367The kernel interface for signals is described by 368.Xr signal 9 . 369.Pp 370Signals can be sent to processes or process groups using the functions 371described by 372.Xr psignal 9 . 373.Ss Security 374See the generic security overview in 375.Xr security 7 . 376.Pp 377The basic structure for user credentials is 378.Vt struct ucred , 379managed by the 380.Xr ucred 9 381API. 382Thread credentials are verified using 383.Xr priv 9 384to allow or deny certain privileged actions. 385.Pp 386Policies influenced by 387.Va kern.securelevel 388must use the 389.Xr securelevel_gt 9 390or 391.Xr securelevel_ge 9 392functions. 393.Pp 394The Mandatory Access Control (MAC) framework provides a wide set of hooks, 395supporting dynamically-registered security modules; 396see 397.Xr mac 9 . 398.Pp 399Cryptographic services are provided by the OpenCrypto framework. 400This API provides and interface for both consumers and crypto drivers; 401see 402.Xr crypto 9 . 403.Pp 404For information on random number generation, see 405.Xr random 9 406and 407.Xr prng 9 . 408.Ss Kernel Modules 409The interfaces for declaring loadable kernel modules are described by 410.Xr module 9 . 411.Ss Interrupts 412The machine-independent portion of the interrupt framework supporting the 413registration and execution of interrupt handlers is described by 414.Xr intr_event 9 . 415.Pp 416Software interrupts are provided by 417.Xr swi 9 . 418.Pp 419Device drivers register their interrupt handlers using the 420.Xr bus_setup_intr 9 421function. 422.Ss Testing and Debugging Tools 423A kernel test framework: 424.Xr kern_testfrwk 9 425.Pp 426A facility for defining configurable fail points is described by 427.Xr fail 9 . 428.Pp 429Commands for the 430.Xr ddb 4 431kernel debugger are defined with the 432.Xr DB_COMMAND 9 433family of macros. 434.Pp 435The 436.Xr ktr 4 437tracing facility adds static tracepoints to many areas of the kernel. 438These tracepoints are defined using the macros described by 439.Xr ktr 9 . 440.Pp 441Static probes for DTrace are defined using the 442.Xr SDT 9 443macros. 444.Pp 445Stack traces can be captured and printed with the 446.Xr stack 9 447API. 448.Pp 449Kernel sanitizers can perform additional compiler-assisted checks against 450memory use/access. 451These runtimes are capable of detecting difficult-to-identify classes of bugs, 452at the cost of a large overhead. 453Supported is the Kernel Address Sanitizer 454.Xr KASAN 9 , 455and the Kernel Memory Sanitizer 456.Xr KMSAN 9 . 457.Pp 458The 459.Cd LOCK_PROFILING 460kernel config option enables extra code to assist with profiling and/or 461debugging lock performance; 462see 463.Xr LOCK_PROFILING 9 . 464.Ss Driver Tools 465Defined functions/APIs for specific types of devices. 466.Bl -tag -width "Xr usbdi 9" 467.It Xr iflib 9 468Programming interface for 469.Xr iflib 4 470based network drivers. 471.It Xr pci 9 472Peripheral Component Interconnect (PCI) and PCI Express (PCIe) programming API. 473.It Xr pwmbus 9 474Pulse-Width Modulation (PWM) bus interface methods. 475.It Xr usbdi 9 476Universal Serial Bus programming interface. 477.It Xr superio 9 478Functions for Super I/O controller devices. 479.El 480.Ss Miscellaneous 481Dynamic per-CPU variables: 482.Xr dpcpu 9 . 483.Pp 484CPU bitmap management: 485.Xr cpuset 9 . 486.Pp 487Kernel environment management: 488.Xr getenv 9 . 489.Pp 490Contexts for CPU floating-point registers are managed by the 491.Xr fpu_kern 9 492facility. 493.Pp 494For details on the shutdown/reboot procedure and available shutdown hooks, see 495.Xr reboot 9 . 496.Pp 497A facility for asynchronous logging to files from within the kernel is provided 498by 499.Xr alq 9 . 500.Pp 501The 502.Xr osd 9 503framework provides a mechanism to dynamically extend core structures in a way 504that preserves KBI. 505See the 506.Xr hhook 9 507and 508.Xr khelp 9 509APIs for information on how this is used. 510.Pp 511The kernel object implementation is described by 512.Xr kobj 9 . 513.Sh SEE ALSO 514.Xr man 1 , 515.Xr style 9 516.Rs 517.%T "The FreeBSD Architecture Handbook" 518.%U "https://docs.freebsd.org/en/books/arch-handbook/" 519.Re 520