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/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/proc.h>
42 #include <sys/resource.h>
43 #include <sys/resourcevar.h>
44 #include <sys/signalvar.h>
45 #include <sys/syscallsubr.h>
46 #include <sys/sysproto.h>
47 #include <sys/unistd.h>
48
49 #include <machine/frame.h>
50 #include <machine/psl.h>
51 #include <machine/segments.h>
52 #include <machine/sysarch.h>
53
54 #include <vm/vm.h>
55 #include <vm/pmap.h>
56 #include <vm/vm_map.h>
57
58 #include <i386/linux/linux.h>
59 #include <i386/linux/linux_proto.h>
60 #include <compat/linux/linux_ipc.h>
61 #include <compat/linux/linux_signal.h>
62 #include <compat/linux/linux_util.h>
63
64 struct l_descriptor {
65 l_uint entry_number;
66 l_ulong base_addr;
67 l_uint limit;
68 l_uint seg_32bit:1;
69 l_uint contents:2;
70 l_uint read_exec_only:1;
71 l_uint limit_in_pages:1;
72 l_uint seg_not_present:1;
73 l_uint useable:1;
74 };
75
76 struct l_old_select_argv {
77 l_int nfds;
78 l_fd_set *readfds;
79 l_fd_set *writefds;
80 l_fd_set *exceptfds;
81 struct l_timeval *timeout;
82 };
83
84 int
85 linux_to_bsd_sigaltstack(int lsa)
86 {
87 int bsa = 0;
88
89 if (lsa & LINUX_SS_DISABLE)
90 bsa |= SS_DISABLE;
91 if (lsa & LINUX_SS_ONSTACK)
92 bsa |= SS_ONSTACK;
93 return (bsa);
94 }
95
96 int
97 bsd_to_linux_sigaltstack(int bsa)
98 {
99 int lsa = 0;
100
101 if (bsa & SS_DISABLE)
102 lsa |= LINUX_SS_DISABLE;
103 if (bsa & SS_ONSTACK)
104 lsa |= LINUX_SS_ONSTACK;
105 return (lsa);
106 }
107
108 int
109 linux_execve(struct thread *td, struct linux_execve_args *args)
110 {
111 int error;
112 char *newpath;
113 struct image_args eargs;
114
115 LCONVPATHEXIST(td, args->path, &newpath);
116
117 #ifdef DEBUG
118 if (ldebug(execve))
119 printf(ARGS(execve, "%s"), newpath);
120 #endif
121
122 error = exec_copyin_args(&eargs, newpath, UIO_SYSSPACE,
123 args->argp, args->envp);
124 free(newpath, M_TEMP);
125 if (error == 0)
126 error = kern_execve(td, &eargs, NULL);
127 exec_free_args(&eargs);
128 return (error);
129 }
130
131 struct l_ipc_kludge {
132 struct l_msgbuf *msgp;
133 l_long msgtyp;
134 };
135
136 int
137 linux_ipc(struct thread *td, struct linux_ipc_args *args)
138 {
139
140 switch (args->what & 0xFFFF) {
141 case LINUX_SEMOP: {
142 struct linux_semop_args a;
143
144 a.semid = args->arg1;
145 a.tsops = args->ptr;
146 a.nsops = args->arg2;
147 return (linux_semop(td, &a));
148 }
149 case LINUX_SEMGET: {
150 struct linux_semget_args a;
151
152 a.key = args->arg1;
153 a.nsems = args->arg2;
154 a.semflg = args->arg3;
155 return (linux_semget(td, &a));
156 }
157 case LINUX_SEMCTL: {
158 struct linux_semctl_args a;
159 int error;
160
161 a.semid = args->arg1;
162 a.semnum = args->arg2;
163 a.cmd = args->arg3;
164 error = copyin(args->ptr, &a.arg, sizeof(a.arg));
165 if (error)
166 return (error);
167 return (linux_semctl(td, &a));
168 }
169 case LINUX_MSGSND: {
170 struct linux_msgsnd_args a;
171
172 a.msqid = args->arg1;
173 a.msgp = args->ptr;
174 a.msgsz = args->arg2;
175 a.msgflg = args->arg3;
176 return (linux_msgsnd(td, &a));
177 }
178 case LINUX_MSGRCV: {
179 struct linux_msgrcv_args a;
180
181 a.msqid = args->arg1;
182 a.msgsz = args->arg2;
183 a.msgflg = args->arg3;
184 if ((args->what >> 16) == 0) {
185 struct l_ipc_kludge tmp;
186 int error;
187
188 if (args->ptr == NULL)
189 return (EINVAL);
190 error = copyin(args->ptr, &tmp, sizeof(tmp));
191 if (error)
192 return (error);
193 a.msgp = tmp.msgp;
194 a.msgtyp = tmp.msgtyp;
195 } else {
196 a.msgp = args->ptr;
197 a.msgtyp = args->arg5;
198 }
199 return (linux_msgrcv(td, &a));
200 }
201 case LINUX_MSGGET: {
202 struct linux_msgget_args a;
203
204 a.key = args->arg1;
205 a.msgflg = args->arg2;
206 return (linux_msgget(td, &a));
207 }
208 case LINUX_MSGCTL: {
209 struct linux_msgctl_args a;
210
211 a.msqid = args->arg1;
212 a.cmd = args->arg2;
213 a.buf = args->ptr;
214 return (linux_msgctl(td, &a));
215 }
216 case LINUX_SHMAT: {
217 struct linux_shmat_args a;
218
219 a.shmid = args->arg1;
220 a.shmaddr = args->ptr;
221 a.shmflg = args->arg2;
222 a.raddr = (l_ulong *)args->arg3;
223 return (linux_shmat(td, &a));
224 }
225 case LINUX_SHMDT: {
226 struct linux_shmdt_args a;
227
228 a.shmaddr = args->ptr;
229 return (linux_shmdt(td, &a));
230 }
231 case LINUX_SHMGET: {
232 struct linux_shmget_args a;
233
234 a.key = args->arg1;
235 a.size = args->arg2;
236 a.shmflg = args->arg3;
237 return (linux_shmget(td, &a));
238 }
239 case LINUX_SHMCTL: {
240 struct linux_shmctl_args a;
241
242 a.shmid = args->arg1;
243 a.cmd = args->arg2;
244 a.buf = args->ptr;
245 return (linux_shmctl(td, &a));
246 }
247 default:
248 break;
249 }
250
251 return (EINVAL);
252 }
253
254 int
255 linux_old_select(struct thread *td, struct linux_old_select_args *args)
256 {
257 struct l_old_select_argv linux_args;
258 struct linux_select_args newsel;
259 int error;
260
261 #ifdef DEBUG
262 if (ldebug(old_select))
263 printf(ARGS(old_select, "%p"), args->ptr);
264 #endif
265
266 error = copyin(args->ptr, &linux_args, sizeof(linux_args));
267 if (error)
268 return (error);
269
270 newsel.nfds = linux_args.nfds;
271 newsel.readfds = linux_args.readfds;
272 newsel.writefds = linux_args.writefds;
273 newsel.exceptfds = linux_args.exceptfds;
274 newsel.timeout = linux_args.timeout;
275 return (linux_select(td, &newsel));
276 }
277
278 int
279 linux_fork(struct thread *td, struct linux_fork_args *args)
280 {
281 int error;
282
283 #ifdef DEBUG
284 if (ldebug(fork))
285 printf(ARGS(fork, ""));
286 #endif
287
288 if ((error = fork(td, (struct fork_args *)args)) != 0)
289 return (error);
290
291 if (td->td_retval[1] == 1)
292 td->td_retval[0] = 0;
293 return (0);
294 }
295
296 int
297 linux_vfork(struct thread *td, struct linux_vfork_args *args)
298 {
299 int error;
300
301 #ifdef DEBUG
302 if (ldebug(vfork))
303 printf(ARGS(vfork, ""));
304 #endif
305
306 if ((error = vfork(td, (struct vfork_args *)args)) != 0)
307 return (error);
308 /* Are we the child? */
309 if (td->td_retval[1] == 1)
310 td->td_retval[0] = 0;
311 return (0);
312 }
313
314 #define CLONE_VM 0x100
315 #define CLONE_FS 0x200
316 #define CLONE_FILES 0x400
317 #define CLONE_SIGHAND 0x800
318 #define CLONE_PID 0x1000
319 #define CLONE_THREAD 0x10000
320
321 #define THREADING_FLAGS (CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND)
322
323 int
324 linux_clone(struct thread *td, struct linux_clone_args *args)
325 {
326 int error, ff = RFPROC | RFSTOPPED;
327 struct proc *p2;
328 struct thread *td2;
329 int exit_signal;
330
331 #ifdef DEBUG
332 if (ldebug(clone)) {
333 printf(ARGS(clone, "flags %x, stack %x"),
334 (unsigned int)args->flags, (unsigned int)args->stack);
335 if (args->flags & CLONE_PID)
336 printf(LMSG("CLONE_PID not yet supported"));
337 }
338 #endif
339
340 if (!args->stack)
341 return (EINVAL);
342
343 exit_signal = args->flags & 0x000000ff;
344 if (exit_signal >= LINUX_NSIG)
345 return (EINVAL);
346
347 if (exit_signal <= LINUX_SIGTBLSZ)
348 exit_signal = linux_to_bsd_signal[_SIG_IDX(exit_signal)];
349
350 if (args->flags & CLONE_VM)
351 ff |= RFMEM;
352 if (args->flags & CLONE_SIGHAND)
353 ff |= RFSIGSHARE;
354 if (!(args->flags & CLONE_FILES))
355 ff |= RFFDG;
356
357 /*
358 * Attempt to detect when linux_clone(2) is used for creating
359 * kernel threads. Unfortunately despite the existence of the
360 * CLONE_THREAD flag, version of linuxthreads package used in
361 * most popular distros as of beginning of 2005 doesn't make
362 * any use of it. Therefore, this detection relies on
363 * empirical observation that linuxthreads sets certain
364 * combination of flags, so that we can make more or less
365 * precise detection and notify the FreeBSD kernel that several
366 * processes are in fact part of the same threading group, so
367 * that special treatment is necessary for signal delivery
368 * between those processes and fd locking.
369 */
370 if ((args->flags & 0xffffff00) == THREADING_FLAGS)
371 ff |= RFTHREAD;
372
373 error = fork1(td, ff, 0, &p2);
374 if (error)
375 return (error);
376
377
378 PROC_LOCK(p2);
379 p2->p_sigparent = exit_signal;
380 PROC_UNLOCK(p2);
381 td2 = FIRST_THREAD_IN_PROC(p2);
382 td2->td_frame->tf_esp = (unsigned int)args->stack;
383
384 #ifdef DEBUG
385 if (ldebug(clone))
386 printf(LMSG("clone: successful rfork to %ld, stack %p sig = %d"),
387 (long)p2->p_pid, args->stack, exit_signal);
388 #endif
389
390 /*
391 * Make this runnable after we are finished with it.
392 */
393 mtx_lock_spin(&sched_lock);
394 TD_SET_CAN_RUN(td2);
395 setrunqueue(td2, SRQ_BORING);
396 mtx_unlock_spin(&sched_lock);
397
398 td->td_retval[0] = p2->p_pid;
399 td->td_retval[1] = 0;
400 return (0);
401 }
402
403 #define STACK_SIZE (2 * 1024 * 1024)
404 #define GUARD_SIZE (4 * PAGE_SIZE)
405
406 static int linux_mmap_common(struct thread *, struct l_mmap_argv *);
407
408 int
409 linux_mmap2(struct thread *td, struct linux_mmap2_args *args)
410 {
411 struct l_mmap_argv linux_args;
412
413 #ifdef DEBUG
414 if (ldebug(mmap2))
415 printf(ARGS(mmap2, "%p, %d, %d, 0x%08x, %d, %d"),
416 (void *)args->addr, args->len, args->prot,
417 args->flags, args->fd, args->pgoff);
418 #endif
419
420 linux_args.addr = args->addr;
421 linux_args.len = args->len;
422 linux_args.prot = args->prot;
423 linux_args.flags = args->flags;
424 linux_args.fd = args->fd;
425 linux_args.pgoff = args->pgoff * PAGE_SIZE;
426
427 return (linux_mmap_common(td, &linux_args));
428 }
429
430 int
431 linux_mmap(struct thread *td, struct linux_mmap_args *args)
432 {
433 int error;
434 struct l_mmap_argv linux_args;
435
436 error = copyin(args->ptr, &linux_args, sizeof(linux_args));
437 if (error)
438 return (error);
439
440 #ifdef DEBUG
441 if (ldebug(mmap))
442 printf(ARGS(mmap, "%p, %d, %d, 0x%08x, %d, %d"),
443 (void *)linux_args.addr, linux_args.len, linux_args.prot,
444 linux_args.flags, linux_args.fd, linux_args.pgoff);
445 #endif
446
447 return (linux_mmap_common(td, &linux_args));
448 }
449
450 static int
451 linux_mmap_common(struct thread *td, struct l_mmap_argv *linux_args)
452 {
453 struct proc *p = td->td_proc;
454 struct mmap_args /* {
455 caddr_t addr;
456 size_t len;
457 int prot;
458 int flags;
459 int fd;
460 long pad;
461 off_t pos;
462 } */ bsd_args;
463 int error;
464 struct file *fp;
465
466 error = 0;
467 bsd_args.flags = 0;
468 fp = NULL;
469
470 /*
471 * Linux mmap(2):
472 * You must specify exactly one of MAP_SHARED and MAP_PRIVATE
473 */
474 if (! ((linux_args->flags & LINUX_MAP_SHARED) ^
475 (linux_args->flags & LINUX_MAP_PRIVATE)))
476 return (EINVAL);
477
478 if (linux_args->flags & LINUX_MAP_SHARED)
479 bsd_args.flags |= MAP_SHARED;
480 if (linux_args->flags & LINUX_MAP_PRIVATE)
481 bsd_args.flags |= MAP_PRIVATE;
482 if (linux_args->flags & LINUX_MAP_FIXED)
483 bsd_args.flags |= MAP_FIXED;
484 if (linux_args->flags & LINUX_MAP_ANON)
485 bsd_args.flags |= MAP_ANON;
486 else
487 bsd_args.flags |= MAP_NOSYNC;
488 if (linux_args->flags & LINUX_MAP_GROWSDOWN)
489 bsd_args.flags |= MAP_STACK;
490
491 /*
492 * PROT_READ, PROT_WRITE, or PROT_EXEC implies PROT_READ and PROT_EXEC
493 * on Linux/i386. We do this to ensure maximum compatibility.
494 * Linux/ia64 does the same in i386 emulation mode.
495 */
496 bsd_args.prot = linux_args->prot;
497 if (bsd_args.prot & (PROT_READ | PROT_WRITE | PROT_EXEC))
498 bsd_args.prot |= PROT_READ | PROT_EXEC;
499
500 /* Linux does not check file descriptor when MAP_ANONYMOUS is set. */
501 bsd_args.fd = (bsd_args.flags & MAP_ANON) ? -1 : linux_args->fd;
502 if (bsd_args.fd != -1) {
503 /*
504 * Linux follows Solaris mmap(2) description:
505 * The file descriptor fildes is opened with
506 * read permission, regardless of the
507 * protection options specified.
508 */
509
510 if ((error = fget(td, bsd_args.fd, &fp)) != 0)
511 return (error);
512 if (fp->f_type != DTYPE_VNODE) {
513 fdrop(fp, td);
514 return (EINVAL);
515 }
516
517 /* Linux mmap() just fails for O_WRONLY files */
518 if (!(fp->f_flag & FREAD)) {
519 fdrop(fp, td);
520 return (EACCES);
521 }
522
523 fdrop(fp, td);
524 }
525
526 if (linux_args->flags & LINUX_MAP_GROWSDOWN) {
527 /*
528 * The linux MAP_GROWSDOWN option does not limit auto
529 * growth of the region. Linux mmap with this option
530 * takes as addr the inital BOS, and as len, the initial
531 * region size. It can then grow down from addr without
532 * limit. However, linux threads has an implicit internal
533 * limit to stack size of STACK_SIZE. Its just not
534 * enforced explicitly in linux. But, here we impose
535 * a limit of (STACK_SIZE - GUARD_SIZE) on the stack
536 * region, since we can do this with our mmap.
537 *
538 * Our mmap with MAP_STACK takes addr as the maximum
539 * downsize limit on BOS, and as len the max size of
540 * the region. It them maps the top SGROWSIZ bytes,
541 * and auto grows the region down, up to the limit
542 * in addr.
543 *
544 * If we don't use the MAP_STACK option, the effect
545 * of this code is to allocate a stack region of a
546 * fixed size of (STACK_SIZE - GUARD_SIZE).
547 */
548
549 if ((caddr_t)PTRIN(linux_args->addr) + linux_args->len >
550 p->p_vmspace->vm_maxsaddr) {
551 /*
552 * Some linux apps will attempt to mmap
553 * thread stacks near the top of their
554 * address space. If their TOS is greater
555 * than vm_maxsaddr, vm_map_growstack()
556 * will confuse the thread stack with the
557 * process stack and deliver a SEGV if they
558 * attempt to grow the thread stack past their
559 * current stacksize rlimit. To avoid this,
560 * adjust vm_maxsaddr upwards to reflect
561 * the current stacksize rlimit rather
562 * than the maximum possible stacksize.
563 * It would be better to adjust the
564 * mmap'ed region, but some apps do not check
565 * mmap's return value.
566 */
567 PROC_LOCK(p);
568 p->p_vmspace->vm_maxsaddr = (char *)USRSTACK -
569 lim_cur(p, RLIMIT_STACK);
570 PROC_UNLOCK(p);
571 }
572
573 /*
574 * This gives us our maximum stack size and a new BOS.
575 * If we're using VM_STACK, then mmap will just map
576 * the top SGROWSIZ bytes, and let the stack grow down
577 * to the limit at BOS. If we're not using VM_STACK
578 * we map the full stack, since we don't have a way
579 * to autogrow it.
580 */
581 if (linux_args->len > STACK_SIZE - GUARD_SIZE) {
582 bsd_args.addr = (caddr_t)PTRIN(linux_args->addr);
583 bsd_args.len = linux_args->len;
584 } else {
585 bsd_args.addr = (caddr_t)PTRIN(linux_args->addr) -
586 (STACK_SIZE - GUARD_SIZE - linux_args->len);
587 bsd_args.len = STACK_SIZE - GUARD_SIZE;
588 }
589 } else {
590 bsd_args.addr = (caddr_t)PTRIN(linux_args->addr);
591 bsd_args.len = linux_args->len;
592 }
593 bsd_args.pos = linux_args->pgoff;
594 bsd_args.pad = 0;
595
596 #ifdef DEBUG
597 if (ldebug(mmap))
598 printf("-> %s(%p, %d, %d, 0x%08x, %d, 0x%x)\n",
599 __func__,
600 (void *)bsd_args.addr, bsd_args.len, bsd_args.prot,
601 bsd_args.flags, bsd_args.fd, (int)bsd_args.pos);
602 #endif
603 error = mmap(td, &bsd_args);
604 #ifdef DEBUG
605 if (ldebug(mmap))
606 printf("-> %s() return: 0x%x (0x%08x)\n",
607 __func__, error, (u_int)td->td_retval[0]);
608 #endif
609 return (error);
610 }
611
612 int
613 linux_mprotect(struct thread *td, struct linux_mprotect_args *uap)
614 {
615 struct mprotect_args bsd_args;
616
617 bsd_args.addr = uap->addr;
618 bsd_args.len = uap->len;
619 bsd_args.prot = uap->prot;
620 if (bsd_args.prot & (PROT_READ | PROT_WRITE | PROT_EXEC))
621 bsd_args.prot |= PROT_READ | PROT_EXEC;
622 return (mprotect(td, &bsd_args));
623 }
624
625 int
626 linux_pipe(struct thread *td, struct linux_pipe_args *args)
627 {
628 int error;
629 int reg_edx;
630
631 #ifdef DEBUG
632 if (ldebug(pipe))
633 printf(ARGS(pipe, "*"));
634 #endif
635
636 reg_edx = td->td_retval[1];
637 error = pipe(td, 0);
638 if (error) {
639 td->td_retval[1] = reg_edx;
640 return (error);
641 }
642
643 error = copyout(td->td_retval, args->pipefds, 2*sizeof(int));
644 if (error) {
645 td->td_retval[1] = reg_edx;
646 return (error);
647 }
648
649 td->td_retval[1] = reg_edx;
650 td->td_retval[0] = 0;
651 return (0);
652 }
653
654 int
655 linux_ioperm(struct thread *td, struct linux_ioperm_args *args)
656 {
657 int error;
658 struct i386_ioperm_args iia;
659
660 iia.start = args->start;
661 iia.length = args->length;
662 iia.enable = args->enable;
663 mtx_lock(&Giant);
664 error = i386_set_ioperm(td, &iia);
665 mtx_unlock(&Giant);
666 return (error);
667 }
668
669 int
670 linux_iopl(struct thread *td, struct linux_iopl_args *args)
671 {
672 int error;
673
674 if (args->level < 0 || args->level > 3)
675 return (EINVAL);
676 if ((error = suser(td)) != 0)
677 return (error);
678 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
679 return (error);
680 td->td_frame->tf_eflags = (td->td_frame->tf_eflags & ~PSL_IOPL) |
681 (args->level * (PSL_IOPL / 3));
682 return (0);
683 }
684
685 int
686 linux_modify_ldt(struct thread *td, struct linux_modify_ldt_args *uap)
687 {
688 int error;
689 struct i386_ldt_args ldt;
690 struct l_descriptor ld;
691 union descriptor desc;
692
693 if (uap->ptr == NULL)
694 return (EINVAL);
695
696 switch (uap->func) {
697 case 0x00: /* read_ldt */
698 ldt.start = 0;
699 ldt.descs = uap->ptr;
700 ldt.num = uap->bytecount / sizeof(union descriptor);
701 mtx_lock(&Giant);
702 error = i386_get_ldt(td, &ldt);
703 td->td_retval[0] *= sizeof(union descriptor);
704 mtx_unlock(&Giant);
705 break;
706 case 0x01: /* write_ldt */
707 case 0x11: /* write_ldt */
708 if (uap->bytecount != sizeof(ld))
709 return (EINVAL);
710
711 error = copyin(uap->ptr, &ld, sizeof(ld));
712 if (error)
713 return (error);
714
715 ldt.start = ld.entry_number;
716 ldt.descs = &desc;
717 ldt.num = 1;
718 desc.sd.sd_lolimit = (ld.limit & 0x0000ffff);
719 desc.sd.sd_hilimit = (ld.limit & 0x000f0000) >> 16;
720 desc.sd.sd_lobase = (ld.base_addr & 0x00ffffff);
721 desc.sd.sd_hibase = (ld.base_addr & 0xff000000) >> 24;
722 desc.sd.sd_type = SDT_MEMRO | ((ld.read_exec_only ^ 1) << 1) |
723 (ld.contents << 2);
724 desc.sd.sd_dpl = 3;
725 desc.sd.sd_p = (ld.seg_not_present ^ 1);
726 desc.sd.sd_xx = 0;
727 desc.sd.sd_def32 = ld.seg_32bit;
728 desc.sd.sd_gran = ld.limit_in_pages;
729 mtx_lock(&Giant);
730 error = i386_set_ldt(td, &ldt, &desc);
731 mtx_unlock(&Giant);
732 break;
733 default:
734 error = EINVAL;
735 break;
736 }
737
738 if (error == EOPNOTSUPP) {
739 printf("linux: modify_ldt needs kernel option USER_LDT\n");
740 error = ENOSYS;
741 }
742
743 return (error);
744 }
745
746 int
747 linux_sigaction(struct thread *td, struct linux_sigaction_args *args)
748 {
749 l_osigaction_t osa;
750 l_sigaction_t act, oact;
751 int error;
752
753 #ifdef DEBUG
754 if (ldebug(sigaction))
755 printf(ARGS(sigaction, "%d, %p, %p"),
756 args->sig, (void *)args->nsa, (void *)args->osa);
757 #endif
758
759 if (args->nsa != NULL) {
760 error = copyin(args->nsa, &osa, sizeof(l_osigaction_t));
761 if (error)
762 return (error);
763 act.lsa_handler = osa.lsa_handler;
764 act.lsa_flags = osa.lsa_flags;
765 act.lsa_restorer = osa.lsa_restorer;
766 LINUX_SIGEMPTYSET(act.lsa_mask);
767 act.lsa_mask.__bits[0] = osa.lsa_mask;
768 }
769
770 error = linux_do_sigaction(td, args->sig, args->nsa ? &act : NULL,
771 args->osa ? &oact : NULL);
772
773 if (args->osa != NULL && !error) {
774 osa.lsa_handler = oact.lsa_handler;
775 osa.lsa_flags = oact.lsa_flags;
776 osa.lsa_restorer = oact.lsa_restorer;
777 osa.lsa_mask = oact.lsa_mask.__bits[0];
778 error = copyout(&osa, args->osa, sizeof(l_osigaction_t));
779 }
780
781 return (error);
782 }
783
784 /*
785 * Linux has two extra args, restart and oldmask. We dont use these,
786 * but it seems that "restart" is actually a context pointer that
787 * enables the signal to happen with a different register set.
788 */
789 int
790 linux_sigsuspend(struct thread *td, struct linux_sigsuspend_args *args)
791 {
792 sigset_t sigmask;
793 l_sigset_t mask;
794
795 #ifdef DEBUG
796 if (ldebug(sigsuspend))
797 printf(ARGS(sigsuspend, "%08lx"), (unsigned long)args->mask);
798 #endif
799
800 LINUX_SIGEMPTYSET(mask);
801 mask.__bits[0] = args->mask;
802 linux_to_bsd_sigset(&mask, &sigmask);
803 return (kern_sigsuspend(td, sigmask));
804 }
805
806 int
807 linux_rt_sigsuspend(struct thread *td, struct linux_rt_sigsuspend_args *uap)
808 {
809 l_sigset_t lmask;
810 sigset_t sigmask;
811 int error;
812
813 #ifdef DEBUG
814 if (ldebug(rt_sigsuspend))
815 printf(ARGS(rt_sigsuspend, "%p, %d"),
816 (void *)uap->newset, uap->sigsetsize);
817 #endif
818
819 if (uap->sigsetsize != sizeof(l_sigset_t))
820 return (EINVAL);
821
822 error = copyin(uap->newset, &lmask, sizeof(l_sigset_t));
823 if (error)
824 return (error);
825
826 linux_to_bsd_sigset(&lmask, &sigmask);
827 return (kern_sigsuspend(td, sigmask));
828 }
829
830 int
831 linux_pause(struct thread *td, struct linux_pause_args *args)
832 {
833 struct proc *p = td->td_proc;
834 sigset_t sigmask;
835
836 #ifdef DEBUG
837 if (ldebug(pause))
838 printf(ARGS(pause, ""));
839 #endif
840
841 PROC_LOCK(p);
842 sigmask = td->td_sigmask;
843 PROC_UNLOCK(p);
844 return (kern_sigsuspend(td, sigmask));
845 }
846
847 int
848 linux_sigaltstack(struct thread *td, struct linux_sigaltstack_args *uap)
849 {
850 stack_t ss, oss;
851 l_stack_t lss;
852 int error;
853
854 #ifdef DEBUG
855 if (ldebug(sigaltstack))
856 printf(ARGS(sigaltstack, "%p, %p"), uap->uss, uap->uoss);
857 #endif
858
859 if (uap->uss != NULL) {
860 error = copyin(uap->uss, &lss, sizeof(l_stack_t));
861 if (error)
862 return (error);
863
864 ss.ss_sp = lss.ss_sp;
865 ss.ss_size = lss.ss_size;
866 ss.ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags);
867 }
868 error = kern_sigaltstack(td, (uap->uss != NULL) ? &ss : NULL,
869 (uap->uoss != NULL) ? &oss : NULL);
870 if (!error && uap->uoss != NULL) {
871 lss.ss_sp = oss.ss_sp;
872 lss.ss_size = oss.ss_size;
873 lss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags);
874 error = copyout(&lss, uap->uoss, sizeof(l_stack_t));
875 }
876
877 return (error);
878 }
879
880 int
881 linux_ftruncate64(struct thread *td, struct linux_ftruncate64_args *args)
882 {
883 struct ftruncate_args sa;
884
885 #ifdef DEBUG
886 if (ldebug(ftruncate64))
887 printf(ARGS(ftruncate64, "%u, %jd"), args->fd,
888 (intmax_t)args->length);
889 #endif
890
891 sa.fd = args->fd;
892 sa.pad = 0;
893 sa.length = args->length;
894 return ftruncate(td, &sa);
895 }
896
897 int
898 linux_set_thread_area(struct thread *td, struct linux_set_thread_area_args *args)
899 {
900 /*
901 * Return an error code instead of raising a SIGSYS so that
902 * the caller will fall back to simpler LDT methods.
903 */
904 return (ENOSYS);
905 }
906
907 int
908 linux_gettid(struct thread *td, struct linux_gettid_args *args)
909 {
910
911 td->td_retval[0] = td->td_proc->p_pid;
912 return (0);
913 }
914
915 int
916 linux_tkill(struct thread *td, struct linux_tkill_args *args)
917 {
918
919 return (linux_kill(td, (struct linux_kill_args *) args));
920 }
921
Cache object: f87fb98d3c0dd2db9741d06f16afa544
|