xref: /freebsd/share/man/man9/intro.9 (revision e1c4c8dd8d2d10b6104f06856a77bd5b4813a801)
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 January 30, 2024
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 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
181.Xr vnode 9
182is the abstract and filesystem-independent representation of a file,
183directory, or other file-like entity within the kernel.
184.Pp
185The implementation of access control lists for filesystems is described by
186.Xr acl 9 .
187Also
188.Xr vaccess 9 .
189.Ss I/O and Storage
190.\" TODO: This page needs to be rewritten before it can be included here.
191.\" The buffer cache is described by:
192.\" .Xr buf 9
193.\" .Pp
194The GEOM framework represents I/O requests using the
195.Xr bio 9
196structure.
197.Pp
198Disk drivers connect themselves to GEOM using the
199.Xr disk 9
200API.
201.Pp
202The
203.Xr devstat 9
204facility provides an interface for recording device statistics in disk drivers.
205.Ss Networking
206Much of the networking stack uses the
207.Xr mbuf 9 ,
208a flexible memory management unit commonly used to store network packets.
209.Pp
210Network interfaces are implemented using the
211.Xr ifnet 9
212API, which has functions for drivers and consumers.
213.Pp
214A framework for managing packet output queues is described by
215.Xr altq 9 .
216.Pp
217To receive incoming packets, network protocols register themselves with
218.Xr netisr 9 .
219.Pp
220Virtualization of the network stack is provided by
221.Xr VNET 9 .
222.Pp
223The front-end for interfacing with network sockets from within the kernel is
224described by
225.Xr socket 9 .
226The back-end interface for socket implementations is
227.Xr domain 9 .
228.Pp
229The low-level packet filter interface is described by
230.Xr pfil 9 .
231.Pp
232The
233.Xr bpf 9
234interface provides a mechanism to redirect packets to userspace.
235.Pp
236The subsystem for IEEE 802.11 wireless networking is described by
237.Xr ieee80211 9 .
238.Pp
239A framework for modular TCP implementations is described by
240.Xr tcp_functions 9 .
241.Pp
242A framework for modular congestion control algorithms is described by
243.Xr mod_cc 9 .
244.Ss Device Drivers
245.\" TODO: a bus(9) or newbus(9) page, as well as updates to existing pages
246.\" would be helpful in laying out the high-level concepts of FreeBSD's device
247.\" structure, and explaining the organization of existing documentation.
248Consult the
249.Xr device 9
250and
251.Xr driver 9
252pages first.
253.Pp
254Most drivers act as devices, and provide a set of methods implementing the
255device interface.
256This includes methods such as
257.Xr DEVICE_PROBE 9 ,
258.Xr DEVICE_ATTACH 9 ,
259and
260.Xr DEVICE_DETACH 9 .
261.Pp
262In addition to devices, there are buses.
263Buses may have children, in the form of devices or other buses.
264Bus drivers will implement additional methods, such as
265.Xr BUS_ADD_CHILD 9 ,
266.Xr BUS_READ_IVAR 9 ,
267or
268.Xr BUS_RESCAN 9 .
269.Pp
270Buses often perform resource accounting on behalf of their children.
271For this there is the
272.Xr rman 9
273API.
274.Pp
275Drivers can request and manage their resources (e.g. memory-space or IRQ
276number) from their parent using the following sets of functions:
277.Bd -ragged -offset indent
278.Xr bus_alloc_resource 9 ,
279.Xr bus_adjust_resource 9 ,
280.Xr bus_get_resource 9 ,
281.Xr bus_map_resource 9 ,
282.Xr bus_release_resource 9 ,
283.Xr bus_set_resource 9
284.Ed
285.Pp
286Direct Memory Access (DMA) is handled using the
287.Xr busdma 9
288framework.
289.Pp
290Functions for accessing bus space (i.e. read/write) are provided by
291.Xr bus_space 9 .
292.Ss Clocks and Timekeeping
293The kernel clock frequency and overall system time model is described by
294.Xr hz 9 .
295.Pp
296A few global time variables, such as system up-time, are described by
297.Xr time 9 .
298.Pp
299Raw CPU cycles are provided by
300.Xr get_cyclecount 9 .
301.Ss Userspace Memory Access
302Direct read/write access of userspace memory from the kernel is not permitted,
303and memory transactions that cross the kernel/user boundary must go through one
304of several interfaces built for this task.
305.Pp
306Most device drivers use the
307.Xr uiomove 9
308set of routines.
309.Pp
310Simpler primitives for reading or writing smaller chunks of memory are
311described by
312.Xr casuword 9 ,
313.Xr copy 9 ,
314.Xr fetch 9 ,
315and
316.Xr store 9 .
317.Ss Kernel Threads, Tasks, and Callbacks
318Kernel threads and processes are created using the
319.Xr kthread 9
320and
321.Xr kproc 9
322interfaces, respectively.
323.Pp
324Where dedicated kernel threads are too heavyweight, there is also the
325.Xr taskqueue 9
326interface.
327.Pp
328For low-latency callback handling, the
329.Xr callout 9
330framework should be used.
331.Pp
332Dynamic handlers for pre-defined event hooks are registered and invoked using
333the
334.Xr EVENTHANDLER 9
335API.
336.Ss Thread Switching and Scheduling
337The machine-independent interface to a context switch is
338.Xr mi_switch 9 .
339.Pp
340To prevent preemption, use a
341.Xr critical 9
342section.
343.Pp
344To voluntarily yield the processor, use
345.Xr kern_yield 9 .
346.Pp
347The various functions which will deliberately put a thread to sleep are
348described by
349.Xr sleep 9 .
350Sleeping threads are removed from the scheduler and placed on a
351.Xr sleepqueue 9 .
352.\" TODO: This page is outdated and can't be included here yet.
353.\".Pp
354.\"The thread scheduler interface is described by
355.\".Xr scheduler 9 .
356.Ss Processes and Signals
357To locate a process or process group by its identifier, use
358.Xr pfind 9
359and
360.Xr pgfind 9 .
361Alternatively, the
362.Xr pget 9
363function provides additional search specificity.
364.Pp
365The "hold count" of a process can be manipulated with
366.Xr PHOLD 9 .
367.Pp
368The kernel interface for signals is described by
369.Xr signal 9 .
370.Pp
371Signals can be sent to processes or process groups using the functions
372described by
373.Xr psignal 9 .
374.Ss Security
375See the overview in
376.Xr security 7 .
377.Pp
378The basic structure for user credentials is
379.Vt struct ucred ,
380managed by the
381.Xr ucred 9
382API.
383Thread credentials are verified using
384.Xr priv 9
385to allow or deny certain privileged actions.
386.Pp
387Policies influenced by
388.Va kern.securelevel
389must use the
390.Xr securelevel_gt 9
391or
392.Xr securelevel_ge 9
393functions.
394.Pp
395The Mandatory Access Control (MAC) framework provides a wide set of hooks,
396supporting dynamically-registered security modules;
397see
398.Xr mac 9 .
399.Pp
400Cryptographic services are provided by the OpenCrypto framework.
401This API provides an interface for both consumers and crypto drivers;
402see
403.Xr crypto 9 .
404.Pp
405For information on random number generation, see
406.Xr random 9
407and
408.Xr prng 9 .
409.Ss Kernel Modules
410The interfaces for declaring loadable kernel modules are described by
411.Xr module 9 .
412.Ss Interrupts
413.Xr intr_event 9
414describes the machine-independent portion of the interrupt framework
415that supports registration and execution of interrupt handlers.
416.Pp
417Software interrupts are provided by
418.Xr swi 9 .
419.Pp
420Device drivers register their interrupt handlers using the
421.Xr bus_setup_intr 9
422function.
423.Ss Testing and Debugging Tools
424A kernel test framework:
425.Xr kern_testfrwk 9
426.Pp
427A facility for defining configurable fail points is described by
428.Xr fail 9 .
429.Pp
430Commands for the
431.Xr ddb 4
432kernel debugger are defined with the
433.Xr DB_COMMAND 9
434family of macros.
435.Pp
436The
437.Xr ktr 4
438tracing facility adds static tracepoints to many areas of the kernel.
439These tracepoints are defined using the macros described by
440.Xr ktr 9 .
441.Pp
442Static probes for DTrace are defined using the
443.Xr SDT 9
444macros.
445.Pp
446Stack traces can be captured and printed with the
447.Xr stack 9
448API.
449.Pp
450Kernel sanitizers can perform additional compiler-assisted checks against
451memory use/access.
452These runtimes are capable of detecting difficult-to-identify classes of bugs,
453at the cost of a large overhead.
454The Kernel Address Sanitizer
455.Xr KASAN 9
456and Kernel Memory Sanitizer
457.Xr KMSAN 9
458are supported.
459.Pp
460The
461.Xr LOCK_PROFILING 9
462kernel config option enables extra code to assist with profiling and/or
463debugging lock performance.
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