1 /* $NetBSD: procfs_map.c,v 1.47 2019/09/27 14:36:19 christos Exp $ */ 2 3 /* 4 * Copyright (c) 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Jan-Simon Pendry. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * @(#)procfs_status.c 8.3 (Berkeley) 2/17/94 35 * 36 * $FreeBSD: procfs_map.c,v 1.18 1998/12/04 22:54:51 archie Exp $ 37 */ 38 39 /* 40 * Copyright (c) 1993 Jan-Simon Pendry 41 * 42 * This code is derived from software contributed to Berkeley by 43 * Jan-Simon Pendry. 44 * 45 * Redistribution and use in source and binary forms, with or without 46 * modification, are permitted provided that the following conditions 47 * are met: 48 * 1. Redistributions of source code must retain the above copyright 49 * notice, this list of conditions and the following disclaimer. 50 * 2. Redistributions in binary form must reproduce the above copyright 51 * notice, this list of conditions and the following disclaimer in the 52 * documentation and/or other materials provided with the distribution. 53 * 3. All advertising materials mentioning features or use of this software 54 * must display the following acknowledgement: 55 * This product includes software developed by the University of 56 * California, Berkeley and its contributors. 57 * 4. Neither the name of the University nor the names of its contributors 58 * may be used to endorse or promote products derived from this software 59 * without specific prior written permission. 60 * 61 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 62 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 63 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 64 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 65 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 66 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 67 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 68 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 69 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 70 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 71 * SUCH DAMAGE. 72 * 73 * @(#)procfs_status.c 8.3 (Berkeley) 2/17/94 74 * 75 * $FreeBSD: procfs_map.c,v 1.18 1998/12/04 22:54:51 archie Exp $ 76 */ 77 78 #include <sys/cdefs.h> 79 __KERNEL_RCSID(0, "$NetBSD: procfs_map.c,v 1.47 2019/09/27 14:36:19 christos Exp $"); 80 81 #include <sys/param.h> 82 #include <sys/systm.h> 83 #include <sys/proc.h> 84 #include <sys/vnode.h> 85 #include <sys/malloc.h> 86 #include <sys/namei.h> 87 #include <sys/filedesc.h> 88 #include <miscfs/procfs/procfs.h> 89 90 #include <sys/lock.h> 91 92 #include <uvm/uvm.h> 93 94 #define BUFFERSIZE (64 * 1024) 95 #define MAXBUFFERSIZE (256 * 1024) 96 97 /* 98 * The map entries can *almost* be read with programs like cat. However, 99 * large maps need special programs to read. It is not easy to implement 100 * a program that can sense the required size of the buffer, and then 101 * subsequently do a read with the appropriate size. This operation cannot 102 * be atomic. The best that we can do is to allow the program to do a read 103 * with an arbitrarily large buffer, and return as much as we can. We can 104 * return an error code if the buffer is too small (EFBIG), then the program 105 * can try a bigger buffer. 106 */ 107 int 108 procfs_domap(struct lwp *curl, struct proc *p, struct pfsnode *pfs, 109 struct uio *uio, int linuxmode) 110 { 111 int error; 112 struct vmspace *vm; 113 struct vm_map *map; 114 struct vm_map_entry *entry; 115 char *buffer = NULL; 116 size_t bufsize = BUFFERSIZE; 117 char *path; 118 struct vnode *vp; 119 struct vattr va; 120 dev_t dev; 121 long fileid; 122 size_t pos; 123 int width = (int)((curl->l_proc->p_flag & PK_32) ? sizeof(int32_t) : 124 sizeof(void *)) * 2; 125 126 if (uio->uio_rw != UIO_READ) 127 return EOPNOTSUPP; 128 129 error = 0; 130 131 if (linuxmode != 0) 132 path = malloc(MAXPATHLEN * 4, M_TEMP, M_WAITOK); 133 else 134 path = NULL; 135 136 if ((error = proc_vmspace_getref(p, &vm)) != 0) 137 goto out; 138 139 map = &vm->vm_map; 140 vm_map_lock_read(map); 141 142 again: 143 buffer = malloc(bufsize, M_TEMP, M_WAITOK); 144 pos = 0; 145 for (entry = map->header.next; entry != &map->header; 146 entry = entry->next) { 147 148 if (UVM_ET_ISSUBMAP(entry)) 149 continue; 150 151 if (linuxmode != 0) { 152 *path = 0; 153 dev = (dev_t)0; 154 fileid = 0; 155 if (UVM_ET_ISOBJ(entry) && 156 UVM_OBJ_IS_VNODE(entry->object.uvm_obj)) { 157 vp = (struct vnode *)entry->object.uvm_obj; 158 vn_lock(vp, LK_SHARED | LK_RETRY); 159 error = VOP_GETATTR(vp, &va, curl->l_cred); 160 VOP_UNLOCK(vp); 161 if (error == 0 && vp != pfs->pfs_vnode) { 162 fileid = va.va_fileid; 163 dev = va.va_fsid; 164 error = vnode_to_path(path, 165 MAXPATHLEN * 4, vp, curl, p); 166 } 167 } 168 pos += snprintf(buffer + pos, bufsize - pos, 169 "%.*"PRIxVADDR"-%.*"PRIxVADDR" %c%c%c%c " 170 "%.*lx %.2llx:%.2llx %-8ld %25.s %s\n", 171 width, entry->start, 172 width, entry->end, 173 (entry->protection & VM_PROT_READ) ? 'r' : '-', 174 (entry->protection & VM_PROT_WRITE) ? 'w' : '-', 175 (entry->protection & VM_PROT_EXECUTE) ? 'x' : '-', 176 (entry->etype & UVM_ET_COPYONWRITE) ? 'p' : 's', 177 width, (unsigned long)entry->offset, 178 (unsigned long long)major(dev), 179 (unsigned long long)minor(dev), fileid, "", path); 180 } else { 181 pos += snprintf(buffer + pos, bufsize - pos, 182 "%#"PRIxVADDR" %#"PRIxVADDR" " 183 "%c%c%c %c%c%c %s %s %d %d %d\n", 184 entry->start, entry->end, 185 (entry->protection & VM_PROT_READ) ? 'r' : '-', 186 (entry->protection & VM_PROT_WRITE) ? 'w' : '-', 187 (entry->protection & VM_PROT_EXECUTE) ? 'x' : '-', 188 (entry->max_protection & VM_PROT_READ) ? 'r' : '-', 189 (entry->max_protection & VM_PROT_WRITE) ? 'w' : '-', 190 (entry->max_protection & VM_PROT_EXECUTE) ? 191 'x' : '-', 192 (entry->etype & UVM_ET_COPYONWRITE) ? 193 "COW" : "NCOW", 194 (entry->etype & UVM_ET_NEEDSCOPY) ? "NC" : "NNC", 195 entry->inheritance, entry->wired_count, 196 entry->advice); 197 } 198 if (pos >= bufsize) { 199 bufsize <<= 1; 200 if (bufsize > MAXBUFFERSIZE) { 201 error = ENOMEM; 202 vm_map_unlock_read(map); 203 uvmspace_free(vm); 204 goto out; 205 } 206 free(buffer, M_TEMP); 207 goto again; 208 } 209 } 210 211 vm_map_unlock_read(map); 212 uvmspace_free(vm); 213 214 /* 215 * We support reading from an offset, because linux does. 216 * The map could have changed between the two reads, and 217 * that could result in junk, but typically it does not. 218 */ 219 if ((uintmax_t)uio->uio_offset < pos) 220 error = uiomove(buffer + uio->uio_offset, 221 pos - uio->uio_offset, uio); 222 else 223 error = 0; 224 out: 225 if (path != NULL) 226 free(path, M_TEMP); 227 if (buffer != NULL) 228 free(buffer, M_TEMP); 229 230 return error; 231 } 232 233 int 234 procfs_validmap(struct lwp *l, struct mount *mp) 235 { 236 return ((l->l_flag & LW_SYSTEM) == 0); 237 } 238