Bug Summary

File:modules/linux64/../../compat/linux/linux_misc.c
Warning:line 1093, column 11
Copies out a struct with a union element with different sizes

Annotated Source Code

1/*-
2 * Copyright (c) 2002 Doug Rabson
3 * Copyright (c) 1994-1995 Søren Schmidt
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer
11 * in this position and unchanged.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include <sys/cdefs.h>
31__FBSDID("$FreeBSD: releng/11.0/sys/compat/linux/linux_misc.c 303005 2016-07-18 16:34:11Z dchagin $")__asm__(".ident\t\"" "$FreeBSD: releng/11.0/sys/compat/linux/linux_misc.c 303005 2016-07-18 16:34:11Z dchagin $"
"\"")
;
32
33#include "opt_compat.h"
34
35#include <sys/param.h>
36#include <sys/blist.h>
37#include <sys/fcntl.h>
38#if defined(__i386__)
39#include <sys/imgact_aout.h>
40#endif
41#include <sys/jail.h>
42#include <sys/kernel.h>
43#include <sys/limits.h>
44#include <sys/lock.h>
45#include <sys/malloc.h>
46#include <sys/mman.h>
47#include <sys/mount.h>
48#include <sys/mutex.h>
49#include <sys/namei.h>
50#include <sys/priv.h>
51#include <sys/proc.h>
52#include <sys/reboot.h>
53#include <sys/racct.h>
54#include <sys/resourcevar.h>
55#include <sys/sched.h>
56#include <sys/sdt.h>
57#include <sys/signalvar.h>
58#include <sys/stat.h>
59#include <sys/syscallsubr.h>
60#include <sys/sysctl.h>
61#include <sys/sysproto.h>
62#include <sys/systm.h>
63#include <sys/time.h>
64#include <sys/vmmeter.h>
65#include <sys/vnode.h>
66#include <sys/wait.h>
67#include <sys/cpuset.h>
68
69#include <security/mac/mac_framework.h>
70
71#include <vm/vm.h>
72#include <vm/pmap.h>
73#include <vm/vm_kern.h>
74#include <vm/vm_map.h>
75#include <vm/vm_extern.h>
76#include <vm/vm_object.h>
77#include <vm/swap_pager.h>
78
79#ifdef COMPAT_LINUX32
80#include <machine/../linux32/linux.h>
81#include <machine/../linux32/linux32_proto.h>
82#else
83#include <machine/../linux/linux.h>
84#include <machine/../linux/linux_proto.h>
85#endif
86
87#include <compat/linux/linux_dtrace.h>
88#include <compat/linux/linux_file.h>
89#include <compat/linux/linux_mib.h>
90#include <compat/linux/linux_signal.h>
91#include <compat/linux/linux_timer.h>
92#include <compat/linux/linux_util.h>
93#include <compat/linux/linux_sysproto.h>
94#include <compat/linux/linux_emul.h>
95#include <compat/linux/linux_misc.h>
96
97/**
98 * Special DTrace provider for the linuxulator.
99 *
100 * In this file we define the provider for the entire linuxulator. All
101 * modules (= files of the linuxulator) use it.
102 *
103 * We define a different name depending on the emulated bitsize, see
104 * ../../<ARCH>/linux{,32}/linux.h, e.g.:
105 * native bitsize = linuxulator
106 * amd64, 32bit emulation = linuxulator32
107 */
108LIN_SDT_PROVIDER_DEFINE(LINUX_DTRACE)struct sdt_provider sdt_provider_linuxulator[1] = { { "linuxulator"
, { ((void *)0), ((void *)0) }, 0, 0 } }; __asm__(".globl " "__start_set_sdt_providers_set"
); __asm__(".globl " "__stop_set_sdt_providers_set"); static void
const * const __set_sdt_providers_set_sym_sdt_provider_linuxulator
__attribute__((__section__("set_" "sdt_providers_set"))) __attribute__
((__used__)) = &(sdt_provider_linuxulator);
;
109
110int stclohz; /* Statistics clock frequency */
111
112static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS10] = {
113 RLIMIT_CPU0, RLIMIT_FSIZE1, RLIMIT_DATA2, RLIMIT_STACK3,
114 RLIMIT_CORE4, RLIMIT_RSS5, RLIMIT_NPROC7, RLIMIT_NOFILE8,
115 RLIMIT_MEMLOCK6, RLIMIT_AS10
116};
117
118struct l_sysinfo {
119 l_long uptime; /* Seconds since boot */
120 l_ulong loads[3]; /* 1, 5, and 15 minute load averages */
121#define LINUX_SYSINFO_LOADS_SCALE65536 65536
122 l_ulong totalram; /* Total usable main memory size */
123 l_ulong freeram; /* Available memory size */
124 l_ulong sharedram; /* Amount of shared memory */
125 l_ulong bufferram; /* Memory used by buffers */
126 l_ulong totalswap; /* Total swap space size */
127 l_ulong freeswap; /* swap space still available */
128 l_ushort procs; /* Number of current processes */
129 l_ushort pads;
130 l_ulong totalbig;
131 l_ulong freebig;
132 l_uint mem_unit;
133 char _f[20-2*sizeof(l_long)-sizeof(l_int)]; /* padding */
134};
135
136struct l_pselect6arg {
137 l_uintptr_t ss;
138 l_size_t ss_len;
139};
140
141static int linux_utimensat_nsec_valid(l_long);
142
143
144int
145linux_sysinfo(struct thread *td, struct linux_sysinfo_args *args)
146{
147 struct l_sysinfo sysinfo;
148 vm_object_t object;
149 int i, j;
150 struct timespec ts;
151
152 bzero(&sysinfo, sizeof(sysinfo));
153 getnanouptime(&ts);
154 if (ts.tv_nsec != 0)
155 ts.tv_sec++;
156 sysinfo.uptime = ts.tv_sec;
157
158 /* Use the information from the mib to get our load averages */
159 for (i = 0; i < 3; i++)
160 sysinfo.loads[i] = averunnable.ldavg[i] *
161 LINUX_SYSINFO_LOADS_SCALE65536 / averunnable.fscale;
162
163 sysinfo.totalram = physmem * PAGE_SIZE(1<<12);
164 sysinfo.freeram = sysinfo.totalram - vm_cnt.v_wire_count * PAGE_SIZE(1<<12);
165
166 sysinfo.sharedram = 0;
167 mtx_lock(&vm_object_list_mtx)__mtx_lock_flags(&((((&vm_object_list_mtx))))->mtx_lock
, ((0)), ("/usr/src/sys/modules/linux64/../../compat/linux/linux_misc.c"
), (167))
;
168 TAILQ_FOREACH(object, &vm_object_list, object_list)for ((object) = (((&vm_object_list))->tqh_first); (object
); (object) = (((object))->object_list.tqe_next))
169 if (object->shadow_count > 1)
170 sysinfo.sharedram += object->resident_page_count;
171 mtx_unlock(&vm_object_list_mtx)__mtx_unlock_flags(&((((&vm_object_list_mtx))))->mtx_lock
, ((0)), ("/usr/src/sys/modules/linux64/../../compat/linux/linux_misc.c"
), (171))
;
172
173 sysinfo.sharedram *= PAGE_SIZE(1<<12);
174 sysinfo.bufferram = 0;
175
176 swap_pager_status(&i, &j);
177 sysinfo.totalswap = i * PAGE_SIZE(1<<12);
178 sysinfo.freeswap = (i - j) * PAGE_SIZE(1<<12);
179
180 sysinfo.procs = nprocs;
181
182 /* The following are only present in newer Linux kernels. */
183 sysinfo.totalbig = 0;
184 sysinfo.freebig = 0;
185 sysinfo.mem_unit = 1;
186
187 return (copyout(&sysinfo, args->info, sizeof(sysinfo)));
188}
189
190int
191linux_alarm(struct thread *td, struct linux_alarm_args *args)
192{
193 struct itimerval it, old_it;
194 u_int secs;
195 int error;
196
197#ifdef DEBUG
198 if (ldebug(alarm)((((const unsigned char *)(linux_debug_map))[(37)/8] & (1
<<((37)%8))) == 0)
)
199 printf(ARGS(alarm, "%u")"linux(%ld/%ld): ""alarm""(""%u"")\n", (long)td->td_proc->
p_pid, (long)td->td_tid
, args->secs);
200#endif
201 secs = args->secs;
202 /*
203 * Linux alarm() is always successful. Limit secs to INT32_MAX / 2
204 * to match kern_setitimer()'s limit to avoid error from it.
205 *
206 * XXX. Linux limit secs to INT_MAX on 32 and does not limit on 64-bit
207 * platforms.
208 */
209 if (secs > INT32_MAX0x7fffffff / 2)
210 secs = INT32_MAX0x7fffffff / 2;
211
212 it.it_value.tv_sec = secs;
213 it.it_value.tv_usec = 0;
214 timevalclear(&it.it_interval)((&it.it_interval)->tv_sec = (&it.it_interval)->
tv_usec = 0)
;
215 error = kern_setitimer(td, ITIMER_REAL0, &it, &old_it);
216 KASSERT(error == 0, ("kern_setitimer returns %d", error))do { } while (0);
217
218 if ((old_it.it_value.tv_sec == 0 && old_it.it_value.tv_usec > 0) ||
219 old_it.it_value.tv_usec >= 500000)
220 old_it.it_value.tv_sec++;
221 td->td_retvaltd_uretoff.tdu_retval[0] = old_it.it_value.tv_sec;
222 return (0);
223}
224
225int
226linux_brk(struct thread *td, struct linux_brk_args *args)
227{
228 struct vmspace *vm = td->td_proc->p_vmspace;
229 vm_offset_t new, old;
230 struct obreak_args /* {
231 char * nsize;
232 } */ tmp;
233
234#ifdef DEBUG
235 if (ldebug(brk)((((const unsigned char *)(linux_debug_map))[(12)/8] & (1
<<((12)%8))) == 0)
)
236 printf(ARGS(brk, "%p")"linux(%ld/%ld): ""brk""(""%p"")\n", (long)td->td_proc->
p_pid, (long)td->td_tid
, (void *)(uintptr_t)args->dsend);
237#endif
238 old = (vm_offset_t)vm->vm_daddr + ctob(vm->vm_dsize)((vm->vm_dsize)<<12);
239 new = (vm_offset_t)args->dsend;
240 tmp.nsize = (char *)new;
241 if (((caddr_t)new > vm->vm_daddr) && !sys_obreak(td, &tmp))
242 td->td_retvaltd_uretoff.tdu_retval[0] = (long)new;
243 else
244 td->td_retvaltd_uretoff.tdu_retval[0] = (long)old;
245
246 return (0);
247}
248
249#if defined(__i386__)
250/* XXX: what about amd64/linux32? */
251
252int
253linux_uselib(struct thread *td, struct linux_uselib_args *args)
254{
255 struct nameidata ni;
256 struct vnode *vp;
257 struct exec *a_out;
258 struct vattr attr;
259 vm_offset_t vmaddr;
260 unsigned long file_offset;
261 unsigned long bss_size;
262 char *library;
263 ssize_t aresid;
264 int error, locked, writecount;
265
266 LCONVPATHEXIST(td, args->library, &library)do { int _error; _error = linux_emul_convpath(td, args->library
, UIO_USERSPACE, &library, 0, -100); if (*(&library) ==
((void *)0)) return (_error); } while (0)
;
267
268#ifdef DEBUG
269 if (ldebug(uselib)((((const unsigned char *)(linux_debug_map))[(LINUX_SYS_linux_uselib
)/8] & (1<<((LINUX_SYS_linux_uselib)%8))) == 0)
)
270 printf(ARGS(uselib, "%s")"linux(%ld/%ld): ""uselib""(""%s"")\n", (long)td->td_proc->
p_pid, (long)td->td_tid
, library);
271#endif
272
273 a_out = NULL((void *)0);
274 locked = 0;
275 vp = NULL((void *)0);
276
277 NDINIT(&ni, LOOKUP, ISOPEN | FOLLOW | LOCKLEAF | AUDITVNODE1,NDINIT_ALL(&ni, 0, 0x00200000 | 0x0040 | 0x0004 | 0x04000000
, UIO_SYSSPACE, library, -100, ((void *)0), 0, td)
278 UIO_SYSSPACE, library, td)NDINIT_ALL(&ni, 0, 0x00200000 | 0x0040 | 0x0004 | 0x04000000
, UIO_SYSSPACE, library, -100, ((void *)0), 0, td)
;
279 error = namei(&ni);
280 LFREEPATH(library)free(library, M_TEMP);
281 if (error)
282 goto cleanup;
283
284 vp = ni.ni_vp;
285 NDFREE(&ni, NDF_ONLY_PNBUF(~0x00000020));
286
287 /*
288 * From here on down, we have a locked vnode that must be unlocked.
289 * XXX: The code below largely duplicates exec_check_permissions().
290 */
291 locked = 1;
292
293 /* Writable? */
294 error = VOP_GET_WRITECOUNT(vp, &writecount);
295 if (error != 0)
296 goto cleanup;
297 if (writecount != 0) {
298 error = ETXTBSY26;
299 goto cleanup;
300 }
301
302 /* Executable? */
303 error = VOP_GETATTR(vp, &attr, td->td_ucred);
304 if (error)
305 goto cleanup;
306
307 if ((vp->v_mount->mnt_flag & MNT_NOEXEC0x0000000000000004ULL) ||
308 ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
309 /* EACCESS is what exec(2) returns. */
310 error = ENOEXEC8;
311 goto cleanup;
312 }
313
314 /* Sensible size? */
315 if (attr.va_size == 0) {
316 error = ENOEXEC8;
317 goto cleanup;
318 }
319
320 /* Can we access it? */
321 error = VOP_ACCESS(vp, VEXEC000000000100, td->td_ucred, td);
322 if (error)
323 goto cleanup;
324
325 /*
326 * XXX: This should use vn_open() so that it is properly authorized,
327 * and to reduce code redundancy all over the place here.
328 * XXX: Not really, it duplicates far more of exec_check_permissions()
329 * than vn_open().
330 */
331#ifdef MAC1
332 error = mac_vnode_check_open(td->td_ucred, vp, VREAD000000000400);
333 if (error)
334 goto cleanup;
335#endif
336 error = VOP_OPEN(vp, FREAD0x0001, td->td_ucred, td, NULL((void *)0));
337 if (error)
338 goto cleanup;
339
340 /* Pull in executable header into exec_map */
341 error = vm_mmap(exec_map, (vm_offset_t *)&a_out, PAGE_SIZE(1<<12),
342 VM_PROT_READ((vm_prot_t) 0x01), VM_PROT_READ((vm_prot_t) 0x01), 0, OBJT_VNODE, vp, 0);
343 if (error)
344 goto cleanup;
345
346 /* Is it a Linux binary ? */
347 if (((a_out->a_magic >> 16) & 0xff) != 0x64) {
348 error = ENOEXEC8;
349 goto cleanup;
350 }
351
352 /*
353 * While we are here, we should REALLY do some more checks
354 */
355
356 /* Set file/virtual offset based on a.out variant. */
357 switch ((int)(a_out->a_magic & 0xffff)) {
358 case 0413: /* ZMAGIC */
359 file_offset = 1024;
360 break;
361 case 0314: /* QMAGIC */
362 file_offset = 0;
363 break;
364 default:
365 error = ENOEXEC8;
366 goto cleanup;
367 }
368
369 bss_size = round_page(a_out->a_bss)((((unsigned long)(a_out->a_bss)) + ((1<<12)-1)) &
~(((1<<12)-1)))
;
370
371 /* Check various fields in header for validity/bounds. */
372 if (a_out->a_text & PAGE_MASK((1<<12)-1) || a_out->a_data & PAGE_MASK((1<<12)-1)) {
373 error = ENOEXEC8;
374 goto cleanup;
375 }
376
377 /* text + data can't exceed file size */
378 if (a_out->a_data + a_out->a_text > attr.va_size) {
379 error = EFAULT14;
380 goto cleanup;
381 }
382
383 /*
384 * text/data/bss must not exceed limits
385 * XXX - this is not complete. it should check current usage PLUS
386 * the resources needed by this library.
387 */
388 PROC_LOCK(td->td_proc)__mtx_lock_flags(&((((&(td->td_proc)->p_mtx))))
->mtx_lock, ((0)), ("/usr/src/sys/modules/linux64/../../compat/linux/linux_misc.c"
), (388))
;
389 if (a_out->a_text > maxtsiz ||
390 a_out->a_data + bss_size > lim_cur_proc(td->td_proc, RLIMIT_DATA2) ||
391 racct_set(td->td_proc, RACCT_DATA1, a_out->a_data +
392 bss_size) != 0) {
393 PROC_UNLOCK(td->td_proc)__mtx_unlock_flags(&((((&(td->td_proc)->p_mtx))
))->mtx_lock, ((0)), ("/usr/src/sys/modules/linux64/../../compat/linux/linux_misc.c"
), (393))
;
394 error = ENOMEM12;
395 goto cleanup;
396 }
397 PROC_UNLOCK(td->td_proc)__mtx_unlock_flags(&((((&(td->td_proc)->p_mtx))
))->mtx_lock, ((0)), ("/usr/src/sys/modules/linux64/../../compat/linux/linux_misc.c"
), (397))
;
398
399 /*
400 * Prevent more writers.
401 * XXX: Note that if any of the VM operations fail below we don't
402 * clear this flag.
403 */
404 VOP_SET_TEXT(vp);
405
406 /*
407 * Lock no longer needed
408 */
409 locked = 0;
410 VOP_UNLOCK(vp, 0);
411
412 /*
413 * Check if file_offset page aligned. Currently we cannot handle
414 * misalinged file offsets, and so we read in the entire image
415 * (what a waste).
416 */
417 if (file_offset & PAGE_MASK((1<<12)-1)) {
418#ifdef DEBUG
419 printf("uselib: Non page aligned binary %lu\n", file_offset);
420#endif
421 /* Map text+data read/write/execute */
422
423 /* a_entry is the load address and is page aligned */
424 vmaddr = trunc_page(a_out->a_entry)((unsigned long)(a_out->a_entry) & ~(((1<<12)-1)
))
;
425
426 /* get anon user mapping, read+write+execute */
427 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL((void *)0), 0,
428 &vmaddr, a_out->a_text + a_out->a_data, 0, VMFS_NO_SPACE0,
429 VM_PROT_ALL(((vm_prot_t) 0x01)|((vm_prot_t) 0x02)|((vm_prot_t) 0x04)), VM_PROT_ALL(((vm_prot_t) 0x01)|((vm_prot_t) 0x02)|((vm_prot_t) 0x04)), 0);
430 if (error)
431 goto cleanup;
432
433 error = vn_rdwr(UIO_READ, vp, (void *)vmaddr, file_offset,
434 a_out->a_text + a_out->a_data, UIO_USERSPACE, 0,
435 td->td_ucred, NOCRED((struct ucred *)0), &aresid, td);
436 if (error != 0)
437 goto cleanup;
438 if (aresid != 0) {
439 error = ENOEXEC8;
440 goto cleanup;
441 }
442 } else {
443#ifdef DEBUG
444 printf("uselib: Page aligned binary %lu\n", file_offset);
445#endif
446 /*
447 * for QMAGIC, a_entry is 20 bytes beyond the load address
448 * to skip the executable header
449 */
450 vmaddr = trunc_page(a_out->a_entry)((unsigned long)(a_out->a_entry) & ~(((1<<12)-1)
))
;
451
452 /*
453 * Map it all into the process's space as a single
454 * copy-on-write "data" segment.
455 */
456 error = vm_mmap(&td->td_proc->p_vmspace->vm_map, &vmaddr,
457 a_out->a_text + a_out->a_data, VM_PROT_ALL(((vm_prot_t) 0x01)|((vm_prot_t) 0x02)|((vm_prot_t) 0x04)), VM_PROT_ALL(((vm_prot_t) 0x01)|((vm_prot_t) 0x02)|((vm_prot_t) 0x04)),
458 MAP_PRIVATE0x0002 | MAP_FIXED0x0010, OBJT_VNODE, vp, file_offset);
459 if (error)
460 goto cleanup;
461 }
462#ifdef DEBUG
463 printf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long *)vmaddr)[0],
464 ((long *)vmaddr)[1]);
465#endif
466 if (bss_size != 0) {
467 /* Calculate BSS start address */
468 vmaddr = trunc_page(a_out->a_entry)((unsigned long)(a_out->a_entry) & ~(((1<<12)-1)
))
+ a_out->a_text +
469 a_out->a_data;
470
471 /* allocate some 'anon' space */
472 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL((void *)0), 0,
473 &vmaddr, bss_size, 0, VMFS_NO_SPACE0, VM_PROT_ALL(((vm_prot_t) 0x01)|((vm_prot_t) 0x02)|((vm_prot_t) 0x04)),
474 VM_PROT_ALL(((vm_prot_t) 0x01)|((vm_prot_t) 0x02)|((vm_prot_t) 0x04)), 0);
475 if (error)
476 goto cleanup;
477 }
478
479cleanup:
480 /* Unlock vnode if needed */
481 if (locked)
482 VOP_UNLOCK(vp, 0);
483
484 /* Release the temporary mapping. */
485 if (a_out)
486 kmap_free_wakeup(exec_map, (vm_offset_t)a_out, PAGE_SIZE(1<<12));
487
488 return (error);
489}
490
491#endif /* __i386__ */
492
493int
494linux_select(struct thread *td, struct linux_select_args *args)
495{
496 l_timeval ltv;
497 struct timeval tv0, tv1, utv, *tvp;
498 int error;
499
500#ifdef DEBUG
501 if (ldebug(select)((((const unsigned char *)(linux_debug_map))[(23)/8] & (1
<<((23)%8))) == 0)
)
502 printf(ARGS(select, "%d, %p, %p, %p, %p")"linux(%ld/%ld): ""select""(""%d, %p, %p, %p, %p"")\n", (long
)td->td_proc->p_pid, (long)td->td_tid
, args->nfds,
503 (void *)args->readfds, (void *)args->writefds,
504 (void *)args->exceptfds, (void *)args->timeout);
505#endif
506
507 /*
508 * Store current time for computation of the amount of
509 * time left.
510 */
511 if (args->timeout) {
512 if ((error = copyin(args->timeout, &ltv, sizeof(ltv))))
513 goto select_out;
514 utv.tv_sec = ltv.tv_sec;
515 utv.tv_usec = ltv.tv_usec;
516#ifdef DEBUG
517 if (ldebug(select)((((const unsigned char *)(linux_debug_map))[(23)/8] & (1
<<((23)%8))) == 0)
)
518 printf(LMSG("incoming timeout (%jd/%ld)")"linux(%ld/%ld): ""incoming timeout (%jd/%ld)""\n", (long)td->
td_proc->p_pid, (long)td->td_tid
,
519 (intmax_t)utv.tv_sec, utv.tv_usec);
520#endif
521
522 if (itimerfix(&utv)) {
523 /*
524 * The timeval was invalid. Convert it to something
525 * valid that will act as it does under Linux.
526 */
527 utv.tv_sec += utv.tv_usec / 1000000;
528 utv.tv_usec %= 1000000;
529 if (utv.tv_usec < 0) {
530 utv.tv_sec -= 1;
531 utv.tv_usec += 1000000;
532 }
533 if (utv.tv_sec < 0)
534 timevalclear(&utv)((&utv)->tv_sec = (&utv)->tv_usec = 0);
535 }
536 microtime(&tv0);
537 tvp = &utv;
538 } else
539 tvp = NULL((void *)0);
540
541 error = kern_select(td, args->nfds, args->readfds, args->writefds,
542 args->exceptfds, tvp, LINUX_NFDBITSsizeof(l_fd_mask) * 8);
543
544#ifdef DEBUG
545 if (ldebug(select)((((const unsigned char *)(linux_debug_map))[(23)/8] & (1
<<((23)%8))) == 0)
)
546 printf(LMSG("real select returns %d")"linux(%ld/%ld): ""real select returns %d""\n", (long)td->
td_proc->p_pid, (long)td->td_tid
, error);
547#endif
548 if (error)
549 goto select_out;
550
551 if (args->timeout) {
552 if (td->td_retvaltd_uretoff.tdu_retval[0]) {
553 /*
554 * Compute how much time was left of the timeout,
555 * by subtracting the current time and the time
556 * before we started the call, and subtracting
557 * that result from the user-supplied value.
558 */
559 microtime(&tv1);
560 timevalsub(&tv1, &tv0);
561 timevalsub(&utv, &tv1);
562 if (utv.tv_sec < 0)
563 timevalclear(&utv)((&utv)->tv_sec = (&utv)->tv_usec = 0);
564 } else
565 timevalclear(&utv)((&utv)->tv_sec = (&utv)->tv_usec = 0);
566#ifdef DEBUG
567 if (ldebug(select)((((const unsigned char *)(linux_debug_map))[(23)/8] & (1
<<((23)%8))) == 0)
)
568 printf(LMSG("outgoing timeout (%jd/%ld)")"linux(%ld/%ld): ""outgoing timeout (%jd/%ld)""\n", (long)td->
td_proc->p_pid, (long)td->td_tid
,
569 (intmax_t)utv.tv_sec, utv.tv_usec);
570#endif
571 ltv.tv_sec = utv.tv_sec;
572 ltv.tv_usec = utv.tv_usec;
573 if ((error = copyout(&ltv, args->timeout, sizeof(ltv))))
574 goto select_out;
575 }
576
577select_out:
578#ifdef DEBUG
579 if (ldebug(select)((((const unsigned char *)(linux_debug_map))[(23)/8] & (1
<<((23)%8))) == 0)
)
580 printf(LMSG("select_out -> %d")"linux(%ld/%ld): ""select_out -> %d""\n", (long)td->td_proc
->p_pid, (long)td->td_tid
, error);
581#endif
582 return (error);
583}
584
585int
586linux_mremap(struct thread *td, struct linux_mremap_args *args)
587{
588 struct munmap_args /* {
589 void *addr;
590 size_t len;
591 } */ bsd_args;
592 int error = 0;
593
594#ifdef DEBUG
595 if (ldebug(mremap)((((const unsigned char *)(linux_debug_map))[(25)/8] & (1
<<((25)%8))) == 0)
)
596 printf(ARGS(mremap, "%p, %08lx, %08lx, %08lx")"linux(%ld/%ld): ""mremap""(""%p, %08lx, %08lx, %08lx"")\n", (
long)td->td_proc->p_pid, (long)td->td_tid
,
597 (void *)(uintptr_t)args->addr,
598 (unsigned long)args->old_len,
599 (unsigned long)args->new_len,
600 (unsigned long)args->flags);
601#endif
602
603 if (args->flags & ~(LINUX_MREMAP_FIXED2 | LINUX_MREMAP_MAYMOVE1)) {
604 td->td_retvaltd_uretoff.tdu_retval[0] = 0;
605 return (EINVAL22);
606 }
607
608 /*
609 * Check for the page alignment.
610 * Linux defines PAGE_MASK to be FreeBSD ~PAGE_MASK.
611 */
612 if (args->addr & PAGE_MASK((1<<12)-1)) {
613 td->td_retvaltd_uretoff.tdu_retval[0] = 0;
614 return (EINVAL22);
615 }
616
617 args->new_len = round_page(args->new_len)((((unsigned long)(args->new_len)) + ((1<<12)-1)) &
~(((1<<12)-1)))
;
618 args->old_len = round_page(args->old_len)((((unsigned long)(args->old_len)) + ((1<<12)-1)) &
~(((1<<12)-1)))
;
619
620 if (args->new_len > args->old_len) {
621 td->td_retvaltd_uretoff.tdu_retval[0] = 0;
622 return (ENOMEM12);
623 }
624
625 if (args->new_len < args->old_len) {
626 bsd_args.addr =
627 (caddr_t)((uintptr_t)args->addr + args->new_len);
628 bsd_args.len = args->old_len - args->new_len;
629 error = sys_munmap(td, &bsd_args);
630 }
631
632 td->td_retvaltd_uretoff.tdu_retval[0] = error ? 0 : (uintptr_t)args->addr;
633 return (error);
634}
635
636#define LINUX_MS_ASYNC0x0001 0x0001
637#define LINUX_MS_INVALIDATE0x0002 0x0002
638#define LINUX_MS_SYNC0x0004 0x0004
639
640int
641linux_msync(struct thread *td, struct linux_msync_args *args)
642{
643 struct msync_args bsd_args;
644
645 bsd_args.addr = (caddr_t)(uintptr_t)args->addr;
646 bsd_args.len = (uintptr_t)args->len;
647 bsd_args.flags = args->fl & ~LINUX_MS_SYNC0x0004;
648
649 return (sys_msync(td, &bsd_args));
650}
651
652int
653linux_time(struct thread *td, struct linux_time_args *args)
654{
655 struct timeval tv;
656 l_time_t tm;
657 int error;
658
659#ifdef DEBUG
660 if (ldebug(time)((((const unsigned char *)(linux_debug_map))[(201)/8] & (
1<<((201)%8))) == 0)
)
661 printf(ARGS(time, "*")"linux(%ld/%ld): ""time""(""*"")\n", (long)td->td_proc->
p_pid, (long)td->td_tid
);
662#endif
663
664 microtime(&tv);
665 tm = tv.tv_sec;
666 if (args->tm && (error = copyout(&tm, args->tm, sizeof(tm))))
667 return (error);
668 td->td_retvaltd_uretoff.tdu_retval[0] = tm;
669 return (0);
670}
671
672struct l_times_argv {
673 l_clock_t tms_utime;
674 l_clock_t tms_stime;
675 l_clock_t tms_cutime;
676 l_clock_t tms_cstime;
677};
678
679
680/*
681 * Glibc versions prior to 2.2.1 always use hard-coded CLK_TCK value.
682 * Since 2.2.1 Glibc uses value exported from kernel via AT_CLKTCK
683 * auxiliary vector entry.
684 */
685#define CLK_TCK100 100
686
687#define CONVOTCK(r)(r.tv_sec * 100 + r.tv_usec / (1000000 / 100)) (r.tv_sec * CLK_TCK100 + r.tv_usec / (1000000 / CLK_TCK100))
688#define CONVNTCK(r)(r.tv_sec * stclohz + r.tv_usec / (1000000 / stclohz)) (r.tv_sec * stclohz + r.tv_usec / (1000000 / stclohz))
689
690#define CONVTCK(r)(linux_kernver(td) >= (((2) << 16) + ((4) << 8
) + (0)) ? (r.tv_sec * stclohz + r.tv_usec / (1000000 / stclohz
)) : (r.tv_sec * 100 + r.tv_usec / (1000000 / 100)))
(linux_kernver(td) >= LINUX_KERNVER_2004000(((2) << 16) + ((4) << 8) + (0)) ? \
691 CONVNTCK(r)(r.tv_sec * stclohz + r.tv_usec / (1000000 / stclohz)) : CONVOTCK(r)(r.tv_sec * 100 + r.tv_usec / (1000000 / 100)))
692
693int
694linux_times(struct thread *td, struct linux_times_args *args)
695{
696 struct timeval tv, utime, stime, cutime, cstime;
697 struct l_times_argv tms;
698 struct proc *p;
699 int error;
700
701#ifdef DEBUG
702 if (ldebug(times)((((const unsigned char *)(linux_debug_map))[(100)/8] & (
1<<((100)%8))) == 0)
)
703 printf(ARGS(times, "*")"linux(%ld/%ld): ""times""(""*"")\n", (long)td->td_proc->
p_pid, (long)td->td_tid
);
704#endif
705
706 if (args->buf != NULL((void *)0)) {
707 p = td->td_proc;
708 PROC_LOCK(p)__mtx_lock_flags(&((((&(p)->p_mtx))))->mtx_lock
, ((0)), ("/usr/src/sys/modules/linux64/../../compat/linux/linux_misc.c"
), (708))
;
709 PROC_STATLOCK(p)__mtx_lock_spin_flags(&((((&(p)->p_statmtx))))->
mtx_lock, ((0)), ("/usr/src/sys/modules/linux64/../../compat/linux/linux_misc.c"
), (709))
;
710 calcru(p, &utime, &stime);
711 PROC_STATUNLOCK(p)__mtx_unlock_spin_flags(&((((&(p)->p_statmtx))))->
mtx_lock, ((0)), ("/usr/src/sys/modules/linux64/../../compat/linux/linux_misc.c"
), (711))
;
712 calccru(p, &cutime, &cstime);
713 PROC_UNLOCK(p)__mtx_unlock_flags(&((((&(p)->p_mtx))))->mtx_lock
, ((0)), ("/usr/src/sys/modules/linux64/../../compat/linux/linux_misc.c"
), (713))
;
714
715 tms.tms_utime = CONVTCK(utime)(linux_kernver(td) >= (((2) << 16) + ((4) << 8
) + (0)) ? (utime.tv_sec * stclohz + utime.tv_usec / (1000000
/ stclohz)) : (utime.tv_sec * 100 + utime.tv_usec / (1000000
/ 100)))
;
716 tms.tms_stime = CONVTCK(stime)(linux_kernver(td) >= (((2) << 16) + ((4) << 8
) + (0)) ? (stime.tv_sec * stclohz + stime.tv_usec / (1000000
/ stclohz)) : (stime.tv_sec * 100 + stime.tv_usec / (1000000
/ 100)))
;
717
718 tms.tms_cutime = CONVTCK(cutime)(linux_kernver(td) >= (((2) << 16) + ((4) << 8
) + (0)) ? (cutime.tv_sec * stclohz + cutime.tv_usec / (1000000
/ stclohz)) : (cutime.tv_sec * 100 + cutime.tv_usec / (1000000
/ 100)))
;
719 tms.tms_cstime = CONVTCK(cstime)(linux_kernver(td) >= (((2) << 16) + ((4) << 8
) + (0)) ? (cstime.tv_sec * stclohz + cstime.tv_usec / (1000000
/ stclohz)) : (cstime.tv_sec * 100 + cstime.tv_usec / (1000000
/ 100)))
;
720
721 if ((error = copyout(&tms, args->buf, sizeof(tms))))
722 return (error);
723 }
724
725 microuptime(&tv);
726 td->td_retvaltd_uretoff.tdu_retval[0] = (int)CONVTCK(tv)(linux_kernver(td) >= (((2) << 16) + ((4) << 8
) + (0)) ? (tv.tv_sec * stclohz + tv.tv_usec / (1000000 / stclohz
)) : (tv.tv_sec * 100 + tv.tv_usec / (1000000 / 100)))
;
727 return (0);
728}
729
730int
731linux_newuname(struct thread *td, struct linux_newuname_args *args)
732{
733 struct l_new_utsname utsname;
734 char osname[LINUX_MAX_UTSNAME65];
735 char osrelease[LINUX_MAX_UTSNAME65];
736 char *p;
737
738#ifdef DEBUG
739 if (ldebug(newuname)((((const unsigned char *)(linux_debug_map))[(63)/8] & (1
<<((63)%8))) == 0)
)
740 printf(ARGS(newuname, "*")"linux(%ld/%ld): ""newuname""(""*"")\n", (long)td->td_proc
->p_pid, (long)td->td_tid
);
741#endif
742
743 linux_get_osname(td, osname);
744 linux_get_osrelease(td, osrelease);
745
746 bzero(&utsname, sizeof(utsname));
747 strlcpy(utsname.sysname, osname, LINUX_MAX_UTSNAME65);
748 getcredhostname(td->td_ucred, utsname.nodename, LINUX_MAX_UTSNAME65);
749 getcreddomainname(td->td_ucred, utsname.domainname, LINUX_MAX_UTSNAME65);
750 strlcpy(utsname.release, osrelease, LINUX_MAX_UTSNAME65);
751 strlcpy(utsname.version, version, LINUX_MAX_UTSNAME65);
752 for (p = utsname.version; *p != '\0'; ++p)
753 if (*p == '\n') {
754 *p = '\0';
755 break;
756 }
757 strlcpy(utsname.machine, linux_kplatform, LINUX_MAX_UTSNAME65);
758
759 return (copyout(&utsname, args->buf, sizeof(utsname)));
760}
761
762struct l_utimbuf {
763 l_time_t l_actime;
764 l_time_t l_modtime;
765};
766
767int
768linux_utime(struct thread *td, struct linux_utime_args *args)
769{
770 struct timeval tv[2], *tvp;
771 struct l_utimbuf lut;
772 char *fname;
773 int error;
774
775 LCONVPATHEXIST(td, args->fname, &fname)do { int _error; _error = linux_emul_convpath(td, args->fname
, UIO_USERSPACE, &fname, 0, -100); if (*(&fname) == (
(void *)0)) return (_error); } while (0)
;
776
777#ifdef DEBUG
778 if (ldebug(utime)((((const unsigned char *)(linux_debug_map))[(132)/8] & (
1<<((132)%8))) == 0)
)
779 printf(ARGS(utime, "%s, *")"linux(%ld/%ld): ""utime""(""%s, *"")\n", (long)td->td_proc
->p_pid, (long)td->td_tid
, fname);
780#endif
781
782 if (args->times) {
783 if ((error = copyin(args->times, &lut, sizeof lut))) {
784 LFREEPATH(fname)free(fname, M_TEMP);
785 return (error);
786 }
787 tv[0].tv_sec = lut.l_actime;
788 tv[0].tv_usec = 0;
789 tv[1].tv_sec = lut.l_modtime;
790 tv[1].tv_usec = 0;
791 tvp = tv;
792 } else
793 tvp = NULL((void *)0);
794
795 error = kern_utimesat(td, AT_FDCWD-100, fname, UIO_SYSSPACE, tvp,
796 UIO_SYSSPACE);
797 LFREEPATH(fname)free(fname, M_TEMP);
798 return (error);
799}
800
801int
802linux_utimes(struct thread *td, struct linux_utimes_args *args)
803{
804 l_timeval ltv[2];
805 struct timeval tv[2], *tvp = NULL((void *)0);
806 char *fname;
807 int error;
808
809 LCONVPATHEXIST(td, args->fname, &fname)do { int _error; _error = linux_emul_convpath(td, args->fname
, UIO_USERSPACE, &fname, 0, -100); if (*(&fname) == (
(void *)0)) return (_error); } while (0)
;
810
811#ifdef DEBUG
812 if (ldebug(utimes)((((const unsigned char *)(linux_debug_map))[(235)/8] & (
1<<((235)%8))) == 0)
)
813 printf(ARGS(utimes, "%s, *")"linux(%ld/%ld): ""utimes""(""%s, *"")\n", (long)td->td_proc
->p_pid, (long)td->td_tid
, fname);
814#endif
815
816 if (args->tptr != NULL((void *)0)) {
817 if ((error = copyin(args->tptr, ltv, sizeof ltv))) {
818 LFREEPATH(fname)free(fname, M_TEMP);
819 return (error);
820 }
821 tv[0].tv_sec = ltv[0].tv_sec;
822 tv[0].tv_usec = ltv[0].tv_usec;
823 tv[1].tv_sec = ltv[1].tv_sec;
824 tv[1].tv_usec = ltv[1].tv_usec;
825 tvp = tv;
826 }
827
828 error = kern_utimesat(td, AT_FDCWD-100, fname, UIO_SYSSPACE,
829 tvp, UIO_SYSSPACE);
830 LFREEPATH(fname)free(fname, M_TEMP);
831 return (error);
832}
833
834static int
835linux_utimensat_nsec_valid(l_long nsec)
836{
837
838 if (nsec == LINUX_UTIME_OMIT0x3FFFFFFE || nsec == LINUX_UTIME_NOW0x3FFFFFFF)
839 return (0);
840 if (nsec >= 0 && nsec <= 999999999)
841 return (0);
842 return (1);
843}
844
845int
846linux_utimensat(struct thread *td, struct linux_utimensat_args *args)
847{
848 struct l_timespec l_times[2];
849 struct timespec times[2], *timesp = NULL((void *)0);
850 char *path = NULL((void *)0);
851 int error, dfd, flags = 0;
852
853 dfd = (args->dfd == LINUX_AT_FDCWD-100) ? AT_FDCWD-100 : args->dfd;
854
855#ifdef DEBUG
856 if (ldebug(utimensat)((((const unsigned char *)(linux_debug_map))[(280)/8] & (
1<<((280)%8))) == 0)
)
857 printf(ARGS(utimensat, "%d, *")"linux(%ld/%ld): ""utimensat""(""%d, *"")\n", (long)td->td_proc
->p_pid, (long)td->td_tid
, dfd);
858#endif
859
860 if (args->flags & ~LINUX_AT_SYMLINK_NOFOLLOW0x100)
861 return (EINVAL22);
862
863 if (args->times != NULL((void *)0)) {
864 error = copyin(args->times, l_times, sizeof(l_times));
865 if (error != 0)
866 return (error);
867
868 if (linux_utimensat_nsec_valid(l_times[0].tv_nsec) != 0 ||
869 linux_utimensat_nsec_valid(l_times[1].tv_nsec) != 0)
870 return (EINVAL22);
871
872 times[0].tv_sec = l_times[0].tv_sec;
873 switch (l_times[0].tv_nsec)
874 {
875 case LINUX_UTIME_OMIT0x3FFFFFFE:
876 times[0].tv_nsec = UTIME_OMIT-2;
877 break;
878 case LINUX_UTIME_NOW0x3FFFFFFF:
879 times[0].tv_nsec = UTIME_NOW-1;
880 break;
881 default:
882 times[0].tv_nsec = l_times[0].tv_nsec;
883 }
884
885 times[1].tv_sec = l_times[1].tv_sec;
886 switch (l_times[1].tv_nsec)
887 {
888 case LINUX_UTIME_OMIT0x3FFFFFFE:
889 times[1].tv_nsec = UTIME_OMIT-2;
890 break;
891 case LINUX_UTIME_NOW0x3FFFFFFF:
892 times[1].tv_nsec = UTIME_NOW-1;
893 break;
894 default:
895 times[1].tv_nsec = l_times[1].tv_nsec;
896 break;
897 }
898 timesp = times;
899
900 /* This breaks POSIX, but is what the Linux kernel does
901 * _on purpose_ (documented in the man page for utimensat(2)),
902 * so we must follow that behaviour. */
903 if (times[0].tv_nsec == UTIME_OMIT-2 &&
904 times[1].tv_nsec == UTIME_OMIT-2)
905 return (0);
906 }
907
908 if (args->pathname != NULL((void *)0))
909 LCONVPATHEXIST_AT(td, args->pathname, &path, dfd)do { int _error; _error = linux_emul_convpath(td, args->pathname
, UIO_USERSPACE, &path, 0, dfd); if (*(&path) == ((void
*)0)) return (_error); } while (0)
;
910 else if (args->flags != 0)
911 return (EINVAL22);
912
913 if (args->flags & LINUX_AT_SYMLINK_NOFOLLOW0x100)
914 flags |= AT_SYMLINK_NOFOLLOW0x200;
915
916 if (path == NULL((void *)0))
917 error = kern_futimens(td, dfd, timesp, UIO_SYSSPACE);
918 else {
919 error = kern_utimensat(td, dfd, path, UIO_SYSSPACE, timesp,
920 UIO_SYSSPACE, flags);
921 LFREEPATH(path)free(path, M_TEMP);
922 }
923
924 return (error);
925}
926
927int
928linux_futimesat(struct thread *td, struct linux_futimesat_args *args)
929{
930 l_timeval ltv[2];
931 struct timeval tv[2], *tvp = NULL((void *)0);
932 char *fname;
933 int error, dfd;
934
935 dfd = (args->dfd == LINUX_AT_FDCWD-100) ? AT_FDCWD-100 : args->dfd;
936 LCONVPATHEXIST_AT(td, args->filename, &fname, dfd)do { int _error; _error = linux_emul_convpath(td, args->filename
, UIO_USERSPACE, &fname, 0, dfd); if (*(&fname) == ((
void *)0)) return (_error); } while (0)
;
937
938#ifdef DEBUG
939 if (ldebug(futimesat)((((const unsigned char *)(linux_debug_map))[(261)/8] & (
1<<((261)%8))) == 0)
)
940 printf(ARGS(futimesat, "%s, *")"linux(%ld/%ld): ""futimesat""(""%s, *"")\n", (long)td->td_proc
->p_pid, (long)td->td_tid
, fname);
941#endif
942
943 if (args->utimes != NULL((void *)0)) {
944 if ((error = copyin(args->utimes, ltv, sizeof ltv))) {
945 LFREEPATH(fname)free(fname, M_TEMP);
946 return (error);
947 }
948 tv[0].tv_sec = ltv[0].tv_sec;
949 tv[0].tv_usec = ltv[0].tv_usec;
950 tv[1].tv_sec = ltv[1].tv_sec;
951 tv[1].tv_usec = ltv[1].tv_usec;
952 tvp = tv;
953 }
954
955 error = kern_utimesat(td, dfd, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
956 LFREEPATH(fname)free(fname, M_TEMP);
957 return (error);
958}
959
960int
961linux_common_wait(struct thread *td, int pid, int *status,
962 int options, struct rusage *ru)
963{
964 int error, tmpstat;
965
966 error = kern_wait(td, pid, &tmpstat, options, ru);
967 if (error)
968 return (error);
969
970 if (status) {
971 tmpstat &= 0xffff;
972 if (WIFSIGNALED(tmpstat)(((tmpstat) & 0177) != 0177 && ((tmpstat) & 0177
) != 0 && (tmpstat) != 0x13)
)
973 tmpstat = (tmpstat & 0xffffff80) |
974 bsd_to_linux_signal(WTERMSIG(tmpstat)(((tmpstat) & 0177)));
975 else if (WIFSTOPPED(tmpstat)(((tmpstat) & 0177) == 0177))
976 tmpstat = (tmpstat & 0xffff00ff) |
977 (bsd_to_linux_signal(WSTOPSIG(tmpstat)((tmpstat) >> 8)) << 8);
978 else if (WIFCONTINUED(tmpstat)(tmpstat == 0x13))
979 tmpstat = 0xffff;
980 error = copyout(&tmpstat, status, sizeof(int));
981 }
982
983 return (error);
984}
985
986#if defined(__i386__) || (defined(__amd64__1) && defined(COMPAT_LINUX32))
987int
988linux_waitpid(struct thread *td, struct linux_waitpid_args *args)
989{
990 struct linux_wait4_args wait4_args;
991
992#ifdef DEBUG
993 if (ldebug(waitpid)((((const unsigned char *)(linux_debug_map))[(LINUX_SYS_linux_waitpid
)/8] & (1<<((LINUX_SYS_linux_waitpid)%8))) == 0)
)
994 printf(ARGS(waitpid, "%d, %p, %d")"linux(%ld/%ld): ""waitpid""(""%d, %p, %d"")\n", (long)td->
td_proc->p_pid, (long)td->td_tid
,
995 args->pid, (void *)args->status, args->options);
996#endif
997
998 wait4_args.pid = args->pid;
999 wait4_args.status = args->status;
1000 wait4_args.options = args->options;
1001 wait4_args.rusage = NULL((void *)0);
1002
1003 return (linux_wait4(td, &wait4_args));
1004}
1005#endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1006
1007int
1008linux_wait4(struct thread *td, struct linux_wait4_args *args)
1009{
1010 int error, options;
1011 struct rusage ru, *rup;
1012
1013#ifdef DEBUG
1014 if (ldebug(wait4)((((const unsigned char *)(linux_debug_map))[(61)/8] & (1
<<((61)%8))) == 0)
)
1015 printf(ARGS(wait4, "%d, %p, %d, %p")"linux(%ld/%ld): ""wait4""(""%d, %p, %d, %p"")\n", (long)td->
td_proc->p_pid, (long)td->td_tid
,
1016 args->pid, (void *)args->status, args->options,
1017 (void *)args->rusage);
1018#endif
1019 if (args->options & ~(LINUX_WUNTRACED0x00000002 | LINUX_WNOHANG0x00000001 |
1020 LINUX_WCONTINUED0x00000008 | __WCLONE0x80000000 | __WNOTHREAD0x20000000 | __WALL0x40000000))
1021 return (EINVAL22);
1022
1023 options = WEXITED16;
1024 linux_to_bsd_waitopts(args->options, &options);
1025
1026 if (args->rusage != NULL((void *)0))
1027 rup = &ru;
1028 else
1029 rup = NULL((void *)0);
1030 error = linux_common_wait(td, args->pid, args->status, options, rup);
1031 if (error != 0)
1032 return (error);
1033 if (args->rusage != NULL((void *)0))
1034 error = linux_copyout_rusage(&ru, args->rusage)copyout(&ru, args->rusage, sizeof(*&ru));
1035 return (error);
1036}
1037
1038int
1039linux_waitid(struct thread *td, struct linux_waitid_args *args)
1040{
1041 int status, options, sig;
1042 struct __wrusage wru;
1043 siginfo_t siginfo;
1044 l_siginfo_t lsi;
1045 idtype_t idtype;
1046 struct proc *p;
1047 int error;
1048
1049 options = 0;
1050 linux_to_bsd_waitopts(args->options, &options);
1051
1052 if (options & ~(WNOHANG1 | WNOWAIT8 | WEXITED16 | WUNTRACED2 | WCONTINUED4))
1
Taking false branch
1053 return (EINVAL22);
1054 if (!(options & (WEXITED16 | WUNTRACED2 | WCONTINUED4)))
2
Taking false branch
1055 return (EINVAL22);
1056
1057 switch (args->idtype) {
3
Control jumps to 'case 0:' at line 1058
1058 case LINUX_P_ALL0:
1059 idtype = P_ALL;
1060 break;
4
Execution continues on line 1075
1061 case LINUX_P_PID1:
1062 if (args->id <= 0)
1063 return (EINVAL22);
1064 idtype = P_PID;
1065 break;
1066 case LINUX_P_PGID2:
1067 if (args->id <= 0)
1068 return (EINVAL22);
1069 idtype = P_PGID;
1070 break;
1071 default:
1072 return (EINVAL22);
1073 }
1074
1075 error = kern_wait6(td, idtype, args->id, &status, options,
1076 &wru, &siginfo);
1077 if (error != 0)
5
Assuming 'error' is equal to 0
6
Taking false branch
1078 return (error);
1079 if (args->rusage != NULL((void *)0)) {
7
Taking false branch
1080 error = linux_copyout_rusage(&wru.wru_children,copyout(&wru.wru_children, args->rusage, sizeof(*&
wru.wru_children))
1081 args->rusage)copyout(&wru.wru_children, args->rusage, sizeof(*&
wru.wru_children))
;
1082 if (error != 0)
1083 return (error);
1084 }
1085 if (args->info != NULL((void *)0)) {
8
Taking true branch
1086 p = td->td_proc;
1087 if (td->td_retvaltd_uretoff.tdu_retval[0] == 0)
9
Taking false branch
1088 bzero(&lsi, sizeof(lsi));
1089 else {
1090 sig = bsd_to_linux_signal(siginfo.si_signo);
1091 siginfo_to_lsiginfo(&siginfo, &lsi, sig);
1092 }
1093 error = copyout(&lsi, args->info, sizeof(lsi));
10
Copies out a struct with a union element with different sizes
1094 }
1095 td->td_retvaltd_uretoff.tdu_retval[0] = 0;
1096
1097 return (error);
1098}
1099
1100int
1101linux_mknod(struct thread *td, struct linux_mknod_args *args)
1102{
1103 char *path;
1104 int error;
1105
1106 LCONVPATHCREAT(td, args->path, &path)do { int _error; _error = linux_emul_convpath(td, args->path
, UIO_USERSPACE, &path, 1, -100); if (*(&path) == ((void
*)0)) return (_error); } while (0)
;
1107
1108#ifdef DEBUG
1109 if (ldebug(mknod)((((const unsigned char *)(linux_debug_map))[(133)/8] & (
1<<((133)%8))) == 0)
)
1110 printf(ARGS(mknod, "%s, %d, %ju")"linux(%ld/%ld): ""mknod""(""%s, %d, %ju"")\n", (long)td->
td_proc->p_pid, (long)td->td_tid
, path, args->mode,
1111 (uintmax_t)args->dev);
1112#endif
1113
1114 switch (args->mode & S_IFMT0170000) {
1115 case S_IFIFO0010000:
1116 case S_IFSOCK0140000:
1117 error = kern_mkfifoat(td, AT_FDCWD-100, path, UIO_SYSSPACE,
1118 args->mode);
1119 break;
1120
1121 case S_IFCHR0020000:
1122 case S_IFBLK0060000:
1123 error = kern_mknodat(td, AT_FDCWD-100, path, UIO_SYSSPACE,
1124 args->mode, args->dev);
1125 break;
1126
1127 case S_IFDIR0040000:
1128 error = EPERM1;
1129 break;
1130
1131 case 0:
1132 args->mode |= S_IFREG0100000;
1133 /* FALLTHROUGH */
1134 case S_IFREG0100000:
1135 error = kern_openat(td, AT_FDCWD-100, path, UIO_SYSSPACE,
1136 O_WRONLY0x0001 | O_CREAT0x0200 | O_TRUNC0x0400, args->mode);
1137 if (error == 0)
1138 kern_close(td, td->td_retvaltd_uretoff.tdu_retval[0]);
1139 break;
1140
1141 default:
1142 error = EINVAL22;
1143 break;
1144 }
1145 LFREEPATH(path)free(path, M_TEMP);
1146 return (error);
1147}
1148
1149int
1150linux_mknodat(struct thread *td, struct linux_mknodat_args *args)
1151{
1152 char *path;
1153 int error, dfd;
1154
1155 dfd = (args->dfd == LINUX_AT_FDCWD-100) ? AT_FDCWD-100 : args->dfd;
1156 LCONVPATHCREAT_AT(td, args->filename, &path, dfd)do { int _error; _error = linux_emul_convpath(td, args->filename
, UIO_USERSPACE, &path, 1, dfd); if (*(&path) == ((void
*)0)) return (_error); } while (0)
;
1157
1158#ifdef DEBUG
1159 if (ldebug(mknodat)((((const unsigned char *)(linux_debug_map))[(259)/8] & (
1<<((259)%8))) == 0)
)
1160 printf(ARGS(mknodat, "%s, %d, %d")"linux(%ld/%ld): ""mknodat""(""%s, %d, %d"")\n", (long)td->
td_proc->p_pid, (long)td->td_tid
, path, args->mode, args->dev);
1161#endif
1162
1163 switch (args->mode & S_IFMT0170000) {
1164 case S_IFIFO0010000:
1165 case S_IFSOCK0140000:
1166 error = kern_mkfifoat(td, dfd, path, UIO_SYSSPACE, args->mode);
1167 break;
1168
1169 case S_IFCHR0020000:
1170 case S_IFBLK0060000:
1171 error = kern_mknodat(td, dfd, path, UIO_SYSSPACE, args->mode,
1172 args->dev);
1173 break;
1174
1175 case S_IFDIR0040000:
1176 error = EPERM1;
1177 break;
1178
1179 case 0:
1180 args->mode |= S_IFREG0100000;
1181 /* FALLTHROUGH */
1182 case S_IFREG0100000:
1183 error = kern_openat(td, dfd, path, UIO_SYSSPACE,
1184 O_WRONLY0x0001 | O_CREAT0x0200 | O_TRUNC0x0400, args->mode);
1185 if (error == 0)
1186 kern_close(td, td->td_retvaltd_uretoff.tdu_retval[0]);
1187 break;
1188
1189 default:
1190 error = EINVAL22;
1191 break;
1192 }
1193 LFREEPATH(path)free(path, M_TEMP);
1194 return (error);
1195}
1196
1197/*
1198 * UGH! This is just about the dumbest idea I've ever heard!!
1199 */
1200int
1201linux_personality(struct thread *td, struct linux_personality_args *args)
1202{
1203 struct linux_pemuldata *pem;
1204 struct proc *p = td->td_proc;
1205 uint32_t old;
1206
1207#ifdef DEBUG
1208 if (ldebug(personality)((((const unsigned char *)(linux_debug_map))[(135)/8] & (
1<<((135)%8))) == 0)
)
1209 printf(ARGS(personality, "%u")"linux(%ld/%ld): ""personality""(""%u"")\n", (long)td->td_proc
->p_pid, (long)td->td_tid
, args->per);
1210#endif
1211
1212 PROC_LOCK(p)__mtx_lock_flags(&((((&(p)->p_mtx))))->mtx_lock
, ((0)), ("/usr/src/sys/modules/linux64/../../compat/linux/linux_misc.c"
), (1212))
;
1213 pem = pem_find(p);
1214 old = pem->persona;
1215 if (args->per != 0xffffffff)
1216 pem->persona = args->per;
1217 PROC_UNLOCK(p)__mtx_unlock_flags(&((((&(p)->p_mtx))))->mtx_lock
, ((0)), ("/usr/src/sys/modules/linux64/../../compat/linux/linux_misc.c"
), (1217))
;
1218
1219 td->td_retvaltd_uretoff.tdu_retval[0] = old;
1220 return (0);
1221}
1222
1223struct l_itimerval {
1224 l_timeval it_interval;
1225 l_timeval it_value;
1226};
1227
1228#define B2L_ITIMERVAL(bip, lip)(bip)->it_interval.tv_sec = (lip)->it_interval.tv_sec; (
bip)->it_interval.tv_usec = (lip)->it_interval.tv_usec;
(bip)->it_value.tv_sec = (lip)->it_value.tv_sec; (bip)
->it_value.tv_usec = (lip)->it_value.tv_usec;
\
1229 (bip)->it_interval.tv_sec = (lip)->it_interval.tv_sec; \
1230 (bip)->it_interval.tv_usec = (lip)->it_interval.tv_usec; \
1231 (bip)->it_value.tv_sec = (lip)->it_value.tv_sec; \
1232 (bip)->it_value.tv_usec = (lip)->it_value.tv_usec;
1233
1234int
1235linux_setitimer(struct thread *td, struct linux_setitimer_args *uap)
1236{
1237 int error;
1238 struct l_itimerval ls;
1239 struct itimerval aitv, oitv;
1240
1241#ifdef DEBUG
1242 if (ldebug(setitimer)((((const unsigned char *)(linux_debug_map))[(38)/8] & (1
<<((38)%8))) == 0)
)
1243 printf(ARGS(setitimer, "%p, %p")"linux(%ld/%ld): ""setitimer""(""%p, %p"")\n", (long)td->td_proc
->p_pid, (long)td->td_tid
,
1244 (void *)uap->itv, (void *)uap->oitv);
1245#endif
1246
1247 if (uap->itv == NULL((void *)0)) {
1248 uap->itv = uap->oitv;
1249 return (linux_getitimer(td, (struct linux_getitimer_args *)uap));
1250 }
1251
1252 error = copyin(uap->itv, &ls, sizeof(ls));
1253 if (error != 0)
1254 return (error);
1255 B2L_ITIMERVAL(&aitv, &ls)(&aitv)->it_interval.tv_sec = (&ls)->it_interval
.tv_sec; (&aitv)->it_interval.tv_usec = (&ls)->
it_interval.tv_usec; (&aitv)->it_value.tv_sec = (&
ls)->it_value.tv_sec; (&aitv)->it_value.tv_usec = (
&ls)->it_value.tv_usec;
;
1256#ifdef DEBUG
1257 if (ldebug(setitimer)((((const unsigned char *)(linux_debug_map))[(38)/8] & (1
<<((38)%8))) == 0)
) {
1258 printf("setitimer: value: sec: %jd, usec: %ld\n",
1259 (intmax_t)aitv.it_value.tv_sec, aitv.it_value.tv_usec);
1260 printf("setitimer: interval: sec: %jd, usec: %ld\n",
1261 (intmax_t)aitv.it_interval.tv_sec, aitv.it_interval.tv_usec);
1262 }
1263#endif
1264 error = kern_setitimer(td, uap->which, &aitv, &oitv);
1265 if (error != 0 || uap->oitv == NULL((void *)0))
1266 return (error);
1267 B2L_ITIMERVAL(&ls, &oitv)(&ls)->it_interval.tv_sec = (&oitv)->it_interval
.tv_sec; (&ls)->it_interval.tv_usec = (&oitv)->
it_interval.tv_usec; (&ls)->it_value.tv_sec = (&oitv
)->it_value.tv_sec; (&ls)->it_value.tv_usec = (&
oitv)->it_value.tv_usec;
;
1268
1269 return (copyout(&ls, uap->oitv, sizeof(ls)));
1270}
1271
1272int
1273linux_getitimer(struct thread *td, struct linux_getitimer_args *uap)
1274{
1275 int error;
1276 struct l_itimerval ls;
1277 struct itimerval aitv;
1278
1279#ifdef DEBUG
1280 if (ldebug(getitimer)((((const unsigned char *)(linux_debug_map))[(36)/8] & (1
<<((36)%8))) == 0)
)
1281 printf(ARGS(getitimer, "%p")"linux(%ld/%ld): ""getitimer""(""%p"")\n", (long)td->td_proc
->p_pid, (long)td->td_tid
, (void *)uap->itv);
1282#endif
1283 error = kern_getitimer(td, uap->which, &aitv);
1284 if (error != 0)
1285 return (error);
1286 B2L_ITIMERVAL(&ls, &aitv)(&ls)->it_interval.tv_sec = (&aitv)->it_interval
.tv_sec; (&ls)->it_interval.tv_usec = (&aitv)->
it_interval.tv_usec; (&ls)->it_value.tv_sec = (&aitv
)->it_value.tv_sec; (&ls)->it_value.tv_usec = (&
aitv)->it_value.tv_usec;
;
1287 return (copyout(&ls, uap->itv, sizeof(ls)));
1288}
1289
1290#if defined(__i386__) || (defined(__amd64__1) && defined(COMPAT_LINUX32))
1291int
1292linux_nice(struct thread *td, struct linux_nice_args *args)
1293{
1294 struct setpriority_args bsd_args;
1295
1296 bsd_args.which = PRIO_PROCESS0;
1297 bsd_args.who = 0; /* current process */
1298 bsd_args.prio = args->inc;
1299 return (sys_setpriority(td, &bsd_args));
1300}
1301#endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1302
1303int
1304linux_setgroups(struct thread *td, struct linux_setgroups_args *args)
1305{
1306 struct ucred *newcred, *oldcred;
1307 l_gid_t *linux_gidset;
1308 gid_t *bsd_gidset;
1309 int ngrp, error;
1310 struct proc *p;
1311
1312 ngrp = args->gidsetsize;
1313 if (ngrp < 0 || ngrp >= ngroups_max + 1)
1314 return (EINVAL22);
1315 linux_gidset = malloc(ngrp * sizeof(*linux_gidset), M_LINUX, M_WAITOK0x0002);
1316 error = copyin(args->grouplist, linux_gidset, ngrp * sizeof(l_gid_t));
1317 if (error)
1318 goto out;
1319 newcred = crget();
1320 crextend(newcred, ngrp + 1);
1321 p = td->td_proc;
1322 PROC_LOCK(p)__mtx_lock_flags(&((((&(p)->p_mtx))))->mtx_lock
, ((0)), ("/usr/src/sys/modules/linux64/../../compat/linux/linux_misc.c"
), (1322))
;
1323 oldcred = p->p_ucred;
1324 crcopy(newcred, oldcred);
1325
1326 /*
1327 * cr_groups[0] holds egid. Setting the whole set from
1328 * the supplied set will cause egid to be changed too.
1329 * Keep cr_groups[0] unchanged to prevent that.
1330 */
1331
1332 if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS54, 0)) != 0) {
1333 PROC_UNLOCK(p)__mtx_unlock_flags(&((((&(p)->p_mtx))))->mtx_lock
, ((0)), ("/usr/src/sys/modules/linux64/../../compat/linux/linux_misc.c"
), (1333))
;
1334 crfree(newcred);
1335 goto out;
1336 }
1337
1338 if (ngrp > 0) {
1339 newcred->cr_ngroups = ngrp + 1;
1340
1341 bsd_gidset = newcred->cr_groups;
1342 ngrp--;
1343 while (ngrp >= 0) {
1344 bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
1345 ngrp--;
1346 }
1347 } else
1348 newcred->cr_ngroups = 1;
1349
1350 setsugid(p);
1351 proc_set_cred(p, newcred);
1352 PROC_UNLOCK(p)__mtx_unlock_flags(&((((&(p)->p_mtx))))->mtx_lock
, ((0)), ("/usr/src/sys/modules/linux64/../../compat/linux/linux_misc.c"
), (1352))
;
1353 crfree(oldcred);
1354 error = 0;
1355out:
1356 free(linux_gidset, M_LINUX);
1357 return (error);
1358}
1359
1360int
1361linux_getgroups(struct thread *td, struct linux_getgroups_args *args)
1362{
1363 struct ucred *cred;
1364 l_gid_t *linux_gidset;
1365 gid_t *bsd_gidset;
1366 int bsd_gidsetsz, ngrp, error;
1367
1368 cred = td->td_ucred;
1369 bsd_gidset = cred->cr_groups;
1370 bsd_gidsetsz = cred->cr_ngroups - 1;
1371
1372 /*
1373 * cr_groups[0] holds egid. Returning the whole set
1374 * here will cause a duplicate. Exclude cr_groups[0]
1375 * to prevent that.
1376 */
1377
1378 if ((ngrp = args->gidsetsize) == 0) {
1379 td->td_retvaltd_uretoff.tdu_retval[0] = bsd_gidsetsz;
1380 return (0);
1381 }
1382
1383 if (ngrp < bsd_gidsetsz)
1384 return (EINVAL22);
1385
1386 ngrp = 0;
1387 linux_gidset = malloc(bsd_gidsetsz * sizeof(*linux_gidset),
1388 M_LINUX, M_WAITOK0x0002);
1389 while (ngrp < bsd_gidsetsz) {
1390 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1391 ngrp++;
1392 }
1393
1394 error = copyout(linux_gidset, args->grouplist, ngrp * sizeof(l_gid_t));
1395 free(linux_gidset, M_LINUX);
1396 if (error)
1397 return (error);
1398
1399 td->td_retvaltd_uretoff.tdu_retval[0] = ngrp;
1400 return (0);
1401}
1402
1403int
1404linux_setrlimit(struct thread *td, struct linux_setrlimit_args *args)
1405{
1406 struct rlimit bsd_rlim;
1407 struct l_rlimit rlim;
1408 u_int which;
1409 int error;
1410
1411#ifdef DEBUG
1412 if (ldebug(setrlimit)((((const unsigned char *)(linux_debug_map))[(160)/8] & (
1<<((160)%8))) == 0)
)
1413 printf(ARGS(setrlimit, "%d, %p")"linux(%ld/%ld): ""setrlimit""(""%d, %p"")\n", (long)td->td_proc
->p_pid, (long)td->td_tid
,
1414 args->resource, (void *)args->rlim);
1415#endif
1416
1417 if (args->resource >= LINUX_RLIM_NLIMITS10)
1418 return (EINVAL22);
1419
1420 which = linux_to_bsd_resource[args->resource];
1421 if (which == -1)
1422 return (EINVAL22);
1423
1424 error = copyin(args->rlim, &rlim, sizeof(rlim));
1425 if (error)
1426 return (error);
1427
1428 bsd_rlim.rlim_cur = (rlim_t)rlim.rlim_cur;
1429 bsd_rlim.rlim_max = (rlim_t)rlim.rlim_max;
1430 return (kern_setrlimit(td, which, &bsd_rlim));
1431}
1432
1433#if defined(__i386__) || (defined(__amd64__1) && defined(COMPAT_LINUX32))
1434int
1435linux_old_getrlimit(struct thread *td, struct linux_old_getrlimit_args *args)
1436{
1437 struct l_rlimit rlim;
1438 struct rlimit bsd_rlim;
1439 u_int which;
1440
1441#ifdef DEBUG
1442 if (ldebug(old_getrlimit)((((const unsigned char *)(linux_debug_map))[(LINUX_SYS_linux_old_getrlimit
)/8] & (1<<((LINUX_SYS_linux_old_getrlimit)%8))) ==
0)
)
1443 printf(ARGS(old_getrlimit, "%d, %p")"linux(%ld/%ld): ""old_getrlimit""(""%d, %p"")\n", (long)td->
td_proc->p_pid, (long)td->td_tid
,
1444 args->resource, (void *)args->rlim);
1445#endif
1446
1447 if (args->resource >= LINUX_RLIM_NLIMITS10)
1448 return (EINVAL22);
1449
1450 which = linux_to_bsd_resource[args->resource];
1451 if (which == -1)
1452 return (EINVAL22);
1453
1454 lim_rlimit(td, which, &bsd_rlim);
1455
1456#ifdef COMPAT_LINUX32
1457 rlim.rlim_cur = (unsigned int)bsd_rlim.rlim_cur;
1458 if (rlim.rlim_cur == UINT_MAX0xffffffff)
1459 rlim.rlim_cur = INT_MAX0x7fffffff;
1460 rlim.rlim_max = (unsigned int)bsd_rlim.rlim_max;
1461 if (rlim.rlim_max == UINT_MAX0xffffffff)
1462 rlim.rlim_max = INT_MAX0x7fffffff;
1463#else
1464 rlim.rlim_cur = (unsigned long)bsd_rlim.rlim_cur;
1465 if (rlim.rlim_cur == ULONG_MAX0xffffffffffffffff)
1466 rlim.rlim_cur = LONG_MAX0x7fffffffffffffff;
1467 rlim.rlim_max = (unsigned long)bsd_rlim.rlim_max;
1468 if (rlim.rlim_max == ULONG_MAX0xffffffffffffffff)
1469 rlim.rlim_max = LONG_MAX0x7fffffffffffffff;
1470#endif
1471 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1472}
1473#endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1474
1475int
1476linux_getrlimit(struct thread *td, struct linux_getrlimit_args *args)
1477{
1478 struct l_rlimit rlim;
1479 struct rlimit bsd_rlim;
1480 u_int which;
1481
1482#ifdef DEBUG
1483 if (ldebug(getrlimit)((((const unsigned char *)(linux_debug_map))[(97)/8] & (1
<<((97)%8))) == 0)
)
1484 printf(ARGS(getrlimit, "%d, %p")"linux(%ld/%ld): ""getrlimit""(""%d, %p"")\n", (long)td->td_proc
->p_pid, (long)td->td_tid
,
1485 args->resource, (void *)args->rlim);
1486#endif
1487
1488 if (args->resource >= LINUX_RLIM_NLIMITS10)
1489 return (EINVAL22);
1490
1491 which = linux_to_bsd_resource[args->resource];
1492 if (which == -1)
1493 return (EINVAL22);
1494
1495 lim_rlimit(td, which, &bsd_rlim);
1496
1497 rlim.rlim_cur = (l_ulong)bsd_rlim.rlim_cur;
1498 rlim.rlim_max = (l_ulong)bsd_rlim.rlim_max;
1499 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1500}
1501
1502int
1503linux_sched_setscheduler(struct thread *td,
1504 struct linux_sched_setscheduler_args *args)
1505{
1506 struct sched_param sched_param;
1507 struct thread *tdt;
1508 int error, policy;
1509
1510#ifdef DEBUG
1511 if (ldebug(sched_setscheduler)((((const unsigned char *)(linux_debug_map))[(144)/8] & (
1<<((144)%8))) == 0)
)
1512 printf(ARGS(sched_setscheduler, "%d, %d, %p")"linux(%ld/%ld): ""sched_setscheduler""(""%d, %d, %p"")\n", (
long)td->td_proc->p_pid, (long)td->td_tid
,
1513 args->pid, args->policy, (const void *)args->param);
1514#endif
1515
1516 switch (args->policy) {
1517 case LINUX_SCHED_OTHER0:
1518 policy = SCHED_OTHER2;
1519 break;
1520 case LINUX_SCHED_FIFO1:
1521 policy = SCHED_FIFO1;
1522 break;
1523 case LINUX_SCHED_RR2:
1524 policy = SCHED_RR3;
1525 break;
1526 default:
1527 return (EINVAL22);
1528 }
1529
1530 error = copyin(args->param, &sched_param, sizeof(sched_param));
1531 if (error)
1532 return (error);
1533
1534 tdt = linux_tdfind(td, args->pid, -1);
1535 if (tdt == NULL((void *)0))
1536 return (ESRCH3);
1537
1538 error = kern_sched_setscheduler(td, tdt, policy, &sched_param);
1539 PROC_UNLOCK(tdt->td_proc)__mtx_unlock_flags(&((((&(tdt->td_proc)->p_mtx)
)))->mtx_lock, ((0)), ("/usr/src/sys/modules/linux64/../../compat/linux/linux_misc.c"
), (1539))
;
1540 return (error);
1541}
1542
1543int
1544linux_sched_getscheduler(struct thread *td,
1545 struct linux_sched_getscheduler_args *args)
1546{
1547 struct thread *tdt;
1548 int error, policy;
1549
1550#ifdef DEBUG
1551 if (ldebug(sched_getscheduler)((((const unsigned char *)(linux_debug_map))[(145)/8] & (
1<<((145)%8))) == 0)
)
1552 printf(ARGS(sched_getscheduler, "%d")"linux(%ld/%ld): ""sched_getscheduler""(""%d"")\n", (long)td->
td_proc->p_pid, (long)td->td_tid
, args->pid);
1553#endif
1554
1555 tdt = linux_tdfind(td, args->pid, -1);
1556 if (tdt == NULL((void *)0))
1557 return (ESRCH3);
1558
1559 error = kern_sched_getscheduler(td, tdt, &policy);
1560 PROC_UNLOCK(tdt->td_proc)__mtx_unlock_flags(&((((&(tdt->td_proc)->p_mtx)
)))->mtx_lock, ((0)), ("/usr/src/sys/modules/linux64/../../compat/linux/linux_misc.c"
), (1560))
;
1561
1562 switch (policy) {
1563 case SCHED_OTHER2:
1564 td->td_retvaltd_uretoff.tdu_retval[0] = LINUX_SCHED_OTHER0;
1565 break;
1566 case SCHED_FIFO1:
1567 td->td_retvaltd_uretoff.tdu_retval[0] = LINUX_SCHED_FIFO1;
1568 break;
1569 case SCHED_RR3:
1570 td->td_retvaltd_uretoff.tdu_retval[0] = LINUX_SCHED_RR2;
1571 break;
1572 }
1573 return (error);
1574}
1575
1576int
1577linux_sched_get_priority_max(struct thread *td,
1578 struct linux_sched_get_priority_max_args *args)
1579{
1580 struct sched_get_priority_max_args bsd;
1581
1582#ifdef DEBUG
1583 if (ldebug(sched_get_priority_max)((((const unsigned char *)(linux_debug_map))[(146)/8] & (
1<<((146)%8))) == 0)
)
1584 printf(ARGS(sched_get_priority_max, "%d")"linux(%ld/%ld): ""sched_get_priority_max""(""%d"")\n", (long
)td->td_proc->p_pid, (long)td->td_tid
, args->policy);
1585#endif
1586
1587 switch (args->policy) {
1588 case LINUX_SCHED_OTHER0:
1589 bsd.policy = SCHED_OTHER2;
1590 break;
1591 case LINUX_SCHED_FIFO1:
1592 bsd.policy = SCHED_FIFO1;
1593 break;
1594 case LINUX_SCHED_RR2:
1595 bsd.policy = SCHED_RR3;
1596 break;
1597 default:
1598 return (EINVAL22);
1599 }
1600 return (sys_sched_get_priority_max(td, &bsd));
1601}
1602
1603int
1604linux_sched_get_priority_min(struct thread *td,
1605 struct linux_sched_get_priority_min_args *args)
1606{
1607 struct sched_get_priority_min_args bsd;
1608
1609#ifdef DEBUG
1610 if (ldebug(sched_get_priority_min)((((const unsigned char *)(linux_debug_map))[(147)/8] & (
1<<((147)%8))) == 0)
)
1611 printf(ARGS(sched_get_priority_min, "%d")"linux(%ld/%ld): ""sched_get_priority_min""(""%d"")\n", (long
)td->td_proc->p_pid, (long)td->td_tid
, args->policy);
1612#endif
1613
1614 switch (args->policy) {
1615 case LINUX_SCHED_OTHER0:
1616 bsd.policy = SCHED_OTHER2;
1617 break;
1618 case LINUX_SCHED_FIFO1:
1619 bsd.policy = SCHED_FIFO1;
1620 break;
1621 case LINUX_SCHED_RR2:
1622 bsd.policy = SCHED_RR3;
1623 break;
1624 default:
1625 return (EINVAL22);
1626 }
1627 return (sys_sched_get_priority_min(td, &bsd));
1628}
1629
1630#define REBOOT_CAD_ON0x89abcdef 0x89abcdef
1631#define REBOOT_CAD_OFF0 0
1632#define REBOOT_HALT0xcdef0123 0xcdef0123
1633#define REBOOT_RESTART0x01234567 0x01234567
1634#define REBOOT_RESTART20xA1B2C3D4 0xA1B2C3D4
1635#define REBOOT_POWEROFF0x4321FEDC 0x4321FEDC
1636#define REBOOT_MAGIC10xfee1dead 0xfee1dead
1637#define REBOOT_MAGIC20x28121969 0x28121969
1638#define REBOOT_MAGIC2A0x05121996 0x05121996
1639#define REBOOT_MAGIC2B0x16041998 0x16041998
1640
1641int
1642linux_reboot(struct thread *td, struct linux_reboot_args *args)
1643{
1644 struct reboot_args bsd_args;
1645
1646#ifdef DEBUG
1647 if (ldebug(reboot)((((const unsigned char *)(linux_debug_map))[(169)/8] & (
1<<((169)%8))) == 0)
)
1648 printf(ARGS(reboot, "0x%x")"linux(%ld/%ld): ""reboot""(""0x%x"")\n", (long)td->td_proc
->p_pid, (long)td->td_tid
, args->cmd);
1649#endif
1650
1651 if (args->magic1 != REBOOT_MAGIC10xfee1dead)
1652 return (EINVAL22);
1653
1654 switch (args->magic2) {
1655 case REBOOT_MAGIC20x28121969:
1656 case REBOOT_MAGIC2A0x05121996:
1657 case REBOOT_MAGIC2B0x16041998:
1658 break;
1659 default:
1660 return (EINVAL22);
1661 }
1662
1663 switch (args->cmd) {
1664 case REBOOT_CAD_ON0x89abcdef:
1665 case REBOOT_CAD_OFF0:
1666 return (priv_check(td, PRIV_REBOOT8));
1667 case REBOOT_HALT0xcdef0123:
1668 bsd_args.opt = RB_HALT0x008;
1669 break;
1670 case REBOOT_RESTART0x01234567:
1671 case REBOOT_RESTART20xA1B2C3D4:
1672 bsd_args.opt = 0;
1673 break;
1674 case REBOOT_POWEROFF0x4321FEDC:
1675 bsd_args.opt = RB_POWEROFF0x4000;
1676 break;
1677 default:
1678 return (EINVAL22);
1679 }
1680 return (sys_reboot(td, &bsd_args));
1681}
1682
1683
1684/*
1685 * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
1686 * td->td_retval[1] when COMPAT_43 is defined. This clobbers registers that
1687 * are assumed to be preserved. The following lightweight syscalls fixes
1688 * this. See also linux_getgid16() and linux_getuid16() in linux_uid16.c
1689 *
1690 * linux_getpid() - MP SAFE
1691 * linux_getgid() - MP SAFE
1692 * linux_getuid() - MP SAFE
1693 */
1694
1695int
1696linux_getpid(struct thread *td, struct linux_getpid_args *args)
1697{
1698
1699#ifdef DEBUG
1700 if (ldebug(getpid)((((const unsigned char *)(linux_debug_map))[(39)/8] & (1
<<((39)%8))) == 0)
)
1701 printf(ARGS(getpid, "")"linux(%ld/%ld): ""getpid""("""")\n", (long)td->td_proc->
p_pid, (long)td->td_tid
);
1702#endif
1703 td->td_retvaltd_uretoff.tdu_retval[0] = td->td_proc->p_pid;
1704
1705 return (0);
1706}
1707
1708int
1709linux_gettid(struct thread *td, struct linux_gettid_args *args)
1710{
1711 struct linux_emuldata *em;
1712
1713#ifdef DEBUG
1714 if (ldebug(gettid)((((const unsigned char *)(linux_debug_map))[(186)/8] & (
1<<((186)%8))) == 0)
)
1715 printf(ARGS(gettid, "")"linux(%ld/%ld): ""gettid""("""")\n", (long)td->td_proc->
p_pid, (long)td->td_tid
);
1716#endif
1717
1718 em = em_find(td);
1719 KASSERT(em != NULL, ("gettid: emuldata not found.\n"))do { } while (0);
1720
1721 td->td_retvaltd_uretoff.tdu_retval[0] = em->em_tid;
1722
1723 return (0);
1724}
1725
1726
1727int
1728linux_getppid(struct thread *td, struct linux_getppid_args *args)
1729{
1730
1731#ifdef DEBUG
1732 if (ldebug(getppid)((((const unsigned char *)(linux_debug_map))[(110)/8] & (
1<<((110)%8))) == 0)
)
1733 printf(ARGS(getppid, "")"linux(%ld/%ld): ""getppid""("""")\n", (long)td->td_proc->
p_pid, (long)td->td_tid
);
1734#endif
1735
1736 PROC_LOCK(td->td_proc)__mtx_lock_flags(&((((&(td->td_proc)->p_mtx))))
->mtx_lock, ((0)), ("/usr/src/sys/modules/linux64/../../compat/linux/linux_misc.c"
), (1736))
;
1737 td->td_retvaltd_uretoff.tdu_retval[0] = td->td_proc->p_pptr->p_pid;
1738 PROC_UNLOCK(td->td_proc)__mtx_unlock_flags(&((((&(td->td_proc)->p_mtx))
))->mtx_lock, ((0)), ("/usr/src/sys/modules/linux64/../../compat/linux/linux_misc.c"
), (1738))
;
1739 return (0);
1740}
1741
1742int
1743linux_getgid(struct thread *td, struct linux_getgid_args *args)
1744{
1745
1746#ifdef DEBUG
1747 if (ldebug(getgid)((((const unsigned char *)(linux_debug_map))[(104)/8] & (
1<<((104)%8))) == 0)
)
1748 printf(ARGS(getgid, "")"linux(%ld/%ld): ""getgid""("""")\n", (long)td->td_proc->
p_pid, (long)td->td_tid
);
1749#endif
1750
1751 td->td_retvaltd_uretoff.tdu_retval[0] = td->td_ucred->cr_rgid;
1752 return (0);
1753}
1754
1755int
1756linux_getuid(struct thread *td, struct linux_getuid_args *args)
1757{
1758
1759#ifdef DEBUG
1760 if (ldebug(getuid)((((const unsigned char *)(linux_debug_map))[(102)/8] & (
1<<((102)%8))) == 0)
)
1761 printf(ARGS(getuid, "")"linux(%ld/%ld): ""getuid""("""")\n", (long)td->td_proc->
p_pid, (long)td->td_tid
);
1762#endif
1763
1764 td->td_retvaltd_uretoff.tdu_retval[0] = td->td_ucred->cr_ruid;
1765 return (0);
1766}
1767
1768
1769int
1770linux_getsid(struct thread *td, struct linux_getsid_args *args)
1771{
1772 struct getsid_args bsd;
1773
1774#ifdef DEBUG
1775 if (ldebug(getsid)((((const unsigned char *)(linux_debug_map))[(124)/8] & (
1<<((124)%8))) == 0)
)
1776 printf(ARGS(getsid, "%i")"linux(%ld/%ld): ""getsid""(""%i"")\n", (long)td->td_proc->
p_pid, (long)td->td_tid
, args->pid);
1777#endif
1778
1779 bsd.pid = args->pid;
1780 return (sys_getsid(td, &bsd));
1781}
1782
1783int
1784linux_nosys(struct thread *td, struct nosys_args *ignore)
1785{
1786
1787 return (ENOSYS78);
1788}
1789
1790int
1791linux_getpriority(struct thread *td, struct linux_getpriority_args *args)
1792{
1793 struct getpriority_args bsd_args;
1794 int error;
1795
1796#ifdef DEBUG
1797 if (ldebug(getpriority)((((const unsigned char *)(linux_debug_map))[(140)/8] & (
1<<((140)%8))) == 0)
)
1798 printf(ARGS(getpriority, "%i, %i")"linux(%ld/%ld): ""getpriority""(""%i, %i"")\n", (long)td->
td_proc->p_pid, (long)td->td_tid
, args->which, args->who);
1799#endif
1800
1801 bsd_args.which = args->which;
1802 bsd_args.who = args->who;
1803 error = sys_getpriority(td, &bsd_args);
1804 td->td_retvaltd_uretoff.tdu_retval[0] = 20 - td->td_retvaltd_uretoff.tdu_retval[0];
1805 return (error);
1806}
1807
1808int
1809linux_sethostname(struct thread *td, struct linux_sethostname_args *args)
1810{
1811 int name[2];
1812
1813#ifdef DEBUG
1814 if (ldebug(sethostname)((((const unsigned char *)(linux_debug_map))[(170)/8] & (
1<<((170)%8))) == 0)
)
1815 printf(ARGS(sethostname, "*, %i")"linux(%ld/%ld): ""sethostname""(""*, %i"")\n", (long)td->
td_proc->p_pid, (long)td->td_tid
, args->len);
1816#endif
1817
1818 name[0] = CTL_KERN1;
1819 name[1] = KERN_HOSTNAME10;
1820 return (userland_sysctl(td, name, 2, 0, 0, 0, args->hostname,
1821 args->len, 0, 0));
1822}
1823
1824int
1825linux_setdomainname(struct thread *td, struct linux_setdomainname_args *args)
1826{
1827 int name[2];
1828
1829#ifdef DEBUG
1830 if (ldebug(setdomainname)((((const unsigned char *)(linux_debug_map))[(171)/8] & (
1<<((171)%8))) == 0)
)
1831 printf(ARGS(setdomainname, "*, %i")"linux(%ld/%ld): ""setdomainname""(""*, %i"")\n", (long)td->
td_proc->p_pid, (long)td->td_tid
, args->len);
1832#endif
1833
1834 name[0] = CTL_KERN1;
1835 name[1] = KERN_NISDOMAINNAME22;
1836 return (userland_sysctl(td, name, 2, 0, 0, 0, args->name,
1837 args->len, 0, 0));
1838}
1839
1840int
1841linux_exit_group(struct thread *td, struct linux_exit_group_args *args)
1842{
1843
1844#ifdef DEBUG
1845 if (ldebug(exit_group)((((const unsigned char *)(linux_debug_map))[(231)/8] & (
1<<((231)%8))) == 0)
)
1846 printf(ARGS(exit_group, "%i")"linux(%ld/%ld): ""exit_group""(""%i"")\n", (long)td->td_proc
->p_pid, (long)td->td_tid
, args->error_code);
1847#endif
1848
1849 LINUX_CTR2(exit_group, "thread(%d) (%d)", td->td_tid,
1850 args->error_code);
1851
1852 /*
1853 * XXX: we should send a signal to the parent if
1854 * SIGNAL_EXIT_GROUP is set. We ignore that (temporarily?)
1855 * as it doesnt occur often.
1856 */
1857 exit1(td, args->error_code, 0);
1858 /* NOTREACHED */
1859}
1860
1861#define _LINUX_CAPABILITY_VERSION0x19980330 0x19980330
1862
1863struct l_user_cap_header {
1864 l_int version;
1865 l_int pid;
1866};
1867
1868struct l_user_cap_data {
1869 l_int effective;
1870 l_int permitted;
1871 l_int inheritable;
1872};
1873
1874int
1875linux_capget(struct thread *td, struct linux_capget_args *args)
1876{
1877 struct l_user_cap_header luch;
1878 struct l_user_cap_data lucd;
1879 int error;
1880
1881 if (args->hdrp == NULL((void *)0))
1882 return (EFAULT14);
1883
1884 error = copyin(args->hdrp, &luch, sizeof(luch));
1885 if (error != 0)
1886 return (error);
1887
1888 if (luch.version != _LINUX_CAPABILITY_VERSION0x19980330) {
1889 luch.version = _LINUX_CAPABILITY_VERSION0x19980330;
1890 error = copyout(&luch, args->hdrp, sizeof(luch));
1891 if (error)
1892 return (error);
1893 return (EINVAL22);
1894 }
1895
1896 if (luch.pid)
1897 return (EPERM1);
1898
1899 if (args->datap) {
1900 /*
1901 * The current implementation doesn't support setting
1902 * a capability (it's essentially a stub) so indicate
1903 * that no capabilities are currently set or available
1904 * to request.
1905 */
1906 bzero (&lucd, sizeof(lucd));
1907 error = copyout(&lucd, args->datap, sizeof(lucd));
1908 }
1909
1910 return (error);
1911}
1912
1913int
1914linux_capset(struct thread *td, struct linux_capset_args *args)
1915{
1916 struct l_user_cap_header luch;
1917 struct l_user_cap_data lucd;
1918 int error;
1919
1920 if (args->hdrp == NULL((void *)0) || args->datap == NULL((void *)0))
1921 return (EFAULT14);
1922
1923 error = copyin(args->hdrp, &luch, sizeof(luch));
1924 if (error != 0)
1925 return (error);
1926
1927 if (luch.version != _LINUX_CAPABILITY_VERSION0x19980330) {
1928 luch.version = _LINUX_CAPABILITY_VERSION0x19980330;
1929 error = copyout(&luch, args->hdrp, sizeof(luch));
1930 if (error)
1931 return (error);
1932 return (EINVAL22);
1933 }
1934
1935 if (luch.pid)
1936 return (EPERM1);
1937
1938 error = copyin(args->datap, &lucd, sizeof(lucd));
1939 if (error != 0)
1940 return (error);
1941
1942 /* We currently don't support setting any capabilities. */
1943 if (lucd.effective || lucd.permitted || lucd.inheritable) {
1944 linux_msg(td,
1945 "capset effective=0x%x, permitted=0x%x, "
1946 "inheritable=0x%x is not implemented",
1947 (int)lucd.effective, (int)lucd.permitted,
1948 (int)lucd.inheritable);
1949 return (EPERM1);
1950 }
1951
1952 return (0);
1953}
1954
1955int
1956linux_prctl(struct thread *td, struct linux_prctl_args *args)
1957{
1958 int error = 0, max_size;
1959 struct proc *p = td->td_proc;
1960 char comm[LINUX_MAX_COMM_LEN16];
1961 struct linux_emuldata *em;
1962 int pdeath_signal;
1963
1964#ifdef DEBUG
1965 if (ldebug(prctl)((((const unsigned char *)(linux_debug_map))[(157)/8] & (
1<<((157)%8))) == 0)
)
1966 printf(ARGS(prctl, "%d, %ju, %ju, %ju, %ju")"linux(%ld/%ld): ""prctl""(""%d, %ju, %ju, %ju, %ju"")\n", (long
)td->td_proc->p_pid, (long)td->td_tid
, args->option,
1967 (uintmax_t)args->arg2, (uintmax_t)args->arg3,
1968 (uintmax_t)args->arg4, (uintmax_t)args->arg5);
1969#endif
1970
1971 switch (args->option) {
1972 case LINUX_PR_SET_PDEATHSIG1:
1973 if (!LINUX_SIG_VALID(args->arg2)((args->arg2) <= 64 && (args->arg2) > 0))
1974 return (EINVAL22);
1975 em = em_find(td);
1976 KASSERT(em != NULL, ("prctl: emuldata not found.\n"))do { } while (0);
1977 em->pdeath_signal = args->arg2;
1978 break;
1979 case LINUX_PR_GET_PDEATHSIG2:
1980 em = em_find(td);
1981 KASSERT(em != NULL, ("prctl: emuldata not found.\n"))do { } while (0);
1982 pdeath_signal = em->pdeath_signal;
1983 error = copyout(&pdeath_signal,
1984 (void *)(register_t)args->arg2,
1985 sizeof(pdeath_signal));
1986 break;
1987 case LINUX_PR_GET_KEEPCAPS7:
1988 /*
1989 * Indicate that we always clear the effective and
1990 * permitted capability sets when the user id becomes
1991 * non-zero (actually the capability sets are simply
1992 * always zero in the current implementation).
1993 */
1994 td->td_retvaltd_uretoff.tdu_retval[0] = 0;
1995 break;
1996 case LINUX_PR_SET_KEEPCAPS8:
1997 /*
1998 * Ignore requests to keep the effective and permitted
1999 * capability sets when the user id becomes non-zero.
2000 */
2001 break;
2002 case LINUX_PR_SET_NAME15:
2003 /*
2004 * To be on the safe side we need to make sure to not
2005 * overflow the size a linux program expects. We already
2006 * do this here in the copyin, so that we don't need to
2007 * check on copyout.
2008 */
2009 max_size = MIN(sizeof(comm), sizeof(p->p_comm))(((sizeof(comm))<(sizeof(p->p_comm)))?(sizeof(comm)):(sizeof
(p->p_comm)))
;
2010 error = copyinstr((void *)(register_t)args->arg2, comm,
2011 max_size, NULL((void *)0));
2012
2013 /* Linux silently truncates the name if it is too long. */
2014 if (error == ENAMETOOLONG63) {
2015 /*
2016 * XXX: copyinstr() isn't documented to populate the
2017 * array completely, so do a copyin() to be on the
2018 * safe side. This should be changed in case
2019 * copyinstr() is changed to guarantee this.
2020 */
2021 error = copyin((void *)(register_t)args->arg2, comm,
2022 max_size - 1);
2023 comm[max_size - 1] = '\0';
2024 }
2025 if (error)
2026 return (error);
2027
2028 PROC_LOCK(p)__mtx_lock_flags(&((((&(p)->p_mtx))))->mtx_lock
, ((0)), ("/usr/src/sys/modules/linux64/../../compat/linux/linux_misc.c"
), (2028))
;
2029 strlcpy(p->p_comm, comm, sizeof(p->p_comm));
2030 PROC_UNLOCK(p)__mtx_unlock_flags(&((((&(p)->p_mtx))))->mtx_lock
, ((0)), ("/usr/src/sys/modules/linux64/../../compat/linux/linux_misc.c"
), (2030))
;
2031 break;
2032 case LINUX_PR_GET_NAME16:
2033 PROC_LOCK(p)__mtx_lock_flags(&((((&(p)->p_mtx))))->mtx_lock
, ((0)), ("/usr/src/sys/modules/linux64/../../compat/linux/linux_misc.c"
), (2033))
;
2034 strlcpy(comm, p->p_comm, sizeof(comm));
2035 PROC_UNLOCK(p)__mtx_unlock_flags(&((((&(p)->p_mtx))))->mtx_lock
, ((0)), ("/usr/src/sys/modules/linux64/../../compat/linux/linux_misc.c"
), (2035))
;
2036 error = copyout(comm, (void *)(register_t)args->arg2,
2037 strlen(comm) + 1);
2038 break;
2039 default:
2040 error = EINVAL22;
2041 break;
2042 }
2043
2044 return (error);
2045}
2046
2047int
2048linux_sched_setparam(struct thread *td,
2049 struct linux_sched_setparam_args *uap)
2050{
2051 struct sched_param sched_param;
2052 struct thread *tdt;
2053 int error;
2054
2055#ifdef DEBUG
2056 if (ldebug(sched_setparam)((((const unsigned char *)(linux_debug_map))[(142)/8] & (
1<<((142)%8))) == 0)
)
2057 printf(ARGS(sched_setparam, "%d, *")"linux(%ld/%ld): ""sched_setparam""(""%d, *"")\n", (long)td->
td_proc->p_pid, (long)td->td_tid
, uap->pid);
2058#endif
2059
2060 error = copyin(uap->param, &sched_param, sizeof(sched_param));
2061 if (error)
2062 return (error);
2063
2064 tdt = linux_tdfind(td, uap->pid, -1);
2065 if (tdt == NULL((void *)0))
2066 return (ESRCH3);
2067
2068 error = kern_sched_setparam(td, tdt, &sched_param);
2069 PROC_UNLOCK(tdt->td_proc)__mtx_unlock_flags(&((((&(tdt->td_proc)->p_mtx)
)))->mtx_lock, ((0)), ("/usr/src/sys/modules/linux64/../../compat/linux/linux_misc.c"
), (2069))
;
2070 return (error);
2071}
2072
2073int
2074linux_sched_getparam(struct thread *td,
2075 struct linux_sched_getparam_args *uap)
2076{
2077 struct sched_param sched_param;
2078 struct thread *tdt;
2079 int error;
2080
2081#ifdef DEBUG
2082 if (ldebug(sched_getparam)((((const unsigned char *)(linux_debug_map))[(143)/8] & (
1<<((143)%8))) == 0)
)
2083 printf(ARGS(sched_getparam, "%d, *")"linux(%ld/%ld): ""sched_getparam""(""%d, *"")\n", (long)td->
td_proc->p_pid, (long)td->td_tid
, uap->pid);
2084#endif
2085
2086 tdt = linux_tdfind(td, uap->pid, -1);
2087 if (tdt == NULL((void *)0))
2088 return (ESRCH3);
2089
2090 error = kern_sched_getparam(td, tdt, &sched_param);
2091 PROC_UNLOCK(tdt->td_proc)__mtx_unlock_flags(&((((&(tdt->td_proc)->p_mtx)
)))->mtx_lock, ((0)), ("/usr/src/sys/modules/linux64/../../compat/linux/linux_misc.c"
), (2091))
;
2092 if (error == 0)
2093 error = copyout(&sched_param, uap->param,
2094 sizeof(sched_param));
2095 return (error);
2096}
2097
2098/*
2099 * Get affinity of a process.
2100 */
2101int
2102linux_sched_getaffinity(struct thread *td,
2103 struct linux_sched_getaffinity_args *args)
2104{
2105 int error;
2106 struct thread *tdt;
2107 struct cpuset_getaffinity_args cga;
2108
2109#ifdef DEBUG
2110 if (ldebug(sched_getaffinity)((((const unsigned char *)(linux_debug_map))[(204)/8] & (
1<<((204)%8))) == 0)
)
2111 printf(ARGS(sched_getaffinity, "%d, %d, *")"linux(%ld/%ld): ""sched_getaffinity""(""%d, %d, *"")\n", (long
)td->td_proc->p_pid, (long)td->td_tid
, args->pid,
2112 args->len);
2113#endif
2114 if (args->len < sizeof(cpuset_t))
2115 return (EINVAL22);
2116
2117 tdt = linux_tdfind(td, args->pid, -1);
2118 if (tdt == NULL((void *)0))
2119 return (ESRCH3);
2120
2121 PROC_UNLOCK(tdt->td_proc)__mtx_unlock_flags(&((((&(tdt->td_proc)->p_mtx)
)))->mtx_lock, ((0)), ("/usr/src/sys/modules/linux64/../../compat/linux/linux_misc.c"
), (2121))
;
2122 cga.level = CPU_LEVEL_WHICH3;
2123 cga.which = CPU_WHICH_TID1;
2124 cga.id = tdt->td_tid;
2125 cga.cpusetsize = sizeof(cpuset_t);
2126 cga.mask = (cpuset_t *) args->user_mask_ptr;
2127
2128 if ((error = sys_cpuset_getaffinity(td, &cga)) == 0)
2129 td->td_retvaltd_uretoff.tdu_retval[0] = sizeof(cpuset_t);
2130
2131 return (error);
2132}
2133
2134/*
2135 * Set affinity of a process.
2136 */
2137int
2138linux_sched_setaffinity(struct thread *td,
2139 struct linux_sched_setaffinity_args *args)
2140{
2141 struct cpuset_setaffinity_args csa;
2142 struct thread *tdt;
2143
2144#ifdef DEBUG
2145 if (ldebug(sched_setaffinity)((((const unsigned char *)(linux_debug_map))[(203)/8] & (
1<<((203)%8))) == 0)
)
2146 printf(ARGS(sched_setaffinity, "%d, %d, *")"linux(%ld/%ld): ""sched_setaffinity""(""%d, %d, *"")\n", (long
)td->td_proc->p_pid, (long)td->td_tid
, args->pid,
2147 args->len);
2148#endif
2149 if (args->len < sizeof(cpuset_t))
2150 return (EINVAL22);
2151
2152 tdt = linux_tdfind(td, args->pid, -1);
2153 if (tdt == NULL((void *)0))
2154 return (ESRCH3);
2155
2156 PROC_UNLOCK(tdt->td_proc)__mtx_unlock_flags(&((((&(tdt->td_proc)->p_mtx)
)))->mtx_lock, ((0)), ("/usr/src/sys/modules/linux64/../../compat/linux/linux_misc.c"
), (2156))
;
2157 csa.level = CPU_LEVEL_WHICH3;
2158 csa.which = CPU_WHICH_TID1;
2159 csa.id = tdt->td_tid;
2160 csa.cpusetsize = sizeof(cpuset_t);
2161 csa.mask = (cpuset_t *) args->user_mask_ptr;
2162
2163 return (sys_cpuset_setaffinity(td, &csa));
2164}
2165
2166struct linux_rlimit64 {
2167 uint64_t rlim_cur;
2168 uint64_t rlim_max;
2169};
2170
2171int
2172linux_prlimit64(struct thread *td, struct linux_prlimit64_args *args)
2173{
2174 struct rlimit rlim, nrlim;
2175 struct linux_rlimit64 lrlim;
2176 struct proc *p;
2177 u_int which;
2178 int flags;
2179 int error;
2180
2181#ifdef DEBUG
2182 if (ldebug(prlimit64)((((const unsigned char *)(linux_debug_map))[(302)/8] & (
1<<((302)%8))) == 0)
)
2183 printf(ARGS(prlimit64, "%d, %d, %p, %p")"linux(%ld/%ld): ""prlimit64""(""%d, %d, %p, %p"")\n", (long)
td->td_proc->p_pid, (long)td->td_tid
, args->pid,
2184 args->resource, (void *)args->new, (void *)args->old);
2185#endif
2186
2187 if (args->resource >= LINUX_RLIM_NLIMITS10)
2188 return (EINVAL22);
2189
2190 which = linux_to_bsd_resource[args->resource];
2191 if (which == -1)
2192 return (EINVAL22);
2193
2194 if (args->new != NULL((void *)0)) {
2195 /*
2196 * Note. Unlike FreeBSD where rlim is signed 64-bit Linux
2197 * rlim is unsigned 64-bit. FreeBSD treats negative limits
2198 * as INFINITY so we do not need a conversion even.
2199 */
2200 error = copyin(args->new, &nrlim, sizeof(nrlim));
2201 if (error != 0)
2202 return (error);
2203 }
2204
2205 flags = PGET_HOLD0x00001 | PGET_NOTWEXIT0x00010;
2206 if (args->new != NULL((void *)0))
2207 flags |= PGET_CANDEBUG0x00004;
2208 else
2209 flags |= PGET_CANSEE0x00002;
2210 error = pget(args->pid, flags, &p);
2211 if (error != 0)
2212 return (error);
2213
2214 if (args->old != NULL((void *)0)) {
2215 PROC_LOCK(p)__mtx_lock_flags(&((((&(p)->p_mtx))))->mtx_lock
, ((0)), ("/usr/src/sys/modules/linux64/../../compat/linux/linux_misc.c"
), (2215))
;
2216 lim_rlimit_proc(p, which, &rlim);
2217 PROC_UNLOCK(p)__mtx_unlock_flags(&((((&(p)->p_mtx))))->mtx_lock
, ((0)), ("/usr/src/sys/modules/linux64/../../compat/linux/linux_misc.c"
), (2217))
;
2218 if (rlim.rlim_cur == RLIM_INFINITY((rlim_t)(((__uint64_t)1 << 63) - 1)))
2219 lrlim.rlim_cur = LINUX_RLIM_INFINITY(~0UL);
2220 else
2221 lrlim.rlim_cur = rlim.rlim_cur;
2222 if (rlim.rlim_max == RLIM_INFINITY((rlim_t)(((__uint64_t)1 << 63) - 1)))
2223 lrlim.rlim_max = LINUX_RLIM_INFINITY(~0UL);
2224 else
2225 lrlim.rlim_max = rlim.rlim_max;
2226 error = copyout(&lrlim, args->old, sizeof(lrlim));
2227 if (error != 0)
2228 goto out;
2229 }
2230
2231 if (args->new != NULL((void *)0))
2232 error = kern_proc_setrlimit(td, p, which, &nrlim);
2233
2234 out:
2235 PRELE(p)do { __mtx_lock_flags(&((((&((p))->p_mtx))))->mtx_lock
, ((0)), ("/usr/src/sys/modules/linux64/../../compat/linux/linux_misc.c"
), (2235)); do { (void)0; do { do { } while (0); } while (0);
(--((p))->p_lock); if ((((p))->p_flag & 0x02000) &&
((p))->p_lock == 0) wakeup(&((p))->p_lock); } while
(0); __mtx_unlock_flags(&((((&((p))->p_mtx))))->
mtx_lock, ((0)), ("/usr/src/sys/modules/linux64/../../compat/linux/linux_misc.c"
), (2235)); } while (0)
;
2236 return (error);
2237}
2238
2239int
2240linux_pselect6(struct thread *td, struct linux_pselect6_args *args)
2241{
2242 struct timeval utv, tv0, tv1, *tvp;
2243 struct l_pselect6arg lpse6;
2244 struct l_timespec lts;
2245 struct timespec uts;
2246 l_sigset_t l_ss;
2247 sigset_t *ssp;
2248 sigset_t ss;
2249 int error;
2250
2251 ssp = NULL((void *)0);
2252 if (args->sig != NULL((void *)0)) {
2253 error = copyin(args->sig, &lpse6, sizeof(lpse6));
2254 if (error != 0)
2255 return (error);
2256 if (lpse6.ss_len != sizeof(l_ss))
2257 return (EINVAL22);
2258 if (lpse6.ss != 0) {
2259 error = copyin(PTRIN(lpse6.ss)(void *)(lpse6.ss), &l_ss,
2260 sizeof(l_ss));
2261 if (error != 0)
2262 return (error);
2263 linux_to_bsd_sigset(&l_ss, &ss);
2264 ssp = &ss;
2265 }
2266 }
2267
2268 /*
2269 * Currently glibc changes nanosecond number to microsecond.
2270 * This mean losing precision but for now it is hardly seen.
2271 */
2272 if (args->tsp != NULL((void *)0)) {
2273 error = copyin(args->tsp, &lts, sizeof(lts));
2274 if (error != 0)
2275 return (error);
2276 error = linux_to_native_timespec(&uts, &lts);
2277 if (error != 0)
2278 return (error);
2279
2280 TIMESPEC_TO_TIMEVAL(&utv, &uts)do { (&utv)->tv_sec = (&uts)->tv_sec; (&utv
)->tv_usec = (&uts)->tv_nsec / 1000; } while (0)
;
2281 if (itimerfix(&utv))
2282 return (EINVAL22);
2283
2284 microtime(&tv0);
2285 tvp = &utv;
2286 } else
2287 tvp = NULL((void *)0);
2288
2289 error = kern_pselect(td, args->nfds, args->readfds, args->writefds,
2290 args->exceptfds, tvp, ssp, LINUX_NFDBITSsizeof(l_fd_mask) * 8);
2291
2292 if (error == 0 && args->tsp != NULL((void *)0)) {
2293 if (td->td_retvaltd_uretoff.tdu_retval[0] != 0) {
2294 /*
2295 * Compute how much time was left of the timeout,
2296 * by subtracting the current time and the time
2297 * before we started the call, and subtracting
2298 * that result from the user-supplied value.
2299 */
2300
2301 microtime(&tv1);
2302 timevalsub(&tv1, &tv0);
2303 timevalsub(&utv, &tv1);
2304 if (utv.tv_sec < 0)
2305 timevalclear(&utv)((&utv)->tv_sec = (&utv)->tv_usec = 0);
2306 } else
2307 timevalclear(&utv)((&utv)->tv_sec = (&utv)->tv_usec = 0);
2308
2309 TIMEVAL_TO_TIMESPEC(&utv, &uts)do { (&uts)->tv_sec = (&utv)->tv_sec; (&uts
)->tv_nsec = (&utv)->tv_usec * 1000; } while (0)
;
2310
2311 native_to_linux_timespec(&lts, &uts);
2312 error = copyout(&lts, args->tsp, sizeof(lts));
2313 }
2314
2315 return (error);
2316}
2317
2318int
2319linux_ppoll(struct thread *td, struct linux_ppoll_args *args)
2320{
2321 struct timespec ts0, ts1;
2322 struct l_timespec lts;
2323 struct timespec uts, *tsp;
2324 l_sigset_t l_ss;
2325 sigset_t *ssp;
2326 sigset_t ss;
2327 int error;
2328
2329 if (args->sset != NULL((void *)0)) {
2330 if (args->ssize != sizeof(l_ss))
2331 return (EINVAL22);
2332 error = copyin(args->sset, &l_ss, sizeof(l_ss));
2333 if (error)
2334 return (error);
2335 linux_to_bsd_sigset(&l_ss, &ss);
2336 ssp = &ss;
2337 } else
2338 ssp = NULL((void *)0);
2339 if (args->tsp != NULL((void *)0)) {
2340 error = copyin(args->tsp, &lts, sizeof(lts));
2341 if (error)
2342 return (error);
2343 error = linux_to_native_timespec(&uts, &lts);
2344 if (error != 0)
2345 return (error);
2346
2347 nanotime(&ts0);
2348 tsp = &uts;
2349 } else
2350 tsp = NULL((void *)0);
2351
2352 error = kern_poll(td, args->fds, args->nfds, tsp, ssp);
2353
2354 if (error == 0 && args->tsp != NULL((void *)0)) {
2355 if (td->td_retvaltd_uretoff.tdu_retval[0]) {
2356 nanotime(&ts1);
2357 timespecsub(&ts1, &ts0)do { (&ts1)->tv_sec -= (&ts0)->tv_sec; (&ts1
)->tv_nsec -= (&ts0)->tv_nsec; if ((&ts1)->tv_nsec
< 0) { (&ts1)->tv_sec--; (&ts1)->tv_nsec +=
1000000000; } } while (0)
;
2358 timespecsub(&uts, &ts1)do { (&uts)->tv_sec -= (&ts1)->tv_sec; (&uts
)->tv_nsec -= (&ts1)->tv_nsec; if ((&uts)->tv_nsec
< 0) { (&uts)->tv_sec--; (&uts)->tv_nsec +=
1000000000; } } while (0)
;
2359 if (uts.tv_sec < 0)
2360 timespecclear(&uts)((&uts)->tv_sec = (&uts)->tv_nsec = 0);
2361 } else
2362 timespecclear(&uts)((&uts)->tv_sec = (&uts)->tv_nsec = 0);
2363
2364 native_to_linux_timespec(&lts, &uts);
2365 error = copyout(&lts, args->tsp, sizeof(lts));
2366 }
2367
2368 return (error);
2369}
2370
2371#if defined(DEBUG) || defined(KTR)
2372/* XXX: can be removed when every ldebug(...) and KTR stuff are removed. */
2373
2374#ifdef COMPAT_LINUX32
2375#define L_MAXSYSCALL LINUX32_SYS_MAXSYSCALL
2376#else
2377#define L_MAXSYSCALL LINUX_SYS_MAXSYSCALL314
2378#endif
2379
2380u_char linux_debug_map[howmany(L_MAXSYSCALL, sizeof(u_char))(((L_MAXSYSCALL)+((sizeof(u_char))-1))/(sizeof(u_char)))];
2381
2382static int
2383linux_debug(int syscall, int toggle, int global)
2384{
2385
2386 if (global) {
2387 char c = toggle ? 0 : 0xff;
2388
2389 memset(linux_debug_map, c, sizeof(linux_debug_map));
2390 return (0);
2391 }
2392 if (syscall < 0 || syscall >= L_MAXSYSCALL)
2393 return (EINVAL22);
2394 if (toggle)
2395 clrbit(linux_debug_map, syscall)(((unsigned char *)(linux_debug_map))[(syscall)/8] &= ~(1
<<((syscall)%8)))
;
2396 else
2397 setbit(linux_debug_map, syscall)(((unsigned char *)(linux_debug_map))[(syscall)/8] |= 1<<
((syscall)%8))
;
2398 return (0);
2399}
2400#undef L_MAXSYSCALL
2401
2402/*
2403 * Usage: sysctl linux.debug=<syscall_nr>.<0/1>
2404 *
2405 * E.g.: sysctl linux.debug=21.0
2406 *
2407 * As a special case, syscall "all" will apply to all syscalls globally.
2408 */
2409#define LINUX_MAX_DEBUGSTR 16
2410int
2411linux_sysctl_debug(SYSCTL_HANDLER_ARGSstruct sysctl_oid *oidp, void *arg1, intmax_t arg2, struct sysctl_req
*req
)
2412{
2413 char value[LINUX_MAX_DEBUGSTR], *p;
2414 int error, sysc, toggle;
2415 int global = 0;
2416
2417 value[0] = '\0';
2418 error = sysctl_handle_string(oidp, value, LINUX_MAX_DEBUGSTR, req);
2419 if (error || req->newptr == NULL((void *)0))
2420 return (error);
2421 for (p = value; *p != '\0' && *p != '.'; p++);
2422 if (*p == '\0')
2423 return (EINVAL22);
2424 *p++ = '\0';
2425 sysc = strtol(value, NULL((void *)0), 0);
2426 toggle = strtol(p, NULL((void *)0), 0);
2427 if (strcmp(value, "all") == 0)
2428 global = 1;
2429 error = linux_debug(sysc, toggle, global);
2430 return (error);
2431}
2432
2433#endif /* DEBUG || KTR */
2434
2435int
2436linux_sched_rr_get_interval(struct thread *td,
2437 struct linux_sched_rr_get_interval_args *uap)
2438{
2439 struct timespec ts;
2440 struct l_timespec lts;
2441 struct thread *tdt;
2442 int error;
2443
2444 /*
2445 * According to man in case the invalid pid specified
2446 * EINVAL should be returned.
2447 */
2448 if (uap->pid < 0)
2449 return (EINVAL22);
2450
2451 tdt = linux_tdfind(td, uap->pid, -1);
2452 if (tdt == NULL((void *)0))
2453 return (ESRCH3);
2454
2455 error = kern_sched_rr_get_interval_td(td, tdt, &ts);
2456 PROC_UNLOCK(tdt->td_proc)__mtx_unlock_flags(&((((&(tdt->td_proc)->p_mtx)
)))->mtx_lock, ((0)), ("/usr/src/sys/modules/linux64/../../compat/linux/linux_misc.c"
), (2456))
;
2457 if (error != 0)
2458 return (error);
2459 native_to_linux_timespec(&lts, &ts);
2460 return (copyout(&lts, uap->interval, sizeof(lts)));
2461}
2462
2463/*
2464 * In case when the Linux thread is the initial thread in
2465 * the thread group thread id is equal to the process id.
2466 * Glibc depends on this magic (assert in pthread_getattr_np.c).
2467 */
2468struct thread *
2469linux_tdfind(struct thread *td, lwpid_t tid, pid_t pid)
2470{
2471 struct linux_emuldata *em;
2472 struct thread *tdt;
2473 struct proc *p;
2474
2475 tdt = NULL((void *)0);
2476 if (tid == 0 || tid == td->td_tid) {
2477 tdt = td;
2478 PROC_LOCK(tdt->td_proc)__mtx_lock_flags(&((((&(tdt->td_proc)->p_mtx)))
)->mtx_lock, ((0)), ("/usr/src/sys/modules/linux64/../../compat/linux/linux_misc.c"
), (2478))
;
2479 } else if (tid > PID_MAX99999)
2480 tdt = tdfind(tid, pid);
2481 else {
2482 /*
2483 * Initial thread where the tid equal to the pid.
2484 */
2485 p = pfind(tid);
2486 if (p != NULL((void *)0)) {
2487 if (SV_PROC_ABI(p)((p)->p_sysent->sv_flags & 0xff) != SV_ABI_LINUX3) {
2488 /*
2489 * p is not a Linuxulator process.
2490 */
2491 PROC_UNLOCK(p)__mtx_unlock_flags(&((((&(p)->p_mtx))))->mtx_lock
, ((0)), ("/usr/src/sys/modules/linux64/../../compat/linux/linux_misc.c"
), (2491))
;
2492 return (NULL((void *)0));
2493 }
2494 FOREACH_THREAD_IN_PROC(p, tdt)for (((tdt)) = (((&(p)->p_threads))->tqh_first); ((
tdt)); ((tdt)) = ((((tdt)))->td_plist.tqe_next))
{
2495 em = em_find(tdt);
2496 if (tid == em->em_tid)
2497 return (tdt);
2498 }
2499 PROC_UNLOCK(p)__mtx_unlock_flags(&((((&(p)->p_mtx))))->mtx_lock
, ((0)), ("/usr/src/sys/modules/linux64/../../compat/linux/linux_misc.c"
), (2499))
;
2500 }
2501 return (NULL((void *)0));
2502 }
2503
2504 return (tdt);
2505}
2506
2507void
2508linux_to_bsd_waitopts(int options, int *bsdopts)
2509{
2510
2511 if (options & LINUX_WNOHANG0x00000001)
2512 *bsdopts |= WNOHANG1;
2513 if (options & LINUX_WUNTRACED0x00000002)
2514 *bsdopts |= WUNTRACED2;
2515 if (options & LINUX_WEXITED0x00000004)
2516 *bsdopts |= WEXITED16;
2517 if (options & LINUX_WCONTINUED0x00000008)
2518 *bsdopts |= WCONTINUED4;
2519 if (options & LINUX_WNOWAIT0x01000000)
2520 *bsdopts |= WNOWAIT8;
2521
2522 if (options & __WCLONE0x80000000)
2523 *bsdopts |= WLINUXCLONE0x80000000;
2524}