kernel - VM rework part 1 - Remove shadow_list
[dragonfly.git] / sys / vfs / procfs / procfs_map.c
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. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *      @(#)procfs_status.c     8.3 (Berkeley) 2/17/94
34  *
35  * $FreeBSD: src/sys/miscfs/procfs/procfs_map.c,v 1.24.2.1 2001/08/04 13:12:24 rwatson Exp $
36  * $DragonFly: src/sys/vfs/procfs/procfs_map.c,v 1.7 2007/02/19 01:14:24 corecode Exp $
37  */
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/proc.h>
42 #include <sys/vnode.h>
43 #include <sys/sbuf.h>
44 #include <vfs/procfs/procfs.h>
45
46 #include <vm/vm.h>
47 #include <sys/lock.h>
48 #include <vm/pmap.h>
49 #include <vm/vm_map.h>
50 #include <vm/vm_page.h>
51 #include <vm/vm_object.h>
52
53 #include <machine/limits.h>
54
55 int
56 procfs_domap(struct proc *curp, struct lwp *lp, struct pfsnode *pfs,
57              struct uio *uio)
58 {
59         struct proc *p = lp->lwp_proc;
60         ssize_t buflen = uio->uio_offset + uio->uio_resid;
61         struct vnode *vp;
62         char *fullpath, *freepath;
63         int error;
64         vm_map_t map = &p->p_vmspace->vm_map;
65         vm_map_entry_t entry;
66         struct sbuf *sb = NULL;
67         unsigned int last_timestamp;
68
69         if (uio->uio_rw != UIO_READ)
70                 return (EOPNOTSUPP);
71
72         error = 0;
73
74         if (uio->uio_offset < 0 || uio->uio_resid < 0 || buflen >= INT_MAX)
75                 return EINVAL;
76         sb = sbuf_new (sb, NULL, buflen+1, 0);
77         if (sb == NULL)
78                 return EIO;
79
80         /*
81          * Lock the map so we can access it.  Release the process token
82          * to avoid unnecessary token stalls while we are processing the
83          * map.
84          */
85         vm_map_lock_read(map);
86         lwkt_reltoken(&p->p_token);
87
88         RB_FOREACH(entry, vm_map_rb_tree, &map->rb_root) {
89                 vm_object_t obj, tobj, lobj;
90                 int ref_count, flags;
91                 vm_offset_t e_start, e_end;
92                 vm_eflags_t e_eflags;
93                 vm_prot_t e_prot;
94                 int resident;
95                 char *type;
96
97                 switch(entry->maptype) {
98                 case VM_MAPTYPE_NORMAL:
99                 case VM_MAPTYPE_VPAGETABLE:
100                         obj = entry->object.vm_object;
101                         if (obj != NULL) {
102                                 vm_object_hold(obj);
103                         }
104                         break;
105                 case VM_MAPTYPE_UKSMAP:
106                         obj = NULL;
107                         break;
108                 default:
109                         /* ignore entry */
110                         continue;
111                 }
112
113                 e_eflags = entry->eflags;
114                 e_prot = entry->protection;
115                 e_start = entry->start;
116                 e_end = entry->end;
117
118                 /*
119                  * Don't count resident pages, its impossible on 64-bit.
120                  * A single mapping could be gigabytes or terrabytes.
121                  */
122                 resident = -1;
123 #if 0
124                 pmap_t pmap = vmspace_pmap(p->p_vmspace);
125                 vm_offset_t addr;
126
127                 resident = 0;
128                 addr = entry->start;
129                 while (addr < entry->end) {
130                         if (pmap_extract(pmap, addr, NULL))
131                                 resident++;
132                         addr += PAGE_SIZE;
133                 }
134 #endif
135                 if (obj) {
136                         lobj = obj;
137                         while ((tobj = lobj->backing_object) != NULL) {
138                                 KKASSERT(tobj != obj);
139                                 vm_object_hold(tobj);
140                                 if (tobj == lobj->backing_object) {
141                                         if (lobj != obj) {
142                                                 vm_object_lock_swap();
143                                                 vm_object_drop(lobj);
144                                         }
145                                         lobj = tobj;
146                                 } else {
147                                         vm_object_drop(tobj);
148                                 }
149                         }
150                 } else {
151                         lobj = NULL;
152                 }
153                 last_timestamp = map->timestamp;
154                 vm_map_unlock(map);
155
156                 freepath = NULL;
157                 fullpath = "-";
158                 if (lobj) {
159                         switch(lobj->type) {
160                         default:
161                         case OBJT_DEFAULT:
162                                 type = "default";
163                                 vp = NULL;
164                                 break;
165                         case OBJT_VNODE:
166                                 type = "vnode";
167                                 vp = lobj->handle;
168                                 vref(vp);
169                                 break;
170                         case OBJT_SWAP:
171                                 type = "swap";
172                                 vp = NULL;
173                                 break;
174                         case OBJT_DEVICE:
175                                 type = "device";
176                                 vp = NULL;
177                                 break;
178                         case OBJT_MGTDEVICE:
179                                 type = "mgtdevice";
180                                 vp = NULL;
181                                 break;
182                         }
183                         if (lobj != obj)
184                                 vm_object_drop(lobj);
185                         
186                         flags = obj->flags;
187                         ref_count = obj->ref_count;
188                         vm_object_drop(obj);
189                         if (vp != NULL) {
190                                 vn_fullpath(p, vp, &fullpath, &freepath, 1);
191                                 vrele(vp);
192                         }
193                 } else {
194                         flags = 0;
195                         ref_count = 0;
196                         switch(entry->maptype) {
197                         case VM_MAPTYPE_UNSPECIFIED:
198                                 type = "unspec";
199                                 break;
200                         case VM_MAPTYPE_NORMAL:
201                                 type = "none";
202                                 break;
203                         case VM_MAPTYPE_VPAGETABLE:
204                                 type = "vpgtbl";
205                                 break;
206                         case VM_MAPTYPE_SUBMAP:
207                                 type = "submap";
208                                 break;
209                         case VM_MAPTYPE_UKSMAP:
210                                 type = "uksmap";
211                                 break;
212                         default:
213                                 type = "unknown";
214                                 break;
215                         }
216                 }
217
218                 /*
219                  * format:
220                  *  start, end, res, priv res, cow, access, type, (fullpath).
221                  */
222                 error = sbuf_printf(sb,
223 #if LONG_BIT == 64
224                           "0x%016lx 0x%016lx %d %d %p %s%s%s %d %d "
225 #else
226                           "0x%08lx 0x%08lx %d %d %p %s%s%s %d %d "
227 #endif
228                           "0x%04x %s%s %s %s\n",
229                         (u_long)e_start, (u_long)e_end,
230                         resident, -1, obj,
231                         (e_prot & VM_PROT_READ) ? "r" : "-",
232                         (e_prot & VM_PROT_WRITE) ? "w" : "-",
233                         (e_prot & VM_PROT_EXECUTE) ? "x" : "-",
234                         ref_count, 0, flags,
235                         (e_eflags & MAP_ENTRY_COW) ? "COW" : "NCOW",
236                         (e_eflags & MAP_ENTRY_NEEDS_COPY) ?" NC" : " NNC",
237                         type, fullpath);
238
239                 if (freepath != NULL) {
240                         kfree(freepath, M_TEMP);
241                         freepath = NULL;
242                 }
243
244                 vm_map_lock_read(map);
245                 if (error == -1) {
246                         error = 0;
247                         break;
248                 }
249
250                 if (last_timestamp != map->timestamp) {
251                         vm_map_entry_t reentry;
252                         vm_map_lookup_entry(map, e_start, &reentry);
253                         entry = reentry;
254                 }
255         }
256         vm_map_unlock_read(map);
257         if (sbuf_finish(sb) == 0)
258                 buflen = sbuf_len(sb);
259         error = uiomove_frombuf(sbuf_data(sb), buflen, uio);
260         sbuf_delete(sb);
261
262         lwkt_gettoken(&p->p_token);     /* re-acquire */
263
264         return error;
265 }
266
267 int
268 procfs_validmap(struct lwp *lp)
269 {
270         return ((lp->lwp_proc->p_flags & P_SYSTEM) == 0);
271 }