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 * 3. 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: src/sys/miscfs/procfs/procfs_map.c,v 1.24.2.1 2001/08/04 13:12:24 rwatson Exp $ 36 * $DragonFly: src/sys/vfs/procfs/procfs_map.c,v 1.7 2007/02/19 01:14:24 corecode Exp $ 37 */ 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/proc.h> 42 #include <sys/vnode.h> 43 #include <sys/sbuf.h> 44 #include <vfs/procfs/procfs.h> 45 46 #include <vm/vm.h> 47 #include <sys/lock.h> 48 #include <vm/pmap.h> 49 #include <vm/vm_map.h> 50 #include <vm/vm_page.h> 51 #include <vm/vm_object.h> 52 53 #include <machine/limits.h> 54 55 int 56 procfs_domap(struct proc *curp, struct lwp *lp, struct pfsnode *pfs, 57 struct uio *uio) 58 { 59 struct proc *p = lp->lwp_proc; 60 ssize_t buflen = uio->uio_offset + uio->uio_resid; 61 struct vnode *vp; 62 char *fullpath, *freepath; 63 int error; 64 vm_map_t map = &p->p_vmspace->vm_map; 65 pmap_t pmap = vmspace_pmap(p->p_vmspace); 66 vm_map_entry_t entry; 67 struct sbuf *sb = NULL; 68 unsigned int last_timestamp; 69 70 if (uio->uio_rw != UIO_READ) 71 return (EOPNOTSUPP); 72 73 error = 0; 74 75 if (uio->uio_offset < 0 || uio->uio_resid < 0 || buflen >= INT_MAX) 76 return EINVAL; 77 sb = sbuf_new (sb, NULL, buflen+1, 0); 78 if (sb == NULL) 79 return EIO; 80 81 vm_map_lock_read(map); 82 for (entry = map->header.next; entry != &map->header; 83 entry = entry->next) { 84 vm_object_t obj, tobj, lobj; 85 int ref_count, shadow_count, flags; 86 vm_offset_t e_start, e_end, addr; 87 vm_eflags_t e_eflags; 88 vm_prot_t e_prot; 89 int resident, privateresident; 90 char *type; 91 92 privateresident = 0; 93 switch(entry->maptype) { 94 case VM_MAPTYPE_NORMAL: 95 case VM_MAPTYPE_VPAGETABLE: 96 obj = entry->object.vm_object; 97 if (obj != NULL) { 98 vm_object_hold(obj); 99 if (obj->shadow_count == 1) 100 privateresident = obj->resident_page_count; 101 } 102 break; 103 case VM_MAPTYPE_UKSMAP: 104 obj = NULL; 105 break; 106 default: 107 /* ignore entry */ 108 continue; 109 } 110 111 e_eflags = entry->eflags; 112 e_prot = entry->protection; 113 e_start = entry->start; 114 e_end = entry->end; 115 116 /* 117 * Count resident pages (XXX can be horrible on 64-bit) 118 */ 119 resident = 0; 120 addr = entry->start; 121 while (addr < entry->end) { 122 if (pmap_extract(pmap, addr)) 123 resident++; 124 addr += PAGE_SIZE; 125 } 126 if (obj) { 127 lobj = obj; 128 while ((tobj = lobj->backing_object) != NULL) { 129 KKASSERT(tobj != obj); 130 vm_object_hold(tobj); 131 if (tobj == lobj->backing_object) { 132 if (lobj != obj) { 133 vm_object_lock_swap(); 134 vm_object_drop(lobj); 135 } 136 lobj = tobj; 137 } else { 138 vm_object_drop(tobj); 139 } 140 } 141 } else { 142 lobj = NULL; 143 } 144 last_timestamp = map->timestamp; 145 vm_map_unlock(map); 146 147 freepath = NULL; 148 fullpath = "-"; 149 if (lobj) { 150 switch(lobj->type) { 151 default: 152 case OBJT_DEFAULT: 153 type = "default"; 154 vp = NULL; 155 break; 156 case OBJT_VNODE: 157 type = "vnode"; 158 vp = lobj->handle; 159 vref(vp); 160 break; 161 case OBJT_SWAP: 162 type = "swap"; 163 vp = NULL; 164 break; 165 case OBJT_DEVICE: 166 type = "device"; 167 vp = NULL; 168 break; 169 case OBJT_MGTDEVICE: 170 type = "mgtdevice"; 171 vp = NULL; 172 break; 173 } 174 if (lobj != obj) 175 vm_object_drop(lobj); 176 177 flags = obj->flags; 178 ref_count = obj->ref_count; 179 shadow_count = obj->shadow_count; 180 vm_object_drop(obj); 181 if (vp != NULL) { 182 vn_fullpath(p, vp, &fullpath, &freepath, 1); 183 vrele(vp); 184 } 185 } else { 186 flags = 0; 187 ref_count = 0; 188 shadow_count = 0; 189 switch(entry->maptype) { 190 case VM_MAPTYPE_UKSMAP: 191 type = "uksmap"; 192 break; 193 default: 194 type = "none"; 195 break; 196 } 197 } 198 199 /* 200 * format: 201 * start, end, res, priv res, cow, access, type, (fullpath). 202 */ 203 error = sbuf_printf(sb, 204 #if LONG_BIT == 64 205 "0x%016lx 0x%016lx %d %d %p %s%s%s %d %d " 206 #else 207 "0x%08lx 0x%08lx %d %d %p %s%s%s %d %d " 208 #endif 209 "0x%04x %s %s %s %s\n", 210 (u_long)e_start, (u_long)e_end, 211 resident, privateresident, obj, 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 kfree(freepath, M_TEMP); 222 freepath = NULL; 223 } 224 225 vm_map_lock_read(map); 226 if (error == -1) { 227 error = 0; 228 break; 229 } 230 231 if (last_timestamp != map->timestamp) { 232 vm_map_entry_t reentry; 233 vm_map_lookup_entry(map, e_start, &reentry); 234 entry = reentry; 235 } 236 } 237 vm_map_unlock_read(map); 238 if (sbuf_finish(sb) == 0) 239 buflen = sbuf_len(sb); 240 error = uiomove_frombuf(sbuf_data(sb), buflen, uio); 241 sbuf_delete(sb); 242 243 return error; 244 } 245 246 int 247 procfs_validmap(struct lwp *lp) 248 { 249 return ((lp->lwp_proc->p_flags & P_SYSTEM) == 0); 250 } 251