linux_exec.c revision 1.115.2.1 1 1.115.2.1 yamt /* $NetBSD: linux_exec.c,v 1.115.2.1 2012/04/17 00:07:17 yamt 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.2.1 yamt __KERNEL_RCSID(0, "$NetBSD: linux_exec.c,v 1.115.2.1 2012/04/17 00:07:17 yamt 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_ucsize = 0,
116 1.113 rmind .e_startlwp = NULL
117 1.38 jdolecek };
118 1.39 jdolecek
119 1.114 chs void
120 1.114 chs linux_e_proc_exec(struct proc *p, struct exec_package *epp)
121 1.39 jdolecek {
122 1.114 chs struct lwp *l;
123 1.84 manu
124 1.114 chs l = LIST_FIRST(&p->p_lwps);
125 1.114 chs if (l->l_emuldata == NULL) {
126 1.114 chs l->l_emuldata = kmem_zalloc(sizeof(struct linux_emuldata), KM_SLEEP);
127 1.71 jdolecek } else {
128 1.114 chs memset(l->l_emuldata, 0, sizeof (struct linux_emuldata));
129 1.77 manu }
130 1.77 manu
131 1.114 chs KASSERT(p->p_nlwps == 1);
132 1.114 chs l = LIST_FIRST(&p->p_lwps);
133 1.114 chs mutex_enter(p->p_lock);
134 1.114 chs l->l_lid = p->p_pid;
135 1.114 chs mutex_exit(p->p_lock);
136 1.48 jdolecek }
137 1.48 jdolecek
138 1.114 chs void
139 1.100 dsl linux_e_proc_exit(struct proc *p)
140 1.39 jdolecek {
141 1.114 chs struct lwp *l;
142 1.71 jdolecek
143 1.114 chs KASSERT(p->p_nlwps == 1);
144 1.114 chs l = LIST_FIRST(&p->p_lwps);
145 1.114 chs linux_e_lwp_exit(l);
146 1.39 jdolecek }
147 1.39 jdolecek
148 1.114 chs void
149 1.114 chs linux_e_proc_fork(struct proc *p2, struct lwp *l1, int flags)
150 1.39 jdolecek {
151 1.114 chs struct linux_emuldata *led1, *led2;
152 1.114 chs struct lwp *l2;
153 1.103 ad
154 1.114 chs KASSERT(p2->p_nlwps == 1);
155 1.114 chs l2 = LIST_FIRST(&p2->p_lwps);
156 1.114 chs l2->l_lid = p2->p_pid;
157 1.114 chs led1 = l1->l_emuldata;
158 1.114 chs led2 = l2->l_emuldata;
159 1.114 chs led2->led_child_tidptr = led1->led_child_tidptr;
160 1.77 manu }
161 1.77 manu
162 1.85 manu void
163 1.114 chs linux_e_lwp_fork(struct lwp *l1, struct lwp *l2)
164 1.77 manu {
165 1.114 chs struct linux_emuldata *led2;
166 1.77 manu
167 1.115 christos led2 = kmem_zalloc(sizeof(*led2), KM_SLEEP);
168 1.114 chs l2->l_emuldata = led2;
169 1.39 jdolecek }
170 1.85 manu
171 1.85 manu void
172 1.114 chs linux_e_lwp_exit(struct lwp *l)
173 1.85 manu {
174 1.114 chs struct linux_emuldata *led;
175 1.114 chs struct linux_sys_futex_args cup;
176 1.114 chs register_t retval;
177 1.114 chs int error, zero = 0;
178 1.114 chs
179 1.114 chs led = l->l_emuldata;
180 1.114 chs if (led->led_clear_tid == NULL) {
181 1.114 chs return;
182 1.85 manu }
183 1.85 manu
184 1.85 manu /* Emulate LINUX_CLONE_CHILD_CLEARTID */
185 1.114 chs error = copyout(&zero, led->led_clear_tid, sizeof(zero));
186 1.85 manu #ifdef DEBUG_LINUX
187 1.114 chs if (error != 0)
188 1.114 chs printf("%s: cannot clear TID\n", __func__);
189 1.85 manu #endif
190 1.85 manu
191 1.114 chs SCARG(&cup, uaddr) = led->led_clear_tid;
192 1.114 chs SCARG(&cup, op) = LINUX_FUTEX_WAKE;
193 1.114 chs SCARG(&cup, val) = 0x7fffffff; /* Awake everyone */
194 1.114 chs SCARG(&cup, timeout) = NULL;
195 1.114 chs SCARG(&cup, uaddr2) = NULL;
196 1.114 chs SCARG(&cup, val3) = 0;
197 1.114 chs if ((error = linux_sys_futex(curlwp, &cup, &retval)) != 0)
198 1.114 chs printf("%s: linux_sys_futex failed\n", __func__);
199 1.114 chs
200 1.114 chs release_futexes(l);
201 1.114 chs
202 1.114 chs led = l->l_emuldata;
203 1.114 chs l->l_emuldata = NULL;
204 1.115 christos kmem_free(led, sizeof(*led));
205 1.85 manu }
206