panic() cleanup.
[minix.git] / drivers / memory / memory.c
blob0f7d402108b61b503a7b3b05dd8319a51ba7e618
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 <minix/ds.h>
22 #include <minix/vm.h>
23 #include <sys/mman.h>
24 #include "../../kernel/const.h"
25 #include "../../kernel/config.h"
26 #include "../../kernel/type.h"
28 #include <sys/vm_i386.h>
30 #include "local.h"
32 /* ramdisks (/dev/ram*) */
33 #define RAMDISKS 6
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 */
68 nop_alarm,
69 nop_cancel,
70 nop_select,
71 NULL,
72 NULL
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 /*===========================================================================*
87 * main *
88 *===========================================================================*/
89 PUBLIC int main(void)
91 /* SEF local startup. */
92 sef_local_startup();
94 /* Call the generic receive loop. */
95 driver_task(&m_dtab, DRIVER_STD);
97 return(OK);
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. */
115 sef_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. */
124 struct sigaction sa;
125 u32_t ramdev_size;
126 int i, s;
128 sa.sa_handler = SIG_MESS;
129 sigemptyset(&sa.sa_mask);
130 sa.sa_flags = 0;
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);
138 #if 0
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.");
146 #endif
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++) {
155 dev_zero[i] = '\0';
158 for(i = 0; i < NR_DEVS; i++)
159 openct[i] = 0;
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. */
167 return(OK);
170 /*===========================================================================*
171 * m_name *
172 *===========================================================================*/
173 PRIVATE char *m_name()
175 /* Return a name for the current device. */
176 static char name[] = "memory";
177 return name;
180 /*===========================================================================*
181 * m_prepare *
182 *===========================================================================*/
183 PRIVATE struct device *m_prepare(device)
184 int 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);
188 m_device = device;
190 return(&m_geom[device]);
193 /*===========================================================================*
194 * m_transfer *
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;
206 struct device *dv;
207 unsigned long dv_size;
208 int s, r;
209 off_t position;
210 vir_bytes dev_vaddr;
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];
222 while (nr_req > 0) {
224 /* How much to transfer and where to / from. */
225 count = iov->iov_size;
226 user_vir = iov->iov_addr;
228 switch (m_device) {
230 /* No copying; ignore request. */
231 case NULL_DEV:
232 if (opcode == DEV_GATHER_S) return(OK); /* always at EOF */
233 break;
235 /* Virtual copying. For RAM disks, kernel memory and internal FS. */
236 default:
237 case KMEM_DEV:
238 case RAM_DEV_OLD:
239 case IMGRD_DEV:
240 /* Bogus number. */
241 if(m_device < 0 || m_device >= NR_DEVS) {
242 return(EINVAL);
244 if(!dev_vaddr || dev_vaddr == (vir_bytes) MAP_FAILED) {
245 printf("MEM: dev %d not initialized\n", m_device);
246 return EIO;
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);
253 } else {
254 r=sys_safecopyfrom(proc_nr, user_vir, vir_offset,
255 dev_vaddr + position, count, D);
257 if(r != OK) {
258 panic("I/O copy failed: %d", r);
260 break;
262 /* Physical copying. Only used to access entire memory.
263 * Transfer one 'page window' at a time.
265 case MEM_DEV:
267 u32_t pagestart, page_off;
268 static u32_t pagestart_mapped;
269 static int any_mapped = 0;
270 static char *vaddr;
271 int r;
272 u32_t subcount;
273 phys_bytes mem_phys;
275 if (position >= dv_size)
276 return(OK); /* check for EOF */
277 if (position + count > dv_size)
278 count = dv_size - position;
279 mem_phys = 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) {
288 if(any_mapped) {
289 if(vm_unmap_phys(SELF, vaddr, I386_PAGE_SIZE) != OK)
290 panic("vm_unmap_phys failed");
291 any_mapped = 0;
293 vaddr = vm_map_phys(SELF, (void *) pagestart, I386_PAGE_SIZE);
294 if(vaddr == MAP_FAILED)
295 r = ENOMEM;
296 else
297 r = OK;
298 if(r != OK) {
299 printf("memory: vm_map_phys failed\n");
300 return r;
302 any_mapped = 1;
303 pagestart_mapped = pagestart;
306 /* how much to be done within this page. */
307 subcount = I386_PAGE_SIZE-page_off;
308 if(subcount > count)
309 subcount = count;
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);
314 } else {
315 s=sys_safecopyfrom(proc_nr, user_vir,
316 vir_offset, (vir_bytes) vaddr+page_off, subcount, D);
318 if(s != OK)
319 return s;
320 count = subcount;
321 break;
324 /* Null byte stream generator. */
325 case ZERO_DEV:
326 if (opcode == DEV_GATHER_S) {
327 size_t suboffset = 0;
328 left = count;
329 while (left > 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);
333 if(s != OK)
334 printf("MEM: sys_safecopyto failed: %d\n", s);
335 left -= chunk;
336 suboffset += chunk;
339 break;
343 /* Book the number of bytes transferred. */
344 position += count;
345 vir_offset += count;
346 if ((iov->iov_size -= count) == 0) { iov++; nr_req--; vir_offset = 0; }
349 return(OK);
352 /*===========================================================================*
353 * m_do_open *
354 *===========================================================================*/
355 PRIVATE int m_do_open(dp, m_ptr)
356 struct driver *dp;
357 message *m_ptr;
359 int r;
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);
366 if (r != OK)
368 printf("m_do_open: sys_enable_iop failed for %d: %d\n",
369 m_ptr->IO_ENDPT, r);
370 return r;
374 if(m_device < 0 || m_device >= NR_DEVS) {
375 panic("wrong m_device: %d", m_device);
378 openct[m_device]++;
380 return(OK);
383 /*===========================================================================*
384 * m_do_close *
385 *===========================================================================*/
386 PRIVATE int m_do_close(dp, m_ptr)
387 struct driver *dp;
388 message *m_ptr;
390 int r;
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");
401 openct[m_device]--;
403 return(OK);
406 /*===========================================================================*
407 * m_ioctl *
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.
416 struct device *dv;
418 switch (m_ptr->REQUEST) {
419 case MIOCRAMSIZE: {
420 /* Someone wants to create a new RAM disk with the given size. */
421 u32_t ramdev_size;
422 int s, dev;
423 void *mem;
425 /* A ramdisk can be created only once, and only on RAM disk device. */
426 dev = m_ptr->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);
438 if (s != OK)
439 return s;
440 if(m_vaddrs[dev] && !cmp64(dv->dv_size, cvul64(ramdev_size))) {
441 return(OK);
443 /* openct is 1 for the ioctl(). */
444 if(openct[dev] != 1) {
445 printf("MEM: MIOCRAMSIZE: %d in use (count %d)\n",
446 dev, openct[dev]);
447 return(EBUSY);
449 if(m_vaddrs[dev]) {
450 u32_t size;
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;
459 #if DEBUG
460 printf("MEM:%d: allocating ramdisk of size 0x%x\n", dev, ramdev_size);
461 #endif
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");
466 return(ENOMEM);
468 memset(mem, 0, ramdev_size);
470 m_vaddrs[dev] = (vir_bytes) mem;
472 dv->dv_size = cvul64(ramdev_size);
474 break;
477 default:
478 return(do_diocntl(&m_dtab, m_ptr));
480 return(OK);
483 /*===========================================================================*
484 * m_geometry *
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);
491 entry->heads = 64;
492 entry->sectors = 32;