xref: /freebsd/sys/fs/procfs/procfs_map.c (revision 2b743a9e9ddc6736208dc8ca1ce06ce64ad20a19)
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/mutex.h>
46 #include <sys/proc.h>
47 #include <sys/uio.h>
48 #include <sys/vnode.h>
49 
50 #include <fs/pseudofs/pseudofs.h>
51 #include <fs/procfs/procfs.h>
52 
53 #include <vm/vm.h>
54 #include <vm/pmap.h>
55 #include <vm/vm_map.h>
56 #include <vm/vm_page.h>
57 #include <vm/vm_object.h>
58 
59 #ifdef COMPAT_IA32
60 #include <sys/procfs.h>
61 #include <machine/fpu.h>
62 #include <compat/ia32/ia32_reg.h>
63 
64 extern struct sysentvec ia32_freebsd_sysvec;
65 #endif
66 
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 	int len;
84 	int error;
85 	vm_map_t map = &p->p_vmspace->vm_map;
86 	pmap_t pmap = vmspace_pmap(p->p_vmspace);
87 	vm_map_entry_t entry;
88 	char mebuffer[MEBUFFERSIZE];
89 	char *fullpath, *freepath;
90 #ifdef COMPAT_IA32
91 	int wrap32 = 0;
92 #endif
93 
94 	GIANT_REQUIRED;
95 
96 	PROC_LOCK(p);
97 	error = p_candebug(td, p);
98 	PROC_UNLOCK(p);
99 	if (error)
100 		return (error);
101 
102 	if (uio->uio_rw != UIO_READ)
103 		return (EOPNOTSUPP);
104 
105 	if (uio->uio_offset != 0)
106 		return (0);
107 
108 #ifdef COMPAT_IA32
109         if (curthread->td_proc->p_sysent == &ia32_freebsd_sysvec) {
110                 if (p->p_sysent != &ia32_freebsd_sysvec)
111                         return (EOPNOTSUPP);
112                 wrap32 = 1;
113         }
114 #endif
115 	error = 0;
116 	if (map != &curthread->td_proc->p_vmspace->vm_map)
117 		vm_map_lock_read(map);
118 	for (entry = map->header.next;
119 		((uio->uio_resid > 0) && (entry != &map->header));
120 		entry = entry->next) {
121 		vm_object_t obj, tobj, lobj;
122 		int ref_count, shadow_count, flags;
123 		vm_offset_t addr;
124 		int resident, privateresident;
125 		char *type;
126 
127 		if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
128 			continue;
129 
130 		obj = entry->object.vm_object;
131 		if (obj && (obj->shadow_count == 1))
132 			privateresident = obj->resident_page_count;
133 		else
134 			privateresident = 0;
135 
136 		resident = 0;
137 		addr = entry->start;
138 		while (addr < entry->end) {
139 			if (pmap_extract( pmap, addr))
140 				resident++;
141 			addr += PAGE_SIZE;
142 		}
143 
144 		for (lobj = tobj = obj; tobj; tobj = tobj->backing_object)
145 			lobj = tobj;
146 
147 		freepath = NULL;
148 		fullpath = "-";
149 		if (lobj) {
150 			switch(lobj->type) {
151 			default:
152 			case OBJT_DEFAULT:
153 				type = "default";
154 				break;
155 			case OBJT_VNODE:
156 				type = "vnode";
157 				vn_fullpath(td,
158 				    (struct vnode *)lobj->handle,
159 				    &fullpath,
160 				    &freepath);
161 				break;
162 			case OBJT_SWAP:
163 				type = "swap";
164 				break;
165 			case OBJT_DEVICE:
166 				type = "device";
167 				break;
168 			}
169 
170 			flags = obj->flags;
171 			ref_count = obj->ref_count;
172 			shadow_count = obj->shadow_count;
173 		} else {
174 			type = "none";
175 			flags = 0;
176 			ref_count = 0;
177 			shadow_count = 0;
178 		}
179 
180 		/*
181 		 * format:
182 		 *  start, end, resident, private resident, cow, access, type.
183 		 */
184 		snprintf(mebuffer, sizeof mebuffer,
185 		    "0x%lx 0x%lx %d %d %p %s%s%s %d %d 0x%x %s %s %s %s\n",
186 			(u_long)entry->start, (u_long)entry->end,
187 			resident, privateresident,
188 #ifdef COMPAT_IA32
189 			wrap32 ? NULL : obj,	/* Hide 64 bit value */
190 #else
191 			obj,
192 #endif
193 			(entry->protection & VM_PROT_READ)?"r":"-",
194 			(entry->protection & VM_PROT_WRITE)?"w":"-",
195 			(entry->protection & VM_PROT_EXECUTE)?"x":"-",
196 			ref_count, shadow_count, flags,
197 			(entry->eflags & MAP_ENTRY_COW)?"COW":"NCOW",
198 			(entry->eflags & MAP_ENTRY_NEEDS_COPY)?"NC":"NNC",
199 			type, fullpath);
200 
201 		if (freepath != NULL)
202 			free(freepath, M_TEMP);
203 
204 		len = strlen(mebuffer);
205 		if (len > uio->uio_resid) {
206 			error = EFBIG;
207 			break;
208 		}
209 		error = uiomove(mebuffer, len, uio);
210 		if (error)
211 			break;
212 	}
213 	if (map != &curthread->td_proc->p_vmspace->vm_map)
214 		vm_map_unlock_read(map);
215 
216 	return (error);
217 }
218