linux_machdep.c revision 302964
1/*-
2 * Copyright (c) 2000 Marcel Moolenaar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer
10 *    in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 *    derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD: stable/10/sys/i386/linux/linux_machdep.c 302964 2016-07-17 15:23:32Z dchagin $");
31
32#include <sys/param.h>
33#include <sys/systm.h>
34#include <sys/capsicum.h>
35#include <sys/file.h>
36#include <sys/fcntl.h>
37#include <sys/imgact.h>
38#include <sys/lock.h>
39#include <sys/malloc.h>
40#include <sys/mman.h>
41#include <sys/mutex.h>
42#include <sys/sx.h>
43#include <sys/priv.h>
44#include <sys/proc.h>
45#include <sys/queue.h>
46#include <sys/resource.h>
47#include <sys/resourcevar.h>
48#include <sys/signalvar.h>
49#include <sys/syscallsubr.h>
50#include <sys/sysproto.h>
51#include <sys/unistd.h>
52#include <sys/wait.h>
53#include <sys/sched.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/vm.h>
61#include <vm/pmap.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_ipc.h>
67#include <compat/linux/linux_misc.h>
68#include <compat/linux/linux_mmap.h>
69#include <compat/linux/linux_signal.h>
70#include <compat/linux/linux_util.h>
71#include <compat/linux/linux_emul.h>
72
73#include <i386/include/pcb.h>			/* needed for pcb definition in linux_set_thread_area */
74
75#include "opt_posix.h"
76
77extern struct sysentvec elf32_freebsd_sysvec;	/* defined in i386/i386/elf_machdep.c */
78
79struct 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
91struct 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
100int
101linux_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
122struct l_ipc_kludge {
123	struct l_msgbuf *msgp;
124	l_long msgtyp;
125};
126
127int
128linux_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
245int
246linux_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
269int
270linux_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
325int
326linux_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
340int
341linux_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
356int
357linux_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
378int
379linux_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
385int
386linux_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
398int
399linux_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
414int
415linux_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
477int
478linux_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 */
520int
521linux_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
537int
538linux_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
561int
562linux_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
578int
579linux_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
611int
612linux_ftruncate64(struct thread *td, struct linux_ftruncate64_args *args)
613{
614	struct ftruncate_args sa;
615
616#ifdef DEBUG
617	if (ldebug(ftruncate64))
618		printf(ARGS(ftruncate64, "%u, %jd"), args->fd,
619		    (intmax_t)args->length);
620#endif
621
622	sa.fd = args->fd;
623	sa.length = args->length;
624	return sys_ftruncate(td, &sa);
625}
626
627int
628linux_set_thread_area(struct thread *td, struct linux_set_thread_area_args *args)
629{
630	struct l_user_desc info;
631	int error;
632	int idx;
633	int a[2];
634	struct segment_descriptor sd;
635
636	error = copyin(args->desc, &info, sizeof(struct l_user_desc));
637	if (error)
638		return (error);
639
640#ifdef DEBUG
641	if (ldebug(set_thread_area))
642	   	printf(ARGS(set_thread_area, "%i, %x, %x, %i, %i, %i, %i, %i, %i\n"),
643		      info.entry_number,
644      		      info.base_addr,
645      		      info.limit,
646      		      info.seg_32bit,
647		      info.contents,
648      		      info.read_exec_only,
649      		      info.limit_in_pages,
650      		      info.seg_not_present,
651      		      info.useable);
652#endif
653
654	idx = info.entry_number;
655	/*
656	 * Semantics of linux version: every thread in the system has array of
657	 * 3 tls descriptors. 1st is GLIBC TLS, 2nd is WINE, 3rd unknown. This
658	 * syscall loads one of the selected tls decriptors with a value and
659	 * also loads GDT descriptors 6, 7 and 8 with the content of the
660	 * per-thread descriptors.
661	 *
662	 * Semantics of fbsd version: I think we can ignore that linux has 3
663	 * per-thread descriptors and use just the 1st one. The tls_array[]
664	 * is used only in set/get-thread_area() syscalls and for loading the
665	 * GDT descriptors. In fbsd we use just one GDT descriptor for TLS so
666	 * we will load just one.
667	 *
668	 * XXX: this doesn't work when a user space process tries to use more
669	 * than 1 TLS segment. Comment in the linux sources says wine might do
670	 * this.
671	 */
672
673	/*
674	 * we support just GLIBC TLS now
675	 * we should let 3 proceed as well because we use this segment so
676	 * if code does two subsequent calls it should succeed
677	 */
678	if (idx != 6 && idx != -1 && idx != 3)
679		return (EINVAL);
680
681	/*
682	 * we have to copy out the GDT entry we use
683	 * FreeBSD uses GDT entry #3 for storing %gs so load that
684	 *
685	 * XXX: what if a user space program doesn't check this value and tries
686	 * to use 6, 7 or 8?
687	 */
688	idx = info.entry_number = 3;
689	error = copyout(&info, args->desc, sizeof(struct l_user_desc));
690	if (error)
691		return (error);
692
693	if (LINUX_LDT_empty(&info)) {
694		a[0] = 0;
695		a[1] = 0;
696	} else {
697		a[0] = LINUX_LDT_entry_a(&info);
698		a[1] = LINUX_LDT_entry_b(&info);
699	}
700
701	memcpy(&sd, &a, sizeof(a));
702#ifdef DEBUG
703	if (ldebug(set_thread_area))
704	   	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,
705			sd.sd_hibase,
706			sd.sd_lolimit,
707			sd.sd_hilimit,
708			sd.sd_type,
709			sd.sd_dpl,
710			sd.sd_p,
711			sd.sd_xx,
712			sd.sd_def32,
713			sd.sd_gran);
714#endif
715
716	/* this is taken from i386 version of cpu_set_user_tls() */
717	critical_enter();
718	/* set %gs */
719	td->td_pcb->pcb_gsd = sd;
720	PCPU_GET(fsgs_gdt)[1] = sd;
721	load_gs(GSEL(GUGS_SEL, SEL_UPL));
722	critical_exit();
723
724	return (0);
725}
726
727int
728linux_get_thread_area(struct thread *td, struct linux_get_thread_area_args *args)
729{
730
731	struct l_user_desc info;
732	int error;
733	int idx;
734	struct l_desc_struct desc;
735	struct segment_descriptor sd;
736
737#ifdef DEBUG
738	if (ldebug(get_thread_area))
739		printf(ARGS(get_thread_area, "%p"), args->desc);
740#endif
741
742	error = copyin(args->desc, &info, sizeof(struct l_user_desc));
743	if (error)
744		return (error);
745
746	idx = info.entry_number;
747	/* XXX: I am not sure if we want 3 to be allowed too. */
748	if (idx != 6 && idx != 3)
749		return (EINVAL);
750
751	idx = 3;
752
753	memset(&info, 0, sizeof(info));
754
755	sd = PCPU_GET(fsgs_gdt)[1];
756
757	memcpy(&desc, &sd, sizeof(desc));
758
759	info.entry_number = idx;
760	info.base_addr = LINUX_GET_BASE(&desc);
761	info.limit = LINUX_GET_LIMIT(&desc);
762	info.seg_32bit = LINUX_GET_32BIT(&desc);
763	info.contents = LINUX_GET_CONTENTS(&desc);
764	info.read_exec_only = !LINUX_GET_WRITABLE(&desc);
765	info.limit_in_pages = LINUX_GET_LIMIT_PAGES(&desc);
766	info.seg_not_present = !LINUX_GET_PRESENT(&desc);
767	info.useable = LINUX_GET_USEABLE(&desc);
768
769	error = copyout(&info, args->desc, sizeof(struct l_user_desc));
770	if (error)
771	   	return (EFAULT);
772
773	return (0);
774}
775
776/* XXX: this wont work with module - convert it */
777int
778linux_mq_open(struct thread *td, struct linux_mq_open_args *args)
779{
780#ifdef P1003_1B_MQUEUE
781   	return sys_kmq_open(td, (struct kmq_open_args *) args);
782#else
783	return (ENOSYS);
784#endif
785}
786
787int
788linux_mq_unlink(struct thread *td, struct linux_mq_unlink_args *args)
789{
790#ifdef P1003_1B_MQUEUE
791   	return sys_kmq_unlink(td, (struct kmq_unlink_args *) args);
792#else
793	return (ENOSYS);
794#endif
795}
796
797int
798linux_mq_timedsend(struct thread *td, struct linux_mq_timedsend_args *args)
799{
800#ifdef P1003_1B_MQUEUE
801   	return sys_kmq_timedsend(td, (struct kmq_timedsend_args *) args);
802#else
803	return (ENOSYS);
804#endif
805}
806
807int
808linux_mq_timedreceive(struct thread *td, struct linux_mq_timedreceive_args *args)
809{
810#ifdef P1003_1B_MQUEUE
811   	return sys_kmq_timedreceive(td, (struct kmq_timedreceive_args *) args);
812#else
813	return (ENOSYS);
814#endif
815}
816
817int
818linux_mq_notify(struct thread *td, struct linux_mq_notify_args *args)
819{
820#ifdef P1003_1B_MQUEUE
821	return sys_kmq_notify(td, (struct kmq_notify_args *) args);
822#else
823	return (ENOSYS);
824#endif
825}
826
827int
828linux_mq_getsetattr(struct thread *td, struct linux_mq_getsetattr_args *args)
829{
830#ifdef P1003_1B_MQUEUE
831   	return sys_kmq_setattr(td, (struct kmq_setattr_args *) args);
832#else
833	return (ENOSYS);
834#endif
835}
836