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