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