xref: /freebsd/sys/i386/linux/linux_machdep.c (revision ab40f58ccfe6c07ebefddc72f4661a52fe746353)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2000 Marcel Moolenaar
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  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/capsicum.h>
34 #include <sys/fcntl.h>
35 #include <sys/file.h>
36 #include <sys/imgact.h>
37 #include <sys/lock.h>
38 #include <sys/malloc.h>
39 #include <sys/mman.h>
40 #include <sys/mutex.h>
41 #include <sys/priv.h>
42 #include <sys/proc.h>
43 #include <sys/queue.h>
44 #include <sys/resource.h>
45 #include <sys/resourcevar.h>
46 #include <sys/sched.h>
47 #include <sys/signalvar.h>
48 #include <sys/syscallsubr.h>
49 #include <sys/sysproto.h>
50 #include <sys/systm.h>
51 #include <sys/sx.h>
52 #include <sys/unistd.h>
53 #include <sys/wait.h>
54 
55 #include <machine/frame.h>
56 #include <machine/psl.h>
57 #include <machine/segments.h>
58 #include <machine/sysarch.h>
59 
60 #include <vm/pmap.h>
61 #include <vm/vm.h>
62 #include <vm/vm_map.h>
63 
64 #include <i386/linux/linux.h>
65 #include <i386/linux/linux_proto.h>
66 #include <compat/linux/linux_emul.h>
67 #include <compat/linux/linux_ipc.h>
68 #include <compat/linux/linux_misc.h>
69 #include <compat/linux/linux_mmap.h>
70 #include <compat/linux/linux_signal.h>
71 #include <compat/linux/linux_util.h>
72 
73 #include <i386/include/pcb.h>			/* needed for pcb definition in linux_set_thread_area */
74 
75 #include "opt_posix.h"
76 
77 extern struct sysentvec elf32_freebsd_sysvec;	/* defined in i386/i386/elf_machdep.c */
78 
79 struct l_descriptor {
80 	l_uint		entry_number;
81 	l_ulong		base_addr;
82 	l_uint		limit;
83 	l_uint		seg_32bit:1;
84 	l_uint		contents:2;
85 	l_uint		read_exec_only:1;
86 	l_uint		limit_in_pages:1;
87 	l_uint		seg_not_present:1;
88 	l_uint		useable:1;
89 };
90 
91 struct l_old_select_argv {
92 	l_int		nfds;
93 	l_fd_set	*readfds;
94 	l_fd_set	*writefds;
95 	l_fd_set	*exceptfds;
96 	struct l_timeval	*timeout;
97 };
98 
99 
100 int
101 linux_execve(struct thread *td, struct linux_execve_args *args)
102 {
103 	struct image_args eargs;
104 	char *newpath;
105 	int error;
106 
107 	LCONVPATHEXIST(td, args->path, &newpath);
108 
109 #ifdef DEBUG
110 	if (ldebug(execve))
111 		printf(ARGS(execve, "%s"), newpath);
112 #endif
113 
114 	error = exec_copyin_args(&eargs, newpath, UIO_SYSSPACE,
115 	    args->argp, args->envp);
116 	free(newpath, M_TEMP);
117 	if (error == 0)
118 		error = linux_common_execve(td, &eargs);
119 	return (error);
120 }
121 
122 struct l_ipc_kludge {
123 	struct l_msgbuf *msgp;
124 	l_long msgtyp;
125 };
126 
127 int
128 linux_ipc(struct thread *td, struct linux_ipc_args *args)
129 {
130 
131 	switch (args->what & 0xFFFF) {
132 	case LINUX_SEMOP: {
133 		struct linux_semop_args a;
134 
135 		a.semid = args->arg1;
136 		a.tsops = args->ptr;
137 		a.nsops = args->arg2;
138 		return (linux_semop(td, &a));
139 	}
140 	case LINUX_SEMGET: {
141 		struct linux_semget_args a;
142 
143 		a.key = args->arg1;
144 		a.nsems = args->arg2;
145 		a.semflg = args->arg3;
146 		return (linux_semget(td, &a));
147 	}
148 	case LINUX_SEMCTL: {
149 		struct linux_semctl_args a;
150 		int error;
151 
152 		a.semid = args->arg1;
153 		a.semnum = args->arg2;
154 		a.cmd = args->arg3;
155 		error = copyin(args->ptr, &a.arg, sizeof(a.arg));
156 		if (error)
157 			return (error);
158 		return (linux_semctl(td, &a));
159 	}
160 	case LINUX_MSGSND: {
161 		struct linux_msgsnd_args a;
162 
163 		a.msqid = args->arg1;
164 		a.msgp = args->ptr;
165 		a.msgsz = args->arg2;
166 		a.msgflg = args->arg3;
167 		return (linux_msgsnd(td, &a));
168 	}
169 	case LINUX_MSGRCV: {
170 		struct linux_msgrcv_args a;
171 
172 		a.msqid = args->arg1;
173 		a.msgsz = args->arg2;
174 		a.msgflg = args->arg3;
175 		if ((args->what >> 16) == 0) {
176 			struct l_ipc_kludge tmp;
177 			int error;
178 
179 			if (args->ptr == NULL)
180 				return (EINVAL);
181 			error = copyin(args->ptr, &tmp, sizeof(tmp));
182 			if (error)
183 				return (error);
184 			a.msgp = tmp.msgp;
185 			a.msgtyp = tmp.msgtyp;
186 		} else {
187 			a.msgp = args->ptr;
188 			a.msgtyp = args->arg5;
189 		}
190 		return (linux_msgrcv(td, &a));
191 	}
192 	case LINUX_MSGGET: {
193 		struct linux_msgget_args a;
194 
195 		a.key = args->arg1;
196 		a.msgflg = args->arg2;
197 		return (linux_msgget(td, &a));
198 	}
199 	case LINUX_MSGCTL: {
200 		struct linux_msgctl_args a;
201 
202 		a.msqid = args->arg1;
203 		a.cmd = args->arg2;
204 		a.buf = args->ptr;
205 		return (linux_msgctl(td, &a));
206 	}
207 	case LINUX_SHMAT: {
208 		struct linux_shmat_args a;
209 
210 		a.shmid = args->arg1;
211 		a.shmaddr = args->ptr;
212 		a.shmflg = args->arg2;
213 		a.raddr = (l_ulong *)args->arg3;
214 		return (linux_shmat(td, &a));
215 	}
216 	case LINUX_SHMDT: {
217 		struct linux_shmdt_args a;
218 
219 		a.shmaddr = args->ptr;
220 		return (linux_shmdt(td, &a));
221 	}
222 	case LINUX_SHMGET: {
223 		struct linux_shmget_args a;
224 
225 		a.key = args->arg1;
226 		a.size = args->arg2;
227 		a.shmflg = args->arg3;
228 		return (linux_shmget(td, &a));
229 	}
230 	case LINUX_SHMCTL: {
231 		struct linux_shmctl_args a;
232 
233 		a.shmid = args->arg1;
234 		a.cmd = args->arg2;
235 		a.buf = args->ptr;
236 		return (linux_shmctl(td, &a));
237 	}
238 	default:
239 		break;
240 	}
241 
242 	return (EINVAL);
243 }
244 
245 int
246 linux_old_select(struct thread *td, struct linux_old_select_args *args)
247 {
248 	struct l_old_select_argv linux_args;
249 	struct linux_select_args newsel;
250 	int error;
251 
252 #ifdef DEBUG
253 	if (ldebug(old_select))
254 		printf(ARGS(old_select, "%p"), args->ptr);
255 #endif
256 
257 	error = copyin(args->ptr, &linux_args, sizeof(linux_args));
258 	if (error)
259 		return (error);
260 
261 	newsel.nfds = linux_args.nfds;
262 	newsel.readfds = linux_args.readfds;
263 	newsel.writefds = linux_args.writefds;
264 	newsel.exceptfds = linux_args.exceptfds;
265 	newsel.timeout = linux_args.timeout;
266 	return (linux_select(td, &newsel));
267 }
268 
269 int
270 linux_set_cloned_tls(struct thread *td, void *desc)
271 {
272 	struct segment_descriptor sd;
273 	struct l_user_desc info;
274 	int idx, error;
275 	int a[2];
276 
277 	error = copyin(desc, &info, sizeof(struct l_user_desc));
278 	if (error) {
279 		printf(LMSG("copyin failed!"));
280 	} else {
281 		idx = info.entry_number;
282 
283 		/*
284 		 * looks like we're getting the idx we returned
285 		 * in the set_thread_area() syscall
286 		 */
287 		if (idx != 6 && idx != 3) {
288 			printf(LMSG("resetting idx!"));
289 			idx = 3;
290 		}
291 
292 		/* this doesnt happen in practice */
293 		if (idx == 6) {
294 			/* we might copy out the entry_number as 3 */
295 			info.entry_number = 3;
296 			error = copyout(&info, desc, sizeof(struct l_user_desc));
297 			if (error)
298 				printf(LMSG("copyout failed!"));
299 		}
300 
301 		a[0] = LINUX_LDT_entry_a(&info);
302 		a[1] = LINUX_LDT_entry_b(&info);
303 
304 		memcpy(&sd, &a, sizeof(a));
305 #ifdef DEBUG
306 		if (ldebug(clone))
307 			printf("Segment created in clone with "
308 			"CLONE_SETTLS: lobase: %x, hibase: %x, "
309 			"lolimit: %x, hilimit: %x, type: %i, "
310 			"dpl: %i, p: %i, xx: %i, def32: %i, "
311 			"gran: %i\n", sd.sd_lobase, sd.sd_hibase,
312 			sd.sd_lolimit, sd.sd_hilimit, sd.sd_type,
313 			sd.sd_dpl, sd.sd_p, sd.sd_xx,
314 			sd.sd_def32, sd.sd_gran);
315 #endif
316 
317 		/* set %gs */
318 		td->td_pcb->pcb_gsd = sd;
319 		td->td_pcb->pcb_gs = GSEL(GUGS_SEL, SEL_UPL);
320 	}
321 
322 	return (error);
323 }
324 
325 int
326 linux_set_upcall_kse(struct thread *td, register_t stack)
327 {
328 
329 	if (stack)
330 		td->td_frame->tf_esp = stack;
331 
332 	/*
333 	 * The newly created Linux thread returns
334 	 * to the user space by the same path that a parent do.
335 	 */
336 	td->td_frame->tf_eax = 0;
337 	return (0);
338 }
339 
340 int
341 linux_mmap2(struct thread *td, struct linux_mmap2_args *args)
342 {
343 
344 #ifdef DEBUG
345 	if (ldebug(mmap2))
346 		printf(ARGS(mmap2, "%p, %d, %d, 0x%08x, %d, %d"),
347 		    (void *)args->addr, args->len, args->prot,
348 		    args->flags, args->fd, args->pgoff);
349 #endif
350 
351 	return (linux_mmap_common(td, args->addr, args->len, args->prot,
352 		args->flags, args->fd, (uint64_t)(uint32_t)args->pgoff *
353 		PAGE_SIZE));
354 }
355 
356 int
357 linux_mmap(struct thread *td, struct linux_mmap_args *args)
358 {
359 	int error;
360 	struct l_mmap_argv linux_args;
361 
362 	error = copyin(args->ptr, &linux_args, sizeof(linux_args));
363 	if (error)
364 		return (error);
365 
366 #ifdef DEBUG
367 	if (ldebug(mmap))
368 		printf(ARGS(mmap, "%p, %d, %d, 0x%08x, %d, %d"),
369 		    (void *)linux_args.addr, linux_args.len, linux_args.prot,
370 		    linux_args.flags, linux_args.fd, linux_args.pgoff);
371 #endif
372 
373 	return (linux_mmap_common(td, linux_args.addr, linux_args.len,
374 	    linux_args.prot, linux_args.flags, linux_args.fd,
375 	    (uint32_t)linux_args.pgoff));
376 }
377 
378 int
379 linux_mprotect(struct thread *td, struct linux_mprotect_args *uap)
380 {
381 
382 	return (linux_mprotect_common(td, PTROUT(uap->addr), uap->len, uap->prot));
383 }
384 
385 int
386 linux_ioperm(struct thread *td, struct linux_ioperm_args *args)
387 {
388 	int error;
389 	struct i386_ioperm_args iia;
390 
391 	iia.start = args->start;
392 	iia.length = args->length;
393 	iia.enable = args->enable;
394 	error = i386_set_ioperm(td, &iia);
395 	return (error);
396 }
397 
398 int
399 linux_iopl(struct thread *td, struct linux_iopl_args *args)
400 {
401 	int error;
402 
403 	if (args->level < 0 || args->level > 3)
404 		return (EINVAL);
405 	if ((error = priv_check(td, PRIV_IO)) != 0)
406 		return (error);
407 	if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
408 		return (error);
409 	td->td_frame->tf_eflags = (td->td_frame->tf_eflags & ~PSL_IOPL) |
410 	    (args->level * (PSL_IOPL / 3));
411 	return (0);
412 }
413 
414 int
415 linux_modify_ldt(struct thread *td, struct linux_modify_ldt_args *uap)
416 {
417 	int error;
418 	struct i386_ldt_args ldt;
419 	struct l_descriptor ld;
420 	union descriptor desc;
421 	int size, written;
422 
423 	switch (uap->func) {
424 	case 0x00: /* read_ldt */
425 		ldt.start = 0;
426 		ldt.descs = uap->ptr;
427 		ldt.num = uap->bytecount / sizeof(union descriptor);
428 		error = i386_get_ldt(td, &ldt);
429 		td->td_retval[0] *= sizeof(union descriptor);
430 		break;
431 	case 0x02: /* read_default_ldt = 0 */
432 		size = 5*sizeof(struct l_desc_struct);
433 		if (size > uap->bytecount)
434 			size = uap->bytecount;
435 		for (written = error = 0; written < size && error == 0; written++)
436 			error = subyte((char *)uap->ptr + written, 0);
437 		td->td_retval[0] = written;
438 		break;
439 	case 0x01: /* write_ldt */
440 	case 0x11: /* write_ldt */
441 		if (uap->bytecount != sizeof(ld))
442 			return (EINVAL);
443 
444 		error = copyin(uap->ptr, &ld, sizeof(ld));
445 		if (error)
446 			return (error);
447 
448 		ldt.start = ld.entry_number;
449 		ldt.descs = &desc;
450 		ldt.num = 1;
451 		desc.sd.sd_lolimit = (ld.limit & 0x0000ffff);
452 		desc.sd.sd_hilimit = (ld.limit & 0x000f0000) >> 16;
453 		desc.sd.sd_lobase = (ld.base_addr & 0x00ffffff);
454 		desc.sd.sd_hibase = (ld.base_addr & 0xff000000) >> 24;
455 		desc.sd.sd_type = SDT_MEMRO | ((ld.read_exec_only ^ 1) << 1) |
456 			(ld.contents << 2);
457 		desc.sd.sd_dpl = 3;
458 		desc.sd.sd_p = (ld.seg_not_present ^ 1);
459 		desc.sd.sd_xx = 0;
460 		desc.sd.sd_def32 = ld.seg_32bit;
461 		desc.sd.sd_gran = ld.limit_in_pages;
462 		error = i386_set_ldt(td, &ldt, &desc);
463 		break;
464 	default:
465 		error = ENOSYS;
466 		break;
467 	}
468 
469 	if (error == EOPNOTSUPP) {
470 		printf("linux: modify_ldt needs kernel option USER_LDT\n");
471 		error = ENOSYS;
472 	}
473 
474 	return (error);
475 }
476 
477 int
478 linux_sigaction(struct thread *td, struct linux_sigaction_args *args)
479 {
480 	l_osigaction_t osa;
481 	l_sigaction_t act, oact;
482 	int error;
483 
484 #ifdef DEBUG
485 	if (ldebug(sigaction))
486 		printf(ARGS(sigaction, "%d, %p, %p"),
487 		    args->sig, (void *)args->nsa, (void *)args->osa);
488 #endif
489 
490 	if (args->nsa != NULL) {
491 		error = copyin(args->nsa, &osa, sizeof(l_osigaction_t));
492 		if (error)
493 			return (error);
494 		act.lsa_handler = osa.lsa_handler;
495 		act.lsa_flags = osa.lsa_flags;
496 		act.lsa_restorer = osa.lsa_restorer;
497 		LINUX_SIGEMPTYSET(act.lsa_mask);
498 		act.lsa_mask.__mask = osa.lsa_mask;
499 	}
500 
501 	error = linux_do_sigaction(td, args->sig, args->nsa ? &act : NULL,
502 	    args->osa ? &oact : NULL);
503 
504 	if (args->osa != NULL && !error) {
505 		osa.lsa_handler = oact.lsa_handler;
506 		osa.lsa_flags = oact.lsa_flags;
507 		osa.lsa_restorer = oact.lsa_restorer;
508 		osa.lsa_mask = oact.lsa_mask.__mask;
509 		error = copyout(&osa, args->osa, sizeof(l_osigaction_t));
510 	}
511 
512 	return (error);
513 }
514 
515 /*
516  * Linux has two extra args, restart and oldmask.  We dont use these,
517  * but it seems that "restart" is actually a context pointer that
518  * enables the signal to happen with a different register set.
519  */
520 int
521 linux_sigsuspend(struct thread *td, struct linux_sigsuspend_args *args)
522 {
523 	sigset_t sigmask;
524 	l_sigset_t mask;
525 
526 #ifdef DEBUG
527 	if (ldebug(sigsuspend))
528 		printf(ARGS(sigsuspend, "%08lx"), (unsigned long)args->mask);
529 #endif
530 
531 	LINUX_SIGEMPTYSET(mask);
532 	mask.__mask = args->mask;
533 	linux_to_bsd_sigset(&mask, &sigmask);
534 	return (kern_sigsuspend(td, sigmask));
535 }
536 
537 int
538 linux_rt_sigsuspend(struct thread *td, struct linux_rt_sigsuspend_args *uap)
539 {
540 	l_sigset_t lmask;
541 	sigset_t sigmask;
542 	int error;
543 
544 #ifdef DEBUG
545 	if (ldebug(rt_sigsuspend))
546 		printf(ARGS(rt_sigsuspend, "%p, %d"),
547 		    (void *)uap->newset, uap->sigsetsize);
548 #endif
549 
550 	if (uap->sigsetsize != sizeof(l_sigset_t))
551 		return (EINVAL);
552 
553 	error = copyin(uap->newset, &lmask, sizeof(l_sigset_t));
554 	if (error)
555 		return (error);
556 
557 	linux_to_bsd_sigset(&lmask, &sigmask);
558 	return (kern_sigsuspend(td, sigmask));
559 }
560 
561 int
562 linux_pause(struct thread *td, struct linux_pause_args *args)
563 {
564 	struct proc *p = td->td_proc;
565 	sigset_t sigmask;
566 
567 #ifdef DEBUG
568 	if (ldebug(pause))
569 		printf(ARGS(pause, ""));
570 #endif
571 
572 	PROC_LOCK(p);
573 	sigmask = td->td_sigmask;
574 	PROC_UNLOCK(p);
575 	return (kern_sigsuspend(td, sigmask));
576 }
577 
578 int
579 linux_sigaltstack(struct thread *td, struct linux_sigaltstack_args *uap)
580 {
581 	stack_t ss, oss;
582 	l_stack_t lss;
583 	int error;
584 
585 #ifdef DEBUG
586 	if (ldebug(sigaltstack))
587 		printf(ARGS(sigaltstack, "%p, %p"), uap->uss, uap->uoss);
588 #endif
589 
590 	if (uap->uss != NULL) {
591 		error = copyin(uap->uss, &lss, sizeof(l_stack_t));
592 		if (error)
593 			return (error);
594 
595 		ss.ss_sp = lss.ss_sp;
596 		ss.ss_size = lss.ss_size;
597 		ss.ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags);
598 	}
599 	error = kern_sigaltstack(td, (uap->uss != NULL) ? &ss : NULL,
600 	    (uap->uoss != NULL) ? &oss : NULL);
601 	if (!error && uap->uoss != NULL) {
602 		lss.ss_sp = oss.ss_sp;
603 		lss.ss_size = oss.ss_size;
604 		lss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags);
605 		error = copyout(&lss, uap->uoss, sizeof(l_stack_t));
606 	}
607 
608 	return (error);
609 }
610 
611 int
612 linux_ftruncate64(struct thread *td, struct linux_ftruncate64_args *args)
613 {
614 
615 #ifdef DEBUG
616 	if (ldebug(ftruncate64))
617 		printf(ARGS(ftruncate64, "%u, %jd"), args->fd,
618 		    (intmax_t)args->length);
619 #endif
620 
621 	return (kern_ftruncate(td, args->fd, args->length));
622 }
623 
624 int
625 linux_set_thread_area(struct thread *td, struct linux_set_thread_area_args *args)
626 {
627 	struct l_user_desc info;
628 	int error;
629 	int idx;
630 	int a[2];
631 	struct segment_descriptor sd;
632 
633 	error = copyin(args->desc, &info, sizeof(struct l_user_desc));
634 	if (error)
635 		return (error);
636 
637 #ifdef DEBUG
638 	if (ldebug(set_thread_area))
639 		printf(ARGS(set_thread_area, "%i, %x, %x, %i, %i, %i, %i, %i, %i\n"),
640 		      info.entry_number,
641 		      info.base_addr,
642 		      info.limit,
643 		      info.seg_32bit,
644 		      info.contents,
645 		      info.read_exec_only,
646 		      info.limit_in_pages,
647 		      info.seg_not_present,
648 		      info.useable);
649 #endif
650 
651 	idx = info.entry_number;
652 	/*
653 	 * Semantics of Linux version: every thread in the system has array of
654 	 * 3 tls descriptors. 1st is GLIBC TLS, 2nd is WINE, 3rd unknown. This
655 	 * syscall loads one of the selected tls decriptors with a value and
656 	 * also loads GDT descriptors 6, 7 and 8 with the content of the
657 	 * per-thread descriptors.
658 	 *
659 	 * Semantics of FreeBSD version: I think we can ignore that Linux has 3
660 	 * per-thread descriptors and use just the 1st one. The tls_array[]
661 	 * is used only in set/get-thread_area() syscalls and for loading the
662 	 * GDT descriptors. In FreeBSD we use just one GDT descriptor for TLS
663 	 * so we will load just one.
664 	 *
665 	 * XXX: this doesn't work when a user space process tries to use more
666 	 * than 1 TLS segment. Comment in the Linux sources says wine might do
667 	 * this.
668 	 */
669 
670 	/*
671 	 * we support just GLIBC TLS now
672 	 * we should let 3 proceed as well because we use this segment so
673 	 * if code does two subsequent calls it should succeed
674 	 */
675 	if (idx != 6 && idx != -1 && idx != 3)
676 		return (EINVAL);
677 
678 	/*
679 	 * we have to copy out the GDT entry we use
680 	 * FreeBSD uses GDT entry #3 for storing %gs so load that
681 	 *
682 	 * XXX: what if a user space program doesn't check this value and tries
683 	 * to use 6, 7 or 8?
684 	 */
685 	idx = info.entry_number = 3;
686 	error = copyout(&info, args->desc, sizeof(struct l_user_desc));
687 	if (error)
688 		return (error);
689 
690 	if (LINUX_LDT_empty(&info)) {
691 		a[0] = 0;
692 		a[1] = 0;
693 	} else {
694 		a[0] = LINUX_LDT_entry_a(&info);
695 		a[1] = LINUX_LDT_entry_b(&info);
696 	}
697 
698 	memcpy(&sd, &a, sizeof(a));
699 #ifdef DEBUG
700 	if (ldebug(set_thread_area))
701 		printf("Segment created in set_thread_area: lobase: %x, hibase: %x, lolimit: %x, hilimit: %x, type: %i, dpl: %i, p: %i, xx: %i, def32: %i, gran: %i\n", sd.sd_lobase,
702 			sd.sd_hibase,
703 			sd.sd_lolimit,
704 			sd.sd_hilimit,
705 			sd.sd_type,
706 			sd.sd_dpl,
707 			sd.sd_p,
708 			sd.sd_xx,
709 			sd.sd_def32,
710 			sd.sd_gran);
711 #endif
712 
713 	/* this is taken from i386 version of cpu_set_user_tls() */
714 	critical_enter();
715 	/* set %gs */
716 	td->td_pcb->pcb_gsd = sd;
717 	PCPU_GET(fsgs_gdt)[1] = sd;
718 	load_gs(GSEL(GUGS_SEL, SEL_UPL));
719 	critical_exit();
720 
721 	return (0);
722 }
723 
724 int
725 linux_get_thread_area(struct thread *td, struct linux_get_thread_area_args *args)
726 {
727 
728 	struct l_user_desc info;
729 	int error;
730 	int idx;
731 	struct l_desc_struct desc;
732 	struct segment_descriptor sd;
733 
734 #ifdef DEBUG
735 	if (ldebug(get_thread_area))
736 		printf(ARGS(get_thread_area, "%p"), args->desc);
737 #endif
738 
739 	error = copyin(args->desc, &info, sizeof(struct l_user_desc));
740 	if (error)
741 		return (error);
742 
743 	idx = info.entry_number;
744 	/* XXX: I am not sure if we want 3 to be allowed too. */
745 	if (idx != 6 && idx != 3)
746 		return (EINVAL);
747 
748 	idx = 3;
749 
750 	memset(&info, 0, sizeof(info));
751 
752 	sd = PCPU_GET(fsgs_gdt)[1];
753 
754 	memcpy(&desc, &sd, sizeof(desc));
755 
756 	info.entry_number = idx;
757 	info.base_addr = LINUX_GET_BASE(&desc);
758 	info.limit = LINUX_GET_LIMIT(&desc);
759 	info.seg_32bit = LINUX_GET_32BIT(&desc);
760 	info.contents = LINUX_GET_CONTENTS(&desc);
761 	info.read_exec_only = !LINUX_GET_WRITABLE(&desc);
762 	info.limit_in_pages = LINUX_GET_LIMIT_PAGES(&desc);
763 	info.seg_not_present = !LINUX_GET_PRESENT(&desc);
764 	info.useable = LINUX_GET_USEABLE(&desc);
765 
766 	error = copyout(&info, args->desc, sizeof(struct l_user_desc));
767 	if (error)
768 		return (EFAULT);
769 
770 	return (0);
771 }
772 
773 /* XXX: this wont work with module - convert it */
774 int
775 linux_mq_open(struct thread *td, struct linux_mq_open_args *args)
776 {
777 #ifdef P1003_1B_MQUEUE
778 	return (sys_kmq_open(td, (struct kmq_open_args *)args));
779 #else
780 	return (ENOSYS);
781 #endif
782 }
783 
784 int
785 linux_mq_unlink(struct thread *td, struct linux_mq_unlink_args *args)
786 {
787 #ifdef P1003_1B_MQUEUE
788 	return (sys_kmq_unlink(td, (struct kmq_unlink_args *)args));
789 #else
790 	return (ENOSYS);
791 #endif
792 }
793 
794 int
795 linux_mq_timedsend(struct thread *td, struct linux_mq_timedsend_args *args)
796 {
797 #ifdef P1003_1B_MQUEUE
798 	return (sys_kmq_timedsend(td, (struct kmq_timedsend_args *)args));
799 #else
800 	return (ENOSYS);
801 #endif
802 }
803 
804 int
805 linux_mq_timedreceive(struct thread *td, struct linux_mq_timedreceive_args *args)
806 {
807 #ifdef P1003_1B_MQUEUE
808 	return (sys_kmq_timedreceive(td, (struct kmq_timedreceive_args *)args));
809 #else
810 	return (ENOSYS);
811 #endif
812 }
813 
814 int
815 linux_mq_notify(struct thread *td, struct linux_mq_notify_args *args)
816 {
817 #ifdef P1003_1B_MQUEUE
818 	return (sys_kmq_notify(td, (struct kmq_notify_args *)args));
819 #else
820 	return (ENOSYS);
821 #endif
822 }
823 
824 int
825 linux_mq_getsetattr(struct thread *td, struct linux_mq_getsetattr_args *args)
826 {
827 #ifdef P1003_1B_MQUEUE
828 	return (sys_kmq_setattr(td, (struct kmq_setattr_args *)args));
829 #else
830 	return (ENOSYS);
831 #endif
832 }
833