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$");
31
32#include <sys/param.h>
33#include <sys/systm.h>
34#include <sys/capability.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_signal.h>
69#include <compat/linux/linux_util.h>
70#include <compat/linux/linux_emul.h>
71
72#include <i386/include/pcb.h>			/* needed for pcb definition in linux_set_thread_area */
73
74#include "opt_posix.h"
75
76extern struct sysentvec elf32_freebsd_sysvec;	/* defined in i386/i386/elf_machdep.c */
77
78struct l_descriptor {
79	l_uint		entry_number;
80	l_ulong		base_addr;
81	l_uint		limit;
82	l_uint		seg_32bit:1;
83	l_uint		contents:2;
84	l_uint		read_exec_only:1;
85	l_uint		limit_in_pages:1;
86	l_uint		seg_not_present:1;
87	l_uint		useable:1;
88};
89
90struct l_old_select_argv {
91	l_int		nfds;
92	l_fd_set	*readfds;
93	l_fd_set	*writefds;
94	l_fd_set	*exceptfds;
95	struct l_timeval	*timeout;
96};
97
98static int	linux_mmap_common(struct thread *td, l_uintptr_t addr,
99		    l_size_t len, l_int prot, l_int flags, l_int fd,
100		    l_loff_t pos);
101
102int
103linux_to_bsd_sigaltstack(int lsa)
104{
105	int bsa = 0;
106
107	if (lsa & LINUX_SS_DISABLE)
108		bsa |= SS_DISABLE;
109	if (lsa & LINUX_SS_ONSTACK)
110		bsa |= SS_ONSTACK;
111	return (bsa);
112}
113
114int
115bsd_to_linux_sigaltstack(int bsa)
116{
117	int lsa = 0;
118
119	if (bsa & SS_DISABLE)
120		lsa |= LINUX_SS_DISABLE;
121	if (bsa & SS_ONSTACK)
122		lsa |= LINUX_SS_ONSTACK;
123	return (lsa);
124}
125
126int
127linux_execve(struct thread *td, struct linux_execve_args *args)
128{
129	int error;
130	char *newpath;
131	struct image_args eargs;
132
133	LCONVPATHEXIST(td, args->path, &newpath);
134
135#ifdef DEBUG
136	if (ldebug(execve))
137		printf(ARGS(execve, "%s"), newpath);
138#endif
139
140	error = exec_copyin_args(&eargs, newpath, UIO_SYSSPACE,
141	    args->argp, args->envp);
142	free(newpath, M_TEMP);
143	if (error == 0)
144		error = kern_execve(td, &eargs, NULL);
145	if (error == 0)
146	   	/* linux process can exec fbsd one, dont attempt
147		 * to create emuldata for such process using
148		 * linux_proc_init, this leads to a panic on KASSERT
149		 * because such process has p->p_emuldata == NULL
150		 */
151		if (SV_PROC_ABI(td->td_proc) == SV_ABI_LINUX)
152   			error = linux_proc_init(td, 0, 0);
153	return (error);
154}
155
156struct l_ipc_kludge {
157	struct l_msgbuf *msgp;
158	l_long msgtyp;
159};
160
161int
162linux_ipc(struct thread *td, struct linux_ipc_args *args)
163{
164
165	switch (args->what & 0xFFFF) {
166	case LINUX_SEMOP: {
167		struct linux_semop_args a;
168
169		a.semid = args->arg1;
170		a.tsops = args->ptr;
171		a.nsops = args->arg2;
172		return (linux_semop(td, &a));
173	}
174	case LINUX_SEMGET: {
175		struct linux_semget_args a;
176
177		a.key = args->arg1;
178		a.nsems = args->arg2;
179		a.semflg = args->arg3;
180		return (linux_semget(td, &a));
181	}
182	case LINUX_SEMCTL: {
183		struct linux_semctl_args a;
184		int error;
185
186		a.semid = args->arg1;
187		a.semnum = args->arg2;
188		a.cmd = args->arg3;
189		error = copyin(args->ptr, &a.arg, sizeof(a.arg));
190		if (error)
191			return (error);
192		return (linux_semctl(td, &a));
193	}
194	case LINUX_MSGSND: {
195		struct linux_msgsnd_args a;
196
197		a.msqid = args->arg1;
198		a.msgp = args->ptr;
199		a.msgsz = args->arg2;
200		a.msgflg = args->arg3;
201		return (linux_msgsnd(td, &a));
202	}
203	case LINUX_MSGRCV: {
204		struct linux_msgrcv_args a;
205
206		a.msqid = args->arg1;
207		a.msgsz = args->arg2;
208		a.msgflg = args->arg3;
209		if ((args->what >> 16) == 0) {
210			struct l_ipc_kludge tmp;
211			int error;
212
213			if (args->ptr == NULL)
214				return (EINVAL);
215			error = copyin(args->ptr, &tmp, sizeof(tmp));
216			if (error)
217				return (error);
218			a.msgp = tmp.msgp;
219			a.msgtyp = tmp.msgtyp;
220		} else {
221			a.msgp = args->ptr;
222			a.msgtyp = args->arg5;
223		}
224		return (linux_msgrcv(td, &a));
225	}
226	case LINUX_MSGGET: {
227		struct linux_msgget_args a;
228
229		a.key = args->arg1;
230		a.msgflg = args->arg2;
231		return (linux_msgget(td, &a));
232	}
233	case LINUX_MSGCTL: {
234		struct linux_msgctl_args a;
235
236		a.msqid = args->arg1;
237		a.cmd = args->arg2;
238		a.buf = args->ptr;
239		return (linux_msgctl(td, &a));
240	}
241	case LINUX_SHMAT: {
242		struct linux_shmat_args a;
243
244		a.shmid = args->arg1;
245		a.shmaddr = args->ptr;
246		a.shmflg = args->arg2;
247		a.raddr = (l_ulong *)args->arg3;
248		return (linux_shmat(td, &a));
249	}
250	case LINUX_SHMDT: {
251		struct linux_shmdt_args a;
252
253		a.shmaddr = args->ptr;
254		return (linux_shmdt(td, &a));
255	}
256	case LINUX_SHMGET: {
257		struct linux_shmget_args a;
258
259		a.key = args->arg1;
260		a.size = args->arg2;
261		a.shmflg = args->arg3;
262		return (linux_shmget(td, &a));
263	}
264	case LINUX_SHMCTL: {
265		struct linux_shmctl_args a;
266
267		a.shmid = args->arg1;
268		a.cmd = args->arg2;
269		a.buf = args->ptr;
270		return (linux_shmctl(td, &a));
271	}
272	default:
273		break;
274	}
275
276	return (EINVAL);
277}
278
279int
280linux_old_select(struct thread *td, struct linux_old_select_args *args)
281{
282	struct l_old_select_argv linux_args;
283	struct linux_select_args newsel;
284	int error;
285
286#ifdef DEBUG
287	if (ldebug(old_select))
288		printf(ARGS(old_select, "%p"), args->ptr);
289#endif
290
291	error = copyin(args->ptr, &linux_args, sizeof(linux_args));
292	if (error)
293		return (error);
294
295	newsel.nfds = linux_args.nfds;
296	newsel.readfds = linux_args.readfds;
297	newsel.writefds = linux_args.writefds;
298	newsel.exceptfds = linux_args.exceptfds;
299	newsel.timeout = linux_args.timeout;
300	return (linux_select(td, &newsel));
301}
302
303int
304linux_set_cloned_tls(struct thread *td, void *desc)
305{
306	struct segment_descriptor sd;
307	struct l_user_desc info;
308	int idx, error;
309	int a[2];
310
311	error = copyin(desc, &info, sizeof(struct l_user_desc));
312	if (error) {
313		printf(LMSG("copyin failed!"));
314	} else {
315		idx = info.entry_number;
316
317		/*
318		 * looks like we're getting the idx we returned
319		 * in the set_thread_area() syscall
320		 */
321		if (idx != 6 && idx != 3) {
322			printf(LMSG("resetting idx!"));
323			idx = 3;
324		}
325
326		/* this doesnt happen in practice */
327		if (idx == 6) {
328	   		/* we might copy out the entry_number as 3 */
329		   	info.entry_number = 3;
330			error = copyout(&info, desc, sizeof(struct l_user_desc));
331			if (error)
332				printf(LMSG("copyout failed!"));
333		}
334
335		a[0] = LINUX_LDT_entry_a(&info);
336		a[1] = LINUX_LDT_entry_b(&info);
337
338		memcpy(&sd, &a, sizeof(a));
339#ifdef DEBUG
340		if (ldebug(clone))
341			printf("Segment created in clone with "
342			"CLONE_SETTLS: lobase: %x, hibase: %x, "
343			"lolimit: %x, hilimit: %x, type: %i, "
344			"dpl: %i, p: %i, xx: %i, def32: %i, "
345			"gran: %i\n", sd.sd_lobase, sd.sd_hibase,
346			sd.sd_lolimit, sd.sd_hilimit, sd.sd_type,
347			sd.sd_dpl, sd.sd_p, sd.sd_xx,
348			sd.sd_def32, sd.sd_gran);
349#endif
350
351		/* set %gs */
352		td->td_pcb->pcb_gsd = sd;
353		td->td_pcb->pcb_gs = GSEL(GUGS_SEL, SEL_UPL);
354	}
355
356	return (error);
357}
358
359int
360linux_set_upcall_kse(struct thread *td, register_t stack)
361{
362
363	td->td_frame->tf_esp = stack;
364
365	return (0);
366}
367
368#define STACK_SIZE  (2 * 1024 * 1024)
369#define GUARD_SIZE  (4 * PAGE_SIZE)
370
371int
372linux_mmap2(struct thread *td, struct linux_mmap2_args *args)
373{
374
375#ifdef DEBUG
376	if (ldebug(mmap2))
377		printf(ARGS(mmap2, "%p, %d, %d, 0x%08x, %d, %d"),
378		    (void *)args->addr, args->len, args->prot,
379		    args->flags, args->fd, args->pgoff);
380#endif
381
382	return (linux_mmap_common(td, args->addr, args->len, args->prot,
383		args->flags, args->fd, (uint64_t)(uint32_t)args->pgoff *
384		PAGE_SIZE));
385}
386
387int
388linux_mmap(struct thread *td, struct linux_mmap_args *args)
389{
390	int error;
391	struct l_mmap_argv linux_args;
392
393	error = copyin(args->ptr, &linux_args, sizeof(linux_args));
394	if (error)
395		return (error);
396
397#ifdef DEBUG
398	if (ldebug(mmap))
399		printf(ARGS(mmap, "%p, %d, %d, 0x%08x, %d, %d"),
400		    (void *)linux_args.addr, linux_args.len, linux_args.prot,
401		    linux_args.flags, linux_args.fd, linux_args.pgoff);
402#endif
403
404	return (linux_mmap_common(td, linux_args.addr, linux_args.len,
405	    linux_args.prot, linux_args.flags, linux_args.fd,
406	    (uint32_t)linux_args.pgoff));
407}
408
409static int
410linux_mmap_common(struct thread *td, l_uintptr_t addr, l_size_t len, l_int prot,
411    l_int flags, l_int fd, l_loff_t pos)
412{
413	struct proc *p = td->td_proc;
414	struct mmap_args /* {
415		caddr_t addr;
416		size_t len;
417		int prot;
418		int flags;
419		int fd;
420		long pad;
421		off_t pos;
422	} */ bsd_args;
423	int error;
424	struct file *fp;
425	cap_rights_t rights;
426
427	error = 0;
428	bsd_args.flags = 0;
429	fp = NULL;
430
431	/*
432	 * Linux mmap(2):
433	 * You must specify exactly one of MAP_SHARED and MAP_PRIVATE
434	 */
435	if (!((flags & LINUX_MAP_SHARED) ^ (flags & LINUX_MAP_PRIVATE)))
436		return (EINVAL);
437
438	if (flags & LINUX_MAP_SHARED)
439		bsd_args.flags |= MAP_SHARED;
440	if (flags & LINUX_MAP_PRIVATE)
441		bsd_args.flags |= MAP_PRIVATE;
442	if (flags & LINUX_MAP_FIXED)
443		bsd_args.flags |= MAP_FIXED;
444	if (flags & LINUX_MAP_ANON) {
445		/* Enforce pos to be on page boundary, then ignore. */
446		if ((pos & PAGE_MASK) != 0)
447			return (EINVAL);
448		pos = 0;
449		bsd_args.flags |= MAP_ANON;
450	} else
451		bsd_args.flags |= MAP_NOSYNC;
452	if (flags & LINUX_MAP_GROWSDOWN)
453		bsd_args.flags |= MAP_STACK;
454
455	/*
456	 * PROT_READ, PROT_WRITE, or PROT_EXEC implies PROT_READ and PROT_EXEC
457	 * on Linux/i386. We do this to ensure maximum compatibility.
458	 * Linux/ia64 does the same in i386 emulation mode.
459	 */
460	bsd_args.prot = prot;
461	if (bsd_args.prot & (PROT_READ | PROT_WRITE | PROT_EXEC))
462		bsd_args.prot |= PROT_READ | PROT_EXEC;
463
464	/* Linux does not check file descriptor when MAP_ANONYMOUS is set. */
465	bsd_args.fd = (bsd_args.flags & MAP_ANON) ? -1 : fd;
466	if (bsd_args.fd != -1) {
467		/*
468		 * Linux follows Solaris mmap(2) description:
469		 * The file descriptor fildes is opened with
470		 * read permission, regardless of the
471		 * protection options specified.
472		 *
473		 * Checking just CAP_MMAP is fine here, since the real work
474		 * is done in the FreeBSD mmap().
475		 */
476
477		error = fget(td, bsd_args.fd,
478		    cap_rights_init(&rights, CAP_MMAP), &fp);
479		if (error != 0)
480			return (error);
481		if (fp->f_type != DTYPE_VNODE) {
482			fdrop(fp, td);
483			return (EINVAL);
484		}
485
486		/* Linux mmap() just fails for O_WRONLY files */
487		if (!(fp->f_flag & FREAD)) {
488			fdrop(fp, td);
489			return (EACCES);
490		}
491
492		fdrop(fp, td);
493	}
494
495	if (flags & LINUX_MAP_GROWSDOWN) {
496		/*
497		 * The Linux MAP_GROWSDOWN option does not limit auto
498		 * growth of the region.  Linux mmap with this option
499		 * takes as addr the inital BOS, and as len, the initial
500		 * region size.  It can then grow down from addr without
501		 * limit.  However, linux threads has an implicit internal
502		 * limit to stack size of STACK_SIZE.  Its just not
503		 * enforced explicitly in linux.  But, here we impose
504		 * a limit of (STACK_SIZE - GUARD_SIZE) on the stack
505		 * region, since we can do this with our mmap.
506		 *
507		 * Our mmap with MAP_STACK takes addr as the maximum
508		 * downsize limit on BOS, and as len the max size of
509		 * the region.  It them maps the top SGROWSIZ bytes,
510		 * and auto grows the region down, up to the limit
511		 * in addr.
512		 *
513		 * If we don't use the MAP_STACK option, the effect
514		 * of this code is to allocate a stack region of a
515		 * fixed size of (STACK_SIZE - GUARD_SIZE).
516		 */
517
518		if ((caddr_t)PTRIN(addr) + len > p->p_vmspace->vm_maxsaddr) {
519			/*
520			 * Some linux apps will attempt to mmap
521			 * thread stacks near the top of their
522			 * address space.  If their TOS is greater
523			 * than vm_maxsaddr, vm_map_growstack()
524			 * will confuse the thread stack with the
525			 * process stack and deliver a SEGV if they
526			 * attempt to grow the thread stack past their
527			 * current stacksize rlimit.  To avoid this,
528			 * adjust vm_maxsaddr upwards to reflect
529			 * the current stacksize rlimit rather
530			 * than the maximum possible stacksize.
531			 * It would be better to adjust the
532			 * mmap'ed region, but some apps do not check
533			 * mmap's return value.
534			 */
535			PROC_LOCK(p);
536			p->p_vmspace->vm_maxsaddr = (char *)USRSTACK -
537			    lim_cur(p, RLIMIT_STACK);
538			PROC_UNLOCK(p);
539		}
540
541		/*
542		 * This gives us our maximum stack size and a new BOS.
543		 * If we're using VM_STACK, then mmap will just map
544		 * the top SGROWSIZ bytes, and let the stack grow down
545		 * to the limit at BOS.  If we're not using VM_STACK
546		 * we map the full stack, since we don't have a way
547		 * to autogrow it.
548		 */
549		if (len > STACK_SIZE - GUARD_SIZE) {
550			bsd_args.addr = (caddr_t)PTRIN(addr);
551			bsd_args.len = len;
552		} else {
553			bsd_args.addr = (caddr_t)PTRIN(addr) -
554			    (STACK_SIZE - GUARD_SIZE - len);
555			bsd_args.len = STACK_SIZE - GUARD_SIZE;
556		}
557	} else {
558		bsd_args.addr = (caddr_t)PTRIN(addr);
559		bsd_args.len  = len;
560	}
561	bsd_args.pos = pos;
562
563#ifdef DEBUG
564	if (ldebug(mmap))
565		printf("-> %s(%p, %d, %d, 0x%08x, %d, 0x%x)\n",
566		    __func__,
567		    (void *)bsd_args.addr, bsd_args.len, bsd_args.prot,
568		    bsd_args.flags, bsd_args.fd, (int)bsd_args.pos);
569#endif
570	error = sys_mmap(td, &bsd_args);
571#ifdef DEBUG
572	if (ldebug(mmap))
573		printf("-> %s() return: 0x%x (0x%08x)\n",
574			__func__, error, (u_int)td->td_retval[0]);
575#endif
576	return (error);
577}
578
579int
580linux_mprotect(struct thread *td, struct linux_mprotect_args *uap)
581{
582	struct mprotect_args bsd_args;
583
584	bsd_args.addr = uap->addr;
585	bsd_args.len = uap->len;
586	bsd_args.prot = uap->prot;
587	if (bsd_args.prot & (PROT_READ | PROT_WRITE | PROT_EXEC))
588		bsd_args.prot |= PROT_READ | PROT_EXEC;
589	return (sys_mprotect(td, &bsd_args));
590}
591
592int
593linux_ioperm(struct thread *td, struct linux_ioperm_args *args)
594{
595	int error;
596	struct i386_ioperm_args iia;
597
598	iia.start = args->start;
599	iia.length = args->length;
600	iia.enable = args->enable;
601	error = i386_set_ioperm(td, &iia);
602	return (error);
603}
604
605int
606linux_iopl(struct thread *td, struct linux_iopl_args *args)
607{
608	int error;
609
610	if (args->level < 0 || args->level > 3)
611		return (EINVAL);
612	if ((error = priv_check(td, PRIV_IO)) != 0)
613		return (error);
614	if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
615		return (error);
616	td->td_frame->tf_eflags = (td->td_frame->tf_eflags & ~PSL_IOPL) |
617	    (args->level * (PSL_IOPL / 3));
618	return (0);
619}
620
621int
622linux_modify_ldt(struct thread *td, struct linux_modify_ldt_args *uap)
623{
624	int error;
625	struct i386_ldt_args ldt;
626	struct l_descriptor ld;
627	union descriptor desc;
628	int size, written;
629
630	switch (uap->func) {
631	case 0x00: /* read_ldt */
632		ldt.start = 0;
633		ldt.descs = uap->ptr;
634		ldt.num = uap->bytecount / sizeof(union descriptor);
635		error = i386_get_ldt(td, &ldt);
636		td->td_retval[0] *= sizeof(union descriptor);
637		break;
638	case 0x02: /* read_default_ldt = 0 */
639		size = 5*sizeof(struct l_desc_struct);
640		if (size > uap->bytecount)
641			size = uap->bytecount;
642		for (written = error = 0; written < size && error == 0; written++)
643			error = subyte((char *)uap->ptr + written, 0);
644		td->td_retval[0] = written;
645		break;
646	case 0x01: /* write_ldt */
647	case 0x11: /* write_ldt */
648		if (uap->bytecount != sizeof(ld))
649			return (EINVAL);
650
651		error = copyin(uap->ptr, &ld, sizeof(ld));
652		if (error)
653			return (error);
654
655		ldt.start = ld.entry_number;
656		ldt.descs = &desc;
657		ldt.num = 1;
658		desc.sd.sd_lolimit = (ld.limit & 0x0000ffff);
659		desc.sd.sd_hilimit = (ld.limit & 0x000f0000) >> 16;
660		desc.sd.sd_lobase = (ld.base_addr & 0x00ffffff);
661		desc.sd.sd_hibase = (ld.base_addr & 0xff000000) >> 24;
662		desc.sd.sd_type = SDT_MEMRO | ((ld.read_exec_only ^ 1) << 1) |
663			(ld.contents << 2);
664		desc.sd.sd_dpl = 3;
665		desc.sd.sd_p = (ld.seg_not_present ^ 1);
666		desc.sd.sd_xx = 0;
667		desc.sd.sd_def32 = ld.seg_32bit;
668		desc.sd.sd_gran = ld.limit_in_pages;
669		error = i386_set_ldt(td, &ldt, &desc);
670		break;
671	default:
672		error = ENOSYS;
673		break;
674	}
675
676	if (error == EOPNOTSUPP) {
677		printf("linux: modify_ldt needs kernel option USER_LDT\n");
678		error = ENOSYS;
679	}
680
681	return (error);
682}
683
684int
685linux_sigaction(struct thread *td, struct linux_sigaction_args *args)
686{
687	l_osigaction_t osa;
688	l_sigaction_t act, oact;
689	int error;
690
691#ifdef DEBUG
692	if (ldebug(sigaction))
693		printf(ARGS(sigaction, "%d, %p, %p"),
694		    args->sig, (void *)args->nsa, (void *)args->osa);
695#endif
696
697	if (args->nsa != NULL) {
698		error = copyin(args->nsa, &osa, sizeof(l_osigaction_t));
699		if (error)
700			return (error);
701		act.lsa_handler = osa.lsa_handler;
702		act.lsa_flags = osa.lsa_flags;
703		act.lsa_restorer = osa.lsa_restorer;
704		LINUX_SIGEMPTYSET(act.lsa_mask);
705		act.lsa_mask.__bits[0] = osa.lsa_mask;
706	}
707
708	error = linux_do_sigaction(td, args->sig, args->nsa ? &act : NULL,
709	    args->osa ? &oact : NULL);
710
711	if (args->osa != NULL && !error) {
712		osa.lsa_handler = oact.lsa_handler;
713		osa.lsa_flags = oact.lsa_flags;
714		osa.lsa_restorer = oact.lsa_restorer;
715		osa.lsa_mask = oact.lsa_mask.__bits[0];
716		error = copyout(&osa, args->osa, sizeof(l_osigaction_t));
717	}
718
719	return (error);
720}
721
722/*
723 * Linux has two extra args, restart and oldmask.  We dont use these,
724 * but it seems that "restart" is actually a context pointer that
725 * enables the signal to happen with a different register set.
726 */
727int
728linux_sigsuspend(struct thread *td, struct linux_sigsuspend_args *args)
729{
730	sigset_t sigmask;
731	l_sigset_t mask;
732
733#ifdef DEBUG
734	if (ldebug(sigsuspend))
735		printf(ARGS(sigsuspend, "%08lx"), (unsigned long)args->mask);
736#endif
737
738	LINUX_SIGEMPTYSET(mask);
739	mask.__bits[0] = args->mask;
740	linux_to_bsd_sigset(&mask, &sigmask);
741	return (kern_sigsuspend(td, sigmask));
742}
743
744int
745linux_rt_sigsuspend(struct thread *td, struct linux_rt_sigsuspend_args *uap)
746{
747	l_sigset_t lmask;
748	sigset_t sigmask;
749	int error;
750
751#ifdef DEBUG
752	if (ldebug(rt_sigsuspend))
753		printf(ARGS(rt_sigsuspend, "%p, %d"),
754		    (void *)uap->newset, uap->sigsetsize);
755#endif
756
757	if (uap->sigsetsize != sizeof(l_sigset_t))
758		return (EINVAL);
759
760	error = copyin(uap->newset, &lmask, sizeof(l_sigset_t));
761	if (error)
762		return (error);
763
764	linux_to_bsd_sigset(&lmask, &sigmask);
765	return (kern_sigsuspend(td, sigmask));
766}
767
768int
769linux_pause(struct thread *td, struct linux_pause_args *args)
770{
771	struct proc *p = td->td_proc;
772	sigset_t sigmask;
773
774#ifdef DEBUG
775	if (ldebug(pause))
776		printf(ARGS(pause, ""));
777#endif
778
779	PROC_LOCK(p);
780	sigmask = td->td_sigmask;
781	PROC_UNLOCK(p);
782	return (kern_sigsuspend(td, sigmask));
783}
784
785int
786linux_sigaltstack(struct thread *td, struct linux_sigaltstack_args *uap)
787{
788	stack_t ss, oss;
789	l_stack_t lss;
790	int error;
791
792#ifdef DEBUG
793	if (ldebug(sigaltstack))
794		printf(ARGS(sigaltstack, "%p, %p"), uap->uss, uap->uoss);
795#endif
796
797	if (uap->uss != NULL) {
798		error = copyin(uap->uss, &lss, sizeof(l_stack_t));
799		if (error)
800			return (error);
801
802		ss.ss_sp = lss.ss_sp;
803		ss.ss_size = lss.ss_size;
804		ss.ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags);
805	}
806	error = kern_sigaltstack(td, (uap->uss != NULL) ? &ss : NULL,
807	    (uap->uoss != NULL) ? &oss : NULL);
808	if (!error && uap->uoss != NULL) {
809		lss.ss_sp = oss.ss_sp;
810		lss.ss_size = oss.ss_size;
811		lss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags);
812		error = copyout(&lss, uap->uoss, sizeof(l_stack_t));
813	}
814
815	return (error);
816}
817
818int
819linux_ftruncate64(struct thread *td, struct linux_ftruncate64_args *args)
820{
821	struct ftruncate_args sa;
822
823#ifdef DEBUG
824	if (ldebug(ftruncate64))
825		printf(ARGS(ftruncate64, "%u, %jd"), args->fd,
826		    (intmax_t)args->length);
827#endif
828
829	sa.fd = args->fd;
830	sa.length = args->length;
831	return sys_ftruncate(td, &sa);
832}
833
834int
835linux_set_thread_area(struct thread *td, struct linux_set_thread_area_args *args)
836{
837	struct l_user_desc info;
838	int error;
839	int idx;
840	int a[2];
841	struct segment_descriptor sd;
842
843	error = copyin(args->desc, &info, sizeof(struct l_user_desc));
844	if (error)
845		return (error);
846
847#ifdef DEBUG
848	if (ldebug(set_thread_area))
849	   	printf(ARGS(set_thread_area, "%i, %x, %x, %i, %i, %i, %i, %i, %i\n"),
850		      info.entry_number,
851      		      info.base_addr,
852      		      info.limit,
853      		      info.seg_32bit,
854		      info.contents,
855      		      info.read_exec_only,
856      		      info.limit_in_pages,
857      		      info.seg_not_present,
858      		      info.useable);
859#endif
860
861	idx = info.entry_number;
862	/*
863	 * Semantics of linux version: every thread in the system has array of
864	 * 3 tls descriptors. 1st is GLIBC TLS, 2nd is WINE, 3rd unknown. This
865	 * syscall loads one of the selected tls decriptors with a value and
866	 * also loads GDT descriptors 6, 7 and 8 with the content of the
867	 * per-thread descriptors.
868	 *
869	 * Semantics of fbsd version: I think we can ignore that linux has 3
870	 * per-thread descriptors and use just the 1st one. The tls_array[]
871	 * is used only in set/get-thread_area() syscalls and for loading the
872	 * GDT descriptors. In fbsd we use just one GDT descriptor for TLS so
873	 * we will load just one.
874	 *
875	 * XXX: this doesn't work when a user space process tries to use more
876	 * than 1 TLS segment. Comment in the linux sources says wine might do
877	 * this.
878	 */
879
880	/*
881	 * we support just GLIBC TLS now
882	 * we should let 3 proceed as well because we use this segment so
883	 * if code does two subsequent calls it should succeed
884	 */
885	if (idx != 6 && idx != -1 && idx != 3)
886		return (EINVAL);
887
888	/*
889	 * we have to copy out the GDT entry we use
890	 * FreeBSD uses GDT entry #3 for storing %gs so load that
891	 *
892	 * XXX: what if a user space program doesn't check this value and tries
893	 * to use 6, 7 or 8?
894	 */
895	idx = info.entry_number = 3;
896	error = copyout(&info, args->desc, sizeof(struct l_user_desc));
897	if (error)
898		return (error);
899
900	if (LINUX_LDT_empty(&info)) {
901		a[0] = 0;
902		a[1] = 0;
903	} else {
904		a[0] = LINUX_LDT_entry_a(&info);
905		a[1] = LINUX_LDT_entry_b(&info);
906	}
907
908	memcpy(&sd, &a, sizeof(a));
909#ifdef DEBUG
910	if (ldebug(set_thread_area))
911	   	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,
912			sd.sd_hibase,
913			sd.sd_lolimit,
914			sd.sd_hilimit,
915			sd.sd_type,
916			sd.sd_dpl,
917			sd.sd_p,
918			sd.sd_xx,
919			sd.sd_def32,
920			sd.sd_gran);
921#endif
922
923	/* this is taken from i386 version of cpu_set_user_tls() */
924	critical_enter();
925	/* set %gs */
926	td->td_pcb->pcb_gsd = sd;
927	PCPU_GET(fsgs_gdt)[1] = sd;
928	load_gs(GSEL(GUGS_SEL, SEL_UPL));
929	critical_exit();
930
931	return (0);
932}
933
934int
935linux_get_thread_area(struct thread *td, struct linux_get_thread_area_args *args)
936{
937
938	struct l_user_desc info;
939	int error;
940	int idx;
941	struct l_desc_struct desc;
942	struct segment_descriptor sd;
943
944#ifdef DEBUG
945	if (ldebug(get_thread_area))
946		printf(ARGS(get_thread_area, "%p"), args->desc);
947#endif
948
949	error = copyin(args->desc, &info, sizeof(struct l_user_desc));
950	if (error)
951		return (error);
952
953	idx = info.entry_number;
954	/* XXX: I am not sure if we want 3 to be allowed too. */
955	if (idx != 6 && idx != 3)
956		return (EINVAL);
957
958	idx = 3;
959
960	memset(&info, 0, sizeof(info));
961
962	sd = PCPU_GET(fsgs_gdt)[1];
963
964	memcpy(&desc, &sd, sizeof(desc));
965
966	info.entry_number = idx;
967	info.base_addr = LINUX_GET_BASE(&desc);
968	info.limit = LINUX_GET_LIMIT(&desc);
969	info.seg_32bit = LINUX_GET_32BIT(&desc);
970	info.contents = LINUX_GET_CONTENTS(&desc);
971	info.read_exec_only = !LINUX_GET_WRITABLE(&desc);
972	info.limit_in_pages = LINUX_GET_LIMIT_PAGES(&desc);
973	info.seg_not_present = !LINUX_GET_PRESENT(&desc);
974	info.useable = LINUX_GET_USEABLE(&desc);
975
976	error = copyout(&info, args->desc, sizeof(struct l_user_desc));
977	if (error)
978	   	return (EFAULT);
979
980	return (0);
981}
982
983/* XXX: this wont work with module - convert it */
984int
985linux_mq_open(struct thread *td, struct linux_mq_open_args *args)
986{
987#ifdef P1003_1B_MQUEUE
988   	return sys_kmq_open(td, (struct kmq_open_args *) args);
989#else
990	return (ENOSYS);
991#endif
992}
993
994int
995linux_mq_unlink(struct thread *td, struct linux_mq_unlink_args *args)
996{
997#ifdef P1003_1B_MQUEUE
998   	return sys_kmq_unlink(td, (struct kmq_unlink_args *) args);
999#else
1000	return (ENOSYS);
1001#endif
1002}
1003
1004int
1005linux_mq_timedsend(struct thread *td, struct linux_mq_timedsend_args *args)
1006{
1007#ifdef P1003_1B_MQUEUE
1008   	return sys_kmq_timedsend(td, (struct kmq_timedsend_args *) args);
1009#else
1010	return (ENOSYS);
1011#endif
1012}
1013
1014int
1015linux_mq_timedreceive(struct thread *td, struct linux_mq_timedreceive_args *args)
1016{
1017#ifdef P1003_1B_MQUEUE
1018   	return sys_kmq_timedreceive(td, (struct kmq_timedreceive_args *) args);
1019#else
1020	return (ENOSYS);
1021#endif
1022}
1023
1024int
1025linux_mq_notify(struct thread *td, struct linux_mq_notify_args *args)
1026{
1027#ifdef P1003_1B_MQUEUE
1028	return sys_kmq_notify(td, (struct kmq_notify_args *) args);
1029#else
1030	return (ENOSYS);
1031#endif
1032}
1033
1034int
1035linux_mq_getsetattr(struct thread *td, struct linux_mq_getsetattr_args *args)
1036{
1037#ifdef P1003_1B_MQUEUE
1038   	return sys_kmq_setattr(td, (struct kmq_setattr_args *) args);
1039#else
1040	return (ENOSYS);
1041#endif
1042}
1043
1044int
1045linux_wait4(struct thread *td, struct linux_wait4_args *args)
1046{
1047	int error, options;
1048	struct rusage ru, *rup;
1049
1050#ifdef DEBUG
1051	if (ldebug(wait4))
1052		printf(ARGS(wait4, "%d, %p, %d, %p"),
1053		    args->pid, (void *)args->status, args->options,
1054		    (void *)args->rusage);
1055#endif
1056
1057	options = (args->options & (WNOHANG | WUNTRACED));
1058	/* WLINUXCLONE should be equal to __WCLONE, but we make sure */
1059	if (args->options & __WCLONE)
1060		options |= WLINUXCLONE;
1061
1062	if (args->rusage != NULL)
1063		rup = &ru;
1064	else
1065		rup = NULL;
1066	error = linux_common_wait(td, args->pid, args->status, options, rup);
1067	if (error)
1068		return (error);
1069	if (args->rusage != NULL)
1070		error = copyout(&ru, args->rusage, sizeof(ru));
1071
1072	return (error);
1073}
1074