#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/mount.h>
#include <sys/namei.h>
#include <sys/proc.h>
#include <sys/buf.h>
#include <sys/malloc.h>
#include <sys/mount.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 <mach/memory_object_types.h>
#include <kern/debug.h>
#include <miscfs/devfs/devfs.h>
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, unsigned int *num);
#endif
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);
static struct bdevsw mdevbdevsw = {
mdevopen,
mdevclose,
mdevstrategy,
mdevbioctl,
eno_dump,
mdevsize,
D_DISK,
};
static struct cdevsw mdevcdevsw = {
mdevopen,
mdevclose,
mdevrw,
mdevrw,
mdevcioctl,
eno_stop,
eno_reset,
NULL,
eno_select,
eno_mmap,
eno_strat,
eno_getc,
eno_putc,
D_DISK,
};
struct mdev {
uint64_t mdBase;
uint32_t mdSize;
int mdFlags;
int mdSecsize;
int mdBDev;
int mdCDev;
void * mdbdevb;
void * mdcdevb;
} mdev[16];
#define mdInited 0x01
#define mdRO 0x02
#define mdPhys 0x04
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);
void mdevremoveall(void);
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, __unused int devtype, __unused struct proc *p) {
int devid;
devid = minor(dev);
if (devid > 16) return (ENXIO);
if ((flags & FWRITE) && (mdev[devid].mdFlags & mdRO)) return (EACCES);
return(0);
}
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);
if (devid > 16) return (ENXIO);
if (!(mdev[devid].mdFlags & mdInited)) return (ENXIO);
mdata = ((addr64_t)mdev[devid].mdBase << 12) + uio->uio_offset;
saveflag = uio->uio_segflg;
#if LP64_DEBUG
if (UIO_IS_USER_SPACE(uio) == 0 && UIO_IS_SYS_SPACE(uio) == 0) {
panic("mdevrw - invalid uio_segflg\n");
}
#endif
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, uio_resid(uio), uio);
uio->uio_segflg = saveflag;
return (status);
}
static void mdevstrategy(struct buf *bp) {
unsigned int left, lop, csize;
vm_offset_t vaddr, blkoff;
int devid;
addr64_t paddr, fvaddr;
ppnum_t pp;
devid = minor(buf_device(bp));
if ((mdev[devid].mdFlags & mdInited) == 0) {
buf_seterror(bp, ENXIO);
buf_biodone(bp);
return;
}
buf_setresid(bp, buf_count(bp));
blkoff = buf_blkno(bp) * mdev[devid].mdSecsize;
if (blkoff >= (mdev[devid].mdSize << 12)) {
if(blkoff != (mdev[devid].mdSize << 12)) {
buf_seterror(bp, EINVAL);
}
buf_biodone(bp);
return;
}
if ((blkoff + buf_count(bp)) > (mdev[devid].mdSize << 12)) {
buf_setcount(bp, ((mdev[devid].mdSize << 12) - blkoff));
}
if (buf_map(bp, (caddr_t *)&vaddr))
panic("ramstrategy: buf_map failed\n");
fvaddr = (mdev[devid].mdBase << 12) + blkoff;
if (buf_flags(bp) & B_READ) {
if(!(mdev[devid].mdFlags & mdPhys)) {
bcopy((void *)((uintptr_t)fvaddr),
(void *)vaddr, (size_t)buf_count(bp));
}
else {
left = buf_count(bp);
while(left) {
lop = min((4096 - (vaddr & 4095)), (4096 - (fvaddr & 4095)));
csize = min(lop, left);
pp = pmap_find_phys(kernel_pmap, (addr64_t)((uintptr_t)vaddr));
if(!pp) {
panic("mdevstrategy: sink address %016llX not mapped\n", (addr64_t)((uintptr_t)vaddr));
}
paddr = (addr64_t)(((addr64_t)pp << 12) | (addr64_t)(vaddr & 4095));
bcopy_phys(fvaddr, paddr, csize);
mapping_set_mod(paddr >> 12);
left = left - csize;
vaddr = vaddr + csize;
fvaddr = fvaddr + csize;
}
}
}
else {
if(!(mdev[devid].mdFlags & mdPhys)) {
bcopy((void *)vaddr, (void *)((uintptr_t)fvaddr),
(size_t)buf_count(bp));
}
else {
left = buf_count(bp);
while(left) {
lop = min((4096 - (vaddr & 4095)), (4096 - (fvaddr & 4095)));
csize = min(lop, left);
pp = pmap_find_phys(kernel_pmap, (addr64_t)((uintptr_t)vaddr));
if(!pp) {
panic("mdevstrategy: source address %016llX not mapped\n", (addr64_t)((uintptr_t)vaddr));
}
paddr = (addr64_t)(((addr64_t)pp << 12) | (addr64_t)(vaddr & 4095));
bcopy_phys(paddr, fvaddr, csize);
left = left - csize;
vaddr = vaddr + csize;
fvaddr = fvaddr + csize;
}
}
}
buf_unmap(bp);
buf_setresid(bp, 0);
buf_biodone(bp);
}
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 mdevioctl(dev_t dev, u_long cmd, caddr_t data, __unused int flag,
struct proc *p, int is_char) {
int error;
u_int32_t *f;
u_int64_t *o;
int devid;
devid = minor(dev);
if (devid > 16) return (ENXIO);
error = proc_suser(p);
if (error) return (error);
f = (u_int32_t*)data;
o = (u_int64_t *)data;
switch (cmd) {
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);
if (*f < DEV_BSIZE) return (EINVAL);
mdev[devid].mdSecsize = *f;
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);
}
static int mdevsize(dev_t dev) {
int devid;
devid = minor(dev);
if (devid > 16) return (ENXIO);
if ((mdev[devid].mdFlags & mdInited) == 0) return(-1);
return(mdev[devid].mdSecsize);
}
#include <pexpert/pexpert.h>
void mdevinit(__unused int the_cnt) {
#ifdef CONFIG_MEMDEV_INSECURE
int devid, phys;
ppnum_t base;
unsigned int size;
char *ba, *lp;
dev_t dev;
ba = PE_boot_args();
lp = ba + 256;
while(1) {
phys = 0;
ba = nonspace(ba, lp);
if(ba >= lp) return;
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'))) {
ba = getspace(ba, lp);
continue;
}
if(ba[0] == 'p') phys = 1;
devid = ba[3] & 0xF;
if(ba[3] > '9') devid += 9;
ba = &ba[5];
ba = cvtnum(ba, lp, &base);
if(ba >= lp) return;
if(ba[0] != '.') continue;
if(base & 0xFFF) continue;
ba++;
ba = cvtnum(ba, lp, &size);
if(!size || (size & 0xFFF)) continue;
if(ba < lp) {
if((ba[0] != ' ') && (ba[0] != 0)) continue;
}
dev = mdevadd(devid, base >> 12, size >> 12, phys);
}
#endif
return;
}
#ifdef CONFIG_MEMDEV_INSECURE
char *nonspace(char *pos, char *end) {
if(pos >= end) return end;
if(pos[0] == 0) return end;
while(1) {
if(pos[0] != ' ') return pos;
pos++;
if(pos >= end) return end;
}
}
char *getspace(char *pos, char *end) {
while(1) {
if(pos >= end) return end;
if(pos[0] == 0) return end;
if(pos[0] == ' ') return pos;
pos++;
}
}
char *cvtnum(char *pos, char *end, unsigned int *num) {
int rad, dig;
*num = 0;
rad = 10;
if(pos >= end) return end;
if(pos[0] == 0) return end;
if(pos[0] == '0' && ((pos[1] == 'x') || (pos[1] == 'x'))) {
rad = 16;
pos += 2;
}
while(1) {
if(pos >= end) return end;
if(pos[0] == 0) return end;
if(pos[0] < '0') return pos;
dig = pos[0] & 0xF;
if(pos[0] > '9') {
if(rad == 10) return pos;
if(!(((pos[0] >= 'A') && (pos[0] <= 'F'))
|| ((pos[0] >= 'a') && (pos[0] <= 'f')))) return pos;
dig = dig + 9;
}
*num = (*num * rad) + dig;
pos++;
}
}
#endif
dev_t mdevadd(int devid, ppnum_t base, unsigned int size, int phys) {
int i;
if(devid < 0) {
devid = -1;
for(i = 0; i < 16; i++) {
if(!(mdev[i].mdFlags & mdInited)) {
if(devid < 0)devid = i;
continue;
}
if(!(((base + size -1 ) < mdev[i].mdBase) || ((mdev[i].mdBase + mdev[i].mdSize - 1) < base))) {
panic("mdevadd: attempt to add overlapping memory device at %08lX-%08lX\n", (long) mdev[i].mdBase, (long) mdev[i].mdBase + mdev[i].mdSize - 1);
}
}
if(devid < 0) {
panic("mdevadd: attempt to add more than 16 memory devices\n");
}
}
else {
if(devid >= 16) {
panic("mdevadd: attempt to explicitly add a bogus memory device: %08X\n", devid);
}
if(mdev[devid].mdFlags &mdInited) {
panic("mdevadd: attempt to explicitly add a previously defined memory device: %08X\n", devid);
}
}
if(mdevBMajor < 0) {
mdevBMajor = bdevsw_add(-1, &mdevbdevsw);
if (mdevBMajor < 0) {
printf("mdevadd: error - bdevsw_add() returned %d\n", mdevBMajor);
return -1;
}
}
if(mdevCMajor < 0) {
mdevCMajor = cdevsw_add_with_bdev(-1, &mdevcdevsw, mdevBMajor);
if (mdevCMajor < 0) {
printf("ramdevice_init: error - cdevsw_add() returned %d\n", mdevCMajor);
return -1;
}
}
mdev[devid].mdBDev = makedev(mdevBMajor, devid);
mdev[devid].mdbdevb = devfs_make_node(mdev[devid].mdBDev, DEVFS_BLOCK,
UID_ROOT, GID_OPERATOR,
0600, "md%d", devid);
if (mdev[devid].mdbdevb == NULL) {
printf("mdevadd: devfs_make_node for block failed!\n");
return -1;
}
mdev[devid].mdCDev = makedev(mdevCMajor, devid);
mdev[devid].mdcdevb = devfs_make_node(mdev[devid].mdCDev, DEVFS_CHAR,
UID_ROOT, GID_OPERATOR,
0600, "rmd%d", devid);
if (mdev[devid].mdcdevb == NULL) {
printf("mdevadd: devfs_make_node for character failed!\n");
return -1;
}
mdev[devid].mdBase = base;
mdev[devid].mdSize = size;
mdev[devid].mdSecsize = DEV_BSIZE;
if(phys) mdev[devid].mdFlags |= mdPhys;
mdev[devid].mdFlags |= mdInited;
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 mdev[devid].mdBDev;
}
dev_t mdevlookup(int devid) {
if((devid < 0) || (devid > 15)) return -1;
if(!(mdev[devid].mdFlags & mdInited)) return -1;
return mdev[devid].mdBDev;
}
void mdevremoveall(void) {
int i;
for(i = 0; i < 16; i++) {
if(!(mdev[i].mdFlags & mdInited)) continue;
devfs_remove(mdev[i].mdbdevb);
devfs_remove(mdev[i].mdcdevb);
mdev[i].mdBase = 0;
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;
}
}