xref: /freebsd/sys/compat/linprocfs/linprocfs.c (revision bb3c7a5440062fda70085eab2a7a62654b4dde14)
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, "linsysfs") == 0) {
466 			sbuf_printf(sb, "/sys %s sysfs %s", mntto,
467 			    sp->f_flags & MNT_RDONLY ? "ro" : "rw");
468 		} else {
469 			/* For Linux msdosfs is called vfat */
470 			if (strcmp(fstype, "msdosfs") == 0)
471 				fstype = "vfat";
472 			sbuf_printf(sb, "%s %s %s %s", mntfrom, mntto, fstype,
473 			    sp->f_flags & MNT_RDONLY ? "ro" : "rw");
474 		}
475 #define ADD_OPTION(opt, name) \
476 	if (sp->f_flags & (opt)) sbuf_printf(sb, "," name);
477 		ADD_OPTION(MNT_SYNCHRONOUS,	"sync");
478 		ADD_OPTION(MNT_NOEXEC,		"noexec");
479 		ADD_OPTION(MNT_NOSUID,		"nosuid");
480 		ADD_OPTION(MNT_UNION,		"union");
481 		ADD_OPTION(MNT_ASYNC,		"async");
482 		ADD_OPTION(MNT_SUIDDIR,		"suiddir");
483 		ADD_OPTION(MNT_NOSYMFOLLOW,	"nosymfollow");
484 		ADD_OPTION(MNT_NOATIME,		"noatime");
485 #undef ADD_OPTION
486 		/* a real Linux mtab will also show NFS options */
487 		sbuf_printf(sb, " 0 0\n");
488 	}
489 
490 	free(buf, M_TEMP);
491 	free(flep, M_TEMP);
492 	return (error);
493 }
494 
495 /*
496  * Filler function for proc/partitions
497  */
498 static int
499 linprocfs_dopartitions(PFS_FILL_ARGS)
500 {
501 	struct g_class *cp;
502 	struct g_geom *gp;
503 	struct g_provider *pp;
504 	int major, minor;
505 
506 	g_topology_lock();
507 	sbuf_printf(sb, "major minor  #blocks  name rio rmerge rsect "
508 	    "ruse wio wmerge wsect wuse running use aveq\n");
509 
510 	LIST_FOREACH(cp, &g_classes, class) {
511 		if (strcmp(cp->name, "DISK") == 0 ||
512 		    strcmp(cp->name, "PART") == 0)
513 			LIST_FOREACH(gp, &cp->geom, geom) {
514 				LIST_FOREACH(pp, &gp->provider, provider) {
515 					if (linux_driver_get_major_minor(
516 					    pp->name, &major, &minor) != 0) {
517 						major = 0;
518 						minor = 0;
519 					}
520 					sbuf_printf(sb, "%d %d %lld %s "
521 					    "%d %d %d %d %d "
522 					     "%d %d %d %d %d %d\n",
523 					     major, minor,
524 					     (long long)pp->mediasize, pp->name,
525 					     0, 0, 0, 0, 0,
526 					     0, 0, 0, 0, 0, 0);
527 				}
528 			}
529 	}
530 	g_topology_unlock();
531 
532 	return (0);
533 }
534 
535 /*
536  * Filler function for proc/stat
537  *
538  * Output depends on kernel version:
539  *
540  * v2.5.40 <=
541  *   user nice system idle
542  * v2.5.41
543  *   user nice system idle iowait
544  * v2.6.11
545  *   user nice system idle iowait irq softirq steal
546  * v2.6.24
547  *   user nice system idle iowait irq softirq steal guest
548  * v2.6.33 >=
549  *   user nice system idle iowait irq softirq steal guest guest_nice
550  */
551 static int
552 linprocfs_dostat(PFS_FILL_ARGS)
553 {
554 	struct pcpu *pcpu;
555 	long cp_time[CPUSTATES];
556 	long *cp;
557 	struct timeval boottime;
558 	int i;
559 	char *zero_pad;
560 	bool has_intr = true;
561 
562 	if (linux_kernver(td) >= LINUX_KERNVER(2,6,33)) {
563 		zero_pad = " 0 0 0 0\n";
564 	} else if (linux_kernver(td) >= LINUX_KERNVER(2,6,24)) {
565 		zero_pad = " 0 0 0\n";
566 	} else if (linux_kernver(td) >= LINUX_KERNVER(2,6,11)) {
567 		zero_pad = " 0 0\n";
568 	} else if (linux_kernver(td) >= LINUX_KERNVER(2,5,41)) {
569 		has_intr = false;
570 		zero_pad = " 0\n";
571 	} else {
572 		has_intr = false;
573 		zero_pad = "\n";
574 	}
575 
576 	read_cpu_time(cp_time);
577 	getboottime(&boottime);
578 	/* Parameters common to all versions */
579 	sbuf_printf(sb, "cpu %lu %lu %lu %lu",
580 	    T2J(cp_time[CP_USER]),
581 	    T2J(cp_time[CP_NICE]),
582 	    T2J(cp_time[CP_SYS]),
583 	    T2J(cp_time[CP_IDLE]));
584 
585 	/* Print interrupt stats if available */
586 	if (has_intr) {
587 		sbuf_printf(sb, " 0 %lu", T2J(cp_time[CP_INTR]));
588 	}
589 
590 	/* Pad out remaining fields depending on version */
591 	sbuf_printf(sb, "%s", zero_pad);
592 
593 	CPU_FOREACH(i) {
594 		pcpu = pcpu_find(i);
595 		cp = pcpu->pc_cp_time;
596 		sbuf_printf(sb, "cpu%d %lu %lu %lu %lu", i,
597 		    T2J(cp[CP_USER]),
598 		    T2J(cp[CP_NICE]),
599 		    T2J(cp[CP_SYS]),
600 		    T2J(cp[CP_IDLE]));
601 
602 		if (has_intr) {
603 			sbuf_printf(sb, " 0 %lu", T2J(cp[CP_INTR]));
604 		}
605 
606 		sbuf_printf(sb, "%s", zero_pad);
607 	}
608 	sbuf_printf(sb,
609 	    "disk 0 0 0 0\n"
610 	    "page %ju %ju\n"
611 	    "swap %ju %ju\n"
612 	    "intr %ju\n"
613 	    "ctxt %ju\n"
614 	    "btime %lld\n",
615 	    (uintmax_t)VM_CNT_FETCH(v_vnodepgsin),
616 	    (uintmax_t)VM_CNT_FETCH(v_vnodepgsout),
617 	    (uintmax_t)VM_CNT_FETCH(v_swappgsin),
618 	    (uintmax_t)VM_CNT_FETCH(v_swappgsout),
619 	    (uintmax_t)VM_CNT_FETCH(v_intr),
620 	    (uintmax_t)VM_CNT_FETCH(v_swtch),
621 	    (long long)boottime.tv_sec);
622 	return (0);
623 }
624 
625 static int
626 linprocfs_doswaps(PFS_FILL_ARGS)
627 {
628 	struct xswdev xsw;
629 	uintmax_t total, used;
630 	int n;
631 	char devname[SPECNAMELEN + 1];
632 
633 	sbuf_printf(sb, "Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n");
634 	for (n = 0; ; n++) {
635 		if (swap_dev_info(n, &xsw, devname, sizeof(devname)) != 0)
636 			break;
637 		total = (uintmax_t)xsw.xsw_nblks * PAGE_SIZE / 1024;
638 		used  = (uintmax_t)xsw.xsw_used * PAGE_SIZE / 1024;
639 
640 		/*
641 		 * The space and not tab after the device name is on
642 		 * purpose.  Linux does so.
643 		 */
644 		sbuf_printf(sb, "/dev/%-34s unknown\t\t%jd\t%jd\t-1\n",
645 		    devname, total, used);
646 	}
647 	return (0);
648 }
649 
650 /*
651  * Filler function for proc/uptime
652  */
653 static int
654 linprocfs_douptime(PFS_FILL_ARGS)
655 {
656 	long cp_time[CPUSTATES];
657 	struct timeval tv;
658 
659 	getmicrouptime(&tv);
660 	read_cpu_time(cp_time);
661 	sbuf_printf(sb, "%lld.%02ld %ld.%02lu\n",
662 	    (long long)tv.tv_sec, tv.tv_usec / 10000,
663 	    T2S(cp_time[CP_IDLE] / mp_ncpus),
664 	    T2CS(cp_time[CP_IDLE] / mp_ncpus) % 100);
665 	return (0);
666 }
667 
668 /*
669  * Get OS build date
670  */
671 static void
672 linprocfs_osbuild(struct thread *td, struct sbuf *sb)
673 {
674 #if 0
675 	char osbuild[256];
676 	char *cp1, *cp2;
677 
678 	strncpy(osbuild, version, 256);
679 	osbuild[255] = '\0';
680 	cp1 = strstr(osbuild, "\n");
681 	cp2 = strstr(osbuild, ":");
682 	if (cp1 && cp2) {
683 		*cp1 = *cp2 = '\0';
684 		cp1 = strstr(osbuild, "#");
685 	} else
686 		cp1 = NULL;
687 	if (cp1)
688 		sbuf_printf(sb, "%s%s", cp1, cp2 + 1);
689 	else
690 #endif
691 		sbuf_cat(sb, "#4 Sun Dec 18 04:30:00 CET 1977");
692 }
693 
694 /*
695  * Get OS builder
696  */
697 static void
698 linprocfs_osbuilder(struct thread *td, struct sbuf *sb)
699 {
700 #if 0
701 	char builder[256];
702 	char *cp;
703 
704 	cp = strstr(version, "\n    ");
705 	if (cp) {
706 		strncpy(builder, cp + 5, 256);
707 		builder[255] = '\0';
708 		cp = strstr(builder, ":");
709 		if (cp)
710 			*cp = '\0';
711 	}
712 	if (cp)
713 		sbuf_cat(sb, builder);
714 	else
715 #endif
716 		sbuf_cat(sb, "des@freebsd.org");
717 }
718 
719 /*
720  * Filler function for proc/version
721  */
722 static int
723 linprocfs_doversion(PFS_FILL_ARGS)
724 {
725 	char osname[LINUX_MAX_UTSNAME];
726 	char osrelease[LINUX_MAX_UTSNAME];
727 
728 	linux_get_osname(td, osname);
729 	linux_get_osrelease(td, osrelease);
730 	sbuf_printf(sb, "%s version %s (", osname, osrelease);
731 	linprocfs_osbuilder(td, sb);
732 	sbuf_cat(sb, ") (gcc version " __VERSION__ ") ");
733 	linprocfs_osbuild(td, sb);
734 	sbuf_cat(sb, "\n");
735 
736 	return (0);
737 }
738 
739 /*
740  * Filler function for proc/loadavg
741  */
742 static int
743 linprocfs_doloadavg(PFS_FILL_ARGS)
744 {
745 
746 	sbuf_printf(sb,
747 	    "%d.%02d %d.%02d %d.%02d %d/%d %d\n",
748 	    (int)(averunnable.ldavg[0] / averunnable.fscale),
749 	    (int)(averunnable.ldavg[0] * 100 / averunnable.fscale % 100),
750 	    (int)(averunnable.ldavg[1] / averunnable.fscale),
751 	    (int)(averunnable.ldavg[1] * 100 / averunnable.fscale % 100),
752 	    (int)(averunnable.ldavg[2] / averunnable.fscale),
753 	    (int)(averunnable.ldavg[2] * 100 / averunnable.fscale % 100),
754 	    1,				/* number of running tasks */
755 	    nprocs,			/* number of tasks */
756 	    lastpid			/* the last pid */
757 	);
758 	return (0);
759 }
760 
761 /*
762  * Filler function for proc/pid/stat
763  */
764 static int
765 linprocfs_doprocstat(PFS_FILL_ARGS)
766 {
767 	struct kinfo_proc kp;
768 	struct timeval boottime;
769 	char state;
770 	static int ratelimit = 0;
771 	vm_offset_t startcode, startdata;
772 
773 	getboottime(&boottime);
774 	sx_slock(&proctree_lock);
775 	PROC_LOCK(p);
776 	fill_kinfo_proc(p, &kp);
777 	sx_sunlock(&proctree_lock);
778 	if (p->p_vmspace) {
779 	   startcode = (vm_offset_t)p->p_vmspace->vm_taddr;
780 	   startdata = (vm_offset_t)p->p_vmspace->vm_daddr;
781 	} else {
782 	   startcode = 0;
783 	   startdata = 0;
784 	}
785 	sbuf_printf(sb, "%d", p->p_pid);
786 #define PS_ADD(name, fmt, arg) sbuf_printf(sb, " " fmt, arg)
787 	PS_ADD("comm",		"(%s)",	p->p_comm);
788 	if (kp.ki_stat > sizeof(linux_state)) {
789 		state = 'R';
790 
791 		if (ratelimit == 0) {
792 			printf("linprocfs: don't know how to handle unknown FreeBSD state %d/%zd, mapping to R\n",
793 			    kp.ki_stat, sizeof(linux_state));
794 			++ratelimit;
795 		}
796 	} else
797 		state = linux_state[kp.ki_stat - 1];
798 	PS_ADD("state",		"%c",	state);
799 	PS_ADD("ppid",		"%d",	p->p_pptr ? p->p_pptr->p_pid : 0);
800 	PS_ADD("pgrp",		"%d",	p->p_pgid);
801 	PS_ADD("session",	"%d",	p->p_session->s_sid);
802 	PROC_UNLOCK(p);
803 	PS_ADD("tty",		"%ju",	(uintmax_t)kp.ki_tdev);
804 	PS_ADD("tpgid",		"%d",	kp.ki_tpgid);
805 	PS_ADD("flags",		"%u",	0); /* XXX */
806 	PS_ADD("minflt",	"%lu",	kp.ki_rusage.ru_minflt);
807 	PS_ADD("cminflt",	"%lu",	kp.ki_rusage_ch.ru_minflt);
808 	PS_ADD("majflt",	"%lu",	kp.ki_rusage.ru_majflt);
809 	PS_ADD("cmajflt",	"%lu",	kp.ki_rusage_ch.ru_majflt);
810 	PS_ADD("utime",		"%ld",	TV2J(&kp.ki_rusage.ru_utime));
811 	PS_ADD("stime",		"%ld",	TV2J(&kp.ki_rusage.ru_stime));
812 	PS_ADD("cutime",	"%ld",	TV2J(&kp.ki_rusage_ch.ru_utime));
813 	PS_ADD("cstime",	"%ld",	TV2J(&kp.ki_rusage_ch.ru_stime));
814 	PS_ADD("priority",	"%d",	kp.ki_pri.pri_user);
815 	PS_ADD("nice",		"%d",	kp.ki_nice); /* 19 (nicest) to -19 */
816 	PS_ADD("0",		"%d",	0); /* removed field */
817 	PS_ADD("itrealvalue",	"%d",	0); /* XXX */
818 	PS_ADD("starttime",	"%lu",	TV2J(&kp.ki_start) - TV2J(&boottime));
819 	PS_ADD("vsize",		"%ju",	P2K((uintmax_t)kp.ki_size));
820 	PS_ADD("rss",		"%ju",	(uintmax_t)kp.ki_rssize);
821 	PS_ADD("rlim",		"%lu",	kp.ki_rusage.ru_maxrss);
822 	PS_ADD("startcode",	"%ju",	(uintmax_t)startcode);
823 	PS_ADD("endcode",	"%ju",	(uintmax_t)startdata);
824 	PS_ADD("startstack",	"%u",	0); /* XXX */
825 	PS_ADD("kstkesp",	"%u",	0); /* XXX */
826 	PS_ADD("kstkeip",	"%u",	0); /* XXX */
827 	PS_ADD("signal",	"%u",	0); /* XXX */
828 	PS_ADD("blocked",	"%u",	0); /* XXX */
829 	PS_ADD("sigignore",	"%u",	0); /* XXX */
830 	PS_ADD("sigcatch",	"%u",	0); /* XXX */
831 	PS_ADD("wchan",		"%u",	0); /* XXX */
832 	PS_ADD("nswap",		"%lu",	kp.ki_rusage.ru_nswap);
833 	PS_ADD("cnswap",	"%lu",	kp.ki_rusage_ch.ru_nswap);
834 	PS_ADD("exitsignal",	"%d",	0); /* XXX */
835 	PS_ADD("processor",	"%u",	kp.ki_lastcpu);
836 	PS_ADD("rt_priority",	"%u",	0); /* XXX */ /* >= 2.5.19 */
837 	PS_ADD("policy",	"%u",	kp.ki_pri.pri_class); /* >= 2.5.19 */
838 #undef PS_ADD
839 	sbuf_putc(sb, '\n');
840 
841 	return (0);
842 }
843 
844 /*
845  * Filler function for proc/pid/statm
846  */
847 static int
848 linprocfs_doprocstatm(PFS_FILL_ARGS)
849 {
850 	struct kinfo_proc kp;
851 	segsz_t lsize;
852 
853 	sx_slock(&proctree_lock);
854 	PROC_LOCK(p);
855 	fill_kinfo_proc(p, &kp);
856 	PROC_UNLOCK(p);
857 	sx_sunlock(&proctree_lock);
858 
859 	/*
860 	 * See comments in linprocfs_doprocstatus() regarding the
861 	 * computation of lsize.
862 	 */
863 	/* size resident share trs drs lrs dt */
864 	sbuf_printf(sb, "%ju ", B2P((uintmax_t)kp.ki_size));
865 	sbuf_printf(sb, "%ju ", (uintmax_t)kp.ki_rssize);
866 	sbuf_printf(sb, "%ju ", (uintmax_t)0); /* XXX */
867 	sbuf_printf(sb, "%ju ",	(uintmax_t)kp.ki_tsize);
868 	sbuf_printf(sb, "%ju ", (uintmax_t)(kp.ki_dsize + kp.ki_ssize));
869 	lsize = B2P(kp.ki_size) - kp.ki_dsize -
870 	    kp.ki_ssize - kp.ki_tsize - 1;
871 	sbuf_printf(sb, "%ju ", (uintmax_t)lsize);
872 	sbuf_printf(sb, "%ju\n", (uintmax_t)0); /* XXX */
873 
874 	return (0);
875 }
876 
877 /*
878  * Filler function for proc/pid/status
879  */
880 static int
881 linprocfs_doprocstatus(PFS_FILL_ARGS)
882 {
883 	struct kinfo_proc kp;
884 	char *state;
885 	segsz_t lsize;
886 	struct thread *td2;
887 	struct sigacts *ps;
888 	l_sigset_t siglist, sigignore, sigcatch;
889 	int i;
890 
891 	sx_slock(&proctree_lock);
892 	PROC_LOCK(p);
893 	td2 = FIRST_THREAD_IN_PROC(p); /* XXXKSE pretend only one thread */
894 
895 	if (P_SHOULDSTOP(p)) {
896 		state = "T (stopped)";
897 	} else {
898 		switch(p->p_state) {
899 		case PRS_NEW:
900 			state = "I (idle)";
901 			break;
902 		case PRS_NORMAL:
903 			if (p->p_flag & P_WEXIT) {
904 				state = "X (exiting)";
905 				break;
906 			}
907 			switch(td2->td_state) {
908 			case TDS_INHIBITED:
909 				state = "S (sleeping)";
910 				break;
911 			case TDS_RUNQ:
912 			case TDS_RUNNING:
913 				state = "R (running)";
914 				break;
915 			default:
916 				state = "? (unknown)";
917 				break;
918 			}
919 			break;
920 		case PRS_ZOMBIE:
921 			state = "Z (zombie)";
922 			break;
923 		default:
924 			state = "? (unknown)";
925 			break;
926 		}
927 	}
928 
929 	fill_kinfo_proc(p, &kp);
930 	sx_sunlock(&proctree_lock);
931 
932 	sbuf_printf(sb, "Name:\t%s\n",		p->p_comm); /* XXX escape */
933 	sbuf_printf(sb, "State:\t%s\n",		state);
934 
935 	/*
936 	 * Credentials
937 	 */
938 	sbuf_printf(sb, "Tgid:\t%d\n",		p->p_pid);
939 	sbuf_printf(sb, "Pid:\t%d\n",		p->p_pid);
940 	sbuf_printf(sb, "PPid:\t%d\n",		kp.ki_ppid );
941 	sbuf_printf(sb, "TracerPid:\t%d\n",	kp.ki_tracer );
942 	sbuf_printf(sb, "Uid:\t%d %d %d %d\n",	p->p_ucred->cr_ruid,
943 						p->p_ucred->cr_uid,
944 						p->p_ucred->cr_svuid,
945 						/* FreeBSD doesn't have fsuid */
946 						p->p_ucred->cr_uid);
947 	sbuf_printf(sb, "Gid:\t%d %d %d %d\n",	p->p_ucred->cr_rgid,
948 						p->p_ucred->cr_gid,
949 						p->p_ucred->cr_svgid,
950 						/* FreeBSD doesn't have fsgid */
951 						p->p_ucred->cr_gid);
952 	sbuf_cat(sb, "Groups:\t");
953 	for (i = 0; i < p->p_ucred->cr_ngroups; i++)
954 		sbuf_printf(sb, "%d ",		p->p_ucred->cr_groups[i]);
955 	PROC_UNLOCK(p);
956 	sbuf_putc(sb, '\n');
957 
958 	/*
959 	 * Memory
960 	 *
961 	 * While our approximation of VmLib may not be accurate (I
962 	 * don't know of a simple way to verify it, and I'm not sure
963 	 * it has much meaning anyway), I believe it's good enough.
964 	 *
965 	 * The same code that could (I think) accurately compute VmLib
966 	 * could also compute VmLck, but I don't really care enough to
967 	 * implement it. Submissions are welcome.
968 	 */
969 	sbuf_printf(sb, "VmSize:\t%8ju kB\n",	B2K((uintmax_t)kp.ki_size));
970 	sbuf_printf(sb, "VmLck:\t%8u kB\n",	P2K(0)); /* XXX */
971 	sbuf_printf(sb, "VmRSS:\t%8ju kB\n",	P2K((uintmax_t)kp.ki_rssize));
972 	sbuf_printf(sb, "VmData:\t%8ju kB\n",	P2K((uintmax_t)kp.ki_dsize));
973 	sbuf_printf(sb, "VmStk:\t%8ju kB\n",	P2K((uintmax_t)kp.ki_ssize));
974 	sbuf_printf(sb, "VmExe:\t%8ju kB\n",	P2K((uintmax_t)kp.ki_tsize));
975 	lsize = B2P(kp.ki_size) - kp.ki_dsize -
976 	    kp.ki_ssize - kp.ki_tsize - 1;
977 	sbuf_printf(sb, "VmLib:\t%8ju kB\n",	P2K((uintmax_t)lsize));
978 
979 	/*
980 	 * Signal masks
981 	 */
982 	PROC_LOCK(p);
983 	bsd_to_linux_sigset(&p->p_siglist, &siglist);
984 	ps = p->p_sigacts;
985 	mtx_lock(&ps->ps_mtx);
986 	bsd_to_linux_sigset(&ps->ps_sigignore, &sigignore);
987 	bsd_to_linux_sigset(&ps->ps_sigcatch, &sigcatch);
988 	mtx_unlock(&ps->ps_mtx);
989 	PROC_UNLOCK(p);
990 
991 	sbuf_printf(sb, "SigPnd:\t%016jx\n",	siglist.__mask);
992 	/*
993 	 * XXX. SigBlk - target thread's signal mask, td_sigmask.
994 	 * To implement SigBlk pseudofs should support proc/tid dir entries.
995 	 */
996 	sbuf_printf(sb, "SigBlk:\t%016x\n",	0);
997 	sbuf_printf(sb, "SigIgn:\t%016jx\n",	sigignore.__mask);
998 	sbuf_printf(sb, "SigCgt:\t%016jx\n",	sigcatch.__mask);
999 
1000 	/*
1001 	 * Linux also prints the capability masks, but we don't have
1002 	 * capabilities yet, and when we do get them they're likely to
1003 	 * be meaningless to Linux programs, so we lie. XXX
1004 	 */
1005 	sbuf_printf(sb, "CapInh:\t%016x\n",	0);
1006 	sbuf_printf(sb, "CapPrm:\t%016x\n",	0);
1007 	sbuf_printf(sb, "CapEff:\t%016x\n",	0);
1008 
1009 	return (0);
1010 }
1011 
1012 
1013 /*
1014  * Filler function for proc/pid/cwd
1015  */
1016 static int
1017 linprocfs_doproccwd(PFS_FILL_ARGS)
1018 {
1019 	struct filedesc *fdp;
1020 	struct vnode *vp;
1021 	char *fullpath = "unknown";
1022 	char *freepath = NULL;
1023 
1024 	fdp = p->p_fd;
1025 	FILEDESC_SLOCK(fdp);
1026 	vp = fdp->fd_cdir;
1027 	if (vp != NULL)
1028 		VREF(vp);
1029 	FILEDESC_SUNLOCK(fdp);
1030 	vn_fullpath(td, vp, &fullpath, &freepath);
1031 	if (vp != NULL)
1032 		vrele(vp);
1033 	sbuf_printf(sb, "%s", fullpath);
1034 	if (freepath)
1035 		free(freepath, M_TEMP);
1036 	return (0);
1037 }
1038 
1039 /*
1040  * Filler function for proc/pid/root
1041  */
1042 static int
1043 linprocfs_doprocroot(PFS_FILL_ARGS)
1044 {
1045 	struct filedesc *fdp;
1046 	struct vnode *vp;
1047 	char *fullpath = "unknown";
1048 	char *freepath = NULL;
1049 
1050 	fdp = p->p_fd;
1051 	FILEDESC_SLOCK(fdp);
1052 	vp = jailed(p->p_ucred) ? fdp->fd_jdir : fdp->fd_rdir;
1053 	if (vp != NULL)
1054 		VREF(vp);
1055 	FILEDESC_SUNLOCK(fdp);
1056 	vn_fullpath(td, vp, &fullpath, &freepath);
1057 	if (vp != NULL)
1058 		vrele(vp);
1059 	sbuf_printf(sb, "%s", fullpath);
1060 	if (freepath)
1061 		free(freepath, M_TEMP);
1062 	return (0);
1063 }
1064 
1065 /*
1066  * Filler function for proc/pid/cmdline
1067  */
1068 static int
1069 linprocfs_doproccmdline(PFS_FILL_ARGS)
1070 {
1071 	int ret;
1072 
1073 	PROC_LOCK(p);
1074 	if ((ret = p_cansee(td, p)) != 0) {
1075 		PROC_UNLOCK(p);
1076 		return (ret);
1077 	}
1078 
1079 	/*
1080 	 * Mimic linux behavior and pass only processes with usermode
1081 	 * address space as valid.  Return zero silently otherwize.
1082 	 */
1083 	if (p->p_vmspace == &vmspace0) {
1084 		PROC_UNLOCK(p);
1085 		return (0);
1086 	}
1087 	if (p->p_args != NULL) {
1088 		sbuf_bcpy(sb, p->p_args->ar_args, p->p_args->ar_length);
1089 		PROC_UNLOCK(p);
1090 		return (0);
1091 	}
1092 
1093 	if ((p->p_flag & P_SYSTEM) != 0) {
1094 		PROC_UNLOCK(p);
1095 		return (0);
1096 	}
1097 
1098 	PROC_UNLOCK(p);
1099 
1100 	ret = proc_getargv(td, p, sb);
1101 	return (ret);
1102 }
1103 
1104 /*
1105  * Filler function for proc/pid/environ
1106  */
1107 static int
1108 linprocfs_doprocenviron(PFS_FILL_ARGS)
1109 {
1110 
1111 	/*
1112 	 * Mimic linux behavior and pass only processes with usermode
1113 	 * address space as valid.  Return zero silently otherwize.
1114 	 */
1115 	if (p->p_vmspace == &vmspace0)
1116 		return (0);
1117 
1118 	return (proc_getenvv(td, p, sb));
1119 }
1120 
1121 static char l32_map_str[] = "%08lx-%08lx %s%s%s%s %08lx %02x:%02x %lu%s%s\n";
1122 static char l64_map_str[] = "%016lx-%016lx %s%s%s%s %08lx %02x:%02x %lu%s%s\n";
1123 static char vdso_str[] = "      [vdso]";
1124 static char stack_str[] = "      [stack]";
1125 
1126 /*
1127  * Filler function for proc/pid/maps
1128  */
1129 static int
1130 linprocfs_doprocmaps(PFS_FILL_ARGS)
1131 {
1132 	struct vmspace *vm;
1133 	vm_map_t map;
1134 	vm_map_entry_t entry, tmp_entry;
1135 	vm_object_t obj, tobj, lobj;
1136 	vm_offset_t e_start, e_end;
1137 	vm_ooffset_t off = 0;
1138 	vm_prot_t e_prot;
1139 	unsigned int last_timestamp;
1140 	char *name = "", *freename = NULL;
1141 	const char *l_map_str;
1142 	ino_t ino;
1143 	int ref_count, shadow_count, flags;
1144 	int error;
1145 	struct vnode *vp;
1146 	struct vattr vat;
1147 
1148 	PROC_LOCK(p);
1149 	error = p_candebug(td, p);
1150 	PROC_UNLOCK(p);
1151 	if (error)
1152 		return (error);
1153 
1154 	if (uio->uio_rw != UIO_READ)
1155 		return (EOPNOTSUPP);
1156 
1157 	error = 0;
1158 	vm = vmspace_acquire_ref(p);
1159 	if (vm == NULL)
1160 		return (ESRCH);
1161 
1162 	if (SV_CURPROC_FLAG(SV_LP64))
1163 		l_map_str = l64_map_str;
1164 	else
1165 		l_map_str = l32_map_str;
1166 	map = &vm->vm_map;
1167 	vm_map_lock_read(map);
1168 	for (entry = map->header.next; entry != &map->header;
1169 	    entry = entry->next) {
1170 		name = "";
1171 		freename = NULL;
1172 		if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
1173 			continue;
1174 		e_prot = entry->protection;
1175 		e_start = entry->start;
1176 		e_end = entry->end;
1177 		obj = entry->object.vm_object;
1178 		for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) {
1179 			VM_OBJECT_RLOCK(tobj);
1180 			if (lobj != obj)
1181 				VM_OBJECT_RUNLOCK(lobj);
1182 			lobj = tobj;
1183 		}
1184 		last_timestamp = map->timestamp;
1185 		vm_map_unlock_read(map);
1186 		ino = 0;
1187 		if (lobj) {
1188 			off = IDX_TO_OFF(lobj->size);
1189 			vp = vm_object_vnode(lobj);
1190 			if (vp != NULL)
1191 				vref(vp);
1192 			if (lobj != obj)
1193 				VM_OBJECT_RUNLOCK(lobj);
1194 			flags = obj->flags;
1195 			ref_count = obj->ref_count;
1196 			shadow_count = obj->shadow_count;
1197 			VM_OBJECT_RUNLOCK(obj);
1198 			if (vp != NULL) {
1199 				vn_fullpath(td, vp, &name, &freename);
1200 				vn_lock(vp, LK_SHARED | LK_RETRY);
1201 				VOP_GETATTR(vp, &vat, td->td_ucred);
1202 				ino = vat.va_fileid;
1203 				vput(vp);
1204 			} else if (SV_PROC_ABI(p) == SV_ABI_LINUX) {
1205 				if (e_start == p->p_sysent->sv_shared_page_base)
1206 					name = vdso_str;
1207 				if (e_end == p->p_sysent->sv_usrstack)
1208 					name = stack_str;
1209 			}
1210 		} else {
1211 			flags = 0;
1212 			ref_count = 0;
1213 			shadow_count = 0;
1214 		}
1215 
1216 		/*
1217 		 * format:
1218 		 *  start, end, access, offset, major, minor, inode, name.
1219 		 */
1220 		error = sbuf_printf(sb, l_map_str,
1221 		    (u_long)e_start, (u_long)e_end,
1222 		    (e_prot & VM_PROT_READ)?"r":"-",
1223 		    (e_prot & VM_PROT_WRITE)?"w":"-",
1224 		    (e_prot & VM_PROT_EXECUTE)?"x":"-",
1225 		    "p",
1226 		    (u_long)off,
1227 		    0,
1228 		    0,
1229 		    (u_long)ino,
1230 		    *name ? "     " : "",
1231 		    name
1232 		    );
1233 		if (freename)
1234 			free(freename, M_TEMP);
1235 		vm_map_lock_read(map);
1236 		if (error == -1) {
1237 			error = 0;
1238 			break;
1239 		}
1240 		if (last_timestamp != map->timestamp) {
1241 			/*
1242 			 * Look again for the entry because the map was
1243 			 * modified while it was unlocked.  Specifically,
1244 			 * the entry may have been clipped, merged, or deleted.
1245 			 */
1246 			vm_map_lookup_entry(map, e_end - 1, &tmp_entry);
1247 			entry = tmp_entry;
1248 		}
1249 	}
1250 	vm_map_unlock_read(map);
1251 	vmspace_free(vm);
1252 
1253 	return (error);
1254 }
1255 
1256 /*
1257  * Criteria for interface name translation
1258  */
1259 #define IFP_IS_ETH(ifp) (ifp->if_type == IFT_ETHER)
1260 
1261 static int
1262 linux_ifname(struct ifnet *ifp, char *buffer, size_t buflen)
1263 {
1264 	struct ifnet *ifscan;
1265 	int ethno;
1266 
1267 	IFNET_RLOCK_ASSERT();
1268 
1269 	/* Short-circuit non ethernet interfaces */
1270 	if (!IFP_IS_ETH(ifp))
1271 		return (strlcpy(buffer, ifp->if_xname, buflen));
1272 
1273 	/* Determine the (relative) unit number for ethernet interfaces */
1274 	ethno = 0;
1275 	CK_STAILQ_FOREACH(ifscan, &V_ifnet, if_link) {
1276 		if (ifscan == ifp)
1277 			return (snprintf(buffer, buflen, "eth%d", ethno));
1278 		if (IFP_IS_ETH(ifscan))
1279 			ethno++;
1280 	}
1281 
1282 	return (0);
1283 }
1284 
1285 /*
1286  * Filler function for proc/net/dev
1287  */
1288 static int
1289 linprocfs_donetdev(PFS_FILL_ARGS)
1290 {
1291 	char ifname[16]; /* XXX LINUX_IFNAMSIZ */
1292 	struct ifnet *ifp;
1293 
1294 	sbuf_printf(sb, "%6s|%58s|%s\n"
1295 	    "%6s|%58s|%58s\n",
1296 	    "Inter-", "   Receive", "  Transmit",
1297 	    " face",
1298 	    "bytes    packets errs drop fifo frame compressed multicast",
1299 	    "bytes    packets errs drop fifo colls carrier compressed");
1300 
1301 	CURVNET_SET(TD_TO_VNET(curthread));
1302 	IFNET_RLOCK();
1303 	CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1304 		linux_ifname(ifp, ifname, sizeof ifname);
1305 		sbuf_printf(sb, "%6.6s: ", ifname);
1306 		sbuf_printf(sb, "%7ju %7ju %4ju %4ju %4lu %5lu %10lu %9ju ",
1307 		    (uintmax_t )ifp->if_get_counter(ifp, IFCOUNTER_IBYTES),
1308 		    (uintmax_t )ifp->if_get_counter(ifp, IFCOUNTER_IPACKETS),
1309 		    (uintmax_t )ifp->if_get_counter(ifp, IFCOUNTER_IERRORS),
1310 		    (uintmax_t )ifp->if_get_counter(ifp, IFCOUNTER_IQDROPS),
1311 							/* rx_missed_errors */
1312 		    0UL,				/* rx_fifo_errors */
1313 		    0UL,				/* rx_length_errors +
1314 							 * rx_over_errors +
1315 							 * rx_crc_errors +
1316 							 * rx_frame_errors */
1317 		    0UL,				/* rx_compressed */
1318 		    (uintmax_t )ifp->if_get_counter(ifp, IFCOUNTER_IMCASTS));
1319 							/* XXX-BZ rx only? */
1320 		sbuf_printf(sb, "%8ju %7ju %4ju %4ju %4lu %5ju %7lu %10lu\n",
1321 		    (uintmax_t )ifp->if_get_counter(ifp, IFCOUNTER_OBYTES),
1322 		    (uintmax_t )ifp->if_get_counter(ifp, IFCOUNTER_OPACKETS),
1323 		    (uintmax_t )ifp->if_get_counter(ifp, IFCOUNTER_OERRORS),
1324 		    (uintmax_t )ifp->if_get_counter(ifp, IFCOUNTER_OQDROPS),
1325 		    0UL,				/* tx_fifo_errors */
1326 		    (uintmax_t )ifp->if_get_counter(ifp, IFCOUNTER_COLLISIONS),
1327 		    0UL,				/* tx_carrier_errors +
1328 							 * tx_aborted_errors +
1329 							 * tx_window_errors +
1330 							 * tx_heartbeat_errors*/
1331 		    0UL);				/* tx_compressed */
1332 	}
1333 	IFNET_RUNLOCK();
1334 	CURVNET_RESTORE();
1335 
1336 	return (0);
1337 }
1338 
1339 /*
1340  * Filler function for proc/sys/kernel/osrelease
1341  */
1342 static int
1343 linprocfs_doosrelease(PFS_FILL_ARGS)
1344 {
1345 	char osrelease[LINUX_MAX_UTSNAME];
1346 
1347 	linux_get_osrelease(td, osrelease);
1348 	sbuf_printf(sb, "%s\n", osrelease);
1349 
1350 	return (0);
1351 }
1352 
1353 /*
1354  * Filler function for proc/sys/kernel/ostype
1355  */
1356 static int
1357 linprocfs_doostype(PFS_FILL_ARGS)
1358 {
1359 	char osname[LINUX_MAX_UTSNAME];
1360 
1361 	linux_get_osname(td, osname);
1362 	sbuf_printf(sb, "%s\n", osname);
1363 
1364 	return (0);
1365 }
1366 
1367 /*
1368  * Filler function for proc/sys/kernel/version
1369  */
1370 static int
1371 linprocfs_doosbuild(PFS_FILL_ARGS)
1372 {
1373 
1374 	linprocfs_osbuild(td, sb);
1375 	sbuf_cat(sb, "\n");
1376 	return (0);
1377 }
1378 
1379 /*
1380  * Filler function for proc/sys/kernel/msgmni
1381  */
1382 static int
1383 linprocfs_domsgmni(PFS_FILL_ARGS)
1384 {
1385 
1386 	sbuf_printf(sb, "%d\n", msginfo.msgmni);
1387 	return (0);
1388 }
1389 
1390 /*
1391  * Filler function for proc/sys/kernel/pid_max
1392  */
1393 static int
1394 linprocfs_dopid_max(PFS_FILL_ARGS)
1395 {
1396 
1397 	sbuf_printf(sb, "%i\n", PID_MAX);
1398 	return (0);
1399 }
1400 
1401 /*
1402  * Filler function for proc/sys/kernel/sem
1403  */
1404 static int
1405 linprocfs_dosem(PFS_FILL_ARGS)
1406 {
1407 
1408 	sbuf_printf(sb, "%d %d %d %d\n", seminfo.semmsl, seminfo.semmns,
1409 	    seminfo.semopm, seminfo.semmni);
1410 	return (0);
1411 }
1412 
1413 /*
1414  * Filler function for proc/sys/vm/min_free_kbytes
1415  *
1416  * This mirrors the approach in illumos to return zero for reads. Effectively,
1417  * it says, no memory is kept in reserve for "atomic allocations". This class
1418  * of allocation can be used at times when a thread cannot be suspended.
1419  */
1420 static int
1421 linprocfs_dominfree(PFS_FILL_ARGS)
1422 {
1423 
1424 	sbuf_printf(sb, "%d\n", 0);
1425 	return (0);
1426 }
1427 
1428 /*
1429  * Filler function for proc/scsi/device_info
1430  */
1431 static int
1432 linprocfs_doscsidevinfo(PFS_FILL_ARGS)
1433 {
1434 
1435 	return (0);
1436 }
1437 
1438 /*
1439  * Filler function for proc/scsi/scsi
1440  */
1441 static int
1442 linprocfs_doscsiscsi(PFS_FILL_ARGS)
1443 {
1444 
1445 	return (0);
1446 }
1447 
1448 /*
1449  * Filler function for proc/devices
1450  */
1451 static int
1452 linprocfs_dodevices(PFS_FILL_ARGS)
1453 {
1454 	char *char_devices;
1455 	sbuf_printf(sb, "Character devices:\n");
1456 
1457 	char_devices = linux_get_char_devices();
1458 	sbuf_printf(sb, "%s", char_devices);
1459 	linux_free_get_char_devices(char_devices);
1460 
1461 	sbuf_printf(sb, "\nBlock devices:\n");
1462 
1463 	return (0);
1464 }
1465 
1466 /*
1467  * Filler function for proc/cmdline
1468  */
1469 static int
1470 linprocfs_docmdline(PFS_FILL_ARGS)
1471 {
1472 
1473 	sbuf_printf(sb, "BOOT_IMAGE=%s", kernelname);
1474 	sbuf_printf(sb, " ro root=302\n");
1475 	return (0);
1476 }
1477 
1478 /*
1479  * Filler function for proc/filesystems
1480  */
1481 static int
1482 linprocfs_dofilesystems(PFS_FILL_ARGS)
1483 {
1484 	struct vfsconf *vfsp;
1485 
1486 	vfsconf_slock();
1487 	TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
1488 		if (vfsp->vfc_flags & VFCF_SYNTHETIC)
1489 			sbuf_printf(sb, "nodev");
1490 		sbuf_printf(sb, "\t%s\n", vfsp->vfc_name);
1491 	}
1492 	vfsconf_sunlock();
1493 	return(0);
1494 }
1495 
1496 #if 0
1497 /*
1498  * Filler function for proc/modules
1499  */
1500 static int
1501 linprocfs_domodules(PFS_FILL_ARGS)
1502 {
1503 	struct linker_file *lf;
1504 
1505 	TAILQ_FOREACH(lf, &linker_files, link) {
1506 		sbuf_printf(sb, "%-20s%8lu%4d\n", lf->filename,
1507 		    (unsigned long)lf->size, lf->refs);
1508 	}
1509 	return (0);
1510 }
1511 #endif
1512 
1513 /*
1514  * Filler function for proc/pid/fd
1515  */
1516 static int
1517 linprocfs_dofdescfs(PFS_FILL_ARGS)
1518 {
1519 
1520 	if (p == curproc)
1521 		sbuf_printf(sb, "/dev/fd");
1522 	else
1523 		sbuf_printf(sb, "unknown");
1524 	return (0);
1525 }
1526 
1527 /*
1528  * Filler function for proc/pid/limits
1529  */
1530 static const struct linux_rlimit_ident {
1531 	const char	*desc;
1532 	const char	*unit;
1533 	unsigned int	rlim_id;
1534 } linux_rlimits_ident[] = {
1535 	{ "Max cpu time",	"seconds",	RLIMIT_CPU },
1536 	{ "Max file size", 	"bytes",	RLIMIT_FSIZE },
1537 	{ "Max data size",	"bytes", 	RLIMIT_DATA },
1538 	{ "Max stack size",	"bytes", 	RLIMIT_STACK },
1539 	{ "Max core file size",  "bytes",	RLIMIT_CORE },
1540 	{ "Max resident set",	"bytes",	RLIMIT_RSS },
1541 	{ "Max processes",	"processes",	RLIMIT_NPROC },
1542 	{ "Max open files",	"files",	RLIMIT_NOFILE },
1543 	{ "Max locked memory",	"bytes",	RLIMIT_MEMLOCK },
1544 	{ "Max address space",	"bytes",	RLIMIT_AS },
1545 	{ "Max file locks",	"locks",	LINUX_RLIMIT_LOCKS },
1546 	{ "Max pending signals", "signals",	LINUX_RLIMIT_SIGPENDING },
1547 	{ "Max msgqueue size",	"bytes",	LINUX_RLIMIT_MSGQUEUE },
1548 	{ "Max nice priority", 		"",	LINUX_RLIMIT_NICE },
1549 	{ "Max realtime priority",	"",	LINUX_RLIMIT_RTPRIO },
1550 	{ "Max realtime timeout",	"us",	LINUX_RLIMIT_RTTIME },
1551 	{ 0, 0, 0 }
1552 };
1553 
1554 static int
1555 linprocfs_doproclimits(PFS_FILL_ARGS)
1556 {
1557 	const struct linux_rlimit_ident *li;
1558 	struct plimit *limp;
1559 	struct rlimit rl;
1560 	ssize_t size;
1561 	int res, error;
1562 
1563 	error = 0;
1564 
1565 	PROC_LOCK(p);
1566 	limp = lim_hold(p->p_limit);
1567 	PROC_UNLOCK(p);
1568 	size = sizeof(res);
1569 	sbuf_printf(sb, "%-26s%-21s%-21s%-21s\n", "Limit", "Soft Limit",
1570 			"Hard Limit", "Units");
1571 	for (li = linux_rlimits_ident; li->desc != NULL; ++li) {
1572 		switch (li->rlim_id)
1573 		{
1574 		case LINUX_RLIMIT_LOCKS:
1575 			/* FALLTHROUGH */
1576 		case LINUX_RLIMIT_RTTIME:
1577 			rl.rlim_cur = RLIM_INFINITY;
1578 			break;
1579 		case LINUX_RLIMIT_SIGPENDING:
1580 			error = kernel_sysctlbyname(td,
1581 			    "kern.sigqueue.max_pending_per_proc",
1582 			    &res, &size, 0, 0, 0, 0);
1583 			if (error != 0)
1584 				goto out;
1585 			rl.rlim_cur = res;
1586 			rl.rlim_max = res;
1587 			break;
1588 		case LINUX_RLIMIT_MSGQUEUE:
1589 			error = kernel_sysctlbyname(td,
1590 			    "kern.ipc.msgmnb", &res, &size, 0, 0, 0, 0);
1591 			if (error != 0)
1592 				goto out;
1593 			rl.rlim_cur = res;
1594 			rl.rlim_max = res;
1595 			break;
1596 		case LINUX_RLIMIT_NICE:
1597 			/* FALLTHROUGH */
1598 		case LINUX_RLIMIT_RTPRIO:
1599 			rl.rlim_cur = 0;
1600 			rl.rlim_max = 0;
1601 			break;
1602 		default:
1603 			rl = limp->pl_rlimit[li->rlim_id];
1604 			break;
1605 		}
1606 		if (rl.rlim_cur == RLIM_INFINITY)
1607 			sbuf_printf(sb, "%-26s%-21s%-21s%-10s\n",
1608 			    li->desc, "unlimited", "unlimited", li->unit);
1609 		else
1610 			sbuf_printf(sb, "%-26s%-21llu%-21llu%-10s\n",
1611 			    li->desc, (unsigned long long)rl.rlim_cur,
1612 			    (unsigned long long)rl.rlim_max, li->unit);
1613 	}
1614 out:
1615 	lim_free(limp);
1616 	return (error);
1617 }
1618 
1619 /*
1620  * Filler function for proc/sys/kernel/random/uuid
1621  */
1622 static int
1623 linprocfs_douuid(PFS_FILL_ARGS)
1624 {
1625 	struct uuid uuid;
1626 
1627 	kern_uuidgen(&uuid, 1);
1628 	sbuf_printf_uuid(sb, &uuid);
1629 	sbuf_printf(sb, "\n");
1630 	return(0);
1631 }
1632 
1633 /*
1634  * Filler function for proc/pid/auxv
1635  */
1636 static int
1637 linprocfs_doauxv(PFS_FILL_ARGS)
1638 {
1639 	struct sbuf *asb;
1640 	off_t buflen, resid;
1641 	int error;
1642 
1643 	/*
1644 	 * Mimic linux behavior and pass only processes with usermode
1645 	 * address space as valid. Return zero silently otherwise.
1646 	 */
1647 	if (p->p_vmspace == &vmspace0)
1648 		return (0);
1649 
1650 	if (uio->uio_resid == 0)
1651 		return (0);
1652 	if (uio->uio_offset < 0 || uio->uio_resid < 0)
1653 		return (EINVAL);
1654 
1655 	asb = sbuf_new_auto();
1656 	if (asb == NULL)
1657 		return (ENOMEM);
1658 	error = proc_getauxv(td, p, asb);
1659 	if (error == 0)
1660 		error = sbuf_finish(asb);
1661 
1662 	resid = sbuf_len(asb) - uio->uio_offset;
1663 	if (resid > uio->uio_resid)
1664 		buflen = uio->uio_resid;
1665 	else
1666 		buflen = resid;
1667 	if (buflen > IOSIZE_MAX)
1668 		return (EINVAL);
1669 	if (buflen > MAXPHYS)
1670 		buflen = MAXPHYS;
1671 	if (resid <= 0)
1672 		return (0);
1673 
1674 	if (error == 0)
1675 		error = uiomove(sbuf_data(asb) + uio->uio_offset, buflen, uio);
1676 	sbuf_delete(asb);
1677 	return (error);
1678 }
1679 
1680 /*
1681  * Constructor
1682  */
1683 static int
1684 linprocfs_init(PFS_INIT_ARGS)
1685 {
1686 	struct pfs_node *root;
1687 	struct pfs_node *dir;
1688 	struct pfs_node *sys;
1689 
1690 	root = pi->pi_root;
1691 
1692 	/* /proc/... */
1693 	pfs_create_file(root, "cmdline", &linprocfs_docmdline,
1694 	    NULL, NULL, NULL, PFS_RD);
1695 	pfs_create_file(root, "cpuinfo", &linprocfs_docpuinfo,
1696 	    NULL, NULL, NULL, PFS_RD);
1697 	pfs_create_file(root, "devices", &linprocfs_dodevices,
1698 	    NULL, NULL, NULL, PFS_RD);
1699 	pfs_create_file(root, "filesystems", &linprocfs_dofilesystems,
1700 	    NULL, NULL, NULL, PFS_RD);
1701 	pfs_create_file(root, "loadavg", &linprocfs_doloadavg,
1702 	    NULL, NULL, NULL, PFS_RD);
1703 	pfs_create_file(root, "meminfo", &linprocfs_domeminfo,
1704 	    NULL, NULL, NULL, PFS_RD);
1705 #if 0
1706 	pfs_create_file(root, "modules", &linprocfs_domodules,
1707 	    NULL, NULL, NULL, PFS_RD);
1708 #endif
1709 	pfs_create_file(root, "mounts", &linprocfs_domtab,
1710 	    NULL, NULL, NULL, PFS_RD);
1711 	pfs_create_file(root, "mtab", &linprocfs_domtab,
1712 	    NULL, NULL, NULL, PFS_RD);
1713 	pfs_create_file(root, "partitions", &linprocfs_dopartitions,
1714 	    NULL, NULL, NULL, PFS_RD);
1715 	pfs_create_link(root, "self", &procfs_docurproc,
1716 	    NULL, NULL, NULL, 0);
1717 	pfs_create_file(root, "stat", &linprocfs_dostat,
1718 	    NULL, NULL, NULL, PFS_RD);
1719 	pfs_create_file(root, "swaps", &linprocfs_doswaps,
1720 	    NULL, NULL, NULL, PFS_RD);
1721 	pfs_create_file(root, "uptime", &linprocfs_douptime,
1722 	    NULL, NULL, NULL, PFS_RD);
1723 	pfs_create_file(root, "version", &linprocfs_doversion,
1724 	    NULL, NULL, NULL, PFS_RD);
1725 
1726 	/* /proc/net/... */
1727 	dir = pfs_create_dir(root, "net", NULL, NULL, NULL, 0);
1728 	pfs_create_file(dir, "dev", &linprocfs_donetdev,
1729 	    NULL, NULL, NULL, PFS_RD);
1730 
1731 	/* /proc/<pid>/... */
1732 	dir = pfs_create_dir(root, "pid", NULL, NULL, NULL, PFS_PROCDEP);
1733 	pfs_create_file(dir, "cmdline", &linprocfs_doproccmdline,
1734 	    NULL, NULL, NULL, PFS_RD);
1735 	pfs_create_link(dir, "cwd", &linprocfs_doproccwd,
1736 	    NULL, NULL, NULL, 0);
1737 	pfs_create_file(dir, "environ", &linprocfs_doprocenviron,
1738 	    NULL, &procfs_candebug, NULL, PFS_RD);
1739 	pfs_create_link(dir, "exe", &procfs_doprocfile,
1740 	    NULL, &procfs_notsystem, NULL, 0);
1741 	pfs_create_file(dir, "maps", &linprocfs_doprocmaps,
1742 	    NULL, NULL, NULL, PFS_RD);
1743 	pfs_create_file(dir, "mem", &procfs_doprocmem,
1744 	    procfs_attr_rw, &procfs_candebug, NULL, PFS_RDWR | PFS_RAW);
1745 	pfs_create_file(dir, "mounts", &linprocfs_domtab,
1746 	    NULL, NULL, NULL, PFS_RD);
1747 	pfs_create_link(dir, "root", &linprocfs_doprocroot,
1748 	    NULL, NULL, NULL, 0);
1749 	pfs_create_file(dir, "stat", &linprocfs_doprocstat,
1750 	    NULL, NULL, NULL, PFS_RD);
1751 	pfs_create_file(dir, "statm", &linprocfs_doprocstatm,
1752 	    NULL, NULL, NULL, PFS_RD);
1753 	pfs_create_file(dir, "status", &linprocfs_doprocstatus,
1754 	    NULL, NULL, NULL, PFS_RD);
1755 	pfs_create_link(dir, "fd", &linprocfs_dofdescfs,
1756 	    NULL, NULL, NULL, 0);
1757 	pfs_create_file(dir, "auxv", &linprocfs_doauxv,
1758 	    NULL, &procfs_candebug, NULL, PFS_RD|PFS_RAWRD);
1759 	pfs_create_file(dir, "limits", &linprocfs_doproclimits,
1760 	    NULL, NULL, NULL, PFS_RD);
1761 
1762 	/* /proc/scsi/... */
1763 	dir = pfs_create_dir(root, "scsi", NULL, NULL, NULL, 0);
1764 	pfs_create_file(dir, "device_info", &linprocfs_doscsidevinfo,
1765 	    NULL, NULL, NULL, PFS_RD);
1766 	pfs_create_file(dir, "scsi", &linprocfs_doscsiscsi,
1767 	    NULL, NULL, NULL, PFS_RD);
1768 
1769 	/* /proc/sys/... */
1770 	sys = pfs_create_dir(root, "sys", NULL, NULL, NULL, 0);
1771 	/* /proc/sys/kernel/... */
1772 	dir = pfs_create_dir(sys, "kernel", NULL, NULL, NULL, 0);
1773 	pfs_create_file(dir, "osrelease", &linprocfs_doosrelease,
1774 	    NULL, NULL, NULL, PFS_RD);
1775 	pfs_create_file(dir, "ostype", &linprocfs_doostype,
1776 	    NULL, NULL, NULL, PFS_RD);
1777 	pfs_create_file(dir, "version", &linprocfs_doosbuild,
1778 	    NULL, NULL, NULL, PFS_RD);
1779 	pfs_create_file(dir, "msgmni", &linprocfs_domsgmni,
1780 	    NULL, NULL, NULL, PFS_RD);
1781 	pfs_create_file(dir, "pid_max", &linprocfs_dopid_max,
1782 	    NULL, NULL, NULL, PFS_RD);
1783 	pfs_create_file(dir, "sem", &linprocfs_dosem,
1784 	    NULL, NULL, NULL, PFS_RD);
1785 
1786 	/* /proc/sys/kernel/random/... */
1787 	dir = pfs_create_dir(dir, "random", NULL, NULL, NULL, 0);
1788 	pfs_create_file(dir, "uuid", &linprocfs_douuid,
1789 	    NULL, NULL, NULL, PFS_RD);
1790 
1791 	/* /proc/sys/vm/.... */
1792 	dir = pfs_create_dir(sys, "vm", NULL, NULL, NULL, 0);
1793 	pfs_create_file(dir, "min_free_kbytes", &linprocfs_dominfree,
1794 	    NULL, NULL, NULL, PFS_RD);
1795 
1796 	return (0);
1797 }
1798 
1799 /*
1800  * Destructor
1801  */
1802 static int
1803 linprocfs_uninit(PFS_INIT_ARGS)
1804 {
1805 
1806 	/* nothing to do, pseudofs will GC */
1807 	return (0);
1808 }
1809 
1810 PSEUDOFS(linprocfs, 1, VFCF_JAIL);
1811 #if defined(__aarch64__) || defined(__amd64__)
1812 MODULE_DEPEND(linprocfs, linux_common, 1, 1, 1);
1813 #else
1814 MODULE_DEPEND(linprocfs, linux, 1, 1, 1);
1815 #endif
1816 MODULE_DEPEND(linprocfs, procfs, 1, 1, 1);
1817 MODULE_DEPEND(linprocfs, sysvmsg, 1, 1, 1);
1818 MODULE_DEPEND(linprocfs, sysvsem, 1, 1, 1);
1819