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