]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/i386/linux/linux_machdep.c
MFC r283383:
[FreeBSD/stable/10.git] / sys / i386 / linux / 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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/capsicum.h>
35 #include <sys/file.h>
36 #include <sys/fcntl.h>
37 #include <sys/imgact.h>
38 #include <sys/lock.h>
39 #include <sys/malloc.h>
40 #include <sys/mman.h>
41 #include <sys/mutex.h>
42 #include <sys/sx.h>
43 #include <sys/priv.h>
44 #include <sys/proc.h>
45 #include <sys/queue.h>
46 #include <sys/resource.h>
47 #include <sys/resourcevar.h>
48 #include <sys/signalvar.h>
49 #include <sys/syscallsubr.h>
50 #include <sys/sysproto.h>
51 #include <sys/unistd.h>
52 #include <sys/wait.h>
53 #include <sys/sched.h>
54
55 #include <machine/frame.h>
56 #include <machine/psl.h>
57 #include <machine/segments.h>
58 #include <machine/sysarch.h>
59
60 #include <vm/vm.h>
61 #include <vm/pmap.h>
62 #include <vm/vm_map.h>
63
64 #include <i386/linux/linux.h>
65 #include <i386/linux/linux_proto.h>
66 #include <compat/linux/linux_ipc.h>
67 #include <compat/linux/linux_misc.h>
68 #include <compat/linux/linux_signal.h>
69 #include <compat/linux/linux_util.h>
70 #include <compat/linux/linux_emul.h>
71
72 #include <i386/include/pcb.h>                   /* needed for pcb definition in linux_set_thread_area */
73
74 #include "opt_posix.h"
75
76 extern struct sysentvec elf32_freebsd_sysvec;   /* defined in i386/i386/elf_machdep.c */
77
78 struct l_descriptor {
79         l_uint          entry_number;
80         l_ulong         base_addr;
81         l_uint          limit;
82         l_uint          seg_32bit:1;
83         l_uint          contents:2;
84         l_uint          read_exec_only:1;
85         l_uint          limit_in_pages:1;
86         l_uint          seg_not_present:1;
87         l_uint          useable:1;
88 };
89
90 struct l_old_select_argv {
91         l_int           nfds;
92         l_fd_set        *readfds;
93         l_fd_set        *writefds;
94         l_fd_set        *exceptfds;
95         struct l_timeval        *timeout;
96 };
97
98 static int      linux_mmap_common(struct thread *td, l_uintptr_t addr,
99                     l_size_t len, l_int prot, l_int flags, l_int fd,
100                     l_loff_t pos);
101
102 int
103 linux_to_bsd_sigaltstack(int lsa)
104 {
105         int bsa = 0;
106
107         if (lsa & LINUX_SS_DISABLE)
108                 bsa |= SS_DISABLE;
109         if (lsa & LINUX_SS_ONSTACK)
110                 bsa |= SS_ONSTACK;
111         return (bsa);
112 }
113
114 int
115 bsd_to_linux_sigaltstack(int bsa)
116 {
117         int lsa = 0;
118
119         if (bsa & SS_DISABLE)
120                 lsa |= LINUX_SS_DISABLE;
121         if (bsa & SS_ONSTACK)
122                 lsa |= LINUX_SS_ONSTACK;
123         return (lsa);
124 }
125
126 int
127 linux_execve(struct thread *td, struct linux_execve_args *args)
128 {
129         struct image_args eargs;
130         struct vmspace *oldvmspace;
131         char *newpath;
132         int error;
133
134         LCONVPATHEXIST(td, args->path, &newpath);
135
136 #ifdef DEBUG
137         if (ldebug(execve))
138                 printf(ARGS(execve, "%s"), newpath);
139 #endif
140
141         error = pre_execve(td, &oldvmspace);
142         if (error != 0) {
143                 free(newpath, M_TEMP);
144                 return (error);
145         }
146         error = exec_copyin_args(&eargs, newpath, UIO_SYSSPACE,
147             args->argp, args->envp);
148         free(newpath, M_TEMP);
149         if (error == 0)
150                 error = kern_execve(td, &eargs, NULL);
151         if (error == 0)
152                 error = linux_common_execve(td, &eargs);
153         post_execve(td, error, oldvmspace);
154         return (error);
155 }
156
157 struct l_ipc_kludge {
158         struct l_msgbuf *msgp;
159         l_long msgtyp;
160 };
161
162 int
163 linux_ipc(struct thread *td, struct linux_ipc_args *args)
164 {
165
166         switch (args->what & 0xFFFF) {
167         case LINUX_SEMOP: {
168                 struct linux_semop_args a;
169
170                 a.semid = args->arg1;
171                 a.tsops = args->ptr;
172                 a.nsops = args->arg2;
173                 return (linux_semop(td, &a));
174         }
175         case LINUX_SEMGET: {
176                 struct linux_semget_args a;
177
178                 a.key = args->arg1;
179                 a.nsems = args->arg2;
180                 a.semflg = args->arg3;
181                 return (linux_semget(td, &a));
182         }
183         case LINUX_SEMCTL: {
184                 struct linux_semctl_args a;
185                 int error;
186
187                 a.semid = args->arg1;
188                 a.semnum = args->arg2;
189                 a.cmd = args->arg3;
190                 error = copyin(args->ptr, &a.arg, sizeof(a.arg));
191                 if (error)
192                         return (error);
193                 return (linux_semctl(td, &a));
194         }
195         case LINUX_MSGSND: {
196                 struct linux_msgsnd_args a;
197
198                 a.msqid = args->arg1;
199                 a.msgp = args->ptr;
200                 a.msgsz = args->arg2;
201                 a.msgflg = args->arg3;
202                 return (linux_msgsnd(td, &a));
203         }
204         case LINUX_MSGRCV: {
205                 struct linux_msgrcv_args a;
206
207                 a.msqid = args->arg1;
208                 a.msgsz = args->arg2;
209                 a.msgflg = args->arg3;
210                 if ((args->what >> 16) == 0) {
211                         struct l_ipc_kludge tmp;
212                         int error;
213
214                         if (args->ptr == NULL)
215                                 return (EINVAL);
216                         error = copyin(args->ptr, &tmp, sizeof(tmp));
217                         if (error)
218                                 return (error);
219                         a.msgp = tmp.msgp;
220                         a.msgtyp = tmp.msgtyp;
221                 } else {
222                         a.msgp = args->ptr;
223                         a.msgtyp = args->arg5;
224                 }
225                 return (linux_msgrcv(td, &a));
226         }
227         case LINUX_MSGGET: {
228                 struct linux_msgget_args a;
229
230                 a.key = args->arg1;
231                 a.msgflg = args->arg2;
232                 return (linux_msgget(td, &a));
233         }
234         case LINUX_MSGCTL: {
235                 struct linux_msgctl_args a;
236
237                 a.msqid = args->arg1;
238                 a.cmd = args->arg2;
239                 a.buf = args->ptr;
240                 return (linux_msgctl(td, &a));
241         }
242         case LINUX_SHMAT: {
243                 struct linux_shmat_args a;
244
245                 a.shmid = args->arg1;
246                 a.shmaddr = args->ptr;
247                 a.shmflg = args->arg2;
248                 a.raddr = (l_ulong *)args->arg3;
249                 return (linux_shmat(td, &a));
250         }
251         case LINUX_SHMDT: {
252                 struct linux_shmdt_args a;
253
254                 a.shmaddr = args->ptr;
255                 return (linux_shmdt(td, &a));
256         }
257         case LINUX_SHMGET: {
258                 struct linux_shmget_args a;
259
260                 a.key = args->arg1;
261                 a.size = args->arg2;
262                 a.shmflg = args->arg3;
263                 return (linux_shmget(td, &a));
264         }
265         case LINUX_SHMCTL: {
266                 struct linux_shmctl_args a;
267
268                 a.shmid = args->arg1;
269                 a.cmd = args->arg2;
270                 a.buf = args->ptr;
271                 return (linux_shmctl(td, &a));
272         }
273         default:
274                 break;
275         }
276
277         return (EINVAL);
278 }
279
280 int
281 linux_old_select(struct thread *td, struct linux_old_select_args *args)
282 {
283         struct l_old_select_argv linux_args;
284         struct linux_select_args newsel;
285         int error;
286
287 #ifdef DEBUG
288         if (ldebug(old_select))
289                 printf(ARGS(old_select, "%p"), args->ptr);
290 #endif
291
292         error = copyin(args->ptr, &linux_args, sizeof(linux_args));
293         if (error)
294                 return (error);
295
296         newsel.nfds = linux_args.nfds;
297         newsel.readfds = linux_args.readfds;
298         newsel.writefds = linux_args.writefds;
299         newsel.exceptfds = linux_args.exceptfds;
300         newsel.timeout = linux_args.timeout;
301         return (linux_select(td, &newsel));
302 }
303
304 int
305 linux_set_cloned_tls(struct thread *td, void *desc)
306 {
307         struct segment_descriptor sd;
308         struct l_user_desc info;
309         int idx, error;
310         int a[2];
311
312         error = copyin(desc, &info, sizeof(struct l_user_desc));
313         if (error) {
314                 printf(LMSG("copyin failed!"));
315         } else {
316                 idx = info.entry_number;
317
318                 /* 
319                  * looks like we're getting the idx we returned
320                  * in the set_thread_area() syscall
321                  */
322                 if (idx != 6 && idx != 3) {
323                         printf(LMSG("resetting idx!"));
324                         idx = 3;
325                 }
326
327                 /* this doesnt happen in practice */
328                 if (idx == 6) {
329                         /* we might copy out the entry_number as 3 */
330                         info.entry_number = 3;
331                         error = copyout(&info, desc, sizeof(struct l_user_desc));
332                         if (error)
333                                 printf(LMSG("copyout failed!"));
334                 }
335
336                 a[0] = LINUX_LDT_entry_a(&info);
337                 a[1] = LINUX_LDT_entry_b(&info);
338
339                 memcpy(&sd, &a, sizeof(a));
340 #ifdef DEBUG
341                 if (ldebug(clone))
342                         printf("Segment created in clone with "
343                         "CLONE_SETTLS: lobase: %x, hibase: %x, "
344                         "lolimit: %x, hilimit: %x, type: %i, "
345                         "dpl: %i, p: %i, xx: %i, def32: %i, "
346                         "gran: %i\n", sd.sd_lobase, sd.sd_hibase,
347                         sd.sd_lolimit, sd.sd_hilimit, sd.sd_type,
348                         sd.sd_dpl, sd.sd_p, sd.sd_xx,
349                         sd.sd_def32, sd.sd_gran);
350 #endif
351
352                 /* set %gs */
353                 td->td_pcb->pcb_gsd = sd;
354                 td->td_pcb->pcb_gs = GSEL(GUGS_SEL, SEL_UPL);
355         }
356
357         return (error);
358 }
359
360 int
361 linux_set_upcall_kse(struct thread *td, register_t stack)
362 {
363
364         if (stack)
365                 td->td_frame->tf_esp = stack;
366
367         /*
368          * The newly created Linux thread returns
369          * to the user space by the same path that a parent do.
370          */
371         td->td_frame->tf_eax = 0;
372         return (0);
373 }
374
375 #define STACK_SIZE  (2 * 1024 * 1024)
376 #define GUARD_SIZE  (4 * PAGE_SIZE)
377
378 int
379 linux_mmap2(struct thread *td, struct linux_mmap2_args *args)
380 {
381
382 #ifdef DEBUG
383         if (ldebug(mmap2))
384                 printf(ARGS(mmap2, "%p, %d, %d, 0x%08x, %d, %d"),
385                     (void *)args->addr, args->len, args->prot,
386                     args->flags, args->fd, args->pgoff);
387 #endif
388
389         return (linux_mmap_common(td, args->addr, args->len, args->prot,
390                 args->flags, args->fd, (uint64_t)(uint32_t)args->pgoff *
391                 PAGE_SIZE));
392 }
393
394 int
395 linux_mmap(struct thread *td, struct linux_mmap_args *args)
396 {
397         int error;
398         struct l_mmap_argv linux_args;
399
400         error = copyin(args->ptr, &linux_args, sizeof(linux_args));
401         if (error)
402                 return (error);
403
404 #ifdef DEBUG
405         if (ldebug(mmap))
406                 printf(ARGS(mmap, "%p, %d, %d, 0x%08x, %d, %d"),
407                     (void *)linux_args.addr, linux_args.len, linux_args.prot,
408                     linux_args.flags, linux_args.fd, linux_args.pgoff);
409 #endif
410
411         return (linux_mmap_common(td, linux_args.addr, linux_args.len,
412             linux_args.prot, linux_args.flags, linux_args.fd,
413             (uint32_t)linux_args.pgoff));
414 }
415
416 static int
417 linux_mmap_common(struct thread *td, l_uintptr_t addr, l_size_t len, l_int prot,
418     l_int flags, l_int fd, l_loff_t pos)
419 {
420         struct proc *p = td->td_proc;
421         struct mmap_args /* {
422                 caddr_t addr;
423                 size_t len;
424                 int prot;
425                 int flags;
426                 int fd;
427                 long pad;
428                 off_t pos;
429         } */ bsd_args;
430         int error;
431         struct file *fp;
432         cap_rights_t rights;
433
434         error = 0;
435         bsd_args.flags = 0;
436         fp = NULL;
437
438         /*
439          * Linux mmap(2):
440          * You must specify exactly one of MAP_SHARED and MAP_PRIVATE
441          */
442         if (!((flags & LINUX_MAP_SHARED) ^ (flags & LINUX_MAP_PRIVATE)))
443                 return (EINVAL);
444
445         if (flags & LINUX_MAP_SHARED)
446                 bsd_args.flags |= MAP_SHARED;
447         if (flags & LINUX_MAP_PRIVATE)
448                 bsd_args.flags |= MAP_PRIVATE;
449         if (flags & LINUX_MAP_FIXED)
450                 bsd_args.flags |= MAP_FIXED;
451         if (flags & LINUX_MAP_ANON) {
452                 /* Enforce pos to be on page boundary, then ignore. */
453                 if ((pos & PAGE_MASK) != 0)
454                         return (EINVAL);
455                 pos = 0;
456                 bsd_args.flags |= MAP_ANON;
457         } else
458                 bsd_args.flags |= MAP_NOSYNC;
459         if (flags & LINUX_MAP_GROWSDOWN)
460                 bsd_args.flags |= MAP_STACK;
461
462         /*
463          * PROT_READ, PROT_WRITE, or PROT_EXEC implies PROT_READ and PROT_EXEC
464          * on Linux/i386. We do this to ensure maximum compatibility.
465          * Linux/ia64 does the same in i386 emulation mode.
466          */
467         bsd_args.prot = prot;
468         if (bsd_args.prot & (PROT_READ | PROT_WRITE | PROT_EXEC))
469                 bsd_args.prot |= PROT_READ | PROT_EXEC;
470
471         /* Linux does not check file descriptor when MAP_ANONYMOUS is set. */
472         bsd_args.fd = (bsd_args.flags & MAP_ANON) ? -1 : fd;
473         if (bsd_args.fd != -1) {
474                 /*
475                  * Linux follows Solaris mmap(2) description:
476                  * The file descriptor fildes is opened with
477                  * read permission, regardless of the
478                  * protection options specified.
479                  *
480                  * Checking just CAP_MMAP is fine here, since the real work
481                  * is done in the FreeBSD mmap().
482                  */
483
484                 error = fget(td, bsd_args.fd,
485                     cap_rights_init(&rights, CAP_MMAP), &fp);
486                 if (error != 0)
487                         return (error);
488                 if (fp->f_type != DTYPE_VNODE) {
489                         fdrop(fp, td);
490                         return (EINVAL);
491                 }
492
493                 /* Linux mmap() just fails for O_WRONLY files */
494                 if (!(fp->f_flag & FREAD)) {
495                         fdrop(fp, td);
496                         return (EACCES);
497                 }
498
499                 fdrop(fp, td);
500         }
501
502         if (flags & LINUX_MAP_GROWSDOWN) {
503                 /* 
504                  * The Linux MAP_GROWSDOWN option does not limit auto
505                  * growth of the region.  Linux mmap with this option
506                  * takes as addr the inital BOS, and as len, the initial
507                  * region size.  It can then grow down from addr without
508                  * limit.  However, linux threads has an implicit internal
509                  * limit to stack size of STACK_SIZE.  Its just not
510                  * enforced explicitly in linux.  But, here we impose
511                  * a limit of (STACK_SIZE - GUARD_SIZE) on the stack
512                  * region, since we can do this with our mmap.
513                  *
514                  * Our mmap with MAP_STACK takes addr as the maximum
515                  * downsize limit on BOS, and as len the max size of
516                  * the region.  It them maps the top SGROWSIZ bytes,
517                  * and auto grows the region down, up to the limit
518                  * in addr.
519                  *
520                  * If we don't use the MAP_STACK option, the effect
521                  * of this code is to allocate a stack region of a
522                  * fixed size of (STACK_SIZE - GUARD_SIZE).
523                  */
524
525                 if ((caddr_t)PTRIN(addr) + len > p->p_vmspace->vm_maxsaddr) {
526                         /* 
527                          * Some linux apps will attempt to mmap
528                          * thread stacks near the top of their
529                          * address space.  If their TOS is greater
530                          * than vm_maxsaddr, vm_map_growstack()
531                          * will confuse the thread stack with the
532                          * process stack and deliver a SEGV if they
533                          * attempt to grow the thread stack past their
534                          * current stacksize rlimit.  To avoid this,
535                          * adjust vm_maxsaddr upwards to reflect
536                          * the current stacksize rlimit rather
537                          * than the maximum possible stacksize.
538                          * It would be better to adjust the
539                          * mmap'ed region, but some apps do not check
540                          * mmap's return value.
541                          */
542                         PROC_LOCK(p);
543                         p->p_vmspace->vm_maxsaddr = (char *)USRSTACK -
544                             lim_cur(p, RLIMIT_STACK);
545                         PROC_UNLOCK(p);
546                 }
547
548                 /*
549                  * This gives us our maximum stack size and a new BOS.
550                  * If we're using VM_STACK, then mmap will just map
551                  * the top SGROWSIZ bytes, and let the stack grow down
552                  * to the limit at BOS.  If we're not using VM_STACK
553                  * we map the full stack, since we don't have a way
554                  * to autogrow it.
555                  */
556                 if (len > STACK_SIZE - GUARD_SIZE) {
557                         bsd_args.addr = (caddr_t)PTRIN(addr);
558                         bsd_args.len = len;
559                 } else {
560                         bsd_args.addr = (caddr_t)PTRIN(addr) -
561                             (STACK_SIZE - GUARD_SIZE - len);
562                         bsd_args.len = STACK_SIZE - GUARD_SIZE;
563                 }
564         } else {
565                 bsd_args.addr = (caddr_t)PTRIN(addr);
566                 bsd_args.len  = len;
567         }
568         bsd_args.pos = pos;
569
570 #ifdef DEBUG
571         if (ldebug(mmap))
572                 printf("-> %s(%p, %d, %d, 0x%08x, %d, 0x%x)\n",
573                     __func__,
574                     (void *)bsd_args.addr, bsd_args.len, bsd_args.prot,
575                     bsd_args.flags, bsd_args.fd, (int)bsd_args.pos);
576 #endif
577         error = sys_mmap(td, &bsd_args);
578 #ifdef DEBUG
579         if (ldebug(mmap))
580                 printf("-> %s() return: 0x%x (0x%08x)\n",
581                         __func__, error, (u_int)td->td_retval[0]);
582 #endif
583         return (error);
584 }
585
586 int
587 linux_mprotect(struct thread *td, struct linux_mprotect_args *uap)
588 {
589         struct mprotect_args bsd_args;
590
591         bsd_args.addr = uap->addr;
592         bsd_args.len = uap->len;
593         bsd_args.prot = uap->prot;
594         if (bsd_args.prot & (PROT_READ | PROT_WRITE | PROT_EXEC))
595                 bsd_args.prot |= PROT_READ | PROT_EXEC;
596         return (sys_mprotect(td, &bsd_args));
597 }
598
599 int
600 linux_ioperm(struct thread *td, struct linux_ioperm_args *args)
601 {
602         int error;
603         struct i386_ioperm_args iia;
604
605         iia.start = args->start;
606         iia.length = args->length;
607         iia.enable = args->enable;
608         error = i386_set_ioperm(td, &iia);
609         return (error);
610 }
611
612 int
613 linux_iopl(struct thread *td, struct linux_iopl_args *args)
614 {
615         int error;
616
617         if (args->level < 0 || args->level > 3)
618                 return (EINVAL);
619         if ((error = priv_check(td, PRIV_IO)) != 0)
620                 return (error);
621         if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
622                 return (error);
623         td->td_frame->tf_eflags = (td->td_frame->tf_eflags & ~PSL_IOPL) |
624             (args->level * (PSL_IOPL / 3));
625         return (0);
626 }
627
628 int
629 linux_modify_ldt(struct thread *td, struct linux_modify_ldt_args *uap)
630 {
631         int error;
632         struct i386_ldt_args ldt;
633         struct l_descriptor ld;
634         union descriptor desc;
635         int size, written;
636
637         switch (uap->func) {
638         case 0x00: /* read_ldt */
639                 ldt.start = 0;
640                 ldt.descs = uap->ptr;
641                 ldt.num = uap->bytecount / sizeof(union descriptor);
642                 error = i386_get_ldt(td, &ldt);
643                 td->td_retval[0] *= sizeof(union descriptor);
644                 break;
645         case 0x02: /* read_default_ldt = 0 */
646                 size = 5*sizeof(struct l_desc_struct);
647                 if (size > uap->bytecount)
648                         size = uap->bytecount;
649                 for (written = error = 0; written < size && error == 0; written++)
650                         error = subyte((char *)uap->ptr + written, 0);
651                 td->td_retval[0] = written;
652                 break;
653         case 0x01: /* write_ldt */
654         case 0x11: /* write_ldt */
655                 if (uap->bytecount != sizeof(ld))
656                         return (EINVAL);
657
658                 error = copyin(uap->ptr, &ld, sizeof(ld));
659                 if (error)
660                         return (error);
661
662                 ldt.start = ld.entry_number;
663                 ldt.descs = &desc;
664                 ldt.num = 1;
665                 desc.sd.sd_lolimit = (ld.limit & 0x0000ffff);
666                 desc.sd.sd_hilimit = (ld.limit & 0x000f0000) >> 16;
667                 desc.sd.sd_lobase = (ld.base_addr & 0x00ffffff);
668                 desc.sd.sd_hibase = (ld.base_addr & 0xff000000) >> 24;
669                 desc.sd.sd_type = SDT_MEMRO | ((ld.read_exec_only ^ 1) << 1) |
670                         (ld.contents << 2);
671                 desc.sd.sd_dpl = 3;
672                 desc.sd.sd_p = (ld.seg_not_present ^ 1);
673                 desc.sd.sd_xx = 0;
674                 desc.sd.sd_def32 = ld.seg_32bit;
675                 desc.sd.sd_gran = ld.limit_in_pages;
676                 error = i386_set_ldt(td, &ldt, &desc);
677                 break;
678         default:
679                 error = ENOSYS;
680                 break;
681         }
682
683         if (error == EOPNOTSUPP) {
684                 printf("linux: modify_ldt needs kernel option USER_LDT\n");
685                 error = ENOSYS;
686         }
687
688         return (error);
689 }
690
691 int
692 linux_sigaction(struct thread *td, struct linux_sigaction_args *args)
693 {
694         l_osigaction_t osa;
695         l_sigaction_t act, oact;
696         int error;
697
698 #ifdef DEBUG
699         if (ldebug(sigaction))
700                 printf(ARGS(sigaction, "%d, %p, %p"),
701                     args->sig, (void *)args->nsa, (void *)args->osa);
702 #endif
703
704         if (args->nsa != NULL) {
705                 error = copyin(args->nsa, &osa, sizeof(l_osigaction_t));
706                 if (error)
707                         return (error);
708                 act.lsa_handler = osa.lsa_handler;
709                 act.lsa_flags = osa.lsa_flags;
710                 act.lsa_restorer = osa.lsa_restorer;
711                 LINUX_SIGEMPTYSET(act.lsa_mask);
712                 act.lsa_mask.__bits[0] = osa.lsa_mask;
713         }
714
715         error = linux_do_sigaction(td, args->sig, args->nsa ? &act : NULL,
716             args->osa ? &oact : NULL);
717
718         if (args->osa != NULL && !error) {
719                 osa.lsa_handler = oact.lsa_handler;
720                 osa.lsa_flags = oact.lsa_flags;
721                 osa.lsa_restorer = oact.lsa_restorer;
722                 osa.lsa_mask = oact.lsa_mask.__bits[0];
723                 error = copyout(&osa, args->osa, sizeof(l_osigaction_t));
724         }
725
726         return (error);
727 }
728
729 /*
730  * Linux has two extra args, restart and oldmask.  We dont use these,
731  * but it seems that "restart" is actually a context pointer that
732  * enables the signal to happen with a different register set.
733  */
734 int
735 linux_sigsuspend(struct thread *td, struct linux_sigsuspend_args *args)
736 {
737         sigset_t sigmask;
738         l_sigset_t mask;
739
740 #ifdef DEBUG
741         if (ldebug(sigsuspend))
742                 printf(ARGS(sigsuspend, "%08lx"), (unsigned long)args->mask);
743 #endif
744
745         LINUX_SIGEMPTYSET(mask);
746         mask.__bits[0] = args->mask;
747         linux_to_bsd_sigset(&mask, &sigmask);
748         return (kern_sigsuspend(td, sigmask));
749 }
750
751 int
752 linux_rt_sigsuspend(struct thread *td, struct linux_rt_sigsuspend_args *uap)
753 {
754         l_sigset_t lmask;
755         sigset_t sigmask;
756         int error;
757
758 #ifdef DEBUG
759         if (ldebug(rt_sigsuspend))
760                 printf(ARGS(rt_sigsuspend, "%p, %d"),
761                     (void *)uap->newset, uap->sigsetsize);
762 #endif
763
764         if (uap->sigsetsize != sizeof(l_sigset_t))
765                 return (EINVAL);
766
767         error = copyin(uap->newset, &lmask, sizeof(l_sigset_t));
768         if (error)
769                 return (error);
770
771         linux_to_bsd_sigset(&lmask, &sigmask);
772         return (kern_sigsuspend(td, sigmask));
773 }
774
775 int
776 linux_pause(struct thread *td, struct linux_pause_args *args)
777 {
778         struct proc *p = td->td_proc;
779         sigset_t sigmask;
780
781 #ifdef DEBUG
782         if (ldebug(pause))
783                 printf(ARGS(pause, ""));
784 #endif
785
786         PROC_LOCK(p);
787         sigmask = td->td_sigmask;
788         PROC_UNLOCK(p);
789         return (kern_sigsuspend(td, sigmask));
790 }
791
792 int
793 linux_sigaltstack(struct thread *td, struct linux_sigaltstack_args *uap)
794 {
795         stack_t ss, oss;
796         l_stack_t lss;
797         int error;
798
799 #ifdef DEBUG
800         if (ldebug(sigaltstack))
801                 printf(ARGS(sigaltstack, "%p, %p"), uap->uss, uap->uoss);
802 #endif
803
804         if (uap->uss != NULL) {
805                 error = copyin(uap->uss, &lss, sizeof(l_stack_t));
806                 if (error)
807                         return (error);
808
809                 ss.ss_sp = lss.ss_sp;
810                 ss.ss_size = lss.ss_size;
811                 ss.ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags);
812         }
813         error = kern_sigaltstack(td, (uap->uss != NULL) ? &ss : NULL,
814             (uap->uoss != NULL) ? &oss : NULL);
815         if (!error && uap->uoss != NULL) {
816                 lss.ss_sp = oss.ss_sp;
817                 lss.ss_size = oss.ss_size;
818                 lss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags);
819                 error = copyout(&lss, uap->uoss, sizeof(l_stack_t));
820         }
821
822         return (error);
823 }
824
825 int
826 linux_ftruncate64(struct thread *td, struct linux_ftruncate64_args *args)
827 {
828         struct ftruncate_args sa;
829
830 #ifdef DEBUG
831         if (ldebug(ftruncate64))
832                 printf(ARGS(ftruncate64, "%u, %jd"), args->fd,
833                     (intmax_t)args->length);
834 #endif
835
836         sa.fd = args->fd;
837         sa.length = args->length;
838         return sys_ftruncate(td, &sa);
839 }
840
841 int
842 linux_set_thread_area(struct thread *td, struct linux_set_thread_area_args *args)
843 {
844         struct l_user_desc info;
845         int error;
846         int idx;
847         int a[2];
848         struct segment_descriptor sd;
849
850         error = copyin(args->desc, &info, sizeof(struct l_user_desc));
851         if (error)
852                 return (error);
853
854 #ifdef DEBUG
855         if (ldebug(set_thread_area))
856                 printf(ARGS(set_thread_area, "%i, %x, %x, %i, %i, %i, %i, %i, %i\n"),
857                       info.entry_number,
858                       info.base_addr,
859                       info.limit,
860                       info.seg_32bit,
861                       info.contents,
862                       info.read_exec_only,
863                       info.limit_in_pages,
864                       info.seg_not_present,
865                       info.useable);
866 #endif
867
868         idx = info.entry_number;
869         /* 
870          * Semantics of linux version: every thread in the system has array of
871          * 3 tls descriptors. 1st is GLIBC TLS, 2nd is WINE, 3rd unknown. This 
872          * syscall loads one of the selected tls decriptors with a value and
873          * also loads GDT descriptors 6, 7 and 8 with the content of the
874          * per-thread descriptors.
875          *
876          * Semantics of fbsd version: I think we can ignore that linux has 3 
877          * per-thread descriptors and use just the 1st one. The tls_array[]
878          * is used only in set/get-thread_area() syscalls and for loading the
879          * GDT descriptors. In fbsd we use just one GDT descriptor for TLS so
880          * we will load just one. 
881          *
882          * XXX: this doesn't work when a user space process tries to use more
883          * than 1 TLS segment. Comment in the linux sources says wine might do
884          * this.
885          */
886
887         /* 
888          * we support just GLIBC TLS now 
889          * we should let 3 proceed as well because we use this segment so
890          * if code does two subsequent calls it should succeed
891          */
892         if (idx != 6 && idx != -1 && idx != 3)
893                 return (EINVAL);
894
895         /* 
896          * we have to copy out the GDT entry we use
897          * FreeBSD uses GDT entry #3 for storing %gs so load that
898          *
899          * XXX: what if a user space program doesn't check this value and tries
900          * to use 6, 7 or 8? 
901          */
902         idx = info.entry_number = 3;
903         error = copyout(&info, args->desc, sizeof(struct l_user_desc));
904         if (error)
905                 return (error);
906
907         if (LINUX_LDT_empty(&info)) {
908                 a[0] = 0;
909                 a[1] = 0;
910         } else {
911                 a[0] = LINUX_LDT_entry_a(&info);
912                 a[1] = LINUX_LDT_entry_b(&info);
913         }
914
915         memcpy(&sd, &a, sizeof(a));
916 #ifdef DEBUG
917         if (ldebug(set_thread_area))
918                 printf("Segment created in set_thread_area: lobase: %x, hibase: %x, lolimit: %x, hilimit: %x, type: %i, dpl: %i, p: %i, xx: %i, def32: %i, gran: %i\n", sd.sd_lobase,
919                         sd.sd_hibase,
920                         sd.sd_lolimit,
921                         sd.sd_hilimit,
922                         sd.sd_type,
923                         sd.sd_dpl,
924                         sd.sd_p,
925                         sd.sd_xx,
926                         sd.sd_def32,
927                         sd.sd_gran);
928 #endif
929
930         /* this is taken from i386 version of cpu_set_user_tls() */
931         critical_enter();
932         /* set %gs */
933         td->td_pcb->pcb_gsd = sd;
934         PCPU_GET(fsgs_gdt)[1] = sd;
935         load_gs(GSEL(GUGS_SEL, SEL_UPL));
936         critical_exit();
937    
938         return (0);
939 }
940
941 int
942 linux_get_thread_area(struct thread *td, struct linux_get_thread_area_args *args)
943 {
944         
945         struct l_user_desc info;
946         int error;
947         int idx;
948         struct l_desc_struct desc;
949         struct segment_descriptor sd;
950
951 #ifdef DEBUG
952         if (ldebug(get_thread_area))
953                 printf(ARGS(get_thread_area, "%p"), args->desc);
954 #endif
955
956         error = copyin(args->desc, &info, sizeof(struct l_user_desc));
957         if (error)
958                 return (error);
959
960         idx = info.entry_number;
961         /* XXX: I am not sure if we want 3 to be allowed too. */
962         if (idx != 6 && idx != 3)
963                 return (EINVAL);
964
965         idx = 3;
966
967         memset(&info, 0, sizeof(info));
968
969         sd = PCPU_GET(fsgs_gdt)[1];
970
971         memcpy(&desc, &sd, sizeof(desc));
972
973         info.entry_number = idx;
974         info.base_addr = LINUX_GET_BASE(&desc);
975         info.limit = LINUX_GET_LIMIT(&desc);
976         info.seg_32bit = LINUX_GET_32BIT(&desc);
977         info.contents = LINUX_GET_CONTENTS(&desc);
978         info.read_exec_only = !LINUX_GET_WRITABLE(&desc);
979         info.limit_in_pages = LINUX_GET_LIMIT_PAGES(&desc);
980         info.seg_not_present = !LINUX_GET_PRESENT(&desc);
981         info.useable = LINUX_GET_USEABLE(&desc);
982
983         error = copyout(&info, args->desc, sizeof(struct l_user_desc));
984         if (error)
985                 return (EFAULT);
986
987         return (0);
988 }
989
990 /* XXX: this wont work with module - convert it */
991 int
992 linux_mq_open(struct thread *td, struct linux_mq_open_args *args)
993 {
994 #ifdef P1003_1B_MQUEUE
995         return sys_kmq_open(td, (struct kmq_open_args *) args);
996 #else
997         return (ENOSYS);
998 #endif
999 }
1000
1001 int
1002 linux_mq_unlink(struct thread *td, struct linux_mq_unlink_args *args)
1003 {
1004 #ifdef P1003_1B_MQUEUE
1005         return sys_kmq_unlink(td, (struct kmq_unlink_args *) args);
1006 #else
1007         return (ENOSYS);
1008 #endif
1009 }
1010
1011 int
1012 linux_mq_timedsend(struct thread *td, struct linux_mq_timedsend_args *args)
1013 {
1014 #ifdef P1003_1B_MQUEUE
1015         return sys_kmq_timedsend(td, (struct kmq_timedsend_args *) args);
1016 #else
1017         return (ENOSYS);
1018 #endif
1019 }
1020
1021 int
1022 linux_mq_timedreceive(struct thread *td, struct linux_mq_timedreceive_args *args)
1023 {
1024 #ifdef P1003_1B_MQUEUE
1025         return sys_kmq_timedreceive(td, (struct kmq_timedreceive_args *) args);
1026 #else
1027         return (ENOSYS);
1028 #endif
1029 }
1030
1031 int
1032 linux_mq_notify(struct thread *td, struct linux_mq_notify_args *args)
1033 {
1034 #ifdef P1003_1B_MQUEUE
1035         return sys_kmq_notify(td, (struct kmq_notify_args *) args);
1036 #else
1037         return (ENOSYS);
1038 #endif
1039 }
1040
1041 int
1042 linux_mq_getsetattr(struct thread *td, struct linux_mq_getsetattr_args *args)
1043 {
1044 #ifdef P1003_1B_MQUEUE
1045         return sys_kmq_setattr(td, (struct kmq_setattr_args *) args);
1046 #else
1047         return (ENOSYS);
1048 #endif
1049 }
1050
1051 int
1052 linux_wait4(struct thread *td, struct linux_wait4_args *args)
1053 {
1054         int error, options;
1055         struct rusage ru, *rup;
1056
1057 #ifdef DEBUG
1058         if (ldebug(wait4))
1059                 printf(ARGS(wait4, "%d, %p, %d, %p"),
1060                     args->pid, (void *)args->status, args->options,
1061                     (void *)args->rusage);
1062 #endif
1063
1064         options = (args->options & (WNOHANG | WUNTRACED));
1065         /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
1066         if (args->options & __WCLONE)
1067                 options |= WLINUXCLONE;
1068
1069         if (args->rusage != NULL)
1070                 rup = &ru;
1071         else
1072                 rup = NULL;
1073         error = linux_common_wait(td, args->pid, args->status, options, rup);
1074         if (error)
1075                 return (error);
1076         if (args->rusage != NULL)
1077                 error = copyout(&ru, args->rusage, sizeof(ru));
1078
1079         return (error);
1080 }