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.\" 2456388ef9SKonstantin Belousov.\" $FreeBSD$ 2556388ef9SKonstantin Belousov.\" 26*04d4c8e4SChristian Brueffer.Dd June 23, 2014 27*04d4c8e4SChristian Brueffer.Dt FPU_KERN 9 2856388ef9SKonstantin Belousov.Os 2956388ef9SKonstantin Belousov.Sh NAME 3056388ef9SKonstantin Belousov.Nm fpu_kern 3156388ef9SKonstantin Belousov.Nd "facility to use the FPU in the kernel" 3256388ef9SKonstantin Belousov.Sh SYNOPSIS 3356388ef9SKonstantin Belousov.Ft struct fpu_kern_ctx * 3456388ef9SKonstantin Belousov.Fn fpu_kern_alloc_ctx "u_int flags" 3556388ef9SKonstantin Belousov.Ft void 3656388ef9SKonstantin Belousov.Fn fpu_kern_free_ctx "struct fpu_kern_ctx *ctx" 3756388ef9SKonstantin Belousov.Ft int 3856388ef9SKonstantin Belousov.Fn fpu_kern_enter "struct thread *td" "struct fpu_kern_ctx *ctx" "u_int flags" 3956388ef9SKonstantin Belousov.Ft int 4056388ef9SKonstantin Belousov.Fn fpu_kern_leave "struct thread *td" "struct fpu_kern_ctx *ctx" 4156388ef9SKonstantin Belousov.Ft int 4256388ef9SKonstantin Belousov.Fn fpu_kern_thread "u_int flags" 4356388ef9SKonstantin Belousov.Ft int 4456388ef9SKonstantin Belousov.Fn is_fpu_kern_thread "u_int flags" 4556388ef9SKonstantin Belousov.Sh DESCRIPTION 4656388ef9SKonstantin BelousovThe 4756388ef9SKonstantin Belousov.Nm 4856388ef9SKonstantin Belousovfamily of functions allows the use of FPU hardware in kernel code. 4956388ef9SKonstantin BelousovModern FPUs are not limited to providing hardware implementation for 50*04d4c8e4SChristian Bruefferfloating point arithmetic; they offer advanced accelerators for cryptography 5156388ef9SKonstantin Belousovand other computational-intensive algorithms. 5256388ef9SKonstantin BelousovThese facilities share registers with the FPU hardware. 5356388ef9SKonstantin Belousov.Pp 54*04d4c8e4SChristian BruefferTypical kernel code does not need access to the FPU. 5556388ef9SKonstantin BelousovSaving a large register file on each entry to the kernel would waste 5656388ef9SKonstantin Belousovtime. 5756388ef9SKonstantin BelousovWhen kernel code uses the FPU, the current FPU state must be saved to 5856388ef9SKonstantin Belousovavoid corrupting the user-mode state, and vice versa. 5956388ef9SKonstantin Belousov.Pp 6056388ef9SKonstantin BelousovThe management of the save and restore is automatic. 6156388ef9SKonstantin BelousovThe processor catches accesses to the FPU registers 6256388ef9SKonstantin Belousovwhen the non-current context tries to access them. 6356388ef9SKonstantin BelousovExplicit calls are required for the allocation of the save area and 6456388ef9SKonstantin Belousovthe notification of the start and end of the code using the FPU. 6556388ef9SKonstantin Belousov.Pp 6656388ef9SKonstantin BelousovThe 6756388ef9SKonstantin Belousov.Fn fpu_kern_alloc_ctx 6856388ef9SKonstantin Belousovfunction allocates the memory used by 6956388ef9SKonstantin Belousov.Nm 7056388ef9SKonstantin Belousovto track the use of the FPU hardware state and the related software state. 7156388ef9SKonstantin BelousovThe 7256388ef9SKonstantin Belousov.Fn fpu_kern_alloc_ctx 7356388ef9SKonstantin Belousovfunction requires the 7456388ef9SKonstantin Belousov.Fa flags 7556388ef9SKonstantin Belousovargument, which currently accepts the following flags: 7656388ef9SKonstantin Belousov.Bl -tag -width ".Dv FPU_KERN_NOWAIT" -offset indent 7756388ef9SKonstantin Belousov.It Dv FPU_KERN_NOWAIT 7856388ef9SKonstantin BelousovDo not wait for the available memory if the request could not be satisfied 7956388ef9SKonstantin Belousovwithout sleep. 8056388ef9SKonstantin Belousov.It 0 8156388ef9SKonstantin BelousovNo special handling is required. 8256388ef9SKonstantin Belousov.El 83*04d4c8e4SChristian Brueffer.Pp 8456388ef9SKonstantin BelousovThe function returns the allocated context area, or 8556388ef9SKonstantin Belousov.Va NULL 8656388ef9SKonstantin Belousovif the allocation failed. 8756388ef9SKonstantin Belousov.Pp 8856388ef9SKonstantin BelousovThe 8956388ef9SKonstantin Belousov.Fn fpu_kern_free_ctx 9056388ef9SKonstantin Belousovfunction frees the context previously allocated by 9156388ef9SKonstantin Belousov.Fn fpu_kern_alloc_ctx . 9256388ef9SKonstantin Belousov.Pp 9356388ef9SKonstantin BelousovThe 9456388ef9SKonstantin Belousov.Fn fpu_kern_enter 9556388ef9SKonstantin Belousovfunction designates the start of the region of kernel code where the 9656388ef9SKonstantin Belousovuse of the FPU is allowed. 9756388ef9SKonstantin BelousovIts arguments are: 9856388ef9SKonstantin Belousov.Bl -tag -width ".Fa ctx" -offset indent 9956388ef9SKonstantin Belousov.It Fa td 10056388ef9SKonstantin BelousovCurrently must be 10156388ef9SKonstantin Belousov.Va curthread . 10256388ef9SKonstantin Belousov.It Fa ctx 10356388ef9SKonstantin BelousovThe context save area previously allocated by 10456388ef9SKonstantin Belousov.Fn fpu_kern_alloc_ctx 10556388ef9SKonstantin Belousovand not currently in use by another call to 10656388ef9SKonstantin Belousov.Fn fpu_kern_enter . 10756388ef9SKonstantin Belousov.It Fa flags 10856388ef9SKonstantin BelousovThis argument currently accepts the following flags: 10956388ef9SKonstantin Belousov.Bl -tag -width ".Dv FPU_KERN_NORMAL" -offset indent 11056388ef9SKonstantin Belousov.It Dv FPU_KERN_NORMAL 11156388ef9SKonstantin BelousovIndicates that the caller intends to access the full FPU state. 11256388ef9SKonstantin BelousovMust be specified currently. 11356388ef9SKonstantin Belousov.It Dv FPU_KERN_KTHR 11456388ef9SKonstantin BelousovIndicates that no saving of the current FPU state should be performed, 11556388ef9SKonstantin Belousovif the thread called 11656388ef9SKonstantin Belousov.Xr fpu_kern_thread 9 11756388ef9SKonstantin Belousovfunction. 11856388ef9SKonstantin BelousovThis is intended to minimize code duplication in callers which 11956388ef9SKonstantin Belousovcould be used from both kernel thread and syscall contexts. 12056388ef9SKonstantin BelousovThe 12156388ef9SKonstantin Belousov.Fn fpu_kern_leave 12256388ef9SKonstantin Belousovfunction correctly handles such contexts. 12356388ef9SKonstantin Belousov.El 12456388ef9SKonstantin Belousov.El 125*04d4c8e4SChristian Brueffer.Pp 12656388ef9SKonstantin BelousovThe function does not sleep or block. 12756388ef9SKonstantin BelousovIt could cause the 12856388ef9SKonstantin Belousov.Nm Device Not Available 12956388ef9SKonstantin Belousovexception during execution, and on the first FPU access after the 13056388ef9SKonstantin Belousovfunction returns, as well as after each context switch 13156388ef9SKonstantin Belousov(see Intel Software Developer Manual for the reference). 13256388ef9SKonstantin BelousovCurrently, no errors are defined which can be returned by 13356388ef9SKonstantin Belousov.Fn fpu_kern_enter 13456388ef9SKonstantin Belousovto the caller. 13556388ef9SKonstantin Belousov.Pp 13656388ef9SKonstantin BelousovThe 13756388ef9SKonstantin Belousov.Fn fpu_kern_leave 13856388ef9SKonstantin Belousovfunction ends the region started by 13956388ef9SKonstantin Belousov.Fn fpu_kern_enter . 14056388ef9SKonstantin BelousovThe uses of FPU in the kernel after the call to 14156388ef9SKonstantin Belousov.Fn fpu_kern_leave 14256388ef9SKonstantin Belousovare erronous until the next call to 14356388ef9SKonstantin Belousov.Fn fpu_kern_enter 14456388ef9SKonstantin Belousovis performed. 14556388ef9SKonstantin BelousovThe function takes the 14656388ef9SKonstantin Belousov.Fa td 14756388ef9SKonstantin Belousovthread argument, which currently must be 14856388ef9SKonstantin Belousov.Va curthread , 14956388ef9SKonstantin Belousovand the 15056388ef9SKonstantin Belousov.Fa ctx 15156388ef9SKonstantin Belousovcontext pointer, previously passed to 15256388ef9SKonstantin Belousov.Fn fpu_kern_enter . 15356388ef9SKonstantin BelousovAfter the function returns, the context may be freed or reused 15456388ef9SKonstantin Belousovby other invocation of 15556388ef9SKonstantin Belousov.Fn fpu_kern_enter . 15656388ef9SKonstantin BelousovThere are no errors defined for the function, it always returns 0. 15756388ef9SKonstantin Belousov.Pp 15856388ef9SKonstantin BelousovThe 15956388ef9SKonstantin Belousov.Fn fpu_kern_thread 16056388ef9SKonstantin Belousovfunction provides an optimization for threads which never leave to 16156388ef9SKonstantin Belousovthe usermode. 16256388ef9SKonstantin BelousovSuch thread can reuse the usermode save area for the FPU state, 16356388ef9SKonstantin Belousovwhich is allowed by the function call. 16456388ef9SKonstantin BelousovThere is no flags defined for the function, and no error states 16556388ef9SKonstantin Belousovthat the function returns. 16656388ef9SKonstantin Belousov.Pp 16756388ef9SKonstantin BelousovThe 16856388ef9SKonstantin Belousov.Fn is_fpu_kern_thread 16956388ef9SKonstantin Belousovfunction returns the boolean indicating whether the current thread 17056388ef9SKonstantin Belousoventered the mode enabled by 17156388ef9SKonstantin Belousov.Fn fpu_kern_thread . 17256388ef9SKonstantin BelousovThere is currently no flags defined for the function, the return 17356388ef9SKonstantin Belousovvalue is true if the current thread have the permanent FPU save area, 17456388ef9SKonstantin Belousovand false otherwise. 17556388ef9SKonstantin Belousov.Sh NOTES 17656388ef9SKonstantin BelousovThe 17756388ef9SKonstantin Belousov.Nm 178*04d4c8e4SChristian Bruefferis currently implemented only for the i386 and amd64 architectures. 17956388ef9SKonstantin Belousov.Pp 18056388ef9SKonstantin BelousovThere is no way to handle floating point exceptions raised from 18156388ef9SKonstantin Belousovkernel mode. 18256388ef9SKonstantin Belousov.Pp 18356388ef9SKonstantin BelousovThe unused 18456388ef9SKonstantin Belousov.Fa flags 18556388ef9SKonstantin Belousovarguments 18656388ef9SKonstantin Belousovto the 18756388ef9SKonstantin Belousov.Nm 18856388ef9SKonstantin Belousovfunctions are to be extended to allow specification of the 18956388ef9SKonstantin Belousovset of the FPU hardware state used by the code region. 19056388ef9SKonstantin BelousovThis would allow optimizations of saving and restoring the state. 19156388ef9SKonstantin Belousov.Sh AUTHORS 19256388ef9SKonstantin BelousovThe 19356388ef9SKonstantin Belousov.Nm 19456388ef9SKonstantin Belousovfacitily and this manual page were written by 19556388ef9SKonstantin Belousov.An Konstantin Belousov Aq Mt kib@FreeBSD.org . 196