2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (c) 2000 Dag-Erling Smørgrav
5 * Copyright (c) 1999 Pierre Beyssac
6 * Copyright (c) 1993 Jan-Simon Pendry
8 * The Regents of the University of California. All rights reserved.
10 * This code is derived from software contributed to Berkeley by
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
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.
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
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/blist.h>
49 #include <sys/fcntl.h>
50 #include <sys/filedesc.h>
52 #include <sys/kernel.h>
53 #include <sys/limits.h>
54 #include <sys/linker.h>
56 #include <sys/malloc.h>
58 #include <sys/mutex.h>
59 #include <sys/namei.h>
61 #include <sys/ptrace.h>
62 #include <sys/queue.h>
63 #include <sys/resourcevar.h>
64 #include <sys/resource.h>
69 #include <sys/socket.h>
70 #include <sys/syscallsubr.h>
71 #include <sys/sysctl.h>
72 #include <sys/sysent.h>
77 #include <sys/vmmeter.h>
78 #include <sys/vnode.h>
83 #include <net/if_var.h>
84 #include <net/if_types.h>
86 #include <net/route.h>
87 #include <net/route/nhop.h>
88 #include <net/route/route_ctl.h>
91 #include <vm/vm_extern.h>
93 #include <vm/vm_map.h>
94 #include <vm/vm_param.h>
95 #include <vm/vm_object.h>
96 #include <vm/swap_pager.h>
98 #include <machine/clock.h>
100 #include <geom/geom.h>
101 #include <geom/geom_int.h>
103 #if defined(__i386__) || defined(__amd64__)
104 #include <machine/cputypes.h>
105 #include <machine/md_var.h>
106 #endif /* __i386__ || __amd64__ */
108 #include <compat/linux/linux.h>
109 #include <compat/linux/linux_common.h>
110 #include <compat/linux/linux_emul.h>
111 #include <compat/linux/linux_mib.h>
112 #include <compat/linux/linux_misc.h>
113 #include <compat/linux/linux_util.h>
114 #include <fs/pseudofs/pseudofs.h>
115 #include <fs/procfs/procfs.h>
118 * Various conversion macros
120 #define T2J(x) ((long)(((x) * 100ULL) / (stathz ? stathz : hz))) /* ticks to jiffies */
121 #define T2CS(x) ((unsigned long)(((x) * 100ULL) / (stathz ? stathz : hz))) /* ticks to centiseconds */
122 #define T2S(x) ((x) / (stathz ? stathz : hz)) /* ticks to seconds */
123 #define B2K(x) ((x) >> 10) /* bytes to kbytes */
124 #define B2P(x) ((x) >> PAGE_SHIFT) /* bytes to pages */
125 #define P2B(x) ((x) << PAGE_SHIFT) /* pages to bytes */
126 #define P2K(x) ((x) << (PAGE_SHIFT - 10)) /* pages to kbytes */
127 #define TV2J(x) ((x)->tv_sec * 100UL + (x)->tv_usec / 10000)
130 * @brief Mapping of ki_stat in struct kinfo_proc to the linux state
132 * The linux procfs state field displays one of the characters RSDZTW to
133 * denote running, sleeping in an interruptible wait, waiting in an
134 * uninterruptible disk sleep, a zombie process, process is being traced
135 * or stopped, or process is paging respectively.
137 * Our struct kinfo_proc contains the variable ki_stat which contains a
138 * value out of SIDL, SRUN, SSLEEP, SSTOP, SZOMB, SWAIT and SLOCK.
140 * This character array is used with ki_stati-1 as an index and tries to
141 * map our states to suitable linux states.
143 static char linux_state[] = "RRSTZDD";
146 * Filler function for proc/meminfo
149 linprocfs_domeminfo(PFS_FILL_ARGS)
151 unsigned long memtotal; /* total memory in bytes */
152 unsigned long memfree; /* free memory in bytes */
153 unsigned long cached; /* page cache */
154 unsigned long buffers; /* buffer cache */
155 unsigned long long swaptotal; /* total swap space in bytes */
156 unsigned long long swapused; /* used swap space in bytes */
157 unsigned long long swapfree; /* free swap space in bytes */
161 memtotal = physmem * PAGE_SIZE;
162 memfree = (unsigned long)vm_free_count() * PAGE_SIZE;
163 swap_pager_status(&i, &j);
164 swaptotal = (unsigned long long)i * PAGE_SIZE;
165 swapused = (unsigned long long)j * PAGE_SIZE;
166 swapfree = swaptotal - swapused;
169 * This value may exclude wired pages, but we have no good way of
170 * accounting for that.
173 (vm_active_count() + vm_inactive_count() + vm_laundry_count()) *
176 sz = sizeof(buffers);
177 error = kernel_sysctlbyname(curthread, "vfs.bufspace", &buffers, &sz,
183 "MemTotal: %9lu kB\n"
187 "SwapTotal:%9llu kB\n"
188 "SwapFree: %9llu kB\n",
189 B2K(memtotal), B2K(memfree), B2K(buffers),
190 B2K(cached), B2K(swaptotal), B2K(swapfree));
195 #if defined(__i386__) || defined(__amd64__)
197 * Filler function for proc/cpuinfo (i386 & amd64 version)
200 linprocfs_docpuinfo(PFS_FILL_ARGS)
207 u_int regs[4] = { 0 };
212 * We default the flags to include all non-conflicting flags,
213 * and the Intel versions of conflicting flags.
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"
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"
237 static char *cpu_feature2_names[] = {
238 /* 0 */ "pni", "pclmulqdq", "dtes64", "monitor",
239 /* 4 */ "ds_cpl", "vmx", "smx", "est",
240 /* 8 */ "tm2", "ssse3", "cid", "sdbg",
241 /* 12 */ "fma", "cx16", "xtpr", "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"
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", "", ""
259 static char *cpu_stdext_feature_names[] = {
260 /* 0 */ "fsgsbase", "tsc_adjust", "sgx", "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"
270 static char *cpu_stdext_feature2_names[] = {
271 /* 0 */ "prefetchwt1", "avx512vbmi", "umip", "pku",
272 /* 4 */ "ospke", "waitpkg", "avx512_vbmi2", "",
273 /* 8 */ "gfni", "vaes", "vpclmulqdq", "avx512_vnni",
274 /* 12 */ "avx512_bitalg", "", "avx512_vpopcntdq", "",
275 /* 16 */ "", "", "", "",
276 /* 20 */ "", "", "rdpid", "",
277 /* 24 */ "", "cldemote", "", "movdiri",
278 /* 28 */ "movdir64b", "enqcmd", "sgx_lc", ""
281 static char *cpu_stdext_feature3_names[] = {
282 /* 0 */ "", "", "avx512_4vnniw", "avx512_4fmaps",
283 /* 4 */ "fsrm", "", "", "",
284 /* 8 */ "avx512_vp2intersect", "", "md_clear", "",
285 /* 12 */ "", "", "", "",
286 /* 16 */ "", "", "pconfig", "",
287 /* 20 */ "", "", "", "",
288 /* 24 */ "", "", "ibrs", "stibp",
289 /* 28 */ "flush_l1d", "arch_capabilities", "core_capabilities", "ssbd"
292 static char *cpu_stdext_feature_l1_names[] = {
293 /* 0 */ "xsaveopt", "xsavec", "xgetbv1", "xsaves",
297 static char *power_flags[] = {
300 "100mhzsteps", "hwpstate", "",
301 "cpb", "eff_freq_ro", "proc_feedback",
305 hw_model[0] = CTL_HW;
306 hw_model[1] = HW_MODEL;
308 size = sizeof(model);
309 if (kernel_sysctl(td, hw_model, 2, &model, &size, 0, 0, 0, 0) != 0)
310 strcpy(model, "unknown");
312 switch (cpu_vendor_id) {
314 if (cpu_class < CPUCLASS_686)
315 cpu_feature_names[16] = "fcmov";
317 case CPU_VENDOR_CYRIX:
318 cpu_feature_names[24] = "cxmmx";
322 if (cpu_exthigh >= 0x80000006)
323 do_cpuid(0x80000006, cache_size);
325 memset(cache_size, 0, sizeof(cache_size));
326 for (i = 0; i < mp_ncpus; ++i) {
329 freq = atomic_load_acq_64(&tsc_freq);
331 fqmhz = (freq + 4999) / 1000000;
332 fqkhz = ((freq + 4999) / 10000) % 100;
336 "vendor_id\t: %.20s\n"
341 "cpu MHz\t\t: %d.%02d\n"
342 "cache size\t: %d KB\n"
343 "physical id\t: %d\n"
348 "initial apicid\t: %d\n"
350 "fpu_exception\t: %s\n"
351 "cpuid level\t: %d\n"
353 i, cpu_vendor, CPUID_TO_FAMILY(cpu_id),
354 CPUID_TO_MODEL(cpu_id), model, cpu_id & CPUID_STEPPING,
356 (cache_size[2] >> 16), 0, mp_ncpus, i, mp_ncpus,
357 i, i, /*cpu_id & CPUID_LOCAL_APIC_ID ??*/
358 (cpu_feature & CPUID_FPU) ? "yes" : "no", "yes",
359 CPUID_TO_FAMILY(cpu_id), "yes");
360 sbuf_cat(sb, "flags\t\t:");
361 for (j = 0; j < nitems(cpu_feature_names); j++)
362 if (cpu_feature & (1 << j) &&
363 cpu_feature_names[j][0] != '\0')
364 sbuf_printf(sb, " %s", cpu_feature_names[j]);
365 for (j = 0; j < nitems(amd_feature_names); j++)
366 if (amd_feature & (1 << j) &&
367 amd_feature_names[j][0] != '\0')
368 sbuf_printf(sb, " %s", amd_feature_names[j]);
369 for (j = 0; j < nitems(cpu_feature2_names); j++)
370 if (cpu_feature2 & (1 << j) &&
371 cpu_feature2_names[j][0] != '\0')
372 sbuf_printf(sb, " %s", cpu_feature2_names[j]);
373 for (j = 0; j < nitems(amd_feature2_names); j++)
374 if (amd_feature2 & (1 << j) &&
375 amd_feature2_names[j][0] != '\0')
376 sbuf_printf(sb, " %s", amd_feature2_names[j]);
377 for (j = 0; j < nitems(cpu_stdext_feature_names); j++)
378 if (cpu_stdext_feature & (1 << j) &&
379 cpu_stdext_feature_names[j][0] != '\0')
380 sbuf_printf(sb, " %s",
381 cpu_stdext_feature_names[j]);
382 if (tsc_is_invariant)
383 sbuf_cat(sb, " constant_tsc");
384 for (j = 0; j < nitems(cpu_stdext_feature2_names); j++)
385 if (cpu_stdext_feature2 & (1 << j) &&
386 cpu_stdext_feature2_names[j][0] != '\0')
387 sbuf_printf(sb, " %s",
388 cpu_stdext_feature2_names[j]);
389 for (j = 0; j < nitems(cpu_stdext_feature3_names); j++)
390 if (cpu_stdext_feature3 & (1 << j) &&
391 cpu_stdext_feature3_names[j][0] != '\0')
392 sbuf_printf(sb, " %s",
393 cpu_stdext_feature3_names[j]);
394 if ((cpu_feature2 & CPUID2_XSAVE) != 0) {
395 cpuid_count(0xd, 0x1, regs);
396 for (j = 0; j < nitems(cpu_stdext_feature_l1_names); j++)
397 if (regs[0] & (1 << j) &&
398 cpu_stdext_feature_l1_names[j][0] != '\0')
399 sbuf_printf(sb, " %s",
400 cpu_stdext_feature_l1_names[j]);
405 "bogomips\t: %d.%02d\n"
406 "clflush size\t: %d\n"
407 "cache_alignment\t: %d\n"
408 "address sizes\t: %d bits physical, %d bits virtual\n",
409 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
410 (has_f00f_bug) ? "Intel F00F" : "",
415 cpu_clflush_line_size, cpu_clflush_line_size,
417 (cpu_maxphyaddr > 32) ? 48 : 0);
418 sbuf_cat(sb, "power management: ");
419 for (j = 0; j < nitems(power_flags); j++)
420 if (amd_pminfo & (1 << j))
421 sbuf_printf(sb, " %s", power_flags[j]);
422 sbuf_cat(sb, "\n\n");
424 /* XXX per-cpu vendor / class / model / id? */
431 /* ARM64TODO: implement non-stubbed linprocfs_docpuinfo */
433 linprocfs_docpuinfo(PFS_FILL_ARGS)
437 for (i = 0; i < mp_ncpus; ++i) {
440 "BogoMIPS\t: %d.%02d\n",
442 sbuf_cat(sb, "Features\t: ");
445 "CPU implementer\t: \n"
446 "CPU architecture: \n"
447 "CPU variant\t: 0x%x\n"
449 "CPU revision\t: %d\n",
456 #endif /* __i386__ || __amd64__ */
458 static const char *path_slash_sys = "/sys";
459 static const char *fstype_sysfs = "sysfs";
462 _mtab_helper(const struct pfs_node *pn, const struct statfs *sp,
463 const char **mntfrom, const char **mntto, const char **fstype)
465 /* determine device name */
466 *mntfrom = sp->f_mntfromname;
468 /* determine mount point */
469 *mntto = sp->f_mntonname;
471 /* determine fs type */
472 *fstype = sp->f_fstypename;
473 if (strcmp(*fstype, pn->pn_info->pi_name) == 0)
474 *mntfrom = *fstype = "proc";
475 else if (strcmp(*fstype, "procfs") == 0)
478 if (strcmp(*fstype, "autofs") == 0) {
480 * FreeBSD uses eg "map -hosts", whereas Linux
481 * expects just "-hosts".
483 if (strncmp(*mntfrom, "map ", 4) == 0)
487 if (strcmp(*fstype, "linsysfs") == 0) {
488 *mntfrom = path_slash_sys;
489 *fstype = fstype_sysfs;
491 /* For Linux msdosfs is called vfat */
492 if (strcmp(*fstype, "msdosfs") == 0)
499 _sbuf_mntoptions_helper(struct sbuf *sb, uint64_t f_flags)
501 sbuf_cat(sb, (f_flags & MNT_RDONLY) ? "ro" : "rw");
502 #define ADD_OPTION(opt, name) \
503 if (f_flags & (opt)) sbuf_cat(sb, "," name);
504 ADD_OPTION(MNT_SYNCHRONOUS, "sync");
505 ADD_OPTION(MNT_NOEXEC, "noexec");
506 ADD_OPTION(MNT_NOSUID, "nosuid");
507 ADD_OPTION(MNT_UNION, "union");
508 ADD_OPTION(MNT_ASYNC, "async");
509 ADD_OPTION(MNT_SUIDDIR, "suiddir");
510 ADD_OPTION(MNT_NOSYMFOLLOW, "nosymfollow");
511 ADD_OPTION(MNT_NOATIME, "noatime");
516 * Filler function for proc/mtab and proc/<pid>/mounts.
518 * /proc/mtab doesn't exist in Linux' procfs, but is included here so
519 * users can symlink /compat/linux/etc/mtab to /proc/mtab
522 linprocfs_domtab(PFS_FILL_ARGS)
524 const char *mntto, *mntfrom, *fstype;
530 struct statfs *buf, *sp;
534 * Resolve emulation tree prefix
539 error = vn_fullpath_global(vp, &dlep, &flep);
543 lep_len = strlen(dlep);
546 error = kern_getfsstat(td, &buf, SIZE_T_MAX, &count,
547 UIO_SYSSPACE, MNT_WAIT);
554 for (sp = buf; count > 0; sp++, count--) {
555 error = _mtab_helper(pn, sp, &mntfrom, &mntto, &fstype);
557 MPASS(error == ECANCELED);
561 /* determine mount point */
562 if (strncmp(mntto, dlep, lep_len) == 0 && mntto[lep_len] == '/')
565 sbuf_printf(sb, "%s %s %s ", mntfrom, mntto, fstype);
566 _sbuf_mntoptions_helper(sb, sp->f_flags);
567 /* a real Linux mtab will also show NFS options */
568 sbuf_printf(sb, " 0 0\n");
577 linprocfs_doprocmountinfo(PFS_FILL_ARGS)
579 const char *mntfrom, *mntto, *fstype;
581 struct statfs *buf, *sp;
582 size_t count, lep_len;
588 * Resolve emulation tree prefix
593 error = vn_fullpath_global(vp, &dlep, &flep);
597 lep_len = strlen(dlep);
600 error = kern_getfsstat(td, &buf, SIZE_T_MAX, &count,
601 UIO_SYSSPACE, MNT_WAIT);
605 for (sp = buf; count > 0; sp++, count--) {
606 error = _mtab_helper(pn, sp, &mntfrom, &mntto, &fstype);
608 MPASS(error == ECANCELED);
612 if (strncmp(mntto, dlep, lep_len) == 0 && mntto[lep_len] == '/')
616 * If the prefix is a chroot, and this mountpoint is not under
617 * the prefix, we should skip it. Leave it for now for
618 * consistency with procmtab above.
627 * (2) parent mount id -- we don't have this cheaply, so
628 * provide a dummy value
630 * (3) major:minor -- ditto
632 * (4) root filesystem mount -- probably a namespaces thing
636 sbuf_printf(sb, "%u 0 0:0 / %s ",
637 sp->f_fsid.val[0] ^ sp->f_fsid.val[1], mntto);
638 /* (6) mount options */
639 _sbuf_mntoptions_helper(sb, sp->f_flags);
641 * (7) zero or more optional fields -- again, namespace related
643 * (8) End of variable length fields separator ("-")
649 * (11) "superblock" options -- like (6), but different
652 sbuf_printf(sb, " - %s %s %s\n", fstype, mntfrom,
653 (sp->f_flags & MNT_RDONLY) ? "ro" : "rw");
664 * Filler function for proc/partitions
667 linprocfs_dopartitions(PFS_FILL_ARGS)
671 struct g_provider *pp;
675 sbuf_printf(sb, "major minor #blocks name rio rmerge rsect "
676 "ruse wio wmerge wsect wuse running use aveq\n");
678 LIST_FOREACH(cp, &g_classes, class) {
679 if (strcmp(cp->name, "DISK") == 0 ||
680 strcmp(cp->name, "PART") == 0)
681 LIST_FOREACH(gp, &cp->geom, geom) {
682 LIST_FOREACH(pp, &gp->provider, provider) {
683 if (linux_driver_get_major_minor(
684 pp->name, &major, &minor) != 0) {
688 sbuf_printf(sb, "%d %d %lld %s "
690 "%d %d %d %d %d %d\n",
692 (long long)pp->mediasize, pp->name,
704 * Filler function for proc/stat
706 * Output depends on kernel version:
709 * user nice system idle
711 * user nice system idle iowait
713 * user nice system idle iowait irq softirq steal
715 * user nice system idle iowait irq softirq steal guest
717 * user nice system idle iowait irq softirq steal guest guest_nice
720 linprocfs_dostat(PFS_FILL_ARGS)
723 long cp_time[CPUSTATES];
725 struct timeval boottime;
728 bool has_intr = true;
730 if (linux_kernver(td) >= LINUX_KERNVER(2,6,33)) {
731 zero_pad = " 0 0 0 0\n";
732 } else if (linux_kernver(td) >= LINUX_KERNVER(2,6,24)) {
733 zero_pad = " 0 0 0\n";
734 } else if (linux_kernver(td) >= LINUX_KERNVER(2,6,11)) {
736 } else if (linux_kernver(td) >= LINUX_KERNVER(2,5,41)) {
744 read_cpu_time(cp_time);
745 getboottime(&boottime);
746 /* Parameters common to all versions */
747 sbuf_printf(sb, "cpu %lu %lu %lu %lu",
748 T2J(cp_time[CP_USER]),
749 T2J(cp_time[CP_NICE]),
750 T2J(cp_time[CP_SYS]),
751 T2J(cp_time[CP_IDLE]));
753 /* Print interrupt stats if available */
755 sbuf_printf(sb, " 0 %lu", T2J(cp_time[CP_INTR]));
758 /* Pad out remaining fields depending on version */
759 sbuf_printf(sb, "%s", zero_pad);
763 cp = pcpu->pc_cp_time;
764 sbuf_printf(sb, "cpu%d %lu %lu %lu %lu", i,
771 sbuf_printf(sb, " 0 %lu", T2J(cp[CP_INTR]));
774 sbuf_printf(sb, "%s", zero_pad);
783 (uintmax_t)VM_CNT_FETCH(v_vnodepgsin),
784 (uintmax_t)VM_CNT_FETCH(v_vnodepgsout),
785 (uintmax_t)VM_CNT_FETCH(v_swappgsin),
786 (uintmax_t)VM_CNT_FETCH(v_swappgsout),
787 (uintmax_t)VM_CNT_FETCH(v_intr),
788 (uintmax_t)VM_CNT_FETCH(v_swtch),
789 (long long)boottime.tv_sec);
794 linprocfs_doswaps(PFS_FILL_ARGS)
797 uintmax_t total, used;
799 char devname[SPECNAMELEN + 1];
801 sbuf_printf(sb, "Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n");
803 if (swap_dev_info(n, &xsw, devname, sizeof(devname)) != 0)
805 total = (uintmax_t)xsw.xsw_nblks * PAGE_SIZE / 1024;
806 used = (uintmax_t)xsw.xsw_used * PAGE_SIZE / 1024;
809 * The space and not tab after the device name is on
810 * purpose. Linux does so.
812 sbuf_printf(sb, "/dev/%-34s unknown\t\t%jd\t%jd\t-1\n",
813 devname, total, used);
819 * Filler function for proc/uptime
822 linprocfs_douptime(PFS_FILL_ARGS)
824 long cp_time[CPUSTATES];
828 read_cpu_time(cp_time);
829 sbuf_printf(sb, "%lld.%02ld %ld.%02lu\n",
830 (long long)tv.tv_sec, tv.tv_usec / 10000,
831 T2S(cp_time[CP_IDLE] / mp_ncpus),
832 T2CS(cp_time[CP_IDLE] / mp_ncpus) % 100);
840 linprocfs_osbuild(struct thread *td, struct sbuf *sb)
846 strncpy(osbuild, version, 256);
848 cp1 = strstr(osbuild, "\n");
849 cp2 = strstr(osbuild, ":");
852 cp1 = strstr(osbuild, "#");
856 sbuf_printf(sb, "%s%s", cp1, cp2 + 1);
859 sbuf_cat(sb, "#4 Sun Dec 18 04:30:00 CET 1977");
866 linprocfs_osbuilder(struct thread *td, struct sbuf *sb)
872 cp = strstr(version, "\n ");
874 strncpy(builder, cp + 5, 256);
876 cp = strstr(builder, ":");
881 sbuf_cat(sb, builder);
884 sbuf_cat(sb, "des@freebsd.org");
888 * Filler function for proc/version
891 linprocfs_doversion(PFS_FILL_ARGS)
893 char osname[LINUX_MAX_UTSNAME];
894 char osrelease[LINUX_MAX_UTSNAME];
896 linux_get_osname(td, osname);
897 linux_get_osrelease(td, osrelease);
898 sbuf_printf(sb, "%s version %s (", osname, osrelease);
899 linprocfs_osbuilder(td, sb);
900 sbuf_cat(sb, ") (gcc version " __VERSION__ ") ");
901 linprocfs_osbuild(td, sb);
908 * Filler function for proc/loadavg
911 linprocfs_doloadavg(PFS_FILL_ARGS)
915 "%d.%02d %d.%02d %d.%02d %d/%d %d\n",
916 (int)(averunnable.ldavg[0] / averunnable.fscale),
917 (int)(averunnable.ldavg[0] * 100 / averunnable.fscale % 100),
918 (int)(averunnable.ldavg[1] / averunnable.fscale),
919 (int)(averunnable.ldavg[1] * 100 / averunnable.fscale % 100),
920 (int)(averunnable.ldavg[2] / averunnable.fscale),
921 (int)(averunnable.ldavg[2] * 100 / averunnable.fscale % 100),
922 1, /* number of running tasks */
923 nprocs, /* number of tasks */
924 lastpid /* the last pid */
930 linprocfs_get_tty_nr(struct proc *p)
934 int error, major, minor, nr;
936 PROC_LOCK_ASSERT(p, MA_OWNED);
937 sx_assert(&proctree_lock, SX_LOCKED);
939 if ((p->p_flag & P_CONTROLT) == 0)
942 sp = p->p_pgrp->pg_session;
946 ttyname = devtoname(sp->s_ttyp->t_dev);
947 error = linux_driver_get_major_minor(ttyname, &major, &minor);
951 nr = makedev(major, minor);
956 * Filler function for proc/pid/stat
959 linprocfs_doprocstat(PFS_FILL_ARGS)
961 struct kinfo_proc kp;
962 struct timeval boottime;
964 static int ratelimit = 0;
966 vm_offset_t startcode, startdata;
968 getboottime(&boottime);
969 sx_slock(&proctree_lock);
971 fill_kinfo_proc(p, &kp);
972 tty_nr = linprocfs_get_tty_nr(p);
973 sx_sunlock(&proctree_lock);
975 startcode = (vm_offset_t)p->p_vmspace->vm_taddr;
976 startdata = (vm_offset_t)p->p_vmspace->vm_daddr;
981 sbuf_printf(sb, "%d", p->p_pid);
982 #define PS_ADD(name, fmt, arg) sbuf_printf(sb, " " fmt, arg)
983 PS_ADD("comm", "(%s)", p->p_comm);
984 if (kp.ki_stat > sizeof(linux_state)) {
987 if (ratelimit == 0) {
988 printf("linprocfs: don't know how to handle unknown FreeBSD state %d/%zd, mapping to R\n",
989 kp.ki_stat, sizeof(linux_state));
993 state = linux_state[kp.ki_stat - 1];
994 PS_ADD("state", "%c", state);
995 PS_ADD("ppid", "%d", p->p_pptr ? p->p_pptr->p_pid : 0);
996 PS_ADD("pgrp", "%d", p->p_pgid);
997 PS_ADD("session", "%d", p->p_session->s_sid);
999 PS_ADD("tty", "%d", tty_nr);
1000 PS_ADD("tpgid", "%d", kp.ki_tpgid);
1001 PS_ADD("flags", "%u", 0); /* XXX */
1002 PS_ADD("minflt", "%lu", kp.ki_rusage.ru_minflt);
1003 PS_ADD("cminflt", "%lu", kp.ki_rusage_ch.ru_minflt);
1004 PS_ADD("majflt", "%lu", kp.ki_rusage.ru_majflt);
1005 PS_ADD("cmajflt", "%lu", kp.ki_rusage_ch.ru_majflt);
1006 PS_ADD("utime", "%ld", TV2J(&kp.ki_rusage.ru_utime));
1007 PS_ADD("stime", "%ld", TV2J(&kp.ki_rusage.ru_stime));
1008 PS_ADD("cutime", "%ld", TV2J(&kp.ki_rusage_ch.ru_utime));
1009 PS_ADD("cstime", "%ld", TV2J(&kp.ki_rusage_ch.ru_stime));
1010 PS_ADD("priority", "%d", kp.ki_pri.pri_user);
1011 PS_ADD("nice", "%d", kp.ki_nice); /* 19 (nicest) to -19 */
1012 PS_ADD("0", "%d", 0); /* removed field */
1013 PS_ADD("itrealvalue", "%d", 0); /* XXX */
1014 PS_ADD("starttime", "%lu", TV2J(&kp.ki_start) - TV2J(&boottime));
1015 PS_ADD("vsize", "%ju", (uintmax_t)kp.ki_size);
1016 PS_ADD("rss", "%ju", (uintmax_t)kp.ki_rssize);
1017 PS_ADD("rlim", "%lu", kp.ki_rusage.ru_maxrss);
1018 PS_ADD("startcode", "%ju", (uintmax_t)startcode);
1019 PS_ADD("endcode", "%ju", (uintmax_t)startdata);
1020 PS_ADD("startstack", "%u", 0); /* XXX */
1021 PS_ADD("kstkesp", "%u", 0); /* XXX */
1022 PS_ADD("kstkeip", "%u", 0); /* XXX */
1023 PS_ADD("signal", "%u", 0); /* XXX */
1024 PS_ADD("blocked", "%u", 0); /* XXX */
1025 PS_ADD("sigignore", "%u", 0); /* XXX */
1026 PS_ADD("sigcatch", "%u", 0); /* XXX */
1027 PS_ADD("wchan", "%u", 0); /* XXX */
1028 PS_ADD("nswap", "%lu", kp.ki_rusage.ru_nswap);
1029 PS_ADD("cnswap", "%lu", kp.ki_rusage_ch.ru_nswap);
1030 PS_ADD("exitsignal", "%d", 0); /* XXX */
1031 PS_ADD("processor", "%u", kp.ki_lastcpu);
1032 PS_ADD("rt_priority", "%u", 0); /* XXX */ /* >= 2.5.19 */
1033 PS_ADD("policy", "%u", kp.ki_pri.pri_class); /* >= 2.5.19 */
1035 sbuf_putc(sb, '\n');
1041 * Filler function for proc/pid/statm
1044 linprocfs_doprocstatm(PFS_FILL_ARGS)
1046 struct kinfo_proc kp;
1049 sx_slock(&proctree_lock);
1051 fill_kinfo_proc(p, &kp);
1053 sx_sunlock(&proctree_lock);
1056 * See comments in linprocfs_doprocstatus() regarding the
1057 * computation of lsize.
1059 /* size resident share trs drs lrs dt */
1060 sbuf_printf(sb, "%ju ", B2P((uintmax_t)kp.ki_size));
1061 sbuf_printf(sb, "%ju ", (uintmax_t)kp.ki_rssize);
1062 sbuf_printf(sb, "%ju ", (uintmax_t)0); /* XXX */
1063 sbuf_printf(sb, "%ju ", (uintmax_t)kp.ki_tsize);
1064 sbuf_printf(sb, "%ju ", (uintmax_t)(kp.ki_dsize + kp.ki_ssize));
1065 lsize = B2P(kp.ki_size) - kp.ki_dsize -
1066 kp.ki_ssize - kp.ki_tsize - 1;
1067 sbuf_printf(sb, "%ju ", (uintmax_t)lsize);
1068 sbuf_printf(sb, "%ju\n", (uintmax_t)0); /* XXX */
1074 * Filler function for proc/pid/status
1077 linprocfs_doprocstatus(PFS_FILL_ARGS)
1079 struct kinfo_proc kp;
1084 l_sigset_t siglist, sigignore, sigcatch;
1087 sx_slock(&proctree_lock);
1089 td2 = FIRST_THREAD_IN_PROC(p);
1091 if (P_SHOULDSTOP(p)) {
1092 state = "T (stopped)";
1094 switch(p->p_state) {
1099 if (p->p_flag & P_WEXIT) {
1100 state = "X (exiting)";
1103 switch(TD_GET_STATE(td2)) {
1105 state = "S (sleeping)";
1109 state = "R (running)";
1112 state = "? (unknown)";
1117 state = "Z (zombie)";
1120 state = "? (unknown)";
1125 fill_kinfo_proc(p, &kp);
1126 sx_sunlock(&proctree_lock);
1128 sbuf_printf(sb, "Name:\t%s\n", p->p_comm); /* XXX escape */
1129 sbuf_printf(sb, "State:\t%s\n", state);
1134 sbuf_printf(sb, "Tgid:\t%d\n", p->p_pid);
1135 sbuf_printf(sb, "Pid:\t%d\n", p->p_pid);
1136 sbuf_printf(sb, "PPid:\t%d\n", kp.ki_ppid );
1137 sbuf_printf(sb, "TracerPid:\t%d\n", kp.ki_tracer );
1138 sbuf_printf(sb, "Uid:\t%d\t%d\t%d\t%d\n", p->p_ucred->cr_ruid,
1140 p->p_ucred->cr_svuid,
1141 /* FreeBSD doesn't have fsuid */
1142 p->p_ucred->cr_uid);
1143 sbuf_printf(sb, "Gid:\t%d\t%d\t%d\t%d\n", p->p_ucred->cr_rgid,
1145 p->p_ucred->cr_svgid,
1146 /* FreeBSD doesn't have fsgid */
1147 p->p_ucred->cr_gid);
1148 sbuf_cat(sb, "Groups:\t");
1149 for (i = 0; i < p->p_ucred->cr_ngroups; i++)
1150 sbuf_printf(sb, "%d ", p->p_ucred->cr_groups[i]);
1152 sbuf_putc(sb, '\n');
1157 * While our approximation of VmLib may not be accurate (I
1158 * don't know of a simple way to verify it, and I'm not sure
1159 * it has much meaning anyway), I believe it's good enough.
1161 * The same code that could (I think) accurately compute VmLib
1162 * could also compute VmLck, but I don't really care enough to
1163 * implement it. Submissions are welcome.
1165 sbuf_printf(sb, "VmSize:\t%8ju kB\n", B2K((uintmax_t)kp.ki_size));
1166 sbuf_printf(sb, "VmLck:\t%8u kB\n", P2K(0)); /* XXX */
1167 sbuf_printf(sb, "VmRSS:\t%8ju kB\n", P2K((uintmax_t)kp.ki_rssize));
1168 sbuf_printf(sb, "VmData:\t%8ju kB\n", P2K((uintmax_t)kp.ki_dsize));
1169 sbuf_printf(sb, "VmStk:\t%8ju kB\n", P2K((uintmax_t)kp.ki_ssize));
1170 sbuf_printf(sb, "VmExe:\t%8ju kB\n", P2K((uintmax_t)kp.ki_tsize));
1171 lsize = B2P(kp.ki_size) - kp.ki_dsize -
1172 kp.ki_ssize - kp.ki_tsize - 1;
1173 sbuf_printf(sb, "VmLib:\t%8ju kB\n", P2K((uintmax_t)lsize));
1179 bsd_to_linux_sigset(&p->p_siglist, &siglist);
1181 mtx_lock(&ps->ps_mtx);
1182 bsd_to_linux_sigset(&ps->ps_sigignore, &sigignore);
1183 bsd_to_linux_sigset(&ps->ps_sigcatch, &sigcatch);
1184 mtx_unlock(&ps->ps_mtx);
1187 sbuf_printf(sb, "SigPnd:\t%016jx\n", siglist.__mask);
1189 * XXX. SigBlk - target thread's signal mask, td_sigmask.
1190 * To implement SigBlk pseudofs should support proc/tid dir entries.
1192 sbuf_printf(sb, "SigBlk:\t%016x\n", 0);
1193 sbuf_printf(sb, "SigIgn:\t%016jx\n", sigignore.__mask);
1194 sbuf_printf(sb, "SigCgt:\t%016jx\n", sigcatch.__mask);
1197 * Linux also prints the capability masks, but we don't have
1198 * capabilities yet, and when we do get them they're likely to
1199 * be meaningless to Linux programs, so we lie. XXX
1201 sbuf_printf(sb, "CapInh:\t%016x\n", 0);
1202 sbuf_printf(sb, "CapPrm:\t%016x\n", 0);
1203 sbuf_printf(sb, "CapEff:\t%016x\n", 0);
1209 * Filler function for proc/pid/cwd
1212 linprocfs_doproccwd(PFS_FILL_ARGS)
1215 char *fullpath = "unknown";
1216 char *freepath = NULL;
1218 pwd = pwd_hold_proc(p);
1219 vn_fullpath(pwd->pwd_cdir, &fullpath, &freepath);
1220 sbuf_printf(sb, "%s", fullpath);
1222 free(freepath, M_TEMP);
1228 * Filler function for proc/pid/root
1231 linprocfs_doprocroot(PFS_FILL_ARGS)
1235 char *fullpath = "unknown";
1236 char *freepath = NULL;
1238 pwd = pwd_hold_proc(p);
1239 vp = jailed(p->p_ucred) ? pwd->pwd_jdir : pwd->pwd_rdir;
1240 vn_fullpath(vp, &fullpath, &freepath);
1241 sbuf_printf(sb, "%s", fullpath);
1243 free(freepath, M_TEMP);
1249 * Filler function for proc/pid/cmdline
1252 linprocfs_doproccmdline(PFS_FILL_ARGS)
1257 if ((ret = p_cansee(td, p)) != 0) {
1263 * Mimic linux behavior and pass only processes with usermode
1264 * address space as valid. Return zero silently otherwize.
1266 if (p->p_vmspace == &vmspace0) {
1270 if (p->p_args != NULL) {
1271 sbuf_bcpy(sb, p->p_args->ar_args, p->p_args->ar_length);
1276 if ((p->p_flag & P_SYSTEM) != 0) {
1283 ret = proc_getargv(td, p, sb);
1288 * Filler function for proc/pid/environ
1291 linprocfs_doprocenviron(PFS_FILL_ARGS)
1295 * Mimic linux behavior and pass only processes with usermode
1296 * address space as valid. Return zero silently otherwize.
1298 if (p->p_vmspace == &vmspace0)
1301 return (proc_getenvv(td, p, sb));
1304 static char l32_map_str[] = "%08lx-%08lx %s%s%s%s %08lx %02x:%02x %lu%s%s\n";
1305 static char l64_map_str[] = "%016lx-%016lx %s%s%s%s %08lx %02x:%02x %lu%s%s\n";
1306 static char vdso_str[] = " [vdso]";
1307 static char stack_str[] = " [stack]";
1310 * Filler function for proc/pid/maps
1313 linprocfs_doprocmaps(PFS_FILL_ARGS)
1317 vm_map_entry_t entry, tmp_entry;
1318 vm_object_t obj, tobj, lobj;
1319 vm_offset_t e_start, e_end;
1322 unsigned int last_timestamp;
1323 char *name = "", *freename = NULL;
1324 const char *l_map_str;
1332 error = p_candebug(td, p);
1337 if (uio->uio_rw != UIO_READ)
1338 return (EOPNOTSUPP);
1341 vm = vmspace_acquire_ref(p);
1345 if (SV_CURPROC_FLAG(SV_LP64))
1346 l_map_str = l64_map_str;
1348 l_map_str = l32_map_str;
1350 vm_map_lock_read(map);
1351 VM_MAP_ENTRY_FOREACH(entry, map) {
1355 * Skip printing of the guard page of the stack region, as
1356 * it confuses glibc pthread_getattr_np() method, where both
1357 * the base address and size of the stack of the initial thread
1360 if ((entry->eflags & (MAP_ENTRY_IS_SUB_MAP | MAP_ENTRY_GUARD)) != 0)
1362 e_prot = entry->protection;
1363 e_start = entry->start;
1365 obj = entry->object.vm_object;
1366 off = entry->offset;
1367 for (lobj = tobj = obj; tobj != NULL;
1368 lobj = tobj, tobj = tobj->backing_object) {
1369 VM_OBJECT_RLOCK(tobj);
1370 off += lobj->backing_object_offset;
1372 VM_OBJECT_RUNLOCK(lobj);
1374 private = (entry->eflags & MAP_ENTRY_COW) != 0 || obj == NULL ||
1375 (obj->flags & OBJ_ANON) != 0;
1376 last_timestamp = map->timestamp;
1377 vm_map_unlock_read(map);
1380 vp = vm_object_vnode(lobj);
1384 VM_OBJECT_RUNLOCK(lobj);
1385 VM_OBJECT_RUNLOCK(obj);
1387 vn_fullpath(vp, &name, &freename);
1388 vn_lock(vp, LK_SHARED | LK_RETRY);
1389 VOP_GETATTR(vp, &vat, td->td_ucred);
1390 ino = vat.va_fileid;
1392 } else if (SV_PROC_ABI(p) == SV_ABI_LINUX) {
1394 * sv_shared_page_base pointed out to the
1395 * FreeBSD sharedpage, PAGE_SIZE is a size
1396 * of it. The vDSO page is above.
1398 if (e_start == p->p_sysent->sv_shared_page_base +
1401 if (e_end == p->p_sysent->sv_usrstack)
1408 * start, end, access, offset, major, minor, inode, name.
1410 error = sbuf_printf(sb, l_map_str,
1411 (u_long)e_start, (u_long)e_end,
1412 (e_prot & VM_PROT_READ)?"r":"-",
1413 (e_prot & VM_PROT_WRITE)?"w":"-",
1414 (e_prot & VM_PROT_EXECUTE)?"x":"-",
1415 private ? "p" : "s",
1424 free(freename, M_TEMP);
1425 vm_map_lock_read(map);
1430 if (last_timestamp != map->timestamp) {
1432 * Look again for the entry because the map was
1433 * modified while it was unlocked. Specifically,
1434 * the entry may have been clipped, merged, or deleted.
1436 vm_map_lookup_entry(map, e_end - 1, &tmp_entry);
1440 vm_map_unlock_read(map);
1447 * Filler function for proc/pid/mem
1450 linprocfs_doprocmem(PFS_FILL_ARGS)
1455 resid = uio->uio_resid;
1456 error = procfs_doprocmem(PFS_FILL_ARGNAMES);
1458 if (uio->uio_rw == UIO_READ && resid != uio->uio_resid)
1461 if (error == EFAULT)
1468 * Filler function for proc/net/dev
1471 linprocfs_donetdev_cb(if_t ifp, void *arg)
1473 char ifname[LINUX_IFNAMSIZ];
1474 struct sbuf *sb = arg;
1476 if (ifname_bsd_to_linux_ifp(ifp, ifname, sizeof(ifname)) <= 0)
1479 sbuf_printf(sb, "%6.6s: ", ifname);
1480 sbuf_printf(sb, "%7ju %7ju %4ju %4ju %4lu %5lu %10lu %9ju ",
1481 (uintmax_t)if_getcounter(ifp, IFCOUNTER_IBYTES),
1482 (uintmax_t)if_getcounter(ifp, IFCOUNTER_IPACKETS),
1483 (uintmax_t)if_getcounter(ifp, IFCOUNTER_IERRORS),
1484 (uintmax_t)if_getcounter(ifp, IFCOUNTER_IQDROPS),
1485 /* rx_missed_errors */
1486 0UL, /* rx_fifo_errors */
1487 0UL, /* rx_length_errors +
1490 * rx_frame_errors */
1491 0UL, /* rx_compressed */
1492 (uintmax_t)if_getcounter(ifp, IFCOUNTER_IMCASTS));
1493 /* XXX-BZ rx only? */
1494 sbuf_printf(sb, "%8ju %7ju %4ju %4ju %4lu %5ju %7lu %10lu\n",
1495 (uintmax_t)if_getcounter(ifp, IFCOUNTER_OBYTES),
1496 (uintmax_t)if_getcounter(ifp, IFCOUNTER_OPACKETS),
1497 (uintmax_t)if_getcounter(ifp, IFCOUNTER_OERRORS),
1498 (uintmax_t)if_getcounter(ifp, IFCOUNTER_OQDROPS),
1499 0UL, /* tx_fifo_errors */
1500 (uintmax_t)if_getcounter(ifp, IFCOUNTER_COLLISIONS),
1501 0UL, /* tx_carrier_errors +
1502 * tx_aborted_errors +
1503 * tx_window_errors +
1504 * tx_heartbeat_errors*/
1505 0UL); /* tx_compressed */
1510 linprocfs_donetdev(PFS_FILL_ARGS)
1512 struct epoch_tracker et;
1514 sbuf_printf(sb, "%6s|%58s|%s\n"
1516 "Inter-", " Receive", " Transmit",
1518 "bytes packets errs drop fifo frame compressed multicast",
1519 "bytes packets errs drop fifo colls carrier compressed");
1521 CURVNET_SET(TD_TO_VNET(curthread));
1522 NET_EPOCH_ENTER(et);
1523 if_foreach(linprocfs_donetdev_cb, sb);
1535 linux_route_print(struct rtentry *rt, void *vw)
1538 struct walkarg *w = vw;
1539 struct route_nhop_data rnd;
1540 struct in_addr dst, mask;
1541 struct nhop_object *nh;
1543 uint32_t scopeid = 0;
1545 uint32_t linux_flags = 0;
1547 rt_get_inet_prefix_pmask(rt, &dst, &mask, &scopeid);
1549 rt_get_rnd(rt, &rnd);
1551 /* select only first route in case of multipath */
1552 nh = nhop_select_func(rnd.rnd_nhop, 0);
1554 if (ifname_bsd_to_linux_ifp(nh->nh_ifp, ifname, sizeof(ifname)) <= 0)
1557 gw = (nh->nh_flags & NHF_GATEWAY)
1558 ? nh->gw4_sa.sin_addr.s_addr : 0;
1560 linux_flags = RTF_UP |
1561 (nhop_get_rtflags(nh) & (RTF_GATEWAY | RTF_HOST));
1565 "%08X\t%08X\t%04X\t"
1569 dst.s_addr, gw, linux_flags,
1570 0, 0, rnd.rnd_weight,
1571 mask.s_addr, nh->nh_mtu, 0, 0);
1573 sbuf_printf(w->sb, "\n\n");
1579 * Filler function for proc/net/route
1582 linprocfs_donetroute(PFS_FILL_ARGS)
1584 struct epoch_tracker et;
1585 struct walkarg w = {
1588 uint32_t fibnum = curthread->td_proc->p_fibnum;
1590 sbuf_printf(w.sb, "%-127s\n", "Iface\tDestination\tGateway "
1591 "\tFlags\tRefCnt\tUse\tMetric\tMask\t\tMTU"
1594 CURVNET_SET(TD_TO_VNET(curthread));
1595 NET_EPOCH_ENTER(et);
1596 rib_walk(fibnum, AF_INET, false, linux_route_print, &w);
1604 * Filler function for proc/sys/kernel/osrelease
1607 linprocfs_doosrelease(PFS_FILL_ARGS)
1609 char osrelease[LINUX_MAX_UTSNAME];
1611 linux_get_osrelease(td, osrelease);
1612 sbuf_printf(sb, "%s\n", osrelease);
1618 * Filler function for proc/sys/kernel/ostype
1621 linprocfs_doostype(PFS_FILL_ARGS)
1623 char osname[LINUX_MAX_UTSNAME];
1625 linux_get_osname(td, osname);
1626 sbuf_printf(sb, "%s\n", osname);
1632 * Filler function for proc/sys/kernel/version
1635 linprocfs_doosbuild(PFS_FILL_ARGS)
1638 linprocfs_osbuild(td, sb);
1644 * Filler function for proc/sys/kernel/msgmax
1647 linprocfs_domsgmax(PFS_FILL_ARGS)
1650 sbuf_printf(sb, "%d\n", msginfo.msgmax);
1655 * Filler function for proc/sys/kernel/msgmni
1658 linprocfs_domsgmni(PFS_FILL_ARGS)
1661 sbuf_printf(sb, "%d\n", msginfo.msgmni);
1666 * Filler function for proc/sys/kernel/msgmnb
1669 linprocfs_domsgmnb(PFS_FILL_ARGS)
1672 sbuf_printf(sb, "%d\n", msginfo.msgmnb);
1677 * Filler function for proc/sys/kernel/ngroups_max
1679 * Note that in Linux it defaults to 65536, not 1023.
1682 linprocfs_dongroups_max(PFS_FILL_ARGS)
1685 sbuf_printf(sb, "%d\n", ngroups_max);
1690 * Filler function for proc/sys/kernel/pid_max
1693 linprocfs_dopid_max(PFS_FILL_ARGS)
1696 sbuf_printf(sb, "%i\n", PID_MAX);
1701 * Filler function for proc/sys/kernel/sem
1704 linprocfs_dosem(PFS_FILL_ARGS)
1707 sbuf_printf(sb, "%d %d %d %d\n", seminfo.semmsl, seminfo.semmns,
1708 seminfo.semopm, seminfo.semmni);
1713 * Filler function for proc/sys/kernel/shmall
1716 linprocfs_doshmall(PFS_FILL_ARGS)
1719 sbuf_printf(sb, "%lu\n", shminfo.shmall);
1724 * Filler function for proc/sys/kernel/shmmax
1727 linprocfs_doshmmax(PFS_FILL_ARGS)
1730 sbuf_printf(sb, "%lu\n", shminfo.shmmax);
1735 * Filler function for proc/sys/kernel/shmmni
1738 linprocfs_doshmmni(PFS_FILL_ARGS)
1741 sbuf_printf(sb, "%lu\n", shminfo.shmmni);
1746 * Filler function for proc/sys/kernel/tainted
1749 linprocfs_dotainted(PFS_FILL_ARGS)
1752 sbuf_printf(sb, "0\n");
1757 * Filler function for proc/sys/vm/min_free_kbytes
1759 * This mirrors the approach in illumos to return zero for reads. Effectively,
1760 * it says, no memory is kept in reserve for "atomic allocations". This class
1761 * of allocation can be used at times when a thread cannot be suspended.
1764 linprocfs_dominfree(PFS_FILL_ARGS)
1767 sbuf_printf(sb, "%d\n", 0);
1772 * Filler function for proc/scsi/device_info
1775 linprocfs_doscsidevinfo(PFS_FILL_ARGS)
1782 * Filler function for proc/scsi/scsi
1785 linprocfs_doscsiscsi(PFS_FILL_ARGS)
1792 * Filler function for proc/devices
1795 linprocfs_dodevices(PFS_FILL_ARGS)
1798 sbuf_printf(sb, "Character devices:\n");
1800 char_devices = linux_get_char_devices();
1801 sbuf_printf(sb, "%s", char_devices);
1802 linux_free_get_char_devices(char_devices);
1804 sbuf_printf(sb, "\nBlock devices:\n");
1810 * Filler function for proc/cmdline
1813 linprocfs_docmdline(PFS_FILL_ARGS)
1816 sbuf_printf(sb, "BOOT_IMAGE=%s", kernelname);
1817 sbuf_printf(sb, " ro root=302\n");
1822 * Filler function for proc/filesystems
1825 linprocfs_dofilesystems(PFS_FILL_ARGS)
1827 struct vfsconf *vfsp;
1830 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
1831 if (vfsp->vfc_flags & VFCF_SYNTHETIC)
1832 sbuf_printf(sb, "nodev");
1833 sbuf_printf(sb, "\t%s\n", vfsp->vfc_name);
1840 * Filler function for proc/modules
1843 linprocfs_domodules(PFS_FILL_ARGS)
1846 struct linker_file *lf;
1848 TAILQ_FOREACH(lf, &linker_files, link) {
1849 sbuf_printf(sb, "%-20s%8lu%4d\n", lf->filename,
1850 (unsigned long)lf->size, lf->refs);
1857 * Filler function for proc/pid/fd
1860 linprocfs_dofdescfs(PFS_FILL_ARGS)
1864 sbuf_printf(sb, "/dev/fd");
1866 sbuf_printf(sb, "unknown");
1871 * Filler function for proc/pid/limits
1873 static const struct linux_rlimit_ident {
1876 unsigned int rlim_id;
1877 } linux_rlimits_ident[] = {
1878 { "Max cpu time", "seconds", RLIMIT_CPU },
1879 { "Max file size", "bytes", RLIMIT_FSIZE },
1880 { "Max data size", "bytes", RLIMIT_DATA },
1881 { "Max stack size", "bytes", RLIMIT_STACK },
1882 { "Max core file size", "bytes", RLIMIT_CORE },
1883 { "Max resident set", "bytes", RLIMIT_RSS },
1884 { "Max processes", "processes", RLIMIT_NPROC },
1885 { "Max open files", "files", RLIMIT_NOFILE },
1886 { "Max locked memory", "bytes", RLIMIT_MEMLOCK },
1887 { "Max address space", "bytes", RLIMIT_AS },
1888 { "Max file locks", "locks", LINUX_RLIMIT_LOCKS },
1889 { "Max pending signals", "signals", LINUX_RLIMIT_SIGPENDING },
1890 { "Max msgqueue size", "bytes", LINUX_RLIMIT_MSGQUEUE },
1891 { "Max nice priority", "", LINUX_RLIMIT_NICE },
1892 { "Max realtime priority", "", LINUX_RLIMIT_RTPRIO },
1893 { "Max realtime timeout", "us", LINUX_RLIMIT_RTTIME },
1898 linprocfs_doproclimits(PFS_FILL_ARGS)
1900 const struct linux_rlimit_ident *li;
1901 struct plimit *limp;
1909 limp = lim_hold(p->p_limit);
1912 sbuf_printf(sb, "%-26s%-21s%-21s%-21s\n", "Limit", "Soft Limit",
1913 "Hard Limit", "Units");
1914 for (li = linux_rlimits_ident; li->desc != NULL; ++li) {
1915 switch (li->rlim_id)
1917 case LINUX_RLIMIT_LOCKS:
1919 case LINUX_RLIMIT_RTTIME:
1920 rl.rlim_cur = RLIM_INFINITY;
1922 case LINUX_RLIMIT_SIGPENDING:
1923 error = kernel_sysctlbyname(td,
1924 "kern.sigqueue.max_pending_per_proc",
1925 &res, &size, 0, 0, 0, 0);
1931 case LINUX_RLIMIT_MSGQUEUE:
1932 error = kernel_sysctlbyname(td,
1933 "kern.ipc.msgmnb", &res, &size, 0, 0, 0, 0);
1939 case LINUX_RLIMIT_NICE:
1941 case LINUX_RLIMIT_RTPRIO:
1946 rl = limp->pl_rlimit[li->rlim_id];
1949 if (rl.rlim_cur == RLIM_INFINITY)
1950 sbuf_printf(sb, "%-26s%-21s%-21s%-10s\n",
1951 li->desc, "unlimited", "unlimited", li->unit);
1953 sbuf_printf(sb, "%-26s%-21llu%-21llu%-10s\n",
1954 li->desc, (unsigned long long)rl.rlim_cur,
1955 (unsigned long long)rl.rlim_max, li->unit);
1963 * The point of the following two functions is to work around
1964 * an assertion in Chromium; see kern/240991 for details.
1967 linprocfs_dotaskattr(PFS_ATTR_ARGS)
1975 * Filler function for proc/<pid>/task/.dummy
1978 linprocfs_dotaskdummy(PFS_FILL_ARGS)
1985 * Filler function for proc/sys/kernel/random/uuid
1988 linprocfs_douuid(PFS_FILL_ARGS)
1992 kern_uuidgen(&uuid, 1);
1993 sbuf_printf_uuid(sb, &uuid);
1994 sbuf_printf(sb, "\n");
1999 * Filler function for proc/sys/kernel/random/boot_id
2002 linprocfs_doboot_id(PFS_FILL_ARGS)
2004 static bool firstboot = 1;
2005 static struct uuid uuid;
2008 kern_uuidgen(&uuid, 1);
2011 sbuf_printf_uuid(sb, &uuid);
2012 sbuf_printf(sb, "\n");
2017 * Filler function for proc/pid/auxv
2020 linprocfs_doauxv(PFS_FILL_ARGS)
2023 off_t buflen, resid;
2027 * Mimic linux behavior and pass only processes with usermode
2028 * address space as valid. Return zero silently otherwise.
2030 if (p->p_vmspace == &vmspace0)
2033 if (uio->uio_resid == 0)
2035 if (uio->uio_offset < 0 || uio->uio_resid < 0)
2038 asb = sbuf_new_auto();
2041 error = proc_getauxv(td, p, asb);
2043 error = sbuf_finish(asb);
2045 resid = sbuf_len(asb) - uio->uio_offset;
2046 if (resid > uio->uio_resid)
2047 buflen = uio->uio_resid;
2050 if (buflen > IOSIZE_MAX)
2052 if (buflen > maxphys)
2058 error = uiomove(sbuf_data(asb) + uio->uio_offset, buflen, uio);
2064 * Filler function for proc/self/oom_score_adj
2067 linprocfs_do_oom_score_adj(PFS_FILL_ARGS)
2069 struct linux_pemuldata *pem;
2073 if (pem == NULL || uio == NULL)
2074 return (EOPNOTSUPP);
2075 if (uio->uio_rw == UIO_READ) {
2076 sbuf_printf(sb, "%d\n", pem->oom_score_adj);
2080 oom = strtol(sbuf_data(sb), NULL, 10);
2081 if (oom < LINUX_OOM_SCORE_ADJ_MIN ||
2082 oom > LINUX_OOM_SCORE_ADJ_MAX)
2084 pem->oom_score_adj = oom;
2090 * Filler function for proc/sys/vm/max_map_count
2092 * Maximum number of active map areas, on Linux this limits the number
2093 * of vmaps per mm struct. We don't limit mappings, return a suitable
2097 linprocfs_domax_map_cnt(PFS_FILL_ARGS)
2100 sbuf_printf(sb, "%d\n", INT32_MAX);
2108 linprocfs_init(PFS_INIT_ARGS)
2110 struct pfs_node *root;
2111 struct pfs_node *dir;
2112 struct pfs_node *sys;
2117 pfs_create_file(root, "cmdline", &linprocfs_docmdline,
2118 NULL, NULL, NULL, PFS_RD);
2119 pfs_create_file(root, "cpuinfo", &linprocfs_docpuinfo,
2120 NULL, NULL, NULL, PFS_RD);
2121 pfs_create_file(root, "devices", &linprocfs_dodevices,
2122 NULL, NULL, NULL, PFS_RD);
2123 pfs_create_file(root, "filesystems", &linprocfs_dofilesystems,
2124 NULL, NULL, NULL, PFS_RD);
2125 pfs_create_file(root, "loadavg", &linprocfs_doloadavg,
2126 NULL, NULL, NULL, PFS_RD);
2127 pfs_create_file(root, "meminfo", &linprocfs_domeminfo,
2128 NULL, NULL, NULL, PFS_RD);
2129 pfs_create_file(root, "modules", &linprocfs_domodules,
2130 NULL, NULL, NULL, PFS_RD);
2131 pfs_create_file(root, "mounts", &linprocfs_domtab,
2132 NULL, NULL, NULL, PFS_RD);
2133 pfs_create_file(root, "mtab", &linprocfs_domtab,
2134 NULL, NULL, NULL, PFS_RD);
2135 pfs_create_file(root, "partitions", &linprocfs_dopartitions,
2136 NULL, NULL, NULL, PFS_RD);
2137 pfs_create_link(root, "self", &procfs_docurproc,
2138 NULL, NULL, NULL, 0);
2139 pfs_create_file(root, "stat", &linprocfs_dostat,
2140 NULL, NULL, NULL, PFS_RD);
2141 pfs_create_file(root, "swaps", &linprocfs_doswaps,
2142 NULL, NULL, NULL, PFS_RD);
2143 pfs_create_file(root, "uptime", &linprocfs_douptime,
2144 NULL, NULL, NULL, PFS_RD);
2145 pfs_create_file(root, "version", &linprocfs_doversion,
2146 NULL, NULL, NULL, PFS_RD);
2149 dir = pfs_create_dir(root, "bus", NULL, NULL, NULL, 0);
2150 dir = pfs_create_dir(dir, "pci", NULL, NULL, NULL, 0);
2151 dir = pfs_create_dir(dir, "devices", NULL, NULL, NULL, 0);
2154 dir = pfs_create_dir(root, "net", NULL, NULL, NULL, 0);
2155 pfs_create_file(dir, "dev", &linprocfs_donetdev,
2156 NULL, NULL, NULL, PFS_RD);
2157 pfs_create_file(dir, "route", &linprocfs_donetroute,
2158 NULL, NULL, NULL, PFS_RD);
2160 /* /proc/<pid>/... */
2161 dir = pfs_create_dir(root, "pid", NULL, NULL, NULL, PFS_PROCDEP);
2162 pfs_create_file(dir, "cmdline", &linprocfs_doproccmdline,
2163 NULL, NULL, NULL, PFS_RD);
2164 pfs_create_link(dir, "cwd", &linprocfs_doproccwd,
2165 NULL, NULL, NULL, 0);
2166 pfs_create_file(dir, "environ", &linprocfs_doprocenviron,
2167 NULL, &procfs_candebug, NULL, PFS_RD);
2168 pfs_create_link(dir, "exe", &procfs_doprocfile,
2169 NULL, &procfs_notsystem, NULL, 0);
2170 pfs_create_file(dir, "maps", &linprocfs_doprocmaps,
2171 NULL, NULL, NULL, PFS_RD | PFS_AUTODRAIN);
2172 pfs_create_file(dir, "mem", &linprocfs_doprocmem,
2173 procfs_attr_rw, &procfs_candebug, NULL, PFS_RDWR | PFS_RAW);
2174 pfs_create_file(dir, "mountinfo", &linprocfs_doprocmountinfo,
2175 NULL, NULL, NULL, PFS_RD);
2176 pfs_create_file(dir, "mounts", &linprocfs_domtab,
2177 NULL, NULL, NULL, PFS_RD);
2178 pfs_create_link(dir, "root", &linprocfs_doprocroot,
2179 NULL, NULL, NULL, 0);
2180 pfs_create_file(dir, "stat", &linprocfs_doprocstat,
2181 NULL, NULL, NULL, PFS_RD);
2182 pfs_create_file(dir, "statm", &linprocfs_doprocstatm,
2183 NULL, NULL, NULL, PFS_RD);
2184 pfs_create_file(dir, "status", &linprocfs_doprocstatus,
2185 NULL, NULL, NULL, PFS_RD);
2186 pfs_create_link(dir, "fd", &linprocfs_dofdescfs,
2187 NULL, NULL, NULL, 0);
2188 pfs_create_file(dir, "auxv", &linprocfs_doauxv,
2189 NULL, &procfs_candebug, NULL, PFS_RD|PFS_RAWRD);
2190 pfs_create_file(dir, "limits", &linprocfs_doproclimits,
2191 NULL, NULL, NULL, PFS_RD);
2192 pfs_create_file(dir, "oom_score_adj", &linprocfs_do_oom_score_adj,
2193 procfs_attr_rw, &procfs_candebug, NULL, PFS_RDWR);
2195 /* /proc/<pid>/task/... */
2196 dir = pfs_create_dir(dir, "task", linprocfs_dotaskattr, NULL, NULL, 0);
2197 pfs_create_file(dir, ".dummy", &linprocfs_dotaskdummy,
2198 NULL, NULL, NULL, PFS_RD);
2200 /* /proc/scsi/... */
2201 dir = pfs_create_dir(root, "scsi", NULL, NULL, NULL, 0);
2202 pfs_create_file(dir, "device_info", &linprocfs_doscsidevinfo,
2203 NULL, NULL, NULL, PFS_RD);
2204 pfs_create_file(dir, "scsi", &linprocfs_doscsiscsi,
2205 NULL, NULL, NULL, PFS_RD);
2208 sys = pfs_create_dir(root, "sys", NULL, NULL, NULL, 0);
2210 /* /proc/sys/kernel/... */
2211 dir = pfs_create_dir(sys, "kernel", NULL, NULL, NULL, 0);
2212 pfs_create_file(dir, "osrelease", &linprocfs_doosrelease,
2213 NULL, NULL, NULL, PFS_RD);
2214 pfs_create_file(dir, "ostype", &linprocfs_doostype,
2215 NULL, NULL, NULL, PFS_RD);
2216 pfs_create_file(dir, "version", &linprocfs_doosbuild,
2217 NULL, NULL, NULL, PFS_RD);
2218 pfs_create_file(dir, "msgmax", &linprocfs_domsgmax,
2219 NULL, NULL, NULL, PFS_RD);
2220 pfs_create_file(dir, "msgmni", &linprocfs_domsgmni,
2221 NULL, NULL, NULL, PFS_RD);
2222 pfs_create_file(dir, "msgmnb", &linprocfs_domsgmnb,
2223 NULL, NULL, NULL, PFS_RD);
2224 pfs_create_file(dir, "ngroups_max", &linprocfs_dongroups_max,
2225 NULL, NULL, NULL, PFS_RD);
2226 pfs_create_file(dir, "pid_max", &linprocfs_dopid_max,
2227 NULL, NULL, NULL, PFS_RD);
2228 pfs_create_file(dir, "sem", &linprocfs_dosem,
2229 NULL, NULL, NULL, PFS_RD);
2230 pfs_create_file(dir, "shmall", &linprocfs_doshmall,
2231 NULL, NULL, NULL, PFS_RD);
2232 pfs_create_file(dir, "shmmax", &linprocfs_doshmmax,
2233 NULL, NULL, NULL, PFS_RD);
2234 pfs_create_file(dir, "shmmni", &linprocfs_doshmmni,
2235 NULL, NULL, NULL, PFS_RD);
2236 pfs_create_file(dir, "tainted", &linprocfs_dotainted,
2237 NULL, NULL, NULL, PFS_RD);
2239 /* /proc/sys/kernel/random/... */
2240 dir = pfs_create_dir(dir, "random", NULL, NULL, NULL, 0);
2241 pfs_create_file(dir, "uuid", &linprocfs_douuid,
2242 NULL, NULL, NULL, PFS_RD);
2243 pfs_create_file(dir, "boot_id", &linprocfs_doboot_id,
2244 NULL, NULL, NULL, PFS_RD);
2246 /* /proc/sys/vm/.... */
2247 dir = pfs_create_dir(sys, "vm", NULL, NULL, NULL, 0);
2248 pfs_create_file(dir, "min_free_kbytes", &linprocfs_dominfree,
2249 NULL, NULL, NULL, PFS_RD);
2250 pfs_create_file(dir, "max_map_count", &linprocfs_domax_map_cnt,
2251 NULL, NULL, NULL, PFS_RD);
2260 linprocfs_uninit(PFS_INIT_ARGS)
2263 /* nothing to do, pseudofs will GC */
2267 PSEUDOFS(linprocfs, 1, VFCF_JAIL);
2268 #if defined(__aarch64__) || defined(__amd64__)
2269 MODULE_DEPEND(linprocfs, linux_common, 1, 1, 1);
2271 MODULE_DEPEND(linprocfs, linux, 1, 1, 1);
2273 MODULE_DEPEND(linprocfs, procfs, 1, 1, 1);
2274 MODULE_DEPEND(linprocfs, sysvmsg, 1, 1, 1);
2275 MODULE_DEPEND(linprocfs, sysvsem, 1, 1, 1);
2276 MODULE_DEPEND(linprocfs, sysvshm, 1, 1, 1);