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