xref: /freebsd/sys/i386/linux/linux_machdep.c (revision 4bc52338)
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 = PTRIN(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(PTRIN(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 = PTRIN(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 == 0)
180 				return (EINVAL);
181 			error = copyin(PTRIN(args->ptr), &tmp, sizeof(tmp));
182 			if (error)
183 				return (error);
184 			a.msgp = PTRIN(tmp.msgp);
185 			a.msgtyp = tmp.msgtyp;
186 		} else {
187 			a.msgp = PTRIN(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 = PTRIN(args->ptr);
205 		return (linux_msgctl(td, &a));
206 	}
207 	case LINUX_SHMAT: {
208 		struct linux_shmat_args a;
209 		l_uintptr_t addr;
210 		int error;
211 
212 		a.shmid = args->arg1;
213 		a.shmaddr = PTRIN(args->ptr);
214 		a.shmflg = args->arg2;
215 		error = linux_shmat(td, &a);
216 		if (error != 0)
217 			return (error);
218 		addr = td->td_retval[0];
219 		error = copyout(&addr, PTRIN(args->arg3), sizeof(addr));
220 		td->td_retval[0] = 0;
221 		return (error);
222 	}
223 	case LINUX_SHMDT: {
224 		struct linux_shmdt_args a;
225 
226 		a.shmaddr = PTRIN(args->ptr);
227 		return (linux_shmdt(td, &a));
228 	}
229 	case LINUX_SHMGET: {
230 		struct linux_shmget_args a;
231 
232 		a.key = args->arg1;
233 		a.size = args->arg2;
234 		a.shmflg = args->arg3;
235 		return (linux_shmget(td, &a));
236 	}
237 	case LINUX_SHMCTL: {
238 		struct linux_shmctl_args a;
239 
240 		a.shmid = args->arg1;
241 		a.cmd = args->arg2;
242 		a.buf = PTRIN(args->ptr);
243 		return (linux_shmctl(td, &a));
244 	}
245 	default:
246 		break;
247 	}
248 
249 	return (EINVAL);
250 }
251 
252 int
253 linux_old_select(struct thread *td, struct linux_old_select_args *args)
254 {
255 	struct l_old_select_argv linux_args;
256 	struct linux_select_args newsel;
257 	int error;
258 
259 #ifdef DEBUG
260 	if (ldebug(old_select))
261 		printf(ARGS(old_select, "%p"), args->ptr);
262 #endif
263 
264 	error = copyin(args->ptr, &linux_args, sizeof(linux_args));
265 	if (error)
266 		return (error);
267 
268 	newsel.nfds = linux_args.nfds;
269 	newsel.readfds = linux_args.readfds;
270 	newsel.writefds = linux_args.writefds;
271 	newsel.exceptfds = linux_args.exceptfds;
272 	newsel.timeout = linux_args.timeout;
273 	return (linux_select(td, &newsel));
274 }
275 
276 int
277 linux_set_cloned_tls(struct thread *td, void *desc)
278 {
279 	struct segment_descriptor sd;
280 	struct l_user_desc info;
281 	int idx, error;
282 	int a[2];
283 
284 	error = copyin(desc, &info, sizeof(struct l_user_desc));
285 	if (error) {
286 		printf(LMSG("copyin failed!"));
287 	} else {
288 		idx = info.entry_number;
289 
290 		/*
291 		 * looks like we're getting the idx we returned
292 		 * in the set_thread_area() syscall
293 		 */
294 		if (idx != 6 && idx != 3) {
295 			printf(LMSG("resetting idx!"));
296 			idx = 3;
297 		}
298 
299 		/* this doesnt happen in practice */
300 		if (idx == 6) {
301 			/* we might copy out the entry_number as 3 */
302 			info.entry_number = 3;
303 			error = copyout(&info, desc, sizeof(struct l_user_desc));
304 			if (error)
305 				printf(LMSG("copyout failed!"));
306 		}
307 
308 		a[0] = LINUX_LDT_entry_a(&info);
309 		a[1] = LINUX_LDT_entry_b(&info);
310 
311 		memcpy(&sd, &a, sizeof(a));
312 #ifdef DEBUG
313 		if (ldebug(clone))
314 			printf("Segment created in clone with "
315 			"CLONE_SETTLS: lobase: %x, hibase: %x, "
316 			"lolimit: %x, hilimit: %x, type: %i, "
317 			"dpl: %i, p: %i, xx: %i, def32: %i, "
318 			"gran: %i\n", sd.sd_lobase, sd.sd_hibase,
319 			sd.sd_lolimit, sd.sd_hilimit, sd.sd_type,
320 			sd.sd_dpl, sd.sd_p, sd.sd_xx,
321 			sd.sd_def32, sd.sd_gran);
322 #endif
323 
324 		/* set %gs */
325 		td->td_pcb->pcb_gsd = sd;
326 		td->td_pcb->pcb_gs = GSEL(GUGS_SEL, SEL_UPL);
327 	}
328 
329 	return (error);
330 }
331 
332 int
333 linux_set_upcall_kse(struct thread *td, register_t stack)
334 {
335 
336 	if (stack)
337 		td->td_frame->tf_esp = stack;
338 
339 	/*
340 	 * The newly created Linux thread returns
341 	 * to the user space by the same path that a parent do.
342 	 */
343 	td->td_frame->tf_eax = 0;
344 	return (0);
345 }
346 
347 int
348 linux_mmap2(struct thread *td, struct linux_mmap2_args *args)
349 {
350 
351 #ifdef DEBUG
352 	if (ldebug(mmap2))
353 		printf(ARGS(mmap2, "%p, %d, %d, 0x%08x, %d, %d"),
354 		    (void *)args->addr, args->len, args->prot,
355 		    args->flags, args->fd, args->pgoff);
356 #endif
357 
358 	return (linux_mmap_common(td, args->addr, args->len, args->prot,
359 		args->flags, args->fd, (uint64_t)(uint32_t)args->pgoff *
360 		PAGE_SIZE));
361 }
362 
363 int
364 linux_mmap(struct thread *td, struct linux_mmap_args *args)
365 {
366 	int error;
367 	struct l_mmap_argv linux_args;
368 
369 	error = copyin(args->ptr, &linux_args, sizeof(linux_args));
370 	if (error)
371 		return (error);
372 
373 #ifdef DEBUG
374 	if (ldebug(mmap))
375 		printf(ARGS(mmap, "%p, %d, %d, 0x%08x, %d, %d"),
376 		    (void *)linux_args.addr, linux_args.len, linux_args.prot,
377 		    linux_args.flags, linux_args.fd, linux_args.pgoff);
378 #endif
379 
380 	return (linux_mmap_common(td, linux_args.addr, linux_args.len,
381 	    linux_args.prot, linux_args.flags, linux_args.fd,
382 	    (uint32_t)linux_args.pgoff));
383 }
384 
385 int
386 linux_mprotect(struct thread *td, struct linux_mprotect_args *uap)
387 {
388 
389 	return (linux_mprotect_common(td, PTROUT(uap->addr), uap->len, uap->prot));
390 }
391 
392 int
393 linux_ioperm(struct thread *td, struct linux_ioperm_args *args)
394 {
395 	int error;
396 	struct i386_ioperm_args iia;
397 
398 	iia.start = args->start;
399 	iia.length = args->length;
400 	iia.enable = args->enable;
401 	error = i386_set_ioperm(td, &iia);
402 	return (error);
403 }
404 
405 int
406 linux_iopl(struct thread *td, struct linux_iopl_args *args)
407 {
408 	int error;
409 
410 	if (args->level < 0 || args->level > 3)
411 		return (EINVAL);
412 	if ((error = priv_check(td, PRIV_IO)) != 0)
413 		return (error);
414 	if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
415 		return (error);
416 	td->td_frame->tf_eflags = (td->td_frame->tf_eflags & ~PSL_IOPL) |
417 	    (args->level * (PSL_IOPL / 3));
418 	return (0);
419 }
420 
421 int
422 linux_modify_ldt(struct thread *td, struct linux_modify_ldt_args *uap)
423 {
424 	int error;
425 	struct i386_ldt_args ldt;
426 	struct l_descriptor ld;
427 	union descriptor desc;
428 	int size, written;
429 
430 	switch (uap->func) {
431 	case 0x00: /* read_ldt */
432 		ldt.start = 0;
433 		ldt.descs = uap->ptr;
434 		ldt.num = uap->bytecount / sizeof(union descriptor);
435 		error = i386_get_ldt(td, &ldt);
436 		td->td_retval[0] *= sizeof(union descriptor);
437 		break;
438 	case 0x02: /* read_default_ldt = 0 */
439 		size = 5*sizeof(struct l_desc_struct);
440 		if (size > uap->bytecount)
441 			size = uap->bytecount;
442 		for (written = error = 0; written < size && error == 0; written++)
443 			error = subyte((char *)uap->ptr + written, 0);
444 		td->td_retval[0] = written;
445 		break;
446 	case 0x01: /* write_ldt */
447 	case 0x11: /* write_ldt */
448 		if (uap->bytecount != sizeof(ld))
449 			return (EINVAL);
450 
451 		error = copyin(uap->ptr, &ld, sizeof(ld));
452 		if (error)
453 			return (error);
454 
455 		ldt.start = ld.entry_number;
456 		ldt.descs = &desc;
457 		ldt.num = 1;
458 		desc.sd.sd_lolimit = (ld.limit & 0x0000ffff);
459 		desc.sd.sd_hilimit = (ld.limit & 0x000f0000) >> 16;
460 		desc.sd.sd_lobase = (ld.base_addr & 0x00ffffff);
461 		desc.sd.sd_hibase = (ld.base_addr & 0xff000000) >> 24;
462 		desc.sd.sd_type = SDT_MEMRO | ((ld.read_exec_only ^ 1) << 1) |
463 			(ld.contents << 2);
464 		desc.sd.sd_dpl = 3;
465 		desc.sd.sd_p = (ld.seg_not_present ^ 1);
466 		desc.sd.sd_xx = 0;
467 		desc.sd.sd_def32 = ld.seg_32bit;
468 		desc.sd.sd_gran = ld.limit_in_pages;
469 		error = i386_set_ldt(td, &ldt, &desc);
470 		break;
471 	default:
472 		error = ENOSYS;
473 		break;
474 	}
475 
476 	if (error == EOPNOTSUPP) {
477 		printf("linux: modify_ldt needs kernel option USER_LDT\n");
478 		error = ENOSYS;
479 	}
480 
481 	return (error);
482 }
483 
484 int
485 linux_sigaction(struct thread *td, struct linux_sigaction_args *args)
486 {
487 	l_osigaction_t osa;
488 	l_sigaction_t act, oact;
489 	int error;
490 
491 #ifdef DEBUG
492 	if (ldebug(sigaction))
493 		printf(ARGS(sigaction, "%d, %p, %p"),
494 		    args->sig, (void *)args->nsa, (void *)args->osa);
495 #endif
496 
497 	if (args->nsa != NULL) {
498 		error = copyin(args->nsa, &osa, sizeof(l_osigaction_t));
499 		if (error)
500 			return (error);
501 		act.lsa_handler = osa.lsa_handler;
502 		act.lsa_flags = osa.lsa_flags;
503 		act.lsa_restorer = osa.lsa_restorer;
504 		LINUX_SIGEMPTYSET(act.lsa_mask);
505 		act.lsa_mask.__mask = osa.lsa_mask;
506 	}
507 
508 	error = linux_do_sigaction(td, args->sig, args->nsa ? &act : NULL,
509 	    args->osa ? &oact : NULL);
510 
511 	if (args->osa != NULL && !error) {
512 		osa.lsa_handler = oact.lsa_handler;
513 		osa.lsa_flags = oact.lsa_flags;
514 		osa.lsa_restorer = oact.lsa_restorer;
515 		osa.lsa_mask = oact.lsa_mask.__mask;
516 		error = copyout(&osa, args->osa, sizeof(l_osigaction_t));
517 	}
518 
519 	return (error);
520 }
521 
522 /*
523  * Linux has two extra args, restart and oldmask.  We dont use these,
524  * but it seems that "restart" is actually a context pointer that
525  * enables the signal to happen with a different register set.
526  */
527 int
528 linux_sigsuspend(struct thread *td, struct linux_sigsuspend_args *args)
529 {
530 	sigset_t sigmask;
531 	l_sigset_t mask;
532 
533 #ifdef DEBUG
534 	if (ldebug(sigsuspend))
535 		printf(ARGS(sigsuspend, "%08lx"), (unsigned long)args->mask);
536 #endif
537 
538 	LINUX_SIGEMPTYSET(mask);
539 	mask.__mask = args->mask;
540 	linux_to_bsd_sigset(&mask, &sigmask);
541 	return (kern_sigsuspend(td, sigmask));
542 }
543 
544 int
545 linux_rt_sigsuspend(struct thread *td, struct linux_rt_sigsuspend_args *uap)
546 {
547 	l_sigset_t lmask;
548 	sigset_t sigmask;
549 	int error;
550 
551 #ifdef DEBUG
552 	if (ldebug(rt_sigsuspend))
553 		printf(ARGS(rt_sigsuspend, "%p, %d"),
554 		    (void *)uap->newset, uap->sigsetsize);
555 #endif
556 
557 	if (uap->sigsetsize != sizeof(l_sigset_t))
558 		return (EINVAL);
559 
560 	error = copyin(uap->newset, &lmask, sizeof(l_sigset_t));
561 	if (error)
562 		return (error);
563 
564 	linux_to_bsd_sigset(&lmask, &sigmask);
565 	return (kern_sigsuspend(td, sigmask));
566 }
567 
568 int
569 linux_pause(struct thread *td, struct linux_pause_args *args)
570 {
571 	struct proc *p = td->td_proc;
572 	sigset_t sigmask;
573 
574 #ifdef DEBUG
575 	if (ldebug(pause))
576 		printf(ARGS(pause, ""));
577 #endif
578 
579 	PROC_LOCK(p);
580 	sigmask = td->td_sigmask;
581 	PROC_UNLOCK(p);
582 	return (kern_sigsuspend(td, sigmask));
583 }
584 
585 int
586 linux_sigaltstack(struct thread *td, struct linux_sigaltstack_args *uap)
587 {
588 	stack_t ss, oss;
589 	l_stack_t lss;
590 	int error;
591 
592 #ifdef DEBUG
593 	if (ldebug(sigaltstack))
594 		printf(ARGS(sigaltstack, "%p, %p"), uap->uss, uap->uoss);
595 #endif
596 
597 	if (uap->uss != NULL) {
598 		error = copyin(uap->uss, &lss, sizeof(l_stack_t));
599 		if (error)
600 			return (error);
601 
602 		ss.ss_sp = lss.ss_sp;
603 		ss.ss_size = lss.ss_size;
604 		ss.ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags);
605 	}
606 	error = kern_sigaltstack(td, (uap->uss != NULL) ? &ss : NULL,
607 	    (uap->uoss != NULL) ? &oss : NULL);
608 	if (!error && uap->uoss != NULL) {
609 		lss.ss_sp = oss.ss_sp;
610 		lss.ss_size = oss.ss_size;
611 		lss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags);
612 		error = copyout(&lss, uap->uoss, sizeof(l_stack_t));
613 	}
614 
615 	return (error);
616 }
617 
618 int
619 linux_ftruncate64(struct thread *td, struct linux_ftruncate64_args *args)
620 {
621 
622 #ifdef DEBUG
623 	if (ldebug(ftruncate64))
624 		printf(ARGS(ftruncate64, "%u, %jd"), args->fd,
625 		    (intmax_t)args->length);
626 #endif
627 
628 	return (kern_ftruncate(td, args->fd, args->length));
629 }
630 
631 int
632 linux_set_thread_area(struct thread *td, struct linux_set_thread_area_args *args)
633 {
634 	struct l_user_desc info;
635 	int error;
636 	int idx;
637 	int a[2];
638 	struct segment_descriptor sd;
639 
640 	error = copyin(args->desc, &info, sizeof(struct l_user_desc));
641 	if (error)
642 		return (error);
643 
644 #ifdef DEBUG
645 	if (ldebug(set_thread_area))
646 		printf(ARGS(set_thread_area, "%i, %x, %x, %i, %i, %i, %i, %i, %i\n"),
647 		      info.entry_number,
648 		      info.base_addr,
649 		      info.limit,
650 		      info.seg_32bit,
651 		      info.contents,
652 		      info.read_exec_only,
653 		      info.limit_in_pages,
654 		      info.seg_not_present,
655 		      info.useable);
656 #endif
657 
658 	idx = info.entry_number;
659 	/*
660 	 * Semantics of Linux version: every thread in the system has array of
661 	 * 3 tls descriptors. 1st is GLIBC TLS, 2nd is WINE, 3rd unknown. This
662 	 * syscall loads one of the selected tls decriptors with a value and
663 	 * also loads GDT descriptors 6, 7 and 8 with the content of the
664 	 * per-thread descriptors.
665 	 *
666 	 * Semantics of FreeBSD version: I think we can ignore that Linux has 3
667 	 * per-thread descriptors and use just the 1st one. The tls_array[]
668 	 * is used only in set/get-thread_area() syscalls and for loading the
669 	 * GDT descriptors. In FreeBSD we use just one GDT descriptor for TLS
670 	 * so we will load just one.
671 	 *
672 	 * XXX: this doesn't work when a user space process tries to use more
673 	 * than 1 TLS segment. Comment in the Linux sources says wine might do
674 	 * this.
675 	 */
676 
677 	/*
678 	 * we support just GLIBC TLS now
679 	 * we should let 3 proceed as well because we use this segment so
680 	 * if code does two subsequent calls it should succeed
681 	 */
682 	if (idx != 6 && idx != -1 && idx != 3)
683 		return (EINVAL);
684 
685 	/*
686 	 * we have to copy out the GDT entry we use
687 	 * FreeBSD uses GDT entry #3 for storing %gs so load that
688 	 *
689 	 * XXX: what if a user space program doesn't check this value and tries
690 	 * to use 6, 7 or 8?
691 	 */
692 	idx = info.entry_number = 3;
693 	error = copyout(&info, args->desc, sizeof(struct l_user_desc));
694 	if (error)
695 		return (error);
696 
697 	if (LINUX_LDT_empty(&info)) {
698 		a[0] = 0;
699 		a[1] = 0;
700 	} else {
701 		a[0] = LINUX_LDT_entry_a(&info);
702 		a[1] = LINUX_LDT_entry_b(&info);
703 	}
704 
705 	memcpy(&sd, &a, sizeof(a));
706 #ifdef DEBUG
707 	if (ldebug(set_thread_area))
708 		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,
709 			sd.sd_hibase,
710 			sd.sd_lolimit,
711 			sd.sd_hilimit,
712 			sd.sd_type,
713 			sd.sd_dpl,
714 			sd.sd_p,
715 			sd.sd_xx,
716 			sd.sd_def32,
717 			sd.sd_gran);
718 #endif
719 
720 	/* this is taken from i386 version of cpu_set_user_tls() */
721 	critical_enter();
722 	/* set %gs */
723 	td->td_pcb->pcb_gsd = sd;
724 	PCPU_GET(fsgs_gdt)[1] = sd;
725 	load_gs(GSEL(GUGS_SEL, SEL_UPL));
726 	critical_exit();
727 
728 	return (0);
729 }
730 
731 int
732 linux_get_thread_area(struct thread *td, struct linux_get_thread_area_args *args)
733 {
734 
735 	struct l_user_desc info;
736 	int error;
737 	int idx;
738 	struct l_desc_struct desc;
739 	struct segment_descriptor sd;
740 
741 #ifdef DEBUG
742 	if (ldebug(get_thread_area))
743 		printf(ARGS(get_thread_area, "%p"), args->desc);
744 #endif
745 
746 	error = copyin(args->desc, &info, sizeof(struct l_user_desc));
747 	if (error)
748 		return (error);
749 
750 	idx = info.entry_number;
751 	/* XXX: I am not sure if we want 3 to be allowed too. */
752 	if (idx != 6 && idx != 3)
753 		return (EINVAL);
754 
755 	idx = 3;
756 
757 	memset(&info, 0, sizeof(info));
758 
759 	sd = PCPU_GET(fsgs_gdt)[1];
760 
761 	memcpy(&desc, &sd, sizeof(desc));
762 
763 	info.entry_number = idx;
764 	info.base_addr = LINUX_GET_BASE(&desc);
765 	info.limit = LINUX_GET_LIMIT(&desc);
766 	info.seg_32bit = LINUX_GET_32BIT(&desc);
767 	info.contents = LINUX_GET_CONTENTS(&desc);
768 	info.read_exec_only = !LINUX_GET_WRITABLE(&desc);
769 	info.limit_in_pages = LINUX_GET_LIMIT_PAGES(&desc);
770 	info.seg_not_present = !LINUX_GET_PRESENT(&desc);
771 	info.useable = LINUX_GET_USEABLE(&desc);
772 
773 	error = copyout(&info, args->desc, sizeof(struct l_user_desc));
774 	if (error)
775 		return (EFAULT);
776 
777 	return (0);
778 }
779 
780 /* XXX: this wont work with module - convert it */
781 int
782 linux_mq_open(struct thread *td, struct linux_mq_open_args *args)
783 {
784 #ifdef P1003_1B_MQUEUE
785 	return (sys_kmq_open(td, (struct kmq_open_args *)args));
786 #else
787 	return (ENOSYS);
788 #endif
789 }
790 
791 int
792 linux_mq_unlink(struct thread *td, struct linux_mq_unlink_args *args)
793 {
794 #ifdef P1003_1B_MQUEUE
795 	return (sys_kmq_unlink(td, (struct kmq_unlink_args *)args));
796 #else
797 	return (ENOSYS);
798 #endif
799 }
800 
801 int
802 linux_mq_timedsend(struct thread *td, struct linux_mq_timedsend_args *args)
803 {
804 #ifdef P1003_1B_MQUEUE
805 	return (sys_kmq_timedsend(td, (struct kmq_timedsend_args *)args));
806 #else
807 	return (ENOSYS);
808 #endif
809 }
810 
811 int
812 linux_mq_timedreceive(struct thread *td, struct linux_mq_timedreceive_args *args)
813 {
814 #ifdef P1003_1B_MQUEUE
815 	return (sys_kmq_timedreceive(td, (struct kmq_timedreceive_args *)args));
816 #else
817 	return (ENOSYS);
818 #endif
819 }
820 
821 int
822 linux_mq_notify(struct thread *td, struct linux_mq_notify_args *args)
823 {
824 #ifdef P1003_1B_MQUEUE
825 	return (sys_kmq_notify(td, (struct kmq_notify_args *)args));
826 #else
827 	return (ENOSYS);
828 #endif
829 }
830 
831 int
832 linux_mq_getsetattr(struct thread *td, struct linux_mq_getsetattr_args *args)
833 {
834 #ifdef P1003_1B_MQUEUE
835 	return (sys_kmq_setattr(td, (struct kmq_setattr_args *)args));
836 #else
837 	return (ENOSYS);
838 #endif
839 }
840