15b81b6b3SRodney W. Grimes /*-
251369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni *
45b81b6b3SRodney W. Grimes * Copyright (c) 1990 The Regents of the University of California.
55b81b6b3SRodney W. Grimes * All rights reserved.
65b81b6b3SRodney W. Grimes *
75b81b6b3SRodney W. Grimes * Redistribution and use in source and binary forms, with or without
85b81b6b3SRodney W. Grimes * modification, are permitted provided that the following conditions
95b81b6b3SRodney W. Grimes * are met:
105b81b6b3SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
115b81b6b3SRodney W. Grimes * notice, this list of conditions and the following disclaimer.
125b81b6b3SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
135b81b6b3SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
145b81b6b3SRodney W. Grimes * documentation and/or other materials provided with the distribution.
15846351f8SWarner Losh * 3. Neither the name of the University nor the names of its contributors
165b81b6b3SRodney W. Grimes * may be used to endorse or promote products derived from this software
175b81b6b3SRodney W. Grimes * without specific prior written permission.
185b81b6b3SRodney W. Grimes *
195b81b6b3SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
205b81b6b3SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
215b81b6b3SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
225b81b6b3SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
235b81b6b3SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
245b81b6b3SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
255b81b6b3SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
265b81b6b3SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
275b81b6b3SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
285b81b6b3SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
295b81b6b3SRodney W. Grimes * SUCH DAMAGE.
305b81b6b3SRodney W. Grimes */
315b81b6b3SRodney W. Grimes
329676a785SDavid E. O'Brien #include <sys/cdefs.h>
3324c1c3bfSJonathan Anderson #include "opt_capsicum.h"
34b40ce416SJulian Elischer #include "opt_kstack_pages.h"
3518282c47SMark Johnston #include "opt_ktrace.h"
36268bdb43SPeter Wemm
37f540b106SGarrett Wollman #include <sys/param.h>
384a144410SRobert Watson #include <sys/capsicum.h>
39f540b106SGarrett Wollman #include <sys/systm.h>
4018282c47SMark Johnston #include <sys/ktrace.h>
41fb919e4dSMark Murray #include <sys/lock.h>
4291c28bfdSLuoqi Chen #include <sys/malloc.h>
43fb919e4dSMark Murray #include <sys/mutex.h>
44acd3428bSRobert Watson #include <sys/priv.h>
45f540b106SGarrett Wollman #include <sys/proc.h>
466caa8a15SJohn Baldwin #include <sys/smp.h>
47fb919e4dSMark Murray #include <sys/sysproto.h>
48efeaf95aSDavid Greenman
49efeaf95aSDavid Greenman #include <vm/vm.h>
50efeaf95aSDavid Greenman #include <vm/pmap.h>
51efeaf95aSDavid Greenman #include <vm/vm_map.h>
52efeaf95aSDavid Greenman #include <vm/vm_extern.h>
53efeaf95aSDavid Greenman
543cabd93eSKonstantin Belousov #include <machine/atomic.h>
55f540b106SGarrett Wollman #include <machine/cpu.h>
566004362eSDavid Schultz #include <machine/pcb.h>
576004362eSDavid Schultz #include <machine/pcb_ext.h>
5824db0459SJohn Baldwin #include <machine/proc.h>
59f540b106SGarrett Wollman #include <machine/sysarch.h>
60812a11a2SDavid Greenman
6198bf5a70SRobert Watson #include <security/audit/audit.h>
6298bf5a70SRobert Watson
63f540b106SGarrett Wollman #include <vm/vm_kern.h> /* for kernel_map */
64da59a31cSDavid Greenman
650dbf6d73SJordan K. Hubbard #define MAX_LD 8192
660dbf6d73SJordan K. Hubbard #define LD_PER_PAGE 512
67d9c9c81cSPedro F. Giffuni #define NEW_MAX_LD(num) rounddown2(num + LD_PER_PAGE, LD_PER_PAGE)
680dbf6d73SJordan K. Hubbard #define SIZE_FROM_LARGEST_LD(num) (NEW_MAX_LD(num) << 3)
6905dfa22fSAttilio Rao #define NULL_LDT_BASE ((caddr_t)NULL)
700dbf6d73SJordan K. Hubbard
71ea11c140SAttilio Rao #ifdef SMP
7213a5d911SKonstantin Belousov static void set_user_ldt_rv(void *arg);
7305dfa22fSAttilio Rao #endif
74dae8d52dSDavid Xu static int i386_set_ldt_data(struct thread *, int start, int num,
75dae8d52dSDavid Xu union descriptor *descs);
76dae8d52dSDavid Xu static int i386_ldt_grow(struct thread *td, int len);
775b81b6b3SRodney W. Grimes
7806d058bdSKonstantin Belousov void
fill_based_sd(struct segment_descriptor * sdp,uint32_t base)7906d058bdSKonstantin Belousov fill_based_sd(struct segment_descriptor *sdp, uint32_t base)
8006d058bdSKonstantin Belousov {
8106d058bdSKonstantin Belousov
8206d058bdSKonstantin Belousov sdp->sd_lobase = base & 0xffffff;
8306d058bdSKonstantin Belousov sdp->sd_hibase = (base >> 24) & 0xff;
8406d058bdSKonstantin Belousov sdp->sd_lolimit = 0xffff; /* 4GB limit, wraps around */
8506d058bdSKonstantin Belousov sdp->sd_hilimit = 0xf;
8606d058bdSKonstantin Belousov sdp->sd_type = SDT_MEMRWA;
8706d058bdSKonstantin Belousov sdp->sd_dpl = SEL_UPL;
8806d058bdSKonstantin Belousov sdp->sd_p = 1;
8906d058bdSKonstantin Belousov sdp->sd_xx = 0;
9006d058bdSKonstantin Belousov sdp->sd_def32 = 1;
9106d058bdSKonstantin Belousov sdp->sd_gran = 1;
9206d058bdSKonstantin Belousov }
9306d058bdSKonstantin Belousov
94787f835bSKonstantin Belousov /*
95787f835bSKonstantin Belousov * Construct special descriptors for "base" selectors. Store them in
96787f835bSKonstantin Belousov * the PCB for later use by cpu_switch(). Store them in the GDT for
97787f835bSKonstantin Belousov * more immediate use. The GDT entries are part of the current
98787f835bSKonstantin Belousov * context. Callers must load related segment registers to complete
99787f835bSKonstantin Belousov * setting up the current context.
100787f835bSKonstantin Belousov */
101787f835bSKonstantin Belousov void
set_fsbase(struct thread * td,uint32_t base)102787f835bSKonstantin Belousov set_fsbase(struct thread *td, uint32_t base)
103787f835bSKonstantin Belousov {
104787f835bSKonstantin Belousov struct segment_descriptor sd;
105787f835bSKonstantin Belousov
106787f835bSKonstantin Belousov fill_based_sd(&sd, base);
107787f835bSKonstantin Belousov critical_enter();
108787f835bSKonstantin Belousov td->td_pcb->pcb_fsd = sd;
109c7b02135SJohn Baldwin if (td == curthread)
110787f835bSKonstantin Belousov PCPU_GET(fsgs_gdt)[0] = sd;
111787f835bSKonstantin Belousov critical_exit();
112787f835bSKonstantin Belousov }
113787f835bSKonstantin Belousov
114787f835bSKonstantin Belousov void
set_gsbase(struct thread * td,uint32_t base)115787f835bSKonstantin Belousov set_gsbase(struct thread *td, uint32_t base)
116787f835bSKonstantin Belousov {
117787f835bSKonstantin Belousov struct segment_descriptor sd;
118787f835bSKonstantin Belousov
119787f835bSKonstantin Belousov fill_based_sd(&sd, base);
120787f835bSKonstantin Belousov critical_enter();
121787f835bSKonstantin Belousov td->td_pcb->pcb_gsd = sd;
122c7b02135SJohn Baldwin if (td == curthread)
123787f835bSKonstantin Belousov PCPU_GET(fsgs_gdt)[1] = sd;
124787f835bSKonstantin Belousov critical_exit();
125787f835bSKonstantin Belousov }
126787f835bSKonstantin Belousov
1272f1ba63bSBruce Evans #ifndef _SYS_SYSPROTO_H_
128812a11a2SDavid Greenman struct sysarch_args {
129812a11a2SDavid Greenman int op;
130812a11a2SDavid Greenman char *parms;
13101ae5b20SDavid Greenman };
1322f1ba63bSBruce Evans #endif
13301ae5b20SDavid Greenman
134812a11a2SDavid Greenman int
sysarch(struct thread * td,struct sysarch_args * uap)1353e85b721SEd Maste sysarch(struct thread *td, struct sysarch_args *uap)
1365b81b6b3SRodney W. Grimes {
1377ff022c4SJohn Baldwin int error;
13884569dffSMaxim Sobolev union descriptor *lp;
13984569dffSMaxim Sobolev union {
14084569dffSMaxim Sobolev struct i386_ldt_args largs;
14184569dffSMaxim Sobolev struct i386_ioperm_args iargs;
142824fc460SJohn Baldwin struct i386_get_xfpustate xfpu;
14384569dffSMaxim Sobolev } kargs;
144e0ab2c6dSPeter Wemm uint32_t base;
145787f835bSKonstantin Belousov struct segment_descriptor *sdp;
14684569dffSMaxim Sobolev
14714961ba7SRobert Watson AUDIT_ARG_CMD(uap->op);
14874b5505eSRobert Watson
14924c1c3bfSJonathan Anderson #ifdef CAPABILITY_MODE
15074b5505eSRobert Watson /*
15112bc222eSJonathan Anderson * When adding new operations, add a new case statement here to
15212bc222eSJonathan Anderson * explicitly indicate whether or not the operation is safe to
15312bc222eSJonathan Anderson * perform in capability mode.
15474b5505eSRobert Watson */
15574b5505eSRobert Watson switch (uap->op) {
15674b5505eSRobert Watson case I386_GET_LDT:
15774b5505eSRobert Watson case I386_SET_LDT:
15874b5505eSRobert Watson case I386_GET_IOPERM:
15974b5505eSRobert Watson case I386_GET_FSBASE:
16074b5505eSRobert Watson case I386_SET_FSBASE:
16174b5505eSRobert Watson case I386_GET_GSBASE:
16274b5505eSRobert Watson case I386_SET_GSBASE:
163824fc460SJohn Baldwin case I386_GET_XFPUSTATE:
16474b5505eSRobert Watson break;
16574b5505eSRobert Watson
16674b5505eSRobert Watson case I386_SET_IOPERM:
16774b5505eSRobert Watson default:
16805296a0fSJake Freeland if (CAP_TRACING(td))
16905296a0fSJake Freeland ktrcapfail(CAPFAIL_SYSCALL, &uap->op);
17005296a0fSJake Freeland if (IN_CAPABILITY_MODE(td))
17174b5505eSRobert Watson return (ECAPMODE);
17205296a0fSJake Freeland break;
17374b5505eSRobert Watson }
17474b5505eSRobert Watson #endif
17574b5505eSRobert Watson
17684569dffSMaxim Sobolev switch (uap->op) {
17784569dffSMaxim Sobolev case I386_GET_IOPERM:
17884569dffSMaxim Sobolev case I386_SET_IOPERM:
17984569dffSMaxim Sobolev if ((error = copyin(uap->parms, &kargs.iargs,
18084569dffSMaxim Sobolev sizeof(struct i386_ioperm_args))) != 0)
18184569dffSMaxim Sobolev return (error);
18284569dffSMaxim Sobolev break;
18384569dffSMaxim Sobolev case I386_GET_LDT:
18484569dffSMaxim Sobolev case I386_SET_LDT:
18584569dffSMaxim Sobolev if ((error = copyin(uap->parms, &kargs.largs,
18684569dffSMaxim Sobolev sizeof(struct i386_ldt_args))) != 0)
18784569dffSMaxim Sobolev return (error);
18884569dffSMaxim Sobolev break;
189824fc460SJohn Baldwin case I386_GET_XFPUSTATE:
190824fc460SJohn Baldwin if ((error = copyin(uap->parms, &kargs.xfpu,
191824fc460SJohn Baldwin sizeof(struct i386_get_xfpustate))) != 0)
192824fc460SJohn Baldwin return (error);
193824fc460SJohn Baldwin break;
19484569dffSMaxim Sobolev default:
19584569dffSMaxim Sobolev break;
19684569dffSMaxim Sobolev }
1975b81b6b3SRodney W. Grimes
198812a11a2SDavid Greenman switch (uap->op) {
199812a11a2SDavid Greenman case I386_GET_LDT:
20084569dffSMaxim Sobolev error = i386_get_ldt(td, &kargs.largs);
2015b81b6b3SRodney W. Grimes break;
202812a11a2SDavid Greenman case I386_SET_LDT:
20384569dffSMaxim Sobolev if (kargs.largs.descs != NULL) {
204a4730cdbSKonstantin Belousov if (kargs.largs.num > MAX_LD)
205a4730cdbSKonstantin Belousov return (EINVAL);
20657f99aedSKonstantin Belousov lp = malloc(kargs.largs.num * sizeof(union descriptor),
20731241fa7SKonstantin Belousov M_TEMP, M_WAITOK);
20884569dffSMaxim Sobolev error = copyin(kargs.largs.descs, lp,
20984569dffSMaxim Sobolev kargs.largs.num * sizeof(union descriptor));
21084569dffSMaxim Sobolev if (error == 0)
21184569dffSMaxim Sobolev error = i386_set_ldt(td, &kargs.largs, lp);
21231241fa7SKonstantin Belousov free(lp, M_TEMP);
21384569dffSMaxim Sobolev } else {
21484569dffSMaxim Sobolev error = i386_set_ldt(td, &kargs.largs, NULL);
21584569dffSMaxim Sobolev }
2165b81b6b3SRodney W. Grimes break;
21748a09cf2SJohn Dyson case I386_GET_IOPERM:
21884569dffSMaxim Sobolev error = i386_get_ioperm(td, &kargs.iargs);
21984569dffSMaxim Sobolev if (error == 0)
22084569dffSMaxim Sobolev error = copyout(&kargs.iargs, uap->parms,
22184569dffSMaxim Sobolev sizeof(struct i386_ioperm_args));
22248a09cf2SJohn Dyson break;
22348a09cf2SJohn Dyson case I386_SET_IOPERM:
22484569dffSMaxim Sobolev error = i386_set_ioperm(td, &kargs.iargs);
22548a09cf2SJohn Dyson break;
22648a09cf2SJohn Dyson case I386_VM86:
227b40ce416SJulian Elischer error = vm86_sysarch(td, uap->parms);
22848a09cf2SJohn Dyson break;
229e0ab2c6dSPeter Wemm case I386_GET_FSBASE:
230e0ab2c6dSPeter Wemm sdp = &td->td_pcb->pcb_fsd;
231e0ab2c6dSPeter Wemm base = sdp->sd_hibase << 24 | sdp->sd_lobase;
232e0ab2c6dSPeter Wemm error = copyout(&base, uap->parms, sizeof(base));
233e0ab2c6dSPeter Wemm break;
234e0ab2c6dSPeter Wemm case I386_SET_FSBASE:
235e0ab2c6dSPeter Wemm error = copyin(uap->parms, &base, sizeof(base));
23606d058bdSKonstantin Belousov if (error == 0) {
237e0ab2c6dSPeter Wemm /*
238787f835bSKonstantin Belousov * Construct the special descriptor for fsbase
239787f835bSKonstantin Belousov * and arrange for doreti to load its selector
240787f835bSKonstantin Belousov * soon enough.
241e0ab2c6dSPeter Wemm */
242787f835bSKonstantin Belousov set_fsbase(td, base);
2439a045ca1SDavid Xu td->td_frame->tf_fs = GSEL(GUFS_SEL, SEL_UPL);
244e0ab2c6dSPeter Wemm }
245e0ab2c6dSPeter Wemm break;
246e0ab2c6dSPeter Wemm case I386_GET_GSBASE:
247e0ab2c6dSPeter Wemm sdp = &td->td_pcb->pcb_gsd;
248e0ab2c6dSPeter Wemm base = sdp->sd_hibase << 24 | sdp->sd_lobase;
249e0ab2c6dSPeter Wemm error = copyout(&base, uap->parms, sizeof(base));
250e0ab2c6dSPeter Wemm break;
251e0ab2c6dSPeter Wemm case I386_SET_GSBASE:
252e0ab2c6dSPeter Wemm error = copyin(uap->parms, &base, sizeof(base));
25306d058bdSKonstantin Belousov if (error == 0) {
254e0ab2c6dSPeter Wemm /*
255787f835bSKonstantin Belousov * Construct the special descriptor for gsbase.
256787f835bSKonstantin Belousov * The selector is loaded immediately, since we
257787f835bSKonstantin Belousov * normally only reload %gs on context switches.
258e0ab2c6dSPeter Wemm */
259787f835bSKonstantin Belousov set_gsbase(td, base);
260e0ab2c6dSPeter Wemm load_gs(GSEL(GUGS_SEL, SEL_UPL));
261e0ab2c6dSPeter Wemm }
262e0ab2c6dSPeter Wemm break;
263824fc460SJohn Baldwin case I386_GET_XFPUSTATE:
264824fc460SJohn Baldwin if (kargs.xfpu.len > cpu_max_ext_state_size -
265824fc460SJohn Baldwin sizeof(union savefpu))
266824fc460SJohn Baldwin return (EINVAL);
267824fc460SJohn Baldwin npxgetregs(td);
268824fc460SJohn Baldwin error = copyout((char *)(get_pcb_user_save_td(td) + 1),
269824fc460SJohn Baldwin kargs.xfpu.addr, kargs.xfpu.len);
270824fc460SJohn Baldwin break;
271812a11a2SDavid Greenman default:
2727ff022c4SJohn Baldwin error = EINVAL;
273812a11a2SDavid Greenman break;
274812a11a2SDavid Greenman }
275812a11a2SDavid Greenman return (error);
276812a11a2SDavid Greenman }
277da59a31cSDavid Greenman
27848a09cf2SJohn Dyson int
i386_extend_pcb(struct thread * td)279b40ce416SJulian Elischer i386_extend_pcb(struct thread *td)
28048a09cf2SJohn Dyson {
28148a09cf2SJohn Dyson int i, offset;
28248a09cf2SJohn Dyson u_long *addr;
28348a09cf2SJohn Dyson struct pcb_ext *ext;
28448a09cf2SJohn Dyson struct soft_segment_descriptor ssd = {
28548a09cf2SJohn Dyson 0, /* segment base address (overwritten) */
28648a09cf2SJohn Dyson ctob(IOPAGES + 1) - 1, /* length */
28748a09cf2SJohn Dyson SDT_SYS386TSS, /* segment type */
28848a09cf2SJohn Dyson 0, /* priority level */
28948a09cf2SJohn Dyson 1, /* descriptor present */
29048a09cf2SJohn Dyson 0, 0,
29148a09cf2SJohn Dyson 0, /* default 32 size */
29248a09cf2SJohn Dyson 0 /* granularity */
29348a09cf2SJohn Dyson };
29448a09cf2SJohn Dyson
295d86c1f0dSKonstantin Belousov ext = pmap_trm_alloc(ctob(IOPAGES + 1), M_WAITOK | M_ZERO);
296b40ce416SJulian Elischer /* -16 is so we can convert a trapframe into vm86trapframe inplace */
29748a09cf2SJohn Dyson ext->ext_tss.tss_ss0 = GSEL(GDATA_SEL, SEL_KPL);
29848a09cf2SJohn Dyson /*
29948a09cf2SJohn Dyson * The last byte of the i/o map must be followed by an 0xff byte.
30048a09cf2SJohn Dyson * We arbitrarily allocate 16 bytes here, to keep the starting
30148a09cf2SJohn Dyson * address on a doubleword boundary.
30248a09cf2SJohn Dyson */
30348a09cf2SJohn Dyson offset = PAGE_SIZE - 16;
30448a09cf2SJohn Dyson ext->ext_tss.tss_ioopt =
30548a09cf2SJohn Dyson (offset - ((unsigned)&ext->ext_tss - (unsigned)ext)) << 16;
30648a09cf2SJohn Dyson ext->ext_iomap = (caddr_t)ext + offset;
30748a09cf2SJohn Dyson ext->ext_vm86.vm86_intmap = (caddr_t)ext + offset - 32;
30848a09cf2SJohn Dyson
30948a09cf2SJohn Dyson addr = (u_long *)ext->ext_vm86.vm86_intmap;
31048a09cf2SJohn Dyson for (i = 0; i < (ctob(IOPAGES) + 32 + 16) / sizeof(u_long); i++)
31148a09cf2SJohn Dyson *addr++ = ~0;
31248a09cf2SJohn Dyson
31348a09cf2SJohn Dyson ssd.ssd_base = (unsigned)&ext->ext_tss;
31448a09cf2SJohn Dyson ssd.ssd_limit -= ((unsigned)&ext->ext_tss - (unsigned)ext);
31548a09cf2SJohn Dyson ssdtosd(&ssd, &ext->ext_tssd);
31648a09cf2SJohn Dyson
317f726a873SJohn Baldwin KASSERT(td == curthread, ("giving TSS to !curthread"));
318b40ce416SJulian Elischer KASSERT(td->td_pcb->pcb_ext == 0, ("already have a TSS!"));
319f726a873SJohn Baldwin
320f726a873SJohn Baldwin /* Switch to the new TSS. */
3211bfa9108SDavid Xu critical_enter();
322d86c1f0dSKonstantin Belousov ext->ext_tss.tss_esp0 = PCPU_GET(trampstk);
323b40ce416SJulian Elischer td->td_pcb->pcb_ext = ext;
3241bfa9108SDavid Xu PCPU_SET(private_tss, 1);
325f726a873SJohn Baldwin *PCPU_GET(tss_gdt) = ext->ext_tssd;
326f726a873SJohn Baldwin ltr(GSEL(GPROC0_SEL, SEL_KPL));
3271bfa9108SDavid Xu critical_exit();
32848a09cf2SJohn Dyson
32948a09cf2SJohn Dyson return 0;
33048a09cf2SJohn Dyson }
33148a09cf2SJohn Dyson
33284569dffSMaxim Sobolev int
i386_set_ioperm(struct thread * td,struct i386_ioperm_args * uap)333f0fe68a9SDmitry Chagin i386_set_ioperm(struct thread *td, struct i386_ioperm_args *uap)
33448a09cf2SJohn Dyson {
33548a09cf2SJohn Dyson char *iomap;
3360bfad8e4SKonstantin Belousov u_int i;
3370bfad8e4SKonstantin Belousov int error;
33848a09cf2SJohn Dyson
339acd3428bSRobert Watson if ((error = priv_check(td, PRIV_IO)) != 0)
34048a09cf2SJohn Dyson return (error);
341a854ed98SJohn Baldwin if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
342330e7889SRobert Watson return (error);
34348a09cf2SJohn Dyson /*
34448a09cf2SJohn Dyson * XXX
34548a09cf2SJohn Dyson * While this is restricted to root, we should probably figure out
34648a09cf2SJohn Dyson * whether any other driver is using this i/o address, as so not to
34748a09cf2SJohn Dyson * cause confusion. This probably requires a global 'usage registry'.
34848a09cf2SJohn Dyson */
34948a09cf2SJohn Dyson
350b40ce416SJulian Elischer if (td->td_pcb->pcb_ext == 0)
351b40ce416SJulian Elischer if ((error = i386_extend_pcb(td)) != 0)
35248a09cf2SJohn Dyson return (error);
353b40ce416SJulian Elischer iomap = (char *)td->td_pcb->pcb_ext->ext_iomap;
35448a09cf2SJohn Dyson
3550bfad8e4SKonstantin Belousov if (uap->start > uap->start + uap->length ||
3560bfad8e4SKonstantin Belousov uap->start + uap->length > IOPAGES * PAGE_SIZE * NBBY)
35748a09cf2SJohn Dyson return (EINVAL);
35848a09cf2SJohn Dyson
35984569dffSMaxim Sobolev for (i = uap->start; i < uap->start + uap->length; i++) {
36084569dffSMaxim Sobolev if (uap->enable)
36148a09cf2SJohn Dyson iomap[i >> 3] &= ~(1 << (i & 7));
36248a09cf2SJohn Dyson else
36348a09cf2SJohn Dyson iomap[i >> 3] |= (1 << (i & 7));
36448a09cf2SJohn Dyson }
36548a09cf2SJohn Dyson return (error);
36648a09cf2SJohn Dyson }
36748a09cf2SJohn Dyson
36884569dffSMaxim Sobolev int
i386_get_ioperm(struct thread * td,struct i386_ioperm_args * uap)369f0fe68a9SDmitry Chagin i386_get_ioperm(struct thread *td, struct i386_ioperm_args *uap)
37048a09cf2SJohn Dyson {
37184569dffSMaxim Sobolev int i, state;
37248a09cf2SJohn Dyson char *iomap;
37348a09cf2SJohn Dyson
37484569dffSMaxim Sobolev if (uap->start >= IOPAGES * PAGE_SIZE * NBBY)
3759729f279SBruce Evans return (EINVAL);
37648a09cf2SJohn Dyson
377b40ce416SJulian Elischer if (td->td_pcb->pcb_ext == 0) {
37884569dffSMaxim Sobolev uap->length = 0;
37948a09cf2SJohn Dyson goto done;
38048a09cf2SJohn Dyson }
38148a09cf2SJohn Dyson
382b40ce416SJulian Elischer iomap = (char *)td->td_pcb->pcb_ext->ext_iomap;
38348a09cf2SJohn Dyson
38484569dffSMaxim Sobolev i = uap->start;
38538861023SPeter Wemm state = (iomap[i >> 3] >> (i & 7)) & 1;
38684569dffSMaxim Sobolev uap->enable = !state;
38784569dffSMaxim Sobolev uap->length = 1;
38848a09cf2SJohn Dyson
38984569dffSMaxim Sobolev for (i = uap->start + 1; i < IOPAGES * PAGE_SIZE * NBBY; i++) {
39048a09cf2SJohn Dyson if (state != ((iomap[i >> 3] >> (i & 7)) & 1))
39148a09cf2SJohn Dyson break;
39284569dffSMaxim Sobolev uap->length++;
39348a09cf2SJohn Dyson }
39448a09cf2SJohn Dyson
39548a09cf2SJohn Dyson done:
39684569dffSMaxim Sobolev return (0);
39748a09cf2SJohn Dyson }
39848a09cf2SJohn Dyson
3990dbf6d73SJordan K. Hubbard /*
4000dbf6d73SJordan K. Hubbard * Update the GDT entry pointing to the LDT to point to the LDT of the
4010ad5e7f3SJeff Roberson * current process. Manage dt_lock holding/unholding autonomously.
4020dbf6d73SJordan K. Hubbard */
40313a5d911SKonstantin Belousov static void
set_user_ldt_locked(struct mdproc * mdp)40413a5d911SKonstantin Belousov set_user_ldt_locked(struct mdproc *mdp)
40513a5d911SKonstantin Belousov {
40613a5d911SKonstantin Belousov struct proc_ldt *pldt;
40713a5d911SKonstantin Belousov int gdt_idx;
40813a5d911SKonstantin Belousov
40913a5d911SKonstantin Belousov mtx_assert(&dt_lock, MA_OWNED);
41013a5d911SKonstantin Belousov
41113a5d911SKonstantin Belousov pldt = mdp->md_ldt;
41213a5d911SKonstantin Belousov gdt_idx = GUSERLDT_SEL;
41313a5d911SKonstantin Belousov gdt_idx += PCPU_GET(cpuid) * NGDT; /* always 0 on UP */
41413a5d911SKonstantin Belousov gdt[gdt_idx].sd = pldt->ldt_sd;
41513a5d911SKonstantin Belousov lldt(GSEL(GUSERLDT_SEL, SEL_KPL));
41613a5d911SKonstantin Belousov PCPU_SET(currentldt, GSEL(GUSERLDT_SEL, SEL_KPL));
41713a5d911SKonstantin Belousov }
41813a5d911SKonstantin Belousov
419da59a31cSDavid Greenman void
set_user_ldt(struct mdproc * mdp)42024db0459SJohn Baldwin set_user_ldt(struct mdproc *mdp)
421da59a31cSDavid Greenman {
4220ad5e7f3SJeff Roberson
4230ad5e7f3SJeff Roberson mtx_lock_spin(&dt_lock);
42413a5d911SKonstantin Belousov set_user_ldt_locked(mdp);
4250ad5e7f3SJeff Roberson mtx_unlock_spin(&dt_lock);
4261acf256dSJohn Baldwin }
4271acf256dSJohn Baldwin
4285eb6e279SJohn Baldwin #ifdef SMP
4295eb6e279SJohn Baldwin static void
set_user_ldt_rv(void * arg)43013a5d911SKonstantin Belousov set_user_ldt_rv(void *arg)
4311acf256dSJohn Baldwin {
43213a5d911SKonstantin Belousov struct proc *p;
4331acf256dSJohn Baldwin
43413a5d911SKonstantin Belousov p = curproc;
43513a5d911SKonstantin Belousov if (arg == p->p_vmspace)
43613a5d911SKonstantin Belousov set_user_ldt(&p->p_md);
437da59a31cSDavid Greenman }
4385eb6e279SJohn Baldwin #endif
439da59a31cSDavid Greenman
440df4d012bSJohn Baldwin /*
4410ad5e7f3SJeff Roberson * dt_lock must be held. Returns with dt_lock held.
442df4d012bSJohn Baldwin */
44324db0459SJohn Baldwin struct proc_ldt *
user_ldt_alloc(struct mdproc * mdp,int len)44424db0459SJohn Baldwin user_ldt_alloc(struct mdproc *mdp, int len)
44591c28bfdSLuoqi Chen {
44624db0459SJohn Baldwin struct proc_ldt *pldt, *new_ldt;
44791c28bfdSLuoqi Chen
4480ad5e7f3SJeff Roberson mtx_assert(&dt_lock, MA_OWNED);
4490ad5e7f3SJeff Roberson mtx_unlock_spin(&dt_lock);
450711d5183SKonstantin Belousov new_ldt = malloc(sizeof(struct proc_ldt), M_SUBPROC, M_WAITOK);
45191c28bfdSLuoqi Chen
45291c28bfdSLuoqi Chen new_ldt->ldt_len = len = NEW_MAX_LD(len);
453d86c1f0dSKonstantin Belousov new_ldt->ldt_base = pmap_trm_alloc(len * sizeof(union descriptor),
454d86c1f0dSKonstantin Belousov M_WAITOK | M_ZERO);
45505dfa22fSAttilio Rao new_ldt->ldt_refcnt = 1;
45691c28bfdSLuoqi Chen new_ldt->ldt_active = 0;
45791c28bfdSLuoqi Chen
4580ad5e7f3SJeff Roberson mtx_lock_spin(&dt_lock);
45991c28bfdSLuoqi Chen gdt_segs[GUSERLDT_SEL].ssd_base = (unsigned)new_ldt->ldt_base;
46091c28bfdSLuoqi Chen gdt_segs[GUSERLDT_SEL].ssd_limit = len * sizeof(union descriptor) - 1;
46191c28bfdSLuoqi Chen ssdtosd(&gdt_segs[GUSERLDT_SEL], &new_ldt->ldt_sd);
46291c28bfdSLuoqi Chen
4630ad5e7f3SJeff Roberson if ((pldt = mdp->md_ldt) != NULL) {
46424db0459SJohn Baldwin if (len > pldt->ldt_len)
46524db0459SJohn Baldwin len = pldt->ldt_len;
46624db0459SJohn Baldwin bcopy(pldt->ldt_base, new_ldt->ldt_base,
46791c28bfdSLuoqi Chen len * sizeof(union descriptor));
4680ad5e7f3SJeff Roberson } else
469d86c1f0dSKonstantin Belousov bcopy(ldt, new_ldt->ldt_base, sizeof(union descriptor) * NLDT);
4700ad5e7f3SJeff Roberson
4710ad5e7f3SJeff Roberson return (new_ldt);
47291c28bfdSLuoqi Chen }
47391c28bfdSLuoqi Chen
474df4d012bSJohn Baldwin /*
47502b0a160SAttilio Rao * Must be called with dt_lock held. Returns with dt_lock unheld.
476df4d012bSJohn Baldwin */
47791c28bfdSLuoqi Chen void
user_ldt_free(struct thread * td)47824db0459SJohn Baldwin user_ldt_free(struct thread *td)
47991c28bfdSLuoqi Chen {
480711d5183SKonstantin Belousov struct mdproc *mdp;
4810ad5e7f3SJeff Roberson struct proc_ldt *pldt;
48291c28bfdSLuoqi Chen
4830ad5e7f3SJeff Roberson mtx_assert(&dt_lock, MA_OWNED);
484711d5183SKonstantin Belousov mdp = &td->td_proc->p_md;
485cb1d78d0SKonstantin Belousov if ((pldt = mdp->md_ldt) == NULL) {
486cb1d78d0SKonstantin Belousov mtx_unlock_spin(&dt_lock);
48791c28bfdSLuoqi Chen return;
488cb1d78d0SKonstantin Belousov }
48991c28bfdSLuoqi Chen
490b494482fSJohn Baldwin if (td == curthread) {
49191c28bfdSLuoqi Chen lldt(_default_ldt);
4924ef34f39SJake Burkholder PCPU_SET(currentldt, _default_ldt);
49391c28bfdSLuoqi Chen }
49491c28bfdSLuoqi Chen
49524db0459SJohn Baldwin mdp->md_ldt = NULL;
4969719da13SKonstantin Belousov user_ldt_deref(pldt);
4979719da13SKonstantin Belousov }
4989719da13SKonstantin Belousov
4999719da13SKonstantin Belousov void
user_ldt_deref(struct proc_ldt * pldt)5009719da13SKonstantin Belousov user_ldt_deref(struct proc_ldt *pldt)
5019719da13SKonstantin Belousov {
5029719da13SKonstantin Belousov
5039719da13SKonstantin Belousov mtx_assert(&dt_lock, MA_OWNED);
50405dfa22fSAttilio Rao if (--pldt->ldt_refcnt == 0) {
50502b0a160SAttilio Rao mtx_unlock_spin(&dt_lock);
506d86c1f0dSKonstantin Belousov pmap_trm_free(pldt->ldt_base, pldt->ldt_len *
507d86c1f0dSKonstantin Belousov sizeof(union descriptor));
5081ede983cSDag-Erling Smørgrav free(pldt, M_SUBPROC);
50905dfa22fSAttilio Rao } else
51005dfa22fSAttilio Rao mtx_unlock_spin(&dt_lock);
51191c28bfdSLuoqi Chen }
51291c28bfdSLuoqi Chen
51384569dffSMaxim Sobolev /*
51484569dffSMaxim Sobolev * Note for the authors of compat layers (linux, etc): copyout() in
51584569dffSMaxim Sobolev * the function below is not a problem since it presents data in
51684569dffSMaxim Sobolev * arch-specific format (i.e. i386-specific in this case), not in
51784569dffSMaxim Sobolev * the OS-specific one.
51884569dffSMaxim Sobolev */
51984569dffSMaxim Sobolev int
i386_get_ldt(struct thread * td,struct i386_ldt_args * uap)5206cdf28b7SKonstantin Belousov i386_get_ldt(struct thread *td, struct i386_ldt_args *uap)
521da59a31cSDavid Greenman {
5220ad5e7f3SJeff Roberson struct proc_ldt *pldt;
523a4730cdbSKonstantin Belousov char *data;
524a4730cdbSKonstantin Belousov u_int nldt, num;
525a4730cdbSKonstantin Belousov int error;
526da59a31cSDavid Greenman
527da59a31cSDavid Greenman #ifdef DEBUG
5281f32d2a7SKonstantin Belousov printf("i386_get_ldt: start=%u num=%u descs=%p\n",
5297a2bb3b8SLuoqi Chen uap->start, uap->num, (void *)uap->descs);
530da59a31cSDavid Greenman #endif
531da59a31cSDavid Greenman
53200d37313SKonstantin Belousov num = min(uap->num, MAX_LD);
53300d37313SKonstantin Belousov data = malloc(num * sizeof(union descriptor), M_TEMP, M_WAITOK);
534a4730cdbSKonstantin Belousov mtx_lock_spin(&dt_lock);
535a4730cdbSKonstantin Belousov pldt = td->td_proc->p_md.md_ldt;
5360f166953SDimitry Andric nldt = pldt != NULL ? pldt->ldt_len : NLDT;
53700d37313SKonstantin Belousov if (uap->start >= nldt) {
53800d37313SKonstantin Belousov num = 0;
53900d37313SKonstantin Belousov } else {
54000d37313SKonstantin Belousov num = min(num, nldt - uap->start);
541a4730cdbSKonstantin Belousov bcopy(pldt != NULL ?
542a4730cdbSKonstantin Belousov &((union descriptor *)(pldt->ldt_base))[uap->start] :
543a4730cdbSKonstantin Belousov &ldt[uap->start], data, num * sizeof(union descriptor));
54400d37313SKonstantin Belousov }
545a4730cdbSKonstantin Belousov mtx_unlock_spin(&dt_lock);
546a4730cdbSKonstantin Belousov error = copyout(data, uap->descs, num * sizeof(union descriptor));
54798c2674eSKonstantin Belousov if (error == 0)
548b40ce416SJulian Elischer td->td_retval[0] = num;
549a4730cdbSKonstantin Belousov free(data, M_TEMP);
550da59a31cSDavid Greenman return (error);
551da59a31cSDavid Greenman }
552da59a31cSDavid Greenman
55384569dffSMaxim Sobolev int
i386_set_ldt(struct thread * td,struct i386_ldt_args * uap,union descriptor * descs)5546cdf28b7SKonstantin Belousov i386_set_ldt(struct thread *td, struct i386_ldt_args *uap,
5556cdf28b7SKonstantin Belousov union descriptor *descs)
556da59a31cSDavid Greenman {
55798c2674eSKonstantin Belousov struct mdproc *mdp;
558bc2e774aSJohn Baldwin struct proc_ldt *pldt;
55984569dffSMaxim Sobolev union descriptor *dp;
560a4730cdbSKonstantin Belousov u_int largest_ld, i;
561a4730cdbSKonstantin Belousov int error;
562da59a31cSDavid Greenman
5631716a1afSJulian Elischer #ifdef DEBUG
5641f32d2a7SKonstantin Belousov printf("i386_set_ldt: start=%u num=%u descs=%p\n",
5651716a1afSJulian Elischer uap->start, uap->num, (void *)uap->descs);
5661716a1afSJulian Elischer #endif
5673cabd93eSKonstantin Belousov error = 0;
56898c2674eSKonstantin Belousov mdp = &td->td_proc->p_md;
5691716a1afSJulian Elischer
57084569dffSMaxim Sobolev if (descs == NULL) {
571dae8d52dSDavid Xu /* Free descriptors */
572dae8d52dSDavid Xu if (uap->start == 0 && uap->num == 0) {
573dae8d52dSDavid Xu /*
574dae8d52dSDavid Xu * Treat this as a special case, so userland needn't
575dae8d52dSDavid Xu * know magic number NLDT.
576dae8d52dSDavid Xu */
577dae8d52dSDavid Xu uap->start = NLDT;
578dae8d52dSDavid Xu uap->num = MAX_LD - NLDT;
5790dbf6d73SJordan K. Hubbard }
5800ad5e7f3SJeff Roberson mtx_lock_spin(&dt_lock);
5810ad5e7f3SJeff Roberson if ((pldt = mdp->md_ldt) == NULL ||
5820ad5e7f3SJeff Roberson uap->start >= pldt->ldt_len) {
5830ad5e7f3SJeff Roberson mtx_unlock_spin(&dt_lock);
584dae8d52dSDavid Xu return (0);
585dae8d52dSDavid Xu }
586dae8d52dSDavid Xu largest_ld = uap->start + uap->num;
587dae8d52dSDavid Xu if (largest_ld > pldt->ldt_len)
588dae8d52dSDavid Xu largest_ld = pldt->ldt_len;
5893cabd93eSKonstantin Belousov for (i = uap->start; i < largest_ld; i++)
5903cabd93eSKonstantin Belousov atomic_store_rel_64(&((uint64_t *)(pldt->ldt_base))[i],
5913cabd93eSKonstantin Belousov 0);
5920ad5e7f3SJeff Roberson mtx_unlock_spin(&dt_lock);
593dae8d52dSDavid Xu return (0);
594dae8d52dSDavid Xu }
595dae8d52dSDavid Xu
596711d5183SKonstantin Belousov if (uap->start != LDT_AUTO_ALLOC || uap->num != 1) {
597dae8d52dSDavid Xu /* verify range of descriptors to modify */
598dae8d52dSDavid Xu largest_ld = uap->start + uap->num;
599711d5183SKonstantin Belousov if (uap->start >= MAX_LD || largest_ld > MAX_LD)
600dae8d52dSDavid Xu return (EINVAL);
601dae8d52dSDavid Xu }
602da59a31cSDavid Greenman
603da59a31cSDavid Greenman /* Check descriptors for access violations */
604dae8d52dSDavid Xu for (i = 0; i < uap->num; i++) {
6058eb8107bSPeter Wemm dp = &descs[i];
606da59a31cSDavid Greenman
6078eb8107bSPeter Wemm switch (dp->sd.sd_type) {
6080dbf6d73SJordan K. Hubbard case SDT_SYSNULL: /* system null */
6098eb8107bSPeter Wemm dp->sd.sd_p = 0;
6100dbf6d73SJordan K. Hubbard break;
6110dbf6d73SJordan K. Hubbard case SDT_SYS286TSS: /* system 286 TSS available */
6120dbf6d73SJordan K. Hubbard case SDT_SYSLDT: /* system local descriptor table */
6130dbf6d73SJordan K. Hubbard case SDT_SYS286BSY: /* system 286 TSS busy */
6140dbf6d73SJordan K. Hubbard case SDT_SYSTASKGT: /* system task gate */
6150dbf6d73SJordan K. Hubbard case SDT_SYS286IGT: /* system 286 interrupt gate */
6160dbf6d73SJordan K. Hubbard case SDT_SYS286TGT: /* system 286 trap gate */
6170dbf6d73SJordan K. Hubbard case SDT_SYSNULL2: /* undefined by Intel */
6180dbf6d73SJordan K. Hubbard case SDT_SYS386TSS: /* system 386 TSS available */
6190dbf6d73SJordan K. Hubbard case SDT_SYSNULL3: /* undefined by Intel */
6200dbf6d73SJordan K. Hubbard case SDT_SYS386BSY: /* system 386 TSS busy */
6210dbf6d73SJordan K. Hubbard case SDT_SYSNULL4: /* undefined by Intel */
6220dbf6d73SJordan K. Hubbard case SDT_SYS386IGT: /* system 386 interrupt gate */
6230dbf6d73SJordan K. Hubbard case SDT_SYS386TGT: /* system 386 trap gate */
6240dbf6d73SJordan K. Hubbard case SDT_SYS286CGT: /* system 286 call gate */
6250dbf6d73SJordan K. Hubbard case SDT_SYS386CGT: /* system 386 call gate */
626dae8d52dSDavid Xu return (EACCES);
627da59a31cSDavid Greenman
6280dbf6d73SJordan K. Hubbard /* memory segment types */
6290dbf6d73SJordan K. Hubbard case SDT_MEMEC: /* memory execute only conforming */
6300dbf6d73SJordan K. Hubbard case SDT_MEMEAC: /* memory execute only accessed conforming */
6310dbf6d73SJordan K. Hubbard case SDT_MEMERC: /* memory execute read conforming */
6320dbf6d73SJordan K. Hubbard case SDT_MEMERAC: /* memory execute read accessed conforming */
6330dbf6d73SJordan K. Hubbard /* Must be "present" if executable and conforming. */
63484569dffSMaxim Sobolev if (dp->sd.sd_p == 0)
635da59a31cSDavid Greenman return (EACCES);
636da59a31cSDavid Greenman break;
6370dbf6d73SJordan K. Hubbard case SDT_MEMRO: /* memory read only */
6380dbf6d73SJordan K. Hubbard case SDT_MEMROA: /* memory read only accessed */
6390dbf6d73SJordan K. Hubbard case SDT_MEMRW: /* memory read write */
6400dbf6d73SJordan K. Hubbard case SDT_MEMRWA: /* memory read write accessed */
6410dbf6d73SJordan K. Hubbard case SDT_MEMROD: /* memory read only expand dwn limit */
6420dbf6d73SJordan K. Hubbard case SDT_MEMRODA: /* memory read only expand dwn lim accessed */
6430dbf6d73SJordan K. Hubbard case SDT_MEMRWD: /* memory read write expand dwn limit */
6440dbf6d73SJordan K. Hubbard case SDT_MEMRWDA: /* memory read write expand dwn lim acessed */
6450dbf6d73SJordan K. Hubbard case SDT_MEME: /* memory execute only */
6460dbf6d73SJordan K. Hubbard case SDT_MEMEA: /* memory execute only accessed */
6470dbf6d73SJordan K. Hubbard case SDT_MEMER: /* memory execute read */
6480dbf6d73SJordan K. Hubbard case SDT_MEMERA: /* memory execute read accessed */
649da59a31cSDavid Greenman break;
650da59a31cSDavid Greenman default:
6510dbf6d73SJordan K. Hubbard return (EINVAL);
652da59a31cSDavid Greenman }
6530dbf6d73SJordan K. Hubbard
6540dbf6d73SJordan K. Hubbard /* Only user (ring-3) descriptors may be present. */
655711d5183SKonstantin Belousov if (dp->sd.sd_p != 0 && dp->sd.sd_dpl != SEL_UPL)
6560dbf6d73SJordan K. Hubbard return (EACCES);
657da59a31cSDavid Greenman }
658da59a31cSDavid Greenman
6595774db75SJulian Elischer if (uap->start == LDT_AUTO_ALLOC && uap->num == 1) {
660dae8d52dSDavid Xu /* Allocate a free slot */
6610ad5e7f3SJeff Roberson mtx_lock_spin(&dt_lock);
6620ad5e7f3SJeff Roberson if ((pldt = mdp->md_ldt) == NULL) {
6630ad5e7f3SJeff Roberson if ((error = i386_ldt_grow(td, NLDT + 1))) {
6640ad5e7f3SJeff Roberson mtx_unlock_spin(&dt_lock);
665dae8d52dSDavid Xu return (error);
6660ad5e7f3SJeff Roberson }
667dae8d52dSDavid Xu pldt = mdp->md_ldt;
668dae8d52dSDavid Xu }
669dae8d52dSDavid Xu again:
670f09fc81cSJulian Elischer /*
671f09fc81cSJulian Elischer * start scanning a bit up to leave room for NVidia and
672f09fc81cSJulian Elischer * Wine, which still user the "Blat" method of allocation.
673f09fc81cSJulian Elischer */
6745774db75SJulian Elischer dp = &((union descriptor *)(pldt->ldt_base))[NLDT];
675e4e2c613SJulian Elischer for (i = NLDT; i < pldt->ldt_len; ++i) {
676dae8d52dSDavid Xu if (dp->sd.sd_type == SDT_SYSNULL)
677dae8d52dSDavid Xu break;
678dae8d52dSDavid Xu dp++;
679dae8d52dSDavid Xu }
680dae8d52dSDavid Xu if (i >= pldt->ldt_len) {
6810ad5e7f3SJeff Roberson if ((error = i386_ldt_grow(td, pldt->ldt_len+1))) {
6820ad5e7f3SJeff Roberson mtx_unlock_spin(&dt_lock);
683dae8d52dSDavid Xu return (error);
6840ad5e7f3SJeff Roberson }
685dae8d52dSDavid Xu goto again;
686dae8d52dSDavid Xu }
687dae8d52dSDavid Xu uap->start = i;
688dae8d52dSDavid Xu error = i386_set_ldt_data(td, i, 1, descs);
6890ad5e7f3SJeff Roberson mtx_unlock_spin(&dt_lock);
690dae8d52dSDavid Xu } else {
691dae8d52dSDavid Xu largest_ld = uap->start + uap->num;
6920ad5e7f3SJeff Roberson mtx_lock_spin(&dt_lock);
6930ad5e7f3SJeff Roberson if (!(error = i386_ldt_grow(td, largest_ld))) {
694dae8d52dSDavid Xu error = i386_set_ldt_data(td, uap->start, uap->num,
695dae8d52dSDavid Xu descs);
696dae8d52dSDavid Xu }
6970ad5e7f3SJeff Roberson mtx_unlock_spin(&dt_lock);
698dae8d52dSDavid Xu }
699dae8d52dSDavid Xu if (error == 0)
700dae8d52dSDavid Xu td->td_retval[0] = uap->start;
701dae8d52dSDavid Xu return (error);
702dae8d52dSDavid Xu }
703dae8d52dSDavid Xu
70493ee134aSKip Macy static int
i386_set_ldt_data(struct thread * td,int start,int num,union descriptor * descs)70593ee134aSKip Macy i386_set_ldt_data(struct thread *td, int start, int num,
70693ee134aSKip Macy union descriptor *descs)
70793ee134aSKip Macy {
7083cabd93eSKonstantin Belousov struct mdproc *mdp;
7093cabd93eSKonstantin Belousov struct proc_ldt *pldt;
7103cabd93eSKonstantin Belousov uint64_t *dst, *src;
7113cabd93eSKonstantin Belousov int i;
71293ee134aSKip Macy
71393ee134aSKip Macy mtx_assert(&dt_lock, MA_OWNED);
71493ee134aSKip Macy
7153cabd93eSKonstantin Belousov mdp = &td->td_proc->p_md;
7163cabd93eSKonstantin Belousov pldt = mdp->md_ldt;
7173cabd93eSKonstantin Belousov dst = (uint64_t *)(pldt->ldt_base);
7183cabd93eSKonstantin Belousov src = (uint64_t *)descs;
7193cabd93eSKonstantin Belousov
7203cabd93eSKonstantin Belousov /*
7213cabd93eSKonstantin Belousov * Atomic(9) is used only to get 64bit atomic store with
7223cabd93eSKonstantin Belousov * cmpxchg8b when available. There is no op without release
7233cabd93eSKonstantin Belousov * semantic.
7243cabd93eSKonstantin Belousov */
7253cabd93eSKonstantin Belousov for (i = 0; i < num; i++)
7263cabd93eSKonstantin Belousov atomic_store_rel_64(&dst[start + i], src[i]);
72793ee134aSKip Macy return (0);
72893ee134aSKip Macy }
729dae8d52dSDavid Xu
730dae8d52dSDavid Xu static int
i386_ldt_grow(struct thread * td,int len)731dae8d52dSDavid Xu i386_ldt_grow(struct thread *td, int len)
732dae8d52dSDavid Xu {
7334f7c931aSKonstantin Belousov struct mdproc *mdp;
73405dfa22fSAttilio Rao struct proc_ldt *new_ldt, *pldt;
7354f7c931aSKonstantin Belousov caddr_t old_ldt_base;
7364f7c931aSKonstantin Belousov int old_ldt_len;
737dae8d52dSDavid Xu
7380ad5e7f3SJeff Roberson mtx_assert(&dt_lock, MA_OWNED);
7390ad5e7f3SJeff Roberson
740dae8d52dSDavid Xu if (len > MAX_LD)
741dae8d52dSDavid Xu return (ENOMEM);
742dae8d52dSDavid Xu if (len < NLDT + 1)
743dae8d52dSDavid Xu len = NLDT + 1;
744bc2e774aSJohn Baldwin
7454f7c931aSKonstantin Belousov mdp = &td->td_proc->p_md;
7464f7c931aSKonstantin Belousov old_ldt_base = NULL_LDT_BASE;
7474f7c931aSKonstantin Belousov old_ldt_len = 0;
7484f7c931aSKonstantin Belousov
749bc2e774aSJohn Baldwin /* Allocate a user ldt. */
75002b0a160SAttilio Rao if ((pldt = mdp->md_ldt) == NULL || len > pldt->ldt_len) {
751bc2e774aSJohn Baldwin new_ldt = user_ldt_alloc(mdp, len);
752dae8d52dSDavid Xu if (new_ldt == NULL)
753dae8d52dSDavid Xu return (ENOMEM);
754dae8d52dSDavid Xu pldt = mdp->md_ldt;
755bc2e774aSJohn Baldwin
7560ad5e7f3SJeff Roberson if (pldt != NULL) {
75705dfa22fSAttilio Rao if (new_ldt->ldt_len <= pldt->ldt_len) {
758dae8d52dSDavid Xu /*
75905dfa22fSAttilio Rao * We just lost the race for allocation, so
76005dfa22fSAttilio Rao * free the new object and return.
761dae8d52dSDavid Xu */
76202b0a160SAttilio Rao mtx_unlock_spin(&dt_lock);
763d86c1f0dSKonstantin Belousov pmap_trm_free(new_ldt->ldt_base,
764dae8d52dSDavid Xu new_ldt->ldt_len * sizeof(union descriptor));
7651ede983cSDag-Erling Smørgrav free(new_ldt, M_SUBPROC);
76602b0a160SAttilio Rao mtx_lock_spin(&dt_lock);
767dae8d52dSDavid Xu return (0);
768dae8d52dSDavid Xu }
76905dfa22fSAttilio Rao
77005dfa22fSAttilio Rao /*
77105dfa22fSAttilio Rao * We have to substitute the current LDT entry for
77205dfa22fSAttilio Rao * curproc with the new one since its size grew.
77305dfa22fSAttilio Rao */
77405dfa22fSAttilio Rao old_ldt_base = pldt->ldt_base;
77505dfa22fSAttilio Rao old_ldt_len = pldt->ldt_len;
77605dfa22fSAttilio Rao pldt->ldt_sd = new_ldt->ldt_sd;
77705dfa22fSAttilio Rao pldt->ldt_base = new_ldt->ldt_base;
77805dfa22fSAttilio Rao pldt->ldt_len = new_ldt->ldt_len;
7790ad5e7f3SJeff Roberson } else
780dae8d52dSDavid Xu mdp->md_ldt = pldt = new_ldt;
781dae8d52dSDavid Xu #ifdef SMP
78202b0a160SAttilio Rao /*
78302b0a160SAttilio Rao * Signal other cpus to reload ldt. We need to unlock dt_lock
78402b0a160SAttilio Rao * here because other CPU will contest on it since their
78502b0a160SAttilio Rao * curthreads won't hold the lock and will block when trying
78602b0a160SAttilio Rao * to acquire it.
78702b0a160SAttilio Rao */
78802b0a160SAttilio Rao mtx_unlock_spin(&dt_lock);
78913a5d911SKonstantin Belousov smp_rendezvous(NULL, set_user_ldt_rv, NULL,
79013a5d911SKonstantin Belousov td->td_proc->p_vmspace);
791dae8d52dSDavid Xu #else
79213a5d911SKonstantin Belousov set_user_ldt_locked(&td->td_proc->p_md);
79305dfa22fSAttilio Rao mtx_unlock_spin(&dt_lock);
794dae8d52dSDavid Xu #endif
79505dfa22fSAttilio Rao if (old_ldt_base != NULL_LDT_BASE) {
796d86c1f0dSKonstantin Belousov pmap_trm_free(old_ldt_base, old_ldt_len *
797d86c1f0dSKonstantin Belousov sizeof(union descriptor));
7981ede983cSDag-Erling Smørgrav free(new_ldt, M_SUBPROC);
79905dfa22fSAttilio Rao }
80005dfa22fSAttilio Rao mtx_lock_spin(&dt_lock);
801dae8d52dSDavid Xu }
8028eb8107bSPeter Wemm return (0);
803da59a31cSDavid Greenman }
804