2 * Copyright (c) 2000 Marcel Moolenaar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/imgact.h>
36 #include <sys/malloc.h>
38 #include <sys/mount.h>
39 #include <sys/mutex.h>
41 #include <sys/resource.h>
42 #include <sys/resourcevar.h>
43 #include <sys/syscallsubr.h>
44 #include <sys/sysproto.h>
45 #include <sys/unistd.h>
47 #include <machine/pcb.h>
51 #include <vm/vm_map.h>
53 #include <alpha/linux/linux.h>
54 #include <alpha/linux/linux_proto.h>
55 #include <compat/linux/linux_signal.h>
56 #include <compat/linux/linux_util.h>
58 struct linux_select_argv {
63 struct timeval *timeout;
67 linux_execve(struct thread *td, struct linux_execve_args *args)
69 struct image_args eargs;
73 LCONVPATHEXIST(td, args->path, &path);
77 printf(ARGS(execve, "%s"), path);
79 error = exec_copyin_args(&eargs, path, UIO_SYSSPACE, args->argp,
83 error = kern_execve(td, &eargs, NULL);
84 exec_free_args(&eargs);
92 linux_fork(struct thread *td, struct linux_fork_args *args)
98 printf(ARGS(fork, ""));
100 if ((error = fork(td, (struct fork_args *)args)) != 0)
103 if (td->td_retval[1] == 1)
104 td->td_retval[0] = 0;
113 linux_vfork(struct thread *td, struct linux_vfork_args *args)
119 printf(ARGS(vfork, ""));
121 if ((error = vfork(td, (struct vfork_args *)args)) != 0)
123 /* Are we the child? */
124 if (td->td_retval[1] == 1)
125 td->td_retval[0] = 0;
129 #define CLONE_VM 0x100
130 #define CLONE_FS 0x200
131 #define CLONE_FILES 0x400
132 #define CLONE_SIGHAND 0x800
133 #define CLONE_PID 0x1000
136 linux_clone(struct thread *td, struct linux_clone_args *args)
138 int error, ff = RFPROC | RFSTOPPED;
145 printf(ARGS(clone, "flags %x, stack %p"),
146 (unsigned int)args->flags, args->stack);
147 if (args->flags & CLONE_PID)
148 printf(LMSG("CLONE_PID not yet supported"));
155 exit_signal = args->flags & 0x000000ff;
156 if (exit_signal >= LINUX_NSIG)
159 /* if (exit_signal <= LINUX_SIGTBLSZ)
160 exit_signal = linux_to_bsd_signal[_SIG_IDX(exit_signal)];
163 if (args->flags & CLONE_VM)
165 if (args->flags & CLONE_SIGHAND)
167 if (!(args->flags & CLONE_FILES))
170 error = fork1(td, ff, 0, &p2);
175 p2->p_sigparent = exit_signal;
177 td2 = FIRST_THREAD_IN_PROC(p2);
178 td2->td_pcb->pcb_hw.apcb_usp = (unsigned long)args->stack;
182 printf(LMSG("clone: successful rfork to %ld, stack %p sig = %d"),
183 (long)p2->p_pid, args->stack, exit_signal);
187 * Make this runnable after we are finished with it.
189 mtx_lock_spin(&sched_lock);
191 setrunqueue(td2, SRQ_BORING);
192 mtx_unlock_spin(&sched_lock);
194 td->td_retval[0] = p2->p_pid;
195 td->td_retval[1] = 0;
199 #define STACK_SIZE (2 * 1024 * 1024)
200 #define GUARD_SIZE (4 * PAGE_SIZE)
203 linux_mmap(struct thread *td, struct linux_mmap_args *linux_args)
205 struct mmap_args /* {
218 printf(ARGS(mmap, "%p, 0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx"),
219 (void *)linux_args->addr, linux_args->len,
220 linux_args->prot, linux_args->flags, linux_args->fd,
223 bsd_args.prot = linux_args->prot | PROT_READ; /* always required */
226 if (linux_args->flags & LINUX_MAP_SHARED)
227 bsd_args.flags |= MAP_SHARED;
228 if (linux_args->flags & LINUX_MAP_PRIVATE)
229 bsd_args.flags |= MAP_PRIVATE;
230 if (linux_args->flags & LINUX_MAP_FIXED){
231 bsd_args.flags |= MAP_FIXED;
232 bsd_args.pos = trunc_page(linux_args->pos);
234 bsd_args.pos = linux_args->pos;
236 if (linux_args->flags & LINUX_MAP_ANON)
237 bsd_args.flags |= MAP_ANON;
238 if (linux_args->flags & LINUX_MAP_GROWSDOWN) {
239 bsd_args.flags |= MAP_STACK;
241 /* The linux MAP_GROWSDOWN option does not limit auto
242 * growth of the region. Linux mmap with this option
243 * takes as addr the inital BOS, and as len, the initial
244 * region size. It can then grow down from addr without
245 * limit. However, linux threads has an implicit internal
246 * limit to stack size of STACK_SIZE. Its just not
247 * enforced explicitly in linux. But, here we impose
248 * a limit of (STACK_SIZE - GUARD_SIZE) on the stack
249 * region, since we can do this with our mmap.
251 * Our mmap with MAP_STACK takes addr as the maximum
252 * downsize limit on BOS, and as len the max size of
253 * the region. It them maps the top SGROWSIZ bytes,
254 * and autgrows the region down, up to the limit
257 * If we don't use the MAP_STACK option, the effect
258 * of this code is to allocate a stack region of a
259 * fixed size of (STACK_SIZE - GUARD_SIZE).
262 /* This gives us TOS */
263 bsd_args.addr = (caddr_t)(linux_args->addr + linux_args->len);
265 /* This gives us our maximum stack size */
266 if (linux_args->len > STACK_SIZE - GUARD_SIZE)
267 bsd_args.len = linux_args->len;
269 bsd_args.len = STACK_SIZE - GUARD_SIZE;
271 /* This gives us a new BOS. If we're using VM_STACK, then
272 * mmap will just map the top SGROWSIZ bytes, and let
273 * the stack grow down to the limit at BOS. If we're
274 * not using VM_STACK we map the full stack, since we
275 * don't have a way to autogrow it.
277 bsd_args.addr -= bsd_args.len;
278 bsd_args.addr = (caddr_t)round_page(bsd_args.addr); /* XXXX */
280 bsd_args.addr = (caddr_t)linux_args->addr;
281 bsd_args.len = linux_args->len;
284 bsd_args.fd = linux_args->fd;
285 if(linux_args->fd == 0)
291 printf(ARGS(mmap, "%p, 0x%lx, 0x%x, 0x%x, 0x%x, 0x%lx"),
292 (void *)bsd_args.addr,
299 if (bsd_args.addr == 0)
300 bsd_args.addr = (caddr_t)0x40000000UL;
301 error = mmap(td, &bsd_args);
304 printf(LMSG("mmap returns %d, 0x%lx"), error, td->td_retval[0]);
310 linux_rt_sigsuspend(td, uap)
312 struct linux_rt_sigsuspend_args *uap;
319 if (ldebug(rt_sigsuspend))
320 printf(ARGS(rt_sigsuspend, "%p, %zd"),
321 (void *)uap->newset, uap->sigsetsize);
323 if (uap->sigsetsize != sizeof(l_sigset_t))
326 error = copyin(uap->newset, &lmask, sizeof(l_sigset_t));
330 linux_to_bsd_sigset(&lmask, &bmask);
331 return (kern_sigsuspend(td, bmask));
335 linux_mprotect(td, uap)
337 struct linux_mprotect_args *uap;
341 if (ldebug(mprotect))
342 printf(ARGS(mprotect, "%p, 0x%zx, 0x%lx"),
343 (void *)uap->addr, uap->len, uap->prot);
345 return (mprotect(td, (void *)uap));
349 linux_munmap(td, uap)
351 struct linux_munmap_args *uap;
356 printf(ARGS(munmap, "%p, 0x%lx"),
357 (void *)uap->addr, uap->len);
359 return (munmap(td, (void *)uap));
362 static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = {
363 RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK,
364 RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NOFILE, -1,
365 RLIMIT_NPROC, RLIMIT_MEMLOCK
369 linux_setrlimit(td, uap)
371 struct linux_setrlimit_args *uap;
378 if (ldebug(setrlimit))
379 printf(ARGS(setrlimit, "%d, %p"),
380 uap->resource, (void *)uap->rlim);
382 if (uap->resource >= LINUX_RLIM_NLIMITS)
385 which = linux_to_bsd_resource[uap->resource];
391 copyin(uap->rlim, &rlim, sizeof (struct rlimit))))
393 return (kern_setrlimit(td, which, &rlim));
397 linux_getrlimit(td, uap)
399 struct linux_getrlimit_args *uap;
406 if (ldebug(getrlimit))
407 printf(ARGS(getrlimit, "%d, %p"),
408 uap->resource, (void *)uap->rlim);
410 if (uap->resource >= LINUX_RLIM_NLIMITS)
413 which = linux_to_bsd_resource[uap->resource];
418 PROC_LOCK(td->td_proc);
419 lim_rlimit(td->td_proc, which, &rlim);
420 PROC_UNLOCK(td->td_proc);
421 error = copyout(&rlim, uap->rlim, sizeof (struct rlimit));