linux_exec.c revision 1.115 1 1.115 christos /* $NetBSD: linux_exec.c,v 1.115 2011/08/18 02:21:02 christos Exp $ */
2 1.29 christos
3 1.29 christos /*-
4 1.103 ad * Copyright (c) 1994, 1995, 1998, 2000, 2007, 2008 The NetBSD Foundation, Inc.
5 1.29 christos * All rights reserved.
6 1.29 christos *
7 1.29 christos * This code is derived from software contributed to The NetBSD Foundation
8 1.33 fvdl * by Christos Zoulas, Frank van der Linden, Eric Haszlakiewicz and
9 1.33 fvdl * Thor Lancelot Simon.
10 1.29 christos *
11 1.29 christos * Redistribution and use in source and binary forms, with or without
12 1.29 christos * modification, are permitted provided that the following conditions
13 1.29 christos * are met:
14 1.29 christos * 1. Redistributions of source code must retain the above copyright
15 1.29 christos * notice, this list of conditions and the following disclaimer.
16 1.29 christos * 2. Redistributions in binary form must reproduce the above copyright
17 1.29 christos * notice, this list of conditions and the following disclaimer in the
18 1.29 christos * documentation and/or other materials provided with the distribution.
19 1.29 christos *
20 1.29 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 1.29 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.29 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.29 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 1.29 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 1.29 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 1.29 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.29 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.29 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.29 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.29 christos * POSSIBILITY OF SUCH DAMAGE.
31 1.1 fvdl */
32 1.54 lukem
33 1.54 lukem #include <sys/cdefs.h>
34 1.115 christos __KERNEL_RCSID(0, "$NetBSD: linux_exec.c,v 1.115 2011/08/18 02:21:02 christos Exp $");
35 1.1 fvdl
36 1.1 fvdl #include <sys/param.h>
37 1.1 fvdl #include <sys/systm.h>
38 1.1 fvdl #include <sys/kernel.h>
39 1.1 fvdl #include <sys/proc.h>
40 1.1 fvdl #include <sys/malloc.h>
41 1.1 fvdl #include <sys/namei.h>
42 1.1 fvdl #include <sys/vnode.h>
43 1.13 christos #include <sys/mount.h>
44 1.25 christos #include <sys/exec.h>
45 1.8 fvdl #include <sys/exec_elf.h>
46 1.1 fvdl
47 1.1 fvdl #include <sys/mman.h>
48 1.13 christos #include <sys/syscallargs.h>
49 1.1 fvdl
50 1.85 manu #include <sys/ptrace.h> /* For proc_reparent() */
51 1.85 manu
52 1.73 fvdl #include <uvm/uvm_extern.h>
53 1.73 fvdl
54 1.96 ad #include <sys/cpu.h>
55 1.1 fvdl #include <machine/reg.h>
56 1.1 fvdl
57 1.32 christos #include <compat/linux/common/linux_types.h>
58 1.32 christos #include <compat/linux/common/linux_signal.h>
59 1.32 christos #include <compat/linux/common/linux_util.h>
60 1.85 manu #include <compat/linux/common/linux_sched.h>
61 1.85 manu #include <compat/linux/common/linux_machdep.h>
62 1.32 christos #include <compat/linux/common/linux_exec.h>
63 1.78 manu #include <compat/linux/common/linux_futex.h>
64 1.97 njoly #include <compat/linux/common/linux_ipc.h>
65 1.97 njoly #include <compat/linux/common/linux_sem.h>
66 1.32 christos
67 1.32 christos #include <compat/linux/linux_syscallargs.h>
68 1.4 christos #include <compat/linux/linux_syscall.h>
69 1.38 jdolecek #include <compat/linux/common/linux_misc.h>
70 1.38 jdolecek #include <compat/linux/common/linux_errno.h>
71 1.39 jdolecek #include <compat/linux/common/linux_emuldata.h>
72 1.8 fvdl
73 1.38 jdolecek extern struct sysent linux_sysent[];
74 1.38 jdolecek extern const char * const linux_syscallnames[];
75 1.38 jdolecek extern char linux_sigcode[], linux_esigcode[];
76 1.1 fvdl
77 1.1 fvdl /*
78 1.38 jdolecek * Emulation switch.
79 1.38 jdolecek */
80 1.65 chs
81 1.65 chs struct uvm_object *emul_linux_object;
82 1.65 chs
83 1.109 ad struct emul emul_linux = {
84 1.113 rmind .e_name = "linux",
85 1.113 rmind .e_path = "/emul/linux",
86 1.44 mycroft #ifndef __HAVE_MINIMAL_EMUL
87 1.113 rmind .e_flags = 0,
88 1.114 chs .e_errno = native_to_linux_errno,
89 1.113 rmind .e_nosys = LINUX_SYS_syscall,
90 1.113 rmind .e_nsysent = LINUX_SYS_NSYSENT,
91 1.44 mycroft #endif
92 1.113 rmind .e_sysent = linux_sysent,
93 1.113 rmind .e_syscallnames = linux_syscallnames,
94 1.113 rmind .e_sendsig = linux_sendsig,
95 1.113 rmind .e_trapsignal = linux_trapsignal,
96 1.113 rmind .e_tracesig = NULL,
97 1.113 rmind .e_sigcode = linux_sigcode,
98 1.113 rmind .e_esigcode = linux_esigcode,
99 1.113 rmind .e_sigobject = &emul_linux_object,
100 1.113 rmind .e_setregs = linux_setregs,
101 1.113 rmind .e_proc_exec = linux_e_proc_exec,
102 1.113 rmind .e_proc_fork = linux_e_proc_fork,
103 1.113 rmind .e_proc_exit = linux_e_proc_exit,
104 1.114 chs .e_lwp_fork = linux_e_lwp_fork,
105 1.114 chs .e_lwp_exit = linux_e_lwp_exit,
106 1.44 mycroft #ifdef __HAVE_SYSCALL_INTERN
107 1.113 rmind .e_syscall_intern = linux_syscall_intern,
108 1.42 jdolecek #else
109 1.62 thorpej #error Implement __HAVE_SYSCALL_INTERN for this platform
110 1.41 jdolecek #endif
111 1.113 rmind .e_sysctlovly = NULL,
112 1.113 rmind .e_fault = NULL,
113 1.113 rmind .e_vm_default_addr = uvm_default_mapaddr,
114 1.113 rmind .e_usertrap = linux_usertrap,
115 1.113 rmind .e_sa = NULL,
116 1.113 rmind .e_ucsize = 0,
117 1.113 rmind .e_startlwp = NULL
118 1.38 jdolecek };
119 1.39 jdolecek
120 1.114 chs void
121 1.114 chs linux_e_proc_exec(struct proc *p, struct exec_package *epp)
122 1.39 jdolecek {
123 1.114 chs struct lwp *l;
124 1.84 manu
125 1.114 chs l = LIST_FIRST(&p->p_lwps);
126 1.114 chs if (l->l_emuldata == NULL) {
127 1.114 chs l->l_emuldata = kmem_zalloc(sizeof(struct linux_emuldata), KM_SLEEP);
128 1.71 jdolecek } else {
129 1.114 chs memset(l->l_emuldata, 0, sizeof (struct linux_emuldata));
130 1.77 manu }
131 1.77 manu
132 1.114 chs KASSERT(p->p_nlwps == 1);
133 1.114 chs l = LIST_FIRST(&p->p_lwps);
134 1.114 chs mutex_enter(p->p_lock);
135 1.114 chs l->l_lid = p->p_pid;
136 1.114 chs mutex_exit(p->p_lock);
137 1.48 jdolecek }
138 1.48 jdolecek
139 1.114 chs void
140 1.100 dsl linux_e_proc_exit(struct proc *p)
141 1.39 jdolecek {
142 1.114 chs struct lwp *l;
143 1.71 jdolecek
144 1.114 chs KASSERT(p->p_nlwps == 1);
145 1.114 chs l = LIST_FIRST(&p->p_lwps);
146 1.114 chs linux_e_lwp_exit(l);
147 1.39 jdolecek }
148 1.39 jdolecek
149 1.114 chs void
150 1.114 chs linux_e_proc_fork(struct proc *p2, struct lwp *l1, int flags)
151 1.39 jdolecek {
152 1.114 chs struct linux_emuldata *led1, *led2;
153 1.114 chs struct lwp *l2;
154 1.103 ad
155 1.114 chs KASSERT(p2->p_nlwps == 1);
156 1.114 chs l2 = LIST_FIRST(&p2->p_lwps);
157 1.114 chs l2->l_lid = p2->p_pid;
158 1.114 chs led1 = l1->l_emuldata;
159 1.114 chs led2 = l2->l_emuldata;
160 1.114 chs led2->led_child_tidptr = led1->led_child_tidptr;
161 1.77 manu }
162 1.77 manu
163 1.85 manu void
164 1.114 chs linux_e_lwp_fork(struct lwp *l1, struct lwp *l2)
165 1.77 manu {
166 1.114 chs struct linux_emuldata *led2;
167 1.77 manu
168 1.115 christos led2 = kmem_zalloc(sizeof(*led2), KM_SLEEP);
169 1.114 chs l2->l_emuldata = led2;
170 1.39 jdolecek }
171 1.85 manu
172 1.85 manu void
173 1.114 chs linux_e_lwp_exit(struct lwp *l)
174 1.85 manu {
175 1.114 chs struct linux_emuldata *led;
176 1.114 chs struct linux_sys_futex_args cup;
177 1.114 chs register_t retval;
178 1.114 chs int error, zero = 0;
179 1.114 chs
180 1.114 chs led = l->l_emuldata;
181 1.114 chs if (led->led_clear_tid == NULL) {
182 1.114 chs return;
183 1.85 manu }
184 1.85 manu
185 1.85 manu /* Emulate LINUX_CLONE_CHILD_CLEARTID */
186 1.114 chs error = copyout(&zero, led->led_clear_tid, sizeof(zero));
187 1.85 manu #ifdef DEBUG_LINUX
188 1.114 chs if (error != 0)
189 1.114 chs printf("%s: cannot clear TID\n", __func__);
190 1.85 manu #endif
191 1.85 manu
192 1.114 chs SCARG(&cup, uaddr) = led->led_clear_tid;
193 1.114 chs SCARG(&cup, op) = LINUX_FUTEX_WAKE;
194 1.114 chs SCARG(&cup, val) = 0x7fffffff; /* Awake everyone */
195 1.114 chs SCARG(&cup, timeout) = NULL;
196 1.114 chs SCARG(&cup, uaddr2) = NULL;
197 1.114 chs SCARG(&cup, val3) = 0;
198 1.114 chs if ((error = linux_sys_futex(curlwp, &cup, &retval)) != 0)
199 1.114 chs printf("%s: linux_sys_futex failed\n", __func__);
200 1.114 chs
201 1.114 chs release_futexes(l);
202 1.114 chs
203 1.114 chs led = l->l_emuldata;
204 1.114 chs l->l_emuldata = NULL;
205 1.115 christos kmem_free(led, sizeof(*led));
206 1.85 manu }
207