15b81b6b3SRodney W. Grimes /*- 25b81b6b3SRodney W. Grimes * Copyright (c) 1990 The Regents of the University of California. 35b81b6b3SRodney W. Grimes * All rights reserved. 45b81b6b3SRodney W. Grimes * 55b81b6b3SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 65b81b6b3SRodney W. Grimes * modification, are permitted provided that the following conditions 75b81b6b3SRodney W. Grimes * are met: 85b81b6b3SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 95b81b6b3SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 105b81b6b3SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 115b81b6b3SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 125b81b6b3SRodney W. Grimes * documentation and/or other materials provided with the distribution. 13846351f8SWarner Losh * 3. Neither the name of the University nor the names of its contributors 145b81b6b3SRodney W. Grimes * may be used to endorse or promote products derived from this software 155b81b6b3SRodney W. Grimes * without specific prior written permission. 165b81b6b3SRodney W. Grimes * 175b81b6b3SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 185b81b6b3SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 195b81b6b3SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 205b81b6b3SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 215b81b6b3SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 225b81b6b3SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 235b81b6b3SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 245b81b6b3SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 255b81b6b3SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 265b81b6b3SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 275b81b6b3SRodney W. Grimes * SUCH DAMAGE. 285b81b6b3SRodney W. Grimes * 2947cacd38SRodney W. Grimes * from: @(#)sys_machdep.c 5.5 (Berkeley) 1/19/91 305b81b6b3SRodney W. Grimes */ 315b81b6b3SRodney W. Grimes 329676a785SDavid E. O'Brien #include <sys/cdefs.h> 339676a785SDavid E. O'Brien __FBSDID("$FreeBSD$"); 349676a785SDavid E. O'Brien 3524c1c3bfSJonathan Anderson #include "opt_capsicum.h" 36b40ce416SJulian Elischer #include "opt_kstack_pages.h" 37268bdb43SPeter Wemm 38f540b106SGarrett Wollman #include <sys/param.h> 394a144410SRobert Watson #include <sys/capsicum.h> 40f540b106SGarrett Wollman #include <sys/systm.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 7205dfa22fSAttilio Rao static void set_user_ldt_rv(struct vmspace *vmsp); 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 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 942f1ba63bSBruce Evans #ifndef _SYS_SYSPROTO_H_ 95812a11a2SDavid Greenman struct sysarch_args { 96812a11a2SDavid Greenman int op; 97812a11a2SDavid Greenman char *parms; 9801ae5b20SDavid Greenman }; 992f1ba63bSBruce Evans #endif 10001ae5b20SDavid Greenman 101812a11a2SDavid Greenman int 1023e85b721SEd Maste sysarch(struct thread *td, struct sysarch_args *uap) 1035b81b6b3SRodney W. Grimes { 1047ff022c4SJohn Baldwin int error; 10584569dffSMaxim Sobolev union descriptor *lp; 10684569dffSMaxim Sobolev union { 10784569dffSMaxim Sobolev struct i386_ldt_args largs; 10884569dffSMaxim Sobolev struct i386_ioperm_args iargs; 109824fc460SJohn Baldwin struct i386_get_xfpustate xfpu; 11084569dffSMaxim Sobolev } kargs; 111e0ab2c6dSPeter Wemm uint32_t base; 112e0ab2c6dSPeter Wemm struct segment_descriptor sd, *sdp; 11384569dffSMaxim Sobolev 11414961ba7SRobert Watson AUDIT_ARG_CMD(uap->op); 11574b5505eSRobert Watson 11624c1c3bfSJonathan Anderson #ifdef CAPABILITY_MODE 11774b5505eSRobert Watson /* 11812bc222eSJonathan Anderson * When adding new operations, add a new case statement here to 11912bc222eSJonathan Anderson * explicitly indicate whether or not the operation is safe to 12012bc222eSJonathan Anderson * perform in capability mode. 12174b5505eSRobert Watson */ 12274b5505eSRobert Watson if (IN_CAPABILITY_MODE(td)) { 12374b5505eSRobert Watson switch (uap->op) { 12474b5505eSRobert Watson case I386_GET_LDT: 12574b5505eSRobert Watson case I386_SET_LDT: 12674b5505eSRobert Watson case I386_GET_IOPERM: 12774b5505eSRobert Watson case I386_GET_FSBASE: 12874b5505eSRobert Watson case I386_SET_FSBASE: 12974b5505eSRobert Watson case I386_GET_GSBASE: 13074b5505eSRobert Watson case I386_SET_GSBASE: 131824fc460SJohn Baldwin case I386_GET_XFPUSTATE: 13274b5505eSRobert Watson break; 13374b5505eSRobert Watson 13474b5505eSRobert Watson case I386_SET_IOPERM: 13574b5505eSRobert Watson default: 136a417d4a4SDag-Erling Smørgrav #ifdef KTRACE 137a417d4a4SDag-Erling Smørgrav if (KTRPOINT(td, KTR_CAPFAIL)) 1383fded357SPawel Jakub Dawidek ktrcapfail(CAPFAIL_SYSCALL, NULL, NULL); 139a417d4a4SDag-Erling Smørgrav #endif 14074b5505eSRobert Watson return (ECAPMODE); 14174b5505eSRobert Watson } 14274b5505eSRobert Watson } 14374b5505eSRobert Watson #endif 14474b5505eSRobert Watson 14584569dffSMaxim Sobolev switch (uap->op) { 14684569dffSMaxim Sobolev case I386_GET_IOPERM: 14784569dffSMaxim Sobolev case I386_SET_IOPERM: 14884569dffSMaxim Sobolev if ((error = copyin(uap->parms, &kargs.iargs, 14984569dffSMaxim Sobolev sizeof(struct i386_ioperm_args))) != 0) 15084569dffSMaxim Sobolev return (error); 15184569dffSMaxim Sobolev break; 15284569dffSMaxim Sobolev case I386_GET_LDT: 15384569dffSMaxim Sobolev case I386_SET_LDT: 15484569dffSMaxim Sobolev if ((error = copyin(uap->parms, &kargs.largs, 15584569dffSMaxim Sobolev sizeof(struct i386_ldt_args))) != 0) 15684569dffSMaxim Sobolev return (error); 1578a4d2b06SDavid Schultz if (kargs.largs.num > MAX_LD || kargs.largs.num <= 0) 1588a4d2b06SDavid Schultz return (EINVAL); 15984569dffSMaxim Sobolev break; 160824fc460SJohn Baldwin case I386_GET_XFPUSTATE: 161824fc460SJohn Baldwin if ((error = copyin(uap->parms, &kargs.xfpu, 162824fc460SJohn Baldwin sizeof(struct i386_get_xfpustate))) != 0) 163824fc460SJohn Baldwin return (error); 164824fc460SJohn Baldwin break; 16584569dffSMaxim Sobolev default: 16684569dffSMaxim Sobolev break; 16784569dffSMaxim Sobolev } 1685b81b6b3SRodney W. Grimes 169812a11a2SDavid Greenman switch (uap->op) { 170812a11a2SDavid Greenman case I386_GET_LDT: 17184569dffSMaxim Sobolev error = i386_get_ldt(td, &kargs.largs); 1725b81b6b3SRodney W. Grimes break; 173812a11a2SDavid Greenman case I386_SET_LDT: 17484569dffSMaxim Sobolev if (kargs.largs.descs != NULL) { 17531241fa7SKonstantin Belousov lp = (union descriptor *)malloc( 1762f84c08eSJeff Roberson kargs.largs.num * sizeof(union descriptor), 17731241fa7SKonstantin Belousov M_TEMP, M_WAITOK); 17884569dffSMaxim Sobolev error = copyin(kargs.largs.descs, lp, 17984569dffSMaxim Sobolev kargs.largs.num * sizeof(union descriptor)); 18084569dffSMaxim Sobolev if (error == 0) 18184569dffSMaxim Sobolev error = i386_set_ldt(td, &kargs.largs, lp); 18231241fa7SKonstantin Belousov free(lp, M_TEMP); 18384569dffSMaxim Sobolev } else { 18484569dffSMaxim Sobolev error = i386_set_ldt(td, &kargs.largs, NULL); 18584569dffSMaxim Sobolev } 1865b81b6b3SRodney W. Grimes break; 18748a09cf2SJohn Dyson case I386_GET_IOPERM: 18884569dffSMaxim Sobolev error = i386_get_ioperm(td, &kargs.iargs); 18984569dffSMaxim Sobolev if (error == 0) 19084569dffSMaxim Sobolev error = copyout(&kargs.iargs, uap->parms, 19184569dffSMaxim Sobolev sizeof(struct i386_ioperm_args)); 19248a09cf2SJohn Dyson break; 19348a09cf2SJohn Dyson case I386_SET_IOPERM: 19484569dffSMaxim Sobolev error = i386_set_ioperm(td, &kargs.iargs); 19548a09cf2SJohn Dyson break; 19648a09cf2SJohn Dyson case I386_VM86: 197b40ce416SJulian Elischer error = vm86_sysarch(td, uap->parms); 19848a09cf2SJohn Dyson break; 199e0ab2c6dSPeter Wemm case I386_GET_FSBASE: 200e0ab2c6dSPeter Wemm sdp = &td->td_pcb->pcb_fsd; 201e0ab2c6dSPeter Wemm base = sdp->sd_hibase << 24 | sdp->sd_lobase; 202e0ab2c6dSPeter Wemm error = copyout(&base, uap->parms, sizeof(base)); 203e0ab2c6dSPeter Wemm break; 204e0ab2c6dSPeter Wemm case I386_SET_FSBASE: 205e0ab2c6dSPeter Wemm error = copyin(uap->parms, &base, sizeof(base)); 20606d058bdSKonstantin Belousov if (error == 0) { 207e0ab2c6dSPeter Wemm /* 208e0ab2c6dSPeter Wemm * Construct a descriptor and store it in the pcb for 209e0ab2c6dSPeter Wemm * the next context switch. Also store it in the gdt 210e0ab2c6dSPeter Wemm * so that the load of tf_fs into %fs will activate it 211e0ab2c6dSPeter Wemm * at return to userland. 212e0ab2c6dSPeter Wemm */ 21306d058bdSKonstantin Belousov fill_based_sd(&sd, base); 2144b1fa239SDavid Xu critical_enter(); 215e0ab2c6dSPeter Wemm td->td_pcb->pcb_fsd = sd; 216e0ab2c6dSPeter Wemm PCPU_GET(fsgs_gdt)[0] = sd; 2174b1fa239SDavid Xu critical_exit(); 2189a045ca1SDavid Xu td->td_frame->tf_fs = GSEL(GUFS_SEL, SEL_UPL); 219e0ab2c6dSPeter Wemm } 220e0ab2c6dSPeter Wemm break; 221e0ab2c6dSPeter Wemm case I386_GET_GSBASE: 222e0ab2c6dSPeter Wemm sdp = &td->td_pcb->pcb_gsd; 223e0ab2c6dSPeter Wemm base = sdp->sd_hibase << 24 | sdp->sd_lobase; 224e0ab2c6dSPeter Wemm error = copyout(&base, uap->parms, sizeof(base)); 225e0ab2c6dSPeter Wemm break; 226e0ab2c6dSPeter Wemm case I386_SET_GSBASE: 227e0ab2c6dSPeter Wemm error = copyin(uap->parms, &base, sizeof(base)); 22806d058bdSKonstantin Belousov if (error == 0) { 229e0ab2c6dSPeter Wemm /* 230e0ab2c6dSPeter Wemm * Construct a descriptor and store it in the pcb for 231e0ab2c6dSPeter Wemm * the next context switch. Also store it in the gdt 232e0ab2c6dSPeter Wemm * because we have to do a load_gs() right now. 233e0ab2c6dSPeter Wemm */ 23406d058bdSKonstantin Belousov fill_based_sd(&sd, base); 2354b1fa239SDavid Xu critical_enter(); 236e0ab2c6dSPeter Wemm td->td_pcb->pcb_gsd = sd; 237e0ab2c6dSPeter Wemm PCPU_GET(fsgs_gdt)[1] = sd; 2384b1fa239SDavid Xu critical_exit(); 239e0ab2c6dSPeter Wemm load_gs(GSEL(GUGS_SEL, SEL_UPL)); 240e0ab2c6dSPeter Wemm } 241e0ab2c6dSPeter Wemm break; 242824fc460SJohn Baldwin case I386_GET_XFPUSTATE: 243824fc460SJohn Baldwin if (kargs.xfpu.len > cpu_max_ext_state_size - 244824fc460SJohn Baldwin sizeof(union savefpu)) 245824fc460SJohn Baldwin return (EINVAL); 246824fc460SJohn Baldwin npxgetregs(td); 247824fc460SJohn Baldwin error = copyout((char *)(get_pcb_user_save_td(td) + 1), 248824fc460SJohn Baldwin kargs.xfpu.addr, kargs.xfpu.len); 249824fc460SJohn Baldwin break; 250812a11a2SDavid Greenman default: 2517ff022c4SJohn Baldwin error = EINVAL; 252812a11a2SDavid Greenman break; 253812a11a2SDavid Greenman } 254812a11a2SDavid Greenman return (error); 255812a11a2SDavid Greenman } 256da59a31cSDavid Greenman 25748a09cf2SJohn Dyson int 258b40ce416SJulian Elischer i386_extend_pcb(struct thread *td) 25948a09cf2SJohn Dyson { 26048a09cf2SJohn Dyson int i, offset; 26148a09cf2SJohn Dyson u_long *addr; 26248a09cf2SJohn Dyson struct pcb_ext *ext; 26348a09cf2SJohn Dyson struct soft_segment_descriptor ssd = { 26448a09cf2SJohn Dyson 0, /* segment base address (overwritten) */ 26548a09cf2SJohn Dyson ctob(IOPAGES + 1) - 1, /* length */ 26648a09cf2SJohn Dyson SDT_SYS386TSS, /* segment type */ 26748a09cf2SJohn Dyson 0, /* priority level */ 26848a09cf2SJohn Dyson 1, /* descriptor present */ 26948a09cf2SJohn Dyson 0, 0, 27048a09cf2SJohn Dyson 0, /* default 32 size */ 27148a09cf2SJohn Dyson 0 /* granularity */ 27248a09cf2SJohn Dyson }; 27348a09cf2SJohn Dyson 2745df87b21SJeff Roberson ext = (struct pcb_ext *)kmem_malloc(kernel_arena, ctob(IOPAGES+1), 27531241fa7SKonstantin Belousov M_WAITOK | M_ZERO); 276b40ce416SJulian Elischer /* -16 is so we can convert a trapframe into vm86trapframe inplace */ 277d9977530SJohn Baldwin ext->ext_tss.tss_esp0 = (vm_offset_t)td->td_pcb - 16; 27848a09cf2SJohn Dyson ext->ext_tss.tss_ss0 = GSEL(GDATA_SEL, SEL_KPL); 27948a09cf2SJohn Dyson /* 28048a09cf2SJohn Dyson * The last byte of the i/o map must be followed by an 0xff byte. 28148a09cf2SJohn Dyson * We arbitrarily allocate 16 bytes here, to keep the starting 28248a09cf2SJohn Dyson * address on a doubleword boundary. 28348a09cf2SJohn Dyson */ 28448a09cf2SJohn Dyson offset = PAGE_SIZE - 16; 28548a09cf2SJohn Dyson ext->ext_tss.tss_ioopt = 28648a09cf2SJohn Dyson (offset - ((unsigned)&ext->ext_tss - (unsigned)ext)) << 16; 28748a09cf2SJohn Dyson ext->ext_iomap = (caddr_t)ext + offset; 28848a09cf2SJohn Dyson ext->ext_vm86.vm86_intmap = (caddr_t)ext + offset - 32; 28948a09cf2SJohn Dyson 29048a09cf2SJohn Dyson addr = (u_long *)ext->ext_vm86.vm86_intmap; 29148a09cf2SJohn Dyson for (i = 0; i < (ctob(IOPAGES) + 32 + 16) / sizeof(u_long); i++) 29248a09cf2SJohn Dyson *addr++ = ~0; 29348a09cf2SJohn Dyson 29448a09cf2SJohn Dyson ssd.ssd_base = (unsigned)&ext->ext_tss; 29548a09cf2SJohn Dyson ssd.ssd_limit -= ((unsigned)&ext->ext_tss - (unsigned)ext); 29648a09cf2SJohn Dyson ssdtosd(&ssd, &ext->ext_tssd); 29748a09cf2SJohn Dyson 298f726a873SJohn Baldwin KASSERT(td == curthread, ("giving TSS to !curthread")); 299b40ce416SJulian Elischer KASSERT(td->td_pcb->pcb_ext == 0, ("already have a TSS!")); 300f726a873SJohn Baldwin 301f726a873SJohn Baldwin /* Switch to the new TSS. */ 3021bfa9108SDavid Xu critical_enter(); 303b40ce416SJulian Elischer td->td_pcb->pcb_ext = ext; 3041bfa9108SDavid Xu PCPU_SET(private_tss, 1); 305f726a873SJohn Baldwin *PCPU_GET(tss_gdt) = ext->ext_tssd; 306f726a873SJohn Baldwin ltr(GSEL(GPROC0_SEL, SEL_KPL)); 3071bfa9108SDavid Xu critical_exit(); 30848a09cf2SJohn Dyson 30948a09cf2SJohn Dyson return 0; 31048a09cf2SJohn Dyson } 31148a09cf2SJohn Dyson 31284569dffSMaxim Sobolev int 31384569dffSMaxim Sobolev i386_set_ioperm(td, uap) 314b40ce416SJulian Elischer struct thread *td; 31584569dffSMaxim Sobolev struct i386_ioperm_args *uap; 31648a09cf2SJohn Dyson { 31748a09cf2SJohn Dyson char *iomap; 3180bfad8e4SKonstantin Belousov u_int i; 3190bfad8e4SKonstantin Belousov int error; 32048a09cf2SJohn Dyson 321acd3428bSRobert Watson if ((error = priv_check(td, PRIV_IO)) != 0) 32248a09cf2SJohn Dyson return (error); 323a854ed98SJohn Baldwin if ((error = securelevel_gt(td->td_ucred, 0)) != 0) 324330e7889SRobert Watson return (error); 32548a09cf2SJohn Dyson /* 32648a09cf2SJohn Dyson * XXX 32748a09cf2SJohn Dyson * While this is restricted to root, we should probably figure out 32848a09cf2SJohn Dyson * whether any other driver is using this i/o address, as so not to 32948a09cf2SJohn Dyson * cause confusion. This probably requires a global 'usage registry'. 33048a09cf2SJohn Dyson */ 33148a09cf2SJohn Dyson 332b40ce416SJulian Elischer if (td->td_pcb->pcb_ext == 0) 333b40ce416SJulian Elischer if ((error = i386_extend_pcb(td)) != 0) 33448a09cf2SJohn Dyson return (error); 335b40ce416SJulian Elischer iomap = (char *)td->td_pcb->pcb_ext->ext_iomap; 33648a09cf2SJohn Dyson 3370bfad8e4SKonstantin Belousov if (uap->start > uap->start + uap->length || 3380bfad8e4SKonstantin Belousov uap->start + uap->length > IOPAGES * PAGE_SIZE * NBBY) 33948a09cf2SJohn Dyson return (EINVAL); 34048a09cf2SJohn Dyson 34184569dffSMaxim Sobolev for (i = uap->start; i < uap->start + uap->length; i++) { 34284569dffSMaxim Sobolev if (uap->enable) 34348a09cf2SJohn Dyson iomap[i >> 3] &= ~(1 << (i & 7)); 34448a09cf2SJohn Dyson else 34548a09cf2SJohn Dyson iomap[i >> 3] |= (1 << (i & 7)); 34648a09cf2SJohn Dyson } 34748a09cf2SJohn Dyson return (error); 34848a09cf2SJohn Dyson } 34948a09cf2SJohn Dyson 35084569dffSMaxim Sobolev int 35184569dffSMaxim Sobolev i386_get_ioperm(td, uap) 352b40ce416SJulian Elischer struct thread *td; 35384569dffSMaxim Sobolev struct i386_ioperm_args *uap; 35448a09cf2SJohn Dyson { 35584569dffSMaxim Sobolev int i, state; 35648a09cf2SJohn Dyson char *iomap; 35748a09cf2SJohn Dyson 35884569dffSMaxim Sobolev if (uap->start >= IOPAGES * PAGE_SIZE * NBBY) 3599729f279SBruce Evans return (EINVAL); 36048a09cf2SJohn Dyson 361b40ce416SJulian Elischer if (td->td_pcb->pcb_ext == 0) { 36284569dffSMaxim Sobolev uap->length = 0; 36348a09cf2SJohn Dyson goto done; 36448a09cf2SJohn Dyson } 36548a09cf2SJohn Dyson 366b40ce416SJulian Elischer iomap = (char *)td->td_pcb->pcb_ext->ext_iomap; 36748a09cf2SJohn Dyson 36884569dffSMaxim Sobolev i = uap->start; 36938861023SPeter Wemm state = (iomap[i >> 3] >> (i & 7)) & 1; 37084569dffSMaxim Sobolev uap->enable = !state; 37184569dffSMaxim Sobolev uap->length = 1; 37248a09cf2SJohn Dyson 37384569dffSMaxim Sobolev for (i = uap->start + 1; i < IOPAGES * PAGE_SIZE * NBBY; i++) { 37448a09cf2SJohn Dyson if (state != ((iomap[i >> 3] >> (i & 7)) & 1)) 37548a09cf2SJohn Dyson break; 37684569dffSMaxim Sobolev uap->length++; 37748a09cf2SJohn Dyson } 37848a09cf2SJohn Dyson 37948a09cf2SJohn Dyson done: 38084569dffSMaxim Sobolev return (0); 38148a09cf2SJohn Dyson } 38248a09cf2SJohn Dyson 3830dbf6d73SJordan K. Hubbard /* 3840dbf6d73SJordan K. Hubbard * Update the GDT entry pointing to the LDT to point to the LDT of the 3850ad5e7f3SJeff Roberson * current process. Manage dt_lock holding/unholding autonomously. 3860dbf6d73SJordan K. Hubbard */ 387da59a31cSDavid Greenman void 38824db0459SJohn Baldwin set_user_ldt(struct mdproc *mdp) 389da59a31cSDavid Greenman { 39024db0459SJohn Baldwin struct proc_ldt *pldt; 3910ad5e7f3SJeff Roberson int dtlocked; 3920ad5e7f3SJeff Roberson 3930ad5e7f3SJeff Roberson dtlocked = 0; 3940ad5e7f3SJeff Roberson if (!mtx_owned(&dt_lock)) { 3950ad5e7f3SJeff Roberson mtx_lock_spin(&dt_lock); 3960ad5e7f3SJeff Roberson dtlocked = 1; 3970ad5e7f3SJeff Roberson } 39891c28bfdSLuoqi Chen 39924db0459SJohn Baldwin pldt = mdp->md_ldt; 4005206bca1SLuoqi Chen #ifdef SMP 40124db0459SJohn Baldwin gdt[PCPU_GET(cpuid) * NGDT + GUSERLDT_SEL].sd = pldt->ldt_sd; 4025206bca1SLuoqi Chen #else 40324db0459SJohn Baldwin gdt[GUSERLDT_SEL].sd = pldt->ldt_sd; 4045206bca1SLuoqi Chen #endif 405da59a31cSDavid Greenman lldt(GSEL(GUSERLDT_SEL, SEL_KPL)); 4064ef34f39SJake Burkholder PCPU_SET(currentldt, GSEL(GUSERLDT_SEL, SEL_KPL)); 4070ad5e7f3SJeff Roberson if (dtlocked) 4080ad5e7f3SJeff Roberson mtx_unlock_spin(&dt_lock); 4091acf256dSJohn Baldwin } 4101acf256dSJohn Baldwin 4115eb6e279SJohn Baldwin #ifdef SMP 4125eb6e279SJohn Baldwin static void 41305dfa22fSAttilio Rao set_user_ldt_rv(struct vmspace *vmsp) 4141acf256dSJohn Baldwin { 41505dfa22fSAttilio Rao struct thread *td; 4161acf256dSJohn Baldwin 41705dfa22fSAttilio Rao td = curthread; 41805dfa22fSAttilio Rao if (vmsp != td->td_proc->p_vmspace) 4191acf256dSJohn Baldwin return; 4201acf256dSJohn Baldwin 42124db0459SJohn Baldwin set_user_ldt(&td->td_proc->p_md); 422da59a31cSDavid Greenman } 4235eb6e279SJohn Baldwin #endif 424da59a31cSDavid Greenman 425df4d012bSJohn Baldwin /* 4260ad5e7f3SJeff Roberson * dt_lock must be held. Returns with dt_lock held. 427df4d012bSJohn Baldwin */ 42824db0459SJohn Baldwin struct proc_ldt * 42924db0459SJohn Baldwin user_ldt_alloc(struct mdproc *mdp, int len) 43091c28bfdSLuoqi Chen { 43124db0459SJohn Baldwin struct proc_ldt *pldt, *new_ldt; 43291c28bfdSLuoqi Chen 4330ad5e7f3SJeff Roberson mtx_assert(&dt_lock, MA_OWNED); 4340ad5e7f3SJeff Roberson mtx_unlock_spin(&dt_lock); 435711d5183SKonstantin Belousov new_ldt = malloc(sizeof(struct proc_ldt), M_SUBPROC, M_WAITOK); 43691c28bfdSLuoqi Chen 43791c28bfdSLuoqi Chen new_ldt->ldt_len = len = NEW_MAX_LD(len); 4385df87b21SJeff Roberson new_ldt->ldt_base = (caddr_t)kmem_malloc(kernel_arena, 43981d7ca77STijl Coosemans len * sizeof(union descriptor), M_WAITOK | M_ZERO); 44005dfa22fSAttilio Rao new_ldt->ldt_refcnt = 1; 44191c28bfdSLuoqi Chen new_ldt->ldt_active = 0; 44291c28bfdSLuoqi Chen 4430ad5e7f3SJeff Roberson mtx_lock_spin(&dt_lock); 44491c28bfdSLuoqi Chen gdt_segs[GUSERLDT_SEL].ssd_base = (unsigned)new_ldt->ldt_base; 44591c28bfdSLuoqi Chen gdt_segs[GUSERLDT_SEL].ssd_limit = len * sizeof(union descriptor) - 1; 44691c28bfdSLuoqi Chen ssdtosd(&gdt_segs[GUSERLDT_SEL], &new_ldt->ldt_sd); 44791c28bfdSLuoqi Chen 4480ad5e7f3SJeff Roberson if ((pldt = mdp->md_ldt) != NULL) { 44924db0459SJohn Baldwin if (len > pldt->ldt_len) 45024db0459SJohn Baldwin len = pldt->ldt_len; 45124db0459SJohn Baldwin bcopy(pldt->ldt_base, new_ldt->ldt_base, 45291c28bfdSLuoqi Chen len * sizeof(union descriptor)); 4530ad5e7f3SJeff Roberson } else 45491c28bfdSLuoqi Chen bcopy(ldt, new_ldt->ldt_base, sizeof(ldt)); 4550ad5e7f3SJeff Roberson 4560ad5e7f3SJeff Roberson return (new_ldt); 45791c28bfdSLuoqi Chen } 45891c28bfdSLuoqi Chen 459df4d012bSJohn Baldwin /* 46002b0a160SAttilio Rao * Must be called with dt_lock held. Returns with dt_lock unheld. 461df4d012bSJohn Baldwin */ 46291c28bfdSLuoqi Chen void 46324db0459SJohn Baldwin user_ldt_free(struct thread *td) 46491c28bfdSLuoqi Chen { 465711d5183SKonstantin Belousov struct mdproc *mdp; 4660ad5e7f3SJeff Roberson struct proc_ldt *pldt; 46791c28bfdSLuoqi Chen 4680ad5e7f3SJeff Roberson mtx_assert(&dt_lock, MA_OWNED); 469711d5183SKonstantin Belousov mdp = &td->td_proc->p_md; 470cb1d78d0SKonstantin Belousov if ((pldt = mdp->md_ldt) == NULL) { 471cb1d78d0SKonstantin Belousov mtx_unlock_spin(&dt_lock); 47291c28bfdSLuoqi Chen return; 473cb1d78d0SKonstantin Belousov } 47491c28bfdSLuoqi Chen 475b494482fSJohn Baldwin if (td == curthread) { 47691c28bfdSLuoqi Chen lldt(_default_ldt); 4774ef34f39SJake Burkholder PCPU_SET(currentldt, _default_ldt); 47891c28bfdSLuoqi Chen } 47991c28bfdSLuoqi Chen 48024db0459SJohn Baldwin mdp->md_ldt = NULL; 4819719da13SKonstantin Belousov user_ldt_deref(pldt); 4829719da13SKonstantin Belousov } 4839719da13SKonstantin Belousov 4849719da13SKonstantin Belousov void 4859719da13SKonstantin Belousov user_ldt_deref(struct proc_ldt *pldt) 4869719da13SKonstantin Belousov { 4879719da13SKonstantin Belousov 4889719da13SKonstantin Belousov mtx_assert(&dt_lock, MA_OWNED); 48905dfa22fSAttilio Rao if (--pldt->ldt_refcnt == 0) { 49002b0a160SAttilio Rao mtx_unlock_spin(&dt_lock); 4915df87b21SJeff Roberson kmem_free(kernel_arena, (vm_offset_t)pldt->ldt_base, 49224db0459SJohn Baldwin pldt->ldt_len * sizeof(union descriptor)); 4931ede983cSDag-Erling Smørgrav free(pldt, M_SUBPROC); 49405dfa22fSAttilio Rao } else 49505dfa22fSAttilio Rao mtx_unlock_spin(&dt_lock); 49691c28bfdSLuoqi Chen } 49791c28bfdSLuoqi Chen 49884569dffSMaxim Sobolev /* 49984569dffSMaxim Sobolev * Note for the authors of compat layers (linux, etc): copyout() in 50084569dffSMaxim Sobolev * the function below is not a problem since it presents data in 50184569dffSMaxim Sobolev * arch-specific format (i.e. i386-specific in this case), not in 50284569dffSMaxim Sobolev * the OS-specific one. 50384569dffSMaxim Sobolev */ 50484569dffSMaxim Sobolev int 50584569dffSMaxim Sobolev i386_get_ldt(td, uap) 506b40ce416SJulian Elischer struct thread *td; 50784569dffSMaxim Sobolev struct i386_ldt_args *uap; 508da59a31cSDavid Greenman { 509da59a31cSDavid Greenman int error = 0; 5100ad5e7f3SJeff Roberson struct proc_ldt *pldt; 511da59a31cSDavid Greenman int nldt, num; 512da59a31cSDavid Greenman union descriptor *lp; 513da59a31cSDavid Greenman 514da59a31cSDavid Greenman #ifdef DEBUG 5151f32d2a7SKonstantin Belousov printf("i386_get_ldt: start=%u num=%u descs=%p\n", 5167a2bb3b8SLuoqi Chen uap->start, uap->num, (void *)uap->descs); 517da59a31cSDavid Greenman #endif 518da59a31cSDavid Greenman 5190ad5e7f3SJeff Roberson mtx_lock_spin(&dt_lock); 5200ad5e7f3SJeff Roberson if ((pldt = td->td_proc->p_md.md_ldt) != NULL) { 52124db0459SJohn Baldwin nldt = pldt->ldt_len; 52224db0459SJohn Baldwin lp = &((union descriptor *)(pldt->ldt_base))[uap->start]; 5230ad5e7f3SJeff Roberson mtx_unlock_spin(&dt_lock); 5240ad5e7f3SJeff Roberson num = min(uap->num, nldt); 525da59a31cSDavid Greenman } else { 5260ad5e7f3SJeff Roberson mtx_unlock_spin(&dt_lock); 527da59a31cSDavid Greenman nldt = sizeof(ldt)/sizeof(ldt[0]); 528da59a31cSDavid Greenman num = min(uap->num, nldt); 529da59a31cSDavid Greenman lp = &ldt[uap->start]; 530da59a31cSDavid Greenman } 5319b2dc15cSColin Percival 5329b2dc15cSColin Percival if ((uap->start > (unsigned int)nldt) || 5339b2dc15cSColin Percival ((unsigned int)num > (unsigned int)nldt) || 5349b2dc15cSColin Percival ((unsigned int)(uap->start + num) > (unsigned int)nldt)) 535da59a31cSDavid Greenman return(EINVAL); 536da59a31cSDavid Greenman 5377a2bb3b8SLuoqi Chen error = copyout(lp, uap->descs, num * sizeof(union descriptor)); 538da59a31cSDavid Greenman if (!error) 539b40ce416SJulian Elischer td->td_retval[0] = num; 540da59a31cSDavid Greenman 541da59a31cSDavid Greenman return(error); 542da59a31cSDavid Greenman } 543da59a31cSDavid Greenman 54484569dffSMaxim Sobolev int 54584569dffSMaxim Sobolev i386_set_ldt(td, uap, descs) 546b40ce416SJulian Elischer struct thread *td; 54784569dffSMaxim Sobolev struct i386_ldt_args *uap; 54884569dffSMaxim Sobolev union descriptor *descs; 549da59a31cSDavid Greenman { 5503cabd93eSKonstantin Belousov int error, i; 5510dbf6d73SJordan K. Hubbard int largest_ld; 55224db0459SJohn Baldwin struct mdproc *mdp = &td->td_proc->p_md; 553bc2e774aSJohn Baldwin struct proc_ldt *pldt; 55484569dffSMaxim Sobolev union descriptor *dp; 555da59a31cSDavid Greenman 5561716a1afSJulian Elischer #ifdef DEBUG 5571f32d2a7SKonstantin Belousov printf("i386_set_ldt: start=%u num=%u descs=%p\n", 5581716a1afSJulian Elischer uap->start, uap->num, (void *)uap->descs); 5591716a1afSJulian Elischer #endif 5603cabd93eSKonstantin Belousov error = 0; 5611716a1afSJulian Elischer 56284569dffSMaxim Sobolev if (descs == NULL) { 563dae8d52dSDavid Xu /* Free descriptors */ 564dae8d52dSDavid Xu if (uap->start == 0 && uap->num == 0) { 565dae8d52dSDavid Xu /* 566dae8d52dSDavid Xu * Treat this as a special case, so userland needn't 567dae8d52dSDavid Xu * know magic number NLDT. 568dae8d52dSDavid Xu */ 569dae8d52dSDavid Xu uap->start = NLDT; 570dae8d52dSDavid Xu uap->num = MAX_LD - NLDT; 5710dbf6d73SJordan K. Hubbard } 5726bd823f3SJohn Baldwin if (uap->num == 0) 573da59a31cSDavid Greenman return (EINVAL); 5740ad5e7f3SJeff Roberson mtx_lock_spin(&dt_lock); 5750ad5e7f3SJeff Roberson if ((pldt = mdp->md_ldt) == NULL || 5760ad5e7f3SJeff Roberson uap->start >= pldt->ldt_len) { 5770ad5e7f3SJeff Roberson mtx_unlock_spin(&dt_lock); 578dae8d52dSDavid Xu return (0); 579dae8d52dSDavid Xu } 580dae8d52dSDavid Xu largest_ld = uap->start + uap->num; 581dae8d52dSDavid Xu if (largest_ld > pldt->ldt_len) 582dae8d52dSDavid Xu largest_ld = pldt->ldt_len; 5833cabd93eSKonstantin Belousov for (i = uap->start; i < largest_ld; i++) 5843cabd93eSKonstantin Belousov atomic_store_rel_64(&((uint64_t *)(pldt->ldt_base))[i], 5853cabd93eSKonstantin Belousov 0); 5860ad5e7f3SJeff Roberson mtx_unlock_spin(&dt_lock); 587dae8d52dSDavid Xu return (0); 588dae8d52dSDavid Xu } 589dae8d52dSDavid Xu 590711d5183SKonstantin Belousov if (uap->start != LDT_AUTO_ALLOC || uap->num != 1) { 591dae8d52dSDavid Xu /* verify range of descriptors to modify */ 592dae8d52dSDavid Xu largest_ld = uap->start + uap->num; 593711d5183SKonstantin Belousov if (uap->start >= MAX_LD || largest_ld > MAX_LD) 594dae8d52dSDavid Xu return (EINVAL); 595dae8d52dSDavid Xu } 596da59a31cSDavid Greenman 597da59a31cSDavid Greenman /* Check descriptors for access violations */ 598dae8d52dSDavid Xu for (i = 0; i < uap->num; i++) { 5998eb8107bSPeter Wemm dp = &descs[i]; 600da59a31cSDavid Greenman 6018eb8107bSPeter Wemm switch (dp->sd.sd_type) { 6020dbf6d73SJordan K. Hubbard case SDT_SYSNULL: /* system null */ 6038eb8107bSPeter Wemm dp->sd.sd_p = 0; 6040dbf6d73SJordan K. Hubbard break; 6050dbf6d73SJordan K. Hubbard case SDT_SYS286TSS: /* system 286 TSS available */ 6060dbf6d73SJordan K. Hubbard case SDT_SYSLDT: /* system local descriptor table */ 6070dbf6d73SJordan K. Hubbard case SDT_SYS286BSY: /* system 286 TSS busy */ 6080dbf6d73SJordan K. Hubbard case SDT_SYSTASKGT: /* system task gate */ 6090dbf6d73SJordan K. Hubbard case SDT_SYS286IGT: /* system 286 interrupt gate */ 6100dbf6d73SJordan K. Hubbard case SDT_SYS286TGT: /* system 286 trap gate */ 6110dbf6d73SJordan K. Hubbard case SDT_SYSNULL2: /* undefined by Intel */ 6120dbf6d73SJordan K. Hubbard case SDT_SYS386TSS: /* system 386 TSS available */ 6130dbf6d73SJordan K. Hubbard case SDT_SYSNULL3: /* undefined by Intel */ 6140dbf6d73SJordan K. Hubbard case SDT_SYS386BSY: /* system 386 TSS busy */ 6150dbf6d73SJordan K. Hubbard case SDT_SYSNULL4: /* undefined by Intel */ 6160dbf6d73SJordan K. Hubbard case SDT_SYS386IGT: /* system 386 interrupt gate */ 6170dbf6d73SJordan K. Hubbard case SDT_SYS386TGT: /* system 386 trap gate */ 6180dbf6d73SJordan K. Hubbard case SDT_SYS286CGT: /* system 286 call gate */ 6190dbf6d73SJordan K. Hubbard case SDT_SYS386CGT: /* system 386 call gate */ 620dae8d52dSDavid Xu return (EACCES); 621da59a31cSDavid Greenman 6220dbf6d73SJordan K. Hubbard /* memory segment types */ 6230dbf6d73SJordan K. Hubbard case SDT_MEMEC: /* memory execute only conforming */ 6240dbf6d73SJordan K. Hubbard case SDT_MEMEAC: /* memory execute only accessed conforming */ 6250dbf6d73SJordan K. Hubbard case SDT_MEMERC: /* memory execute read conforming */ 6260dbf6d73SJordan K. Hubbard case SDT_MEMERAC: /* memory execute read accessed conforming */ 6270dbf6d73SJordan K. Hubbard /* Must be "present" if executable and conforming. */ 62884569dffSMaxim Sobolev if (dp->sd.sd_p == 0) 629da59a31cSDavid Greenman return (EACCES); 630da59a31cSDavid Greenman break; 6310dbf6d73SJordan K. Hubbard case SDT_MEMRO: /* memory read only */ 6320dbf6d73SJordan K. Hubbard case SDT_MEMROA: /* memory read only accessed */ 6330dbf6d73SJordan K. Hubbard case SDT_MEMRW: /* memory read write */ 6340dbf6d73SJordan K. Hubbard case SDT_MEMRWA: /* memory read write accessed */ 6350dbf6d73SJordan K. Hubbard case SDT_MEMROD: /* memory read only expand dwn limit */ 6360dbf6d73SJordan K. Hubbard case SDT_MEMRODA: /* memory read only expand dwn lim accessed */ 6370dbf6d73SJordan K. Hubbard case SDT_MEMRWD: /* memory read write expand dwn limit */ 6380dbf6d73SJordan K. Hubbard case SDT_MEMRWDA: /* memory read write expand dwn lim acessed */ 6390dbf6d73SJordan K. Hubbard case SDT_MEME: /* memory execute only */ 6400dbf6d73SJordan K. Hubbard case SDT_MEMEA: /* memory execute only accessed */ 6410dbf6d73SJordan K. Hubbard case SDT_MEMER: /* memory execute read */ 6420dbf6d73SJordan K. Hubbard case SDT_MEMERA: /* memory execute read accessed */ 643da59a31cSDavid Greenman break; 644da59a31cSDavid Greenman default: 6450dbf6d73SJordan K. Hubbard return (EINVAL); 646da59a31cSDavid Greenman } 6470dbf6d73SJordan K. Hubbard 6480dbf6d73SJordan K. Hubbard /* Only user (ring-3) descriptors may be present. */ 649711d5183SKonstantin Belousov if (dp->sd.sd_p != 0 && dp->sd.sd_dpl != SEL_UPL) 6500dbf6d73SJordan K. Hubbard return (EACCES); 651da59a31cSDavid Greenman } 652da59a31cSDavid Greenman 6535774db75SJulian Elischer if (uap->start == LDT_AUTO_ALLOC && uap->num == 1) { 654dae8d52dSDavid Xu /* Allocate a free slot */ 6550ad5e7f3SJeff Roberson mtx_lock_spin(&dt_lock); 6560ad5e7f3SJeff Roberson if ((pldt = mdp->md_ldt) == NULL) { 6570ad5e7f3SJeff Roberson if ((error = i386_ldt_grow(td, NLDT + 1))) { 6580ad5e7f3SJeff Roberson mtx_unlock_spin(&dt_lock); 659dae8d52dSDavid Xu return (error); 6600ad5e7f3SJeff Roberson } 661dae8d52dSDavid Xu pldt = mdp->md_ldt; 662dae8d52dSDavid Xu } 663dae8d52dSDavid Xu again: 664f09fc81cSJulian Elischer /* 665f09fc81cSJulian Elischer * start scanning a bit up to leave room for NVidia and 666f09fc81cSJulian Elischer * Wine, which still user the "Blat" method of allocation. 667f09fc81cSJulian Elischer */ 6685774db75SJulian Elischer dp = &((union descriptor *)(pldt->ldt_base))[NLDT]; 669e4e2c613SJulian Elischer for (i = NLDT; i < pldt->ldt_len; ++i) { 670dae8d52dSDavid Xu if (dp->sd.sd_type == SDT_SYSNULL) 671dae8d52dSDavid Xu break; 672dae8d52dSDavid Xu dp++; 673dae8d52dSDavid Xu } 674dae8d52dSDavid Xu if (i >= pldt->ldt_len) { 6750ad5e7f3SJeff Roberson if ((error = i386_ldt_grow(td, pldt->ldt_len+1))) { 6760ad5e7f3SJeff Roberson mtx_unlock_spin(&dt_lock); 677dae8d52dSDavid Xu return (error); 6780ad5e7f3SJeff Roberson } 679dae8d52dSDavid Xu goto again; 680dae8d52dSDavid Xu } 681dae8d52dSDavid Xu uap->start = i; 682dae8d52dSDavid Xu error = i386_set_ldt_data(td, i, 1, descs); 6830ad5e7f3SJeff Roberson mtx_unlock_spin(&dt_lock); 684dae8d52dSDavid Xu } else { 685dae8d52dSDavid Xu largest_ld = uap->start + uap->num; 6860ad5e7f3SJeff Roberson mtx_lock_spin(&dt_lock); 6870ad5e7f3SJeff Roberson if (!(error = i386_ldt_grow(td, largest_ld))) { 688dae8d52dSDavid Xu error = i386_set_ldt_data(td, uap->start, uap->num, 689dae8d52dSDavid Xu descs); 690dae8d52dSDavid Xu } 6910ad5e7f3SJeff Roberson mtx_unlock_spin(&dt_lock); 692dae8d52dSDavid Xu } 693dae8d52dSDavid Xu if (error == 0) 694dae8d52dSDavid Xu td->td_retval[0] = uap->start; 695dae8d52dSDavid Xu return (error); 696dae8d52dSDavid Xu } 697dae8d52dSDavid Xu 69893ee134aSKip Macy static int 69993ee134aSKip Macy i386_set_ldt_data(struct thread *td, int start, int num, 70093ee134aSKip Macy union descriptor *descs) 70193ee134aSKip Macy { 7023cabd93eSKonstantin Belousov struct mdproc *mdp; 7033cabd93eSKonstantin Belousov struct proc_ldt *pldt; 7043cabd93eSKonstantin Belousov uint64_t *dst, *src; 7053cabd93eSKonstantin Belousov int i; 70693ee134aSKip Macy 70793ee134aSKip Macy mtx_assert(&dt_lock, MA_OWNED); 70893ee134aSKip Macy 7093cabd93eSKonstantin Belousov mdp = &td->td_proc->p_md; 7103cabd93eSKonstantin Belousov pldt = mdp->md_ldt; 7113cabd93eSKonstantin Belousov dst = (uint64_t *)(pldt->ldt_base); 7123cabd93eSKonstantin Belousov src = (uint64_t *)descs; 7133cabd93eSKonstantin Belousov 7143cabd93eSKonstantin Belousov /* 7153cabd93eSKonstantin Belousov * Atomic(9) is used only to get 64bit atomic store with 7163cabd93eSKonstantin Belousov * cmpxchg8b when available. There is no op without release 7173cabd93eSKonstantin Belousov * semantic. 7183cabd93eSKonstantin Belousov */ 7193cabd93eSKonstantin Belousov for (i = 0; i < num; i++) 7203cabd93eSKonstantin Belousov atomic_store_rel_64(&dst[start + i], src[i]); 72193ee134aSKip Macy return (0); 72293ee134aSKip Macy } 723dae8d52dSDavid Xu 724dae8d52dSDavid Xu static int 725dae8d52dSDavid Xu i386_ldt_grow(struct thread *td, int len) 726dae8d52dSDavid Xu { 727dae8d52dSDavid Xu struct mdproc *mdp = &td->td_proc->p_md; 72805dfa22fSAttilio Rao struct proc_ldt *new_ldt, *pldt; 72905dfa22fSAttilio Rao caddr_t old_ldt_base = NULL_LDT_BASE; 73005dfa22fSAttilio Rao int old_ldt_len = 0; 731dae8d52dSDavid Xu 7320ad5e7f3SJeff Roberson mtx_assert(&dt_lock, MA_OWNED); 7330ad5e7f3SJeff Roberson 734dae8d52dSDavid Xu if (len > MAX_LD) 735dae8d52dSDavid Xu return (ENOMEM); 736dae8d52dSDavid Xu if (len < NLDT + 1) 737dae8d52dSDavid Xu len = NLDT + 1; 738bc2e774aSJohn Baldwin 739bc2e774aSJohn Baldwin /* Allocate a user ldt. */ 74002b0a160SAttilio Rao if ((pldt = mdp->md_ldt) == NULL || len > pldt->ldt_len) { 741bc2e774aSJohn Baldwin new_ldt = user_ldt_alloc(mdp, len); 742dae8d52dSDavid Xu if (new_ldt == NULL) 743dae8d52dSDavid Xu return (ENOMEM); 744dae8d52dSDavid Xu pldt = mdp->md_ldt; 745bc2e774aSJohn Baldwin 7460ad5e7f3SJeff Roberson if (pldt != NULL) { 74705dfa22fSAttilio Rao if (new_ldt->ldt_len <= pldt->ldt_len) { 748dae8d52dSDavid Xu /* 74905dfa22fSAttilio Rao * We just lost the race for allocation, so 75005dfa22fSAttilio Rao * free the new object and return. 751dae8d52dSDavid Xu */ 75202b0a160SAttilio Rao mtx_unlock_spin(&dt_lock); 7535df87b21SJeff Roberson kmem_free(kernel_arena, 754dae8d52dSDavid Xu (vm_offset_t)new_ldt->ldt_base, 755dae8d52dSDavid Xu new_ldt->ldt_len * sizeof(union descriptor)); 7561ede983cSDag-Erling Smørgrav free(new_ldt, M_SUBPROC); 75702b0a160SAttilio Rao mtx_lock_spin(&dt_lock); 758dae8d52dSDavid Xu return (0); 759dae8d52dSDavid Xu } 76005dfa22fSAttilio Rao 76105dfa22fSAttilio Rao /* 76205dfa22fSAttilio Rao * We have to substitute the current LDT entry for 76305dfa22fSAttilio Rao * curproc with the new one since its size grew. 76405dfa22fSAttilio Rao */ 76505dfa22fSAttilio Rao old_ldt_base = pldt->ldt_base; 76605dfa22fSAttilio Rao old_ldt_len = pldt->ldt_len; 76705dfa22fSAttilio Rao pldt->ldt_sd = new_ldt->ldt_sd; 76805dfa22fSAttilio Rao pldt->ldt_base = new_ldt->ldt_base; 76905dfa22fSAttilio Rao pldt->ldt_len = new_ldt->ldt_len; 7700ad5e7f3SJeff Roberson } else 771dae8d52dSDavid Xu mdp->md_ldt = pldt = new_ldt; 772dae8d52dSDavid Xu #ifdef SMP 77302b0a160SAttilio Rao /* 77402b0a160SAttilio Rao * Signal other cpus to reload ldt. We need to unlock dt_lock 77502b0a160SAttilio Rao * here because other CPU will contest on it since their 77602b0a160SAttilio Rao * curthreads won't hold the lock and will block when trying 77702b0a160SAttilio Rao * to acquire it. 77802b0a160SAttilio Rao */ 77902b0a160SAttilio Rao mtx_unlock_spin(&dt_lock); 780dae8d52dSDavid Xu smp_rendezvous(NULL, (void (*)(void *))set_user_ldt_rv, 78105dfa22fSAttilio Rao NULL, td->td_proc->p_vmspace); 782dae8d52dSDavid Xu #else 783ea11c140SAttilio Rao set_user_ldt(&td->td_proc->p_md); 78405dfa22fSAttilio Rao mtx_unlock_spin(&dt_lock); 785dae8d52dSDavid Xu #endif 78605dfa22fSAttilio Rao if (old_ldt_base != NULL_LDT_BASE) { 7875df87b21SJeff Roberson kmem_free(kernel_arena, (vm_offset_t)old_ldt_base, 78805dfa22fSAttilio Rao old_ldt_len * sizeof(union descriptor)); 7891ede983cSDag-Erling Smørgrav free(new_ldt, M_SUBPROC); 79005dfa22fSAttilio Rao } 79105dfa22fSAttilio Rao mtx_lock_spin(&dt_lock); 792dae8d52dSDavid Xu } 7938eb8107bSPeter Wemm return (0); 794da59a31cSDavid Greenman } 795