procfs_map.c revision 22975
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.3 (Berkeley) 2/17/94
38 *
39 *	$Id$
40 */
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/time.h>
45#include <sys/kernel.h>
46#include <sys/proc.h>
47#include <sys/vnode.h>
48#include <sys/ioctl.h>
49#include <sys/tty.h>
50#include <sys/resource.h>
51#include <sys/resourcevar.h>
52#include <miscfs/procfs/procfs.h>
53#include <sys/queue.h>
54#include <sys/vmmeter.h>
55#include <sys/mman.h>
56#include <sys/malloc.h>
57
58#include <vm/vm.h>
59#include <vm/vm_param.h>
60#include <vm/vm_prot.h>
61#include <vm/vm_inherit.h>
62#include <sys/lock.h>
63#include <vm/pmap.h>
64#include <vm/vm_map.h>
65#include <vm/vm_page.h>
66#include <vm/vm_object.h>
67#include <vm/vm_kern.h>
68#include <vm/vm_pager.h>
69#include <vm/vm_extern.h>
70#include <vm/default_pager.h>
71
72
73#define MEBUFFERSIZE 256
74
75/*
76 * The map entries can *almost* be read with programs like cat.  However,
77 * large maps need special programs to read.  It is not easy to implement
78 * a program that can sense the required size of the buffer, and then
79 * subsequently do a read with the appropriate size.  This operation cannot
80 * be atomic.  The best that we can do is to allow the program to do a read
81 * with an arbitrarily large buffer, and return as much as we can.  We can
82 * return an error code if the buffer is too small (EFBIG), then the program
83 * can try a bigger buffer.
84 */
85int
86procfs_domap(curp, p, pfs, uio)
87	struct proc *curp;
88	struct proc *p;
89	struct pfsnode *pfs;
90	struct uio *uio;
91{
92	int len;
93	int error;
94	vm_map_t map = &p->p_vmspace->vm_map;
95	pmap_t pmap = &p->p_vmspace->vm_pmap;
96	vm_map_entry_t entry;
97	char mebuffer[MEBUFFERSIZE];
98
99	if (uio->uio_rw != UIO_READ)
100		return (EOPNOTSUPP);
101
102	if (uio->uio_offset != 0)
103		return (0);
104
105	error = 0;
106	if (map != &curproc->p_vmspace->vm_map)
107		vm_map_lock(map);
108	for (entry = map->header.next;
109		((uio->uio_resid > 0) && (entry != &map->header));
110		entry = entry->next) {
111		vm_object_t obj, tobj, lobj;
112		vm_offset_t addr;
113		int resident, privateresident;
114		char *type;
115
116		if (entry->eflags & (MAP_ENTRY_IS_A_MAP|MAP_ENTRY_IS_SUB_MAP))
117			continue;
118
119		obj = entry->object.vm_object;
120		if (obj && (obj->ref_count == 1))
121			privateresident = obj->resident_page_count;
122		else
123			privateresident = 0;
124
125		resident = 0;
126		addr = entry->start;
127		while (addr < entry->end) {
128			if (pmap_extract( pmap, addr))
129				resident++;
130			addr += PAGE_SIZE;
131		}
132
133		for( lobj = tobj = obj; tobj; tobj = tobj->backing_object)
134			lobj = tobj;
135
136		if (lobj) switch(lobj->type) {
137
138default:
139case OBJT_DEFAULT:
140			type = "default";
141			break;
142case OBJT_VNODE:
143			type = "vnode";
144			break;
145case OBJT_SWAP:
146			type = "swap";
147			break;
148case OBJT_DEVICE:
149			type = "device";
150			break;
151		} else {
152			type = "none";
153		}
154
155
156		/*
157		 * format:
158		 *  start, end, resident, private resident, cow, access, type.
159		 */
160		sprintf(mebuffer, "0x%-8.8x 0x%-8.8x %9d %9d %s%s%s %s %s\n",
161			entry->start, entry->end,
162			resident, privateresident,
163			(entry->protection & VM_PROT_READ)?"r":"-",
164			(entry->protection & VM_PROT_WRITE)?"w":"-",
165			(entry->protection & VM_PROT_EXECUTE)?"x":"-",
166			(entry->eflags & MAP_ENTRY_COW)?"COW":"   ",
167			type);
168
169		len = strlen(mebuffer);
170		if (len > uio->uio_resid) {
171			error = EFBIG;
172			break;
173		}
174		error = uiomove(mebuffer, len, uio);
175		if (error)
176			break;
177	}
178	if (map != &curproc->p_vmspace->vm_map)
179		vm_map_unlock(map);
180	return error;
181}
182
183int
184procfs_validmap(p)
185	struct proc *p;
186{
187	return ((p->p_flag & P_SYSTEM) == 0);
188}
189