1*c54f35caSApple OSS Distributions /*
2*c54f35caSApple OSS Distributions * Copyright (c) 2004-2019 Apple Inc. All rights reserved.
3*c54f35caSApple OSS Distributions *
4*c54f35caSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*c54f35caSApple OSS Distributions *
6*c54f35caSApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*c54f35caSApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*c54f35caSApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*c54f35caSApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*c54f35caSApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*c54f35caSApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*c54f35caSApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*c54f35caSApple OSS Distributions * terms of an Apple operating system software license agreement.
14*c54f35caSApple OSS Distributions *
15*c54f35caSApple OSS Distributions * Please obtain a copy of the License at
16*c54f35caSApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*c54f35caSApple OSS Distributions *
18*c54f35caSApple OSS Distributions * The Original Code and all software distributed under the License are
19*c54f35caSApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*c54f35caSApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*c54f35caSApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*c54f35caSApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*c54f35caSApple OSS Distributions * Please see the License for the specific language governing rights and
24*c54f35caSApple OSS Distributions * limitations under the License.
25*c54f35caSApple OSS Distributions *
26*c54f35caSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*c54f35caSApple OSS Distributions */
28*c54f35caSApple OSS Distributions /*
29*c54f35caSApple OSS Distributions * Copyright (c) 1988 University of Utah.
30*c54f35caSApple OSS Distributions * Copyright (c) 1990, 1993
31*c54f35caSApple OSS Distributions * The Regents of the University of California. All rights reserved.
32*c54f35caSApple OSS Distributions *
33*c54f35caSApple OSS Distributions * This code is derived from software contributed to Berkeley by
34*c54f35caSApple OSS Distributions * the Systems Programming Group of the University of Utah Computer
35*c54f35caSApple OSS Distributions * Science Department.
36*c54f35caSApple OSS Distributions *
37*c54f35caSApple OSS Distributions * Redistribution and use in source and binary forms, with or without
38*c54f35caSApple OSS Distributions * modification, are permitted provided that the following conditions
39*c54f35caSApple OSS Distributions * are met:
40*c54f35caSApple OSS Distributions * 1. Redistributions of source code must retain the above copyright
41*c54f35caSApple OSS Distributions * notice, this list of conditions and the following disclaimer.
42*c54f35caSApple OSS Distributions * 2. Redistributions in binary form must reproduce the above copyright
43*c54f35caSApple OSS Distributions * notice, this list of conditions and the following disclaimer in the
44*c54f35caSApple OSS Distributions * documentation and/or other materials provided with the distribution.
45*c54f35caSApple OSS Distributions * 3. All advertising materials mentioning features or use of this software
46*c54f35caSApple OSS Distributions * must display the following acknowledgement:
47*c54f35caSApple OSS Distributions * This product includes software developed by the University of
48*c54f35caSApple OSS Distributions * California, Berkeley and its contributors.
49*c54f35caSApple OSS Distributions * 4. Neither the name of the University nor the names of its contributors
50*c54f35caSApple OSS Distributions * may be used to endorse or promote products derived from this software
51*c54f35caSApple OSS Distributions * without specific prior written permission.
52*c54f35caSApple OSS Distributions *
53*c54f35caSApple OSS Distributions * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54*c54f35caSApple OSS Distributions * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55*c54f35caSApple OSS Distributions * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56*c54f35caSApple OSS Distributions * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57*c54f35caSApple OSS Distributions * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58*c54f35caSApple OSS Distributions * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59*c54f35caSApple OSS Distributions * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60*c54f35caSApple OSS Distributions * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61*c54f35caSApple OSS Distributions * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62*c54f35caSApple OSS Distributions * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63*c54f35caSApple OSS Distributions * SUCH DAMAGE.
64*c54f35caSApple OSS Distributions *
65*c54f35caSApple OSS Distributions * from: Utah Hdr: vn.c 1.13 94/04/02
66*c54f35caSApple OSS Distributions *
67*c54f35caSApple OSS Distributions * from: @(#)vn.c 8.6 (Berkeley) 4/1/94
68*c54f35caSApple OSS Distributions * $FreeBSD: src/sys/dev/vn/vn.c,v 1.105.2.4 2001/11/18 07:11:00 dillon Exp $
69*c54f35caSApple OSS Distributions */
70*c54f35caSApple OSS Distributions
71*c54f35caSApple OSS Distributions /*
72*c54f35caSApple OSS Distributions * RAM disk driver.
73*c54f35caSApple OSS Distributions *
74*c54f35caSApple OSS Distributions * Block interface to a ramdisk.
75*c54f35caSApple OSS Distributions *
76*c54f35caSApple OSS Distributions */
77*c54f35caSApple OSS Distributions
78*c54f35caSApple OSS Distributions #include <sys/param.h>
79*c54f35caSApple OSS Distributions #include <sys/kernel.h>
80*c54f35caSApple OSS Distributions #include <sys/mount.h>
81*c54f35caSApple OSS Distributions #include <sys/namei.h>
82*c54f35caSApple OSS Distributions #include <sys/proc.h>
83*c54f35caSApple OSS Distributions #include <sys/buf.h>
84*c54f35caSApple OSS Distributions #include <sys/malloc.h>
85*c54f35caSApple OSS Distributions #include <sys/mount.h>
86*c54f35caSApple OSS Distributions #include <sys/fcntl.h>
87*c54f35caSApple OSS Distributions #include <sys/conf.h>
88*c54f35caSApple OSS Distributions #include <sys/disk.h>
89*c54f35caSApple OSS Distributions #include <sys/stat.h>
90*c54f35caSApple OSS Distributions #include <sys/vm.h>
91*c54f35caSApple OSS Distributions #include <sys/uio_internal.h>
92*c54f35caSApple OSS Distributions #include <libkern/libkern.h>
93*c54f35caSApple OSS Distributions
94*c54f35caSApple OSS Distributions #include <vm/pmap.h>
95*c54f35caSApple OSS Distributions #include <vm/vm_pager.h>
96*c54f35caSApple OSS Distributions #include <mach/memory_object_types.h>
97*c54f35caSApple OSS Distributions #include <kern/debug.h>
98*c54f35caSApple OSS Distributions
99*c54f35caSApple OSS Distributions #include <miscfs/devfs/devfs.h>
100*c54f35caSApple OSS Distributions
101*c54f35caSApple OSS Distributions
102*c54f35caSApple OSS Distributions void mdevinit(int the_cnt);
103*c54f35caSApple OSS Distributions
104*c54f35caSApple OSS Distributions static open_close_fcn_t mdevopen;
105*c54f35caSApple OSS Distributions static open_close_fcn_t mdevclose;
106*c54f35caSApple OSS Distributions static psize_fcn_t mdevsize;
107*c54f35caSApple OSS Distributions static strategy_fcn_t mdevstrategy;
108*c54f35caSApple OSS Distributions static int mdevbioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p);
109*c54f35caSApple OSS Distributions static int mdevcioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p);
110*c54f35caSApple OSS Distributions static int mdevrw(dev_t dev, struct uio *uio, int ioflag);
111*c54f35caSApple OSS Distributions
112*c54f35caSApple OSS Distributions #ifdef CONFIG_MEMDEV_INSECURE
113*c54f35caSApple OSS Distributions static char * nonspace(char *pos, char *end);
114*c54f35caSApple OSS Distributions static char * getspace(char *pos, char *end);
115*c54f35caSApple OSS Distributions static char * cvtnum(char *pos, char *end, uint64_t *num);
116*c54f35caSApple OSS Distributions #endif /* CONFIG_MEMDEV_INSECURE */
117*c54f35caSApple OSS Distributions
118*c54f35caSApple OSS Distributions extern void bcopy_phys(addr64_t from, addr64_t to, vm_size_t bytes);
119*c54f35caSApple OSS Distributions extern void mapping_set_mod(ppnum_t pn);
120*c54f35caSApple OSS Distributions extern ppnum_t pmap_find_phys(pmap_t pmap, addr64_t va);
121*c54f35caSApple OSS Distributions
122*c54f35caSApple OSS Distributions /*
123*c54f35caSApple OSS Distributions * Maximal number of memory devices.
124*c54f35caSApple OSS Distributions */
125*c54f35caSApple OSS Distributions #define NB_MAX_MDEVICES (16)
126*c54f35caSApple OSS Distributions
127*c54f35caSApple OSS Distributions /*
128*c54f35caSApple OSS Distributions * cdevsw
129*c54f35caSApple OSS Distributions * D_DISK we want to look like a disk
130*c54f35caSApple OSS Distributions * D_CANFREE We support B_FREEBUF
131*c54f35caSApple OSS Distributions */
132*c54f35caSApple OSS Distributions
133*c54f35caSApple OSS Distributions static const struct bdevsw mdevbdevsw = {
134*c54f35caSApple OSS Distributions .d_open = mdevopen,
135*c54f35caSApple OSS Distributions .d_close = mdevclose,
136*c54f35caSApple OSS Distributions .d_strategy = mdevstrategy,
137*c54f35caSApple OSS Distributions .d_ioctl = mdevbioctl,
138*c54f35caSApple OSS Distributions .d_dump = eno_dump,
139*c54f35caSApple OSS Distributions .d_psize = mdevsize,
140*c54f35caSApple OSS Distributions .d_type = D_DISK,
141*c54f35caSApple OSS Distributions };
142*c54f35caSApple OSS Distributions
143*c54f35caSApple OSS Distributions static const struct cdevsw mdevcdevsw = {
144*c54f35caSApple OSS Distributions .d_open = mdevopen,
145*c54f35caSApple OSS Distributions .d_close = mdevclose,
146*c54f35caSApple OSS Distributions .d_read = mdevrw,
147*c54f35caSApple OSS Distributions .d_write = mdevrw,
148*c54f35caSApple OSS Distributions .d_ioctl = mdevcioctl,
149*c54f35caSApple OSS Distributions .d_stop = eno_stop,
150*c54f35caSApple OSS Distributions .d_reset = eno_reset,
151*c54f35caSApple OSS Distributions .d_ttys = NULL,
152*c54f35caSApple OSS Distributions .d_select = eno_select,
153*c54f35caSApple OSS Distributions .d_mmap = eno_mmap,
154*c54f35caSApple OSS Distributions .d_strategy = eno_strat,
155*c54f35caSApple OSS Distributions .d_reserved_1 = eno_getc,
156*c54f35caSApple OSS Distributions .d_reserved_2 = eno_putc,
157*c54f35caSApple OSS Distributions .d_type = D_DISK,
158*c54f35caSApple OSS Distributions };
159*c54f35caSApple OSS Distributions
160*c54f35caSApple OSS Distributions struct mdev {
161*c54f35caSApple OSS Distributions uint64_t mdBase; /* base page number (pages are assumed to be 4K). Multiply by 4096 to find actual address */
162*c54f35caSApple OSS Distributions uint32_t mdSize; /* size in pages (pages are assumed to be 4K). Multiply by 4096 to find actual size. */
163*c54f35caSApple OSS Distributions int mdFlags; /* flags */
164*c54f35caSApple OSS Distributions int mdSecsize; /* sector size */
165*c54f35caSApple OSS Distributions int mdBDev; /* Block device number */
166*c54f35caSApple OSS Distributions int mdCDev; /* Character device number */
167*c54f35caSApple OSS Distributions void * mdbdevb;
168*c54f35caSApple OSS Distributions void * mdcdevb;
169*c54f35caSApple OSS Distributions } mdev[NB_MAX_MDEVICES];
170*c54f35caSApple OSS Distributions
171*c54f35caSApple OSS Distributions /* mdFlags */
172*c54f35caSApple OSS Distributions #define mdInited 0x01 /* This device defined */
173*c54f35caSApple OSS Distributions #define mdRO 0x02 /* This device is read-only */
174*c54f35caSApple OSS Distributions #define mdPhys 0x04 /* This device is in physical memory */
175*c54f35caSApple OSS Distributions
176*c54f35caSApple OSS Distributions int mdevBMajor = -1;
177*c54f35caSApple OSS Distributions int mdevCMajor = -1;
178*c54f35caSApple OSS Distributions
179*c54f35caSApple OSS Distributions static int mdevioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p, int is_char);
180*c54f35caSApple OSS Distributions dev_t mdevadd(int devid, uint64_t base, unsigned int size, int phys);
181*c54f35caSApple OSS Distributions dev_t mdevlookup(int devid);
182*c54f35caSApple OSS Distributions void mdevremoveall(void);
183*c54f35caSApple OSS Distributions int mdevgetrange(int devid, uint64_t *base, uint64_t *size);
184*c54f35caSApple OSS Distributions
185*c54f35caSApple OSS Distributions static int
mdevclose(__unused dev_t dev,__unused int flags,__unused int devtype,__unused struct proc * p)186*c54f35caSApple OSS Distributions mdevclose(__unused dev_t dev, __unused int flags,
187*c54f35caSApple OSS Distributions __unused int devtype, __unused struct proc *p)
188*c54f35caSApple OSS Distributions {
189*c54f35caSApple OSS Distributions return 0;
190*c54f35caSApple OSS Distributions }
191*c54f35caSApple OSS Distributions
192*c54f35caSApple OSS Distributions static int
mdevopen(dev_t dev,int flags,__unused int devtype,__unused struct proc * p)193*c54f35caSApple OSS Distributions mdevopen(dev_t dev, int flags, __unused int devtype, __unused struct proc *p)
194*c54f35caSApple OSS Distributions {
195*c54f35caSApple OSS Distributions int devid;
196*c54f35caSApple OSS Distributions
197*c54f35caSApple OSS Distributions devid = minor(dev); /* Get minor device number */
198*c54f35caSApple OSS Distributions
199*c54f35caSApple OSS Distributions if (devid >= NB_MAX_MDEVICES || devid < 0) {
200*c54f35caSApple OSS Distributions return ENXIO; /* Not valid */
201*c54f35caSApple OSS Distributions }
202*c54f35caSApple OSS Distributions if ((flags & FWRITE) && (mdev[devid].mdFlags & mdRO)) {
203*c54f35caSApple OSS Distributions return EACCES; /* Currently mounted RO */
204*c54f35caSApple OSS Distributions }
205*c54f35caSApple OSS Distributions return 0;
206*c54f35caSApple OSS Distributions }
207*c54f35caSApple OSS Distributions
208*c54f35caSApple OSS Distributions static int
mdevrw(dev_t dev,struct uio * uio,__unused int ioflag)209*c54f35caSApple OSS Distributions mdevrw(dev_t dev, struct uio *uio, __unused int ioflag)
210*c54f35caSApple OSS Distributions {
211*c54f35caSApple OSS Distributions int status;
212*c54f35caSApple OSS Distributions addr64_t mdata;
213*c54f35caSApple OSS Distributions int devid;
214*c54f35caSApple OSS Distributions enum uio_seg saveflag;
215*c54f35caSApple OSS Distributions int count;
216*c54f35caSApple OSS Distributions
217*c54f35caSApple OSS Distributions devid = minor(dev); /* Get minor device number */
218*c54f35caSApple OSS Distributions
219*c54f35caSApple OSS Distributions if (devid >= NB_MAX_MDEVICES || devid < 0) {
220*c54f35caSApple OSS Distributions return ENXIO; /* Not valid */
221*c54f35caSApple OSS Distributions }
222*c54f35caSApple OSS Distributions if (!(mdev[devid].mdFlags & mdInited)) {
223*c54f35caSApple OSS Distributions return ENXIO; /* Have we actually been defined yet? */
224*c54f35caSApple OSS Distributions }
225*c54f35caSApple OSS Distributions if (uio->uio_offset < 0) {
226*c54f35caSApple OSS Distributions return EINVAL; /* invalid offset */
227*c54f35caSApple OSS Distributions }
228*c54f35caSApple OSS Distributions if (uio_resid(uio) < 0) {
229*c54f35caSApple OSS Distributions return EINVAL;
230*c54f35caSApple OSS Distributions }
231*c54f35caSApple OSS Distributions mdata = ((addr64_t)mdev[devid].mdBase << 12) + uio->uio_offset; /* Point to the area in "file" */
232*c54f35caSApple OSS Distributions
233*c54f35caSApple OSS Distributions saveflag = uio->uio_segflg; /* Remember what the request is */
234*c54f35caSApple OSS Distributions #if LP64_DEBUG
235*c54f35caSApple OSS Distributions if (UIO_IS_USER_SPACE(uio) == 0 && UIO_IS_SYS_SPACE(uio) == 0) {
236*c54f35caSApple OSS Distributions panic("mdevrw - invalid uio_segflg");
237*c54f35caSApple OSS Distributions }
238*c54f35caSApple OSS Distributions #endif /* LP64_DEBUG */
239*c54f35caSApple OSS Distributions /* Make sure we are moving from physical ram if physical device */
240*c54f35caSApple OSS Distributions if (mdev[devid].mdFlags & mdPhys) {
241*c54f35caSApple OSS Distributions if (uio->uio_segflg == UIO_USERSPACE64) {
242*c54f35caSApple OSS Distributions uio->uio_segflg = UIO_PHYS_USERSPACE64;
243*c54f35caSApple OSS Distributions } else if (uio->uio_segflg == UIO_USERSPACE32) {
244*c54f35caSApple OSS Distributions uio->uio_segflg = UIO_PHYS_USERSPACE32;
245*c54f35caSApple OSS Distributions } else {
246*c54f35caSApple OSS Distributions uio->uio_segflg = UIO_PHYS_USERSPACE;
247*c54f35caSApple OSS Distributions }
248*c54f35caSApple OSS Distributions }
249*c54f35caSApple OSS Distributions
250*c54f35caSApple OSS Distributions if (uio->uio_offset > (mdev[devid].mdSize << 12)) {
251*c54f35caSApple OSS Distributions count = 0;
252*c54f35caSApple OSS Distributions } else {
253*c54f35caSApple OSS Distributions count = imin(uio_resid(uio), (mdev[devid].mdSize << 12) - uio->uio_offset);
254*c54f35caSApple OSS Distributions }
255*c54f35caSApple OSS Distributions
256*c54f35caSApple OSS Distributions status = uiomove64(mdata, count, uio); /* Move the data */
257*c54f35caSApple OSS Distributions uio->uio_segflg = saveflag; /* Restore the flag */
258*c54f35caSApple OSS Distributions
259*c54f35caSApple OSS Distributions return status;
260*c54f35caSApple OSS Distributions }
261*c54f35caSApple OSS Distributions
262*c54f35caSApple OSS Distributions static void
mdevstrategy(struct buf * bp)263*c54f35caSApple OSS Distributions mdevstrategy(struct buf *bp)
264*c54f35caSApple OSS Distributions {
265*c54f35caSApple OSS Distributions unsigned int left, lop, csize;
266*c54f35caSApple OSS Distributions vm_offset_t vaddr, blkoff;
267*c54f35caSApple OSS Distributions int devid;
268*c54f35caSApple OSS Distributions addr64_t paddr, fvaddr;
269*c54f35caSApple OSS Distributions ppnum_t pp;
270*c54f35caSApple OSS Distributions
271*c54f35caSApple OSS Distributions devid = minor(buf_device(bp)); /* Get minor device number */
272*c54f35caSApple OSS Distributions
273*c54f35caSApple OSS Distributions if ((mdev[devid].mdFlags & mdInited) == 0) { /* Have we actually been defined yet? */
274*c54f35caSApple OSS Distributions buf_seterror(bp, ENXIO);
275*c54f35caSApple OSS Distributions buf_biodone(bp);
276*c54f35caSApple OSS Distributions return;
277*c54f35caSApple OSS Distributions }
278*c54f35caSApple OSS Distributions
279*c54f35caSApple OSS Distributions buf_setresid(bp, buf_count(bp)); /* Set byte count */
280*c54f35caSApple OSS Distributions
281*c54f35caSApple OSS Distributions blkoff = buf_blkno(bp) * mdev[devid].mdSecsize; /* Get offset into file */
282*c54f35caSApple OSS Distributions
283*c54f35caSApple OSS Distributions /*
284*c54f35caSApple OSS Distributions * Note that reading past end is an error, but reading at end is an EOF. For these
285*c54f35caSApple OSS Distributions * we just return with resid == count.
286*c54f35caSApple OSS Distributions */
287*c54f35caSApple OSS Distributions
288*c54f35caSApple OSS Distributions if (blkoff >= (mdev[devid].mdSize << 12)) { /* Are they trying to read/write at/after end? */
289*c54f35caSApple OSS Distributions if (blkoff != (mdev[devid].mdSize << 12)) { /* Are we trying to read after EOF? */
290*c54f35caSApple OSS Distributions buf_seterror(bp, EINVAL); /* Yeah, this is an error */
291*c54f35caSApple OSS Distributions }
292*c54f35caSApple OSS Distributions buf_biodone(bp); /* Return */
293*c54f35caSApple OSS Distributions return;
294*c54f35caSApple OSS Distributions }
295*c54f35caSApple OSS Distributions
296*c54f35caSApple OSS Distributions if ((blkoff + buf_count(bp)) > (mdev[devid].mdSize << 12)) { /* Will this read go past end? */
297*c54f35caSApple OSS Distributions buf_setcount(bp, (uint32_t)((mdev[devid].mdSize << 12) - blkoff)); /* Yes, trim to max */
298*c54f35caSApple OSS Distributions }
299*c54f35caSApple OSS Distributions /*
300*c54f35caSApple OSS Distributions * make sure the buffer's data area is
301*c54f35caSApple OSS Distributions * accessible
302*c54f35caSApple OSS Distributions */
303*c54f35caSApple OSS Distributions if (buf_map(bp, (caddr_t *)&vaddr)) {
304*c54f35caSApple OSS Distributions panic("ramstrategy: buf_map failed");
305*c54f35caSApple OSS Distributions }
306*c54f35caSApple OSS Distributions
307*c54f35caSApple OSS Distributions fvaddr = (mdev[devid].mdBase << 12) + blkoff; /* Point to offset into ram disk */
308*c54f35caSApple OSS Distributions
309*c54f35caSApple OSS Distributions if (buf_flags(bp) & B_READ) { /* Is this a read? */
310*c54f35caSApple OSS Distributions if (!(mdev[devid].mdFlags & mdPhys)) { /* Physical mapped disk? */
311*c54f35caSApple OSS Distributions bcopy((void *)((uintptr_t)fvaddr),
312*c54f35caSApple OSS Distributions (void *)vaddr, (size_t)buf_count(bp)); /* This is virtual, just get the data */
313*c54f35caSApple OSS Distributions } else {
314*c54f35caSApple OSS Distributions left = buf_count(bp); /* Init the amount left to copy */
315*c54f35caSApple OSS Distributions while (left) { /* Go until it is all copied */
316*c54f35caSApple OSS Distributions lop = min((4096 - (vaddr & 4095)), (4096 - (fvaddr & 4095))); /* Get smallest amount left on sink and source */
317*c54f35caSApple OSS Distributions csize = min(lop, left); /* Don't move more than we need to */
318*c54f35caSApple OSS Distributions
319*c54f35caSApple OSS Distributions pp = pmap_find_phys(kernel_pmap, (addr64_t)((uintptr_t)vaddr)); /* Get the sink physical address */
320*c54f35caSApple OSS Distributions if (!pp) { /* Not found, what gives? */
321*c54f35caSApple OSS Distributions panic("mdevstrategy: sink address %016llX not mapped", (addr64_t)((uintptr_t)vaddr));
322*c54f35caSApple OSS Distributions }
323*c54f35caSApple OSS Distributions paddr = (addr64_t)(((addr64_t)pp << 12) | (addr64_t)(vaddr & 4095)); /* Get actual address */
324*c54f35caSApple OSS Distributions bcopy_phys(fvaddr, paddr, csize); /* Copy this on in */
325*c54f35caSApple OSS Distributions mapping_set_mod((ppnum_t)(paddr >> 12)); /* Make sure we know that it is modified */
326*c54f35caSApple OSS Distributions
327*c54f35caSApple OSS Distributions left = left - csize; /* Calculate what is left */
328*c54f35caSApple OSS Distributions vaddr = vaddr + csize; /* Move to next sink address */
329*c54f35caSApple OSS Distributions fvaddr = fvaddr + csize; /* Bump to next physical address */
330*c54f35caSApple OSS Distributions }
331*c54f35caSApple OSS Distributions }
332*c54f35caSApple OSS Distributions } else { /* This is a write */
333*c54f35caSApple OSS Distributions if (!(mdev[devid].mdFlags & mdPhys)) { /* Physical mapped disk? */
334*c54f35caSApple OSS Distributions bcopy((void *)vaddr, (void *)((uintptr_t)fvaddr),
335*c54f35caSApple OSS Distributions (size_t)buf_count(bp)); /* This is virtual, just put the data */
336*c54f35caSApple OSS Distributions } else {
337*c54f35caSApple OSS Distributions left = buf_count(bp); /* Init the amount left to copy */
338*c54f35caSApple OSS Distributions while (left) { /* Go until it is all copied */
339*c54f35caSApple OSS Distributions lop = min((4096 - (vaddr & 4095)), (4096 - (fvaddr & 4095))); /* Get smallest amount left on sink and source */
340*c54f35caSApple OSS Distributions csize = min(lop, left); /* Don't move more than we need to */
341*c54f35caSApple OSS Distributions
342*c54f35caSApple OSS Distributions pp = pmap_find_phys(kernel_pmap, (addr64_t)((uintptr_t)vaddr)); /* Get the source physical address */
343*c54f35caSApple OSS Distributions if (!pp) { /* Not found, what gives? */
344*c54f35caSApple OSS Distributions panic("mdevstrategy: source address %016llX not mapped", (addr64_t)((uintptr_t)vaddr));
345*c54f35caSApple OSS Distributions }
346*c54f35caSApple OSS Distributions paddr = (addr64_t)(((addr64_t)pp << 12) | (addr64_t)(vaddr & 4095)); /* Get actual address */
347*c54f35caSApple OSS Distributions
348*c54f35caSApple OSS Distributions bcopy_phys(paddr, fvaddr, csize); /* Move this on out */
349*c54f35caSApple OSS Distributions
350*c54f35caSApple OSS Distributions left = left - csize; /* Calculate what is left */
351*c54f35caSApple OSS Distributions vaddr = vaddr + csize; /* Move to next sink address */
352*c54f35caSApple OSS Distributions fvaddr = fvaddr + csize; /* Bump to next physical address */
353*c54f35caSApple OSS Distributions }
354*c54f35caSApple OSS Distributions }
355*c54f35caSApple OSS Distributions }
356*c54f35caSApple OSS Distributions /*
357*c54f35caSApple OSS Distributions * buf_unmap takes care of all the cases
358*c54f35caSApple OSS Distributions * it will unmap the buffer from kernel
359*c54f35caSApple OSS Distributions * virtual space if that was the state
360*c54f35caSApple OSS Distributions * when we mapped it.
361*c54f35caSApple OSS Distributions */
362*c54f35caSApple OSS Distributions buf_unmap(bp);
363*c54f35caSApple OSS Distributions
364*c54f35caSApple OSS Distributions buf_setresid(bp, 0); /* Nothing more to do */
365*c54f35caSApple OSS Distributions buf_biodone(bp); /* Say we've finished */
366*c54f35caSApple OSS Distributions }
367*c54f35caSApple OSS Distributions
368*c54f35caSApple OSS Distributions static int
mdevbioctl(dev_t dev,u_long cmd,caddr_t data,int flag,struct proc * p)369*c54f35caSApple OSS Distributions mdevbioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
370*c54f35caSApple OSS Distributions {
371*c54f35caSApple OSS Distributions return mdevioctl(dev, cmd, data, flag, p, 0);
372*c54f35caSApple OSS Distributions }
373*c54f35caSApple OSS Distributions
374*c54f35caSApple OSS Distributions static int
mdevcioctl(dev_t dev,u_long cmd,caddr_t data,int flag,struct proc * p)375*c54f35caSApple OSS Distributions mdevcioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
376*c54f35caSApple OSS Distributions {
377*c54f35caSApple OSS Distributions return mdevioctl(dev, cmd, data, flag, p, 1);
378*c54f35caSApple OSS Distributions }
379*c54f35caSApple OSS Distributions
380*c54f35caSApple OSS Distributions static int
mdevioctl(dev_t dev,u_long cmd,caddr_t data,__unused int flag,struct proc * p,int is_char)381*c54f35caSApple OSS Distributions mdevioctl(dev_t dev, u_long cmd, caddr_t data, __unused int flag,
382*c54f35caSApple OSS Distributions struct proc *p, int is_char)
383*c54f35caSApple OSS Distributions {
384*c54f35caSApple OSS Distributions int error;
385*c54f35caSApple OSS Distributions u_int32_t *f;
386*c54f35caSApple OSS Distributions u_int64_t *o;
387*c54f35caSApple OSS Distributions int devid;
388*c54f35caSApple OSS Distributions dk_memdev_info_t * memdev_info;
389*c54f35caSApple OSS Distributions
390*c54f35caSApple OSS Distributions devid = minor(dev); /* Get minor device number */
391*c54f35caSApple OSS Distributions
392*c54f35caSApple OSS Distributions if (devid >= NB_MAX_MDEVICES || devid < 0) {
393*c54f35caSApple OSS Distributions return ENXIO; /* Not valid */
394*c54f35caSApple OSS Distributions }
395*c54f35caSApple OSS Distributions error = proc_suser(p); /* Are we superman? */
396*c54f35caSApple OSS Distributions if (error) {
397*c54f35caSApple OSS Distributions return error; /* Nope... */
398*c54f35caSApple OSS Distributions }
399*c54f35caSApple OSS Distributions f = (u_int32_t*)data;
400*c54f35caSApple OSS Distributions o = (u_int64_t *)data;
401*c54f35caSApple OSS Distributions memdev_info = (dk_memdev_info_t *) data;
402*c54f35caSApple OSS Distributions
403*c54f35caSApple OSS Distributions switch (cmd) {
404*c54f35caSApple OSS Distributions case DKIOCGETMAXBLOCKCOUNTREAD:
405*c54f35caSApple OSS Distributions *o = 32;
406*c54f35caSApple OSS Distributions break;
407*c54f35caSApple OSS Distributions
408*c54f35caSApple OSS Distributions case DKIOCGETMAXBLOCKCOUNTWRITE:
409*c54f35caSApple OSS Distributions *o = 32;
410*c54f35caSApple OSS Distributions break;
411*c54f35caSApple OSS Distributions
412*c54f35caSApple OSS Distributions case DKIOCGETMAXSEGMENTCOUNTREAD:
413*c54f35caSApple OSS Distributions *o = 32;
414*c54f35caSApple OSS Distributions break;
415*c54f35caSApple OSS Distributions
416*c54f35caSApple OSS Distributions case DKIOCGETMAXSEGMENTCOUNTWRITE:
417*c54f35caSApple OSS Distributions *o = 32;
418*c54f35caSApple OSS Distributions break;
419*c54f35caSApple OSS Distributions
420*c54f35caSApple OSS Distributions case DKIOCGETBLOCKSIZE:
421*c54f35caSApple OSS Distributions *f = mdev[devid].mdSecsize;
422*c54f35caSApple OSS Distributions break;
423*c54f35caSApple OSS Distributions
424*c54f35caSApple OSS Distributions case DKIOCSETBLOCKSIZE:
425*c54f35caSApple OSS Distributions if (is_char) {
426*c54f35caSApple OSS Distributions return ENODEV; /* We can only do this for a block */
427*c54f35caSApple OSS Distributions }
428*c54f35caSApple OSS Distributions if (*f < DEV_BSIZE) {
429*c54f35caSApple OSS Distributions return EINVAL; /* Too short? */
430*c54f35caSApple OSS Distributions }
431*c54f35caSApple OSS Distributions mdev[devid].mdSecsize = *f; /* set the new block size */
432*c54f35caSApple OSS Distributions break;
433*c54f35caSApple OSS Distributions
434*c54f35caSApple OSS Distributions case DKIOCISWRITABLE:
435*c54f35caSApple OSS Distributions *f = 1;
436*c54f35caSApple OSS Distributions break;
437*c54f35caSApple OSS Distributions
438*c54f35caSApple OSS Distributions case DKIOCGETBLOCKCOUNT:
439*c54f35caSApple OSS Distributions if (!(mdev[devid].mdFlags & mdInited)) {
440*c54f35caSApple OSS Distributions return ENXIO;
441*c54f35caSApple OSS Distributions }
442*c54f35caSApple OSS Distributions *o = ((mdev[devid].mdSize << 12) + mdev[devid].mdSecsize - 1) / mdev[devid].mdSecsize;
443*c54f35caSApple OSS Distributions break;
444*c54f35caSApple OSS Distributions
445*c54f35caSApple OSS Distributions /*
446*c54f35caSApple OSS Distributions * We're interested in the following bits of information:
447*c54f35caSApple OSS Distributions * Are you a memory-backed device (always yes, in this case)?
448*c54f35caSApple OSS Distributions * Physical memory (mdPhys)?
449*c54f35caSApple OSS Distributions * What is your base page?
450*c54f35caSApple OSS Distributions * What is your size?
451*c54f35caSApple OSS Distributions */
452*c54f35caSApple OSS Distributions case DKIOCGETMEMDEVINFO:
453*c54f35caSApple OSS Distributions if (!(mdev[devid].mdFlags & mdInited)) {
454*c54f35caSApple OSS Distributions return ENXIO;
455*c54f35caSApple OSS Distributions }
456*c54f35caSApple OSS Distributions memdev_info->mi_mdev = TRUE;
457*c54f35caSApple OSS Distributions memdev_info->mi_phys = (mdev[devid].mdFlags & mdPhys) ? TRUE : FALSE;
458*c54f35caSApple OSS Distributions memdev_info->mi_base = (uint32_t)mdev[devid].mdBase;
459*c54f35caSApple OSS Distributions memdev_info->mi_size = mdev[devid].mdSize;
460*c54f35caSApple OSS Distributions break;
461*c54f35caSApple OSS Distributions
462*c54f35caSApple OSS Distributions default:
463*c54f35caSApple OSS Distributions error = ENOTTY;
464*c54f35caSApple OSS Distributions break;
465*c54f35caSApple OSS Distributions }
466*c54f35caSApple OSS Distributions return error;
467*c54f35caSApple OSS Distributions }
468*c54f35caSApple OSS Distributions
469*c54f35caSApple OSS Distributions
470*c54f35caSApple OSS Distributions static int
mdevsize(dev_t dev)471*c54f35caSApple OSS Distributions mdevsize(dev_t dev)
472*c54f35caSApple OSS Distributions {
473*c54f35caSApple OSS Distributions int devid;
474*c54f35caSApple OSS Distributions
475*c54f35caSApple OSS Distributions devid = minor(dev); /* Get minor device number */
476*c54f35caSApple OSS Distributions if (devid >= NB_MAX_MDEVICES || devid < 0) {
477*c54f35caSApple OSS Distributions return ENXIO; /* Not valid */
478*c54f35caSApple OSS Distributions }
479*c54f35caSApple OSS Distributions if ((mdev[devid].mdFlags & mdInited) == 0) {
480*c54f35caSApple OSS Distributions return -1; /* Not inited yet */
481*c54f35caSApple OSS Distributions }
482*c54f35caSApple OSS Distributions return mdev[devid].mdSecsize;
483*c54f35caSApple OSS Distributions }
484*c54f35caSApple OSS Distributions
485*c54f35caSApple OSS Distributions #include <pexpert/pexpert.h>
486*c54f35caSApple OSS Distributions
487*c54f35caSApple OSS Distributions void
mdevinit(__unused int the_cnt)488*c54f35caSApple OSS Distributions mdevinit(__unused int the_cnt)
489*c54f35caSApple OSS Distributions {
490*c54f35caSApple OSS Distributions #ifdef CONFIG_MEMDEV_INSECURE
491*c54f35caSApple OSS Distributions
492*c54f35caSApple OSS Distributions int devid, phys;
493*c54f35caSApple OSS Distributions uint64_t base;
494*c54f35caSApple OSS Distributions uint64_t size;
495*c54f35caSApple OSS Distributions char *ba, *lp;
496*c54f35caSApple OSS Distributions dev_t dev;
497*c54f35caSApple OSS Distributions
498*c54f35caSApple OSS Distributions
499*c54f35caSApple OSS Distributions ba = PE_boot_args(); /* Get the boot arguments */
500*c54f35caSApple OSS Distributions lp = ba + 256; /* Point to the end */
501*c54f35caSApple OSS Distributions
502*c54f35caSApple OSS Distributions while (1) { /* Step through, looking for our keywords */
503*c54f35caSApple OSS Distributions phys = 0; /* Assume virtual memory device */
504*c54f35caSApple OSS Distributions ba = nonspace(ba, lp); /* Find non-space */
505*c54f35caSApple OSS Distributions if (ba >= lp) {
506*c54f35caSApple OSS Distributions return; /* We are done if no more... */
507*c54f35caSApple OSS Distributions }
508*c54f35caSApple OSS Distributions if (((ba[0] != 'v') && (ba[0] != 'p'))
509*c54f35caSApple OSS Distributions || (ba[1] != 'm') || (ba[2] != 'd') || (ba[4] != '=')
510*c54f35caSApple OSS Distributions || (ba[3] < '0') || (ba[3] > 'f')
511*c54f35caSApple OSS Distributions || ((ba[3] > '9') && (ba[3] < 'a'))) { /* Is this of form "vmdx=" or "pmdx=" where x is hex digit? */
512*c54f35caSApple OSS Distributions ba = getspace(ba, lp); /* Find next white space or end */
513*c54f35caSApple OSS Distributions continue; /* Start looking for the next one */
514*c54f35caSApple OSS Distributions }
515*c54f35caSApple OSS Distributions
516*c54f35caSApple OSS Distributions if (ba[0] == 'p') {
517*c54f35caSApple OSS Distributions phys = 1; /* Set physical memory disk */
518*c54f35caSApple OSS Distributions }
519*c54f35caSApple OSS Distributions devid = ba[3] & 0xF; /* Assume digit */
520*c54f35caSApple OSS Distributions if (ba[3] > '9') {
521*c54f35caSApple OSS Distributions devid += 9; /* Adjust for hex digits */
522*c54f35caSApple OSS Distributions }
523*c54f35caSApple OSS Distributions ba = &ba[5]; /* Step past keyword */
524*c54f35caSApple OSS Distributions ba = cvtnum(ba, lp, &base); /* Convert base of memory disk */
525*c54f35caSApple OSS Distributions if (ba >= lp) {
526*c54f35caSApple OSS Distributions return; /* Malformed one at the end, leave */
527*c54f35caSApple OSS Distributions }
528*c54f35caSApple OSS Distributions if (ba[0] != '.') {
529*c54f35caSApple OSS Distributions continue; /* If not length separater, try next... */
530*c54f35caSApple OSS Distributions }
531*c54f35caSApple OSS Distributions if (base & 0xFFF) {
532*c54f35caSApple OSS Distributions continue; /* Only allow page aligned stuff */
533*c54f35caSApple OSS Distributions }
534*c54f35caSApple OSS Distributions ba++; /* Step past '.' */
535*c54f35caSApple OSS Distributions ba = cvtnum(ba, lp, &size); /* Try to convert it */
536*c54f35caSApple OSS Distributions if (!size || (size & 0xFFF)) {
537*c54f35caSApple OSS Distributions continue; /* Allow only non-zer page size multiples */
538*c54f35caSApple OSS Distributions }
539*c54f35caSApple OSS Distributions if (ba < lp) { /* If we are not at end, check end character */
540*c54f35caSApple OSS Distributions if ((ba[0] != ' ') && (ba[0] != 0)) {
541*c54f35caSApple OSS Distributions continue; /* End must be null or space */
542*c54f35caSApple OSS Distributions }
543*c54f35caSApple OSS Distributions }
544*c54f35caSApple OSS Distributions
545*c54f35caSApple OSS Distributions dev = mdevadd(devid, base >> 12, (unsigned)size >> 12, phys); /* Go add the device */
546*c54f35caSApple OSS Distributions }
547*c54f35caSApple OSS Distributions
548*c54f35caSApple OSS Distributions #endif /* CONFIG_MEMDEV_INSECURE */
549*c54f35caSApple OSS Distributions
550*c54f35caSApple OSS Distributions return;
551*c54f35caSApple OSS Distributions }
552*c54f35caSApple OSS Distributions
553*c54f35caSApple OSS Distributions #ifdef CONFIG_MEMDEV_INSECURE
554*c54f35caSApple OSS Distributions
555*c54f35caSApple OSS Distributions char *
nonspace(char * pos,char * end)556*c54f35caSApple OSS Distributions nonspace(char *pos, char *end) /* Find next non-space in string */
557*c54f35caSApple OSS Distributions {
558*c54f35caSApple OSS Distributions if (pos >= end) {
559*c54f35caSApple OSS Distributions return end; /* Don't go past end */
560*c54f35caSApple OSS Distributions }
561*c54f35caSApple OSS Distributions if (pos[0] == 0) {
562*c54f35caSApple OSS Distributions return end; /* If at null, make end */
563*c54f35caSApple OSS Distributions }
564*c54f35caSApple OSS Distributions while (1) { /* Keep going */
565*c54f35caSApple OSS Distributions if (pos[0] != ' ') {
566*c54f35caSApple OSS Distributions return pos; /* Leave if we found one */
567*c54f35caSApple OSS Distributions }
568*c54f35caSApple OSS Distributions pos++; /* Stop */
569*c54f35caSApple OSS Distributions if (pos >= end) {
570*c54f35caSApple OSS Distributions return end; /* Quit if we run off end */
571*c54f35caSApple OSS Distributions }
572*c54f35caSApple OSS Distributions }
573*c54f35caSApple OSS Distributions }
574*c54f35caSApple OSS Distributions
575*c54f35caSApple OSS Distributions char *
getspace(char * pos,char * end)576*c54f35caSApple OSS Distributions getspace(char *pos, char *end) /* Find next non-space in string */
577*c54f35caSApple OSS Distributions {
578*c54f35caSApple OSS Distributions while (1) { /* Keep going */
579*c54f35caSApple OSS Distributions if (pos >= end) {
580*c54f35caSApple OSS Distributions return end; /* Don't go past end */
581*c54f35caSApple OSS Distributions }
582*c54f35caSApple OSS Distributions if (pos[0] == 0) {
583*c54f35caSApple OSS Distributions return end; /* Leave if we hit null */
584*c54f35caSApple OSS Distributions }
585*c54f35caSApple OSS Distributions if (pos[0] == ' ') {
586*c54f35caSApple OSS Distributions return pos; /* Leave if we found one */
587*c54f35caSApple OSS Distributions }
588*c54f35caSApple OSS Distributions pos++; /* Stop */
589*c54f35caSApple OSS Distributions }
590*c54f35caSApple OSS Distributions }
591*c54f35caSApple OSS Distributions
592*c54f35caSApple OSS Distributions char *
cvtnum(char * pos,char * end,uint64_t * num)593*c54f35caSApple OSS Distributions cvtnum(char *pos, char *end, uint64_t *num) /* Convert to a number */
594*c54f35caSApple OSS Distributions {
595*c54f35caSApple OSS Distributions int rad, dig;
596*c54f35caSApple OSS Distributions
597*c54f35caSApple OSS Distributions *num = 0; /* Set answer to 0 to start */
598*c54f35caSApple OSS Distributions rad = 10;
599*c54f35caSApple OSS Distributions
600*c54f35caSApple OSS Distributions if (pos >= end) {
601*c54f35caSApple OSS Distributions return end; /* Don't go past end */
602*c54f35caSApple OSS Distributions }
603*c54f35caSApple OSS Distributions if (pos[0] == 0) {
604*c54f35caSApple OSS Distributions return end; /* If at null, make end */
605*c54f35caSApple OSS Distributions }
606*c54f35caSApple OSS Distributions if (pos[0] == '0' && ((pos[1] == 'x') || (pos[1] == 'x'))) { /* A hex constant? */
607*c54f35caSApple OSS Distributions rad = 16;
608*c54f35caSApple OSS Distributions pos += 2; /* Point to the number */
609*c54f35caSApple OSS Distributions }
610*c54f35caSApple OSS Distributions
611*c54f35caSApple OSS Distributions while (1) { /* Convert it */
612*c54f35caSApple OSS Distributions if (pos >= end) {
613*c54f35caSApple OSS Distributions return end; /* Don't go past end */
614*c54f35caSApple OSS Distributions }
615*c54f35caSApple OSS Distributions if (pos[0] == 0) {
616*c54f35caSApple OSS Distributions return end; /* If at null, make end */
617*c54f35caSApple OSS Distributions }
618*c54f35caSApple OSS Distributions if (pos[0] < '0') {
619*c54f35caSApple OSS Distributions return pos; /* Leave if non-digit */
620*c54f35caSApple OSS Distributions }
621*c54f35caSApple OSS Distributions dig = pos[0] & 0xF; /* Extract digit */
622*c54f35caSApple OSS Distributions if (pos[0] > '9') { /* Is it bigger than 9? */
623*c54f35caSApple OSS Distributions if (rad == 10) {
624*c54f35caSApple OSS Distributions return pos; /* Leave if not base 10 */
625*c54f35caSApple OSS Distributions }
626*c54f35caSApple OSS Distributions if (!(((pos[0] >= 'A') && (pos[0] <= 'F'))
627*c54f35caSApple OSS Distributions || ((pos[0] >= 'a') && (pos[0] <= 'f')))) {
628*c54f35caSApple OSS Distributions return pos; /* Leave if bogus char */
629*c54f35caSApple OSS Distributions }
630*c54f35caSApple OSS Distributions dig = dig + 9; /* Adjust for character */
631*c54f35caSApple OSS Distributions }
632*c54f35caSApple OSS Distributions *num = (*num * rad) + dig; /* Accumulate the number */
633*c54f35caSApple OSS Distributions pos++; /* Step on */
634*c54f35caSApple OSS Distributions }
635*c54f35caSApple OSS Distributions }
636*c54f35caSApple OSS Distributions
637*c54f35caSApple OSS Distributions #endif /* CONFIG_MEMDEV_INSECURE */
638*c54f35caSApple OSS Distributions
639*c54f35caSApple OSS Distributions dev_t
mdevadd(int devid,uint64_t base,unsigned int size,int phys)640*c54f35caSApple OSS Distributions mdevadd(int devid, uint64_t base, unsigned int size, int phys)
641*c54f35caSApple OSS Distributions {
642*c54f35caSApple OSS Distributions int i;
643*c54f35caSApple OSS Distributions
644*c54f35caSApple OSS Distributions if (devid < 0) {
645*c54f35caSApple OSS Distributions devid = -1;
646*c54f35caSApple OSS Distributions for (i = 0; i < NB_MAX_MDEVICES; i++) { /* Search all known memory devices */
647*c54f35caSApple OSS Distributions if (!(mdev[i].mdFlags & mdInited)) { /* Is this a free one? */
648*c54f35caSApple OSS Distributions if (devid < 0) {
649*c54f35caSApple OSS Distributions devid = i; /* Remember first free one */
650*c54f35caSApple OSS Distributions }
651*c54f35caSApple OSS Distributions continue; /* Skip check */
652*c54f35caSApple OSS Distributions }
653*c54f35caSApple OSS Distributions if (!(((base + size - 1) < mdev[i].mdBase) || ((mdev[i].mdBase + mdev[i].mdSize - 1) < base))) { /* Is there any overlap? */
654*c54f35caSApple OSS Distributions panic("mdevadd: attempt to add overlapping memory device at %016llX-%016llX", mdev[i].mdBase, mdev[i].mdBase + mdev[i].mdSize - 1);
655*c54f35caSApple OSS Distributions }
656*c54f35caSApple OSS Distributions }
657*c54f35caSApple OSS Distributions if (devid < 0) { /* Do we have free slots? */
658*c54f35caSApple OSS Distributions panic("mdevadd: attempt to add more than %d memory devices", NB_MAX_MDEVICES);
659*c54f35caSApple OSS Distributions }
660*c54f35caSApple OSS Distributions } else {
661*c54f35caSApple OSS Distributions if (devid >= NB_MAX_MDEVICES) { /* Giving us something bogus? */
662*c54f35caSApple OSS Distributions panic("mdevadd: attempt to explicitly add a bogus memory device: %08X", devid);
663*c54f35caSApple OSS Distributions }
664*c54f35caSApple OSS Distributions if (mdev[devid].mdFlags & mdInited) { /* Already there? */
665*c54f35caSApple OSS Distributions panic("mdevadd: attempt to explicitly add a previously defined memory device: %08X", devid);
666*c54f35caSApple OSS Distributions }
667*c54f35caSApple OSS Distributions }
668*c54f35caSApple OSS Distributions
669*c54f35caSApple OSS Distributions if (mdevBMajor < 0) { /* Have we gotten a major number yet? */
670*c54f35caSApple OSS Distributions mdevBMajor = bdevsw_add(-1, &mdevbdevsw); /* Add to the table and figure out a major number */
671*c54f35caSApple OSS Distributions if (mdevBMajor < 0) {
672*c54f35caSApple OSS Distributions printf("mdevadd: error - bdevsw_add() returned %d\n", mdevBMajor);
673*c54f35caSApple OSS Distributions return -1;
674*c54f35caSApple OSS Distributions }
675*c54f35caSApple OSS Distributions }
676*c54f35caSApple OSS Distributions
677*c54f35caSApple OSS Distributions if (mdevCMajor < 0) { /* Have we gotten a major number yet? */
678*c54f35caSApple OSS Distributions mdevCMajor = cdevsw_add_with_bdev(-1, &mdevcdevsw, mdevBMajor); /* Add to the table and figure out a major number */
679*c54f35caSApple OSS Distributions if (mdevCMajor < 0) {
680*c54f35caSApple OSS Distributions printf("ramdevice_init: error - cdevsw_add() returned %d\n", mdevCMajor);
681*c54f35caSApple OSS Distributions return -1;
682*c54f35caSApple OSS Distributions }
683*c54f35caSApple OSS Distributions }
684*c54f35caSApple OSS Distributions
685*c54f35caSApple OSS Distributions mdev[devid].mdBDev = makedev(mdevBMajor, devid); /* Get the device number */
686*c54f35caSApple OSS Distributions mdev[devid].mdbdevb = devfs_make_node(mdev[devid].mdBDev, DEVFS_BLOCK, /* Make the node */
687*c54f35caSApple OSS Distributions UID_ROOT, GID_OPERATOR,
688*c54f35caSApple OSS Distributions 0600, "md%d", devid);
689*c54f35caSApple OSS Distributions if (mdev[devid].mdbdevb == NULL) { /* Did we make one? */
690*c54f35caSApple OSS Distributions printf("mdevadd: devfs_make_node for block failed!\n");
691*c54f35caSApple OSS Distributions return -1; /* Nope... */
692*c54f35caSApple OSS Distributions }
693*c54f35caSApple OSS Distributions
694*c54f35caSApple OSS Distributions mdev[devid].mdCDev = makedev(mdevCMajor, devid); /* Get the device number */
695*c54f35caSApple OSS Distributions mdev[devid].mdcdevb = devfs_make_node(mdev[devid].mdCDev, DEVFS_CHAR, /* Make the node */
696*c54f35caSApple OSS Distributions UID_ROOT, GID_OPERATOR,
697*c54f35caSApple OSS Distributions 0600, "rmd%d", devid);
698*c54f35caSApple OSS Distributions if (mdev[devid].mdcdevb == NULL) { /* Did we make one? */
699*c54f35caSApple OSS Distributions printf("mdevadd: devfs_make_node for character failed!\n");
700*c54f35caSApple OSS Distributions return -1; /* Nope... */
701*c54f35caSApple OSS Distributions }
702*c54f35caSApple OSS Distributions
703*c54f35caSApple OSS Distributions mdev[devid].mdBase = base; /* Set the base address of ram disk */
704*c54f35caSApple OSS Distributions mdev[devid].mdSize = size; /* Set the length of the ram disk */
705*c54f35caSApple OSS Distributions mdev[devid].mdSecsize = DEV_BSIZE; /* Set starting block size */
706*c54f35caSApple OSS Distributions if (phys) {
707*c54f35caSApple OSS Distributions mdev[devid].mdFlags |= mdPhys; /* Show that we are in physical memory */
708*c54f35caSApple OSS Distributions }
709*c54f35caSApple OSS Distributions mdev[devid].mdFlags |= mdInited; /* Show we are all set up */
710*c54f35caSApple OSS Distributions printf("Added memory device md%x/rmd%x (%08X/%08X) at %016llX for %016llX\n",
711*c54f35caSApple OSS Distributions devid, devid, mdev[devid].mdBDev, mdev[devid].mdCDev, base << 12, (uint64_t)size << 12);
712*c54f35caSApple OSS Distributions return mdev[devid].mdBDev;
713*c54f35caSApple OSS Distributions }
714*c54f35caSApple OSS Distributions
715*c54f35caSApple OSS Distributions
716*c54f35caSApple OSS Distributions dev_t
mdevlookup(int devid)717*c54f35caSApple OSS Distributions mdevlookup(int devid)
718*c54f35caSApple OSS Distributions {
719*c54f35caSApple OSS Distributions if ((devid < 0) || (devid >= NB_MAX_MDEVICES)) {
720*c54f35caSApple OSS Distributions return -1; /* Filter any bogus requests */
721*c54f35caSApple OSS Distributions }
722*c54f35caSApple OSS Distributions if (!(mdev[devid].mdFlags & mdInited)) {
723*c54f35caSApple OSS Distributions return -1; /* This one hasn't been defined */
724*c54f35caSApple OSS Distributions }
725*c54f35caSApple OSS Distributions return mdev[devid].mdBDev; /* Return the device number */
726*c54f35caSApple OSS Distributions }
727*c54f35caSApple OSS Distributions
728*c54f35caSApple OSS Distributions void
mdevremoveall(void)729*c54f35caSApple OSS Distributions mdevremoveall(void)
730*c54f35caSApple OSS Distributions {
731*c54f35caSApple OSS Distributions int i;
732*c54f35caSApple OSS Distributions
733*c54f35caSApple OSS Distributions for (i = 0; i < NB_MAX_MDEVICES; i++) {
734*c54f35caSApple OSS Distributions if (!(mdev[i].mdFlags & mdInited)) {
735*c54f35caSApple OSS Distributions continue; /* Ignore unused mdevs */
736*c54f35caSApple OSS Distributions }
737*c54f35caSApple OSS Distributions devfs_remove(mdev[i].mdbdevb); /* Remove the block device */
738*c54f35caSApple OSS Distributions devfs_remove(mdev[i].mdcdevb); /* Remove the character device */
739*c54f35caSApple OSS Distributions
740*c54f35caSApple OSS Distributions mdev[i].mdBase = 0; /* Clear the mdev's storage */
741*c54f35caSApple OSS Distributions mdev[i].mdSize = 0;
742*c54f35caSApple OSS Distributions mdev[i].mdSecsize = 0;
743*c54f35caSApple OSS Distributions mdev[i].mdFlags = 0;
744*c54f35caSApple OSS Distributions mdev[i].mdBDev = 0;
745*c54f35caSApple OSS Distributions mdev[i].mdCDev = 0;
746*c54f35caSApple OSS Distributions mdev[i].mdbdevb = 0;
747*c54f35caSApple OSS Distributions mdev[i].mdcdevb = 0;
748*c54f35caSApple OSS Distributions }
749*c54f35caSApple OSS Distributions }
750*c54f35caSApple OSS Distributions
751*c54f35caSApple OSS Distributions int
mdevgetrange(int devid,uint64_t * base,uint64_t * size)752*c54f35caSApple OSS Distributions mdevgetrange(int devid, uint64_t *base, uint64_t *size)
753*c54f35caSApple OSS Distributions {
754*c54f35caSApple OSS Distributions assert(base);
755*c54f35caSApple OSS Distributions assert(size);
756*c54f35caSApple OSS Distributions
757*c54f35caSApple OSS Distributions /* filter invalid request */
758*c54f35caSApple OSS Distributions if ((devid < 0) || (devid >= NB_MAX_MDEVICES)) {
759*c54f35caSApple OSS Distributions return -1;
760*c54f35caSApple OSS Distributions }
761*c54f35caSApple OSS Distributions
762*c54f35caSApple OSS Distributions /* filter non-initialized memory devices */
763*c54f35caSApple OSS Distributions if ((mdev[devid].mdFlags & mdInited) == 0) {
764*c54f35caSApple OSS Distributions return -1;
765*c54f35caSApple OSS Distributions }
766*c54f35caSApple OSS Distributions
767*c54f35caSApple OSS Distributions *base = mdev[devid].mdBase << 12;
768*c54f35caSApple OSS Distributions *size = mdev[devid].mdSize << 12;
769*c54f35caSApple OSS Distributions
770*c54f35caSApple OSS Distributions /* make sure (base, size) is a valid range and will not overflow */
771*c54f35caSApple OSS Distributions assert(*size < (UINT64_MAX - *base));
772*c54f35caSApple OSS Distributions
773*c54f35caSApple OSS Distributions return 0;
774*c54f35caSApple OSS Distributions }
775