xref: /freebsd/sys/fs/procfs/procfs_map.c (revision 952d112864d8008aa87278a30a539d888a8493cd)
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.10 1997/02/22 09:40:28 peter Exp $
40  */
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/time.h>
45 #include <sys/kernel.h>
46 #include <sys/proc.h>
47 #include <sys/vnode.h>
48 #include <sys/tty.h>
49 #include <sys/resource.h>
50 #include <sys/resourcevar.h>
51 #include <miscfs/procfs/procfs.h>
52 #include <sys/queue.h>
53 #include <sys/vmmeter.h>
54 #include <sys/mman.h>
55 #include <sys/malloc.h>
56 
57 #include <vm/vm.h>
58 #include <vm/vm_param.h>
59 #include <vm/vm_prot.h>
60 #include <vm/vm_inherit.h>
61 #include <sys/lock.h>
62 #include <vm/pmap.h>
63 #include <vm/vm_map.h>
64 #include <vm/vm_page.h>
65 #include <vm/vm_object.h>
66 #include <vm/vm_kern.h>
67 #include <vm/vm_pager.h>
68 #include <vm/vm_extern.h>
69 #include <vm/default_pager.h>
70 
71 
72 #define MEBUFFERSIZE 256
73 
74 /*
75  * The map entries can *almost* be read with programs like cat.  However,
76  * large maps need special programs to read.  It is not easy to implement
77  * a program that can sense the required size of the buffer, and then
78  * subsequently do a read with the appropriate size.  This operation cannot
79  * be atomic.  The best that we can do is to allow the program to do a read
80  * with an arbitrarily large buffer, and return as much as we can.  We can
81  * return an error code if the buffer is too small (EFBIG), then the program
82  * can try a bigger buffer.
83  */
84 int
85 procfs_domap(curp, p, pfs, uio)
86 	struct proc *curp;
87 	struct proc *p;
88 	struct pfsnode *pfs;
89 	struct uio *uio;
90 {
91 	int len;
92 	int error;
93 	vm_map_t map = &p->p_vmspace->vm_map;
94 	pmap_t pmap = &p->p_vmspace->vm_pmap;
95 	vm_map_entry_t entry;
96 	char mebuffer[MEBUFFERSIZE];
97 
98 	if (uio->uio_rw != UIO_READ)
99 		return (EOPNOTSUPP);
100 
101 	if (uio->uio_offset != 0)
102 		return (0);
103 
104 	error = 0;
105 	if (map != &curproc->p_vmspace->vm_map)
106 		vm_map_lock(map);
107 	for (entry = map->header.next;
108 		((uio->uio_resid > 0) && (entry != &map->header));
109 		entry = entry->next) {
110 		vm_object_t obj, tobj, lobj;
111 		vm_offset_t addr;
112 		int resident, privateresident;
113 		char *type;
114 
115 		if (entry->eflags & (MAP_ENTRY_IS_A_MAP|MAP_ENTRY_IS_SUB_MAP))
116 			continue;
117 
118 		obj = entry->object.vm_object;
119 		if (obj && (obj->ref_count == 1))
120 			privateresident = obj->resident_page_count;
121 		else
122 			privateresident = 0;
123 
124 		resident = 0;
125 		addr = entry->start;
126 		while (addr < entry->end) {
127 			if (pmap_extract( pmap, addr))
128 				resident++;
129 			addr += PAGE_SIZE;
130 		}
131 
132 		for( lobj = tobj = obj; tobj; tobj = tobj->backing_object)
133 			lobj = tobj;
134 
135 		if (lobj) switch(lobj->type) {
136 
137 default:
138 case OBJT_DEFAULT:
139 			type = "default";
140 			break;
141 case OBJT_VNODE:
142 			type = "vnode";
143 			break;
144 case OBJT_SWAP:
145 			type = "swap";
146 			break;
147 case OBJT_DEVICE:
148 			type = "device";
149 			break;
150 		} else {
151 			type = "none";
152 		}
153 
154 
155 		/*
156 		 * format:
157 		 *  start, end, resident, private resident, cow, access, type.
158 		 */
159 		sprintf(mebuffer, "0x%-8.8x 0x%-8.8x %9d %9d %s%s%s %s %s\n",
160 			entry->start, entry->end,
161 			resident, privateresident,
162 			(entry->protection & VM_PROT_READ)?"r":"-",
163 			(entry->protection & VM_PROT_WRITE)?"w":"-",
164 			(entry->protection & VM_PROT_EXECUTE)?"x":"-",
165 			(entry->eflags & MAP_ENTRY_COW)?"COW":"   ",
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(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