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