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/6.3/sys/i386/linux/linux_machdep.c 171303 2007-07-08 12:20:36Z netchild $");
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 /* This gives us our maximum stack size */
574 if (linux_args->len > STACK_SIZE - GUARD_SIZE)
575 bsd_args.len = linux_args->len;
576 else
577 bsd_args.len = STACK_SIZE - GUARD_SIZE;
578
579 /*
580 * This gives us a new BOS. If we're using VM_STACK, then
581 * mmap will just map the top SGROWSIZ bytes, and let
582 * the stack grow down to the limit at BOS. If we're
583 * not using VM_STACK we map the full stack, since we
584 * don't have a way to autogrow it.
585 */
586 bsd_args.addr = (caddr_t)PTRIN(linux_args->addr) -
587 bsd_args.len;
588 } else {
589 bsd_args.addr = (caddr_t)PTRIN(linux_args->addr);
590 bsd_args.len = linux_args->len;
591 }
592 bsd_args.pos = linux_args->pgoff;
593 bsd_args.pad = 0;
594
595 #ifdef DEBUG
596 if (ldebug(mmap))
597 printf("-> %s(%p, %d, %d, 0x%08x, %d, 0x%x)\n",
598 __func__,
599 (void *)bsd_args.addr, bsd_args.len, bsd_args.prot,
600 bsd_args.flags, bsd_args.fd, (int)bsd_args.pos);
601 #endif
602 error = mmap(td, &bsd_args);
603 #ifdef DEBUG
604 if (ldebug(mmap))
605 printf("-> %s() return: 0x%x (0x%08x)\n",
606 __func__, error, (u_int)td->td_retval[0]);
607 #endif
608 return (error);
609 }
610
611 int
612 linux_mprotect(struct thread *td, struct linux_mprotect_args *uap)
613 {
614 struct mprotect_args bsd_args;
615
616 bsd_args.addr = uap->addr;
617 bsd_args.len = uap->len;
618 bsd_args.prot = uap->prot;
619 if (bsd_args.prot & (PROT_READ | PROT_WRITE | PROT_EXEC))
620 bsd_args.prot |= PROT_READ | PROT_EXEC;
621 return (mprotect(td, &bsd_args));
622 }
623
624 int
625 linux_pipe(struct thread *td, struct linux_pipe_args *args)
626 {
627 int error;
628 int reg_edx;
629
630 #ifdef DEBUG
631 if (ldebug(pipe))
632 printf(ARGS(pipe, "*"));
633 #endif
634
635 reg_edx = td->td_retval[1];
636 error = pipe(td, 0);
637 if (error) {
638 td->td_retval[1] = reg_edx;
639 return (error);
640 }
641
642 error = copyout(td->td_retval, args->pipefds, 2*sizeof(int));
643 if (error) {
644 td->td_retval[1] = reg_edx;
645 return (error);
646 }
647
648 td->td_retval[1] = reg_edx;
649 td->td_retval[0] = 0;
650 return (0);
651 }
652
653 int
654 linux_ioperm(struct thread *td, struct linux_ioperm_args *args)
655 {
656 int error;
657 struct i386_ioperm_args iia;
658
659 iia.start = args->start;
660 iia.length = args->length;
661 iia.enable = args->enable;
662 mtx_lock(&Giant);
663 error = i386_set_ioperm(td, &iia);
664 mtx_unlock(&Giant);
665 return (error);
666 }
667
668 int
669 linux_iopl(struct thread *td, struct linux_iopl_args *args)
670 {
671 int error;
672
673 if (args->level < 0 || args->level > 3)
674 return (EINVAL);
675 if ((error = suser(td)) != 0)
676 return (error);
677 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
678 return (error);
679 td->td_frame->tf_eflags = (td->td_frame->tf_eflags & ~PSL_IOPL) |
680 (args->level * (PSL_IOPL / 3));
681 return (0);
682 }
683
684 int
685 linux_modify_ldt(struct thread *td, struct linux_modify_ldt_args *uap)
686 {
687 int error;
688 struct i386_ldt_args ldt;
689 struct l_descriptor ld;
690 union descriptor desc;
691
692 if (uap->ptr == NULL)
693 return (EINVAL);
694
695 switch (uap->func) {
696 case 0x00: /* read_ldt */
697 ldt.start = 0;
698 ldt.descs = uap->ptr;
699 ldt.num = uap->bytecount / sizeof(union descriptor);
700 mtx_lock(&Giant);
701 error = i386_get_ldt(td, &ldt);
702 td->td_retval[0] *= sizeof(union descriptor);
703 mtx_unlock(&Giant);
704 break;
705 case 0x01: /* write_ldt */
706 case 0x11: /* write_ldt */
707 if (uap->bytecount != sizeof(ld))
708 return (EINVAL);
709
710 error = copyin(uap->ptr, &ld, sizeof(ld));
711 if (error)
712 return (error);
713
714 ldt.start = ld.entry_number;
715 ldt.descs = &desc;
716 ldt.num = 1;
717 desc.sd.sd_lolimit = (ld.limit & 0x0000ffff);
718 desc.sd.sd_hilimit = (ld.limit & 0x000f0000) >> 16;
719 desc.sd.sd_lobase = (ld.base_addr & 0x00ffffff);
720 desc.sd.sd_hibase = (ld.base_addr & 0xff000000) >> 24;
721 desc.sd.sd_type = SDT_MEMRO | ((ld.read_exec_only ^ 1) << 1) |
722 (ld.contents << 2);
723 desc.sd.sd_dpl = 3;
724 desc.sd.sd_p = (ld.seg_not_present ^ 1);
725 desc.sd.sd_xx = 0;
726 desc.sd.sd_def32 = ld.seg_32bit;
727 desc.sd.sd_gran = ld.limit_in_pages;
728 mtx_lock(&Giant);
729 error = i386_set_ldt(td, &ldt, &desc);
730 mtx_unlock(&Giant);
731 break;
732 default:
733 error = EINVAL;
734 break;
735 }
736
737 if (error == EOPNOTSUPP) {
738 printf("linux: modify_ldt needs kernel option USER_LDT\n");
739 error = ENOSYS;
740 }
741
742 return (error);
743 }
744
745 int
746 linux_sigaction(struct thread *td, struct linux_sigaction_args *args)
747 {
748 l_osigaction_t osa;
749 l_sigaction_t act, oact;
750 int error;
751
752 #ifdef DEBUG
753 if (ldebug(sigaction))
754 printf(ARGS(sigaction, "%d, %p, %p"),
755 args->sig, (void *)args->nsa, (void *)args->osa);
756 #endif
757
758 if (args->nsa != NULL) {
759 error = copyin(args->nsa, &osa, sizeof(l_osigaction_t));
760 if (error)
761 return (error);
762 act.lsa_handler = osa.lsa_handler;
763 act.lsa_flags = osa.lsa_flags;
764 act.lsa_restorer = osa.lsa_restorer;
765 LINUX_SIGEMPTYSET(act.lsa_mask);
766 act.lsa_mask.__bits[0] = osa.lsa_mask;
767 }
768
769 error = linux_do_sigaction(td, args->sig, args->nsa ? &act : NULL,
770 args->osa ? &oact : NULL);
771
772 if (args->osa != NULL && !error) {
773 osa.lsa_handler = oact.lsa_handler;
774 osa.lsa_flags = oact.lsa_flags;
775 osa.lsa_restorer = oact.lsa_restorer;
776 osa.lsa_mask = oact.lsa_mask.__bits[0];
777 error = copyout(&osa, args->osa, sizeof(l_osigaction_t));
778 }
779
780 return (error);
781 }
782
783 /*
784 * Linux has two extra args, restart and oldmask. We dont use these,
785 * but it seems that "restart" is actually a context pointer that
786 * enables the signal to happen with a different register set.
787 */
788 int
789 linux_sigsuspend(struct thread *td, struct linux_sigsuspend_args *args)
790 {
791 sigset_t sigmask;
792 l_sigset_t mask;
793
794 #ifdef DEBUG
795 if (ldebug(sigsuspend))
796 printf(ARGS(sigsuspend, "%08lx"), (unsigned long)args->mask);
797 #endif
798
799 LINUX_SIGEMPTYSET(mask);
800 mask.__bits[0] = args->mask;
801 linux_to_bsd_sigset(&mask, &sigmask);
802 return (kern_sigsuspend(td, sigmask));
803 }
804
805 int
806 linux_rt_sigsuspend(struct thread *td, struct linux_rt_sigsuspend_args *uap)
807 {
808 l_sigset_t lmask;
809 sigset_t sigmask;
810 int error;
811
812 #ifdef DEBUG
813 if (ldebug(rt_sigsuspend))
814 printf(ARGS(rt_sigsuspend, "%p, %d"),
815 (void *)uap->newset, uap->sigsetsize);
816 #endif
817
818 if (uap->sigsetsize != sizeof(l_sigset_t))
819 return (EINVAL);
820
821 error = copyin(uap->newset, &lmask, sizeof(l_sigset_t));
822 if (error)
823 return (error);
824
825 linux_to_bsd_sigset(&lmask, &sigmask);
826 return (kern_sigsuspend(td, sigmask));
827 }
828
829 int
830 linux_pause(struct thread *td, struct linux_pause_args *args)
831 {
832 struct proc *p = td->td_proc;
833 sigset_t sigmask;
834
835 #ifdef DEBUG
836 if (ldebug(pause))
837 printf(ARGS(pause, ""));
838 #endif
839
840 PROC_LOCK(p);
841 sigmask = td->td_sigmask;
842 PROC_UNLOCK(p);
843 return (kern_sigsuspend(td, sigmask));
844 }
845
846 int
847 linux_sigaltstack(struct thread *td, struct linux_sigaltstack_args *uap)
848 {
849 stack_t ss, oss;
850 l_stack_t lss;
851 int error;
852
853 #ifdef DEBUG
854 if (ldebug(sigaltstack))
855 printf(ARGS(sigaltstack, "%p, %p"), uap->uss, uap->uoss);
856 #endif
857
858 if (uap->uss != NULL) {
859 error = copyin(uap->uss, &lss, sizeof(l_stack_t));
860 if (error)
861 return (error);
862
863 ss.ss_sp = lss.ss_sp;
864 ss.ss_size = lss.ss_size;
865 ss.ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags);
866 }
867 error = kern_sigaltstack(td, (uap->uss != NULL) ? &ss : NULL,
868 (uap->uoss != NULL) ? &oss : NULL);
869 if (!error && uap->uoss != NULL) {
870 lss.ss_sp = oss.ss_sp;
871 lss.ss_size = oss.ss_size;
872 lss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags);
873 error = copyout(&lss, uap->uoss, sizeof(l_stack_t));
874 }
875
876 return (error);
877 }
878
879 int
880 linux_ftruncate64(struct thread *td, struct linux_ftruncate64_args *args)
881 {
882 struct ftruncate_args sa;
883
884 #ifdef DEBUG
885 if (ldebug(ftruncate64))
886 printf(ARGS(ftruncate64, "%u, %jd"), args->fd,
887 (intmax_t)args->length);
888 #endif
889
890 sa.fd = args->fd;
891 sa.pad = 0;
892 sa.length = args->length;
893 return ftruncate(td, &sa);
894 }
895
896 int
897 linux_set_thread_area(struct thread *td, struct linux_set_thread_area_args *args)
898 {
899 /*
900 * Return an error code instead of raising a SIGSYS so that
901 * the caller will fall back to simpler LDT methods.
902 */
903 return (ENOSYS);
904 }
905
906 int
907 linux_gettid(struct thread *td, struct linux_gettid_args *args)
908 {
909
910 td->td_retval[0] = td->td_proc->p_pid;
911 return (0);
912 }
913
914 int
915 linux_tkill(struct thread *td, struct linux_tkill_args *args)
916 {
917
918 return (linux_kill(td, (struct linux_kill_args *) args));
919 }
920
Cache object: 1a8ebd967509a948aed9e565b43ce210
|