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