xref: /freebsd/sys/fs/procfs/procfs_map.c (revision a9148abd9da5db2f1c682fb17bed791845fc41c9)
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 #include <sys/uio.h>
50 #include <sys/vnode.h>
51 
52 #include <fs/pseudofs/pseudofs.h>
53 #include <fs/procfs/procfs.h>
54 
55 #include <vm/vm.h>
56 #include <vm/pmap.h>
57 #include <vm/vm_map.h>
58 #include <vm/vm_page.h>
59 #include <vm/vm_object.h>
60 
61 #ifdef COMPAT_IA32
62 #include <sys/procfs.h>
63 #include <machine/fpu.h>
64 #include <compat/ia32/ia32_reg.h>
65 
66 extern struct sysentvec ia32_freebsd_sysvec;
67 #endif
68 
69 
70 #define MEBUFFERSIZE 256
71 
72 /*
73  * The map entries can *almost* be read with programs like cat.  However,
74  * large maps need special programs to read.  It is not easy to implement
75  * a program that can sense the required size of the buffer, and then
76  * subsequently do a read with the appropriate size.  This operation cannot
77  * be atomic.  The best that we can do is to allow the program to do a read
78  * with an arbitrarily large buffer, and return as much as we can.  We can
79  * return an error code if the buffer is too small (EFBIG), then the program
80  * can try a bigger buffer.
81  */
82 int
83 procfs_doprocmap(PFS_FILL_ARGS)
84 {
85 	int error, vfslocked;
86 	vm_map_t map = &p->p_vmspace->vm_map;
87 	vm_map_entry_t entry;
88 	struct vnode *vp;
89 	char *fullpath, *freepath;
90 #ifdef COMPAT_IA32
91 	int wrap32 = 0;
92 #endif
93 
94 	PROC_LOCK(p);
95 	error = p_candebug(td, p);
96 	PROC_UNLOCK(p);
97 	if (error)
98 		return (error);
99 
100 	if (uio->uio_rw != UIO_READ)
101 		return (EOPNOTSUPP);
102 
103 #ifdef COMPAT_IA32
104         if (curthread->td_proc->p_sysent == &ia32_freebsd_sysvec) {
105                 if (p->p_sysent != &ia32_freebsd_sysvec)
106                         return (EOPNOTSUPP);
107                 wrap32 = 1;
108         }
109 #endif
110 
111 	vm_map_lock_read(map);
112 	for (entry = map->header.next; entry != &map->header;
113 	     entry = entry->next) {
114 		vm_object_t obj, tobj, lobj;
115 		int ref_count, shadow_count, flags;
116 		vm_offset_t addr;
117 		int resident, privateresident;
118 		char *type;
119 
120 		if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
121 			continue;
122 
123 		privateresident = 0;
124 		obj = entry->object.vm_object;
125 		if (obj != NULL) {
126 			VM_OBJECT_LOCK(obj);
127 			if (obj->shadow_count == 1)
128 				privateresident = obj->resident_page_count;
129 		}
130 
131 		resident = 0;
132 		addr = entry->start;
133 		while (addr < entry->end) {
134 			if (pmap_extract(map->pmap, addr))
135 				resident++;
136 			addr += PAGE_SIZE;
137 		}
138 
139 		for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) {
140 			if (tobj != obj)
141 				VM_OBJECT_LOCK(tobj);
142 			if (lobj != obj)
143 				VM_OBJECT_UNLOCK(lobj);
144 			lobj = tobj;
145 		}
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 			}
170 			if (lobj != obj)
171 				VM_OBJECT_UNLOCK(lobj);
172 
173 			flags = obj->flags;
174 			ref_count = obj->ref_count;
175 			shadow_count = obj->shadow_count;
176 			VM_OBJECT_UNLOCK(obj);
177 			if (vp != NULL) {
178 				vfslocked = VFS_LOCK_GIANT(vp->v_mount);
179 				vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
180 				vn_fullpath(td, vp, &fullpath, &freepath);
181 				vput(vp);
182 				VFS_UNLOCK_GIANT(vfslocked);
183 			}
184 		} else {
185 			type = "none";
186 			flags = 0;
187 			ref_count = 0;
188 			shadow_count = 0;
189 		}
190 
191 		/*
192 		 * format:
193 		 *  start, end, resident, private resident, cow, access, type.
194 		 */
195 		error = sbuf_printf(sb,
196 		    "0x%lx 0x%lx %d %d %p %s%s%s %d %d 0x%x %s %s %s %s\n",
197 			(u_long)entry->start, (u_long)entry->end,
198 			resident, privateresident,
199 #ifdef COMPAT_IA32
200 			wrap32 ? NULL : obj,	/* Hide 64 bit value */
201 #else
202 			obj,
203 #endif
204 			(entry->protection & VM_PROT_READ)?"r":"-",
205 			(entry->protection & VM_PROT_WRITE)?"w":"-",
206 			(entry->protection & VM_PROT_EXECUTE)?"x":"-",
207 			ref_count, shadow_count, flags,
208 			(entry->eflags & MAP_ENTRY_COW)?"COW":"NCOW",
209 			(entry->eflags & MAP_ENTRY_NEEDS_COPY)?"NC":"NNC",
210 			type, fullpath);
211 
212 		if (freepath != NULL)
213 			free(freepath, M_TEMP);
214 
215 		if (error == -1) {
216 			error = 0;
217 			break;
218 		}
219 	}
220 	vm_map_unlock_read(map);
221 	return (error);
222 }
223