xref: /freebsd/sys/fs/procfs/procfs_map.c (revision e627b39baccd1ec9129690167cf5e6d860509655)
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.2 1996/06/18 05:15:59 dyson 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/ioctl.h>
49 #include <sys/tty.h>
50 #include <sys/resource.h>
51 #include <sys/resourcevar.h>
52 #include <miscfs/procfs/procfs.h>
53 #include <sys/queue.h>
54 #include <sys/vmmeter.h>
55 #include <sys/mman.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 <vm/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 MAXKBUFFER 16384
73 
74 int
75 procfs_domap(curp, p, pfs, uio)
76 	struct proc *curp;
77 	struct proc *p;
78 	struct pfsnode *pfs;
79 	struct uio *uio;
80 {
81 	int len;
82 	int error;
83 	/*
84 	 * dynamically allocated buffer for entire snapshot
85 	 */
86 	char *kbuffer, *kbufferp;
87 	/*
88 	 * buffer for each map entry
89 	 */
90 	char mebuffer[256];
91 	vm_map_t map = &p->p_vmspace->vm_map;
92 	pmap_t pmap = &p->p_vmspace->vm_pmap;
93 	vm_map_entry_t entry;
94 
95 	if (uio->uio_rw != UIO_READ)
96 		return (EOPNOTSUPP);
97 
98 	if (uio->uio_offset != 0)
99 		return (0);
100 
101 	kbuffer = (char *)kmem_alloc_pageable(kernel_map, MAXKBUFFER);
102 	kbufferp = kbuffer;
103 	vm_map_lock(map);
104 	for (entry = map->header.next; entry != &map->header;
105 		entry = entry->next) {
106 		vm_object_t obj, tobj, lobj;
107 		vm_offset_t addr;
108 		int resident, privateresident;
109 		char *type;
110 
111 		if (entry->is_a_map || entry->is_sub_map)
112 			continue;
113 
114 		obj = entry->object.vm_object;
115 		if (obj && (obj->ref_count == 1))
116 			privateresident = obj->resident_page_count;
117 		else
118 			privateresident = 0;
119 
120 		resident = 0;
121 		addr = entry->start;
122 		while (addr < entry->end) {
123 			if (pmap_extract( pmap, addr))
124 				resident++;
125 			addr += PAGE_SIZE;
126 		}
127 
128 		for( lobj = tobj = obj; tobj; tobj = tobj->backing_object)
129 			lobj = tobj;
130 
131 		if (lobj) switch(lobj->type) {
132 
133 default:
134 case OBJT_DEFAULT:
135 			type = "default";
136 			break;
137 case OBJT_VNODE:
138 			type = "vnode";
139 			break;
140 case OBJT_SWAP:
141 			type = "swap";
142 			break;
143 case OBJT_DEVICE:
144 			type = "device";
145 			break;
146 		} else {
147 			type = "none";
148 		}
149 
150 
151 		/*
152 		 * format:
153 		 *  start, end, resident, private resident, cow, access, type.
154 		 */
155 		sprintf(mebuffer, "0x%-8.8x 0x%-8.8x %9d %9d %s%s%s %s %s\n",
156 			entry->start, entry->end,
157 			resident, privateresident,
158 			(entry->protection & VM_PROT_READ)?"r":"-",
159 			(entry->protection & VM_PROT_WRITE)?"w":"-",
160 			(entry->protection & VM_PROT_EXECUTE)?"x":"-",
161 			entry->copy_on_write?"COW":"   ",
162 			type);
163 
164 		len = strlen(mebuffer);
165 		if (len + (kbufferp - kbuffer) < MAXKBUFFER) {
166 			memcpy(kbufferp, mebuffer, len);
167 			kbufferp += len;
168 		}
169 	}
170 	vm_map_unlock(map);
171 	error = uiomove(kbuffer, (kbufferp - kbuffer), uio);
172 	kmem_free(kernel_map, (vm_offset_t)kbuffer, MAXKBUFFER);
173 	return error;
174 }
175 
176 int
177 procfs_validmap(p)
178 	struct proc *p;
179 {
180 	return ((p->p_flag & P_SYSTEM) == 0);
181 }
182