xref: /freebsd/sys/fs/procfs/procfs_map.c (revision 2a4a1db342263067035ce69a4017c645da63455d)
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. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed by the University of
20  *	California, Berkeley and its contributors.
21  * 4. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *	@(#)procfs_status.c	8.3 (Berkeley) 2/17/94
38  *
39  * $FreeBSD$
40  */
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/lock.h>
45 #include <sys/mutex.h>
46 #include <sys/proc.h>
47 #include <sys/uio.h>
48 
49 #include <fs/pseudofs/pseudofs.h>
50 #include <fs/procfs/procfs.h>
51 
52 #include <vm/vm.h>
53 #include <vm/pmap.h>
54 #include <vm/vm_map.h>
55 #include <vm/vm_page.h>
56 #include <vm/vm_object.h>
57 
58 
59 #define MEBUFFERSIZE 256
60 
61 /*
62  * The map entries can *almost* be read with programs like cat.  However,
63  * large maps need special programs to read.  It is not easy to implement
64  * a program that can sense the required size of the buffer, and then
65  * subsequently do a read with the appropriate size.  This operation cannot
66  * be atomic.  The best that we can do is to allow the program to do a read
67  * with an arbitrarily large buffer, and return as much as we can.  We can
68  * return an error code if the buffer is too small (EFBIG), then the program
69  * can try a bigger buffer.
70  */
71 int
72 procfs_doprocmap(PFS_FILL_ARGS)
73 {
74 	int len;
75 	int error;
76 	vm_map_t map = &p->p_vmspace->vm_map;
77 	pmap_t pmap = vmspace_pmap(p->p_vmspace);
78 	vm_map_entry_t entry;
79 	char mebuffer[MEBUFFERSIZE];
80 
81 	GIANT_REQUIRED;
82 
83 	if (uio->uio_rw != UIO_READ)
84 		return (EOPNOTSUPP);
85 
86 	if (uio->uio_offset != 0)
87 		return (0);
88 
89 	error = 0;
90 	if (map != &curthread->td_proc->p_vmspace->vm_map)
91 		vm_map_lock_read(map);
92 	for (entry = map->header.next;
93 		((uio->uio_resid > 0) && (entry != &map->header));
94 		entry = entry->next) {
95 		vm_object_t obj, tobj, lobj;
96 		int ref_count, shadow_count, flags;
97 		vm_offset_t addr;
98 		int resident, privateresident;
99 		char *type;
100 
101 		if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
102 			continue;
103 
104 		obj = entry->object.vm_object;
105 		if (obj && (obj->shadow_count == 1))
106 			privateresident = obj->resident_page_count;
107 		else
108 			privateresident = 0;
109 
110 		resident = 0;
111 		addr = entry->start;
112 		while (addr < entry->end) {
113 			if (pmap_extract( pmap, addr))
114 				resident++;
115 			addr += PAGE_SIZE;
116 		}
117 
118 		for (lobj = tobj = obj; tobj; tobj = tobj->backing_object)
119 			lobj = tobj;
120 
121 		if (lobj) {
122 			switch(lobj->type) {
123 			default:
124 			case OBJT_DEFAULT:
125 				type = "default";
126 				break;
127 			case OBJT_VNODE:
128 				type = "vnode";
129 				break;
130 			case OBJT_SWAP:
131 				type = "swap";
132 				break;
133 			case OBJT_DEVICE:
134 				type = "device";
135 				break;
136 			}
137 
138 			flags = obj->flags;
139 			ref_count = obj->ref_count;
140 			shadow_count = obj->shadow_count;
141 		} else {
142 			type = "none";
143 			flags = 0;
144 			ref_count = 0;
145 			shadow_count = 0;
146 		}
147 
148 		/*
149 		 * format:
150 		 *  start, end, resident, private resident, cow, access, type.
151 		 */
152 		snprintf(mebuffer, sizeof mebuffer,
153 		    "0x%lx 0x%lx %d %d %p %s%s%s %d %d 0x%x %s %s %s\n",
154 			(u_long)entry->start, (u_long)entry->end,
155 			resident, privateresident, obj,
156 			(entry->protection & VM_PROT_READ)?"r":"-",
157 			(entry->protection & VM_PROT_WRITE)?"w":"-",
158 			(entry->protection & VM_PROT_EXECUTE)?"x":"-",
159 			ref_count, shadow_count, flags,
160 			(entry->eflags & MAP_ENTRY_COW)?"COW":"NCOW",
161 			(entry->eflags & MAP_ENTRY_NEEDS_COPY)?"NC":"NNC",
162 			type);
163 
164 		len = strlen(mebuffer);
165 		if (len > uio->uio_resid) {
166 			error = EFBIG;
167 			break;
168 		}
169 		error = uiomove(mebuffer, len, uio);
170 		if (error)
171 			break;
172 	}
173 	if (map != &curthread->td_proc->p_vmspace->vm_map)
174 		vm_map_unlock_read(map);
175 
176 	return (error);
177 }
178