source: trunk/minix/drivers/memory/memory.c@ 12

Last change on this file since 12 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 13.3 KB
Line 
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 *
10 * Changes:
11 * Apr 29, 2005 added null byte generator (Jorrit N. Herder)
12 * Apr 09, 2005 added support for boot device (Jorrit N. Herder)
13 * Jul 26, 2004 moved RAM driver to user-space (Jorrit N. Herder)
14 * Apr 20, 1992 device dependent/independent split (Kees J. Bot)
15 */
16
17#include "../drivers.h"
18#include "../libdriver/driver.h"
19#include <sys/ioc_memory.h>
20#include "../../kernel/const.h"
21#include "../../kernel/config.h"
22#include "../../kernel/type.h"
23
24#include <sys/vm.h>
25
26#include "assert.h"
27
28#include "local.h"
29
30#define NR_DEVS 7 /* number of minor devices */
31
32PRIVATE struct device m_geom[NR_DEVS]; /* base and size of each device */
33PRIVATE int m_seg[NR_DEVS]; /* segment index of each device */
34PRIVATE int m_device; /* current device */
35PRIVATE struct kinfo kinfo; /* kernel information */
36PRIVATE struct machine machine; /* machine information */
37
38extern int errno; /* error number for PM calls */
39
40FORWARD _PROTOTYPE( char *m_name, (void) );
41FORWARD _PROTOTYPE( struct device *m_prepare, (int device) );
42FORWARD _PROTOTYPE( int m_transfer, (int proc_nr, int opcode, off_t position,
43 iovec_t *iov, unsigned nr_req) );
44FORWARD _PROTOTYPE( int m_do_open, (struct driver *dp, message *m_ptr) );
45FORWARD _PROTOTYPE( void m_init, (void) );
46FORWARD _PROTOTYPE( int m_ioctl, (struct driver *dp, message *m_ptr) );
47FORWARD _PROTOTYPE( void m_geometry, (struct partition *entry) );
48
49/* Entry points to this driver. */
50PRIVATE struct driver m_dtab = {
51 m_name, /* current device's name */
52 m_do_open, /* open or mount */
53 do_nop, /* nothing on a close */
54 m_ioctl, /* specify ram disk geometry */
55 m_prepare, /* prepare for I/O on a given minor device */
56 m_transfer, /* do the I/O */
57 nop_cleanup, /* no need to clean up */
58 m_geometry, /* memory device "geometry" */
59 nop_signal, /* system signals */
60 nop_alarm,
61 nop_cancel,
62 nop_select,
63 NULL,
64 NULL
65};
66
67/* Buffer for the /dev/zero null byte feed. */
68#define ZERO_BUF_SIZE 1024
69PRIVATE char dev_zero[ZERO_BUF_SIZE];
70
71#define click_to_round_k(n) \
72 ((unsigned) ((((unsigned long) (n) << CLICK_SHIFT) + 512) / 1024))
73
74/*===========================================================================*
75 * main *
76 *===========================================================================*/
77PUBLIC int main(void)
78{
79/* Main program. Initialize the memory driver and start the main loop. */
80 struct sigaction sa;
81
82 sa.sa_handler = SIG_MESS;
83 sigemptyset(&sa.sa_mask);
84 sa.sa_flags = 0;
85 if (sigaction(SIGTERM,&sa,NULL)<0) panic("MEM","sigaction failed", errno);
86
87 m_init();
88 driver_task(&m_dtab);
89 return(OK);
90}
91
92/*===========================================================================*
93 * m_name *
94 *===========================================================================*/
95PRIVATE char *m_name()
96{
97/* Return a name for the current device. */
98 static char name[] = "memory";
99 return name;
100}
101
102/*===========================================================================*
103 * m_prepare *
104 *===========================================================================*/
105PRIVATE struct device *m_prepare(device)
106int device;
107{
108/* Prepare for I/O on a device: check if the minor device number is ok. */
109 if (device < 0 || device >= NR_DEVS) return(NIL_DEV);
110 m_device = device;
111
112 return(&m_geom[device]);
113}
114
115/*===========================================================================*
116 * m_transfer *
117 *===========================================================================*/
118PRIVATE int m_transfer(proc_nr, opcode, position, iov, nr_req)
119int proc_nr; /* process doing the request */
120int opcode; /* DEV_GATHER or DEV_SCATTER */
121off_t position; /* offset on device to read or write */
122iovec_t *iov; /* pointer to read or write request vector */
123unsigned nr_req; /* length of request vector */
124{
125/* Read or write one the driver's minor devices. */
126 phys_bytes mem_phys;
127 int seg;
128 unsigned count, left, chunk;
129 vir_bytes user_vir;
130 struct device *dv;
131 unsigned long dv_size;
132 int s;
133
134 /* Get minor device number and check for /dev/null. */
135 dv = &m_geom[m_device];
136 dv_size = cv64ul(dv->dv_size);
137
138 while (nr_req > 0) {
139
140 /* How much to transfer and where to / from. */
141 count = iov->iov_size;
142 user_vir = iov->iov_addr;
143
144 switch (m_device) {
145
146 /* No copying; ignore request. */
147 case NULL_DEV:
148 if (opcode == DEV_GATHER) return(OK); /* always at EOF */
149 break;
150
151 /* Virtual copying. For RAM disk, kernel memory and boot device. */
152 case RAM_DEV:
153 case KMEM_DEV:
154 case BOOT_DEV:
155 if (position >= dv_size) return(OK); /* check for EOF */
156 if (position + count > dv_size) count = dv_size - position;
157 seg = m_seg[m_device];
158
159 if (opcode == DEV_GATHER) { /* copy actual data */
160 sys_vircopy(SELF,seg,position, proc_nr,D,user_vir, count);
161 } else {
162 sys_vircopy(proc_nr,D,user_vir, SELF,seg,position, count);
163 }
164 break;
165
166 /* Physical copying. Only used to access entire memory. */
167 case MEM_DEV:
168 if (position >= dv_size) return(OK); /* check for EOF */
169 if (position + count > dv_size) count = dv_size - position;
170 mem_phys = cv64ul(dv->dv_base) + position;
171
172 if (opcode == DEV_GATHER) { /* copy data */
173 sys_physcopy(NONE, PHYS_SEG, mem_phys,
174 proc_nr, D, user_vir, count);
175 } else {
176 sys_physcopy(proc_nr, D, user_vir,
177 NONE, PHYS_SEG, mem_phys, count);
178 }
179 break;
180
181 /* Null byte stream generator. */
182 case ZERO_DEV:
183 if (opcode == DEV_GATHER) {
184 left = count;
185 while (left > 0) {
186 chunk = (left > ZERO_BUF_SIZE) ? ZERO_BUF_SIZE : left;
187 if (OK != (s=sys_vircopy(SELF, D, (vir_bytes) dev_zero,
188 proc_nr, D, user_vir, chunk)))
189 report("MEM","sys_vircopy failed", s);
190 left -= chunk;
191 user_vir += chunk;
192 }
193 }
194 break;
195
196 case IMGRD_DEV:
197 if (position >= dv_size) return(OK); /* check for EOF */
198 if (position + count > dv_size) count = dv_size - position;
199
200 if (opcode == DEV_GATHER) { /* copy actual data */
201 sys_vircopy(SELF, D, (vir_bytes)&imgrd[position],
202 proc_nr, D, user_vir, count);
203 } else {
204 sys_vircopy(proc_nr, D, user_vir,
205 SELF, D, (vir_bytes)&imgrd[position], count);
206 }
207 break;
208
209 /* Unknown (illegal) minor device. */
210 default:
211 return(EINVAL);
212 }
213
214 /* Book the number of bytes transferred. */
215 position += count;
216 iov->iov_addr += count;
217 if ((iov->iov_size -= count) == 0) { iov++; nr_req--; }
218
219 }
220 return(OK);
221}
222
223/*===========================================================================*
224 * m_do_open *
225 *===========================================================================*/
226PRIVATE int m_do_open(dp, m_ptr)
227struct driver *dp;
228message *m_ptr;
229{
230 int r;
231
232/* Check device number on open. */
233 if (m_prepare(m_ptr->DEVICE) == NIL_DEV) return(ENXIO);
234 if (m_device == MEM_DEV)
235 {
236 r = sys_enable_iop(m_ptr->IO_ENDPT);
237 if (r != OK)
238 {
239 printf("m_do_open: sys_enable_iop failed for %d: %d\n",
240 m_ptr->IO_ENDPT, r);
241 return r;
242 }
243 }
244 return(OK);
245}
246
247/*===========================================================================*
248 * m_init *
249 *===========================================================================*/
250PRIVATE void m_init()
251{
252 /* Initialize this task. All minor devices are initialized one by one. */
253 phys_bytes ramdev_size;
254 phys_bytes ramdev_base;
255 message m;
256 int i, s;
257
258 if (OK != (s=sys_getkinfo(&kinfo))) {
259 panic("MEM","Couldn't get kernel information.",s);
260 }
261
262 /* Install remote segment for /dev/kmem memory. */
263 m_geom[KMEM_DEV].dv_base = cvul64(kinfo.kmem_base);
264 m_geom[KMEM_DEV].dv_size = cvul64(kinfo.kmem_size);
265 if (OK != (s=sys_segctl(&m_seg[KMEM_DEV], (u16_t *) &s, (vir_bytes *) &s,
266 kinfo.kmem_base, kinfo.kmem_size))) {
267 panic("MEM","Couldn't install remote segment.",s);
268 }
269
270 /* Install remote segment for /dev/boot memory, if enabled. */
271 m_geom[BOOT_DEV].dv_base = cvul64(kinfo.bootdev_base);
272 m_geom[BOOT_DEV].dv_size = cvul64(kinfo.bootdev_size);
273 if (kinfo.bootdev_base > 0) {
274 if (OK != (s=sys_segctl(&m_seg[BOOT_DEV], (u16_t *) &s, (vir_bytes *) &s,
275 kinfo.bootdev_base, kinfo.bootdev_size))) {
276 panic("MEM","Couldn't install remote segment.",s);
277 }
278 }
279
280 /* See if there are already RAM disk details at the Data Store server. */
281 m.DS_KEY = MEMORY_MAJOR;
282 if (OK == (s = _taskcall(DS_PROC_NR, DS_RETRIEVE, &m))) {
283 ramdev_size = m.DS_VAL_L1;
284 ramdev_base = m.DS_VAL_L2;
285 printf("MEM retrieved size %u and base %u from DS, status %d\n",
286 ramdev_size, ramdev_base, s);
287 if (OK != (s=sys_segctl(&m_seg[RAM_DEV], (u16_t *) &s,
288 (vir_bytes *) &s, ramdev_base, ramdev_size))) {
289 panic("MEM","Couldn't install remote segment.",s);
290 }
291 m_geom[RAM_DEV].dv_base = cvul64(ramdev_base);
292 m_geom[RAM_DEV].dv_size = cvul64(ramdev_size);
293 printf("MEM stored retrieved details as new RAM disk\n");
294 }
295
296 /* Ramdisk image built into the memory driver */
297 m_geom[IMGRD_DEV].dv_base= cvul64(0);
298 m_geom[IMGRD_DEV].dv_size= cvul64(imgrd_size);
299
300 /* Initialize /dev/zero. Simply write zeros into the buffer. */
301 for (i=0; i<ZERO_BUF_SIZE; i++) {
302 dev_zero[i] = '\0';
303 }
304
305 /* Set up memory ranges for /dev/mem. */
306#if (CHIP == INTEL)
307 if (OK != (s=sys_getmachine(&machine))) {
308 panic("MEM","Couldn't get machine information.",s);
309 }
310 if (! machine.prot) {
311 m_geom[MEM_DEV].dv_size = cvul64(0x100000); /* 1M for 8086 systems */
312 } else {
313#if _WORD_SIZE == 2
314 m_geom[MEM_DEV].dv_size = cvul64(0x1000000); /* 16M for 286 systems */
315#else
316 m_geom[MEM_DEV].dv_size = cvul64(0xFFFFFFFF); /* 4G-1 for 386 systems */
317#endif
318 }
319#else /* !(CHIP == INTEL) */
320#if (CHIP == M68000)
321 m_geom[MEM_DEV].dv_size = cvul64(MEM_BYTES);
322#else /* !(CHIP == M68000) */
323#error /* memory limit not set up */
324#endif /* !(CHIP == M68000) */
325#endif /* !(CHIP == INTEL) */
326}
327
328/*===========================================================================*
329 * m_ioctl *
330 *===========================================================================*/
331PRIVATE int m_ioctl(dp, m_ptr)
332struct driver *dp; /* pointer to driver structure */
333message *m_ptr; /* pointer to control message */
334{
335/* I/O controls for the memory driver. Currently there is one I/O control:
336 * - MIOCRAMSIZE: to set the size of the RAM disk.
337 */
338 struct device *dv;
339
340 switch (m_ptr->REQUEST) {
341 case MIOCRAMSIZE: {
342 /* Someone wants to create a new RAM disk with the given size. */
343 static int first_time= 1;
344
345 u32_t ramdev_size;
346 phys_bytes ramdev_base;
347 message m;
348 int s;
349
350 /* A ramdisk can be created only once, and only on RAM disk device. */
351 if (!first_time) return(EPERM);
352 if (m_ptr->DEVICE != RAM_DEV) return(EINVAL);
353 if ((dv = m_prepare(m_ptr->DEVICE)) == NIL_DEV) return(ENXIO);
354
355#if 0
356 ramdev_size= m_ptr->POSITION;
357#else
358 /* Get request structure */
359 s= sys_vircopy(m_ptr->IO_ENDPT, D, (vir_bytes)m_ptr->ADDRESS,
360 SELF, D, (vir_bytes)&ramdev_size, sizeof(ramdev_size));
361 if (s != OK)
362 return s;
363#endif
364
365#if DEBUG
366 printf("allocating ramdisk of size 0x%x\n", ramdev_size);
367#endif
368
369 /* Try to allocate a piece of memory for the RAM disk. */
370 if (allocmem(ramdev_size, &ramdev_base) < 0) {
371 report("MEM", "warning, allocmem failed", errno);
372 return(ENOMEM);
373 }
374
375 /* Store the values we got in the data store so we can retrieve
376 * them later on, in the unfortunate event of a crash.
377 */
378 m.DS_KEY = MEMORY_MAJOR;
379 m.DS_VAL_L1 = ramdev_size;
380 m.DS_VAL_L2 = ramdev_base;
381 if (OK != (s = _taskcall(DS_PROC_NR, DS_PUBLISH, &m))) {
382 panic("MEM","Couldn't store RAM disk details at DS.",s);
383 }
384#if DEBUG
385 printf("MEM stored size %u and base %u at DS, status %d\n",
386 ramdev_size, ramdev_base, s);
387#endif
388
389 if (OK != (s=sys_segctl(&m_seg[RAM_DEV], (u16_t *) &s,
390 (vir_bytes *) &s, ramdev_base, ramdev_size))) {
391 panic("MEM","Couldn't install remote segment.",s);
392 }
393
394 dv->dv_base = cvul64(ramdev_base);
395 dv->dv_size = cvul64(ramdev_size);
396 /* first_time= 0; */
397 break;
398 }
399 case MIOCMAP:
400 case MIOCUNMAP: {
401 int r, do_map;
402 struct mapreq mapreq;
403
404 if ((*dp->dr_prepare)(m_ptr->DEVICE) == NIL_DEV) return(ENXIO);
405 if (m_device != MEM_DEV)
406 return ENOTTY;
407
408 do_map= (m_ptr->REQUEST == MIOCMAP); /* else unmap */
409
410 /* Get request structure */
411 r= sys_vircopy(m_ptr->IO_ENDPT, D, (vir_bytes)m_ptr->ADDRESS,
412 SELF, D, (vir_bytes)&mapreq, sizeof(mapreq));
413 if (r != OK)
414 return r;
415 r= sys_vm_map(m_ptr->IO_ENDPT, do_map,
416 (phys_bytes)mapreq.base, mapreq.size, mapreq.offset);
417 return r;
418 }
419
420 default:
421 return(do_diocntl(&m_dtab, m_ptr));
422 }
423 return(OK);
424}
425
426/*===========================================================================*
427 * m_geometry *
428 *===========================================================================*/
429PRIVATE void m_geometry(entry)
430struct partition *entry;
431{
432 /* Memory devices don't have a geometry, but the outside world insists. */
433 entry->cylinders = div64u(m_geom[m_device].dv_size, SECTOR_SIZE) / (64 * 32);
434 entry->heads = 64;
435 entry->sectors = 32;
436}
437
Note: See TracBrowser for help on using the repository browser.