FPU_KERN(9)
β¨ FPU_KERN π β facility to use the FPU in the kernel
Okay bestie, so this is from the Kernel Developer's Manual and it's literally all about using the FPU inside kernel code. No cap, it's giving "niche but iconic" energy. Let's get into it π«Ά
πΈ SYNOPSIS πΈ
#include <machine/fpu.h>
struct fpu_kern_ctx *
fpu_kern_alloc_ctx(u_int flags);
void
fpu_kern_free_ctx(struct fpu_kern_ctx *ctx);
void
fpu_kern_enter(struct thread *td, struct fpu_kern_ctx *ctx,
u_int flags);
int
fpu_kern_leave(struct thread *td, struct fpu_kern_ctx *ctx);
int
fpu_kern_thread(u_int flags);
int
is_fpu_kern_thread(u_int flags);
π DESCRIPTION π
So the fpu_kern family of functions is literally the whole vibe for letting kernel code use the FPU hardware. And listenβmodern FPUs are not just about floating point arithmetic anymore. They're highkey serving advanced accelerators for cryptography and other computational-intensive algorithms. These facilities share registers with the FPU hardware, bestie. It's giving "multitasking queen" π
Typical kernel code does not need access to the FPU. Saving a large register file on each entry to the kernel would waste time, and we don't stan wasted cycles. When kernel code does use the FPU, the current FPU state must be saved to avoid corrupting the user-mode state, and vice versa. Periodt.
The management of the save and restore is automatic. The processor catches accesses to the FPU registers when the non-current context tries to access them. But explicit calls are required for the allocation of the save area and the notification of the start and end of the code using the FPU. So like, you gotta do some of the work yourself, bestie. No free rides π
The fpu_kern_alloc_ctx() function allocates the memory used by fpu_kern to track the use of the FPU hardware state and the related software state. It requires the flags argument, which currently accepts the following flags:
FPU_KERN_NOWAITβ Do not wait for the available memory if the request could not be satisfied without sleep.0β No special handling is required. Literally just vibes β¨
The function returns the allocated context area, or NULL if the allocation failed. So like, check your return values, bestie. Don't be out here dereferencing NULL, that's not the move π
The fpu_kern_free_ctx() function frees the context previously allocated by fpu_kern_alloc_ctx(). Clean up after yourself, queen π
The fpu_kern_enter() function designates the start of the region of kernel code where the use of the FPU is allowed. Its arguments are:
tdβ Currently must becurthread.ctxβ The context save area previously allocated byfpu_kern_alloc_ctx()and not currently in use by another call tofpu_kern_enter().flagsβ This argument currently accepts the following flags:FPU_KERN_NORMALβ Indicates that the caller intends to access the full FPU state. Must be specified currently. Like, it's mandatory, bestie.FPU_KERN_KTHRβ Indicates that no saving of the current FPU state should be performed, if the thread calledfpu_kern_thread(9)function. This is intended to minimize code duplication in callers which could be used from both kernel thread and syscall contexts. Thefpu_kern_leave()function correctly handles such contexts. iykyk π§FPU_KERN_NOCTXβ Avoid nesting save area. If the flag is specified, the ctx must be passed asNULL. The flag should only be used for really short code blocks which can be executed in a critical section. It avoids the need to allocate the FPU context by the cost of increased system latency. So like, it's a trade-off, bestie. Choose wisely π
The function does not sleep or block. It could cause an FPU trap during execution, and on the first FPU access after the function returns, as well as after each context switch. Oni386andamd64this will be the Device Not Available exception (see Intel Software Developer Manual for the reference). So like, don't be surprised if you see that exception, it's part of the process π₯
The fpu_kern_leave() function ends the region started by fpu_kern_enter(). It is erroneous to use the FPU in the kernel before fpu_kern_enter() or after fpu_kern_leave(). The function takes the td thread argument, which currently must be curthread, and the ctx context pointer, previously passed to fpu_kern_enter(). After the function returns, the context may be freed or reused by another invocation of fpu_kern_enter(). The function always returns 0. Like, it's not gonna fail you, bestie. It's reliable like that π
The fpu_kern_thread() function enables an optimization for threads which never leave to the usermode. The current thread will reuse the usermode save area for the kernel FPU state instead of requiring an explicitly allocated context. There are no flags defined for the function, and no error states that the function returns. Once this function has been called, neither fpu_kern_enter() nor fpu_kern_leave() is required to be called and the fpu is available for use in the calling thread. It's giving "set it and forget it" energy β¨
The is_fpu_kern_thread() function returns the boolean indicating whether the current thread entered the mode enabled by fpu_kern_thread(). There is currently no flags defined for the function, the return value is true if the current thread have the permanent FPU save area, and false otherwise. So like, it's a vibe check for your thread π
π¦ NOTES π¦
The fpu_kern is currently implemented only for the i386, amd64, arm64, and powerpc architectures. So like, if you're on something else, sorry bestie, not today π
There is no way to handle floating point exceptions raised from kernel mode. So like, don't come for us if that happens, it's just not in the cards π
The unused flags arguments to the fpu_kern functions are to be extended to allow specification of the set of the FPU hardware state used by the code region. This would allow optimizations of saving and restoring the state. So like, future improvements are coming, watch this space π
π AUTHORS π
The fpu_kern facitily and this manual page were written by Konstantin Belousov <kib@FreeBSD.org>. The arm64 support was added by Andrew Turner <andrew@FreeBSD.org>. The powerpc support was added by Shawn Anastasio <sanastasio@raptorengineering.com>. We stan our contributors π
β¨
π BUGS π
fpu_kern_leave() should probably have type void (like fpu_kern_enter()). Like, it's a minor thing but it's giving "inconsistent" energy, bestie. Not me pointing it out but like... someone had to say it π
This manual page dropped on October 13, 2020, from Debian. Stay iconic, bestie! π β¨π
π¦ where this page comes from
- freebsd 14.5: base 14.5
- freebsd 15.1: base 15.1
- nixpkgs unstable: source
- Copyright (c) 2014 Konstantin Belousov <kib@FreeBSD.org>. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms