xref: /freebsd/sys/compat/linprocfs/linprocfs.c (revision 1f1e2261e341e6ca6862f82261066ef1705f0a7a)
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 		/*
1311 		 * Skip printing of the guard page of the stack region, as
1312 		 * it confuses glibc pthread_getattr_np() method, where both
1313 		 * the base address and size of the stack of the initial thread
1314 		 * are calculated.
1315 		 */
1316 		if ((entry->eflags & (MAP_ENTRY_IS_SUB_MAP | MAP_ENTRY_GUARD)) != 0)
1317 			continue;
1318 		e_prot = entry->protection;
1319 		e_start = entry->start;
1320 		e_end = entry->end;
1321 		obj = entry->object.vm_object;
1322 		off = entry->offset;
1323 		for (lobj = tobj = obj; tobj != NULL;
1324 		    lobj = tobj, tobj = tobj->backing_object) {
1325 			VM_OBJECT_RLOCK(tobj);
1326 			off += lobj->backing_object_offset;
1327 			if (lobj != obj)
1328 				VM_OBJECT_RUNLOCK(lobj);
1329 		}
1330 		private = (entry->eflags & MAP_ENTRY_COW) != 0 || obj == NULL ||
1331 		    (obj->flags & OBJ_ANON) != 0;
1332 		last_timestamp = map->timestamp;
1333 		vm_map_unlock_read(map);
1334 		ino = 0;
1335 		if (lobj) {
1336 			vp = vm_object_vnode(lobj);
1337 			if (vp != NULL)
1338 				vref(vp);
1339 			if (lobj != obj)
1340 				VM_OBJECT_RUNLOCK(lobj);
1341 			VM_OBJECT_RUNLOCK(obj);
1342 			if (vp != NULL) {
1343 				vn_fullpath(vp, &name, &freename);
1344 				vn_lock(vp, LK_SHARED | LK_RETRY);
1345 				VOP_GETATTR(vp, &vat, td->td_ucred);
1346 				ino = vat.va_fileid;
1347 				vput(vp);
1348 			} else if (SV_PROC_ABI(p) == SV_ABI_LINUX) {
1349 				/*
1350 				 * sv_shared_page_base pointed out to the
1351 				 * FreeBSD sharedpage, PAGE_SIZE is a size
1352 				 * of it. The vDSO page is above.
1353 				 */
1354 				if (e_start == p->p_sysent->sv_shared_page_base +
1355 				    PAGE_SIZE)
1356 					name = vdso_str;
1357 				if (e_end == p->p_sysent->sv_usrstack)
1358 					name = stack_str;
1359 			}
1360 		}
1361 
1362 		/*
1363 		 * format:
1364 		 *  start, end, access, offset, major, minor, inode, name.
1365 		 */
1366 		error = sbuf_printf(sb, l_map_str,
1367 		    (u_long)e_start, (u_long)e_end,
1368 		    (e_prot & VM_PROT_READ)?"r":"-",
1369 		    (e_prot & VM_PROT_WRITE)?"w":"-",
1370 		    (e_prot & VM_PROT_EXECUTE)?"x":"-",
1371 		    private ? "p" : "s",
1372 		    (u_long)off,
1373 		    0,
1374 		    0,
1375 		    (u_long)ino,
1376 		    *name ? "     " : " ",
1377 		    name
1378 		    );
1379 		if (freename)
1380 			free(freename, M_TEMP);
1381 		vm_map_lock_read(map);
1382 		if (error == -1) {
1383 			error = 0;
1384 			break;
1385 		}
1386 		if (last_timestamp != map->timestamp) {
1387 			/*
1388 			 * Look again for the entry because the map was
1389 			 * modified while it was unlocked.  Specifically,
1390 			 * the entry may have been clipped, merged, or deleted.
1391 			 */
1392 			vm_map_lookup_entry(map, e_end - 1, &tmp_entry);
1393 			entry = tmp_entry;
1394 		}
1395 	}
1396 	vm_map_unlock_read(map);
1397 	vmspace_free(vm);
1398 
1399 	return (error);
1400 }
1401 
1402 /*
1403  * Filler function for proc/pid/mem
1404  */
1405 static int
1406 linprocfs_doprocmem(PFS_FILL_ARGS)
1407 {
1408 	ssize_t resid;
1409 	int error;
1410 
1411 	resid = uio->uio_resid;
1412 	error = procfs_doprocmem(PFS_FILL_ARGNAMES);
1413 
1414 	if (uio->uio_rw == UIO_READ && resid != uio->uio_resid)
1415 		return (0);
1416 
1417 	if (error == EFAULT)
1418 		error = EIO;
1419 
1420 	return (error);
1421 }
1422 
1423 static int
1424 linux_ifname(struct ifnet *ifp, char *buffer, size_t buflen)
1425 {
1426 	struct ifnet *ifscan;
1427 	int ethno;
1428 
1429 	IFNET_RLOCK_ASSERT();
1430 
1431 	/* Short-circuit non ethernet interfaces */
1432 	if (linux_use_real_ifname(ifp))
1433 		return (strlcpy(buffer, ifp->if_xname, buflen));
1434 
1435 	/* Determine the (relative) unit number for ethernet interfaces */
1436 	ethno = 0;
1437 	CK_STAILQ_FOREACH(ifscan, &V_ifnet, if_link) {
1438 		if (ifscan == ifp)
1439 			return (snprintf(buffer, buflen, "eth%d", ethno));
1440 		if (!linux_use_real_ifname(ifscan))
1441 			ethno++;
1442 	}
1443 
1444 	return (0);
1445 }
1446 
1447 /*
1448  * Filler function for proc/net/dev
1449  */
1450 static int
1451 linprocfs_donetdev(PFS_FILL_ARGS)
1452 {
1453 	char ifname[16]; /* XXX LINUX_IFNAMSIZ */
1454 	struct ifnet *ifp;
1455 
1456 	sbuf_printf(sb, "%6s|%58s|%s\n"
1457 	    "%6s|%58s|%58s\n",
1458 	    "Inter-", "   Receive", "  Transmit",
1459 	    " face",
1460 	    "bytes    packets errs drop fifo frame compressed multicast",
1461 	    "bytes    packets errs drop fifo colls carrier compressed");
1462 
1463 	CURVNET_SET(TD_TO_VNET(curthread));
1464 	IFNET_RLOCK();
1465 	CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1466 		linux_ifname(ifp, ifname, sizeof ifname);
1467 		sbuf_printf(sb, "%6.6s: ", ifname);
1468 		sbuf_printf(sb, "%7ju %7ju %4ju %4ju %4lu %5lu %10lu %9ju ",
1469 		    (uintmax_t )ifp->if_get_counter(ifp, IFCOUNTER_IBYTES),
1470 		    (uintmax_t )ifp->if_get_counter(ifp, IFCOUNTER_IPACKETS),
1471 		    (uintmax_t )ifp->if_get_counter(ifp, IFCOUNTER_IERRORS),
1472 		    (uintmax_t )ifp->if_get_counter(ifp, IFCOUNTER_IQDROPS),
1473 							/* rx_missed_errors */
1474 		    0UL,				/* rx_fifo_errors */
1475 		    0UL,				/* rx_length_errors +
1476 							 * rx_over_errors +
1477 							 * rx_crc_errors +
1478 							 * rx_frame_errors */
1479 		    0UL,				/* rx_compressed */
1480 		    (uintmax_t )ifp->if_get_counter(ifp, IFCOUNTER_IMCASTS));
1481 							/* XXX-BZ rx only? */
1482 		sbuf_printf(sb, "%8ju %7ju %4ju %4ju %4lu %5ju %7lu %10lu\n",
1483 		    (uintmax_t )ifp->if_get_counter(ifp, IFCOUNTER_OBYTES),
1484 		    (uintmax_t )ifp->if_get_counter(ifp, IFCOUNTER_OPACKETS),
1485 		    (uintmax_t )ifp->if_get_counter(ifp, IFCOUNTER_OERRORS),
1486 		    (uintmax_t )ifp->if_get_counter(ifp, IFCOUNTER_OQDROPS),
1487 		    0UL,				/* tx_fifo_errors */
1488 		    (uintmax_t )ifp->if_get_counter(ifp, IFCOUNTER_COLLISIONS),
1489 		    0UL,				/* tx_carrier_errors +
1490 							 * tx_aborted_errors +
1491 							 * tx_window_errors +
1492 							 * tx_heartbeat_errors*/
1493 		    0UL);				/* tx_compressed */
1494 	}
1495 	IFNET_RUNLOCK();
1496 	CURVNET_RESTORE();
1497 
1498 	return (0);
1499 }
1500 
1501 /*
1502  * Filler function for proc/sys/kernel/osrelease
1503  */
1504 static int
1505 linprocfs_doosrelease(PFS_FILL_ARGS)
1506 {
1507 	char osrelease[LINUX_MAX_UTSNAME];
1508 
1509 	linux_get_osrelease(td, osrelease);
1510 	sbuf_printf(sb, "%s\n", osrelease);
1511 
1512 	return (0);
1513 }
1514 
1515 /*
1516  * Filler function for proc/sys/kernel/ostype
1517  */
1518 static int
1519 linprocfs_doostype(PFS_FILL_ARGS)
1520 {
1521 	char osname[LINUX_MAX_UTSNAME];
1522 
1523 	linux_get_osname(td, osname);
1524 	sbuf_printf(sb, "%s\n", osname);
1525 
1526 	return (0);
1527 }
1528 
1529 /*
1530  * Filler function for proc/sys/kernel/version
1531  */
1532 static int
1533 linprocfs_doosbuild(PFS_FILL_ARGS)
1534 {
1535 
1536 	linprocfs_osbuild(td, sb);
1537 	sbuf_cat(sb, "\n");
1538 	return (0);
1539 }
1540 
1541 /*
1542  * Filler function for proc/sys/kernel/msgmax
1543  */
1544 static int
1545 linprocfs_domsgmax(PFS_FILL_ARGS)
1546 {
1547 
1548 	sbuf_printf(sb, "%d\n", msginfo.msgmax);
1549 	return (0);
1550 }
1551 
1552 /*
1553  * Filler function for proc/sys/kernel/msgmni
1554  */
1555 static int
1556 linprocfs_domsgmni(PFS_FILL_ARGS)
1557 {
1558 
1559 	sbuf_printf(sb, "%d\n", msginfo.msgmni);
1560 	return (0);
1561 }
1562 
1563 /*
1564  * Filler function for proc/sys/kernel/msgmnb
1565  */
1566 static int
1567 linprocfs_domsgmnb(PFS_FILL_ARGS)
1568 {
1569 
1570 	sbuf_printf(sb, "%d\n", msginfo.msgmnb);
1571 	return (0);
1572 }
1573 
1574 /*
1575  * Filler function for proc/sys/kernel/ngroups_max
1576  *
1577  * Note that in Linux it defaults to 65536, not 1023.
1578  */
1579 static int
1580 linprocfs_dongroups_max(PFS_FILL_ARGS)
1581 {
1582 
1583 	sbuf_printf(sb, "%d\n", ngroups_max);
1584 	return (0);
1585 }
1586 
1587 /*
1588  * Filler function for proc/sys/kernel/pid_max
1589  */
1590 static int
1591 linprocfs_dopid_max(PFS_FILL_ARGS)
1592 {
1593 
1594 	sbuf_printf(sb, "%i\n", PID_MAX);
1595 	return (0);
1596 }
1597 
1598 /*
1599  * Filler function for proc/sys/kernel/sem
1600  */
1601 static int
1602 linprocfs_dosem(PFS_FILL_ARGS)
1603 {
1604 
1605 	sbuf_printf(sb, "%d %d %d %d\n", seminfo.semmsl, seminfo.semmns,
1606 	    seminfo.semopm, seminfo.semmni);
1607 	return (0);
1608 }
1609 
1610 /*
1611  * Filler function for proc/sys/kernel/shmall
1612  */
1613 static int
1614 linprocfs_doshmall(PFS_FILL_ARGS)
1615 {
1616 
1617 	sbuf_printf(sb, "%lu\n", shminfo.shmall);
1618 	return (0);
1619 }
1620 
1621 /*
1622  * Filler function for proc/sys/kernel/shmmax
1623  */
1624 static int
1625 linprocfs_doshmmax(PFS_FILL_ARGS)
1626 {
1627 
1628 	sbuf_printf(sb, "%lu\n", shminfo.shmmax);
1629 	return (0);
1630 }
1631 
1632 /*
1633  * Filler function for proc/sys/kernel/shmmni
1634  */
1635 static int
1636 linprocfs_doshmmni(PFS_FILL_ARGS)
1637 {
1638 
1639 	sbuf_printf(sb, "%lu\n", shminfo.shmmni);
1640 	return (0);
1641 }
1642 
1643 /*
1644  * Filler function for proc/sys/kernel/tainted
1645  */
1646 static int
1647 linprocfs_dotainted(PFS_FILL_ARGS)
1648 {
1649 
1650 	sbuf_printf(sb, "0\n");
1651 	return (0);
1652 }
1653 
1654 /*
1655  * Filler function for proc/sys/vm/min_free_kbytes
1656  *
1657  * This mirrors the approach in illumos to return zero for reads. Effectively,
1658  * it says, no memory is kept in reserve for "atomic allocations". This class
1659  * of allocation can be used at times when a thread cannot be suspended.
1660  */
1661 static int
1662 linprocfs_dominfree(PFS_FILL_ARGS)
1663 {
1664 
1665 	sbuf_printf(sb, "%d\n", 0);
1666 	return (0);
1667 }
1668 
1669 /*
1670  * Filler function for proc/scsi/device_info
1671  */
1672 static int
1673 linprocfs_doscsidevinfo(PFS_FILL_ARGS)
1674 {
1675 
1676 	return (0);
1677 }
1678 
1679 /*
1680  * Filler function for proc/scsi/scsi
1681  */
1682 static int
1683 linprocfs_doscsiscsi(PFS_FILL_ARGS)
1684 {
1685 
1686 	return (0);
1687 }
1688 
1689 /*
1690  * Filler function for proc/devices
1691  */
1692 static int
1693 linprocfs_dodevices(PFS_FILL_ARGS)
1694 {
1695 	char *char_devices;
1696 	sbuf_printf(sb, "Character devices:\n");
1697 
1698 	char_devices = linux_get_char_devices();
1699 	sbuf_printf(sb, "%s", char_devices);
1700 	linux_free_get_char_devices(char_devices);
1701 
1702 	sbuf_printf(sb, "\nBlock devices:\n");
1703 
1704 	return (0);
1705 }
1706 
1707 /*
1708  * Filler function for proc/cmdline
1709  */
1710 static int
1711 linprocfs_docmdline(PFS_FILL_ARGS)
1712 {
1713 
1714 	sbuf_printf(sb, "BOOT_IMAGE=%s", kernelname);
1715 	sbuf_printf(sb, " ro root=302\n");
1716 	return (0);
1717 }
1718 
1719 /*
1720  * Filler function for proc/filesystems
1721  */
1722 static int
1723 linprocfs_dofilesystems(PFS_FILL_ARGS)
1724 {
1725 	struct vfsconf *vfsp;
1726 
1727 	vfsconf_slock();
1728 	TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
1729 		if (vfsp->vfc_flags & VFCF_SYNTHETIC)
1730 			sbuf_printf(sb, "nodev");
1731 		sbuf_printf(sb, "\t%s\n", vfsp->vfc_name);
1732 	}
1733 	vfsconf_sunlock();
1734 	return(0);
1735 }
1736 
1737 /*
1738  * Filler function for proc/modules
1739  */
1740 static int
1741 linprocfs_domodules(PFS_FILL_ARGS)
1742 {
1743 #if 0
1744 	struct linker_file *lf;
1745 
1746 	TAILQ_FOREACH(lf, &linker_files, link) {
1747 		sbuf_printf(sb, "%-20s%8lu%4d\n", lf->filename,
1748 		    (unsigned long)lf->size, lf->refs);
1749 	}
1750 #endif
1751 	return (0);
1752 }
1753 
1754 /*
1755  * Filler function for proc/pid/fd
1756  */
1757 static int
1758 linprocfs_dofdescfs(PFS_FILL_ARGS)
1759 {
1760 
1761 	if (p == curproc)
1762 		sbuf_printf(sb, "/dev/fd");
1763 	else
1764 		sbuf_printf(sb, "unknown");
1765 	return (0);
1766 }
1767 
1768 /*
1769  * Filler function for proc/pid/limits
1770  */
1771 static const struct linux_rlimit_ident {
1772 	const char	*desc;
1773 	const char	*unit;
1774 	unsigned int	rlim_id;
1775 } linux_rlimits_ident[] = {
1776 	{ "Max cpu time",	"seconds",	RLIMIT_CPU },
1777 	{ "Max file size", 	"bytes",	RLIMIT_FSIZE },
1778 	{ "Max data size",	"bytes", 	RLIMIT_DATA },
1779 	{ "Max stack size",	"bytes", 	RLIMIT_STACK },
1780 	{ "Max core file size",  "bytes",	RLIMIT_CORE },
1781 	{ "Max resident set",	"bytes",	RLIMIT_RSS },
1782 	{ "Max processes",	"processes",	RLIMIT_NPROC },
1783 	{ "Max open files",	"files",	RLIMIT_NOFILE },
1784 	{ "Max locked memory",	"bytes",	RLIMIT_MEMLOCK },
1785 	{ "Max address space",	"bytes",	RLIMIT_AS },
1786 	{ "Max file locks",	"locks",	LINUX_RLIMIT_LOCKS },
1787 	{ "Max pending signals", "signals",	LINUX_RLIMIT_SIGPENDING },
1788 	{ "Max msgqueue size",	"bytes",	LINUX_RLIMIT_MSGQUEUE },
1789 	{ "Max nice priority", 		"",	LINUX_RLIMIT_NICE },
1790 	{ "Max realtime priority",	"",	LINUX_RLIMIT_RTPRIO },
1791 	{ "Max realtime timeout",	"us",	LINUX_RLIMIT_RTTIME },
1792 	{ 0, 0, 0 }
1793 };
1794 
1795 static int
1796 linprocfs_doproclimits(PFS_FILL_ARGS)
1797 {
1798 	const struct linux_rlimit_ident *li;
1799 	struct plimit *limp;
1800 	struct rlimit rl;
1801 	ssize_t size;
1802 	int res, error;
1803 
1804 	error = 0;
1805 
1806 	PROC_LOCK(p);
1807 	limp = lim_hold(p->p_limit);
1808 	PROC_UNLOCK(p);
1809 	size = sizeof(res);
1810 	sbuf_printf(sb, "%-26s%-21s%-21s%-21s\n", "Limit", "Soft Limit",
1811 			"Hard Limit", "Units");
1812 	for (li = linux_rlimits_ident; li->desc != NULL; ++li) {
1813 		switch (li->rlim_id)
1814 		{
1815 		case LINUX_RLIMIT_LOCKS:
1816 			/* FALLTHROUGH */
1817 		case LINUX_RLIMIT_RTTIME:
1818 			rl.rlim_cur = RLIM_INFINITY;
1819 			break;
1820 		case LINUX_RLIMIT_SIGPENDING:
1821 			error = kernel_sysctlbyname(td,
1822 			    "kern.sigqueue.max_pending_per_proc",
1823 			    &res, &size, 0, 0, 0, 0);
1824 			if (error != 0)
1825 				goto out;
1826 			rl.rlim_cur = res;
1827 			rl.rlim_max = res;
1828 			break;
1829 		case LINUX_RLIMIT_MSGQUEUE:
1830 			error = kernel_sysctlbyname(td,
1831 			    "kern.ipc.msgmnb", &res, &size, 0, 0, 0, 0);
1832 			if (error != 0)
1833 				goto out;
1834 			rl.rlim_cur = res;
1835 			rl.rlim_max = res;
1836 			break;
1837 		case LINUX_RLIMIT_NICE:
1838 			/* FALLTHROUGH */
1839 		case LINUX_RLIMIT_RTPRIO:
1840 			rl.rlim_cur = 0;
1841 			rl.rlim_max = 0;
1842 			break;
1843 		default:
1844 			rl = limp->pl_rlimit[li->rlim_id];
1845 			break;
1846 		}
1847 		if (rl.rlim_cur == RLIM_INFINITY)
1848 			sbuf_printf(sb, "%-26s%-21s%-21s%-10s\n",
1849 			    li->desc, "unlimited", "unlimited", li->unit);
1850 		else
1851 			sbuf_printf(sb, "%-26s%-21llu%-21llu%-10s\n",
1852 			    li->desc, (unsigned long long)rl.rlim_cur,
1853 			    (unsigned long long)rl.rlim_max, li->unit);
1854 	}
1855 out:
1856 	lim_free(limp);
1857 	return (error);
1858 }
1859 
1860 /*
1861  * The point of the following two functions is to work around
1862  * an assertion in Chromium; see kern/240991 for details.
1863  */
1864 static int
1865 linprocfs_dotaskattr(PFS_ATTR_ARGS)
1866 {
1867 
1868 	vap->va_nlink = 3;
1869 	return (0);
1870 }
1871 
1872 /*
1873  * Filler function for proc/<pid>/task/.dummy
1874  */
1875 static int
1876 linprocfs_dotaskdummy(PFS_FILL_ARGS)
1877 {
1878 
1879 	return (0);
1880 }
1881 
1882 /*
1883  * Filler function for proc/sys/kernel/random/uuid
1884  */
1885 static int
1886 linprocfs_douuid(PFS_FILL_ARGS)
1887 {
1888 	struct uuid uuid;
1889 
1890 	kern_uuidgen(&uuid, 1);
1891 	sbuf_printf_uuid(sb, &uuid);
1892 	sbuf_printf(sb, "\n");
1893 	return(0);
1894 }
1895 
1896 /*
1897  * Filler function for proc/sys/kernel/random/boot_id
1898  */
1899 static int
1900 linprocfs_doboot_id(PFS_FILL_ARGS)
1901 {
1902        static bool firstboot = 1;
1903        static struct uuid uuid;
1904 
1905        if (firstboot) {
1906                kern_uuidgen(&uuid, 1);
1907                firstboot = 0;
1908        }
1909        sbuf_printf_uuid(sb, &uuid);
1910        sbuf_printf(sb, "\n");
1911        return(0);
1912 }
1913 
1914 /*
1915  * Filler function for proc/pid/auxv
1916  */
1917 static int
1918 linprocfs_doauxv(PFS_FILL_ARGS)
1919 {
1920 	struct sbuf *asb;
1921 	off_t buflen, resid;
1922 	int error;
1923 
1924 	/*
1925 	 * Mimic linux behavior and pass only processes with usermode
1926 	 * address space as valid. Return zero silently otherwise.
1927 	 */
1928 	if (p->p_vmspace == &vmspace0)
1929 		return (0);
1930 
1931 	if (uio->uio_resid == 0)
1932 		return (0);
1933 	if (uio->uio_offset < 0 || uio->uio_resid < 0)
1934 		return (EINVAL);
1935 
1936 	asb = sbuf_new_auto();
1937 	if (asb == NULL)
1938 		return (ENOMEM);
1939 	error = proc_getauxv(td, p, asb);
1940 	if (error == 0)
1941 		error = sbuf_finish(asb);
1942 
1943 	resid = sbuf_len(asb) - uio->uio_offset;
1944 	if (resid > uio->uio_resid)
1945 		buflen = uio->uio_resid;
1946 	else
1947 		buflen = resid;
1948 	if (buflen > IOSIZE_MAX)
1949 		return (EINVAL);
1950 	if (buflen > maxphys)
1951 		buflen = maxphys;
1952 	if (resid <= 0)
1953 		return (0);
1954 
1955 	if (error == 0)
1956 		error = uiomove(sbuf_data(asb) + uio->uio_offset, buflen, uio);
1957 	sbuf_delete(asb);
1958 	return (error);
1959 }
1960 
1961 /*
1962  * Filler function for proc/self/oom_score_adj
1963  */
1964 static int
1965 linprocfs_do_oom_score_adj(PFS_FILL_ARGS)
1966 {
1967 	struct linux_pemuldata *pem;
1968 	long oom;
1969 
1970 	pem = pem_find(p);
1971 	if (pem == NULL || uio == NULL)
1972 		return (EOPNOTSUPP);
1973 	if (uio->uio_rw == UIO_READ) {
1974 		sbuf_printf(sb, "%d\n", pem->oom_score_adj);
1975 	} else {
1976 		sbuf_trim(sb);
1977 		sbuf_finish(sb);
1978 		oom = strtol(sbuf_data(sb), NULL, 10);
1979 		if (oom < LINUX_OOM_SCORE_ADJ_MIN ||
1980 		    oom > LINUX_OOM_SCORE_ADJ_MAX)
1981 			return (EINVAL);
1982 		pem->oom_score_adj = oom;
1983 	}
1984 	return (0);
1985 }
1986 
1987 /*
1988  * Filler function for proc/sys/vm/max_map_count
1989  *
1990  * Maximum number of active map areas, on Linux this limits the number
1991  * of vmaps per mm struct. We don't limit mappings, return a suitable
1992  * large value.
1993  */
1994 static int
1995 linprocfs_domax_map_cnt(PFS_FILL_ARGS)
1996 {
1997 
1998 	sbuf_printf(sb, "%d\n", INT32_MAX);
1999 	return (0);
2000 }
2001 
2002 /*
2003  * Constructor
2004  */
2005 static int
2006 linprocfs_init(PFS_INIT_ARGS)
2007 {
2008 	struct pfs_node *root;
2009 	struct pfs_node *dir;
2010 	struct pfs_node *sys;
2011 
2012 	root = pi->pi_root;
2013 
2014 	/* /proc/... */
2015 	pfs_create_file(root, "cmdline", &linprocfs_docmdline,
2016 	    NULL, NULL, NULL, PFS_RD);
2017 	pfs_create_file(root, "cpuinfo", &linprocfs_docpuinfo,
2018 	    NULL, NULL, NULL, PFS_RD);
2019 	pfs_create_file(root, "devices", &linprocfs_dodevices,
2020 	    NULL, NULL, NULL, PFS_RD);
2021 	pfs_create_file(root, "filesystems", &linprocfs_dofilesystems,
2022 	    NULL, NULL, NULL, PFS_RD);
2023 	pfs_create_file(root, "loadavg", &linprocfs_doloadavg,
2024 	    NULL, NULL, NULL, PFS_RD);
2025 	pfs_create_file(root, "meminfo", &linprocfs_domeminfo,
2026 	    NULL, NULL, NULL, PFS_RD);
2027 	pfs_create_file(root, "modules", &linprocfs_domodules,
2028 	    NULL, NULL, NULL, PFS_RD);
2029 	pfs_create_file(root, "mounts", &linprocfs_domtab,
2030 	    NULL, NULL, NULL, PFS_RD);
2031 	pfs_create_file(root, "mtab", &linprocfs_domtab,
2032 	    NULL, NULL, NULL, PFS_RD);
2033 	pfs_create_file(root, "partitions", &linprocfs_dopartitions,
2034 	    NULL, NULL, NULL, PFS_RD);
2035 	pfs_create_link(root, "self", &procfs_docurproc,
2036 	    NULL, NULL, NULL, 0);
2037 	pfs_create_file(root, "stat", &linprocfs_dostat,
2038 	    NULL, NULL, NULL, PFS_RD);
2039 	pfs_create_file(root, "swaps", &linprocfs_doswaps,
2040 	    NULL, NULL, NULL, PFS_RD);
2041 	pfs_create_file(root, "uptime", &linprocfs_douptime,
2042 	    NULL, NULL, NULL, PFS_RD);
2043 	pfs_create_file(root, "version", &linprocfs_doversion,
2044 	    NULL, NULL, NULL, PFS_RD);
2045 
2046 	/* /proc/bus/... */
2047 	dir = pfs_create_dir(root, "bus", NULL, NULL, NULL, 0);
2048 	dir = pfs_create_dir(dir, "pci", NULL, NULL, NULL, 0);
2049 	dir = pfs_create_dir(dir, "devices", NULL, NULL, NULL, 0);
2050 
2051 	/* /proc/net/... */
2052 	dir = pfs_create_dir(root, "net", NULL, NULL, NULL, 0);
2053 	pfs_create_file(dir, "dev", &linprocfs_donetdev,
2054 	    NULL, NULL, NULL, PFS_RD);
2055 
2056 	/* /proc/<pid>/... */
2057 	dir = pfs_create_dir(root, "pid", NULL, NULL, NULL, PFS_PROCDEP);
2058 	pfs_create_file(dir, "cmdline", &linprocfs_doproccmdline,
2059 	    NULL, NULL, NULL, PFS_RD);
2060 	pfs_create_link(dir, "cwd", &linprocfs_doproccwd,
2061 	    NULL, NULL, NULL, 0);
2062 	pfs_create_file(dir, "environ", &linprocfs_doprocenviron,
2063 	    NULL, &procfs_candebug, NULL, PFS_RD);
2064 	pfs_create_link(dir, "exe", &procfs_doprocfile,
2065 	    NULL, &procfs_notsystem, NULL, 0);
2066 	pfs_create_file(dir, "maps", &linprocfs_doprocmaps,
2067 	    NULL, NULL, NULL, PFS_RD | PFS_AUTODRAIN);
2068 	pfs_create_file(dir, "mem", &linprocfs_doprocmem,
2069 	    procfs_attr_rw, &procfs_candebug, NULL, PFS_RDWR | PFS_RAW);
2070 	pfs_create_file(dir, "mountinfo", &linprocfs_doprocmountinfo,
2071 	    NULL, NULL, NULL, PFS_RD);
2072 	pfs_create_file(dir, "mounts", &linprocfs_domtab,
2073 	    NULL, NULL, NULL, PFS_RD);
2074 	pfs_create_link(dir, "root", &linprocfs_doprocroot,
2075 	    NULL, NULL, NULL, 0);
2076 	pfs_create_file(dir, "stat", &linprocfs_doprocstat,
2077 	    NULL, NULL, NULL, PFS_RD);
2078 	pfs_create_file(dir, "statm", &linprocfs_doprocstatm,
2079 	    NULL, NULL, NULL, PFS_RD);
2080 	pfs_create_file(dir, "status", &linprocfs_doprocstatus,
2081 	    NULL, NULL, NULL, PFS_RD);
2082 	pfs_create_link(dir, "fd", &linprocfs_dofdescfs,
2083 	    NULL, NULL, NULL, 0);
2084 	pfs_create_file(dir, "auxv", &linprocfs_doauxv,
2085 	    NULL, &procfs_candebug, NULL, PFS_RD|PFS_RAWRD);
2086 	pfs_create_file(dir, "limits", &linprocfs_doproclimits,
2087 	    NULL, NULL, NULL, PFS_RD);
2088 	pfs_create_file(dir, "oom_score_adj", &linprocfs_do_oom_score_adj,
2089 	    procfs_attr_rw, &procfs_candebug, NULL, PFS_RDWR);
2090 
2091 	/* /proc/<pid>/task/... */
2092 	dir = pfs_create_dir(dir, "task", linprocfs_dotaskattr, NULL, NULL, 0);
2093 	pfs_create_file(dir, ".dummy", &linprocfs_dotaskdummy,
2094 	    NULL, NULL, NULL, PFS_RD);
2095 
2096 	/* /proc/scsi/... */
2097 	dir = pfs_create_dir(root, "scsi", NULL, NULL, NULL, 0);
2098 	pfs_create_file(dir, "device_info", &linprocfs_doscsidevinfo,
2099 	    NULL, NULL, NULL, PFS_RD);
2100 	pfs_create_file(dir, "scsi", &linprocfs_doscsiscsi,
2101 	    NULL, NULL, NULL, PFS_RD);
2102 
2103 	/* /proc/sys/... */
2104 	sys = pfs_create_dir(root, "sys", NULL, NULL, NULL, 0);
2105 
2106 	/* /proc/sys/kernel/... */
2107 	dir = pfs_create_dir(sys, "kernel", NULL, NULL, NULL, 0);
2108 	pfs_create_file(dir, "osrelease", &linprocfs_doosrelease,
2109 	    NULL, NULL, NULL, PFS_RD);
2110 	pfs_create_file(dir, "ostype", &linprocfs_doostype,
2111 	    NULL, NULL, NULL, PFS_RD);
2112 	pfs_create_file(dir, "version", &linprocfs_doosbuild,
2113 	    NULL, NULL, NULL, PFS_RD);
2114 	pfs_create_file(dir, "msgmax", &linprocfs_domsgmax,
2115 	    NULL, NULL, NULL, PFS_RD);
2116 	pfs_create_file(dir, "msgmni", &linprocfs_domsgmni,
2117 	    NULL, NULL, NULL, PFS_RD);
2118 	pfs_create_file(dir, "msgmnb", &linprocfs_domsgmnb,
2119 	    NULL, NULL, NULL, PFS_RD);
2120 	pfs_create_file(dir, "ngroups_max", &linprocfs_dongroups_max,
2121 	    NULL, NULL, NULL, PFS_RD);
2122 	pfs_create_file(dir, "pid_max", &linprocfs_dopid_max,
2123 	    NULL, NULL, NULL, PFS_RD);
2124 	pfs_create_file(dir, "sem", &linprocfs_dosem,
2125 	    NULL, NULL, NULL, PFS_RD);
2126 	pfs_create_file(dir, "shmall", &linprocfs_doshmall,
2127 	    NULL, NULL, NULL, PFS_RD);
2128 	pfs_create_file(dir, "shmmax", &linprocfs_doshmmax,
2129 	    NULL, NULL, NULL, PFS_RD);
2130 	pfs_create_file(dir, "shmmni", &linprocfs_doshmmni,
2131 	    NULL, NULL, NULL, PFS_RD);
2132 	pfs_create_file(dir, "tainted", &linprocfs_dotainted,
2133 	    NULL, NULL, NULL, PFS_RD);
2134 
2135 	/* /proc/sys/kernel/random/... */
2136 	dir = pfs_create_dir(dir, "random", NULL, NULL, NULL, 0);
2137 	pfs_create_file(dir, "uuid", &linprocfs_douuid,
2138 	    NULL, NULL, NULL, PFS_RD);
2139 	pfs_create_file(dir, "boot_id", &linprocfs_doboot_id,
2140 	    NULL, NULL, NULL, PFS_RD);
2141 
2142 	/* /proc/sys/vm/.... */
2143 	dir = pfs_create_dir(sys, "vm", NULL, NULL, NULL, 0);
2144 	pfs_create_file(dir, "min_free_kbytes", &linprocfs_dominfree,
2145 	    NULL, NULL, NULL, PFS_RD);
2146 	pfs_create_file(dir, "max_map_count", &linprocfs_domax_map_cnt,
2147 	    NULL, NULL, NULL, PFS_RD);
2148 
2149 	return (0);
2150 }
2151 
2152 /*
2153  * Destructor
2154  */
2155 static int
2156 linprocfs_uninit(PFS_INIT_ARGS)
2157 {
2158 
2159 	/* nothing to do, pseudofs will GC */
2160 	return (0);
2161 }
2162 
2163 PSEUDOFS(linprocfs, 1, VFCF_JAIL);
2164 #if defined(__aarch64__) || defined(__amd64__)
2165 MODULE_DEPEND(linprocfs, linux_common, 1, 1, 1);
2166 #else
2167 MODULE_DEPEND(linprocfs, linux, 1, 1, 1);
2168 #endif
2169 MODULE_DEPEND(linprocfs, procfs, 1, 1, 1);
2170 MODULE_DEPEND(linprocfs, sysvmsg, 1, 1, 1);
2171 MODULE_DEPEND(linprocfs, sysvsem, 1, 1, 1);
2172 MODULE_DEPEND(linprocfs, sysvshm, 1, 1, 1);
2173