xref: /freebsd/sys/i386/i386/sys_machdep.c (revision 05296a0f)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1990 The Regents of the University of California.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #include "opt_capsicum.h"
34 #include "opt_kstack_pages.h"
35 #include "opt_ktrace.h"
36 
37 #include <sys/param.h>
38 #include <sys/capsicum.h>
39 #include <sys/systm.h>
40 #include <sys/ktrace.h>
41 #include <sys/lock.h>
42 #include <sys/malloc.h>
43 #include <sys/mutex.h>
44 #include <sys/priv.h>
45 #include <sys/proc.h>
46 #include <sys/smp.h>
47 #include <sys/sysproto.h>
48 
49 #include <vm/vm.h>
50 #include <vm/pmap.h>
51 #include <vm/vm_map.h>
52 #include <vm/vm_extern.h>
53 
54 #include <machine/atomic.h>
55 #include <machine/cpu.h>
56 #include <machine/pcb.h>
57 #include <machine/pcb_ext.h>
58 #include <machine/proc.h>
59 #include <machine/sysarch.h>
60 
61 #include <security/audit/audit.h>
62 
63 #include <vm/vm_kern.h>		/* for kernel_map */
64 
65 #define MAX_LD 8192
66 #define LD_PER_PAGE 512
67 #define	NEW_MAX_LD(num)  rounddown2(num + LD_PER_PAGE, LD_PER_PAGE)
68 #define SIZE_FROM_LARGEST_LD(num) (NEW_MAX_LD(num) << 3)
69 #define	NULL_LDT_BASE	((caddr_t)NULL)
70 
71 #ifdef SMP
72 static void set_user_ldt_rv(void *arg);
73 #endif
74 static int i386_set_ldt_data(struct thread *, int start, int num,
75     union descriptor *descs);
76 static int i386_ldt_grow(struct thread *td, int len);
77 
78 void
fill_based_sd(struct segment_descriptor * sdp,uint32_t base)79 fill_based_sd(struct segment_descriptor *sdp, uint32_t base)
80 {
81 
82 	sdp->sd_lobase = base & 0xffffff;
83 	sdp->sd_hibase = (base >> 24) & 0xff;
84 	sdp->sd_lolimit = 0xffff;	/* 4GB limit, wraps around */
85 	sdp->sd_hilimit = 0xf;
86 	sdp->sd_type = SDT_MEMRWA;
87 	sdp->sd_dpl = SEL_UPL;
88 	sdp->sd_p = 1;
89 	sdp->sd_xx = 0;
90 	sdp->sd_def32 = 1;
91 	sdp->sd_gran = 1;
92 }
93 
94 /*
95  * Construct special descriptors for "base" selectors.  Store them in
96  * the PCB for later use by cpu_switch().  Store them in the GDT for
97  * more immediate use.  The GDT entries are part of the current
98  * context.  Callers must load related segment registers to complete
99  * setting up the current context.
100  */
101 void
set_fsbase(struct thread * td,uint32_t base)102 set_fsbase(struct thread *td, uint32_t base)
103 {
104 	struct segment_descriptor sd;
105 
106 	fill_based_sd(&sd, base);
107 	critical_enter();
108 	td->td_pcb->pcb_fsd = sd;
109 	if (td == curthread)
110 		PCPU_GET(fsgs_gdt)[0] = sd;
111 	critical_exit();
112 }
113 
114 void
set_gsbase(struct thread * td,uint32_t base)115 set_gsbase(struct thread *td, uint32_t base)
116 {
117 	struct segment_descriptor sd;
118 
119 	fill_based_sd(&sd, base);
120 	critical_enter();
121 	td->td_pcb->pcb_gsd = sd;
122 	if (td == curthread)
123 		PCPU_GET(fsgs_gdt)[1] = sd;
124 	critical_exit();
125 }
126 
127 #ifndef _SYS_SYSPROTO_H_
128 struct sysarch_args {
129 	int op;
130 	char *parms;
131 };
132 #endif
133 
134 int
sysarch(struct thread * td,struct sysarch_args * uap)135 sysarch(struct thread *td, struct sysarch_args *uap)
136 {
137 	int error;
138 	union descriptor *lp;
139 	union {
140 		struct i386_ldt_args largs;
141 		struct i386_ioperm_args iargs;
142 		struct i386_get_xfpustate xfpu;
143 	} kargs;
144 	uint32_t base;
145 	struct segment_descriptor *sdp;
146 
147 	AUDIT_ARG_CMD(uap->op);
148 
149 #ifdef CAPABILITY_MODE
150 	/*
151 	 * When adding new operations, add a new case statement here to
152 	 * explicitly indicate whether or not the operation is safe to
153 	 * perform in capability mode.
154 	 */
155 	switch (uap->op) {
156 	case I386_GET_LDT:
157 	case I386_SET_LDT:
158 	case I386_GET_IOPERM:
159 	case I386_GET_FSBASE:
160 	case I386_SET_FSBASE:
161 	case I386_GET_GSBASE:
162 	case I386_SET_GSBASE:
163 	case I386_GET_XFPUSTATE:
164 		break;
165 
166 	case I386_SET_IOPERM:
167 	default:
168 		if (CAP_TRACING(td))
169 			ktrcapfail(CAPFAIL_SYSCALL, &uap->op);
170 		if (IN_CAPABILITY_MODE(td))
171 			return (ECAPMODE);
172 		break;
173 	}
174 #endif
175 
176 	switch (uap->op) {
177 	case I386_GET_IOPERM:
178 	case I386_SET_IOPERM:
179 		if ((error = copyin(uap->parms, &kargs.iargs,
180 		    sizeof(struct i386_ioperm_args))) != 0)
181 			return (error);
182 		break;
183 	case I386_GET_LDT:
184 	case I386_SET_LDT:
185 		if ((error = copyin(uap->parms, &kargs.largs,
186 		    sizeof(struct i386_ldt_args))) != 0)
187 			return (error);
188 		break;
189 	case I386_GET_XFPUSTATE:
190 		if ((error = copyin(uap->parms, &kargs.xfpu,
191 		    sizeof(struct i386_get_xfpustate))) != 0)
192 			return (error);
193 		break;
194 	default:
195 		break;
196 	}
197 
198 	switch (uap->op) {
199 	case I386_GET_LDT:
200 		error = i386_get_ldt(td, &kargs.largs);
201 		break;
202 	case I386_SET_LDT:
203 		if (kargs.largs.descs != NULL) {
204 			if (kargs.largs.num > MAX_LD)
205 				return (EINVAL);
206 			lp = malloc(kargs.largs.num * sizeof(union descriptor),
207 			    M_TEMP, M_WAITOK);
208 			error = copyin(kargs.largs.descs, lp,
209 			    kargs.largs.num * sizeof(union descriptor));
210 			if (error == 0)
211 				error = i386_set_ldt(td, &kargs.largs, lp);
212 			free(lp, M_TEMP);
213 		} else {
214 			error = i386_set_ldt(td, &kargs.largs, NULL);
215 		}
216 		break;
217 	case I386_GET_IOPERM:
218 		error = i386_get_ioperm(td, &kargs.iargs);
219 		if (error == 0)
220 			error = copyout(&kargs.iargs, uap->parms,
221 			    sizeof(struct i386_ioperm_args));
222 		break;
223 	case I386_SET_IOPERM:
224 		error = i386_set_ioperm(td, &kargs.iargs);
225 		break;
226 	case I386_VM86:
227 		error = vm86_sysarch(td, uap->parms);
228 		break;
229 	case I386_GET_FSBASE:
230 		sdp = &td->td_pcb->pcb_fsd;
231 		base = sdp->sd_hibase << 24 | sdp->sd_lobase;
232 		error = copyout(&base, uap->parms, sizeof(base));
233 		break;
234 	case I386_SET_FSBASE:
235 		error = copyin(uap->parms, &base, sizeof(base));
236 		if (error == 0) {
237 			/*
238 			 * Construct the special descriptor for fsbase
239 			 * and arrange for doreti to load its selector
240 			 * soon enough.
241 			 */
242 			set_fsbase(td, base);
243 			td->td_frame->tf_fs = GSEL(GUFS_SEL, SEL_UPL);
244 		}
245 		break;
246 	case I386_GET_GSBASE:
247 		sdp = &td->td_pcb->pcb_gsd;
248 		base = sdp->sd_hibase << 24 | sdp->sd_lobase;
249 		error = copyout(&base, uap->parms, sizeof(base));
250 		break;
251 	case I386_SET_GSBASE:
252 		error = copyin(uap->parms, &base, sizeof(base));
253 		if (error == 0) {
254 			/*
255 			 * Construct the special descriptor for gsbase.
256 			 * The selector is loaded immediately, since we
257 			 * normally only reload %gs on context switches.
258 			 */
259 			set_gsbase(td, base);
260 			load_gs(GSEL(GUGS_SEL, SEL_UPL));
261 		}
262 		break;
263 	case I386_GET_XFPUSTATE:
264 		if (kargs.xfpu.len > cpu_max_ext_state_size -
265 		    sizeof(union savefpu))
266 			return (EINVAL);
267 		npxgetregs(td);
268 		error = copyout((char *)(get_pcb_user_save_td(td) + 1),
269 		    kargs.xfpu.addr, kargs.xfpu.len);
270 		break;
271 	default:
272 		error = EINVAL;
273 		break;
274 	}
275 	return (error);
276 }
277 
278 int
i386_extend_pcb(struct thread * td)279 i386_extend_pcb(struct thread *td)
280 {
281 	int i, offset;
282 	u_long *addr;
283 	struct pcb_ext *ext;
284 	struct soft_segment_descriptor ssd = {
285 		0,			/* segment base address (overwritten) */
286 		ctob(IOPAGES + 1) - 1,	/* length */
287 		SDT_SYS386TSS,		/* segment type */
288 		0,			/* priority level */
289 		1,			/* descriptor present */
290 		0, 0,
291 		0,			/* default 32 size */
292 		0			/* granularity */
293 	};
294 
295 	ext = pmap_trm_alloc(ctob(IOPAGES + 1), M_WAITOK | M_ZERO);
296 	/* -16 is so we can convert a trapframe into vm86trapframe inplace */
297 	ext->ext_tss.tss_ss0 = GSEL(GDATA_SEL, SEL_KPL);
298 	/*
299 	 * The last byte of the i/o map must be followed by an 0xff byte.
300 	 * We arbitrarily allocate 16 bytes here, to keep the starting
301 	 * address on a doubleword boundary.
302 	 */
303 	offset = PAGE_SIZE - 16;
304 	ext->ext_tss.tss_ioopt =
305 	    (offset - ((unsigned)&ext->ext_tss - (unsigned)ext)) << 16;
306 	ext->ext_iomap = (caddr_t)ext + offset;
307 	ext->ext_vm86.vm86_intmap = (caddr_t)ext + offset - 32;
308 
309 	addr = (u_long *)ext->ext_vm86.vm86_intmap;
310 	for (i = 0; i < (ctob(IOPAGES) + 32 + 16) / sizeof(u_long); i++)
311 		*addr++ = ~0;
312 
313 	ssd.ssd_base = (unsigned)&ext->ext_tss;
314 	ssd.ssd_limit -= ((unsigned)&ext->ext_tss - (unsigned)ext);
315 	ssdtosd(&ssd, &ext->ext_tssd);
316 
317 	KASSERT(td == curthread, ("giving TSS to !curthread"));
318 	KASSERT(td->td_pcb->pcb_ext == 0, ("already have a TSS!"));
319 
320 	/* Switch to the new TSS. */
321 	critical_enter();
322 	ext->ext_tss.tss_esp0 = PCPU_GET(trampstk);
323 	td->td_pcb->pcb_ext = ext;
324 	PCPU_SET(private_tss, 1);
325 	*PCPU_GET(tss_gdt) = ext->ext_tssd;
326 	ltr(GSEL(GPROC0_SEL, SEL_KPL));
327 	critical_exit();
328 
329 	return 0;
330 }
331 
332 int
i386_set_ioperm(struct thread * td,struct i386_ioperm_args * uap)333 i386_set_ioperm(struct thread *td, struct i386_ioperm_args *uap)
334 {
335 	char *iomap;
336 	u_int i;
337 	int error;
338 
339 	if ((error = priv_check(td, PRIV_IO)) != 0)
340 		return (error);
341 	if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
342 		return (error);
343 	/*
344 	 * XXX
345 	 * While this is restricted to root, we should probably figure out
346 	 * whether any other driver is using this i/o address, as so not to
347 	 * cause confusion.  This probably requires a global 'usage registry'.
348 	 */
349 
350 	if (td->td_pcb->pcb_ext == 0)
351 		if ((error = i386_extend_pcb(td)) != 0)
352 			return (error);
353 	iomap = (char *)td->td_pcb->pcb_ext->ext_iomap;
354 
355 	if (uap->start > uap->start + uap->length ||
356 	    uap->start + uap->length > IOPAGES * PAGE_SIZE * NBBY)
357 		return (EINVAL);
358 
359 	for (i = uap->start; i < uap->start + uap->length; i++) {
360 		if (uap->enable)
361 			iomap[i >> 3] &= ~(1 << (i & 7));
362 		else
363 			iomap[i >> 3] |= (1 << (i & 7));
364 	}
365 	return (error);
366 }
367 
368 int
i386_get_ioperm(struct thread * td,struct i386_ioperm_args * uap)369 i386_get_ioperm(struct thread *td, struct i386_ioperm_args *uap)
370 {
371 	int i, state;
372 	char *iomap;
373 
374 	if (uap->start >= IOPAGES * PAGE_SIZE * NBBY)
375 		return (EINVAL);
376 
377 	if (td->td_pcb->pcb_ext == 0) {
378 		uap->length = 0;
379 		goto done;
380 	}
381 
382 	iomap = (char *)td->td_pcb->pcb_ext->ext_iomap;
383 
384 	i = uap->start;
385 	state = (iomap[i >> 3] >> (i & 7)) & 1;
386 	uap->enable = !state;
387 	uap->length = 1;
388 
389 	for (i = uap->start + 1; i < IOPAGES * PAGE_SIZE * NBBY; i++) {
390 		if (state != ((iomap[i >> 3] >> (i & 7)) & 1))
391 			break;
392 		uap->length++;
393 	}
394 
395 done:
396 	return (0);
397 }
398 
399 /*
400  * Update the GDT entry pointing to the LDT to point to the LDT of the
401  * current process. Manage dt_lock holding/unholding autonomously.
402  */
403 static void
set_user_ldt_locked(struct mdproc * mdp)404 set_user_ldt_locked(struct mdproc *mdp)
405 {
406 	struct proc_ldt *pldt;
407 	int gdt_idx;
408 
409 	mtx_assert(&dt_lock, MA_OWNED);
410 
411 	pldt = mdp->md_ldt;
412 	gdt_idx = GUSERLDT_SEL;
413 	gdt_idx += PCPU_GET(cpuid) * NGDT;	/* always 0 on UP */
414 	gdt[gdt_idx].sd = pldt->ldt_sd;
415 	lldt(GSEL(GUSERLDT_SEL, SEL_KPL));
416 	PCPU_SET(currentldt, GSEL(GUSERLDT_SEL, SEL_KPL));
417 }
418 
419 void
set_user_ldt(struct mdproc * mdp)420 set_user_ldt(struct mdproc *mdp)
421 {
422 
423 	mtx_lock_spin(&dt_lock);
424 	set_user_ldt_locked(mdp);
425 	mtx_unlock_spin(&dt_lock);
426 }
427 
428 #ifdef SMP
429 static void
set_user_ldt_rv(void * arg)430 set_user_ldt_rv(void *arg)
431 {
432 	struct proc *p;
433 
434 	p = curproc;
435 	if (arg == p->p_vmspace)
436 		set_user_ldt(&p->p_md);
437 }
438 #endif
439 
440 /*
441  * dt_lock must be held. Returns with dt_lock held.
442  */
443 struct proc_ldt *
user_ldt_alloc(struct mdproc * mdp,int len)444 user_ldt_alloc(struct mdproc *mdp, int len)
445 {
446 	struct proc_ldt *pldt, *new_ldt;
447 
448 	mtx_assert(&dt_lock, MA_OWNED);
449 	mtx_unlock_spin(&dt_lock);
450 	new_ldt = malloc(sizeof(struct proc_ldt), M_SUBPROC, M_WAITOK);
451 
452 	new_ldt->ldt_len = len = NEW_MAX_LD(len);
453 	new_ldt->ldt_base = pmap_trm_alloc(len * sizeof(union descriptor),
454 	    M_WAITOK | M_ZERO);
455 	new_ldt->ldt_refcnt = 1;
456 	new_ldt->ldt_active = 0;
457 
458 	mtx_lock_spin(&dt_lock);
459 	gdt_segs[GUSERLDT_SEL].ssd_base = (unsigned)new_ldt->ldt_base;
460 	gdt_segs[GUSERLDT_SEL].ssd_limit = len * sizeof(union descriptor) - 1;
461 	ssdtosd(&gdt_segs[GUSERLDT_SEL], &new_ldt->ldt_sd);
462 
463 	if ((pldt = mdp->md_ldt) != NULL) {
464 		if (len > pldt->ldt_len)
465 			len = pldt->ldt_len;
466 		bcopy(pldt->ldt_base, new_ldt->ldt_base,
467 		    len * sizeof(union descriptor));
468 	} else
469 		bcopy(ldt, new_ldt->ldt_base, sizeof(union descriptor) * NLDT);
470 
471 	return (new_ldt);
472 }
473 
474 /*
475  * Must be called with dt_lock held.  Returns with dt_lock unheld.
476  */
477 void
user_ldt_free(struct thread * td)478 user_ldt_free(struct thread *td)
479 {
480 	struct mdproc *mdp;
481 	struct proc_ldt *pldt;
482 
483 	mtx_assert(&dt_lock, MA_OWNED);
484 	mdp = &td->td_proc->p_md;
485 	if ((pldt = mdp->md_ldt) == NULL) {
486 		mtx_unlock_spin(&dt_lock);
487 		return;
488 	}
489 
490 	if (td == curthread) {
491 		lldt(_default_ldt);
492 		PCPU_SET(currentldt, _default_ldt);
493 	}
494 
495 	mdp->md_ldt = NULL;
496 	user_ldt_deref(pldt);
497 }
498 
499 void
user_ldt_deref(struct proc_ldt * pldt)500 user_ldt_deref(struct proc_ldt *pldt)
501 {
502 
503 	mtx_assert(&dt_lock, MA_OWNED);
504 	if (--pldt->ldt_refcnt == 0) {
505 		mtx_unlock_spin(&dt_lock);
506 		pmap_trm_free(pldt->ldt_base, pldt->ldt_len *
507 		    sizeof(union descriptor));
508 		free(pldt, M_SUBPROC);
509 	} else
510 		mtx_unlock_spin(&dt_lock);
511 }
512 
513 /*
514  * Note for the authors of compat layers (linux, etc): copyout() in
515  * the function below is not a problem since it presents data in
516  * arch-specific format (i.e. i386-specific in this case), not in
517  * the OS-specific one.
518  */
519 int
i386_get_ldt(struct thread * td,struct i386_ldt_args * uap)520 i386_get_ldt(struct thread *td, struct i386_ldt_args *uap)
521 {
522 	struct proc_ldt *pldt;
523 	char *data;
524 	u_int nldt, num;
525 	int error;
526 
527 #ifdef DEBUG
528 	printf("i386_get_ldt: start=%u num=%u descs=%p\n",
529 	    uap->start, uap->num, (void *)uap->descs);
530 #endif
531 
532 	num = min(uap->num, MAX_LD);
533 	data = malloc(num * sizeof(union descriptor), M_TEMP, M_WAITOK);
534 	mtx_lock_spin(&dt_lock);
535 	pldt = td->td_proc->p_md.md_ldt;
536 	nldt = pldt != NULL ? pldt->ldt_len : NLDT;
537 	if (uap->start >= nldt) {
538 		num = 0;
539 	} else {
540 		num = min(num, nldt - uap->start);
541 		bcopy(pldt != NULL ?
542 		    &((union descriptor *)(pldt->ldt_base))[uap->start] :
543 		    &ldt[uap->start], data, num * sizeof(union descriptor));
544 	}
545 	mtx_unlock_spin(&dt_lock);
546 	error = copyout(data, uap->descs, num * sizeof(union descriptor));
547 	if (error == 0)
548 		td->td_retval[0] = num;
549 	free(data, M_TEMP);
550 	return (error);
551 }
552 
553 int
i386_set_ldt(struct thread * td,struct i386_ldt_args * uap,union descriptor * descs)554 i386_set_ldt(struct thread *td, struct i386_ldt_args *uap,
555     union descriptor *descs)
556 {
557 	struct mdproc *mdp;
558 	struct proc_ldt *pldt;
559 	union descriptor *dp;
560 	u_int largest_ld, i;
561 	int error;
562 
563 #ifdef DEBUG
564 	printf("i386_set_ldt: start=%u num=%u descs=%p\n",
565 	    uap->start, uap->num, (void *)uap->descs);
566 #endif
567 	error = 0;
568 	mdp = &td->td_proc->p_md;
569 
570 	if (descs == NULL) {
571 		/* Free descriptors */
572 		if (uap->start == 0 && uap->num == 0) {
573 			/*
574 			 * Treat this as a special case, so userland needn't
575 			 * know magic number NLDT.
576 			 */
577 			uap->start = NLDT;
578 			uap->num = MAX_LD - NLDT;
579 		}
580 		mtx_lock_spin(&dt_lock);
581 		if ((pldt = mdp->md_ldt) == NULL ||
582 		    uap->start >= pldt->ldt_len) {
583 			mtx_unlock_spin(&dt_lock);
584 			return (0);
585 		}
586 		largest_ld = uap->start + uap->num;
587 		if (largest_ld > pldt->ldt_len)
588 			largest_ld = pldt->ldt_len;
589 		for (i = uap->start; i < largest_ld; i++)
590 			atomic_store_rel_64(&((uint64_t *)(pldt->ldt_base))[i],
591 			    0);
592 		mtx_unlock_spin(&dt_lock);
593 		return (0);
594 	}
595 
596 	if (uap->start != LDT_AUTO_ALLOC || uap->num != 1) {
597 		/* verify range of descriptors to modify */
598 		largest_ld = uap->start + uap->num;
599 		if (uap->start >= MAX_LD || largest_ld > MAX_LD)
600 			return (EINVAL);
601 	}
602 
603 	/* Check descriptors for access violations */
604 	for (i = 0; i < uap->num; i++) {
605 		dp = &descs[i];
606 
607 		switch (dp->sd.sd_type) {
608 		case SDT_SYSNULL:	/* system null */
609 			dp->sd.sd_p = 0;
610 			break;
611 		case SDT_SYS286TSS: /* system 286 TSS available */
612 		case SDT_SYSLDT:    /* system local descriptor table */
613 		case SDT_SYS286BSY: /* system 286 TSS busy */
614 		case SDT_SYSTASKGT: /* system task gate */
615 		case SDT_SYS286IGT: /* system 286 interrupt gate */
616 		case SDT_SYS286TGT: /* system 286 trap gate */
617 		case SDT_SYSNULL2:  /* undefined by Intel */
618 		case SDT_SYS386TSS: /* system 386 TSS available */
619 		case SDT_SYSNULL3:  /* undefined by Intel */
620 		case SDT_SYS386BSY: /* system 386 TSS busy */
621 		case SDT_SYSNULL4:  /* undefined by Intel */
622 		case SDT_SYS386IGT: /* system 386 interrupt gate */
623 		case SDT_SYS386TGT: /* system 386 trap gate */
624 		case SDT_SYS286CGT: /* system 286 call gate */
625 		case SDT_SYS386CGT: /* system 386 call gate */
626 			return (EACCES);
627 
628 		/* memory segment types */
629 		case SDT_MEMEC:   /* memory execute only conforming */
630 		case SDT_MEMEAC:  /* memory execute only accessed conforming */
631 		case SDT_MEMERC:  /* memory execute read conforming */
632 		case SDT_MEMERAC: /* memory execute read accessed conforming */
633 			 /* Must be "present" if executable and conforming. */
634 			if (dp->sd.sd_p == 0)
635 				return (EACCES);
636 			break;
637 		case SDT_MEMRO:   /* memory read only */
638 		case SDT_MEMROA:  /* memory read only accessed */
639 		case SDT_MEMRW:   /* memory read write */
640 		case SDT_MEMRWA:  /* memory read write accessed */
641 		case SDT_MEMROD:  /* memory read only expand dwn limit */
642 		case SDT_MEMRODA: /* memory read only expand dwn lim accessed */
643 		case SDT_MEMRWD:  /* memory read write expand dwn limit */
644 		case SDT_MEMRWDA: /* memory read write expand dwn lim acessed */
645 		case SDT_MEME:    /* memory execute only */
646 		case SDT_MEMEA:   /* memory execute only accessed */
647 		case SDT_MEMER:   /* memory execute read */
648 		case SDT_MEMERA:  /* memory execute read accessed */
649 			break;
650 		default:
651 			return (EINVAL);
652 		}
653 
654 		/* Only user (ring-3) descriptors may be present. */
655 		if (dp->sd.sd_p != 0 && dp->sd.sd_dpl != SEL_UPL)
656 			return (EACCES);
657 	}
658 
659 	if (uap->start == LDT_AUTO_ALLOC && uap->num == 1) {
660 		/* Allocate a free slot */
661 		mtx_lock_spin(&dt_lock);
662 		if ((pldt = mdp->md_ldt) == NULL) {
663 			if ((error = i386_ldt_grow(td, NLDT + 1))) {
664 				mtx_unlock_spin(&dt_lock);
665 				return (error);
666 			}
667 			pldt = mdp->md_ldt;
668 		}
669 again:
670 		/*
671 		 * start scanning a bit up to leave room for NVidia and
672 		 * Wine, which still user the "Blat" method of allocation.
673 		 */
674 		dp = &((union descriptor *)(pldt->ldt_base))[NLDT];
675 		for (i = NLDT; i < pldt->ldt_len; ++i) {
676 			if (dp->sd.sd_type == SDT_SYSNULL)
677 				break;
678 			dp++;
679 		}
680 		if (i >= pldt->ldt_len) {
681 			if ((error = i386_ldt_grow(td, pldt->ldt_len+1))) {
682 				mtx_unlock_spin(&dt_lock);
683 				return (error);
684 			}
685 			goto again;
686 		}
687 		uap->start = i;
688 		error = i386_set_ldt_data(td, i, 1, descs);
689 		mtx_unlock_spin(&dt_lock);
690 	} else {
691 		largest_ld = uap->start + uap->num;
692 		mtx_lock_spin(&dt_lock);
693 		if (!(error = i386_ldt_grow(td, largest_ld))) {
694 			error = i386_set_ldt_data(td, uap->start, uap->num,
695 			    descs);
696 		}
697 		mtx_unlock_spin(&dt_lock);
698 	}
699 	if (error == 0)
700 		td->td_retval[0] = uap->start;
701 	return (error);
702 }
703 
704 static int
i386_set_ldt_data(struct thread * td,int start,int num,union descriptor * descs)705 i386_set_ldt_data(struct thread *td, int start, int num,
706     union descriptor *descs)
707 {
708 	struct mdproc *mdp;
709 	struct proc_ldt *pldt;
710 	uint64_t *dst, *src;
711 	int i;
712 
713 	mtx_assert(&dt_lock, MA_OWNED);
714 
715 	mdp = &td->td_proc->p_md;
716 	pldt = mdp->md_ldt;
717 	dst = (uint64_t *)(pldt->ldt_base);
718 	src = (uint64_t *)descs;
719 
720 	/*
721 	 * Atomic(9) is used only to get 64bit atomic store with
722 	 * cmpxchg8b when available.  There is no op without release
723 	 * semantic.
724 	 */
725 	for (i = 0; i < num; i++)
726 		atomic_store_rel_64(&dst[start + i], src[i]);
727 	return (0);
728 }
729 
730 static int
i386_ldt_grow(struct thread * td,int len)731 i386_ldt_grow(struct thread *td, int len)
732 {
733 	struct mdproc *mdp;
734 	struct proc_ldt *new_ldt, *pldt;
735 	caddr_t old_ldt_base;
736 	int old_ldt_len;
737 
738 	mtx_assert(&dt_lock, MA_OWNED);
739 
740 	if (len > MAX_LD)
741 		return (ENOMEM);
742 	if (len < NLDT + 1)
743 		len = NLDT + 1;
744 
745 	mdp = &td->td_proc->p_md;
746 	old_ldt_base = NULL_LDT_BASE;
747 	old_ldt_len = 0;
748 
749 	/* Allocate a user ldt. */
750 	if ((pldt = mdp->md_ldt) == NULL || len > pldt->ldt_len) {
751 		new_ldt = user_ldt_alloc(mdp, len);
752 		if (new_ldt == NULL)
753 			return (ENOMEM);
754 		pldt = mdp->md_ldt;
755 
756 		if (pldt != NULL) {
757 			if (new_ldt->ldt_len <= pldt->ldt_len) {
758 				/*
759 				 * We just lost the race for allocation, so
760 				 * free the new object and return.
761 				 */
762 				mtx_unlock_spin(&dt_lock);
763 				pmap_trm_free(new_ldt->ldt_base,
764 				   new_ldt->ldt_len * sizeof(union descriptor));
765 				free(new_ldt, M_SUBPROC);
766 				mtx_lock_spin(&dt_lock);
767 				return (0);
768 			}
769 
770 			/*
771 			 * We have to substitute the current LDT entry for
772 			 * curproc with the new one since its size grew.
773 			 */
774 			old_ldt_base = pldt->ldt_base;
775 			old_ldt_len = pldt->ldt_len;
776 			pldt->ldt_sd = new_ldt->ldt_sd;
777 			pldt->ldt_base = new_ldt->ldt_base;
778 			pldt->ldt_len = new_ldt->ldt_len;
779 		} else
780 			mdp->md_ldt = pldt = new_ldt;
781 #ifdef SMP
782 		/*
783 		 * Signal other cpus to reload ldt.  We need to unlock dt_lock
784 		 * here because other CPU will contest on it since their
785 		 * curthreads won't hold the lock and will block when trying
786 		 * to acquire it.
787 		 */
788 		mtx_unlock_spin(&dt_lock);
789 		smp_rendezvous(NULL, set_user_ldt_rv, NULL,
790 		    td->td_proc->p_vmspace);
791 #else
792 		set_user_ldt_locked(&td->td_proc->p_md);
793 		mtx_unlock_spin(&dt_lock);
794 #endif
795 		if (old_ldt_base != NULL_LDT_BASE) {
796 			pmap_trm_free(old_ldt_base, old_ldt_len *
797 			    sizeof(union descriptor));
798 			free(new_ldt, M_SUBPROC);
799 		}
800 		mtx_lock_spin(&dt_lock);
801 	}
802 	return (0);
803 }
804