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