xref: /freebsd/sys/fs/procfs/procfs_map.c (revision 2ad872c5794e4c26fdf6ed219ad3f09ca0d5304a)
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  *	$Id: procfs_map.c,v 1.17 1998/04/29 04:28:22 dyson Exp $
40  */
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/proc.h>
45 #include <sys/vnode.h>
46 #include <miscfs/procfs/procfs.h>
47 
48 #include <vm/vm.h>
49 #include <vm/vm_prot.h>
50 #include <sys/lock.h>
51 #include <vm/pmap.h>
52 #include <vm/vm_map.h>
53 #include <vm/vm_page.h>
54 #include <vm/vm_object.h>
55 
56 
57 #define MEBUFFERSIZE 256
58 
59 /*
60  * The map entries can *almost* be read with programs like cat.  However,
61  * large maps need special programs to read.  It is not easy to implement
62  * a program that can sense the required size of the buffer, and then
63  * subsequently do a read with the appropriate size.  This operation cannot
64  * be atomic.  The best that we can do is to allow the program to do a read
65  * with an arbitrarily large buffer, and return as much as we can.  We can
66  * return an error code if the buffer is too small (EFBIG), then the program
67  * can try a bigger buffer.
68  */
69 int
70 procfs_domap(curp, p, pfs, uio)
71 	struct proc *curp;
72 	struct proc *p;
73 	struct pfsnode *pfs;
74 	struct uio *uio;
75 {
76 	int len;
77 	int error;
78 	vm_map_t map = &p->p_vmspace->vm_map;
79 	pmap_t pmap = &p->p_vmspace->vm_pmap;
80 	vm_map_entry_t entry;
81 	char mebuffer[MEBUFFERSIZE];
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 != &curproc->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, id, flags;
97 		vm_offset_t addr;
98 		int resident, privateresident;
99 		char *type;
100 
101 		if (entry->eflags & (MAP_ENTRY_IS_A_MAP|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 
124 default:
125 case OBJT_DEFAULT:
126 				type = "default";
127 				break;
128 case OBJT_VNODE:
129 				type = "vnode";
130 				break;
131 case OBJT_SWAP:
132 				type = "swap";
133 				break;
134 case OBJT_DEVICE:
135 				type = "device";
136 				break;
137 			}
138 
139 			flags = obj->flags;
140 			ref_count = obj->ref_count;
141 			shadow_count = obj->shadow_count;
142 			id = obj->id;
143 		} else {
144 			type = "none";
145 			flags = 0;
146 			ref_count = 0;
147 			shadow_count = 0;
148 			id = 0;
149 		}
150 
151 
152 		/*
153 		 * format:
154 		 *  start, end, resident, private resident, cow, access, type.
155 		 */
156 		snprintf(mebuffer, sizeof(mebuffer),
157 		    "0x%x 0x%x %d %d %d %s%s%s %d %d 0x%x %s %s %s\n",
158 			entry->start, entry->end,
159 			resident, privateresident, id,
160 			(entry->protection & VM_PROT_READ)?"r":"-",
161 			(entry->protection & VM_PROT_WRITE)?"w":"-",
162 			(entry->protection & VM_PROT_EXECUTE)?"x":"-",
163 			ref_count, shadow_count, flags,
164 			(entry->eflags & MAP_ENTRY_COW)?"COW":"NCOW",
165 			(entry->eflags & MAP_ENTRY_NEEDS_COPY)?"NC":"NNC",
166 			type);
167 
168 		len = strlen(mebuffer);
169 		if (len > uio->uio_resid) {
170 			error = EFBIG;
171 			break;
172 		}
173 		error = uiomove(mebuffer, len, uio);
174 		if (error)
175 			break;
176 	}
177 	if (map != &curproc->p_vmspace->vm_map)
178 		vm_map_unlock_read(map);
179 	return error;
180 }
181 
182 int
183 procfs_validmap(p)
184 	struct proc *p;
185 {
186 	return ((p->p_flag & P_SYSTEM) == 0);
187 }
188