156388ef9SKonstantin Belousov.\" Copyright (c) 2014 256388ef9SKonstantin Belousov.\" Konstantin Belousov <kib@FreeBSD.org>. All rights reserved. 356388ef9SKonstantin Belousov.\" 456388ef9SKonstantin Belousov.\" Redistribution and use in source and binary forms, with or without 556388ef9SKonstantin Belousov.\" modification, are permitted provided that the following conditions 656388ef9SKonstantin Belousov.\" are met: 756388ef9SKonstantin Belousov.\" 1. Redistributions of source code must retain the above copyright 856388ef9SKonstantin Belousov.\" notice, this list of conditions and the following disclaimer. 956388ef9SKonstantin Belousov.\" 2. Redistributions in binary form must reproduce the above copyright 1056388ef9SKonstantin Belousov.\" notice, this list of conditions and the following disclaimer in the 1156388ef9SKonstantin Belousov.\" documentation and/or other materials provided with the distribution. 1256388ef9SKonstantin Belousov.\" 1356388ef9SKonstantin Belousov.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1456388ef9SKonstantin Belousov.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1556388ef9SKonstantin Belousov.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 1656388ef9SKonstantin Belousov.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 1756388ef9SKonstantin Belousov.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 1856388ef9SKonstantin Belousov.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1956388ef9SKonstantin Belousov.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2056388ef9SKonstantin Belousov.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2156388ef9SKonstantin Belousov.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2256388ef9SKonstantin Belousov.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2356388ef9SKonstantin Belousov.\" 244ef6ea38SJohn Baldwin.Dd October 13, 2020 2504d4c8e4SChristian Brueffer.Dt FPU_KERN 9 2656388ef9SKonstantin Belousov.Os 2756388ef9SKonstantin Belousov.Sh NAME 2856388ef9SKonstantin Belousov.Nm fpu_kern 2956388ef9SKonstantin Belousov.Nd "facility to use the FPU in the kernel" 3056388ef9SKonstantin Belousov.Sh SYNOPSIS 314ef6ea38SJohn Baldwin.In machine/fpu.h 3256388ef9SKonstantin Belousov.Ft struct fpu_kern_ctx * 3356388ef9SKonstantin Belousov.Fn fpu_kern_alloc_ctx "u_int flags" 3456388ef9SKonstantin Belousov.Ft void 3556388ef9SKonstantin Belousov.Fn fpu_kern_free_ctx "struct fpu_kern_ctx *ctx" 364e6bbdd6SConrad Meyer.Ft void 3756388ef9SKonstantin Belousov.Fn fpu_kern_enter "struct thread *td" "struct fpu_kern_ctx *ctx" "u_int flags" 3856388ef9SKonstantin Belousov.Ft int 3956388ef9SKonstantin Belousov.Fn fpu_kern_leave "struct thread *td" "struct fpu_kern_ctx *ctx" 4056388ef9SKonstantin Belousov.Ft int 4156388ef9SKonstantin Belousov.Fn fpu_kern_thread "u_int flags" 4256388ef9SKonstantin Belousov.Ft int 4356388ef9SKonstantin Belousov.Fn is_fpu_kern_thread "u_int flags" 4456388ef9SKonstantin Belousov.Sh DESCRIPTION 4556388ef9SKonstantin BelousovThe 4656388ef9SKonstantin Belousov.Nm 4756388ef9SKonstantin Belousovfamily of functions allows the use of FPU hardware in kernel code. 4856388ef9SKonstantin BelousovModern FPUs are not limited to providing hardware implementation for 4904d4c8e4SChristian Bruefferfloating point arithmetic; they offer advanced accelerators for cryptography 5056388ef9SKonstantin Belousovand other computational-intensive algorithms. 5156388ef9SKonstantin BelousovThese facilities share registers with the FPU hardware. 5256388ef9SKonstantin Belousov.Pp 5304d4c8e4SChristian BruefferTypical kernel code does not need access to the FPU. 5456388ef9SKonstantin BelousovSaving a large register file on each entry to the kernel would waste 5556388ef9SKonstantin Belousovtime. 5656388ef9SKonstantin BelousovWhen kernel code uses the FPU, the current FPU state must be saved to 5756388ef9SKonstantin Belousovavoid corrupting the user-mode state, and vice versa. 5856388ef9SKonstantin Belousov.Pp 5956388ef9SKonstantin BelousovThe management of the save and restore is automatic. 6056388ef9SKonstantin BelousovThe processor catches accesses to the FPU registers 6156388ef9SKonstantin Belousovwhen the non-current context tries to access them. 6256388ef9SKonstantin BelousovExplicit calls are required for the allocation of the save area and 6356388ef9SKonstantin Belousovthe notification of the start and end of the code using the FPU. 6456388ef9SKonstantin Belousov.Pp 6556388ef9SKonstantin BelousovThe 6656388ef9SKonstantin Belousov.Fn fpu_kern_alloc_ctx 6756388ef9SKonstantin Belousovfunction allocates the memory used by 6856388ef9SKonstantin Belousov.Nm 6956388ef9SKonstantin Belousovto track the use of the FPU hardware state and the related software state. 7056388ef9SKonstantin BelousovThe 7156388ef9SKonstantin Belousov.Fn fpu_kern_alloc_ctx 7256388ef9SKonstantin Belousovfunction requires the 7356388ef9SKonstantin Belousov.Fa flags 7456388ef9SKonstantin Belousovargument, which currently accepts the following flags: 7556388ef9SKonstantin Belousov.Bl -tag -width ".Dv FPU_KERN_NOWAIT" -offset indent 7656388ef9SKonstantin Belousov.It Dv FPU_KERN_NOWAIT 7756388ef9SKonstantin BelousovDo not wait for the available memory if the request could not be satisfied 7856388ef9SKonstantin Belousovwithout sleep. 7956388ef9SKonstantin Belousov.It 0 8056388ef9SKonstantin BelousovNo special handling is required. 8156388ef9SKonstantin Belousov.El 8204d4c8e4SChristian Brueffer.Pp 8356388ef9SKonstantin BelousovThe function returns the allocated context area, or 8456388ef9SKonstantin Belousov.Va NULL 8556388ef9SKonstantin Belousovif the allocation failed. 8656388ef9SKonstantin Belousov.Pp 8756388ef9SKonstantin BelousovThe 8856388ef9SKonstantin Belousov.Fn fpu_kern_free_ctx 8956388ef9SKonstantin Belousovfunction frees the context previously allocated by 9056388ef9SKonstantin Belousov.Fn fpu_kern_alloc_ctx . 9156388ef9SKonstantin Belousov.Pp 9256388ef9SKonstantin BelousovThe 9356388ef9SKonstantin Belousov.Fn fpu_kern_enter 9456388ef9SKonstantin Belousovfunction designates the start of the region of kernel code where the 9556388ef9SKonstantin Belousovuse of the FPU is allowed. 9656388ef9SKonstantin BelousovIts arguments are: 9756388ef9SKonstantin Belousov.Bl -tag -width ".Fa ctx" -offset indent 9856388ef9SKonstantin Belousov.It Fa td 9956388ef9SKonstantin BelousovCurrently must be 10056388ef9SKonstantin Belousov.Va curthread . 10156388ef9SKonstantin Belousov.It Fa ctx 10256388ef9SKonstantin BelousovThe context save area previously allocated by 10356388ef9SKonstantin Belousov.Fn fpu_kern_alloc_ctx 10456388ef9SKonstantin Belousovand not currently in use by another call to 10556388ef9SKonstantin Belousov.Fn fpu_kern_enter . 10656388ef9SKonstantin Belousov.It Fa flags 10756388ef9SKonstantin BelousovThis argument currently accepts the following flags: 10856388ef9SKonstantin Belousov.Bl -tag -width ".Dv FPU_KERN_NORMAL" -offset indent 10956388ef9SKonstantin Belousov.It Dv FPU_KERN_NORMAL 11056388ef9SKonstantin BelousovIndicates that the caller intends to access the full FPU state. 11156388ef9SKonstantin BelousovMust be specified currently. 11256388ef9SKonstantin Belousov.It Dv FPU_KERN_KTHR 11356388ef9SKonstantin BelousovIndicates that no saving of the current FPU state should be performed, 11456388ef9SKonstantin Belousovif the thread called 11556388ef9SKonstantin Belousov.Xr fpu_kern_thread 9 11656388ef9SKonstantin Belousovfunction. 11756388ef9SKonstantin BelousovThis is intended to minimize code duplication in callers which 11856388ef9SKonstantin Belousovcould be used from both kernel thread and syscall contexts. 11956388ef9SKonstantin BelousovThe 12056388ef9SKonstantin Belousov.Fn fpu_kern_leave 12156388ef9SKonstantin Belousovfunction correctly handles such contexts. 122cf1c4776SKonstantin Belousov.It Dv FPU_KERN_NOCTX 123cf1c4776SKonstantin BelousovAvoid nesting save area. 124cf1c4776SKonstantin BelousovIf the flag is specified, the 125cf1c4776SKonstantin Belousov.Fa ctx 126cf1c4776SKonstantin Belousovmust be passed as 127cf1c4776SKonstantin Belousov.Va NULL . 128cf1c4776SKonstantin BelousovThe flag should only be used for really short code blocks 129cf1c4776SKonstantin Belousovwhich can be executed in a critical section. 130cf1c4776SKonstantin BelousovIt avoids the need to allocate the FPU context by the cost 131cf1c4776SKonstantin Belousovof increased system latency. 13256388ef9SKonstantin Belousov.El 13356388ef9SKonstantin Belousov.El 13404d4c8e4SChristian Brueffer.Pp 13556388ef9SKonstantin BelousovThe function does not sleep or block. 1366ed982a2SAndrew TurnerIt could cause an FPU trap during execution, and on the first FPU access 1376ed982a2SAndrew Turnerafter the function returns, as well as after each context switch. 1386ed982a2SAndrew TurnerOn i386 and amd64 this will be the 13956388ef9SKonstantin Belousov.Nm Device Not Available 1406ed982a2SAndrew Turnerexception (see Intel Software Developer Manual for the reference). 14156388ef9SKonstantin Belousov.Pp 14256388ef9SKonstantin BelousovThe 14356388ef9SKonstantin Belousov.Fn fpu_kern_leave 14456388ef9SKonstantin Belousovfunction ends the region started by 14556388ef9SKonstantin Belousov.Fn fpu_kern_enter . 1464e6bbdd6SConrad MeyerIt is erroneous to use the FPU in the kernel before 14756388ef9SKonstantin Belousov.Fn fpu_kern_enter 1484e6bbdd6SConrad Meyeror after 1494e6bbdd6SConrad Meyer.Fn fpu_kern_leave . 15056388ef9SKonstantin BelousovThe function takes the 15156388ef9SKonstantin Belousov.Fa td 15256388ef9SKonstantin Belousovthread argument, which currently must be 15356388ef9SKonstantin Belousov.Va curthread , 15456388ef9SKonstantin Belousovand the 15556388ef9SKonstantin Belousov.Fa ctx 15656388ef9SKonstantin Belousovcontext pointer, previously passed to 15756388ef9SKonstantin Belousov.Fn fpu_kern_enter . 15856388ef9SKonstantin BelousovAfter the function returns, the context may be freed or reused 1594e6bbdd6SConrad Meyerby another invocation of 16056388ef9SKonstantin Belousov.Fn fpu_kern_enter . 1614e6bbdd6SConrad MeyerThe function always returns 0. 16256388ef9SKonstantin Belousov.Pp 16356388ef9SKonstantin BelousovThe 16456388ef9SKonstantin Belousov.Fn fpu_kern_thread 165dbb91b14SJohn-Mark Gurneyfunction enables an optimization for threads which never leave to 16656388ef9SKonstantin Belousovthe usermode. 167dbb91b14SJohn-Mark GurneyThe current thread will reuse the usermode save area for the kernel FPU state 168dbb91b14SJohn-Mark Gurneyinstead of requiring an explicitly allocated context. 169dbb91b14SJohn-Mark GurneyThere are no flags defined for the function, and no error states 17056388ef9SKonstantin Belousovthat the function returns. 171dbb91b14SJohn-Mark GurneyOnce this function has been called, neither 172dbb91b14SJohn-Mark Gurney.Fn fpu_kern_enter 173dbb91b14SJohn-Mark Gurneynor 174dbb91b14SJohn-Mark Gurney.Fn fpu_kern_leave 175dbb91b14SJohn-Mark Gurneyis required to be called and the fpu is available for use in the calling thread. 17656388ef9SKonstantin Belousov.Pp 17756388ef9SKonstantin BelousovThe 17856388ef9SKonstantin Belousov.Fn is_fpu_kern_thread 17956388ef9SKonstantin Belousovfunction returns the boolean indicating whether the current thread 18056388ef9SKonstantin Belousoventered the mode enabled by 18156388ef9SKonstantin Belousov.Fn fpu_kern_thread . 18256388ef9SKonstantin BelousovThere is currently no flags defined for the function, the return 18356388ef9SKonstantin Belousovvalue is true if the current thread have the permanent FPU save area, 18456388ef9SKonstantin Belousovand false otherwise. 18556388ef9SKonstantin Belousov.Sh NOTES 18656388ef9SKonstantin BelousovThe 18756388ef9SKonstantin Belousov.Nm 188*a6662c37SShawn Anastasiois currently implemented only for the i386, amd64, arm64, and powerpc 189*a6662c37SShawn Anastasioarchitectures. 19056388ef9SKonstantin Belousov.Pp 19156388ef9SKonstantin BelousovThere is no way to handle floating point exceptions raised from 19256388ef9SKonstantin Belousovkernel mode. 19356388ef9SKonstantin Belousov.Pp 19456388ef9SKonstantin BelousovThe unused 19556388ef9SKonstantin Belousov.Fa flags 19656388ef9SKonstantin Belousovarguments 19756388ef9SKonstantin Belousovto the 19856388ef9SKonstantin Belousov.Nm 19956388ef9SKonstantin Belousovfunctions are to be extended to allow specification of the 20056388ef9SKonstantin Belousovset of the FPU hardware state used by the code region. 20156388ef9SKonstantin BelousovThis would allow optimizations of saving and restoring the state. 20256388ef9SKonstantin Belousov.Sh AUTHORS 20356388ef9SKonstantin BelousovThe 20456388ef9SKonstantin Belousov.Nm 20556388ef9SKonstantin Belousovfacitily and this manual page were written by 20656388ef9SKonstantin Belousov.An Konstantin Belousov Aq Mt kib@FreeBSD.org . 2076ed982a2SAndrew TurnerThe arm64 support was added by 2086ed982a2SAndrew Turner.An Andrew Turner Aq Mt andrew@FreeBSD.org . 209*a6662c37SShawn AnastasioThe powerpc support was added by 210*a6662c37SShawn Anastasio.An Shawn Anastasio Aq Mt sanastasio@raptorengineering.com . 2114e6bbdd6SConrad Meyer.Sh BUGS 2124e6bbdd6SConrad Meyer.Fn fpu_kern_leave 2134e6bbdd6SConrad Meyershould probably have type 2144e6bbdd6SConrad Meyer.Ft void 2154e6bbdd6SConrad Meyer(like 2164e6bbdd6SConrad Meyer.Fn fpu_kern_enter ) . 217