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