Annotation of XNU/bsd/miscfs/procfs/procfs_mem.c, revision 1.1

1.1     ! root        1: /*
        !             2:  * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
        !             3:  *
        !             4:  * @APPLE_LICENSE_HEADER_START@
        !             5:  * 
        !             6:  * The contents of this file constitute Original Code as defined in and
        !             7:  * are subject to the Apple Public Source License Version 1.1 (the
        !             8:  * "License").  You may not use this file except in compliance with the
        !             9:  * License.  Please obtain a copy of the License at
        !            10:  * http://www.apple.com/publicsource and read it before using this file.
        !            11:  * 
        !            12:  * This Original Code and all software distributed under the License are
        !            13:  * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
        !            14:  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
        !            15:  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
        !            16:  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
        !            17:  * License for the specific language governing rights and limitations
        !            18:  * under the License.
        !            19:  * 
        !            20:  * @APPLE_LICENSE_HEADER_END@
        !            21:  */
        !            22: /*     $NetBSD: procfs_mem.c,v 1.7 1995/01/05 07:10:54 chopps Exp $    */
        !            23: 
        !            24: /*
        !            25:  * Copyright (c) 1993 Jan-Simon Pendry
        !            26:  * Copyright (c) 1993 Sean Eric Fagan
        !            27:  * Copyright (c) 1993
        !            28:  *     The Regents of the University of California.  All rights reserved.
        !            29:  *
        !            30:  * This code is derived from software contributed to Berkeley by
        !            31:  * Jan-Simon Pendry and Sean Eric Fagan.
        !            32:  *
        !            33:  * Redistribution and use in source and binary forms, with or without
        !            34:  * modification, are permitted provided that the following conditions
        !            35:  * are met:
        !            36:  * 1. Redistributions of source code must retain the above copyright
        !            37:  *    notice, this list of conditions and the following disclaimer.
        !            38:  * 2. Redistributions in binary form must reproduce the above copyright
        !            39:  *    notice, this list of conditions and the following disclaimer in the
        !            40:  *    documentation and/or other materials provided with the distribution.
        !            41:  * 3. All advertising materials mentioning features or use of this software
        !            42:  *    must display the following acknowledgement:
        !            43:  *     This product includes software developed by the University of
        !            44:  *     California, Berkeley and its contributors.
        !            45:  * 4. Neither the name of the University nor the names of its contributors
        !            46:  *    may be used to endorse or promote products derived from this software
        !            47:  *    without specific prior written permission.
        !            48:  *
        !            49:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
        !            50:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
        !            51:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
        !            52:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
        !            53:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
        !            54:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
        !            55:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
        !            56:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
        !            57:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
        !            58:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
        !            59:  * SUCH DAMAGE.
        !            60:  *
        !            61:  *     @(#)procfs_mem.c        8.5 (Berkeley) 6/15/94
        !            62:  */
        !            63: 
        !            64: /*
        !            65:  * This is a lightly hacked and merged version
        !            66:  * of sef's pread/pwrite functions
        !            67:  */
        !            68: 
        !            69: #include <sys/param.h>
        !            70: #include <sys/systm.h>
        !            71: #include <sys/time.h>
        !            72: #include <sys/kernel.h>
        !            73: #include <sys/proc.h>
        !            74: #include <sys/vnode.h>
        !            75: #include <miscfs/procfs/procfs.h>
        !            76: #include <vm/vm.h>
        !            77: #include <vm/vm_kern.h>
        !            78: #include <vm/vm_page.h>
        !            79: 
        !            80: static int
        !            81: procfs_rwmem(p, uio)
        !            82:        struct proc *p;
        !            83:        struct uio *uio;
        !            84: {
        !            85:        int error;
        !            86:        int writing;
        !            87: 
        !            88:        writing = uio->uio_rw == UIO_WRITE;
        !            89: 
        !            90:        /*
        !            91:         * Only map in one page at a time.  We don't have to, but it
        !            92:         * makes things easier.  This way is trivial - right?
        !            93:         */
        !            94:        do {
        !            95:                vm_map_t map, tmap;
        !            96:                vm_object_t object;
        !            97:                vm_offset_t kva;
        !            98:                vm_offset_t uva;
        !            99:                int page_offset;                /* offset into page */
        !           100:                vm_offset_t pageno;             /* page number */
        !           101:                vm_map_entry_t out_entry;
        !           102:                vm_prot_t out_prot;
        !           103:                vm_page_t m;
        !           104:                boolean_t wired, single_use;
        !           105:                vm_offset_t off;
        !           106:                u_int len;
        !           107:                int fix_prot;
        !           108: 
        !           109:                uva = (vm_offset_t) uio->uio_offset;
        !           110:                if (uva > VM_MAXUSER_ADDRESS) {
        !           111:                        error = 0;
        !           112:                        break;
        !           113:                }
        !           114: 
        !           115:                /*
        !           116:                 * Get the page number of this segment.
        !           117:                 */
        !           118:                pageno = trunc_page(uva);
        !           119:                page_offset = uva - pageno;
        !           120: 
        !           121:                /*
        !           122:                 * How many bytes to copy
        !           123:                 */
        !           124:                len = min(PAGE_SIZE - page_offset, uio->uio_resid);
        !           125: 
        !           126:                /*
        !           127:                 * The map we want...
        !           128:                 */
        !           129:                map = &p->p_vmspace->vm_map;
        !           130:   
        !           131:                /*
        !           132:                 * Check the permissions for the area we're interested
        !           133:                 * in.
        !           134:                 */
        !           135:                fix_prot = 0;
        !           136:                if (writing)
        !           137:                        fix_prot = !vm_map_check_protection(map, pageno,
        !           138:                                        pageno + PAGE_SIZE, VM_PROT_WRITE);
        !           139: 
        !           140:                if (fix_prot) {
        !           141:                        /*
        !           142:                         * If the page is not writable, we make it so.
        !           143:                         * XXX It is possible that a page may *not* be
        !           144:                         * read/executable, if a process changes that!
        !           145:                         * We will assume, for now, that a page is either
        !           146:                         * VM_PROT_ALL, or VM_PROT_READ|VM_PROT_EXECUTE.
        !           147:                         */
        !           148:                        error = vm_map_protect(map, pageno,
        !           149:                                        pageno + PAGE_SIZE, VM_PROT_ALL, 0);
        !           150:                        if (error)
        !           151:                                break;
        !           152:                }
        !           153: 
        !           154:                /*
        !           155:                 * Now we need to get the page.  out_entry, out_prot, wired,
        !           156:                 * and single_use aren't used.  One would think the vm code
        !           157:                 * would be a *bit* nicer...  We use tmap because
        !           158:                 * vm_map_lookup() can change the map argument.
        !           159:                 */
        !           160:                tmap = map;
        !           161:                error = vm_map_lookup(&tmap, pageno,
        !           162:                                      writing ? VM_PROT_WRITE : VM_PROT_READ,
        !           163:                                      &out_entry, &object, &off, &out_prot,
        !           164:                                      &wired, &single_use);
        !           165:                /*
        !           166:                 * We're done with tmap now.
        !           167:                 */
        !           168:                if (!error)
        !           169:                        vm_map_lookup_done(tmap, out_entry);
        !           170:   
        !           171:                /*
        !           172:                 * Fault the page in...
        !           173:                 */
        !           174:                if (!error && writing && object->shadow) {
        !           175:                        m = vm_page_lookup(object, off);
        !           176:                        if (m == 0 || (m->flags & PG_COPYONWRITE))
        !           177:                                error = vm_fault(map, pageno,
        !           178:                                                        VM_PROT_WRITE, FALSE);
        !           179:                }
        !           180: 
        !           181:                /* Find space in kernel_map for the page we're interested in */
        !           182:                if (!error) {
        !           183:                        kva = VM_MIN_KERNEL_ADDRESS;
        !           184:                        error = vm_map_find(kernel_map, object, off, &kva,
        !           185:                                        PAGE_SIZE, 1);
        !           186:                }
        !           187: 
        !           188:                if (!error) {
        !           189:                        /*
        !           190:                         * Neither vm_map_lookup() nor vm_map_find() appear
        !           191:                         * to add a reference count to the object, so we do
        !           192:                         * that here and now.
        !           193:                         */
        !           194:                        vm_object_reference(object);
        !           195: 
        !           196:                        /*
        !           197:                         * Mark the page we just found as pageable.
        !           198:                         */
        !           199:                        error = vm_map_pageable(kernel_map, kva,
        !           200:                                kva + PAGE_SIZE, 0);
        !           201: 
        !           202:                        /*
        !           203:                         * Now do the i/o move.
        !           204:                         */
        !           205:                        if (!error)
        !           206:                                error = uiomove(kva + page_offset, len, uio);
        !           207: 
        !           208:                        vm_map_remove(kernel_map, kva, kva + PAGE_SIZE);
        !           209:                }
        !           210:                if (fix_prot)
        !           211:                        vm_map_protect(map, pageno, pageno + PAGE_SIZE,
        !           212:                                        VM_PROT_READ|VM_PROT_EXECUTE, 0);
        !           213:        } while (error == 0 && uio->uio_resid > 0);
        !           214: 
        !           215:        return (error);
        !           216: }
        !           217: 
        !           218: /*
        !           219:  * Copy data in and out of the target process.
        !           220:  * We do this by mapping the process's page into
        !           221:  * the kernel and then doing a uiomove direct
        !           222:  * from the kernel address space.
        !           223:  */
        !           224: int
        !           225: procfs_domem(curp, p, pfs, uio)
        !           226:        struct proc *curp;
        !           227:        struct proc *p;
        !           228:        struct pfsnode *pfs;
        !           229:        struct uio *uio;
        !           230: {
        !           231: 
        !           232:        if (uio->uio_resid == 0)
        !           233:                return (0);
        !           234: 
        !           235:        return (procfs_rwmem(p, uio));
        !           236: }
        !           237: 
        !           238: /*
        !           239:  * Given process (p), find the vnode from which
        !           240:  * it's text segment is being executed.
        !           241:  *
        !           242:  * It would be nice to grab this information from
        !           243:  * the VM system, however, there is no sure-fire
        !           244:  * way of doing that.  Instead, fork(), exec() and
        !           245:  * wait() all maintain the p_textvp field in the
        !           246:  * process proc structure which contains a held
        !           247:  * reference to the exec'ed vnode.
        !           248:  */
        !           249: struct vnode *
        !           250: procfs_findtextvp(p)
        !           251:        struct proc *p;
        !           252: {
        !           253: 
        !           254:        return (p->p_textvp);
        !           255: }
        !           256: 
        !           257: 
        !           258: #ifdef probably_never
        !           259: /*
        !           260:  * Given process (p), find the vnode from which
        !           261:  * it's text segment is being mapped.
        !           262:  *
        !           263:  * (This is here, rather than in procfs_subr in order
        !           264:  * to keep all the VM related code in one place.)
        !           265:  */
        !           266: struct vnode *
        !           267: procfs_findtextvp(p)
        !           268:        struct proc *p;
        !           269: {
        !           270:        int error;
        !           271:        vm_object_t object;
        !           272:        vm_offset_t pageno;             /* page number */
        !           273: 
        !           274:        /* find a vnode pager for the user address space */
        !           275: 
        !           276:        for (pageno = VM_MIN_ADDRESS;
        !           277:                        pageno < VM_MAXUSER_ADDRESS;
        !           278:                        pageno += PAGE_SIZE) {
        !           279:                vm_map_t map;
        !           280:                vm_map_entry_t out_entry;
        !           281:                vm_prot_t out_prot;
        !           282:                boolean_t wired, single_use;
        !           283:                vm_offset_t off;
        !           284: 
        !           285:                map = &p->p_vmspace->vm_map;
        !           286:                error = vm_map_lookup(&map, pageno,
        !           287:                              VM_PROT_READ,
        !           288:                              &out_entry, &object, &off, &out_prot,
        !           289:                              &wired, &single_use);
        !           290: 
        !           291:                if (!error) {
        !           292:                        vm_pager_t pager;
        !           293: 
        !           294:                        printf("procfs: found vm object\n");
        !           295:                        vm_map_lookup_done(map, out_entry);
        !           296:                        printf("procfs: vm object = %x\n", object);
        !           297: 
        !           298:                        /*
        !           299:                         * At this point, assuming no errors, object
        !           300:                         * is the VM object mapping UVA (pageno).
        !           301:                         * Ensure it has a vnode pager, then grab
        !           302:                         * the vnode from that pager's handle.
        !           303:                         */
        !           304: 
        !           305:                        pager = object->pager;
        !           306:                        printf("procfs: pager = %x\n", pager);
        !           307:                        if (pager)
        !           308:                                printf("procfs: found pager, type = %d\n", pager->pg_type);
        !           309:                        if (pager && pager->pg_type == PG_VNODE) {
        !           310:                                struct vnode *vp;
        !           311: 
        !           312:                                vp = (struct vnode *) pager->pg_handle;
        !           313:                                printf("procfs: vp = 0x%x\n", vp);
        !           314:                                return (vp);
        !           315:                        }
        !           316:                }
        !           317:        }
        !           318: 
        !           319:        printf("procfs: text object not found\n");
        !           320:        return (0);
        !           321: }
        !           322: #endif /* probably_never */

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.