1 /* This file contains the device dependent part of the drivers for the
2 * following special files:
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
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)
19 #include <minix/drivers.h>
20 #include <minix/chardriver.h>
21 #include <minix/blockdriver.h>
22 #include <sys/ioc_memory.h>
25 #include <machine/param.h>
26 #include <machine/vmparam.h>
28 #include "kernel/const.h"
29 #include "kernel/config.h"
30 #include "kernel/type.h"
32 #include <machine/vm.h>
36 /* ramdisks (/dev/ram*) */
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 /*===========================================================================*
87 *===========================================================================*/
93 /* SEF local startup. */
96 /* The receive loop. */
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
);
104 chardriver_process(&m_cdtab
, &msg
, ipc_status
);
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. */
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. */
131 struct kinfo kinfo
; /* kernel information */
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.");
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
++)
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
);
167 /*===========================================================================*
169 *===========================================================================*/
170 static int m_is_block(devminor_t minor
)
172 /* Return TRUE iff the given minor device number is for a block device. */
186 /*===========================================================================*
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
;
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
);
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
);
210 r
= sys_safecopyfrom(endpt
, grant
, 0, dev_vaddr
+ position
, size
);
212 return (r
!= OK
) ? r
: size
;
215 /*===========================================================================*
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
;
225 phys_bytes mem_phys
, pagestart
;
226 size_t off
, page_off
, subcount
;
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.
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
) {
249 if (vm_unmap_phys(SELF
, vaddr
, PAGE_SIZE
) != OK
)
250 panic("vm_unmap_phys failed");
254 vaddr
= vm_map_phys(SELF
, (void *) pagestart
, PAGE_SIZE
);
255 if (vaddr
== MAP_FAILED
) {
256 printf("memory: vm_map_phys failed\n");
260 pagestart_mapped
= pagestart
;
263 /* how much to be done within this page. */
264 subcount
= PAGE_SIZE
- page_off
;
268 if (!do_write
) /* copy data */
269 r
= sys_safecopyto(endpt
, grant
, off
,
270 (vir_bytes
) vaddr
+ page_off
, subcount
);
272 r
= sys_safecopyfrom(endpt
, grant
, off
,
273 (vir_bytes
) vaddr
+ page_off
, subcount
);
277 position
+= subcount
;
284 /*===========================================================================*
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. */
294 /* Check if the minor device number is ok. */
295 if (minor
< 0 || minor
>= NR_DEVS
|| m_is_block(minor
)) return ENXIO
;
299 r
= 0; /* always at EOF */
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
)
309 r
= m_transfer_kmem(minor
, FALSE
, position
, endpt
, grant
, size
);
313 r
= m_transfer_mem(minor
, FALSE
, position
, endpt
, grant
, size
);
317 panic("unknown character device %d", minor
);
323 /*===========================================================================*
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. */
333 /* Check if the minor device number is ok. */
334 if (minor
< 0 || minor
>= NR_DEVS
|| m_is_block(minor
)) return ENXIO
;
339 r
= size
; /* just eat everything */
343 r
= m_transfer_kmem(minor
, TRUE
, position
, endpt
, grant
, size
);
347 r
= m_transfer_mem(minor
, TRUE
, position
, endpt
, grant
, size
);
351 panic("unknown character device %d", minor
);
357 /*===========================================================================*
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
);
373 printf("m_char_open: sys_enable_iop failed for %d: %d\n",
385 /*===========================================================================*
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
);
403 /*===========================================================================*
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 /*===========================================================================*
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. */
429 vir_bytes vir_offset
= 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 */
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
);
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
);
462 r
=sys_safecopyfrom(endpt
, grant
, vir_offset
,
463 dev_vaddr
+ position
, count
);
466 panic("I/O copy failed: %d", r
);
469 /* Book the number of bytes transferred. */
473 if ((iov
->iov_size
-= count
) == 0) { iov
++; nr_req
--; vir_offset
= 0; }
479 /*===========================================================================*
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
);
492 /*===========================================================================*
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
);
509 /*===========================================================================*
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.
525 if (request
!= MIOCRAMSIZE
)
528 if(minor
== IMGRD_DEV
)
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
);
541 /* Get request structure */
542 s
= sys_safecopyfrom(endpt
, grant
, 0, (vir_bytes
)&ramdev_size
,
543 sizeof(ramdev_size
));
548 if(m_vaddrs
[minor
] && dv
->dv_size
== (u64_t
) ramdev_size
) {
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
]);
557 if(m_vaddrs
[minor
]) {
561 if(ex64hi(dv
->dv_size
)) {
562 panic("huge old ramdisk");
566 if((o
= a
% PAGE_SIZE
)) {
567 vir_bytes l
= PAGE_SIZE
- o
;
571 size
= rounddown(size
, PAGE_SIZE
);
572 r
= munmap((void *) a
, size
);
574 printf("memory: WARNING: munmap failed: %d\n", r
);
576 m_vaddrs
[minor
] = (vir_bytes
) NULL
;
581 printf("MEM:%d: allocating ramdisk of size 0x%x\n", minor
, ramdev_size
);
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");
594 m_vaddrs
[minor
] = (vir_bytes
) mem
;
596 dv
->dv_size
= ramdev_size
;