xref: /dflybsd-src/sys/vfs/procfs/procfs_map.c (revision 3546e044efc2789e203cdc14abe0226e163e03d4)
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: src/sys/miscfs/procfs/procfs_map.c,v 1.24.2.1 2001/08/04 13:12:24 rwatson Exp $
40  * $DragonFly: src/sys/vfs/procfs/procfs_map.c,v 1.7 2007/02/19 01:14:24 corecode Exp $
41  */
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/proc.h>
46 #include <sys/vnode.h>
47 #include <vfs/procfs/procfs.h>
48 
49 #include <vm/vm.h>
50 #include <sys/lock.h>
51 #include <vm/pmap.h>
52 #include <vm/vm_map.h>
53 #include <vm/vm_page.h>
54 #include <vm/vm_object.h>
55 
56 
57 #define MEBUFFERSIZE 256
58 
59 /*
60  * The map entries can *almost* be read with programs like cat.  However,
61  * large maps need special programs to read.  It is not easy to implement
62  * a program that can sense the required size of the buffer, and then
63  * subsequently do a read with the appropriate size.  This operation cannot
64  * be atomic.  The best that we can do is to allow the program to do a read
65  * with an arbitrarily large buffer, and return as much as we can.  We can
66  * return an error code if the buffer is too small (EFBIG), then the program
67  * can try a bigger buffer.
68  */
69 int
70 procfs_domap(struct proc *curp, struct lwp *lp, struct pfsnode *pfs,
71 	     struct uio *uio)
72 {
73 	struct proc *p = lp->lwp_proc;
74 	int len;
75 	int error;
76 	vm_map_t map = &p->p_vmspace->vm_map;
77 	pmap_t pmap = vmspace_pmap(p->p_vmspace);
78 	vm_map_entry_t entry;
79 	char mebuffer[MEBUFFERSIZE];
80 
81 	if (uio->uio_rw != UIO_READ)
82 		return (EOPNOTSUPP);
83 
84 	if (uio->uio_offset != 0)
85 		return (0);
86 
87 	error = 0;
88 	vm_map_lock_read(map);
89 	for (entry = map->header.next;
90 		((uio->uio_resid > 0) && (entry != &map->header));
91 		entry = entry->next) {
92 		vm_object_t obj, tobj, lobj;
93 		int ref_count, shadow_count, flags;
94 		vm_offset_t addr;
95 		vm_offset_t ostart;
96 		int resident, privateresident;
97 		char *type;
98 
99 		if (entry->maptype != VM_MAPTYPE_NORMAL &&
100 		    entry->maptype != VM_MAPTYPE_VPAGETABLE) {
101 			continue;
102 		}
103 
104 		obj = entry->object.vm_object;
105 		if (obj && (obj->shadow_count == 1))
106 			privateresident = obj->resident_page_count;
107 		else
108 			privateresident = 0;
109 
110 		/*
111 		 * Use map->hint as a poor man's ripout detector.
112 		 */
113 		map->hint = entry;
114 		ostart = entry->start;
115 
116 		/*
117 		 * Count resident pages (XXX can be horrible on 64-bit)
118 		 */
119 		resident = 0;
120 		addr = entry->start;
121 		while (addr < entry->end) {
122 			if (pmap_extract( pmap, addr))
123 				resident++;
124 			addr += PAGE_SIZE;
125 		}
126 
127 		for( lobj = tobj = obj; tobj; tobj = tobj->backing_object)
128 			lobj = tobj;
129 
130 		if (lobj) {
131 			switch(lobj->type) {
132 
133 default:
134 case OBJT_DEFAULT:
135 				type = "default";
136 				break;
137 case OBJT_VNODE:
138 				type = "vnode";
139 				break;
140 case OBJT_SWAP:
141 				type = "swap";
142 				break;
143 case OBJT_DEVICE:
144 				type = "device";
145 				break;
146 			}
147 
148 			flags = obj->flags;
149 			ref_count = obj->ref_count;
150 			shadow_count = obj->shadow_count;
151 		} else {
152 			type = "none";
153 			flags = 0;
154 			ref_count = 0;
155 			shadow_count = 0;
156 		}
157 
158 
159 		/*
160 		 * format:
161 		 *  start, end, resident, private resident, cow, access, type.
162 		 */
163 		ksnprintf(mebuffer, sizeof(mebuffer),
164 		    "0x%lx 0x%lx %d %d %p %s%s%s %d %d 0x%x %s %s %s\n",
165 			(u_long)entry->start, (u_long)entry->end,
166 			resident, privateresident, obj,
167 			(entry->protection & VM_PROT_READ)?"r":"-",
168 			(entry->protection & VM_PROT_WRITE)?"w":"-",
169 			(entry->protection & VM_PROT_EXECUTE)?"x":"-",
170 			ref_count, shadow_count, flags,
171 			(entry->eflags & MAP_ENTRY_COW)?"COW":"NCOW",
172 			(entry->eflags & MAP_ENTRY_NEEDS_COPY)?"NC":"NNC",
173 			type);
174 
175 		len = strlen(mebuffer);
176 		if (len > uio->uio_resid) {
177 			error = EFBIG;
178 			break;
179 		}
180 
181 		/*
182 		 * We cannot safely hold the map locked while accessing
183 		 * userspace as a VM fault might recurse the locked map.
184 		 */
185 		vm_map_unlock_read(map);
186 		error = uiomove(mebuffer, len, uio);
187 		vm_map_lock_read(map);
188 		if (error)
189 			break;
190 
191 		/*
192 		 * We use map->hint as a poor man's ripout detector.  If
193 		 * it does not match the entry we set it to prior to
194 		 * unlocking the map the entry MIGHT now be stale.  In
195 		 * this case we do an expensive lookup to find our place
196 		 * in the iteration again.
197 		 */
198 		if (map->hint != entry) {
199 			vm_map_entry_t reentry;
200 
201 			vm_map_lookup_entry(map, ostart, &reentry);
202 			entry = reentry;
203 		}
204 	}
205 	vm_map_unlock_read(map);
206 
207 	return error;
208 }
209 
210 int
211 procfs_validmap(struct lwp *lp)
212 {
213 	return ((lp->lwp_proc->p_flag & P_SYSTEM) == 0);
214 }
215