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.2/sys/i386/linux/linux_machdep.c 162810 2006-09-29 19:05:24Z 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 relay fully 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 /* XXX move */
404 struct l_mmap_argv {
405 l_caddr_t addr;
406 l_int len;
407 l_int prot;
408 l_int flags;
409 l_int fd;
410 l_int pos;
411 };
412
413 #define STACK_SIZE (2 * 1024 * 1024)
414 #define GUARD_SIZE (4 * PAGE_SIZE)
415
416 static int linux_mmap_common(struct thread *, struct l_mmap_argv *);
417
418 int
419 linux_mmap2(struct thread *td, struct linux_mmap2_args *args)
420 {
421 struct l_mmap_argv linux_args;
422
423 #ifdef DEBUG
424 if (ldebug(mmap2))
425 printf(ARGS(mmap2, "%p, %d, %d, 0x%08x, %d, %d"),
426 (void *)args->addr, args->len, args->prot,
427 args->flags, args->fd, args->pgoff);
428 #endif
429
430 linux_args.addr = (l_caddr_t)args->addr;
431 linux_args.len = args->len;
432 linux_args.prot = args->prot;
433 linux_args.flags = args->flags;
434 linux_args.fd = args->fd;
435 linux_args.pos = args->pgoff * PAGE_SIZE;
436
437 return (linux_mmap_common(td, &linux_args));
438 }
439
440 int
441 linux_mmap(struct thread *td, struct linux_mmap_args *args)
442 {
443 int error;
444 struct l_mmap_argv linux_args;
445
446 error = copyin(args->ptr, &linux_args, sizeof(linux_args));
447 if (error)
448 return (error);
449
450 #ifdef DEBUG
451 if (ldebug(mmap))
452 printf(ARGS(mmap, "%p, %d, %d, 0x%08x, %d, %d"),
453 (void *)linux_args.addr, linux_args.len, linux_args.prot,
454 linux_args.flags, linux_args.fd, linux_args.pos);
455 #endif
456
457 return (linux_mmap_common(td, &linux_args));
458 }
459
460 static int
461 linux_mmap_common(struct thread *td, struct l_mmap_argv *linux_args)
462 {
463 struct proc *p = td->td_proc;
464 struct mmap_args /* {
465 caddr_t addr;
466 size_t len;
467 int prot;
468 int flags;
469 int fd;
470 long pad;
471 off_t pos;
472 } */ bsd_args;
473 int error;
474 struct file *fp;
475
476 error = 0;
477 bsd_args.flags = 0;
478 fp = NULL;
479
480 /*
481 * Linux mmap(2):
482 * You must specify exactly one of MAP_SHARED and MAP_PRIVATE
483 */
484 if (! ((linux_args->flags & LINUX_MAP_SHARED) ^
485 (linux_args->flags & LINUX_MAP_PRIVATE)))
486 return (EINVAL);
487
488 if (linux_args->flags & LINUX_MAP_SHARED)
489 bsd_args.flags |= MAP_SHARED;
490 if (linux_args->flags & LINUX_MAP_PRIVATE)
491 bsd_args.flags |= MAP_PRIVATE;
492 if (linux_args->flags & LINUX_MAP_FIXED)
493 bsd_args.flags |= MAP_FIXED;
494 if (linux_args->flags & LINUX_MAP_ANON)
495 bsd_args.flags |= MAP_ANON;
496 else
497 bsd_args.flags |= MAP_NOSYNC;
498 if (linux_args->flags & LINUX_MAP_GROWSDOWN) {
499 bsd_args.flags |= MAP_STACK;
500
501 /* The linux MAP_GROWSDOWN option does not limit auto
502 * growth of the region. Linux mmap with this option
503 * takes as addr the inital BOS, and as len, the initial
504 * region size. It can then grow down from addr without
505 * limit. However, linux threads has an implicit internal
506 * limit to stack size of STACK_SIZE. Its just not
507 * enforced explicitly in linux. But, here we impose
508 * a limit of (STACK_SIZE - GUARD_SIZE) on the stack
509 * region, since we can do this with our mmap.
510 *
511 * Our mmap with MAP_STACK takes addr as the maximum
512 * downsize limit on BOS, and as len the max size of
513 * the region. It them maps the top SGROWSIZ bytes,
514 * and autgrows the region down, up to the limit
515 * in addr.
516 *
517 * If we don't use the MAP_STACK option, the effect
518 * of this code is to allocate a stack region of a
519 * fixed size of (STACK_SIZE - GUARD_SIZE).
520 */
521
522 /* This gives us TOS */
523 bsd_args.addr = linux_args->addr + linux_args->len;
524
525 if (bsd_args.addr > p->p_vmspace->vm_maxsaddr) {
526 /* Some linux apps will attempt to mmap
527 * thread stacks near the top of their
528 * address space. If their TOS is greater
529 * than vm_maxsaddr, vm_map_growstack()
530 * will confuse the thread stack with the
531 * process stack and deliver a SEGV if they
532 * attempt to grow the thread stack past their
533 * current stacksize rlimit. To avoid this,
534 * adjust vm_maxsaddr upwards to reflect
535 * the current stacksize rlimit rather
536 * than the maximum possible stacksize.
537 * It would be better to adjust the
538 * mmap'ed region, but some apps do not check
539 * mmap's return value.
540 */
541 PROC_LOCK(p);
542 p->p_vmspace->vm_maxsaddr = (char *)USRSTACK -
543 lim_cur(p, RLIMIT_STACK);
544 PROC_UNLOCK(p);
545 }
546
547 /* This gives us our maximum stack size */
548 if (linux_args->len > STACK_SIZE - GUARD_SIZE)
549 bsd_args.len = linux_args->len;
550 else
551 bsd_args.len = STACK_SIZE - GUARD_SIZE;
552
553 /* This gives us a new BOS. If we're using VM_STACK, then
554 * mmap will just map the top SGROWSIZ bytes, and let
555 * the stack grow down to the limit at BOS. If we're
556 * not using VM_STACK we map the full stack, since we
557 * don't have a way to autogrow it.
558 */
559 bsd_args.addr -= bsd_args.len;
560 } else {
561 bsd_args.addr = linux_args->addr;
562 bsd_args.len = linux_args->len;
563 }
564
565 bsd_args.prot = linux_args->prot;
566 if (linux_args->flags & LINUX_MAP_ANON)
567 bsd_args.fd = -1;
568 else {
569 /*
570 * Linux follows Solaris mmap(2) description:
571 * The file descriptor fildes is opened with
572 * read permission, regardless of the
573 * protection options specified.
574 * If PROT_WRITE is specified, the application
575 * must have opened the file descriptor
576 * fildes with write permission unless
577 * MAP_PRIVATE is specified in the flag
578 * argument as described below.
579 */
580
581 if ((error = fget(td, linux_args->fd, &fp)) != 0)
582 return (error);
583 if (fp->f_type != DTYPE_VNODE) {
584 fdrop(fp, td);
585 return (EINVAL);
586 }
587
588 /* Linux mmap() just fails for O_WRONLY files */
589 if (! (fp->f_flag & FREAD)) {
590 fdrop(fp, td);
591 return (EACCES);
592 }
593
594 bsd_args.fd = linux_args->fd;
595 fdrop(fp, td);
596 }
597 bsd_args.pos = linux_args->pos;
598 bsd_args.pad = 0;
599
600 #ifdef DEBUG
601 if (ldebug(mmap))
602 printf("-> %s(%p, %d, %d, 0x%08x, %d, 0x%x)\n",
603 __func__,
604 (void *)bsd_args.addr, bsd_args.len, bsd_args.prot,
605 bsd_args.flags, bsd_args.fd, (int)bsd_args.pos);
606 #endif
607 error = mmap(td, &bsd_args);
608 #ifdef DEBUG
609 if (ldebug(mmap))
610 printf("-> %s() return: 0x%x (0x%08x)\n",
611 __func__, error, (u_int)td->td_retval[0]);
612 #endif
613 return (error);
614 }
615
616 int
617 linux_pipe(struct thread *td, struct linux_pipe_args *args)
618 {
619 int error;
620 int reg_edx;
621
622 #ifdef DEBUG
623 if (ldebug(pipe))
624 printf(ARGS(pipe, "*"));
625 #endif
626
627 reg_edx = td->td_retval[1];
628 error = pipe(td, 0);
629 if (error) {
630 td->td_retval[1] = reg_edx;
631 return (error);
632 }
633
634 error = copyout(td->td_retval, args->pipefds, 2*sizeof(int));
635 if (error) {
636 td->td_retval[1] = reg_edx;
637 return (error);
638 }
639
640 td->td_retval[1] = reg_edx;
641 td->td_retval[0] = 0;
642 return (0);
643 }
644
645 int
646 linux_ioperm(struct thread *td, struct linux_ioperm_args *args)
647 {
648 int error;
649 struct i386_ioperm_args iia;
650
651 iia.start = args->start;
652 iia.length = args->length;
653 iia.enable = args->enable;
654 mtx_lock(&Giant);
655 error = i386_set_ioperm(td, &iia);
656 mtx_unlock(&Giant);
657 return (error);
658 }
659
660 int
661 linux_iopl(struct thread *td, struct linux_iopl_args *args)
662 {
663 int error;
664
665 if (args->level < 0 || args->level > 3)
666 return (EINVAL);
667 if ((error = suser(td)) != 0)
668 return (error);
669 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
670 return (error);
671 td->td_frame->tf_eflags = (td->td_frame->tf_eflags & ~PSL_IOPL) |
672 (args->level * (PSL_IOPL / 3));
673 return (0);
674 }
675
676 int
677 linux_modify_ldt(struct thread *td, struct linux_modify_ldt_args *uap)
678 {
679 int error;
680 struct i386_ldt_args ldt;
681 struct l_descriptor ld;
682 union descriptor desc;
683
684 if (uap->ptr == NULL)
685 return (EINVAL);
686
687 switch (uap->func) {
688 case 0x00: /* read_ldt */
689 ldt.start = 0;
690 ldt.descs = uap->ptr;
691 ldt.num = uap->bytecount / sizeof(union descriptor);
692 mtx_lock(&Giant);
693 error = i386_get_ldt(td, &ldt);
694 td->td_retval[0] *= sizeof(union descriptor);
695 mtx_unlock(&Giant);
696 break;
697 case 0x01: /* write_ldt */
698 case 0x11: /* write_ldt */
699 if (uap->bytecount != sizeof(ld))
700 return (EINVAL);
701
702 error = copyin(uap->ptr, &ld, sizeof(ld));
703 if (error)
704 return (error);
705
706 ldt.start = ld.entry_number;
707 ldt.descs = &desc;
708 ldt.num = 1;
709 desc.sd.sd_lolimit = (ld.limit & 0x0000ffff);
710 desc.sd.sd_hilimit = (ld.limit & 0x000f0000) >> 16;
711 desc.sd.sd_lobase = (ld.base_addr & 0x00ffffff);
712 desc.sd.sd_hibase = (ld.base_addr & 0xff000000) >> 24;
713 desc.sd.sd_type = SDT_MEMRO | ((ld.read_exec_only ^ 1) << 1) |
714 (ld.contents << 2);
715 desc.sd.sd_dpl = 3;
716 desc.sd.sd_p = (ld.seg_not_present ^ 1);
717 desc.sd.sd_xx = 0;
718 desc.sd.sd_def32 = ld.seg_32bit;
719 desc.sd.sd_gran = ld.limit_in_pages;
720 mtx_lock(&Giant);
721 error = i386_set_ldt(td, &ldt, &desc);
722 mtx_unlock(&Giant);
723 break;
724 default:
725 error = EINVAL;
726 break;
727 }
728
729 if (error == EOPNOTSUPP) {
730 printf("linux: modify_ldt needs kernel option USER_LDT\n");
731 error = ENOSYS;
732 }
733
734 return (error);
735 }
736
737 int
738 linux_sigaction(struct thread *td, struct linux_sigaction_args *args)
739 {
740 l_osigaction_t osa;
741 l_sigaction_t act, oact;
742 int error;
743
744 #ifdef DEBUG
745 if (ldebug(sigaction))
746 printf(ARGS(sigaction, "%d, %p, %p"),
747 args->sig, (void *)args->nsa, (void *)args->osa);
748 #endif
749
750 if (args->nsa != NULL) {
751 error = copyin(args->nsa, &osa, sizeof(l_osigaction_t));
752 if (error)
753 return (error);
754 act.lsa_handler = osa.lsa_handler;
755 act.lsa_flags = osa.lsa_flags;
756 act.lsa_restorer = osa.lsa_restorer;
757 LINUX_SIGEMPTYSET(act.lsa_mask);
758 act.lsa_mask.__bits[0] = osa.lsa_mask;
759 }
760
761 error = linux_do_sigaction(td, args->sig, args->nsa ? &act : NULL,
762 args->osa ? &oact : NULL);
763
764 if (args->osa != NULL && !error) {
765 osa.lsa_handler = oact.lsa_handler;
766 osa.lsa_flags = oact.lsa_flags;
767 osa.lsa_restorer = oact.lsa_restorer;
768 osa.lsa_mask = oact.lsa_mask.__bits[0];
769 error = copyout(&osa, args->osa, sizeof(l_osigaction_t));
770 }
771
772 return (error);
773 }
774
775 /*
776 * Linux has two extra args, restart and oldmask. We dont use these,
777 * but it seems that "restart" is actually a context pointer that
778 * enables the signal to happen with a different register set.
779 */
780 int
781 linux_sigsuspend(struct thread *td, struct linux_sigsuspend_args *args)
782 {
783 sigset_t sigmask;
784 l_sigset_t mask;
785
786 #ifdef DEBUG
787 if (ldebug(sigsuspend))
788 printf(ARGS(sigsuspend, "%08lx"), (unsigned long)args->mask);
789 #endif
790
791 LINUX_SIGEMPTYSET(mask);
792 mask.__bits[0] = args->mask;
793 linux_to_bsd_sigset(&mask, &sigmask);
794 return (kern_sigsuspend(td, sigmask));
795 }
796
797 int
798 linux_rt_sigsuspend(struct thread *td, struct linux_rt_sigsuspend_args *uap)
799 {
800 l_sigset_t lmask;
801 sigset_t sigmask;
802 int error;
803
804 #ifdef DEBUG
805 if (ldebug(rt_sigsuspend))
806 printf(ARGS(rt_sigsuspend, "%p, %d"),
807 (void *)uap->newset, uap->sigsetsize);
808 #endif
809
810 if (uap->sigsetsize != sizeof(l_sigset_t))
811 return (EINVAL);
812
813 error = copyin(uap->newset, &lmask, sizeof(l_sigset_t));
814 if (error)
815 return (error);
816
817 linux_to_bsd_sigset(&lmask, &sigmask);
818 return (kern_sigsuspend(td, sigmask));
819 }
820
821 int
822 linux_pause(struct thread *td, struct linux_pause_args *args)
823 {
824 struct proc *p = td->td_proc;
825 sigset_t sigmask;
826
827 #ifdef DEBUG
828 if (ldebug(pause))
829 printf(ARGS(pause, ""));
830 #endif
831
832 PROC_LOCK(p);
833 sigmask = td->td_sigmask;
834 PROC_UNLOCK(p);
835 return (kern_sigsuspend(td, sigmask));
836 }
837
838 int
839 linux_sigaltstack(struct thread *td, struct linux_sigaltstack_args *uap)
840 {
841 stack_t ss, oss;
842 l_stack_t lss;
843 int error;
844
845 #ifdef DEBUG
846 if (ldebug(sigaltstack))
847 printf(ARGS(sigaltstack, "%p, %p"), uap->uss, uap->uoss);
848 #endif
849
850 if (uap->uss != NULL) {
851 error = copyin(uap->uss, &lss, sizeof(l_stack_t));
852 if (error)
853 return (error);
854
855 ss.ss_sp = lss.ss_sp;
856 ss.ss_size = lss.ss_size;
857 ss.ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags);
858 }
859 error = kern_sigaltstack(td, (uap->uss != NULL) ? &ss : NULL,
860 (uap->uoss != NULL) ? &oss : NULL);
861 if (!error && uap->uoss != NULL) {
862 lss.ss_sp = oss.ss_sp;
863 lss.ss_size = oss.ss_size;
864 lss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags);
865 error = copyout(&lss, uap->uoss, sizeof(l_stack_t));
866 }
867
868 return (error);
869 }
870
871 int
872 linux_ftruncate64(struct thread *td, struct linux_ftruncate64_args *args)
873 {
874 struct ftruncate_args sa;
875
876 #ifdef DEBUG
877 if (ldebug(ftruncate64))
878 printf(ARGS(ftruncate64, "%u, %jd"), args->fd,
879 (intmax_t)args->length);
880 #endif
881
882 sa.fd = args->fd;
883 sa.pad = 0;
884 sa.length = args->length;
885 return ftruncate(td, &sa);
886 }
887
888 int
889 linux_set_thread_area(struct thread *td, struct linux_set_thread_area_args *args)
890 {
891 /*
892 * Return an error code instead of raising a SIGSYS so that
893 * the caller will fall back to simpler LDT methods.
894 */
895 return (ENOSYS);
896 }
897
898 int
899 linux_gettid(struct thread *td, struct linux_gettid_args *args)
900 {
901
902 td->td_retval[0] = td->td_proc->p_pid;
903 return (0);
904 }
905
906 int
907 linux_tkill(struct thread *td, struct linux_tkill_args *args)
908 {
909
910 return (linux_kill(td, (struct linux_kill_args *) args));
911 }
912
Cache object: 8244132f0325a33daa00188d70eba636
|