]> git.saurik.com Git - apple/xnu.git/blame - bsd/dev/memdev.c
xnu-3789.1.32.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
91447636
A
113static char * nonspace(char *pos, char *end);
114static char * getspace(char *pos, char *end);
316670eb 115static char * cvtnum(char *pos, char *end, uint64_t *num);
593a1d5f 116#endif /* CONFIG_MEMDEV_INSECURE */
91447636
A
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
39037602
A
122/*
123 * Maximal number of memory devices.
124 */
125#define NB_MAX_MDEVICES (16)
55e303ae
A
126
127/*
128 * cdevsw
129 * D_DISK we want to look like a disk
130 * D_CANFREE We support B_FREEBUF
131 */
132
133static struct bdevsw mdevbdevsw = {
134 /* open */ mdevopen,
135 /* close */ mdevclose,
136 /* strategy */ mdevstrategy,
137 /* ioctl */ mdevbioctl,
138 /* dump */ eno_dump,
139 /* psize */ mdevsize,
140 /* flags */ D_DISK,
141};
142
143static struct cdevsw mdevcdevsw = {
144 /* open */ mdevopen,
145 /* close */ mdevclose,
146 /* read */ mdevrw,
147 /* write */ mdevrw,
148 /* ioctl */ mdevcioctl,
149 /* stop */ eno_stop,
150 /* reset */ eno_reset,
2d21ac55 151 /* ttys */ NULL,
55e303ae
A
152 /* select */ eno_select,
153 /* mmap */ eno_mmap,
154 /* strategy */ eno_strat,
155 /* getc */ eno_getc,
156 /* putc */ eno_putc,
157 /* flags */ D_DISK,
158};
159
160struct mdev {
b0d623f7 161 uint64_t mdBase; /* file size in bytes */
55e303ae
A
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;
39037602 169} mdev[NB_MAX_MDEVICES];
55e303ae
A
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
39037602
A
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);
55e303ae 184
91447636
A
185static int mdevclose(__unused dev_t dev, __unused int flags,
186 __unused int devtype, __unused struct proc *p) {
187
55e303ae
A
188 return (0);
189}
190
91447636 191static int mdevopen(dev_t dev, int flags, __unused int devtype, __unused struct proc *p) {
55e303ae
A
192
193 int devid;
194
195 devid = minor(dev); /* Get minor device number */
196
39037602 197 if (devid >= NB_MAX_MDEVICES) return (ENXIO); /* Not valid */
55e303ae
A
198
199 if ((flags & FWRITE) && (mdev[devid].mdFlags & mdRO)) return (EACCES); /* Currently mounted RO */
200
201 return(0);
202}
203
91447636 204static int mdevrw(dev_t dev, struct uio *uio, __unused int ioflag) {
55e303ae 205 int status;
55e303ae 206 addr64_t mdata;
91447636
A
207 int devid;
208 enum uio_seg saveflag;
55e303ae
A
209
210 devid = minor(dev); /* Get minor device number */
211
39037602 212 if (devid >= NB_MAX_MDEVICES) return (ENXIO); /* Not valid */
55e303ae
A
213 if (!(mdev[devid].mdFlags & mdInited)) return (ENXIO); /* Have we actually been defined yet? */
214
215 mdata = ((addr64_t)mdev[devid].mdBase << 12) + uio->uio_offset; /* Point to the area in "file" */
216
217 saveflag = uio->uio_segflg; /* Remember what the request is */
91447636 218#if LP64_DEBUG
b0d623f7 219 if (UIO_IS_USER_SPACE(uio) == 0 && UIO_IS_SYS_SPACE(uio) == 0) {
91447636
A
220 panic("mdevrw - invalid uio_segflg\n");
221 }
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;
229 else
230 uio->uio_segflg = UIO_PHYS_USERSPACE;
231 }
232 status = uiomove64(mdata, uio_resid(uio), uio); /* Move the data */
55e303ae
A
233 uio->uio_segflg = saveflag; /* Restore the flag */
234
235 return (status);
236}
237
238static void mdevstrategy(struct buf *bp) {
91447636 239 unsigned int left, lop, csize;
55e303ae 240 vm_offset_t vaddr, blkoff;
55e303ae
A
241 int devid;
242 addr64_t paddr, fvaddr;
243 ppnum_t pp;
244
91447636 245 devid = minor(buf_device(bp)); /* Get minor device number */
55e303ae
A
246
247 if ((mdev[devid].mdFlags & mdInited) == 0) { /* Have we actually been defined yet? */
91447636
A
248 buf_seterror(bp, ENXIO);
249 buf_biodone(bp);
55e303ae
A
250 return;
251 }
252
91447636 253 buf_setresid(bp, buf_count(bp)); /* Set byte count */
55e303ae 254
91447636 255 blkoff = buf_blkno(bp) * mdev[devid].mdSecsize; /* Get offset into file */
55e303ae
A
256
257/*
258 * Note that reading past end is an error, but reading at end is an EOF. For these
91447636 259 * we just return with resid == count.
55e303ae
A
260 */
261
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? */
91447636 264 buf_seterror(bp, EINVAL); /* Yeah, this is an error */
55e303ae 265 }
91447636 266 buf_biodone(bp); /* Return */
55e303ae
A
267 return;
268 }
269
91447636
A
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 */
55e303ae 272 }
91447636
A
273 /*
274 * make sure the buffer's data area is
275 * accessible
276 */
277 if (buf_map(bp, (caddr_t *)&vaddr))
278 panic("ramstrategy: buf_map failed\n");
55e303ae 279
55e303ae
A
280 fvaddr = (mdev[devid].mdBase << 12) + blkoff; /* Point to offset into ram disk */
281
91447636 282 if (buf_flags(bp) & B_READ) { /* Is this a read? */
55e303ae
A
283 if(!(mdev[devid].mdFlags & mdPhys)) { /* Physical mapped disk? */
284 bcopy((void *)((uintptr_t)fvaddr),
91447636 285 (void *)vaddr, (size_t)buf_count(bp)); /* This is virtual, just get the data */
55e303ae
A
286 }
287 else {
91447636 288 left = buf_count(bp); /* Init the amount left to copy */
55e303ae
A
289 while(left) { /* Go until it is all copied */
290
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 */
293
b0d623f7 294 pp = pmap_find_phys(kernel_pmap, (addr64_t)((uintptr_t)vaddr)); /* Get the sink physical address */
55e303ae 295 if(!pp) { /* Not found, what gives? */
b0d623f7 296 panic("mdevstrategy: sink address %016llX not mapped\n", (addr64_t)((uintptr_t)vaddr));
55e303ae
A
297 }
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 */
301
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 */
305 }
306 }
307 }
308 else { /* This is a write */
309 if(!(mdev[devid].mdFlags & mdPhys)) { /* Physical mapped disk? */
310 bcopy((void *)vaddr, (void *)((uintptr_t)fvaddr),
91447636 311 (size_t)buf_count(bp)); /* This is virtual, just put the data */
55e303ae
A
312 }
313 else {
91447636 314 left = buf_count(bp); /* Init the amount left to copy */
55e303ae
A
315 while(left) { /* Go until it is all copied */
316
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 */
319
b0d623f7 320 pp = pmap_find_phys(kernel_pmap, (addr64_t)((uintptr_t)vaddr)); /* Get the source physical address */
55e303ae 321 if(!pp) { /* Not found, what gives? */
b0d623f7 322 panic("mdevstrategy: source address %016llX not mapped\n", (addr64_t)((uintptr_t)vaddr));
55e303ae
A
323 }
324 paddr = (addr64_t)(((addr64_t)pp << 12) | (addr64_t)(vaddr & 4095)); /* Get actual address */
325
326 bcopy_phys(paddr, fvaddr, csize); /* Move this on out */
327
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 */
331 }
332 }
333 }
91447636
A
334 /*
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
338 * when we mapped it.
339 */
340 buf_unmap(bp);
341
342 buf_setresid(bp, 0); /* Nothing more to do */
343 buf_biodone(bp); /* Say we've finished */
55e303ae
A
344}
345
346static 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));
348}
349
350static 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));
352}
353
91447636
A
354static int mdevioctl(dev_t dev, u_long cmd, caddr_t data, __unused int flag,
355 struct proc *p, int is_char) {
55e303ae 356 int error;
b0d623f7 357 u_int32_t *f;
55e303ae
A
358 u_int64_t *o;
359 int devid;
39236c6e 360 dk_memdev_info_t * memdev_info;
55e303ae
A
361
362 devid = minor(dev); /* Get minor device number */
363
39037602 364 if (devid >= NB_MAX_MDEVICES) return (ENXIO); /* Not valid */
55e303ae 365
91447636 366 error = proc_suser(p); /* Are we superman? */
55e303ae
A
367 if (error) return (error); /* Nope... */
368
b0d623f7 369 f = (u_int32_t*)data;
55e303ae 370 o = (u_int64_t *)data;
39236c6e 371 memdev_info = (dk_memdev_info_t *) data;
55e303ae
A
372
373 switch (cmd) {
374
375 case DKIOCGETMAXBLOCKCOUNTREAD:
376 *o = 32;
377 break;
378
379 case DKIOCGETMAXBLOCKCOUNTWRITE:
380 *o = 32;
381 break;
382
383 case DKIOCGETMAXSEGMENTCOUNTREAD:
384 *o = 32;
385 break;
386
387 case DKIOCGETMAXSEGMENTCOUNTWRITE:
388 *o = 32;
389 break;
390
391 case DKIOCGETBLOCKSIZE:
392 *f = mdev[devid].mdSecsize;
393 break;
394
395 case DKIOCSETBLOCKSIZE:
396 if (is_char) return (ENODEV); /* We can only do this for a block */
397
398 if (*f < DEV_BSIZE) return (EINVAL); /* Too short? */
399
400 mdev[devid].mdSecsize = *f; /* set the new block size */
401 break;
402
403 case DKIOCISWRITABLE:
404 *f = 1;
405 break;
406
55e303ae
A
407 case DKIOCGETBLOCKCOUNT:
408 if(!(mdev[devid].mdFlags & mdInited)) return (ENXIO);
409 *o = ((mdev[devid].mdSize << 12) + mdev[devid].mdSecsize - 1) / mdev[devid].mdSecsize;
410 break;
39236c6e
A
411
412 /*
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?
417 * What is your size?
418 */
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;
425 break;
426
55e303ae
A
427 default:
428 error = ENOTTY;
429 break;
430 }
431 return(error);
432}
433
434
435static int mdevsize(dev_t dev) {
436
437 int devid;
438
439 devid = minor(dev); /* Get minor device number */
39037602 440 if (devid >= NB_MAX_MDEVICES) return (ENXIO); /* Not valid */
55e303ae
A
441
442 if ((mdev[devid].mdFlags & mdInited) == 0) return(-1); /* Not inited yet */
443
444 return(mdev[devid].mdSecsize);
445}
446
447#include <pexpert/pexpert.h>
448
91447636 449void mdevinit(__unused int the_cnt) {
55e303ae 450
593a1d5f 451#ifdef CONFIG_MEMDEV_INSECURE
b0d623f7 452
55e303ae 453 int devid, phys;
316670eb
A
454 uint64_t base;
455 uint64_t size;
55e303ae
A
456 char *ba, *lp;
457 dev_t dev;
458
b0d623f7 459
55e303ae
A
460 ba = PE_boot_args(); /* Get the boot arguments */
461 lp = ba + 256; /* Point to the end */
462
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? */
471
472 ba = getspace(ba, lp); /* Find next white space or end */
473 continue; /* Start looking for the next one */
474 }
475
476 if(ba[0] == 'p') phys = 1; /* Set physical memory disk */
477
478 devid = ba[3] & 0xF; /* Assume digit */
479 if(ba[3] > '9') devid += 9; /* Adjust for hex digits */
480
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 */
486
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 */
492 }
493
316670eb 494 dev = mdevadd(devid, base >> 12, (unsigned)size >> 12, phys); /* Go add the device */
55e303ae 495 }
b0d623f7 496
593a1d5f 497#endif /* CONFIG_MEMDEV_INSECURE */
b0d623f7 498
55e303ae
A
499 return;
500
501}
502
593a1d5f 503#ifdef CONFIG_MEMDEV_INSECURE
b0d623f7 504
55e303ae
A
505char *nonspace(char *pos, char *end) { /* Find next non-space in string */
506
507 if(pos >= end) return end; /* Don't go past end */
508 if(pos[0] == 0) return end; /* If at null, make end */
509
510 while(1) { /* Keep going */
511 if(pos[0] != ' ') return pos; /* Leave if we found one */
512 pos++; /* Stop */
513 if(pos >= end) return end; /* Quit if we run off end */
514 }
515}
516
517char *getspace(char *pos, char *end) { /* Find next non-space in string */
518
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 */
523 pos++; /* Stop */
524 }
525}
526
316670eb 527char *cvtnum(char *pos, char *end, uint64_t *num) { /* Convert to a number */
55e303ae
A
528
529 int rad, dig;
530
531 *num = 0; /* Set answer to 0 to start */
532 rad = 10;
533
534 if(pos >= end) return end; /* Don't go past end */
535 if(pos[0] == 0) return end; /* If at null, make end */
536
537 if(pos[0] == '0' && ((pos[1] == 'x') || (pos[1] == 'x'))) { /* A hex constant? */
538 rad = 16;
539 pos += 2; /* Point to the number */
540 }
541
542 while(1) { /* Convert it */
543
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 */
553 }
554 *num = (*num * rad) + dig; /* Accumulate the number */
555 pos++; /* Step on */
556 }
557}
b0d623f7 558
593a1d5f 559#endif /* CONFIG_MEMDEV_INSECURE */
55e303ae 560
6d2010ae 561dev_t mdevadd(int devid, uint64_t base, unsigned int size, int phys) {
55e303ae
A
562
563 int i;
564
565 if(devid < 0) {
566
567 devid = -1;
39037602 568 for(i = 0; i < NB_MAX_MDEVICES; i++) { /* Search all known memory devices */
55e303ae
A
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 */
572 }
573 if(!(((base + size -1 ) < mdev[i].mdBase) || ((mdev[i].mdBase + mdev[i].mdSize - 1) < base))) { /* Is there any overlap? */
6d2010ae 574 panic("mdevadd: attempt to add overlapping memory device at %016llX-%016llX\n", mdev[i].mdBase, mdev[i].mdBase + mdev[i].mdSize - 1);
55e303ae
A
575 }
576 }
577 if(devid < 0) { /* Do we have free slots? */
39037602 578 panic("mdevadd: attempt to add more than %d memory devices\n", NB_MAX_MDEVICES);
55e303ae
A
579 }
580 }
581 else {
39037602 582 if(devid >= NB_MAX_MDEVICES) { /* Giving us something bogus? */
2d21ac55 583 panic("mdevadd: attempt to explicitly add a bogus memory device: %08X\n", devid);
55e303ae 584 }
6d2010ae 585 if(mdev[devid].mdFlags & mdInited) { /* Already there? */
2d21ac55 586 panic("mdevadd: attempt to explicitly add a previously defined memory device: %08X\n", devid);
55e303ae
A
587 }
588 }
589
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);
594 return -1;
595 }
596 }
597
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);
602 return -1;
603 }
604 }
605
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... */
613 }
614
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... */
622 }
623
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 */
6d2010ae
A
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);
55e303ae
A
631 return mdev[devid].mdBDev;
632}
633
634
635dev_t mdevlookup(int devid) {
636
39037602 637 if((devid < 0) || (devid >= NB_MAX_MDEVICES)) return -1; /* Filter any bogus requests */
55e303ae
A
638 if(!(mdev[devid].mdFlags & mdInited)) return -1; /* This one hasn't been defined */
639 return mdev[devid].mdBDev; /* Return the device number */
640}
4a3eedf9
A
641
642void mdevremoveall(void) {
643
644 int i;
645
39037602 646 for(i = 0; i < NB_MAX_MDEVICES; i++) {
4a3eedf9
A
647 if(!(mdev[i].mdFlags & mdInited)) continue; /* Ignore unused mdevs */
648
649 devfs_remove(mdev[i].mdbdevb); /* Remove the block device */
650 devfs_remove(mdev[i].mdcdevb); /* Remove the character device */
651
652 mdev[i].mdBase = 0; /* Clear the mdev's storage */
653 mdev[i].mdSize = 0;
654 mdev[i].mdSecsize = 0;
655 mdev[i].mdFlags = 0;
656 mdev[i].mdBDev = 0;
657 mdev[i].mdCDev = 0;
658 mdev[i].mdbdevb = 0;
659 mdev[i].mdcdevb = 0;
660 }
661}
39037602
A
662
663int
664mdevgetrange(int devid, uint64_t *base, uint64_t *size)
665{
666 assert(base);
667 assert(size);
668
669 /* filter invalid request */
670 if ((devid < 0) || (devid >= NB_MAX_MDEVICES)) {
671 return -1;
672 }
673
674 /* filter non-initialized memory devices */
675 if ((mdev[devid].mdFlags & mdInited) == 0) {
676 return -1;
677 }
678
679 *base = mdev[devid].mdBase << 12;
680 *size = mdev[devid].mdSize << 12;
681
682 /* make sure (base, size) is a valid range and will not overflow */
683 assert(*size < (UINT64_MAX - *base));
684
685 return 0;
686}
687