]> git.saurik.com Git - apple/xnu.git/blame - bsd/dev/memdev.c
xnu-3248.60.10.tar.gz
[apple/xnu.git] / bsd / dev / memdev.c
CommitLineData
91447636 1/*
2d21ac55 2 * Copyright (c) 2004-2006 Apple Computer, Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
91447636 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
91447636 27 */
55e303ae
A
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
55e303ae 78#include <sys/param.h>
55e303ae
A
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>
55e303ae
A
86#include <sys/fcntl.h>
87#include <sys/conf.h>
88#include <sys/disk.h>
89#include <sys/stat.h>
55e303ae 90#include <sys/vm.h>
91447636
A
91#include <sys/uio_internal.h>
92#include <libkern/libkern.h>
55e303ae 93
91447636 94#include <vm/pmap.h>
55e303ae 95#include <vm/vm_pager.h>
55e303ae 96#include <mach/memory_object_types.h>
2d21ac55 97#include <kern/debug.h>
55e303ae
A
98
99#include <miscfs/devfs/devfs.h>
100
91447636
A
101
102void mdevinit(int the_cnt);
103
104static open_close_fcn_t mdevopen;
105static open_close_fcn_t mdevclose;
55e303ae 106static psize_fcn_t mdevsize;
91447636
A
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);
b0d623f7 111
593a1d5f 112#ifdef CONFIG_MEMDEV_INSECURE
b0d623f7 113
91447636
A
114static char * nonspace(char *pos, char *end);
115static char * getspace(char *pos, char *end);
316670eb 116static char * cvtnum(char *pos, char *end, uint64_t *num);
b0d623f7 117
593a1d5f 118#endif /* CONFIG_MEMDEV_INSECURE */
91447636
A
119
120extern void bcopy_phys(addr64_t from, addr64_t to, vm_size_t bytes);
121extern void mapping_set_mod(ppnum_t pn);
122extern ppnum_t pmap_find_phys(pmap_t pmap, addr64_t va);
123
55e303ae
A
124
125/*
126 * cdevsw
127 * D_DISK we want to look like a disk
128 * D_CANFREE We support B_FREEBUF
129 */
130
131static struct bdevsw mdevbdevsw = {
132 /* open */ mdevopen,
133 /* close */ mdevclose,
134 /* strategy */ mdevstrategy,
135 /* ioctl */ mdevbioctl,
136 /* dump */ eno_dump,
137 /* psize */ mdevsize,
138 /* flags */ D_DISK,
139};
140
141static struct cdevsw mdevcdevsw = {
142 /* open */ mdevopen,
143 /* close */ mdevclose,
144 /* read */ mdevrw,
145 /* write */ mdevrw,
146 /* ioctl */ mdevcioctl,
147 /* stop */ eno_stop,
148 /* reset */ eno_reset,
2d21ac55 149 /* ttys */ NULL,
55e303ae
A
150 /* select */ eno_select,
151 /* mmap */ eno_mmap,
152 /* strategy */ eno_strat,
153 /* getc */ eno_getc,
154 /* putc */ eno_putc,
155 /* flags */ D_DISK,
156};
157
158struct mdev {
b0d623f7 159 uint64_t mdBase; /* file size in bytes */
55e303ae
A
160 uint32_t mdSize; /* file size in bytes */
161 int mdFlags; /* flags */
162 int mdSecsize; /* sector size */
163 int mdBDev; /* Block device number */
164 int mdCDev; /* Character device number */
165 void * mdbdevb;
166 void * mdcdevb;
167} mdev[16];
168
169/* mdFlags */
170#define mdInited 0x01 /* This device defined */
171#define mdRO 0x02 /* This device is read-only */
172#define mdPhys 0x04 /* This device is in physical memory */
173
174int mdevBMajor = -1;
175int mdevCMajor = -1;
176
177static int mdevioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p, int is_char);
6d2010ae 178dev_t mdevadd(int devid, uint64_t base, unsigned int size, int phys);
55e303ae 179dev_t mdevlookup(int devid);
4a3eedf9 180void mdevremoveall(void);
55e303ae 181
91447636
A
182static int mdevclose(__unused dev_t dev, __unused int flags,
183 __unused int devtype, __unused struct proc *p) {
184
55e303ae
A
185 return (0);
186}
187
91447636 188static int mdevopen(dev_t dev, int flags, __unused int devtype, __unused struct proc *p) {
55e303ae
A
189
190 int devid;
191
192 devid = minor(dev); /* Get minor device number */
193
3e170ce0 194 if (devid >= 16) return (ENXIO); /* Not valid */
55e303ae
A
195
196 if ((flags & FWRITE) && (mdev[devid].mdFlags & mdRO)) return (EACCES); /* Currently mounted RO */
197
198 return(0);
199}
200
91447636 201static int mdevrw(dev_t dev, struct uio *uio, __unused int ioflag) {
55e303ae 202 int status;
55e303ae 203 addr64_t mdata;
91447636
A
204 int devid;
205 enum uio_seg saveflag;
55e303ae
A
206
207 devid = minor(dev); /* Get minor device number */
208
3e170ce0 209 if (devid >= 16) return (ENXIO); /* Not valid */
55e303ae
A
210 if (!(mdev[devid].mdFlags & mdInited)) return (ENXIO); /* Have we actually been defined yet? */
211
212 mdata = ((addr64_t)mdev[devid].mdBase << 12) + uio->uio_offset; /* Point to the area in "file" */
213
214 saveflag = uio->uio_segflg; /* Remember what the request is */
91447636 215#if LP64_DEBUG
b0d623f7 216 if (UIO_IS_USER_SPACE(uio) == 0 && UIO_IS_SYS_SPACE(uio) == 0) {
91447636
A
217 panic("mdevrw - invalid uio_segflg\n");
218 }
219#endif /* LP64_DEBUG */
220 /* Make sure we are moving from physical ram if physical device */
221 if (mdev[devid].mdFlags & mdPhys) {
222 if (uio->uio_segflg == UIO_USERSPACE64)
223 uio->uio_segflg = UIO_PHYS_USERSPACE64;
224 else if (uio->uio_segflg == UIO_USERSPACE32)
225 uio->uio_segflg = UIO_PHYS_USERSPACE32;
226 else
227 uio->uio_segflg = UIO_PHYS_USERSPACE;
228 }
229 status = uiomove64(mdata, uio_resid(uio), uio); /* Move the data */
55e303ae
A
230 uio->uio_segflg = saveflag; /* Restore the flag */
231
232 return (status);
233}
234
235static void mdevstrategy(struct buf *bp) {
91447636 236 unsigned int left, lop, csize;
55e303ae 237 vm_offset_t vaddr, blkoff;
55e303ae
A
238 int devid;
239 addr64_t paddr, fvaddr;
240 ppnum_t pp;
241
91447636 242 devid = minor(buf_device(bp)); /* Get minor device number */
55e303ae
A
243
244 if ((mdev[devid].mdFlags & mdInited) == 0) { /* Have we actually been defined yet? */
91447636
A
245 buf_seterror(bp, ENXIO);
246 buf_biodone(bp);
55e303ae
A
247 return;
248 }
249
91447636 250 buf_setresid(bp, buf_count(bp)); /* Set byte count */
55e303ae 251
91447636 252 blkoff = buf_blkno(bp) * mdev[devid].mdSecsize; /* Get offset into file */
55e303ae
A
253
254/*
255 * Note that reading past end is an error, but reading at end is an EOF. For these
91447636 256 * we just return with resid == count.
55e303ae
A
257 */
258
259 if (blkoff >= (mdev[devid].mdSize << 12)) { /* Are they trying to read/write at/after end? */
260 if(blkoff != (mdev[devid].mdSize << 12)) { /* Are we trying to read after EOF? */
91447636 261 buf_seterror(bp, EINVAL); /* Yeah, this is an error */
55e303ae 262 }
91447636 263 buf_biodone(bp); /* Return */
55e303ae
A
264 return;
265 }
266
91447636
A
267 if ((blkoff + buf_count(bp)) > (mdev[devid].mdSize << 12)) { /* Will this read go past end? */
268 buf_setcount(bp, ((mdev[devid].mdSize << 12) - blkoff)); /* Yes, trim to max */
55e303ae 269 }
91447636
A
270 /*
271 * make sure the buffer's data area is
272 * accessible
273 */
274 if (buf_map(bp, (caddr_t *)&vaddr))
275 panic("ramstrategy: buf_map failed\n");
55e303ae 276
55e303ae
A
277 fvaddr = (mdev[devid].mdBase << 12) + blkoff; /* Point to offset into ram disk */
278
91447636 279 if (buf_flags(bp) & B_READ) { /* Is this a read? */
55e303ae
A
280 if(!(mdev[devid].mdFlags & mdPhys)) { /* Physical mapped disk? */
281 bcopy((void *)((uintptr_t)fvaddr),
91447636 282 (void *)vaddr, (size_t)buf_count(bp)); /* This is virtual, just get the data */
55e303ae
A
283 }
284 else {
91447636 285 left = buf_count(bp); /* Init the amount left to copy */
55e303ae
A
286 while(left) { /* Go until it is all copied */
287
288 lop = min((4096 - (vaddr & 4095)), (4096 - (fvaddr & 4095))); /* Get smallest amount left on sink and source */
289 csize = min(lop, left); /* Don't move more than we need to */
290
b0d623f7 291 pp = pmap_find_phys(kernel_pmap, (addr64_t)((uintptr_t)vaddr)); /* Get the sink physical address */
55e303ae 292 if(!pp) { /* Not found, what gives? */
b0d623f7 293 panic("mdevstrategy: sink address %016llX not mapped\n", (addr64_t)((uintptr_t)vaddr));
55e303ae
A
294 }
295 paddr = (addr64_t)(((addr64_t)pp << 12) | (addr64_t)(vaddr & 4095)); /* Get actual address */
296 bcopy_phys(fvaddr, paddr, csize); /* Copy this on in */
297 mapping_set_mod(paddr >> 12); /* Make sure we know that it is modified */
298
299 left = left - csize; /* Calculate what is left */
300 vaddr = vaddr + csize; /* Move to next sink address */
301 fvaddr = fvaddr + csize; /* Bump to next physical address */
302 }
303 }
304 }
305 else { /* This is a write */
306 if(!(mdev[devid].mdFlags & mdPhys)) { /* Physical mapped disk? */
307 bcopy((void *)vaddr, (void *)((uintptr_t)fvaddr),
91447636 308 (size_t)buf_count(bp)); /* This is virtual, just put the data */
55e303ae
A
309 }
310 else {
91447636 311 left = buf_count(bp); /* Init the amount left to copy */
55e303ae
A
312 while(left) { /* Go until it is all copied */
313
314 lop = min((4096 - (vaddr & 4095)), (4096 - (fvaddr & 4095))); /* Get smallest amount left on sink and source */
315 csize = min(lop, left); /* Don't move more than we need to */
316
b0d623f7 317 pp = pmap_find_phys(kernel_pmap, (addr64_t)((uintptr_t)vaddr)); /* Get the source physical address */
55e303ae 318 if(!pp) { /* Not found, what gives? */
b0d623f7 319 panic("mdevstrategy: source address %016llX not mapped\n", (addr64_t)((uintptr_t)vaddr));
55e303ae
A
320 }
321 paddr = (addr64_t)(((addr64_t)pp << 12) | (addr64_t)(vaddr & 4095)); /* Get actual address */
322
323 bcopy_phys(paddr, fvaddr, csize); /* Move this on out */
324
325 left = left - csize; /* Calculate what is left */
326 vaddr = vaddr + csize; /* Move to next sink address */
327 fvaddr = fvaddr + csize; /* Bump to next physical address */
328 }
329 }
330 }
91447636
A
331 /*
332 * buf_unmap takes care of all the cases
333 * it will unmap the buffer from kernel
334 * virtual space if that was the state
335 * when we mapped it.
336 */
337 buf_unmap(bp);
338
339 buf_setresid(bp, 0); /* Nothing more to do */
340 buf_biodone(bp); /* Say we've finished */
55e303ae
A
341}
342
343static int mdevbioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p) {
344 return (mdevioctl(dev, cmd, data, flag, p, 0));
345}
346
347static int mdevcioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p) {
348 return (mdevioctl(dev, cmd, data, flag, p, 1));
349}
350
91447636
A
351static int mdevioctl(dev_t dev, u_long cmd, caddr_t data, __unused int flag,
352 struct proc *p, int is_char) {
55e303ae 353 int error;
b0d623f7 354 u_int32_t *f;
55e303ae
A
355 u_int64_t *o;
356 int devid;
39236c6e 357 dk_memdev_info_t * memdev_info;
55e303ae
A
358
359 devid = minor(dev); /* Get minor device number */
360
3e170ce0 361 if (devid >= 16) return (ENXIO); /* Not valid */
55e303ae 362
91447636 363 error = proc_suser(p); /* Are we superman? */
55e303ae
A
364 if (error) return (error); /* Nope... */
365
b0d623f7 366 f = (u_int32_t*)data;
55e303ae 367 o = (u_int64_t *)data;
39236c6e 368 memdev_info = (dk_memdev_info_t *) data;
55e303ae
A
369
370 switch (cmd) {
371
372 case DKIOCGETMAXBLOCKCOUNTREAD:
373 *o = 32;
374 break;
375
376 case DKIOCGETMAXBLOCKCOUNTWRITE:
377 *o = 32;
378 break;
379
380 case DKIOCGETMAXSEGMENTCOUNTREAD:
381 *o = 32;
382 break;
383
384 case DKIOCGETMAXSEGMENTCOUNTWRITE:
385 *o = 32;
386 break;
387
388 case DKIOCGETBLOCKSIZE:
389 *f = mdev[devid].mdSecsize;
390 break;
391
392 case DKIOCSETBLOCKSIZE:
393 if (is_char) return (ENODEV); /* We can only do this for a block */
394
395 if (*f < DEV_BSIZE) return (EINVAL); /* Too short? */
396
397 mdev[devid].mdSecsize = *f; /* set the new block size */
398 break;
399
400 case DKIOCISWRITABLE:
401 *f = 1;
402 break;
403
55e303ae
A
404 case DKIOCGETBLOCKCOUNT:
405 if(!(mdev[devid].mdFlags & mdInited)) return (ENXIO);
406 *o = ((mdev[devid].mdSize << 12) + mdev[devid].mdSecsize - 1) / mdev[devid].mdSecsize;
407 break;
39236c6e
A
408
409 /*
410 * We're interested in the following bits of information:
411 * Are you a memory-backed device (always yes, in this case)?
412 * Physical memory (mdPhys)?
413 * What is your base page?
414 * What is your size?
415 */
416 case DKIOCGETMEMDEVINFO:
417 if (!(mdev[devid].mdFlags & mdInited)) return (ENXIO);
418 memdev_info->mi_mdev = TRUE;
419 memdev_info->mi_phys = (mdev[devid].mdFlags & mdPhys) ? TRUE : FALSE;
420 memdev_info->mi_base = mdev[devid].mdBase;
421 memdev_info->mi_size = mdev[devid].mdSize;
422 break;
423
55e303ae
A
424 default:
425 error = ENOTTY;
426 break;
427 }
428 return(error);
429}
430
431
432static int mdevsize(dev_t dev) {
433
434 int devid;
435
436 devid = minor(dev); /* Get minor device number */
3e170ce0 437 if (devid >= 16) return (ENXIO); /* Not valid */
55e303ae
A
438
439 if ((mdev[devid].mdFlags & mdInited) == 0) return(-1); /* Not inited yet */
440
441 return(mdev[devid].mdSecsize);
442}
443
444#include <pexpert/pexpert.h>
445
91447636 446void mdevinit(__unused int the_cnt) {
55e303ae 447
593a1d5f 448#ifdef CONFIG_MEMDEV_INSECURE
b0d623f7 449
55e303ae 450 int devid, phys;
316670eb
A
451 uint64_t base;
452 uint64_t size;
55e303ae
A
453 char *ba, *lp;
454 dev_t dev;
455
b0d623f7 456
55e303ae
A
457 ba = PE_boot_args(); /* Get the boot arguments */
458 lp = ba + 256; /* Point to the end */
459
460 while(1) { /* Step through, looking for our keywords */
461 phys = 0; /* Assume virtual memory device */
462 ba = nonspace(ba, lp); /* Find non-space */
463 if(ba >= lp) return; /* We are done if no more... */
464 if(((ba[0] != 'v') && (ba[0] != 'p'))
465 || (ba[1] != 'm') || (ba[2] != 'd') || (ba[4] != '=')
466 || (ba[3] < '0') || (ba[3] > 'f')
467 || ((ba[3] > '9') && (ba[3] < 'a'))) { /* Is this of form "vmdx=" or "pmdx=" where x is hex digit? */
468
469 ba = getspace(ba, lp); /* Find next white space or end */
470 continue; /* Start looking for the next one */
471 }
472
473 if(ba[0] == 'p') phys = 1; /* Set physical memory disk */
474
475 devid = ba[3] & 0xF; /* Assume digit */
476 if(ba[3] > '9') devid += 9; /* Adjust for hex digits */
477
478 ba = &ba[5]; /* Step past keyword */
479 ba = cvtnum(ba, lp, &base); /* Convert base of memory disk */
480 if(ba >= lp) return; /* Malformed one at the end, leave */
481 if(ba[0] != '.') continue; /* If not length separater, try next... */
482 if(base & 0xFFF) continue; /* Only allow page aligned stuff */
483
484 ba++; /* Step past '.' */
485 ba = cvtnum(ba, lp, &size); /* Try to convert it */
486 if(!size || (size & 0xFFF)) continue; /* Allow only non-zer page size multiples */
487 if(ba < lp) { /* If we are not at end, check end character */
488 if((ba[0] != ' ') && (ba[0] != 0)) continue; /* End must be null or space */
489 }
490
316670eb 491 dev = mdevadd(devid, base >> 12, (unsigned)size >> 12, phys); /* Go add the device */
55e303ae 492 }
b0d623f7 493
593a1d5f 494#endif /* CONFIG_MEMDEV_INSECURE */
b0d623f7 495
55e303ae
A
496 return;
497
498}
499
593a1d5f 500#ifdef CONFIG_MEMDEV_INSECURE
b0d623f7 501
55e303ae
A
502char *nonspace(char *pos, char *end) { /* Find next non-space in string */
503
504 if(pos >= end) return end; /* Don't go past end */
505 if(pos[0] == 0) return end; /* If at null, make end */
506
507 while(1) { /* Keep going */
508 if(pos[0] != ' ') return pos; /* Leave if we found one */
509 pos++; /* Stop */
510 if(pos >= end) return end; /* Quit if we run off end */
511 }
512}
513
514char *getspace(char *pos, char *end) { /* Find next non-space in string */
515
516 while(1) { /* Keep going */
517 if(pos >= end) return end; /* Don't go past end */
518 if(pos[0] == 0) return end; /* Leave if we hit null */
519 if(pos[0] == ' ') return pos; /* Leave if we found one */
520 pos++; /* Stop */
521 }
522}
523
316670eb 524char *cvtnum(char *pos, char *end, uint64_t *num) { /* Convert to a number */
55e303ae
A
525
526 int rad, dig;
527
528 *num = 0; /* Set answer to 0 to start */
529 rad = 10;
530
531 if(pos >= end) return end; /* Don't go past end */
532 if(pos[0] == 0) return end; /* If at null, make end */
533
534 if(pos[0] == '0' && ((pos[1] == 'x') || (pos[1] == 'x'))) { /* A hex constant? */
535 rad = 16;
536 pos += 2; /* Point to the number */
537 }
538
539 while(1) { /* Convert it */
540
541 if(pos >= end) return end; /* Don't go past end */
542 if(pos[0] == 0) return end; /* If at null, make end */
543 if(pos[0] < '0') return pos; /* Leave if non-digit */
544 dig = pos[0] & 0xF; /* Extract digit */
545 if(pos[0] > '9') { /* Is it bigger than 9? */
546 if(rad == 10) return pos; /* Leave if not base 10 */
547 if(!(((pos[0] >= 'A') && (pos[0] <= 'F'))
548 || ((pos[0] >= 'a') && (pos[0] <= 'f')))) return pos; /* Leave if bogus char */
549 dig = dig + 9; /* Adjust for character */
550 }
551 *num = (*num * rad) + dig; /* Accumulate the number */
552 pos++; /* Step on */
553 }
554}
b0d623f7 555
593a1d5f 556#endif /* CONFIG_MEMDEV_INSECURE */
55e303ae 557
6d2010ae 558dev_t mdevadd(int devid, uint64_t base, unsigned int size, int phys) {
55e303ae
A
559
560 int i;
561
562 if(devid < 0) {
563
564 devid = -1;
565 for(i = 0; i < 16; i++) { /* Search all known memory devices */
566 if(!(mdev[i].mdFlags & mdInited)) { /* Is this a free one? */
567 if(devid < 0)devid = i; /* Remember first free one */
568 continue; /* Skip check */
569 }
570 if(!(((base + size -1 ) < mdev[i].mdBase) || ((mdev[i].mdBase + mdev[i].mdSize - 1) < base))) { /* Is there any overlap? */
6d2010ae 571 panic("mdevadd: attempt to add overlapping memory device at %016llX-%016llX\n", mdev[i].mdBase, mdev[i].mdBase + mdev[i].mdSize - 1);
55e303ae
A
572 }
573 }
574 if(devid < 0) { /* Do we have free slots? */
575 panic("mdevadd: attempt to add more than 16 memory devices\n");
576 }
577 }
578 else {
579 if(devid >= 16) { /* Giving us something bogus? */
2d21ac55 580 panic("mdevadd: attempt to explicitly add a bogus memory device: %08X\n", devid);
55e303ae 581 }
6d2010ae 582 if(mdev[devid].mdFlags & mdInited) { /* Already there? */
2d21ac55 583 panic("mdevadd: attempt to explicitly add a previously defined memory device: %08X\n", devid);
55e303ae
A
584 }
585 }
586
587 if(mdevBMajor < 0) { /* Have we gotten a major number yet? */
588 mdevBMajor = bdevsw_add(-1, &mdevbdevsw); /* Add to the table and figure out a major number */
589 if (mdevBMajor < 0) {
590 printf("mdevadd: error - bdevsw_add() returned %d\n", mdevBMajor);
591 return -1;
592 }
593 }
594
595 if(mdevCMajor < 0) { /* Have we gotten a major number yet? */
596 mdevCMajor = cdevsw_add_with_bdev(-1, &mdevcdevsw, mdevBMajor); /* Add to the table and figure out a major number */
597 if (mdevCMajor < 0) {
598 printf("ramdevice_init: error - cdevsw_add() returned %d\n", mdevCMajor);
599 return -1;
600 }
601 }
602
603 mdev[devid].mdBDev = makedev(mdevBMajor, devid); /* Get the device number */
604 mdev[devid].mdbdevb = devfs_make_node(mdev[devid].mdBDev, DEVFS_BLOCK, /* Make the node */
605 UID_ROOT, GID_OPERATOR,
606 0600, "md%d", devid);
607 if (mdev[devid].mdbdevb == NULL) { /* Did we make one? */
608 printf("mdevadd: devfs_make_node for block failed!\n");
609 return -1; /* Nope... */
610 }
611
612 mdev[devid].mdCDev = makedev(mdevCMajor, devid); /* Get the device number */
613 mdev[devid].mdcdevb = devfs_make_node(mdev[devid].mdCDev, DEVFS_CHAR, /* Make the node */
614 UID_ROOT, GID_OPERATOR,
615 0600, "rmd%d", devid);
616 if (mdev[devid].mdcdevb == NULL) { /* Did we make one? */
617 printf("mdevadd: devfs_make_node for character failed!\n");
618 return -1; /* Nope... */
619 }
620
621 mdev[devid].mdBase = base; /* Set the base address of ram disk */
622 mdev[devid].mdSize = size; /* Set the length of the ram disk */
623 mdev[devid].mdSecsize = DEV_BSIZE; /* Set starting block size */
624 if(phys) mdev[devid].mdFlags |= mdPhys; /* Show that we are in physical memory */
625 mdev[devid].mdFlags |= mdInited; /* Show we are all set up */
6d2010ae
A
626 printf("Added memory device md%x/rmd%x (%08X/%08X) at %016llX for %016llX\n",
627 devid, devid, mdev[devid].mdBDev, mdev[devid].mdCDev, base << 12, (uint64_t)size << 12);
55e303ae
A
628 return mdev[devid].mdBDev;
629}
630
631
632dev_t mdevlookup(int devid) {
633
634 if((devid < 0) || (devid > 15)) return -1; /* Filter any bogus requests */
635 if(!(mdev[devid].mdFlags & mdInited)) return -1; /* This one hasn't been defined */
636 return mdev[devid].mdBDev; /* Return the device number */
637}
4a3eedf9
A
638
639void mdevremoveall(void) {
640
641 int i;
642
643 for(i = 0; i < 16; i++) {
644 if(!(mdev[i].mdFlags & mdInited)) continue; /* Ignore unused mdevs */
645
646 devfs_remove(mdev[i].mdbdevb); /* Remove the block device */
647 devfs_remove(mdev[i].mdcdevb); /* Remove the character device */
648
649 mdev[i].mdBase = 0; /* Clear the mdev's storage */
650 mdev[i].mdSize = 0;
651 mdev[i].mdSecsize = 0;
652 mdev[i].mdFlags = 0;
653 mdev[i].mdBDev = 0;
654 mdev[i].mdCDev = 0;
655 mdev[i].mdbdevb = 0;
656 mdev[i].mdcdevb = 0;
657 }
658}