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