xref: /freebsd/sys/amd64/linux32/linux32_machdep.c (revision cfd6422a5217410fbd66f7a7a8a64d9d85e61229)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2004 Tim J. Robbins
5  * Copyright (c) 2002 Doug Rabson
6  * Copyright (c) 2000 Marcel Moolenaar
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer
14  *    in this position and unchanged.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include "opt_compat.h"
37 
38 #include <sys/param.h>
39 #include <sys/capsicum.h>
40 #include <sys/clock.h>
41 #include <sys/fcntl.h>
42 #include <sys/file.h>
43 #include <sys/imgact.h>
44 #include <sys/kernel.h>
45 #include <sys/limits.h>
46 #include <sys/lock.h>
47 #include <sys/malloc.h>
48 #include <sys/mman.h>
49 #include <sys/mutex.h>
50 #include <sys/priv.h>
51 #include <sys/proc.h>
52 #include <sys/resource.h>
53 #include <sys/resourcevar.h>
54 #include <sys/syscallsubr.h>
55 #include <sys/sysproto.h>
56 #include <sys/systm.h>
57 #include <sys/unistd.h>
58 #include <sys/wait.h>
59 
60 #include <machine/frame.h>
61 #include <machine/md_var.h>
62 #include <machine/pcb.h>
63 #include <machine/psl.h>
64 #include <machine/segments.h>
65 #include <machine/specialreg.h>
66 #include <x86/ifunc.h>
67 
68 #include <vm/pmap.h>
69 #include <vm/vm.h>
70 #include <vm/vm_map.h>
71 
72 #include <security/audit/audit.h>
73 
74 #include <compat/freebsd32/freebsd32_util.h>
75 #include <amd64/linux32/linux.h>
76 #include <amd64/linux32/linux32_proto.h>
77 #include <compat/linux/linux_emul.h>
78 #include <compat/linux/linux_ipc.h>
79 #include <compat/linux/linux_misc.h>
80 #include <compat/linux/linux_mmap.h>
81 #include <compat/linux/linux_signal.h>
82 #include <compat/linux/linux_util.h>
83 
84 static void	bsd_to_linux_rusage(struct rusage *ru, struct l_rusage *lru);
85 
86 struct l_old_select_argv {
87 	l_int		nfds;
88 	l_uintptr_t	readfds;
89 	l_uintptr_t	writefds;
90 	l_uintptr_t	exceptfds;
91 	l_uintptr_t	timeout;
92 } __packed;
93 
94 static void
95 bsd_to_linux_rusage(struct rusage *ru, struct l_rusage *lru)
96 {
97 
98 	lru->ru_utime.tv_sec = ru->ru_utime.tv_sec;
99 	lru->ru_utime.tv_usec = ru->ru_utime.tv_usec;
100 	lru->ru_stime.tv_sec = ru->ru_stime.tv_sec;
101 	lru->ru_stime.tv_usec = ru->ru_stime.tv_usec;
102 	lru->ru_maxrss = ru->ru_maxrss;
103 	lru->ru_ixrss = ru->ru_ixrss;
104 	lru->ru_idrss = ru->ru_idrss;
105 	lru->ru_isrss = ru->ru_isrss;
106 	lru->ru_minflt = ru->ru_minflt;
107 	lru->ru_majflt = ru->ru_majflt;
108 	lru->ru_nswap = ru->ru_nswap;
109 	lru->ru_inblock = ru->ru_inblock;
110 	lru->ru_oublock = ru->ru_oublock;
111 	lru->ru_msgsnd = ru->ru_msgsnd;
112 	lru->ru_msgrcv = ru->ru_msgrcv;
113 	lru->ru_nsignals = ru->ru_nsignals;
114 	lru->ru_nvcsw = ru->ru_nvcsw;
115 	lru->ru_nivcsw = ru->ru_nivcsw;
116 }
117 
118 int
119 linux_copyout_rusage(struct rusage *ru, void *uaddr)
120 {
121 	struct l_rusage lru;
122 
123 	bsd_to_linux_rusage(ru, &lru);
124 
125 	return (copyout(&lru, uaddr, sizeof(struct l_rusage)));
126 }
127 
128 int
129 linux_execve(struct thread *td, struct linux_execve_args *args)
130 {
131 	struct image_args eargs;
132 	char *path;
133 	int error;
134 
135 	LCONVPATHEXIST(td, args->path, &path);
136 
137 	error = freebsd32_exec_copyin_args(&eargs, path, UIO_SYSSPACE,
138 	    args->argp, args->envp);
139 	free(path, M_TEMP);
140 	if (error == 0)
141 		error = linux_common_execve(td, &eargs);
142 	AUDIT_SYSCALL_EXIT(error == EJUSTRETURN ? 0 : error, td);
143 	return (error);
144 }
145 
146 CTASSERT(sizeof(struct l_iovec32) == 8);
147 
148 int
149 linux32_copyinuio(struct l_iovec32 *iovp, l_ulong iovcnt, struct uio **uiop)
150 {
151 	struct l_iovec32 iov32;
152 	struct iovec *iov;
153 	struct uio *uio;
154 	uint32_t iovlen;
155 	int error, i;
156 
157 	*uiop = NULL;
158 	if (iovcnt > UIO_MAXIOV)
159 		return (EINVAL);
160 	iovlen = iovcnt * sizeof(struct iovec);
161 	uio = malloc(iovlen + sizeof(*uio), M_IOV, M_WAITOK);
162 	iov = (struct iovec *)(uio + 1);
163 	for (i = 0; i < iovcnt; i++) {
164 		error = copyin(&iovp[i], &iov32, sizeof(struct l_iovec32));
165 		if (error) {
166 			free(uio, M_IOV);
167 			return (error);
168 		}
169 		iov[i].iov_base = PTRIN(iov32.iov_base);
170 		iov[i].iov_len = iov32.iov_len;
171 	}
172 	uio->uio_iov = iov;
173 	uio->uio_iovcnt = iovcnt;
174 	uio->uio_segflg = UIO_USERSPACE;
175 	uio->uio_offset = -1;
176 	uio->uio_resid = 0;
177 	for (i = 0; i < iovcnt; i++) {
178 		if (iov->iov_len > INT_MAX - uio->uio_resid) {
179 			free(uio, M_IOV);
180 			return (EINVAL);
181 		}
182 		uio->uio_resid += iov->iov_len;
183 		iov++;
184 	}
185 	*uiop = uio;
186 	return (0);
187 }
188 
189 int
190 linux32_copyiniov(struct l_iovec32 *iovp32, l_ulong iovcnt, struct iovec **iovp,
191     int error)
192 {
193 	struct l_iovec32 iov32;
194 	struct iovec *iov;
195 	uint32_t iovlen;
196 	int i;
197 
198 	*iovp = NULL;
199 	if (iovcnt > UIO_MAXIOV)
200 		return (error);
201 	iovlen = iovcnt * sizeof(struct iovec);
202 	iov = malloc(iovlen, M_IOV, M_WAITOK);
203 	for (i = 0; i < iovcnt; i++) {
204 		error = copyin(&iovp32[i], &iov32, sizeof(struct l_iovec32));
205 		if (error) {
206 			free(iov, M_IOV);
207 			return (error);
208 		}
209 		iov[i].iov_base = PTRIN(iov32.iov_base);
210 		iov[i].iov_len = iov32.iov_len;
211 	}
212 	*iovp = iov;
213 	return(0);
214 
215 }
216 
217 int
218 linux_readv(struct thread *td, struct linux_readv_args *uap)
219 {
220 	struct uio *auio;
221 	int error;
222 
223 	error = linux32_copyinuio(uap->iovp, uap->iovcnt, &auio);
224 	if (error)
225 		return (error);
226 	error = kern_readv(td, uap->fd, auio);
227 	free(auio, M_IOV);
228 	return (error);
229 }
230 
231 int
232 linux_writev(struct thread *td, struct linux_writev_args *uap)
233 {
234 	struct uio *auio;
235 	int error;
236 
237 	error = linux32_copyinuio(uap->iovp, uap->iovcnt, &auio);
238 	if (error)
239 		return (error);
240 	error = kern_writev(td, uap->fd, auio);
241 	free(auio, M_IOV);
242 	return (error);
243 }
244 
245 struct l_ipc_kludge {
246 	l_uintptr_t msgp;
247 	l_long msgtyp;
248 } __packed;
249 
250 int
251 linux_ipc(struct thread *td, struct linux_ipc_args *args)
252 {
253 
254 	switch (args->what & 0xFFFF) {
255 	case LINUX_SEMOP: {
256 		struct linux_semop_args a;
257 
258 		a.semid = args->arg1;
259 		a.tsops = PTRIN(args->ptr);
260 		a.nsops = args->arg2;
261 		return (linux_semop(td, &a));
262 	}
263 	case LINUX_SEMGET: {
264 		struct linux_semget_args a;
265 
266 		a.key = args->arg1;
267 		a.nsems = args->arg2;
268 		a.semflg = args->arg3;
269 		return (linux_semget(td, &a));
270 	}
271 	case LINUX_SEMCTL: {
272 		struct linux_semctl_args a;
273 		int error;
274 
275 		a.semid = args->arg1;
276 		a.semnum = args->arg2;
277 		a.cmd = args->arg3;
278 		error = copyin(PTRIN(args->ptr), &a.arg, sizeof(a.arg));
279 		if (error)
280 			return (error);
281 		return (linux_semctl(td, &a));
282 	}
283 	case LINUX_MSGSND: {
284 		struct linux_msgsnd_args a;
285 
286 		a.msqid = args->arg1;
287 		a.msgp = PTRIN(args->ptr);
288 		a.msgsz = args->arg2;
289 		a.msgflg = args->arg3;
290 		return (linux_msgsnd(td, &a));
291 	}
292 	case LINUX_MSGRCV: {
293 		struct linux_msgrcv_args a;
294 
295 		a.msqid = args->arg1;
296 		a.msgsz = args->arg2;
297 		a.msgflg = args->arg3;
298 		if ((args->what >> 16) == 0) {
299 			struct l_ipc_kludge tmp;
300 			int error;
301 
302 			if (args->ptr == 0)
303 				return (EINVAL);
304 			error = copyin(PTRIN(args->ptr), &tmp, sizeof(tmp));
305 			if (error)
306 				return (error);
307 			a.msgp = PTRIN(tmp.msgp);
308 			a.msgtyp = tmp.msgtyp;
309 		} else {
310 			a.msgp = PTRIN(args->ptr);
311 			a.msgtyp = args->arg5;
312 		}
313 		return (linux_msgrcv(td, &a));
314 	}
315 	case LINUX_MSGGET: {
316 		struct linux_msgget_args a;
317 
318 		a.key = args->arg1;
319 		a.msgflg = args->arg2;
320 		return (linux_msgget(td, &a));
321 	}
322 	case LINUX_MSGCTL: {
323 		struct linux_msgctl_args a;
324 
325 		a.msqid = args->arg1;
326 		a.cmd = args->arg2;
327 		a.buf = PTRIN(args->ptr);
328 		return (linux_msgctl(td, &a));
329 	}
330 	case LINUX_SHMAT: {
331 		struct linux_shmat_args a;
332 		l_uintptr_t addr;
333 		int error;
334 
335 		a.shmid = args->arg1;
336 		a.shmaddr = PTRIN(args->ptr);
337 		a.shmflg = args->arg2;
338 		error = linux_shmat(td, &a);
339 		if (error != 0)
340 			return (error);
341 		addr = td->td_retval[0];
342 		error = copyout(&addr, PTRIN(args->arg3), sizeof(addr));
343 		td->td_retval[0] = 0;
344 		return (error);
345 	}
346 	case LINUX_SHMDT: {
347 		struct linux_shmdt_args a;
348 
349 		a.shmaddr = PTRIN(args->ptr);
350 		return (linux_shmdt(td, &a));
351 	}
352 	case LINUX_SHMGET: {
353 		struct linux_shmget_args a;
354 
355 		a.key = args->arg1;
356 		a.size = args->arg2;
357 		a.shmflg = args->arg3;
358 		return (linux_shmget(td, &a));
359 	}
360 	case LINUX_SHMCTL: {
361 		struct linux_shmctl_args a;
362 
363 		a.shmid = args->arg1;
364 		a.cmd = args->arg2;
365 		a.buf = PTRIN(args->ptr);
366 		return (linux_shmctl(td, &a));
367 	}
368 	default:
369 		break;
370 	}
371 
372 	return (EINVAL);
373 }
374 
375 int
376 linux_old_select(struct thread *td, struct linux_old_select_args *args)
377 {
378 	struct l_old_select_argv linux_args;
379 	struct linux_select_args newsel;
380 	int error;
381 
382 	error = copyin(args->ptr, &linux_args, sizeof(linux_args));
383 	if (error)
384 		return (error);
385 
386 	newsel.nfds = linux_args.nfds;
387 	newsel.readfds = PTRIN(linux_args.readfds);
388 	newsel.writefds = PTRIN(linux_args.writefds);
389 	newsel.exceptfds = PTRIN(linux_args.exceptfds);
390 	newsel.timeout = PTRIN(linux_args.timeout);
391 	return (linux_select(td, &newsel));
392 }
393 
394 int
395 linux_set_cloned_tls(struct thread *td, void *desc)
396 {
397 	struct user_segment_descriptor sd;
398 	struct l_user_desc info;
399 	struct pcb *pcb;
400 	int error;
401 	int a[2];
402 
403 	error = copyin(desc, &info, sizeof(struct l_user_desc));
404 	if (error) {
405 		linux_msg(td, "set_cloned_tls copyin info failed!");
406 	} else {
407 		/* We might copy out the entry_number as GUGS32_SEL. */
408 		info.entry_number = GUGS32_SEL;
409 		error = copyout(&info, desc, sizeof(struct l_user_desc));
410 		if (error)
411 			linux_msg(td, "set_cloned_tls copyout info failed!");
412 
413 		a[0] = LINUX_LDT_entry_a(&info);
414 		a[1] = LINUX_LDT_entry_b(&info);
415 
416 		memcpy(&sd, &a, sizeof(a));
417 		pcb = td->td_pcb;
418 		pcb->pcb_gsbase = (register_t)info.base_addr;
419 		td->td_frame->tf_gs = GSEL(GUGS32_SEL, SEL_UPL);
420 		set_pcb_flags(pcb, PCB_32BIT);
421 	}
422 
423 	return (error);
424 }
425 
426 int
427 linux_set_upcall_kse(struct thread *td, register_t stack)
428 {
429 
430 	if (stack)
431 		td->td_frame->tf_rsp = stack;
432 
433 	/*
434 	 * The newly created Linux thread returns
435 	 * to the user space by the same path that a parent do.
436 	 */
437 	td->td_frame->tf_rax = 0;
438 	return (0);
439 }
440 
441 int
442 linux_mmap2(struct thread *td, struct linux_mmap2_args *args)
443 {
444 
445 	return (linux_mmap_common(td, PTROUT(args->addr), args->len, args->prot,
446 		args->flags, args->fd, (uint64_t)(uint32_t)args->pgoff *
447 		PAGE_SIZE));
448 }
449 
450 int
451 linux_mmap(struct thread *td, struct linux_mmap_args *args)
452 {
453 	int error;
454 	struct l_mmap_argv linux_args;
455 
456 	error = copyin(args->ptr, &linux_args, sizeof(linux_args));
457 	if (error)
458 		return (error);
459 
460 	return (linux_mmap_common(td, linux_args.addr, linux_args.len,
461 	    linux_args.prot, linux_args.flags, linux_args.fd,
462 	    (uint32_t)linux_args.pgoff));
463 }
464 
465 int
466 linux_mprotect(struct thread *td, struct linux_mprotect_args *uap)
467 {
468 
469 	return (linux_mprotect_common(td, PTROUT(uap->addr), uap->len, uap->prot));
470 }
471 
472 int
473 linux_madvise(struct thread *td, struct linux_madvise_args *uap)
474 {
475 
476 	return (linux_madvise_common(td, PTROUT(uap->addr), uap->len, uap->behav));
477 }
478 
479 int
480 linux_iopl(struct thread *td, struct linux_iopl_args *args)
481 {
482 	int error;
483 
484 	if (args->level < 0 || args->level > 3)
485 		return (EINVAL);
486 	if ((error = priv_check(td, PRIV_IO)) != 0)
487 		return (error);
488 	if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
489 		return (error);
490 	td->td_frame->tf_rflags = (td->td_frame->tf_rflags & ~PSL_IOPL) |
491 	    (args->level * (PSL_IOPL / 3));
492 
493 	return (0);
494 }
495 
496 int
497 linux_sigaction(struct thread *td, struct linux_sigaction_args *args)
498 {
499 	l_osigaction_t osa;
500 	l_sigaction_t act, oact;
501 	int error;
502 
503 	if (args->nsa != NULL) {
504 		error = copyin(args->nsa, &osa, sizeof(l_osigaction_t));
505 		if (error)
506 			return (error);
507 		act.lsa_handler = osa.lsa_handler;
508 		act.lsa_flags = osa.lsa_flags;
509 		act.lsa_restorer = osa.lsa_restorer;
510 		LINUX_SIGEMPTYSET(act.lsa_mask);
511 		act.lsa_mask.__mask = osa.lsa_mask;
512 	}
513 
514 	error = linux_do_sigaction(td, args->sig, args->nsa ? &act : NULL,
515 	    args->osa ? &oact : NULL);
516 
517 	if (args->osa != NULL && !error) {
518 		osa.lsa_handler = oact.lsa_handler;
519 		osa.lsa_flags = oact.lsa_flags;
520 		osa.lsa_restorer = oact.lsa_restorer;
521 		osa.lsa_mask = oact.lsa_mask.__mask;
522 		error = copyout(&osa, args->osa, sizeof(l_osigaction_t));
523 	}
524 
525 	return (error);
526 }
527 
528 /*
529  * Linux has two extra args, restart and oldmask.  We don't use these,
530  * but it seems that "restart" is actually a context pointer that
531  * enables the signal to happen with a different register set.
532  */
533 int
534 linux_sigsuspend(struct thread *td, struct linux_sigsuspend_args *args)
535 {
536 	sigset_t sigmask;
537 	l_sigset_t mask;
538 
539 	LINUX_SIGEMPTYSET(mask);
540 	mask.__mask = args->mask;
541 	linux_to_bsd_sigset(&mask, &sigmask);
542 	return (kern_sigsuspend(td, sigmask));
543 }
544 
545 int
546 linux_rt_sigsuspend(struct thread *td, struct linux_rt_sigsuspend_args *uap)
547 {
548 	l_sigset_t lmask;
549 	sigset_t sigmask;
550 	int error;
551 
552 	if (uap->sigsetsize != sizeof(l_sigset_t))
553 		return (EINVAL);
554 
555 	error = copyin(uap->newset, &lmask, sizeof(l_sigset_t));
556 	if (error)
557 		return (error);
558 
559 	linux_to_bsd_sigset(&lmask, &sigmask);
560 	return (kern_sigsuspend(td, sigmask));
561 }
562 
563 int
564 linux_pause(struct thread *td, struct linux_pause_args *args)
565 {
566 	struct proc *p = td->td_proc;
567 	sigset_t sigmask;
568 
569 	PROC_LOCK(p);
570 	sigmask = td->td_sigmask;
571 	PROC_UNLOCK(p);
572 	return (kern_sigsuspend(td, sigmask));
573 }
574 
575 int
576 linux_sigaltstack(struct thread *td, struct linux_sigaltstack_args *uap)
577 {
578 	stack_t ss, oss;
579 	l_stack_t lss;
580 	int error;
581 
582 	if (uap->uss != NULL) {
583 		error = copyin(uap->uss, &lss, sizeof(l_stack_t));
584 		if (error)
585 			return (error);
586 
587 		ss.ss_sp = PTRIN(lss.ss_sp);
588 		ss.ss_size = lss.ss_size;
589 		ss.ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags);
590 	}
591 	error = kern_sigaltstack(td, (uap->uss != NULL) ? &ss : NULL,
592 	    (uap->uoss != NULL) ? &oss : NULL);
593 	if (!error && uap->uoss != NULL) {
594 		lss.ss_sp = PTROUT(oss.ss_sp);
595 		lss.ss_size = oss.ss_size;
596 		lss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags);
597 		error = copyout(&lss, uap->uoss, sizeof(l_stack_t));
598 	}
599 
600 	return (error);
601 }
602 
603 int
604 linux_gettimeofday(struct thread *td, struct linux_gettimeofday_args *uap)
605 {
606 	struct timeval atv;
607 	l_timeval atv32;
608 	struct timezone rtz;
609 	int error = 0;
610 
611 	if (uap->tp) {
612 		microtime(&atv);
613 		atv32.tv_sec = atv.tv_sec;
614 		atv32.tv_usec = atv.tv_usec;
615 		error = copyout(&atv32, uap->tp, sizeof(atv32));
616 	}
617 	if (error == 0 && uap->tzp != NULL) {
618 		rtz.tz_minuteswest = 0;
619 		rtz.tz_dsttime = 0;
620 		error = copyout(&rtz, uap->tzp, sizeof(rtz));
621 	}
622 	return (error);
623 }
624 
625 int
626 linux_settimeofday(struct thread *td, struct linux_settimeofday_args *uap)
627 {
628 	l_timeval atv32;
629 	struct timeval atv, *tvp;
630 	struct timezone atz, *tzp;
631 	int error;
632 
633 	if (uap->tp) {
634 		error = copyin(uap->tp, &atv32, sizeof(atv32));
635 		if (error)
636 			return (error);
637 		atv.tv_sec = atv32.tv_sec;
638 		atv.tv_usec = atv32.tv_usec;
639 		tvp = &atv;
640 	} else
641 		tvp = NULL;
642 	if (uap->tzp) {
643 		error = copyin(uap->tzp, &atz, sizeof(atz));
644 		if (error)
645 			return (error);
646 		tzp = &atz;
647 	} else
648 		tzp = NULL;
649 	return (kern_settimeofday(td, tvp, tzp));
650 }
651 
652 int
653 linux_getrusage(struct thread *td, struct linux_getrusage_args *uap)
654 {
655 	struct rusage s;
656 	int error;
657 
658 	error = kern_getrusage(td, uap->who, &s);
659 	if (error != 0)
660 		return (error);
661 	if (uap->rusage != NULL)
662 		error = linux_copyout_rusage(&s, uap->rusage);
663 	return (error);
664 }
665 
666 int
667 linux_set_thread_area(struct thread *td,
668     struct linux_set_thread_area_args *args)
669 {
670 	struct l_user_desc info;
671 	struct user_segment_descriptor sd;
672 	struct pcb *pcb;
673 	int a[2];
674 	int error;
675 
676 	error = copyin(args->desc, &info, sizeof(struct l_user_desc));
677 	if (error)
678 		return (error);
679 
680 	/*
681 	 * Semantics of Linux version: every thread in the system has array
682 	 * of three TLS descriptors. 1st is GLIBC TLS, 2nd is WINE, 3rd unknown.
683 	 * This syscall loads one of the selected TLS decriptors with a value
684 	 * and also loads GDT descriptors 6, 7 and 8 with the content of
685 	 * the per-thread descriptors.
686 	 *
687 	 * Semantics of FreeBSD version: I think we can ignore that Linux has
688 	 * three per-thread descriptors and use just the first one.
689 	 * The tls_array[] is used only in [gs]et_thread_area() syscalls and
690 	 * for loading the GDT descriptors. We use just one GDT descriptor
691 	 * for TLS, so we will load just one.
692 	 *
693 	 * XXX: This doesn't work when a user space process tries to use more
694 	 * than one TLS segment. Comment in the Linux source says wine might
695 	 * do this.
696 	 */
697 
698 	/*
699 	 * GLIBC reads current %gs and call set_thread_area() with it.
700 	 * We should let GUDATA_SEL and GUGS32_SEL proceed as well because
701 	 * we use these segments.
702 	 */
703 	switch (info.entry_number) {
704 	case GUGS32_SEL:
705 	case GUDATA_SEL:
706 	case 6:
707 	case -1:
708 		info.entry_number = GUGS32_SEL;
709 		break;
710 	default:
711 		return (EINVAL);
712 	}
713 
714 	/*
715 	 * We have to copy out the GDT entry we use.
716 	 *
717 	 * XXX: What if a user space program does not check the return value
718 	 * and tries to use 6, 7 or 8?
719 	 */
720 	error = copyout(&info, args->desc, sizeof(struct l_user_desc));
721 	if (error)
722 		return (error);
723 
724 	if (LINUX_LDT_empty(&info)) {
725 		a[0] = 0;
726 		a[1] = 0;
727 	} else {
728 		a[0] = LINUX_LDT_entry_a(&info);
729 		a[1] = LINUX_LDT_entry_b(&info);
730 	}
731 
732 	memcpy(&sd, &a, sizeof(a));
733 	pcb = td->td_pcb;
734 	pcb->pcb_gsbase = (register_t)info.base_addr;
735 	set_pcb_flags(pcb, PCB_32BIT);
736 	update_gdt_gsbase(td, info.base_addr);
737 
738 	return (0);
739 }
740 
741 int futex_xchgl_nosmap(int oparg, uint32_t *uaddr, int *oldval);
742 int futex_xchgl_smap(int oparg, uint32_t *uaddr, int *oldval);
743 DEFINE_IFUNC(, int, futex_xchgl, (int, uint32_t *, int *))
744 {
745 
746 	return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
747 	    futex_xchgl_smap : futex_xchgl_nosmap);
748 }
749 
750 int futex_addl_nosmap(int oparg, uint32_t *uaddr, int *oldval);
751 int futex_addl_smap(int oparg, uint32_t *uaddr, int *oldval);
752 DEFINE_IFUNC(, int, futex_addl, (int, uint32_t *, int *))
753 {
754 
755 	return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
756 	    futex_addl_smap : futex_addl_nosmap);
757 }
758 
759 int futex_orl_nosmap(int oparg, uint32_t *uaddr, int *oldval);
760 int futex_orl_smap(int oparg, uint32_t *uaddr, int *oldval);
761 DEFINE_IFUNC(, int, futex_orl, (int, uint32_t *, int *))
762 {
763 
764 	return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
765 	    futex_orl_smap : futex_orl_nosmap);
766 }
767 
768 int futex_andl_nosmap(int oparg, uint32_t *uaddr, int *oldval);
769 int futex_andl_smap(int oparg, uint32_t *uaddr, int *oldval);
770 DEFINE_IFUNC(, int, futex_andl, (int, uint32_t *, int *))
771 {
772 
773 	return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
774 	    futex_andl_smap : futex_andl_nosmap);
775 }
776 
777 int futex_xorl_nosmap(int oparg, uint32_t *uaddr, int *oldval);
778 int futex_xorl_smap(int oparg, uint32_t *uaddr, int *oldval);
779 DEFINE_IFUNC(, int, futex_xorl, (int, uint32_t *, int *))
780 {
781 
782 	return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
783 	    futex_xorl_smap : futex_xorl_nosmap);
784 }
785