procfs_map.c revision 185766
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: head/sys/fs/procfs/procfs_map.c 185766 2008-12-08 13:15:31Z kib $
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
66extern 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 */
82int
83procfs_doprocmap(PFS_FILL_ARGS)
84{
85	vm_map_t map = &p->p_vmspace->vm_map;
86	vm_map_entry_t entry, tmp_entry;
87	struct vnode *vp;
88	char *fullpath, *freepath;
89	int error, vfslocked;
90	unsigned int last_timestamp;
91#ifdef COMPAT_IA32
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_IA32
105        if (curthread->td_proc->p_sysent == &ia32_freebsd_sysvec) {
106                if (p->p_sysent != &ia32_freebsd_sysvec)
107                        return (EOPNOTSUPP);
108                wrap32 = 1;
109        }
110#endif
111
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
223		if (error == -1) {
224			error = 0;
225			break;
226		}
227		vm_map_lock_read(map);
228		if (last_timestamp + 1 != map->timestamp) {
229			/*
230			 * Look again for the entry because the map was
231			 * modified while it was unlocked.  Specifically,
232			 * the entry may have been clipped, merged, or deleted.
233			 */
234			vm_map_lookup_entry(map, e_end - 1, &tmp_entry);
235			entry = tmp_entry;
236		}
237	}
238	vm_map_unlock_read(map);
239	return (error);
240}
241