]>
Commit | Line | Data |
---|---|---|
55e303ae A |
1 | /* |
2 | * Copyright (c) 1988 University of Utah. | |
3 | * Copyright (c) 1990, 1993 | |
4 | * The Regents of the University of California. All rights reserved. | |
5 | * | |
6 | * This code is derived from software contributed to Berkeley by | |
7 | * the Systems Programming Group of the University of Utah Computer | |
8 | * Science Department. | |
9 | * | |
10 | * Redistribution and use in source and binary forms, with or without | |
11 | * modification, are permitted provided that the following conditions | |
12 | * are met: | |
13 | * 1. Redistributions of source code must retain the above copyright | |
14 | * notice, this list of conditions and the following disclaimer. | |
15 | * 2. Redistributions in binary form must reproduce the above copyright | |
16 | * notice, this list of conditions and the following disclaimer in the | |
17 | * documentation and/or other materials provided with the distribution. | |
18 | * 3. All advertising materials mentioning features or use of this software | |
19 | * must display the following acknowledgement: | |
20 | * This product includes software developed by the University of | |
21 | * California, Berkeley and its contributors. | |
22 | * 4. Neither the name of the University nor the names of its contributors | |
23 | * may be used to endorse or promote products derived from this software | |
24 | * without specific prior written permission. | |
25 | * | |
26 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
27 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
28 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
29 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
30 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
31 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
32 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
33 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
34 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
35 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
36 | * SUCH DAMAGE. | |
37 | * | |
38 | * from: Utah Hdr: vn.c 1.13 94/04/02 | |
39 | * | |
40 | * from: @(#)vn.c 8.6 (Berkeley) 4/1/94 | |
41 | * $FreeBSD: src/sys/dev/vn/vn.c,v 1.105.2.4 2001/11/18 07:11:00 dillon Exp $ | |
42 | */ | |
43 | ||
44 | /* | |
45 | * RAM disk driver. | |
46 | * | |
47 | * Block interface to a ramdisk. | |
48 | * | |
49 | */ | |
50 | ||
51 | ||
52 | #include <sys/param.h> | |
53 | #include <sys/systm.h> | |
54 | #include <sys/kernel.h> | |
55 | #include <sys/mount.h> | |
56 | #include <sys/namei.h> | |
57 | #include <sys/proc.h> | |
58 | #include <sys/buf.h> | |
59 | #include <sys/malloc.h> | |
60 | #include <sys/mount.h> | |
61 | #include <sys/vnode.h> | |
62 | #include <sys/fcntl.h> | |
63 | #include <sys/conf.h> | |
64 | #include <sys/disk.h> | |
65 | #include <sys/stat.h> | |
66 | ||
67 | #include <sys/vm.h> | |
68 | ||
69 | #include <vm/vm_pager.h> | |
70 | #include <vm/vm_pageout.h> | |
71 | #include <mach/memory_object_types.h> | |
72 | ||
73 | #include <miscfs/devfs/devfs.h> | |
74 | ||
75 | static open_close_fcn_t mdevopen; | |
76 | static open_close_fcn_t mdevclose; | |
77 | static psize_fcn_t mdevsize; | |
78 | static strategy_fcn_t mdevstrategy; | |
79 | static int mdevbioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p); | |
80 | static int mdevcioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p); | |
81 | static int mdevrw(dev_t dev, struct uio *uio, int ioflag); | |
82 | static char *nonspace(char *pos, char *end); | |
83 | static char *getspace(char *pos, char *end); | |
84 | static char *cvtnum(char *pos, char *end, unsigned int *num); | |
85 | ||
86 | /* | |
87 | * cdevsw | |
88 | * D_DISK we want to look like a disk | |
89 | * D_CANFREE We support B_FREEBUF | |
90 | */ | |
91 | ||
92 | static struct bdevsw mdevbdevsw = { | |
93 | /* open */ mdevopen, | |
94 | /* close */ mdevclose, | |
95 | /* strategy */ mdevstrategy, | |
96 | /* ioctl */ mdevbioctl, | |
97 | /* dump */ eno_dump, | |
98 | /* psize */ mdevsize, | |
99 | /* flags */ D_DISK, | |
100 | }; | |
101 | ||
102 | static struct cdevsw mdevcdevsw = { | |
103 | /* open */ mdevopen, | |
104 | /* close */ mdevclose, | |
105 | /* read */ mdevrw, | |
106 | /* write */ mdevrw, | |
107 | /* ioctl */ mdevcioctl, | |
108 | /* stop */ eno_stop, | |
109 | /* reset */ eno_reset, | |
110 | /* ttys */ 0, | |
111 | /* select */ eno_select, | |
112 | /* mmap */ eno_mmap, | |
113 | /* strategy */ eno_strat, | |
114 | /* getc */ eno_getc, | |
115 | /* putc */ eno_putc, | |
116 | /* flags */ D_DISK, | |
117 | }; | |
118 | ||
119 | struct mdev { | |
120 | vm_offset_t mdBase; /* file size in bytes */ | |
121 | uint32_t mdSize; /* file size in bytes */ | |
122 | int mdFlags; /* flags */ | |
123 | int mdSecsize; /* sector size */ | |
124 | int mdBDev; /* Block device number */ | |
125 | int mdCDev; /* Character device number */ | |
126 | void * mdbdevb; | |
127 | void * mdcdevb; | |
128 | } mdev[16]; | |
129 | ||
130 | /* mdFlags */ | |
131 | #define mdInited 0x01 /* This device defined */ | |
132 | #define mdRO 0x02 /* This device is read-only */ | |
133 | #define mdPhys 0x04 /* This device is in physical memory */ | |
134 | ||
135 | int mdevBMajor = -1; | |
136 | int mdevCMajor = -1; | |
137 | ||
138 | static int mdevioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p, int is_char); | |
139 | dev_t mdevadd(int devid, ppnum_t base, unsigned int size, int phys); | |
140 | dev_t mdevlookup(int devid); | |
141 | ||
142 | static int mdevclose(dev_t dev, int flags, int devtype, struct proc *p) { | |
143 | return (0); | |
144 | } | |
145 | ||
146 | static int mdevopen(dev_t dev, int flags, int devtype, struct proc *p) { | |
147 | ||
148 | int devid; | |
149 | ||
150 | devid = minor(dev); /* Get minor device number */ | |
151 | ||
152 | if (devid > 16) return (ENXIO); /* Not valid */ | |
153 | ||
154 | if ((flags & FWRITE) && (mdev[devid].mdFlags & mdRO)) return (EACCES); /* Currently mounted RO */ | |
155 | ||
156 | return(0); | |
157 | } | |
158 | ||
159 | static int mdevrw(dev_t dev, struct uio *uio, int ioflag) { | |
160 | int status; | |
161 | int unit; | |
162 | addr64_t mdata; | |
163 | int devid; | |
164 | enum uio_seg saveflag; | |
165 | ||
166 | devid = minor(dev); /* Get minor device number */ | |
167 | ||
168 | if (devid > 16) return (ENXIO); /* Not valid */ | |
169 | if (!(mdev[devid].mdFlags & mdInited)) return (ENXIO); /* Have we actually been defined yet? */ | |
170 | ||
171 | mdata = ((addr64_t)mdev[devid].mdBase << 12) + uio->uio_offset; /* Point to the area in "file" */ | |
172 | ||
173 | saveflag = uio->uio_segflg; /* Remember what the request is */ | |
174 | if (mdev[devid].mdFlags & mdPhys) uio->uio_segflg = UIO_PHYS_USERSPACE; /* Make sure we are moving from physical ram if physical device */ | |
175 | status = uiomove64(mdata, uio->uio_resid, uio); /* Move the data */ | |
176 | uio->uio_segflg = saveflag; /* Restore the flag */ | |
177 | ||
178 | return (status); | |
179 | } | |
180 | ||
181 | static void mdevstrategy(struct buf *bp) { | |
182 | int unmap; | |
183 | unsigned int sz, left, lop, csize; | |
184 | kern_return_t ret; | |
185 | vm_offset_t vaddr, blkoff; | |
186 | struct buf *tbuf; | |
187 | int devid; | |
188 | addr64_t paddr, fvaddr; | |
189 | ppnum_t pp; | |
190 | ||
191 | devid = minor(bp->b_dev); /* Get minor device number */ | |
192 | ||
193 | if ((mdev[devid].mdFlags & mdInited) == 0) { /* Have we actually been defined yet? */ | |
194 | bp->b_error = ENXIO; | |
195 | bp->b_flags |= B_ERROR; | |
196 | biodone(bp); | |
197 | return; | |
198 | } | |
199 | ||
200 | bp->b_resid = bp->b_bcount; /* Set byte count */ | |
201 | ||
202 | blkoff = bp->b_blkno * mdev[devid].mdSecsize; /* Get offset into file */ | |
203 | ||
204 | /* | |
205 | * Note that reading past end is an error, but reading at end is an EOF. For these | |
206 | * we just return with b_resid == b_bcount. | |
207 | */ | |
208 | ||
209 | if (blkoff >= (mdev[devid].mdSize << 12)) { /* Are they trying to read/write at/after end? */ | |
210 | if(blkoff != (mdev[devid].mdSize << 12)) { /* Are we trying to read after EOF? */ | |
211 | bp->b_error = EINVAL; /* Yeah, this is an error */ | |
212 | bp->b_flags |= B_ERROR | B_INVAL; | |
213 | } | |
214 | biodone(bp); /* Return */ | |
215 | return; | |
216 | } | |
217 | ||
218 | if ((blkoff + bp->b_bcount) > (mdev[devid].mdSize << 12)) { /* Will this read go past end? */ | |
219 | bp->b_bcount = ((mdev[devid].mdSize << 12) - blkoff); /* Yes, trim to max */ | |
220 | } | |
221 | ||
222 | vaddr = 0; /* Assume not mapped yet */ | |
223 | unmap = 0; | |
224 | ||
225 | if (bp->b_flags & B_VECTORLIST) { /* Do we have a list of UPLs? */ | |
226 | tbuf = (struct buf *)bp->b_real_bp; /* Get this for C's inadequacies */ | |
227 | if((bp->b_flags & B_NEED_IODONE) && /* If we have a UPL, is it already mapped? */ | |
228 | tbuf && | |
229 | tbuf->b_data) { | |
230 | vaddr = (vm_offset_t)tbuf->b_data; /* We already have this mapped in, get base address */ | |
231 | } | |
232 | else { /* Not mapped yet */ | |
233 | ret = ubc_upl_map(bp->b_pagelist, &vaddr); /* Map it in */ | |
234 | if(ret != KERN_SUCCESS) panic("ramstrategy: ubc_upl_map failed, rc = %08X\n", ret); | |
235 | unmap = 1; /* Remember to unmap later */ | |
236 | } | |
237 | vaddr = vaddr += bp->b_uploffset; /* Calculate actual vaddr */ | |
238 | } | |
239 | else vaddr = (vm_offset_t)bp->b_data; /* No UPL, we already have address */ | |
240 | ||
241 | fvaddr = (mdev[devid].mdBase << 12) + blkoff; /* Point to offset into ram disk */ | |
242 | ||
243 | if(bp->b_flags & B_READ) { /* Is this a read? */ | |
244 | if(!(mdev[devid].mdFlags & mdPhys)) { /* Physical mapped disk? */ | |
245 | bcopy((void *)((uintptr_t)fvaddr), | |
246 | (void *)vaddr, (size_t)bp->b_bcount); /* This is virtual, just get the data */ | |
247 | } | |
248 | else { | |
249 | left = bp->b_bcount; /* Init the amount left to copy */ | |
250 | while(left) { /* Go until it is all copied */ | |
251 | ||
252 | lop = min((4096 - (vaddr & 4095)), (4096 - (fvaddr & 4095))); /* Get smallest amount left on sink and source */ | |
253 | csize = min(lop, left); /* Don't move more than we need to */ | |
254 | ||
255 | pp = pmap_find_phys(kernel_pmap, (addr64_t)((unsigned int)vaddr)); /* Get the sink physical address */ | |
256 | if(!pp) { /* Not found, what gives? */ | |
257 | panic("mdevstrategy: sink address %016llX not mapped\n", (addr64_t)((unsigned int)vaddr)); | |
258 | } | |
259 | paddr = (addr64_t)(((addr64_t)pp << 12) | (addr64_t)(vaddr & 4095)); /* Get actual address */ | |
260 | bcopy_phys(fvaddr, paddr, csize); /* Copy this on in */ | |
261 | mapping_set_mod(paddr >> 12); /* Make sure we know that it is modified */ | |
262 | ||
263 | left = left - csize; /* Calculate what is left */ | |
264 | vaddr = vaddr + csize; /* Move to next sink address */ | |
265 | fvaddr = fvaddr + csize; /* Bump to next physical address */ | |
266 | } | |
267 | } | |
268 | } | |
269 | else { /* This is a write */ | |
270 | if(!(mdev[devid].mdFlags & mdPhys)) { /* Physical mapped disk? */ | |
271 | bcopy((void *)vaddr, (void *)((uintptr_t)fvaddr), | |
272 | (size_t)bp->b_bcount); /* This is virtual, just put the data */ | |
273 | } | |
274 | else { | |
275 | left = bp->b_bcount; /* Init the amount left to copy */ | |
276 | while(left) { /* Go until it is all copied */ | |
277 | ||
278 | lop = min((4096 - (vaddr & 4095)), (4096 - (fvaddr & 4095))); /* Get smallest amount left on sink and source */ | |
279 | csize = min(lop, left); /* Don't move more than we need to */ | |
280 | ||
281 | pp = pmap_find_phys(kernel_pmap, (addr64_t)((unsigned int)vaddr)); /* Get the source physical address */ | |
282 | if(!pp) { /* Not found, what gives? */ | |
283 | panic("mdevstrategy: source address %016llX not mapped\n", (addr64_t)((unsigned int)vaddr)); | |
284 | } | |
285 | paddr = (addr64_t)(((addr64_t)pp << 12) | (addr64_t)(vaddr & 4095)); /* Get actual address */ | |
286 | ||
287 | bcopy_phys(paddr, fvaddr, csize); /* Move this on out */ | |
288 | ||
289 | left = left - csize; /* Calculate what is left */ | |
290 | vaddr = vaddr + csize; /* Move to next sink address */ | |
291 | fvaddr = fvaddr + csize; /* Bump to next physical address */ | |
292 | } | |
293 | } | |
294 | } | |
295 | ||
296 | if (unmap) { /* Do we need to unmap this? */ | |
297 | ubc_upl_unmap(bp->b_pagelist); /* Yes, unmap it */ | |
298 | } | |
299 | ||
300 | bp->b_resid = 0; /* Nothing more to do */ | |
301 | biodone(bp); /* Say we've finished */ | |
302 | } | |
303 | ||
304 | static int mdevbioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p) { | |
305 | return (mdevioctl(dev, cmd, data, flag, p, 0)); | |
306 | } | |
307 | ||
308 | static int mdevcioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p) { | |
309 | return (mdevioctl(dev, cmd, data, flag, p, 1)); | |
310 | } | |
311 | ||
312 | static int mdevioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p, int is_char) { | |
313 | ||
314 | int error; | |
315 | u_long *f; | |
316 | u_int64_t *o; | |
317 | int devid; | |
318 | ||
319 | devid = minor(dev); /* Get minor device number */ | |
320 | ||
321 | if (devid > 16) return (ENXIO); /* Not valid */ | |
322 | ||
323 | error = suser(p->p_ucred, &p->p_acflag); /* Are we superman? */ | |
324 | if (error) return (error); /* Nope... */ | |
325 | ||
326 | f = (u_long*)data; | |
327 | o = (u_int64_t *)data; | |
328 | ||
329 | switch (cmd) { | |
330 | ||
331 | case DKIOCGETMAXBLOCKCOUNTREAD: | |
332 | *o = 32; | |
333 | break; | |
334 | ||
335 | case DKIOCGETMAXBLOCKCOUNTWRITE: | |
336 | *o = 32; | |
337 | break; | |
338 | ||
339 | case DKIOCGETMAXSEGMENTCOUNTREAD: | |
340 | *o = 32; | |
341 | break; | |
342 | ||
343 | case DKIOCGETMAXSEGMENTCOUNTWRITE: | |
344 | *o = 32; | |
345 | break; | |
346 | ||
347 | case DKIOCGETBLOCKSIZE: | |
348 | *f = mdev[devid].mdSecsize; | |
349 | break; | |
350 | ||
351 | case DKIOCSETBLOCKSIZE: | |
352 | if (is_char) return (ENODEV); /* We can only do this for a block */ | |
353 | ||
354 | if (*f < DEV_BSIZE) return (EINVAL); /* Too short? */ | |
355 | ||
356 | mdev[devid].mdSecsize = *f; /* set the new block size */ | |
357 | break; | |
358 | ||
359 | case DKIOCISWRITABLE: | |
360 | *f = 1; | |
361 | break; | |
362 | ||
363 | case DKIOCGETBLOCKCOUNT32: | |
364 | if(!(mdev[devid].mdFlags & mdInited)) return (ENXIO); | |
365 | *f = ((mdev[devid].mdSize << 12) + mdev[devid].mdSecsize - 1) / mdev[devid].mdSecsize; | |
366 | break; | |
367 | ||
368 | case DKIOCGETBLOCKCOUNT: | |
369 | if(!(mdev[devid].mdFlags & mdInited)) return (ENXIO); | |
370 | *o = ((mdev[devid].mdSize << 12) + mdev[devid].mdSecsize - 1) / mdev[devid].mdSecsize; | |
371 | break; | |
372 | ||
373 | default: | |
374 | error = ENOTTY; | |
375 | break; | |
376 | } | |
377 | return(error); | |
378 | } | |
379 | ||
380 | ||
381 | static int mdevsize(dev_t dev) { | |
382 | ||
383 | int devid; | |
384 | ||
385 | devid = minor(dev); /* Get minor device number */ | |
386 | if (devid > 16) return (ENXIO); /* Not valid */ | |
387 | ||
388 | if ((mdev[devid].mdFlags & mdInited) == 0) return(-1); /* Not inited yet */ | |
389 | ||
390 | return(mdev[devid].mdSecsize); | |
391 | } | |
392 | ||
393 | #include <pexpert/pexpert.h> | |
394 | ||
395 | void mdevinit(int cnt) { | |
396 | ||
397 | int devid, phys; | |
398 | ppnum_t base; | |
399 | unsigned int size; | |
400 | char *ba, *lp; | |
401 | dev_t dev; | |
402 | ||
403 | ||
404 | ba = PE_boot_args(); /* Get the boot arguments */ | |
405 | lp = ba + 256; /* Point to the end */ | |
406 | ||
407 | while(1) { /* Step through, looking for our keywords */ | |
408 | phys = 0; /* Assume virtual memory device */ | |
409 | ba = nonspace(ba, lp); /* Find non-space */ | |
410 | if(ba >= lp) return; /* We are done if no more... */ | |
411 | if(((ba[0] != 'v') && (ba[0] != 'p')) | |
412 | || (ba[1] != 'm') || (ba[2] != 'd') || (ba[4] != '=') | |
413 | || (ba[3] < '0') || (ba[3] > 'f') | |
414 | || ((ba[3] > '9') && (ba[3] < 'a'))) { /* Is this of form "vmdx=" or "pmdx=" where x is hex digit? */ | |
415 | ||
416 | ba = getspace(ba, lp); /* Find next white space or end */ | |
417 | continue; /* Start looking for the next one */ | |
418 | } | |
419 | ||
420 | if(ba[0] == 'p') phys = 1; /* Set physical memory disk */ | |
421 | ||
422 | devid = ba[3] & 0xF; /* Assume digit */ | |
423 | if(ba[3] > '9') devid += 9; /* Adjust for hex digits */ | |
424 | ||
425 | ba = &ba[5]; /* Step past keyword */ | |
426 | ba = cvtnum(ba, lp, &base); /* Convert base of memory disk */ | |
427 | if(ba >= lp) return; /* Malformed one at the end, leave */ | |
428 | if(ba[0] != '.') continue; /* If not length separater, try next... */ | |
429 | if(base & 0xFFF) continue; /* Only allow page aligned stuff */ | |
430 | ||
431 | ba++; /* Step past '.' */ | |
432 | ba = cvtnum(ba, lp, &size); /* Try to convert it */ | |
433 | if(!size || (size & 0xFFF)) continue; /* Allow only non-zer page size multiples */ | |
434 | if(ba < lp) { /* If we are not at end, check end character */ | |
435 | if((ba[0] != ' ') && (ba[0] != 0)) continue; /* End must be null or space */ | |
436 | } | |
437 | ||
438 | dev = mdevadd(devid, base >> 12, size >> 12, phys); /* Go add the device */ | |
439 | } | |
440 | ||
441 | return; | |
442 | ||
443 | } | |
444 | ||
445 | char *nonspace(char *pos, char *end) { /* Find next non-space in string */ | |
446 | ||
447 | if(pos >= end) return end; /* Don't go past end */ | |
448 | if(pos[0] == 0) return end; /* If at null, make end */ | |
449 | ||
450 | while(1) { /* Keep going */ | |
451 | if(pos[0] != ' ') return pos; /* Leave if we found one */ | |
452 | pos++; /* Stop */ | |
453 | if(pos >= end) return end; /* Quit if we run off end */ | |
454 | } | |
455 | } | |
456 | ||
457 | char *getspace(char *pos, char *end) { /* Find next non-space in string */ | |
458 | ||
459 | while(1) { /* Keep going */ | |
460 | if(pos >= end) return end; /* Don't go past end */ | |
461 | if(pos[0] == 0) return end; /* Leave if we hit null */ | |
462 | if(pos[0] == ' ') return pos; /* Leave if we found one */ | |
463 | pos++; /* Stop */ | |
464 | } | |
465 | } | |
466 | ||
467 | char *cvtnum(char *pos, char *end, unsigned int *num) { /* Convert to a number */ | |
468 | ||
469 | int rad, dig; | |
470 | ||
471 | *num = 0; /* Set answer to 0 to start */ | |
472 | rad = 10; | |
473 | ||
474 | if(pos >= end) return end; /* Don't go past end */ | |
475 | if(pos[0] == 0) return end; /* If at null, make end */ | |
476 | ||
477 | if(pos[0] == '0' && ((pos[1] == 'x') || (pos[1] == 'x'))) { /* A hex constant? */ | |
478 | rad = 16; | |
479 | pos += 2; /* Point to the number */ | |
480 | } | |
481 | ||
482 | while(1) { /* Convert it */ | |
483 | ||
484 | if(pos >= end) return end; /* Don't go past end */ | |
485 | if(pos[0] == 0) return end; /* If at null, make end */ | |
486 | if(pos[0] < '0') return pos; /* Leave if non-digit */ | |
487 | dig = pos[0] & 0xF; /* Extract digit */ | |
488 | if(pos[0] > '9') { /* Is it bigger than 9? */ | |
489 | if(rad == 10) return pos; /* Leave if not base 10 */ | |
490 | if(!(((pos[0] >= 'A') && (pos[0] <= 'F')) | |
491 | || ((pos[0] >= 'a') && (pos[0] <= 'f')))) return pos; /* Leave if bogus char */ | |
492 | dig = dig + 9; /* Adjust for character */ | |
493 | } | |
494 | *num = (*num * rad) + dig; /* Accumulate the number */ | |
495 | pos++; /* Step on */ | |
496 | } | |
497 | } | |
498 | ||
499 | dev_t mdevadd(int devid, ppnum_t base, unsigned int size, int phys) { | |
500 | ||
501 | int i; | |
502 | ||
503 | if(devid < 0) { | |
504 | ||
505 | devid = -1; | |
506 | for(i = 0; i < 16; i++) { /* Search all known memory devices */ | |
507 | if(!(mdev[i].mdFlags & mdInited)) { /* Is this a free one? */ | |
508 | if(devid < 0)devid = i; /* Remember first free one */ | |
509 | continue; /* Skip check */ | |
510 | } | |
511 | if(!(((base + size -1 ) < mdev[i].mdBase) || ((mdev[i].mdBase + mdev[i].mdSize - 1) < base))) { /* Is there any overlap? */ | |
512 | panic("mdevadd: attempt to add overlapping memory device at %08X-%08X\n", mdev[i].mdBase, mdev[i].mdBase + mdev[i].mdSize - 1); | |
513 | } | |
514 | } | |
515 | if(devid < 0) { /* Do we have free slots? */ | |
516 | panic("mdevadd: attempt to add more than 16 memory devices\n"); | |
517 | } | |
518 | } | |
519 | else { | |
520 | if(devid >= 16) { /* Giving us something bogus? */ | |
521 | panic("mdevadd: attempt to explicitly add a bogus memory device: &08X\n", devid); | |
522 | } | |
523 | if(mdev[devid].mdFlags &mdInited) { /* Already there? */ | |
524 | panic("mdevadd: attempt to explicitly add a previously defined memory device: &08X\n", devid); | |
525 | } | |
526 | } | |
527 | ||
528 | if(mdevBMajor < 0) { /* Have we gotten a major number yet? */ | |
529 | mdevBMajor = bdevsw_add(-1, &mdevbdevsw); /* Add to the table and figure out a major number */ | |
530 | if (mdevBMajor < 0) { | |
531 | printf("mdevadd: error - bdevsw_add() returned %d\n", mdevBMajor); | |
532 | return -1; | |
533 | } | |
534 | } | |
535 | ||
536 | if(mdevCMajor < 0) { /* Have we gotten a major number yet? */ | |
537 | mdevCMajor = cdevsw_add_with_bdev(-1, &mdevcdevsw, mdevBMajor); /* Add to the table and figure out a major number */ | |
538 | if (mdevCMajor < 0) { | |
539 | printf("ramdevice_init: error - cdevsw_add() returned %d\n", mdevCMajor); | |
540 | return -1; | |
541 | } | |
542 | } | |
543 | ||
544 | mdev[devid].mdBDev = makedev(mdevBMajor, devid); /* Get the device number */ | |
545 | mdev[devid].mdbdevb = devfs_make_node(mdev[devid].mdBDev, DEVFS_BLOCK, /* Make the node */ | |
546 | UID_ROOT, GID_OPERATOR, | |
547 | 0600, "md%d", devid); | |
548 | if (mdev[devid].mdbdevb == NULL) { /* Did we make one? */ | |
549 | printf("mdevadd: devfs_make_node for block failed!\n"); | |
550 | return -1; /* Nope... */ | |
551 | } | |
552 | ||
553 | mdev[devid].mdCDev = makedev(mdevCMajor, devid); /* Get the device number */ | |
554 | mdev[devid].mdcdevb = devfs_make_node(mdev[devid].mdCDev, DEVFS_CHAR, /* Make the node */ | |
555 | UID_ROOT, GID_OPERATOR, | |
556 | 0600, "rmd%d", devid); | |
557 | if (mdev[devid].mdcdevb == NULL) { /* Did we make one? */ | |
558 | printf("mdevadd: devfs_make_node for character failed!\n"); | |
559 | return -1; /* Nope... */ | |
560 | } | |
561 | ||
562 | mdev[devid].mdBase = base; /* Set the base address of ram disk */ | |
563 | mdev[devid].mdSize = size; /* Set the length of the ram disk */ | |
564 | mdev[devid].mdSecsize = DEV_BSIZE; /* Set starting block size */ | |
565 | if(phys) mdev[devid].mdFlags |= mdPhys; /* Show that we are in physical memory */ | |
566 | mdev[devid].mdFlags |= mdInited; /* Show we are all set up */ | |
567 | printf("Added memory device md%x/rmd%x (%08X/%08X) at %08X for %08X\n", | |
568 | devid, devid, mdev[devid].mdBDev, mdev[devid].mdCDev, base << 12, size << 12); | |
569 | return mdev[devid].mdBDev; | |
570 | } | |
571 | ||
572 | ||
573 | dev_t mdevlookup(int devid) { | |
574 | ||
575 | if((devid < 0) || (devid > 15)) return -1; /* Filter any bogus requests */ | |
576 | if(!(mdev[devid].mdFlags & mdInited)) return -1; /* This one hasn't been defined */ | |
577 | return mdev[devid].mdBDev; /* Return the device number */ | |
578 | } |