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
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/lock.h>
39 #include <sys/filedesc.h>
40 #include <sys/malloc.h>
41 #include <sys/mount.h>
42 #include <sys/proc.h>
43 #include <sys/ptrace.h>
44 #include <sys/resourcevar.h>
45 #include <sys/rwlock.h>
46 #include <sys/sbuf.h>
47 #ifdef COMPAT_FREEBSD32
48 #include <sys/sysent.h>
49 #endif
50 #include <sys/uio.h>
51 #include <sys/user.h>
52 #include <sys/vnode.h>
53
54 #include <fs/pseudofs/pseudofs.h>
55 #include <fs/procfs/procfs.h>
56
57 #include <vm/vm.h>
58 #include <vm/vm_extern.h>
59 #include <vm/pmap.h>
60 #include <vm/vm_map.h>
61 #include <vm/vm_page.h>
62 #include <vm/vm_object.h>
63
64 #define MEBUFFERSIZE 256
65
66 /*
67 * The map entries can *almost* be read with programs like cat. However,
68 * large maps need special programs to read. It is not easy to implement
69 * a program that can sense the required size of the buffer, and then
70 * subsequently do a read with the appropriate size. This operation cannot
71 * be atomic. The best that we can do is to allow the program to do a read
72 * with an arbitrarily large buffer, and return as much as we can. We can
73 * return an error code if the buffer is too small (EFBIG), then the program
74 * can try a bigger buffer.
75 */
76 int
procfs_doprocmap(PFS_FILL_ARGS)77 procfs_doprocmap(PFS_FILL_ARGS)
78 {
79 struct vmspace *vm;
80 vm_map_t map;
81 vm_map_entry_t entry, tmp_entry;
82 struct vnode *vp;
83 char *fullpath, *freepath, *type;
84 struct ucred *cred;
85 vm_object_t lobj, nobj, obj, tobj;
86 int error, flags, kvme, privateresident, ref_count, resident;
87 int shadow_count;
88 vm_offset_t e_start, e_end;
89 vm_eflags_t e_eflags;
90 vm_prot_t e_prot;
91 unsigned int last_timestamp;
92 bool super;
93 #ifdef COMPAT_FREEBSD32
94 bool wrap32;
95 #endif
96
97 if (uio->uio_rw != UIO_READ)
98 return (EOPNOTSUPP);
99
100 error = proc_vmspace_ref(td, p, PRVM_BLOCK_EXEC | PRVM_CHECK_DEBUG,
101 &vm);
102 if (error != 0)
103 return (error);
104
105 #ifdef COMPAT_FREEBSD32
106 wrap32 = false;
107 if (SV_CURPROC_FLAG(SV_ILP32)) {
108 if (!(SV_PROC_FLAG(p, SV_ILP32)))
109 return (EOPNOTSUPP);
110 wrap32 = true;
111 }
112 #endif
113
114 map = &vm->vm_map;
115 vm_map_lock_read(map);
116 VM_MAP_ENTRY_FOREACH(entry, map) {
117 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
118 continue;
119
120 e_eflags = entry->eflags;
121 e_prot = entry->protection;
122 e_start = entry->start;
123 e_end = entry->end;
124 privateresident = 0;
125 resident = 0;
126 obj = entry->object.vm_object;
127 if (obj != NULL) {
128 VM_OBJECT_RLOCK(obj);
129 if (obj->shadow_count == 1)
130 privateresident = obj->resident_page_count;
131 }
132 cred = (entry->cred) ? entry->cred : (obj ? obj->cred : NULL);
133
134 for (lobj = tobj = obj; tobj != NULL;
135 tobj = tobj->backing_object) {
136 if (tobj != obj)
137 VM_OBJECT_RLOCK(tobj);
138 lobj = tobj;
139 }
140 if (obj != NULL)
141 kern_proc_vmmap_resident(map, entry, &resident, &super);
142 for (tobj = obj; tobj != NULL; tobj = nobj) {
143 nobj = tobj->backing_object;
144 if (tobj != obj && tobj != lobj)
145 VM_OBJECT_RUNLOCK(tobj);
146 }
147 last_timestamp = map->timestamp;
148 vm_map_unlock_read(map);
149
150 freepath = NULL;
151 fullpath = "-";
152 if (lobj) {
153 kvme = vm_object_kvme_type(lobj, &vp);
154 if (vp != NULL)
155 vref(vp);
156 switch (kvme) {
157 default:
158 type = "unknown";
159 break;
160 case KVME_TYPE_PHYS:
161 type = "phys";
162 break;
163 case KVME_TYPE_SWAP:
164 type = "swap";
165 break;
166 case KVME_TYPE_DEAD:
167 type = "dead";
168 break;
169 case KVME_TYPE_VNODE:
170 type = "vnode";
171 break;
172 case KVME_TYPE_SG:
173 case KVME_TYPE_DEVICE:
174 case KVME_TYPE_MGTDEVICE:
175 type = "device";
176 break;
177 }
178 if (lobj != obj)
179 VM_OBJECT_RUNLOCK(lobj);
180
181 flags = obj->flags;
182 ref_count = obj->ref_count;
183 shadow_count = obj->shadow_count;
184 VM_OBJECT_RUNLOCK(obj);
185 if (vp != NULL) {
186 vn_fullpath(vp, &fullpath, &freepath);
187 vrele(vp);
188 }
189 } else {
190 type = "none";
191 flags = 0;
192 ref_count = 0;
193 shadow_count = 0;
194 }
195
196 /*
197 * format:
198 * start, end, resident, private-resident, obj, access,
199 * ref_count, shadow_count, flags, cow, needs_copy, type,
200 * fullpath, charged, charged uid.
201 */
202 error = sbuf_printf(sb,
203 "0x%lx 0x%lx %d %d %p %s%s%s %d %d 0x%x %s %s %s %s %s %d\n",
204 (u_long)e_start, (u_long)e_end,
205 resident, privateresident,
206 #ifdef COMPAT_FREEBSD32
207 wrap32 ? NULL : obj, /* Hide 64 bit value */
208 #else
209 obj,
210 #endif
211 (e_prot & VM_PROT_READ)?"r":"-",
212 (e_prot & VM_PROT_WRITE)?"w":"-",
213 (e_prot & VM_PROT_EXECUTE)?"x":"-",
214 ref_count, shadow_count, flags,
215 (e_eflags & MAP_ENTRY_COW)?"COW":"NCOW",
216 (e_eflags & MAP_ENTRY_NEEDS_COPY)?"NC":"NNC",
217 type, fullpath,
218 cred ? "CH":"NCH", cred ? cred->cr_ruid : -1);
219
220 if (freepath != NULL)
221 free(freepath, M_TEMP);
222 vm_map_lock_read(map);
223 if (error == -1) {
224 error = 0;
225 break;
226 }
227 if (last_timestamp != map->timestamp) {
228 /*
229 * Look again for the entry because the map was
230 * modified while it was unlocked. Specifically,
231 * the entry may have been clipped, merged, or deleted.
232 */
233 vm_map_lookup_entry(map, e_end - 1, &tmp_entry);
234 entry = tmp_entry;
235 }
236 }
237 vm_map_unlock_read(map);
238 proc_vmspace_unref(td, p, PRVM_BLOCK_EXEC | PRVM_CHECK_DEBUG, vm);
239 return (error);
240 }
241