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