procfs_map.c revision 1.5 1 /* $NetBSD: procfs_map.c,v 1.5 1999/04/10 14:00:38 drochner Exp $ */
2
3 /*
4 * Copyright (c) 1993 Jan-Simon Pendry
5 * Copyright (c) 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * Jan-Simon Pendry.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 *
39 * @(#)procfs_status.c 8.3 (Berkeley) 2/17/94
40 *
41 * $FreeBSD: procfs_map.c,v 1.18 1998/12/04 22:54:51 archie Exp $
42 */
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/proc.h>
47 #include <sys/vnode.h>
48 #include <miscfs/procfs/procfs.h>
49
50 #include <vm/vm.h>
51 #include <vm/vm_prot.h>
52 #include <sys/lock.h>
53 #include <vm/pmap.h>
54 #include <vm/vm_map.h>
55 #include <vm/vm_page.h>
56
57 #include <uvm/uvm.h>
58
59 #define MEBUFFERSIZE 256
60
61 /*
62 * The map entries can *almost* be read with programs like cat. However,
63 * large maps need special programs to read. It is not easy to implement
64 * a program that can sense the required size of the buffer, and then
65 * subsequently do a read with the appropriate size. This operation cannot
66 * be atomic. The best that we can do is to allow the program to do a read
67 * with an arbitrarily large buffer, and return as much as we can. We can
68 * return an error code if the buffer is too small (EFBIG), then the program
69 * can try a bigger buffer.
70 */
71 int
72 procfs_domap(curp, p, pfs, uio)
73 struct proc *curp;
74 struct proc *p;
75 struct pfsnode *pfs;
76 struct uio *uio;
77 {
78 int len;
79 int error;
80 vm_map_t map = &p->p_vmspace->vm_map;
81 vm_map_entry_t entry;
82 char mebuffer[MEBUFFERSIZE];
83
84 if (uio->uio_rw != UIO_READ)
85 return (EOPNOTSUPP);
86
87 if (uio->uio_offset != 0)
88 return (0);
89
90 error = 0;
91 if (map != &curproc->p_vmspace->vm_map)
92 vm_map_lock_read(map);
93 for (entry = map->header.next;
94 ((uio->uio_resid > 0) && (entry != &map->header));
95 entry = entry->next) {
96
97 if (UVM_ET_ISSUBMAP(entry))
98 continue;
99
100 /*
101 * format:
102 * start, end, resident, private resident, cow, access, type.
103 */
104 snprintf(mebuffer, sizeof(mebuffer),
105 "0x%lx 0x%lx %c%c%c %c%c%c %s %s %d %d %d\n",
106 entry->start, entry->end,
107
108 (entry->protection & VM_PROT_READ) ? 'r' : '-',
109 (entry->protection & VM_PROT_WRITE) ? 'w' : '-',
110 (entry->protection & VM_PROT_EXECUTE) ? 'x' : '-',
111 (entry->max_protection & VM_PROT_READ) ? 'r' : '-',
112 (entry->max_protection & VM_PROT_WRITE) ? 'w' : '-',
113 (entry->max_protection & VM_PROT_EXECUTE) ? 'x' : '-',
114
115 (entry->etype & UVM_ET_COPYONWRITE) ? "COW" : "NCOW",
116 (entry->etype & UVM_ET_NEEDSCOPY) ? "NC" : "NNC",
117 entry->inheritance, entry->wired_count , entry->advice
118 );
119
120
121 len = strlen(mebuffer);
122 if (len > uio->uio_resid) {
123 error = EFBIG;
124 break;
125 }
126 error = uiomove(mebuffer, len, uio);
127 if (error)
128 break;
129 }
130 if (map != &curproc->p_vmspace->vm_map)
131 vm_map_unlock_read(map);
132 return error;
133 }
134
135 int
136 procfs_validmap(p)
137 struct proc *p;
138 {
139 return ((p->p_flag & P_SYSTEM) == 0);
140 }
141