linux_exec_machdep.c revision 1.6 1 1.6 christos /* $NetBSD: linux_exec_machdep.c,v 1.6 2008/10/25 23:38:28 christos Exp $ */
2 1.1 christos
3 1.1 christos /*-
4 1.1 christos * Copyright (c) 2004 The NetBSD Foundation, Inc.
5 1.1 christos * All rights reserved.
6 1.1 christos *
7 1.1 christos * This code is derived from software contributed to The NetBSD Foundation
8 1.1 christos * by Christos Zoulas.
9 1.1 christos *
10 1.1 christos * Redistribution and use in source and binary forms, with or without
11 1.1 christos * modification, are permitted provided that the following conditions
12 1.1 christos * are met:
13 1.1 christos * 1. Redistributions of source code must retain the above copyright
14 1.1 christos * notice, this list of conditions and the following disclaimer.
15 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 christos * notice, this list of conditions and the following disclaimer in the
17 1.1 christos * documentation and/or other materials provided with the distribution.
18 1.1 christos *
19 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 christos * POSSIBILITY OF SUCH DAMAGE.
30 1.1 christos */
31 1.1 christos
32 1.1 christos #include <sys/cdefs.h>
33 1.6 christos __KERNEL_RCSID(0, "$NetBSD: linux_exec_machdep.c,v 1.6 2008/10/25 23:38:28 christos Exp $");
34 1.1 christos
35 1.1 christos #if defined(_KERNEL_OPT)
36 1.1 christos #include "opt_vm86.h"
37 1.1 christos #include "opt_user_ldt.h"
38 1.1 christos #endif
39 1.1 christos
40 1.1 christos #include <sys/param.h>
41 1.1 christos #include <sys/systm.h>
42 1.1 christos #include <sys/resource.h>
43 1.1 christos #include <sys/proc.h>
44 1.1 christos #include <sys/conf.h>
45 1.1 christos #include <sys/exec.h>
46 1.1 christos #include <sys/exec_elf.h>
47 1.1 christos #include <sys/vnode.h>
48 1.1 christos #include <sys/lwp.h>
49 1.1 christos
50 1.4 ad #include <sys/cpu.h>
51 1.1 christos #include <machine/vmparam.h>
52 1.1 christos
53 1.1 christos #include <uvm/uvm.h>
54 1.1 christos
55 1.6 christos #include <sys/syscallargs.h>
56 1.6 christos
57 1.6 christos #ifndef DEBUG_LINUX
58 1.6 christos #define DPRINTF(a)
59 1.6 christos #else
60 1.6 christos #define DPRINTF(a) uprintf a
61 1.6 christos #endif
62 1.6 christos
63 1.1 christos #include <compat/linux/common/linux_types.h>
64 1.1 christos #include <compat/linux/common/linux_signal.h>
65 1.6 christos #include <compat/linux/common/linux_machdep.h>
66 1.1 christos #include <compat/linux/common/linux_util.h>
67 1.1 christos #include <compat/linux/common/linux_ioctl.h>
68 1.1 christos #include <compat/linux/common/linux_hdio.h>
69 1.1 christos #include <compat/linux/common/linux_exec.h>
70 1.1 christos #include <compat/linux/common/linux_errno.h>
71 1.6 christos #include <compat/linux//linux_syscallargs.h>
72 1.1 christos
73 1.1 christos int
74 1.3 christos linux_exec_setup_stack(struct lwp *l, struct exec_package *epp)
75 1.1 christos {
76 1.1 christos u_long max_stack_size;
77 1.1 christos u_long access_linear_min, access_size;
78 1.1 christos u_long noaccess_linear_min, noaccess_size;
79 1.1 christos
80 1.1 christos #ifndef USRSTACK32
81 1.1 christos #define USRSTACK32 (0x00000000ffffffffL&~PGOFSET)
82 1.1 christos #endif
83 1.1 christos
84 1.1 christos if (epp->ep_flags & EXEC_32) {
85 1.1 christos epp->ep_minsaddr = USRSTACK32;
86 1.1 christos max_stack_size = MAXSSIZ;
87 1.1 christos } else {
88 1.1 christos epp->ep_minsaddr = USRSTACK;
89 1.1 christos max_stack_size = MAXSSIZ;
90 1.1 christos }
91 1.1 christos
92 1.1 christos if (epp->ep_minsaddr > LINUX_USRSTACK)
93 1.1 christos epp->ep_minsaddr = LINUX_USRSTACK;
94 1.1 christos #ifdef DEBUG_LINUX
95 1.1 christos else {
96 1.1 christos /*
97 1.1 christos * Someone needs to make KERNBASE and TEXTADDR
98 1.1 christos * java versions < 1.4.2 need the stack to be
99 1.1 christos * at 0xC0000000
100 1.1 christos */
101 1.1 christos uprintf("Cannot setup stack to 0xC0000000, "
102 1.1 christos "java will not work properly\n");
103 1.1 christos }
104 1.1 christos #endif
105 1.2 perry epp->ep_maxsaddr = (u_long)STACK_GROW(epp->ep_minsaddr,
106 1.1 christos max_stack_size);
107 1.3 christos epp->ep_ssize = l->l_proc->p_rlimit[RLIMIT_STACK].rlim_cur;
108 1.1 christos
109 1.1 christos /*
110 1.1 christos * set up commands for stack. note that this takes *two*, one to
111 1.1 christos * map the part of the stack which we can access, and one to map
112 1.1 christos * the part which we can't.
113 1.1 christos *
114 1.1 christos * arguably, it could be made into one, but that would require the
115 1.1 christos * addition of another mapping proc, which is unnecessary
116 1.1 christos */
117 1.1 christos access_size = epp->ep_ssize;
118 1.1 christos access_linear_min = (u_long)STACK_ALLOC(epp->ep_minsaddr, access_size);
119 1.1 christos noaccess_size = max_stack_size - access_size;
120 1.2 perry noaccess_linear_min = (u_long)STACK_ALLOC(STACK_GROW(epp->ep_minsaddr,
121 1.1 christos access_size), noaccess_size);
122 1.1 christos if (noaccess_size > 0) {
123 1.1 christos NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, noaccess_size,
124 1.1 christos noaccess_linear_min, NULLVP, 0, VM_PROT_NONE);
125 1.1 christos }
126 1.1 christos KASSERT(access_size > 0);
127 1.1 christos NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, access_size,
128 1.1 christos access_linear_min, NULLVP, 0, VM_PROT_READ | VM_PROT_WRITE);
129 1.1 christos
130 1.1 christos return 0;
131 1.1 christos }
132 1.6 christos
133 1.6 christos
134 1.6 christos #ifdef LINUX_NPTL
135 1.6 christos
136 1.6 christos int
137 1.6 christos linux_init_thread_area(struct lwp *l, struct lwp *l2)
138 1.6 christos {
139 1.6 christos struct trapframe *tf = l->l_md.md_regs, *tf2 = l2->l_md.md_regs;
140 1.6 christos struct pcb *pcb2 = &l2->l_addr->u_pcb;
141 1.6 christos struct linux_user_desc info;
142 1.6 christos struct segment_descriptor sd;
143 1.6 christos int error, idx, a[2];
144 1.6 christos
145 1.6 christos error = copyin((void *)tf->tf_esi, &info, sizeof(info));
146 1.6 christos if (error)
147 1.6 christos return error;
148 1.6 christos idx = info.entry_number;
149 1.6 christos
150 1.6 christos /*
151 1.6 christos * looks like we're getting the idx we returned
152 1.6 christos * in the set_thread_area() syscall
153 1.6 christos */
154 1.6 christos if (idx != 6 && idx != 3) {
155 1.6 christos printf("resetting idx %d to 3", idx);
156 1.6 christos idx = 3;
157 1.6 christos }
158 1.6 christos
159 1.6 christos /* this doesnt happen in practice */
160 1.6 christos if (idx == 6) {
161 1.6 christos /* we might copy out the entry_number as 3 */
162 1.6 christos info.entry_number = 3;
163 1.6 christos error = copyout(&info, (void *)tf->tf_esi, sizeof(info));
164 1.6 christos if (error)
165 1.6 christos return error;
166 1.6 christos }
167 1.6 christos
168 1.6 christos a[0] = LINUX_LDT_entry_a(&info);
169 1.6 christos a[1] = LINUX_LDT_entry_b(&info);
170 1.6 christos
171 1.6 christos (void)memcpy(&sd, &a, sizeof(a));
172 1.6 christos DPRINTF(("Segment created in clone with CLONE_SETTLS: lobase: %x, "
173 1.6 christos "hibase: %x, lolimit: %x, hilimit: %x, type: %i, dpl: %i, p: %i, "
174 1.6 christos "xx: %i, def32: %i, gran: %i\n", sd.sd_lobase,
175 1.6 christos sd.sd_hibase, sd.sd_lolimit, sd.sd_hilimit, sd.sd_type, sd.sd_dpl,
176 1.6 christos sd.sd_p, sd.sd_xx, sd.sd_def32, sd.sd_gran));
177 1.6 christos
178 1.6 christos (void)memcpy(pcb2->pcb_gsd, &sd, sizeof(pcb2->pcb_gsd));
179 1.6 christos tf2->tf_gs = GSEL(GUGS_SEL, SEL_UPL);
180 1.6 christos return 0;
181 1.6 christos }
182 1.6 christos
183 1.6 christos
184 1.6 christos int
185 1.6 christos linux_sys_set_thread_area(struct lwp *l,
186 1.6 christos const struct linux_sys_set_thread_area_args *uap, register_t *retval)
187 1.6 christos {
188 1.6 christos struct trapframe *tf = l->l_md.md_regs;
189 1.6 christos struct pcb *pcb = &l->l_addr->u_pcb;
190 1.6 christos struct linux_user_desc info;
191 1.6 christos struct segment_descriptor sd;
192 1.6 christos int error, idx, a[2];
193 1.6 christos
194 1.6 christos *retval = 0;
195 1.6 christos error = copyin(SCARG(uap, desc), &info, sizeof(info));
196 1.6 christos if (error)
197 1.6 christos return error;
198 1.6 christos
199 1.6 christos DPRINTF(("set thread area: %i, %x, %x, %i, %i, %i, %i, %i, %i\n",
200 1.6 christos info.entry_number, info.base_addr, info.limit, info.seg_32bit,
201 1.6 christos info.contents, info.read_exec_only, info.limit_in_pages,
202 1.6 christos info.seg_not_present, info.useable));
203 1.6 christos
204 1.6 christos idx = info.entry_number;
205 1.6 christos /*
206 1.6 christos * Semantics of linux version: every thread in the system has array of
207 1.6 christos * 3 tls descriptors. 1st is GLIBC TLS, 2nd is WINE, 3rd unknown. This
208 1.6 christos * syscall loads one of the selected tls decriptors with a value and
209 1.6 christos * also loads GDT descriptors 6, 7 and 8 with the content of the
210 1.6 christos * per-thread descriptors.
211 1.6 christos *
212 1.6 christos * Semantics of fbsd version: I think we can ignore that linux has 3
213 1.6 christos * per-thread descriptors and use just the 1st one. The tls_array[]
214 1.6 christos * is used only in set/get-thread_area() syscalls and for loading the
215 1.6 christos * GDT descriptors. In fbsd we use just one GDT descriptor for TLS so
216 1.6 christos * we will load just one.
217 1.6 christos *
218 1.6 christos * XXX: this doesn't work when a user space process tries to use more
219 1.6 christos * than 1 TLS segment. Comment in the linux sources says wine might do
220 1.6 christos * this.
221 1.6 christos */
222 1.6 christos
223 1.6 christos /*
224 1.6 christos * we support just GLIBC TLS now
225 1.6 christos * we should let 3 proceed as well because we use this segment so
226 1.6 christos * if code does two subsequent calls it should succeed
227 1.6 christos */
228 1.6 christos if (idx != 6 && idx != -1 && idx != 3)
229 1.6 christos return EINVAL;
230 1.6 christos
231 1.6 christos /*
232 1.6 christos * we have to copy out the GDT entry we use
233 1.6 christos * FreeBSD uses GDT entry #3 for storing %gs so load that
234 1.6 christos *
235 1.6 christos * XXX: what if a user space program doesn't check this value and tries
236 1.6 christos * to use 6, 7 or 8?
237 1.6 christos */
238 1.6 christos idx = info.entry_number = 3;
239 1.6 christos error = copyout(&info, SCARG(uap, desc), sizeof(info));
240 1.6 christos if (error)
241 1.6 christos return error;
242 1.6 christos
243 1.6 christos if (LINUX_LDT_empty(&info)) {
244 1.6 christos a[0] = 0;
245 1.6 christos a[1] = 0;
246 1.6 christos } else {
247 1.6 christos a[0] = LINUX_LDT_entry_a(&info);
248 1.6 christos a[1] = LINUX_LDT_entry_b(&info);
249 1.6 christos }
250 1.6 christos
251 1.6 christos (void)memcpy(&sd, &a, sizeof(a));
252 1.6 christos DPRINTF(("Segment created in set_thread_area: lobase: %x, hibase: %x, "
253 1.6 christos "lolimit: %x, hilimit: %x, type: %i, dpl: %i, p: %i, xx: %i, "
254 1.6 christos "def32: %i, gran: %i\n", sd.sd_lobase, sd.sd_hibase, sd.sd_lolimit,
255 1.6 christos sd.sd_hilimit, sd.sd_type, sd.sd_dpl, sd.sd_p, sd.sd_xx,
256 1.6 christos sd.sd_def32, sd.sd_gran));
257 1.6 christos
258 1.6 christos (void)memcpy(pcb->pcb_gsd, &sd, sizeof(pcb->pcb_gsd));
259 1.6 christos tf->tf_gs = GSEL(GUGS_SEL, SEL_UPL);
260 1.6 christos return 0;
261 1.6 christos }
262 1.6 christos
263 1.6 christos int
264 1.6 christos linux_sys_get_thread_area(struct lwp *l,
265 1.6 christos const struct linux_sys_get_thread_area_args *uap, register_t *retval)
266 1.6 christos {
267 1.6 christos struct pcb *pcb = &l->l_addr->u_pcb;
268 1.6 christos struct linux_user_desc info;
269 1.6 christos struct linux_desc_struct desc;
270 1.6 christos struct segment_descriptor sd;
271 1.6 christos int error, idx;
272 1.6 christos
273 1.6 christos *retval = 0;
274 1.6 christos error = copyin(SCARG(uap, desc), &info, sizeof(info));
275 1.6 christos if (error)
276 1.6 christos return error;
277 1.6 christos
278 1.6 christos idx = info.entry_number;
279 1.6 christos /* XXX: I am not sure if we want 3 to be allowed too. */
280 1.6 christos if (idx != 6 && idx != 3)
281 1.6 christos return EINVAL;
282 1.6 christos
283 1.6 christos idx = 3;
284 1.6 christos
285 1.6 christos (void)memset(&info, 0, sizeof(info));
286 1.6 christos (void)memcpy(&sd, pcb->pcb_gsd, sizeof(sd));
287 1.6 christos (void)memcpy(&desc, &sd, sizeof(desc));
288 1.6 christos
289 1.6 christos info.entry_number = idx;
290 1.6 christos info.base_addr = LINUX_GET_BASE(&desc);
291 1.6 christos info.limit = LINUX_GET_LIMIT(&desc);
292 1.6 christos info.seg_32bit = LINUX_GET_32BIT(&desc);
293 1.6 christos info.contents = LINUX_GET_CONTENTS(&desc);
294 1.6 christos info.read_exec_only = !LINUX_GET_WRITABLE(&desc);
295 1.6 christos info.limit_in_pages = LINUX_GET_LIMIT_PAGES(&desc);
296 1.6 christos info.seg_not_present = !LINUX_GET_PRESENT(&desc);
297 1.6 christos info.useable = LINUX_GET_USEABLE(&desc);
298 1.6 christos
299 1.6 christos return copyout(&info, SCARG(uap, desc), sizeof(info));
300 1.6 christos }
301 1.6 christos
302 1.6 christos #endif
303