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
102 void mdevinit(int the_cnt);
103
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);
111
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 */
117
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);
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
133 static 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
143 static 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
160 struct mdev {
161 uint64_t mdBase; /* base page number (pages are assumed to be 4K). Multiply by 4096 to find actual address */
162 uint32_t mdSize; /* size in pages (pages are assumed to be 4K). Multiply by 4096 to find actual size. */
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
176 int mdevBMajor = -1;
177 int mdevCMajor = -1;
178
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);
184
185 static int
mdevclose(__unused dev_t dev,__unused int flags,__unused int devtype,__unused struct proc * p)186 mdevclose(__unused dev_t dev, __unused int flags,
187 __unused int devtype, __unused struct proc *p)
188 {
189 return 0;
190 }
191
192 static int
mdevopen(dev_t dev,int flags,__unused int devtype,__unused struct proc * p)193 mdevopen(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
208 static int
mdevrw(dev_t dev,struct uio * uio,__unused int ioflag)209 mdevrw(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 int count;
216
217 devid = minor(dev); /* Get minor device number */
218
219 if (devid >= NB_MAX_MDEVICES) {
220 return ENXIO; /* Not valid */
221 }
222 if (!(mdev[devid].mdFlags & mdInited)) {
223 return ENXIO; /* Have we actually been defined yet? */
224 }
225 if (uio->uio_offset < 0) {
226 return EINVAL; /* invalid offset */
227 }
228 if (uio_resid(uio) < 0) {
229 return EINVAL;
230 }
231 mdata = ((addr64_t)mdev[devid].mdBase << 12) + uio->uio_offset; /* Point to the area in "file" */
232
233 saveflag = uio->uio_segflg; /* Remember what the request is */
234 #if LP64_DEBUG
235 if (UIO_IS_USER_SPACE(uio) == 0 && UIO_IS_SYS_SPACE(uio) == 0) {
236 panic("mdevrw - invalid uio_segflg");
237 }
238 #endif /* LP64_DEBUG */
239 /* Make sure we are moving from physical ram if physical device */
240 if (mdev[devid].mdFlags & mdPhys) {
241 if (uio->uio_segflg == UIO_USERSPACE64) {
242 uio->uio_segflg = UIO_PHYS_USERSPACE64;
243 } else if (uio->uio_segflg == UIO_USERSPACE32) {
244 uio->uio_segflg = UIO_PHYS_USERSPACE32;
245 } else {
246 uio->uio_segflg = UIO_PHYS_USERSPACE;
247 }
248 }
249
250 if (uio->uio_offset > (mdev[devid].mdSize << 12)) {
251 count = 0;
252 } else {
253 count = imin(uio_resid(uio), (mdev[devid].mdSize << 12) - uio->uio_offset);
254 }
255
256 status = uiomove64(mdata, count, uio); /* Move the data */
257 uio->uio_segflg = saveflag; /* Restore the flag */
258
259 return status;
260 }
261
262 static void
mdevstrategy(struct buf * bp)263 mdevstrategy(struct buf *bp)
264 {
265 unsigned int left, lop, csize;
266 vm_offset_t vaddr, blkoff;
267 int devid;
268 addr64_t paddr, fvaddr;
269 ppnum_t pp;
270
271 devid = minor(buf_device(bp)); /* Get minor device number */
272
273 if ((mdev[devid].mdFlags & mdInited) == 0) { /* Have we actually been defined yet? */
274 buf_seterror(bp, ENXIO);
275 buf_biodone(bp);
276 return;
277 }
278
279 buf_setresid(bp, buf_count(bp)); /* Set byte count */
280
281 blkoff = buf_blkno(bp) * mdev[devid].mdSecsize; /* Get offset into file */
282
283 /*
284 * Note that reading past end is an error, but reading at end is an EOF. For these
285 * we just return with resid == count.
286 */
287
288 if (blkoff >= (mdev[devid].mdSize << 12)) { /* Are they trying to read/write at/after end? */
289 if (blkoff != (mdev[devid].mdSize << 12)) { /* Are we trying to read after EOF? */
290 buf_seterror(bp, EINVAL); /* Yeah, this is an error */
291 }
292 buf_biodone(bp); /* Return */
293 return;
294 }
295
296 if ((blkoff + buf_count(bp)) > (mdev[devid].mdSize << 12)) { /* Will this read go past end? */
297 buf_setcount(bp, (uint32_t)((mdev[devid].mdSize << 12) - blkoff)); /* Yes, trim to max */
298 }
299 /*
300 * make sure the buffer's data area is
301 * accessible
302 */
303 if (buf_map(bp, (caddr_t *)&vaddr)) {
304 panic("ramstrategy: buf_map failed");
305 }
306
307 fvaddr = (mdev[devid].mdBase << 12) + blkoff; /* Point to offset into ram disk */
308
309 if (buf_flags(bp) & B_READ) { /* Is this a read? */
310 if (!(mdev[devid].mdFlags & mdPhys)) { /* Physical mapped disk? */
311 bcopy((void *)((uintptr_t)fvaddr),
312 (void *)vaddr, (size_t)buf_count(bp)); /* This is virtual, just get the data */
313 } else {
314 left = buf_count(bp); /* Init the amount left to copy */
315 while (left) { /* Go until it is all copied */
316 lop = min((4096 - (vaddr & 4095)), (4096 - (fvaddr & 4095))); /* Get smallest amount left on sink and source */
317 csize = min(lop, left); /* Don't move more than we need to */
318
319 pp = pmap_find_phys(kernel_pmap, (addr64_t)((uintptr_t)vaddr)); /* Get the sink physical address */
320 if (!pp) { /* Not found, what gives? */
321 panic("mdevstrategy: sink address %016llX not mapped", (addr64_t)((uintptr_t)vaddr));
322 }
323 paddr = (addr64_t)(((addr64_t)pp << 12) | (addr64_t)(vaddr & 4095)); /* Get actual address */
324 bcopy_phys(fvaddr, paddr, csize); /* Copy this on in */
325 mapping_set_mod((ppnum_t)(paddr >> 12)); /* Make sure we know that it is modified */
326
327 left = left - csize; /* Calculate what is left */
328 vaddr = vaddr + csize; /* Move to next sink address */
329 fvaddr = fvaddr + csize; /* Bump to next physical address */
330 }
331 }
332 } else { /* This is a write */
333 if (!(mdev[devid].mdFlags & mdPhys)) { /* Physical mapped disk? */
334 bcopy((void *)vaddr, (void *)((uintptr_t)fvaddr),
335 (size_t)buf_count(bp)); /* This is virtual, just put the data */
336 } else {
337 left = buf_count(bp); /* Init the amount left to copy */
338 while (left) { /* Go until it is all copied */
339 lop = min((4096 - (vaddr & 4095)), (4096 - (fvaddr & 4095))); /* Get smallest amount left on sink and source */
340 csize = min(lop, left); /* Don't move more than we need to */
341
342 pp = pmap_find_phys(kernel_pmap, (addr64_t)((uintptr_t)vaddr)); /* Get the source physical address */
343 if (!pp) { /* Not found, what gives? */
344 panic("mdevstrategy: source address %016llX not mapped", (addr64_t)((uintptr_t)vaddr));
345 }
346 paddr = (addr64_t)(((addr64_t)pp << 12) | (addr64_t)(vaddr & 4095)); /* Get actual address */
347
348 bcopy_phys(paddr, fvaddr, csize); /* Move this on out */
349
350 left = left - csize; /* Calculate what is left */
351 vaddr = vaddr + csize; /* Move to next sink address */
352 fvaddr = fvaddr + csize; /* Bump to next physical address */
353 }
354 }
355 }
356 /*
357 * buf_unmap takes care of all the cases
358 * it will unmap the buffer from kernel
359 * virtual space if that was the state
360 * when we mapped it.
361 */
362 buf_unmap(bp);
363
364 buf_setresid(bp, 0); /* Nothing more to do */
365 buf_biodone(bp); /* Say we've finished */
366 }
367
368 static int
mdevbioctl(dev_t dev,u_long cmd,caddr_t data,int flag,struct proc * p)369 mdevbioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
370 {
371 return mdevioctl(dev, cmd, data, flag, p, 0);
372 }
373
374 static int
mdevcioctl(dev_t dev,u_long cmd,caddr_t data,int flag,struct proc * p)375 mdevcioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
376 {
377 return mdevioctl(dev, cmd, data, flag, p, 1);
378 }
379
380 static int
mdevioctl(dev_t dev,u_long cmd,caddr_t data,__unused int flag,struct proc * p,int is_char)381 mdevioctl(dev_t dev, u_long cmd, caddr_t data, __unused int flag,
382 struct proc *p, int is_char)
383 {
384 int error;
385 u_int32_t *f;
386 u_int64_t *o;
387 int devid;
388 dk_memdev_info_t * memdev_info;
389
390 devid = minor(dev); /* Get minor device number */
391
392 if (devid >= NB_MAX_MDEVICES) {
393 return ENXIO; /* Not valid */
394 }
395 error = proc_suser(p); /* Are we superman? */
396 if (error) {
397 return error; /* Nope... */
398 }
399 f = (u_int32_t*)data;
400 o = (u_int64_t *)data;
401 memdev_info = (dk_memdev_info_t *) data;
402
403 switch (cmd) {
404 case DKIOCGETMAXBLOCKCOUNTREAD:
405 *o = 32;
406 break;
407
408 case DKIOCGETMAXBLOCKCOUNTWRITE:
409 *o = 32;
410 break;
411
412 case DKIOCGETMAXSEGMENTCOUNTREAD:
413 *o = 32;
414 break;
415
416 case DKIOCGETMAXSEGMENTCOUNTWRITE:
417 *o = 32;
418 break;
419
420 case DKIOCGETBLOCKSIZE:
421 *f = mdev[devid].mdSecsize;
422 break;
423
424 case DKIOCSETBLOCKSIZE:
425 if (is_char) {
426 return ENODEV; /* We can only do this for a block */
427 }
428 if (*f < DEV_BSIZE) {
429 return EINVAL; /* Too short? */
430 }
431 mdev[devid].mdSecsize = *f; /* set the new block size */
432 break;
433
434 case DKIOCISWRITABLE:
435 *f = 1;
436 break;
437
438 case DKIOCGETBLOCKCOUNT:
439 if (!(mdev[devid].mdFlags & mdInited)) {
440 return ENXIO;
441 }
442 *o = ((mdev[devid].mdSize << 12) + mdev[devid].mdSecsize - 1) / mdev[devid].mdSecsize;
443 break;
444
445 /*
446 * We're interested in the following bits of information:
447 * Are you a memory-backed device (always yes, in this case)?
448 * Physical memory (mdPhys)?
449 * What is your base page?
450 * What is your size?
451 */
452 case DKIOCGETMEMDEVINFO:
453 if (!(mdev[devid].mdFlags & mdInited)) {
454 return ENXIO;
455 }
456 memdev_info->mi_mdev = TRUE;
457 memdev_info->mi_phys = (mdev[devid].mdFlags & mdPhys) ? TRUE : FALSE;
458 memdev_info->mi_base = (uint32_t)mdev[devid].mdBase;
459 memdev_info->mi_size = mdev[devid].mdSize;
460 break;
461
462 default:
463 error = ENOTTY;
464 break;
465 }
466 return error;
467 }
468
469
470 static int
mdevsize(dev_t dev)471 mdevsize(dev_t dev)
472 {
473 int devid;
474
475 devid = minor(dev); /* Get minor device number */
476 if (devid >= NB_MAX_MDEVICES) {
477 return ENXIO; /* Not valid */
478 }
479 if ((mdev[devid].mdFlags & mdInited) == 0) {
480 return -1; /* Not inited yet */
481 }
482 return mdev[devid].mdSecsize;
483 }
484
485 #include <pexpert/pexpert.h>
486
487 void
mdevinit(__unused int the_cnt)488 mdevinit(__unused int the_cnt)
489 {
490 #ifdef CONFIG_MEMDEV_INSECURE
491
492 int devid, phys;
493 uint64_t base;
494 uint64_t size;
495 char *ba, *lp;
496 dev_t dev;
497
498
499 ba = PE_boot_args(); /* Get the boot arguments */
500 lp = ba + 256; /* Point to the end */
501
502 while (1) { /* Step through, looking for our keywords */
503 phys = 0; /* Assume virtual memory device */
504 ba = nonspace(ba, lp); /* Find non-space */
505 if (ba >= lp) {
506 return; /* We are done if no more... */
507 }
508 if (((ba[0] != 'v') && (ba[0] != 'p'))
509 || (ba[1] != 'm') || (ba[2] != 'd') || (ba[4] != '=')
510 || (ba[3] < '0') || (ba[3] > 'f')
511 || ((ba[3] > '9') && (ba[3] < 'a'))) { /* Is this of form "vmdx=" or "pmdx=" where x is hex digit? */
512 ba = getspace(ba, lp); /* Find next white space or end */
513 continue; /* Start looking for the next one */
514 }
515
516 if (ba[0] == 'p') {
517 phys = 1; /* Set physical memory disk */
518 }
519 devid = ba[3] & 0xF; /* Assume digit */
520 if (ba[3] > '9') {
521 devid += 9; /* Adjust for hex digits */
522 }
523 ba = &ba[5]; /* Step past keyword */
524 ba = cvtnum(ba, lp, &base); /* Convert base of memory disk */
525 if (ba >= lp) {
526 return; /* Malformed one at the end, leave */
527 }
528 if (ba[0] != '.') {
529 continue; /* If not length separater, try next... */
530 }
531 if (base & 0xFFF) {
532 continue; /* Only allow page aligned stuff */
533 }
534 ba++; /* Step past '.' */
535 ba = cvtnum(ba, lp, &size); /* Try to convert it */
536 if (!size || (size & 0xFFF)) {
537 continue; /* Allow only non-zer page size multiples */
538 }
539 if (ba < lp) { /* If we are not at end, check end character */
540 if ((ba[0] != ' ') && (ba[0] != 0)) {
541 continue; /* End must be null or space */
542 }
543 }
544
545 dev = mdevadd(devid, base >> 12, (unsigned)size >> 12, phys); /* Go add the device */
546 }
547
548 #endif /* CONFIG_MEMDEV_INSECURE */
549
550 return;
551 }
552
553 #ifdef CONFIG_MEMDEV_INSECURE
554
555 char *
nonspace(char * pos,char * end)556 nonspace(char *pos, char *end) /* Find next non-space in string */
557 {
558 if (pos >= end) {
559 return end; /* Don't go past end */
560 }
561 if (pos[0] == 0) {
562 return end; /* If at null, make end */
563 }
564 while (1) { /* Keep going */
565 if (pos[0] != ' ') {
566 return pos; /* Leave if we found one */
567 }
568 pos++; /* Stop */
569 if (pos >= end) {
570 return end; /* Quit if we run off end */
571 }
572 }
573 }
574
575 char *
getspace(char * pos,char * end)576 getspace(char *pos, char *end) /* Find next non-space in string */
577 {
578 while (1) { /* Keep going */
579 if (pos >= end) {
580 return end; /* Don't go past end */
581 }
582 if (pos[0] == 0) {
583 return end; /* Leave if we hit null */
584 }
585 if (pos[0] == ' ') {
586 return pos; /* Leave if we found one */
587 }
588 pos++; /* Stop */
589 }
590 }
591
592 char *
cvtnum(char * pos,char * end,uint64_t * num)593 cvtnum(char *pos, char *end, uint64_t *num) /* Convert to a number */
594 {
595 int rad, dig;
596
597 *num = 0; /* Set answer to 0 to start */
598 rad = 10;
599
600 if (pos >= end) {
601 return end; /* Don't go past end */
602 }
603 if (pos[0] == 0) {
604 return end; /* If at null, make end */
605 }
606 if (pos[0] == '0' && ((pos[1] == 'x') || (pos[1] == 'x'))) { /* A hex constant? */
607 rad = 16;
608 pos += 2; /* Point to the number */
609 }
610
611 while (1) { /* Convert it */
612 if (pos >= end) {
613 return end; /* Don't go past end */
614 }
615 if (pos[0] == 0) {
616 return end; /* If at null, make end */
617 }
618 if (pos[0] < '0') {
619 return pos; /* Leave if non-digit */
620 }
621 dig = pos[0] & 0xF; /* Extract digit */
622 if (pos[0] > '9') { /* Is it bigger than 9? */
623 if (rad == 10) {
624 return pos; /* Leave if not base 10 */
625 }
626 if (!(((pos[0] >= 'A') && (pos[0] <= 'F'))
627 || ((pos[0] >= 'a') && (pos[0] <= 'f')))) {
628 return pos; /* Leave if bogus char */
629 }
630 dig = dig + 9; /* Adjust for character */
631 }
632 *num = (*num * rad) + dig; /* Accumulate the number */
633 pos++; /* Step on */
634 }
635 }
636
637 #endif /* CONFIG_MEMDEV_INSECURE */
638
639 dev_t
mdevadd(int devid,uint64_t base,unsigned int size,int phys)640 mdevadd(int devid, uint64_t base, unsigned int size, int phys)
641 {
642 int i;
643
644 if (devid < 0) {
645 devid = -1;
646 for (i = 0; i < NB_MAX_MDEVICES; i++) { /* Search all known memory devices */
647 if (!(mdev[i].mdFlags & mdInited)) { /* Is this a free one? */
648 if (devid < 0) {
649 devid = i; /* Remember first free one */
650 }
651 continue; /* Skip check */
652 }
653 if (!(((base + size - 1) < mdev[i].mdBase) || ((mdev[i].mdBase + mdev[i].mdSize - 1) < base))) { /* Is there any overlap? */
654 panic("mdevadd: attempt to add overlapping memory device at %016llX-%016llX", mdev[i].mdBase, mdev[i].mdBase + mdev[i].mdSize - 1);
655 }
656 }
657 if (devid < 0) { /* Do we have free slots? */
658 panic("mdevadd: attempt to add more than %d memory devices", NB_MAX_MDEVICES);
659 }
660 } else {
661 if (devid >= NB_MAX_MDEVICES) { /* Giving us something bogus? */
662 panic("mdevadd: attempt to explicitly add a bogus memory device: %08X", devid);
663 }
664 if (mdev[devid].mdFlags & mdInited) { /* Already there? */
665 panic("mdevadd: attempt to explicitly add a previously defined memory device: %08X", devid);
666 }
667 }
668
669 if (mdevBMajor < 0) { /* Have we gotten a major number yet? */
670 mdevBMajor = bdevsw_add(-1, &mdevbdevsw); /* Add to the table and figure out a major number */
671 if (mdevBMajor < 0) {
672 printf("mdevadd: error - bdevsw_add() returned %d\n", mdevBMajor);
673 return -1;
674 }
675 }
676
677 if (mdevCMajor < 0) { /* Have we gotten a major number yet? */
678 mdevCMajor = cdevsw_add_with_bdev(-1, &mdevcdevsw, mdevBMajor); /* Add to the table and figure out a major number */
679 if (mdevCMajor < 0) {
680 printf("ramdevice_init: error - cdevsw_add() returned %d\n", mdevCMajor);
681 return -1;
682 }
683 }
684
685 mdev[devid].mdBDev = makedev(mdevBMajor, devid); /* Get the device number */
686 mdev[devid].mdbdevb = devfs_make_node(mdev[devid].mdBDev, DEVFS_BLOCK, /* Make the node */
687 UID_ROOT, GID_OPERATOR,
688 0600, "md%d", devid);
689 if (mdev[devid].mdbdevb == NULL) { /* Did we make one? */
690 printf("mdevadd: devfs_make_node for block failed!\n");
691 return -1; /* Nope... */
692 }
693
694 mdev[devid].mdCDev = makedev(mdevCMajor, devid); /* Get the device number */
695 mdev[devid].mdcdevb = devfs_make_node(mdev[devid].mdCDev, DEVFS_CHAR, /* Make the node */
696 UID_ROOT, GID_OPERATOR,
697 0600, "rmd%d", devid);
698 if (mdev[devid].mdcdevb == NULL) { /* Did we make one? */
699 printf("mdevadd: devfs_make_node for character failed!\n");
700 return -1; /* Nope... */
701 }
702
703 mdev[devid].mdBase = base; /* Set the base address of ram disk */
704 mdev[devid].mdSize = size; /* Set the length of the ram disk */
705 mdev[devid].mdSecsize = DEV_BSIZE; /* Set starting block size */
706 if (phys) {
707 mdev[devid].mdFlags |= mdPhys; /* Show that we are in physical memory */
708 }
709 mdev[devid].mdFlags |= mdInited; /* Show we are all set up */
710 printf("Added memory device md%x/rmd%x (%08X/%08X) at %016llX for %016llX\n",
711 devid, devid, mdev[devid].mdBDev, mdev[devid].mdCDev, base << 12, (uint64_t)size << 12);
712 return mdev[devid].mdBDev;
713 }
714
715
716 dev_t
mdevlookup(int devid)717 mdevlookup(int devid)
718 {
719 if ((devid < 0) || (devid >= NB_MAX_MDEVICES)) {
720 return -1; /* Filter any bogus requests */
721 }
722 if (!(mdev[devid].mdFlags & mdInited)) {
723 return -1; /* This one hasn't been defined */
724 }
725 return mdev[devid].mdBDev; /* Return the device number */
726 }
727
728 void
mdevremoveall(void)729 mdevremoveall(void)
730 {
731 int i;
732
733 for (i = 0; i < NB_MAX_MDEVICES; i++) {
734 if (!(mdev[i].mdFlags & mdInited)) {
735 continue; /* Ignore unused mdevs */
736 }
737 devfs_remove(mdev[i].mdbdevb); /* Remove the block device */
738 devfs_remove(mdev[i].mdcdevb); /* Remove the character device */
739
740 mdev[i].mdBase = 0; /* Clear the mdev's storage */
741 mdev[i].mdSize = 0;
742 mdev[i].mdSecsize = 0;
743 mdev[i].mdFlags = 0;
744 mdev[i].mdBDev = 0;
745 mdev[i].mdCDev = 0;
746 mdev[i].mdbdevb = 0;
747 mdev[i].mdcdevb = 0;
748 }
749 }
750
751 int
mdevgetrange(int devid,uint64_t * base,uint64_t * size)752 mdevgetrange(int devid, uint64_t *base, uint64_t *size)
753 {
754 assert(base);
755 assert(size);
756
757 /* filter invalid request */
758 if ((devid < 0) || (devid >= NB_MAX_MDEVICES)) {
759 return -1;
760 }
761
762 /* filter non-initialized memory devices */
763 if ((mdev[devid].mdFlags & mdInited) == 0) {
764 return -1;
765 }
766
767 *base = mdev[devid].mdBase << 12;
768 *size = mdev[devid].mdSize << 12;
769
770 /* make sure (base, size) is a valid range and will not overflow */
771 assert(*size < (UINT64_MAX - *base));
772
773 return 0;
774 }
775