1 /*-
2 * Copyright (c) 2004 Tim J. Robbins
3 * Copyright (c) 2002 Doug Rabson
4 * Copyright (c) 2000 Marcel Moolenaar
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer
12 * in this position and unchanged.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 #include "opt_compat.h"
35
36 #include <sys/param.h>
37 #include <sys/capsicum.h>
38 #include <sys/clock.h>
39 #include <sys/fcntl.h>
40 #include <sys/file.h>
41 #include <sys/imgact.h>
42 #include <sys/kernel.h>
43 #include <sys/limits.h>
44 #include <sys/lock.h>
45 #include <sys/malloc.h>
46 #include <sys/mman.h>
47 #include <sys/mutex.h>
48 #include <sys/priv.h>
49 #include <sys/proc.h>
50 #include <sys/resource.h>
51 #include <sys/resourcevar.h>
52 #include <sys/syscallsubr.h>
53 #include <sys/sysproto.h>
54 #include <sys/systm.h>
55 #include <sys/unistd.h>
56 #include <sys/wait.h>
57
58 #include <machine/frame.h>
59 #include <machine/pcb.h>
60 #include <machine/psl.h>
61 #include <machine/segments.h>
62 #include <machine/specialreg.h>
63
64 #include <vm/pmap.h>
65 #include <vm/vm.h>
66 #include <vm/vm_map.h>
67
68 #include <compat/freebsd32/freebsd32_util.h>
69 #include <amd64/linux32/linux.h>
70 #include <amd64/linux32/linux32_proto.h>
71 #include <compat/linux/linux_emul.h>
72 #include <compat/linux/linux_ipc.h>
73 #include <compat/linux/linux_misc.h>
74 #include <compat/linux/linux_mmap.h>
75 #include <compat/linux/linux_signal.h>
76 #include <compat/linux/linux_util.h>
77
78 static void bsd_to_linux_rusage(struct rusage *ru, struct l_rusage *lru);
79
80 struct l_old_select_argv {
81 l_int nfds;
82 l_uintptr_t readfds;
83 l_uintptr_t writefds;
84 l_uintptr_t exceptfds;
85 l_uintptr_t timeout;
86 } __packed;
87
88
89 static void
90 bsd_to_linux_rusage(struct rusage *ru, struct l_rusage *lru)
91 {
92
93 lru->ru_utime.tv_sec = ru->ru_utime.tv_sec;
94 lru->ru_utime.tv_usec = ru->ru_utime.tv_usec;
95 lru->ru_stime.tv_sec = ru->ru_stime.tv_sec;
96 lru->ru_stime.tv_usec = ru->ru_stime.tv_usec;
97 lru->ru_maxrss = ru->ru_maxrss;
98 lru->ru_ixrss = ru->ru_ixrss;
99 lru->ru_idrss = ru->ru_idrss;
100 lru->ru_isrss = ru->ru_isrss;
101 lru->ru_minflt = ru->ru_minflt;
102 lru->ru_majflt = ru->ru_majflt;
103 lru->ru_nswap = ru->ru_nswap;
104 lru->ru_inblock = ru->ru_inblock;
105 lru->ru_oublock = ru->ru_oublock;
106 lru->ru_msgsnd = ru->ru_msgsnd;
107 lru->ru_msgrcv = ru->ru_msgrcv;
108 lru->ru_nsignals = ru->ru_nsignals;
109 lru->ru_nvcsw = ru->ru_nvcsw;
110 lru->ru_nivcsw = ru->ru_nivcsw;
111 }
112
113 int
114 linux_copyout_rusage(struct rusage *ru, void *uaddr)
115 {
116 struct l_rusage lru;
117
118 bsd_to_linux_rusage(ru, &lru);
119
120 return (copyout(&lru, uaddr, sizeof(struct l_rusage)));
121 }
122
123 int
124 linux_execve(struct thread *td, struct linux_execve_args *args)
125 {
126 struct image_args eargs;
127 char *path;
128 int error;
129
130 LCONVPATHEXIST(td, args->path, &path);
131
132 #ifdef DEBUG
133 if (ldebug(execve))
134 printf(ARGS(execve, "%s"), path);
135 #endif
136
137 error = freebsd32_exec_copyin_args(&eargs, path, UIO_SYSSPACE,
138 args->argp, args->envp);
139 free(path, M_TEMP);
140 if (error == 0)
141 error = linux_common_execve(td, &eargs);
142 return (error);
143 }
144
145 CTASSERT(sizeof(struct l_iovec32) == 8);
146
147 int
148 linux32_copyinuio(struct l_iovec32 *iovp, l_ulong iovcnt, struct uio **uiop)
149 {
150 struct l_iovec32 iov32;
151 struct iovec *iov;
152 struct uio *uio;
153 uint32_t iovlen;
154 int error, i;
155
156 *uiop = NULL;
157 if (iovcnt > UIO_MAXIOV)
158 return (EINVAL);
159 iovlen = iovcnt * sizeof(struct iovec);
160 uio = malloc(iovlen + sizeof(*uio), M_IOV, M_WAITOK);
161 iov = (struct iovec *)(uio + 1);
162 for (i = 0; i < iovcnt; i++) {
163 error = copyin(&iovp[i], &iov32, sizeof(struct l_iovec32));
164 if (error) {
165 free(uio, M_IOV);
166 return (error);
167 }
168 iov[i].iov_base = PTRIN(iov32.iov_base);
169 iov[i].iov_len = iov32.iov_len;
170 }
171 uio->uio_iov = iov;
172 uio->uio_iovcnt = iovcnt;
173 uio->uio_segflg = UIO_USERSPACE;
174 uio->uio_offset = -1;
175 uio->uio_resid = 0;
176 for (i = 0; i < iovcnt; i++) {
177 if (iov->iov_len > INT_MAX - uio->uio_resid) {
178 free(uio, M_IOV);
179 return (EINVAL);
180 }
181 uio->uio_resid += iov->iov_len;
182 iov++;
183 }
184 *uiop = uio;
185 return (0);
186 }
187
188 int
189 linux32_copyiniov(struct l_iovec32 *iovp32, l_ulong iovcnt, struct iovec **iovp,
190 int error)
191 {
192 struct l_iovec32 iov32;
193 struct iovec *iov;
194 uint32_t iovlen;
195 int i;
196
197 *iovp = NULL;
198 if (iovcnt > UIO_MAXIOV)
199 return (error);
200 iovlen = iovcnt * sizeof(struct iovec);
201 iov = malloc(iovlen, M_IOV, M_WAITOK);
202 for (i = 0; i < iovcnt; i++) {
203 error = copyin(&iovp32[i], &iov32, sizeof(struct l_iovec32));
204 if (error) {
205 free(iov, M_IOV);
206 return (error);
207 }
208 iov[i].iov_base = PTRIN(iov32.iov_base);
209 iov[i].iov_len = iov32.iov_len;
210 }
211 *iovp = iov;
212 return(0);
213
214 }
215
216 int
217 linux_readv(struct thread *td, struct linux_readv_args *uap)
218 {
219 struct uio *auio;
220 int error;
221
222 error = linux32_copyinuio(uap->iovp, uap->iovcnt, &auio);
223 if (error)
224 return (error);
225 error = kern_readv(td, uap->fd, auio);
226 free(auio, M_IOV);
227 return (error);
228 }
229
230 int
231 linux_writev(struct thread *td, struct linux_writev_args *uap)
232 {
233 struct uio *auio;
234 int error;
235
236 error = linux32_copyinuio(uap->iovp, uap->iovcnt, &auio);
237 if (error)
238 return (error);
239 error = kern_writev(td, uap->fd, auio);
240 free(auio, M_IOV);
241 return (error);
242 }
243
244 struct l_ipc_kludge {
245 l_uintptr_t msgp;
246 l_long msgtyp;
247 } __packed;
248
249 int
250 linux_ipc(struct thread *td, struct linux_ipc_args *args)
251 {
252
253 switch (args->what & 0xFFFF) {
254 case LINUX_SEMOP: {
255 struct linux_semop_args a;
256
257 a.semid = args->arg1;
258 a.tsops = PTRIN(args->ptr);
259 a.nsops = args->arg2;
260 return (linux_semop(td, &a));
261 }
262 case LINUX_SEMGET: {
263 struct linux_semget_args a;
264
265 a.key = args->arg1;
266 a.nsems = args->arg2;
267 a.semflg = args->arg3;
268 return (linux_semget(td, &a));
269 }
270 case LINUX_SEMCTL: {
271 struct linux_semctl_args a;
272 int error;
273
274 a.semid = args->arg1;
275 a.semnum = args->arg2;
276 a.cmd = args->arg3;
277 error = copyin(PTRIN(args->ptr), &a.arg, sizeof(a.arg));
278 if (error)
279 return (error);
280 return (linux_semctl(td, &a));
281 }
282 case LINUX_MSGSND: {
283 struct linux_msgsnd_args a;
284
285 a.msqid = args->arg1;
286 a.msgp = PTRIN(args->ptr);
287 a.msgsz = args->arg2;
288 a.msgflg = args->arg3;
289 return (linux_msgsnd(td, &a));
290 }
291 case LINUX_MSGRCV: {
292 struct linux_msgrcv_args a;
293
294 a.msqid = args->arg1;
295 a.msgsz = args->arg2;
296 a.msgflg = args->arg3;
297 if ((args->what >> 16) == 0) {
298 struct l_ipc_kludge tmp;
299 int error;
300
301 if (args->ptr == 0)
302 return (EINVAL);
303 error = copyin(PTRIN(args->ptr), &tmp, sizeof(tmp));
304 if (error)
305 return (error);
306 a.msgp = PTRIN(tmp.msgp);
307 a.msgtyp = tmp.msgtyp;
308 } else {
309 a.msgp = PTRIN(args->ptr);
310 a.msgtyp = args->arg5;
311 }
312 return (linux_msgrcv(td, &a));
313 }
314 case LINUX_MSGGET: {
315 struct linux_msgget_args a;
316
317 a.key = args->arg1;
318 a.msgflg = args->arg2;
319 return (linux_msgget(td, &a));
320 }
321 case LINUX_MSGCTL: {
322 struct linux_msgctl_args a;
323
324 a.msqid = args->arg1;
325 a.cmd = args->arg2;
326 a.buf = PTRIN(args->ptr);
327 return (linux_msgctl(td, &a));
328 }
329 case LINUX_SHMAT: {
330 struct linux_shmat_args a;
331 l_uintptr_t addr;
332 int error;
333
334 a.shmid = args->arg1;
335 a.shmaddr = PTRIN(args->ptr);
336 a.shmflg = args->arg2;
337 error = linux_shmat(td, &a);
338 if (error != 0)
339 return (error);
340 addr = td->td_retval[0];
341 error = copyout(&addr, PTRIN(args->arg3), sizeof(addr));
342 td->td_retval[0] = 0;
343 return (error);
344 }
345 case LINUX_SHMDT: {
346 struct linux_shmdt_args a;
347
348 a.shmaddr = PTRIN(args->ptr);
349 return (linux_shmdt(td, &a));
350 }
351 case LINUX_SHMGET: {
352 struct linux_shmget_args a;
353
354 a.key = args->arg1;
355 a.size = args->arg2;
356 a.shmflg = args->arg3;
357 return (linux_shmget(td, &a));
358 }
359 case LINUX_SHMCTL: {
360 struct linux_shmctl_args a;
361
362 a.shmid = args->arg1;
363 a.cmd = args->arg2;
364 a.buf = PTRIN(args->ptr);
365 return (linux_shmctl(td, &a));
366 }
367 default:
368 break;
369 }
370
371 return (EINVAL);
372 }
373
374 int
375 linux_old_select(struct thread *td, struct linux_old_select_args *args)
376 {
377 struct l_old_select_argv linux_args;
378 struct linux_select_args newsel;
379 int error;
380
381 #ifdef DEBUG
382 if (ldebug(old_select))
383 printf(ARGS(old_select, "%p"), args->ptr);
384 #endif
385
386 error = copyin(args->ptr, &linux_args, sizeof(linux_args));
387 if (error)
388 return (error);
389
390 newsel.nfds = linux_args.nfds;
391 newsel.readfds = PTRIN(linux_args.readfds);
392 newsel.writefds = PTRIN(linux_args.writefds);
393 newsel.exceptfds = PTRIN(linux_args.exceptfds);
394 newsel.timeout = PTRIN(linux_args.timeout);
395 return (linux_select(td, &newsel));
396 }
397
398 int
399 linux_set_cloned_tls(struct thread *td, void *desc)
400 {
401 struct user_segment_descriptor sd;
402 struct l_user_desc info;
403 struct pcb *pcb;
404 int error;
405 int a[2];
406
407 error = copyin(desc, &info, sizeof(struct l_user_desc));
408 if (error) {
409 printf(LMSG("copyin failed!"));
410 } else {
411 /* We might copy out the entry_number as GUGS32_SEL. */
412 info.entry_number = GUGS32_SEL;
413 error = copyout(&info, desc, sizeof(struct l_user_desc));
414 if (error)
415 printf(LMSG("copyout failed!"));
416
417 a[0] = LINUX_LDT_entry_a(&info);
418 a[1] = LINUX_LDT_entry_b(&info);
419
420 memcpy(&sd, &a, sizeof(a));
421 #ifdef DEBUG
422 if (ldebug(clone))
423 printf("Segment created in clone with "
424 "CLONE_SETTLS: lobase: %x, hibase: %x, "
425 "lolimit: %x, hilimit: %x, type: %i, "
426 "dpl: %i, p: %i, xx: %i, long: %i, "
427 "def32: %i, gran: %i\n", sd.sd_lobase,
428 sd.sd_hibase, sd.sd_lolimit, sd.sd_hilimit,
429 sd.sd_type, sd.sd_dpl, sd.sd_p, sd.sd_xx,
430 sd.sd_long, sd.sd_def32, sd.sd_gran);
431 #endif
432 pcb = td->td_pcb;
433 pcb->pcb_gsbase = (register_t)info.base_addr;
434 td->td_frame->tf_gs = GSEL(GUGS32_SEL, SEL_UPL);
435 set_pcb_flags(pcb, PCB_32BIT);
436 }
437
438 return (error);
439 }
440
441 int
442 linux_set_upcall_kse(struct thread *td, register_t stack)
443 {
444
445 if (stack)
446 td->td_frame->tf_rsp = stack;
447
448 /*
449 * The newly created Linux thread returns
450 * to the user space by the same path that a parent do.
451 */
452 td->td_frame->tf_rax = 0;
453 return (0);
454 }
455
456 int
457 linux_mmap2(struct thread *td, struct linux_mmap2_args *args)
458 {
459
460 #ifdef DEBUG
461 if (ldebug(mmap2))
462 printf(ARGS(mmap2, "0x%08x, %d, %d, 0x%08x, %d, %d"),
463 args->addr, args->len, args->prot,
464 args->flags, args->fd, args->pgoff);
465 #endif
466
467 return (linux_mmap_common(td, PTROUT(args->addr), args->len, args->prot,
468 args->flags, args->fd, (uint64_t)(uint32_t)args->pgoff *
469 PAGE_SIZE));
470 }
471
472 int
473 linux_mmap(struct thread *td, struct linux_mmap_args *args)
474 {
475 int error;
476 struct l_mmap_argv linux_args;
477
478 error = copyin(args->ptr, &linux_args, sizeof(linux_args));
479 if (error)
480 return (error);
481
482 #ifdef DEBUG
483 if (ldebug(mmap))
484 printf(ARGS(mmap, "0x%08x, %d, %d, 0x%08x, %d, %d"),
485 linux_args.addr, linux_args.len, linux_args.prot,
486 linux_args.flags, linux_args.fd, linux_args.pgoff);
487 #endif
488
489 return (linux_mmap_common(td, linux_args.addr, linux_args.len,
490 linux_args.prot, linux_args.flags, linux_args.fd,
491 (uint32_t)linux_args.pgoff));
492 }
493
494 int
495 linux_mprotect(struct thread *td, struct linux_mprotect_args *uap)
496 {
497
498 return (linux_mprotect_common(td, PTROUT(uap->addr), uap->len, uap->prot));
499 }
500
501 int
502 linux_iopl(struct thread *td, struct linux_iopl_args *args)
503 {
504 int error;
505
506 if (args->level < 0 || args->level > 3)
507 return (EINVAL);
508 if ((error = priv_check(td, PRIV_IO)) != 0)
509 return (error);
510 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
511 return (error);
512 td->td_frame->tf_rflags = (td->td_frame->tf_rflags & ~PSL_IOPL) |
513 (args->level * (PSL_IOPL / 3));
514
515 return (0);
516 }
517
518 int
519 linux_sigaction(struct thread *td, struct linux_sigaction_args *args)
520 {
521 l_osigaction_t osa;
522 l_sigaction_t act, oact;
523 int error;
524
525 #ifdef DEBUG
526 if (ldebug(sigaction))
527 printf(ARGS(sigaction, "%d, %p, %p"),
528 args->sig, (void *)args->nsa, (void *)args->osa);
529 #endif
530
531 if (args->nsa != NULL) {
532 error = copyin(args->nsa, &osa, sizeof(l_osigaction_t));
533 if (error)
534 return (error);
535 act.lsa_handler = osa.lsa_handler;
536 act.lsa_flags = osa.lsa_flags;
537 act.lsa_restorer = osa.lsa_restorer;
538 LINUX_SIGEMPTYSET(act.lsa_mask);
539 act.lsa_mask.__mask = osa.lsa_mask;
540 }
541
542 error = linux_do_sigaction(td, args->sig, args->nsa ? &act : NULL,
543 args->osa ? &oact : NULL);
544
545 if (args->osa != NULL && !error) {
546 osa.lsa_handler = oact.lsa_handler;
547 osa.lsa_flags = oact.lsa_flags;
548 osa.lsa_restorer = oact.lsa_restorer;
549 osa.lsa_mask = oact.lsa_mask.__mask;
550 error = copyout(&osa, args->osa, sizeof(l_osigaction_t));
551 }
552
553 return (error);
554 }
555
556 /*
557 * Linux has two extra args, restart and oldmask. We don't use these,
558 * but it seems that "restart" is actually a context pointer that
559 * enables the signal to happen with a different register set.
560 */
561 int
562 linux_sigsuspend(struct thread *td, struct linux_sigsuspend_args *args)
563 {
564 sigset_t sigmask;
565 l_sigset_t mask;
566
567 #ifdef DEBUG
568 if (ldebug(sigsuspend))
569 printf(ARGS(sigsuspend, "%08lx"), (unsigned long)args->mask);
570 #endif
571
572 LINUX_SIGEMPTYSET(mask);
573 mask.__mask = args->mask;
574 linux_to_bsd_sigset(&mask, &sigmask);
575 return (kern_sigsuspend(td, sigmask));
576 }
577
578 int
579 linux_rt_sigsuspend(struct thread *td, struct linux_rt_sigsuspend_args *uap)
580 {
581 l_sigset_t lmask;
582 sigset_t sigmask;
583 int error;
584
585 #ifdef DEBUG
586 if (ldebug(rt_sigsuspend))
587 printf(ARGS(rt_sigsuspend, "%p, %d"),
588 (void *)uap->newset, uap->sigsetsize);
589 #endif
590
591 if (uap->sigsetsize != sizeof(l_sigset_t))
592 return (EINVAL);
593
594 error = copyin(uap->newset, &lmask, sizeof(l_sigset_t));
595 if (error)
596 return (error);
597
598 linux_to_bsd_sigset(&lmask, &sigmask);
599 return (kern_sigsuspend(td, sigmask));
600 }
601
602 int
603 linux_pause(struct thread *td, struct linux_pause_args *args)
604 {
605 struct proc *p = td->td_proc;
606 sigset_t sigmask;
607
608 #ifdef DEBUG
609 if (ldebug(pause))
610 printf(ARGS(pause, ""));
611 #endif
612
613 PROC_LOCK(p);
614 sigmask = td->td_sigmask;
615 PROC_UNLOCK(p);
616 return (kern_sigsuspend(td, sigmask));
617 }
618
619 int
620 linux_sigaltstack(struct thread *td, struct linux_sigaltstack_args *uap)
621 {
622 stack_t ss, oss;
623 l_stack_t lss;
624 int error;
625
626 #ifdef DEBUG
627 if (ldebug(sigaltstack))
628 printf(ARGS(sigaltstack, "%p, %p"), uap->uss, uap->uoss);
629 #endif
630
631 if (uap->uss != NULL) {
632 error = copyin(uap->uss, &lss, sizeof(l_stack_t));
633 if (error)
634 return (error);
635
636 ss.ss_sp = PTRIN(lss.ss_sp);
637 ss.ss_size = lss.ss_size;
638 ss.ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags);
639 }
640 error = kern_sigaltstack(td, (uap->uss != NULL) ? &ss : NULL,
641 (uap->uoss != NULL) ? &oss : NULL);
642 if (!error && uap->uoss != NULL) {
643 lss.ss_sp = PTROUT(oss.ss_sp);
644 lss.ss_size = oss.ss_size;
645 lss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags);
646 error = copyout(&lss, uap->uoss, sizeof(l_stack_t));
647 }
648
649 return (error);
650 }
651
652 int
653 linux_ftruncate64(struct thread *td, struct linux_ftruncate64_args *args)
654 {
655
656 #ifdef DEBUG
657 if (ldebug(ftruncate64))
658 printf(ARGS(ftruncate64, "%u, %jd"), args->fd,
659 (intmax_t)args->length);
660 #endif
661
662 return (kern_ftruncate(td, args->fd, args->length));
663 }
664
665 int
666 linux_gettimeofday(struct thread *td, struct linux_gettimeofday_args *uap)
667 {
668 struct timeval atv;
669 l_timeval atv32;
670 struct timezone rtz;
671 int error = 0;
672
673 if (uap->tp) {
674 microtime(&atv);
675 atv32.tv_sec = atv.tv_sec;
676 atv32.tv_usec = atv.tv_usec;
677 error = copyout(&atv32, uap->tp, sizeof(atv32));
678 }
679 if (error == 0 && uap->tzp != NULL) {
680 rtz.tz_minuteswest = tz_minuteswest;
681 rtz.tz_dsttime = tz_dsttime;
682 error = copyout(&rtz, uap->tzp, sizeof(rtz));
683 }
684 return (error);
685 }
686
687 int
688 linux_settimeofday(struct thread *td, struct linux_settimeofday_args *uap)
689 {
690 l_timeval atv32;
691 struct timeval atv, *tvp;
692 struct timezone atz, *tzp;
693 int error;
694
695 if (uap->tp) {
696 error = copyin(uap->tp, &atv32, sizeof(atv32));
697 if (error)
698 return (error);
699 atv.tv_sec = atv32.tv_sec;
700 atv.tv_usec = atv32.tv_usec;
701 tvp = &atv;
702 } else
703 tvp = NULL;
704 if (uap->tzp) {
705 error = copyin(uap->tzp, &atz, sizeof(atz));
706 if (error)
707 return (error);
708 tzp = &atz;
709 } else
710 tzp = NULL;
711 return (kern_settimeofday(td, tvp, tzp));
712 }
713
714 int
715 linux_getrusage(struct thread *td, struct linux_getrusage_args *uap)
716 {
717 struct rusage s;
718 int error;
719
720 error = kern_getrusage(td, uap->who, &s);
721 if (error != 0)
722 return (error);
723 if (uap->rusage != NULL)
724 error = linux_copyout_rusage(&s, uap->rusage);
725 return (error);
726 }
727
728 int
729 linux_set_thread_area(struct thread *td,
730 struct linux_set_thread_area_args *args)
731 {
732 struct l_user_desc info;
733 struct user_segment_descriptor sd;
734 struct pcb *pcb;
735 int a[2];
736 int error;
737
738 error = copyin(args->desc, &info, sizeof(struct l_user_desc));
739 if (error)
740 return (error);
741
742 #ifdef DEBUG
743 if (ldebug(set_thread_area))
744 printf(ARGS(set_thread_area, "%i, %x, %x, %i, %i, %i, "
745 "%i, %i, %i"), info.entry_number, info.base_addr,
746 info.limit, info.seg_32bit, info.contents,
747 info.read_exec_only, info.limit_in_pages,
748 info.seg_not_present, info.useable);
749 #endif
750
751 /*
752 * Semantics of Linux version: every thread in the system has array
753 * of three TLS descriptors. 1st is GLIBC TLS, 2nd is WINE, 3rd unknown.
754 * This syscall loads one of the selected TLS decriptors with a value
755 * and also loads GDT descriptors 6, 7 and 8 with the content of
756 * the per-thread descriptors.
757 *
758 * Semantics of FreeBSD version: I think we can ignore that Linux has
759 * three per-thread descriptors and use just the first one.
760 * The tls_array[] is used only in [gs]et_thread_area() syscalls and
761 * for loading the GDT descriptors. We use just one GDT descriptor
762 * for TLS, so we will load just one.
763 *
764 * XXX: This doesn't work when a user space process tries to use more
765 * than one TLS segment. Comment in the Linux source says wine might
766 * do this.
767 */
768
769 /*
770 * GLIBC reads current %gs and call set_thread_area() with it.
771 * We should let GUDATA_SEL and GUGS32_SEL proceed as well because
772 * we use these segments.
773 */
774 switch (info.entry_number) {
775 case GUGS32_SEL:
776 case GUDATA_SEL:
777 case 6:
778 case -1:
779 info.entry_number = GUGS32_SEL;
780 break;
781 default:
782 return (EINVAL);
783 }
784
785 /*
786 * We have to copy out the GDT entry we use.
787 *
788 * XXX: What if a user space program does not check the return value
789 * and tries to use 6, 7 or 8?
790 */
791 error = copyout(&info, args->desc, sizeof(struct l_user_desc));
792 if (error)
793 return (error);
794
795 if (LINUX_LDT_empty(&info)) {
796 a[0] = 0;
797 a[1] = 0;
798 } else {
799 a[0] = LINUX_LDT_entry_a(&info);
800 a[1] = LINUX_LDT_entry_b(&info);
801 }
802
803 memcpy(&sd, &a, sizeof(a));
804 #ifdef DEBUG
805 if (ldebug(set_thread_area))
806 printf("Segment created in set_thread_area: "
807 "lobase: %x, hibase: %x, lolimit: %x, hilimit: %x, "
808 "type: %i, dpl: %i, p: %i, xx: %i, long: %i, "
809 "def32: %i, gran: %i\n",
810 sd.sd_lobase,
811 sd.sd_hibase,
812 sd.sd_lolimit,
813 sd.sd_hilimit,
814 sd.sd_type,
815 sd.sd_dpl,
816 sd.sd_p,
817 sd.sd_xx,
818 sd.sd_long,
819 sd.sd_def32,
820 sd.sd_gran);
821 #endif
822
823 pcb = td->td_pcb;
824 pcb->pcb_gsbase = (register_t)info.base_addr;
825 set_pcb_flags(pcb, PCB_32BIT);
826 update_gdt_gsbase(td, info.base_addr);
827
828 return (0);
829 }
Cache object: c9b067a2fd462acb9823184099aa327f
|