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