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 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)procfs_status.c 8.3 (Berkeley) 2/17/94 34 * 35 * $FreeBSD$ 36 */ 37 38 #include "opt_compat.h" 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/mutex.h> 47 #include <sys/proc.h> 48 #include <sys/sbuf.h> 49 #ifdef COMPAT_IA32 50 #include <sys/sysent.h> 51 #endif 52 #include <sys/uio.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 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; 85 int error, vfslocked; 86 unsigned int last_timestamp; 87 #ifdef COMPAT_IA32 88 int wrap32 = 0; 89 #endif 90 91 PROC_LOCK(p); 92 error = p_candebug(td, p); 93 PROC_UNLOCK(p); 94 if (error) 95 return (error); 96 97 if (uio->uio_rw != UIO_READ) 98 return (EOPNOTSUPP); 99 100 #ifdef COMPAT_IA32 101 if (curproc->p_sysent->sv_flags & SV_ILP32) { 102 if (!(p->p_sysent->sv_flags & SV_ILP32)) 103 return (EOPNOTSUPP); 104 wrap32 = 1; 105 } 106 #endif 107 108 vm = vmspace_acquire_ref(p); 109 if (vm == NULL) 110 return (ESRCH); 111 map = &vm->vm_map; 112 vm_map_lock_read(map); 113 for (entry = map->header.next; entry != &map->header; 114 entry = entry->next) { 115 vm_object_t obj, tobj, lobj; 116 int ref_count, shadow_count, flags; 117 vm_offset_t e_start, e_end, addr; 118 int resident, privateresident; 119 char *type; 120 vm_eflags_t e_eflags; 121 vm_prot_t e_prot; 122 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 obj = entry->object.vm_object; 132 if (obj != NULL) { 133 VM_OBJECT_LOCK(obj); 134 if (obj->shadow_count == 1) 135 privateresident = obj->resident_page_count; 136 } 137 138 resident = 0; 139 addr = entry->start; 140 while (addr < entry->end) { 141 if (pmap_extract(map->pmap, addr)) 142 resident++; 143 addr += PAGE_SIZE; 144 } 145 146 for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) { 147 if (tobj != obj) 148 VM_OBJECT_LOCK(tobj); 149 if (lobj != obj) 150 VM_OBJECT_UNLOCK(lobj); 151 lobj = tobj; 152 } 153 last_timestamp = map->timestamp; 154 vm_map_unlock_read(map); 155 156 freepath = NULL; 157 fullpath = "-"; 158 if (lobj) { 159 switch (lobj->type) { 160 default: 161 case OBJT_DEFAULT: 162 type = "default"; 163 vp = NULL; 164 break; 165 case OBJT_VNODE: 166 type = "vnode"; 167 vp = lobj->handle; 168 vref(vp); 169 break; 170 case OBJT_SWAP: 171 type = "swap"; 172 vp = NULL; 173 break; 174 case OBJT_DEVICE: 175 type = "device"; 176 vp = NULL; 177 break; 178 } 179 if (lobj != obj) 180 VM_OBJECT_UNLOCK(lobj); 181 182 flags = obj->flags; 183 ref_count = obj->ref_count; 184 shadow_count = obj->shadow_count; 185 VM_OBJECT_UNLOCK(obj); 186 if (vp != NULL) { 187 vn_fullpath(td, vp, &fullpath, &freepath); 188 vfslocked = VFS_LOCK_GIANT(vp->v_mount); 189 vrele(vp); 190 VFS_UNLOCK_GIANT(vfslocked); 191 } 192 } else { 193 type = "none"; 194 flags = 0; 195 ref_count = 0; 196 shadow_count = 0; 197 } 198 199 /* 200 * format: 201 * start, end, resident, private resident, cow, access, type. 202 */ 203 error = sbuf_printf(sb, 204 "0x%lx 0x%lx %d %d %p %s%s%s %d %d 0x%x %s %s %s %s\n", 205 (u_long)e_start, (u_long)e_end, 206 resident, privateresident, 207 #ifdef COMPAT_IA32 208 wrap32 ? NULL : obj, /* Hide 64 bit value */ 209 #else 210 obj, 211 #endif 212 (e_prot & VM_PROT_READ)?"r":"-", 213 (e_prot & VM_PROT_WRITE)?"w":"-", 214 (e_prot & VM_PROT_EXECUTE)?"x":"-", 215 ref_count, shadow_count, flags, 216 (e_eflags & MAP_ENTRY_COW)?"COW":"NCOW", 217 (e_eflags & MAP_ENTRY_NEEDS_COPY)?"NC":"NNC", 218 type, fullpath); 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 vmspace_free(vm); 239 return (error); 240 } 241