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