+/*
+ * Copyright (c) 2004-2019 Apple Inc. All rights reserved.
+ *
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
+ *
+ * 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 2.0 (the 'License'). You may not use this file except in
+ * compliance with the License. The rights granted to you under the License
+ * may not be used to create, or enable the creation or redistribution of,
+ * unlawful or unlicensed copies of an Apple operating system, or to
+ * circumvent, violate, or enable the circumvention or violation of, any
+ * terms of an Apple operating system software license agreement.
+ *
+ * Please obtain a copy of the License at
+ * http://www.opensource.apple.com/apsl/ 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, QUIET ENJOYMENT OR NON-INFRINGEMENT.
+ * Please see the License for the specific language governing rights and
+ * limitations under the License.
+ *
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
+ */
/*
* Copyright (c) 1988 University of Utah.
* Copyright (c) 1990, 1993
/*
* RAM disk driver.
*
- * Block interface to a ramdisk.
+ * Block interface to a ramdisk.
*
*/
-
#include <sys/param.h>
-#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/mount.h>
#include <sys/namei.h>
#include <sys/buf.h>
#include <sys/malloc.h>
#include <sys/mount.h>
-#include <sys/vnode.h>
#include <sys/fcntl.h>
#include <sys/conf.h>
#include <sys/disk.h>
#include <sys/stat.h>
-
#include <sys/vm.h>
+#include <sys/uio_internal.h>
+#include <libkern/libkern.h>
+#include <vm/pmap.h>
#include <vm/vm_pager.h>
-#include <vm/vm_pageout.h>
#include <mach/memory_object_types.h>
+#include <kern/debug.h>
#include <miscfs/devfs/devfs.h>
-static open_close_fcn_t mdevopen;
-static open_close_fcn_t mdevclose;
-static psize_fcn_t mdevsize;
-static strategy_fcn_t mdevstrategy;
-static int mdevbioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p);
-static int mdevcioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p);
-static int mdevrw(dev_t dev, struct uio *uio, int ioflag);
-static char *nonspace(char *pos, char *end);
-static char *getspace(char *pos, char *end);
-static char *cvtnum(char *pos, char *end, unsigned int *num);
+
+void mdevinit(int the_cnt);
+
+static open_close_fcn_t mdevopen;
+static open_close_fcn_t mdevclose;
+static psize_fcn_t mdevsize;
+static strategy_fcn_t mdevstrategy;
+static int mdevbioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p);
+static int mdevcioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p);
+static int mdevrw(dev_t dev, struct uio *uio, int ioflag);
+
+#ifdef CONFIG_MEMDEV_INSECURE
+static char * nonspace(char *pos, char *end);
+static char * getspace(char *pos, char *end);
+static char * cvtnum(char *pos, char *end, uint64_t *num);
+#endif /* CONFIG_MEMDEV_INSECURE */
+
+extern void bcopy_phys(addr64_t from, addr64_t to, vm_size_t bytes);
+extern void mapping_set_mod(ppnum_t pn);
+extern ppnum_t pmap_find_phys(pmap_t pmap, addr64_t va);
+
+/*
+ * Maximal number of memory devices.
+ */
+#define NB_MAX_MDEVICES (16)
/*
* cdevsw
* D_CANFREE We support B_FREEBUF
*/
-static struct bdevsw mdevbdevsw = {
- /* open */ mdevopen,
- /* close */ mdevclose,
- /* strategy */ mdevstrategy,
- /* ioctl */ mdevbioctl,
- /* dump */ eno_dump,
- /* psize */ mdevsize,
- /* flags */ D_DISK,
+static const struct bdevsw mdevbdevsw = {
+ .d_open = mdevopen,
+ .d_close = mdevclose,
+ .d_strategy = mdevstrategy,
+ .d_ioctl = mdevbioctl,
+ .d_dump = eno_dump,
+ .d_psize = mdevsize,
+ .d_type = D_DISK,
};
-static struct cdevsw mdevcdevsw = {
- /* open */ mdevopen,
- /* close */ mdevclose,
- /* read */ mdevrw,
- /* write */ mdevrw,
- /* ioctl */ mdevcioctl,
- /* stop */ eno_stop,
- /* reset */ eno_reset,
- /* ttys */ 0,
- /* select */ eno_select,
- /* mmap */ eno_mmap,
- /* strategy */ eno_strat,
- /* getc */ eno_getc,
- /* putc */ eno_putc,
- /* flags */ D_DISK,
+static const struct cdevsw mdevcdevsw = {
+ .d_open = mdevopen,
+ .d_close = mdevclose,
+ .d_read = mdevrw,
+ .d_write = mdevrw,
+ .d_ioctl = mdevcioctl,
+ .d_stop = eno_stop,
+ .d_reset = eno_reset,
+ .d_ttys = NULL,
+ .d_select = eno_select,
+ .d_mmap = eno_mmap,
+ .d_strategy = eno_strat,
+ .d_reserved_1 = eno_getc,
+ .d_reserved_2 = eno_putc,
+ .d_type = D_DISK,
};
struct mdev {
- vm_offset_t mdBase; /* file size in bytes */
- uint32_t mdSize; /* file size in bytes */
- int mdFlags; /* flags */
- int mdSecsize; /* sector size */
- int mdBDev; /* Block device number */
- int mdCDev; /* Character device number */
- void * mdbdevb;
- void * mdcdevb;
-} mdev[16];
+ uint64_t mdBase; /* file size in bytes */
+ uint32_t mdSize; /* file size in bytes */
+ int mdFlags; /* flags */
+ int mdSecsize; /* sector size */
+ int mdBDev; /* Block device number */
+ int mdCDev; /* Character device number */
+ void * mdbdevb;
+ void * mdcdevb;
+} mdev[NB_MAX_MDEVICES];
/* mdFlags */
-#define mdInited 0x01 /* This device defined */
-#define mdRO 0x02 /* This device is read-only */
-#define mdPhys 0x04 /* This device is in physical memory */
+#define mdInited 0x01 /* This device defined */
+#define mdRO 0x02 /* This device is read-only */
+#define mdPhys 0x04 /* This device is in physical memory */
int mdevBMajor = -1;
int mdevCMajor = -1;
-static int mdevioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p, int is_char);
-dev_t mdevadd(int devid, ppnum_t base, unsigned int size, int phys);
-dev_t mdevlookup(int devid);
-
-static int mdevclose(dev_t dev, int flags, int devtype, struct proc *p) {
- return (0);
+static int mdevioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p, int is_char);
+dev_t mdevadd(int devid, uint64_t base, unsigned int size, int phys);
+dev_t mdevlookup(int devid);
+void mdevremoveall(void);
+int mdevgetrange(int devid, uint64_t *base, uint64_t *size);
+
+static int
+mdevclose(__unused dev_t dev, __unused int flags,
+ __unused int devtype, __unused struct proc *p)
+{
+ return 0;
}
-static int mdevopen(dev_t dev, int flags, int devtype, struct proc *p) {
-
+static int
+mdevopen(dev_t dev, int flags, __unused int devtype, __unused struct proc *p)
+{
int devid;
- devid = minor(dev); /* Get minor device number */
-
- if (devid > 16) return (ENXIO); /* Not valid */
-
- if ((flags & FWRITE) && (mdev[devid].mdFlags & mdRO)) return (EACCES); /* Currently mounted RO */
+ devid = minor(dev); /* Get minor device number */
- return(0);
+ if (devid >= NB_MAX_MDEVICES) {
+ return ENXIO; /* Not valid */
+ }
+ if ((flags & FWRITE) && (mdev[devid].mdFlags & mdRO)) {
+ return EACCES; /* Currently mounted RO */
+ }
+ return 0;
}
-static int mdevrw(dev_t dev, struct uio *uio, int ioflag) {
- int status;
- int unit;
- addr64_t mdata;
- int devid;
- enum uio_seg saveflag;
+static int
+mdevrw(dev_t dev, struct uio *uio, __unused int ioflag)
+{
+ int status;
+ addr64_t mdata;
+ int devid;
+ enum uio_seg saveflag;
- devid = minor(dev); /* Get minor device number */
+ devid = minor(dev); /* Get minor device number */
- if (devid > 16) return (ENXIO); /* Not valid */
- if (!(mdev[devid].mdFlags & mdInited)) return (ENXIO); /* Have we actually been defined yet? */
+ if (devid >= NB_MAX_MDEVICES) {
+ return ENXIO; /* Not valid */
+ }
+ if (!(mdev[devid].mdFlags & mdInited)) {
+ return ENXIO; /* Have we actually been defined yet? */
+ }
+ mdata = ((addr64_t)mdev[devid].mdBase << 12) + uio->uio_offset; /* Point to the area in "file" */
- mdata = ((addr64_t)mdev[devid].mdBase << 12) + uio->uio_offset; /* Point to the area in "file" */
-
- saveflag = uio->uio_segflg; /* Remember what the request is */
- if (mdev[devid].mdFlags & mdPhys) uio->uio_segflg = UIO_PHYS_USERSPACE; /* Make sure we are moving from physical ram if physical device */
- status = uiomove64(mdata, uio->uio_resid, uio); /* Move the data */
- uio->uio_segflg = saveflag; /* Restore the flag */
+ saveflag = uio->uio_segflg; /* Remember what the request is */
+#if LP64_DEBUG
+ if (UIO_IS_USER_SPACE(uio) == 0 && UIO_IS_SYS_SPACE(uio) == 0) {
+ panic("mdevrw - invalid uio_segflg\n");
+ }
+#endif /* LP64_DEBUG */
+ /* Make sure we are moving from physical ram if physical device */
+ if (mdev[devid].mdFlags & mdPhys) {
+ if (uio->uio_segflg == UIO_USERSPACE64) {
+ uio->uio_segflg = UIO_PHYS_USERSPACE64;
+ } else if (uio->uio_segflg == UIO_USERSPACE32) {
+ uio->uio_segflg = UIO_PHYS_USERSPACE32;
+ } else {
+ uio->uio_segflg = UIO_PHYS_USERSPACE;
+ }
+ }
+ status = uiomove64(mdata, (int)uio_resid(uio), uio); /* Move the data */
+ uio->uio_segflg = saveflag; /* Restore the flag */
- return (status);
+ return status;
}
-static void mdevstrategy(struct buf *bp) {
- int unmap;
- unsigned int sz, left, lop, csize;
- kern_return_t ret;
+static void
+mdevstrategy(struct buf *bp)
+{
+ unsigned int left, lop, csize;
vm_offset_t vaddr, blkoff;
- struct buf *tbuf;
int devid;
addr64_t paddr, fvaddr;
ppnum_t pp;
- devid = minor(bp->b_dev); /* Get minor device number */
+ devid = minor(buf_device(bp)); /* Get minor device number */
- if ((mdev[devid].mdFlags & mdInited) == 0) { /* Have we actually been defined yet? */
- bp->b_error = ENXIO;
- bp->b_flags |= B_ERROR;
- biodone(bp);
+ if ((mdev[devid].mdFlags & mdInited) == 0) { /* Have we actually been defined yet? */
+ buf_seterror(bp, ENXIO);
+ buf_biodone(bp);
return;
}
- bp->b_resid = bp->b_bcount; /* Set byte count */
-
- blkoff = bp->b_blkno * mdev[devid].mdSecsize; /* Get offset into file */
+ buf_setresid(bp, buf_count(bp)); /* Set byte count */
+
+ blkoff = buf_blkno(bp) * mdev[devid].mdSecsize; /* Get offset into file */
/*
* Note that reading past end is an error, but reading at end is an EOF. For these
- * we just return with b_resid == b_bcount.
+ * we just return with resid == count.
*/
- if (blkoff >= (mdev[devid].mdSize << 12)) { /* Are they trying to read/write at/after end? */
- if(blkoff != (mdev[devid].mdSize << 12)) { /* Are we trying to read after EOF? */
- bp->b_error = EINVAL; /* Yeah, this is an error */
- bp->b_flags |= B_ERROR | B_INVAL;
+ if (blkoff >= (mdev[devid].mdSize << 12)) { /* Are they trying to read/write at/after end? */
+ if (blkoff != (mdev[devid].mdSize << 12)) { /* Are we trying to read after EOF? */
+ buf_seterror(bp, EINVAL); /* Yeah, this is an error */
}
- biodone(bp); /* Return */
+ buf_biodone(bp); /* Return */
return;
}
- if ((blkoff + bp->b_bcount) > (mdev[devid].mdSize << 12)) { /* Will this read go past end? */
- bp->b_bcount = ((mdev[devid].mdSize << 12) - blkoff); /* Yes, trim to max */
+ if ((blkoff + buf_count(bp)) > (mdev[devid].mdSize << 12)) { /* Will this read go past end? */
+ buf_setcount(bp, (uint32_t)((mdev[devid].mdSize << 12) - blkoff)); /* Yes, trim to max */
+ }
+ /*
+ * make sure the buffer's data area is
+ * accessible
+ */
+ if (buf_map(bp, (caddr_t *)&vaddr)) {
+ panic("ramstrategy: buf_map failed\n");
}
- vaddr = 0; /* Assume not mapped yet */
- unmap = 0;
+ fvaddr = (mdev[devid].mdBase << 12) + blkoff; /* Point to offset into ram disk */
- if (bp->b_flags & B_VECTORLIST) { /* Do we have a list of UPLs? */
- tbuf = (struct buf *)bp->b_real_bp; /* Get this for C's inadequacies */
- if((bp->b_flags & B_NEED_IODONE) && /* If we have a UPL, is it already mapped? */
- tbuf &&
- tbuf->b_data) {
- vaddr = (vm_offset_t)tbuf->b_data; /* We already have this mapped in, get base address */
- }
- else { /* Not mapped yet */
- ret = ubc_upl_map(bp->b_pagelist, &vaddr); /* Map it in */
- if(ret != KERN_SUCCESS) panic("ramstrategy: ubc_upl_map failed, rc = %08X\n", ret);
- unmap = 1; /* Remember to unmap later */
- }
- vaddr = vaddr += bp->b_uploffset; /* Calculate actual vaddr */
- }
- else vaddr = (vm_offset_t)bp->b_data; /* No UPL, we already have address */
-
- fvaddr = (mdev[devid].mdBase << 12) + blkoff; /* Point to offset into ram disk */
-
- if(bp->b_flags & B_READ) { /* Is this a read? */
- if(!(mdev[devid].mdFlags & mdPhys)) { /* Physical mapped disk? */
+ if (buf_flags(bp) & B_READ) { /* Is this a read? */
+ if (!(mdev[devid].mdFlags & mdPhys)) { /* Physical mapped disk? */
bcopy((void *)((uintptr_t)fvaddr),
- (void *)vaddr, (size_t)bp->b_bcount); /* This is virtual, just get the data */
- }
- else {
- left = bp->b_bcount; /* Init the amount left to copy */
- while(left) { /* Go until it is all copied */
-
- lop = min((4096 - (vaddr & 4095)), (4096 - (fvaddr & 4095))); /* Get smallest amount left on sink and source */
- csize = min(lop, left); /* Don't move more than we need to */
-
- pp = pmap_find_phys(kernel_pmap, (addr64_t)((unsigned int)vaddr)); /* Get the sink physical address */
- if(!pp) { /* Not found, what gives? */
- panic("mdevstrategy: sink address %016llX not mapped\n", (addr64_t)((unsigned int)vaddr));
+ (void *)vaddr, (size_t)buf_count(bp)); /* This is virtual, just get the data */
+ } else {
+ left = buf_count(bp); /* Init the amount left to copy */
+ while (left) { /* Go until it is all copied */
+ lop = min((4096 - (vaddr & 4095)), (4096 - (fvaddr & 4095))); /* Get smallest amount left on sink and source */
+ csize = min(lop, left); /* Don't move more than we need to */
+
+ pp = pmap_find_phys(kernel_pmap, (addr64_t)((uintptr_t)vaddr)); /* Get the sink physical address */
+ if (!pp) { /* Not found, what gives? */
+ panic("mdevstrategy: sink address %016llX not mapped\n", (addr64_t)((uintptr_t)vaddr));
}
- paddr = (addr64_t)(((addr64_t)pp << 12) | (addr64_t)(vaddr & 4095)); /* Get actual address */
- bcopy_phys(fvaddr, paddr, csize); /* Copy this on in */
- mapping_set_mod(paddr >> 12); /* Make sure we know that it is modified */
-
- left = left - csize; /* Calculate what is left */
- vaddr = vaddr + csize; /* Move to next sink address */
- fvaddr = fvaddr + csize; /* Bump to next physical address */
+ paddr = (addr64_t)(((addr64_t)pp << 12) | (addr64_t)(vaddr & 4095)); /* Get actual address */
+ bcopy_phys(fvaddr, paddr, csize); /* Copy this on in */
+ mapping_set_mod((ppnum_t)(paddr >> 12)); /* Make sure we know that it is modified */
+
+ left = left - csize; /* Calculate what is left */
+ vaddr = vaddr + csize; /* Move to next sink address */
+ fvaddr = fvaddr + csize; /* Bump to next physical address */
}
}
- }
- else { /* This is a write */
- if(!(mdev[devid].mdFlags & mdPhys)) { /* Physical mapped disk? */
+ } else { /* This is a write */
+ if (!(mdev[devid].mdFlags & mdPhys)) { /* Physical mapped disk? */
bcopy((void *)vaddr, (void *)((uintptr_t)fvaddr),
- (size_t)bp->b_bcount); /* This is virtual, just put the data */
- }
- else {
- left = bp->b_bcount; /* Init the amount left to copy */
- while(left) { /* Go until it is all copied */
-
- lop = min((4096 - (vaddr & 4095)), (4096 - (fvaddr & 4095))); /* Get smallest amount left on sink and source */
- csize = min(lop, left); /* Don't move more than we need to */
-
- pp = pmap_find_phys(kernel_pmap, (addr64_t)((unsigned int)vaddr)); /* Get the source physical address */
- if(!pp) { /* Not found, what gives? */
- panic("mdevstrategy: source address %016llX not mapped\n", (addr64_t)((unsigned int)vaddr));
+ (size_t)buf_count(bp)); /* This is virtual, just put the data */
+ } else {
+ left = buf_count(bp); /* Init the amount left to copy */
+ while (left) { /* Go until it is all copied */
+ lop = min((4096 - (vaddr & 4095)), (4096 - (fvaddr & 4095))); /* Get smallest amount left on sink and source */
+ csize = min(lop, left); /* Don't move more than we need to */
+
+ pp = pmap_find_phys(kernel_pmap, (addr64_t)((uintptr_t)vaddr)); /* Get the source physical address */
+ if (!pp) { /* Not found, what gives? */
+ panic("mdevstrategy: source address %016llX not mapped\n", (addr64_t)((uintptr_t)vaddr));
}
- paddr = (addr64_t)(((addr64_t)pp << 12) | (addr64_t)(vaddr & 4095)); /* Get actual address */
-
- bcopy_phys(paddr, fvaddr, csize); /* Move this on out */
-
- left = left - csize; /* Calculate what is left */
- vaddr = vaddr + csize; /* Move to next sink address */
- fvaddr = fvaddr + csize; /* Bump to next physical address */
+ paddr = (addr64_t)(((addr64_t)pp << 12) | (addr64_t)(vaddr & 4095)); /* Get actual address */
+
+ bcopy_phys(paddr, fvaddr, csize); /* Move this on out */
+
+ left = left - csize; /* Calculate what is left */
+ vaddr = vaddr + csize; /* Move to next sink address */
+ fvaddr = fvaddr + csize; /* Bump to next physical address */
}
}
}
-
- if (unmap) { /* Do we need to unmap this? */
- ubc_upl_unmap(bp->b_pagelist); /* Yes, unmap it */
- }
-
- bp->b_resid = 0; /* Nothing more to do */
- biodone(bp); /* Say we've finished */
+ /*
+ * buf_unmap takes care of all the cases
+ * it will unmap the buffer from kernel
+ * virtual space if that was the state
+ * when we mapped it.
+ */
+ buf_unmap(bp);
+
+ buf_setresid(bp, 0); /* Nothing more to do */
+ buf_biodone(bp); /* Say we've finished */
}
-static int mdevbioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p) {
- return (mdevioctl(dev, cmd, data, flag, p, 0));
+static int
+mdevbioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
+{
+ return mdevioctl(dev, cmd, data, flag, p, 0);
}
-static int mdevcioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p) {
- return (mdevioctl(dev, cmd, data, flag, p, 1));
+static int
+mdevcioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
+{
+ return mdevioctl(dev, cmd, data, flag, p, 1);
}
-static int mdevioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p, int is_char) {
-
+static int
+mdevioctl(dev_t dev, u_long cmd, caddr_t data, __unused int flag,
+ struct proc *p, int is_char)
+{
int error;
- u_long *f;
+ u_int32_t *f;
u_int64_t *o;
int devid;
+ dk_memdev_info_t * memdev_info;
- devid = minor(dev); /* Get minor device number */
-
- if (devid > 16) return (ENXIO); /* Not valid */
+ devid = minor(dev); /* Get minor device number */
- error = suser(p->p_ucred, &p->p_acflag); /* Are we superman? */
- if (error) return (error); /* Nope... */
-
- f = (u_long*)data;
+ if (devid >= NB_MAX_MDEVICES) {
+ return ENXIO; /* Not valid */
+ }
+ error = proc_suser(p); /* Are we superman? */
+ if (error) {
+ return error; /* Nope... */
+ }
+ f = (u_int32_t*)data;
o = (u_int64_t *)data;
+ memdev_info = (dk_memdev_info_t *) data;
switch (cmd) {
+ case DKIOCGETMAXBLOCKCOUNTREAD:
+ *o = 32;
+ break;
- case DKIOCGETMAXBLOCKCOUNTREAD:
- *o = 32;
- break;
-
- case DKIOCGETMAXBLOCKCOUNTWRITE:
- *o = 32;
- break;
-
- case DKIOCGETMAXSEGMENTCOUNTREAD:
- *o = 32;
- break;
-
- case DKIOCGETMAXSEGMENTCOUNTWRITE:
- *o = 32;
- break;
-
- case DKIOCGETBLOCKSIZE:
- *f = mdev[devid].mdSecsize;
- break;
-
- case DKIOCSETBLOCKSIZE:
- if (is_char) return (ENODEV); /* We can only do this for a block */
-
- if (*f < DEV_BSIZE) return (EINVAL); /* Too short? */
-
- mdev[devid].mdSecsize = *f; /* set the new block size */
- break;
-
- case DKIOCISWRITABLE:
- *f = 1;
- break;
-
- case DKIOCGETBLOCKCOUNT32:
- if(!(mdev[devid].mdFlags & mdInited)) return (ENXIO);
- *f = ((mdev[devid].mdSize << 12) + mdev[devid].mdSecsize - 1) / mdev[devid].mdSecsize;
- break;
-
- case DKIOCGETBLOCKCOUNT:
- if(!(mdev[devid].mdFlags & mdInited)) return (ENXIO);
- *o = ((mdev[devid].mdSize << 12) + mdev[devid].mdSecsize - 1) / mdev[devid].mdSecsize;
- break;
-
- default:
- error = ENOTTY;
- break;
- }
- return(error);
-}
+ case DKIOCGETMAXBLOCKCOUNTWRITE:
+ *o = 32;
+ break;
+ case DKIOCGETMAXSEGMENTCOUNTREAD:
+ *o = 32;
+ break;
-static int mdevsize(dev_t dev) {
+ case DKIOCGETMAXSEGMENTCOUNTWRITE:
+ *o = 32;
+ break;
- int devid;
+ case DKIOCGETBLOCKSIZE:
+ *f = mdev[devid].mdSecsize;
+ break;
- devid = minor(dev); /* Get minor device number */
- if (devid > 16) return (ENXIO); /* Not valid */
+ case DKIOCSETBLOCKSIZE:
+ if (is_char) {
+ return ENODEV; /* We can only do this for a block */
+ }
+ if (*f < DEV_BSIZE) {
+ return EINVAL; /* Too short? */
+ }
+ mdev[devid].mdSecsize = *f; /* set the new block size */
+ break;
- if ((mdev[devid].mdFlags & mdInited) == 0) return(-1); /* Not inited yet */
+ case DKIOCISWRITABLE:
+ *f = 1;
+ break;
- return(mdev[devid].mdSecsize);
+ case DKIOCGETBLOCKCOUNT:
+ if (!(mdev[devid].mdFlags & mdInited)) {
+ return ENXIO;
+ }
+ *o = ((mdev[devid].mdSize << 12) + mdev[devid].mdSecsize - 1) / mdev[devid].mdSecsize;
+ break;
+
+ /*
+ * We're interested in the following bits of information:
+ * Are you a memory-backed device (always yes, in this case)?
+ * Physical memory (mdPhys)?
+ * What is your base page?
+ * What is your size?
+ */
+ case DKIOCGETMEMDEVINFO:
+ if (!(mdev[devid].mdFlags & mdInited)) {
+ return ENXIO;
+ }
+ memdev_info->mi_mdev = TRUE;
+ memdev_info->mi_phys = (mdev[devid].mdFlags & mdPhys) ? TRUE : FALSE;
+ memdev_info->mi_base = (uint32_t)mdev[devid].mdBase;
+ memdev_info->mi_size = mdev[devid].mdSize;
+ break;
+
+ default:
+ error = ENOTTY;
+ break;
+ }
+ return error;
+}
+
+
+static int
+mdevsize(dev_t dev)
+{
+ int devid;
+
+ devid = minor(dev); /* Get minor device number */
+ if (devid >= NB_MAX_MDEVICES) {
+ return ENXIO; /* Not valid */
+ }
+ if ((mdev[devid].mdFlags & mdInited) == 0) {
+ return -1; /* Not inited yet */
+ }
+ return mdev[devid].mdSecsize;
}
#include <pexpert/pexpert.h>
-void mdevinit(int cnt) {
+void
+mdevinit(__unused int the_cnt)
+{
+#ifdef CONFIG_MEMDEV_INSECURE
int devid, phys;
- ppnum_t base;
- unsigned int size;
+ uint64_t base;
+ uint64_t size;
char *ba, *lp;
dev_t dev;
-
-
- ba = PE_boot_args(); /* Get the boot arguments */
- lp = ba + 256; /* Point to the end */
-
- while(1) { /* Step through, looking for our keywords */
- phys = 0; /* Assume virtual memory device */
- ba = nonspace(ba, lp); /* Find non-space */
- if(ba >= lp) return; /* We are done if no more... */
- if(((ba[0] != 'v') && (ba[0] != 'p'))
- || (ba[1] != 'm') || (ba[2] != 'd') || (ba[4] != '=')
- || (ba[3] < '0') || (ba[3] > 'f')
- || ((ba[3] > '9') && (ba[3] < 'a'))) { /* Is this of form "vmdx=" or "pmdx=" where x is hex digit? */
-
- ba = getspace(ba, lp); /* Find next white space or end */
- continue; /* Start looking for the next one */
- }
-
- if(ba[0] == 'p') phys = 1; /* Set physical memory disk */
-
- devid = ba[3] & 0xF; /* Assume digit */
- if(ba[3] > '9') devid += 9; /* Adjust for hex digits */
-
- ba = &ba[5]; /* Step past keyword */
- ba = cvtnum(ba, lp, &base); /* Convert base of memory disk */
- if(ba >= lp) return; /* Malformed one at the end, leave */
- if(ba[0] != '.') continue; /* If not length separater, try next... */
- if(base & 0xFFF) continue; /* Only allow page aligned stuff */
-
- ba++; /* Step past '.' */
- ba = cvtnum(ba, lp, &size); /* Try to convert it */
- if(!size || (size & 0xFFF)) continue; /* Allow only non-zer page size multiples */
- if(ba < lp) { /* If we are not at end, check end character */
- if((ba[0] != ' ') && (ba[0] != 0)) continue; /* End must be null or space */
- }
-
- dev = mdevadd(devid, base >> 12, size >> 12, phys); /* Go add the device */
- }
- return;
-}
+ ba = PE_boot_args(); /* Get the boot arguments */
+ lp = ba + 256; /* Point to the end */
-char *nonspace(char *pos, char *end) { /* Find next non-space in string */
+ while (1) { /* Step through, looking for our keywords */
+ phys = 0; /* Assume virtual memory device */
+ ba = nonspace(ba, lp); /* Find non-space */
+ if (ba >= lp) {
+ return; /* We are done if no more... */
+ }
+ if (((ba[0] != 'v') && (ba[0] != 'p'))
+ || (ba[1] != 'm') || (ba[2] != 'd') || (ba[4] != '=')
+ || (ba[3] < '0') || (ba[3] > 'f')
+ || ((ba[3] > '9') && (ba[3] < 'a'))) { /* Is this of form "vmdx=" or "pmdx=" where x is hex digit? */
+ ba = getspace(ba, lp); /* Find next white space or end */
+ continue; /* Start looking for the next one */
+ }
- if(pos >= end) return end; /* Don't go past end */
- if(pos[0] == 0) return end; /* If at null, make end */
-
- while(1) { /* Keep going */
- if(pos[0] != ' ') return pos; /* Leave if we found one */
- pos++; /* Stop */
- if(pos >= end) return end; /* Quit if we run off end */
+ if (ba[0] == 'p') {
+ phys = 1; /* Set physical memory disk */
+ }
+ devid = ba[3] & 0xF; /* Assume digit */
+ if (ba[3] > '9') {
+ devid += 9; /* Adjust for hex digits */
+ }
+ ba = &ba[5]; /* Step past keyword */
+ ba = cvtnum(ba, lp, &base); /* Convert base of memory disk */
+ if (ba >= lp) {
+ return; /* Malformed one at the end, leave */
+ }
+ if (ba[0] != '.') {
+ continue; /* If not length separater, try next... */
+ }
+ if (base & 0xFFF) {
+ continue; /* Only allow page aligned stuff */
+ }
+ ba++; /* Step past '.' */
+ ba = cvtnum(ba, lp, &size); /* Try to convert it */
+ if (!size || (size & 0xFFF)) {
+ continue; /* Allow only non-zer page size multiples */
+ }
+ if (ba < lp) { /* If we are not at end, check end character */
+ if ((ba[0] != ' ') && (ba[0] != 0)) {
+ continue; /* End must be null or space */
+ }
+ }
+
+ dev = mdevadd(devid, base >> 12, (unsigned)size >> 12, phys); /* Go add the device */
}
+
+#endif /* CONFIG_MEMDEV_INSECURE */
+
+ return;
}
-char *getspace(char *pos, char *end) { /* Find next non-space in string */
+#ifdef CONFIG_MEMDEV_INSECURE
- while(1) { /* Keep going */
- if(pos >= end) return end; /* Don't go past end */
- if(pos[0] == 0) return end; /* Leave if we hit null */
- if(pos[0] == ' ') return pos; /* Leave if we found one */
- pos++; /* Stop */
+char *
+nonspace(char *pos, char *end) /* Find next non-space in string */
+{
+ if (pos >= end) {
+ return end; /* Don't go past end */
+ }
+ if (pos[0] == 0) {
+ return end; /* If at null, make end */
+ }
+ while (1) { /* Keep going */
+ if (pos[0] != ' ') {
+ return pos; /* Leave if we found one */
+ }
+ pos++; /* Stop */
+ if (pos >= end) {
+ return end; /* Quit if we run off end */
+ }
}
}
-char *cvtnum(char *pos, char *end, unsigned int *num) { /* Convert to a number */
+char *
+getspace(char *pos, char *end) /* Find next non-space in string */
+{
+ while (1) { /* Keep going */
+ if (pos >= end) {
+ return end; /* Don't go past end */
+ }
+ if (pos[0] == 0) {
+ return end; /* Leave if we hit null */
+ }
+ if (pos[0] == ' ') {
+ return pos; /* Leave if we found one */
+ }
+ pos++; /* Stop */
+ }
+}
+char *
+cvtnum(char *pos, char *end, uint64_t *num) /* Convert to a number */
+{
int rad, dig;
-
- *num = 0; /* Set answer to 0 to start */
+
+ *num = 0; /* Set answer to 0 to start */
rad = 10;
- if(pos >= end) return end; /* Don't go past end */
- if(pos[0] == 0) return end; /* If at null, make end */
-
- if(pos[0] == '0' && ((pos[1] == 'x') || (pos[1] == 'x'))) { /* A hex constant? */
+ if (pos >= end) {
+ return end; /* Don't go past end */
+ }
+ if (pos[0] == 0) {
+ return end; /* If at null, make end */
+ }
+ if (pos[0] == '0' && ((pos[1] == 'x') || (pos[1] == 'x'))) { /* A hex constant? */
rad = 16;
- pos += 2; /* Point to the number */
- }
-
- while(1) { /* Convert it */
-
- if(pos >= end) return end; /* Don't go past end */
- if(pos[0] == 0) return end; /* If at null, make end */
- if(pos[0] < '0') return pos; /* Leave if non-digit */
- dig = pos[0] & 0xF; /* Extract digit */
- if(pos[0] > '9') { /* Is it bigger than 9? */
- if(rad == 10) return pos; /* Leave if not base 10 */
- if(!(((pos[0] >= 'A') && (pos[0] <= 'F'))
- || ((pos[0] >= 'a') && (pos[0] <= 'f')))) return pos; /* Leave if bogus char */
- dig = dig + 9; /* Adjust for character */
- }
- *num = (*num * rad) + dig; /* Accumulate the number */
- pos++; /* Step on */
+ pos += 2; /* Point to the number */
+ }
+
+ while (1) { /* Convert it */
+ if (pos >= end) {
+ return end; /* Don't go past end */
+ }
+ if (pos[0] == 0) {
+ return end; /* If at null, make end */
+ }
+ if (pos[0] < '0') {
+ return pos; /* Leave if non-digit */
+ }
+ dig = pos[0] & 0xF; /* Extract digit */
+ if (pos[0] > '9') { /* Is it bigger than 9? */
+ if (rad == 10) {
+ return pos; /* Leave if not base 10 */
+ }
+ if (!(((pos[0] >= 'A') && (pos[0] <= 'F'))
+ || ((pos[0] >= 'a') && (pos[0] <= 'f')))) {
+ return pos; /* Leave if bogus char */
+ }
+ dig = dig + 9; /* Adjust for character */
+ }
+ *num = (*num * rad) + dig; /* Accumulate the number */
+ pos++; /* Step on */
}
}
-dev_t mdevadd(int devid, ppnum_t base, unsigned int size, int phys) {
-
+#endif /* CONFIG_MEMDEV_INSECURE */
+
+dev_t
+mdevadd(int devid, uint64_t base, unsigned int size, int phys)
+{
int i;
-
- if(devid < 0) {
+ if (devid < 0) {
devid = -1;
- for(i = 0; i < 16; i++) { /* Search all known memory devices */
- if(!(mdev[i].mdFlags & mdInited)) { /* Is this a free one? */
- if(devid < 0)devid = i; /* Remember first free one */
- continue; /* Skip check */
+ for (i = 0; i < NB_MAX_MDEVICES; i++) { /* Search all known memory devices */
+ if (!(mdev[i].mdFlags & mdInited)) { /* Is this a free one? */
+ if (devid < 0) {
+ devid = i; /* Remember first free one */
+ }
+ continue; /* Skip check */
}
- if(!(((base + size -1 ) < mdev[i].mdBase) || ((mdev[i].mdBase + mdev[i].mdSize - 1) < base))) { /* Is there any overlap? */
- panic("mdevadd: attempt to add overlapping memory device at %08X-%08X\n", mdev[i].mdBase, mdev[i].mdBase + mdev[i].mdSize - 1);
+ if (!(((base + size - 1) < mdev[i].mdBase) || ((mdev[i].mdBase + mdev[i].mdSize - 1) < base))) { /* Is there any overlap? */
+ panic("mdevadd: attempt to add overlapping memory device at %016llX-%016llX\n", mdev[i].mdBase, mdev[i].mdBase + mdev[i].mdSize - 1);
}
}
- if(devid < 0) { /* Do we have free slots? */
- panic("mdevadd: attempt to add more than 16 memory devices\n");
+ if (devid < 0) { /* Do we have free slots? */
+ panic("mdevadd: attempt to add more than %d memory devices\n", NB_MAX_MDEVICES);
}
- }
- else {
- if(devid >= 16) { /* Giving us something bogus? */
- panic("mdevadd: attempt to explicitly add a bogus memory device: &08X\n", devid);
+ } else {
+ if (devid >= NB_MAX_MDEVICES) { /* Giving us something bogus? */
+ panic("mdevadd: attempt to explicitly add a bogus memory device: %08X\n", devid);
}
- if(mdev[devid].mdFlags &mdInited) { /* Already there? */
- panic("mdevadd: attempt to explicitly add a previously defined memory device: &08X\n", devid);
+ if (mdev[devid].mdFlags & mdInited) { /* Already there? */
+ panic("mdevadd: attempt to explicitly add a previously defined memory device: %08X\n", devid);
}
}
-
- if(mdevBMajor < 0) { /* Have we gotten a major number yet? */
- mdevBMajor = bdevsw_add(-1, &mdevbdevsw); /* Add to the table and figure out a major number */
+
+ if (mdevBMajor < 0) { /* Have we gotten a major number yet? */
+ mdevBMajor = bdevsw_add(-1, &mdevbdevsw); /* Add to the table and figure out a major number */
if (mdevBMajor < 0) {
printf("mdevadd: error - bdevsw_add() returned %d\n", mdevBMajor);
return -1;
}
}
-
- if(mdevCMajor < 0) { /* Have we gotten a major number yet? */
- mdevCMajor = cdevsw_add_with_bdev(-1, &mdevcdevsw, mdevBMajor); /* Add to the table and figure out a major number */
+
+ if (mdevCMajor < 0) { /* Have we gotten a major number yet? */
+ mdevCMajor = cdevsw_add_with_bdev(-1, &mdevcdevsw, mdevBMajor); /* Add to the table and figure out a major number */
if (mdevCMajor < 0) {
printf("ramdevice_init: error - cdevsw_add() returned %d\n", mdevCMajor);
return -1;
}
}
- mdev[devid].mdBDev = makedev(mdevBMajor, devid); /* Get the device number */
- mdev[devid].mdbdevb = devfs_make_node(mdev[devid].mdBDev, DEVFS_BLOCK, /* Make the node */
- UID_ROOT, GID_OPERATOR,
- 0600, "md%d", devid);
- if (mdev[devid].mdbdevb == NULL) { /* Did we make one? */
+ mdev[devid].mdBDev = makedev(mdevBMajor, devid); /* Get the device number */
+ mdev[devid].mdbdevb = devfs_make_node(mdev[devid].mdBDev, DEVFS_BLOCK, /* Make the node */
+ UID_ROOT, GID_OPERATOR,
+ 0600, "md%d", devid);
+ if (mdev[devid].mdbdevb == NULL) { /* Did we make one? */
printf("mdevadd: devfs_make_node for block failed!\n");
- return -1; /* Nope... */
+ return -1; /* Nope... */
}
- mdev[devid].mdCDev = makedev(mdevCMajor, devid); /* Get the device number */
- mdev[devid].mdcdevb = devfs_make_node(mdev[devid].mdCDev, DEVFS_CHAR, /* Make the node */
- UID_ROOT, GID_OPERATOR,
- 0600, "rmd%d", devid);
- if (mdev[devid].mdcdevb == NULL) { /* Did we make one? */
+ mdev[devid].mdCDev = makedev(mdevCMajor, devid); /* Get the device number */
+ mdev[devid].mdcdevb = devfs_make_node(mdev[devid].mdCDev, DEVFS_CHAR, /* Make the node */
+ UID_ROOT, GID_OPERATOR,
+ 0600, "rmd%d", devid);
+ if (mdev[devid].mdcdevb == NULL) { /* Did we make one? */
printf("mdevadd: devfs_make_node for character failed!\n");
- return -1; /* Nope... */
- }
-
- mdev[devid].mdBase = base; /* Set the base address of ram disk */
- mdev[devid].mdSize = size; /* Set the length of the ram disk */
- mdev[devid].mdSecsize = DEV_BSIZE; /* Set starting block size */
- if(phys) mdev[devid].mdFlags |= mdPhys; /* Show that we are in physical memory */
- mdev[devid].mdFlags |= mdInited; /* Show we are all set up */
- printf("Added memory device md%x/rmd%x (%08X/%08X) at %08X for %08X\n",
- devid, devid, mdev[devid].mdBDev, mdev[devid].mdCDev, base << 12, size << 12);
+ return -1; /* Nope... */
+ }
+
+ mdev[devid].mdBase = base; /* Set the base address of ram disk */
+ mdev[devid].mdSize = size; /* Set the length of the ram disk */
+ mdev[devid].mdSecsize = DEV_BSIZE; /* Set starting block size */
+ if (phys) {
+ mdev[devid].mdFlags |= mdPhys; /* Show that we are in physical memory */
+ }
+ mdev[devid].mdFlags |= mdInited; /* Show we are all set up */
+ printf("Added memory device md%x/rmd%x (%08X/%08X) at %016llX for %016llX\n",
+ devid, devid, mdev[devid].mdBDev, mdev[devid].mdCDev, base << 12, (uint64_t)size << 12);
return mdev[devid].mdBDev;
}
-dev_t mdevlookup(int devid) {
-
- if((devid < 0) || (devid > 15)) return -1; /* Filter any bogus requests */
- if(!(mdev[devid].mdFlags & mdInited)) return -1; /* This one hasn't been defined */
- return mdev[devid].mdBDev; /* Return the device number */
+dev_t
+mdevlookup(int devid)
+{
+ if ((devid < 0) || (devid >= NB_MAX_MDEVICES)) {
+ return -1; /* Filter any bogus requests */
+ }
+ if (!(mdev[devid].mdFlags & mdInited)) {
+ return -1; /* This one hasn't been defined */
+ }
+ return mdev[devid].mdBDev; /* Return the device number */
+}
+
+void
+mdevremoveall(void)
+{
+ int i;
+
+ for (i = 0; i < NB_MAX_MDEVICES; i++) {
+ if (!(mdev[i].mdFlags & mdInited)) {
+ continue; /* Ignore unused mdevs */
+ }
+ devfs_remove(mdev[i].mdbdevb); /* Remove the block device */
+ devfs_remove(mdev[i].mdcdevb); /* Remove the character device */
+
+ mdev[i].mdBase = 0; /* Clear the mdev's storage */
+ mdev[i].mdSize = 0;
+ mdev[i].mdSecsize = 0;
+ mdev[i].mdFlags = 0;
+ mdev[i].mdBDev = 0;
+ mdev[i].mdCDev = 0;
+ mdev[i].mdbdevb = 0;
+ mdev[i].mdcdevb = 0;
+ }
+}
+
+int
+mdevgetrange(int devid, uint64_t *base, uint64_t *size)
+{
+ assert(base);
+ assert(size);
+
+ /* filter invalid request */
+ if ((devid < 0) || (devid >= NB_MAX_MDEVICES)) {
+ return -1;
+ }
+
+ /* filter non-initialized memory devices */
+ if ((mdev[devid].mdFlags & mdInited) == 0) {
+ return -1;
+ }
+
+ *base = mdev[devid].mdBase << 12;
+ *size = mdev[devid].mdSize << 12;
+
+ /* make sure (base, size) is a valid range and will not overflow */
+ assert(*size < (UINT64_MAX - *base));
+
+ return 0;
}