xref: /dragonfly/sys/vfs/procfs/procfs_map.c (revision 984263bc)
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: src/sys/miscfs/procfs/procfs_map.c,v 1.24.2.1 2001/08/04 13:12:24 rwatson 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 <sys/lock.h>
50 #include <vm/pmap.h>
51 #include <vm/vm_map.h>
52 #include <vm/vm_page.h>
53 #include <vm/vm_object.h>
54 
55 
56 #define MEBUFFERSIZE 256
57 
58 /*
59  * The map entries can *almost* be read with programs like cat.  However,
60  * large maps need special programs to read.  It is not easy to implement
61  * a program that can sense the required size of the buffer, and then
62  * subsequently do a read with the appropriate size.  This operation cannot
63  * be atomic.  The best that we can do is to allow the program to do a read
64  * with an arbitrarily large buffer, and return as much as we can.  We can
65  * return an error code if the buffer is too small (EFBIG), then the program
66  * can try a bigger buffer.
67  */
68 int
69 procfs_domap(curp, p, pfs, uio)
70 	struct proc *curp;
71 	struct proc *p;
72 	struct pfsnode *pfs;
73 	struct uio *uio;
74 {
75 	int len;
76 	int error;
77 	vm_map_t map = &p->p_vmspace->vm_map;
78 	pmap_t pmap = vmspace_pmap(p->p_vmspace);
79 	vm_map_entry_t entry;
80 	char mebuffer[MEBUFFERSIZE];
81 
82 	if (uio->uio_rw != UIO_READ)
83 		return (EOPNOTSUPP);
84 
85 	if (uio->uio_offset != 0)
86 		return (0);
87 
88 	error = 0;
89 	if (map != &curproc->p_vmspace->vm_map)
90 		vm_map_lock_read(map);
91 	for (entry = map->header.next;
92 		((uio->uio_resid > 0) && (entry != &map->header));
93 		entry = entry->next) {
94 		vm_object_t obj, tobj, lobj;
95 		int ref_count, shadow_count, flags;
96 		vm_offset_t addr;
97 		int resident, privateresident;
98 		char *type;
99 
100 		if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
101 			continue;
102 
103 		obj = entry->object.vm_object;
104 		if (obj && (obj->shadow_count == 1))
105 			privateresident = obj->resident_page_count;
106 		else
107 			privateresident = 0;
108 
109 		resident = 0;
110 		addr = entry->start;
111 		while (addr < entry->end) {
112 			if (pmap_extract( pmap, addr))
113 				resident++;
114 			addr += PAGE_SIZE;
115 		}
116 
117 		for( lobj = tobj = obj; tobj; tobj = tobj->backing_object)
118 			lobj = tobj;
119 
120 		if (lobj) {
121 			switch(lobj->type) {
122 
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 		/*
150 		 * format:
151 		 *  start, end, resident, private resident, cow, access, type.
152 		 */
153 		snprintf(mebuffer, sizeof(mebuffer),
154 		    "0x%lx 0x%lx %d %d %p %s%s%s %d %d 0x%x %s %s %s\n",
155 			(u_long)entry->start, (u_long)entry->end,
156 			resident, privateresident, obj,
157 			(entry->protection & VM_PROT_READ)?"r":"-",
158 			(entry->protection & VM_PROT_WRITE)?"w":"-",
159 			(entry->protection & VM_PROT_EXECUTE)?"x":"-",
160 			ref_count, shadow_count, flags,
161 			(entry->eflags & MAP_ENTRY_COW)?"COW":"NCOW",
162 			(entry->eflags & MAP_ENTRY_NEEDS_COPY)?"NC":"NNC",
163 			type);
164 
165 		len = strlen(mebuffer);
166 		if (len > uio->uio_resid) {
167 			error = EFBIG;
168 			break;
169 		}
170 		error = uiomove(mebuffer, len, uio);
171 		if (error)
172 			break;
173 	}
174 	if (map != &curproc->p_vmspace->vm_map)
175 		vm_map_unlock_read(map);
176 
177 	return error;
178 }
179 
180 int
181 procfs_validmap(p)
182 	struct proc *p;
183 {
184 	return ((p->p_flag & P_SYSTEM) == 0);
185 }
186