xref: /freebsd/sys/compat/linprocfs/linprocfs.c (revision 23f282aa31e9b6fceacd449020e936e98d6f2298)
1 /*
2  * Copyright (c) 1993 Jan-Simon Pendry
3  * Copyright (c) 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * Jan-Simon Pendry.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed by the University of
20  *	California, Berkeley and its contributors.
21  * 4. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *	@(#)procfs_status.c	8.4 (Berkeley) 6/15/94
38  *
39  * $FreeBSD$
40  */
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/proc.h>
45 #include <sys/jail.h>
46 #include <sys/vnode.h>
47 #include <sys/blist.h>
48 #include <sys/tty.h>
49 #include <sys/resourcevar.h>
50 #include <i386/linux/linprocfs/linprocfs.h>
51 
52 #include <vm/vm.h>
53 #include <vm/pmap.h>
54 #include <vm/vm_param.h>
55 #include <vm/swap_pager.h>
56 #include <sys/vmmeter.h>
57 #include <sys/exec.h>
58 
59 #include <machine/md_var.h>
60 #include <machine/cputypes.h>
61 
62 struct proc;
63 
64 int
65 linprocfs_domeminfo(curp, p, pfs, uio)
66 	struct proc *curp;
67 	struct proc *p;
68 	struct pfsnode *pfs;
69 	struct uio *uio;
70 {
71 	char *ps;
72 	int xlen;
73 	int error;
74 	char psbuf[512];		/* XXX - conservative */
75 	unsigned long memtotal;		/* total memory in bytes */
76 	unsigned long memused;		/* used memory in bytes */
77 	unsigned long memfree;		/* free memory in bytes */
78 	unsigned long memshared;	/* shared memory ??? */
79 	unsigned long buffers, cached;	/* buffer / cache memory ??? */
80 	unsigned long swaptotal;	/* total swap space in bytes */
81 	unsigned long swapused;		/* used swap space in bytes */
82 	unsigned long swapfree;		/* free swap space in bytes */
83 
84 	if (uio->uio_rw != UIO_READ)
85 		return (EOPNOTSUPP);
86 
87 	memtotal = physmem * PAGE_SIZE;
88 	/*
89 	 * The correct thing here would be:
90 	 *
91 	memfree = cnt.v_free_count * PAGE_SIZE;
92 	memused = memtotal - memfree;
93 	 *
94 	 * but it might mislead linux binaries into thinking there
95 	 * is very little memory left, so we cheat and tell them that
96 	 * all memory that isn't wired down is free.
97 	 */
98 	memused = cnt.v_wire_count * PAGE_SIZE;
99 	memfree = memtotal - memused;
100 	swaptotal = swapblist->bl_blocks * 1024; /* XXX why 1024? */
101 	swapfree = swapblist->bl_root->u.bmu_avail * PAGE_SIZE;
102 	swapused = swaptotal - swapfree;
103 	memshared = 0; /* XXX what's this supposed to be? */
104 	/*
105 	 * We'd love to be able to write:
106 	 *
107 	buffers = bufspace;
108 	 *
109 	 * but bufspace is internal to vfs_bio.c and we don't feel
110 	 * like unstaticizing it just for linprocfs's sake.
111 	 */
112 	buffers = 0;
113 	cached = cnt.v_cache_count * PAGE_SIZE;
114 
115 	ps = psbuf;
116 	ps += sprintf(ps,
117 		"        total:    used:    free:  shared: buffers:  cached:\n"
118 		"Mem:  %lu %lu %lu %lu %lu %lu\n"
119 		"Swap: %lu %lu %lu\n"
120 		"MemTotal: %9lu kB\n"
121 		"MemFree:  %9lu kB\n"
122 		"MemShared:%9lu kB\n"
123 		"Buffers:  %9lu kB\n"
124 		"Cached:   %9lu kB\n"
125 		"SwapTotal:%9lu kB\n"
126 		"SwapFree: %9lu kB\n",
127 		memtotal, memused, memfree, memshared, buffers, cached,
128 		swaptotal, swapused, swapfree,
129 		memtotal >> 10, memfree >> 10,
130 		memshared >> 10, buffers >> 10, cached >> 10,
131 		swaptotal >> 10, swapfree >> 10);
132 
133 	xlen = ps - psbuf;
134 	xlen -= uio->uio_offset;
135 	ps = psbuf + uio->uio_offset;
136 	xlen = imin(xlen, uio->uio_resid);
137 	if (xlen <= 0)
138 		error = 0;
139 	else
140 		error = uiomove(ps, xlen, uio);
141 	return (error);
142 }
143 
144 int
145 linprocfs_docpuinfo(curp, p, pfs, uio)
146 	struct proc *curp;
147 	struct proc *p;
148 	struct pfsnode *pfs;
149 	struct uio *uio;
150 {
151 	char *ps;
152 	int xlen;
153 	int error;
154 	char psbuf[512];		/* XXX - conservative */
155 	char *class;
156 #if 0
157 	extern char *cpu_model;		/* Yuck */
158 #endif
159 
160 	if (uio->uio_rw != UIO_READ)
161 		return (EOPNOTSUPP);
162 
163 	switch (cpu_class) {
164 	case CPUCLASS_286:
165 		class = "286";
166 		break;
167 	case CPUCLASS_386:
168 		class = "386";
169 		break;
170 	case CPUCLASS_486:
171 		class = "486";
172 		break;
173 	case CPUCLASS_586:
174 		class = "586";
175 		break;
176 	case CPUCLASS_686:
177 		class = "686";
178 		break;
179 	default:
180 		class = "unknown";
181 		break;
182 	}
183 
184 	ps = psbuf;
185 	ps += sprintf(ps,
186 			"processor       : %d\n"
187 			"cpu             : %.3s\n"
188 			"model           : %.20s\n"
189 			"vendor_id       : %.20s\n"
190 			"stepping        : %d\n",
191 			0, class, "unknown", cpu_vendor, cpu_id);
192 
193 	xlen = ps - psbuf;
194 	xlen -= uio->uio_offset;
195 	ps = psbuf + uio->uio_offset;
196 	xlen = imin(xlen, uio->uio_resid);
197 	if (xlen <= 0)
198 		error = 0;
199 	else
200 		error = uiomove(ps, xlen, uio);
201 	return (error);
202 }
203