xref: /freebsd/sys/i386/linux/linux_machdep.c (revision 553b1a4e)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 "opt_posix.h"
30 
31 #include <sys/param.h>
32 #include <sys/imgact_aout.h>
33 #include <sys/fcntl.h>
34 #include <sys/lock.h>
35 #include <sys/malloc.h>
36 #include <sys/mman.h>
37 #include <sys/mutex.h>
38 #include <sys/namei.h>
39 #include <sys/priv.h>
40 #include <sys/proc.h>
41 #include <sys/racct.h>
42 #include <sys/resource.h>
43 #include <sys/resourcevar.h>
44 #include <sys/syscallsubr.h>
45 #include <sys/sysproto.h>
46 #include <sys/vnode.h>
47 
48 #include <security/audit/audit.h>
49 #include <security/mac/mac_framework.h>
50 
51 #include <machine/frame.h>
52 #include <machine/pcb.h>			/* needed for pcb definition in linux_set_thread_area */
53 #include <machine/psl.h>
54 #include <machine/segments.h>
55 #include <machine/sysarch.h>
56 
57 #include <vm/pmap.h>
58 #include <vm/vm.h>
59 #include <vm/vm_extern.h>
60 #include <vm/vm_kern.h>
61 #include <vm/vm_map.h>
62 #include <vm/vm_param.h>
63 
64 #include <x86/reg.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_fork.h>
70 #include <compat/linux/linux_ipc.h>
71 #include <compat/linux/linux_misc.h>
72 #include <compat/linux/linux_mmap.h>
73 #include <compat/linux/linux_signal.h>
74 #include <compat/linux/linux_util.h>
75 
76 
77 struct l_descriptor {
78 	l_uint		entry_number;
79 	l_ulong		base_addr;
80 	l_uint		limit;
81 	l_uint		seg_32bit:1;
82 	l_uint		contents:2;
83 	l_uint		read_exec_only:1;
84 	l_uint		limit_in_pages:1;
85 	l_uint		seg_not_present:1;
86 	l_uint		useable:1;
87 };
88 
89 struct l_old_select_argv {
90 	l_int		nfds;
91 	l_fd_set	*readfds;
92 	l_fd_set	*writefds;
93 	l_fd_set	*exceptfds;
94 	struct l_timeval	*timeout;
95 };
96 
97 struct l_ipc_kludge {
98 	struct l_msgbuf *msgp;
99 	l_long msgtyp;
100 };
101 
102 int
103 linux_ipc(struct thread *td, struct linux_ipc_args *args)
104 {
105 
106 	switch (args->what & 0xFFFF) {
107 	case LINUX_SEMOP: {
108 
109 		return (kern_semop(td, args->arg1, PTRIN(args->ptr),
110 		    args->arg2, NULL));
111 	}
112 	case LINUX_SEMGET: {
113 		struct linux_semget_args a;
114 
115 		a.key = args->arg1;
116 		a.nsems = args->arg2;
117 		a.semflg = args->arg3;
118 		return (linux_semget(td, &a));
119 	}
120 	case LINUX_SEMCTL: {
121 		struct linux_semctl_args a;
122 		int error;
123 
124 		a.semid = args->arg1;
125 		a.semnum = args->arg2;
126 		a.cmd = args->arg3;
127 		error = copyin(PTRIN(args->ptr), &a.arg, sizeof(a.arg));
128 		if (error)
129 			return (error);
130 		return (linux_semctl(td, &a));
131 	}
132 	case LINUX_SEMTIMEDOP: {
133 		struct linux_semtimedop_args a;
134 
135 		a.semid = args->arg1;
136 		a.tsops = PTRIN(args->ptr);
137 		a.nsops = args->arg2;
138 		a.timeout = PTRIN(args->arg5);
139 		return (linux_semtimedop(td, &a));
140 	}
141 	case LINUX_MSGSND: {
142 		struct linux_msgsnd_args a;
143 
144 		a.msqid = args->arg1;
145 		a.msgp = PTRIN(args->ptr);
146 		a.msgsz = args->arg2;
147 		a.msgflg = args->arg3;
148 		return (linux_msgsnd(td, &a));
149 	}
150 	case LINUX_MSGRCV: {
151 		struct linux_msgrcv_args a;
152 
153 		a.msqid = args->arg1;
154 		a.msgsz = args->arg2;
155 		a.msgflg = args->arg3;
156 		if ((args->what >> 16) == 0) {
157 			struct l_ipc_kludge tmp;
158 			int error;
159 
160 			if (args->ptr == 0)
161 				return (EINVAL);
162 			error = copyin(PTRIN(args->ptr), &tmp, sizeof(tmp));
163 			if (error)
164 				return (error);
165 			a.msgp = PTRIN(tmp.msgp);
166 			a.msgtyp = tmp.msgtyp;
167 		} else {
168 			a.msgp = PTRIN(args->ptr);
169 			a.msgtyp = args->arg5;
170 		}
171 		return (linux_msgrcv(td, &a));
172 	}
173 	case LINUX_MSGGET: {
174 		struct linux_msgget_args a;
175 
176 		a.key = args->arg1;
177 		a.msgflg = args->arg2;
178 		return (linux_msgget(td, &a));
179 	}
180 	case LINUX_MSGCTL: {
181 		struct linux_msgctl_args a;
182 
183 		a.msqid = args->arg1;
184 		a.cmd = args->arg2;
185 		a.buf = PTRIN(args->ptr);
186 		return (linux_msgctl(td, &a));
187 	}
188 	case LINUX_SHMAT: {
189 		struct linux_shmat_args a;
190 		l_uintptr_t addr;
191 		int error;
192 
193 		a.shmid = args->arg1;
194 		a.shmaddr = PTRIN(args->ptr);
195 		a.shmflg = args->arg2;
196 		error = linux_shmat(td, &a);
197 		if (error != 0)
198 			return (error);
199 		addr = td->td_retval[0];
200 		error = copyout(&addr, PTRIN(args->arg3), sizeof(addr));
201 		td->td_retval[0] = 0;
202 		return (error);
203 	}
204 	case LINUX_SHMDT: {
205 		struct linux_shmdt_args a;
206 
207 		a.shmaddr = PTRIN(args->ptr);
208 		return (linux_shmdt(td, &a));
209 	}
210 	case LINUX_SHMGET: {
211 		struct linux_shmget_args a;
212 
213 		a.key = args->arg1;
214 		a.size = args->arg2;
215 		a.shmflg = args->arg3;
216 		return (linux_shmget(td, &a));
217 	}
218 	case LINUX_SHMCTL: {
219 		struct linux_shmctl_args a;
220 
221 		a.shmid = args->arg1;
222 		a.cmd = args->arg2;
223 		a.buf = PTRIN(args->ptr);
224 		return (linux_shmctl(td, &a));
225 	}
226 	default:
227 		break;
228 	}
229 
230 	return (EINVAL);
231 }
232 
233 int
234 linux_old_select(struct thread *td, struct linux_old_select_args *args)
235 {
236 	struct l_old_select_argv linux_args;
237 	struct linux_select_args newsel;
238 	int error;
239 
240 	error = copyin(args->ptr, &linux_args, sizeof(linux_args));
241 	if (error)
242 		return (error);
243 
244 	newsel.nfds = linux_args.nfds;
245 	newsel.readfds = linux_args.readfds;
246 	newsel.writefds = linux_args.writefds;
247 	newsel.exceptfds = linux_args.exceptfds;
248 	newsel.timeout = linux_args.timeout;
249 	return (linux_select(td, &newsel));
250 }
251 
252 int
253 linux_set_cloned_tls(struct thread *td, void *desc)
254 {
255 	struct segment_descriptor sd;
256 	struct l_user_desc info;
257 	int idx, error;
258 	int a[2];
259 
260 	error = copyin(desc, &info, sizeof(struct l_user_desc));
261 	if (error) {
262 		linux_msg(td, "set_cloned_tls copyin failed!");
263 	} else {
264 		idx = info.entry_number;
265 
266 		/*
267 		 * looks like we're getting the idx we returned
268 		 * in the set_thread_area() syscall
269 		 */
270 		if (idx != 6 && idx != 3) {
271 			linux_msg(td, "set_cloned_tls resetting idx!");
272 			idx = 3;
273 		}
274 
275 		/* this doesnt happen in practice */
276 		if (idx == 6) {
277 			/* we might copy out the entry_number as 3 */
278 			info.entry_number = 3;
279 			error = copyout(&info, desc, sizeof(struct l_user_desc));
280 			if (error)
281 				linux_msg(td, "set_cloned_tls copyout failed!");
282 		}
283 
284 		a[0] = LINUX_LDT_entry_a(&info);
285 		a[1] = LINUX_LDT_entry_b(&info);
286 
287 		memcpy(&sd, &a, sizeof(a));
288 		/* set %gs */
289 		td->td_pcb->pcb_gsd = sd;
290 		td->td_pcb->pcb_gs = GSEL(GUGS_SEL, SEL_UPL);
291 	}
292 
293 	return (error);
294 }
295 
296 int
297 linux_set_upcall(struct thread *td, register_t stack)
298 {
299 
300 	if (stack)
301 		td->td_frame->tf_esp = stack;
302 
303 	/*
304 	 * The newly created Linux thread returns
305 	 * to the user space by the same path that a parent do.
306 	 */
307 	td->td_frame->tf_eax = 0;
308 	return (0);
309 }
310 
311 int
312 linux_mmap2(struct thread *td, struct linux_mmap2_args *args)
313 {
314 
315 	return (linux_mmap_common(td, args->addr, args->len, args->prot,
316 		args->flags, args->fd, (uint64_t)(uint32_t)args->pgoff *
317 		PAGE_SIZE));
318 }
319 
320 int
321 linux_mmap(struct thread *td, struct linux_mmap_args *args)
322 {
323 	int error;
324 	struct l_mmap_argv linux_args;
325 
326 	error = copyin(args->ptr, &linux_args, sizeof(linux_args));
327 	if (error)
328 		return (error);
329 
330 	return (linux_mmap_common(td, linux_args.addr, linux_args.len,
331 	    linux_args.prot, linux_args.flags, linux_args.fd,
332 	    (uint32_t)linux_args.pgoff));
333 }
334 
335 int
336 linux_ioperm(struct thread *td, struct linux_ioperm_args *args)
337 {
338 	int error;
339 	struct i386_ioperm_args iia;
340 
341 	iia.start = args->start;
342 	iia.length = args->length;
343 	iia.enable = args->enable;
344 	error = i386_set_ioperm(td, &iia);
345 	return (error);
346 }
347 
348 int
349 linux_iopl(struct thread *td, struct linux_iopl_args *args)
350 {
351 	int error;
352 
353 	if (args->level < 0 || args->level > 3)
354 		return (EINVAL);
355 	if ((error = priv_check(td, PRIV_IO)) != 0)
356 		return (error);
357 	if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
358 		return (error);
359 	td->td_frame->tf_eflags = (td->td_frame->tf_eflags & ~PSL_IOPL) |
360 	    (args->level * (PSL_IOPL / 3));
361 	return (0);
362 }
363 
364 int
365 linux_modify_ldt(struct thread *td, struct linux_modify_ldt_args *uap)
366 {
367 	int error;
368 	struct i386_ldt_args ldt;
369 	struct l_descriptor ld;
370 	union descriptor desc;
371 	int size, written;
372 
373 	switch (uap->func) {
374 	case 0x00: /* read_ldt */
375 		ldt.start = 0;
376 		ldt.descs = uap->ptr;
377 		ldt.num = uap->bytecount / sizeof(union descriptor);
378 		error = i386_get_ldt(td, &ldt);
379 		td->td_retval[0] *= sizeof(union descriptor);
380 		break;
381 	case 0x02: /* read_default_ldt = 0 */
382 		size = 5*sizeof(struct l_desc_struct);
383 		if (size > uap->bytecount)
384 			size = uap->bytecount;
385 		for (written = error = 0; written < size && error == 0; written++)
386 			error = subyte((char *)uap->ptr + written, 0);
387 		td->td_retval[0] = written;
388 		break;
389 	case 0x01: /* write_ldt */
390 	case 0x11: /* write_ldt */
391 		if (uap->bytecount != sizeof(ld))
392 			return (EINVAL);
393 
394 		error = copyin(uap->ptr, &ld, sizeof(ld));
395 		if (error)
396 			return (error);
397 
398 		ldt.start = ld.entry_number;
399 		ldt.descs = &desc;
400 		ldt.num = 1;
401 		desc.sd.sd_lolimit = (ld.limit & 0x0000ffff);
402 		desc.sd.sd_hilimit = (ld.limit & 0x000f0000) >> 16;
403 		desc.sd.sd_lobase = (ld.base_addr & 0x00ffffff);
404 		desc.sd.sd_hibase = (ld.base_addr & 0xff000000) >> 24;
405 		desc.sd.sd_type = SDT_MEMRO | ((ld.read_exec_only ^ 1) << 1) |
406 			(ld.contents << 2);
407 		desc.sd.sd_dpl = 3;
408 		desc.sd.sd_p = (ld.seg_not_present ^ 1);
409 		desc.sd.sd_xx = 0;
410 		desc.sd.sd_def32 = ld.seg_32bit;
411 		desc.sd.sd_gran = ld.limit_in_pages;
412 		error = i386_set_ldt(td, &ldt, &desc);
413 		break;
414 	default:
415 		error = ENOSYS;
416 		break;
417 	}
418 
419 	if (error == EOPNOTSUPP) {
420 		linux_msg(td, "modify_ldt needs kernel option USER_LDT");
421 		error = ENOSYS;
422 	}
423 
424 	return (error);
425 }
426 
427 int
428 linux_sigaction(struct thread *td, struct linux_sigaction_args *args)
429 {
430 	l_osigaction_t osa;
431 	l_sigaction_t act, oact;
432 	int error;
433 
434 	if (args->nsa != NULL) {
435 		error = copyin(args->nsa, &osa, sizeof(l_osigaction_t));
436 		if (error)
437 			return (error);
438 		act.lsa_handler = osa.lsa_handler;
439 		act.lsa_flags = osa.lsa_flags;
440 		act.lsa_restorer = osa.lsa_restorer;
441 		LINUX_SIGEMPTYSET(act.lsa_mask);
442 		act.lsa_mask.__mask = osa.lsa_mask;
443 	}
444 
445 	error = linux_do_sigaction(td, args->sig, args->nsa ? &act : NULL,
446 	    args->osa ? &oact : NULL);
447 
448 	if (args->osa != NULL && !error) {
449 		osa.lsa_handler = oact.lsa_handler;
450 		osa.lsa_flags = oact.lsa_flags;
451 		osa.lsa_restorer = oact.lsa_restorer;
452 		osa.lsa_mask = oact.lsa_mask.__mask;
453 		error = copyout(&osa, args->osa, sizeof(l_osigaction_t));
454 	}
455 
456 	return (error);
457 }
458 
459 /*
460  * Linux has two extra args, restart and oldmask.  We dont use these,
461  * but it seems that "restart" is actually a context pointer that
462  * enables the signal to happen with a different register set.
463  */
464 int
465 linux_sigsuspend(struct thread *td, struct linux_sigsuspend_args *args)
466 {
467 	sigset_t sigmask;
468 	l_sigset_t mask;
469 
470 	LINUX_SIGEMPTYSET(mask);
471 	mask.__mask = args->mask;
472 	linux_to_bsd_sigset(&mask, &sigmask);
473 	return (kern_sigsuspend(td, sigmask));
474 }
475 
476 int
477 linux_pause(struct thread *td, struct linux_pause_args *args)
478 {
479 	struct proc *p = td->td_proc;
480 	sigset_t sigmask;
481 
482 	PROC_LOCK(p);
483 	sigmask = td->td_sigmask;
484 	PROC_UNLOCK(p);
485 	return (kern_sigsuspend(td, sigmask));
486 }
487 
488 int
489 linux_set_thread_area(struct thread *td, struct linux_set_thread_area_args *args)
490 {
491 	struct l_user_desc info;
492 	int error;
493 	int idx;
494 	int a[2];
495 	struct segment_descriptor sd;
496 
497 	error = copyin(args->desc, &info, sizeof(struct l_user_desc));
498 	if (error)
499 		return (error);
500 
501 	idx = info.entry_number;
502 	/*
503 	 * Semantics of Linux version: every thread in the system has array of
504 	 * 3 tls descriptors. 1st is GLIBC TLS, 2nd is WINE, 3rd unknown. This
505 	 * syscall loads one of the selected tls descriptors with a value and
506 	 * also loads GDT descriptors 6, 7 and 8 with the content of the
507 	 * per-thread descriptors.
508 	 *
509 	 * Semantics of FreeBSD version: I think we can ignore that Linux has 3
510 	 * per-thread descriptors and use just the 1st one. The tls_array[]
511 	 * is used only in set/get-thread_area() syscalls and for loading the
512 	 * GDT descriptors. In FreeBSD we use just one GDT descriptor for TLS
513 	 * so we will load just one.
514 	 *
515 	 * XXX: this doesn't work when a user space process tries to use more
516 	 * than 1 TLS segment. Comment in the Linux sources says wine might do
517 	 * this.
518 	 */
519 
520 	/*
521 	 * we support just GLIBC TLS now
522 	 * we should let 3 proceed as well because we use this segment so
523 	 * if code does two subsequent calls it should succeed
524 	 */
525 	if (idx != 6 && idx != -1 && idx != 3)
526 		return (EINVAL);
527 
528 	/*
529 	 * we have to copy out the GDT entry we use
530 	 * FreeBSD uses GDT entry #3 for storing %gs so load that
531 	 *
532 	 * XXX: what if a user space program doesn't check this value and tries
533 	 * to use 6, 7 or 8?
534 	 */
535 	idx = info.entry_number = 3;
536 	error = copyout(&info, args->desc, sizeof(struct l_user_desc));
537 	if (error)
538 		return (error);
539 
540 	if (LINUX_LDT_empty(&info)) {
541 		a[0] = 0;
542 		a[1] = 0;
543 	} else {
544 		a[0] = LINUX_LDT_entry_a(&info);
545 		a[1] = LINUX_LDT_entry_b(&info);
546 	}
547 
548 	memcpy(&sd, &a, sizeof(a));
549 	/* this is taken from i386 version of cpu_set_user_tls() */
550 	critical_enter();
551 	/* set %gs */
552 	td->td_pcb->pcb_gsd = sd;
553 	PCPU_GET(fsgs_gdt)[1] = sd;
554 	load_gs(GSEL(GUGS_SEL, SEL_UPL));
555 	critical_exit();
556 
557 	return (0);
558 }
559 
560 int
561 linux_get_thread_area(struct thread *td, struct linux_get_thread_area_args *args)
562 {
563 
564 	struct l_user_desc info;
565 	int error;
566 	int idx;
567 	struct l_desc_struct desc;
568 	struct segment_descriptor sd;
569 
570 	error = copyin(args->desc, &info, sizeof(struct l_user_desc));
571 	if (error)
572 		return (error);
573 
574 	idx = info.entry_number;
575 	/* XXX: I am not sure if we want 3 to be allowed too. */
576 	if (idx != 6 && idx != 3)
577 		return (EINVAL);
578 
579 	idx = 3;
580 
581 	memset(&info, 0, sizeof(info));
582 
583 	sd = PCPU_GET(fsgs_gdt)[1];
584 
585 	memcpy(&desc, &sd, sizeof(desc));
586 
587 	info.entry_number = idx;
588 	info.base_addr = LINUX_GET_BASE(&desc);
589 	info.limit = LINUX_GET_LIMIT(&desc);
590 	info.seg_32bit = LINUX_GET_32BIT(&desc);
591 	info.contents = LINUX_GET_CONTENTS(&desc);
592 	info.read_exec_only = !LINUX_GET_WRITABLE(&desc);
593 	info.limit_in_pages = LINUX_GET_LIMIT_PAGES(&desc);
594 	info.seg_not_present = !LINUX_GET_PRESENT(&desc);
595 	info.useable = LINUX_GET_USEABLE(&desc);
596 
597 	error = copyout(&info, args->desc, sizeof(struct l_user_desc));
598 	if (error)
599 		return (EFAULT);
600 
601 	return (0);
602 }
603 
604 /* XXX: this wont work with module - convert it */
605 int
606 linux_mq_open(struct thread *td, struct linux_mq_open_args *args)
607 {
608 #ifdef P1003_1B_MQUEUE
609 	return (sys_kmq_open(td, (struct kmq_open_args *)args));
610 #else
611 	return (ENOSYS);
612 #endif
613 }
614 
615 int
616 linux_mq_unlink(struct thread *td, struct linux_mq_unlink_args *args)
617 {
618 #ifdef P1003_1B_MQUEUE
619 	return (sys_kmq_unlink(td, (struct kmq_unlink_args *)args));
620 #else
621 	return (ENOSYS);
622 #endif
623 }
624 
625 int
626 linux_mq_timedsend(struct thread *td, struct linux_mq_timedsend_args *args)
627 {
628 #ifdef P1003_1B_MQUEUE
629 	return (sys_kmq_timedsend(td, (struct kmq_timedsend_args *)args));
630 #else
631 	return (ENOSYS);
632 #endif
633 }
634 
635 int
636 linux_mq_timedreceive(struct thread *td, struct linux_mq_timedreceive_args *args)
637 {
638 #ifdef P1003_1B_MQUEUE
639 	return (sys_kmq_timedreceive(td, (struct kmq_timedreceive_args *)args));
640 #else
641 	return (ENOSYS);
642 #endif
643 }
644 
645 int
646 linux_mq_notify(struct thread *td, struct linux_mq_notify_args *args)
647 {
648 #ifdef P1003_1B_MQUEUE
649 	return (sys_kmq_notify(td, (struct kmq_notify_args *)args));
650 #else
651 	return (ENOSYS);
652 #endif
653 }
654 
655 int
656 linux_mq_getsetattr(struct thread *td, struct linux_mq_getsetattr_args *args)
657 {
658 #ifdef P1003_1B_MQUEUE
659 	return (sys_kmq_setattr(td, (struct kmq_setattr_args *)args));
660 #else
661 	return (ENOSYS);
662 #endif
663 }
664 
665 void
666 bsd_to_linux_regset(const struct reg *b_reg,
667     struct linux_pt_regset *l_regset)
668 {
669 
670 	l_regset->ebx = b_reg->r_ebx;
671 	l_regset->ecx = b_reg->r_ecx;
672 	l_regset->edx = b_reg->r_edx;
673 	l_regset->esi = b_reg->r_esi;
674 	l_regset->edi = b_reg->r_edi;
675 	l_regset->ebp = b_reg->r_ebp;
676 	l_regset->eax = b_reg->r_eax;
677 	l_regset->ds = b_reg->r_ds;
678 	l_regset->es = b_reg->r_es;
679 	l_regset->fs = b_reg->r_fs;
680 	l_regset->gs = b_reg->r_gs;
681 	l_regset->orig_eax = b_reg->r_eax;
682 	l_regset->eip = b_reg->r_eip;
683 	l_regset->cs = b_reg->r_cs;
684 	l_regset->eflags = b_reg->r_eflags;
685 	l_regset->esp = b_reg->r_esp;
686 	l_regset->ss = b_reg->r_ss;
687 }
688 
689 int
690 linux_uselib(struct thread *td, struct linux_uselib_args *args)
691 {
692 	struct nameidata ni;
693 	struct vnode *vp;
694 	struct exec *a_out;
695 	vm_map_t map;
696 	vm_map_entry_t entry;
697 	struct vattr attr;
698 	vm_offset_t vmaddr;
699 	unsigned long file_offset;
700 	unsigned long bss_size;
701 	ssize_t aresid;
702 	int error;
703 	bool locked, opened, textset;
704 
705 	a_out = NULL;
706 	vp = NULL;
707 	locked = false;
708 	textset = false;
709 	opened = false;
710 
711 	NDINIT(&ni, LOOKUP, ISOPEN | FOLLOW | LOCKLEAF | AUDITVNODE1,
712 	    UIO_USERSPACE, args->library);
713 	error = namei(&ni);
714 	if (error)
715 		goto cleanup;
716 
717 	vp = ni.ni_vp;
718 	NDFREE_PNBUF(&ni);
719 
720 	/*
721 	 * From here on down, we have a locked vnode that must be unlocked.
722 	 * XXX: The code below largely duplicates exec_check_permissions().
723 	 */
724 	locked = true;
725 
726 	/* Executable? */
727 	error = VOP_GETATTR(vp, &attr, td->td_ucred);
728 	if (error)
729 		goto cleanup;
730 
731 	if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
732 	    ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
733 		/* EACCESS is what exec(2) returns. */
734 		error = ENOEXEC;
735 		goto cleanup;
736 	}
737 
738 	/* Sensible size? */
739 	if (attr.va_size == 0) {
740 		error = ENOEXEC;
741 		goto cleanup;
742 	}
743 
744 	/* Can we access it? */
745 	error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
746 	if (error)
747 		goto cleanup;
748 
749 	/*
750 	 * XXX: This should use vn_open() so that it is properly authorized,
751 	 * and to reduce code redundancy all over the place here.
752 	 * XXX: Not really, it duplicates far more of exec_check_permissions()
753 	 * than vn_open().
754 	 */
755 #ifdef MAC
756 	error = mac_vnode_check_open(td->td_ucred, vp, VREAD);
757 	if (error)
758 		goto cleanup;
759 #endif
760 	error = VOP_OPEN(vp, FREAD, td->td_ucred, td, NULL);
761 	if (error)
762 		goto cleanup;
763 	opened = true;
764 
765 	/* Pull in executable header into exec_map */
766 	error = vm_mmap(exec_map, (vm_offset_t *)&a_out, PAGE_SIZE,
767 	    VM_PROT_READ, VM_PROT_READ, 0, OBJT_VNODE, vp, 0);
768 	if (error)
769 		goto cleanup;
770 
771 	/* Is it a Linux binary ? */
772 	if (((a_out->a_magic >> 16) & 0xff) != 0x64) {
773 		error = ENOEXEC;
774 		goto cleanup;
775 	}
776 
777 	/*
778 	 * While we are here, we should REALLY do some more checks
779 	 */
780 
781 	/* Set file/virtual offset based on a.out variant. */
782 	switch ((int)(a_out->a_magic & 0xffff)) {
783 	case 0413:			/* ZMAGIC */
784 		file_offset = 1024;
785 		break;
786 	case 0314:			/* QMAGIC */
787 		file_offset = 0;
788 		break;
789 	default:
790 		error = ENOEXEC;
791 		goto cleanup;
792 	}
793 
794 	bss_size = round_page(a_out->a_bss);
795 
796 	/* Check various fields in header for validity/bounds. */
797 	if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) {
798 		error = ENOEXEC;
799 		goto cleanup;
800 	}
801 
802 	/* text + data can't exceed file size */
803 	if (a_out->a_data + a_out->a_text > attr.va_size) {
804 		error = EFAULT;
805 		goto cleanup;
806 	}
807 
808 	/*
809 	 * text/data/bss must not exceed limits
810 	 * XXX - this is not complete. it should check current usage PLUS
811 	 * the resources needed by this library.
812 	 */
813 	PROC_LOCK(td->td_proc);
814 	if (a_out->a_text > maxtsiz ||
815 	    a_out->a_data + bss_size > lim_cur_proc(td->td_proc, RLIMIT_DATA) ||
816 	    racct_set(td->td_proc, RACCT_DATA, a_out->a_data +
817 	    bss_size) != 0) {
818 		PROC_UNLOCK(td->td_proc);
819 		error = ENOMEM;
820 		goto cleanup;
821 	}
822 	PROC_UNLOCK(td->td_proc);
823 
824 	/*
825 	 * Prevent more writers.
826 	 */
827 	error = VOP_SET_TEXT(vp);
828 	if (error != 0)
829 		goto cleanup;
830 	textset = true;
831 
832 	/*
833 	 * Lock no longer needed
834 	 */
835 	locked = false;
836 	VOP_UNLOCK(vp);
837 
838 	/*
839 	 * Check if file_offset page aligned. Currently we cannot handle
840 	 * misalinged file offsets, and so we read in the entire image
841 	 * (what a waste).
842 	 */
843 	if (file_offset & PAGE_MASK) {
844 		/* Map text+data read/write/execute */
845 
846 		/* a_entry is the load address and is page aligned */
847 		vmaddr = trunc_page(a_out->a_entry);
848 
849 		/* get anon user mapping, read+write+execute */
850 		error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
851 		    &vmaddr, a_out->a_text + a_out->a_data, 0, VMFS_NO_SPACE,
852 		    VM_PROT_ALL, VM_PROT_ALL, 0);
853 		if (error)
854 			goto cleanup;
855 
856 		error = vn_rdwr(UIO_READ, vp, (void *)vmaddr, file_offset,
857 		    a_out->a_text + a_out->a_data, UIO_USERSPACE, 0,
858 		    td->td_ucred, NOCRED, &aresid, td);
859 		if (error != 0)
860 			goto cleanup;
861 		if (aresid != 0) {
862 			error = ENOEXEC;
863 			goto cleanup;
864 		}
865 	} else {
866 		/*
867 		 * for QMAGIC, a_entry is 20 bytes beyond the load address
868 		 * to skip the executable header
869 		 */
870 		vmaddr = trunc_page(a_out->a_entry);
871 
872 		/*
873 		 * Map it all into the process's space as a single
874 		 * copy-on-write "data" segment.
875 		 */
876 		map = &td->td_proc->p_vmspace->vm_map;
877 		error = vm_mmap(map, &vmaddr,
878 		    a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL,
879 		    MAP_PRIVATE | MAP_FIXED, OBJT_VNODE, vp, file_offset);
880 		if (error)
881 			goto cleanup;
882 		vm_map_lock(map);
883 		if (!vm_map_lookup_entry(map, vmaddr, &entry)) {
884 			vm_map_unlock(map);
885 			error = EDOOFUS;
886 			goto cleanup;
887 		}
888 		entry->eflags |= MAP_ENTRY_VN_EXEC;
889 		vm_map_unlock(map);
890 		textset = false;
891 	}
892 
893 	if (bss_size != 0) {
894 		/* Calculate BSS start address */
895 		vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
896 		    a_out->a_data;
897 
898 		/* allocate some 'anon' space */
899 		error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
900 		    &vmaddr, bss_size, 0, VMFS_NO_SPACE, VM_PROT_ALL,
901 		    VM_PROT_ALL, 0);
902 		if (error)
903 			goto cleanup;
904 	}
905 
906 cleanup:
907 	if (opened) {
908 		if (locked)
909 			VOP_UNLOCK(vp);
910 		locked = false;
911 		VOP_CLOSE(vp, FREAD, td->td_ucred, td);
912 	}
913 	if (textset) {
914 		if (!locked) {
915 			locked = true;
916 			VOP_LOCK(vp, LK_SHARED | LK_RETRY);
917 		}
918 		VOP_UNSET_TEXT_CHECKED(vp);
919 	}
920 	if (locked)
921 		VOP_UNLOCK(vp);
922 
923 	/* Release the temporary mapping. */
924 	if (a_out)
925 		kmap_free_wakeup(exec_map, (vm_offset_t)a_out, PAGE_SIZE);
926 
927 	return (error);
928 }
929