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_lu(sef_cb_init_fresh
);
118 sef_setcb_init_restart(sef_cb_init_fresh
);
120 /* Register live update callbacks. */
121 sef_setcb_lu_prepare(sef_cb_lu_prepare_always_ready
);
122 sef_setcb_lu_state_isvalid(sef_cb_lu_state_isvalid_standard
);
124 /* Let SEF perform startup. */
128 /*===========================================================================*
129 * sef_cb_init_fresh *
130 *===========================================================================*/
131 static int sef_cb_init_fresh(int UNUSED(type
), sef_init_info_t
*UNUSED(info
))
133 /* Initialize the memory driver. */
136 struct kinfo kinfo
; /* kernel information */
139 if (OK
!= (s
=sys_getkinfo(&kinfo
))) {
140 panic("Couldn't get kernel information: %d", s
);
143 /* Map in kernel memory for /dev/kmem. */
144 m_geom
[KMEM_DEV
].dv_base
= kinfo
.kmem_base
;
145 m_geom
[KMEM_DEV
].dv_size
= kinfo
.kmem_size
;
146 if((m_vaddrs
[KMEM_DEV
] = vm_map_phys(SELF
, (void *) kinfo
.kmem_base
,
147 kinfo
.kmem_size
)) == MAP_FAILED
) {
148 printf("MEM: Couldn't map in /dev/kmem.");
152 /* Ramdisk image built into the memory driver */
153 m_geom
[IMGRD_DEV
].dv_base
= 0;
154 m_geom
[IMGRD_DEV
].dv_size
= imgrd_size
;
155 m_vaddrs
[IMGRD_DEV
] = (vir_bytes
) imgrd
;
157 for(i
= 0; i
< NR_DEVS
; i
++)
160 /* Set up memory range for /dev/mem. */
161 m_geom
[MEM_DEV
].dv_base
= 0;
162 m_geom
[MEM_DEV
].dv_size
= 0xffffffffULL
;
164 m_vaddrs
[MEM_DEV
] = (vir_bytes
) MAP_FAILED
; /* we are not mapping this in. */
169 /*===========================================================================*
171 *===========================================================================*/
172 static int m_is_block(devminor_t minor
)
174 /* Return TRUE iff the given minor device number is for a block device. */
188 /*===========================================================================*
190 *===========================================================================*/
191 static ssize_t
m_transfer_kmem(devminor_t minor
, int do_write
, u64_t position
,
192 endpoint_t endpt
, cp_grant_id_t grant
, size_t size
)
194 /* Transfer from or to the KMEM device. */
195 u64_t dv_size
, dev_vaddr
;
198 dv_size
= m_geom
[minor
].dv_size
;
199 dev_vaddr
= m_vaddrs
[minor
];
201 if (!dev_vaddr
|| dev_vaddr
== (vir_bytes
) MAP_FAILED
) {
202 printf("MEM: dev %d not initialized\n", minor
);
206 if (position
>= dv_size
) return 0; /* check for EOF */
207 if (position
+ size
> dv_size
) size
= dv_size
- position
;
209 if (!do_write
) /* copy actual data */
210 r
= sys_safecopyto(endpt
, grant
, 0, dev_vaddr
+ position
, size
);
212 r
= sys_safecopyfrom(endpt
, grant
, 0, dev_vaddr
+ position
, size
);
214 return (r
!= OK
) ? r
: size
;
217 /*===========================================================================*
219 *===========================================================================*/
220 static ssize_t
m_transfer_mem(devminor_t minor
, int do_write
, u64_t position
,
221 endpoint_t endpt
, cp_grant_id_t grant
, size_t size
)
223 /* Transfer from or to the MEM device. */
224 static int any_mapped
= 0;
225 static phys_bytes pagestart_mapped
;
227 phys_bytes mem_phys
, pagestart
;
228 size_t off
, page_off
, subcount
;
232 dv_size
= m_geom
[minor
].dv_size
;
233 if (position
>= dv_size
) return 0; /* check for EOF */
234 if (position
+ size
> dv_size
) size
= dv_size
- position
;
236 /* Physical copying. Only used to access entire memory.
237 * Transfer one 'page window' at a time.
241 mem_phys
= (phys_bytes
) position
;
243 page_off
= (size_t) (mem_phys
% PAGE_SIZE
);
244 pagestart
= mem_phys
- page_off
;
246 /* All memory to the map call has to be page-aligned.
247 * Don't have to map same page over and over.
249 if (!any_mapped
|| pagestart_mapped
!= pagestart
) {
251 if (vm_unmap_phys(SELF
, vaddr
, PAGE_SIZE
) != OK
)
252 panic("vm_unmap_phys failed");
256 vaddr
= vm_map_phys(SELF
, (void *) pagestart
, PAGE_SIZE
);
257 if (vaddr
== MAP_FAILED
) {
258 printf("memory: vm_map_phys failed\n");
262 pagestart_mapped
= pagestart
;
265 /* how much to be done within this page. */
266 subcount
= PAGE_SIZE
- page_off
;
270 if (!do_write
) /* copy data */
271 r
= sys_safecopyto(endpt
, grant
, off
,
272 (vir_bytes
) vaddr
+ page_off
, subcount
);
274 r
= sys_safecopyfrom(endpt
, grant
, off
,
275 (vir_bytes
) vaddr
+ page_off
, subcount
);
279 position
+= subcount
;
286 /*===========================================================================*
288 *===========================================================================*/
289 static ssize_t
m_char_read(devminor_t minor
, u64_t position
, endpoint_t endpt
,
290 cp_grant_id_t grant
, size_t size
, int UNUSED(flags
),
291 cdev_id_t
UNUSED(id
))
293 /* Read from one of the driver's character devices. */
296 /* Check if the minor device number is ok. */
297 if (minor
< 0 || minor
>= NR_DEVS
|| m_is_block(minor
)) return ENXIO
;
301 r
= 0; /* always at EOF */
305 /* Fill the target area with zeroes. In fact, let the kernel do it! */
306 if ((r
= sys_safememset(endpt
, grant
, 0, '\0', size
)) == OK
)
311 r
= m_transfer_kmem(minor
, FALSE
, position
, endpt
, grant
, size
);
315 r
= m_transfer_mem(minor
, FALSE
, position
, endpt
, grant
, size
);
319 panic("unknown character device %d", minor
);
325 /*===========================================================================*
327 *===========================================================================*/
328 static ssize_t
m_char_write(devminor_t minor
, u64_t position
, endpoint_t endpt
,
329 cp_grant_id_t grant
, size_t size
, int UNUSED(flags
),
330 cdev_id_t
UNUSED(id
))
332 /* Write to one of the driver's character devices. */
335 /* Check if the minor device number is ok. */
336 if (minor
< 0 || minor
>= NR_DEVS
|| m_is_block(minor
)) return ENXIO
;
341 r
= size
; /* just eat everything */
345 r
= m_transfer_kmem(minor
, TRUE
, position
, endpt
, grant
, size
);
349 r
= m_transfer_mem(minor
, TRUE
, position
, endpt
, grant
, size
);
353 panic("unknown character device %d", minor
);
359 /*===========================================================================*
361 *===========================================================================*/
362 static int m_char_open(devminor_t minor
, int access
, endpoint_t user_endpt
)
364 /* Open a memory character device. */
366 /* Check if the minor device number is ok. */
367 if (minor
< 0 || minor
>= NR_DEVS
|| m_is_block(minor
)) return ENXIO
;
369 #if defined(__i386__)
370 if (minor
== MEM_DEV
)
372 int r
= sys_enable_iop(user_endpt
);
375 printf("m_char_open: sys_enable_iop failed for %d: %d\n",
387 /*===========================================================================*
389 *===========================================================================*/
390 static int m_char_close(devminor_t minor
)
392 /* Close a memory character device. */
394 if (minor
< 0 || minor
>= NR_DEVS
|| m_is_block(minor
)) return ENXIO
;
396 if(openct
[minor
] < 1) {
397 printf("MEMORY: closing unopened device %d\n", minor
);
405 /*===========================================================================*
407 *===========================================================================*/
408 static struct device
*m_block_part(devminor_t minor
)
410 /* Prepare for I/O on a device: check if the minor device number is ok. */
411 if (minor
< 0 || minor
>= NR_DEVS
|| !m_is_block(minor
)) return(NULL
);
413 return(&m_geom
[minor
]);
416 /*===========================================================================*
418 *===========================================================================*/
419 static int m_block_transfer(
420 devminor_t minor
, /* minor device number */
421 int do_write
, /* read or write? */
422 u64_t position
, /* offset on device to read or write */
423 endpoint_t endpt
, /* process doing the request */
424 iovec_t
*iov
, /* pointer to read or write request vector */
425 unsigned int nr_req
, /* length of request vector */
426 int UNUSED(flags
) /* transfer flags */
429 /* Read or write one the driver's block devices. */
431 vir_bytes vir_offset
= 0;
439 /* Get minor device information. */
440 if ((dv
= m_block_part(minor
)) == NULL
) return(ENXIO
);
441 dv_size
= dv
->dv_size
;
442 dev_vaddr
= m_vaddrs
[minor
];
444 if (ex64hi(position
) != 0)
445 return OK
; /* Beyond EOF */
449 /* How much to transfer and where to / from. */
450 count
= iov
->iov_size
;
451 grant
= (cp_grant_id_t
) iov
->iov_addr
;
453 /* Virtual copying. For RAM disks and internal FS. */
454 if(!dev_vaddr
|| dev_vaddr
== (vir_bytes
) MAP_FAILED
) {
455 printf("MEM: dev %d not initialized\n", minor
);
458 if (position
>= dv_size
) return(total
); /* check for EOF */
459 if (position
+ count
> dv_size
) count
= dv_size
- position
;
460 if (!do_write
) { /* copy actual data */
461 r
=sys_safecopyto(endpt
, grant
, vir_offset
,
462 dev_vaddr
+ position
, count
);
464 r
=sys_safecopyfrom(endpt
, grant
, vir_offset
,
465 dev_vaddr
+ position
, count
);
468 panic("I/O copy failed: %d", r
);
471 /* Book the number of bytes transferred. */
475 if ((iov
->iov_size
-= count
) == 0) { iov
++; nr_req
--; vir_offset
= 0; }
481 /*===========================================================================*
483 *===========================================================================*/
484 static int m_block_open(devminor_t minor
, int UNUSED(access
))
486 /* Open a memory block device. */
487 if (m_block_part(minor
) == NULL
) return(ENXIO
);
494 /*===========================================================================*
496 *===========================================================================*/
497 static int m_block_close(devminor_t minor
)
499 /* Close a memory block device. */
500 if (m_block_part(minor
) == NULL
) return(ENXIO
);
502 if(openct
[minor
] < 1) {
503 printf("MEMORY: closing unopened device %d\n", minor
);
511 /*===========================================================================*
513 *===========================================================================*/
514 static int m_block_ioctl(devminor_t minor
, unsigned long request
,
515 endpoint_t endpt
, cp_grant_id_t grant
, endpoint_t
UNUSED(user_endpt
))
517 /* I/O controls for the block devices of the memory driver. Currently there is
518 * one I/O control specific to the memory driver:
519 * - MIOCRAMSIZE: to set the size of the RAM disk.
527 if (request
!= MIOCRAMSIZE
)
530 if(minor
== IMGRD_DEV
)
533 /* Someone wants to create a new RAM disk with the given size.
534 * A ramdisk can be created only once, and only on RAM disk device.
536 if ((dv
= m_block_part(minor
)) == NULL
) return ENXIO
;
537 if((minor
< RAM_DEV_FIRST
|| minor
> RAM_DEV_LAST
) &&
538 minor
!= RAM_DEV_OLD
&& !is_imgrd
) {
539 printf("MEM: MIOCRAMSIZE: %d not a ramdisk\n", minor
);
543 /* Get request structure */
544 s
= sys_safecopyfrom(endpt
, grant
, 0, (vir_bytes
)&ramdev_size
,
545 sizeof(ramdev_size
));
550 if(m_vaddrs
[minor
] && dv
->dv_size
== (u64_t
) ramdev_size
) {
553 /* openct is 1 for the ioctl(). */
554 if(openct
[minor
] != 1) {
555 printf("MEM: MIOCRAMSIZE: %d in use (count %d)\n",
556 minor
, openct
[minor
]);
559 if(m_vaddrs
[minor
]) {
563 if(ex64hi(dv
->dv_size
)) {
564 panic("huge old ramdisk");
568 if((o
= a
% PAGE_SIZE
)) {
569 vir_bytes l
= PAGE_SIZE
- o
;
573 size
= rounddown(size
, PAGE_SIZE
);
574 r
= munmap((void *) a
, size
);
576 printf("memory: WARNING: munmap failed: %d\n", r
);
578 m_vaddrs
[minor
] = (vir_bytes
) NULL
;
583 printf("MEM:%d: allocating ramdisk of size 0x%x\n", minor
, ramdev_size
);
588 /* Try to allocate a piece of memory for the RAM disk. */
589 if(ramdev_size
> 0 &&
590 (mem
= mmap(NULL
, ramdev_size
, PROT_READ
|PROT_WRITE
,
591 MAP_PREALLOC
|MAP_ANON
, -1, 0)) == MAP_FAILED
) {
592 printf("MEM: failed to get memory for ramdisk\n");
596 m_vaddrs
[minor
] = (vir_bytes
) mem
;
598 dv
->dv_size
= ramdev_size
;