xref: /freebsd/sys/i386/linux/linux_machdep.c (revision d056fa046c6a91b90cd98165face0e42a33a5173)
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/imgact.h>
35 #include <sys/lock.h>
36 #include <sys/malloc.h>
37 #include <sys/mman.h>
38 #include <sys/mutex.h>
39 #include <sys/proc.h>
40 #include <sys/resource.h>
41 #include <sys/resourcevar.h>
42 #include <sys/signalvar.h>
43 #include <sys/syscallsubr.h>
44 #include <sys/sysproto.h>
45 #include <sys/unistd.h>
46 
47 #include <machine/frame.h>
48 #include <machine/psl.h>
49 #include <machine/segments.h>
50 #include <machine/sysarch.h>
51 
52 #include <vm/vm.h>
53 #include <vm/pmap.h>
54 #include <vm/vm_map.h>
55 
56 #include <i386/linux/linux.h>
57 #include <i386/linux/linux_proto.h>
58 #include <compat/linux/linux_ipc.h>
59 #include <compat/linux/linux_signal.h>
60 #include <compat/linux/linux_util.h>
61 
62 struct l_descriptor {
63 	l_uint		entry_number;
64 	l_ulong		base_addr;
65 	l_uint		limit;
66 	l_uint		seg_32bit:1;
67 	l_uint		contents:2;
68 	l_uint		read_exec_only:1;
69 	l_uint		limit_in_pages:1;
70 	l_uint		seg_not_present:1;
71 	l_uint		useable:1;
72 };
73 
74 struct l_old_select_argv {
75 	l_int		nfds;
76 	l_fd_set	*readfds;
77 	l_fd_set	*writefds;
78 	l_fd_set	*exceptfds;
79 	struct l_timeval	*timeout;
80 };
81 
82 int
83 linux_to_bsd_sigaltstack(int lsa)
84 {
85 	int bsa = 0;
86 
87 	if (lsa & LINUX_SS_DISABLE)
88 		bsa |= SS_DISABLE;
89 	if (lsa & LINUX_SS_ONSTACK)
90 		bsa |= SS_ONSTACK;
91 	return (bsa);
92 }
93 
94 int
95 bsd_to_linux_sigaltstack(int bsa)
96 {
97 	int lsa = 0;
98 
99 	if (bsa & SS_DISABLE)
100 		lsa |= LINUX_SS_DISABLE;
101 	if (bsa & SS_ONSTACK)
102 		lsa |= LINUX_SS_ONSTACK;
103 	return (lsa);
104 }
105 
106 int
107 linux_execve(struct thread *td, struct linux_execve_args *args)
108 {
109 	int error;
110 	char *newpath;
111 	struct image_args eargs;
112 
113 	LCONVPATHEXIST(td, args->path, &newpath);
114 
115 #ifdef DEBUG
116 	if (ldebug(execve))
117 		printf(ARGS(execve, "%s"), newpath);
118 #endif
119 
120 	error = exec_copyin_args(&eargs, newpath, UIO_SYSSPACE,
121 	    args->argp, args->envp);
122 	free(newpath, M_TEMP);
123 	if (error == 0)
124 		error = kern_execve(td, &eargs, NULL);
125 	return (error);
126 }
127 
128 struct l_ipc_kludge {
129 	struct l_msgbuf *msgp;
130 	l_long msgtyp;
131 };
132 
133 int
134 linux_ipc(struct thread *td, struct linux_ipc_args *args)
135 {
136 
137 	switch (args->what & 0xFFFF) {
138 	case LINUX_SEMOP: {
139 		struct linux_semop_args a;
140 
141 		a.semid = args->arg1;
142 		a.tsops = args->ptr;
143 		a.nsops = args->arg2;
144 		return (linux_semop(td, &a));
145 	}
146 	case LINUX_SEMGET: {
147 		struct linux_semget_args a;
148 
149 		a.key = args->arg1;
150 		a.nsems = args->arg2;
151 		a.semflg = args->arg3;
152 		return (linux_semget(td, &a));
153 	}
154 	case LINUX_SEMCTL: {
155 		struct linux_semctl_args a;
156 		int error;
157 
158 		a.semid = args->arg1;
159 		a.semnum = args->arg2;
160 		a.cmd = args->arg3;
161 		error = copyin(args->ptr, &a.arg, sizeof(a.arg));
162 		if (error)
163 			return (error);
164 		return (linux_semctl(td, &a));
165 	}
166 	case LINUX_MSGSND: {
167 		struct linux_msgsnd_args a;
168 
169 		a.msqid = args->arg1;
170 		a.msgp = args->ptr;
171 		a.msgsz = args->arg2;
172 		a.msgflg = args->arg3;
173 		return (linux_msgsnd(td, &a));
174 	}
175 	case LINUX_MSGRCV: {
176 		struct linux_msgrcv_args a;
177 
178 		a.msqid = args->arg1;
179 		a.msgsz = args->arg2;
180 		a.msgflg = args->arg3;
181 		if ((args->what >> 16) == 0) {
182 			struct l_ipc_kludge tmp;
183 			int error;
184 
185 			if (args->ptr == NULL)
186 				return (EINVAL);
187 			error = copyin(args->ptr, &tmp, sizeof(tmp));
188 			if (error)
189 				return (error);
190 			a.msgp = tmp.msgp;
191 			a.msgtyp = tmp.msgtyp;
192 		} else {
193 			a.msgp = args->ptr;
194 			a.msgtyp = args->arg5;
195 		}
196 		return (linux_msgrcv(td, &a));
197 	}
198 	case LINUX_MSGGET: {
199 		struct linux_msgget_args a;
200 
201 		a.key = args->arg1;
202 		a.msgflg = args->arg2;
203 		return (linux_msgget(td, &a));
204 	}
205 	case LINUX_MSGCTL: {
206 		struct linux_msgctl_args a;
207 
208 		a.msqid = args->arg1;
209 		a.cmd = args->arg2;
210 		a.buf = args->ptr;
211 		return (linux_msgctl(td, &a));
212 	}
213 	case LINUX_SHMAT: {
214 		struct linux_shmat_args a;
215 
216 		a.shmid = args->arg1;
217 		a.shmaddr = args->ptr;
218 		a.shmflg = args->arg2;
219 		a.raddr = (l_ulong *)args->arg3;
220 		return (linux_shmat(td, &a));
221 	}
222 	case LINUX_SHMDT: {
223 		struct linux_shmdt_args a;
224 
225 		a.shmaddr = args->ptr;
226 		return (linux_shmdt(td, &a));
227 	}
228 	case LINUX_SHMGET: {
229 		struct linux_shmget_args a;
230 
231 		a.key = args->arg1;
232 		a.size = args->arg2;
233 		a.shmflg = args->arg3;
234 		return (linux_shmget(td, &a));
235 	}
236 	case LINUX_SHMCTL: {
237 		struct linux_shmctl_args a;
238 
239 		a.shmid = args->arg1;
240 		a.cmd = args->arg2;
241 		a.buf = args->ptr;
242 		return (linux_shmctl(td, &a));
243 	}
244 	default:
245 		break;
246 	}
247 
248 	return (EINVAL);
249 }
250 
251 int
252 linux_old_select(struct thread *td, struct linux_old_select_args *args)
253 {
254 	struct l_old_select_argv linux_args;
255 	struct linux_select_args newsel;
256 	int error;
257 
258 #ifdef DEBUG
259 	if (ldebug(old_select))
260 		printf(ARGS(old_select, "%p"), args->ptr);
261 #endif
262 
263 	error = copyin(args->ptr, &linux_args, sizeof(linux_args));
264 	if (error)
265 		return (error);
266 
267 	newsel.nfds = linux_args.nfds;
268 	newsel.readfds = linux_args.readfds;
269 	newsel.writefds = linux_args.writefds;
270 	newsel.exceptfds = linux_args.exceptfds;
271 	newsel.timeout = linux_args.timeout;
272 	return (linux_select(td, &newsel));
273 }
274 
275 int
276 linux_fork(struct thread *td, struct linux_fork_args *args)
277 {
278 	int error;
279 
280 #ifdef DEBUG
281 	if (ldebug(fork))
282 		printf(ARGS(fork, ""));
283 #endif
284 
285 	if ((error = fork(td, (struct fork_args *)args)) != 0)
286 		return (error);
287 
288 	if (td->td_retval[1] == 1)
289 		td->td_retval[0] = 0;
290 	return (0);
291 }
292 
293 int
294 linux_vfork(struct thread *td, struct linux_vfork_args *args)
295 {
296 	int error;
297 
298 #ifdef DEBUG
299 	if (ldebug(vfork))
300 		printf(ARGS(vfork, ""));
301 #endif
302 
303 	if ((error = vfork(td, (struct vfork_args *)args)) != 0)
304 		return (error);
305 	/* Are we the child? */
306 	if (td->td_retval[1] == 1)
307 		td->td_retval[0] = 0;
308 	return (0);
309 }
310 
311 #define CLONE_VM	0x100
312 #define CLONE_FS	0x200
313 #define CLONE_FILES	0x400
314 #define CLONE_SIGHAND	0x800
315 #define CLONE_PID	0x1000
316 #define CLONE_THREAD	0x10000
317 
318 #define THREADING_FLAGS	(CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND)
319 
320 int
321 linux_clone(struct thread *td, struct linux_clone_args *args)
322 {
323 	int error, ff = RFPROC | RFSTOPPED;
324 	struct proc *p2;
325 	struct thread *td2;
326 	int exit_signal;
327 
328 #ifdef DEBUG
329 	if (ldebug(clone)) {
330 		printf(ARGS(clone, "flags %x, stack %x"),
331 		    (unsigned int)args->flags, (unsigned int)args->stack);
332 		if (args->flags & CLONE_PID)
333 			printf(LMSG("CLONE_PID not yet supported"));
334 	}
335 #endif
336 
337 	if (!args->stack)
338 		return (EINVAL);
339 
340 	exit_signal = args->flags & 0x000000ff;
341 	if (exit_signal >= LINUX_NSIG)
342 		return (EINVAL);
343 
344 	if (exit_signal <= LINUX_SIGTBLSZ)
345 		exit_signal = linux_to_bsd_signal[_SIG_IDX(exit_signal)];
346 
347 	if (args->flags & CLONE_VM)
348 		ff |= RFMEM;
349 	if (args->flags & CLONE_SIGHAND)
350 		ff |= RFSIGSHARE;
351 	if (!(args->flags & CLONE_FILES))
352 		ff |= RFFDG;
353 
354 	/*
355 	 * Attempt to detect when linux_clone(2) is used for creating
356 	 * kernel threads. Unfortunately despite the existence of the
357 	 * CLONE_THREAD flag, version of linuxthreads package used in
358 	 * most popular distros as of beginning of 2005 doesn't make
359 	 * any use of it. Therefore, this detection relay fully on
360 	 * empirical observation that linuxthreads sets certain
361 	 * combination of flags, so that we can make more or less
362 	 * precise detection and notify the FreeBSD kernel that several
363 	 * processes are in fact part of the same threading group, so
364 	 * that special treatment is necessary for signal delivery
365 	 * between those processes and fd locking.
366 	 */
367 	if ((args->flags & 0xffffff00) == THREADING_FLAGS)
368 		ff |= RFTHREAD;
369 
370 	error = fork1(td, ff, 0, &p2);
371 	if (error)
372 		return (error);
373 
374 
375 	PROC_LOCK(p2);
376 	p2->p_sigparent = exit_signal;
377 	PROC_UNLOCK(p2);
378 	td2 = FIRST_THREAD_IN_PROC(p2);
379 	td2->td_frame->tf_esp = (unsigned int)args->stack;
380 
381 #ifdef DEBUG
382 	if (ldebug(clone))
383 		printf(LMSG("clone: successful rfork to %ld, stack %p sig = %d"),
384 		    (long)p2->p_pid, args->stack, exit_signal);
385 #endif
386 
387 	/*
388 	 * Make this runnable after we are finished with it.
389 	 */
390 	mtx_lock_spin(&sched_lock);
391 	TD_SET_CAN_RUN(td2);
392 	setrunqueue(td2, SRQ_BORING);
393 	mtx_unlock_spin(&sched_lock);
394 
395 	td->td_retval[0] = p2->p_pid;
396 	td->td_retval[1] = 0;
397 	return (0);
398 }
399 
400 /* XXX move */
401 struct l_mmap_argv {
402 	l_caddr_t	addr;
403 	l_int		len;
404 	l_int		prot;
405 	l_int		flags;
406 	l_int		fd;
407 	l_int		pos;
408 };
409 
410 #define STACK_SIZE  (2 * 1024 * 1024)
411 #define GUARD_SIZE  (4 * PAGE_SIZE)
412 
413 static int linux_mmap_common(struct thread *, struct l_mmap_argv *);
414 
415 int
416 linux_mmap2(struct thread *td, struct linux_mmap2_args *args)
417 {
418 	struct l_mmap_argv linux_args;
419 
420 #ifdef DEBUG
421 	if (ldebug(mmap2))
422 		printf(ARGS(mmap2, "%p, %d, %d, 0x%08x, %d, %d"),
423 		    (void *)args->addr, args->len, args->prot,
424 		    args->flags, args->fd, args->pgoff);
425 #endif
426 
427 	linux_args.addr = (l_caddr_t)args->addr;
428 	linux_args.len = args->len;
429 	linux_args.prot = args->prot;
430 	linux_args.flags = args->flags;
431 	linux_args.fd = args->fd;
432 	linux_args.pos = args->pgoff * PAGE_SIZE;
433 
434 	return (linux_mmap_common(td, &linux_args));
435 }
436 
437 int
438 linux_mmap(struct thread *td, struct linux_mmap_args *args)
439 {
440 	int error;
441 	struct l_mmap_argv linux_args;
442 
443 	error = copyin(args->ptr, &linux_args, sizeof(linux_args));
444 	if (error)
445 		return (error);
446 
447 #ifdef DEBUG
448 	if (ldebug(mmap))
449 		printf(ARGS(mmap, "%p, %d, %d, 0x%08x, %d, %d"),
450 		    (void *)linux_args.addr, linux_args.len, linux_args.prot,
451 		    linux_args.flags, linux_args.fd, linux_args.pos);
452 #endif
453 
454 	return (linux_mmap_common(td, &linux_args));
455 }
456 
457 static int
458 linux_mmap_common(struct thread *td, struct l_mmap_argv *linux_args)
459 {
460 	struct proc *p = td->td_proc;
461 	struct mmap_args /* {
462 		caddr_t addr;
463 		size_t len;
464 		int prot;
465 		int flags;
466 		int fd;
467 		long pad;
468 		off_t pos;
469 	} */ bsd_args;
470 	int error;
471 
472 	error = 0;
473 	bsd_args.flags = 0;
474 	if (linux_args->flags & LINUX_MAP_SHARED)
475 		bsd_args.flags |= MAP_SHARED;
476 	if (linux_args->flags & LINUX_MAP_PRIVATE)
477 		bsd_args.flags |= MAP_PRIVATE;
478 	if (linux_args->flags & LINUX_MAP_FIXED)
479 		bsd_args.flags |= MAP_FIXED;
480 	if (linux_args->flags & LINUX_MAP_ANON)
481 		bsd_args.flags |= MAP_ANON;
482 	else
483 		bsd_args.flags |= MAP_NOSYNC;
484 	if (linux_args->flags & LINUX_MAP_GROWSDOWN) {
485 		bsd_args.flags |= MAP_STACK;
486 
487 		/* The linux MAP_GROWSDOWN option does not limit auto
488 		 * growth of the region.  Linux mmap with this option
489 		 * takes as addr the inital BOS, and as len, the initial
490 		 * region size.  It can then grow down from addr without
491 		 * limit.  However, linux threads has an implicit internal
492 		 * limit to stack size of STACK_SIZE.  Its just not
493 		 * enforced explicitly in linux.  But, here we impose
494 		 * a limit of (STACK_SIZE - GUARD_SIZE) on the stack
495 		 * region, since we can do this with our mmap.
496 		 *
497 		 * Our mmap with MAP_STACK takes addr as the maximum
498 		 * downsize limit on BOS, and as len the max size of
499 		 * the region.  It them maps the top SGROWSIZ bytes,
500 		 * and autgrows the region down, up to the limit
501 		 * in addr.
502 		 *
503 		 * If we don't use the MAP_STACK option, the effect
504 		 * of this code is to allocate a stack region of a
505 		 * fixed size of (STACK_SIZE - GUARD_SIZE).
506 		 */
507 
508 		/* This gives us TOS */
509 		bsd_args.addr = linux_args->addr + linux_args->len;
510 
511 		if (bsd_args.addr > p->p_vmspace->vm_maxsaddr) {
512 			/* Some linux apps will attempt to mmap
513 			 * thread stacks near the top of their
514 			 * address space.  If their TOS is greater
515 			 * than vm_maxsaddr, vm_map_growstack()
516 			 * will confuse the thread stack with the
517 			 * process stack and deliver a SEGV if they
518 			 * attempt to grow the thread stack past their
519 			 * current stacksize rlimit.  To avoid this,
520 			 * adjust vm_maxsaddr upwards to reflect
521 			 * the current stacksize rlimit rather
522 			 * than the maximum possible stacksize.
523 			 * It would be better to adjust the
524 			 * mmap'ed region, but some apps do not check
525 			 * mmap's return value.
526 			 */
527 			PROC_LOCK(p);
528 			p->p_vmspace->vm_maxsaddr = (char *)USRSTACK -
529 			    lim_cur(p, RLIMIT_STACK);
530 			PROC_UNLOCK(p);
531 		}
532 
533 		/* This gives us our maximum stack size */
534 		if (linux_args->len > STACK_SIZE - GUARD_SIZE)
535 			bsd_args.len = linux_args->len;
536 		else
537 			bsd_args.len  = STACK_SIZE - GUARD_SIZE;
538 
539 		/* This gives us a new BOS.  If we're using VM_STACK, then
540 		 * mmap will just map the top SGROWSIZ bytes, and let
541 		 * the stack grow down to the limit at BOS.  If we're
542 		 * not using VM_STACK we map the full stack, since we
543 		 * don't have a way to autogrow it.
544 		 */
545 		bsd_args.addr -= bsd_args.len;
546 	} else {
547 		bsd_args.addr = linux_args->addr;
548 		bsd_args.len  = linux_args->len;
549 	}
550 
551 	bsd_args.prot = linux_args->prot | PROT_READ;	/* always required */
552 	if (linux_args->flags & LINUX_MAP_ANON)
553 		bsd_args.fd = -1;
554 	else
555 		bsd_args.fd = linux_args->fd;
556 	bsd_args.pos = linux_args->pos;
557 	bsd_args.pad = 0;
558 
559 #ifdef DEBUG
560 	if (ldebug(mmap))
561 		printf("-> %s(%p, %d, %d, 0x%08x, %d, 0x%x)\n",
562 		    __func__,
563 		    (void *)bsd_args.addr, bsd_args.len, bsd_args.prot,
564 		    bsd_args.flags, bsd_args.fd, (int)bsd_args.pos);
565 #endif
566 	error = mmap(td, &bsd_args);
567 #ifdef DEBUG
568 	if (ldebug(mmap))
569 		printf("-> %s() return: 0x%x (0x%08x)\n",
570 			__func__, error, (u_int)td->td_retval[0]);
571 #endif
572 	return (error);
573 }
574 
575 int
576 linux_pipe(struct thread *td, struct linux_pipe_args *args)
577 {
578 	int error;
579 	int reg_edx;
580 
581 #ifdef DEBUG
582 	if (ldebug(pipe))
583 		printf(ARGS(pipe, "*"));
584 #endif
585 
586 	reg_edx = td->td_retval[1];
587 	error = pipe(td, 0);
588 	if (error) {
589 		td->td_retval[1] = reg_edx;
590 		return (error);
591 	}
592 
593 	error = copyout(td->td_retval, args->pipefds, 2*sizeof(int));
594 	if (error) {
595 		td->td_retval[1] = reg_edx;
596 		return (error);
597 	}
598 
599 	td->td_retval[1] = reg_edx;
600 	td->td_retval[0] = 0;
601 	return (0);
602 }
603 
604 int
605 linux_ioperm(struct thread *td, struct linux_ioperm_args *args)
606 {
607 	int error;
608 	struct i386_ioperm_args iia;
609 
610 	iia.start = args->start;
611 	iia.length = args->length;
612 	iia.enable = args->enable;
613 	mtx_lock(&Giant);
614 	error = i386_set_ioperm(td, &iia);
615 	mtx_unlock(&Giant);
616 	return (error);
617 }
618 
619 int
620 linux_iopl(struct thread *td, struct linux_iopl_args *args)
621 {
622 	int error;
623 
624 	if (args->level < 0 || args->level > 3)
625 		return (EINVAL);
626 	if ((error = suser(td)) != 0)
627 		return (error);
628 	if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
629 		return (error);
630 	td->td_frame->tf_eflags = (td->td_frame->tf_eflags & ~PSL_IOPL) |
631 	    (args->level * (PSL_IOPL / 3));
632 	return (0);
633 }
634 
635 int
636 linux_modify_ldt(struct thread *td, struct linux_modify_ldt_args *uap)
637 {
638 	int error;
639 	struct i386_ldt_args ldt;
640 	struct l_descriptor ld;
641 	union descriptor desc;
642 
643 	if (uap->ptr == NULL)
644 		return (EINVAL);
645 
646 	switch (uap->func) {
647 	case 0x00: /* read_ldt */
648 		ldt.start = 0;
649 		ldt.descs = uap->ptr;
650 		ldt.num = uap->bytecount / sizeof(union descriptor);
651 		mtx_lock(&Giant);
652 		error = i386_get_ldt(td, &ldt);
653 		td->td_retval[0] *= sizeof(union descriptor);
654 		mtx_unlock(&Giant);
655 		break;
656 	case 0x01: /* write_ldt */
657 	case 0x11: /* write_ldt */
658 		if (uap->bytecount != sizeof(ld))
659 			return (EINVAL);
660 
661 		error = copyin(uap->ptr, &ld, sizeof(ld));
662 		if (error)
663 			return (error);
664 
665 		ldt.start = ld.entry_number;
666 		ldt.descs = &desc;
667 		ldt.num = 1;
668 		desc.sd.sd_lolimit = (ld.limit & 0x0000ffff);
669 		desc.sd.sd_hilimit = (ld.limit & 0x000f0000) >> 16;
670 		desc.sd.sd_lobase = (ld.base_addr & 0x00ffffff);
671 		desc.sd.sd_hibase = (ld.base_addr & 0xff000000) >> 24;
672 		desc.sd.sd_type = SDT_MEMRO | ((ld.read_exec_only ^ 1) << 1) |
673 			(ld.contents << 2);
674 		desc.sd.sd_dpl = 3;
675 		desc.sd.sd_p = (ld.seg_not_present ^ 1);
676 		desc.sd.sd_xx = 0;
677 		desc.sd.sd_def32 = ld.seg_32bit;
678 		desc.sd.sd_gran = ld.limit_in_pages;
679 		mtx_lock(&Giant);
680 		error = i386_set_ldt(td, &ldt, &desc);
681 		mtx_unlock(&Giant);
682 		break;
683 	default:
684 		error = EINVAL;
685 		break;
686 	}
687 
688 	if (error == EOPNOTSUPP) {
689 		printf("linux: modify_ldt needs kernel option USER_LDT\n");
690 		error = ENOSYS;
691 	}
692 
693 	return (error);
694 }
695 
696 int
697 linux_sigaction(struct thread *td, struct linux_sigaction_args *args)
698 {
699 	l_osigaction_t osa;
700 	l_sigaction_t act, oact;
701 	int error;
702 
703 #ifdef DEBUG
704 	if (ldebug(sigaction))
705 		printf(ARGS(sigaction, "%d, %p, %p"),
706 		    args->sig, (void *)args->nsa, (void *)args->osa);
707 #endif
708 
709 	if (args->nsa != NULL) {
710 		error = copyin(args->nsa, &osa, sizeof(l_osigaction_t));
711 		if (error)
712 			return (error);
713 		act.lsa_handler = osa.lsa_handler;
714 		act.lsa_flags = osa.lsa_flags;
715 		act.lsa_restorer = osa.lsa_restorer;
716 		LINUX_SIGEMPTYSET(act.lsa_mask);
717 		act.lsa_mask.__bits[0] = osa.lsa_mask;
718 	}
719 
720 	error = linux_do_sigaction(td, args->sig, args->nsa ? &act : NULL,
721 	    args->osa ? &oact : NULL);
722 
723 	if (args->osa != NULL && !error) {
724 		osa.lsa_handler = oact.lsa_handler;
725 		osa.lsa_flags = oact.lsa_flags;
726 		osa.lsa_restorer = oact.lsa_restorer;
727 		osa.lsa_mask = oact.lsa_mask.__bits[0];
728 		error = copyout(&osa, args->osa, sizeof(l_osigaction_t));
729 	}
730 
731 	return (error);
732 }
733 
734 /*
735  * Linux has two extra args, restart and oldmask.  We dont use these,
736  * but it seems that "restart" is actually a context pointer that
737  * enables the signal to happen with a different register set.
738  */
739 int
740 linux_sigsuspend(struct thread *td, struct linux_sigsuspend_args *args)
741 {
742 	sigset_t sigmask;
743 	l_sigset_t mask;
744 
745 #ifdef DEBUG
746 	if (ldebug(sigsuspend))
747 		printf(ARGS(sigsuspend, "%08lx"), (unsigned long)args->mask);
748 #endif
749 
750 	LINUX_SIGEMPTYSET(mask);
751 	mask.__bits[0] = args->mask;
752 	linux_to_bsd_sigset(&mask, &sigmask);
753 	return (kern_sigsuspend(td, sigmask));
754 }
755 
756 int
757 linux_rt_sigsuspend(struct thread *td, struct linux_rt_sigsuspend_args *uap)
758 {
759 	l_sigset_t lmask;
760 	sigset_t sigmask;
761 	int error;
762 
763 #ifdef DEBUG
764 	if (ldebug(rt_sigsuspend))
765 		printf(ARGS(rt_sigsuspend, "%p, %d"),
766 		    (void *)uap->newset, uap->sigsetsize);
767 #endif
768 
769 	if (uap->sigsetsize != sizeof(l_sigset_t))
770 		return (EINVAL);
771 
772 	error = copyin(uap->newset, &lmask, sizeof(l_sigset_t));
773 	if (error)
774 		return (error);
775 
776 	linux_to_bsd_sigset(&lmask, &sigmask);
777 	return (kern_sigsuspend(td, sigmask));
778 }
779 
780 int
781 linux_pause(struct thread *td, struct linux_pause_args *args)
782 {
783 	struct proc *p = td->td_proc;
784 	sigset_t sigmask;
785 
786 #ifdef DEBUG
787 	if (ldebug(pause))
788 		printf(ARGS(pause, ""));
789 #endif
790 
791 	PROC_LOCK(p);
792 	sigmask = td->td_sigmask;
793 	PROC_UNLOCK(p);
794 	return (kern_sigsuspend(td, sigmask));
795 }
796 
797 int
798 linux_sigaltstack(struct thread *td, struct linux_sigaltstack_args *uap)
799 {
800 	stack_t ss, oss;
801 	l_stack_t lss;
802 	int error;
803 
804 #ifdef DEBUG
805 	if (ldebug(sigaltstack))
806 		printf(ARGS(sigaltstack, "%p, %p"), uap->uss, uap->uoss);
807 #endif
808 
809 	if (uap->uss != NULL) {
810 		error = copyin(uap->uss, &lss, sizeof(l_stack_t));
811 		if (error)
812 			return (error);
813 
814 		ss.ss_sp = lss.ss_sp;
815 		ss.ss_size = lss.ss_size;
816 		ss.ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags);
817 	}
818 	error = kern_sigaltstack(td, (uap->uss != NULL) ? &ss : NULL,
819 	    (uap->uoss != NULL) ? &oss : NULL);
820 	if (!error && uap->uoss != NULL) {
821 		lss.ss_sp = oss.ss_sp;
822 		lss.ss_size = oss.ss_size;
823 		lss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags);
824 		error = copyout(&lss, uap->uoss, sizeof(l_stack_t));
825 	}
826 
827 	return (error);
828 }
829 
830 int
831 linux_ftruncate64(struct thread *td, struct linux_ftruncate64_args *args)
832 {
833 	struct ftruncate_args sa;
834 
835 #ifdef DEBUG
836 	if (ldebug(ftruncate64))
837 		printf(ARGS(ftruncate64, "%u, %jd"), args->fd,
838 		    (intmax_t)args->length);
839 #endif
840 
841 	sa.fd = args->fd;
842 	sa.pad = 0;
843 	sa.length = args->length;
844 	return ftruncate(td, &sa);
845 }
846 
847 int
848 linux_set_thread_area(struct thread *td, struct linux_set_thread_area_args *args)
849 {
850 	/*
851 	 * Return an error code instead of raising a SIGSYS so that
852 	 * the caller will fall back to simpler LDT methods.
853 	 */
854 	return (ENOSYS);
855 }
856 
857 int
858 linux_gettid(struct thread *td, struct linux_gettid_args *args)
859 {
860 
861 	td->td_retval[0] = td->td_proc->p_pid;
862 	return (0);
863 }
864 
865 int
866 linux_tkill(struct thread *td, struct linux_tkill_args *args)
867 {
868 
869 	return (linux_kill(td, (struct linux_kill_args *) args));
870 }
871 
872