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