Source to bsd/dev/ppc/mem.c
/*
* Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
* Reserved. This file contains Original Code and/or Modifications of
* Original Code as defined in and that are subject to the Apple Public
* Source License Version 1.0 (the 'License'). You may not use this file
* except in compliance with the License. Please obtain a copy of the
* License at http://www.apple.com/publicsource and read it before using
* this file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
* License for the specific language governing rights and limitations
* under the License."
*
* @APPLE_LICENSE_HEADER_END@
*/
/*-
* Copyright (c) 1988 University of Utah.
* Copyright (c) 1982, 1986, 1990, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* the Systems Programming Group of the University of Utah Computer
* Science Department, and code derived from software contributed to
* Berkeley by William Jolitz.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* from: Utah $Hdr: mem.c 1.13 89/10/08$
* @(#)mem.c 8.1 (Berkeley) 6/11/93
*/
#include <mach_load.h>
/*
* Memory special file
*/
#include <sys/param.h>
#include <sys/dir.h>
#include <sys/proc.h>
#include <sys/buf.h>
#include <sys/systm.h>
#include <sys/vm.h>
#include <sys/uio.h>
#include <vm/vm_map.h>
#include <vm/vm_kern.h>
#include <mach/vm_param.h>
extern int loadpt;
mmread(dev, uio)
dev_t dev;
struct uio *uio;
{
return (mmrw(dev, uio, UIO_READ));
}
mmwrite(dev, uio)
dev_t dev;
struct uio *uio;
{
return (mmrw(dev, uio, UIO_WRITE));
}
mmrw(dev, uio, rw)
dev_t dev;
struct uio *uio;
enum uio_rw rw;
{
register int o;
register u_int c, v;
register struct iovec *iov;
int error = 0;
vm_offset_t where;
int spl;
while (uio->uio_resid > 0 && error == 0) {
iov = uio->uio_iov;
if (iov->iov_len == 0) {
uio->uio_iov++;
uio->uio_iovcnt--;
if (uio->uio_iovcnt < 0)
panic("mmrw");
continue;
}
switch (minor(dev)) {
/* minor device 0 is physical memory */
case 0:
#if MACH_LOAD
v = trunc_page(uio->uio_offset + loadpt);
#else MACH_LOAD
v = trunc_page(uio->uio_offset);
#endif MACH_LOAD
if (uio->uio_offset >= mem_size)
goto fault;
spl = splvm();
where = vm_map_min(kernel_map);
if (vm_map_find(kernel_map, VM_OBJECT_NULL,
(vm_offset_t) 0, &where, PAGE_SIZE, TRUE) != KERN_SUCCESS) {
splx(spl);
goto fault;
}
pmap_enter(vm_map_pmap(kernel_map), where, v,
VM_PROT_READ|VM_PROT_WRITE, TRUE);
#if MACH_LOAD
o = (uio->uio_offset + loadpt) - v;
#else MACH_LOAD
o = uio->uio_offset - v;
#endif MACH_LOAD
c = min(PAGE_SIZE - o, (u_int)iov->iov_len);
error = uiomove((caddr_t) (where + o), c, uio);
vm_map_remove(kernel_map, where, where + PAGE_SIZE);
splx(spl);
continue;
/* minor device 1 is kernel memory */
case 1:
/* Do some sanity checking */
if (((caddr_t)uio->uio_offset >= VM_MAX_KERNEL_ADDRESS) ||
((caddr_t)uio->uio_offset <= VM_MIN_KERNEL_ADDRESS))
goto fault;
c = iov->iov_len;
error = uiomove((caddr_t)uio->uio_offset, (int)c, uio);
continue;
/* minor device 2 is EOF/RATHOLE */
case 2:
if (rw == UIO_READ)
return (0);
c = iov->iov_len;
break;
default:
goto fault;
break;
}
if (error)
break;
iov->iov_base += c;
iov->iov_len -= c;
uio->uio_offset += c;
uio->uio_resid -= c;
}
return (error);
fault:
return (EFAULT);
}