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