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