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