xref: /netbsd-src/sys/miscfs/procfs/procfs_linux.c (revision d20841bb642898112fe68f0ad3f7b26dddf56f07)
1 /*      $NetBSD: procfs_linux.c,v 1.15 2003/10/30 14:51:01 christos Exp $      */
2 
3 /*
4  * Copyright (c) 2001 Wasabi Systems, Inc.
5  * All rights reserved.
6  *
7  * Written by Frank van der Linden for Wasabi Systems, Inc.
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 for the NetBSD Project by
20  *      Wasabi Systems, Inc.
21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22  *    or promote products derived from this software without specific prior
23  *    written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: procfs_linux.c,v 1.15 2003/10/30 14:51:01 christos Exp $");
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/time.h>
44 #include <sys/kernel.h>
45 #include <sys/proc.h>
46 #include <sys/vnode.h>
47 #include <sys/exec.h>
48 #include <sys/resource.h>
49 #include <sys/resourcevar.h>
50 #include <sys/signal.h>
51 #include <sys/signalvar.h>
52 #include <sys/tty.h>
53 
54 #include <miscfs/procfs/procfs.h>
55 #include <compat/linux/common/linux_exec.h>
56 
57 #include <uvm/uvm_extern.h>
58 #include <uvm/uvm.h>
59 
60 #define PGTOB(p)	((unsigned long)(p) << PAGE_SHIFT)
61 #define PGTOKB(p)	((unsigned long)(p) << (PAGE_SHIFT - 10))
62 
63 /*
64  * Linux compatible /proc/meminfo. Only active when the -o linux
65  * mountflag is used.
66  */
67 int
68 procfs_domeminfo(struct proc *curp, struct proc *p, struct pfsnode *pfs,
69 		 struct uio *uio)
70 {
71 	char buf[512], *cp;
72 	int len, error;
73 
74 	len = snprintf(buf, sizeof buf,
75 		"        total:    used:    free:  shared: buffers: cached:\n"
76 		"Mem:  %8lu %8lu %8lu %8lu %8lu %8lu\n"
77 		"Swap: %8lu %8lu %8lu\n"
78 		"MemTotal:  %8lu kB\n"
79 		"MemFree:   %8lu kB\n"
80 		"MemShared: %8lu kB\n"
81 		"Buffers:   %8lu kB\n"
82 		"Cached:    %8lu kB\n"
83 		"SwapTotal: %8lu kB\n"
84 		"SwapFree:  %8lu kB\n",
85 		PGTOB(uvmexp.npages),
86 		PGTOB(uvmexp.npages - uvmexp.free),
87 		PGTOB(uvmexp.free),
88 		0L,
89 		PGTOB(uvmexp.filepages),
90 		PGTOB(uvmexp.anonpages + uvmexp.filepages + uvmexp.execpages),
91 		PGTOB(uvmexp.swpages),
92 		PGTOB(uvmexp.swpginuse),
93 		PGTOB(uvmexp.swpages - uvmexp.swpginuse),
94 		PGTOKB(uvmexp.npages),
95 		PGTOKB(uvmexp.free),
96 		0L,
97 		PGTOKB(uvmexp.filepages),
98 		PGTOKB(uvmexp.anonpages + uvmexp.filepages + uvmexp.execpages),
99 		PGTOKB(uvmexp.swpages),
100 		PGTOKB(uvmexp.swpages - uvmexp.swpginuse));
101 
102 	if (len == 0)
103 		return 0;
104 
105 	len -= uio->uio_offset;
106 	cp = buf + uio->uio_offset;
107 	len = imin(len, uio->uio_resid);
108 	if (len <= 0)
109 		error = 0;
110 	else
111 		error = uiomove(cp, len, uio);
112 	return error;
113 }
114 
115 /*
116  * Linux compatible /proc/<pid>/stat. Only active when the -o linux
117  * mountflag is used.
118  */
119 int
120 procfs_do_pid_stat(struct proc *p, struct lwp *l, struct pfsnode *pfs,
121 		 struct uio *uio)
122 {
123 	char buf[512], *cp;
124 	int len, error;
125 	struct tty *tty = p->p_session->s_ttyp;
126 	struct rusage *ru = &p->p_stats->p_ru;
127 	struct rusage *cru = &p->p_stats->p_cru;
128 	struct vm_map *map = &p->p_vmspace->vm_map;
129 	struct vm_map_entry *entry;
130 	unsigned long stext = 0, etext = 0, sstack = 0;
131 
132 	if (map != &curproc->p_vmspace->vm_map)
133 		vm_map_lock_read(map);
134 	for (entry = map->header.next; entry != &map->header;
135 	    entry = entry->next) {
136 		if (UVM_ET_ISSUBMAP(entry))
137 			continue;
138 		/* assume text is the first entry */
139 		if (stext == etext) {
140 			stext = entry->start;
141 			etext = entry->end;
142 			break;
143 		}
144 	}
145 #ifdef LINUX_USRSTACK
146 	if (strcmp(p->p_emul->e_name, "linux") == 0 &&
147 	    LINUX_USRSTACK < USRSTACK)
148 		sstack = (unsigned long) LINUX_USRSTACK;
149 	else
150 #endif
151 		sstack = (unsigned long) USRSTACK;
152 
153 	if (map != &curproc->p_vmspace->vm_map)
154 		vm_map_unlock_read(map);
155 
156 	len = snprintf(buf, sizeof(buf),
157 	    "%d (%s) %c %d %d %d %d %d "
158 	    "%u "
159 	    "%lu %lu %lu %lu %lu %lu %lu %lu "
160 	    "%d %d %d "
161 	    "%lu %lu %lu %lu %" PRIu64 " "
162 	    "%lu %lu %lu "
163 	    "%u %u "
164 	    "%u %u %u %u "
165 	    "%lu %lu %lu %d %d\n",
166 
167 	    p->p_pid,
168 	    p->p_comm,
169 	    "0IR3SZD"[(p->p_stat > 6) ? 0 : (int)p->p_stat],
170 	    p->p_pptr->p_pid,
171 
172 	    p->p_pgid,
173 	    p->p_session->s_sid,
174 	    tty ? tty->t_dev : 0,
175 	    (tty && tty->t_pgrp) ? tty->t_pgrp->pg_id : 0,
176 
177 	    p->p_flag,
178 
179 	    ru->ru_minflt,
180 	    cru->ru_minflt,
181 	    ru->ru_majflt,
182 	    cru->ru_majflt,
183 	    ru->ru_utime.tv_sec,
184 	    ru->ru_stime.tv_sec,
185 	    cru->ru_utime.tv_sec,
186 	    cru->ru_stime.tv_sec,
187 
188 	    p->p_nice,					/* XXX: priority */
189 	    p->p_nice,
190 	    0,
191 
192 	    p->p_rtime.tv_sec,
193 	    p->p_stats->p_start.tv_sec,
194 	    ru->ru_ixrss + ru->ru_idrss + ru->ru_isrss,
195 	    ru->ru_maxrss,
196 	    p->p_rlimit[RLIMIT_RSS].rlim_cur,
197 
198 	    stext,					/* start code */
199 	    etext,					/* end code */
200 	    sstack,					/* mm start stack */
201 	    0,						/* XXX: pc */
202 	    0,						/* XXX: sp */
203 	    p->p_sigctx.ps_siglist.__bits[0],		/* pending */
204 	    p->p_sigctx.ps_sigmask.__bits[0],		/* blocked */
205 	    p->p_sigctx.ps_sigignore.__bits[0],		/* ignored */
206 	    p->p_sigctx.ps_sigcatch.__bits[0],		/* caught */
207 
208 	    (unsigned long)(intptr_t)l->l_wchan,
209 	    ru->ru_nvcsw,
210 	    ru->ru_nivcsw,
211 	    p->p_exitsig,
212 	    0);						/* XXX: processor */
213 
214 	if (len == 0)
215 		return 0;
216 
217 	len -= uio->uio_offset;
218 	cp = buf + uio->uio_offset;
219 	len = imin(len, uio->uio_resid);
220 	if (len <= 0)
221 		error = 0;
222 	else
223 		error = uiomove(cp, len, uio);
224 	return error;
225 }
226 
227 int
228 procfs_docpuinfo(struct proc *curp, struct proc *p, struct pfsnode *pfs,
229 		 struct uio *uio)
230 {
231 	char buf[512], *cp;
232 	int len, error;
233 
234 	len = sizeof buf;
235 	if (procfs_getcpuinfstr(buf, &len) < 0)
236 		return EIO;
237 
238 	if (len == 0)
239 		return 0;
240 
241 	len -= uio->uio_offset;
242 	cp = buf + uio->uio_offset;
243 	len = imin(len, uio->uio_resid);
244 	if (len <= 0)
245 		error = 0;
246 	else
247 		error = uiomove(cp, len, uio);
248 	return error;
249 }
250 
251 int
252 procfs_douptime(struct proc *curp, struct proc *p, struct pfsnode *pfs,
253 		 struct uio *uio)
254 {
255 	char buf[512], *cp;
256 	int len, error;
257 	struct timeval runtime;
258 	u_int64_t idle;
259 
260 	timersub(&curcpu()->ci_schedstate.spc_runtime, &boottime, &runtime);
261 	idle = curcpu()->ci_schedstate.spc_cp_time[CP_IDLE];
262 	len = sprintf(buf, "%lu.%02lu %" PRIu64 ".%02" PRIu64 "\n",
263 		      runtime.tv_sec, runtime.tv_usec / 10000,
264 		      idle / hz, (((idle % hz) * 100) / hz) % 100);
265 
266 	if (len == 0)
267 		return 0;
268 
269 	len -= uio->uio_offset;
270 	cp = buf + uio->uio_offset;
271 	len = imin(len, uio->uio_resid);
272 	if (len <= 0)
273 		error = 0;
274 	else
275 		error = uiomove(cp, len, uio);
276 	return error;
277 }
278