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)
18 #include "../drivers.h"
19 #include "../libdriver/driver.h"
20 #include <sys/ioc_memory.h>
24 #include "../../kernel/const.h"
25 #include "../../kernel/config.h"
26 #include "../../kernel/type.h"
28 #include <sys/vm_i386.h>
32 /* ramdisks (/dev/ram*) */
35 #define RAM_DEV_LAST (RAM_DEV_FIRST+RAMDISKS-1)
37 #define NR_DEVS (7+RAMDISKS) /* number of minor devices */
39 PRIVATE
struct device m_geom
[NR_DEVS
]; /* base and size of each device */
40 PRIVATE vir_bytes m_vaddrs
[NR_DEVS
];
41 PRIVATE
int m_device
; /* current device */
42 PRIVATE
struct kinfo kinfo
; /* kernel information */
44 extern int errno
; /* error number for PM calls */
46 PRIVATE
int openct
[NR_DEVS
];
48 FORWARD
_PROTOTYPE( char *m_name
, (void) );
49 FORWARD
_PROTOTYPE( struct device
*m_prepare
, (int device
) );
50 FORWARD
_PROTOTYPE( int m_transfer
, (int proc_nr
, int opcode
,
51 u64_t position
, iovec_t
*iov
, unsigned nr_req
) );
52 FORWARD
_PROTOTYPE( int m_do_open
, (struct driver
*dp
, message
*m_ptr
) );
53 FORWARD
_PROTOTYPE( int m_do_close
, (struct driver
*dp
, message
*m_ptr
) );
54 FORWARD
_PROTOTYPE( int m_ioctl
, (struct driver
*dp
, message
*m_ptr
) );
55 FORWARD
_PROTOTYPE( void m_geometry
, (struct partition
*entry
) );
57 /* Entry points to this driver. */
58 PRIVATE
struct driver m_dtab
= {
59 m_name
, /* current device's name */
60 m_do_open
, /* open or mount */
61 m_do_close
, /* nothing on a close */
62 m_ioctl
, /* specify ram disk geometry */
63 m_prepare
, /* prepare for I/O on a given minor device */
64 m_transfer
, /* do the I/O */
65 nop_cleanup
, /* no need to clean up */
66 m_geometry
, /* memory device "geometry" */
67 nop_signal
, /* system signals */
75 /* Buffer for the /dev/zero null byte feed. */
76 #define ZERO_BUF_SIZE 1024
77 PRIVATE
char dev_zero
[ZERO_BUF_SIZE
];
79 #define click_to_round_k(n) \
80 ((unsigned) ((((unsigned long) (n) << CLICK_SHIFT) + 512) / 1024))
82 /* SEF functions and variables. */
83 FORWARD
_PROTOTYPE( void sef_local_startup
, (void) );
84 FORWARD
_PROTOTYPE( int sef_cb_init_fresh
, (int type
, sef_init_info_t
*info
) );
86 /*===========================================================================*
88 *===========================================================================*/
91 /* SEF local startup. */
94 /* Call the generic receive loop. */
95 driver_task(&m_dtab
, DRIVER_STD
);
100 /*===========================================================================*
101 * sef_local_startup *
102 *===========================================================================*/
103 PRIVATE
void sef_local_startup()
105 /* Register init callbacks. */
106 sef_setcb_init_fresh(sef_cb_init_fresh
);
107 sef_setcb_init_lu(sef_cb_init_fresh
);
108 sef_setcb_init_restart(sef_cb_init_fresh
);
110 /* Register live update callbacks. */
111 sef_setcb_lu_prepare(sef_cb_lu_prepare_always_ready
);
112 sef_setcb_lu_state_isvalid(sef_cb_lu_state_isvalid_standard
);
114 /* Let SEF perform startup. */
118 /*===========================================================================*
119 * sef_cb_init_fresh *
120 *===========================================================================*/
121 PRIVATE
int sef_cb_init_fresh(int type
, sef_init_info_t
*info
)
123 /* Initialize the memory driver. */
128 sa
.sa_handler
= SIG_MESS
;
129 sigemptyset(&sa
.sa_mask
);
131 if (sigaction(SIGTERM
,&sa
,NULL
)<0) panic("sigaction failed: %d", errno
);
133 /* Initialize all minor devices one by one. */
134 if (OK
!= (s
=sys_getkinfo(&kinfo
))) {
135 panic("Couldn't get kernel information: %d", s
);
139 /* Map in kernel memory for /dev/kmem. */
140 m_geom
[KMEM_DEV
].dv_base
= cvul64(kinfo
.kmem_base
);
141 m_geom
[KMEM_DEV
].dv_size
= cvul64(kinfo
.kmem_size
);
142 if((m_vaddrs
[KMEM_DEV
] = vm_map_phys(SELF
, (void *) kinfo
.kmem_base
,
143 kinfo
.kmem_size
)) == MAP_FAILED
) {
144 printf("MEM: Couldn't map in /dev/kmem.");
148 /* Ramdisk image built into the memory driver */
149 m_geom
[IMGRD_DEV
].dv_base
= cvul64(0);
150 m_geom
[IMGRD_DEV
].dv_size
= cvul64(imgrd_size
);
151 m_vaddrs
[IMGRD_DEV
] = (vir_bytes
) imgrd
;
153 /* Initialize /dev/zero. Simply write zeros into the buffer. */
154 for (i
=0; i
<ZERO_BUF_SIZE
; i
++) {
158 for(i
= 0; i
< NR_DEVS
; i
++)
161 /* Set up memory range for /dev/mem. */
162 m_geom
[MEM_DEV
].dv_base
= cvul64(0);
163 m_geom
[MEM_DEV
].dv_size
= cvul64(0xffffffff);
165 m_vaddrs
[MEM_DEV
] = (vir_bytes
) MAP_FAILED
; /* we are not mapping this in. */
170 /*===========================================================================*
172 *===========================================================================*/
173 PRIVATE
char *m_name()
175 /* Return a name for the current device. */
176 static char name
[] = "memory";
180 /*===========================================================================*
182 *===========================================================================*/
183 PRIVATE
struct device
*m_prepare(device
)
186 /* Prepare for I/O on a device: check if the minor device number is ok. */
187 if (device
< 0 || device
>= NR_DEVS
) return(NIL_DEV
);
190 return(&m_geom
[device
]);
193 /*===========================================================================*
195 *===========================================================================*/
196 PRIVATE
int m_transfer(proc_nr
, opcode
, pos64
, iov
, nr_req
)
197 int proc_nr
; /* process doing the request */
198 int opcode
; /* DEV_GATHER_S or DEV_SCATTER_S */
199 u64_t pos64
; /* offset on device to read or write */
200 iovec_t
*iov
; /* pointer to read or write request vector */
201 unsigned nr_req
; /* length of request vector */
203 /* Read or write one the driver's minor devices. */
204 unsigned count
, left
, chunk
;
205 vir_bytes user_vir
, vir_offset
= 0;
207 unsigned long dv_size
;
212 /* ZERO_DEV and NULL_DEV are infinite in size. */
213 if (m_device
!= ZERO_DEV
&& m_device
!= NULL_DEV
&& ex64hi(pos64
) != 0)
214 return OK
; /* Beyond EOF */
215 position
= cv64ul(pos64
);
217 /* Get minor device number and check for /dev/null. */
218 dv
= &m_geom
[m_device
];
219 dv_size
= cv64ul(dv
->dv_size
);
220 dev_vaddr
= m_vaddrs
[m_device
];
224 /* How much to transfer and where to / from. */
225 count
= iov
->iov_size
;
226 user_vir
= iov
->iov_addr
;
230 /* No copying; ignore request. */
232 if (opcode
== DEV_GATHER_S
) return(OK
); /* always at EOF */
235 /* Virtual copying. For RAM disks, kernel memory and internal FS. */
241 if(m_device
< 0 || m_device
>= NR_DEVS
) {
244 if(!dev_vaddr
|| dev_vaddr
== (vir_bytes
) MAP_FAILED
) {
245 printf("MEM: dev %d not initialized\n", m_device
);
248 if (position
>= dv_size
) return(OK
); /* check for EOF */
249 if (position
+ count
> dv_size
) count
= dv_size
- position
;
250 if (opcode
== DEV_GATHER_S
) { /* copy actual data */
251 r
=sys_safecopyto(proc_nr
, user_vir
, vir_offset
,
252 dev_vaddr
+ position
, count
, D
);
254 r
=sys_safecopyfrom(proc_nr
, user_vir
, vir_offset
,
255 dev_vaddr
+ position
, count
, D
);
258 panic("I/O copy failed: %d", r
);
262 /* Physical copying. Only used to access entire memory.
263 * Transfer one 'page window' at a time.
267 u32_t pagestart
, page_off
;
268 static u32_t pagestart_mapped
;
269 static int any_mapped
= 0;
275 if (position
>= dv_size
)
276 return(OK
); /* check for EOF */
277 if (position
+ count
> dv_size
)
278 count
= dv_size
- position
;
281 page_off
= mem_phys
% I386_PAGE_SIZE
;
282 pagestart
= mem_phys
- page_off
;
284 /* All memory to the map call has to be page-aligned.
285 * Don't have to map same page over and over.
287 if(!any_mapped
|| pagestart_mapped
!= pagestart
) {
289 if(vm_unmap_phys(SELF
, vaddr
, I386_PAGE_SIZE
) != OK
)
290 panic("vm_unmap_phys failed");
293 vaddr
= vm_map_phys(SELF
, (void *) pagestart
, I386_PAGE_SIZE
);
294 if(vaddr
== MAP_FAILED
)
299 printf("memory: vm_map_phys failed\n");
303 pagestart_mapped
= pagestart
;
306 /* how much to be done within this page. */
307 subcount
= I386_PAGE_SIZE
-page_off
;
311 if (opcode
== DEV_GATHER_S
) { /* copy data */
312 s
=sys_safecopyto(proc_nr
, user_vir
,
313 vir_offset
, (vir_bytes
) vaddr
+page_off
, subcount
, D
);
315 s
=sys_safecopyfrom(proc_nr
, user_vir
,
316 vir_offset
, (vir_bytes
) vaddr
+page_off
, subcount
, D
);
324 /* Null byte stream generator. */
326 if (opcode
== DEV_GATHER_S
) {
327 size_t suboffset
= 0;
330 chunk
= (left
> ZERO_BUF_SIZE
) ? ZERO_BUF_SIZE
: left
;
331 s
=sys_safecopyto(proc_nr
, user_vir
,
332 vir_offset
+suboffset
, (vir_bytes
) dev_zero
, chunk
, D
);
334 printf("MEM: sys_safecopyto failed: %d\n", s
);
343 /* Book the number of bytes transferred. */
346 if ((iov
->iov_size
-= count
) == 0) { iov
++; nr_req
--; vir_offset
= 0; }
352 /*===========================================================================*
354 *===========================================================================*/
355 PRIVATE
int m_do_open(dp
, m_ptr
)
361 /* Check device number on open. */
362 if (m_prepare(m_ptr
->DEVICE
) == NIL_DEV
) return(ENXIO
);
363 if (m_device
== MEM_DEV
)
365 r
= sys_enable_iop(m_ptr
->IO_ENDPT
);
368 printf("m_do_open: sys_enable_iop failed for %d: %d\n",
374 if(m_device
< 0 || m_device
>= NR_DEVS
) {
375 panic("wrong m_device: %d", m_device
);
383 /*===========================================================================*
385 *===========================================================================*/
386 PRIVATE
int m_do_close(dp
, m_ptr
)
392 if (m_prepare(m_ptr
->DEVICE
) == NIL_DEV
) return(ENXIO
);
394 if(m_device
< 0 || m_device
>= NR_DEVS
) {
395 panic("wrong m_device: %d", m_device
);
398 if(openct
[m_device
] < 1) {
399 panic("closed too often");
406 /*===========================================================================*
408 *===========================================================================*/
409 PRIVATE
int m_ioctl(dp
, m_ptr
)
410 struct driver
*dp
; /* pointer to driver structure */
411 message
*m_ptr
; /* pointer to control message */
413 /* I/O controls for the memory driver. Currently there is one I/O control:
414 * - MIOCRAMSIZE: to set the size of the RAM disk.
418 switch (m_ptr
->REQUEST
) {
420 /* Someone wants to create a new RAM disk with the given size. */
425 /* A ramdisk can be created only once, and only on RAM disk device. */
427 if(dev
< 0 || dev
>= NR_DEVS
) {
428 printf("MEM: MIOCRAMSIZE: %d not a valid device\n", dev
);
430 if((dev
< RAM_DEV_FIRST
|| dev
> RAM_DEV_LAST
) && dev
!= RAM_DEV_OLD
) {
431 printf("MEM: MIOCRAMSIZE: %d not a ramdisk\n", dev
);
433 if ((dv
= m_prepare(dev
)) == NIL_DEV
) return(ENXIO
);
435 /* Get request structure */
436 s
= sys_safecopyfrom(m_ptr
->IO_ENDPT
, (vir_bytes
)m_ptr
->IO_GRANT
,
437 0, (vir_bytes
)&ramdev_size
, sizeof(ramdev_size
), D
);
440 if(m_vaddrs
[dev
] && !cmp64(dv
->dv_size
, cvul64(ramdev_size
))) {
443 /* openct is 1 for the ioctl(). */
444 if(openct
[dev
] != 1) {
445 printf("MEM: MIOCRAMSIZE: %d in use (count %d)\n",
451 if(ex64hi(dv
->dv_size
)) {
452 panic("huge old ramdisk");
454 size
= ex64lo(dv
->dv_size
);
455 free((void *) m_vaddrs
[dev
]);
456 m_vaddrs
[dev
] = (vir_bytes
) NULL
;
460 printf("MEM:%d: allocating ramdisk of size 0x%x\n", dev
, ramdev_size
);
463 /* Try to allocate a piece of memory for the RAM disk. */
464 if(!(mem
= malloc(ramdev_size
))) {
465 printf("MEM: failed to get memory for ramdisk\n");
468 memset(mem
, 0, ramdev_size
);
470 m_vaddrs
[dev
] = (vir_bytes
) mem
;
472 dv
->dv_size
= cvul64(ramdev_size
);
478 return(do_diocntl(&m_dtab
, m_ptr
));
483 /*===========================================================================*
485 *===========================================================================*/
486 PRIVATE
void m_geometry(entry
)
487 struct partition
*entry
;
489 /* Memory devices don't have a geometry, but the outside world insists. */
490 entry
->cylinders
= div64u(m_geom
[m_device
].dv_size
, SECTOR_SIZE
) / (64 * 32);