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