linux_machdep.c revision 104893
138032Speter/*-
238032Speter * Copyright (c) 2000 Marcel Moolenaar
338032Speter * All rights reserved.
438032Speter *
538032Speter * Redistribution and use in source and binary forms, with or without
638032Speter * modification, are permitted provided that the following conditions
738032Speter * are met:
838032Speter * 1. Redistributions of source code must retain the above copyright
938032Speter *    notice, this list of conditions and the following disclaimer
1038032Speter *    in this position and unchanged.
1138032Speter * 2. Redistributions in binary form must reproduce the above copyright
1238032Speter *    notice, this list of conditions and the following disclaimer in the
1338032Speter *    documentation and/or other materials provided with the distribution.
1438032Speter * 3. The name of the author may not be used to endorse or promote products
1538032Speter *    derived from this software without specific prior written permission.
1638032Speter *
1738032Speter * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1838032Speter * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1938032Speter * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2038032Speter * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2138032Speter * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2238032Speter * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2338032Speter * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2438032Speter * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2538032Speter * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2638032Speter * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2738032Speter *
2838032Speter * $FreeBSD: head/sys/i386/linux/linux_machdep.c 104893 2002-10-11 11:43:09Z sobomax $
2938032Speter */
3038032Speter
3138032Speter#include <sys/param.h>
3238032Speter#include <sys/systm.h>
3338032Speter#include <sys/lock.h>
3438032Speter#include <sys/mman.h>
3538032Speter#include <sys/mutex.h>
3638032Speter#include <sys/proc.h>
3738032Speter#include <sys/resource.h>
3838032Speter#include <sys/resourcevar.h>
3938032Speter#include <sys/syscallsubr.h>
4038032Speter#include <sys/sysproto.h>
4138032Speter#include <sys/unistd.h>
4238032Speter
4338032Speter#include <machine/frame.h>
4438032Speter#include <machine/psl.h>
4538032Speter#include <machine/segments.h>
4638032Speter#include <machine/sysarch.h>
4738032Speter
4838032Speter#include <vm/vm.h>
4938032Speter#include <vm/pmap.h>
5038032Speter#include <vm/vm_map.h>
5138032Speter
5238032Speter#include <i386/linux/linux.h>
5338032Speter#include <i386/linux/linux_proto.h>
5438032Speter#include <compat/linux/linux_ipc.h>
5538032Speter#include <compat/linux/linux_signal.h>
5638032Speter#include <compat/linux/linux_util.h>
5738032Speter
5838032Speterstruct l_descriptor {
5938032Speter	l_uint		entry_number;
6038032Speter	l_ulong		base_addr;
6138032Speter	l_uint		limit;
6238032Speter	l_uint		seg_32bit:1;
6338032Speter	l_uint		contents:2;
6438032Speter	l_uint		read_exec_only:1;
6538032Speter	l_uint		limit_in_pages:1;
6638032Speter	l_uint		seg_not_present:1;
6738032Speter	l_uint		useable:1;
6838032Speter};
6938032Speter
7038032Speterstruct l_old_select_argv {
7138032Speter	l_int		nfds;
7238032Speter	l_fd_set	*readfds;
7338032Speter	l_fd_set	*writefds;
7438032Speter	l_fd_set	*exceptfds;
7538032Speter	struct l_timeval	*timeout;
7638032Speter};
7738032Speter
7838032Speterint
7938032Speterlinux_to_bsd_sigaltstack(int lsa)
8038032Speter{
8138032Speter	int bsa = 0;
8238032Speter
8338032Speter	if (lsa & LINUX_SS_DISABLE)
8438032Speter		bsa |= SS_DISABLE;
8538032Speter	if (lsa & LINUX_SS_ONSTACK)
8638032Speter		bsa |= SS_ONSTACK;
8764562Sgshapiro	return (bsa);
8864562Sgshapiro}
8964562Sgshapiro
9064562Sgshapiroint
9164562Sgshapirobsd_to_linux_sigaltstack(int bsa)
9264562Sgshapiro{
9364562Sgshapiro	int lsa = 0;
9464562Sgshapiro
9564562Sgshapiro	if (bsa & SS_DISABLE)
9664562Sgshapiro		lsa |= LINUX_SS_DISABLE;
9764562Sgshapiro	if (bsa & SS_ONSTACK)
9864562Sgshapiro		lsa |= LINUX_SS_ONSTACK;
9964562Sgshapiro	return (lsa);
10064562Sgshapiro}
10164562Sgshapiro
10264562Sgshapiroint
10364562Sgshapirolinux_execve(struct thread *td, struct linux_execve_args *args)
10464562Sgshapiro{
10564562Sgshapiro	struct execve_args bsd;
10664562Sgshapiro	caddr_t sg;
10738032Speter
10864562Sgshapiro	sg = stackgap_init();
10938032Speter	CHECKALTEXIST(td, &sg, args->path);
11038032Speter
11138032Speter#ifdef DEBUG
11238032Speter	if (ldebug(execve))
11338032Speter		printf(ARGS(execve, "%s"), args->path);
11438032Speter#endif
11538032Speter
11638032Speter	bsd.fname = args->path;
11738032Speter	bsd.argv = args->argp;
11838032Speter	bsd.envv = args->envp;
11938032Speter	return (execve(td, &bsd));
12038032Speter}
12138032Speter
12238032Speterstruct l_ipc_kludge {
12338032Speter	struct l_msgbuf *msgp;
12438032Speter	l_long msgtyp;
12538032Speter};
12638032Speter
12738032Speterint
12838032Speterlinux_ipc(struct thread *td, struct linux_ipc_args *args)
12938032Speter{
13038032Speter
13138032Speter	switch (args->what & 0xFFFF) {
13238032Speter	case LINUX_SEMOP: {
13338032Speter		struct linux_semop_args a;
13438032Speter
13538032Speter		a.semid = args->arg1;
13638032Speter		a.tsops = args->ptr;
13738032Speter		a.nsops = args->arg2;
13838032Speter		return (linux_semop(td, &a));
13938032Speter	}
14038032Speter	case LINUX_SEMGET: {
14138032Speter		struct linux_semget_args a;
14238032Speter
14338032Speter		a.key = args->arg1;
14438032Speter		a.nsems = args->arg2;
14538032Speter		a.semflg = args->arg3;
14638032Speter		return (linux_semget(td, &a));
14738032Speter	}
14864562Sgshapiro	case LINUX_SEMCTL: {
14938032Speter		struct linux_semctl_args a;
15038032Speter		int error;
15138032Speter
15238032Speter		a.semid = args->arg1;
15338032Speter		a.semnum = args->arg2;
15438032Speter		a.cmd = args->arg3;
15538032Speter		error = copyin((caddr_t)args->ptr, &a.arg, sizeof(a.arg));
15638032Speter		if (error)
15764562Sgshapiro			return (error);
15864562Sgshapiro		return (linux_semctl(td, &a));
15964562Sgshapiro	}
16064562Sgshapiro	case LINUX_MSGSND: {
16138032Speter		struct linux_msgsnd_args a;
16238032Speter
16338032Speter		a.msqid = args->arg1;
16438032Speter		a.msgp = args->ptr;
16538032Speter		a.msgsz = args->arg2;
16638032Speter		a.msgflg = args->arg3;
16738032Speter		return (linux_msgsnd(td, &a));
16838032Speter	}
16938032Speter	case LINUX_MSGRCV: {
17038032Speter		struct linux_msgrcv_args a;
17138032Speter
17238032Speter		a.msqid = args->arg1;
17338032Speter		a.msgsz = args->arg2;
17438032Speter		a.msgflg = args->arg3;
17538032Speter		if ((args->what >> 16) == 0) {
17638032Speter			struct l_ipc_kludge tmp;
17764562Sgshapiro			int error;
17864562Sgshapiro
17964562Sgshapiro			if (args->ptr == NULL)
18064562Sgshapiro				return (EINVAL);
18138032Speter			error = copyin((caddr_t)args->ptr, &tmp, sizeof(tmp));
18238032Speter			if (error)
18338032Speter				return (error);
18438032Speter			a.msgp = tmp.msgp;
18538032Speter			a.msgtyp = tmp.msgtyp;
18638032Speter		} else {
18738032Speter			a.msgp = args->ptr;
18838032Speter			a.msgtyp = args->arg5;
18938032Speter		}
19038032Speter		return (linux_msgrcv(td, &a));
19138032Speter	}
19238032Speter	case LINUX_MSGGET: {
19338032Speter		struct linux_msgget_args a;
19438032Speter
19564562Sgshapiro		a.key = args->arg1;
19664562Sgshapiro		a.msgflg = args->arg2;
19764562Sgshapiro		return (linux_msgget(td, &a));
19864562Sgshapiro	}
19964562Sgshapiro	case LINUX_MSGCTL: {
20064562Sgshapiro		struct linux_msgctl_args a;
20164562Sgshapiro
20264562Sgshapiro		a.msqid = args->arg1;
20364562Sgshapiro		a.cmd = args->arg2;
20464562Sgshapiro		a.buf = args->ptr;
20564562Sgshapiro		return (linux_msgctl(td, &a));
20638032Speter	}
20738032Speter	case LINUX_SHMAT: {
20864562Sgshapiro		struct linux_shmat_args a;
20964562Sgshapiro
21064562Sgshapiro		a.shmid = args->arg1;
21138032Speter		a.shmaddr = args->ptr;
21238032Speter		a.shmflg = args->arg2;
21338032Speter		a.raddr = (l_ulong *)args->arg3;
21464562Sgshapiro		return (linux_shmat(td, &a));
21564562Sgshapiro	}
21664562Sgshapiro	case LINUX_SHMDT: {
21738032Speter		struct linux_shmdt_args a;
21838032Speter
21938032Speter		a.shmaddr = args->ptr;
22064562Sgshapiro		return (linux_shmdt(td, &a));
22164562Sgshapiro	}
22264562Sgshapiro	case LINUX_SHMGET: {
22364562Sgshapiro		struct linux_shmget_args a;
22464562Sgshapiro
22564562Sgshapiro		a.key = args->arg1;
22664562Sgshapiro		a.size = args->arg2;
22764562Sgshapiro		a.shmflg = args->arg3;
22864562Sgshapiro		return (linux_shmget(td, &a));
22964562Sgshapiro	}
23038032Speter	case LINUX_SHMCTL: {
23138032Speter		struct linux_shmctl_args a;
23238032Speter
23338032Speter		a.shmid = args->arg1;
23438032Speter		a.cmd = args->arg2;
23564562Sgshapiro		a.buf = args->ptr;
23664562Sgshapiro		return (linux_shmctl(td, &a));
23764562Sgshapiro	}
23838032Speter	default:
23938032Speter		break;
24038032Speter	}
24164562Sgshapiro
24264562Sgshapiro	return (EINVAL);
24364562Sgshapiro}
24438032Speter
24538032Speterint
24664562Sgshapirolinux_old_select(struct thread *td, struct linux_old_select_args *args)
24764562Sgshapiro{
24864562Sgshapiro	struct l_old_select_argv linux_args;
24964562Sgshapiro	struct linux_select_args newsel;
25064562Sgshapiro	int error;
25164562Sgshapiro
25264562Sgshapiro#ifdef DEBUG
25364562Sgshapiro	if (ldebug(old_select))
25438032Speter		printf(ARGS(old_select, "%p"), args->ptr);
25538032Speter#endif
25638032Speter
25738032Speter	error = copyin((caddr_t)args->ptr, &linux_args, sizeof(linux_args));
25838032Speter	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_fork(struct thread *td, struct linux_fork_args *args)
271{
272	int error;
273
274#ifdef DEBUG
275	if (ldebug(fork))
276		printf(ARGS(fork, ""));
277#endif
278
279	if ((error = fork(td, (struct fork_args *)args)) != 0)
280		return (error);
281
282	if (td->td_retval[1] == 1)
283		td->td_retval[0] = 0;
284	return (0);
285}
286
287int
288linux_vfork(struct thread *td, struct linux_vfork_args *args)
289{
290	int error;
291
292#ifdef DEBUG
293	if (ldebug(vfork))
294		printf(ARGS(vfork, ""));
295#endif
296
297	if ((error = vfork(td, (struct vfork_args *)args)) != 0)
298		return (error);
299	/* Are we the child? */
300	if (td->td_retval[1] == 1)
301		td->td_retval[0] = 0;
302	return (0);
303}
304
305#define CLONE_VM	0x100
306#define CLONE_FS	0x200
307#define CLONE_FILES	0x400
308#define CLONE_SIGHAND	0x800
309#define CLONE_PID	0x1000
310
311int
312linux_clone(struct thread *td, struct linux_clone_args *args)
313{
314	int error, ff = RFPROC | RFSTOPPED;
315	struct proc *p2;
316	int exit_signal;
317
318#ifdef DEBUG
319	if (ldebug(clone)) {
320		printf(ARGS(clone, "flags %x, stack %x"),
321		    (unsigned int)args->flags, (unsigned int)args->stack);
322		if (args->flags & CLONE_PID)
323			printf(LMSG("CLONE_PID not yet supported"));
324	}
325#endif
326
327	if (!args->stack)
328		return (EINVAL);
329
330	exit_signal = args->flags & 0x000000ff;
331	if (exit_signal >= LINUX_NSIG)
332		return (EINVAL);
333
334	if (exit_signal <= LINUX_SIGTBLSZ)
335		exit_signal = linux_to_bsd_signal[_SIG_IDX(exit_signal)];
336
337	if (args->flags & CLONE_VM)
338		ff |= RFMEM;
339	if (args->flags & CLONE_SIGHAND)
340		ff |= RFSIGSHARE;
341	if (!(args->flags & CLONE_FILES))
342		ff |= RFFDG;
343
344	mtx_lock(&Giant);
345	error = fork1(td, ff, 0, &p2);
346	if (error == 0) {
347		td->td_retval[0] = p2->p_pid;
348		td->td_retval[1] = 0;
349
350		PROC_LOCK(p2);
351		p2->p_sigparent = exit_signal;
352		FIRST_THREAD_IN_PROC(p2)->td_frame->tf_esp =
353					(unsigned int)args->stack;
354
355#ifdef DEBUG
356		if (ldebug(clone))
357			printf(LMSG("clone: successful rfork to %ld"),
358			    (long)p2->p_pid);
359#endif
360
361		/*
362		 * Make this runnable after we are finished with it.
363		 */
364		mtx_lock_spin(&sched_lock);
365		TD_SET_CAN_RUN(FIRST_THREAD_IN_PROC(p2));
366		setrunqueue(FIRST_THREAD_IN_PROC(p2));
367		mtx_unlock_spin(&sched_lock);
368		PROC_UNLOCK(p2);
369	}
370	mtx_unlock(&Giant);
371
372	return (error);
373}
374
375/* XXX move */
376struct l_mmap_argv {
377	l_caddr_t	addr;
378	l_int		len;
379	l_int		prot;
380	l_int		flags;
381	l_int		fd;
382	l_int		pos;
383};
384
385#define STACK_SIZE  (2 * 1024 * 1024)
386#define GUARD_SIZE  (4 * PAGE_SIZE)
387
388static int linux_mmap_common(struct thread *, struct l_mmap_argv *);
389
390int
391linux_mmap2(struct thread *td, struct linux_mmap2_args *args)
392{
393	struct l_mmap_argv linux_args;
394
395#ifdef DEBUG
396	if (ldebug(mmap2))
397		printf(ARGS(mmap2, "%p, %d, %d, 0x%08x, %d, %d"),
398		    (void *)args->addr, args->len, args->prot,
399		    args->flags, args->fd, args->pgoff);
400#endif
401
402	linux_args.addr = (l_caddr_t)args->addr;
403	linux_args.len = args->len;
404	linux_args.prot = args->prot;
405	linux_args.flags = args->flags;
406	linux_args.fd = args->fd;
407	linux_args.pos = args->pgoff * PAGE_SIZE;
408
409	return (linux_mmap_common(td, &linux_args));
410}
411
412int
413linux_mmap(struct thread *td, struct linux_mmap_args *args)
414{
415	int error;
416	struct l_mmap_argv linux_args;
417
418	error = copyin((caddr_t)args->ptr, &linux_args, sizeof(linux_args));
419	if (error)
420		return (error);
421
422#ifdef DEBUG
423	if (ldebug(mmap))
424		printf(ARGS(mmap, "%p, %d, %d, 0x%08x, %d, %d"),
425		    (void *)linux_args->addr, linux_args->len, linux_args->prot,
426		    linux_args->flags, linux_args->fd, linux_args->pos);
427#endif
428
429	return (linux_mmap_common(td, &linux_args));
430}
431
432static int
433linux_mmap_common(struct thread *td, struct l_mmap_argv *linux_args)
434{
435	struct proc *p = td->td_proc;
436	struct mmap_args /* {
437		caddr_t addr;
438		size_t len;
439		int prot;
440		int flags;
441		int fd;
442		long pad;
443		off_t pos;
444	} */ bsd_args;
445
446	bsd_args.flags = 0;
447	if (linux_args->flags & LINUX_MAP_SHARED)
448		bsd_args.flags |= MAP_SHARED;
449	if (linux_args->flags & LINUX_MAP_PRIVATE)
450		bsd_args.flags |= MAP_PRIVATE;
451	if (linux_args->flags & LINUX_MAP_FIXED)
452		bsd_args.flags |= MAP_FIXED;
453	if (linux_args->flags & LINUX_MAP_ANON)
454		bsd_args.flags |= MAP_ANON;
455	else
456		bsd_args.flags |= MAP_NOSYNC;
457	if (linux_args->flags & LINUX_MAP_GROWSDOWN) {
458		bsd_args.flags |= MAP_STACK;
459
460		/* The linux MAP_GROWSDOWN option does not limit auto
461		 * growth of the region.  Linux mmap with this option
462		 * takes as addr the inital BOS, and as len, the initial
463		 * region size.  It can then grow down from addr without
464		 * limit.  However, linux threads has an implicit internal
465		 * limit to stack size of STACK_SIZE.  Its just not
466		 * enforced explicitly in linux.  But, here we impose
467		 * a limit of (STACK_SIZE - GUARD_SIZE) on the stack
468		 * region, since we can do this with our mmap.
469		 *
470		 * Our mmap with MAP_STACK takes addr as the maximum
471		 * downsize limit on BOS, and as len the max size of
472		 * the region.  It them maps the top SGROWSIZ bytes,
473		 * and autgrows the region down, up to the limit
474		 * in addr.
475		 *
476		 * If we don't use the MAP_STACK option, the effect
477		 * of this code is to allocate a stack region of a
478		 * fixed size of (STACK_SIZE - GUARD_SIZE).
479		 */
480
481		/* This gives us TOS */
482		bsd_args.addr = linux_args->addr + linux_args->len;
483
484		if (bsd_args.addr > p->p_vmspace->vm_maxsaddr) {
485			/* Some linux apps will attempt to mmap
486			 * thread stacks near the top of their
487			 * address space.  If their TOS is greater
488			 * than vm_maxsaddr, vm_map_growstack()
489			 * will confuse the thread stack with the
490			 * process stack and deliver a SEGV if they
491			 * attempt to grow the thread stack past their
492			 * current stacksize rlimit.  To avoid this,
493			 * adjust vm_maxsaddr upwards to reflect
494			 * the current stacksize rlimit rather
495			 * than the maximum possible stacksize.
496			 * It would be better to adjust the
497			 * mmap'ed region, but some apps do not check
498			 * mmap's return value.
499			 */
500			mtx_assert(&Giant, MA_OWNED);
501			p->p_vmspace->vm_maxsaddr = (char *)USRSTACK -
502			    p->p_rlimit[RLIMIT_STACK].rlim_cur;
503		}
504
505		/* This gives us our maximum stack size */
506		if (linux_args->len > STACK_SIZE - GUARD_SIZE)
507			bsd_args.len = linux_args->len;
508		else
509			bsd_args.len  = STACK_SIZE - GUARD_SIZE;
510
511		/* This gives us a new BOS.  If we're using VM_STACK, then
512		 * mmap will just map the top SGROWSIZ bytes, and let
513		 * the stack grow down to the limit at BOS.  If we're
514		 * not using VM_STACK we map the full stack, since we
515		 * don't have a way to autogrow it.
516		 */
517		bsd_args.addr -= bsd_args.len;
518	} else {
519		bsd_args.addr = linux_args->addr;
520		bsd_args.len  = linux_args->len;
521	}
522
523	bsd_args.prot = linux_args->prot | PROT_READ;	/* always required */
524	if (linux_args->flags & LINUX_MAP_ANON)
525		bsd_args.fd = -1;
526	else
527		bsd_args.fd = linux_args->fd;
528	bsd_args.pos = linux_args->pos;
529	bsd_args.pad = 0;
530
531#ifdef DEBUG
532	if (ldebug(mmap))
533		printf("-> (%p, %d, %d, 0x%08x, %d, %d)\n",
534		    (void *)bsd_args.addr, bsd_args.len, bsd_args.prot,
535		    bsd_args.flags, bsd_args.fd, (int)bsd_args.pos);
536#endif
537
538	return (mmap(td, &bsd_args));
539}
540
541int
542linux_pipe(struct thread *td, struct linux_pipe_args *args)
543{
544	int error;
545	int reg_edx;
546
547#ifdef DEBUG
548	if (ldebug(pipe))
549		printf(ARGS(pipe, "*"));
550#endif
551
552	reg_edx = td->td_retval[1];
553	error = pipe(td, 0);
554	if (error) {
555		td->td_retval[1] = reg_edx;
556		return (error);
557	}
558
559	error = copyout(td->td_retval, args->pipefds, 2*sizeof(int));
560	if (error) {
561		td->td_retval[1] = reg_edx;
562		return (error);
563	}
564
565	td->td_retval[1] = reg_edx;
566	td->td_retval[0] = 0;
567	return (0);
568}
569
570int
571linux_ioperm(struct thread *td, struct linux_ioperm_args *args)
572{
573	struct sysarch_args sa;
574	struct i386_ioperm_args *iia;
575	caddr_t sg;
576
577	sg = stackgap_init();
578	iia = stackgap_alloc(&sg, sizeof(struct i386_ioperm_args));
579	iia->start = args->start;
580	iia->length = args->length;
581	iia->enable = args->enable;
582	sa.op = I386_SET_IOPERM;
583	sa.parms = (char *)iia;
584	return (sysarch(td, &sa));
585}
586
587int
588linux_iopl(struct thread *td, struct linux_iopl_args *args)
589{
590	int error;
591
592	if (args->level < 0 || args->level > 3)
593		return (EINVAL);
594	if ((error = suser(td)) != 0)
595		return (error);
596	if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
597		return (error);
598	td->td_frame->tf_eflags = (td->td_frame->tf_eflags & ~PSL_IOPL) |
599	    (args->level * (PSL_IOPL / 3));
600	return (0);
601}
602
603int
604linux_modify_ldt(td, uap)
605	struct thread *td;
606	struct linux_modify_ldt_args *uap;
607{
608	int error;
609	caddr_t sg;
610	struct sysarch_args args;
611	struct i386_ldt_args *ldt;
612	struct l_descriptor ld;
613	union descriptor *desc;
614
615	sg = stackgap_init();
616
617	if (uap->ptr == NULL)
618		return (EINVAL);
619
620	switch (uap->func) {
621	case 0x00: /* read_ldt */
622		ldt = stackgap_alloc(&sg, sizeof(*ldt));
623		ldt->start = 0;
624		ldt->descs = uap->ptr;
625		ldt->num = uap->bytecount / sizeof(union descriptor);
626		args.op = I386_GET_LDT;
627		args.parms = (char*)ldt;
628		error = sysarch(td, &args);
629		td->td_retval[0] *= sizeof(union descriptor);
630		break;
631	case 0x01: /* write_ldt */
632	case 0x11: /* write_ldt */
633		if (uap->bytecount != sizeof(ld))
634			return (EINVAL);
635
636		error = copyin(uap->ptr, &ld, sizeof(ld));
637		if (error)
638			return (error);
639
640		ldt = stackgap_alloc(&sg, sizeof(*ldt));
641		desc = stackgap_alloc(&sg, sizeof(*desc));
642		ldt->start = ld.entry_number;
643		ldt->descs = desc;
644		ldt->num = 1;
645		desc->sd.sd_lolimit = (ld.limit & 0x0000ffff);
646		desc->sd.sd_hilimit = (ld.limit & 0x000f0000) >> 16;
647		desc->sd.sd_lobase = (ld.base_addr & 0x00ffffff);
648		desc->sd.sd_hibase = (ld.base_addr & 0xff000000) >> 24;
649		desc->sd.sd_type = SDT_MEMRO | ((ld.read_exec_only ^ 1) << 1) |
650			(ld.contents << 2);
651		desc->sd.sd_dpl = 3;
652		desc->sd.sd_p = (ld.seg_not_present ^ 1);
653		desc->sd.sd_xx = 0;
654		desc->sd.sd_def32 = ld.seg_32bit;
655		desc->sd.sd_gran = ld.limit_in_pages;
656		args.op = I386_SET_LDT;
657		args.parms = (char*)ldt;
658		error = sysarch(td, &args);
659		break;
660	default:
661		error = EINVAL;
662		break;
663	}
664
665	if (error == EOPNOTSUPP) {
666		printf("linux: modify_ldt needs kernel option USER_LDT\n");
667		error = ENOSYS;
668	}
669
670	return (error);
671}
672
673int
674linux_sigaction(struct thread *td, struct linux_sigaction_args *args)
675{
676	l_osigaction_t osa;
677	l_sigaction_t act, oact;
678	int error;
679
680#ifdef DEBUG
681	if (ldebug(sigaction))
682		printf(ARGS(sigaction, "%d, %p, %p"),
683		    args->sig, (void *)args->nsa, (void *)args->osa);
684#endif
685
686	if (args->nsa != NULL) {
687		error = copyin((caddr_t)args->nsa, &osa,
688		    sizeof(l_osigaction_t));
689		if (error)
690			return (error);
691		act.lsa_handler = osa.lsa_handler;
692		act.lsa_flags = osa.lsa_flags;
693		act.lsa_restorer = osa.lsa_restorer;
694		LINUX_SIGEMPTYSET(act.lsa_mask);
695		act.lsa_mask.__bits[0] = osa.lsa_mask;
696	}
697
698	error = linux_do_sigaction(td, args->sig, args->nsa ? &act : NULL,
699	    args->osa ? &oact : NULL);
700
701	if (args->osa != NULL && !error) {
702		osa.lsa_handler = oact.lsa_handler;
703		osa.lsa_flags = oact.lsa_flags;
704		osa.lsa_restorer = oact.lsa_restorer;
705		osa.lsa_mask = oact.lsa_mask.__bits[0];
706		error = copyout(&osa, (caddr_t)args->osa,
707		    sizeof(l_osigaction_t));
708	}
709
710	return (error);
711}
712
713/*
714 * Linux has two extra args, restart and oldmask.  We dont use these,
715 * but it seems that "restart" is actually a context pointer that
716 * enables the signal to happen with a different register set.
717 */
718int
719linux_sigsuspend(struct thread *td, struct linux_sigsuspend_args *args)
720{
721	sigset_t sigmask;
722	l_sigset_t mask;
723
724#ifdef DEBUG
725	if (ldebug(sigsuspend))
726		printf(ARGS(sigsuspend, "%08lx"), (unsigned long)args->mask);
727#endif
728
729	LINUX_SIGEMPTYSET(mask);
730	mask.__bits[0] = args->mask;
731	linux_to_bsd_sigset(&mask, &sigmask);
732	return (kern_sigsuspend(td, sigmask));
733}
734
735int
736linux_rt_sigsuspend(td, uap)
737	struct thread *td;
738	struct linux_rt_sigsuspend_args *uap;
739{
740	l_sigset_t lmask;
741	sigset_t sigmask;
742	int error;
743
744#ifdef DEBUG
745	if (ldebug(rt_sigsuspend))
746		printf(ARGS(rt_sigsuspend, "%p, %d"),
747		    (void *)uap->newset, uap->sigsetsize);
748#endif
749
750	if (uap->sigsetsize != sizeof(l_sigset_t))
751		return (EINVAL);
752
753	error = copyin(uap->newset, &lmask, sizeof(l_sigset_t));
754	if (error)
755		return (error);
756
757	linux_to_bsd_sigset(&lmask, &sigmask);
758	return (kern_sigsuspend(td, sigmask));
759}
760
761int
762linux_pause(struct thread *td, struct linux_pause_args *args)
763{
764	struct proc *p = td->td_proc;
765	sigset_t sigmask;
766
767#ifdef DEBUG
768	if (ldebug(pause))
769		printf(ARGS(pause, ""));
770#endif
771
772	PROC_LOCK(p);
773	sigmask = p->p_sigmask;
774	PROC_UNLOCK(p);
775	return (kern_sigsuspend(td, sigmask));
776}
777
778int
779linux_sigaltstack(struct thread *td, struct linux_sigaltstack_args *uap)
780{
781	stack_t ss, oss;
782	l_stack_t lss;
783	int error;
784
785#ifdef DEBUG
786	if (ldebug(sigaltstack))
787		printf(ARGS(sigaltstack, "%p, %p"), uap->uss, uap->uoss);
788#endif
789
790	if (uap->uss != NULL) {
791		error = copyin(uap->uss, &lss, sizeof(l_stack_t));
792		if (error)
793			return (error);
794
795		ss.ss_sp = lss.ss_sp;
796		ss.ss_size = lss.ss_size;
797		ss.ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags);
798	}
799	error = kern_sigaltstack(td, (uap->uoss != NULL) ? &oss : NULL,
800	    (uap->uss != NULL) ? &ss : NULL);
801	if (!error && uap->uoss != NULL) {
802		lss.ss_sp = oss.ss_sp;
803		lss.ss_size = oss.ss_size;
804		lss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags);
805		error = copyout(&lss, uap->uoss, sizeof(l_stack_t));
806	}
807
808	return (error);
809}
810
811int
812linux_ftruncate64(struct thread *td, struct linux_ftruncate64_args *args)
813{
814	struct ftruncate_args sa;
815
816#ifdef DEBUG
817	if (ldebug(ftruncate64))
818		printf(ARGS(ftruncate64, "%d, %d"), args->fd, args->length);
819#endif
820
821	sa.fd = args->fd;
822	sa.pad = 0;
823	sa.length = args->length;
824	return ftruncate(td, &sa);
825}
826