xref: /freebsd/sys/i386/linux/linux_machdep.c (revision cec50dea12481dc578c0805c887ab2097e1c06c5)
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/lock.h>
35 #include <sys/mman.h>
36 #include <sys/mutex.h>
37 #include <sys/proc.h>
38 #include <sys/resource.h>
39 #include <sys/resourcevar.h>
40 #include <sys/signalvar.h>
41 #include <sys/syscallsubr.h>
42 #include <sys/sysproto.h>
43 #include <sys/unistd.h>
44 
45 #include <machine/frame.h>
46 #include <machine/psl.h>
47 #include <machine/segments.h>
48 #include <machine/sysarch.h>
49 
50 #include <vm/vm.h>
51 #include <vm/pmap.h>
52 #include <vm/vm_map.h>
53 
54 #include <i386/linux/linux.h>
55 #include <i386/linux/linux_proto.h>
56 #include <compat/linux/linux_ipc.h>
57 #include <compat/linux/linux_signal.h>
58 #include <compat/linux/linux_util.h>
59 
60 struct l_descriptor {
61 	l_uint		entry_number;
62 	l_ulong		base_addr;
63 	l_uint		limit;
64 	l_uint		seg_32bit:1;
65 	l_uint		contents:2;
66 	l_uint		read_exec_only:1;
67 	l_uint		limit_in_pages:1;
68 	l_uint		seg_not_present:1;
69 	l_uint		useable:1;
70 };
71 
72 struct l_old_select_argv {
73 	l_int		nfds;
74 	l_fd_set	*readfds;
75 	l_fd_set	*writefds;
76 	l_fd_set	*exceptfds;
77 	struct l_timeval	*timeout;
78 };
79 
80 int
81 linux_to_bsd_sigaltstack(int lsa)
82 {
83 	int bsa = 0;
84 
85 	if (lsa & LINUX_SS_DISABLE)
86 		bsa |= SS_DISABLE;
87 	if (lsa & LINUX_SS_ONSTACK)
88 		bsa |= SS_ONSTACK;
89 	return (bsa);
90 }
91 
92 int
93 bsd_to_linux_sigaltstack(int bsa)
94 {
95 	int lsa = 0;
96 
97 	if (bsa & SS_DISABLE)
98 		lsa |= LINUX_SS_DISABLE;
99 	if (bsa & SS_ONSTACK)
100 		lsa |= LINUX_SS_ONSTACK;
101 	return (lsa);
102 }
103 
104 int
105 linux_execve(struct thread *td, struct linux_execve_args *args)
106 {
107 	struct execve_args bsd;
108 	caddr_t sg;
109 
110 	sg = stackgap_init();
111 	CHECKALTEXIST(td, &sg, args->path);
112 
113 #ifdef DEBUG
114 	if (ldebug(execve))
115 		printf(ARGS(execve, "%s"), args->path);
116 #endif
117 
118 	bsd.fname = args->path;
119 	bsd.argv = args->argp;
120 	bsd.envv = args->envp;
121 	return (execve(td, &bsd));
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_fork(struct thread *td, struct linux_fork_args *args)
273 {
274 	int error;
275 
276 #ifdef DEBUG
277 	if (ldebug(fork))
278 		printf(ARGS(fork, ""));
279 #endif
280 
281 	if ((error = fork(td, (struct fork_args *)args)) != 0)
282 		return (error);
283 
284 	if (td->td_retval[1] == 1)
285 		td->td_retval[0] = 0;
286 	return (0);
287 }
288 
289 int
290 linux_vfork(struct thread *td, struct linux_vfork_args *args)
291 {
292 	int error;
293 
294 #ifdef DEBUG
295 	if (ldebug(vfork))
296 		printf(ARGS(vfork, ""));
297 #endif
298 
299 	if ((error = vfork(td, (struct vfork_args *)args)) != 0)
300 		return (error);
301 	/* Are we the child? */
302 	if (td->td_retval[1] == 1)
303 		td->td_retval[0] = 0;
304 	return (0);
305 }
306 
307 #define CLONE_VM	0x100
308 #define CLONE_FS	0x200
309 #define CLONE_FILES	0x400
310 #define CLONE_SIGHAND	0x800
311 #define CLONE_PID	0x1000
312 
313 int
314 linux_clone(struct thread *td, struct linux_clone_args *args)
315 {
316 	int error, ff = RFPROC | RFSTOPPED;
317 	struct proc *p2;
318 	struct thread *td2;
319 	int exit_signal;
320 
321 #ifdef DEBUG
322 	if (ldebug(clone)) {
323 		printf(ARGS(clone, "flags %x, stack %x"),
324 		    (unsigned int)args->flags, (unsigned int)args->stack);
325 		if (args->flags & CLONE_PID)
326 			printf(LMSG("CLONE_PID not yet supported"));
327 	}
328 #endif
329 
330 	if (!args->stack)
331 		return (EINVAL);
332 
333 	exit_signal = args->flags & 0x000000ff;
334 	if (exit_signal >= LINUX_NSIG)
335 		return (EINVAL);
336 
337 	if (exit_signal <= LINUX_SIGTBLSZ)
338 		exit_signal = linux_to_bsd_signal[_SIG_IDX(exit_signal)];
339 
340 	if (args->flags & CLONE_VM)
341 		ff |= RFMEM;
342 	if (args->flags & CLONE_SIGHAND)
343 		ff |= RFSIGSHARE;
344 	if (!(args->flags & CLONE_FILES))
345 		ff |= RFFDG;
346 
347 	error = fork1(td, ff, 0, &p2);
348 	if (error)
349 		return (error);
350 
351 
352 	PROC_LOCK(p2);
353 	p2->p_sigparent = exit_signal;
354 	PROC_UNLOCK(p2);
355 	td2 = FIRST_THREAD_IN_PROC(p2);
356 	td2->td_frame->tf_esp = (unsigned int)args->stack;
357 
358 #ifdef DEBUG
359 	if (ldebug(clone))
360 		printf(LMSG("clone: successful rfork to %ld, stack %p sig = %d"),
361 		    (long)p2->p_pid, args->stack, exit_signal);
362 #endif
363 
364 	/*
365 	 * Make this runnable after we are finished with it.
366 	 */
367 	mtx_lock_spin(&sched_lock);
368 	TD_SET_CAN_RUN(td2);
369 	setrunqueue(td2, SRQ_BORING);
370 	mtx_unlock_spin(&sched_lock);
371 
372 	td->td_retval[0] = p2->p_pid;
373 	td->td_retval[1] = 0;
374 	return (0);
375 }
376 
377 /* XXX move */
378 struct l_mmap_argv {
379 	l_caddr_t	addr;
380 	l_int		len;
381 	l_int		prot;
382 	l_int		flags;
383 	l_int		fd;
384 	l_int		pos;
385 };
386 
387 #define STACK_SIZE  (2 * 1024 * 1024)
388 #define GUARD_SIZE  (4 * PAGE_SIZE)
389 
390 static int linux_mmap_common(struct thread *, struct l_mmap_argv *);
391 
392 int
393 linux_mmap2(struct thread *td, struct linux_mmap2_args *args)
394 {
395 	struct l_mmap_argv linux_args;
396 
397 #ifdef DEBUG
398 	if (ldebug(mmap2))
399 		printf(ARGS(mmap2, "%p, %d, %d, 0x%08x, %d, %d"),
400 		    (void *)args->addr, args->len, args->prot,
401 		    args->flags, args->fd, args->pgoff);
402 #endif
403 
404 	linux_args.addr = (l_caddr_t)args->addr;
405 	linux_args.len = args->len;
406 	linux_args.prot = args->prot;
407 	linux_args.flags = args->flags;
408 	linux_args.fd = args->fd;
409 	linux_args.pos = args->pgoff * PAGE_SIZE;
410 
411 	return (linux_mmap_common(td, &linux_args));
412 }
413 
414 int
415 linux_mmap(struct thread *td, struct linux_mmap_args *args)
416 {
417 	int error;
418 	struct l_mmap_argv linux_args;
419 
420 	error = copyin(args->ptr, &linux_args, sizeof(linux_args));
421 	if (error)
422 		return (error);
423 
424 #ifdef DEBUG
425 	if (ldebug(mmap))
426 		printf(ARGS(mmap, "%p, %d, %d, 0x%08x, %d, %d"),
427 		    (void *)linux_args.addr, linux_args.len, linux_args.prot,
428 		    linux_args.flags, linux_args.fd, linux_args.pos);
429 #endif
430 
431 	return (linux_mmap_common(td, &linux_args));
432 }
433 
434 static int
435 linux_mmap_common(struct thread *td, struct l_mmap_argv *linux_args)
436 {
437 	struct proc *p = td->td_proc;
438 	struct mmap_args /* {
439 		caddr_t addr;
440 		size_t len;
441 		int prot;
442 		int flags;
443 		int fd;
444 		long pad;
445 		off_t pos;
446 	} */ bsd_args;
447 	int error;
448 
449 	error = 0;
450 	bsd_args.flags = 0;
451 	if (linux_args->flags & LINUX_MAP_SHARED)
452 		bsd_args.flags |= MAP_SHARED;
453 	if (linux_args->flags & LINUX_MAP_PRIVATE)
454 		bsd_args.flags |= MAP_PRIVATE;
455 	if (linux_args->flags & LINUX_MAP_FIXED)
456 		bsd_args.flags |= MAP_FIXED;
457 	if (linux_args->flags & LINUX_MAP_ANON)
458 		bsd_args.flags |= MAP_ANON;
459 	else
460 		bsd_args.flags |= MAP_NOSYNC;
461 	if (linux_args->flags & LINUX_MAP_GROWSDOWN) {
462 		bsd_args.flags |= MAP_STACK;
463 
464 		/* The linux MAP_GROWSDOWN option does not limit auto
465 		 * growth of the region.  Linux mmap with this option
466 		 * takes as addr the inital BOS, and as len, the initial
467 		 * region size.  It can then grow down from addr without
468 		 * limit.  However, linux threads has an implicit internal
469 		 * limit to stack size of STACK_SIZE.  Its just not
470 		 * enforced explicitly in linux.  But, here we impose
471 		 * a limit of (STACK_SIZE - GUARD_SIZE) on the stack
472 		 * region, since we can do this with our mmap.
473 		 *
474 		 * Our mmap with MAP_STACK takes addr as the maximum
475 		 * downsize limit on BOS, and as len the max size of
476 		 * the region.  It them maps the top SGROWSIZ bytes,
477 		 * and autgrows the region down, up to the limit
478 		 * in addr.
479 		 *
480 		 * If we don't use the MAP_STACK option, the effect
481 		 * of this code is to allocate a stack region of a
482 		 * fixed size of (STACK_SIZE - GUARD_SIZE).
483 		 */
484 
485 		/* This gives us TOS */
486 		bsd_args.addr = linux_args->addr + linux_args->len;
487 
488 		if (bsd_args.addr > p->p_vmspace->vm_maxsaddr) {
489 			/* Some linux apps will attempt to mmap
490 			 * thread stacks near the top of their
491 			 * address space.  If their TOS is greater
492 			 * than vm_maxsaddr, vm_map_growstack()
493 			 * will confuse the thread stack with the
494 			 * process stack and deliver a SEGV if they
495 			 * attempt to grow the thread stack past their
496 			 * current stacksize rlimit.  To avoid this,
497 			 * adjust vm_maxsaddr upwards to reflect
498 			 * the current stacksize rlimit rather
499 			 * than the maximum possible stacksize.
500 			 * It would be better to adjust the
501 			 * mmap'ed region, but some apps do not check
502 			 * mmap's return value.
503 			 */
504 			PROC_LOCK(p);
505 			p->p_vmspace->vm_maxsaddr = (char *)USRSTACK -
506 			    lim_cur(p, RLIMIT_STACK);
507 			PROC_UNLOCK(p);
508 		}
509 
510 		/* This gives us our maximum stack size */
511 		if (linux_args->len > STACK_SIZE - GUARD_SIZE)
512 			bsd_args.len = linux_args->len;
513 		else
514 			bsd_args.len  = STACK_SIZE - GUARD_SIZE;
515 
516 		/* This gives us a new BOS.  If we're using VM_STACK, then
517 		 * mmap will just map the top SGROWSIZ bytes, and let
518 		 * the stack grow down to the limit at BOS.  If we're
519 		 * not using VM_STACK we map the full stack, since we
520 		 * don't have a way to autogrow it.
521 		 */
522 		bsd_args.addr -= bsd_args.len;
523 	} else {
524 		bsd_args.addr = linux_args->addr;
525 		bsd_args.len  = linux_args->len;
526 	}
527 
528 	bsd_args.prot = linux_args->prot | PROT_READ;	/* always required */
529 	if (linux_args->flags & LINUX_MAP_ANON)
530 		bsd_args.fd = -1;
531 	else
532 		bsd_args.fd = linux_args->fd;
533 	bsd_args.pos = linux_args->pos;
534 	bsd_args.pad = 0;
535 
536 #ifdef DEBUG
537 	if (ldebug(mmap))
538 		printf("-> %s(%p, %d, %d, 0x%08x, %d, 0x%x)\n",
539 		    __func__,
540 		    (void *)bsd_args.addr, bsd_args.len, bsd_args.prot,
541 		    bsd_args.flags, bsd_args.fd, (int)bsd_args.pos);
542 #endif
543 	error = mmap(td, &bsd_args);
544 #ifdef DEBUG
545 	if (ldebug(mmap))
546 		printf("-> %s() return: 0x%x (0x%08x)\n",
547 			__func__, error, (u_int)td->td_retval[0]);
548 #endif
549 	return (error);
550 }
551 
552 int
553 linux_pipe(struct thread *td, struct linux_pipe_args *args)
554 {
555 	int error;
556 	int reg_edx;
557 
558 #ifdef DEBUG
559 	if (ldebug(pipe))
560 		printf(ARGS(pipe, "*"));
561 #endif
562 
563 	reg_edx = td->td_retval[1];
564 	error = pipe(td, 0);
565 	if (error) {
566 		td->td_retval[1] = reg_edx;
567 		return (error);
568 	}
569 
570 	error = copyout(td->td_retval, args->pipefds, 2*sizeof(int));
571 	if (error) {
572 		td->td_retval[1] = reg_edx;
573 		return (error);
574 	}
575 
576 	td->td_retval[1] = reg_edx;
577 	td->td_retval[0] = 0;
578 	return (0);
579 }
580 
581 int
582 linux_ioperm(struct thread *td, struct linux_ioperm_args *args)
583 {
584 	struct sysarch_args sa;
585 	struct i386_ioperm_args *iia;
586 	caddr_t sg;
587 
588 	sg = stackgap_init();
589 	iia = stackgap_alloc(&sg, sizeof(struct i386_ioperm_args));
590 	iia->start = args->start;
591 	iia->length = args->length;
592 	iia->enable = args->enable;
593 	sa.op = I386_SET_IOPERM;
594 	sa.parms = (char *)iia;
595 	return (sysarch(td, &sa));
596 }
597 
598 int
599 linux_iopl(struct thread *td, struct linux_iopl_args *args)
600 {
601 	int error;
602 
603 	if (args->level < 0 || args->level > 3)
604 		return (EINVAL);
605 	if ((error = suser(td)) != 0)
606 		return (error);
607 	if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
608 		return (error);
609 	td->td_frame->tf_eflags = (td->td_frame->tf_eflags & ~PSL_IOPL) |
610 	    (args->level * (PSL_IOPL / 3));
611 	return (0);
612 }
613 
614 int
615 linux_modify_ldt(struct thread *td, struct linux_modify_ldt_args *uap)
616 {
617 	int error;
618 	caddr_t sg;
619 	struct sysarch_args args;
620 	struct i386_ldt_args *ldt;
621 	struct l_descriptor ld;
622 	union descriptor *desc;
623 
624 	sg = stackgap_init();
625 
626 	if (uap->ptr == NULL)
627 		return (EINVAL);
628 
629 	switch (uap->func) {
630 	case 0x00: /* read_ldt */
631 		ldt = stackgap_alloc(&sg, sizeof(*ldt));
632 		ldt->start = 0;
633 		ldt->descs = uap->ptr;
634 		ldt->num = uap->bytecount / sizeof(union descriptor);
635 		args.op = I386_GET_LDT;
636 		args.parms = (char*)ldt;
637 		error = sysarch(td, &args);
638 		td->td_retval[0] *= sizeof(union descriptor);
639 		break;
640 	case 0x01: /* write_ldt */
641 	case 0x11: /* write_ldt */
642 		if (uap->bytecount != sizeof(ld))
643 			return (EINVAL);
644 
645 		error = copyin(uap->ptr, &ld, sizeof(ld));
646 		if (error)
647 			return (error);
648 
649 		ldt = stackgap_alloc(&sg, sizeof(*ldt));
650 		desc = stackgap_alloc(&sg, sizeof(*desc));
651 		ldt->start = ld.entry_number;
652 		ldt->descs = desc;
653 		ldt->num = 1;
654 		desc->sd.sd_lolimit = (ld.limit & 0x0000ffff);
655 		desc->sd.sd_hilimit = (ld.limit & 0x000f0000) >> 16;
656 		desc->sd.sd_lobase = (ld.base_addr & 0x00ffffff);
657 		desc->sd.sd_hibase = (ld.base_addr & 0xff000000) >> 24;
658 		desc->sd.sd_type = SDT_MEMRO | ((ld.read_exec_only ^ 1) << 1) |
659 			(ld.contents << 2);
660 		desc->sd.sd_dpl = 3;
661 		desc->sd.sd_p = (ld.seg_not_present ^ 1);
662 		desc->sd.sd_xx = 0;
663 		desc->sd.sd_def32 = ld.seg_32bit;
664 		desc->sd.sd_gran = ld.limit_in_pages;
665 		args.op = I386_SET_LDT;
666 		args.parms = (char*)ldt;
667 		error = sysarch(td, &args);
668 		break;
669 	default:
670 		error = EINVAL;
671 		break;
672 	}
673 
674 	if (error == EOPNOTSUPP) {
675 		printf("linux: modify_ldt needs kernel option USER_LDT\n");
676 		error = ENOSYS;
677 	}
678 
679 	return (error);
680 }
681 
682 int
683 linux_sigaction(struct thread *td, struct linux_sigaction_args *args)
684 {
685 	l_osigaction_t osa;
686 	l_sigaction_t act, oact;
687 	int error;
688 
689 #ifdef DEBUG
690 	if (ldebug(sigaction))
691 		printf(ARGS(sigaction, "%d, %p, %p"),
692 		    args->sig, (void *)args->nsa, (void *)args->osa);
693 #endif
694 
695 	if (args->nsa != NULL) {
696 		error = copyin(args->nsa, &osa, sizeof(l_osigaction_t));
697 		if (error)
698 			return (error);
699 		act.lsa_handler = osa.lsa_handler;
700 		act.lsa_flags = osa.lsa_flags;
701 		act.lsa_restorer = osa.lsa_restorer;
702 		LINUX_SIGEMPTYSET(act.lsa_mask);
703 		act.lsa_mask.__bits[0] = osa.lsa_mask;
704 	}
705 
706 	error = linux_do_sigaction(td, args->sig, args->nsa ? &act : NULL,
707 	    args->osa ? &oact : NULL);
708 
709 	if (args->osa != NULL && !error) {
710 		osa.lsa_handler = oact.lsa_handler;
711 		osa.lsa_flags = oact.lsa_flags;
712 		osa.lsa_restorer = oact.lsa_restorer;
713 		osa.lsa_mask = oact.lsa_mask.__bits[0];
714 		error = copyout(&osa, args->osa, sizeof(l_osigaction_t));
715 	}
716 
717 	return (error);
718 }
719 
720 /*
721  * Linux has two extra args, restart and oldmask.  We dont use these,
722  * but it seems that "restart" is actually a context pointer that
723  * enables the signal to happen with a different register set.
724  */
725 int
726 linux_sigsuspend(struct thread *td, struct linux_sigsuspend_args *args)
727 {
728 	sigset_t sigmask;
729 	l_sigset_t mask;
730 
731 #ifdef DEBUG
732 	if (ldebug(sigsuspend))
733 		printf(ARGS(sigsuspend, "%08lx"), (unsigned long)args->mask);
734 #endif
735 
736 	LINUX_SIGEMPTYSET(mask);
737 	mask.__bits[0] = args->mask;
738 	linux_to_bsd_sigset(&mask, &sigmask);
739 	return (kern_sigsuspend(td, sigmask));
740 }
741 
742 int
743 linux_rt_sigsuspend(struct thread *td, struct linux_rt_sigsuspend_args *uap)
744 {
745 	l_sigset_t lmask;
746 	sigset_t sigmask;
747 	int error;
748 
749 #ifdef DEBUG
750 	if (ldebug(rt_sigsuspend))
751 		printf(ARGS(rt_sigsuspend, "%p, %d"),
752 		    (void *)uap->newset, uap->sigsetsize);
753 #endif
754 
755 	if (uap->sigsetsize != sizeof(l_sigset_t))
756 		return (EINVAL);
757 
758 	error = copyin(uap->newset, &lmask, sizeof(l_sigset_t));
759 	if (error)
760 		return (error);
761 
762 	linux_to_bsd_sigset(&lmask, &sigmask);
763 	return (kern_sigsuspend(td, sigmask));
764 }
765 
766 int
767 linux_pause(struct thread *td, struct linux_pause_args *args)
768 {
769 	struct proc *p = td->td_proc;
770 	sigset_t sigmask;
771 
772 #ifdef DEBUG
773 	if (ldebug(pause))
774 		printf(ARGS(pause, ""));
775 #endif
776 
777 	PROC_LOCK(p);
778 	sigmask = td->td_sigmask;
779 	PROC_UNLOCK(p);
780 	return (kern_sigsuspend(td, sigmask));
781 }
782 
783 int
784 linux_sigaltstack(struct thread *td, struct linux_sigaltstack_args *uap)
785 {
786 	stack_t ss, oss;
787 	l_stack_t lss;
788 	int error;
789 
790 #ifdef DEBUG
791 	if (ldebug(sigaltstack))
792 		printf(ARGS(sigaltstack, "%p, %p"), uap->uss, uap->uoss);
793 #endif
794 
795 	if (uap->uss != NULL) {
796 		error = copyin(uap->uss, &lss, sizeof(l_stack_t));
797 		if (error)
798 			return (error);
799 
800 		ss.ss_sp = lss.ss_sp;
801 		ss.ss_size = lss.ss_size;
802 		ss.ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags);
803 	}
804 	error = kern_sigaltstack(td, (uap->uss != NULL) ? &ss : NULL,
805 	    (uap->uoss != NULL) ? &oss : NULL);
806 	if (!error && uap->uoss != NULL) {
807 		lss.ss_sp = oss.ss_sp;
808 		lss.ss_size = oss.ss_size;
809 		lss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags);
810 		error = copyout(&lss, uap->uoss, sizeof(l_stack_t));
811 	}
812 
813 	return (error);
814 }
815 
816 int
817 linux_ftruncate64(struct thread *td, struct linux_ftruncate64_args *args)
818 {
819 	struct ftruncate_args sa;
820 
821 #ifdef DEBUG
822 	if (ldebug(ftruncate64))
823 		printf(ARGS(ftruncate64, "%u, %jd"), args->fd,
824 		    (intmax_t)args->length);
825 #endif
826 
827 	sa.fd = args->fd;
828 	sa.pad = 0;
829 	sa.length = args->length;
830 	return ftruncate(td, &sa);
831 }
832 
833 int
834 linux_set_thread_area(struct thread *td, struct linux_set_thread_area_args *args)
835 {
836 	/*
837 	 * Return an error code instead of raising a SIGSYS so that
838 	 * the caller will fall back to simpler LDT methods.
839 	 */
840 	return (ENOSYS);
841 }
842 
843 int
844 linux_gettid(struct thread *td, struct linux_gettid_args *args)
845 {
846 
847 	td->td_retval[0] = td->td_proc->p_pid;
848 	return (0);
849 }
850 
851 int
852 linux_tkill(struct thread *td, struct linux_tkill_args *args)
853 {
854 
855 	return (linux_kill(td, (struct linux_kill_args *) args));
856 }
857 
858