]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/compat/linprocfs/linprocfs.c
Merge llvm-project release/17.x llvmorg-17.0.0-rc4-10-g0176e8729ea4
[FreeBSD/FreeBSD.git] / sys / compat / linprocfs / linprocfs.c
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 2000 Dag-Erling 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
42 #include "opt_inet.h"
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/blist.h>
47 #include <sys/conf.h>
48 #include <sys/exec.h>
49 #include <sys/fcntl.h>
50 #include <sys/filedesc.h>
51 #include <sys/jail.h>
52 #include <sys/kernel.h>
53 #include <sys/limits.h>
54 #include <sys/linker.h>
55 #include <sys/lock.h>
56 #include <sys/malloc.h>
57 #include <sys/msg.h>
58 #include <sys/mutex.h>
59 #include <sys/namei.h>
60 #include <sys/proc.h>
61 #include <sys/ptrace.h>
62 #include <sys/queue.h>
63 #include <sys/resourcevar.h>
64 #include <sys/resource.h>
65 #include <sys/sbuf.h>
66 #include <sys/sem.h>
67 #include <sys/shm.h>
68 #include <sys/smp.h>
69 #include <sys/socket.h>
70 #include <sys/syscallsubr.h>
71 #include <sys/sysctl.h>
72 #include <sys/sysent.h>
73 #include <sys/time.h>
74 #include <sys/tty.h>
75 #include <sys/user.h>
76 #include <sys/uuid.h>
77 #include <sys/vmmeter.h>
78 #include <sys/vnode.h>
79 #include <sys/bus.h>
80 #include <sys/uio.h>
81
82 #include <net/if.h>
83 #include <net/if_var.h>
84 #include <net/if_types.h>
85
86 #include <net/route.h>
87 #include <net/route/nhop.h>
88 #include <net/route/route_ctl.h>
89
90 #include <vm/vm.h>
91 #include <vm/vm_extern.h>
92 #include <vm/pmap.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>
97
98 #include <machine/clock.h>
99
100 #include <geom/geom.h>
101 #include <geom/geom_int.h>
102
103 #if defined(__i386__) || defined(__amd64__)
104 #include <machine/cputypes.h>
105 #include <machine/md_var.h>
106 #endif /* __i386__ || __amd64__ */
107
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>
116
117 /*
118  * Various conversion macros
119  */
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)
128
129 /**
130  * @brief Mapping of ki_stat in struct kinfo_proc to the linux state
131  *
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.
136  *
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.
139  *
140  * This character array is used with ki_stati-1 as an index and tries to
141  * map our states to suitable linux states.
142  */
143 static char linux_state[] = "RRSTZDD";
144
145 /*
146  * Filler function for proc/meminfo
147  */
148 static int
149 linprocfs_domeminfo(PFS_FILL_ARGS)
150 {
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 */
158         size_t sz;
159         int error, i, j;
160
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;
167
168         /*
169          * This value may exclude wired pages, but we have no good way of
170          * accounting for that.
171          */
172         cached =
173             (vm_active_count() + vm_inactive_count() + vm_laundry_count()) *
174             PAGE_SIZE;
175
176         sz = sizeof(buffers);
177         error = kernel_sysctlbyname(curthread, "vfs.bufspace", &buffers, &sz,
178             NULL, 0, 0, 0);
179         if (error != 0)
180                 buffers = 0;
181
182         sbuf_printf(sb,
183             "MemTotal: %9lu kB\n"
184             "MemFree:  %9lu kB\n"
185             "Buffers:  %9lu kB\n"
186             "Cached:   %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));
191
192         return (0);
193 }
194
195 #if defined(__i386__) || defined(__amd64__)
196 /*
197  * Filler function for proc/cpuinfo (i386 & amd64 version)
198  */
199 static int
200 linprocfs_docpuinfo(PFS_FILL_ARGS)
201 {
202         int hw_model[2];
203         char model[128];
204         uint64_t freq;
205         size_t size;
206         u_int cache_size[4];
207         u_int regs[4] = { 0 };
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", "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"
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", "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"
268         };
269
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", ""
279         };
280
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"
290         };
291
292         static char *cpu_stdext_feature_l1_names[] = {
293                 /*  0 */ "xsaveopt", "xsavec", "xgetbv1", "xsaves",
294                 /*  4 */ "xfd"
295         };
296
297         static char *power_flags[] = {
298                 "ts",           "fid",          "vid",
299                 "ttp",          "tm",           "stc",
300                 "100mhzsteps",  "hwpstate",     "",
301                 "cpb",          "eff_freq_ro",  "proc_feedback",
302                 "acc_power",
303         };
304
305         hw_model[0] = CTL_HW;
306         hw_model[1] = HW_MODEL;
307         model[0] = '\0';
308         size = sizeof(model);
309         if (kernel_sysctl(td, hw_model, 2, &model, &size, 0, 0, 0, 0) != 0)
310                 strcpy(model, "unknown");
311 #ifdef __i386__
312         switch (cpu_vendor_id) {
313         case CPU_VENDOR_AMD:
314                 if (cpu_class < CPUCLASS_686)
315                         cpu_feature_names[16] = "fcmov";
316                 break;
317         case CPU_VENDOR_CYRIX:
318                 cpu_feature_names[24] = "cxmmx";
319                 break;
320         }
321 #endif
322         if (cpu_exthigh >= 0x80000006)
323                 do_cpuid(0x80000006, cache_size);
324         else
325                 memset(cache_size, 0, sizeof(cache_size));
326         for (i = 0; i < mp_ncpus; ++i) {
327                 fqmhz = 0;
328                 fqkhz = 0;
329                 freq = atomic_load_acq_64(&tsc_freq);
330                 if (freq != 0) {
331                         fqmhz = (freq + 4999) / 1000000;
332                         fqkhz = ((freq + 4999) / 10000) % 100;
333                 }
334                 sbuf_printf(sb,
335                     "processor\t: %d\n"
336                     "vendor_id\t: %.20s\n"
337                     "cpu family\t: %u\n"
338                     "model\t\t: %u\n"
339                     "model name\t: %s\n"
340                     "stepping\t: %u\n"
341                     "cpu MHz\t\t: %d.%02d\n"
342                     "cache size\t: %d KB\n"
343                     "physical id\t: %d\n"
344                     "siblings\t: %d\n"
345                     "core id\t\t: %d\n"
346                     "cpu cores\t: %d\n"
347                     "apicid\t\t: %d\n"
348                     "initial apicid\t: %d\n"
349                     "fpu\t\t: %s\n"
350                     "fpu_exception\t: %s\n"
351                     "cpuid level\t: %d\n"
352                     "wp\t\t: %s\n",
353                     i, cpu_vendor, CPUID_TO_FAMILY(cpu_id),
354                     CPUID_TO_MODEL(cpu_id), model, cpu_id & CPUID_STEPPING,
355                     fqmhz, fqkhz,
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]);
401                 }
402                 sbuf_cat(sb, "\n");
403                 sbuf_printf(sb,
404                     "bugs\t\t: %s\n"
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" : "",
411 #else
412                     "",
413 #endif
414                     fqmhz * 2, fqkhz,
415                     cpu_clflush_line_size, cpu_clflush_line_size,
416                     cpu_maxphyaddr,
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");
423
424                 /* XXX per-cpu vendor / class / model / id? */
425         }
426         sbuf_cat(sb, "\n");
427
428         return (0);
429 }
430 #else
431 /* ARM64TODO: implement non-stubbed linprocfs_docpuinfo */
432 static int
433 linprocfs_docpuinfo(PFS_FILL_ARGS)
434 {
435         int i;
436
437         for (i = 0; i < mp_ncpus; ++i) {
438                 sbuf_printf(sb,
439                     "processor\t: %d\n"
440                     "BogoMIPS\t: %d.%02d\n",
441                     i, 0, 0);
442                 sbuf_cat(sb, "Features\t: ");
443                 sbuf_cat(sb, "\n");
444                 sbuf_printf(sb,
445                     "CPU implementer\t: \n"
446                     "CPU architecture: \n"
447                     "CPU variant\t: 0x%x\n"
448                     "CPU part\t: 0x%x\n"
449                     "CPU revision\t: %d\n",
450                     0, 0, 0);
451                 sbuf_cat(sb, "\n");
452         }
453
454         return (0);
455 }
456 #endif /* __i386__ || __amd64__ */
457
458 static const char *path_slash_sys = "/sys";
459 static const char *fstype_sysfs = "sysfs";
460
461 static int
462 _mtab_helper(const struct pfs_node *pn, const struct statfs *sp,
463     const char **mntfrom, const char **mntto, const char **fstype)
464 {
465         /* determine device name */
466         *mntfrom = sp->f_mntfromname;
467
468         /* determine mount point */
469         *mntto = sp->f_mntonname;
470
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)
476                 return (ECANCELED);
477
478         if (strcmp(*fstype, "autofs") == 0) {
479                 /*
480                  * FreeBSD uses eg "map -hosts", whereas Linux
481                  * expects just "-hosts".
482                  */
483                 if (strncmp(*mntfrom, "map ", 4) == 0)
484                         *mntfrom += 4;
485         }
486
487         if (strcmp(*fstype, "linsysfs") == 0) {
488                 *mntfrom = path_slash_sys;
489                 *fstype = fstype_sysfs;
490         } else {
491                 /* For Linux msdosfs is called vfat */
492                 if (strcmp(*fstype, "msdosfs") == 0)
493                         *fstype = "vfat";
494         }
495         return (0);
496 }
497
498 static void
499 _sbuf_mntoptions_helper(struct sbuf *sb, uint64_t f_flags)
500 {
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");
512 #undef ADD_OPTION
513 }
514
515 /*
516  * Filler function for proc/mtab and proc/<pid>/mounts.
517  *
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
520  */
521 static int
522 linprocfs_domtab(PFS_FILL_ARGS)
523 {
524         const char *mntto, *mntfrom, *fstype;
525         char *dlep, *flep;
526         struct vnode *vp;
527         struct pwd *pwd;
528         size_t lep_len;
529         int error;
530         struct statfs *buf, *sp;
531         size_t count;
532
533         /*
534          * Resolve emulation tree prefix
535          */
536         flep = NULL;
537         pwd = pwd_hold(td);
538         vp = pwd->pwd_adir;
539         error = vn_fullpath_global(vp, &dlep, &flep);
540         pwd_drop(pwd);
541         if (error != 0)
542                 return (error);
543         lep_len = strlen(dlep);
544
545         buf = NULL;
546         error = kern_getfsstat(td, &buf, SIZE_T_MAX, &count,
547             UIO_SYSSPACE, MNT_WAIT);
548         if (error != 0) {
549                 free(buf, M_TEMP);
550                 free(flep, M_TEMP);
551                 return (error);
552         }
553
554         for (sp = buf; count > 0; sp++, count--) {
555                 error = _mtab_helper(pn, sp, &mntfrom, &mntto, &fstype);
556                 if (error != 0) {
557                         MPASS(error == ECANCELED);
558                         continue;
559                 }
560
561                 /* determine mount point */
562                 if (strncmp(mntto, dlep, lep_len) == 0 && mntto[lep_len] == '/')
563                         mntto += lep_len;
564
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");
569         }
570
571         free(buf, M_TEMP);
572         free(flep, M_TEMP);
573         return (error);
574 }
575
576 static int
577 linprocfs_doprocmountinfo(PFS_FILL_ARGS)
578 {
579         const char *mntfrom, *mntto, *fstype;
580         char *dlep, *flep;
581         struct statfs *buf, *sp;
582         size_t count, lep_len;
583         struct vnode *vp;
584         struct pwd *pwd;
585         int error;
586
587         /*
588          * Resolve emulation tree prefix
589          */
590         flep = NULL;
591         pwd = pwd_hold(td);
592         vp = pwd->pwd_adir;
593         error = vn_fullpath_global(vp, &dlep, &flep);
594         pwd_drop(pwd);
595         if (error != 0)
596                 return (error);
597         lep_len = strlen(dlep);
598
599         buf = NULL;
600         error = kern_getfsstat(td, &buf, SIZE_T_MAX, &count,
601             UIO_SYSSPACE, MNT_WAIT);
602         if (error != 0)
603                 goto out;
604
605         for (sp = buf; count > 0; sp++, count--) {
606                 error = _mtab_helper(pn, sp, &mntfrom, &mntto, &fstype);
607                 if (error != 0) {
608                         MPASS(error == ECANCELED);
609                         continue;
610                 }
611
612                 if (strncmp(mntto, dlep, lep_len) == 0 && mntto[lep_len] == '/')
613                         mntto += lep_len;
614 #if 0
615                 /*
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.
619                  */
620                 else
621                         continue;
622 #endif
623
624                 /*
625                  * (1) mount id
626                  *
627                  * (2) parent mount id -- we don't have this cheaply, so
628                  * provide a dummy value
629                  *
630                  * (3) major:minor -- ditto
631                  *
632                  * (4) root filesystem mount -- probably a namespaces thing
633                  *
634                  * (5) mountto path
635                  */
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);
640                 /*
641                  * (7) zero or more optional fields -- again, namespace related
642                  *
643                  * (8) End of variable length fields separator ("-")
644                  *
645                  * (9) fstype
646                  *
647                  * (10) mount from
648                  *
649                  * (11) "superblock" options -- like (6), but different
650                  * semantics in Linux
651                  */
652                 sbuf_printf(sb, " - %s %s %s\n", fstype, mntfrom,
653                     (sp->f_flags & MNT_RDONLY) ? "ro" : "rw");
654         }
655
656         error = 0;
657 out:
658         free(buf, M_TEMP);
659         free(flep, M_TEMP);
660         return (error);
661 }
662
663 /*
664  * Filler function for proc/partitions
665  */
666 static int
667 linprocfs_dopartitions(PFS_FILL_ARGS)
668 {
669         struct g_class *cp;
670         struct g_geom *gp;
671         struct g_provider *pp;
672         int major, minor;
673
674         g_topology_lock();
675         sbuf_printf(sb, "major minor  #blocks  name rio rmerge rsect "
676             "ruse wio wmerge wsect wuse running use aveq\n");
677
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) {
685                                                 major = 0;
686                                                 minor = 0;
687                                         }
688                                         sbuf_printf(sb, "%d %d %lld %s "
689                                             "%d %d %d %d %d "
690                                              "%d %d %d %d %d %d\n",
691                                              major, minor,
692                                              (long long)pp->mediasize, pp->name,
693                                              0, 0, 0, 0, 0,
694                                              0, 0, 0, 0, 0, 0);
695                                 }
696                         }
697         }
698         g_topology_unlock();
699
700         return (0);
701 }
702
703 /*
704  * Filler function for proc/stat
705  *
706  * Output depends on kernel version:
707  *
708  * v2.5.40 <=
709  *   user nice system idle
710  * v2.5.41
711  *   user nice system idle iowait
712  * v2.6.11
713  *   user nice system idle iowait irq softirq steal
714  * v2.6.24
715  *   user nice system idle iowait irq softirq steal guest
716  * v2.6.33 >=
717  *   user nice system idle iowait irq softirq steal guest guest_nice
718  */
719 static int
720 linprocfs_dostat(PFS_FILL_ARGS)
721 {
722         struct pcpu *pcpu;
723         long cp_time[CPUSTATES];
724         long *cp;
725         struct timeval boottime;
726         int i;
727         char *zero_pad;
728         bool has_intr = true;
729
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)) {
735                 zero_pad = " 0 0\n";
736         } else if (linux_kernver(td) >= LINUX_KERNVER(2,5,41)) {
737                 has_intr = false;
738                 zero_pad = " 0\n";
739         } else {
740                 has_intr = false;
741                 zero_pad = "\n";
742         }
743
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]));
752
753         /* Print interrupt stats if available */
754         if (has_intr) {
755                 sbuf_printf(sb, " 0 %lu", T2J(cp_time[CP_INTR]));
756         }
757
758         /* Pad out remaining fields depending on version */
759         sbuf_printf(sb, "%s", zero_pad);
760
761         CPU_FOREACH(i) {
762                 pcpu = pcpu_find(i);
763                 cp = pcpu->pc_cp_time;
764                 sbuf_printf(sb, "cpu%d %lu %lu %lu %lu", i,
765                     T2J(cp[CP_USER]),
766                     T2J(cp[CP_NICE]),
767                     T2J(cp[CP_SYS]),
768                     T2J(cp[CP_IDLE]));
769
770                 if (has_intr) {
771                         sbuf_printf(sb, " 0 %lu", T2J(cp[CP_INTR]));
772                 }
773
774                 sbuf_printf(sb, "%s", zero_pad);
775         }
776         sbuf_printf(sb,
777             "disk 0 0 0 0\n"
778             "page %ju %ju\n"
779             "swap %ju %ju\n"
780             "intr %ju\n"
781             "ctxt %ju\n"
782             "btime %lld\n",
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);
790         return (0);
791 }
792
793 static int
794 linprocfs_doswaps(PFS_FILL_ARGS)
795 {
796         struct xswdev xsw;
797         uintmax_t total, used;
798         int n;
799         char devname[SPECNAMELEN + 1];
800
801         sbuf_printf(sb, "Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n");
802         for (n = 0; ; n++) {
803                 if (swap_dev_info(n, &xsw, devname, sizeof(devname)) != 0)
804                         break;
805                 total = (uintmax_t)xsw.xsw_nblks * PAGE_SIZE / 1024;
806                 used  = (uintmax_t)xsw.xsw_used * PAGE_SIZE / 1024;
807
808                 /*
809                  * The space and not tab after the device name is on
810                  * purpose.  Linux does so.
811                  */
812                 sbuf_printf(sb, "/dev/%-34s unknown\t\t%jd\t%jd\t-1\n",
813                     devname, total, used);
814         }
815         return (0);
816 }
817
818 /*
819  * Filler function for proc/uptime
820  */
821 static int
822 linprocfs_douptime(PFS_FILL_ARGS)
823 {
824         long cp_time[CPUSTATES];
825         struct timeval tv;
826
827         getmicrouptime(&tv);
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);
833         return (0);
834 }
835
836 /*
837  * Get OS build date
838  */
839 static void
840 linprocfs_osbuild(struct thread *td, struct sbuf *sb)
841 {
842 #if 0
843         char osbuild[256];
844         char *cp1, *cp2;
845
846         strncpy(osbuild, version, 256);
847         osbuild[255] = '\0';
848         cp1 = strstr(osbuild, "\n");
849         cp2 = strstr(osbuild, ":");
850         if (cp1 && cp2) {
851                 *cp1 = *cp2 = '\0';
852                 cp1 = strstr(osbuild, "#");
853         } else
854                 cp1 = NULL;
855         if (cp1)
856                 sbuf_printf(sb, "%s%s", cp1, cp2 + 1);
857         else
858 #endif
859                 sbuf_cat(sb, "#4 Sun Dec 18 04:30:00 CET 1977");
860 }
861
862 /*
863  * Get OS builder
864  */
865 static void
866 linprocfs_osbuilder(struct thread *td, struct sbuf *sb)
867 {
868 #if 0
869         char builder[256];
870         char *cp;
871
872         cp = strstr(version, "\n    ");
873         if (cp) {
874                 strncpy(builder, cp + 5, 256);
875                 builder[255] = '\0';
876                 cp = strstr(builder, ":");
877                 if (cp)
878                         *cp = '\0';
879         }
880         if (cp)
881                 sbuf_cat(sb, builder);
882         else
883 #endif
884                 sbuf_cat(sb, "des@freebsd.org");
885 }
886
887 /*
888  * Filler function for proc/version
889  */
890 static int
891 linprocfs_doversion(PFS_FILL_ARGS)
892 {
893         char osname[LINUX_MAX_UTSNAME];
894         char osrelease[LINUX_MAX_UTSNAME];
895
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);
902         sbuf_cat(sb, "\n");
903
904         return (0);
905 }
906
907 /*
908  * Filler function for proc/loadavg
909  */
910 static int
911 linprocfs_doloadavg(PFS_FILL_ARGS)
912 {
913
914         sbuf_printf(sb,
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 */
925         );
926         return (0);
927 }
928
929 static int
930 linprocfs_get_tty_nr(struct proc *p)
931 {
932         struct session *sp;
933         const char *ttyname;
934         int error, major, minor, nr;
935
936         PROC_LOCK_ASSERT(p, MA_OWNED);
937         sx_assert(&proctree_lock, SX_LOCKED);
938
939         if ((p->p_flag & P_CONTROLT) == 0)
940                 return (-1);
941
942         sp = p->p_pgrp->pg_session;
943         if (sp == NULL)
944                 return (-1);
945
946         ttyname = devtoname(sp->s_ttyp->t_dev);
947         error = linux_driver_get_major_minor(ttyname, &major, &minor);
948         if (error != 0)
949                 return (-1);
950
951         nr = makedev(major, minor);
952         return (nr);
953 }
954
955 /*
956  * Filler function for proc/pid/stat
957  */
958 static int
959 linprocfs_doprocstat(PFS_FILL_ARGS)
960 {
961         struct kinfo_proc kp;
962         struct timeval boottime;
963         char state;
964         static int ratelimit = 0;
965         int tty_nr;
966         vm_offset_t startcode, startdata;
967
968         getboottime(&boottime);
969         sx_slock(&proctree_lock);
970         PROC_LOCK(p);
971         fill_kinfo_proc(p, &kp);
972         tty_nr = linprocfs_get_tty_nr(p);
973         sx_sunlock(&proctree_lock);
974         if (p->p_vmspace) {
975            startcode = (vm_offset_t)p->p_vmspace->vm_taddr;
976            startdata = (vm_offset_t)p->p_vmspace->vm_daddr;
977         } else {
978            startcode = 0;
979            startdata = 0;
980         }
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)) {
985                 state = 'R';
986
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));
990                         ++ratelimit;
991                 }
992         } else
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);
998         PROC_UNLOCK(p);
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 */
1034 #undef PS_ADD
1035         sbuf_putc(sb, '\n');
1036
1037         return (0);
1038 }
1039
1040 /*
1041  * Filler function for proc/pid/statm
1042  */
1043 static int
1044 linprocfs_doprocstatm(PFS_FILL_ARGS)
1045 {
1046         struct kinfo_proc kp;
1047         segsz_t lsize;
1048
1049         sx_slock(&proctree_lock);
1050         PROC_LOCK(p);
1051         fill_kinfo_proc(p, &kp);
1052         PROC_UNLOCK(p);
1053         sx_sunlock(&proctree_lock);
1054
1055         /*
1056          * See comments in linprocfs_doprocstatus() regarding the
1057          * computation of lsize.
1058          */
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 */
1069
1070         return (0);
1071 }
1072
1073 /*
1074  * Filler function for proc/pid/status
1075  */
1076 static int
1077 linprocfs_doprocstatus(PFS_FILL_ARGS)
1078 {
1079         struct kinfo_proc kp;
1080         char *state;
1081         segsz_t lsize;
1082         struct thread *td2;
1083         struct sigacts *ps;
1084         l_sigset_t siglist, sigignore, sigcatch;
1085         int i;
1086
1087         sx_slock(&proctree_lock);
1088         PROC_LOCK(p);
1089         td2 = FIRST_THREAD_IN_PROC(p);
1090
1091         if (P_SHOULDSTOP(p)) {
1092                 state = "T (stopped)";
1093         } else {
1094                 switch(p->p_state) {
1095                 case PRS_NEW:
1096                         state = "I (idle)";
1097                         break;
1098                 case PRS_NORMAL:
1099                         if (p->p_flag & P_WEXIT) {
1100                                 state = "X (exiting)";
1101                                 break;
1102                         }
1103                         switch(TD_GET_STATE(td2)) {
1104                         case TDS_INHIBITED:
1105                                 state = "S (sleeping)";
1106                                 break;
1107                         case TDS_RUNQ:
1108                         case TDS_RUNNING:
1109                                 state = "R (running)";
1110                                 break;
1111                         default:
1112                                 state = "? (unknown)";
1113                                 break;
1114                         }
1115                         break;
1116                 case PRS_ZOMBIE:
1117                         state = "Z (zombie)";
1118                         break;
1119                 default:
1120                         state = "? (unknown)";
1121                         break;
1122                 }
1123         }
1124
1125         fill_kinfo_proc(p, &kp);
1126         sx_sunlock(&proctree_lock);
1127
1128         sbuf_printf(sb, "Name:\t%s\n",          p->p_comm); /* XXX escape */
1129         sbuf_printf(sb, "State:\t%s\n",         state);
1130
1131         /*
1132          * Credentials
1133          */
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,
1139                                                 p->p_ucred->cr_uid,
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,
1144                                                 p->p_ucred->cr_gid,
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]);
1151         PROC_UNLOCK(p);
1152         sbuf_putc(sb, '\n');
1153
1154         /*
1155          * Memory
1156          *
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.
1160          *
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.
1164          */
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));
1174
1175         /*
1176          * Signal masks
1177          */
1178         PROC_LOCK(p);
1179         bsd_to_linux_sigset(&p->p_siglist, &siglist);
1180         ps = p->p_sigacts;
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);
1185         PROC_UNLOCK(p);
1186
1187         sbuf_printf(sb, "SigPnd:\t%016jx\n",    siglist.__mask);
1188         /*
1189          * XXX. SigBlk - target thread's signal mask, td_sigmask.
1190          * To implement SigBlk pseudofs should support proc/tid dir entries.
1191          */
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);
1195
1196         /*
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
1200          */
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);
1204
1205         return (0);
1206 }
1207
1208 /*
1209  * Filler function for proc/pid/cwd
1210  */
1211 static int
1212 linprocfs_doproccwd(PFS_FILL_ARGS)
1213 {
1214         struct pwd *pwd;
1215         char *fullpath = "unknown";
1216         char *freepath = NULL;
1217
1218         pwd = pwd_hold_proc(p);
1219         vn_fullpath(pwd->pwd_cdir, &fullpath, &freepath);
1220         sbuf_printf(sb, "%s", fullpath);
1221         if (freepath)
1222                 free(freepath, M_TEMP);
1223         pwd_drop(pwd);
1224         return (0);
1225 }
1226
1227 /*
1228  * Filler function for proc/pid/root
1229  */
1230 static int
1231 linprocfs_doprocroot(PFS_FILL_ARGS)
1232 {
1233         struct pwd *pwd;
1234         struct vnode *vp;
1235         char *fullpath = "unknown";
1236         char *freepath = NULL;
1237
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);
1242         if (freepath)
1243                 free(freepath, M_TEMP);
1244         pwd_drop(pwd);
1245         return (0);
1246 }
1247
1248 /*
1249  * Filler function for proc/pid/cmdline
1250  */
1251 static int
1252 linprocfs_doproccmdline(PFS_FILL_ARGS)
1253 {
1254         int ret;
1255
1256         PROC_LOCK(p);
1257         if ((ret = p_cansee(td, p)) != 0) {
1258                 PROC_UNLOCK(p);
1259                 return (ret);
1260         }
1261
1262         /*
1263          * Mimic linux behavior and pass only processes with usermode
1264          * address space as valid.  Return zero silently otherwize.
1265          */
1266         if (p->p_vmspace == &vmspace0) {
1267                 PROC_UNLOCK(p);
1268                 return (0);
1269         }
1270         if (p->p_args != NULL) {
1271                 sbuf_bcpy(sb, p->p_args->ar_args, p->p_args->ar_length);
1272                 PROC_UNLOCK(p);
1273                 return (0);
1274         }
1275
1276         if ((p->p_flag & P_SYSTEM) != 0) {
1277                 PROC_UNLOCK(p);
1278                 return (0);
1279         }
1280
1281         PROC_UNLOCK(p);
1282
1283         ret = proc_getargv(td, p, sb);
1284         return (ret);
1285 }
1286
1287 /*
1288  * Filler function for proc/pid/environ
1289  */
1290 static int
1291 linprocfs_doprocenviron(PFS_FILL_ARGS)
1292 {
1293
1294         /*
1295          * Mimic linux behavior and pass only processes with usermode
1296          * address space as valid.  Return zero silently otherwize.
1297          */
1298         if (p->p_vmspace == &vmspace0)
1299                 return (0);
1300
1301         return (proc_getenvv(td, p, sb));
1302 }
1303
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]";
1308
1309 /*
1310  * Filler function for proc/pid/maps
1311  */
1312 static int
1313 linprocfs_doprocmaps(PFS_FILL_ARGS)
1314 {
1315         struct vmspace *vm;
1316         vm_map_t map;
1317         vm_map_entry_t entry, tmp_entry;
1318         vm_object_t obj, tobj, lobj;
1319         vm_offset_t e_start, e_end;
1320         vm_ooffset_t off;
1321         vm_prot_t e_prot;
1322         unsigned int last_timestamp;
1323         char *name = "", *freename = NULL;
1324         const char *l_map_str;
1325         ino_t ino;
1326         int error;
1327         struct vnode *vp;
1328         struct vattr vat;
1329         bool private;
1330
1331         PROC_LOCK(p);
1332         error = p_candebug(td, p);
1333         PROC_UNLOCK(p);
1334         if (error)
1335                 return (error);
1336
1337         if (uio->uio_rw != UIO_READ)
1338                 return (EOPNOTSUPP);
1339
1340         error = 0;
1341         vm = vmspace_acquire_ref(p);
1342         if (vm == NULL)
1343                 return (ESRCH);
1344
1345         if (SV_CURPROC_FLAG(SV_LP64))
1346                 l_map_str = l64_map_str;
1347         else
1348                 l_map_str = l32_map_str;
1349         map = &vm->vm_map;
1350         vm_map_lock_read(map);
1351         VM_MAP_ENTRY_FOREACH(entry, map) {
1352                 name = "";
1353                 freename = NULL;
1354                 /*
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
1358                  * are calculated.
1359                  */
1360                 if ((entry->eflags & (MAP_ENTRY_IS_SUB_MAP | MAP_ENTRY_GUARD)) != 0)
1361                         continue;
1362                 e_prot = entry->protection;
1363                 e_start = entry->start;
1364                 e_end = entry->end;
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;
1371                         if (lobj != obj)
1372                                 VM_OBJECT_RUNLOCK(lobj);
1373                 }
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);
1378                 ino = 0;
1379                 if (lobj) {
1380                         vp = vm_object_vnode(lobj);
1381                         if (vp != NULL)
1382                                 vref(vp);
1383                         if (lobj != obj)
1384                                 VM_OBJECT_RUNLOCK(lobj);
1385                         VM_OBJECT_RUNLOCK(obj);
1386                         if (vp != NULL) {
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;
1391                                 vput(vp);
1392                         } else if (SV_PROC_ABI(p) == SV_ABI_LINUX) {
1393                                 /*
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.
1397                                  */
1398                                 if (e_start == p->p_sysent->sv_shared_page_base +
1399                                     PAGE_SIZE)
1400                                         name = vdso_str;
1401                                 if (e_end == p->p_sysent->sv_usrstack)
1402                                         name = stack_str;
1403                         }
1404                 }
1405
1406                 /*
1407                  * format:
1408                  *  start, end, access, offset, major, minor, inode, name.
1409                  */
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",
1416                     (u_long)off,
1417                     0,
1418                     0,
1419                     (u_long)ino,
1420                     *name ? "     " : " ",
1421                     name
1422                     );
1423                 if (freename)
1424                         free(freename, M_TEMP);
1425                 vm_map_lock_read(map);
1426                 if (error == -1) {
1427                         error = 0;
1428                         break;
1429                 }
1430                 if (last_timestamp != map->timestamp) {
1431                         /*
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.
1435                          */
1436                         vm_map_lookup_entry(map, e_end - 1, &tmp_entry);
1437                         entry = tmp_entry;
1438                 }
1439         }
1440         vm_map_unlock_read(map);
1441         vmspace_free(vm);
1442
1443         return (error);
1444 }
1445
1446 /*
1447  * Filler function for proc/pid/mem
1448  */
1449 static int
1450 linprocfs_doprocmem(PFS_FILL_ARGS)
1451 {
1452         ssize_t resid;
1453         int error;
1454
1455         resid = uio->uio_resid;
1456         error = procfs_doprocmem(PFS_FILL_ARGNAMES);
1457
1458         if (uio->uio_rw == UIO_READ && resid != uio->uio_resid)
1459                 return (0);
1460
1461         if (error == EFAULT)
1462                 error = EIO;
1463
1464         return (error);
1465 }
1466
1467 /*
1468  * Filler function for proc/net/dev
1469  */
1470 static int
1471 linprocfs_donetdev_cb(if_t ifp, void *arg)
1472 {
1473         char ifname[LINUX_IFNAMSIZ];
1474         struct sbuf *sb = arg;
1475
1476         if (ifname_bsd_to_linux_ifp(ifp, ifname, sizeof(ifname)) <= 0)
1477                 return (ENODEV);
1478
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 +
1488                                                  * rx_over_errors +
1489                                                  * rx_crc_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 */
1506         return (0);
1507 }
1508
1509 static int
1510 linprocfs_donetdev(PFS_FILL_ARGS)
1511 {
1512         struct epoch_tracker et;
1513
1514         sbuf_printf(sb, "%6s|%58s|%s\n"
1515             "%6s|%58s|%58s\n",
1516             "Inter-", "   Receive", "  Transmit",
1517             " face",
1518             "bytes    packets errs drop fifo frame compressed multicast",
1519             "bytes    packets errs drop fifo colls carrier compressed");
1520
1521         CURVNET_SET(TD_TO_VNET(curthread));
1522         NET_EPOCH_ENTER(et);
1523         if_foreach(linprocfs_donetdev_cb, sb);
1524         NET_EPOCH_EXIT(et);
1525         CURVNET_RESTORE();
1526
1527         return (0);
1528 }
1529
1530 struct walkarg {
1531         struct sbuf *sb;
1532 };
1533
1534 static int
1535 linux_route_print(struct rtentry *rt, void *vw)
1536 {
1537 #ifdef INET
1538         struct walkarg *w = vw;
1539         struct route_nhop_data rnd;
1540         struct in_addr dst, mask;
1541         struct nhop_object *nh;
1542         char ifname[16];
1543         uint32_t scopeid = 0;
1544         uint32_t gw = 0;
1545         uint32_t linux_flags = 0;
1546
1547         rt_get_inet_prefix_pmask(rt, &dst, &mask, &scopeid);
1548
1549         rt_get_rnd(rt, &rnd);
1550
1551         /* select only first route in case of multipath */
1552         nh = nhop_select_func(rnd.rnd_nhop, 0);
1553
1554         if (ifname_bsd_to_linux_ifp(nh->nh_ifp, ifname, sizeof(ifname)) <= 0)
1555                 return (ENODEV);
1556
1557         gw = (nh->nh_flags & NHF_GATEWAY)
1558                 ? nh->gw4_sa.sin_addr.s_addr : 0;
1559
1560         linux_flags = RTF_UP |
1561                 (nhop_get_rtflags(nh) & (RTF_GATEWAY | RTF_HOST));
1562
1563         sbuf_printf(w->sb,
1564                 "%s\t"
1565                 "%08X\t%08X\t%04X\t"
1566                 "%d\t%u\t%d\t"
1567                 "%08X\t%d\t%u\t%u",
1568                 ifname,
1569                 dst.s_addr, gw, linux_flags,
1570                 0, 0, rnd.rnd_weight,
1571                 mask.s_addr, nh->nh_mtu, 0, 0);
1572
1573         sbuf_printf(w->sb, "\n\n");
1574 #endif
1575         return (0);
1576 }
1577
1578 /*
1579  * Filler function for proc/net/route
1580  */
1581 static int
1582 linprocfs_donetroute(PFS_FILL_ARGS)
1583 {
1584         struct epoch_tracker et;
1585         struct walkarg w = {
1586                 .sb = sb
1587         };
1588         uint32_t fibnum = curthread->td_proc->p_fibnum;
1589
1590         sbuf_printf(w.sb, "%-127s\n", "Iface\tDestination\tGateway "
1591                "\tFlags\tRefCnt\tUse\tMetric\tMask\t\tMTU"
1592                "\tWindow\tIRTT");
1593
1594         CURVNET_SET(TD_TO_VNET(curthread));
1595         NET_EPOCH_ENTER(et);
1596         rib_walk(fibnum, AF_INET, false, linux_route_print, &w);
1597         NET_EPOCH_EXIT(et);
1598         CURVNET_RESTORE();
1599
1600         return (0);
1601 }
1602
1603 /*
1604  * Filler function for proc/sys/kernel/osrelease
1605  */
1606 static int
1607 linprocfs_doosrelease(PFS_FILL_ARGS)
1608 {
1609         char osrelease[LINUX_MAX_UTSNAME];
1610
1611         linux_get_osrelease(td, osrelease);
1612         sbuf_printf(sb, "%s\n", osrelease);
1613
1614         return (0);
1615 }
1616
1617 /*
1618  * Filler function for proc/sys/kernel/ostype
1619  */
1620 static int
1621 linprocfs_doostype(PFS_FILL_ARGS)
1622 {
1623         char osname[LINUX_MAX_UTSNAME];
1624
1625         linux_get_osname(td, osname);
1626         sbuf_printf(sb, "%s\n", osname);
1627
1628         return (0);
1629 }
1630
1631 /*
1632  * Filler function for proc/sys/kernel/version
1633  */
1634 static int
1635 linprocfs_doosbuild(PFS_FILL_ARGS)
1636 {
1637
1638         linprocfs_osbuild(td, sb);
1639         sbuf_cat(sb, "\n");
1640         return (0);
1641 }
1642
1643 /*
1644  * Filler function for proc/sys/kernel/msgmax
1645  */
1646 static int
1647 linprocfs_domsgmax(PFS_FILL_ARGS)
1648 {
1649
1650         sbuf_printf(sb, "%d\n", msginfo.msgmax);
1651         return (0);
1652 }
1653
1654 /*
1655  * Filler function for proc/sys/kernel/msgmni
1656  */
1657 static int
1658 linprocfs_domsgmni(PFS_FILL_ARGS)
1659 {
1660
1661         sbuf_printf(sb, "%d\n", msginfo.msgmni);
1662         return (0);
1663 }
1664
1665 /*
1666  * Filler function for proc/sys/kernel/msgmnb
1667  */
1668 static int
1669 linprocfs_domsgmnb(PFS_FILL_ARGS)
1670 {
1671
1672         sbuf_printf(sb, "%d\n", msginfo.msgmnb);
1673         return (0);
1674 }
1675
1676 /*
1677  * Filler function for proc/sys/kernel/ngroups_max
1678  *
1679  * Note that in Linux it defaults to 65536, not 1023.
1680  */
1681 static int
1682 linprocfs_dongroups_max(PFS_FILL_ARGS)
1683 {
1684
1685         sbuf_printf(sb, "%d\n", ngroups_max);
1686         return (0);
1687 }
1688
1689 /*
1690  * Filler function for proc/sys/kernel/pid_max
1691  */
1692 static int
1693 linprocfs_dopid_max(PFS_FILL_ARGS)
1694 {
1695
1696         sbuf_printf(sb, "%i\n", PID_MAX);
1697         return (0);
1698 }
1699
1700 /*
1701  * Filler function for proc/sys/kernel/sem
1702  */
1703 static int
1704 linprocfs_dosem(PFS_FILL_ARGS)
1705 {
1706
1707         sbuf_printf(sb, "%d %d %d %d\n", seminfo.semmsl, seminfo.semmns,
1708             seminfo.semopm, seminfo.semmni);
1709         return (0);
1710 }
1711
1712 /*
1713  * Filler function for proc/sys/kernel/shmall
1714  */
1715 static int
1716 linprocfs_doshmall(PFS_FILL_ARGS)
1717 {
1718
1719         sbuf_printf(sb, "%lu\n", shminfo.shmall);
1720         return (0);
1721 }
1722
1723 /*
1724  * Filler function for proc/sys/kernel/shmmax
1725  */
1726 static int
1727 linprocfs_doshmmax(PFS_FILL_ARGS)
1728 {
1729
1730         sbuf_printf(sb, "%lu\n", shminfo.shmmax);
1731         return (0);
1732 }
1733
1734 /*
1735  * Filler function for proc/sys/kernel/shmmni
1736  */
1737 static int
1738 linprocfs_doshmmni(PFS_FILL_ARGS)
1739 {
1740
1741         sbuf_printf(sb, "%lu\n", shminfo.shmmni);
1742         return (0);
1743 }
1744
1745 /*
1746  * Filler function for proc/sys/kernel/tainted
1747  */
1748 static int
1749 linprocfs_dotainted(PFS_FILL_ARGS)
1750 {
1751
1752         sbuf_printf(sb, "0\n");
1753         return (0);
1754 }
1755
1756 /*
1757  * Filler function for proc/sys/vm/min_free_kbytes
1758  *
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.
1762  */
1763 static int
1764 linprocfs_dominfree(PFS_FILL_ARGS)
1765 {
1766
1767         sbuf_printf(sb, "%d\n", 0);
1768         return (0);
1769 }
1770
1771 /*
1772  * Filler function for proc/scsi/device_info
1773  */
1774 static int
1775 linprocfs_doscsidevinfo(PFS_FILL_ARGS)
1776 {
1777
1778         return (0);
1779 }
1780
1781 /*
1782  * Filler function for proc/scsi/scsi
1783  */
1784 static int
1785 linprocfs_doscsiscsi(PFS_FILL_ARGS)
1786 {
1787
1788         return (0);
1789 }
1790
1791 /*
1792  * Filler function for proc/devices
1793  */
1794 static int
1795 linprocfs_dodevices(PFS_FILL_ARGS)
1796 {
1797         char *char_devices;
1798         sbuf_printf(sb, "Character devices:\n");
1799
1800         char_devices = linux_get_char_devices();
1801         sbuf_printf(sb, "%s", char_devices);
1802         linux_free_get_char_devices(char_devices);
1803
1804         sbuf_printf(sb, "\nBlock devices:\n");
1805
1806         return (0);
1807 }
1808
1809 /*
1810  * Filler function for proc/cmdline
1811  */
1812 static int
1813 linprocfs_docmdline(PFS_FILL_ARGS)
1814 {
1815
1816         sbuf_printf(sb, "BOOT_IMAGE=%s", kernelname);
1817         sbuf_printf(sb, " ro root=302\n");
1818         return (0);
1819 }
1820
1821 /*
1822  * Filler function for proc/filesystems
1823  */
1824 static int
1825 linprocfs_dofilesystems(PFS_FILL_ARGS)
1826 {
1827         struct vfsconf *vfsp;
1828
1829         vfsconf_slock();
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);
1834         }
1835         vfsconf_sunlock();
1836         return(0);
1837 }
1838
1839 /*
1840  * Filler function for proc/modules
1841  */
1842 static int
1843 linprocfs_domodules(PFS_FILL_ARGS)
1844 {
1845 #if 0
1846         struct linker_file *lf;
1847
1848         TAILQ_FOREACH(lf, &linker_files, link) {
1849                 sbuf_printf(sb, "%-20s%8lu%4d\n", lf->filename,
1850                     (unsigned long)lf->size, lf->refs);
1851         }
1852 #endif
1853         return (0);
1854 }
1855
1856 /*
1857  * Filler function for proc/pid/fd
1858  */
1859 static int
1860 linprocfs_dofdescfs(PFS_FILL_ARGS)
1861 {
1862
1863         if (p == curproc)
1864                 sbuf_printf(sb, "/dev/fd");
1865         else
1866                 sbuf_printf(sb, "unknown");
1867         return (0);
1868 }
1869
1870 /*
1871  * Filler function for proc/pid/limits
1872  */
1873 static const struct linux_rlimit_ident {
1874         const char      *desc;
1875         const char      *unit;
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 },
1894         { 0, 0, 0 }
1895 };
1896
1897 static int
1898 linprocfs_doproclimits(PFS_FILL_ARGS)
1899 {
1900         const struct linux_rlimit_ident *li;
1901         struct plimit *limp;
1902         struct rlimit rl;
1903         ssize_t size;
1904         int res, error;
1905
1906         error = 0;
1907
1908         PROC_LOCK(p);
1909         limp = lim_hold(p->p_limit);
1910         PROC_UNLOCK(p);
1911         size = sizeof(res);
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)
1916                 {
1917                 case LINUX_RLIMIT_LOCKS:
1918                         /* FALLTHROUGH */
1919                 case LINUX_RLIMIT_RTTIME:
1920                         rl.rlim_cur = RLIM_INFINITY;
1921                         break;
1922                 case LINUX_RLIMIT_SIGPENDING:
1923                         error = kernel_sysctlbyname(td,
1924                             "kern.sigqueue.max_pending_per_proc",
1925                             &res, &size, 0, 0, 0, 0);
1926                         if (error != 0)
1927                                 goto out;
1928                         rl.rlim_cur = res;
1929                         rl.rlim_max = res;
1930                         break;
1931                 case LINUX_RLIMIT_MSGQUEUE:
1932                         error = kernel_sysctlbyname(td,
1933                             "kern.ipc.msgmnb", &res, &size, 0, 0, 0, 0);
1934                         if (error != 0)
1935                                 goto out;
1936                         rl.rlim_cur = res;
1937                         rl.rlim_max = res;
1938                         break;
1939                 case LINUX_RLIMIT_NICE:
1940                         /* FALLTHROUGH */
1941                 case LINUX_RLIMIT_RTPRIO:
1942                         rl.rlim_cur = 0;
1943                         rl.rlim_max = 0;
1944                         break;
1945                 default:
1946                         rl = limp->pl_rlimit[li->rlim_id];
1947                         break;
1948                 }
1949                 if (rl.rlim_cur == RLIM_INFINITY)
1950                         sbuf_printf(sb, "%-26s%-21s%-21s%-10s\n",
1951                             li->desc, "unlimited", "unlimited", li->unit);
1952                 else
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);
1956         }
1957 out:
1958         lim_free(limp);
1959         return (error);
1960 }
1961
1962 /*
1963  * The point of the following two functions is to work around
1964  * an assertion in Chromium; see kern/240991 for details.
1965  */
1966 static int
1967 linprocfs_dotaskattr(PFS_ATTR_ARGS)
1968 {
1969
1970         vap->va_nlink = 3;
1971         return (0);
1972 }
1973
1974 /*
1975  * Filler function for proc/<pid>/task/.dummy
1976  */
1977 static int
1978 linprocfs_dotaskdummy(PFS_FILL_ARGS)
1979 {
1980
1981         return (0);
1982 }
1983
1984 /*
1985  * Filler function for proc/sys/kernel/random/uuid
1986  */
1987 static int
1988 linprocfs_douuid(PFS_FILL_ARGS)
1989 {
1990         struct uuid uuid;
1991
1992         kern_uuidgen(&uuid, 1);
1993         sbuf_printf_uuid(sb, &uuid);
1994         sbuf_printf(sb, "\n");
1995         return(0);
1996 }
1997
1998 /*
1999  * Filler function for proc/sys/kernel/random/boot_id
2000  */
2001 static int
2002 linprocfs_doboot_id(PFS_FILL_ARGS)
2003 {
2004        static bool firstboot = 1;
2005        static struct uuid uuid;
2006
2007        if (firstboot) {
2008                kern_uuidgen(&uuid, 1);
2009                firstboot = 0;
2010        }
2011        sbuf_printf_uuid(sb, &uuid);
2012        sbuf_printf(sb, "\n");
2013        return(0);
2014 }
2015
2016 /*
2017  * Filler function for proc/pid/auxv
2018  */
2019 static int
2020 linprocfs_doauxv(PFS_FILL_ARGS)
2021 {
2022         struct sbuf *asb;
2023         off_t buflen, resid;
2024         int error;
2025
2026         /*
2027          * Mimic linux behavior and pass only processes with usermode
2028          * address space as valid. Return zero silently otherwise.
2029          */
2030         if (p->p_vmspace == &vmspace0)
2031                 return (0);
2032
2033         if (uio->uio_resid == 0)
2034                 return (0);
2035         if (uio->uio_offset < 0 || uio->uio_resid < 0)
2036                 return (EINVAL);
2037
2038         asb = sbuf_new_auto();
2039         if (asb == NULL)
2040                 return (ENOMEM);
2041         error = proc_getauxv(td, p, asb);
2042         if (error == 0)
2043                 error = sbuf_finish(asb);
2044
2045         resid = sbuf_len(asb) - uio->uio_offset;
2046         if (resid > uio->uio_resid)
2047                 buflen = uio->uio_resid;
2048         else
2049                 buflen = resid;
2050         if (buflen > IOSIZE_MAX)
2051                 return (EINVAL);
2052         if (buflen > maxphys)
2053                 buflen = maxphys;
2054         if (resid <= 0)
2055                 return (0);
2056
2057         if (error == 0)
2058                 error = uiomove(sbuf_data(asb) + uio->uio_offset, buflen, uio);
2059         sbuf_delete(asb);
2060         return (error);
2061 }
2062
2063 /*
2064  * Filler function for proc/self/oom_score_adj
2065  */
2066 static int
2067 linprocfs_do_oom_score_adj(PFS_FILL_ARGS)
2068 {
2069         struct linux_pemuldata *pem;
2070         long oom;
2071
2072         pem = pem_find(p);
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);
2077         } else {
2078                 sbuf_trim(sb);
2079                 sbuf_finish(sb);
2080                 oom = strtol(sbuf_data(sb), NULL, 10);
2081                 if (oom < LINUX_OOM_SCORE_ADJ_MIN ||
2082                     oom > LINUX_OOM_SCORE_ADJ_MAX)
2083                         return (EINVAL);
2084                 pem->oom_score_adj = oom;
2085         }
2086         return (0);
2087 }
2088
2089 /*
2090  * Filler function for proc/sys/vm/max_map_count
2091  *
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
2094  * large value.
2095  */
2096 static int
2097 linprocfs_domax_map_cnt(PFS_FILL_ARGS)
2098 {
2099
2100         sbuf_printf(sb, "%d\n", INT32_MAX);
2101         return (0);
2102 }
2103
2104 /*
2105  * Constructor
2106  */
2107 static int
2108 linprocfs_init(PFS_INIT_ARGS)
2109 {
2110         struct pfs_node *root;
2111         struct pfs_node *dir;
2112         struct pfs_node *sys;
2113
2114         root = pi->pi_root;
2115
2116         /* /proc/... */
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);
2147
2148         /* /proc/bus/... */
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);
2152
2153         /* /proc/net/... */
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);
2159
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);
2194
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);
2199
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);
2206
2207         /* /proc/sys/... */
2208         sys = pfs_create_dir(root, "sys", NULL, NULL, NULL, 0);
2209
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);
2238
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);
2245
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);
2252
2253         return (0);
2254 }
2255
2256 /*
2257  * Destructor
2258  */
2259 static int
2260 linprocfs_uninit(PFS_INIT_ARGS)
2261 {
2262
2263         /* nothing to do, pseudofs will GC */
2264         return (0);
2265 }
2266
2267 PSEUDOFS(linprocfs, 1, VFCF_JAIL);
2268 #if defined(__aarch64__) || defined(__amd64__)
2269 MODULE_DEPEND(linprocfs, linux_common, 1, 1, 1);
2270 #else
2271 MODULE_DEPEND(linprocfs, linux, 1, 1, 1);
2272 #endif
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);