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