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>
25 #include "../../kernel/const.h"
26 #include "../../kernel/config.h"
27 #include "../../kernel/type.h"
30 #include <sys/vm_i386.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 PRIVATE
struct device m_geom
[NR_DEVS
]; /* base and size of each device */
44 PRIVATE vir_bytes m_vaddrs
[NR_DEVS
];
45 PRIVATE
int m_device
; /* current device */
46 PRIVATE
struct kinfo kinfo
; /* kernel information */
48 extern int errno
; /* error number for PM calls */
50 PRIVATE
int openct
[NR_DEVS
];
52 FORWARD
_PROTOTYPE( char *m_name
, (void) );
53 FORWARD
_PROTOTYPE( struct device
*m_prepare
, (int device
) );
54 FORWARD
_PROTOTYPE( int m_transfer
, (int proc_nr
, int opcode
,
55 u64_t position
, iovec_t
*iov
, unsigned nr_req
) );
56 FORWARD
_PROTOTYPE( int m_do_open
, (struct driver
*dp
, message
*m_ptr
) );
57 FORWARD
_PROTOTYPE( int m_do_close
, (struct driver
*dp
, message
*m_ptr
) );
58 FORWARD
_PROTOTYPE( int m_ioctl
, (struct driver
*dp
, message
*m_ptr
) );
59 FORWARD
_PROTOTYPE( void m_geometry
, (struct partition
*entry
) );
61 /* Entry points to this driver. */
62 PRIVATE
struct driver m_dtab
= {
63 m_name
, /* current device's name */
64 m_do_open
, /* open or mount */
65 m_do_close
, /* nothing on a close */
66 m_ioctl
, /* specify ram disk geometry */
67 m_prepare
, /* prepare for I/O on a given minor device */
68 m_transfer
, /* do the I/O */
69 nop_cleanup
, /* no need to clean up */
70 m_geometry
, /* memory device "geometry" */
71 nop_signal
, /* system signals */
79 /* Buffer for the /dev/zero null byte feed. */
80 #define ZERO_BUF_SIZE 1024
81 PRIVATE
char dev_zero
[ZERO_BUF_SIZE
];
83 #define click_to_round_k(n) \
84 ((unsigned) ((((unsigned long) (n) << CLICK_SHIFT) + 512) / 1024))
86 /* SEF functions and variables. */
87 FORWARD
_PROTOTYPE( void sef_local_startup
, (void) );
88 FORWARD
_PROTOTYPE( int sef_cb_init_fresh
, (int type
, sef_init_info_t
*info
) );
90 /*===========================================================================*
92 *===========================================================================*/
95 /* SEF local startup. */
98 /* Call the generic receive loop. */
99 driver_task(&m_dtab
, DRIVER_STD
);
104 /*===========================================================================*
105 * sef_local_startup *
106 *===========================================================================*/
107 PRIVATE
void sef_local_startup()
109 /* Register init callbacks. */
110 sef_setcb_init_fresh(sef_cb_init_fresh
);
111 sef_setcb_init_lu(sef_cb_init_fresh
);
112 sef_setcb_init_restart(sef_cb_init_fresh
);
114 /* Register live update callbacks. */
115 sef_setcb_lu_prepare(sef_cb_lu_prepare_always_ready
);
116 sef_setcb_lu_state_isvalid(sef_cb_lu_state_isvalid_standard
);
118 /* Let SEF perform startup. */
122 /*===========================================================================*
123 * sef_cb_init_fresh *
124 *===========================================================================*/
125 PRIVATE
int sef_cb_init_fresh(int type
, sef_init_info_t
*info
)
127 /* Initialize the memory driver. */
132 sa
.sa_handler
= SIG_MESS
;
133 sigemptyset(&sa
.sa_mask
);
135 if (sigaction(SIGTERM
,&sa
,NULL
)<0) panic("MEM","sigaction failed", errno
);
137 /* Initialize all minor devices one by one. */
138 if (OK
!= (s
=sys_getkinfo(&kinfo
))) {
139 panic("MEM","Couldn't get kernel information.",s
);
143 /* Map in kernel memory for /dev/kmem. */
144 m_geom
[KMEM_DEV
].dv_base
= cvul64(kinfo
.kmem_base
);
145 m_geom
[KMEM_DEV
].dv_size
= cvul64(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
= cvul64(0);
154 m_geom
[IMGRD_DEV
].dv_size
= cvul64(imgrd_size
);
155 m_vaddrs
[IMGRD_DEV
] = (vir_bytes
) imgrd
;
157 /* Initialize /dev/zero. Simply write zeros into the buffer. */
158 for (i
=0; i
<ZERO_BUF_SIZE
; i
++) {
162 for(i
= 0; i
< NR_DEVS
; i
++)
165 /* Set up memory range for /dev/mem. */
166 m_geom
[MEM_DEV
].dv_base
= cvul64(0);
167 m_geom
[MEM_DEV
].dv_size
= cvul64(0xffffffff);
169 m_vaddrs
[MEM_DEV
] = (vir_bytes
) MAP_FAILED
; /* we are not mapping this in. */
174 /*===========================================================================*
176 *===========================================================================*/
177 PRIVATE
char *m_name()
179 /* Return a name for the current device. */
180 static char name
[] = "memory";
184 /*===========================================================================*
186 *===========================================================================*/
187 PRIVATE
struct device
*m_prepare(device
)
190 /* Prepare for I/O on a device: check if the minor device number is ok. */
191 if (device
< 0 || device
>= NR_DEVS
) return(NIL_DEV
);
194 return(&m_geom
[device
]);
197 /*===========================================================================*
199 *===========================================================================*/
200 PRIVATE
int m_transfer(proc_nr
, opcode
, pos64
, iov
, nr_req
)
201 int proc_nr
; /* process doing the request */
202 int opcode
; /* DEV_GATHER_S or DEV_SCATTER_S */
203 u64_t pos64
; /* offset on device to read or write */
204 iovec_t
*iov
; /* pointer to read or write request vector */
205 unsigned nr_req
; /* length of request vector */
207 /* Read or write one the driver's minor devices. */
208 unsigned count
, left
, chunk
;
209 vir_bytes user_vir
, vir_offset
= 0;
211 unsigned long dv_size
;
216 /* ZERO_DEV and NULL_DEV are infinite in size. */
217 if (m_device
!= ZERO_DEV
&& m_device
!= NULL_DEV
&& ex64hi(pos64
) != 0)
218 return OK
; /* Beyond EOF */
219 position
= cv64ul(pos64
);
221 /* Get minor device number and check for /dev/null. */
222 dv
= &m_geom
[m_device
];
223 dv_size
= cv64ul(dv
->dv_size
);
224 dev_vaddr
= m_vaddrs
[m_device
];
228 /* How much to transfer and where to / from. */
229 count
= iov
->iov_size
;
230 user_vir
= iov
->iov_addr
;
234 /* No copying; ignore request. */
236 if (opcode
== DEV_GATHER_S
) return(OK
); /* always at EOF */
239 /* Virtual copying. For RAM disks, kernel memory and internal FS. */
245 if(m_device
< 0 || m_device
>= NR_DEVS
) {
248 if(!dev_vaddr
|| dev_vaddr
== (vir_bytes
) MAP_FAILED
) {
249 printf("MEM: dev %d not initialized\n", m_device
);
252 if (position
>= dv_size
) return(OK
); /* check for EOF */
253 if (position
+ count
> dv_size
) count
= dv_size
- position
;
254 if (opcode
== DEV_GATHER_S
) { /* copy actual data */
255 r
=sys_safecopyto(proc_nr
, user_vir
, vir_offset
,
256 dev_vaddr
+ position
, count
, D
);
258 r
=sys_safecopyfrom(proc_nr
, user_vir
, vir_offset
,
259 dev_vaddr
+ position
, count
, D
);
262 panic("MEM","I/O copy failed",r
);
266 /* Physical copying. Only used to access entire memory.
267 * Transfer one 'page window' at a time.
271 u32_t pagestart
, page_off
;
272 static u32_t pagestart_mapped
;
273 static int any_mapped
= 0;
279 if (position
>= dv_size
)
280 return(OK
); /* check for EOF */
281 if (position
+ count
> dv_size
)
282 count
= dv_size
- position
;
285 page_off
= mem_phys
% I386_PAGE_SIZE
;
286 pagestart
= mem_phys
- page_off
;
288 /* All memory to the map call has to be page-aligned.
289 * Don't have to map same page over and over.
291 if(!any_mapped
|| pagestart_mapped
!= pagestart
) {
293 if(vm_unmap_phys(SELF
, vaddr
, I386_PAGE_SIZE
) != OK
)
294 panic("MEM","vm_unmap_phys failed",NO_NUM
);
297 vaddr
= vm_map_phys(SELF
, (void *) pagestart
, I386_PAGE_SIZE
);
298 if(vaddr
== MAP_FAILED
)
303 printf("memory: vm_map_phys failed\n");
307 pagestart_mapped
= pagestart
;
310 /* how much to be done within this page. */
311 subcount
= I386_PAGE_SIZE
-page_off
;
315 if (opcode
== DEV_GATHER_S
) { /* copy data */
316 s
=sys_safecopyto(proc_nr
, user_vir
,
317 vir_offset
, (vir_bytes
) vaddr
+page_off
, subcount
, D
);
319 s
=sys_safecopyfrom(proc_nr
, user_vir
,
320 vir_offset
, (vir_bytes
) vaddr
+page_off
, subcount
, D
);
328 /* Null byte stream generator. */
330 if (opcode
== DEV_GATHER_S
) {
331 size_t suboffset
= 0;
334 chunk
= (left
> ZERO_BUF_SIZE
) ? ZERO_BUF_SIZE
: left
;
335 s
=sys_safecopyto(proc_nr
, user_vir
,
336 vir_offset
+suboffset
, (vir_bytes
) dev_zero
, chunk
, D
);
338 report("MEM","sys_safecopyto failed", s
);
347 /* Book the number of bytes transferred. */
350 if ((iov
->iov_size
-= count
) == 0) { iov
++; nr_req
--; vir_offset
= 0; }
356 /*===========================================================================*
358 *===========================================================================*/
359 PRIVATE
int m_do_open(dp
, m_ptr
)
365 /* Check device number on open. */
366 if (m_prepare(m_ptr
->DEVICE
) == NIL_DEV
) return(ENXIO
);
367 if (m_device
== MEM_DEV
)
369 r
= sys_enable_iop(m_ptr
->IO_ENDPT
);
372 printf("m_do_open: sys_enable_iop failed for %d: %d\n",
378 if(m_device
< 0 || m_device
>= NR_DEVS
) {
379 panic("MEM","wrong m_device",m_device
);
387 /*===========================================================================*
389 *===========================================================================*/
390 PRIVATE
int m_do_close(dp
, m_ptr
)
396 if (m_prepare(m_ptr
->DEVICE
) == NIL_DEV
) return(ENXIO
);
398 if(m_device
< 0 || m_device
>= NR_DEVS
) {
399 panic("MEM","wrong m_device",m_device
);
402 if(openct
[m_device
] < 1) {
403 panic("MEM","closed too often",NO_NUM
);
410 /*===========================================================================*
412 *===========================================================================*/
413 PRIVATE
int m_ioctl(dp
, m_ptr
)
414 struct driver
*dp
; /* pointer to driver structure */
415 message
*m_ptr
; /* pointer to control message */
417 /* I/O controls for the memory driver. Currently there is one I/O control:
418 * - MIOCRAMSIZE: to set the size of the RAM disk.
422 switch (m_ptr
->REQUEST
) {
424 /* Someone wants to create a new RAM disk with the given size. */
429 /* A ramdisk can be created only once, and only on RAM disk device. */
431 if(dev
< 0 || dev
>= NR_DEVS
) {
432 printf("MEM: MIOCRAMSIZE: %d not a valid device\n", dev
);
434 if((dev
< RAM_DEV_FIRST
|| dev
> RAM_DEV_LAST
) && dev
!= RAM_DEV_OLD
) {
435 printf("MEM: MIOCRAMSIZE: %d not a ramdisk\n", dev
);
437 if ((dv
= m_prepare(dev
)) == NIL_DEV
) return(ENXIO
);
439 /* Get request structure */
440 s
= sys_safecopyfrom(m_ptr
->IO_ENDPT
, (vir_bytes
)m_ptr
->IO_GRANT
,
441 0, (vir_bytes
)&ramdev_size
, sizeof(ramdev_size
), D
);
444 if(m_vaddrs
[dev
] && !cmp64(dv
->dv_size
, cvul64(ramdev_size
))) {
447 /* openct is 1 for the ioctl(). */
448 if(openct
[dev
] != 1) {
449 printf("MEM: MIOCRAMSIZE: %d in use (count %d)\n",
455 if(ex64hi(dv
->dv_size
)) {
456 panic("MEM","huge old ramdisk", NO_NUM
);
458 size
= ex64lo(dv
->dv_size
);
459 munmap((void *) m_vaddrs
[dev
], size
);
460 m_vaddrs
[dev
] = (vir_bytes
) NULL
;
464 printf("MEM:%d: allocating ramdisk of size 0x%x\n", dev
, ramdev_size
);
467 /* Try to allocate a piece of memory for the RAM disk. */
468 if((mem
= mmap(0, ramdev_size
, PROT_READ
|PROT_WRITE
,
469 MAP_PREALLOC
|MAP_ANON
, -1, 0)) == MAP_FAILED
) {
470 printf("MEM: failed to get memory for ramdisk\n");
474 m_vaddrs
[dev
] = (vir_bytes
) mem
;
476 dv
->dv_size
= cvul64(ramdev_size
);
482 return(do_diocntl(&m_dtab
, m_ptr
));
487 /*===========================================================================*
489 *===========================================================================*/
490 PRIVATE
void m_geometry(entry
)
491 struct partition
*entry
;
493 /* Memory devices don't have a geometry, but the outside world insists. */
494 entry
->cylinders
= div64u(m_geom
[m_device
].dv_size
, SECTOR_SIZE
) / (64 * 32);