dec21140A ethernet driver for virtualpc, contributed by nicolas tittley.
[minix.git] / drivers / memory / memory.c
blob231a74161c11e46b4b35a70c095777d993c808c8
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 "../drivers.h"
19 #include "../libdriver/driver.h"
20 #include <sys/ioc_memory.h>
21 #include <env.h>
22 #include <minix/ds.h>
23 #include <minix/vm.h>
24 #include <sys/mman.h>
25 #include "../../kernel/const.h"
26 #include "../../kernel/config.h"
27 #include "../../kernel/type.h"
29 #include <sys/vm.h>
30 #include <sys/vm_i386.h>
32 #include "assert.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 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 */
72 nop_alarm,
73 nop_cancel,
74 nop_select,
75 NULL,
76 NULL
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 /*===========================================================================*
91 * main *
92 *===========================================================================*/
93 PUBLIC int main(void)
95 /* SEF local startup. */
96 sef_local_startup();
98 /* Call the generic receive loop. */
99 driver_task(&m_dtab, DRIVER_STD);
101 return(OK);
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. */
119 sef_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. */
128 struct sigaction sa;
129 u32_t ramdev_size;
130 int i, s;
132 sa.sa_handler = SIG_MESS;
133 sigemptyset(&sa.sa_mask);
134 sa.sa_flags = 0;
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);
142 #if 0
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.");
150 #endif
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++) {
159 dev_zero[i] = '\0';
162 for(i = 0; i < NR_DEVS; i++)
163 openct[i] = 0;
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. */
171 return(OK);
174 /*===========================================================================*
175 * m_name *
176 *===========================================================================*/
177 PRIVATE char *m_name()
179 /* Return a name for the current device. */
180 static char name[] = "memory";
181 return name;
184 /*===========================================================================*
185 * m_prepare *
186 *===========================================================================*/
187 PRIVATE struct device *m_prepare(device)
188 int 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);
192 m_device = device;
194 return(&m_geom[device]);
197 /*===========================================================================*
198 * m_transfer *
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;
210 struct device *dv;
211 unsigned long dv_size;
212 int s, r;
213 off_t position;
214 vir_bytes dev_vaddr;
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];
226 while (nr_req > 0) {
228 /* How much to transfer and where to / from. */
229 count = iov->iov_size;
230 user_vir = iov->iov_addr;
232 switch (m_device) {
234 /* No copying; ignore request. */
235 case NULL_DEV:
236 if (opcode == DEV_GATHER_S) return(OK); /* always at EOF */
237 break;
239 /* Virtual copying. For RAM disks, kernel memory and internal FS. */
240 default:
241 case KMEM_DEV:
242 case RAM_DEV_OLD:
243 case IMGRD_DEV:
244 /* Bogus number. */
245 if(m_device < 0 || m_device >= NR_DEVS) {
246 return(EINVAL);
248 if(!dev_vaddr || dev_vaddr == (vir_bytes) MAP_FAILED) {
249 printf("MEM: dev %d not initialized\n", m_device);
250 return EIO;
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);
257 } else {
258 r=sys_safecopyfrom(proc_nr, user_vir, vir_offset,
259 dev_vaddr + position, count, D);
261 if(r != OK) {
262 panic("MEM","I/O copy failed",r);
264 break;
266 /* Physical copying. Only used to access entire memory.
267 * Transfer one 'page window' at a time.
269 case MEM_DEV:
271 u32_t pagestart, page_off;
272 static u32_t pagestart_mapped;
273 static int any_mapped = 0;
274 static char *vaddr;
275 int r;
276 u32_t subcount;
277 phys_bytes mem_phys;
279 if (position >= dv_size)
280 return(OK); /* check for EOF */
281 if (position + count > dv_size)
282 count = dv_size - position;
283 mem_phys = 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) {
292 if(any_mapped) {
293 if(vm_unmap_phys(SELF, vaddr, I386_PAGE_SIZE) != OK)
294 panic("MEM","vm_unmap_phys failed",NO_NUM);
295 any_mapped = 0;
297 vaddr = vm_map_phys(SELF, (void *) pagestart, I386_PAGE_SIZE);
298 if(vaddr == MAP_FAILED)
299 r = ENOMEM;
300 else
301 r = OK;
302 if(r != OK) {
303 printf("memory: vm_map_phys failed\n");
304 return r;
306 any_mapped = 1;
307 pagestart_mapped = pagestart;
310 /* how much to be done within this page. */
311 subcount = I386_PAGE_SIZE-page_off;
312 if(subcount > count)
313 subcount = count;
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);
318 } else {
319 s=sys_safecopyfrom(proc_nr, user_vir,
320 vir_offset, (vir_bytes) vaddr+page_off, subcount, D);
322 if(s != OK)
323 return s;
324 count = subcount;
325 break;
328 /* Null byte stream generator. */
329 case ZERO_DEV:
330 if (opcode == DEV_GATHER_S) {
331 size_t suboffset = 0;
332 left = count;
333 while (left > 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);
337 if(s != OK)
338 report("MEM","sys_safecopyto failed", s);
339 left -= chunk;
340 suboffset += chunk;
343 break;
347 /* Book the number of bytes transferred. */
348 position += count;
349 vir_offset += count;
350 if ((iov->iov_size -= count) == 0) { iov++; nr_req--; vir_offset = 0; }
353 return(OK);
356 /*===========================================================================*
357 * m_do_open *
358 *===========================================================================*/
359 PRIVATE int m_do_open(dp, m_ptr)
360 struct driver *dp;
361 message *m_ptr;
363 int r;
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);
370 if (r != OK)
372 printf("m_do_open: sys_enable_iop failed for %d: %d\n",
373 m_ptr->IO_ENDPT, r);
374 return r;
378 if(m_device < 0 || m_device >= NR_DEVS) {
379 panic("MEM","wrong m_device",m_device);
382 openct[m_device]++;
384 return(OK);
387 /*===========================================================================*
388 * m_do_close *
389 *===========================================================================*/
390 PRIVATE int m_do_close(dp, m_ptr)
391 struct driver *dp;
392 message *m_ptr;
394 int r;
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);
405 openct[m_device]--;
407 return(OK);
410 /*===========================================================================*
411 * m_ioctl *
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.
420 struct device *dv;
422 switch (m_ptr->REQUEST) {
423 case MIOCRAMSIZE: {
424 /* Someone wants to create a new RAM disk with the given size. */
425 u32_t ramdev_size;
426 int s, dev;
427 void *mem;
429 /* A ramdisk can be created only once, and only on RAM disk device. */
430 dev = m_ptr->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);
442 if (s != OK)
443 return s;
444 if(m_vaddrs[dev] && !cmp64(dv->dv_size, cvul64(ramdev_size))) {
445 return(OK);
447 /* openct is 1 for the ioctl(). */
448 if(openct[dev] != 1) {
449 printf("MEM: MIOCRAMSIZE: %d in use (count %d)\n",
450 dev, openct[dev]);
451 return(EBUSY);
453 if(m_vaddrs[dev]) {
454 u32_t size;
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;
463 #if DEBUG
464 printf("MEM:%d: allocating ramdisk of size 0x%x\n", dev, ramdev_size);
465 #endif
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");
471 return(ENOMEM);
474 m_vaddrs[dev] = (vir_bytes) mem;
476 dv->dv_size = cvul64(ramdev_size);
478 break;
481 default:
482 return(do_diocntl(&m_dtab, m_ptr));
484 return(OK);
487 /*===========================================================================*
488 * m_geometry *
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);
495 entry->heads = 64;
496 entry->sectors = 32;