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: releng/7.3/sys/i386/linux/linux_machdep.c 198926 2009-11-04 20:53:35Z jhb $");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/file.h>
35 #include <sys/fcntl.h>
36 #include <sys/imgact.h>
37 #include <sys/lock.h>
38 #include <sys/malloc.h>
39 #include <sys/mman.h>
40 #include <sys/mutex.h>
41 #include <sys/sx.h>
42 #include <sys/priv.h>
43 #include <sys/proc.h>
44 #include <sys/queue.h>
45 #include <sys/resource.h>
46 #include <sys/resourcevar.h>
47 #include <sys/signalvar.h>
48 #include <sys/syscallsubr.h>
49 #include <sys/sysproto.h>
50 #include <sys/unistd.h>
51 #include <sys/wait.h>
52 #include <sys/sched.h>
53
54 #include <machine/frame.h>
55 #include <machine/psl.h>
56 #include <machine/segments.h>
57 #include <machine/sysarch.h>
58
59 #include <vm/vm.h>
60 #include <vm/pmap.h>
61 #include <vm/vm_map.h>
62
63 #include <i386/linux/linux.h>
64 #include <i386/linux/linux_proto.h>
65 #include <compat/linux/linux_ipc.h>
66 #include <compat/linux/linux_signal.h>
67 #include <compat/linux/linux_util.h>
68 #include <compat/linux/linux_emul.h>
69
70 #include <i386/include/pcb.h> /* needed for pcb definition in linux_set_thread_area */
71
72 #include "opt_posix.h"
73
74 extern struct sysentvec elf32_freebsd_sysvec; /* defined in i386/i386/elf_machdep.c */
75
76 struct l_descriptor {
77 l_uint entry_number;
78 l_ulong base_addr;
79 l_uint limit;
80 l_uint seg_32bit:1;
81 l_uint contents:2;
82 l_uint read_exec_only:1;
83 l_uint limit_in_pages:1;
84 l_uint seg_not_present:1;
85 l_uint useable:1;
86 };
87
88 struct l_old_select_argv {
89 l_int nfds;
90 l_fd_set *readfds;
91 l_fd_set *writefds;
92 l_fd_set *exceptfds;
93 struct l_timeval *timeout;
94 };
95
96 static int linux_mmap_common(struct thread *td, l_uintptr_t addr,
97 l_size_t len, l_int prot, l_int flags, l_int fd,
98 l_loff_t pos);
99
100 int
101 linux_to_bsd_sigaltstack(int lsa)
102 {
103 int bsa = 0;
104
105 if (lsa & LINUX_SS_DISABLE)
106 bsa |= SS_DISABLE;
107 if (lsa & LINUX_SS_ONSTACK)
108 bsa |= SS_ONSTACK;
109 return (bsa);
110 }
111
112 int
113 bsd_to_linux_sigaltstack(int bsa)
114 {
115 int lsa = 0;
116
117 if (bsa & SS_DISABLE)
118 lsa |= LINUX_SS_DISABLE;
119 if (bsa & SS_ONSTACK)
120 lsa |= LINUX_SS_ONSTACK;
121 return (lsa);
122 }
123
124 int
125 linux_execve(struct thread *td, struct linux_execve_args *args)
126 {
127 int error;
128 char *newpath;
129 struct image_args eargs;
130
131 LCONVPATHEXIST(td, args->path, &newpath);
132
133 #ifdef DEBUG
134 if (ldebug(execve))
135 printf(ARGS(execve, "%s"), newpath);
136 #endif
137
138 error = exec_copyin_args(&eargs, newpath, UIO_SYSSPACE,
139 args->argp, args->envp);
140 free(newpath, M_TEMP);
141 if (error == 0)
142 error = kern_execve(td, &eargs, NULL);
143 if (error == 0)
144 /* linux process can exec fbsd one, dont attempt
145 * to create emuldata for such process using
146 * linux_proc_init, this leads to a panic on KASSERT
147 * because such process has p->p_emuldata == NULL
148 */
149 if (td->td_proc->p_sysent == &elf_linux_sysvec)
150 error = linux_proc_init(td, 0, 0);
151 return (error);
152 }
153
154 struct l_ipc_kludge {
155 struct l_msgbuf *msgp;
156 l_long msgtyp;
157 };
158
159 int
160 linux_ipc(struct thread *td, struct linux_ipc_args *args)
161 {
162
163 switch (args->what & 0xFFFF) {
164 case LINUX_SEMOP: {
165 struct linux_semop_args a;
166
167 a.semid = args->arg1;
168 a.tsops = args->ptr;
169 a.nsops = args->arg2;
170 return (linux_semop(td, &a));
171 }
172 case LINUX_SEMGET: {
173 struct linux_semget_args a;
174
175 a.key = args->arg1;
176 a.nsems = args->arg2;
177 a.semflg = args->arg3;
178 return (linux_semget(td, &a));
179 }
180 case LINUX_SEMCTL: {
181 struct linux_semctl_args a;
182 int error;
183
184 a.semid = args->arg1;
185 a.semnum = args->arg2;
186 a.cmd = args->arg3;
187 error = copyin(args->ptr, &a.arg, sizeof(a.arg));
188 if (error)
189 return (error);
190 return (linux_semctl(td, &a));
191 }
192 case LINUX_MSGSND: {
193 struct linux_msgsnd_args a;
194
195 a.msqid = args->arg1;
196 a.msgp = args->ptr;
197 a.msgsz = args->arg2;
198 a.msgflg = args->arg3;
199 return (linux_msgsnd(td, &a));
200 }
201 case LINUX_MSGRCV: {
202 struct linux_msgrcv_args a;
203
204 a.msqid = args->arg1;
205 a.msgsz = args->arg2;
206 a.msgflg = args->arg3;
207 if ((args->what >> 16) == 0) {
208 struct l_ipc_kludge tmp;
209 int error;
210
211 if (args->ptr == NULL)
212 return (EINVAL);
213 error = copyin(args->ptr, &tmp, sizeof(tmp));
214 if (error)
215 return (error);
216 a.msgp = tmp.msgp;
217 a.msgtyp = tmp.msgtyp;
218 } else {
219 a.msgp = args->ptr;
220 a.msgtyp = args->arg5;
221 }
222 return (linux_msgrcv(td, &a));
223 }
224 case LINUX_MSGGET: {
225 struct linux_msgget_args a;
226
227 a.key = args->arg1;
228 a.msgflg = args->arg2;
229 return (linux_msgget(td, &a));
230 }
231 case LINUX_MSGCTL: {
232 struct linux_msgctl_args a;
233
234 a.msqid = args->arg1;
235 a.cmd = args->arg2;
236 a.buf = args->ptr;
237 return (linux_msgctl(td, &a));
238 }
239 case LINUX_SHMAT: {
240 struct linux_shmat_args a;
241
242 a.shmid = args->arg1;
243 a.shmaddr = args->ptr;
244 a.shmflg = args->arg2;
245 a.raddr = (l_ulong *)args->arg3;
246 return (linux_shmat(td, &a));
247 }
248 case LINUX_SHMDT: {
249 struct linux_shmdt_args a;
250
251 a.shmaddr = args->ptr;
252 return (linux_shmdt(td, &a));
253 }
254 case LINUX_SHMGET: {
255 struct linux_shmget_args a;
256
257 a.key = args->arg1;
258 a.size = args->arg2;
259 a.shmflg = args->arg3;
260 return (linux_shmget(td, &a));
261 }
262 case LINUX_SHMCTL: {
263 struct linux_shmctl_args a;
264
265 a.shmid = args->arg1;
266 a.cmd = args->arg2;
267 a.buf = args->ptr;
268 return (linux_shmctl(td, &a));
269 }
270 default:
271 break;
272 }
273
274 return (EINVAL);
275 }
276
277 int
278 linux_old_select(struct thread *td, struct linux_old_select_args *args)
279 {
280 struct l_old_select_argv linux_args;
281 struct linux_select_args newsel;
282 int error;
283
284 #ifdef DEBUG
285 if (ldebug(old_select))
286 printf(ARGS(old_select, "%p"), args->ptr);
287 #endif
288
289 error = copyin(args->ptr, &linux_args, sizeof(linux_args));
290 if (error)
291 return (error);
292
293 newsel.nfds = linux_args.nfds;
294 newsel.readfds = linux_args.readfds;
295 newsel.writefds = linux_args.writefds;
296 newsel.exceptfds = linux_args.exceptfds;
297 newsel.timeout = linux_args.timeout;
298 return (linux_select(td, &newsel));
299 }
300
301 int
302 linux_fork(struct thread *td, struct linux_fork_args *args)
303 {
304 int error;
305 struct proc *p2;
306 struct thread *td2;
307
308 #ifdef DEBUG
309 if (ldebug(fork))
310 printf(ARGS(fork, ""));
311 #endif
312
313 if ((error = fork1(td, RFFDG | RFPROC | RFSTOPPED, 0, &p2)) != 0)
314 return (error);
315
316 if (error == 0) {
317 td->td_retval[0] = p2->p_pid;
318 td->td_retval[1] = 0;
319 }
320
321 if (td->td_retval[1] == 1)
322 td->td_retval[0] = 0;
323 error = linux_proc_init(td, td->td_retval[0], 0);
324 if (error)
325 return (error);
326
327 td2 = FIRST_THREAD_IN_PROC(p2);
328
329 /*
330 * Make this runnable after we are finished with it.
331 */
332 thread_lock(td2);
333 TD_SET_CAN_RUN(td2);
334 sched_add(td2, SRQ_BORING);
335 thread_unlock(td2);
336
337 return (0);
338 }
339
340 int
341 linux_vfork(struct thread *td, struct linux_vfork_args *args)
342 {
343 int error;
344 struct proc *p2;
345 struct thread *td2;
346
347 #ifdef DEBUG
348 if (ldebug(vfork))
349 printf(ARGS(vfork, ""));
350 #endif
351
352 /* exclude RFPPWAIT */
353 if ((error = fork1(td, RFFDG | RFPROC | RFMEM | RFSTOPPED, 0, &p2)) != 0)
354 return (error);
355 if (error == 0) {
356 td->td_retval[0] = p2->p_pid;
357 td->td_retval[1] = 0;
358 }
359 /* Are we the child? */
360 if (td->td_retval[1] == 1)
361 td->td_retval[0] = 0;
362 error = linux_proc_init(td, td->td_retval[0], 0);
363 if (error)
364 return (error);
365
366 PROC_LOCK(p2);
367 p2->p_flag |= P_PPWAIT;
368 PROC_UNLOCK(p2);
369
370 td2 = FIRST_THREAD_IN_PROC(p2);
371
372 /*
373 * Make this runnable after we are finished with it.
374 */
375 thread_lock(td2);
376 TD_SET_CAN_RUN(td2);
377 sched_add(td2, SRQ_BORING);
378 thread_unlock(td2);
379
380 /* wait for the children to exit, ie. emulate vfork */
381 PROC_LOCK(p2);
382 while (p2->p_flag & P_PPWAIT)
383 cv_wait(&p2->p_pwait, &p2->p_mtx);
384 PROC_UNLOCK(p2);
385
386 return (0);
387 }
388
389 int
390 linux_clone(struct thread *td, struct linux_clone_args *args)
391 {
392 int error, ff = RFPROC | RFSTOPPED;
393 struct proc *p2;
394 struct thread *td2;
395 int exit_signal;
396 struct linux_emuldata *em;
397
398 #ifdef DEBUG
399 if (ldebug(clone)) {
400 printf(ARGS(clone, "flags %x, stack %x, parent tid: %x, child tid: %x"),
401 (unsigned int)args->flags, (unsigned int)args->stack,
402 (unsigned int)args->parent_tidptr, (unsigned int)args->child_tidptr);
403 }
404 #endif
405
406 exit_signal = args->flags & 0x000000ff;
407 if (LINUX_SIG_VALID(exit_signal)) {
408 if (exit_signal <= LINUX_SIGTBLSZ)
409 exit_signal =
410 linux_to_bsd_signal[_SIG_IDX(exit_signal)];
411 } else if (exit_signal != 0)
412 return (EINVAL);
413
414 if (args->flags & LINUX_CLONE_VM)
415 ff |= RFMEM;
416 if (args->flags & LINUX_CLONE_SIGHAND)
417 ff |= RFSIGSHARE;
418 /*
419 * XXX: in linux sharing of fs info (chroot/cwd/umask)
420 * and open files is independant. in fbsd its in one
421 * structure but in reality it doesn't cause any problems
422 * because both of these flags are usually set together.
423 */
424 if (!(args->flags & (LINUX_CLONE_FILES | LINUX_CLONE_FS)))
425 ff |= RFFDG;
426
427 /*
428 * Attempt to detect when linux_clone(2) is used for creating
429 * kernel threads. Unfortunately despite the existence of the
430 * CLONE_THREAD flag, version of linuxthreads package used in
431 * most popular distros as of beginning of 2005 doesn't make
432 * any use of it. Therefore, this detection relies on
433 * empirical observation that linuxthreads sets certain
434 * combination of flags, so that we can make more or less
435 * precise detection and notify the FreeBSD kernel that several
436 * processes are in fact part of the same threading group, so
437 * that special treatment is necessary for signal delivery
438 * between those processes and fd locking.
439 */
440 if ((args->flags & 0xffffff00) == LINUX_THREADING_FLAGS)
441 ff |= RFTHREAD;
442
443 if (args->flags & LINUX_CLONE_PARENT_SETTID)
444 if (args->parent_tidptr == NULL)
445 return (EINVAL);
446
447 error = fork1(td, ff, 0, &p2);
448 if (error)
449 return (error);
450
451 if (args->flags & (LINUX_CLONE_PARENT | LINUX_CLONE_THREAD)) {
452 sx_xlock(&proctree_lock);
453 PROC_LOCK(p2);
454 proc_reparent(p2, td->td_proc->p_pptr);
455 PROC_UNLOCK(p2);
456 sx_xunlock(&proctree_lock);
457 }
458
459 /* create the emuldata */
460 error = linux_proc_init(td, p2->p_pid, args->flags);
461 /* reference it - no need to check this */
462 em = em_find(p2, EMUL_DOLOCK);
463 KASSERT(em != NULL, ("clone: emuldata not found.\n"));
464 /* and adjust it */
465
466 if (args->flags & LINUX_CLONE_THREAD) {
467 /* XXX: linux mangles pgrp and pptr somehow
468 * I think it might be this but I am not sure.
469 */
470 #ifdef notyet
471 PROC_LOCK(p2);
472 p2->p_pgrp = td->td_proc->p_pgrp;
473 PROC_UNLOCK(p2);
474 #endif
475 exit_signal = 0;
476 }
477
478 if (args->flags & LINUX_CLONE_CHILD_SETTID)
479 em->child_set_tid = args->child_tidptr;
480 else
481 em->child_set_tid = NULL;
482
483 if (args->flags & LINUX_CLONE_CHILD_CLEARTID)
484 em->child_clear_tid = args->child_tidptr;
485 else
486 em->child_clear_tid = NULL;
487
488 EMUL_UNLOCK(&emul_lock);
489
490 if (args->flags & LINUX_CLONE_PARENT_SETTID) {
491 error = copyout(&p2->p_pid, args->parent_tidptr, sizeof(p2->p_pid));
492 if (error)
493 printf(LMSG("copyout failed!"));
494 }
495
496 PROC_LOCK(p2);
497 p2->p_sigparent = exit_signal;
498 PROC_UNLOCK(p2);
499 td2 = FIRST_THREAD_IN_PROC(p2);
500 /*
501 * in a case of stack = NULL we are supposed to COW calling process stack
502 * this is what normal fork() does so we just keep the tf_esp arg intact
503 */
504 if (args->stack)
505 td2->td_frame->tf_esp = (unsigned int)args->stack;
506
507 if (args->flags & LINUX_CLONE_SETTLS) {
508 struct l_user_desc info;
509 int idx;
510 int a[2];
511 struct segment_descriptor sd;
512
513 error = copyin((void *)td->td_frame->tf_esi, &info, sizeof(struct l_user_desc));
514 if (error) {
515 printf(LMSG("copyin failed!"));
516 } else {
517
518 idx = info.entry_number;
519
520 /*
521 * looks like we're getting the idx we returned
522 * in the set_thread_area() syscall
523 */
524 if (idx != 6 && idx != 3) {
525 printf(LMSG("resetting idx!"));
526 idx = 3;
527 }
528
529 /* this doesnt happen in practice */
530 if (idx == 6) {
531 /* we might copy out the entry_number as 3 */
532 info.entry_number = 3;
533 error = copyout(&info, (void *) td->td_frame->tf_esi, sizeof(struct l_user_desc));
534 if (error)
535 printf(LMSG("copyout failed!"));
536 }
537
538 a[0] = LINUX_LDT_entry_a(&info);
539 a[1] = LINUX_LDT_entry_b(&info);
540
541 memcpy(&sd, &a, sizeof(a));
542 #ifdef DEBUG
543 if (ldebug(clone))
544 printf("Segment created in clone with CLONE_SETTLS: lobase: %x, hibase: %x, lolimit: %x, hilimit: %x, type: %i, dpl: %i, p: %i, xx: %i, def32: %i, gran: %i\n", sd.sd_lobase,
545 sd.sd_hibase,
546 sd.sd_lolimit,
547 sd.sd_hilimit,
548 sd.sd_type,
549 sd.sd_dpl,
550 sd.sd_p,
551 sd.sd_xx,
552 sd.sd_def32,
553 sd.sd_gran);
554 #endif
555
556 /* set %gs */
557 td2->td_pcb->pcb_gsd = sd;
558 td2->td_pcb->pcb_gs = GSEL(GUGS_SEL, SEL_UPL);
559 }
560 }
561
562 #ifdef DEBUG
563 if (ldebug(clone))
564 printf(LMSG("clone: successful rfork to %ld, stack %p sig = %d"),
565 (long)p2->p_pid, args->stack, exit_signal);
566 #endif
567 if (args->flags & LINUX_CLONE_VFORK) {
568 PROC_LOCK(p2);
569 p2->p_flag |= P_PPWAIT;
570 PROC_UNLOCK(p2);
571 }
572
573 /*
574 * Make this runnable after we are finished with it.
575 */
576 thread_lock(td2);
577 TD_SET_CAN_RUN(td2);
578 sched_add(td2, SRQ_BORING);
579 thread_unlock(td2);
580
581 td->td_retval[0] = p2->p_pid;
582 td->td_retval[1] = 0;
583
584 if (args->flags & LINUX_CLONE_VFORK) {
585 /* wait for the children to exit, ie. emulate vfork */
586 PROC_LOCK(p2);
587 while (p2->p_flag & P_PPWAIT)
588 cv_wait(&p2->p_pwait, &p2->p_mtx);
589 PROC_UNLOCK(p2);
590 }
591
592 return (0);
593 }
594
595 #define STACK_SIZE (2 * 1024 * 1024)
596 #define GUARD_SIZE (4 * PAGE_SIZE)
597
598 int
599 linux_mmap2(struct thread *td, struct linux_mmap2_args *args)
600 {
601
602 #ifdef DEBUG
603 if (ldebug(mmap2))
604 printf(ARGS(mmap2, "%p, %d, %d, 0x%08x, %d, %d"),
605 (void *)args->addr, args->len, args->prot,
606 args->flags, args->fd, args->pgoff);
607 #endif
608
609 return (linux_mmap_common(td, args->addr, args->len, args->prot,
610 args->flags, args->fd, (uint64_t)(uint32_t)args->pgoff *
611 PAGE_SIZE));
612 }
613
614 int
615 linux_mmap(struct thread *td, struct linux_mmap_args *args)
616 {
617 int error;
618 struct l_mmap_argv linux_args;
619
620 error = copyin(args->ptr, &linux_args, sizeof(linux_args));
621 if (error)
622 return (error);
623
624 #ifdef DEBUG
625 if (ldebug(mmap))
626 printf(ARGS(mmap, "%p, %d, %d, 0x%08x, %d, %d"),
627 (void *)linux_args.addr, linux_args.len, linux_args.prot,
628 linux_args.flags, linux_args.fd, linux_args.pgoff);
629 #endif
630
631 return (linux_mmap_common(td, linux_args.addr, linux_args.len,
632 linux_args.prot, linux_args.flags, linux_args.fd,
633 (uint32_t)linux_args.pgoff));
634 }
635
636 static int
637 linux_mmap_common(struct thread *td, l_uintptr_t addr, l_size_t len, l_int prot,
638 l_int flags, l_int fd, l_loff_t pos)
639 {
640 struct proc *p = td->td_proc;
641 struct mmap_args /* {
642 caddr_t addr;
643 size_t len;
644 int prot;
645 int flags;
646 int fd;
647 long pad;
648 off_t pos;
649 } */ bsd_args;
650 int error;
651 struct file *fp;
652
653 error = 0;
654 bsd_args.flags = 0;
655 fp = NULL;
656
657 /*
658 * Linux mmap(2):
659 * You must specify exactly one of MAP_SHARED and MAP_PRIVATE
660 */
661 if (!((flags & LINUX_MAP_SHARED) ^ (flags & LINUX_MAP_PRIVATE)))
662 return (EINVAL);
663
664 if (flags & LINUX_MAP_SHARED)
665 bsd_args.flags |= MAP_SHARED;
666 if (flags & LINUX_MAP_PRIVATE)
667 bsd_args.flags |= MAP_PRIVATE;
668 if (flags & LINUX_MAP_FIXED)
669 bsd_args.flags |= MAP_FIXED;
670 if (flags & LINUX_MAP_ANON)
671 bsd_args.flags |= MAP_ANON;
672 else
673 bsd_args.flags |= MAP_NOSYNC;
674 if (flags & LINUX_MAP_GROWSDOWN)
675 bsd_args.flags |= MAP_STACK;
676
677 /*
678 * PROT_READ, PROT_WRITE, or PROT_EXEC implies PROT_READ and PROT_EXEC
679 * on Linux/i386. We do this to ensure maximum compatibility.
680 * Linux/ia64 does the same in i386 emulation mode.
681 */
682 bsd_args.prot = prot;
683 if (bsd_args.prot & (PROT_READ | PROT_WRITE | PROT_EXEC))
684 bsd_args.prot |= PROT_READ | PROT_EXEC;
685
686 /* Linux does not check file descriptor when MAP_ANONYMOUS is set. */
687 bsd_args.fd = (bsd_args.flags & MAP_ANON) ? -1 : fd;
688 if (bsd_args.fd != -1) {
689 /*
690 * Linux follows Solaris mmap(2) description:
691 * The file descriptor fildes is opened with
692 * read permission, regardless of the
693 * protection options specified.
694 */
695
696 if ((error = fget(td, bsd_args.fd, &fp)) != 0)
697 return (error);
698 if (fp->f_type != DTYPE_VNODE) {
699 fdrop(fp, td);
700 return (EINVAL);
701 }
702
703 /* Linux mmap() just fails for O_WRONLY files */
704 if (!(fp->f_flag & FREAD)) {
705 fdrop(fp, td);
706 return (EACCES);
707 }
708
709 fdrop(fp, td);
710 }
711
712 if (flags & LINUX_MAP_GROWSDOWN) {
713 /*
714 * The Linux MAP_GROWSDOWN option does not limit auto
715 * growth of the region. Linux mmap with this option
716 * takes as addr the inital BOS, and as len, the initial
717 * region size. It can then grow down from addr without
718 * limit. However, linux threads has an implicit internal
719 * limit to stack size of STACK_SIZE. Its just not
720 * enforced explicitly in linux. But, here we impose
721 * a limit of (STACK_SIZE - GUARD_SIZE) on the stack
722 * region, since we can do this with our mmap.
723 *
724 * Our mmap with MAP_STACK takes addr as the maximum
725 * downsize limit on BOS, and as len the max size of
726 * the region. It them maps the top SGROWSIZ bytes,
727 * and auto grows the region down, up to the limit
728 * in addr.
729 *
730 * If we don't use the MAP_STACK option, the effect
731 * of this code is to allocate a stack region of a
732 * fixed size of (STACK_SIZE - GUARD_SIZE).
733 */
734
735 if ((caddr_t)PTRIN(addr) + len > p->p_vmspace->vm_maxsaddr) {
736 /*
737 * Some linux apps will attempt to mmap
738 * thread stacks near the top of their
739 * address space. If their TOS is greater
740 * than vm_maxsaddr, vm_map_growstack()
741 * will confuse the thread stack with the
742 * process stack and deliver a SEGV if they
743 * attempt to grow the thread stack past their
744 * current stacksize rlimit. To avoid this,
745 * adjust vm_maxsaddr upwards to reflect
746 * the current stacksize rlimit rather
747 * than the maximum possible stacksize.
748 * It would be better to adjust the
749 * mmap'ed region, but some apps do not check
750 * mmap's return value.
751 */
752 PROC_LOCK(p);
753 p->p_vmspace->vm_maxsaddr = (char *)USRSTACK -
754 lim_cur(p, RLIMIT_STACK);
755 PROC_UNLOCK(p);
756 }
757
758 /*
759 * This gives us our maximum stack size and a new BOS.
760 * If we're using VM_STACK, then mmap will just map
761 * the top SGROWSIZ bytes, and let the stack grow down
762 * to the limit at BOS. If we're not using VM_STACK
763 * we map the full stack, since we don't have a way
764 * to autogrow it.
765 */
766 if (len > STACK_SIZE - GUARD_SIZE) {
767 bsd_args.addr = (caddr_t)PTRIN(addr);
768 bsd_args.len = len;
769 } else {
770 bsd_args.addr = (caddr_t)PTRIN(addr) -
771 (STACK_SIZE - GUARD_SIZE - len);
772 bsd_args.len = STACK_SIZE - GUARD_SIZE;
773 }
774 } else {
775 bsd_args.addr = (caddr_t)PTRIN(addr);
776 bsd_args.len = len;
777 }
778 bsd_args.pos = pos;
779
780 #ifdef DEBUG
781 if (ldebug(mmap))
782 printf("-> %s(%p, %d, %d, 0x%08x, %d, 0x%x)\n",
783 __func__,
784 (void *)bsd_args.addr, bsd_args.len, bsd_args.prot,
785 bsd_args.flags, bsd_args.fd, (int)bsd_args.pos);
786 #endif
787 error = mmap(td, &bsd_args);
788 #ifdef DEBUG
789 if (ldebug(mmap))
790 printf("-> %s() return: 0x%x (0x%08x)\n",
791 __func__, error, (u_int)td->td_retval[0]);
792 #endif
793 return (error);
794 }
795
796 int
797 linux_mprotect(struct thread *td, struct linux_mprotect_args *uap)
798 {
799 struct mprotect_args bsd_args;
800
801 bsd_args.addr = uap->addr;
802 bsd_args.len = uap->len;
803 bsd_args.prot = uap->prot;
804 if (bsd_args.prot & (PROT_READ | PROT_WRITE | PROT_EXEC))
805 bsd_args.prot |= PROT_READ | PROT_EXEC;
806 return (mprotect(td, &bsd_args));
807 }
808
809 int
810 linux_pipe(struct thread *td, struct linux_pipe_args *args)
811 {
812 int error;
813 int reg_edx;
814
815 #ifdef DEBUG
816 if (ldebug(pipe))
817 printf(ARGS(pipe, "*"));
818 #endif
819
820 reg_edx = td->td_retval[1];
821 error = pipe(td, 0);
822 if (error) {
823 td->td_retval[1] = reg_edx;
824 return (error);
825 }
826
827 error = copyout(td->td_retval, args->pipefds, 2*sizeof(int));
828 if (error) {
829 td->td_retval[1] = reg_edx;
830 return (error);
831 }
832
833 td->td_retval[1] = reg_edx;
834 td->td_retval[0] = 0;
835 return (0);
836 }
837
838 int
839 linux_ioperm(struct thread *td, struct linux_ioperm_args *args)
840 {
841 int error;
842 struct i386_ioperm_args iia;
843
844 iia.start = args->start;
845 iia.length = args->length;
846 iia.enable = args->enable;
847 error = i386_set_ioperm(td, &iia);
848 return (error);
849 }
850
851 int
852 linux_iopl(struct thread *td, struct linux_iopl_args *args)
853 {
854 int error;
855
856 if (args->level < 0 || args->level > 3)
857 return (EINVAL);
858 if ((error = priv_check(td, PRIV_IO)) != 0)
859 return (error);
860 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
861 return (error);
862 td->td_frame->tf_eflags = (td->td_frame->tf_eflags & ~PSL_IOPL) |
863 (args->level * (PSL_IOPL / 3));
864 return (0);
865 }
866
867 int
868 linux_modify_ldt(struct thread *td, struct linux_modify_ldt_args *uap)
869 {
870 int error;
871 struct i386_ldt_args ldt;
872 struct l_descriptor ld;
873 union descriptor desc;
874 int size, written;
875
876 if (uap->ptr == NULL)
877 return (EINVAL);
878
879 switch (uap->func) {
880 case 0x00: /* read_ldt */
881 ldt.start = 0;
882 ldt.descs = uap->ptr;
883 ldt.num = uap->bytecount / sizeof(union descriptor);
884 error = i386_get_ldt(td, &ldt);
885 td->td_retval[0] *= sizeof(union descriptor);
886 break;
887 case 0x02: /* read_default_ldt = 0 */
888 size = 5*sizeof(struct l_desc_struct);
889 if (size > uap->bytecount)
890 size = uap->bytecount;
891 for (written = error = 0; written < size && error == 0; written++)
892 error = subyte((char *)uap->ptr + written, 0);
893 td->td_retval[0] = written;
894 break;
895 case 0x01: /* write_ldt */
896 case 0x11: /* write_ldt */
897 if (uap->bytecount != sizeof(ld))
898 return (EINVAL);
899
900 error = copyin(uap->ptr, &ld, sizeof(ld));
901 if (error)
902 return (error);
903
904 ldt.start = ld.entry_number;
905 ldt.descs = &desc;
906 ldt.num = 1;
907 desc.sd.sd_lolimit = (ld.limit & 0x0000ffff);
908 desc.sd.sd_hilimit = (ld.limit & 0x000f0000) >> 16;
909 desc.sd.sd_lobase = (ld.base_addr & 0x00ffffff);
910 desc.sd.sd_hibase = (ld.base_addr & 0xff000000) >> 24;
911 desc.sd.sd_type = SDT_MEMRO | ((ld.read_exec_only ^ 1) << 1) |
912 (ld.contents << 2);
913 desc.sd.sd_dpl = 3;
914 desc.sd.sd_p = (ld.seg_not_present ^ 1);
915 desc.sd.sd_xx = 0;
916 desc.sd.sd_def32 = ld.seg_32bit;
917 desc.sd.sd_gran = ld.limit_in_pages;
918 error = i386_set_ldt(td, &ldt, &desc);
919 break;
920 default:
921 error = EINVAL;
922 break;
923 }
924
925 if (error == EOPNOTSUPP) {
926 printf("linux: modify_ldt needs kernel option USER_LDT\n");
927 error = ENOSYS;
928 }
929
930 return (error);
931 }
932
933 int
934 linux_sigaction(struct thread *td, struct linux_sigaction_args *args)
935 {
936 l_osigaction_t osa;
937 l_sigaction_t act, oact;
938 int error;
939
940 #ifdef DEBUG
941 if (ldebug(sigaction))
942 printf(ARGS(sigaction, "%d, %p, %p"),
943 args->sig, (void *)args->nsa, (void *)args->osa);
944 #endif
945
946 if (args->nsa != NULL) {
947 error = copyin(args->nsa, &osa, sizeof(l_osigaction_t));
948 if (error)
949 return (error);
950 act.lsa_handler = osa.lsa_handler;
951 act.lsa_flags = osa.lsa_flags;
952 act.lsa_restorer = osa.lsa_restorer;
953 LINUX_SIGEMPTYSET(act.lsa_mask);
954 act.lsa_mask.__bits[0] = osa.lsa_mask;
955 }
956
957 error = linux_do_sigaction(td, args->sig, args->nsa ? &act : NULL,
958 args->osa ? &oact : NULL);
959
960 if (args->osa != NULL && !error) {
961 osa.lsa_handler = oact.lsa_handler;
962 osa.lsa_flags = oact.lsa_flags;
963 osa.lsa_restorer = oact.lsa_restorer;
964 osa.lsa_mask = oact.lsa_mask.__bits[0];
965 error = copyout(&osa, args->osa, sizeof(l_osigaction_t));
966 }
967
968 return (error);
969 }
970
971 /*
972 * Linux has two extra args, restart and oldmask. We dont use these,
973 * but it seems that "restart" is actually a context pointer that
974 * enables the signal to happen with a different register set.
975 */
976 int
977 linux_sigsuspend(struct thread *td, struct linux_sigsuspend_args *args)
978 {
979 sigset_t sigmask;
980 l_sigset_t mask;
981
982 #ifdef DEBUG
983 if (ldebug(sigsuspend))
984 printf(ARGS(sigsuspend, "%08lx"), (unsigned long)args->mask);
985 #endif
986
987 LINUX_SIGEMPTYSET(mask);
988 mask.__bits[0] = args->mask;
989 linux_to_bsd_sigset(&mask, &sigmask);
990 return (kern_sigsuspend(td, sigmask));
991 }
992
993 int
994 linux_rt_sigsuspend(struct thread *td, struct linux_rt_sigsuspend_args *uap)
995 {
996 l_sigset_t lmask;
997 sigset_t sigmask;
998 int error;
999
1000 #ifdef DEBUG
1001 if (ldebug(rt_sigsuspend))
1002 printf(ARGS(rt_sigsuspend, "%p, %d"),
1003 (void *)uap->newset, uap->sigsetsize);
1004 #endif
1005
1006 if (uap->sigsetsize != sizeof(l_sigset_t))
1007 return (EINVAL);
1008
1009 error = copyin(uap->newset, &lmask, sizeof(l_sigset_t));
1010 if (error)
1011 return (error);
1012
1013 linux_to_bsd_sigset(&lmask, &sigmask);
1014 return (kern_sigsuspend(td, sigmask));
1015 }
1016
1017 int
1018 linux_pause(struct thread *td, struct linux_pause_args *args)
1019 {
1020 struct proc *p = td->td_proc;
1021 sigset_t sigmask;
1022
1023 #ifdef DEBUG
1024 if (ldebug(pause))
1025 printf(ARGS(pause, ""));
1026 #endif
1027
1028 PROC_LOCK(p);
1029 sigmask = td->td_sigmask;
1030 PROC_UNLOCK(p);
1031 return (kern_sigsuspend(td, sigmask));
1032 }
1033
1034 int
1035 linux_sigaltstack(struct thread *td, struct linux_sigaltstack_args *uap)
1036 {
1037 stack_t ss, oss;
1038 l_stack_t lss;
1039 int error;
1040
1041 #ifdef DEBUG
1042 if (ldebug(sigaltstack))
1043 printf(ARGS(sigaltstack, "%p, %p"), uap->uss, uap->uoss);
1044 #endif
1045
1046 if (uap->uss != NULL) {
1047 error = copyin(uap->uss, &lss, sizeof(l_stack_t));
1048 if (error)
1049 return (error);
1050
1051 ss.ss_sp = lss.ss_sp;
1052 ss.ss_size = lss.ss_size;
1053 ss.ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags);
1054 }
1055 error = kern_sigaltstack(td, (uap->uss != NULL) ? &ss : NULL,
1056 (uap->uoss != NULL) ? &oss : NULL);
1057 if (!error && uap->uoss != NULL) {
1058 lss.ss_sp = oss.ss_sp;
1059 lss.ss_size = oss.ss_size;
1060 lss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags);
1061 error = copyout(&lss, uap->uoss, sizeof(l_stack_t));
1062 }
1063
1064 return (error);
1065 }
1066
1067 int
1068 linux_ftruncate64(struct thread *td, struct linux_ftruncate64_args *args)
1069 {
1070 struct ftruncate_args sa;
1071
1072 #ifdef DEBUG
1073 if (ldebug(ftruncate64))
1074 printf(ARGS(ftruncate64, "%u, %jd"), args->fd,
1075 (intmax_t)args->length);
1076 #endif
1077
1078 sa.fd = args->fd;
1079 sa.length = args->length;
1080 return ftruncate(td, &sa);
1081 }
1082
1083 int
1084 linux_set_thread_area(struct thread *td, struct linux_set_thread_area_args *args)
1085 {
1086 struct l_user_desc info;
1087 int error;
1088 int idx;
1089 int a[2];
1090 struct segment_descriptor sd;
1091
1092 error = copyin(args->desc, &info, sizeof(struct l_user_desc));
1093 if (error)
1094 return (error);
1095
1096 #ifdef DEBUG
1097 if (ldebug(set_thread_area))
1098 printf(ARGS(set_thread_area, "%i, %x, %x, %i, %i, %i, %i, %i, %i\n"),
1099 info.entry_number,
1100 info.base_addr,
1101 info.limit,
1102 info.seg_32bit,
1103 info.contents,
1104 info.read_exec_only,
1105 info.limit_in_pages,
1106 info.seg_not_present,
1107 info.useable);
1108 #endif
1109
1110 idx = info.entry_number;
1111 /*
1112 * Semantics of linux version: every thread in the system has array of
1113 * 3 tls descriptors. 1st is GLIBC TLS, 2nd is WINE, 3rd unknown. This
1114 * syscall loads one of the selected tls decriptors with a value and
1115 * also loads GDT descriptors 6, 7 and 8 with the content of the
1116 * per-thread descriptors.
1117 *
1118 * Semantics of fbsd version: I think we can ignore that linux has 3
1119 * per-thread descriptors and use just the 1st one. The tls_array[]
1120 * is used only in set/get-thread_area() syscalls and for loading the
1121 * GDT descriptors. In fbsd we use just one GDT descriptor for TLS so
1122 * we will load just one.
1123 *
1124 * XXX: this doesn't work when a user space process tries to use more
1125 * than 1 TLS segment. Comment in the linux sources says wine might do
1126 * this.
1127 */
1128
1129 /*
1130 * we support just GLIBC TLS now
1131 * we should let 3 proceed as well because we use this segment so
1132 * if code does two subsequent calls it should succeed
1133 */
1134 if (idx != 6 && idx != -1 && idx != 3)
1135 return (EINVAL);
1136
1137 /*
1138 * we have to copy out the GDT entry we use
1139 * FreeBSD uses GDT entry #3 for storing %gs so load that
1140 *
1141 * XXX: what if a user space program doesn't check this value and tries
1142 * to use 6, 7 or 8?
1143 */
1144 idx = info.entry_number = 3;
1145 error = copyout(&info, args->desc, sizeof(struct l_user_desc));
1146 if (error)
1147 return (error);
1148
1149 if (LINUX_LDT_empty(&info)) {
1150 a[0] = 0;
1151 a[1] = 0;
1152 } else {
1153 a[0] = LINUX_LDT_entry_a(&info);
1154 a[1] = LINUX_LDT_entry_b(&info);
1155 }
1156
1157 memcpy(&sd, &a, sizeof(a));
1158 #ifdef DEBUG
1159 if (ldebug(set_thread_area))
1160 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,
1161 sd.sd_hibase,
1162 sd.sd_lolimit,
1163 sd.sd_hilimit,
1164 sd.sd_type,
1165 sd.sd_dpl,
1166 sd.sd_p,
1167 sd.sd_xx,
1168 sd.sd_def32,
1169 sd.sd_gran);
1170 #endif
1171
1172 /* this is taken from i386 version of cpu_set_user_tls() */
1173 critical_enter();
1174 /* set %gs */
1175 td->td_pcb->pcb_gsd = sd;
1176 PCPU_GET(fsgs_gdt)[1] = sd;
1177 load_gs(GSEL(GUGS_SEL, SEL_UPL));
1178 critical_exit();
1179
1180 return (0);
1181 }
1182
1183 int
1184 linux_get_thread_area(struct thread *td, struct linux_get_thread_area_args *args)
1185 {
1186
1187 struct l_user_desc info;
1188 int error;
1189 int idx;
1190 struct l_desc_struct desc;
1191 struct segment_descriptor sd;
1192
1193 #ifdef DEBUG
1194 if (ldebug(get_thread_area))
1195 printf(ARGS(get_thread_area, "%p"), args->desc);
1196 #endif
1197
1198 error = copyin(args->desc, &info, sizeof(struct l_user_desc));
1199 if (error)
1200 return (error);
1201
1202 idx = info.entry_number;
1203 /* XXX: I am not sure if we want 3 to be allowed too. */
1204 if (idx != 6 && idx != 3)
1205 return (EINVAL);
1206
1207 idx = 3;
1208
1209 memset(&info, 0, sizeof(info));
1210
1211 sd = PCPU_GET(fsgs_gdt)[1];
1212
1213 memcpy(&desc, &sd, sizeof(desc));
1214
1215 info.entry_number = idx;
1216 info.base_addr = LINUX_GET_BASE(&desc);
1217 info.limit = LINUX_GET_LIMIT(&desc);
1218 info.seg_32bit = LINUX_GET_32BIT(&desc);
1219 info.contents = LINUX_GET_CONTENTS(&desc);
1220 info.read_exec_only = !LINUX_GET_WRITABLE(&desc);
1221 info.limit_in_pages = LINUX_GET_LIMIT_PAGES(&desc);
1222 info.seg_not_present = !LINUX_GET_PRESENT(&desc);
1223 info.useable = LINUX_GET_USEABLE(&desc);
1224
1225 error = copyout(&info, args->desc, sizeof(struct l_user_desc));
1226 if (error)
1227 return (EFAULT);
1228
1229 return (0);
1230 }
1231
1232 /* copied from kern/kern_time.c */
1233 int
1234 linux_timer_create(struct thread *td, struct linux_timer_create_args *args)
1235 {
1236 return ktimer_create(td, (struct ktimer_create_args *) args);
1237 }
1238
1239 int
1240 linux_timer_settime(struct thread *td, struct linux_timer_settime_args *args)
1241 {
1242 return ktimer_settime(td, (struct ktimer_settime_args *) args);
1243 }
1244
1245 int
1246 linux_timer_gettime(struct thread *td, struct linux_timer_gettime_args *args)
1247 {
1248 return ktimer_gettime(td, (struct ktimer_gettime_args *) args);
1249 }
1250
1251 int
1252 linux_timer_getoverrun(struct thread *td, struct linux_timer_getoverrun_args *args)
1253 {
1254 return ktimer_getoverrun(td, (struct ktimer_getoverrun_args *) args);
1255 }
1256
1257 int
1258 linux_timer_delete(struct thread *td, struct linux_timer_delete_args *args)
1259 {
1260 return ktimer_delete(td, (struct ktimer_delete_args *) args);
1261 }
1262
1263 /* XXX: this wont work with module - convert it */
1264 int
1265 linux_mq_open(struct thread *td, struct linux_mq_open_args *args)
1266 {
1267 #ifdef P1003_1B_MQUEUE
1268 return kmq_open(td, (struct kmq_open_args *) args);
1269 #else
1270 return (ENOSYS);
1271 #endif
1272 }
1273
1274 int
1275 linux_mq_unlink(struct thread *td, struct linux_mq_unlink_args *args)
1276 {
1277 #ifdef P1003_1B_MQUEUE
1278 return kmq_unlink(td, (struct kmq_unlink_args *) args);
1279 #else
1280 return (ENOSYS);
1281 #endif
1282 }
1283
1284 int
1285 linux_mq_timedsend(struct thread *td, struct linux_mq_timedsend_args *args)
1286 {
1287 #ifdef P1003_1B_MQUEUE
1288 return kmq_timedsend(td, (struct kmq_timedsend_args *) args);
1289 #else
1290 return (ENOSYS);
1291 #endif
1292 }
1293
1294 int
1295 linux_mq_timedreceive(struct thread *td, struct linux_mq_timedreceive_args *args)
1296 {
1297 #ifdef P1003_1B_MQUEUE
1298 return kmq_timedreceive(td, (struct kmq_timedreceive_args *) args);
1299 #else
1300 return (ENOSYS);
1301 #endif
1302 }
1303
1304 int
1305 linux_mq_notify(struct thread *td, struct linux_mq_notify_args *args)
1306 {
1307 #ifdef P1003_1B_MQUEUE
1308 return kmq_notify(td, (struct kmq_notify_args *) args);
1309 #else
1310 return (ENOSYS);
1311 #endif
1312 }
1313
1314 int
1315 linux_mq_getsetattr(struct thread *td, struct linux_mq_getsetattr_args *args)
1316 {
1317 #ifdef P1003_1B_MQUEUE
1318 return kmq_setattr(td, (struct kmq_setattr_args *) args);
1319 #else
1320 return (ENOSYS);
1321 #endif
1322 }
1323
Cache object: f088f9b559cc0f5196a999605a3befb5
|