1 /*-
2 * Copyright (c) 2000 Marcel Moolenaar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD: releng/5.2/sys/alpha/linux/linux_machdep.c 119288 2003-08-22 07:20:27Z imp $");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/lock.h>
35 #include <sys/mman.h>
36 #include <sys/mount.h>
37 #include <sys/mutex.h>
38 #include <sys/proc.h>
39 #include <sys/syscallsubr.h>
40 #include <sys/sysproto.h>
41 #include <sys/unistd.h>
42 #include <sys/user.h>
43
44 #include <vm/vm.h>
45 #include <vm/pmap.h>
46 #include <vm/vm_map.h>
47
48 #include <alpha/linux/linux.h>
49 #include <alpha/linux/linux_proto.h>
50 #include <compat/linux/linux_signal.h>
51 #include <compat/linux/linux_util.h>
52
53 struct linux_select_argv {
54 int nfds;
55 fd_set *readfds;
56 fd_set *writefds;
57 fd_set *exceptfds;
58 struct timeval *timeout;
59 };
60
61 int
62 linux_execve(struct thread *td, struct linux_execve_args *args)
63 {
64 struct execve_args bsd;
65 caddr_t sg;
66
67 sg = stackgap_init();
68 CHECKALTEXIST(td, &sg, args->path);
69
70 #ifdef DEBUG
71 if (ldebug(execve))
72 printf(ARGS(execve, "%s"), args->path);
73 #endif
74 bsd.fname = args->path;
75 bsd.argv = args->argp;
76 bsd.envv = args->envp;
77 return (execve(td, &bsd));
78 }
79
80 /*
81 * MPSAFE
82 */
83 int
84 linux_fork(struct thread *td, struct linux_fork_args *args)
85 {
86 int error;
87
88 #ifdef DEBUG
89 if (ldebug(fork))
90 printf(ARGS(fork, ""));
91 #endif
92 if ((error = fork(td, (struct fork_args *)args)) != 0)
93 return (error);
94
95 if (td->td_retval[1] == 1)
96 td->td_retval[0] = 0;
97
98 return (0);
99 }
100
101 /*
102 * MPSAFE
103 */
104 int
105 linux_vfork(struct thread *td, struct linux_vfork_args *args)
106 {
107 int error;
108
109 #ifdef DEBUG
110 if (ldebug(vfork))
111 printf(ARGS(vfork, ""));
112 #endif
113 if ((error = vfork(td, (struct vfork_args *)args)) != 0)
114 return (error);
115 /* Are we the child? */
116 if (td->td_retval[1] == 1)
117 td->td_retval[0] = 0;
118 return (0);
119 }
120
121 #define CLONE_VM 0x100
122 #define CLONE_FS 0x200
123 #define CLONE_FILES 0x400
124 #define CLONE_SIGHAND 0x800
125 #define CLONE_PID 0x1000
126
127 int
128 linux_clone(struct thread *td, struct linux_clone_args *args)
129 {
130 int error, ff = RFPROC | RFSTOPPED;
131 struct proc *p2;
132 struct thread *td2;
133 int exit_signal;
134
135 #ifdef DEBUG
136 if (ldebug(clone)) {
137 printf(ARGS(clone, "flags %x, stack %p"),
138 (unsigned int)args->flags, args->stack);
139 if (args->flags & CLONE_PID)
140 printf(LMSG("CLONE_PID not yet supported"));
141 }
142 #endif
143
144 if (!args->stack)
145 return (EINVAL);
146
147 exit_signal = args->flags & 0x000000ff;
148 if (exit_signal >= LINUX_NSIG)
149 return (EINVAL);
150
151 /* if (exit_signal <= LINUX_SIGTBLSZ)
152 exit_signal = linux_to_bsd_signal[_SIG_IDX(exit_signal)];
153 */
154
155 if (args->flags & CLONE_VM)
156 ff |= RFMEM;
157 if (args->flags & CLONE_SIGHAND)
158 ff |= RFSIGSHARE;
159 if (!(args->flags & CLONE_FILES))
160 ff |= RFFDG;
161
162 error = fork1(td, ff, 0, &p2);
163 if (error)
164 return (error);
165
166 PROC_LOCK(p2);
167 p2->p_sigparent = exit_signal;
168 PROC_UNLOCK(p2);
169 td2 = FIRST_THREAD_IN_PROC(p2);
170 td2->td_pcb->pcb_hw.apcb_usp = (unsigned long)args->stack;
171
172 #ifdef DEBUG
173 if (ldebug(clone))
174 printf(LMSG("clone: successful rfork to %ld, stack %p sig = %d"),
175 (long)p2->p_pid, args->stack, exit_signal);
176 #endif
177
178 /*
179 * Make this runnable after we are finished with it.
180 */
181 mtx_lock_spin(&sched_lock);
182 TD_SET_CAN_RUN(td2);
183 setrunqueue(td2);
184 mtx_unlock_spin(&sched_lock);
185
186 td->td_retval[0] = p2->p_pid;
187 td->td_retval[1] = 0;
188 return (0);
189 }
190
191 #define STACK_SIZE (2 * 1024 * 1024)
192 #define GUARD_SIZE (4 * PAGE_SIZE)
193
194 int
195 linux_mmap(struct thread *td, struct linux_mmap_args *linux_args)
196 {
197 struct mmap_args /* {
198 caddr_t addr;
199 size_t len;
200 int prot;
201 int flags;
202 int fd;
203 long pad;
204 off_t pos;
205 } */ bsd_args;
206 int error;
207
208 #ifdef DEBUG
209 if (ldebug(mmap))
210 printf(ARGS(mmap, "%p, 0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx"),
211 (void *)linux_args->addr, linux_args->len,
212 linux_args->prot, linux_args->flags, linux_args->fd,
213 linux_args->pos);
214 #endif
215 bsd_args.prot = linux_args->prot | PROT_READ; /* always required */
216
217 bsd_args.flags = 0;
218 if (linux_args->flags & LINUX_MAP_SHARED)
219 bsd_args.flags |= MAP_SHARED;
220 if (linux_args->flags & LINUX_MAP_PRIVATE)
221 bsd_args.flags |= MAP_PRIVATE;
222 if (linux_args->flags & LINUX_MAP_FIXED){
223 bsd_args.flags |= MAP_FIXED;
224 bsd_args.pos = trunc_page(linux_args->pos);
225 } else {
226 bsd_args.pos = linux_args->pos;
227 }
228 if (linux_args->flags & LINUX_MAP_ANON)
229 bsd_args.flags |= MAP_ANON;
230 if (linux_args->flags & LINUX_MAP_GROWSDOWN) {
231 bsd_args.flags |= MAP_STACK;
232
233 /* The linux MAP_GROWSDOWN option does not limit auto
234 * growth of the region. Linux mmap with this option
235 * takes as addr the inital BOS, and as len, the initial
236 * region size. It can then grow down from addr without
237 * limit. However, linux threads has an implicit internal
238 * limit to stack size of STACK_SIZE. Its just not
239 * enforced explicitly in linux. But, here we impose
240 * a limit of (STACK_SIZE - GUARD_SIZE) on the stack
241 * region, since we can do this with our mmap.
242 *
243 * Our mmap with MAP_STACK takes addr as the maximum
244 * downsize limit on BOS, and as len the max size of
245 * the region. It them maps the top SGROWSIZ bytes,
246 * and autgrows the region down, up to the limit
247 * in addr.
248 *
249 * If we don't use the MAP_STACK option, the effect
250 * of this code is to allocate a stack region of a
251 * fixed size of (STACK_SIZE - GUARD_SIZE).
252 */
253
254 /* This gives us TOS */
255 bsd_args.addr = (caddr_t)(linux_args->addr + linux_args->len);
256
257 /* This gives us our maximum stack size */
258 if (linux_args->len > STACK_SIZE - GUARD_SIZE)
259 bsd_args.len = linux_args->len;
260 else
261 bsd_args.len = STACK_SIZE - GUARD_SIZE;
262
263 /* This gives us a new BOS. If we're using VM_STACK, then
264 * mmap will just map the top SGROWSIZ bytes, and let
265 * the stack grow down to the limit at BOS. If we're
266 * not using VM_STACK we map the full stack, since we
267 * don't have a way to autogrow it.
268 */
269 bsd_args.addr -= bsd_args.len;
270 bsd_args.addr = (caddr_t)round_page(bsd_args.addr); /* XXXX */
271 } else {
272 bsd_args.addr = (caddr_t)linux_args->addr;
273 bsd_args.len = linux_args->len;
274 }
275
276 bsd_args.fd = linux_args->fd;
277 if(linux_args->fd == 0)
278 bsd_args.fd = -1;
279
280 bsd_args.pad = 0;
281 #ifdef DEBUG
282 if (ldebug(mmap))
283 printf(ARGS(mmap, "%p, 0x%lx, 0x%x, 0x%x, 0x%x, 0x%lx"),
284 (void *)bsd_args.addr,
285 bsd_args.len,
286 bsd_args.prot,
287 bsd_args.flags,
288 bsd_args.fd,
289 bsd_args.pos);
290 #endif
291 if (bsd_args.addr == 0)
292 bsd_args.addr = (caddr_t)0x40000000UL;
293 error = mmap(td, &bsd_args);
294 #ifdef DEBUG
295 if (ldebug(mmap))
296 printf(LMSG("mmap returns %d, 0x%lx"), error, td->td_retval[0]);
297 #endif
298 return (error);
299 }
300
301 int
302 linux_rt_sigsuspend(td, uap)
303 struct thread *td;
304 struct linux_rt_sigsuspend_args *uap;
305 {
306 int error;
307 l_sigset_t lmask;
308 sigset_t bmask;
309
310 #ifdef DEBUG
311 if (ldebug(rt_sigsuspend))
312 printf(ARGS(rt_sigsuspend, "%p, %zd"),
313 (void *)uap->newset, uap->sigsetsize);
314 #endif
315 if (uap->sigsetsize != sizeof(l_sigset_t))
316 return (EINVAL);
317
318 error = copyin(uap->newset, &lmask, sizeof(l_sigset_t));
319 if (error)
320 return (error);
321
322 linux_to_bsd_sigset(&lmask, &bmask);
323 return (kern_sigsuspend(td, bmask));
324 }
325
326 int
327 linux_mprotect(td, uap)
328 struct thread *td;
329 struct linux_mprotect_args *uap;
330 {
331
332 #ifdef DEBUG
333 if (ldebug(mprotect))
334 printf(ARGS(mprotect, "%p, 0x%zx, 0x%lx"),
335 (void *)uap->addr, uap->len, uap->prot);
336 #endif
337 return (mprotect(td, (void *)uap));
338 }
339
340 int
341 linux_munmap(td, uap)
342 struct thread *td;
343 struct linux_munmap_args *uap;
344 {
345
346 #ifdef DEBUG
347 if (ldebug(munmap))
348 printf(ARGS(munmap, "%p, 0x%lx"),
349 (void *)uap->addr, uap->len);
350 #endif
351 return (munmap(td, (void *)uap));
352 }
353
354 static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = {
355 RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK,
356 RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NOFILE, -1,
357 RLIMIT_NPROC, RLIMIT_MEMLOCK
358 };
359
360 int
361 linux_setrlimit(td, uap)
362 struct thread *td;
363 struct linux_setrlimit_args *uap;
364 {
365 struct rlimit rlim;
366 u_int which;
367 int error;
368
369 #ifdef DEBUG
370 if (ldebug(setrlimit))
371 printf(ARGS(setrlimit, "%d, %p"),
372 uap->resource, (void *)uap->rlim);
373 #endif
374 if (uap->resource >= LINUX_RLIM_NLIMITS)
375 return EINVAL;
376
377 which = linux_to_bsd_resource[uap->resource];
378
379 if (which == -1)
380 return EINVAL;
381
382 if ((error =
383 copyin(uap->rlim, &rlim, sizeof (struct rlimit))))
384 return (error);
385 return dosetrlimit(td, which, &rlim);
386 }
387
388 int
389 linux_getrlimit(td, uap)
390 struct thread *td;
391 struct linux_getrlimit_args *uap;
392 {
393 u_int which;
394
395 #ifdef DEBUG
396 if (ldebug(getrlimit))
397 printf(ARGS(getrlimit, "%d, %p"),
398 uap->resource, (void *)uap->rlim);
399 #endif
400 if (uap->resource >= LINUX_RLIM_NLIMITS)
401 return EINVAL;
402
403 which = linux_to_bsd_resource[uap->resource];
404
405 if (which == -1)
406 return EINVAL;
407
408 return (copyout(&td->td_proc->p_rlimit[which],
409 uap->rlim, sizeof (struct rlimit)));
410 }
Cache object: 60500219ec6e3f009cca26b8894f3e44
|