Merge branch 'vendor/DHCPCD'
[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  */
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/uio.h>
41 #include <sys/proc.h>
42 #include <sys/vnode.h>
43 #include <sys/sbuf.h>
44 #include <sys/malloc.h>
45 #include <vfs/procfs/procfs.h>
46
47 #include <vm/vm.h>
48 #include <sys/lock.h>
49 #include <vm/pmap.h>
50 #include <vm/vm_map.h>
51 #include <vm/vm_page.h>
52 #include <vm/vm_object.h>
53
54 #include <machine/limits.h>
55
56 int
57 procfs_domap(struct proc *curp, struct lwp *lp, struct pfsnode *pfs,
58              struct uio *uio)
59 {
60         struct proc *p = lp->lwp_proc;
61         ssize_t buflen = uio->uio_offset + uio->uio_resid;
62         struct vnode *vp;
63         char *fullpath, *freepath;
64         int error;
65         vm_map_t map = &p->p_vmspace->vm_map;
66         vm_map_entry_t entry;
67         struct sbuf *sb = NULL;
68         unsigned int last_timestamp;
69
70         if (uio->uio_rw != UIO_READ)
71                 return (EOPNOTSUPP);
72
73         error = 0;
74
75         if (uio->uio_offset < 0 || uio->uio_resid < 0 || buflen >= INT_MAX)
76                 return EINVAL;
77         sb = sbuf_new (sb, NULL, buflen+1, 0);
78         if (sb == NULL)
79                 return EIO;
80
81         /*
82          * Lock the map so we can access it.  Release the process token
83          * to avoid unnecessary token stalls while we are processing the
84          * map.
85          */
86         vm_map_lock_read(map);
87         lwkt_reltoken(&p->p_token);
88
89         RB_FOREACH(entry, vm_map_rb_tree, &map->rb_root) {
90                 vm_map_backing_t ba;
91                 vm_object_t obj;
92                 int ref_count, flags;
93                 vm_offset_t e_start, e_end;
94                 vm_eflags_t e_eflags;
95                 vm_prot_t e_prot;
96                 int resident;
97                 char *type;
98
99                 switch(entry->maptype) {
100                 case VM_MAPTYPE_NORMAL:
101                 case VM_MAPTYPE_VPAGETABLE:
102                         ba = &entry->ba;
103                         break;
104                 case VM_MAPTYPE_UKSMAP:
105                         ba = NULL;
106                         break;
107                 default:
108                         /* ignore entry */
109                         continue;
110                 }
111
112                 e_eflags = entry->eflags;
113                 e_prot = entry->protection;
114                 e_start = entry->ba.start;
115                 e_end = entry->ba.end;
116
117                 /*
118                  * Don't count resident pages, its impossible on 64-bit.
119                  * A single mapping could be gigabytes or terrabytes.
120                  */
121                 resident = -1;
122 #if 0
123                 pmap_t pmap = vmspace_pmap(p->p_vmspace);
124                 vm_offset_t addr;
125
126                 resident = 0;
127                 addr = entry->ba.start;
128                 while (addr < entry->ba.end) {
129                         if (pmap_extract(pmap, addr, NULL))
130                                 resident++;
131                         addr += PAGE_SIZE;
132                 }
133 #endif
134                 if (ba) {
135                         while (ba->backing_ba)
136                                 ba = ba->backing_ba;
137                         obj = ba->object;
138                         if (obj)
139                                 vm_object_hold(obj);
140                 } else {
141                         obj = NULL;
142                 }
143                 last_timestamp = map->timestamp;
144                 vm_map_unlock(map);
145
146                 freepath = NULL;
147                 fullpath = "-";
148                 flags = 0;
149                 ref_count = 0;
150
151                 if (obj) {
152                         switch(obj->type) {
153                         default:
154                         case OBJT_DEFAULT:
155                                 type = "default";
156                                 vp = NULL;
157                                 break;
158                         case OBJT_VNODE:
159                                 type = "vnode";
160                                 vp = obj->handle;
161                                 vref(vp);
162                                 break;
163                         case OBJT_SWAP:
164                                 type = "swap";
165                                 vp = NULL;
166                                 break;
167                         case OBJT_DEVICE:
168                                 type = "device";
169                                 vp = NULL;
170                                 break;
171                         case OBJT_MGTDEVICE:
172                                 type = "mgtdevice";
173                                 vp = NULL;
174                                 break;
175                         }
176                         if (ba->object) {
177                                 flags = ba->object->flags;
178                                 ref_count = ba->object->ref_count;
179                         }
180                         vm_object_drop(obj);
181                         if (vp) {
182                                 vn_fullpath(p, vp, &fullpath, &freepath, 1);
183                                 vrele(vp);
184                         }
185                 } else {
186                         switch(entry->maptype) {
187                         case VM_MAPTYPE_UNSPECIFIED:
188                                 type = "unspec";
189                                 break;
190                         case VM_MAPTYPE_NORMAL:
191                                 type = "none";
192                                 break;
193                         case VM_MAPTYPE_VPAGETABLE:
194                                 type = "vpgtbl";
195                                 break;
196                         case VM_MAPTYPE_SUBMAP:
197                                 type = "submap";
198                                 break;
199                         case VM_MAPTYPE_UKSMAP:
200                                 type = "uksmap";
201                                 break;
202                         default:
203                                 type = "unknown";
204                                 break;
205                         }
206                 }
207
208                 /*
209                  * format:
210                  *  start, end, res, priv res, cow, access, type, (fullpath).
211                  */
212                 error = sbuf_printf(sb,
213 #if LONG_BIT == 64
214                           "0x%016lx 0x%016lx %d %d %p %s%s%s %d %d "
215 #else
216                           "0x%08lx 0x%08lx %d %d %p %s%s%s %d %d "
217 #endif
218                           "0x%04x %s%s %s %s\n",
219                         (u_long)e_start, (u_long)e_end,
220                         resident, -1, (ba ? ba->object : NULL),
221                         (e_prot & VM_PROT_READ) ? "r" : "-",
222                         (e_prot & VM_PROT_WRITE) ? "w" : "-",
223                         (e_prot & VM_PROT_EXECUTE) ? "x" : "-",
224                         ref_count, 0, flags,
225                         (e_eflags & MAP_ENTRY_COW) ? "COW" : "NCOW",
226                         (e_eflags & MAP_ENTRY_NEEDS_COPY) ?" NC" : " NNC",
227                         type, fullpath);
228
229                 if (freepath != NULL) {
230                         kfree(freepath, M_TEMP);
231                         freepath = NULL;
232                 }
233
234                 vm_map_lock_read(map);
235                 if (error == -1) {
236                         error = 0;
237                         break;
238                 }
239
240                 if (last_timestamp != map->timestamp) {
241                         vm_map_entry_t reentry;
242                         vm_map_lookup_entry(map, e_start, &reentry);
243                         entry = reentry;
244                 }
245         }
246         vm_map_unlock_read(map);
247         if (sbuf_finish(sb) == 0)
248                 buflen = sbuf_len(sb);
249         error = uiomove_frombuf(sbuf_data(sb), buflen, uio);
250         sbuf_delete(sb);
251
252         lwkt_gettoken(&p->p_token);     /* re-acquire */
253
254         return error;
255 }
256
257 int
258 procfs_validmap(struct lwp *lp)
259 {
260         return ((lp->lwp_proc->p_flags & P_SYSTEM) == 0);
261 }