xref: /freebsd/sys/compat/linprocfs/linprocfs.c (revision 809922b01004daf627ad4b8d92c7f98eb579043c)
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 2000 Dag-Erling Coïdan Smørgrav
5  * Copyright (c) 1999 Pierre Beyssac
6  * Copyright (c) 1993 Jan-Simon Pendry
7  * Copyright (c) 1993
8  *	The Regents of the University of California.  All rights reserved.
9  *
10  * This code is derived from software contributed to Berkeley by
11  * Jan-Simon Pendry.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *	This product includes software developed by the University of
24  *	California, Berkeley and its contributors.
25  * 4. Neither the name of the University nor the names of its contributors
26  *    may be used to endorse or promote products derived from this software
27  *    without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39  * SUCH DAMAGE.
40  *
41  *	@(#)procfs_status.c	8.4 (Berkeley) 6/15/94
42  */
43 
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
46 
47 #include <sys/param.h>
48 #include <sys/queue.h>
49 #include <sys/blist.h>
50 #include <sys/conf.h>
51 #include <sys/exec.h>
52 #include <sys/fcntl.h>
53 #include <sys/filedesc.h>
54 #include <sys/jail.h>
55 #include <sys/kernel.h>
56 #include <sys/limits.h>
57 #include <sys/linker.h>
58 #include <sys/lock.h>
59 #include <sys/malloc.h>
60 #include <sys/msg.h>
61 #include <sys/mutex.h>
62 #include <sys/namei.h>
63 #include <sys/proc.h>
64 #include <sys/ptrace.h>
65 #include <sys/resourcevar.h>
66 #include <sys/resource.h>
67 #include <sys/sbuf.h>
68 #include <sys/sem.h>
69 #include <sys/shm.h>
70 #include <sys/smp.h>
71 #include <sys/socket.h>
72 #include <sys/syscallsubr.h>
73 #include <sys/sysctl.h>
74 #include <sys/sysent.h>
75 #include <sys/systm.h>
76 #include <sys/time.h>
77 #include <sys/tty.h>
78 #include <sys/user.h>
79 #include <sys/uuid.h>
80 #include <sys/vmmeter.h>
81 #include <sys/vnode.h>
82 #include <sys/bus.h>
83 #include <sys/uio.h>
84 
85 #include <net/if.h>
86 #include <net/if_var.h>
87 #include <net/if_types.h>
88 
89 #include <vm/vm.h>
90 #include <vm/vm_extern.h>
91 #include <vm/pmap.h>
92 #include <vm/vm_map.h>
93 #include <vm/vm_param.h>
94 #include <vm/vm_object.h>
95 #include <vm/swap_pager.h>
96 
97 #include <machine/clock.h>
98 
99 #include <geom/geom.h>
100 #include <geom/geom_int.h>
101 
102 #if defined(__i386__) || defined(__amd64__)
103 #include <machine/cputypes.h>
104 #include <machine/md_var.h>
105 #endif /* __i386__ || __amd64__ */
106 
107 #include <compat/linux/linux.h>
108 #include <compat/linux/linux_emul.h>
109 #include <compat/linux/linux_mib.h>
110 #include <compat/linux/linux_misc.h>
111 #include <compat/linux/linux_util.h>
112 #include <fs/pseudofs/pseudofs.h>
113 #include <fs/procfs/procfs.h>
114 
115 /*
116  * Various conversion macros
117  */
118 #define T2J(x) ((long)(((x) * 100ULL) / (stathz ? stathz : hz)))	/* ticks to jiffies */
119 #define T2CS(x) ((unsigned long)(((x) * 100ULL) / (stathz ? stathz : hz)))	/* ticks to centiseconds */
120 #define T2S(x) ((x) / (stathz ? stathz : hz))		/* ticks to seconds */
121 #define B2K(x) ((x) >> 10)				/* bytes to kbytes */
122 #define B2P(x) ((x) >> PAGE_SHIFT)			/* bytes to pages */
123 #define P2B(x) ((x) << PAGE_SHIFT)			/* pages to bytes */
124 #define P2K(x) ((x) << (PAGE_SHIFT - 10))		/* pages to kbytes */
125 #define TV2J(x)	((x)->tv_sec * 100UL + (x)->tv_usec / 10000)
126 
127 /**
128  * @brief Mapping of ki_stat in struct kinfo_proc to the linux state
129  *
130  * The linux procfs state field displays one of the characters RSDZTW to
131  * denote running, sleeping in an interruptible wait, waiting in an
132  * uninterruptible disk sleep, a zombie process, process is being traced
133  * or stopped, or process is paging respectively.
134  *
135  * Our struct kinfo_proc contains the variable ki_stat which contains a
136  * value out of SIDL, SRUN, SSLEEP, SSTOP, SZOMB, SWAIT and SLOCK.
137  *
138  * This character array is used with ki_stati-1 as an index and tries to
139  * map our states to suitable linux states.
140  */
141 static char linux_state[] = "RRSTZDD";
142 
143 /*
144  * Filler function for proc/meminfo
145  */
146 static int
147 linprocfs_domeminfo(PFS_FILL_ARGS)
148 {
149 	unsigned long memtotal;		/* total memory in bytes */
150 	unsigned long memfree;		/* free memory in bytes */
151 	unsigned long cached;		/* page cache */
152 	unsigned long buffers;		/* buffer cache */
153 	unsigned long long swaptotal;	/* total swap space in bytes */
154 	unsigned long long swapused;	/* used swap space in bytes */
155 	unsigned long long swapfree;	/* free swap space in bytes */
156 	size_t sz;
157 	int error, i, j;
158 
159 	memtotal = physmem * PAGE_SIZE;
160 	memfree = (unsigned long)vm_free_count() * PAGE_SIZE;
161 	swap_pager_status(&i, &j);
162 	swaptotal = (unsigned long long)i * PAGE_SIZE;
163 	swapused = (unsigned long long)j * PAGE_SIZE;
164 	swapfree = swaptotal - swapused;
165 
166 	/*
167 	 * This value may exclude wired pages, but we have no good way of
168 	 * accounting for that.
169 	 */
170 	cached =
171 	    (vm_active_count() + vm_inactive_count() + vm_laundry_count()) *
172 	    PAGE_SIZE;
173 
174 	sz = sizeof(buffers);
175 	error = kernel_sysctlbyname(curthread, "vfs.bufspace", &buffers, &sz,
176 	    NULL, 0, 0, 0);
177 	if (error != 0)
178 		buffers = 0;
179 
180 	sbuf_printf(sb,
181 	    "MemTotal: %9lu kB\n"
182 	    "MemFree:  %9lu kB\n"
183 	    "Buffers:  %9lu kB\n"
184 	    "Cached:   %9lu kB\n"
185 	    "SwapTotal:%9llu kB\n"
186 	    "SwapFree: %9llu kB\n",
187 	    B2K(memtotal), B2K(memfree), B2K(buffers),
188 	    B2K(cached), B2K(swaptotal), B2K(swapfree));
189 
190 	return (0);
191 }
192 
193 #if defined(__i386__) || defined(__amd64__)
194 /*
195  * Filler function for proc/cpuinfo (i386 & amd64 version)
196  */
197 static int
198 linprocfs_docpuinfo(PFS_FILL_ARGS)
199 {
200 	int hw_model[2];
201 	char model[128];
202 	uint64_t freq;
203 	size_t size;
204 	u_int cache_size[4];
205 	int fqmhz, fqkhz;
206 	int i, j;
207 
208 	/*
209 	 * We default the flags to include all non-conflicting flags,
210 	 * and the Intel versions of conflicting flags.
211 	 */
212 	static char *cpu_feature_names[] = {
213 		/*  0 */ "fpu", "vme", "de", "pse",
214 		/*  4 */ "tsc", "msr", "pae", "mce",
215 		/*  8 */ "cx8", "apic", "", "sep",
216 		/* 12 */ "mtrr", "pge", "mca", "cmov",
217 		/* 16 */ "pat", "pse36", "pn", "clflush",
218 		/* 20 */ "", "dts", "acpi", "mmx",
219 		/* 24 */ "fxsr", "sse", "sse2", "ss",
220 		/* 28 */ "ht", "tm", "ia64", "pbe"
221 	};
222 
223 	static char *amd_feature_names[] = {
224 		/*  0 */ "", "", "", "",
225 		/*  4 */ "", "", "", "",
226 		/*  8 */ "", "", "", "syscall",
227 		/* 12 */ "", "", "", "",
228 		/* 16 */ "", "", "", "mp",
229 		/* 20 */ "nx", "", "mmxext", "",
230 		/* 24 */ "", "fxsr_opt", "pdpe1gb", "rdtscp",
231 		/* 28 */ "", "lm", "3dnowext", "3dnow"
232 	};
233 
234 	static char *cpu_feature2_names[] = {
235 		/*  0 */ "pni", "pclmulqdq", "dtes64", "monitor",
236 		/*  4 */ "ds_cpl", "vmx", "smx", "est",
237 		/*  8 */ "tm2", "ssse3", "cid", "sdbg",
238 		/* 12 */ "fma", "cx16", "xtpr", "pdcm",
239 		/* 16 */ "", "pcid", "dca", "sse4_1",
240 		/* 20 */ "sse4_2", "x2apic", "movbe", "popcnt",
241 		/* 24 */ "tsc_deadline_timer", "aes", "xsave", "",
242 		/* 28 */ "avx", "f16c", "rdrand", "hypervisor"
243 	};
244 
245 	static char *amd_feature2_names[] = {
246 		/*  0 */ "lahf_lm", "cmp_legacy", "svm", "extapic",
247 		/*  4 */ "cr8_legacy", "abm", "sse4a", "misalignsse",
248 		/*  8 */ "3dnowprefetch", "osvw", "ibs", "xop",
249 		/* 12 */ "skinit", "wdt", "", "lwp",
250 		/* 16 */ "fma4", "tce", "", "nodeid_msr",
251 		/* 20 */ "", "tbm", "topoext", "perfctr_core",
252 		/* 24 */ "perfctr_nb", "", "bpext", "ptsc",
253 		/* 28 */ "perfctr_llc", "mwaitx", "", ""
254 	};
255 
256 	static char *cpu_stdext_feature_names[] = {
257 		/*  0 */ "fsgsbase", "tsc_adjust", "", "bmi1",
258 		/*  4 */ "hle", "avx2", "", "smep",
259 		/*  8 */ "bmi2", "erms", "invpcid", "rtm",
260 		/* 12 */ "cqm", "", "mpx", "rdt_a",
261 		/* 16 */ "avx512f", "avx512dq", "rdseed", "adx",
262 		/* 20 */ "smap", "avx512ifma", "", "clflushopt",
263 		/* 24 */ "clwb", "intel_pt", "avx512pf", "avx512er",
264 		/* 28 */ "avx512cd", "sha_ni", "avx512bw", "avx512vl"
265 	};
266 
267 	static char *power_flags[] = {
268 		"ts",           "fid",          "vid",
269 		"ttp",          "tm",           "stc",
270 		"100mhzsteps",  "hwpstate",     "",
271 		"cpb",          "eff_freq_ro",  "proc_feedback",
272 		"acc_power",
273 	};
274 
275 	hw_model[0] = CTL_HW;
276 	hw_model[1] = HW_MODEL;
277 	model[0] = '\0';
278 	size = sizeof(model);
279 	if (kernel_sysctl(td, hw_model, 2, &model, &size, 0, 0, 0, 0) != 0)
280 		strcpy(model, "unknown");
281 #ifdef __i386__
282 	switch (cpu_vendor_id) {
283 	case CPU_VENDOR_AMD:
284 		if (cpu_class < CPUCLASS_686)
285 			cpu_feature_names[16] = "fcmov";
286 		break;
287 	case CPU_VENDOR_CYRIX:
288 		cpu_feature_names[24] = "cxmmx";
289 		break;
290 	}
291 #endif
292 	if (cpu_exthigh >= 0x80000006)
293 		do_cpuid(0x80000006, cache_size);
294 	else
295 		memset(cache_size, 0, sizeof(cache_size));
296 	for (i = 0; i < mp_ncpus; ++i) {
297 		fqmhz = 0;
298 		fqkhz = 0;
299 		freq = atomic_load_acq_64(&tsc_freq);
300 		if (freq != 0) {
301 			fqmhz = (freq + 4999) / 1000000;
302 			fqkhz = ((freq + 4999) / 10000) % 100;
303 		}
304 		sbuf_printf(sb,
305 		    "processor\t: %d\n"
306 		    "vendor_id\t: %.20s\n"
307 		    "cpu family\t: %u\n"
308 		    "model\t\t: %u\n"
309 		    "model name\t: %s\n"
310 		    "stepping\t: %u\n"
311 		    "cpu MHz\t\t: %d.%02d\n"
312 		    "cache size\t: %d KB\n"
313 		    "physical id\t: %d\n"
314 		    "siblings\t: %d\n"
315 		    "core id\t\t: %d\n"
316 		    "cpu cores\t: %d\n"
317 		    "apicid\t\t: %d\n"
318 		    "initial apicid\t: %d\n"
319 		    "fpu\t\t: %s\n"
320 		    "fpu_exception\t: %s\n"
321 		    "cpuid level\t: %d\n"
322 		    "wp\t\t: %s\n",
323 		    i, cpu_vendor, CPUID_TO_FAMILY(cpu_id),
324 		    CPUID_TO_MODEL(cpu_id), model, cpu_id & CPUID_STEPPING,
325 		    fqmhz, fqkhz,
326 		    (cache_size[2] >> 16), 0, mp_ncpus, i, mp_ncpus,
327 		    i, i, /*cpu_id & CPUID_LOCAL_APIC_ID ??*/
328 		    (cpu_feature & CPUID_FPU) ? "yes" : "no", "yes",
329 		    CPUID_TO_FAMILY(cpu_id), "yes");
330 		sbuf_cat(sb, "flags\t\t:");
331 		for (j = 0; j < nitems(cpu_feature_names); j++)
332 			if (cpu_feature & (1 << j) &&
333 			    cpu_feature_names[j][0] != '\0')
334 				sbuf_printf(sb, " %s", cpu_feature_names[j]);
335 		for (j = 0; j < nitems(amd_feature_names); j++)
336 			if (amd_feature & (1 << j) &&
337 			    amd_feature_names[j][0] != '\0')
338 				sbuf_printf(sb, " %s", amd_feature_names[j]);
339 		for (j = 0; j < nitems(cpu_feature2_names); j++)
340 			if (cpu_feature2 & (1 << j) &&
341 			    cpu_feature2_names[j][0] != '\0')
342 				sbuf_printf(sb, " %s", cpu_feature2_names[j]);
343 		for (j = 0; j < nitems(amd_feature2_names); j++)
344 			if (amd_feature2 & (1 << j) &&
345 			    amd_feature2_names[j][0] != '\0')
346 				sbuf_printf(sb, " %s", amd_feature2_names[j]);
347 		for (j = 0; j < nitems(cpu_stdext_feature_names); j++)
348 			if (cpu_stdext_feature & (1 << j) &&
349 			    cpu_stdext_feature_names[j][0] != '\0')
350 				sbuf_printf(sb, " %s",
351 				    cpu_stdext_feature_names[j]);
352 		sbuf_cat(sb, "\n");
353 		sbuf_printf(sb,
354 		    "bugs\t\t: %s\n"
355 		    "bogomips\t: %d.%02d\n"
356 		    "clflush size\t: %d\n"
357 		    "cache_alignment\t: %d\n"
358 		    "address sizes\t: %d bits physical, %d bits virtual\n",
359 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
360 		    (has_f00f_bug) ? "Intel F00F" : "",
361 #else
362 		    "",
363 #endif
364 		    fqmhz * 2, fqkhz,
365 		    cpu_clflush_line_size, cpu_clflush_line_size,
366 		    cpu_maxphyaddr,
367 		    (cpu_maxphyaddr > 32) ? 48 : 0);
368 		sbuf_cat(sb, "power management: ");
369 		for (j = 0; j < nitems(power_flags); j++)
370 			if (amd_pminfo & (1 << j))
371 				sbuf_printf(sb, " %s", power_flags[j]);
372 		sbuf_cat(sb, "\n\n");
373 
374 		/* XXX per-cpu vendor / class / model / id? */
375 	}
376 	sbuf_cat(sb, "\n");
377 
378 	return (0);
379 }
380 #else
381 /* ARM64TODO: implement non-stubbed linprocfs_docpuinfo */
382 static int
383 linprocfs_docpuinfo(PFS_FILL_ARGS)
384 {
385 	int i;
386 
387 	for (i = 0; i < mp_ncpus; ++i) {
388 		sbuf_printf(sb,
389 		    "processor\t: %d\n"
390 		    "BogoMIPS\t: %d.%02d\n",
391 		    i, 0, 0);
392 		sbuf_cat(sb, "Features\t: ");
393 		sbuf_cat(sb, "\n");
394 		sbuf_printf(sb,
395 		    "CPU implementer\t: \n"
396 		    "CPU architecture: \n"
397 		    "CPU variant\t: 0x%x\n"
398 		    "CPU part\t: 0x%x\n"
399 		    "CPU revision\t: %d\n",
400 		    0, 0, 0);
401 		sbuf_cat(sb, "\n");
402 	}
403 
404 	return (0);
405 }
406 #endif /* __i386__ || __amd64__ */
407 
408 static const char *path_slash_sys = "/sys";
409 static const char *fstype_sysfs = "sysfs";
410 
411 static int
412 _mtab_helper(const struct pfs_node *pn, const struct statfs *sp,
413     const char **mntfrom, const char **mntto, const char **fstype)
414 {
415 	/* determine device name */
416 	*mntfrom = sp->f_mntfromname;
417 
418 	/* determine mount point */
419 	*mntto = sp->f_mntonname;
420 
421 	/* determine fs type */
422 	*fstype = sp->f_fstypename;
423 	if (strcmp(*fstype, pn->pn_info->pi_name) == 0)
424 		*mntfrom = *fstype = "proc";
425 	else if (strcmp(*fstype, "procfs") == 0)
426 		return (ECANCELED);
427 
428 	if (strcmp(*fstype, "autofs") == 0) {
429 		/*
430 		 * FreeBSD uses eg "map -hosts", whereas Linux
431 		 * expects just "-hosts".
432 		 */
433 		if (strncmp(*mntfrom, "map ", 4) == 0)
434 			*mntfrom += 4;
435 	}
436 
437 	if (strcmp(*fstype, "linsysfs") == 0) {
438 		*mntfrom = path_slash_sys;
439 		*fstype = fstype_sysfs;
440 	} else {
441 		/* For Linux msdosfs is called vfat */
442 		if (strcmp(*fstype, "msdosfs") == 0)
443 			*fstype = "vfat";
444 	}
445 	return (0);
446 }
447 
448 static void
449 _sbuf_mntoptions_helper(struct sbuf *sb, uint64_t f_flags)
450 {
451 	sbuf_cat(sb, (f_flags & MNT_RDONLY) ? "ro" : "rw");
452 #define ADD_OPTION(opt, name) \
453 	if (f_flags & (opt)) sbuf_cat(sb, "," name);
454 	ADD_OPTION(MNT_SYNCHRONOUS,	"sync");
455 	ADD_OPTION(MNT_NOEXEC,		"noexec");
456 	ADD_OPTION(MNT_NOSUID,		"nosuid");
457 	ADD_OPTION(MNT_UNION,		"union");
458 	ADD_OPTION(MNT_ASYNC,		"async");
459 	ADD_OPTION(MNT_SUIDDIR,		"suiddir");
460 	ADD_OPTION(MNT_NOSYMFOLLOW,	"nosymfollow");
461 	ADD_OPTION(MNT_NOATIME,		"noatime");
462 #undef ADD_OPTION
463 }
464 
465 /*
466  * Filler function for proc/mtab and proc/<pid>/mounts.
467  *
468  * /proc/mtab doesn't exist in Linux' procfs, but is included here so
469  * users can symlink /compat/linux/etc/mtab to /proc/mtab
470  */
471 static int
472 linprocfs_domtab(PFS_FILL_ARGS)
473 {
474 	struct nameidata nd;
475 	const char *lep, *mntto, *mntfrom, *fstype;
476 	char *dlep, *flep;
477 	size_t lep_len;
478 	int error;
479 	struct statfs *buf, *sp;
480 	size_t count;
481 
482 	/* resolve symlinks etc. in the emulation tree prefix */
483 	/*
484 	 * Ideally, this would use the current chroot rather than some
485 	 * hardcoded path.
486 	 */
487 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, linux_emul_path);
488 	flep = NULL;
489 	error = namei(&nd);
490 	lep = linux_emul_path;
491 	if (error == 0) {
492 		if (vn_fullpath(nd.ni_vp, &dlep, &flep) == 0)
493 			lep = dlep;
494 		vrele(nd.ni_vp);
495 	}
496 	lep_len = strlen(lep);
497 
498 	buf = NULL;
499 	error = kern_getfsstat(td, &buf, SIZE_T_MAX, &count,
500 	    UIO_SYSSPACE, MNT_WAIT);
501 	if (error != 0) {
502 		free(buf, M_TEMP);
503 		free(flep, M_TEMP);
504 		return (error);
505 	}
506 
507 	for (sp = buf; count > 0; sp++, count--) {
508 		error = _mtab_helper(pn, sp, &mntfrom, &mntto, &fstype);
509 		if (error != 0) {
510 			MPASS(error == ECANCELED);
511 			continue;
512 		}
513 
514 		/* determine mount point */
515 		if (strncmp(mntto, lep, lep_len) == 0 && mntto[lep_len] == '/')
516 			mntto += lep_len;
517 
518 		sbuf_printf(sb, "%s %s %s ", mntfrom, mntto, fstype);
519 		_sbuf_mntoptions_helper(sb, sp->f_flags);
520 		/* a real Linux mtab will also show NFS options */
521 		sbuf_printf(sb, " 0 0\n");
522 	}
523 
524 	free(buf, M_TEMP);
525 	free(flep, M_TEMP);
526 	return (error);
527 }
528 
529 static int
530 linprocfs_doprocmountinfo(PFS_FILL_ARGS)
531 {
532 	struct nameidata nd;
533 	const char *mntfrom, *mntto, *fstype;
534 	const char *lep;
535 	char *dlep, *flep;
536 	struct statfs *buf, *sp;
537 	size_t count, lep_len;
538 	int error;
539 
540 	/*
541 	 * Ideally, this would use the current chroot rather than some
542 	 * hardcoded path.
543 	 */
544 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, linux_emul_path);
545 	flep = NULL;
546 	error = namei(&nd);
547 	lep = linux_emul_path;
548 	if (error == 0) {
549 		if (vn_fullpath(nd.ni_vp, &dlep, &flep) == 0)
550 			lep = dlep;
551 		vrele(nd.ni_vp);
552 	}
553 	lep_len = strlen(lep);
554 
555 	buf = NULL;
556 	error = kern_getfsstat(td, &buf, SIZE_T_MAX, &count,
557 	    UIO_SYSSPACE, MNT_WAIT);
558 	if (error != 0)
559 		goto out;
560 
561 	for (sp = buf; count > 0; sp++, count--) {
562 		error = _mtab_helper(pn, sp, &mntfrom, &mntto, &fstype);
563 		if (error != 0) {
564 			MPASS(error == ECANCELED);
565 			continue;
566 		}
567 
568 		if (strncmp(mntto, lep, lep_len) == 0 && mntto[lep_len] == '/')
569 			mntto += lep_len;
570 #if 0
571 		/*
572 		 * If the prefix is a chroot, and this mountpoint is not under
573 		 * the prefix, we should skip it.  Leave it for now for
574 		 * consistency with procmtab above.
575 		 */
576 		else
577 			continue;
578 #endif
579 
580 		/*
581 		 * (1) mount id
582 		 *
583 		 * (2) parent mount id -- we don't have this cheaply, so
584 		 * provide a dummy value
585 		 *
586 		 * (3) major:minor -- ditto
587 		 *
588 		 * (4) root filesystem mount -- probably a namespaces thing
589 		 *
590 		 * (5) mountto path
591 		 */
592 		sbuf_printf(sb, "%u 0 0:0 / %s ",
593 		    sp->f_fsid.val[0] ^ sp->f_fsid.val[1], mntto);
594 		/* (6) mount options */
595 		_sbuf_mntoptions_helper(sb, sp->f_flags);
596 		/*
597 		 * (7) zero or more optional fields -- again, namespace related
598 		 *
599 		 * (8) End of variable length fields separator ("-")
600 		 *
601 		 * (9) fstype
602 		 *
603 		 * (10) mount from
604 		 *
605 		 * (11) "superblock" options -- like (6), but different
606 		 * semantics in Linux
607 		 */
608 		sbuf_printf(sb, " - %s %s %s\n", fstype, mntfrom,
609 		    (sp->f_flags & MNT_RDONLY) ? "ro" : "rw");
610 	}
611 
612 	error = 0;
613 out:
614 	free(buf, M_TEMP);
615 	free(flep, M_TEMP);
616 	return (error);
617 }
618 
619 /*
620  * Filler function for proc/partitions
621  */
622 static int
623 linprocfs_dopartitions(PFS_FILL_ARGS)
624 {
625 	struct g_class *cp;
626 	struct g_geom *gp;
627 	struct g_provider *pp;
628 	int major, minor;
629 
630 	g_topology_lock();
631 	sbuf_printf(sb, "major minor  #blocks  name rio rmerge rsect "
632 	    "ruse wio wmerge wsect wuse running use aveq\n");
633 
634 	LIST_FOREACH(cp, &g_classes, class) {
635 		if (strcmp(cp->name, "DISK") == 0 ||
636 		    strcmp(cp->name, "PART") == 0)
637 			LIST_FOREACH(gp, &cp->geom, geom) {
638 				LIST_FOREACH(pp, &gp->provider, provider) {
639 					if (linux_driver_get_major_minor(
640 					    pp->name, &major, &minor) != 0) {
641 						major = 0;
642 						minor = 0;
643 					}
644 					sbuf_printf(sb, "%d %d %lld %s "
645 					    "%d %d %d %d %d "
646 					     "%d %d %d %d %d %d\n",
647 					     major, minor,
648 					     (long long)pp->mediasize, pp->name,
649 					     0, 0, 0, 0, 0,
650 					     0, 0, 0, 0, 0, 0);
651 				}
652 			}
653 	}
654 	g_topology_unlock();
655 
656 	return (0);
657 }
658 
659 /*
660  * Filler function for proc/stat
661  *
662  * Output depends on kernel version:
663  *
664  * v2.5.40 <=
665  *   user nice system idle
666  * v2.5.41
667  *   user nice system idle iowait
668  * v2.6.11
669  *   user nice system idle iowait irq softirq steal
670  * v2.6.24
671  *   user nice system idle iowait irq softirq steal guest
672  * v2.6.33 >=
673  *   user nice system idle iowait irq softirq steal guest guest_nice
674  */
675 static int
676 linprocfs_dostat(PFS_FILL_ARGS)
677 {
678 	struct pcpu *pcpu;
679 	long cp_time[CPUSTATES];
680 	long *cp;
681 	struct timeval boottime;
682 	int i;
683 	char *zero_pad;
684 	bool has_intr = true;
685 
686 	if (linux_kernver(td) >= LINUX_KERNVER(2,6,33)) {
687 		zero_pad = " 0 0 0 0\n";
688 	} else if (linux_kernver(td) >= LINUX_KERNVER(2,6,24)) {
689 		zero_pad = " 0 0 0\n";
690 	} else if (linux_kernver(td) >= LINUX_KERNVER(2,6,11)) {
691 		zero_pad = " 0 0\n";
692 	} else if (linux_kernver(td) >= LINUX_KERNVER(2,5,41)) {
693 		has_intr = false;
694 		zero_pad = " 0\n";
695 	} else {
696 		has_intr = false;
697 		zero_pad = "\n";
698 	}
699 
700 	read_cpu_time(cp_time);
701 	getboottime(&boottime);
702 	/* Parameters common to all versions */
703 	sbuf_printf(sb, "cpu %lu %lu %lu %lu",
704 	    T2J(cp_time[CP_USER]),
705 	    T2J(cp_time[CP_NICE]),
706 	    T2J(cp_time[CP_SYS]),
707 	    T2J(cp_time[CP_IDLE]));
708 
709 	/* Print interrupt stats if available */
710 	if (has_intr) {
711 		sbuf_printf(sb, " 0 %lu", T2J(cp_time[CP_INTR]));
712 	}
713 
714 	/* Pad out remaining fields depending on version */
715 	sbuf_printf(sb, "%s", zero_pad);
716 
717 	CPU_FOREACH(i) {
718 		pcpu = pcpu_find(i);
719 		cp = pcpu->pc_cp_time;
720 		sbuf_printf(sb, "cpu%d %lu %lu %lu %lu", i,
721 		    T2J(cp[CP_USER]),
722 		    T2J(cp[CP_NICE]),
723 		    T2J(cp[CP_SYS]),
724 		    T2J(cp[CP_IDLE]));
725 
726 		if (has_intr) {
727 			sbuf_printf(sb, " 0 %lu", T2J(cp[CP_INTR]));
728 		}
729 
730 		sbuf_printf(sb, "%s", zero_pad);
731 	}
732 	sbuf_printf(sb,
733 	    "disk 0 0 0 0\n"
734 	    "page %ju %ju\n"
735 	    "swap %ju %ju\n"
736 	    "intr %ju\n"
737 	    "ctxt %ju\n"
738 	    "btime %lld\n",
739 	    (uintmax_t)VM_CNT_FETCH(v_vnodepgsin),
740 	    (uintmax_t)VM_CNT_FETCH(v_vnodepgsout),
741 	    (uintmax_t)VM_CNT_FETCH(v_swappgsin),
742 	    (uintmax_t)VM_CNT_FETCH(v_swappgsout),
743 	    (uintmax_t)VM_CNT_FETCH(v_intr),
744 	    (uintmax_t)VM_CNT_FETCH(v_swtch),
745 	    (long long)boottime.tv_sec);
746 	return (0);
747 }
748 
749 static int
750 linprocfs_doswaps(PFS_FILL_ARGS)
751 {
752 	struct xswdev xsw;
753 	uintmax_t total, used;
754 	int n;
755 	char devname[SPECNAMELEN + 1];
756 
757 	sbuf_printf(sb, "Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n");
758 	for (n = 0; ; n++) {
759 		if (swap_dev_info(n, &xsw, devname, sizeof(devname)) != 0)
760 			break;
761 		total = (uintmax_t)xsw.xsw_nblks * PAGE_SIZE / 1024;
762 		used  = (uintmax_t)xsw.xsw_used * PAGE_SIZE / 1024;
763 
764 		/*
765 		 * The space and not tab after the device name is on
766 		 * purpose.  Linux does so.
767 		 */
768 		sbuf_printf(sb, "/dev/%-34s unknown\t\t%jd\t%jd\t-1\n",
769 		    devname, total, used);
770 	}
771 	return (0);
772 }
773 
774 /*
775  * Filler function for proc/uptime
776  */
777 static int
778 linprocfs_douptime(PFS_FILL_ARGS)
779 {
780 	long cp_time[CPUSTATES];
781 	struct timeval tv;
782 
783 	getmicrouptime(&tv);
784 	read_cpu_time(cp_time);
785 	sbuf_printf(sb, "%lld.%02ld %ld.%02lu\n",
786 	    (long long)tv.tv_sec, tv.tv_usec / 10000,
787 	    T2S(cp_time[CP_IDLE] / mp_ncpus),
788 	    T2CS(cp_time[CP_IDLE] / mp_ncpus) % 100);
789 	return (0);
790 }
791 
792 /*
793  * Get OS build date
794  */
795 static void
796 linprocfs_osbuild(struct thread *td, struct sbuf *sb)
797 {
798 #if 0
799 	char osbuild[256];
800 	char *cp1, *cp2;
801 
802 	strncpy(osbuild, version, 256);
803 	osbuild[255] = '\0';
804 	cp1 = strstr(osbuild, "\n");
805 	cp2 = strstr(osbuild, ":");
806 	if (cp1 && cp2) {
807 		*cp1 = *cp2 = '\0';
808 		cp1 = strstr(osbuild, "#");
809 	} else
810 		cp1 = NULL;
811 	if (cp1)
812 		sbuf_printf(sb, "%s%s", cp1, cp2 + 1);
813 	else
814 #endif
815 		sbuf_cat(sb, "#4 Sun Dec 18 04:30:00 CET 1977");
816 }
817 
818 /*
819  * Get OS builder
820  */
821 static void
822 linprocfs_osbuilder(struct thread *td, struct sbuf *sb)
823 {
824 #if 0
825 	char builder[256];
826 	char *cp;
827 
828 	cp = strstr(version, "\n    ");
829 	if (cp) {
830 		strncpy(builder, cp + 5, 256);
831 		builder[255] = '\0';
832 		cp = strstr(builder, ":");
833 		if (cp)
834 			*cp = '\0';
835 	}
836 	if (cp)
837 		sbuf_cat(sb, builder);
838 	else
839 #endif
840 		sbuf_cat(sb, "des@freebsd.org");
841 }
842 
843 /*
844  * Filler function for proc/version
845  */
846 static int
847 linprocfs_doversion(PFS_FILL_ARGS)
848 {
849 	char osname[LINUX_MAX_UTSNAME];
850 	char osrelease[LINUX_MAX_UTSNAME];
851 
852 	linux_get_osname(td, osname);
853 	linux_get_osrelease(td, osrelease);
854 	sbuf_printf(sb, "%s version %s (", osname, osrelease);
855 	linprocfs_osbuilder(td, sb);
856 	sbuf_cat(sb, ") (gcc version " __VERSION__ ") ");
857 	linprocfs_osbuild(td, sb);
858 	sbuf_cat(sb, "\n");
859 
860 	return (0);
861 }
862 
863 /*
864  * Filler function for proc/loadavg
865  */
866 static int
867 linprocfs_doloadavg(PFS_FILL_ARGS)
868 {
869 
870 	sbuf_printf(sb,
871 	    "%d.%02d %d.%02d %d.%02d %d/%d %d\n",
872 	    (int)(averunnable.ldavg[0] / averunnable.fscale),
873 	    (int)(averunnable.ldavg[0] * 100 / averunnable.fscale % 100),
874 	    (int)(averunnable.ldavg[1] / averunnable.fscale),
875 	    (int)(averunnable.ldavg[1] * 100 / averunnable.fscale % 100),
876 	    (int)(averunnable.ldavg[2] / averunnable.fscale),
877 	    (int)(averunnable.ldavg[2] * 100 / averunnable.fscale % 100),
878 	    1,				/* number of running tasks */
879 	    nprocs,			/* number of tasks */
880 	    lastpid			/* the last pid */
881 	);
882 	return (0);
883 }
884 
885 static int
886 linprocfs_get_tty_nr(struct proc *p)
887 {
888 	struct session *sp;
889 	const char *ttyname;
890 	int error, major, minor, nr;
891 
892 	PROC_LOCK_ASSERT(p, MA_OWNED);
893 	sx_assert(&proctree_lock, SX_LOCKED);
894 
895 	if ((p->p_flag & P_CONTROLT) == 0)
896 		return (-1);
897 
898 	sp = p->p_pgrp->pg_session;
899 	if (sp == NULL)
900 		return (-1);
901 
902 	ttyname = devtoname(sp->s_ttyp->t_dev);
903 	error = linux_driver_get_major_minor(ttyname, &major, &minor);
904 	if (error != 0)
905 		return (-1);
906 
907 	nr = makedev(major, minor);
908 	return (nr);
909 }
910 
911 /*
912  * Filler function for proc/pid/stat
913  */
914 static int
915 linprocfs_doprocstat(PFS_FILL_ARGS)
916 {
917 	struct kinfo_proc kp;
918 	struct timeval boottime;
919 	char state;
920 	static int ratelimit = 0;
921 	int tty_nr;
922 	vm_offset_t startcode, startdata;
923 
924 	getboottime(&boottime);
925 	sx_slock(&proctree_lock);
926 	PROC_LOCK(p);
927 	fill_kinfo_proc(p, &kp);
928 	tty_nr = linprocfs_get_tty_nr(p);
929 	sx_sunlock(&proctree_lock);
930 	if (p->p_vmspace) {
931 	   startcode = (vm_offset_t)p->p_vmspace->vm_taddr;
932 	   startdata = (vm_offset_t)p->p_vmspace->vm_daddr;
933 	} else {
934 	   startcode = 0;
935 	   startdata = 0;
936 	}
937 	sbuf_printf(sb, "%d", p->p_pid);
938 #define PS_ADD(name, fmt, arg) sbuf_printf(sb, " " fmt, arg)
939 	PS_ADD("comm",		"(%s)",	p->p_comm);
940 	if (kp.ki_stat > sizeof(linux_state)) {
941 		state = 'R';
942 
943 		if (ratelimit == 0) {
944 			printf("linprocfs: don't know how to handle unknown FreeBSD state %d/%zd, mapping to R\n",
945 			    kp.ki_stat, sizeof(linux_state));
946 			++ratelimit;
947 		}
948 	} else
949 		state = linux_state[kp.ki_stat - 1];
950 	PS_ADD("state",		"%c",	state);
951 	PS_ADD("ppid",		"%d",	p->p_pptr ? p->p_pptr->p_pid : 0);
952 	PS_ADD("pgrp",		"%d",	p->p_pgid);
953 	PS_ADD("session",	"%d",	p->p_session->s_sid);
954 	PROC_UNLOCK(p);
955 	PS_ADD("tty",		"%d",	tty_nr);
956 	PS_ADD("tpgid",		"%d",	kp.ki_tpgid);
957 	PS_ADD("flags",		"%u",	0); /* XXX */
958 	PS_ADD("minflt",	"%lu",	kp.ki_rusage.ru_minflt);
959 	PS_ADD("cminflt",	"%lu",	kp.ki_rusage_ch.ru_minflt);
960 	PS_ADD("majflt",	"%lu",	kp.ki_rusage.ru_majflt);
961 	PS_ADD("cmajflt",	"%lu",	kp.ki_rusage_ch.ru_majflt);
962 	PS_ADD("utime",		"%ld",	TV2J(&kp.ki_rusage.ru_utime));
963 	PS_ADD("stime",		"%ld",	TV2J(&kp.ki_rusage.ru_stime));
964 	PS_ADD("cutime",	"%ld",	TV2J(&kp.ki_rusage_ch.ru_utime));
965 	PS_ADD("cstime",	"%ld",	TV2J(&kp.ki_rusage_ch.ru_stime));
966 	PS_ADD("priority",	"%d",	kp.ki_pri.pri_user);
967 	PS_ADD("nice",		"%d",	kp.ki_nice); /* 19 (nicest) to -19 */
968 	PS_ADD("0",		"%d",	0); /* removed field */
969 	PS_ADD("itrealvalue",	"%d",	0); /* XXX */
970 	PS_ADD("starttime",	"%lu",	TV2J(&kp.ki_start) - TV2J(&boottime));
971 	PS_ADD("vsize",		"%ju",	P2K((uintmax_t)kp.ki_size));
972 	PS_ADD("rss",		"%ju",	(uintmax_t)kp.ki_rssize);
973 	PS_ADD("rlim",		"%lu",	kp.ki_rusage.ru_maxrss);
974 	PS_ADD("startcode",	"%ju",	(uintmax_t)startcode);
975 	PS_ADD("endcode",	"%ju",	(uintmax_t)startdata);
976 	PS_ADD("startstack",	"%u",	0); /* XXX */
977 	PS_ADD("kstkesp",	"%u",	0); /* XXX */
978 	PS_ADD("kstkeip",	"%u",	0); /* XXX */
979 	PS_ADD("signal",	"%u",	0); /* XXX */
980 	PS_ADD("blocked",	"%u",	0); /* XXX */
981 	PS_ADD("sigignore",	"%u",	0); /* XXX */
982 	PS_ADD("sigcatch",	"%u",	0); /* XXX */
983 	PS_ADD("wchan",		"%u",	0); /* XXX */
984 	PS_ADD("nswap",		"%lu",	kp.ki_rusage.ru_nswap);
985 	PS_ADD("cnswap",	"%lu",	kp.ki_rusage_ch.ru_nswap);
986 	PS_ADD("exitsignal",	"%d",	0); /* XXX */
987 	PS_ADD("processor",	"%u",	kp.ki_lastcpu);
988 	PS_ADD("rt_priority",	"%u",	0); /* XXX */ /* >= 2.5.19 */
989 	PS_ADD("policy",	"%u",	kp.ki_pri.pri_class); /* >= 2.5.19 */
990 #undef PS_ADD
991 	sbuf_putc(sb, '\n');
992 
993 	return (0);
994 }
995 
996 /*
997  * Filler function for proc/pid/statm
998  */
999 static int
1000 linprocfs_doprocstatm(PFS_FILL_ARGS)
1001 {
1002 	struct kinfo_proc kp;
1003 	segsz_t lsize;
1004 
1005 	sx_slock(&proctree_lock);
1006 	PROC_LOCK(p);
1007 	fill_kinfo_proc(p, &kp);
1008 	PROC_UNLOCK(p);
1009 	sx_sunlock(&proctree_lock);
1010 
1011 	/*
1012 	 * See comments in linprocfs_doprocstatus() regarding the
1013 	 * computation of lsize.
1014 	 */
1015 	/* size resident share trs drs lrs dt */
1016 	sbuf_printf(sb, "%ju ", B2P((uintmax_t)kp.ki_size));
1017 	sbuf_printf(sb, "%ju ", (uintmax_t)kp.ki_rssize);
1018 	sbuf_printf(sb, "%ju ", (uintmax_t)0); /* XXX */
1019 	sbuf_printf(sb, "%ju ",	(uintmax_t)kp.ki_tsize);
1020 	sbuf_printf(sb, "%ju ", (uintmax_t)(kp.ki_dsize + kp.ki_ssize));
1021 	lsize = B2P(kp.ki_size) - kp.ki_dsize -
1022 	    kp.ki_ssize - kp.ki_tsize - 1;
1023 	sbuf_printf(sb, "%ju ", (uintmax_t)lsize);
1024 	sbuf_printf(sb, "%ju\n", (uintmax_t)0); /* XXX */
1025 
1026 	return (0);
1027 }
1028 
1029 /*
1030  * Filler function for proc/pid/status
1031  */
1032 static int
1033 linprocfs_doprocstatus(PFS_FILL_ARGS)
1034 {
1035 	struct kinfo_proc kp;
1036 	char *state;
1037 	segsz_t lsize;
1038 	struct thread *td2;
1039 	struct sigacts *ps;
1040 	l_sigset_t siglist, sigignore, sigcatch;
1041 	int i;
1042 
1043 	sx_slock(&proctree_lock);
1044 	PROC_LOCK(p);
1045 	td2 = FIRST_THREAD_IN_PROC(p);
1046 
1047 	if (P_SHOULDSTOP(p)) {
1048 		state = "T (stopped)";
1049 	} else {
1050 		switch(p->p_state) {
1051 		case PRS_NEW:
1052 			state = "I (idle)";
1053 			break;
1054 		case PRS_NORMAL:
1055 			if (p->p_flag & P_WEXIT) {
1056 				state = "X (exiting)";
1057 				break;
1058 			}
1059 			switch(TD_GET_STATE(td2)) {
1060 			case TDS_INHIBITED:
1061 				state = "S (sleeping)";
1062 				break;
1063 			case TDS_RUNQ:
1064 			case TDS_RUNNING:
1065 				state = "R (running)";
1066 				break;
1067 			default:
1068 				state = "? (unknown)";
1069 				break;
1070 			}
1071 			break;
1072 		case PRS_ZOMBIE:
1073 			state = "Z (zombie)";
1074 			break;
1075 		default:
1076 			state = "? (unknown)";
1077 			break;
1078 		}
1079 	}
1080 
1081 	fill_kinfo_proc(p, &kp);
1082 	sx_sunlock(&proctree_lock);
1083 
1084 	sbuf_printf(sb, "Name:\t%s\n",		p->p_comm); /* XXX escape */
1085 	sbuf_printf(sb, "State:\t%s\n",		state);
1086 
1087 	/*
1088 	 * Credentials
1089 	 */
1090 	sbuf_printf(sb, "Tgid:\t%d\n",		p->p_pid);
1091 	sbuf_printf(sb, "Pid:\t%d\n",		p->p_pid);
1092 	sbuf_printf(sb, "PPid:\t%d\n",		kp.ki_ppid );
1093 	sbuf_printf(sb, "TracerPid:\t%d\n",	kp.ki_tracer );
1094 	sbuf_printf(sb, "Uid:\t%d\t%d\t%d\t%d\n", p->p_ucred->cr_ruid,
1095 						p->p_ucred->cr_uid,
1096 						p->p_ucred->cr_svuid,
1097 						/* FreeBSD doesn't have fsuid */
1098 						p->p_ucred->cr_uid);
1099 	sbuf_printf(sb, "Gid:\t%d\t%d\t%d\t%d\n", p->p_ucred->cr_rgid,
1100 						p->p_ucred->cr_gid,
1101 						p->p_ucred->cr_svgid,
1102 						/* FreeBSD doesn't have fsgid */
1103 						p->p_ucred->cr_gid);
1104 	sbuf_cat(sb, "Groups:\t");
1105 	for (i = 0; i < p->p_ucred->cr_ngroups; i++)
1106 		sbuf_printf(sb, "%d ",		p->p_ucred->cr_groups[i]);
1107 	PROC_UNLOCK(p);
1108 	sbuf_putc(sb, '\n');
1109 
1110 	/*
1111 	 * Memory
1112 	 *
1113 	 * While our approximation of VmLib may not be accurate (I
1114 	 * don't know of a simple way to verify it, and I'm not sure
1115 	 * it has much meaning anyway), I believe it's good enough.
1116 	 *
1117 	 * The same code that could (I think) accurately compute VmLib
1118 	 * could also compute VmLck, but I don't really care enough to
1119 	 * implement it. Submissions are welcome.
1120 	 */
1121 	sbuf_printf(sb, "VmSize:\t%8ju kB\n",	B2K((uintmax_t)kp.ki_size));
1122 	sbuf_printf(sb, "VmLck:\t%8u kB\n",	P2K(0)); /* XXX */
1123 	sbuf_printf(sb, "VmRSS:\t%8ju kB\n",	P2K((uintmax_t)kp.ki_rssize));
1124 	sbuf_printf(sb, "VmData:\t%8ju kB\n",	P2K((uintmax_t)kp.ki_dsize));
1125 	sbuf_printf(sb, "VmStk:\t%8ju kB\n",	P2K((uintmax_t)kp.ki_ssize));
1126 	sbuf_printf(sb, "VmExe:\t%8ju kB\n",	P2K((uintmax_t)kp.ki_tsize));
1127 	lsize = B2P(kp.ki_size) - kp.ki_dsize -
1128 	    kp.ki_ssize - kp.ki_tsize - 1;
1129 	sbuf_printf(sb, "VmLib:\t%8ju kB\n",	P2K((uintmax_t)lsize));
1130 
1131 	/*
1132 	 * Signal masks
1133 	 */
1134 	PROC_LOCK(p);
1135 	bsd_to_linux_sigset(&p->p_siglist, &siglist);
1136 	ps = p->p_sigacts;
1137 	mtx_lock(&ps->ps_mtx);
1138 	bsd_to_linux_sigset(&ps->ps_sigignore, &sigignore);
1139 	bsd_to_linux_sigset(&ps->ps_sigcatch, &sigcatch);
1140 	mtx_unlock(&ps->ps_mtx);
1141 	PROC_UNLOCK(p);
1142 
1143 	sbuf_printf(sb, "SigPnd:\t%016jx\n",	siglist.__mask);
1144 	/*
1145 	 * XXX. SigBlk - target thread's signal mask, td_sigmask.
1146 	 * To implement SigBlk pseudofs should support proc/tid dir entries.
1147 	 */
1148 	sbuf_printf(sb, "SigBlk:\t%016x\n",	0);
1149 	sbuf_printf(sb, "SigIgn:\t%016jx\n",	sigignore.__mask);
1150 	sbuf_printf(sb, "SigCgt:\t%016jx\n",	sigcatch.__mask);
1151 
1152 	/*
1153 	 * Linux also prints the capability masks, but we don't have
1154 	 * capabilities yet, and when we do get them they're likely to
1155 	 * be meaningless to Linux programs, so we lie. XXX
1156 	 */
1157 	sbuf_printf(sb, "CapInh:\t%016x\n",	0);
1158 	sbuf_printf(sb, "CapPrm:\t%016x\n",	0);
1159 	sbuf_printf(sb, "CapEff:\t%016x\n",	0);
1160 
1161 	return (0);
1162 }
1163 
1164 /*
1165  * Filler function for proc/pid/cwd
1166  */
1167 static int
1168 linprocfs_doproccwd(PFS_FILL_ARGS)
1169 {
1170 	struct pwd *pwd;
1171 	char *fullpath = "unknown";
1172 	char *freepath = NULL;
1173 
1174 	pwd = pwd_hold_proc(p);
1175 	vn_fullpath(pwd->pwd_cdir, &fullpath, &freepath);
1176 	sbuf_printf(sb, "%s", fullpath);
1177 	if (freepath)
1178 		free(freepath, M_TEMP);
1179 	pwd_drop(pwd);
1180 	return (0);
1181 }
1182 
1183 /*
1184  * Filler function for proc/pid/root
1185  */
1186 static int
1187 linprocfs_doprocroot(PFS_FILL_ARGS)
1188 {
1189 	struct pwd *pwd;
1190 	struct vnode *vp;
1191 	char *fullpath = "unknown";
1192 	char *freepath = NULL;
1193 
1194 	pwd = pwd_hold_proc(p);
1195 	vp = jailed(p->p_ucred) ? pwd->pwd_jdir : pwd->pwd_rdir;
1196 	vn_fullpath(vp, &fullpath, &freepath);
1197 	sbuf_printf(sb, "%s", fullpath);
1198 	if (freepath)
1199 		free(freepath, M_TEMP);
1200 	pwd_drop(pwd);
1201 	return (0);
1202 }
1203 
1204 /*
1205  * Filler function for proc/pid/cmdline
1206  */
1207 static int
1208 linprocfs_doproccmdline(PFS_FILL_ARGS)
1209 {
1210 	int ret;
1211 
1212 	PROC_LOCK(p);
1213 	if ((ret = p_cansee(td, p)) != 0) {
1214 		PROC_UNLOCK(p);
1215 		return (ret);
1216 	}
1217 
1218 	/*
1219 	 * Mimic linux behavior and pass only processes with usermode
1220 	 * address space as valid.  Return zero silently otherwize.
1221 	 */
1222 	if (p->p_vmspace == &vmspace0) {
1223 		PROC_UNLOCK(p);
1224 		return (0);
1225 	}
1226 	if (p->p_args != NULL) {
1227 		sbuf_bcpy(sb, p->p_args->ar_args, p->p_args->ar_length);
1228 		PROC_UNLOCK(p);
1229 		return (0);
1230 	}
1231 
1232 	if ((p->p_flag & P_SYSTEM) != 0) {
1233 		PROC_UNLOCK(p);
1234 		return (0);
1235 	}
1236 
1237 	PROC_UNLOCK(p);
1238 
1239 	ret = proc_getargv(td, p, sb);
1240 	return (ret);
1241 }
1242 
1243 /*
1244  * Filler function for proc/pid/environ
1245  */
1246 static int
1247 linprocfs_doprocenviron(PFS_FILL_ARGS)
1248 {
1249 
1250 	/*
1251 	 * Mimic linux behavior and pass only processes with usermode
1252 	 * address space as valid.  Return zero silently otherwize.
1253 	 */
1254 	if (p->p_vmspace == &vmspace0)
1255 		return (0);
1256 
1257 	return (proc_getenvv(td, p, sb));
1258 }
1259 
1260 static char l32_map_str[] = "%08lx-%08lx %s%s%s%s %08lx %02x:%02x %lu%s%s\n";
1261 static char l64_map_str[] = "%016lx-%016lx %s%s%s%s %08lx %02x:%02x %lu%s%s\n";
1262 static char vdso_str[] = "      [vdso]";
1263 static char stack_str[] = "      [stack]";
1264 
1265 /*
1266  * Filler function for proc/pid/maps
1267  */
1268 static int
1269 linprocfs_doprocmaps(PFS_FILL_ARGS)
1270 {
1271 	struct vmspace *vm;
1272 	vm_map_t map;
1273 	vm_map_entry_t entry, tmp_entry;
1274 	vm_object_t obj, tobj, lobj;
1275 	vm_offset_t e_start, e_end;
1276 	vm_ooffset_t off;
1277 	vm_prot_t e_prot;
1278 	unsigned int last_timestamp;
1279 	char *name = "", *freename = NULL;
1280 	const char *l_map_str;
1281 	ino_t ino;
1282 	int error;
1283 	struct vnode *vp;
1284 	struct vattr vat;
1285 	bool private;
1286 
1287 	PROC_LOCK(p);
1288 	error = p_candebug(td, p);
1289 	PROC_UNLOCK(p);
1290 	if (error)
1291 		return (error);
1292 
1293 	if (uio->uio_rw != UIO_READ)
1294 		return (EOPNOTSUPP);
1295 
1296 	error = 0;
1297 	vm = vmspace_acquire_ref(p);
1298 	if (vm == NULL)
1299 		return (ESRCH);
1300 
1301 	if (SV_CURPROC_FLAG(SV_LP64))
1302 		l_map_str = l64_map_str;
1303 	else
1304 		l_map_str = l32_map_str;
1305 	map = &vm->vm_map;
1306 	vm_map_lock_read(map);
1307 	VM_MAP_ENTRY_FOREACH(entry, map) {
1308 		name = "";
1309 		freename = NULL;
1310 		if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
1311 			continue;
1312 		e_prot = entry->protection;
1313 		e_start = entry->start;
1314 		e_end = entry->end;
1315 		obj = entry->object.vm_object;
1316 		off = entry->offset;
1317 		for (lobj = tobj = obj; tobj != NULL;
1318 		    lobj = tobj, tobj = tobj->backing_object) {
1319 			VM_OBJECT_RLOCK(tobj);
1320 			off += lobj->backing_object_offset;
1321 			if (lobj != obj)
1322 				VM_OBJECT_RUNLOCK(lobj);
1323 		}
1324 		private = (entry->eflags & MAP_ENTRY_COW) != 0 || obj == NULL ||
1325 		    (obj->flags & OBJ_ANON) != 0;
1326 		last_timestamp = map->timestamp;
1327 		vm_map_unlock_read(map);
1328 		ino = 0;
1329 		if (lobj) {
1330 			vp = vm_object_vnode(lobj);
1331 			if (vp != NULL)
1332 				vref(vp);
1333 			if (lobj != obj)
1334 				VM_OBJECT_RUNLOCK(lobj);
1335 			VM_OBJECT_RUNLOCK(obj);
1336 			if (vp != NULL) {
1337 				vn_fullpath(vp, &name, &freename);
1338 				vn_lock(vp, LK_SHARED | LK_RETRY);
1339 				VOP_GETATTR(vp, &vat, td->td_ucred);
1340 				ino = vat.va_fileid;
1341 				vput(vp);
1342 			} else if (SV_PROC_ABI(p) == SV_ABI_LINUX) {
1343 				/*
1344 				 * sv_shared_page_base pointed out to the
1345 				 * FreeBSD sharedpage, PAGE_SIZE is a size
1346 				 * of it. The vDSO page is above.
1347 				 */
1348 				if (e_start == p->p_sysent->sv_shared_page_base +
1349 				    PAGE_SIZE)
1350 					name = vdso_str;
1351 				if (e_end == p->p_sysent->sv_usrstack)
1352 					name = stack_str;
1353 			}
1354 		}
1355 
1356 		/*
1357 		 * format:
1358 		 *  start, end, access, offset, major, minor, inode, name.
1359 		 */
1360 		error = sbuf_printf(sb, l_map_str,
1361 		    (u_long)e_start, (u_long)e_end,
1362 		    (e_prot & VM_PROT_READ)?"r":"-",
1363 		    (e_prot & VM_PROT_WRITE)?"w":"-",
1364 		    (e_prot & VM_PROT_EXECUTE)?"x":"-",
1365 		    private ? "p" : "s",
1366 		    (u_long)off,
1367 		    0,
1368 		    0,
1369 		    (u_long)ino,
1370 		    *name ? "     " : " ",
1371 		    name
1372 		    );
1373 		if (freename)
1374 			free(freename, M_TEMP);
1375 		vm_map_lock_read(map);
1376 		if (error == -1) {
1377 			error = 0;
1378 			break;
1379 		}
1380 		if (last_timestamp != map->timestamp) {
1381 			/*
1382 			 * Look again for the entry because the map was
1383 			 * modified while it was unlocked.  Specifically,
1384 			 * the entry may have been clipped, merged, or deleted.
1385 			 */
1386 			vm_map_lookup_entry(map, e_end - 1, &tmp_entry);
1387 			entry = tmp_entry;
1388 		}
1389 	}
1390 	vm_map_unlock_read(map);
1391 	vmspace_free(vm);
1392 
1393 	return (error);
1394 }
1395 
1396 /*
1397  * Filler function for proc/pid/mem
1398  */
1399 static int
1400 linprocfs_doprocmem(PFS_FILL_ARGS)
1401 {
1402 	ssize_t resid;
1403 	int error;
1404 
1405 	resid = uio->uio_resid;
1406 	error = procfs_doprocmem(PFS_FILL_ARGNAMES);
1407 
1408 	if (uio->uio_rw == UIO_READ && resid != uio->uio_resid)
1409 		return (0);
1410 
1411 	if (error == EFAULT)
1412 		error = EIO;
1413 
1414 	return (error);
1415 }
1416 
1417 static int
1418 linux_ifname(struct ifnet *ifp, char *buffer, size_t buflen)
1419 {
1420 	struct ifnet *ifscan;
1421 	int ethno;
1422 
1423 	IFNET_RLOCK_ASSERT();
1424 
1425 	/* Short-circuit non ethernet interfaces */
1426 	if (linux_use_real_ifname(ifp))
1427 		return (strlcpy(buffer, ifp->if_xname, buflen));
1428 
1429 	/* Determine the (relative) unit number for ethernet interfaces */
1430 	ethno = 0;
1431 	CK_STAILQ_FOREACH(ifscan, &V_ifnet, if_link) {
1432 		if (ifscan == ifp)
1433 			return (snprintf(buffer, buflen, "eth%d", ethno));
1434 		if (!linux_use_real_ifname(ifscan))
1435 			ethno++;
1436 	}
1437 
1438 	return (0);
1439 }
1440 
1441 /*
1442  * Filler function for proc/net/dev
1443  */
1444 static int
1445 linprocfs_donetdev(PFS_FILL_ARGS)
1446 {
1447 	char ifname[16]; /* XXX LINUX_IFNAMSIZ */
1448 	struct ifnet *ifp;
1449 
1450 	sbuf_printf(sb, "%6s|%58s|%s\n"
1451 	    "%6s|%58s|%58s\n",
1452 	    "Inter-", "   Receive", "  Transmit",
1453 	    " face",
1454 	    "bytes    packets errs drop fifo frame compressed multicast",
1455 	    "bytes    packets errs drop fifo colls carrier compressed");
1456 
1457 	CURVNET_SET(TD_TO_VNET(curthread));
1458 	IFNET_RLOCK();
1459 	CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1460 		linux_ifname(ifp, ifname, sizeof ifname);
1461 		sbuf_printf(sb, "%6.6s: ", ifname);
1462 		sbuf_printf(sb, "%7ju %7ju %4ju %4ju %4lu %5lu %10lu %9ju ",
1463 		    (uintmax_t )ifp->if_get_counter(ifp, IFCOUNTER_IBYTES),
1464 		    (uintmax_t )ifp->if_get_counter(ifp, IFCOUNTER_IPACKETS),
1465 		    (uintmax_t )ifp->if_get_counter(ifp, IFCOUNTER_IERRORS),
1466 		    (uintmax_t )ifp->if_get_counter(ifp, IFCOUNTER_IQDROPS),
1467 							/* rx_missed_errors */
1468 		    0UL,				/* rx_fifo_errors */
1469 		    0UL,				/* rx_length_errors +
1470 							 * rx_over_errors +
1471 							 * rx_crc_errors +
1472 							 * rx_frame_errors */
1473 		    0UL,				/* rx_compressed */
1474 		    (uintmax_t )ifp->if_get_counter(ifp, IFCOUNTER_IMCASTS));
1475 							/* XXX-BZ rx only? */
1476 		sbuf_printf(sb, "%8ju %7ju %4ju %4ju %4lu %5ju %7lu %10lu\n",
1477 		    (uintmax_t )ifp->if_get_counter(ifp, IFCOUNTER_OBYTES),
1478 		    (uintmax_t )ifp->if_get_counter(ifp, IFCOUNTER_OPACKETS),
1479 		    (uintmax_t )ifp->if_get_counter(ifp, IFCOUNTER_OERRORS),
1480 		    (uintmax_t )ifp->if_get_counter(ifp, IFCOUNTER_OQDROPS),
1481 		    0UL,				/* tx_fifo_errors */
1482 		    (uintmax_t )ifp->if_get_counter(ifp, IFCOUNTER_COLLISIONS),
1483 		    0UL,				/* tx_carrier_errors +
1484 							 * tx_aborted_errors +
1485 							 * tx_window_errors +
1486 							 * tx_heartbeat_errors*/
1487 		    0UL);				/* tx_compressed */
1488 	}
1489 	IFNET_RUNLOCK();
1490 	CURVNET_RESTORE();
1491 
1492 	return (0);
1493 }
1494 
1495 /*
1496  * Filler function for proc/sys/kernel/osrelease
1497  */
1498 static int
1499 linprocfs_doosrelease(PFS_FILL_ARGS)
1500 {
1501 	char osrelease[LINUX_MAX_UTSNAME];
1502 
1503 	linux_get_osrelease(td, osrelease);
1504 	sbuf_printf(sb, "%s\n", osrelease);
1505 
1506 	return (0);
1507 }
1508 
1509 /*
1510  * Filler function for proc/sys/kernel/ostype
1511  */
1512 static int
1513 linprocfs_doostype(PFS_FILL_ARGS)
1514 {
1515 	char osname[LINUX_MAX_UTSNAME];
1516 
1517 	linux_get_osname(td, osname);
1518 	sbuf_printf(sb, "%s\n", osname);
1519 
1520 	return (0);
1521 }
1522 
1523 /*
1524  * Filler function for proc/sys/kernel/version
1525  */
1526 static int
1527 linprocfs_doosbuild(PFS_FILL_ARGS)
1528 {
1529 
1530 	linprocfs_osbuild(td, sb);
1531 	sbuf_cat(sb, "\n");
1532 	return (0);
1533 }
1534 
1535 /*
1536  * Filler function for proc/sys/kernel/msgmax
1537  */
1538 static int
1539 linprocfs_domsgmax(PFS_FILL_ARGS)
1540 {
1541 
1542 	sbuf_printf(sb, "%d\n", msginfo.msgmax);
1543 	return (0);
1544 }
1545 
1546 /*
1547  * Filler function for proc/sys/kernel/msgmni
1548  */
1549 static int
1550 linprocfs_domsgmni(PFS_FILL_ARGS)
1551 {
1552 
1553 	sbuf_printf(sb, "%d\n", msginfo.msgmni);
1554 	return (0);
1555 }
1556 
1557 /*
1558  * Filler function for proc/sys/kernel/msgmnb
1559  */
1560 static int
1561 linprocfs_domsgmnb(PFS_FILL_ARGS)
1562 {
1563 
1564 	sbuf_printf(sb, "%d\n", msginfo.msgmnb);
1565 	return (0);
1566 }
1567 
1568 /*
1569  * Filler function for proc/sys/kernel/ngroups_max
1570  *
1571  * Note that in Linux it defaults to 65536, not 1023.
1572  */
1573 static int
1574 linprocfs_dongroups_max(PFS_FILL_ARGS)
1575 {
1576 
1577 	sbuf_printf(sb, "%d\n", ngroups_max);
1578 	return (0);
1579 }
1580 
1581 /*
1582  * Filler function for proc/sys/kernel/pid_max
1583  */
1584 static int
1585 linprocfs_dopid_max(PFS_FILL_ARGS)
1586 {
1587 
1588 	sbuf_printf(sb, "%i\n", PID_MAX);
1589 	return (0);
1590 }
1591 
1592 /*
1593  * Filler function for proc/sys/kernel/sem
1594  */
1595 static int
1596 linprocfs_dosem(PFS_FILL_ARGS)
1597 {
1598 
1599 	sbuf_printf(sb, "%d %d %d %d\n", seminfo.semmsl, seminfo.semmns,
1600 	    seminfo.semopm, seminfo.semmni);
1601 	return (0);
1602 }
1603 
1604 /*
1605  * Filler function for proc/sys/kernel/shmall
1606  */
1607 static int
1608 linprocfs_doshmall(PFS_FILL_ARGS)
1609 {
1610 
1611 	sbuf_printf(sb, "%lu\n", shminfo.shmall);
1612 	return (0);
1613 }
1614 
1615 /*
1616  * Filler function for proc/sys/kernel/shmmax
1617  */
1618 static int
1619 linprocfs_doshmmax(PFS_FILL_ARGS)
1620 {
1621 
1622 	sbuf_printf(sb, "%lu\n", shminfo.shmmax);
1623 	return (0);
1624 }
1625 
1626 /*
1627  * Filler function for proc/sys/kernel/shmmni
1628  */
1629 static int
1630 linprocfs_doshmmni(PFS_FILL_ARGS)
1631 {
1632 
1633 	sbuf_printf(sb, "%lu\n", shminfo.shmmni);
1634 	return (0);
1635 }
1636 
1637 /*
1638  * Filler function for proc/sys/kernel/tainted
1639  */
1640 static int
1641 linprocfs_dotainted(PFS_FILL_ARGS)
1642 {
1643 
1644 	sbuf_printf(sb, "0\n");
1645 	return (0);
1646 }
1647 
1648 /*
1649  * Filler function for proc/sys/vm/min_free_kbytes
1650  *
1651  * This mirrors the approach in illumos to return zero for reads. Effectively,
1652  * it says, no memory is kept in reserve for "atomic allocations". This class
1653  * of allocation can be used at times when a thread cannot be suspended.
1654  */
1655 static int
1656 linprocfs_dominfree(PFS_FILL_ARGS)
1657 {
1658 
1659 	sbuf_printf(sb, "%d\n", 0);
1660 	return (0);
1661 }
1662 
1663 /*
1664  * Filler function for proc/scsi/device_info
1665  */
1666 static int
1667 linprocfs_doscsidevinfo(PFS_FILL_ARGS)
1668 {
1669 
1670 	return (0);
1671 }
1672 
1673 /*
1674  * Filler function for proc/scsi/scsi
1675  */
1676 static int
1677 linprocfs_doscsiscsi(PFS_FILL_ARGS)
1678 {
1679 
1680 	return (0);
1681 }
1682 
1683 /*
1684  * Filler function for proc/devices
1685  */
1686 static int
1687 linprocfs_dodevices(PFS_FILL_ARGS)
1688 {
1689 	char *char_devices;
1690 	sbuf_printf(sb, "Character devices:\n");
1691 
1692 	char_devices = linux_get_char_devices();
1693 	sbuf_printf(sb, "%s", char_devices);
1694 	linux_free_get_char_devices(char_devices);
1695 
1696 	sbuf_printf(sb, "\nBlock devices:\n");
1697 
1698 	return (0);
1699 }
1700 
1701 /*
1702  * Filler function for proc/cmdline
1703  */
1704 static int
1705 linprocfs_docmdline(PFS_FILL_ARGS)
1706 {
1707 
1708 	sbuf_printf(sb, "BOOT_IMAGE=%s", kernelname);
1709 	sbuf_printf(sb, " ro root=302\n");
1710 	return (0);
1711 }
1712 
1713 /*
1714  * Filler function for proc/filesystems
1715  */
1716 static int
1717 linprocfs_dofilesystems(PFS_FILL_ARGS)
1718 {
1719 	struct vfsconf *vfsp;
1720 
1721 	vfsconf_slock();
1722 	TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
1723 		if (vfsp->vfc_flags & VFCF_SYNTHETIC)
1724 			sbuf_printf(sb, "nodev");
1725 		sbuf_printf(sb, "\t%s\n", vfsp->vfc_name);
1726 	}
1727 	vfsconf_sunlock();
1728 	return(0);
1729 }
1730 
1731 /*
1732  * Filler function for proc/modules
1733  */
1734 static int
1735 linprocfs_domodules(PFS_FILL_ARGS)
1736 {
1737 #if 0
1738 	struct linker_file *lf;
1739 
1740 	TAILQ_FOREACH(lf, &linker_files, link) {
1741 		sbuf_printf(sb, "%-20s%8lu%4d\n", lf->filename,
1742 		    (unsigned long)lf->size, lf->refs);
1743 	}
1744 #endif
1745 	return (0);
1746 }
1747 
1748 /*
1749  * Filler function for proc/pid/fd
1750  */
1751 static int
1752 linprocfs_dofdescfs(PFS_FILL_ARGS)
1753 {
1754 
1755 	if (p == curproc)
1756 		sbuf_printf(sb, "/dev/fd");
1757 	else
1758 		sbuf_printf(sb, "unknown");
1759 	return (0);
1760 }
1761 
1762 /*
1763  * Filler function for proc/pid/limits
1764  */
1765 static const struct linux_rlimit_ident {
1766 	const char	*desc;
1767 	const char	*unit;
1768 	unsigned int	rlim_id;
1769 } linux_rlimits_ident[] = {
1770 	{ "Max cpu time",	"seconds",	RLIMIT_CPU },
1771 	{ "Max file size", 	"bytes",	RLIMIT_FSIZE },
1772 	{ "Max data size",	"bytes", 	RLIMIT_DATA },
1773 	{ "Max stack size",	"bytes", 	RLIMIT_STACK },
1774 	{ "Max core file size",  "bytes",	RLIMIT_CORE },
1775 	{ "Max resident set",	"bytes",	RLIMIT_RSS },
1776 	{ "Max processes",	"processes",	RLIMIT_NPROC },
1777 	{ "Max open files",	"files",	RLIMIT_NOFILE },
1778 	{ "Max locked memory",	"bytes",	RLIMIT_MEMLOCK },
1779 	{ "Max address space",	"bytes",	RLIMIT_AS },
1780 	{ "Max file locks",	"locks",	LINUX_RLIMIT_LOCKS },
1781 	{ "Max pending signals", "signals",	LINUX_RLIMIT_SIGPENDING },
1782 	{ "Max msgqueue size",	"bytes",	LINUX_RLIMIT_MSGQUEUE },
1783 	{ "Max nice priority", 		"",	LINUX_RLIMIT_NICE },
1784 	{ "Max realtime priority",	"",	LINUX_RLIMIT_RTPRIO },
1785 	{ "Max realtime timeout",	"us",	LINUX_RLIMIT_RTTIME },
1786 	{ 0, 0, 0 }
1787 };
1788 
1789 static int
1790 linprocfs_doproclimits(PFS_FILL_ARGS)
1791 {
1792 	const struct linux_rlimit_ident *li;
1793 	struct plimit *limp;
1794 	struct rlimit rl;
1795 	ssize_t size;
1796 	int res, error;
1797 
1798 	error = 0;
1799 
1800 	PROC_LOCK(p);
1801 	limp = lim_hold(p->p_limit);
1802 	PROC_UNLOCK(p);
1803 	size = sizeof(res);
1804 	sbuf_printf(sb, "%-26s%-21s%-21s%-21s\n", "Limit", "Soft Limit",
1805 			"Hard Limit", "Units");
1806 	for (li = linux_rlimits_ident; li->desc != NULL; ++li) {
1807 		switch (li->rlim_id)
1808 		{
1809 		case LINUX_RLIMIT_LOCKS:
1810 			/* FALLTHROUGH */
1811 		case LINUX_RLIMIT_RTTIME:
1812 			rl.rlim_cur = RLIM_INFINITY;
1813 			break;
1814 		case LINUX_RLIMIT_SIGPENDING:
1815 			error = kernel_sysctlbyname(td,
1816 			    "kern.sigqueue.max_pending_per_proc",
1817 			    &res, &size, 0, 0, 0, 0);
1818 			if (error != 0)
1819 				goto out;
1820 			rl.rlim_cur = res;
1821 			rl.rlim_max = res;
1822 			break;
1823 		case LINUX_RLIMIT_MSGQUEUE:
1824 			error = kernel_sysctlbyname(td,
1825 			    "kern.ipc.msgmnb", &res, &size, 0, 0, 0, 0);
1826 			if (error != 0)
1827 				goto out;
1828 			rl.rlim_cur = res;
1829 			rl.rlim_max = res;
1830 			break;
1831 		case LINUX_RLIMIT_NICE:
1832 			/* FALLTHROUGH */
1833 		case LINUX_RLIMIT_RTPRIO:
1834 			rl.rlim_cur = 0;
1835 			rl.rlim_max = 0;
1836 			break;
1837 		default:
1838 			rl = limp->pl_rlimit[li->rlim_id];
1839 			break;
1840 		}
1841 		if (rl.rlim_cur == RLIM_INFINITY)
1842 			sbuf_printf(sb, "%-26s%-21s%-21s%-10s\n",
1843 			    li->desc, "unlimited", "unlimited", li->unit);
1844 		else
1845 			sbuf_printf(sb, "%-26s%-21llu%-21llu%-10s\n",
1846 			    li->desc, (unsigned long long)rl.rlim_cur,
1847 			    (unsigned long long)rl.rlim_max, li->unit);
1848 	}
1849 out:
1850 	lim_free(limp);
1851 	return (error);
1852 }
1853 
1854 /*
1855  * The point of the following two functions is to work around
1856  * an assertion in Chromium; see kern/240991 for details.
1857  */
1858 static int
1859 linprocfs_dotaskattr(PFS_ATTR_ARGS)
1860 {
1861 
1862 	vap->va_nlink = 3;
1863 	return (0);
1864 }
1865 
1866 /*
1867  * Filler function for proc/<pid>/task/.dummy
1868  */
1869 static int
1870 linprocfs_dotaskdummy(PFS_FILL_ARGS)
1871 {
1872 
1873 	return (0);
1874 }
1875 
1876 /*
1877  * Filler function for proc/sys/kernel/random/uuid
1878  */
1879 static int
1880 linprocfs_douuid(PFS_FILL_ARGS)
1881 {
1882 	struct uuid uuid;
1883 
1884 	kern_uuidgen(&uuid, 1);
1885 	sbuf_printf_uuid(sb, &uuid);
1886 	sbuf_printf(sb, "\n");
1887 	return(0);
1888 }
1889 
1890 /*
1891  * Filler function for proc/sys/kernel/random/boot_id
1892  */
1893 static int
1894 linprocfs_doboot_id(PFS_FILL_ARGS)
1895 {
1896        static bool firstboot = 1;
1897        static struct uuid uuid;
1898 
1899        if (firstboot) {
1900                kern_uuidgen(&uuid, 1);
1901                firstboot = 0;
1902        }
1903        sbuf_printf_uuid(sb, &uuid);
1904        sbuf_printf(sb, "\n");
1905        return(0);
1906 }
1907 
1908 /*
1909  * Filler function for proc/pid/auxv
1910  */
1911 static int
1912 linprocfs_doauxv(PFS_FILL_ARGS)
1913 {
1914 	struct sbuf *asb;
1915 	off_t buflen, resid;
1916 	int error;
1917 
1918 	/*
1919 	 * Mimic linux behavior and pass only processes with usermode
1920 	 * address space as valid. Return zero silently otherwise.
1921 	 */
1922 	if (p->p_vmspace == &vmspace0)
1923 		return (0);
1924 
1925 	if (uio->uio_resid == 0)
1926 		return (0);
1927 	if (uio->uio_offset < 0 || uio->uio_resid < 0)
1928 		return (EINVAL);
1929 
1930 	asb = sbuf_new_auto();
1931 	if (asb == NULL)
1932 		return (ENOMEM);
1933 	error = proc_getauxv(td, p, asb);
1934 	if (error == 0)
1935 		error = sbuf_finish(asb);
1936 
1937 	resid = sbuf_len(asb) - uio->uio_offset;
1938 	if (resid > uio->uio_resid)
1939 		buflen = uio->uio_resid;
1940 	else
1941 		buflen = resid;
1942 	if (buflen > IOSIZE_MAX)
1943 		return (EINVAL);
1944 	if (buflen > maxphys)
1945 		buflen = maxphys;
1946 	if (resid <= 0)
1947 		return (0);
1948 
1949 	if (error == 0)
1950 		error = uiomove(sbuf_data(asb) + uio->uio_offset, buflen, uio);
1951 	sbuf_delete(asb);
1952 	return (error);
1953 }
1954 
1955 /*
1956  * Filler function for proc/self/oom_score_adj
1957  */
1958 static int
1959 linprocfs_do_oom_score_adj(PFS_FILL_ARGS)
1960 {
1961 	struct linux_pemuldata *pem;
1962 	long oom;
1963 
1964 	pem = pem_find(p);
1965 	if (pem == NULL || uio == NULL)
1966 		return (EOPNOTSUPP);
1967 	if (uio->uio_rw == UIO_READ) {
1968 		sbuf_printf(sb, "%d\n", pem->oom_score_adj);
1969 	} else {
1970 		sbuf_trim(sb);
1971 		sbuf_finish(sb);
1972 		oom = strtol(sbuf_data(sb), NULL, 10);
1973 		if (oom < LINUX_OOM_SCORE_ADJ_MIN ||
1974 		    oom > LINUX_OOM_SCORE_ADJ_MAX)
1975 			return (EINVAL);
1976 		pem->oom_score_adj = oom;
1977 	}
1978 	return (0);
1979 }
1980 
1981 /*
1982  * Filler function for proc/sys/vm/max_map_count
1983  *
1984  * Maximum number of active map areas, on Linux this limits the number
1985  * of vmaps per mm struct. We don't limit mappings, return a suitable
1986  * large value.
1987  */
1988 static int
1989 linprocfs_domax_map_cnt(PFS_FILL_ARGS)
1990 {
1991 
1992 	sbuf_printf(sb, "%d\n", INT32_MAX);
1993 	return (0);
1994 }
1995 
1996 /*
1997  * Constructor
1998  */
1999 static int
2000 linprocfs_init(PFS_INIT_ARGS)
2001 {
2002 	struct pfs_node *root;
2003 	struct pfs_node *dir;
2004 	struct pfs_node *sys;
2005 
2006 	root = pi->pi_root;
2007 
2008 	/* /proc/... */
2009 	pfs_create_file(root, "cmdline", &linprocfs_docmdline,
2010 	    NULL, NULL, NULL, PFS_RD);
2011 	pfs_create_file(root, "cpuinfo", &linprocfs_docpuinfo,
2012 	    NULL, NULL, NULL, PFS_RD);
2013 	pfs_create_file(root, "devices", &linprocfs_dodevices,
2014 	    NULL, NULL, NULL, PFS_RD);
2015 	pfs_create_file(root, "filesystems", &linprocfs_dofilesystems,
2016 	    NULL, NULL, NULL, PFS_RD);
2017 	pfs_create_file(root, "loadavg", &linprocfs_doloadavg,
2018 	    NULL, NULL, NULL, PFS_RD);
2019 	pfs_create_file(root, "meminfo", &linprocfs_domeminfo,
2020 	    NULL, NULL, NULL, PFS_RD);
2021 	pfs_create_file(root, "modules", &linprocfs_domodules,
2022 	    NULL, NULL, NULL, PFS_RD);
2023 	pfs_create_file(root, "mounts", &linprocfs_domtab,
2024 	    NULL, NULL, NULL, PFS_RD);
2025 	pfs_create_file(root, "mtab", &linprocfs_domtab,
2026 	    NULL, NULL, NULL, PFS_RD);
2027 	pfs_create_file(root, "partitions", &linprocfs_dopartitions,
2028 	    NULL, NULL, NULL, PFS_RD);
2029 	pfs_create_link(root, "self", &procfs_docurproc,
2030 	    NULL, NULL, NULL, 0);
2031 	pfs_create_file(root, "stat", &linprocfs_dostat,
2032 	    NULL, NULL, NULL, PFS_RD);
2033 	pfs_create_file(root, "swaps", &linprocfs_doswaps,
2034 	    NULL, NULL, NULL, PFS_RD);
2035 	pfs_create_file(root, "uptime", &linprocfs_douptime,
2036 	    NULL, NULL, NULL, PFS_RD);
2037 	pfs_create_file(root, "version", &linprocfs_doversion,
2038 	    NULL, NULL, NULL, PFS_RD);
2039 
2040 	/* /proc/bus/... */
2041 	dir = pfs_create_dir(root, "bus", NULL, NULL, NULL, 0);
2042 	dir = pfs_create_dir(dir, "pci", NULL, NULL, NULL, 0);
2043 	dir = pfs_create_dir(dir, "devices", NULL, NULL, NULL, 0);
2044 
2045 	/* /proc/net/... */
2046 	dir = pfs_create_dir(root, "net", NULL, NULL, NULL, 0);
2047 	pfs_create_file(dir, "dev", &linprocfs_donetdev,
2048 	    NULL, NULL, NULL, PFS_RD);
2049 
2050 	/* /proc/<pid>/... */
2051 	dir = pfs_create_dir(root, "pid", NULL, NULL, NULL, PFS_PROCDEP);
2052 	pfs_create_file(dir, "cmdline", &linprocfs_doproccmdline,
2053 	    NULL, NULL, NULL, PFS_RD);
2054 	pfs_create_link(dir, "cwd", &linprocfs_doproccwd,
2055 	    NULL, NULL, NULL, 0);
2056 	pfs_create_file(dir, "environ", &linprocfs_doprocenviron,
2057 	    NULL, &procfs_candebug, NULL, PFS_RD);
2058 	pfs_create_link(dir, "exe", &procfs_doprocfile,
2059 	    NULL, &procfs_notsystem, NULL, 0);
2060 	pfs_create_file(dir, "maps", &linprocfs_doprocmaps,
2061 	    NULL, NULL, NULL, PFS_RD | PFS_AUTODRAIN);
2062 	pfs_create_file(dir, "mem", &linprocfs_doprocmem,
2063 	    procfs_attr_rw, &procfs_candebug, NULL, PFS_RDWR | PFS_RAW);
2064 	pfs_create_file(dir, "mountinfo", &linprocfs_doprocmountinfo,
2065 	    NULL, NULL, NULL, PFS_RD);
2066 	pfs_create_file(dir, "mounts", &linprocfs_domtab,
2067 	    NULL, NULL, NULL, PFS_RD);
2068 	pfs_create_link(dir, "root", &linprocfs_doprocroot,
2069 	    NULL, NULL, NULL, 0);
2070 	pfs_create_file(dir, "stat", &linprocfs_doprocstat,
2071 	    NULL, NULL, NULL, PFS_RD);
2072 	pfs_create_file(dir, "statm", &linprocfs_doprocstatm,
2073 	    NULL, NULL, NULL, PFS_RD);
2074 	pfs_create_file(dir, "status", &linprocfs_doprocstatus,
2075 	    NULL, NULL, NULL, PFS_RD);
2076 	pfs_create_link(dir, "fd", &linprocfs_dofdescfs,
2077 	    NULL, NULL, NULL, 0);
2078 	pfs_create_file(dir, "auxv", &linprocfs_doauxv,
2079 	    NULL, &procfs_candebug, NULL, PFS_RD|PFS_RAWRD);
2080 	pfs_create_file(dir, "limits", &linprocfs_doproclimits,
2081 	    NULL, NULL, NULL, PFS_RD);
2082 	pfs_create_file(dir, "oom_score_adj", &linprocfs_do_oom_score_adj,
2083 	    procfs_attr_rw, &procfs_candebug, NULL, PFS_RDWR);
2084 
2085 	/* /proc/<pid>/task/... */
2086 	dir = pfs_create_dir(dir, "task", linprocfs_dotaskattr, NULL, NULL, 0);
2087 	pfs_create_file(dir, ".dummy", &linprocfs_dotaskdummy,
2088 	    NULL, NULL, NULL, PFS_RD);
2089 
2090 	/* /proc/scsi/... */
2091 	dir = pfs_create_dir(root, "scsi", NULL, NULL, NULL, 0);
2092 	pfs_create_file(dir, "device_info", &linprocfs_doscsidevinfo,
2093 	    NULL, NULL, NULL, PFS_RD);
2094 	pfs_create_file(dir, "scsi", &linprocfs_doscsiscsi,
2095 	    NULL, NULL, NULL, PFS_RD);
2096 
2097 	/* /proc/sys/... */
2098 	sys = pfs_create_dir(root, "sys", NULL, NULL, NULL, 0);
2099 
2100 	/* /proc/sys/kernel/... */
2101 	dir = pfs_create_dir(sys, "kernel", NULL, NULL, NULL, 0);
2102 	pfs_create_file(dir, "osrelease", &linprocfs_doosrelease,
2103 	    NULL, NULL, NULL, PFS_RD);
2104 	pfs_create_file(dir, "ostype", &linprocfs_doostype,
2105 	    NULL, NULL, NULL, PFS_RD);
2106 	pfs_create_file(dir, "version", &linprocfs_doosbuild,
2107 	    NULL, NULL, NULL, PFS_RD);
2108 	pfs_create_file(dir, "msgmax", &linprocfs_domsgmax,
2109 	    NULL, NULL, NULL, PFS_RD);
2110 	pfs_create_file(dir, "msgmni", &linprocfs_domsgmni,
2111 	    NULL, NULL, NULL, PFS_RD);
2112 	pfs_create_file(dir, "msgmnb", &linprocfs_domsgmnb,
2113 	    NULL, NULL, NULL, PFS_RD);
2114 	pfs_create_file(dir, "ngroups_max", &linprocfs_dongroups_max,
2115 	    NULL, NULL, NULL, PFS_RD);
2116 	pfs_create_file(dir, "pid_max", &linprocfs_dopid_max,
2117 	    NULL, NULL, NULL, PFS_RD);
2118 	pfs_create_file(dir, "sem", &linprocfs_dosem,
2119 	    NULL, NULL, NULL, PFS_RD);
2120 	pfs_create_file(dir, "shmall", &linprocfs_doshmall,
2121 	    NULL, NULL, NULL, PFS_RD);
2122 	pfs_create_file(dir, "shmmax", &linprocfs_doshmmax,
2123 	    NULL, NULL, NULL, PFS_RD);
2124 	pfs_create_file(dir, "shmmni", &linprocfs_doshmmni,
2125 	    NULL, NULL, NULL, PFS_RD);
2126 	pfs_create_file(dir, "tainted", &linprocfs_dotainted,
2127 	    NULL, NULL, NULL, PFS_RD);
2128 
2129 	/* /proc/sys/kernel/random/... */
2130 	dir = pfs_create_dir(dir, "random", NULL, NULL, NULL, 0);
2131 	pfs_create_file(dir, "uuid", &linprocfs_douuid,
2132 	    NULL, NULL, NULL, PFS_RD);
2133 	pfs_create_file(dir, "boot_id", &linprocfs_doboot_id,
2134 	    NULL, NULL, NULL, PFS_RD);
2135 
2136 	/* /proc/sys/vm/.... */
2137 	dir = pfs_create_dir(sys, "vm", NULL, NULL, NULL, 0);
2138 	pfs_create_file(dir, "min_free_kbytes", &linprocfs_dominfree,
2139 	    NULL, NULL, NULL, PFS_RD);
2140 	pfs_create_file(dir, "max_map_count", &linprocfs_domax_map_cnt,
2141 	    NULL, NULL, NULL, PFS_RD);
2142 
2143 	return (0);
2144 }
2145 
2146 /*
2147  * Destructor
2148  */
2149 static int
2150 linprocfs_uninit(PFS_INIT_ARGS)
2151 {
2152 
2153 	/* nothing to do, pseudofs will GC */
2154 	return (0);
2155 }
2156 
2157 PSEUDOFS(linprocfs, 1, VFCF_JAIL);
2158 #if defined(__aarch64__) || defined(__amd64__)
2159 MODULE_DEPEND(linprocfs, linux_common, 1, 1, 1);
2160 #else
2161 MODULE_DEPEND(linprocfs, linux, 1, 1, 1);
2162 #endif
2163 MODULE_DEPEND(linprocfs, procfs, 1, 1, 1);
2164 MODULE_DEPEND(linprocfs, sysvmsg, 1, 1, 1);
2165 MODULE_DEPEND(linprocfs, sysvsem, 1, 1, 1);
2166 MODULE_DEPEND(linprocfs, sysvshm, 1, 1, 1);
2167