xref: /freebsd/sys/fs/procfs/procfs_map.c (revision 4f52dfbb)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1993 Jan-Simon Pendry
5  * Copyright (c) 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Jan-Simon Pendry.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	@(#)procfs_status.c	8.3 (Berkeley) 2/17/94
36  *
37  * $FreeBSD$
38  */
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/lock.h>
43 #include <sys/filedesc.h>
44 #include <sys/malloc.h>
45 #include <sys/mount.h>
46 #include <sys/proc.h>
47 #include <sys/resourcevar.h>
48 #include <sys/rwlock.h>
49 #include <sys/sbuf.h>
50 #ifdef COMPAT_FREEBSD32
51 #include <sys/sysent.h>
52 #endif
53 #include <sys/uio.h>
54 #include <sys/vnode.h>
55 
56 #include <fs/pseudofs/pseudofs.h>
57 #include <fs/procfs/procfs.h>
58 
59 #include <vm/vm.h>
60 #include <vm/vm_extern.h>
61 #include <vm/pmap.h>
62 #include <vm/vm_map.h>
63 #include <vm/vm_page.h>
64 #include <vm/vm_object.h>
65 
66 #define MEBUFFERSIZE 256
67 
68 /*
69  * The map entries can *almost* be read with programs like cat.  However,
70  * large maps need special programs to read.  It is not easy to implement
71  * a program that can sense the required size of the buffer, and then
72  * subsequently do a read with the appropriate size.  This operation cannot
73  * be atomic.  The best that we can do is to allow the program to do a read
74  * with an arbitrarily large buffer, and return as much as we can.  We can
75  * return an error code if the buffer is too small (EFBIG), then the program
76  * can try a bigger buffer.
77  */
78 int
79 procfs_doprocmap(PFS_FILL_ARGS)
80 {
81 	struct vmspace *vm;
82 	vm_map_t map;
83 	vm_map_entry_t entry, tmp_entry;
84 	struct vnode *vp;
85 	char *fullpath, *freepath, *type;
86 	struct ucred *cred;
87 	vm_object_t obj, tobj, lobj;
88 	int error, privateresident, ref_count, resident, shadow_count, flags;
89 	vm_offset_t e_start, e_end;
90 	vm_eflags_t e_eflags;
91 	vm_prot_t e_prot;
92 	unsigned int last_timestamp;
93 	bool super;
94 #ifdef COMPAT_FREEBSD32
95 	bool wrap32;
96 #endif
97 
98 	PROC_LOCK(p);
99 	error = p_candebug(td, p);
100 	PROC_UNLOCK(p);
101 	if (error)
102 		return (error);
103 
104 	if (uio->uio_rw != UIO_READ)
105 		return (EOPNOTSUPP);
106 
107 #ifdef COMPAT_FREEBSD32
108 	wrap32 = false;
109 	if (SV_CURPROC_FLAG(SV_ILP32)) {
110 		if (!(SV_PROC_FLAG(p, SV_ILP32)))
111 			return (EOPNOTSUPP);
112 		wrap32 = true;
113 	}
114 #endif
115 
116 	vm = vmspace_acquire_ref(p);
117 	if (vm == NULL)
118 		return (ESRCH);
119 	map = &vm->vm_map;
120 	vm_map_lock_read(map);
121 	for (entry = map->header.next; entry != &map->header;
122 	     entry = entry->next) {
123 		if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
124 			continue;
125 
126 		e_eflags = entry->eflags;
127 		e_prot = entry->protection;
128 		e_start = entry->start;
129 		e_end = entry->end;
130 		privateresident = 0;
131 		resident = 0;
132 		obj = entry->object.vm_object;
133 		if (obj != NULL) {
134 			VM_OBJECT_RLOCK(obj);
135 			if (obj->shadow_count == 1)
136 				privateresident = obj->resident_page_count;
137 		}
138 		cred = (entry->cred) ? entry->cred : (obj ? obj->cred : NULL);
139 
140 		for (lobj = tobj = obj; tobj != NULL;
141 		    tobj = tobj->backing_object) {
142 			if (tobj != obj)
143 				VM_OBJECT_RLOCK(tobj);
144 			lobj = tobj;
145 		}
146 		if (obj != NULL)
147 			kern_proc_vmmap_resident(map, entry, &resident, &super);
148 		for (tobj = obj; tobj != NULL; tobj = tobj->backing_object) {
149 			if (tobj != obj && tobj != lobj)
150 				VM_OBJECT_RUNLOCK(tobj);
151 		}
152 		last_timestamp = map->timestamp;
153 		vm_map_unlock_read(map);
154 
155 		freepath = NULL;
156 		fullpath = "-";
157 		if (lobj) {
158 			vp = NULL;
159 			switch (lobj->type) {
160 			default:
161 			case OBJT_DEFAULT:
162 				type = "default";
163 				break;
164 			case OBJT_VNODE:
165 				type = "vnode";
166 				vp = lobj->handle;
167 				vref(vp);
168 				break;
169 			case OBJT_SWAP:
170 				if ((lobj->flags & OBJ_TMPFS_NODE) != 0) {
171 					type = "vnode";
172 					if ((lobj->flags & OBJ_TMPFS) != 0) {
173 						vp = lobj->un_pager.swp.swp_tmpfs;
174 						vref(vp);
175 					}
176 				} else {
177 					type = "swap";
178 				}
179 				break;
180 			case OBJT_SG:
181 			case OBJT_DEVICE:
182 				type = "device";
183 				break;
184 			}
185 			if (lobj != obj)
186 				VM_OBJECT_RUNLOCK(lobj);
187 
188 			flags = obj->flags;
189 			ref_count = obj->ref_count;
190 			shadow_count = obj->shadow_count;
191 			VM_OBJECT_RUNLOCK(obj);
192 			if (vp != NULL) {
193 				vn_fullpath(td, vp, &fullpath, &freepath);
194 				vrele(vp);
195 			}
196 		} else {
197 			type = "none";
198 			flags = 0;
199 			ref_count = 0;
200 			shadow_count = 0;
201 		}
202 
203 		/*
204 		 * format:
205 		 *  start, end, resident, private resident, cow, access, type,
206 		 *         charged, charged uid.
207 		 */
208 		error = sbuf_printf(sb,
209 		    "0x%lx 0x%lx %d %d %p %s%s%s %d %d 0x%x %s %s %s %s %s %d\n",
210 			(u_long)e_start, (u_long)e_end,
211 			resident, privateresident,
212 #ifdef COMPAT_FREEBSD32
213 			wrap32 ? NULL : obj,	/* Hide 64 bit value */
214 #else
215 			obj,
216 #endif
217 			(e_prot & VM_PROT_READ)?"r":"-",
218 			(e_prot & VM_PROT_WRITE)?"w":"-",
219 			(e_prot & VM_PROT_EXECUTE)?"x":"-",
220 			ref_count, shadow_count, flags,
221 			(e_eflags & MAP_ENTRY_COW)?"COW":"NCOW",
222 			(e_eflags & MAP_ENTRY_NEEDS_COPY)?"NC":"NNC",
223 			type, fullpath,
224 			cred ? "CH":"NCH", cred ? cred->cr_ruid : -1);
225 
226 		if (freepath != NULL)
227 			free(freepath, M_TEMP);
228 		vm_map_lock_read(map);
229 		if (error == -1) {
230 			error = 0;
231 			break;
232 		}
233 		if (last_timestamp != map->timestamp) {
234 			/*
235 			 * Look again for the entry because the map was
236 			 * modified while it was unlocked.  Specifically,
237 			 * the entry may have been clipped, merged, or deleted.
238 			 */
239 			vm_map_lookup_entry(map, e_end - 1, &tmp_entry);
240 			entry = tmp_entry;
241 		}
242 	}
243 	vm_map_unlock_read(map);
244 	vmspace_free(vm);
245 	return (error);
246 }
247