Remove building with NOCRYPTO option
[minix.git] / minix / drivers / storage / memory / memory.c
blobffa6b49abe94b68536185982657ce93a2738ecbd
1 /* This file contains the device dependent part of the drivers for the
2 * following special files:
3 * /dev/ram - RAM disk
4 * /dev/mem - absolute memory
5 * /dev/kmem - kernel virtual memory
6 * /dev/null - null device (data sink)
7 * /dev/boot - boot device loaded from boot image
8 * /dev/zero - null byte stream generator
9 * /dev/imgrd - boot image RAM disk
11 * Changes:
12 * Apr 29, 2005 added null byte generator (Jorrit N. Herder)
13 * Apr 09, 2005 added support for boot device (Jorrit N. Herder)
14 * Jul 26, 2004 moved RAM driver to user-space (Jorrit N. Herder)
15 * Apr 20, 1992 device dependent/independent split (Kees J. Bot)
18 #include <assert.h>
19 #include <minix/drivers.h>
20 #include <minix/chardriver.h>
21 #include <minix/blockdriver.h>
22 #include <sys/ioc_memory.h>
23 #include <minix/ds.h>
24 #include <minix/vm.h>
25 #include <machine/param.h>
26 #include <machine/vmparam.h>
27 #include <sys/mman.h>
28 #include "kernel/const.h"
29 #include "kernel/config.h"
30 #include "kernel/type.h"
32 #include <machine/vm.h>
34 #include "local.h"
36 /* ramdisks (/dev/ram*) */
37 #define RAMDISKS 6
39 #define RAM_DEV_LAST (RAM_DEV_FIRST+RAMDISKS-1)
41 #define NR_DEVS (7+RAMDISKS) /* number of minor devices */
43 static struct device m_geom[NR_DEVS]; /* base and size of each device */
44 static vir_bytes m_vaddrs[NR_DEVS];
46 static int openct[NR_DEVS];
48 static ssize_t m_char_read(devminor_t minor, u64_t position, endpoint_t endpt,
49 cp_grant_id_t grant, size_t size, int flags, cdev_id_t id);
50 static ssize_t m_char_write(devminor_t minor, u64_t position, endpoint_t endpt,
51 cp_grant_id_t grant, size_t size, int flags, cdev_id_t id);
52 static int m_char_open(devminor_t minor, int access, endpoint_t user_endpt);
53 static int m_char_close(devminor_t minor);
55 static struct device *m_block_part(devminor_t minor);
56 static ssize_t m_block_transfer(devminor_t minor, int do_write, u64_t position,
57 endpoint_t endpt, iovec_t *iov, unsigned int nr_req, int flags);
58 static int m_block_open(devminor_t minor, int access);
59 static int m_block_close(devminor_t minor);
60 static int m_block_ioctl(devminor_t minor, unsigned long request, endpoint_t
61 endpt, cp_grant_id_t grant, endpoint_t user_endpt);
63 /* Entry points to the CHARACTER part of this driver. */
64 static struct chardriver m_cdtab = {
65 .cdr_open = m_char_open, /* open device */
66 .cdr_close = m_char_close, /* close device */
67 .cdr_read = m_char_read, /* read from device */
68 .cdr_write = m_char_write /* write to device */
71 /* Entry points to the BLOCK part of this driver. */
72 static struct blockdriver m_bdtab = {
73 .bdr_type = BLOCKDRIVER_TYPE_DISK,/* handle partition requests */
74 .bdr_open = m_block_open, /* open device */
75 .bdr_close = m_block_close, /* nothing on a close */
76 .bdr_transfer = m_block_transfer, /* do the I/O */
77 .bdr_ioctl = m_block_ioctl, /* ram disk I/O control */
78 .bdr_part = m_block_part /* return partition information */
81 /* SEF functions and variables. */
82 static void sef_local_startup(void);
83 static int sef_cb_init_fresh(int type, sef_init_info_t *info);
85 /*===========================================================================*
86 * main *
87 *===========================================================================*/
88 int main(void)
90 message msg;
91 int r, ipc_status;
93 /* SEF local startup. */
94 sef_local_startup();
96 /* The receive loop. */
97 for (;;) {
98 if ((r = driver_receive(ANY, &msg, &ipc_status)) != OK)
99 panic("memory: driver_receive failed (%d)", r);
101 if (IS_BDEV_RQ(msg.m_type))
102 blockdriver_process(&m_bdtab, &msg, ipc_status);
103 else
104 chardriver_process(&m_cdtab, &msg, ipc_status);
107 return(OK);
110 /*===========================================================================*
111 * sef_local_startup *
112 *===========================================================================*/
113 static void sef_local_startup()
115 /* Register init callbacks. */
116 sef_setcb_init_fresh(sef_cb_init_fresh);
117 sef_setcb_init_restart(SEF_CB_INIT_RESTART_STATEFUL);
119 /* Let SEF perform startup. */
120 sef_startup();
123 /*===========================================================================*
124 * sef_cb_init_fresh *
125 *===========================================================================*/
126 static int sef_cb_init_fresh(int type, sef_init_info_t *UNUSED(info))
128 /* Initialize the memory driver. */
129 int i;
130 #if 0
131 struct kinfo kinfo; /* kernel information */
132 int s;
134 if (OK != (s=sys_getkinfo(&kinfo))) {
135 panic("Couldn't get kernel information: %d", s);
138 /* Map in kernel memory for /dev/kmem. */
139 m_geom[KMEM_DEV].dv_base = kinfo.kmem_base;
140 m_geom[KMEM_DEV].dv_size = kinfo.kmem_size;
141 if((m_vaddrs[KMEM_DEV] = vm_map_phys(SELF, (void *) kinfo.kmem_base,
142 kinfo.kmem_size)) == MAP_FAILED) {
143 printf("MEM: Couldn't map in /dev/kmem.");
145 #endif
147 /* Ramdisk image built into the memory driver */
148 m_geom[IMGRD_DEV].dv_base= 0;
149 m_geom[IMGRD_DEV].dv_size= imgrd_size;
150 m_vaddrs[IMGRD_DEV] = (vir_bytes) imgrd;
152 for(i = 0; i < NR_DEVS; i++)
153 openct[i] = 0;
155 /* Set up memory range for /dev/mem. */
156 m_geom[MEM_DEV].dv_base = 0;
157 m_geom[MEM_DEV].dv_size = 0xffffffffULL;
159 m_vaddrs[MEM_DEV] = (vir_bytes) MAP_FAILED; /* we are not mapping this in. */
161 chardriver_announce();
162 blockdriver_announce(type);
164 return(OK);
167 /*===========================================================================*
168 * m_is_block *
169 *===========================================================================*/
170 static int m_is_block(devminor_t minor)
172 /* Return TRUE iff the given minor device number is for a block device. */
174 switch (minor) {
175 case MEM_DEV:
176 case KMEM_DEV:
177 case NULL_DEV:
178 case ZERO_DEV:
179 return FALSE;
181 default:
182 return TRUE;
186 /*===========================================================================*
187 * m_transfer_kmem *
188 *===========================================================================*/
189 static ssize_t m_transfer_kmem(devminor_t minor, int do_write, u64_t position,
190 endpoint_t endpt, cp_grant_id_t grant, size_t size)
192 /* Transfer from or to the KMEM device. */
193 u64_t dv_size, dev_vaddr;
194 int r;
196 dv_size = m_geom[minor].dv_size;
197 dev_vaddr = m_vaddrs[minor];
199 if (!dev_vaddr || dev_vaddr == (vir_bytes) MAP_FAILED) {
200 printf("MEM: dev %d not initialized\n", minor);
201 return EIO;
204 if (position >= dv_size) return 0; /* check for EOF */
205 if (position + size > dv_size) size = dv_size - position;
207 if (!do_write) /* copy actual data */
208 r = sys_safecopyto(endpt, grant, 0, dev_vaddr + position, size);
209 else
210 r = sys_safecopyfrom(endpt, grant, 0, dev_vaddr + position, size);
212 return (r != OK) ? r : size;
215 /*===========================================================================*
216 * m_transfer_mem *
217 *===========================================================================*/
218 static ssize_t m_transfer_mem(devminor_t minor, int do_write, u64_t position,
219 endpoint_t endpt, cp_grant_id_t grant, size_t size)
221 /* Transfer from or to the MEM device. */
222 static int any_mapped = 0;
223 static phys_bytes pagestart_mapped;
224 static char *vaddr;
225 phys_bytes mem_phys, pagestart;
226 size_t off, page_off, subcount;
227 u64_t dv_size;
228 int r;
230 dv_size = m_geom[minor].dv_size;
231 if (position >= dv_size) return 0; /* check for EOF */
232 if (position + size > dv_size) size = dv_size - position;
234 /* Physical copying. Only used to access entire memory.
235 * Transfer one 'page window' at a time.
237 off = 0;
238 while (off < size) {
239 mem_phys = (phys_bytes) position;
241 page_off = (size_t) (mem_phys % PAGE_SIZE);
242 pagestart = mem_phys - page_off;
244 /* All memory to the map call has to be page-aligned.
245 * Don't have to map same page over and over.
247 if (!any_mapped || pagestart_mapped != pagestart) {
248 if (any_mapped) {
249 if (vm_unmap_phys(SELF, vaddr, PAGE_SIZE) != OK)
250 panic("vm_unmap_phys failed");
251 any_mapped = 0;
254 vaddr = vm_map_phys(SELF, (void *) pagestart, PAGE_SIZE);
255 if (vaddr == MAP_FAILED) {
256 printf("memory: vm_map_phys failed\n");
257 return ENOMEM;
259 any_mapped = 1;
260 pagestart_mapped = pagestart;
263 /* how much to be done within this page. */
264 subcount = PAGE_SIZE - page_off;
265 if (subcount > size)
266 subcount = size;
268 if (!do_write) /* copy data */
269 r = sys_safecopyto(endpt, grant, off,
270 (vir_bytes) vaddr + page_off, subcount);
271 else
272 r = sys_safecopyfrom(endpt, grant, off,
273 (vir_bytes) vaddr + page_off, subcount);
274 if (r != OK)
275 return r;
277 position += subcount;
278 off += subcount;
281 return off;
284 /*===========================================================================*
285 * m_char_read *
286 *===========================================================================*/
287 static ssize_t m_char_read(devminor_t minor, u64_t position, endpoint_t endpt,
288 cp_grant_id_t grant, size_t size, int UNUSED(flags),
289 cdev_id_t UNUSED(id))
291 /* Read from one of the driver's character devices. */
292 ssize_t r;
294 /* Check if the minor device number is ok. */
295 if (minor < 0 || minor >= NR_DEVS || m_is_block(minor)) return ENXIO;
297 switch (minor) {
298 case NULL_DEV:
299 r = 0; /* always at EOF */
300 break;
302 case ZERO_DEV:
303 /* Fill the target area with zeroes. In fact, let the kernel do it! */
304 if ((r = sys_safememset(endpt, grant, 0, '\0', size)) == OK)
305 r = size;
306 break;
308 case KMEM_DEV:
309 r = m_transfer_kmem(minor, FALSE, position, endpt, grant, size);
310 break;
312 case MEM_DEV:
313 r = m_transfer_mem(minor, FALSE, position, endpt, grant, size);
314 break;
316 default:
317 panic("unknown character device %d", minor);
320 return r;
323 /*===========================================================================*
324 * m_char_write *
325 *===========================================================================*/
326 static ssize_t m_char_write(devminor_t minor, u64_t position, endpoint_t endpt,
327 cp_grant_id_t grant, size_t size, int UNUSED(flags),
328 cdev_id_t UNUSED(id))
330 /* Write to one of the driver's character devices. */
331 ssize_t r;
333 /* Check if the minor device number is ok. */
334 if (minor < 0 || minor >= NR_DEVS || m_is_block(minor)) return ENXIO;
336 switch (minor) {
337 case NULL_DEV:
338 case ZERO_DEV:
339 r = size; /* just eat everything */
340 break;
342 case KMEM_DEV:
343 r = m_transfer_kmem(minor, TRUE, position, endpt, grant, size);
344 break;
346 case MEM_DEV:
347 r = m_transfer_mem(minor, TRUE, position, endpt, grant, size);
348 break;
350 default:
351 panic("unknown character device %d", minor);
354 return r;
357 /*===========================================================================*
358 * m_char_open *
359 *===========================================================================*/
360 static int m_char_open(devminor_t minor, int access, endpoint_t user_endpt)
362 /* Open a memory character device. */
364 /* Check if the minor device number is ok. */
365 if (minor < 0 || minor >= NR_DEVS || m_is_block(minor)) return ENXIO;
367 #if defined(__i386__)
368 if (minor == MEM_DEV)
370 int r = sys_enable_iop(user_endpt);
371 if (r != OK)
373 printf("m_char_open: sys_enable_iop failed for %d: %d\n",
374 user_endpt, r);
375 return r;
378 #endif
380 openct[minor]++;
382 return(OK);
385 /*===========================================================================*
386 * m_char_close *
387 *===========================================================================*/
388 static int m_char_close(devminor_t minor)
390 /* Close a memory character device. */
392 if (minor < 0 || minor >= NR_DEVS || m_is_block(minor)) return ENXIO;
394 if(openct[minor] < 1) {
395 printf("MEMORY: closing unopened device %d\n", minor);
396 return(EINVAL);
398 openct[minor]--;
400 return(OK);
403 /*===========================================================================*
404 * m_block_part *
405 *===========================================================================*/
406 static struct device *m_block_part(devminor_t minor)
408 /* Prepare for I/O on a device: check if the minor device number is ok. */
409 if (minor < 0 || minor >= NR_DEVS || !m_is_block(minor)) return(NULL);
411 return(&m_geom[minor]);
414 /*===========================================================================*
415 * m_block_transfer *
416 *===========================================================================*/
417 static int m_block_transfer(
418 devminor_t minor, /* minor device number */
419 int do_write, /* read or write? */
420 u64_t position, /* offset on device to read or write */
421 endpoint_t endpt, /* process doing the request */
422 iovec_t *iov, /* pointer to read or write request vector */
423 unsigned int nr_req, /* length of request vector */
424 int UNUSED(flags) /* transfer flags */
427 /* Read or write one the driver's block devices. */
428 unsigned count;
429 vir_bytes vir_offset = 0;
430 struct device *dv;
431 u64_t dv_size;
432 int r;
433 vir_bytes dev_vaddr;
434 cp_grant_id_t grant;
435 ssize_t total = 0;
437 /* Get minor device information. */
438 if ((dv = m_block_part(minor)) == NULL) return(ENXIO);
439 dv_size = dv->dv_size;
440 dev_vaddr = m_vaddrs[minor];
442 if (ex64hi(position) != 0)
443 return OK; /* Beyond EOF */
445 while (nr_req > 0) {
447 /* How much to transfer and where to / from. */
448 count = iov->iov_size;
449 grant = (cp_grant_id_t) iov->iov_addr;
451 /* Virtual copying. For RAM disks and internal FS. */
452 if(!dev_vaddr || dev_vaddr == (vir_bytes) MAP_FAILED) {
453 printf("MEM: dev %d not initialized\n", minor);
454 return EIO;
456 if (position >= dv_size) return(total); /* check for EOF */
457 if (position + count > dv_size) count = dv_size - position;
458 if (!do_write) { /* copy actual data */
459 r=sys_safecopyto(endpt, grant, vir_offset,
460 dev_vaddr + position, count);
461 } else {
462 r=sys_safecopyfrom(endpt, grant, vir_offset,
463 dev_vaddr + position, count);
465 if(r != OK) {
466 panic("I/O copy failed: %d", r);
469 /* Book the number of bytes transferred. */
470 position += count;
471 vir_offset += count;
472 total += count;
473 if ((iov->iov_size -= count) == 0) { iov++; nr_req--; vir_offset = 0; }
476 return(total);
479 /*===========================================================================*
480 * m_block_open *
481 *===========================================================================*/
482 static int m_block_open(devminor_t minor, int UNUSED(access))
484 /* Open a memory block device. */
485 if (m_block_part(minor) == NULL) return(ENXIO);
487 openct[minor]++;
489 return(OK);
492 /*===========================================================================*
493 * m_block_close *
494 *===========================================================================*/
495 static int m_block_close(devminor_t minor)
497 /* Close a memory block device. */
498 if (m_block_part(minor) == NULL) return(ENXIO);
500 if(openct[minor] < 1) {
501 printf("MEMORY: closing unopened device %d\n", minor);
502 return(EINVAL);
504 openct[minor]--;
506 return(OK);
509 /*===========================================================================*
510 * m_block_ioctl *
511 *===========================================================================*/
512 static int m_block_ioctl(devminor_t minor, unsigned long request,
513 endpoint_t endpt, cp_grant_id_t grant, endpoint_t UNUSED(user_endpt))
515 /* I/O controls for the block devices of the memory driver. Currently there is
516 * one I/O control specific to the memory driver:
517 * - MIOCRAMSIZE: to set the size of the RAM disk.
519 struct device *dv;
520 u32_t ramdev_size;
521 int s;
522 void *mem;
523 int is_imgrd = 0;
525 if (request != MIOCRAMSIZE)
526 return EINVAL;
528 if(minor == IMGRD_DEV)
529 is_imgrd = 1;
531 /* Someone wants to create a new RAM disk with the given size.
532 * A ramdisk can be created only once, and only on RAM disk device.
534 if ((dv = m_block_part(minor)) == NULL) return ENXIO;
535 if((minor < RAM_DEV_FIRST || minor > RAM_DEV_LAST) &&
536 minor != RAM_DEV_OLD && !is_imgrd) {
537 printf("MEM: MIOCRAMSIZE: %d not a ramdisk\n", minor);
538 return EINVAL;
541 /* Get request structure */
542 s= sys_safecopyfrom(endpt, grant, 0, (vir_bytes)&ramdev_size,
543 sizeof(ramdev_size));
544 if (s != OK)
545 return s;
546 if(is_imgrd)
547 ramdev_size = 0;
548 if(m_vaddrs[minor] && dv->dv_size == (u64_t) ramdev_size) {
549 return(OK);
551 /* openct is 1 for the ioctl(). */
552 if(openct[minor] != 1) {
553 printf("MEM: MIOCRAMSIZE: %d in use (count %d)\n",
554 minor, openct[minor]);
555 return(EBUSY);
557 if(m_vaddrs[minor]) {
558 u32_t a, o;
559 u64_t size;
560 int r;
561 if(ex64hi(dv->dv_size)) {
562 panic("huge old ramdisk");
564 size = dv->dv_size;
565 a = m_vaddrs[minor];
566 if((o = a % PAGE_SIZE)) {
567 vir_bytes l = PAGE_SIZE - o;
568 a += l;
569 size -= l;
571 size = rounddown(size, PAGE_SIZE);
572 r = munmap((void *) a, size);
573 if(r != OK) {
574 printf("memory: WARNING: munmap failed: %d\n", r);
576 m_vaddrs[minor] = (vir_bytes) NULL;
577 dv->dv_size = 0;
580 #if DEBUG
581 printf("MEM:%d: allocating ramdisk of size 0x%x\n", minor, ramdev_size);
582 #endif
584 mem = NULL;
586 /* Try to allocate a piece of memory for the RAM disk. */
587 if(ramdev_size > 0 &&
588 (mem = mmap(NULL, ramdev_size, PROT_READ|PROT_WRITE,
589 MAP_PREALLOC|MAP_ANON, -1, 0)) == MAP_FAILED) {
590 printf("MEM: failed to get memory for ramdisk\n");
591 return(ENOMEM);
594 m_vaddrs[minor] = (vir_bytes) mem;
596 dv->dv_size = ramdev_size;
598 return(OK);