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