xref: /freebsd/share/man/man9/fpu_kern.9 (revision 56388ef94b15b616a3620caca41b2356077ec0fe)
1*56388ef9SKonstantin Belousov.\" Copyright (c) 2014
2*56388ef9SKonstantin Belousov.\"	Konstantin Belousov <kib@FreeBSD.org>.  All rights reserved.
3*56388ef9SKonstantin Belousov.\"
4*56388ef9SKonstantin Belousov.\" Redistribution and use in source and binary forms, with or without
5*56388ef9SKonstantin Belousov.\" modification, are permitted provided that the following conditions
6*56388ef9SKonstantin Belousov.\" are met:
7*56388ef9SKonstantin Belousov.\" 1. Redistributions of source code must retain the above copyright
8*56388ef9SKonstantin Belousov.\"    notice, this list of conditions and the following disclaimer.
9*56388ef9SKonstantin Belousov.\" 2. Redistributions in binary form must reproduce the above copyright
10*56388ef9SKonstantin Belousov.\"    notice, this list of conditions and the following disclaimer in the
11*56388ef9SKonstantin Belousov.\"    documentation and/or other materials provided with the distribution.
12*56388ef9SKonstantin Belousov.\"
13*56388ef9SKonstantin Belousov.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14*56388ef9SKonstantin Belousov.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15*56388ef9SKonstantin Belousov.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16*56388ef9SKonstantin Belousov.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17*56388ef9SKonstantin Belousov.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18*56388ef9SKonstantin Belousov.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19*56388ef9SKonstantin Belousov.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20*56388ef9SKonstantin Belousov.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21*56388ef9SKonstantin Belousov.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22*56388ef9SKonstantin Belousov.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23*56388ef9SKonstantin Belousov.\"
24*56388ef9SKonstantin Belousov.\" $FreeBSD$
25*56388ef9SKonstantin Belousov.\"
26*56388ef9SKonstantin Belousov.Dd June 21, 2014
27*56388ef9SKonstantin Belousov.Dt KERN_FPU 9
28*56388ef9SKonstantin Belousov.Os
29*56388ef9SKonstantin Belousov.Sh NAME
30*56388ef9SKonstantin Belousov.Nm fpu_kern
31*56388ef9SKonstantin Belousov.Nd "facility to use the FPU in the kernel"
32*56388ef9SKonstantin Belousov.Sh SYNOPSIS
33*56388ef9SKonstantin Belousov.Ft struct fpu_kern_ctx *
34*56388ef9SKonstantin Belousov.Fn fpu_kern_alloc_ctx "u_int flags"
35*56388ef9SKonstantin Belousov.Ft void
36*56388ef9SKonstantin Belousov.Fn fpu_kern_free_ctx "struct fpu_kern_ctx *ctx"
37*56388ef9SKonstantin Belousov.Ft int
38*56388ef9SKonstantin Belousov.Fn fpu_kern_enter "struct thread *td" "struct fpu_kern_ctx *ctx" "u_int flags"
39*56388ef9SKonstantin Belousov.Ft int
40*56388ef9SKonstantin Belousov.Fn fpu_kern_leave "struct thread *td" "struct fpu_kern_ctx *ctx"
41*56388ef9SKonstantin Belousov.Ft int
42*56388ef9SKonstantin Belousov.Fn fpu_kern_thread "u_int flags"
43*56388ef9SKonstantin Belousov.Ft int
44*56388ef9SKonstantin Belousov.Fn is_fpu_kern_thread "u_int flags"
45*56388ef9SKonstantin Belousov.Sh DESCRIPTION
46*56388ef9SKonstantin BelousovThe
47*56388ef9SKonstantin Belousov.Nm
48*56388ef9SKonstantin Belousovfamily of functions allows the use of FPU hardware in kernel code.
49*56388ef9SKonstantin BelousovModern FPUs are not limited to providing hardware implementation for
50*56388ef9SKonstantin Belousovfloating point arithmetic, they offer advanced accelerators for cryptography
51*56388ef9SKonstantin Belousovand other computational-intensive algorithms.
52*56388ef9SKonstantin BelousovThese facilities share registers with the FPU hardware.
53*56388ef9SKonstantin Belousov.Pp
54*56388ef9SKonstantin BelousovTypical kernel code does not need to access to the FPU.
55*56388ef9SKonstantin BelousovSaving a large register file on each entry to the kernel would waste
56*56388ef9SKonstantin Belousovtime.
57*56388ef9SKonstantin BelousovWhen kernel code uses the FPU, the current FPU state must be saved to
58*56388ef9SKonstantin Belousovavoid corrupting the user-mode state, and vice versa.
59*56388ef9SKonstantin Belousov.Pp
60*56388ef9SKonstantin BelousovThe management of the save and restore is automatic.
61*56388ef9SKonstantin BelousovThe processor catches accesses to the FPU registers
62*56388ef9SKonstantin Belousovwhen the non-current context tries to access them.
63*56388ef9SKonstantin BelousovExplicit calls are required for the allocation of the save area and
64*56388ef9SKonstantin Belousovthe notification of the start and end of the code using the FPU.
65*56388ef9SKonstantin Belousov.Pp
66*56388ef9SKonstantin BelousovThe
67*56388ef9SKonstantin Belousov.Fn fpu_kern_alloc_ctx
68*56388ef9SKonstantin Belousovfunction allocates the memory used by
69*56388ef9SKonstantin Belousov.Nm
70*56388ef9SKonstantin Belousovto track the use of the FPU hardware state and the related software state.
71*56388ef9SKonstantin BelousovThe
72*56388ef9SKonstantin Belousov.Fn fpu_kern_alloc_ctx
73*56388ef9SKonstantin Belousovfunction requires the
74*56388ef9SKonstantin Belousov.Fa flags
75*56388ef9SKonstantin Belousovargument, which currently accepts the following flags:
76*56388ef9SKonstantin Belousov.Bl -tag -width ".Dv FPU_KERN_NOWAIT" -offset indent
77*56388ef9SKonstantin Belousov.It Dv FPU_KERN_NOWAIT
78*56388ef9SKonstantin BelousovDo not wait for the available memory if the request could not be satisfied
79*56388ef9SKonstantin Belousovwithout sleep.
80*56388ef9SKonstantin Belousov.It 0
81*56388ef9SKonstantin BelousovNo special handling is required.
82*56388ef9SKonstantin Belousov.El
83*56388ef9SKonstantin BelousovThe function returns the allocated context area, or
84*56388ef9SKonstantin Belousov.Va NULL
85*56388ef9SKonstantin Belousovif the allocation failed.
86*56388ef9SKonstantin Belousov.Pp
87*56388ef9SKonstantin BelousovThe
88*56388ef9SKonstantin Belousov.Fn fpu_kern_free_ctx
89*56388ef9SKonstantin Belousovfunction frees the context previously allocated by
90*56388ef9SKonstantin Belousov.Fn fpu_kern_alloc_ctx .
91*56388ef9SKonstantin Belousov.Pp
92*56388ef9SKonstantin BelousovThe
93*56388ef9SKonstantin Belousov.Fn fpu_kern_enter
94*56388ef9SKonstantin Belousovfunction designates the start of the region of kernel code where the
95*56388ef9SKonstantin Belousovuse of the FPU is allowed.
96*56388ef9SKonstantin BelousovIts arguments are:
97*56388ef9SKonstantin Belousov.Bl -tag -width ".Fa ctx" -offset indent
98*56388ef9SKonstantin Belousov.It Fa td
99*56388ef9SKonstantin BelousovCurrently must be
100*56388ef9SKonstantin Belousov.Va curthread .
101*56388ef9SKonstantin Belousov.It Fa ctx
102*56388ef9SKonstantin BelousovThe context save area previously allocated by
103*56388ef9SKonstantin Belousov.Fn fpu_kern_alloc_ctx
104*56388ef9SKonstantin Belousovand not currently in use by another call to
105*56388ef9SKonstantin Belousov.Fn fpu_kern_enter .
106*56388ef9SKonstantin Belousov.It Fa flags
107*56388ef9SKonstantin BelousovThis argument currently accepts the following flags:
108*56388ef9SKonstantin Belousov.Bl -tag -width ".Dv FPU_KERN_NORMAL" -offset indent
109*56388ef9SKonstantin Belousov.It Dv FPU_KERN_NORMAL
110*56388ef9SKonstantin BelousovIndicates that the caller intends to access the full FPU state.
111*56388ef9SKonstantin BelousovMust be specified currently.
112*56388ef9SKonstantin Belousov.It Dv FPU_KERN_KTHR
113*56388ef9SKonstantin BelousovIndicates that no saving of the current FPU state should be performed,
114*56388ef9SKonstantin Belousovif the thread called
115*56388ef9SKonstantin Belousov.Xr fpu_kern_thread 9
116*56388ef9SKonstantin Belousovfunction.
117*56388ef9SKonstantin BelousovThis is intended to minimize code duplication in callers which
118*56388ef9SKonstantin Belousovcould be used from both kernel thread and syscall contexts.
119*56388ef9SKonstantin BelousovThe
120*56388ef9SKonstantin Belousov.Fn fpu_kern_leave
121*56388ef9SKonstantin Belousovfunction correctly handles such contexts.
122*56388ef9SKonstantin Belousov.El
123*56388ef9SKonstantin Belousov.El
124*56388ef9SKonstantin BelousovThe function does not sleep or block.
125*56388ef9SKonstantin BelousovIt could cause the
126*56388ef9SKonstantin Belousov.Nm Device Not Available
127*56388ef9SKonstantin Belousovexception during execution, and on the first FPU access after the
128*56388ef9SKonstantin Belousovfunction returns, as well as after each context switch
129*56388ef9SKonstantin Belousov(see Intel Software Developer Manual for the reference).
130*56388ef9SKonstantin BelousovCurrently, no errors are defined which can be returned by
131*56388ef9SKonstantin Belousov.Fn fpu_kern_enter
132*56388ef9SKonstantin Belousovto the caller.
133*56388ef9SKonstantin Belousov.Pp
134*56388ef9SKonstantin BelousovThe
135*56388ef9SKonstantin Belousov.Fn fpu_kern_leave
136*56388ef9SKonstantin Belousovfunction ends the region started by
137*56388ef9SKonstantin Belousov.Fn fpu_kern_enter .
138*56388ef9SKonstantin BelousovThe uses of FPU in the kernel after the call to
139*56388ef9SKonstantin Belousov.Fn fpu_kern_leave
140*56388ef9SKonstantin Belousovare erronous until the next call to
141*56388ef9SKonstantin Belousov.Fn fpu_kern_enter
142*56388ef9SKonstantin Belousovis performed.
143*56388ef9SKonstantin BelousovThe function takes the
144*56388ef9SKonstantin Belousov.Fa td
145*56388ef9SKonstantin Belousovthread argument, which currently must be
146*56388ef9SKonstantin Belousov.Va curthread ,
147*56388ef9SKonstantin Belousovand the
148*56388ef9SKonstantin Belousov.Fa ctx
149*56388ef9SKonstantin Belousovcontext pointer, previously passed to
150*56388ef9SKonstantin Belousov.Fn fpu_kern_enter .
151*56388ef9SKonstantin BelousovAfter the function returns, the context may be freed or reused
152*56388ef9SKonstantin Belousovby other invocation of
153*56388ef9SKonstantin Belousov.Fn fpu_kern_enter .
154*56388ef9SKonstantin BelousovThere are no errors defined for the function, it always returns 0.
155*56388ef9SKonstantin Belousov.Pp
156*56388ef9SKonstantin BelousovThe
157*56388ef9SKonstantin Belousov.Fn fpu_kern_thread
158*56388ef9SKonstantin Belousovfunction provides an optimization for threads which never leave to
159*56388ef9SKonstantin Belousovthe usermode.
160*56388ef9SKonstantin BelousovSuch thread can reuse the usermode save area for the FPU state,
161*56388ef9SKonstantin Belousovwhich is allowed by the function call.
162*56388ef9SKonstantin BelousovThere is no flags defined for the function, and no error states
163*56388ef9SKonstantin Belousovthat the function returns.
164*56388ef9SKonstantin Belousov.Pp
165*56388ef9SKonstantin BelousovThe
166*56388ef9SKonstantin Belousov.Fn is_fpu_kern_thread
167*56388ef9SKonstantin Belousovfunction returns the boolean indicating whether the current thread
168*56388ef9SKonstantin Belousoventered the mode enabled by
169*56388ef9SKonstantin Belousov.Fn fpu_kern_thread .
170*56388ef9SKonstantin BelousovThere is currently no flags defined for the function, the return
171*56388ef9SKonstantin Belousovvalue is true if the current thread have the permanent FPU save area,
172*56388ef9SKonstantin Belousovand false otherwise.
173*56388ef9SKonstantin Belousov.Sh NOTES
174*56388ef9SKonstantin BelousovThe
175*56388ef9SKonstantin Belousov.Nm
176*56388ef9SKonstantin Belousovis currently implemented only for i386 and amd64 architectures.
177*56388ef9SKonstantin Belousov.Pp
178*56388ef9SKonstantin BelousovThere is no way to handle floating point exceptions raised from
179*56388ef9SKonstantin Belousovkernel mode.
180*56388ef9SKonstantin Belousov.Pp
181*56388ef9SKonstantin BelousovThe unused
182*56388ef9SKonstantin Belousov.Fa flags
183*56388ef9SKonstantin Belousovarguments
184*56388ef9SKonstantin Belousovto the
185*56388ef9SKonstantin Belousov.Nm
186*56388ef9SKonstantin Belousovfunctions are to be extended to allow specification of the
187*56388ef9SKonstantin Belousovset of the FPU hardware state used by the code region.
188*56388ef9SKonstantin BelousovThis would allow optimizations of saving and restoring the state.
189*56388ef9SKonstantin Belousov.Sh AUTHORS
190*56388ef9SKonstantin BelousovThe
191*56388ef9SKonstantin Belousov.Nm
192*56388ef9SKonstantin Belousovfacitily and this manual page were written by
193*56388ef9SKonstantin Belousov.An Konstantin Belousov Aq Mt kib@FreeBSD.org .
194