linux_misc.c revision 1.13 1 1.13 mycroft /* $NetBSD: linux_misc.c,v 1.13 1995/08/16 04:29:49 mycroft Exp $ */
2 1.1 fvdl
3 1.1 fvdl /*
4 1.1 fvdl * Copyright (c) 1995 Frank van der Linden
5 1.1 fvdl * All rights reserved.
6 1.1 fvdl *
7 1.1 fvdl * Redistribution and use in source and binary forms, with or without
8 1.1 fvdl * modification, are permitted provided that the following conditions
9 1.1 fvdl * are met:
10 1.1 fvdl * 1. Redistributions of source code must retain the above copyright
11 1.1 fvdl * notice, this list of conditions and the following disclaimer.
12 1.1 fvdl * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 fvdl * notice, this list of conditions and the following disclaimer in the
14 1.1 fvdl * documentation and/or other materials provided with the distribution.
15 1.1 fvdl * 3. All advertising materials mentioning features or use of this software
16 1.1 fvdl * must display the following acknowledgement:
17 1.1 fvdl * This product includes software developed for the NetBSD Project
18 1.1 fvdl * by Frank van der Linden
19 1.1 fvdl * 4. The name of the author may not be used to endorse or promote products
20 1.1 fvdl * derived from this software without specific prior written permission
21 1.1 fvdl *
22 1.1 fvdl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 1.1 fvdl * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.1 fvdl * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.1 fvdl * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 1.1 fvdl * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 1.1 fvdl * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 1.1 fvdl * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 1.1 fvdl * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 1.1 fvdl * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 1.1 fvdl * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.1 fvdl */
33 1.1 fvdl
34 1.1 fvdl /*
35 1.1 fvdl * Linux compatibility module. Try to deal with various Linux system calls.
36 1.1 fvdl */
37 1.1 fvdl
38 1.1 fvdl #include <sys/param.h>
39 1.1 fvdl #include <sys/systm.h>
40 1.1 fvdl #include <sys/namei.h>
41 1.1 fvdl #include <sys/proc.h>
42 1.1 fvdl #include <sys/dir.h>
43 1.1 fvdl #include <sys/file.h>
44 1.1 fvdl #include <sys/stat.h>
45 1.1 fvdl #include <sys/filedesc.h>
46 1.1 fvdl #include <sys/ioctl.h>
47 1.1 fvdl #include <sys/kernel.h>
48 1.1 fvdl #include <sys/malloc.h>
49 1.1 fvdl #include <sys/mbuf.h>
50 1.1 fvdl #include <sys/mman.h>
51 1.1 fvdl #include <sys/mount.h>
52 1.1 fvdl #include <sys/ptrace.h>
53 1.1 fvdl #include <sys/resource.h>
54 1.1 fvdl #include <sys/resourcevar.h>
55 1.1 fvdl #include <sys/signal.h>
56 1.1 fvdl #include <sys/signalvar.h>
57 1.1 fvdl #include <sys/socket.h>
58 1.1 fvdl #include <sys/time.h>
59 1.1 fvdl #include <sys/times.h>
60 1.1 fvdl #include <sys/vnode.h>
61 1.1 fvdl #include <sys/uio.h>
62 1.1 fvdl #include <sys/wait.h>
63 1.1 fvdl #include <sys/utsname.h>
64 1.1 fvdl #include <sys/unistd.h>
65 1.1 fvdl
66 1.1 fvdl #include <sys/syscallargs.h>
67 1.1 fvdl
68 1.1 fvdl #include <vm/vm.h>
69 1.1 fvdl #include <vm/vm_param.h>
70 1.1 fvdl
71 1.1 fvdl #include <compat/linux/linux_types.h>
72 1.1 fvdl #include <compat/linux/linux_fcntl.h>
73 1.1 fvdl #include <compat/linux/linux_mmap.h>
74 1.11 mycroft #include <compat/linux/linux_signal.h>
75 1.1 fvdl #include <compat/linux/linux_syscallargs.h>
76 1.1 fvdl #include <compat/linux/linux_util.h>
77 1.1 fvdl #include <compat/linux/linux_dirent.h>
78 1.1 fvdl
79 1.1 fvdl /*
80 1.1 fvdl * The information on a terminated (or stopped) process needs
81 1.1 fvdl * to be converted in order for Linux binaries to get a valid signal
82 1.1 fvdl * number out of it.
83 1.1 fvdl */
84 1.1 fvdl static int
85 1.1 fvdl bsd_to_linux_wstat(status)
86 1.1 fvdl int *status;
87 1.1 fvdl {
88 1.1 fvdl if (WIFSIGNALED(*status))
89 1.1 fvdl *status = (*status & ~0177) |
90 1.12 mycroft bsd_to_linux_sig[WTERMSIG(*status)];
91 1.1 fvdl else if (WIFSTOPPED(*status))
92 1.1 fvdl *status = (*status & ~0xff00) |
93 1.12 mycroft (bsd_to_linux_sig[WSTOPSIG(*status)] << 8);
94 1.1 fvdl }
95 1.1 fvdl
96 1.1 fvdl /*
97 1.1 fvdl * waitpid(2). Passed on to the NetBSD call, surrounded by code to
98 1.1 fvdl * reserve some space for a NetBSD-style wait status, and converting
99 1.1 fvdl * it to what Linux wants.
100 1.1 fvdl */
101 1.1 fvdl int
102 1.1 fvdl linux_waitpid(p, uap, retval)
103 1.1 fvdl struct proc *p;
104 1.1 fvdl struct linux_waitpid_args /* {
105 1.1 fvdl syscallarg(int) pid;
106 1.1 fvdl syscallarg(int *) status;
107 1.1 fvdl syscallarg(int) options;
108 1.1 fvdl } */ *uap;
109 1.1 fvdl register_t *retval;
110 1.1 fvdl {
111 1.1 fvdl struct wait4_args w4a;
112 1.1 fvdl int error, *status, tstat;
113 1.1 fvdl caddr_t sg;
114 1.1 fvdl
115 1.9 christos sg = stackgap_init(p->p_emul);
116 1.1 fvdl status = (int *) stackgap_alloc(&sg, sizeof status);
117 1.1 fvdl
118 1.1 fvdl SCARG(&w4a, pid) = SCARG(uap, pid);
119 1.1 fvdl SCARG(&w4a, status) = status;
120 1.1 fvdl SCARG(&w4a, options) = SCARG(uap, options);
121 1.1 fvdl SCARG(&w4a, rusage) = NULL;
122 1.1 fvdl
123 1.1 fvdl if ((error = wait4(p, &w4a, retval)))
124 1.1 fvdl return error;
125 1.1 fvdl
126 1.1 fvdl if ((error = copyin(status, &tstat, sizeof tstat)))
127 1.1 fvdl return error;
128 1.1 fvdl
129 1.1 fvdl bsd_to_linux_wstat(&tstat);
130 1.1 fvdl
131 1.1 fvdl return copyout(&tstat, SCARG(uap, status), sizeof tstat);
132 1.1 fvdl }
133 1.1 fvdl
134 1.1 fvdl /*
135 1.1 fvdl * This is very much the same as waitpid()
136 1.1 fvdl */
137 1.1 fvdl int
138 1.1 fvdl linux_wait4(p, uap, retval)
139 1.1 fvdl struct proc *p;
140 1.1 fvdl struct linux_wait4_args /* {
141 1.1 fvdl syscallarg(int) pid;
142 1.1 fvdl syscallarg(int *) status;
143 1.1 fvdl syscallarg(int) options;
144 1.1 fvdl syscallarg(struct rusage *) rusage;
145 1.1 fvdl } */ *uap;
146 1.1 fvdl register_t *retval;
147 1.1 fvdl {
148 1.1 fvdl struct wait4_args w4a;
149 1.1 fvdl int error, *status, tstat;
150 1.1 fvdl caddr_t sg;
151 1.1 fvdl
152 1.9 christos sg = stackgap_init(p->p_emul);
153 1.1 fvdl status = (int *) stackgap_alloc(&sg, sizeof status);
154 1.1 fvdl
155 1.1 fvdl SCARG(&w4a, pid) = SCARG(uap, pid);
156 1.1 fvdl SCARG(&w4a, status) = status;
157 1.1 fvdl SCARG(&w4a, options) = SCARG(uap, options);
158 1.1 fvdl SCARG(&w4a, rusage) = SCARG(uap, rusage);
159 1.1 fvdl
160 1.1 fvdl if ((error = wait4(p, &w4a, retval)))
161 1.1 fvdl return error;
162 1.1 fvdl
163 1.1 fvdl if ((error = copyin(status, &tstat, sizeof tstat)))
164 1.1 fvdl return error;
165 1.1 fvdl
166 1.1 fvdl bsd_to_linux_wstat(&tstat);
167 1.1 fvdl
168 1.1 fvdl return copyout(&tstat, SCARG(uap, status), sizeof tstat);
169 1.1 fvdl }
170 1.1 fvdl
171 1.1 fvdl /*
172 1.1 fvdl * This is the old brk(2) call. I don't think anything in the Linux
173 1.1 fvdl * world uses this anymore
174 1.1 fvdl */
175 1.1 fvdl int
176 1.1 fvdl linux_break(p, uap, retval)
177 1.1 fvdl struct proc *p;
178 1.1 fvdl struct linux_brk_args /* {
179 1.1 fvdl syscallarg(char *) nsize;
180 1.1 fvdl } */ *uap;
181 1.1 fvdl register_t *retval;
182 1.1 fvdl {
183 1.1 fvdl return ENOSYS;
184 1.1 fvdl }
185 1.1 fvdl
186 1.1 fvdl /*
187 1.1 fvdl * Linux brk(2). The check if the new address is >= the old one is
188 1.1 fvdl * done in the kernel in Linux. NetBSD does it in the library.
189 1.1 fvdl */
190 1.1 fvdl int
191 1.1 fvdl linux_brk(p, uap, retval)
192 1.1 fvdl struct proc *p;
193 1.1 fvdl struct linux_brk_args /* {
194 1.1 fvdl syscallarg(char *) nsize;
195 1.1 fvdl } */ *uap;
196 1.1 fvdl register_t *retval;
197 1.1 fvdl {
198 1.1 fvdl char *nbrk = SCARG(uap, nsize);
199 1.1 fvdl struct obreak_args oba;
200 1.1 fvdl struct vmspace *vm = p->p_vmspace;
201 1.1 fvdl int error = 0;
202 1.1 fvdl caddr_t oldbrk, newbrk;
203 1.1 fvdl
204 1.1 fvdl oldbrk = vm->vm_daddr + ctob(vm->vm_dsize);
205 1.1 fvdl /*
206 1.1 fvdl * XXX inconsistent.. Linux always returns at least the old
207 1.1 fvdl * brk value, but it will be page-aligned if this fails,
208 1.1 fvdl * and possibly not page aligned if it succeeds (the user
209 1.1 fvdl * supplied pointer is returned).
210 1.1 fvdl */
211 1.1 fvdl SCARG(&oba, nsize) = nbrk;
212 1.1 fvdl
213 1.1 fvdl if ((caddr_t) nbrk > vm->vm_daddr && obreak(p, &oba, retval) == 0)
214 1.1 fvdl retval[0] = (register_t) nbrk;
215 1.1 fvdl else
216 1.1 fvdl retval[0] = (register_t) oldbrk;
217 1.1 fvdl
218 1.1 fvdl return 0;
219 1.1 fvdl }
220 1.1 fvdl
221 1.1 fvdl /*
222 1.1 fvdl * I wonder why Linux has gettimeofday() _and_ time().. Still, we
223 1.1 fvdl * need to deal with it.
224 1.1 fvdl */
225 1.1 fvdl int
226 1.1 fvdl linux_time(p, uap, retval)
227 1.1 fvdl struct proc *p;
228 1.1 fvdl struct linux_time_args /* {
229 1.1 fvdl linux_time_t *t;
230 1.1 fvdl } */ *uap;
231 1.1 fvdl register_t *retval;
232 1.1 fvdl {
233 1.1 fvdl struct timeval atv;
234 1.1 fvdl linux_time_t tt;
235 1.1 fvdl int error;
236 1.1 fvdl
237 1.1 fvdl microtime(&atv);
238 1.1 fvdl
239 1.1 fvdl tt = atv.tv_sec;
240 1.1 fvdl if (SCARG(uap, t) && (error = copyout(&tt, SCARG(uap, t), sizeof tt)))
241 1.1 fvdl return error;
242 1.1 fvdl
243 1.1 fvdl retval[0] = tt;
244 1.1 fvdl return 0;
245 1.1 fvdl }
246 1.1 fvdl
247 1.1 fvdl /*
248 1.2 fvdl * Convert BSD statfs structure to Linux statfs structure.
249 1.2 fvdl * The Linux structure has less fields, and it also wants
250 1.2 fvdl * the length of a name in a dir entry in a field, which
251 1.2 fvdl * we fake (probably the wrong way).
252 1.2 fvdl */
253 1.2 fvdl static void
254 1.2 fvdl bsd_to_linux_statfs(bsp, lsp)
255 1.2 fvdl struct statfs *bsp;
256 1.2 fvdl struct linux_statfs *lsp;
257 1.2 fvdl {
258 1.2 fvdl lsp->l_ftype = bsp->f_type;
259 1.2 fvdl lsp->l_fbsize = bsp->f_bsize;
260 1.2 fvdl lsp->l_fblocks = bsp->f_blocks;
261 1.2 fvdl lsp->l_fbfree = bsp->f_bfree;
262 1.2 fvdl lsp->l_fbavail = bsp->f_bavail;
263 1.2 fvdl lsp->l_ffiles = bsp->f_files;
264 1.2 fvdl lsp->l_fffree = bsp->f_ffree;
265 1.2 fvdl lsp->l_ffsid.val[0] = bsp->f_fsid.val[0];
266 1.2 fvdl lsp->l_ffsid.val[1] = bsp->f_fsid.val[1];
267 1.2 fvdl lsp->l_fnamelen = MAXNAMLEN; /* XXX */
268 1.2 fvdl }
269 1.2 fvdl
270 1.2 fvdl /*
271 1.2 fvdl * Implement the fs stat functions. Straightforward.
272 1.1 fvdl */
273 1.1 fvdl int
274 1.1 fvdl linux_statfs(p, uap, retval)
275 1.1 fvdl struct proc *p;
276 1.1 fvdl struct linux_statfs_args /* {
277 1.1 fvdl syscallarg(char *) path;
278 1.1 fvdl syscallarg(struct linux_statfs *) sp;
279 1.1 fvdl } */ *uap;
280 1.1 fvdl register_t *retval;
281 1.1 fvdl {
282 1.2 fvdl struct statfs btmp, *bsp;
283 1.2 fvdl struct linux_statfs ltmp;
284 1.2 fvdl struct statfs_args bsa;
285 1.2 fvdl caddr_t sg;
286 1.2 fvdl int error;
287 1.2 fvdl
288 1.9 christos sg = stackgap_init(p->p_emul);
289 1.2 fvdl bsp = (struct statfs *) stackgap_alloc(&sg, sizeof (struct statfs));
290 1.2 fvdl
291 1.9 christos LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
292 1.2 fvdl
293 1.2 fvdl SCARG(&bsa, path) = SCARG(uap, path);
294 1.2 fvdl SCARG(&bsa, buf) = bsp;
295 1.2 fvdl
296 1.2 fvdl if ((error = statfs(p, &bsa, retval)))
297 1.2 fvdl return error;
298 1.2 fvdl
299 1.2 fvdl if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp)))
300 1.2 fvdl return error;
301 1.2 fvdl
302 1.2 fvdl bsd_to_linux_statfs(&btmp, <mp);
303 1.2 fvdl
304 1.2 fvdl return copyout((caddr_t) <mp, (caddr_t) SCARG(uap, sp), sizeof ltmp);
305 1.1 fvdl }
306 1.1 fvdl
307 1.1 fvdl int
308 1.1 fvdl linux_fstatfs(p, uap, retval)
309 1.1 fvdl struct proc *p;
310 1.1 fvdl struct linux_fstatfs_args /* {
311 1.2 fvdl syscallarg(int) fd;
312 1.1 fvdl syscallarg(struct linux_statfs *) sp;
313 1.1 fvdl } */ *uap;
314 1.1 fvdl register_t *retval;
315 1.1 fvdl {
316 1.2 fvdl struct statfs btmp, *bsp;
317 1.2 fvdl struct linux_statfs ltmp;
318 1.2 fvdl struct fstatfs_args bsa;
319 1.2 fvdl caddr_t sg;
320 1.2 fvdl int error;
321 1.2 fvdl
322 1.9 christos sg = stackgap_init(p->p_emul);
323 1.2 fvdl bsp = (struct statfs *) stackgap_alloc(&sg, sizeof (struct statfs));
324 1.2 fvdl
325 1.2 fvdl SCARG(&bsa, fd) = SCARG(uap, fd);
326 1.2 fvdl SCARG(&bsa, buf) = bsp;
327 1.2 fvdl
328 1.2 fvdl if ((error = statfs(p, &bsa, retval)))
329 1.2 fvdl return error;
330 1.2 fvdl
331 1.2 fvdl if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp)))
332 1.2 fvdl return error;
333 1.2 fvdl
334 1.2 fvdl bsd_to_linux_statfs(&btmp, <mp);
335 1.2 fvdl
336 1.2 fvdl return copyout((caddr_t) <mp, (caddr_t) SCARG(uap, sp), sizeof ltmp);
337 1.1 fvdl }
338 1.1 fvdl
339 1.1 fvdl /*
340 1.1 fvdl * uname(). Just copy the info from the various strings stored in the
341 1.1 fvdl * kernel, and put it in the Linux utsname structure. That structure
342 1.1 fvdl * is almost the same as the NetBSD one, only it has fields 65 characters
343 1.1 fvdl * long, and an extra domainname field.
344 1.1 fvdl */
345 1.1 fvdl int
346 1.1 fvdl linux_uname(p, uap, retval)
347 1.1 fvdl struct proc *p;
348 1.1 fvdl struct linux_uname_args /* {
349 1.1 fvdl syscallarg(struct linux_utsname *) up;
350 1.1 fvdl } */ *uap;
351 1.1 fvdl register_t *retval;
352 1.1 fvdl {
353 1.1 fvdl extern char ostype[], osrelease[], version[], hostname[], domainname[];
354 1.1 fvdl extern char machine[];
355 1.1 fvdl struct linux_utsname tluts;
356 1.1 fvdl int len;
357 1.1 fvdl char *cp;
358 1.1 fvdl
359 1.1 fvdl strncpy(tluts.l_sysname, ostype, sizeof (tluts.l_sysname));
360 1.1 fvdl strncpy(tluts.l_nodename, hostname, sizeof (tluts.l_nodename));
361 1.1 fvdl strncpy(tluts.l_release, osrelease, sizeof (tluts.l_release));
362 1.1 fvdl strncpy(tluts.l_machine, machine, sizeof (tluts.l_machine));
363 1.1 fvdl strncpy(tluts.l_domainname, domainname, sizeof (tluts.l_domainname));
364 1.1 fvdl strncpy(tluts.l_version, version, sizeof (tluts.l_version));
365 1.1 fvdl
366 1.1 fvdl /* This part taken from the the uname() in libc */
367 1.1 fvdl len = sizeof (tluts.l_version);
368 1.1 fvdl for (cp = tluts.l_version; len--; ++cp)
369 1.1 fvdl if (*cp == '\n' || *cp == '\t')
370 1.1 fvdl if (len > 1)
371 1.1 fvdl *cp = ' ';
372 1.1 fvdl else
373 1.1 fvdl *cp = '\0';
374 1.1 fvdl
375 1.1 fvdl return copyout(&tluts, SCARG(uap, up), sizeof tluts);
376 1.1 fvdl }
377 1.1 fvdl
378 1.1 fvdl /*
379 1.1 fvdl * Linux wants to pass everything to a syscall in registers. However,
380 1.1 fvdl * mmap() has 6 of them. Oops: out of register error. They just pass
381 1.1 fvdl * everything in a structure.
382 1.1 fvdl */
383 1.1 fvdl int
384 1.1 fvdl linux_mmap(p, uap, retval)
385 1.1 fvdl struct proc *p;
386 1.1 fvdl struct linux_mmap_args /* {
387 1.1 fvdl syscallarg(struct linux_mmap *) lmp;
388 1.1 fvdl } */ *uap;
389 1.1 fvdl register_t *retval;
390 1.1 fvdl {
391 1.1 fvdl struct linux_mmap lmap;
392 1.1 fvdl struct mmap_args cma;
393 1.1 fvdl int error, flags;
394 1.1 fvdl
395 1.1 fvdl if ((error = copyin(SCARG(uap, lmp), &lmap, sizeof lmap)))
396 1.1 fvdl return error;
397 1.1 fvdl
398 1.1 fvdl flags = 0;
399 1.1 fvdl flags |= cvtto_bsd_mask(lmap.lm_flags, LINUX_MAP_SHARED, MAP_SHARED);
400 1.1 fvdl flags |= cvtto_bsd_mask(lmap.lm_flags, LINUX_MAP_PRIVATE, MAP_PRIVATE);
401 1.1 fvdl flags |= cvtto_bsd_mask(lmap.lm_flags, LINUX_MAP_FIXED, MAP_FIXED);
402 1.1 fvdl flags |= cvtto_bsd_mask(lmap.lm_flags, LINUX_MAP_ANON, MAP_ANON);
403 1.1 fvdl
404 1.1 fvdl SCARG(&cma,addr) = lmap.lm_addr;
405 1.1 fvdl SCARG(&cma,len) = lmap.lm_len;
406 1.1 fvdl SCARG(&cma,prot) = lmap.lm_prot;
407 1.1 fvdl SCARG(&cma,flags) = flags;
408 1.1 fvdl SCARG(&cma,fd) = lmap.lm_fd;
409 1.1 fvdl SCARG(&cma,pad) = 0;
410 1.1 fvdl SCARG(&cma,pos) = lmap.lm_pos;
411 1.1 fvdl
412 1.1 fvdl return mmap(p, &cma, retval);
413 1.1 fvdl }
414 1.1 fvdl
415 1.1 fvdl /*
416 1.1 fvdl * Linux doesn't use the retval[1] value to determine whether
417 1.1 fvdl * we are the child or parent.
418 1.1 fvdl */
419 1.1 fvdl int
420 1.1 fvdl linux_fork(p, uap, retval)
421 1.1 fvdl struct proc *p;
422 1.1 fvdl void *uap;
423 1.1 fvdl register_t *retval;
424 1.1 fvdl {
425 1.1 fvdl int error;
426 1.1 fvdl
427 1.1 fvdl if ((error = fork(p, uap, retval)))
428 1.1 fvdl return error;
429 1.1 fvdl
430 1.1 fvdl if (retval[1] == 1)
431 1.1 fvdl retval[0] = 0;
432 1.1 fvdl
433 1.1 fvdl return 0;
434 1.1 fvdl }
435 1.1 fvdl
436 1.1 fvdl /*
437 1.1 fvdl * This code is partly stolen from src/lib/libc/compat-43/times.c
438 1.1 fvdl * XXX - CLK_TCK isn't declared in /sys, just in <time.h>, done here
439 1.1 fvdl */
440 1.1 fvdl
441 1.1 fvdl #define CLK_TCK 100
442 1.1 fvdl #define CONVTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
443 1.1 fvdl
444 1.1 fvdl int
445 1.1 fvdl linux_times(p, uap, retval)
446 1.1 fvdl struct proc *p;
447 1.1 fvdl struct linux_times_args /* {
448 1.1 fvdl syscallarg(struct times *) tms;
449 1.1 fvdl } */ *uap;
450 1.1 fvdl register_t *retval;
451 1.1 fvdl {
452 1.1 fvdl struct timeval t;
453 1.1 fvdl struct linux_tms ltms;
454 1.1 fvdl struct rusage ru;
455 1.4 mycroft int error, s;
456 1.1 fvdl
457 1.1 fvdl calcru(p, &ru.ru_utime, &ru.ru_stime, NULL);
458 1.1 fvdl ltms.ltms_utime = CONVTCK(ru.ru_utime);
459 1.1 fvdl ltms.ltms_stime = CONVTCK(ru.ru_stime);
460 1.1 fvdl
461 1.1 fvdl ltms.ltms_cutime = CONVTCK(p->p_stats->p_cru.ru_utime);
462 1.1 fvdl ltms.ltms_cstime = CONVTCK(p->p_stats->p_cru.ru_stime);
463 1.1 fvdl
464 1.1 fvdl if ((error = copyout(<ms, SCARG(uap, tms), sizeof ltms)))
465 1.1 fvdl return error;
466 1.1 fvdl
467 1.4 mycroft s = splclock();
468 1.4 mycroft timersub(&time, &boottime, &t);
469 1.4 mycroft splx(s);
470 1.1 fvdl
471 1.1 fvdl retval[0] = ((linux_clock_t)(CONVTCK(t)));
472 1.1 fvdl return 0;
473 1.1 fvdl }
474 1.1 fvdl
475 1.1 fvdl /*
476 1.1 fvdl * NetBSD passes fd[0] in retval[0], and fd[1] in retval[1].
477 1.1 fvdl * Linux directly passes the pointer.
478 1.1 fvdl */
479 1.1 fvdl int
480 1.1 fvdl linux_pipe(p, uap, retval)
481 1.1 fvdl struct proc *p;
482 1.1 fvdl struct linux_pipe_args /* {
483 1.1 fvdl syscallarg(int *) pfds;
484 1.1 fvdl } */ *uap;
485 1.1 fvdl register_t *retval;
486 1.1 fvdl {
487 1.1 fvdl int error;
488 1.1 fvdl
489 1.1 fvdl if ((error = pipe(p, 0, retval)))
490 1.1 fvdl return error;
491 1.1 fvdl
492 1.1 fvdl /* Assumes register_t is an int */
493 1.1 fvdl
494 1.1 fvdl if ((error = copyout(retval, SCARG(uap, pfds), 2 * sizeof (int))))
495 1.1 fvdl return error;
496 1.1 fvdl
497 1.1 fvdl retval[0] = 0;
498 1.1 fvdl return 0;
499 1.1 fvdl }
500 1.1 fvdl
501 1.1 fvdl /*
502 1.1 fvdl * Alarm. This is a libc call which used setitimer(2) in NetBSD.
503 1.1 fvdl * Fiddle with the timers to make it work.
504 1.1 fvdl */
505 1.1 fvdl int
506 1.1 fvdl linux_alarm(p, uap, retval)
507 1.1 fvdl struct proc *p;
508 1.1 fvdl struct linux_alarm_args /* {
509 1.1 fvdl syscallarg(unsigned int) secs;
510 1.1 fvdl } */ *uap;
511 1.1 fvdl register_t *retval;
512 1.1 fvdl {
513 1.1 fvdl int error, s;
514 1.1 fvdl struct itimerval *itp, it;
515 1.1 fvdl
516 1.1 fvdl itp = &p->p_realtimer;
517 1.1 fvdl s = splclock();
518 1.1 fvdl /*
519 1.1 fvdl * Clear any pending timer alarms.
520 1.1 fvdl */
521 1.1 fvdl untimeout(realitexpire, p);
522 1.1 fvdl timerclear(&itp->it_interval);
523 1.1 fvdl if (timerisset(&itp->it_value) &&
524 1.1 fvdl timercmp(&itp->it_value, &time, >))
525 1.3 mycroft timersub(&itp->it_value, &time, &itp->it_value);
526 1.1 fvdl /*
527 1.1 fvdl * Return how many seconds were left (rounded up)
528 1.1 fvdl */
529 1.1 fvdl retval[0] = itp->it_value.tv_sec;
530 1.1 fvdl if (itp->it_value.tv_usec)
531 1.1 fvdl retval[0]++;
532 1.1 fvdl
533 1.1 fvdl /*
534 1.1 fvdl * alarm(0) just resets the timer.
535 1.1 fvdl */
536 1.1 fvdl if (SCARG(uap, secs) == 0) {
537 1.1 fvdl timerclear(&itp->it_value);
538 1.1 fvdl splx(s);
539 1.1 fvdl return 0;
540 1.1 fvdl }
541 1.1 fvdl
542 1.1 fvdl /*
543 1.1 fvdl * Check the new alarm time for sanity, and set it.
544 1.1 fvdl */
545 1.1 fvdl timerclear(&it.it_interval);
546 1.1 fvdl it.it_value.tv_sec = SCARG(uap, secs);
547 1.1 fvdl it.it_value.tv_usec = 0;
548 1.1 fvdl if (itimerfix(&it.it_value) || itimerfix(&it.it_interval)) {
549 1.1 fvdl splx(s);
550 1.1 fvdl return (EINVAL);
551 1.1 fvdl }
552 1.1 fvdl
553 1.1 fvdl if (timerisset(&it.it_value)) {
554 1.3 mycroft timeradd(&it.it_value, &time, &it.it_value);
555 1.1 fvdl timeout(realitexpire, p, hzto(&it.it_value));
556 1.1 fvdl }
557 1.1 fvdl p->p_realtimer = it;
558 1.1 fvdl splx(s);
559 1.1 fvdl
560 1.1 fvdl return 0;
561 1.1 fvdl }
562 1.1 fvdl
563 1.1 fvdl /*
564 1.1 fvdl * utime(). Do conversion to things that utimes() understands,
565 1.1 fvdl * and pass it on.
566 1.1 fvdl */
567 1.1 fvdl int
568 1.1 fvdl linux_utime(p, uap, retval)
569 1.1 fvdl struct proc *p;
570 1.1 fvdl struct linux_utime_args /* {
571 1.1 fvdl syscallarg(char *) path;
572 1.1 fvdl syscallarg(struct linux_utimbuf *)times;
573 1.1 fvdl } */ *uap;
574 1.1 fvdl register_t *retval;
575 1.1 fvdl {
576 1.1 fvdl caddr_t sg;
577 1.1 fvdl int error;
578 1.1 fvdl struct utimes_args ua;
579 1.1 fvdl struct timeval tv[2], *tvp;
580 1.1 fvdl struct linux_utimbuf lut;
581 1.1 fvdl
582 1.9 christos sg = stackgap_init(p->p_emul);
583 1.9 christos LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
584 1.1 fvdl
585 1.1 fvdl SCARG(&ua, path) = SCARG(uap, path);
586 1.1 fvdl
587 1.1 fvdl if (SCARG(uap, times) != NULL) {
588 1.1 fvdl if ((error = copyin(SCARG(uap, times), &lut, sizeof lut)))
589 1.1 fvdl return error;
590 1.1 fvdl tv[0].tv_usec = tv[1].tv_usec = 0;
591 1.1 fvdl tv[0].tv_sec = lut.l_actime;
592 1.1 fvdl tv[1].tv_sec = lut.l_modtime;
593 1.9 christos tvp = (struct timeval *) stackgap_alloc(&sg, sizeof(tv));
594 1.1 fvdl if ((error = copyout(tv, tvp, sizeof tv)))
595 1.1 fvdl return error;
596 1.1 fvdl SCARG(&ua, tptr) = tvp;
597 1.1 fvdl }
598 1.1 fvdl else
599 1.1 fvdl SCARG(&ua, tptr) = NULL;
600 1.1 fvdl
601 1.1 fvdl return utimes(p, uap, retval);
602 1.1 fvdl }
603 1.1 fvdl
604 1.1 fvdl /*
605 1.1 fvdl * Linux 'readdir' call. This code is mostly taken from the
606 1.1 fvdl * SunOS getdents call (see compat/sunos/sunos_misc.c), though
607 1.1 fvdl * an attempt has been made to keep it a little cleaner (failing
608 1.1 fvdl * miserably, because of the cruft needed if count 1 is passed).
609 1.1 fvdl *
610 1.1 fvdl * Read in BSD-style entries, convert them, and copy them out.
611 1.1 fvdl * Note that the Linux d_reclen is actually the name length,
612 1.1 fvdl * and d_off is the reclen.
613 1.1 fvdl *
614 1.1 fvdl * Note that this doesn't handle union-mounted filesystems.
615 1.1 fvdl */
616 1.1 fvdl int
617 1.1 fvdl linux_readdir(p, uap, retval)
618 1.1 fvdl struct proc *p;
619 1.1 fvdl struct linux_readdir_args /* {
620 1.1 fvdl syscallarg(int) fd;
621 1.1 fvdl syscallarg(struct linux_dirent *) dent;
622 1.1 fvdl syscallarg(unsigned int) count;
623 1.1 fvdl } */ *uap;
624 1.1 fvdl register_t *retval;
625 1.1 fvdl {
626 1.1 fvdl register struct dirent *bdp;
627 1.1 fvdl struct vnode *vp;
628 1.1 fvdl caddr_t inp, buf; /* BSD-format */
629 1.1 fvdl int len, reclen; /* BSD-format */
630 1.1 fvdl caddr_t outp; /* Linux-format */
631 1.1 fvdl int resid, linuxreclen; /* Linux-format */
632 1.1 fvdl struct file *fp;
633 1.1 fvdl struct uio auio;
634 1.1 fvdl struct iovec aiov;
635 1.1 fvdl struct linux_dirent idb;
636 1.1 fvdl off_t off; /* true file offset */
637 1.1 fvdl linux_off_t soff; /* Linux file offset */
638 1.1 fvdl int buflen, error, eofflag, nbytes, justone;
639 1.1 fvdl struct vattr va;
640 1.1 fvdl
641 1.1 fvdl if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
642 1.1 fvdl return (error);
643 1.1 fvdl
644 1.1 fvdl if ((fp->f_flag & FREAD) == 0)
645 1.1 fvdl return (EBADF);
646 1.1 fvdl
647 1.5 mycroft vp = (struct vnode *)fp->f_data;
648 1.1 fvdl
649 1.1 fvdl if (vp->v_type != VDIR) /* XXX vnode readdir op should do this */
650 1.1 fvdl return (EINVAL);
651 1.1 fvdl
652 1.1 fvdl if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)))
653 1.1 fvdl return error;
654 1.1 fvdl
655 1.1 fvdl nbytes = SCARG(uap, count);
656 1.1 fvdl if (nbytes == 1) { /* Need this for older Linux libs, apparently */
657 1.1 fvdl nbytes = sizeof (struct linux_dirent);
658 1.5 mycroft buflen = max(va.va_blocksize, nbytes);
659 1.1 fvdl justone = 1;
660 1.5 mycroft } else {
661 1.5 mycroft buflen = min(MAXBSIZE, nbytes);
662 1.5 mycroft justone = 0;
663 1.1 fvdl }
664 1.1 fvdl buf = malloc(buflen, M_TEMP, M_WAITOK);
665 1.1 fvdl VOP_LOCK(vp);
666 1.1 fvdl off = fp->f_offset;
667 1.1 fvdl again:
668 1.1 fvdl aiov.iov_base = buf;
669 1.1 fvdl aiov.iov_len = buflen;
670 1.1 fvdl auio.uio_iov = &aiov;
671 1.1 fvdl auio.uio_iovcnt = 1;
672 1.1 fvdl auio.uio_rw = UIO_READ;
673 1.1 fvdl auio.uio_segflg = UIO_SYSSPACE;
674 1.1 fvdl auio.uio_procp = p;
675 1.1 fvdl auio.uio_resid = buflen;
676 1.1 fvdl auio.uio_offset = off;
677 1.1 fvdl /*
678 1.1 fvdl * First we read into the malloc'ed buffer, then
679 1.1 fvdl * we massage it into user space, one record at a time.
680 1.1 fvdl */
681 1.5 mycroft error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, (u_long *)0, 0);
682 1.1 fvdl if (error)
683 1.1 fvdl goto out;
684 1.1 fvdl
685 1.1 fvdl inp = buf;
686 1.1 fvdl outp = (caddr_t) SCARG(uap, dent);
687 1.1 fvdl resid = nbytes;
688 1.1 fvdl if ((len = buflen - auio.uio_resid) == 0)
689 1.1 fvdl goto eof;
690 1.1 fvdl
691 1.1 fvdl for (; len > 0; len -= reclen) {
692 1.5 mycroft bdp = (struct dirent *)inp;
693 1.5 mycroft reclen = bdp->d_reclen;
694 1.1 fvdl if (reclen & 3)
695 1.1 fvdl panic("linux_readdir");
696 1.1 fvdl off += reclen; /* each entry points to next */
697 1.1 fvdl if (bdp->d_fileno == 0) {
698 1.1 fvdl inp += reclen; /* it is a hole; squish it out */
699 1.1 fvdl continue;
700 1.1 fvdl }
701 1.1 fvdl linuxreclen = LINUX_RECLEN(&idb, bdp->d_namlen);
702 1.1 fvdl if (reclen > len || resid < linuxreclen) {
703 1.1 fvdl /* entry too big for buffer, so just stop */
704 1.1 fvdl outp++;
705 1.1 fvdl break;
706 1.1 fvdl }
707 1.1 fvdl /*
708 1.1 fvdl * Massage in place to make a Linux-shaped dirent (otherwise
709 1.1 fvdl * we have to worry about touching user memory outside of
710 1.1 fvdl * the copyout() call).
711 1.1 fvdl */
712 1.5 mycroft idb.d_ino = (long)bdp->d_fileno;
713 1.5 mycroft idb.d_off = (linux_off_t)linuxreclen;
714 1.5 mycroft idb.d_reclen = (u_short)bdp->d_namlen;
715 1.5 mycroft strcpy(idb.d_name, bdp->d_name);
716 1.1 fvdl if ((error = copyout((caddr_t)&idb, outp, linuxreclen)))
717 1.1 fvdl goto out;
718 1.1 fvdl /* advance past this real entry */
719 1.1 fvdl inp += reclen;
720 1.1 fvdl /* advance output past Linux-shaped entry */
721 1.1 fvdl outp += linuxreclen;
722 1.1 fvdl resid -= linuxreclen;
723 1.1 fvdl if (justone)
724 1.1 fvdl break;
725 1.1 fvdl }
726 1.1 fvdl
727 1.1 fvdl /* if we squished out the whole block, try again */
728 1.1 fvdl if (outp == (caddr_t) SCARG(uap, dent))
729 1.1 fvdl goto again;
730 1.1 fvdl fp->f_offset = off; /* update the vnode offset */
731 1.1 fvdl
732 1.1 fvdl if (justone)
733 1.1 fvdl nbytes = resid + linuxreclen;
734 1.1 fvdl
735 1.1 fvdl eof:
736 1.1 fvdl *retval = nbytes - resid;
737 1.1 fvdl out:
738 1.1 fvdl VOP_UNLOCK(vp);
739 1.1 fvdl free(buf, M_TEMP);
740 1.1 fvdl return error;
741 1.1 fvdl }
742 1.1 fvdl
743 1.1 fvdl /*
744 1.7 fvdl * Out of register error once more.. Also, Linux copies the amount of
745 1.7 fvdl * time left into the user-supplied timeval structure.
746 1.1 fvdl */
747 1.1 fvdl int
748 1.1 fvdl linux_select(p, uap, retval)
749 1.1 fvdl struct proc *p;
750 1.1 fvdl struct linux_select_args /* {
751 1.1 fvdl syscallarg(struct linux_select *) lsp;
752 1.1 fvdl } */ *uap;
753 1.1 fvdl register_t *retval;
754 1.1 fvdl {
755 1.1 fvdl struct linux_select ls;
756 1.1 fvdl struct select_args bsa;
757 1.13 mycroft struct timeval tv0, tv1, utv, *tvp;
758 1.13 mycroft caddr_t sg;
759 1.1 fvdl int error;
760 1.1 fvdl
761 1.13 mycroft if ((error = copyin(SCARG(uap, lsp), &ls, sizeof(ls))))
762 1.1 fvdl return error;
763 1.1 fvdl
764 1.1 fvdl SCARG(&bsa, nd) = ls.nfds;
765 1.1 fvdl SCARG(&bsa, in) = ls.readfds;
766 1.1 fvdl SCARG(&bsa, ou) = ls.writefds;
767 1.1 fvdl SCARG(&bsa, ex) = ls.exceptfds;
768 1.1 fvdl SCARG(&bsa, tv) = ls.timeout;
769 1.1 fvdl
770 1.7 fvdl /*
771 1.7 fvdl * Store current time for computation of the amount of
772 1.7 fvdl * time left.
773 1.7 fvdl */
774 1.13 mycroft if (ls.timeout) {
775 1.13 mycroft if ((error = copyin(ls.timeout, &utv, sizeof(utv))))
776 1.13 mycroft return error;
777 1.13 mycroft if (itimerfix(&utv)) {
778 1.13 mycroft /*
779 1.13 mycroft * The timeval was invalid. Convert it to something
780 1.13 mycroft * valid that will act as it does under Linux.
781 1.13 mycroft */
782 1.13 mycroft sg = stackgap_init(p->p_emul);
783 1.13 mycroft tvp = stackgap_alloc(&sg, sizeof(utv));
784 1.13 mycroft utv.tv_sec += utv.tv_usec / 1000000;
785 1.13 mycroft utv.tv_usec %= 1000000;
786 1.13 mycroft if (utv.tv_usec < 0) {
787 1.13 mycroft utv.tv_sec -= 1;
788 1.13 mycroft utv.tv_usec += 1000000;
789 1.13 mycroft }
790 1.13 mycroft if (utv.tv_sec < 0)
791 1.13 mycroft timerclear(&utv);
792 1.13 mycroft if ((error = copyout(&utv, tvp, sizeof(utv))))
793 1.13 mycroft return error;
794 1.13 mycroft SCARG(&bsa, tv) = tvp;
795 1.13 mycroft }
796 1.7 fvdl microtime(&tv0);
797 1.13 mycroft }
798 1.7 fvdl
799 1.10 mycroft error = select(p, &bsa, retval);
800 1.10 mycroft if (error) {
801 1.10 mycroft /*
802 1.10 mycroft * See fs/select.c in the Linux kernel. Without this,
803 1.10 mycroft * Maelstrom doesn't work.
804 1.10 mycroft */
805 1.10 mycroft if (error == ERESTART)
806 1.10 mycroft error = EINTR;
807 1.7 fvdl return error;
808 1.10 mycroft }
809 1.7 fvdl
810 1.7 fvdl if (ls.timeout) {
811 1.7 fvdl if (!*retval) {
812 1.7 fvdl utv.tv_sec = 0;
813 1.7 fvdl utv.tv_usec = 0;
814 1.7 fvdl } else {
815 1.7 fvdl /*
816 1.13 mycroft * Compute how much time was left of the timeout,
817 1.7 fvdl * by subtracting the current time and the time
818 1.7 fvdl * before we started the call, and subtracting
819 1.7 fvdl * that result from the user-supplied value.
820 1.7 fvdl */
821 1.7 fvdl microtime(&tv1);
822 1.7 fvdl timersub(&tv1, &tv0, &tv1);
823 1.7 fvdl timersub(&utv, &tv1, &utv);
824 1.7 fvdl }
825 1.13 mycroft if ((error = copyout(&utv, ls.timeout, sizeof(utv))))
826 1.7 fvdl return error;
827 1.7 fvdl }
828 1.13 mycroft
829 1.7 fvdl return 0;
830 1.1 fvdl }
831 1.1 fvdl
832 1.1 fvdl /*
833 1.1 fvdl * Get the process group of a certain process. Look it up
834 1.1 fvdl * and return the value.
835 1.1 fvdl */
836 1.1 fvdl int
837 1.1 fvdl linux_getpgid(p, uap, retval)
838 1.1 fvdl struct proc *p;
839 1.1 fvdl struct linux_getpgid_args /* {
840 1.1 fvdl syscallarg(int) pid;
841 1.1 fvdl } */ *uap;
842 1.1 fvdl register_t *retval;
843 1.1 fvdl {
844 1.1 fvdl struct proc *targp;
845 1.1 fvdl
846 1.1 fvdl if (SCARG(uap, pid) != 0 && SCARG(uap, pid) != p->p_pid)
847 1.1 fvdl if ((targp = pfind(SCARG(uap, pid))) == 0)
848 1.1 fvdl return ESRCH;
849 1.1 fvdl else
850 1.1 fvdl targp = p;
851 1.1 fvdl
852 1.1 fvdl retval[0] = targp->p_pgid;
853 1.6 fvdl return 0;
854 1.6 fvdl }
855 1.6 fvdl
856 1.6 fvdl /*
857 1.6 fvdl * Set the 'personality' (emulation mode) for the current process. Only
858 1.6 fvdl * accept the Linux personality here (0). This call is needed because
859 1.6 fvdl * the Linux ELF crt0 issues it in an ugly kludge to make sure that
860 1.6 fvdl * ELF binaries run in Linux mode, not SVR4 mode.
861 1.6 fvdl */
862 1.6 fvdl int
863 1.6 fvdl linux_personality(p, uap, retval)
864 1.6 fvdl struct proc *p;
865 1.6 fvdl struct linux_personality_args /* P
866 1.6 fvdl syscallarg(int) per;
867 1.6 fvdl } */ *uap;
868 1.6 fvdl register_t *retval;
869 1.6 fvdl {
870 1.6 fvdl if (SCARG(uap, per) != 0)
871 1.6 fvdl return EINVAL;
872 1.6 fvdl retval[0] = 0;
873 1.1 fvdl return 0;
874 1.1 fvdl }
875