FPU_KERN(9)
π FPU_KERN π
ok so bestie, welcome to fpu_kern(9) β it's the Kernel Developer's Manual page for the facility that lets you use the FPU in the kernel. yes, the FPU, in the kernel, we're doing this π«Ά
β¨ NAME β¨
fpu_kern β the facility to use the FPU in the kernel. that's it, that's the vibe π
β¨ SYNOPSIS β¨
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 squad that lets kernel code use FPU hardware. and ngl, modern FPUs are NOT just doing floating point arithmetic anymore β they're out here offering advanced accelerators for cryptography and other computational-intensive algorithms, main character energy fr. the catch? all these facilities share registers with the FPU hardware. sharing is caring until it isn't π
ok so typical kernel code does not need access to the FPU. saving a big register file on every single entry to the kernel would just waste time, and we don't do that. but the moment kernel code does use the FPU, the current FPU state must be saved to avoid corrupting the user-mode state, and vice versa. it goes both ways bestie π
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. so what do you actually have to do? 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. that's the whole job π
π fpu_kern_alloc_ctx()
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. no waiting, we're busy πβοΈ0β no special handling is required. the "just vibing" option πΈ
the function returns the allocated context area, or NULL if the allocation failed. (yes, check it. please. π)
π fpu_kern_free_ctx()
the fpu_kern_free_ctx() function frees the context previously allocated by fpu_kern_alloc_ctx(). give it back, it's not yours anymore β¨
π fpu_kern_enter()
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(). don't share, it's not that kind of party π«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, mandatory, no skipping πFPU_KERN_KTHRβ indicates that no saving of the current FPU state should be performed, if the thread called thefpu_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. so like, one code path for two situations, we love efficiency πFPU_KERN_NOCTXβ avoid nesting save area. if the flag is specified, thectxmust 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, short and sweet, or the latency tax comes for you π
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. on i386 and amd64 this will be the Device Not Available exception (see Intel Software Developer Manual for the reference). yeah, that's a real exception name, we don't make the rules π
π fpu_kern_leave()
the fpu_kern_leave() function ends the region started by fpu_kern_enter().
β οΈ it is erroneous to use the FPU in the kernel beforefpu_kern_enter()or afterfpu_kern_leave(). do not do this. seriously. π βοΈ
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. always. no drama, no surprises, we love that for us π«Ά
π fpu_kern_thread()
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. iconic behavior honestly β¨
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. permanent FPU access, it's giving main character π
π is_fpu_kern_thread()
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. a little vibe check for your thread π
β¨ NOTES β¨
- the
fpu_kernis currently implemented only for thei386,amd64, andarm64architectures. if you're on something else, sorry bestie π - there is no way to handle floating point exceptions raised from kernel mode. none. zero. it's just not happening. π
- the unused
flagsarguments to thefpu_kernfunctions 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. future glow-up pending β³β¨
β¨ 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>. absolute legends, we stan ππ
β¨ BUGS β¨
fpu_kern_leave()should probably have typevoid(likefpu_kern_enter()). yeah. it just should. π
ok that's the whole page bestie, you're officially FPU-kernel-pilled π β¨
this one's from the Debian edition of the Kernel Developer's Manual, dated March 7, 2018 β mwah, take care of her π
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 2014 Konstantin Belousov <kib@FreeBSD.org>. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms