2 * Copyright (c) 2004-2006 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1988 University of Utah.
30 * Copyright (c) 1990, 1993
31 * The Regents of the University of California. All rights reserved.
33 * This code is derived from software contributed to Berkeley by
34 * the Systems Programming Group of the University of Utah Computer
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the University of
48 * California, Berkeley and its contributors.
49 * 4. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * from: Utah Hdr: vn.c 1.13 94/04/02
67 * from: @(#)vn.c 8.6 (Berkeley) 4/1/94
68 * $FreeBSD: src/sys/dev/vn/vn.c,v 1.105.2.4 2001/11/18 07:11:00 dillon Exp $
74 * Block interface to a ramdisk.
78 #include <sys/param.h>
79 #include <sys/kernel.h>
80 #include <sys/mount.h>
81 #include <sys/namei.h>
84 #include <sys/malloc.h>
85 #include <sys/mount.h>
86 #include <sys/fcntl.h>
91 #include <sys/uio_internal.h>
92 #include <libkern/libkern.h>
95 #include <vm/vm_pager.h>
96 #include <mach/memory_object_types.h>
97 #include <kern/debug.h>
99 #include <miscfs/devfs/devfs.h>
102 void mdevinit(int the_cnt
);
104 static open_close_fcn_t mdevopen
;
105 static open_close_fcn_t mdevclose
;
106 static psize_fcn_t mdevsize
;
107 static strategy_fcn_t mdevstrategy
;
108 static int mdevbioctl(dev_t dev
, u_long cmd
, caddr_t data
, int flag
, struct proc
*p
);
109 static int mdevcioctl(dev_t dev
, u_long cmd
, caddr_t data
, int flag
, struct proc
*p
);
110 static int mdevrw(dev_t dev
, struct uio
*uio
, int ioflag
);
112 #ifdef CONFIG_MEMDEV_INSECURE
113 static char * nonspace(char *pos
, char *end
);
114 static char * getspace(char *pos
, char *end
);
115 static char * cvtnum(char *pos
, char *end
, uint64_t *num
);
116 #endif /* CONFIG_MEMDEV_INSECURE */
118 extern void bcopy_phys(addr64_t from
, addr64_t to
, vm_size_t bytes
);
119 extern void mapping_set_mod(ppnum_t pn
);
120 extern ppnum_t
pmap_find_phys(pmap_t pmap
, addr64_t va
);
123 * Maximal number of memory devices.
125 #define NB_MAX_MDEVICES (16)
129 * D_DISK we want to look like a disk
130 * D_CANFREE We support B_FREEBUF
133 static struct bdevsw mdevbdevsw
= {
135 /* close */ mdevclose
,
136 /* strategy */ mdevstrategy
,
137 /* ioctl */ mdevbioctl
,
139 /* psize */ mdevsize
,
143 static struct cdevsw mdevcdevsw
= {
145 /* close */ mdevclose
,
148 /* ioctl */ mdevcioctl
,
150 /* reset */ eno_reset
,
152 /* select */ eno_select
,
154 /* strategy */ eno_strat
,
161 uint64_t mdBase
; /* file size in bytes */
162 uint32_t mdSize
; /* file size in bytes */
163 int mdFlags
; /* flags */
164 int mdSecsize
; /* sector size */
165 int mdBDev
; /* Block device number */
166 int mdCDev
; /* Character device number */
169 } mdev
[NB_MAX_MDEVICES
];
172 #define mdInited 0x01 /* This device defined */
173 #define mdRO 0x02 /* This device is read-only */
174 #define mdPhys 0x04 /* This device is in physical memory */
179 static int mdevioctl(dev_t dev
, u_long cmd
, caddr_t data
, int flag
, struct proc
*p
, int is_char
);
180 dev_t
mdevadd(int devid
, uint64_t base
, unsigned int size
, int phys
);
181 dev_t
mdevlookup(int devid
);
182 void mdevremoveall(void);
183 int mdevgetrange(int devid
, uint64_t *base
, uint64_t *size
);
185 static int mdevclose(__unused dev_t dev
, __unused
int flags
,
186 __unused
int devtype
, __unused
struct proc
*p
) {
191 static int mdevopen(dev_t dev
, int flags
, __unused
int devtype
, __unused
struct proc
*p
) {
195 devid
= minor(dev
); /* Get minor device number */
197 if (devid
>= NB_MAX_MDEVICES
) return (ENXIO
); /* Not valid */
199 if ((flags
& FWRITE
) && (mdev
[devid
].mdFlags
& mdRO
)) return (EACCES
); /* Currently mounted RO */
204 static int mdevrw(dev_t dev
, struct uio
*uio
, __unused
int ioflag
) {
208 enum uio_seg saveflag
;
210 devid
= minor(dev
); /* Get minor device number */
212 if (devid
>= NB_MAX_MDEVICES
) return (ENXIO
); /* Not valid */
213 if (!(mdev
[devid
].mdFlags
& mdInited
)) return (ENXIO
); /* Have we actually been defined yet? */
215 mdata
= ((addr64_t
)mdev
[devid
].mdBase
<< 12) + uio
->uio_offset
; /* Point to the area in "file" */
217 saveflag
= uio
->uio_segflg
; /* Remember what the request is */
219 if (UIO_IS_USER_SPACE(uio
) == 0 && UIO_IS_SYS_SPACE(uio
) == 0) {
220 panic("mdevrw - invalid uio_segflg\n");
222 #endif /* LP64_DEBUG */
223 /* Make sure we are moving from physical ram if physical device */
224 if (mdev
[devid
].mdFlags
& mdPhys
) {
225 if (uio
->uio_segflg
== UIO_USERSPACE64
)
226 uio
->uio_segflg
= UIO_PHYS_USERSPACE64
;
227 else if (uio
->uio_segflg
== UIO_USERSPACE32
)
228 uio
->uio_segflg
= UIO_PHYS_USERSPACE32
;
230 uio
->uio_segflg
= UIO_PHYS_USERSPACE
;
232 status
= uiomove64(mdata
, uio_resid(uio
), uio
); /* Move the data */
233 uio
->uio_segflg
= saveflag
; /* Restore the flag */
238 static void mdevstrategy(struct buf
*bp
) {
239 unsigned int left
, lop
, csize
;
240 vm_offset_t vaddr
, blkoff
;
242 addr64_t paddr
, fvaddr
;
245 devid
= minor(buf_device(bp
)); /* Get minor device number */
247 if ((mdev
[devid
].mdFlags
& mdInited
) == 0) { /* Have we actually been defined yet? */
248 buf_seterror(bp
, ENXIO
);
253 buf_setresid(bp
, buf_count(bp
)); /* Set byte count */
255 blkoff
= buf_blkno(bp
) * mdev
[devid
].mdSecsize
; /* Get offset into file */
258 * Note that reading past end is an error, but reading at end is an EOF. For these
259 * we just return with resid == count.
262 if (blkoff
>= (mdev
[devid
].mdSize
<< 12)) { /* Are they trying to read/write at/after end? */
263 if(blkoff
!= (mdev
[devid
].mdSize
<< 12)) { /* Are we trying to read after EOF? */
264 buf_seterror(bp
, EINVAL
); /* Yeah, this is an error */
266 buf_biodone(bp
); /* Return */
270 if ((blkoff
+ buf_count(bp
)) > (mdev
[devid
].mdSize
<< 12)) { /* Will this read go past end? */
271 buf_setcount(bp
, ((mdev
[devid
].mdSize
<< 12) - blkoff
)); /* Yes, trim to max */
274 * make sure the buffer's data area is
277 if (buf_map(bp
, (caddr_t
*)&vaddr
))
278 panic("ramstrategy: buf_map failed\n");
280 fvaddr
= (mdev
[devid
].mdBase
<< 12) + blkoff
; /* Point to offset into ram disk */
282 if (buf_flags(bp
) & B_READ
) { /* Is this a read? */
283 if(!(mdev
[devid
].mdFlags
& mdPhys
)) { /* Physical mapped disk? */
284 bcopy((void *)((uintptr_t)fvaddr
),
285 (void *)vaddr
, (size_t)buf_count(bp
)); /* This is virtual, just get the data */
288 left
= buf_count(bp
); /* Init the amount left to copy */
289 while(left
) { /* Go until it is all copied */
291 lop
= min((4096 - (vaddr
& 4095)), (4096 - (fvaddr
& 4095))); /* Get smallest amount left on sink and source */
292 csize
= min(lop
, left
); /* Don't move more than we need to */
294 pp
= pmap_find_phys(kernel_pmap
, (addr64_t
)((uintptr_t)vaddr
)); /* Get the sink physical address */
295 if(!pp
) { /* Not found, what gives? */
296 panic("mdevstrategy: sink address %016llX not mapped\n", (addr64_t
)((uintptr_t)vaddr
));
298 paddr
= (addr64_t
)(((addr64_t
)pp
<< 12) | (addr64_t
)(vaddr
& 4095)); /* Get actual address */
299 bcopy_phys(fvaddr
, paddr
, csize
); /* Copy this on in */
300 mapping_set_mod(paddr
>> 12); /* Make sure we know that it is modified */
302 left
= left
- csize
; /* Calculate what is left */
303 vaddr
= vaddr
+ csize
; /* Move to next sink address */
304 fvaddr
= fvaddr
+ csize
; /* Bump to next physical address */
308 else { /* This is a write */
309 if(!(mdev
[devid
].mdFlags
& mdPhys
)) { /* Physical mapped disk? */
310 bcopy((void *)vaddr
, (void *)((uintptr_t)fvaddr
),
311 (size_t)buf_count(bp
)); /* This is virtual, just put the data */
314 left
= buf_count(bp
); /* Init the amount left to copy */
315 while(left
) { /* Go until it is all copied */
317 lop
= min((4096 - (vaddr
& 4095)), (4096 - (fvaddr
& 4095))); /* Get smallest amount left on sink and source */
318 csize
= min(lop
, left
); /* Don't move more than we need to */
320 pp
= pmap_find_phys(kernel_pmap
, (addr64_t
)((uintptr_t)vaddr
)); /* Get the source physical address */
321 if(!pp
) { /* Not found, what gives? */
322 panic("mdevstrategy: source address %016llX not mapped\n", (addr64_t
)((uintptr_t)vaddr
));
324 paddr
= (addr64_t
)(((addr64_t
)pp
<< 12) | (addr64_t
)(vaddr
& 4095)); /* Get actual address */
326 bcopy_phys(paddr
, fvaddr
, csize
); /* Move this on out */
328 left
= left
- csize
; /* Calculate what is left */
329 vaddr
= vaddr
+ csize
; /* Move to next sink address */
330 fvaddr
= fvaddr
+ csize
; /* Bump to next physical address */
335 * buf_unmap takes care of all the cases
336 * it will unmap the buffer from kernel
337 * virtual space if that was the state
342 buf_setresid(bp
, 0); /* Nothing more to do */
343 buf_biodone(bp
); /* Say we've finished */
346 static int mdevbioctl(dev_t dev
, u_long cmd
, caddr_t data
, int flag
, struct proc
*p
) {
347 return (mdevioctl(dev
, cmd
, data
, flag
, p
, 0));
350 static int mdevcioctl(dev_t dev
, u_long cmd
, caddr_t data
, int flag
, struct proc
*p
) {
351 return (mdevioctl(dev
, cmd
, data
, flag
, p
, 1));
354 static int mdevioctl(dev_t dev
, u_long cmd
, caddr_t data
, __unused
int flag
,
355 struct proc
*p
, int is_char
) {
360 dk_memdev_info_t
* memdev_info
;
362 devid
= minor(dev
); /* Get minor device number */
364 if (devid
>= NB_MAX_MDEVICES
) return (ENXIO
); /* Not valid */
366 error
= proc_suser(p
); /* Are we superman? */
367 if (error
) return (error
); /* Nope... */
369 f
= (u_int32_t
*)data
;
370 o
= (u_int64_t
*)data
;
371 memdev_info
= (dk_memdev_info_t
*) data
;
375 case DKIOCGETMAXBLOCKCOUNTREAD
:
379 case DKIOCGETMAXBLOCKCOUNTWRITE
:
383 case DKIOCGETMAXSEGMENTCOUNTREAD
:
387 case DKIOCGETMAXSEGMENTCOUNTWRITE
:
391 case DKIOCGETBLOCKSIZE
:
392 *f
= mdev
[devid
].mdSecsize
;
395 case DKIOCSETBLOCKSIZE
:
396 if (is_char
) return (ENODEV
); /* We can only do this for a block */
398 if (*f
< DEV_BSIZE
) return (EINVAL
); /* Too short? */
400 mdev
[devid
].mdSecsize
= *f
; /* set the new block size */
403 case DKIOCISWRITABLE
:
407 case DKIOCGETBLOCKCOUNT
:
408 if(!(mdev
[devid
].mdFlags
& mdInited
)) return (ENXIO
);
409 *o
= ((mdev
[devid
].mdSize
<< 12) + mdev
[devid
].mdSecsize
- 1) / mdev
[devid
].mdSecsize
;
413 * We're interested in the following bits of information:
414 * Are you a memory-backed device (always yes, in this case)?
415 * Physical memory (mdPhys)?
416 * What is your base page?
419 case DKIOCGETMEMDEVINFO
:
420 if (!(mdev
[devid
].mdFlags
& mdInited
)) return (ENXIO
);
421 memdev_info
->mi_mdev
= TRUE
;
422 memdev_info
->mi_phys
= (mdev
[devid
].mdFlags
& mdPhys
) ? TRUE
: FALSE
;
423 memdev_info
->mi_base
= mdev
[devid
].mdBase
;
424 memdev_info
->mi_size
= mdev
[devid
].mdSize
;
435 static int mdevsize(dev_t dev
) {
439 devid
= minor(dev
); /* Get minor device number */
440 if (devid
>= NB_MAX_MDEVICES
) return (ENXIO
); /* Not valid */
442 if ((mdev
[devid
].mdFlags
& mdInited
) == 0) return(-1); /* Not inited yet */
444 return(mdev
[devid
].mdSecsize
);
447 #include <pexpert/pexpert.h>
449 void mdevinit(__unused
int the_cnt
) {
451 #ifdef CONFIG_MEMDEV_INSECURE
460 ba
= PE_boot_args(); /* Get the boot arguments */
461 lp
= ba
+ 256; /* Point to the end */
463 while(1) { /* Step through, looking for our keywords */
464 phys
= 0; /* Assume virtual memory device */
465 ba
= nonspace(ba
, lp
); /* Find non-space */
466 if(ba
>= lp
) return; /* We are done if no more... */
467 if(((ba
[0] != 'v') && (ba
[0] != 'p'))
468 || (ba
[1] != 'm') || (ba
[2] != 'd') || (ba
[4] != '=')
469 || (ba
[3] < '0') || (ba
[3] > 'f')
470 || ((ba
[3] > '9') && (ba
[3] < 'a'))) { /* Is this of form "vmdx=" or "pmdx=" where x is hex digit? */
472 ba
= getspace(ba
, lp
); /* Find next white space or end */
473 continue; /* Start looking for the next one */
476 if(ba
[0] == 'p') phys
= 1; /* Set physical memory disk */
478 devid
= ba
[3] & 0xF; /* Assume digit */
479 if(ba
[3] > '9') devid
+= 9; /* Adjust for hex digits */
481 ba
= &ba
[5]; /* Step past keyword */
482 ba
= cvtnum(ba
, lp
, &base
); /* Convert base of memory disk */
483 if(ba
>= lp
) return; /* Malformed one at the end, leave */
484 if(ba
[0] != '.') continue; /* If not length separater, try next... */
485 if(base
& 0xFFF) continue; /* Only allow page aligned stuff */
487 ba
++; /* Step past '.' */
488 ba
= cvtnum(ba
, lp
, &size
); /* Try to convert it */
489 if(!size
|| (size
& 0xFFF)) continue; /* Allow only non-zer page size multiples */
490 if(ba
< lp
) { /* If we are not at end, check end character */
491 if((ba
[0] != ' ') && (ba
[0] != 0)) continue; /* End must be null or space */
494 dev
= mdevadd(devid
, base
>> 12, (unsigned)size
>> 12, phys
); /* Go add the device */
497 #endif /* CONFIG_MEMDEV_INSECURE */
503 #ifdef CONFIG_MEMDEV_INSECURE
505 char *nonspace(char *pos
, char *end
) { /* Find next non-space in string */
507 if(pos
>= end
) return end
; /* Don't go past end */
508 if(pos
[0] == 0) return end
; /* If at null, make end */
510 while(1) { /* Keep going */
511 if(pos
[0] != ' ') return pos
; /* Leave if we found one */
513 if(pos
>= end
) return end
; /* Quit if we run off end */
517 char *getspace(char *pos
, char *end
) { /* Find next non-space in string */
519 while(1) { /* Keep going */
520 if(pos
>= end
) return end
; /* Don't go past end */
521 if(pos
[0] == 0) return end
; /* Leave if we hit null */
522 if(pos
[0] == ' ') return pos
; /* Leave if we found one */
527 char *cvtnum(char *pos
, char *end
, uint64_t *num
) { /* Convert to a number */
531 *num
= 0; /* Set answer to 0 to start */
534 if(pos
>= end
) return end
; /* Don't go past end */
535 if(pos
[0] == 0) return end
; /* If at null, make end */
537 if(pos
[0] == '0' && ((pos
[1] == 'x') || (pos
[1] == 'x'))) { /* A hex constant? */
539 pos
+= 2; /* Point to the number */
542 while(1) { /* Convert it */
544 if(pos
>= end
) return end
; /* Don't go past end */
545 if(pos
[0] == 0) return end
; /* If at null, make end */
546 if(pos
[0] < '0') return pos
; /* Leave if non-digit */
547 dig
= pos
[0] & 0xF; /* Extract digit */
548 if(pos
[0] > '9') { /* Is it bigger than 9? */
549 if(rad
== 10) return pos
; /* Leave if not base 10 */
550 if(!(((pos
[0] >= 'A') && (pos
[0] <= 'F'))
551 || ((pos
[0] >= 'a') && (pos
[0] <= 'f')))) return pos
; /* Leave if bogus char */
552 dig
= dig
+ 9; /* Adjust for character */
554 *num
= (*num
* rad
) + dig
; /* Accumulate the number */
559 #endif /* CONFIG_MEMDEV_INSECURE */
561 dev_t
mdevadd(int devid
, uint64_t base
, unsigned int size
, int phys
) {
568 for(i
= 0; i
< NB_MAX_MDEVICES
; i
++) { /* Search all known memory devices */
569 if(!(mdev
[i
].mdFlags
& mdInited
)) { /* Is this a free one? */
570 if(devid
< 0)devid
= i
; /* Remember first free one */
571 continue; /* Skip check */
573 if(!(((base
+ size
-1 ) < mdev
[i
].mdBase
) || ((mdev
[i
].mdBase
+ mdev
[i
].mdSize
- 1) < base
))) { /* Is there any overlap? */
574 panic("mdevadd: attempt to add overlapping memory device at %016llX-%016llX\n", mdev
[i
].mdBase
, mdev
[i
].mdBase
+ mdev
[i
].mdSize
- 1);
577 if(devid
< 0) { /* Do we have free slots? */
578 panic("mdevadd: attempt to add more than %d memory devices\n", NB_MAX_MDEVICES
);
582 if(devid
>= NB_MAX_MDEVICES
) { /* Giving us something bogus? */
583 panic("mdevadd: attempt to explicitly add a bogus memory device: %08X\n", devid
);
585 if(mdev
[devid
].mdFlags
& mdInited
) { /* Already there? */
586 panic("mdevadd: attempt to explicitly add a previously defined memory device: %08X\n", devid
);
590 if(mdevBMajor
< 0) { /* Have we gotten a major number yet? */
591 mdevBMajor
= bdevsw_add(-1, &mdevbdevsw
); /* Add to the table and figure out a major number */
592 if (mdevBMajor
< 0) {
593 printf("mdevadd: error - bdevsw_add() returned %d\n", mdevBMajor
);
598 if(mdevCMajor
< 0) { /* Have we gotten a major number yet? */
599 mdevCMajor
= cdevsw_add_with_bdev(-1, &mdevcdevsw
, mdevBMajor
); /* Add to the table and figure out a major number */
600 if (mdevCMajor
< 0) {
601 printf("ramdevice_init: error - cdevsw_add() returned %d\n", mdevCMajor
);
606 mdev
[devid
].mdBDev
= makedev(mdevBMajor
, devid
); /* Get the device number */
607 mdev
[devid
].mdbdevb
= devfs_make_node(mdev
[devid
].mdBDev
, DEVFS_BLOCK
, /* Make the node */
608 UID_ROOT
, GID_OPERATOR
,
609 0600, "md%d", devid
);
610 if (mdev
[devid
].mdbdevb
== NULL
) { /* Did we make one? */
611 printf("mdevadd: devfs_make_node for block failed!\n");
612 return -1; /* Nope... */
615 mdev
[devid
].mdCDev
= makedev(mdevCMajor
, devid
); /* Get the device number */
616 mdev
[devid
].mdcdevb
= devfs_make_node(mdev
[devid
].mdCDev
, DEVFS_CHAR
, /* Make the node */
617 UID_ROOT
, GID_OPERATOR
,
618 0600, "rmd%d", devid
);
619 if (mdev
[devid
].mdcdevb
== NULL
) { /* Did we make one? */
620 printf("mdevadd: devfs_make_node for character failed!\n");
621 return -1; /* Nope... */
624 mdev
[devid
].mdBase
= base
; /* Set the base address of ram disk */
625 mdev
[devid
].mdSize
= size
; /* Set the length of the ram disk */
626 mdev
[devid
].mdSecsize
= DEV_BSIZE
; /* Set starting block size */
627 if(phys
) mdev
[devid
].mdFlags
|= mdPhys
; /* Show that we are in physical memory */
628 mdev
[devid
].mdFlags
|= mdInited
; /* Show we are all set up */
629 printf("Added memory device md%x/rmd%x (%08X/%08X) at %016llX for %016llX\n",
630 devid
, devid
, mdev
[devid
].mdBDev
, mdev
[devid
].mdCDev
, base
<< 12, (uint64_t)size
<< 12);
631 return mdev
[devid
].mdBDev
;
635 dev_t
mdevlookup(int devid
) {
637 if((devid
< 0) || (devid
>= NB_MAX_MDEVICES
)) return -1; /* Filter any bogus requests */
638 if(!(mdev
[devid
].mdFlags
& mdInited
)) return -1; /* This one hasn't been defined */
639 return mdev
[devid
].mdBDev
; /* Return the device number */
642 void mdevremoveall(void) {
646 for(i
= 0; i
< NB_MAX_MDEVICES
; i
++) {
647 if(!(mdev
[i
].mdFlags
& mdInited
)) continue; /* Ignore unused mdevs */
649 devfs_remove(mdev
[i
].mdbdevb
); /* Remove the block device */
650 devfs_remove(mdev
[i
].mdcdevb
); /* Remove the character device */
652 mdev
[i
].mdBase
= 0; /* Clear the mdev's storage */
654 mdev
[i
].mdSecsize
= 0;
664 mdevgetrange(int devid
, uint64_t *base
, uint64_t *size
)
669 /* filter invalid request */
670 if ((devid
< 0) || (devid
>= NB_MAX_MDEVICES
)) {
674 /* filter non-initialized memory devices */
675 if ((mdev
[devid
].mdFlags
& mdInited
) == 0) {
679 *base
= mdev
[devid
].mdBase
<< 12;
680 *size
= mdev
[devid
].mdSize
<< 12;
682 /* make sure (base, size) is a valid range and will not overflow */
683 assert(*size
< (UINT64_MAX
- *base
));