c0b0f2e8869f50f4c5b91495738b510e3c847981
[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         EMUL_LOCK();
402
403         em = emuldata_get(curproc);
404
405         if (em->s->refs == 1) {
406                 EMUL_UNLOCK();
407                 exit1(W_EXITCODE(rval, 0));
408                 /* NOTREACHED */
409                 return (0);
410         }
411         KKASSERT(em->proc == curproc);
412         em->flags |= EMUL_DIDKILL;
413         em->s->flags |= LINUX_LES_INEXITGROUP;
414         em->s->xstat = W_EXITCODE(rval, 0);
415
416         LIST_REMOVE(em, threads);
417         LIST_INSERT_HEAD(&em->s->threads, em, threads);
418
419         while ((e = LIST_NEXT(em, threads)) != NULL) {
420                 LIST_REMOVE(em, threads);
421                 LIST_INSERT_AFTER(e, em, threads);
422                 if ((e->flags & EMUL_DIDKILL) == 0) {
423                         e->flags |= EMUL_DIDKILL;
424                         lwkt_gettoken(&proc_token);
425                         KKASSERT(pfindn(e->proc->p_pid) == e->proc);
426                         ksignal(e->proc, SIGKILL);
427                         lwkt_reltoken(&proc_token);
428                 }
429         }
430
431         EMUL_UNLOCK();
432         exit1(W_EXITCODE(rval, 0));
433         /* NOTREACHED */
434
435         return (0);
436 }
437
438 /*
439  * MPSAFE
440  */
441 int
442 sys_linux_vfork(struct linux_vfork_args *args)
443 {
444         struct lwp *lp = curthread->td_lwp;
445         struct proc *p2;
446         int error;
447
448         get_mplock();
449         error = fork1(lp, RFFDG | RFPROC | RFPPWAIT | RFMEM | RFPGLOCK, &p2);
450         if (error == 0) {
451                 emuldata_init(curproc, p2, 0);
452
453                 start_forked_proc(lp, p2);
454                 args->sysmsg_fds[0] = p2->p_pid;
455                 args->sysmsg_fds[1] = 0;
456         }
457         rel_mplock();
458
459         if (args->sysmsg_iresult == 1)
460                 args->sysmsg_iresult = 0;
461
462         return (error);
463 }
464
465 /*
466  * MPALMOSTSAFE
467  */
468 int
469 sys_linux_clone(struct linux_clone_args *args)
470 {
471         struct segment_descriptor *desc;
472         struct l_user_desc info;
473         int idx;
474         int a[2];
475
476         struct lwp *lp = curthread->td_lwp;
477         int error, ff = RFPROC;
478         struct proc *p2 = NULL;
479         int exit_signal;
480         vm_offset_t start;
481
482         exit_signal = args->flags & 0x000000ff;
483         if (exit_signal >= LINUX_NSIG)
484                 return (EINVAL);
485         if (exit_signal <= LINUX_SIGTBLSZ)
486                 exit_signal = linux_to_bsd_signal[_SIG_IDX(exit_signal)];
487
488         if (args->flags & LINUX_CLONE_VM)
489                 ff |= RFMEM;
490         if (args->flags & LINUX_CLONE_SIGHAND)
491                 ff |= RFSIGSHARE;
492         if (!(args->flags & (LINUX_CLONE_FILES | LINUX_CLONE_FS)))
493                 ff |= RFFDG;
494         if ((args->flags & 0xffffff00) == LINUX_THREADING_FLAGS)
495                 ff |= RFTHREAD;
496         if (args->flags & LINUX_CLONE_VFORK)
497                 ff |= RFPPWAIT;
498         if (args->flags & LINUX_CLONE_PARENT_SETTID) {
499                 if (args->parent_tidptr == NULL)
500                         return (EINVAL);
501         }
502
503         error = 0;
504         start = 0;
505
506         get_mplock();
507         error = fork1(lp, ff | RFPGLOCK, &p2);
508         if (error) {
509                 rel_mplock();
510                 return error;
511         }
512
513         args->sysmsg_fds[0] = p2 ? p2->p_pid : 0;
514         args->sysmsg_fds[1] = 0;
515         
516         if (args->flags & (LINUX_CLONE_PARENT | LINUX_CLONE_THREAD))
517                 proc_reparent(p2, curproc->p_pptr /* XXX */);
518
519         emuldata_init(curproc, p2, args->flags);
520         linux_proc_fork(p2, curproc, args->child_tidptr);
521         /*
522          * XXX: this can't happen, p2 is never NULL, or else we'd have
523          *      other problems, too (see p2->p_sigparent == ...,
524          *      linux_proc_fork and emuldata_init.
525          */
526         if (p2 == NULL) {
527                 error = ESRCH;
528         } else {
529                 if (args->flags & LINUX_CLONE_PARENT_SETTID) {
530                         error = copyout(&p2->p_pid, args->parent_tidptr, sizeof(p2->p_pid));
531                 }
532         }
533
534         p2->p_sigparent = exit_signal;
535         if (args->stack) {
536                 ONLY_LWP_IN_PROC(p2)->lwp_md.md_regs->tf_esp =
537                                         (unsigned long)args->stack;
538         }
539
540         if (args->flags & LINUX_CLONE_SETTLS) {
541                 error = copyin((void *)curthread->td_lwp->lwp_md.md_regs->tf_esi, &info, sizeof(struct l_user_desc));
542                 if (error) {
543                         kprintf("copyin of tf_esi to info failed\n");
544                 } else {
545                         idx = info.entry_number;
546                         /*
547                          * We understand both our own entries such as the ones
548                          * we provide on linux_set_thread_area, as well as the
549                          * linux-type entries 6-8.
550                          */
551                         if ((idx < 6 || idx > 8) && (idx < GTLS_START)) {
552                                 kprintf("LINUX_CLONE_SETTLS, invalid idx requested: %d\n", idx);
553                                 goto out;
554                         }
555                         if (idx < GTLS_START) {
556                                 idx -= 6;
557                         } else {
558 #if 0 /* was SMP */
559                                 idx -= (GTLS_START + mycpu->gd_cpuid * NGDT);
560 #endif
561                                 idx -= GTLS_START;
562                         }
563                         KKASSERT(idx >= 0);
564
565                         a[0] = LINUX_LDT_entry_a(&info);
566                         a[1] = LINUX_LDT_entry_b(&info);
567                         if (p2) {
568                                 desc = &FIRST_LWP_IN_PROC(p2)->lwp_thread->td_tls.tls[idx];
569                                 memcpy(desc, &a, sizeof(a));
570                         } else {
571                                 kprintf("linux_clone... we don't have a p2\n");
572                         }
573                 }
574         }
575 out:
576         if (p2)
577                 start_forked_proc(lp, p2);
578
579         rel_mplock();
580 #ifdef DEBUG
581         if (ldebug(clone))
582                 kprintf(LMSG("clone: successful rfork to %ld"),
583                     (long)p2->p_pid);
584 #endif
585
586         return (error);
587 }
588
589 /* XXX move */
590 struct l_mmap_argv {
591         l_caddr_t       addr;
592         l_int           len;
593         l_int           prot;
594         l_int           flags;
595         l_int           fd;
596         l_int           pos;
597 };
598
599 #define STACK_SIZE  (2 * 1024 * 1024)
600 #define GUARD_SIZE  (4 * PAGE_SIZE)
601
602 /*
603  * MPALMOSTSAFE
604  */
605 static int
606 linux_mmap_common(caddr_t linux_addr, size_t linux_len, int linux_prot,
607                   int linux_flags, int linux_fd, off_t pos, void **res)
608 {
609         struct thread *td = curthread;
610         struct proc *p = td->td_proc;
611         caddr_t addr;
612         void *new;
613         int error, flags, len, prot, fd;
614
615         flags = 0;
616         if (linux_flags & LINUX_MAP_SHARED)
617                 flags |= MAP_SHARED;
618         if (linux_flags & LINUX_MAP_PRIVATE)
619                 flags |= MAP_PRIVATE;
620         if (linux_flags & LINUX_MAP_FIXED)
621                 flags |= MAP_FIXED;
622         if (linux_flags & LINUX_MAP_ANON) {
623                 flags |= MAP_ANON;
624         } else {
625                 flags |= MAP_NOSYNC;
626         }
627
628         lwkt_gettoken(&curproc->p_vmspace->vm_map.token);
629
630         if (linux_flags & LINUX_MAP_GROWSDOWN) {
631                 flags |= MAP_STACK;
632                 /* The linux MAP_GROWSDOWN option does not limit auto
633                  * growth of the region.  Linux mmap with this option
634                  * takes as addr the inital BOS, and as len, the initial
635                  * region size.  It can then grow down from addr without
636                  * limit.  However, linux threads has an implicit internal
637                  * limit to stack size of STACK_SIZE.  Its just not
638                  * enforced explicitly in linux.  But, here we impose
639                  * a limit of (STACK_SIZE - GUARD_SIZE) on the stack
640                  * region, since we can do this with our mmap.
641                  *
642                  * Our mmap with MAP_STACK takes addr as the maximum
643                  * downsize limit on BOS, and as len the max size of
644                  * the region.  It them maps the top SGROWSIZ bytes,
645                  * and autgrows the region down, up to the limit
646                  * in addr.
647                  *
648                  * If we don't use the MAP_STACK option, the effect
649                  * of this code is to allocate a stack region of a
650                  * fixed size of (STACK_SIZE - GUARD_SIZE).
651                  */
652
653                 /* This gives us TOS */
654                 addr = linux_addr + linux_len;
655
656                 if (addr > p->p_vmspace->vm_maxsaddr) {
657                         /* Some linux apps will attempt to mmap
658                          * thread stacks near the top of their
659                          * address space.  If their TOS is greater
660                          * than vm_maxsaddr, vm_map_growstack()
661                          * will confuse the thread stack with the
662                          * process stack and deliver a SEGV if they
663                          * attempt to grow the thread stack past their
664                          * current stacksize rlimit.  To avoid this,
665                          * adjust vm_maxsaddr upwards to reflect
666                          * the current stacksize rlimit rather
667                          * than the maximum possible stacksize.
668                          * It would be better to adjust the
669                          * mmap'ed region, but some apps do not check
670                          * mmap's return value.
671                          */
672                         p->p_vmspace->vm_maxsaddr = (char *)USRSTACK -
673                             p->p_rlimit[RLIMIT_STACK].rlim_cur;
674                 }
675
676                 /* This gives us our maximum stack size */
677                 if (linux_len > STACK_SIZE - GUARD_SIZE) {
678                         len = linux_len;
679                 } else {
680                         len = STACK_SIZE - GUARD_SIZE;
681                 }
682                 /* This gives us a new BOS.  If we're using VM_STACK, then
683                  * mmap will just map the top SGROWSIZ bytes, and let
684                  * the stack grow down to the limit at BOS.  If we're
685                  * not using VM_STACK we map the full stack, since we
686                  * don't have a way to autogrow it.
687                  */
688                 addr -= len;
689         } else {
690                 addr = linux_addr;
691                 len = linux_len;
692         }
693
694         prot = linux_prot;
695
696         if (prot & (PROT_READ | PROT_WRITE | PROT_EXEC))
697                 prot |= PROT_READ | PROT_EXEC;
698
699         if (linux_flags & LINUX_MAP_ANON) {
700                 fd = -1;
701         } else {
702                 fd = linux_fd;
703         }
704         
705 #ifdef DEBUG
706         if (ldebug(mmap) || ldebug(mmap2))
707                 kprintf("-> (%p, %d, %d, 0x%08x, %d, %lld)\n",
708                     addr, len, prot, flags, fd, pos);
709 #endif
710         error = kern_mmap(curproc->p_vmspace, addr, len,
711                           prot, flags, fd, pos, &new);
712
713         lwkt_reltoken(&curproc->p_vmspace->vm_map.token);
714
715         if (error == 0)
716                 *res = new;
717         return (error);
718 }
719
720 /*
721  * MPSAFE
722  */
723 int
724 sys_linux_mmap(struct linux_mmap_args *args)
725 {
726         struct l_mmap_argv linux_args;
727         int error;
728
729         error = copyin((caddr_t)args->ptr, &linux_args, sizeof(linux_args));
730         if (error)
731                 return (error);
732
733 #ifdef DEBUG
734         if (ldebug(mmap))
735                 kprintf(ARGS(mmap, "%p, %d, %d, 0x%08x, %d, %d"),
736                     (void *)linux_args.addr, linux_args.len, linux_args.prot,
737                     linux_args.flags, linux_args.fd, linux_args.pos);
738 #endif
739         error = linux_mmap_common(linux_args.addr, linux_args.len,
740             linux_args.prot, linux_args.flags, linux_args.fd,
741             linux_args.pos, &args->sysmsg_resultp);
742 #ifdef DEBUG
743         if (ldebug(mmap))
744                 kprintf("-> %p\n", args->sysmsg_resultp);
745 #endif
746         return(error);
747 }
748
749 /*
750  * MPSAFE
751  */
752 int
753 sys_linux_mmap2(struct linux_mmap2_args *args)
754 {
755         int error;
756
757 #ifdef DEBUG
758         if (ldebug(mmap2))
759                 kprintf(ARGS(mmap2, "%p, %d, %d, 0x%08x, %d, %d"),
760                     (void *)args->addr, args->len, args->prot, args->flags,
761                     args->fd, args->pgoff);
762 #endif
763         error = linux_mmap_common((void *)args->addr, args->len, args->prot,
764             args->flags, args->fd, args->pgoff * PAGE_SIZE,
765             &args->sysmsg_resultp);
766 #ifdef DEBUG
767         if (ldebug(mmap2))
768                 kprintf("-> %p\n", args->sysmsg_resultp);
769 #endif
770         return (error);
771 }
772
773 /*
774  * MPSAFE
775  */
776 int
777 sys_linux_pipe(struct linux_pipe_args *args)
778 {
779         int error;
780         int reg_edx;
781         struct pipe_args bsd_args;
782
783 #ifdef DEBUG
784         if (ldebug(pipe))
785                 kprintf(ARGS(pipe, "*"));
786 #endif
787
788         reg_edx = args->sysmsg_fds[1];
789         error = sys_pipe(&bsd_args);
790         if (error) {
791                 args->sysmsg_fds[1] = reg_edx;
792                 return (error);
793         }
794
795         error = copyout(bsd_args.sysmsg_fds, args->pipefds, 2*sizeof(int));
796         if (error) {
797                 args->sysmsg_fds[1] = reg_edx;
798                 return (error);
799         }
800
801         args->sysmsg_fds[1] = reg_edx;
802         args->sysmsg_fds[0] = 0;
803         return (0);
804 }
805
806 /*
807  * XXX: Preliminary
808  */
809 int
810 sys_linux_pipe2(struct linux_pipe2_args *args)
811 {
812         struct thread *td = curthread;
813         int error;
814         int reg_edx;
815         struct pipe_args bsd_args;
816         union fcntl_dat dat;
817
818         reg_edx = args->sysmsg_fds[1];
819         error = sys_pipe(&bsd_args);
820         if (error) {
821                 args->sysmsg_fds[1] = reg_edx;
822                 return (error);
823         }
824
825 //      if (args->flags & LINUX_O_CLOEXEC) {
826 //      }
827
828         if (args->flags & LINUX_O_NONBLOCK) {
829                 dat.fc_flags = O_NONBLOCK;
830                 kern_fcntl(bsd_args.sysmsg_fds[0], F_SETFL, &dat, td->td_ucred);
831                 kern_fcntl(bsd_args.sysmsg_fds[1], F_SETFL, &dat, td->td_ucred);
832         }
833
834         error = copyout(bsd_args.sysmsg_fds, args->pipefds, 2*sizeof(int));
835         if (error) {
836                 args->sysmsg_fds[1] = reg_edx;
837                 return (error);
838         }
839
840         args->sysmsg_fds[1] = reg_edx;
841         args->sysmsg_fds[0] = 0;
842         return (0);
843 }
844
845 /*
846  * MPSAFE
847  */
848 int
849 sys_linux_ioperm(struct linux_ioperm_args *args)
850 {
851         struct sysarch_args sa;
852         struct i386_ioperm_args *iia;
853         caddr_t sg;
854         int error;
855
856         sg = stackgap_init();
857         iia = stackgap_alloc(&sg, sizeof(struct i386_ioperm_args));
858         iia->start = args->start;
859         iia->length = args->length;
860         iia->enable = args->enable;
861         sa.sysmsg_resultp = NULL;
862         sa.op = I386_SET_IOPERM;
863         sa.parms = (char *)iia;
864         error = sys_sysarch(&sa);
865         args->sysmsg_resultp = sa.sysmsg_resultp;
866         return(error);
867 }
868
869 /*
870  * MPSAFE
871  */
872 int
873 sys_linux_iopl(struct linux_iopl_args *args)
874 {
875         struct thread *td = curthread;
876         struct lwp *lp = td->td_lwp;
877         int error;
878
879         if (args->level < 0 || args->level > 3)
880                 return (EINVAL);
881         if ((error = priv_check(td, PRIV_ROOT)) != 0)
882                 return (error);
883         if (securelevel > 0)
884                 return (EPERM);
885         lp->lwp_md.md_regs->tf_eflags =
886             (lp->lwp_md.md_regs->tf_eflags & ~PSL_IOPL) |
887             (args->level * (PSL_IOPL / 3));
888         return (0);
889 }
890
891 /*
892  * MPSAFE
893  */
894 int
895 sys_linux_modify_ldt(struct linux_modify_ldt_args *uap)
896 {
897         int error;
898         caddr_t sg;
899         struct sysarch_args args;
900         struct i386_ldt_args *ldt;
901         struct l_descriptor ld;
902         union descriptor *desc;
903         int size, written;
904
905         sg = stackgap_init();
906
907         if (uap->ptr == NULL)
908                 return (EINVAL);
909
910         switch (uap->func) {
911         case 0x00: /* read_ldt */
912                 ldt = stackgap_alloc(&sg, sizeof(*ldt));
913                 ldt->start = 0;
914                 ldt->descs = uap->ptr;
915                 ldt->num = uap->bytecount / sizeof(union descriptor);
916                 args.op = I386_GET_LDT;
917                 args.parms = (char*)ldt;
918                 args.sysmsg_iresult = 0;
919                 error = sys_sysarch(&args);
920                 uap->sysmsg_iresult = args.sysmsg_iresult *
921                                       sizeof(union descriptor);
922                 break;
923         case 0x02: /* read_default_ldt = 0 */
924                 size = 5*sizeof(struct l_desc_struct);
925                 if (size > uap->bytecount)
926                         size = uap->bytecount;
927                 for (written = error = 0; written < size && error == 0; written++)
928                         error = subyte((char *)uap->ptr + written, 0);
929                 uap->sysmsg_iresult = written;
930                 break;
931         case 0x01: /* write_ldt */
932         case 0x11: /* write_ldt */
933                 if (uap->bytecount != sizeof(ld))
934                         return (EINVAL);
935
936                 error = copyin(uap->ptr, &ld, sizeof(ld));
937                 if (error)
938                         return (error);
939
940                 ldt = stackgap_alloc(&sg, sizeof(*ldt));
941                 desc = stackgap_alloc(&sg, sizeof(*desc));
942                 ldt->start = ld.entry_number;
943                 ldt->descs = desc;
944                 ldt->num = 1;
945                 desc->sd.sd_lolimit = (ld.limit & 0x0000ffff);
946                 desc->sd.sd_hilimit = (ld.limit & 0x000f0000) >> 16;
947                 desc->sd.sd_lobase = (ld.base_addr & 0x00ffffff);
948                 desc->sd.sd_hibase = (ld.base_addr & 0xff000000) >> 24;
949                 desc->sd.sd_type = SDT_MEMRO | ((ld.read_exec_only ^ 1) << 1) |
950                         (ld.contents << 2);
951                 desc->sd.sd_dpl = 3;
952                 desc->sd.sd_p = (ld.seg_not_present ^ 1);
953                 desc->sd.sd_xx = 0;
954                 desc->sd.sd_def32 = ld.seg_32bit;
955                 desc->sd.sd_gran = ld.limit_in_pages;
956                 args.op = I386_SET_LDT;
957                 args.parms = (char*)ldt;
958                 args.sysmsg_iresult = 0;
959                 error = sys_sysarch(&args);
960                 uap->sysmsg_iresult = args.sysmsg_iresult;
961                 break;
962         default:
963                 error = EINVAL;
964                 break;
965         }
966
967         return (error);
968 }
969
970 /*
971  * MPALMOSTSAFE
972  */
973 int
974 sys_linux_sigaction(struct linux_sigaction_args *args)
975 {
976         l_osigaction_t osa;
977         l_sigaction_t linux_act, linux_oact;
978         struct sigaction act, oact;
979         int error, sig;
980
981 #ifdef DEBUG
982         if (ldebug(sigaction))
983                 kprintf(ARGS(sigaction, "%d, %p, %p"),
984                     args->sig, (void *)args->nsa, (void *)args->osa);
985 #endif
986
987         if (args->nsa) {
988                 error = copyin(args->nsa, &osa, sizeof(l_osigaction_t));
989                 if (error)
990                         return (error);
991                 linux_act.lsa_handler = osa.lsa_handler;
992                 linux_act.lsa_flags = osa.lsa_flags;
993                 linux_act.lsa_restorer = osa.lsa_restorer;
994                 LINUX_SIGEMPTYSET(linux_act.lsa_mask);
995                 linux_act.lsa_mask.__bits[0] = osa.lsa_mask;
996                 linux_to_bsd_sigaction(&linux_act, &act);
997         }
998
999         if (args->sig <= LINUX_SIGTBLSZ)
1000                 sig = linux_to_bsd_signal[_SIG_IDX(args->sig)];
1001         else
1002                 sig = args->sig;
1003
1004         get_mplock();
1005         error = kern_sigaction(sig, args->nsa ? &act : NULL,
1006                                args->osa ? &oact : NULL);
1007         rel_mplock();
1008
1009         if (args->osa != NULL && !error) {
1010                 bsd_to_linux_sigaction(&oact, &linux_oact);
1011                 osa.lsa_handler = linux_oact.lsa_handler;
1012                 osa.lsa_flags = linux_oact.lsa_flags;
1013                 osa.lsa_restorer = linux_oact.lsa_restorer;
1014                 osa.lsa_mask = linux_oact.lsa_mask.__bits[0];
1015                 error = copyout(&osa, args->osa, sizeof(l_osigaction_t));
1016         }
1017         return (error);
1018 }
1019
1020 /*
1021  * Linux has two extra args, restart and oldmask.  We dont use these,
1022  * but it seems that "restart" is actually a context pointer that
1023  * enables the signal to happen with a different register set.
1024  *
1025  * MPALMOSTSAFE
1026  */
1027 int
1028 sys_linux_sigsuspend(struct linux_sigsuspend_args *args)
1029 {
1030         l_sigset_t linux_mask;
1031         sigset_t mask;
1032         int error;
1033
1034 #ifdef DEBUG
1035         if (ldebug(sigsuspend))
1036                 kprintf(ARGS(sigsuspend, "%08lx"), (unsigned long)args->mask);
1037 #endif
1038
1039         LINUX_SIGEMPTYSET(mask);
1040         mask.__bits[0] = args->mask;
1041         linux_to_bsd_sigset(&linux_mask, &mask);
1042
1043         get_mplock();
1044         error = kern_sigsuspend(&mask);
1045         rel_mplock();
1046
1047         return(error);
1048 }
1049
1050 /*
1051  * MPALMOSTSAFE
1052  */
1053 int
1054 sys_linux_rt_sigsuspend(struct linux_rt_sigsuspend_args *uap)
1055 {
1056         l_sigset_t linux_mask;
1057         sigset_t mask;
1058         int error;
1059
1060 #ifdef DEBUG
1061         if (ldebug(rt_sigsuspend))
1062                 kprintf(ARGS(rt_sigsuspend, "%p, %d"),
1063                     (void *)uap->newset, uap->sigsetsize);
1064 #endif
1065
1066         if (uap->sigsetsize != sizeof(l_sigset_t))
1067                 return (EINVAL);
1068
1069         error = copyin(uap->newset, &linux_mask, sizeof(l_sigset_t));
1070         if (error)
1071                 return (error);
1072
1073         linux_to_bsd_sigset(&linux_mask, &mask);
1074
1075         get_mplock();
1076         error = kern_sigsuspend(&mask);
1077         rel_mplock();
1078
1079         return(error);
1080 }
1081
1082 /*
1083  * MPALMOSTSAFE
1084  */
1085 int
1086 sys_linux_pause(struct linux_pause_args *args)
1087 {
1088         struct thread *td = curthread;
1089         struct lwp *lp = td->td_lwp;
1090         sigset_t mask;
1091         int error;
1092
1093 #ifdef DEBUG
1094         if (ldebug(pause))
1095                 kprintf(ARGS(pause, ""));
1096 #endif
1097
1098         mask = lp->lwp_sigmask;
1099
1100         get_mplock();
1101         error = kern_sigsuspend(&mask);
1102         rel_mplock();
1103
1104         return(error);
1105 }
1106
1107 /*
1108  * MPALMOSTSAFE
1109  */
1110 int
1111 sys_linux_sigaltstack(struct linux_sigaltstack_args *uap)
1112 {
1113         stack_t ss, oss;
1114         l_stack_t linux_ss;
1115         int error;
1116
1117 #ifdef DEBUG
1118         if (ldebug(sigaltstack))
1119                 kprintf(ARGS(sigaltstack, "%p, %p"), uap->uss, uap->uoss);
1120 #endif
1121
1122         if (uap->uss) {
1123                 error = copyin(uap->uss, &linux_ss, sizeof(l_stack_t));
1124                 if (error)
1125                         return (error);
1126
1127                 ss.ss_sp = linux_ss.ss_sp;
1128                 ss.ss_size = linux_ss.ss_size;
1129                 ss.ss_flags = linux_to_bsd_sigaltstack(linux_ss.ss_flags);
1130         }
1131
1132         get_mplock();
1133         error = kern_sigaltstack(uap->uss ? &ss : NULL,
1134                                  uap->uoss ? &oss : NULL);
1135         rel_mplock();
1136
1137         if (error == 0 && uap->uoss) {
1138                 linux_ss.ss_sp = oss.ss_sp;
1139                 linux_ss.ss_size = oss.ss_size;
1140                 linux_ss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags);
1141                 error = copyout(&linux_ss, uap->uoss, sizeof(l_stack_t));
1142         }
1143
1144         return (error);
1145 }
1146
1147 int
1148 sys_linux_set_thread_area(struct linux_set_thread_area_args *args)
1149 {
1150         struct segment_descriptor *desc;
1151         struct l_user_desc info;
1152         int error;
1153         int idx;
1154         int a[2];
1155         int i;
1156
1157         error = copyin(args->desc, &info, sizeof(struct l_user_desc));
1158         if (error)
1159                 return (EFAULT);
1160
1161 #ifdef DEBUG
1162         if (ldebug(set_thread_area))
1163                 kprintf(ARGS(set_thread_area, "%i, %x, %x, %i, %i, %i, %i, %i, %i\n"),
1164                       info.entry_number,
1165                       info.base_addr,
1166                       info.limit,
1167                       info.seg_32bit,
1168                       info.contents,
1169                       info.read_exec_only,
1170                       info.limit_in_pages,
1171                       info.seg_not_present,
1172                       info.useable);
1173 #endif
1174
1175         idx = info.entry_number;
1176         if (idx != -1 && (idx < 6 || idx > 8))
1177                 return (EINVAL);
1178
1179         if (idx == -1) {
1180                 /* -1 means finding the first free TLS entry */
1181                 for (i = 0; i < NGTLS; i++) {
1182                         /*
1183                          * try to determine if the TLS entry is empty by looking
1184                          * at the lolimit entry.
1185                          */
1186                         if (curthread->td_tls.tls[idx].sd_lolimit == 0) {
1187                                 idx = i;
1188                                 break;
1189                         }
1190                 }
1191
1192                 if (idx == -1) {
1193                         /*
1194                          * By now we should have an index. If not, it means
1195                          * that no entry is free, so return ESRCH.
1196                          */
1197                         return (ESRCH);
1198                 }
1199         } else {
1200                 /* translate the index from Linux to ours */
1201                 idx -= 6;
1202                 KKASSERT(idx >= 0);
1203         }
1204
1205         /* Tell the caller about the allocated entry number */
1206 #if 0 /* was SMP */
1207         info.entry_number = GTLS_START + mycpu->gd_cpuid * NGDT + idx;
1208 #endif
1209         info.entry_number = GTLS_START + idx;
1210
1211
1212         error = copyout(&info, args->desc, sizeof(struct l_user_desc));
1213         if (error)
1214                 return (error);
1215
1216         if (LINUX_LDT_empty(&info)) {
1217                 a[0] = 0;
1218                 a[1] = 0;
1219         } else {
1220                 a[0] = LINUX_LDT_entry_a(&info);
1221                 a[1] = LINUX_LDT_entry_b(&info);
1222         }
1223
1224         /*
1225          * Update the TLS and the TLS entries in the GDT, but hold a critical
1226          * section as required by set_user_TLS().
1227          */
1228         crit_enter();
1229         desc = &curthread->td_tls.tls[idx];
1230         memcpy(desc, &a, sizeof(a));
1231         set_user_TLS();
1232         crit_exit();
1233
1234         return (0);
1235 }
1236
1237 int
1238 sys_linux_get_thread_area(struct linux_get_thread_area_args *args)
1239 {
1240         struct segment_descriptor *sd;
1241         struct l_desc_struct desc;
1242         struct l_user_desc info;
1243         int error;
1244         int idx;
1245
1246 #ifdef DEBUG
1247         if (ldebug(get_thread_area))
1248                 kprintf(ARGS(get_thread_area, "%p"), args->desc);
1249 #endif
1250
1251         error = copyin(args->desc, &info, sizeof(struct l_user_desc));
1252         if (error)
1253                 return (EFAULT);
1254                 
1255         idx = info.entry_number;
1256         if ((idx < 6 || idx > 8) && (idx < GTLS_START)) {
1257                 kprintf("sys_linux_get_thread_area, invalid idx requested: %d\n", idx);
1258                 return (EINVAL);
1259         }
1260
1261         memset(&info, 0, sizeof(info));
1262
1263         /* translate the index from Linux to ours */
1264         info.entry_number = idx;
1265         if (idx < GTLS_START) {
1266                 idx -= 6;
1267         } else {
1268 #if 0 /* was SMP */
1269                 idx -= (GTLS_START + mycpu->gd_cpuid * NGDT);
1270 #endif
1271                 idx -= GTLS_START;
1272
1273         }
1274         KKASSERT(idx >= 0);
1275
1276         sd = &curthread->td_tls.tls[idx];
1277         memcpy(&desc, sd, sizeof(desc));
1278         info.base_addr = LINUX_GET_BASE(&desc);
1279         info.limit = LINUX_GET_LIMIT(&desc);
1280         info.seg_32bit = LINUX_GET_32BIT(&desc);
1281         info.contents = LINUX_GET_CONTENTS(&desc);
1282         info.read_exec_only = !LINUX_GET_WRITABLE(&desc);
1283         info.limit_in_pages = LINUX_GET_LIMIT_PAGES(&desc);
1284         info.seg_not_present = !LINUX_GET_PRESENT(&desc);
1285         info.useable = LINUX_GET_USEABLE(&desc);
1286
1287         error = copyout(&info, args->desc, sizeof(struct l_user_desc));
1288         if (error)
1289                 return (EFAULT);
1290
1291         return (0);
1292 }