]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - 6/sys/alpha/linux/linux_machdep.c
merge fix for boot-time hang on centos' xen
[FreeBSD/FreeBSD.git] / 6 / sys / alpha / linux / linux_machdep.c
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$");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/imgact.h>
35 #include <sys/lock.h>
36 #include <sys/malloc.h>
37 #include <sys/mman.h>
38 #include <sys/mount.h>
39 #include <sys/mutex.h>
40 #include <sys/proc.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>
46
47 #include <machine/pcb.h>
48
49 #include <vm/vm.h>
50 #include <vm/pmap.h>
51 #include <vm/vm_map.h>
52
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>
57
58 struct linux_select_argv {
59         int nfds;
60         fd_set *readfds;
61         fd_set *writefds;
62         fd_set *exceptfds;
63         struct timeval *timeout;
64 };
65
66 int
67 linux_execve(struct thread *td, struct linux_execve_args *args)
68 {
69         struct image_args eargs;
70         char *path;
71         int error;
72
73         LCONVPATHEXIST(td, args->path, &path);
74
75 #ifdef DEBUG
76         if (ldebug(execve))
77                 printf(ARGS(execve, "%s"), path);
78 #endif
79         error = exec_copyin_args(&eargs, path, UIO_SYSSPACE, args->argp,
80             args->envp);
81         free(path, M_TEMP);
82         if (error == 0)
83                 error = kern_execve(td, &eargs, NULL);
84         exec_free_args(&eargs);
85         return (error);
86 }
87
88 /*
89  * MPSAFE
90  */
91 int
92 linux_fork(struct thread *td, struct linux_fork_args *args)
93 {
94         int error;
95
96 #ifdef DEBUG
97         if (ldebug(fork))
98                 printf(ARGS(fork, ""));
99 #endif
100         if ((error = fork(td, (struct fork_args *)args)) != 0)
101                 return (error);
102
103         if (td->td_retval[1] == 1)
104                 td->td_retval[0] = 0;
105
106         return (0);
107 }
108
109 /*
110  * MPSAFE
111  */
112 int
113 linux_vfork(struct thread *td, struct linux_vfork_args *args)
114 {
115         int error;
116
117 #ifdef DEBUG
118         if (ldebug(vfork))
119                 printf(ARGS(vfork, ""));
120 #endif
121         if ((error = vfork(td, (struct vfork_args *)args)) != 0)
122                 return (error);
123         /* Are we the child? */
124         if (td->td_retval[1] == 1)
125                 td->td_retval[0] = 0;
126         return (0);
127 }
128
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
134
135 int
136 linux_clone(struct thread *td, struct linux_clone_args *args)
137 {
138         int error, ff = RFPROC | RFSTOPPED;
139         struct proc *p2;
140         struct thread *td2;
141         int exit_signal;
142
143 #ifdef DEBUG
144         if (ldebug(clone)) {
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"));
149         }
150 #endif
151
152         if (!args->stack)
153                 return (EINVAL);
154
155         exit_signal = args->flags & 0x000000ff;
156         if (exit_signal >= LINUX_NSIG)
157                 return (EINVAL);
158
159 /*      if (exit_signal <= LINUX_SIGTBLSZ)
160                 exit_signal = linux_to_bsd_signal[_SIG_IDX(exit_signal)];
161 */
162
163         if (args->flags & CLONE_VM)
164                 ff |= RFMEM;
165         if (args->flags & CLONE_SIGHAND)
166                 ff |= RFSIGSHARE;
167         if (!(args->flags & CLONE_FILES))
168                 ff |= RFFDG;
169
170         error = fork1(td, ff, 0, &p2);
171         if (error)
172                 return (error);
173
174         PROC_LOCK(p2);
175         p2->p_sigparent = exit_signal;
176         PROC_UNLOCK(p2);
177         td2 = FIRST_THREAD_IN_PROC(p2);
178         td2->td_pcb->pcb_hw.apcb_usp = (unsigned long)args->stack;
179
180 #ifdef DEBUG
181         if (ldebug(clone))
182                 printf(LMSG("clone: successful rfork to %ld, stack %p sig = %d"),
183                     (long)p2->p_pid, args->stack, exit_signal);
184 #endif
185
186         /*
187          * Make this runnable after we are finished with it.
188          */
189         mtx_lock_spin(&sched_lock);
190         TD_SET_CAN_RUN(td2);
191         setrunqueue(td2, SRQ_BORING);
192         mtx_unlock_spin(&sched_lock);
193
194         td->td_retval[0] = p2->p_pid;
195         td->td_retval[1] = 0;
196         return (0);
197 }
198
199 #define STACK_SIZE  (2 * 1024 * 1024)
200 #define GUARD_SIZE  (4 * PAGE_SIZE)
201
202 int
203 linux_mmap(struct thread *td, struct linux_mmap_args *linux_args)
204 {
205         struct mmap_args /* {
206                 caddr_t addr;
207                 size_t len;
208                 int prot;
209                 int flags;
210                 int fd;
211                 long pad;
212                 off_t pos;
213         } */ bsd_args;
214         int error;
215
216 #ifdef DEBUG
217         if (ldebug(mmap))
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,
221                     linux_args->pos);
222 #endif
223         bsd_args.prot = linux_args->prot | PROT_READ;   /* always required */
224
225         bsd_args.flags = 0;
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);
233         } else {
234                 bsd_args.pos = linux_args->pos;
235         }
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;
240
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.
250                  *
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
255                  * in addr.
256                  *
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).
260                  */
261
262                 /* This gives us TOS */
263                 bsd_args.addr = (caddr_t)(linux_args->addr + linux_args->len);
264
265                 /* This gives us our maximum stack size */
266                 if (linux_args->len > STACK_SIZE - GUARD_SIZE)
267                         bsd_args.len = linux_args->len;
268                 else
269                         bsd_args.len  = STACK_SIZE - GUARD_SIZE;
270
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.
276                  */
277                 bsd_args.addr -= bsd_args.len;
278                 bsd_args.addr = (caddr_t)round_page(bsd_args.addr); /* XXXX */
279         } else {
280                 bsd_args.addr = (caddr_t)linux_args->addr;
281                 bsd_args.len  = linux_args->len;
282         }
283
284         bsd_args.fd = linux_args->fd;
285         if(linux_args->fd == 0)
286                 bsd_args.fd = -1;
287
288         bsd_args.pad = 0;
289 #ifdef DEBUG
290         if (ldebug(mmap))
291                 printf(ARGS(mmap, "%p, 0x%lx, 0x%x, 0x%x, 0x%x, 0x%lx"),
292                     (void *)bsd_args.addr,
293                     bsd_args.len,
294                     bsd_args.prot,
295                     bsd_args.flags,
296                     bsd_args.fd,
297                     bsd_args.pos);
298 #endif
299         if (bsd_args.addr == 0)
300                 bsd_args.addr = (caddr_t)0x40000000UL;
301         error = mmap(td, &bsd_args);
302 #ifdef DEBUG
303         if (ldebug(mmap))
304                 printf(LMSG("mmap returns %d, 0x%lx"), error, td->td_retval[0]);
305 #endif
306         return (error);
307 }
308
309 int
310 linux_rt_sigsuspend(td, uap)
311         struct thread *td;
312         struct linux_rt_sigsuspend_args *uap;
313 {
314         int error;
315         l_sigset_t lmask;
316         sigset_t bmask;
317
318 #ifdef DEBUG
319         if (ldebug(rt_sigsuspend))
320                 printf(ARGS(rt_sigsuspend, "%p, %zd"),
321                     (void *)uap->newset, uap->sigsetsize);
322 #endif
323         if (uap->sigsetsize != sizeof(l_sigset_t))
324                 return (EINVAL);
325
326         error = copyin(uap->newset, &lmask, sizeof(l_sigset_t));
327         if (error)
328                 return (error);
329
330         linux_to_bsd_sigset(&lmask, &bmask);
331         return (kern_sigsuspend(td, bmask));
332 }
333
334 int
335 linux_mprotect(td, uap)
336         struct thread *td;
337         struct linux_mprotect_args *uap;
338 {
339
340 #ifdef DEBUG
341         if (ldebug(mprotect))
342                 printf(ARGS(mprotect, "%p, 0x%zx, 0x%lx"),
343                     (void *)uap->addr, uap->len, uap->prot);
344 #endif
345         return (mprotect(td, (void *)uap));
346 }
347
348 int
349 linux_munmap(td, uap)
350         struct thread *td;
351         struct linux_munmap_args *uap;
352 {
353
354 #ifdef DEBUG
355         if (ldebug(munmap))
356                 printf(ARGS(munmap, "%p, 0x%lx"),
357                     (void *)uap->addr, uap->len);
358 #endif
359         return (munmap(td, (void *)uap));
360 }
361
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
366 };
367
368 int
369 linux_setrlimit(td, uap)
370         struct thread *td;
371         struct linux_setrlimit_args *uap;
372 {
373         struct rlimit rlim;
374         u_int which;
375         int error;
376
377 #ifdef DEBUG
378         if (ldebug(setrlimit))
379                 printf(ARGS(setrlimit, "%d, %p"),
380                     uap->resource, (void *)uap->rlim);
381 #endif
382         if (uap->resource >= LINUX_RLIM_NLIMITS)
383                 return EINVAL;
384
385         which = linux_to_bsd_resource[uap->resource];
386
387         if (which == -1)
388                 return EINVAL;
389
390         if ((error =
391            copyin(uap->rlim, &rlim, sizeof (struct rlimit))))
392                 return (error);
393         return (kern_setrlimit(td,  which, &rlim));
394 }
395
396 int
397 linux_getrlimit(td, uap)
398         struct thread *td;
399         struct linux_getrlimit_args *uap;
400 {
401         struct rlimit rlim;
402         u_int which;
403         int error;
404
405 #ifdef DEBUG
406         if (ldebug(getrlimit))
407                 printf(ARGS(getrlimit, "%d, %p"),
408                     uap->resource, (void *)uap->rlim);
409 #endif
410         if (uap->resource >= LINUX_RLIM_NLIMITS)
411                 return EINVAL;
412
413         which = linux_to_bsd_resource[uap->resource];
414
415         if (which == -1)
416                 return EINVAL;
417
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));
422         return (error);
423 }