linux - Add syscalls
[dragonfly.git] / sys / emulation / linux / i386 / linux_machdep.c
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 $
29  * $DragonFly: src/sys/emulation/linux/i386/linux_machdep.c,v 1.23 2007/07/30 17:41:23 pavalos Exp $
30  */
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/imgact.h>
35 #include <sys/kern_syscall.h>
36 #include <sys/lock.h>
37 #include <sys/mman.h>
38 #include <sys/nlookup.h>
39 #include <sys/proc.h>
40 #include <sys/priv.h>
41 #include <sys/resource.h>
42 #include <sys/resourcevar.h>
43 #include <sys/ptrace.h>
44 #include <sys/sysproto.h>
45 #include <sys/thread2.h>
46 #include <sys/unistd.h>
47 #include <sys/wait.h>
48
49 #include <machine/frame.h>
50 #include <machine/psl.h>
51 #include <machine/segments.h>
52 #include <machine/sysarch.h>
53
54 #include <vm/vm.h>
55 #include <vm/pmap.h>
56 #include <vm/vm_map.h>
57
58 #include <sys/mplock2.h>
59
60 #include "linux.h"
61 #include "linux_proto.h"
62 #include "../linux_ipc.h"
63 #include "../linux_signal.h"
64 #include "../linux_util.h"
65 #include "../linux_emuldata.h"
66
67 struct l_descriptor {
68         l_uint          entry_number;
69         l_ulong         base_addr;
70         l_uint          limit;
71         l_uint          seg_32bit:1;
72         l_uint          contents:2;
73         l_uint          read_exec_only:1;
74         l_uint          limit_in_pages:1;
75         l_uint          seg_not_present:1;
76         l_uint          useable:1;
77 };
78
79 struct l_old_select_argv {
80         l_int           nfds;
81         l_fd_set        *readfds;
82         l_fd_set        *writefds;
83         l_fd_set        *exceptfds;
84         struct l_timeval        *timeout;
85 };
86
87 int
88 linux_to_bsd_sigaltstack(int lsa)
89 {
90         int bsa = 0;
91
92         if (lsa & LINUX_SS_DISABLE)
93                 bsa |= SS_DISABLE;
94         if (lsa & LINUX_SS_ONSTACK)
95                 bsa |= SS_ONSTACK;
96         return (bsa);
97 }
98
99 int
100 bsd_to_linux_sigaltstack(int bsa)
101 {
102         int lsa = 0;
103
104         if (bsa & SS_DISABLE)
105                 lsa |= LINUX_SS_DISABLE;
106         if (bsa & SS_ONSTACK)
107                 lsa |= LINUX_SS_ONSTACK;
108         return (lsa);
109 }
110
111 /*
112  * MPALMOSTSAFE
113  */
114 int
115 sys_linux_execve(struct linux_execve_args *args)
116 {
117         struct nlookupdata nd;
118         struct image_args exec_args;
119         char *path;
120         int error;
121
122         error = linux_copyin_path(args->path, &path, LINUX_PATH_EXISTS);
123         if (error)
124                 return (error);
125 #ifdef DEBUG
126         if (ldebug(execve))
127                 kprintf(ARGS(execve, "%s"), path);
128 #endif
129         get_mplock();
130         error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
131         bzero(&exec_args, sizeof(exec_args));
132         if (error == 0) {
133                 error = exec_copyin_args(&exec_args, path, PATH_SYSSPACE,
134                                         args->argp, args->envp);
135         }
136         if (error == 0)
137                 error = kern_execve(&nd, &exec_args);
138         nlookup_done(&nd);
139
140         /*
141          * The syscall result is returned in registers to the new program.
142          * Linux will register %edx as an atexit function and we must be
143          * sure to set it to 0.  XXX
144          */
145         if (error == 0) {
146                 args->sysmsg_result64 = 0;
147                 if (curproc->p_sysent == &elf_linux_sysvec)
148                         error = emuldata_init(curproc, NULL, 0);
149         }
150
151         exec_free_args(&exec_args);
152         linux_free_path(&path);
153
154         if (error < 0) {
155                 /* We hit a lethal error condition.  Let's die now. */
156                 exit1(W_EXITCODE(0, SIGABRT));
157                 /* NOTREACHED */
158         }
159         rel_mplock();
160
161         return(error);
162 }
163
164 struct l_ipc_kludge {
165         struct l_msgbuf *msgp;
166         l_long msgtyp;
167 };
168
169 /*
170  * MPALMOSTSAFE
171  */
172 int
173 sys_linux_ipc(struct linux_ipc_args *args)
174 {
175         int error = 0;
176
177         get_mplock();
178
179         switch (args->what & 0xFFFF) {
180         case LINUX_SEMOP: {
181                 struct linux_semop_args a;
182
183                 a.semid = args->arg1;
184                 a.tsops = args->ptr;
185                 a.nsops = args->arg2;
186                 a.sysmsg_lresult = 0;
187                 error = linux_semop(&a);
188                 args->sysmsg_lresult = a.sysmsg_lresult;
189                 break;
190         }
191         case LINUX_SEMGET: {
192                 struct linux_semget_args a;
193
194                 a.key = args->arg1;
195                 a.nsems = args->arg2;
196                 a.semflg = args->arg3;
197                 a.sysmsg_lresult = 0;
198                 error = linux_semget(&a);
199                 args->sysmsg_lresult = a.sysmsg_lresult;
200                 break;
201         }
202         case LINUX_SEMCTL: {
203                 struct linux_semctl_args a;
204                 int error;
205
206                 a.semid = args->arg1;
207                 a.semnum = args->arg2;
208                 a.cmd = args->arg3;
209                 a.sysmsg_lresult = 0;
210                 error = copyin((caddr_t)args->ptr, &a.arg, sizeof(a.arg));
211                 if (error)
212                         break;
213                 error = linux_semctl(&a);
214                 args->sysmsg_lresult = a.sysmsg_lresult;
215                 break;
216         }
217         case LINUX_MSGSND: {
218                 struct linux_msgsnd_args a;
219
220                 a.msqid = args->arg1;
221                 a.msgp = args->ptr;
222                 a.msgsz = args->arg2;
223                 a.msgflg = args->arg3;
224                 a.sysmsg_lresult = 0;
225                 error = linux_msgsnd(&a);
226                 args->sysmsg_lresult = a.sysmsg_lresult;
227                 break;
228         }
229         case LINUX_MSGRCV: {
230                 struct linux_msgrcv_args a;
231
232                 a.msqid = args->arg1;
233                 a.msgsz = args->arg2;
234                 if (a.msgsz < 0) {
235                         error = EINVAL;
236                         break;
237                 }
238                 a.msgflg = args->arg3;
239                 a.sysmsg_lresult = 0;
240                 if ((args->what >> 16) == 0) {
241                         struct l_ipc_kludge tmp;
242                         int error;
243
244                         if (args->ptr == NULL) {
245                                 error = EINVAL;
246                                 break;
247                         }
248                         error = copyin((caddr_t)args->ptr, &tmp, sizeof(tmp));
249                         if (error)
250                                 break;
251                         a.msgp = tmp.msgp;
252                         a.msgtyp = tmp.msgtyp;
253                 } else {
254                         a.msgp = args->ptr;
255                         a.msgtyp = args->arg5;
256                 }
257                 error = linux_msgrcv(&a);
258                 args->sysmsg_lresult = a.sysmsg_lresult;
259                 break;
260         }
261         case LINUX_MSGGET: {
262                 struct linux_msgget_args a;
263
264                 a.key = args->arg1;
265                 a.msgflg = args->arg2;
266                 a.sysmsg_lresult = 0;
267                 error = linux_msgget(&a);
268                 args->sysmsg_lresult = a.sysmsg_lresult;
269                 break;
270         }
271         case LINUX_MSGCTL: {
272                 struct linux_msgctl_args a;
273
274                 a.msqid = args->arg1;
275                 a.cmd = args->arg2;
276                 a.buf = args->ptr;
277                 a.sysmsg_lresult = 0;
278                 error = linux_msgctl(&a);
279                 args->sysmsg_lresult = a.sysmsg_lresult;
280                 break;
281         }
282         case LINUX_SHMAT: {
283                 struct linux_shmat_args a;
284
285                 a.shmid = args->arg1;
286                 a.shmaddr = args->ptr;
287                 a.shmflg = args->arg2;
288                 a.raddr = (l_ulong *)args->arg3;
289                 a.sysmsg_lresult = 0;
290                 error = linux_shmat(&a);
291                 args->sysmsg_lresult = a.sysmsg_lresult;
292                 break;
293         }
294         case LINUX_SHMDT: {
295                 struct linux_shmdt_args a;
296
297                 a.shmaddr = args->ptr;
298                 a.sysmsg_lresult = 0;
299                 error = linux_shmdt(&a);
300                 args->sysmsg_lresult = a.sysmsg_lresult;
301                 break;
302         }
303         case LINUX_SHMGET: {
304                 struct linux_shmget_args a;
305
306                 a.key = args->arg1;
307                 a.size = args->arg2;
308                 a.shmflg = args->arg3;
309                 a.sysmsg_lresult = 0;
310                 error = linux_shmget(&a);
311                 args->sysmsg_lresult = a.sysmsg_lresult;
312                 break;
313         }
314         case LINUX_SHMCTL: {
315                 struct linux_shmctl_args a;
316
317                 a.shmid = args->arg1;
318                 a.cmd = args->arg2;
319                 a.buf = args->ptr;
320                 a.sysmsg_lresult = 0;
321                 error = linux_shmctl(&a);
322                 args->sysmsg_lresult = a.sysmsg_lresult;
323                 break;
324         }
325         default:
326                 error = EINVAL;
327                 break;
328         }
329         rel_mplock();
330         return(error);
331 }
332
333 /*
334  * MPSAFE
335  */
336 int
337 sys_linux_old_select(struct linux_old_select_args *args)
338 {
339         struct l_old_select_argv linux_args;
340         struct linux_select_args newsel;
341         int error;
342
343 #ifdef DEBUG
344         if (ldebug(old_select))
345                 kprintf(ARGS(old_select, "%p"), args->ptr);
346 #endif
347
348         error = copyin((caddr_t)args->ptr, &linux_args, sizeof(linux_args));
349         if (error)
350                 return (error);
351
352         newsel.sysmsg_iresult = 0;
353         newsel.nfds = linux_args.nfds;
354         newsel.readfds = linux_args.readfds;
355         newsel.writefds = linux_args.writefds;
356         newsel.exceptfds = linux_args.exceptfds;
357         newsel.timeout = linux_args.timeout;
358         error = sys_linux_select(&newsel);
359         args->sysmsg_iresult = newsel.sysmsg_iresult;
360         return(error);
361 }
362
363 /*
364  * MPSAFE
365  */
366 int
367 sys_linux_fork(struct linux_fork_args *args)
368 {
369         struct lwp *lp = curthread->td_lwp;
370         struct proc *p2;
371         int error;
372
373         get_mplock();
374         error = fork1(lp, RFFDG | RFPROC | RFPGLOCK, &p2);
375         if (error == 0) {
376                 emuldata_init(curproc, p2, 0);
377
378                 start_forked_proc(lp, p2);
379                 args->sysmsg_fds[0] = p2->p_pid;
380                 args->sysmsg_fds[1] = 0;
381         }
382         rel_mplock();
383
384         /* Are we the child? */
385         if (args->sysmsg_iresult == 1)
386                 args->sysmsg_iresult = 0;
387
388         return (error);
389 }
390
391 /*
392  * MPALMOSTSAFE
393  */
394 int
395 sys_linux_exit_group(struct linux_exit_group_args *args)
396 {
397         struct linux_emuldata *em, *e;
398         int rval;
399
400         rval = args->rval;
401
402         get_mplock();
403
404         EMUL_LOCK();
405
406         em = emuldata_get(curproc);
407
408         if (em->s->refs == 1) {
409                 exit1(W_EXITCODE(rval, 0));
410                 /* notreached */
411
412                 EMUL_UNLOCK();
413
414                 rel_mplock();
415                 return (0);
416         }
417         KKASSERT(em->proc == curproc);
418         em->flags |= EMUL_DIDKILL;
419         em->s->flags |= LINUX_LES_INEXITGROUP;
420         em->s->xstat = W_EXITCODE(rval, 0);
421
422         LIST_REMOVE(em, threads);
423         LIST_INSERT_HEAD(&em->s->threads, em, threads);
424         
425         while ((e = LIST_NEXT(em, threads)) != NULL) {
426                 LIST_REMOVE(em, threads);
427                 LIST_INSERT_AFTER(e, em, threads);
428                 if ((e->flags & EMUL_DIDKILL) == 0) {
429                         e->flags |= EMUL_DIDKILL;
430                         KKASSERT(pfind(e->proc->p_pid) == e->proc);
431                         ksignal(e->proc, SIGKILL);
432                 }
433         }
434
435
436         EMUL_UNLOCK();
437
438         exit1(W_EXITCODE(rval, 0));
439         rel_mplock();
440         /* notreached */
441
442         return (0);
443 }
444
445 /*
446  * MPSAFE
447  */
448 int
449 sys_linux_vfork(struct linux_vfork_args *args)
450 {
451         struct lwp *lp = curthread->td_lwp;
452         struct proc *p2;
453         int error;
454
455         get_mplock();
456         error = fork1(lp, RFFDG | RFPROC | RFPPWAIT | RFMEM | RFPGLOCK, &p2);
457         if (error == 0) {
458                 emuldata_init(curproc, p2, 0);
459
460                 start_forked_proc(lp, p2);
461                 args->sysmsg_fds[0] = p2->p_pid;
462                 args->sysmsg_fds[1] = 0;
463         }
464         rel_mplock();
465
466         if (args->sysmsg_iresult == 1)
467                 args->sysmsg_iresult = 0;
468
469         return (error);
470 }
471
472 /*
473  * MPALMOSTSAFE
474  */
475 int
476 sys_linux_clone(struct linux_clone_args *args)
477 {
478         struct segment_descriptor *desc;
479         struct l_user_desc info;
480         int idx;
481         int a[2];
482
483         struct lwp *lp = curthread->td_lwp;
484         int error, ff = RFPROC;
485         struct proc *p2 = NULL;
486         int exit_signal;
487         vm_offset_t start;
488
489         exit_signal = args->flags & 0x000000ff;
490         if (exit_signal >= LINUX_NSIG)
491                 return (EINVAL);
492         if (exit_signal <= LINUX_SIGTBLSZ)
493                 exit_signal = linux_to_bsd_signal[_SIG_IDX(exit_signal)];
494
495         if (args->flags & LINUX_CLONE_VM)
496                 ff |= RFMEM;
497         if (args->flags & LINUX_CLONE_SIGHAND)
498                 ff |= RFSIGSHARE;
499         if (!(args->flags & (LINUX_CLONE_FILES | LINUX_CLONE_FS)))
500                 ff |= RFFDG;
501         if ((args->flags & 0xffffff00) == LINUX_THREADING_FLAGS)
502                 ff |= RFTHREAD;
503         if (args->flags & LINUX_CLONE_VFORK)
504                 ff |= RFPPWAIT;
505         if (args->flags & LINUX_CLONE_PARENT_SETTID) {
506                 if (args->parent_tidptr == NULL)
507                         return (EINVAL);
508         }
509
510         error = 0;
511         start = 0;
512
513         get_mplock();
514         error = fork1(lp, ff | RFPGLOCK, &p2);
515         if (error) {
516                 rel_mplock();
517                 return error;
518         }
519
520         args->sysmsg_fds[0] = p2 ? p2->p_pid : 0;
521         args->sysmsg_fds[1] = 0;
522         
523         if (args->flags & (LINUX_CLONE_PARENT | LINUX_CLONE_THREAD))
524                 proc_reparent(p2, curproc->p_pptr /* XXX */);
525
526         emuldata_init(curproc, p2, args->flags);
527         linux_proc_fork(p2, curproc, args->child_tidptr);
528         /*
529          * XXX: this can't happen, p2 is never NULL, or else we'd have
530          *      other problems, too (see p2->p_sigparent == ...,
531          *      linux_proc_fork and emuldata_init.
532          */
533         if (p2 == NULL) {
534                 error = ESRCH;
535         } else {
536                 if (args->flags & LINUX_CLONE_PARENT_SETTID) {
537                         error = copyout(&p2->p_pid, args->parent_tidptr, sizeof(p2->p_pid));
538                 }
539         }
540
541         p2->p_sigparent = exit_signal;
542         if (args->stack) {
543                 ONLY_LWP_IN_PROC(p2)->lwp_md.md_regs->tf_esp =
544                                         (unsigned long)args->stack;
545         }
546
547         if (args->flags & LINUX_CLONE_SETTLS) {
548                 error = copyin((void *)curthread->td_lwp->lwp_md.md_regs->tf_esi, &info, sizeof(struct l_user_desc));
549                 if (error) {
550                         kprintf("copyin of tf_esi to info failed\n");
551                 } else {
552                         idx = info.entry_number;
553                         /*
554                          * We understand both our own entries such as the ones
555                          * we provide on linux_set_thread_area, as well as the
556                          * linux-type entries 6-8.
557                          */
558                         if ((idx < 6 || idx > 8) && (idx < GTLS_START)) {
559                                 kprintf("LINUX_CLONE_SETTLS, invalid idx requested: %d\n", idx);
560                                 goto out;
561                         }
562                         if (idx < GTLS_START) {
563                                 idx -= 6;
564                         } else {
565 #if 0 /* was SMP */
566                                 idx -= (GTLS_START + mycpu->gd_cpuid * NGDT);
567 #endif
568                                 idx -= GTLS_START;
569                         }
570                         KKASSERT(idx >= 0);
571
572                         a[0] = LINUX_LDT_entry_a(&info);
573                         a[1] = LINUX_LDT_entry_b(&info);
574                         if (p2) {
575                                 desc = &FIRST_LWP_IN_PROC(p2)->lwp_thread->td_tls.tls[idx];
576                                 memcpy(desc, &a, sizeof(a));
577                         } else {
578                                 kprintf("linux_clone... we don't have a p2\n");
579                         }
580                 }
581         }
582 out:
583         if (p2)
584                 start_forked_proc(lp, p2);
585
586         rel_mplock();
587 #ifdef DEBUG
588         if (ldebug(clone))
589                 kprintf(LMSG("clone: successful rfork to %ld"),
590                     (long)p2->p_pid);
591 #endif
592
593         return (error);
594 }
595
596 /* XXX move */
597 struct l_mmap_argv {
598         l_caddr_t       addr;
599         l_int           len;
600         l_int           prot;
601         l_int           flags;
602         l_int           fd;
603         l_int           pos;
604 };
605
606 #define STACK_SIZE  (2 * 1024 * 1024)
607 #define GUARD_SIZE  (4 * PAGE_SIZE)
608
609 /*
610  * MPALMOSTSAFE
611  */
612 static int
613 linux_mmap_common(caddr_t linux_addr, size_t linux_len, int linux_prot,
614                   int linux_flags, int linux_fd, off_t pos, void **res)
615 {
616         struct thread *td = curthread;
617         struct proc *p = td->td_proc;
618         caddr_t addr;
619         void *new;
620         int error, flags, len, prot, fd;
621
622         flags = 0;
623         if (linux_flags & LINUX_MAP_SHARED)
624                 flags |= MAP_SHARED;
625         if (linux_flags & LINUX_MAP_PRIVATE)
626                 flags |= MAP_PRIVATE;
627         if (linux_flags & LINUX_MAP_FIXED)
628                 flags |= MAP_FIXED;
629         if (linux_flags & LINUX_MAP_ANON) {
630                 flags |= MAP_ANON;
631         } else {
632                 flags |= MAP_NOSYNC;
633         }
634
635         get_mplock();
636
637         if (linux_flags & LINUX_MAP_GROWSDOWN) {
638                 flags |= MAP_STACK;
639                 /* The linux MAP_GROWSDOWN option does not limit auto
640                  * growth of the region.  Linux mmap with this option
641                  * takes as addr the inital BOS, and as len, the initial
642                  * region size.  It can then grow down from addr without
643                  * limit.  However, linux threads has an implicit internal
644                  * limit to stack size of STACK_SIZE.  Its just not
645                  * enforced explicitly in linux.  But, here we impose
646                  * a limit of (STACK_SIZE - GUARD_SIZE) on the stack
647                  * region, since we can do this with our mmap.
648                  *
649                  * Our mmap with MAP_STACK takes addr as the maximum
650                  * downsize limit on BOS, and as len the max size of
651                  * the region.  It them maps the top SGROWSIZ bytes,
652                  * and autgrows the region down, up to the limit
653                  * in addr.
654                  *
655                  * If we don't use the MAP_STACK option, the effect
656                  * of this code is to allocate a stack region of a
657                  * fixed size of (STACK_SIZE - GUARD_SIZE).
658                  */
659
660                 /* This gives us TOS */
661                 addr = linux_addr + linux_len;
662
663                 if (addr > p->p_vmspace->vm_maxsaddr) {
664                         /* Some linux apps will attempt to mmap
665                          * thread stacks near the top of their
666                          * address space.  If their TOS is greater
667                          * than vm_maxsaddr, vm_map_growstack()
668                          * will confuse the thread stack with the
669                          * process stack and deliver a SEGV if they
670                          * attempt to grow the thread stack past their
671                          * current stacksize rlimit.  To avoid this,
672                          * adjust vm_maxsaddr upwards to reflect
673                          * the current stacksize rlimit rather
674                          * than the maximum possible stacksize.
675                          * It would be better to adjust the
676                          * mmap'ed region, but some apps do not check
677                          * mmap's return value.
678                          */
679                         p->p_vmspace->vm_maxsaddr = (char *)USRSTACK -
680                             p->p_rlimit[RLIMIT_STACK].rlim_cur;
681                 }
682
683                 /* This gives us our maximum stack size */
684                 if (linux_len > STACK_SIZE - GUARD_SIZE) {
685                         len = linux_len;
686                 } else {
687                         len = STACK_SIZE - GUARD_SIZE;
688                 }
689                 /* This gives us a new BOS.  If we're using VM_STACK, then
690                  * mmap will just map the top SGROWSIZ bytes, and let
691                  * the stack grow down to the limit at BOS.  If we're
692                  * not using VM_STACK we map the full stack, since we
693                  * don't have a way to autogrow it.
694                  */
695                 addr -= len;
696         } else {
697                 addr = linux_addr;
698                 len = linux_len;
699         }
700
701         prot = linux_prot;
702
703         if (prot & (PROT_READ | PROT_WRITE | PROT_EXEC))
704                 prot |= PROT_READ | PROT_EXEC;
705
706         if (linux_flags & LINUX_MAP_ANON) {
707                 fd = -1;
708         } else {
709                 fd = linux_fd;
710         }
711         
712 #ifdef DEBUG
713         if (ldebug(mmap) || ldebug(mmap2))
714                 kprintf("-> (%p, %d, %d, 0x%08x, %d, %lld)\n",
715                     addr, len, prot, flags, fd, pos);
716 #endif
717         error = kern_mmap(curproc->p_vmspace, addr, len,
718                           prot, flags, fd, pos, &new);
719         rel_mplock();
720
721         if (error == 0)
722                 *res = new;
723         return (error);
724 }
725
726 /*
727  * MPSAFE
728  */
729 int
730 sys_linux_mmap(struct linux_mmap_args *args)
731 {
732         struct l_mmap_argv linux_args;
733         int error;
734
735         error = copyin((caddr_t)args->ptr, &linux_args, sizeof(linux_args));
736         if (error)
737                 return (error);
738
739 #ifdef DEBUG
740         if (ldebug(mmap))
741                 kprintf(ARGS(mmap, "%p, %d, %d, 0x%08x, %d, %d"),
742                     (void *)linux_args.addr, linux_args.len, linux_args.prot,
743                     linux_args.flags, linux_args.fd, linux_args.pos);
744 #endif
745         error = linux_mmap_common(linux_args.addr, linux_args.len,
746             linux_args.prot, linux_args.flags, linux_args.fd,
747             linux_args.pos, &args->sysmsg_resultp);
748 #ifdef DEBUG
749         if (ldebug(mmap))
750                 kprintf("-> %p\n", args->sysmsg_resultp);
751 #endif
752         return(error);
753 }
754
755 /*
756  * MPSAFE
757  */
758 int
759 sys_linux_mmap2(struct linux_mmap2_args *args)
760 {
761         int error;
762
763 #ifdef DEBUG
764         if (ldebug(mmap2))
765                 kprintf(ARGS(mmap2, "%p, %d, %d, 0x%08x, %d, %d"),
766                     (void *)args->addr, args->len, args->prot, args->flags,
767                     args->fd, args->pgoff);
768 #endif
769         error = linux_mmap_common((void *)args->addr, args->len, args->prot,
770             args->flags, args->fd, args->pgoff * PAGE_SIZE,
771             &args->sysmsg_resultp);
772 #ifdef DEBUG
773         if (ldebug(mmap2))
774                 kprintf("-> %p\n", args->sysmsg_resultp);
775 #endif
776         return (error);
777 }
778
779 /*
780  * MPSAFE
781  */
782 int
783 sys_linux_pipe(struct linux_pipe_args *args)
784 {
785         int error;
786         int reg_edx;
787         struct pipe_args bsd_args;
788
789 #ifdef DEBUG
790         if (ldebug(pipe))
791                 kprintf(ARGS(pipe, "*"));
792 #endif
793
794         reg_edx = args->sysmsg_fds[1];
795         error = sys_pipe(&bsd_args);
796         if (error) {
797                 args->sysmsg_fds[1] = reg_edx;
798                 return (error);
799         }
800
801         error = copyout(bsd_args.sysmsg_fds, args->pipefds, 2*sizeof(int));
802         if (error) {
803                 args->sysmsg_fds[1] = reg_edx;
804                 return (error);
805         }
806
807         args->sysmsg_fds[1] = reg_edx;
808         args->sysmsg_fds[0] = 0;
809         return (0);
810 }
811
812 /*
813  * XXX: Preliminary
814  */
815 int
816 sys_linux_pipe2(struct linux_pipe2_args *args)
817 {
818         struct thread *td = curthread;
819         int error;
820         int reg_edx;
821         struct pipe_args bsd_args;
822         union fcntl_dat dat;
823
824         reg_edx = args->sysmsg_fds[1];
825         error = sys_pipe(&bsd_args);
826         if (error) {
827                 args->sysmsg_fds[1] = reg_edx;
828                 return (error);
829         }
830
831 //      if (args->flags & LINUX_O_CLOEXEC) {
832 //      }
833
834         if (args->flags & LINUX_O_NONBLOCK) {
835                 dat.fc_flags = O_NONBLOCK;
836                 kern_fcntl(bsd_args.sysmsg_fds[0], F_SETFL, &dat, td->td_ucred);
837                 kern_fcntl(bsd_args.sysmsg_fds[1], F_SETFL, &dat, td->td_ucred);
838         }
839
840         error = copyout(bsd_args.sysmsg_fds, args->pipefds, 2*sizeof(int));
841         if (error) {
842                 args->sysmsg_fds[1] = reg_edx;
843                 return (error);
844         }
845
846         args->sysmsg_fds[1] = reg_edx;
847         args->sysmsg_fds[0] = 0;
848         return (0);
849 }
850
851 /*
852  * MPSAFE
853  */
854 int
855 sys_linux_ioperm(struct linux_ioperm_args *args)
856 {
857         struct sysarch_args sa;
858         struct i386_ioperm_args *iia;
859         caddr_t sg;
860         int error;
861
862         sg = stackgap_init();
863         iia = stackgap_alloc(&sg, sizeof(struct i386_ioperm_args));
864         iia->start = args->start;
865         iia->length = args->length;
866         iia->enable = args->enable;
867         sa.sysmsg_resultp = NULL;
868         sa.op = I386_SET_IOPERM;
869         sa.parms = (char *)iia;
870         error = sys_sysarch(&sa);
871         args->sysmsg_resultp = sa.sysmsg_resultp;
872         return(error);
873 }
874
875 /*
876  * MPSAFE
877  */
878 int
879 sys_linux_iopl(struct linux_iopl_args *args)
880 {
881         struct thread *td = curthread;
882         struct lwp *lp = td->td_lwp;
883         int error;
884
885         if (args->level < 0 || args->level > 3)
886                 return (EINVAL);
887         if ((error = priv_check(td, PRIV_ROOT)) != 0)
888                 return (error);
889         if (securelevel > 0)
890                 return (EPERM);
891         lp->lwp_md.md_regs->tf_eflags =
892             (lp->lwp_md.md_regs->tf_eflags & ~PSL_IOPL) |
893             (args->level * (PSL_IOPL / 3));
894         return (0);
895 }
896
897 /*
898  * MPSAFE
899  */
900 int
901 sys_linux_modify_ldt(struct linux_modify_ldt_args *uap)
902 {
903         int error;
904         caddr_t sg;
905         struct sysarch_args args;
906         struct i386_ldt_args *ldt;
907         struct l_descriptor ld;
908         union descriptor *desc;
909         int size, written;
910
911         sg = stackgap_init();
912
913         if (uap->ptr == NULL)
914                 return (EINVAL);
915
916         switch (uap->func) {
917         case 0x00: /* read_ldt */
918                 ldt = stackgap_alloc(&sg, sizeof(*ldt));
919                 ldt->start = 0;
920                 ldt->descs = uap->ptr;
921                 ldt->num = uap->bytecount / sizeof(union descriptor);
922                 args.op = I386_GET_LDT;
923                 args.parms = (char*)ldt;
924                 args.sysmsg_iresult = 0;
925                 error = sys_sysarch(&args);
926                 uap->sysmsg_iresult = args.sysmsg_iresult *
927                                       sizeof(union descriptor);
928                 break;
929         case 0x02: /* read_default_ldt = 0 */
930                 size = 5*sizeof(struct l_desc_struct);
931                 if (size > uap->bytecount)
932                         size = uap->bytecount;
933                 for (written = error = 0; written < size && error == 0; written++)
934                         error = subyte((char *)uap->ptr + written, 0);
935                 uap->sysmsg_iresult = written;
936                 break;
937         case 0x01: /* write_ldt */
938         case 0x11: /* write_ldt */
939                 if (uap->bytecount != sizeof(ld))
940                         return (EINVAL);
941
942                 error = copyin(uap->ptr, &ld, sizeof(ld));
943                 if (error)
944                         return (error);
945
946                 ldt = stackgap_alloc(&sg, sizeof(*ldt));
947                 desc = stackgap_alloc(&sg, sizeof(*desc));
948                 ldt->start = ld.entry_number;
949                 ldt->descs = desc;
950                 ldt->num = 1;
951                 desc->sd.sd_lolimit = (ld.limit & 0x0000ffff);
952                 desc->sd.sd_hilimit = (ld.limit & 0x000f0000) >> 16;
953                 desc->sd.sd_lobase = (ld.base_addr & 0x00ffffff);
954                 desc->sd.sd_hibase = (ld.base_addr & 0xff000000) >> 24;
955                 desc->sd.sd_type = SDT_MEMRO | ((ld.read_exec_only ^ 1) << 1) |
956                         (ld.contents << 2);
957                 desc->sd.sd_dpl = 3;
958                 desc->sd.sd_p = (ld.seg_not_present ^ 1);
959                 desc->sd.sd_xx = 0;
960                 desc->sd.sd_def32 = ld.seg_32bit;
961                 desc->sd.sd_gran = ld.limit_in_pages;
962                 args.op = I386_SET_LDT;
963                 args.parms = (char*)ldt;
964                 args.sysmsg_iresult = 0;
965                 error = sys_sysarch(&args);
966                 uap->sysmsg_iresult = args.sysmsg_iresult;
967                 break;
968         default:
969                 error = EINVAL;
970                 break;
971         }
972
973         return (error);
974 }
975
976 /*
977  * MPALMOSTSAFE
978  */
979 int
980 sys_linux_sigaction(struct linux_sigaction_args *args)
981 {
982         l_osigaction_t osa;
983         l_sigaction_t linux_act, linux_oact;
984         struct sigaction act, oact;
985         int error, sig;
986
987 #ifdef DEBUG
988         if (ldebug(sigaction))
989                 kprintf(ARGS(sigaction, "%d, %p, %p"),
990                     args->sig, (void *)args->nsa, (void *)args->osa);
991 #endif
992
993         if (args->nsa) {
994                 error = copyin(args->nsa, &osa, sizeof(l_osigaction_t));
995                 if (error)
996                         return (error);
997                 linux_act.lsa_handler = osa.lsa_handler;
998                 linux_act.lsa_flags = osa.lsa_flags;
999                 linux_act.lsa_restorer = osa.lsa_restorer;
1000                 LINUX_SIGEMPTYSET(linux_act.lsa_mask);
1001                 linux_act.lsa_mask.__bits[0] = osa.lsa_mask;
1002                 linux_to_bsd_sigaction(&linux_act, &act);
1003         }
1004
1005         if (args->sig <= LINUX_SIGTBLSZ)
1006                 sig = linux_to_bsd_signal[_SIG_IDX(args->sig)];
1007         else
1008                 sig = args->sig;
1009
1010         get_mplock();
1011         error = kern_sigaction(sig, args->nsa ? &act : NULL,
1012                                args->osa ? &oact : NULL);
1013         rel_mplock();
1014
1015         if (args->osa != NULL && !error) {
1016                 bsd_to_linux_sigaction(&oact, &linux_oact);
1017                 osa.lsa_handler = linux_oact.lsa_handler;
1018                 osa.lsa_flags = linux_oact.lsa_flags;
1019                 osa.lsa_restorer = linux_oact.lsa_restorer;
1020                 osa.lsa_mask = linux_oact.lsa_mask.__bits[0];
1021                 error = copyout(&osa, args->osa, sizeof(l_osigaction_t));
1022         }
1023         return (error);
1024 }
1025
1026 /*
1027  * Linux has two extra args, restart and oldmask.  We dont use these,
1028  * but it seems that "restart" is actually a context pointer that
1029  * enables the signal to happen with a different register set.
1030  *
1031  * MPALMOSTSAFE
1032  */
1033 int
1034 sys_linux_sigsuspend(struct linux_sigsuspend_args *args)
1035 {
1036         l_sigset_t linux_mask;
1037         sigset_t mask;
1038         int error;
1039
1040 #ifdef DEBUG
1041         if (ldebug(sigsuspend))
1042                 kprintf(ARGS(sigsuspend, "%08lx"), (unsigned long)args->mask);
1043 #endif
1044
1045         LINUX_SIGEMPTYSET(mask);
1046         mask.__bits[0] = args->mask;
1047         linux_to_bsd_sigset(&linux_mask, &mask);
1048
1049         get_mplock();
1050         error = kern_sigsuspend(&mask);
1051         rel_mplock();
1052
1053         return(error);
1054 }
1055
1056 /*
1057  * MPALMOSTSAFE
1058  */
1059 int
1060 sys_linux_rt_sigsuspend(struct linux_rt_sigsuspend_args *uap)
1061 {
1062         l_sigset_t linux_mask;
1063         sigset_t mask;
1064         int error;
1065
1066 #ifdef DEBUG
1067         if (ldebug(rt_sigsuspend))
1068                 kprintf(ARGS(rt_sigsuspend, "%p, %d"),
1069                     (void *)uap->newset, uap->sigsetsize);
1070 #endif
1071
1072         if (uap->sigsetsize != sizeof(l_sigset_t))
1073                 return (EINVAL);
1074
1075         error = copyin(uap->newset, &linux_mask, sizeof(l_sigset_t));
1076         if (error)
1077                 return (error);
1078
1079         linux_to_bsd_sigset(&linux_mask, &mask);
1080
1081         get_mplock();
1082         error = kern_sigsuspend(&mask);
1083         rel_mplock();
1084
1085         return(error);
1086 }
1087
1088 /*
1089  * MPALMOSTSAFE
1090  */
1091 int
1092 sys_linux_pause(struct linux_pause_args *args)
1093 {
1094         struct thread *td = curthread;
1095         struct lwp *lp = td->td_lwp;
1096         sigset_t mask;
1097         int error;
1098
1099 #ifdef DEBUG
1100         if (ldebug(pause))
1101                 kprintf(ARGS(pause, ""));
1102 #endif
1103
1104         mask = lp->lwp_sigmask;
1105
1106         get_mplock();
1107         error = kern_sigsuspend(&mask);
1108         rel_mplock();
1109
1110         return(error);
1111 }
1112
1113 /*
1114  * MPALMOSTSAFE
1115  */
1116 int
1117 sys_linux_sigaltstack(struct linux_sigaltstack_args *uap)
1118 {
1119         stack_t ss, oss;
1120         l_stack_t linux_ss;
1121         int error;
1122
1123 #ifdef DEBUG
1124         if (ldebug(sigaltstack))
1125                 kprintf(ARGS(sigaltstack, "%p, %p"), uap->uss, uap->uoss);
1126 #endif
1127
1128         if (uap->uss) {
1129                 error = copyin(uap->uss, &linux_ss, sizeof(l_stack_t));
1130                 if (error)
1131                         return (error);
1132
1133                 ss.ss_sp = linux_ss.ss_sp;
1134                 ss.ss_size = linux_ss.ss_size;
1135                 ss.ss_flags = linux_to_bsd_sigaltstack(linux_ss.ss_flags);
1136         }
1137
1138         get_mplock();
1139         error = kern_sigaltstack(uap->uss ? &ss : NULL,
1140                                  uap->uoss ? &oss : NULL);
1141         rel_mplock();
1142
1143         if (error == 0 && uap->uoss) {
1144                 linux_ss.ss_sp = oss.ss_sp;
1145                 linux_ss.ss_size = oss.ss_size;
1146                 linux_ss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags);
1147                 error = copyout(&linux_ss, uap->uoss, sizeof(l_stack_t));
1148         }
1149
1150         return (error);
1151 }
1152
1153 int
1154 sys_linux_set_thread_area(struct linux_set_thread_area_args *args)
1155 {
1156         struct segment_descriptor *desc;
1157         struct l_user_desc info;
1158         int error;
1159         int idx;
1160         int a[2];
1161         int i;
1162
1163         error = copyin(args->desc, &info, sizeof(struct l_user_desc));
1164         if (error)
1165                 return (EFAULT);
1166
1167 #ifdef DEBUG
1168         if (ldebug(set_thread_area))
1169                 kprintf(ARGS(set_thread_area, "%i, %x, %x, %i, %i, %i, %i, %i, %i\n"),
1170                       info.entry_number,
1171                       info.base_addr,
1172                       info.limit,
1173                       info.seg_32bit,
1174                       info.contents,
1175                       info.read_exec_only,
1176                       info.limit_in_pages,
1177                       info.seg_not_present,
1178                       info.useable);
1179 #endif
1180
1181         idx = info.entry_number;
1182         if (idx != -1 && (idx < 6 || idx > 8))
1183                 return (EINVAL);
1184
1185         if (idx == -1) {
1186                 /* -1 means finding the first free TLS entry */
1187                 for (i = 0; i < NGTLS; i++) {
1188                         /*
1189                          * try to determine if the TLS entry is empty by looking
1190                          * at the lolimit entry.
1191                          */
1192                         if (curthread->td_tls.tls[idx].sd_lolimit == 0) {
1193                                 idx = i;
1194                                 break;
1195                         }
1196                 }
1197
1198                 if (idx == -1) {
1199                         /*
1200                          * By now we should have an index. If not, it means
1201                          * that no entry is free, so return ESRCH.
1202                          */
1203                         return (ESRCH);
1204                 }
1205         } else {
1206                 /* translate the index from Linux to ours */
1207                 idx -= 6;
1208                 KKASSERT(idx >= 0);
1209         }
1210
1211         /* Tell the caller about the allocated entry number */
1212 #if 0 /* was SMP */
1213         info.entry_number = GTLS_START + mycpu->gd_cpuid * NGDT + idx;
1214 #endif
1215         info.entry_number = GTLS_START + idx;
1216
1217
1218         error = copyout(&info, args->desc, sizeof(struct l_user_desc));
1219         if (error)
1220                 return (error);
1221
1222         if (LINUX_LDT_empty(&info)) {
1223                 a[0] = 0;
1224                 a[1] = 0;
1225         } else {
1226                 a[0] = LINUX_LDT_entry_a(&info);
1227                 a[1] = LINUX_LDT_entry_b(&info);
1228         }
1229
1230         /*
1231          * Update the TLS and the TLS entries in the GDT, but hold a critical
1232          * section as required by set_user_TLS().
1233          */
1234         crit_enter();
1235         desc = &curthread->td_tls.tls[idx];
1236         memcpy(desc, &a, sizeof(a));
1237         set_user_TLS();
1238         crit_exit();
1239
1240         return (0);
1241 }
1242
1243 int
1244 sys_linux_get_thread_area(struct linux_get_thread_area_args *args)
1245 {
1246         struct segment_descriptor *sd;
1247         struct l_desc_struct desc;
1248         struct l_user_desc info;
1249         int error;
1250         int idx;
1251
1252 #ifdef DEBUG
1253         if (ldebug(get_thread_area))
1254                 kprintf(ARGS(get_thread_area, "%p"), args->desc);
1255 #endif
1256
1257         error = copyin(args->desc, &info, sizeof(struct l_user_desc));
1258         if (error)
1259                 return (EFAULT);
1260                 
1261         idx = info.entry_number;
1262         if ((idx < 6 || idx > 8) && (idx < GTLS_START)) {
1263                 kprintf("sys_linux_get_thread_area, invalid idx requested: %d\n", idx);
1264                 return (EINVAL);
1265         }
1266
1267         memset(&info, 0, sizeof(info));
1268
1269         /* translate the index from Linux to ours */
1270         info.entry_number = idx;
1271         if (idx < GTLS_START) {
1272                 idx -= 6;
1273         } else {
1274 #if 0 /* was SMP */
1275                 idx -= (GTLS_START + mycpu->gd_cpuid * NGDT);
1276 #endif
1277                 idx -= GTLS_START;
1278
1279         }
1280         KKASSERT(idx >= 0);
1281
1282         sd = &curthread->td_tls.tls[idx];
1283         memcpy(&desc, sd, sizeof(desc));
1284         info.base_addr = LINUX_GET_BASE(&desc);
1285         info.limit = LINUX_GET_LIMIT(&desc);
1286         info.seg_32bit = LINUX_GET_32BIT(&desc);
1287         info.contents = LINUX_GET_CONTENTS(&desc);
1288         info.read_exec_only = !LINUX_GET_WRITABLE(&desc);
1289         info.limit_in_pages = LINUX_GET_LIMIT_PAGES(&desc);
1290         info.seg_not_present = !LINUX_GET_PRESENT(&desc);
1291         info.useable = LINUX_GET_USEABLE(&desc);
1292
1293         error = copyout(&info, args->desc, sizeof(struct l_user_desc));
1294         if (error)
1295                 return (EFAULT);
1296
1297         return (0);
1298 }