source: branches/minix3-book/drivers/memory/memory.c@ 11

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

Importazione sorgenti libro

File size: 10.2 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 "assert.h"
25
26#define NR_DEVS 6 /* number of minor devices */
27
28PRIVATE struct device m_geom[NR_DEVS]; /* base and size of each device */
29PRIVATE int m_seg[NR_DEVS]; /* segment index of each device */
30PRIVATE int m_device; /* current device */
31PRIVATE struct kinfo kinfo; /* kernel information */
32PRIVATE struct machine machine; /* machine information */
33
34extern int errno; /* error number for PM calls */
35
36FORWARD _PROTOTYPE( char *m_name, (void) );
37FORWARD _PROTOTYPE( struct device *m_prepare, (int device) );
38FORWARD _PROTOTYPE( int m_transfer, (int proc_nr, int opcode, off_t position,
39 iovec_t *iov, unsigned nr_req) );
40FORWARD _PROTOTYPE( int m_do_open, (struct driver *dp, message *m_ptr) );
41FORWARD _PROTOTYPE( void m_init, (void) );
42FORWARD _PROTOTYPE( int m_ioctl, (struct driver *dp, message *m_ptr) );
43FORWARD _PROTOTYPE( void m_geometry, (struct partition *entry) );
44
45/* Entry points to this driver. */
46PRIVATE struct driver m_dtab = {
47 m_name, /* current device's name */
48 m_do_open, /* open or mount */
49 do_nop, /* nothing on a close */
50 m_ioctl, /* specify ram disk geometry */
51 m_prepare, /* prepare for I/O on a given minor device */
52 m_transfer, /* do the I/O */
53 nop_cleanup, /* no need to clean up */
54 m_geometry, /* memory device "geometry" */
55 nop_signal, /* system signals */
56 nop_alarm,
57 nop_cancel,
58 nop_select,
59 NULL,
60 NULL
61};
62
63/* Buffer for the /dev/zero null byte feed. */
64#define ZERO_BUF_SIZE 1024
65PRIVATE char dev_zero[ZERO_BUF_SIZE];
66
67#define click_to_round_k(n) \
68 ((unsigned) ((((unsigned long) (n) << CLICK_SHIFT) + 512) / 1024))
69
70/*===========================================================================*
71 * main *
72 *===========================================================================*/
73PUBLIC int main(void)
74{
75/* Main program. Initialize the memory driver and start the main loop. */
76 m_init();
77 driver_task(&m_dtab);
78 return(OK);
79}
80
81/*===========================================================================*
82 * m_name *
83 *===========================================================================*/
84PRIVATE char *m_name()
85{
86/* Return a name for the current device. */
87 static char name[] = "memory";
88 return name;
89}
90
91/*===========================================================================*
92 * m_prepare *
93 *===========================================================================*/
94PRIVATE struct device *m_prepare(device)
95int device;
96{
97/* Prepare for I/O on a device: check if the minor device number is ok. */
98 if (device < 0 || device >= NR_DEVS) return(NIL_DEV);
99 m_device = device;
100
101 return(&m_geom[device]);
102}
103
104/*===========================================================================*
105 * m_transfer *
106 *===========================================================================*/
107PRIVATE int m_transfer(proc_nr, opcode, position, iov, nr_req)
108int proc_nr; /* process doing the request */
109int opcode; /* DEV_GATHER or DEV_SCATTER */
110off_t position; /* offset on device to read or write */
111iovec_t *iov; /* pointer to read or write request vector */
112unsigned nr_req; /* length of request vector */
113{
114/* Read or write one the driver's minor devices. */
115 phys_bytes mem_phys;
116 int seg;
117 unsigned count, left, chunk;
118 vir_bytes user_vir;
119 struct device *dv;
120 unsigned long dv_size;
121 int s;
122
123 /* Get minor device number and check for /dev/null. */
124 dv = &m_geom[m_device];
125 dv_size = cv64ul(dv->dv_size);
126
127 while (nr_req > 0) {
128
129 /* How much to transfer and where to / from. */
130 count = iov->iov_size;
131 user_vir = iov->iov_addr;
132
133 switch (m_device) {
134
135 /* No copying; ignore request. */
136 case NULL_DEV:
137 if (opcode == DEV_GATHER) return(OK); /* always at EOF */
138 break;
139
140 /* Virtual copying. For RAM disk, kernel memory and boot device. */
141 case RAM_DEV:
142 case KMEM_DEV:
143 case BOOT_DEV:
144 if (position >= dv_size) return(OK); /* check for EOF */
145 if (position + count > dv_size) count = dv_size - position;
146 seg = m_seg[m_device];
147
148 if (opcode == DEV_GATHER) { /* copy actual data */
149 sys_vircopy(SELF,seg,position, proc_nr,D,user_vir, count);
150 } else {
151 sys_vircopy(proc_nr,D,user_vir, SELF,seg,position, count);
152 }
153 break;
154
155 /* Physical copying. Only used to access entire memory. */
156 case MEM_DEV:
157 if (position >= dv_size) return(OK); /* check for EOF */
158 if (position + count > dv_size) count = dv_size - position;
159 mem_phys = cv64ul(dv->dv_base) + position;
160
161 if (opcode == DEV_GATHER) { /* copy data */
162 sys_physcopy(NONE, PHYS_SEG, mem_phys,
163 proc_nr, D, user_vir, count);
164 } else {
165 sys_physcopy(proc_nr, D, user_vir,
166 NONE, PHYS_SEG, mem_phys, count);
167 }
168 break;
169
170 /* Null byte stream generator. */
171 case ZERO_DEV:
172 if (opcode == DEV_GATHER) {
173 left = count;
174 while (left > 0) {
175 chunk = (left > ZERO_BUF_SIZE) ? ZERO_BUF_SIZE : left;
176 if (OK != (s=sys_vircopy(SELF, D, (vir_bytes) dev_zero,
177 proc_nr, D, user_vir, chunk)))
178 report("MEM","sys_vircopy failed", s);
179 left -= chunk;
180 user_vir += chunk;
181 }
182 }
183 break;
184
185 /* Unknown (illegal) minor device. */
186 default:
187 return(EINVAL);
188 }
189
190 /* Book the number of bytes transferred. */
191 position += count;
192 iov->iov_addr += count;
193 if ((iov->iov_size -= count) == 0) { iov++; nr_req--; }
194
195 }
196 return(OK);
197}
198
199/*===========================================================================*
200 * m_do_open *
201 *===========================================================================*/
202PRIVATE int m_do_open(dp, m_ptr)
203struct driver *dp;
204message *m_ptr;
205{
206/* Check device number on open. (This used to give I/O privileges to a
207 * process opening /dev/mem or /dev/kmem. This may be needed in case of
208 * memory mapped I/O. With system calls to do I/O this is no longer needed.)
209 */
210 if (m_prepare(m_ptr->DEVICE) == NIL_DEV) return(ENXIO);
211
212 return(OK);
213}
214
215/*===========================================================================*
216 * m_init *
217 *===========================================================================*/
218PRIVATE void m_init()
219{
220 /* Initialize this task. All minor devices are initialized one by one. */
221 int i, s;
222
223 if (OK != (s=sys_getkinfo(&kinfo))) {
224 panic("MEM","Couldn't get kernel information.",s);
225 }
226
227 /* Install remote segment for /dev/kmem memory. */
228 m_geom[KMEM_DEV].dv_base = cvul64(kinfo.kmem_base);
229 m_geom[KMEM_DEV].dv_size = cvul64(kinfo.kmem_size);
230 if (OK != (s=sys_segctl(&m_seg[KMEM_DEV], (u16_t *) &s, (vir_bytes *) &s,
231 kinfo.kmem_base, kinfo.kmem_size))) {
232 panic("MEM","Couldn't install remote segment.",s);
233 }
234
235 /* Install remote segment for /dev/boot memory, if enabled. */
236 m_geom[BOOT_DEV].dv_base = cvul64(kinfo.bootdev_base);
237 m_geom[BOOT_DEV].dv_size = cvul64(kinfo.bootdev_size);
238 if (kinfo.bootdev_base > 0) {
239 if (OK != (s=sys_segctl(&m_seg[BOOT_DEV], (u16_t *) &s, (vir_bytes *) &s,
240 kinfo.bootdev_base, kinfo.bootdev_size))) {
241 panic("MEM","Couldn't install remote segment.",s);
242 }
243 }
244
245 /* Initialize /dev/zero. Simply write zeros into the buffer. */
246 for (i=0; i<ZERO_BUF_SIZE; i++) {
247 dev_zero[i] = '\0';
248 }
249
250 /* Set up memory ranges for /dev/mem. */
251 if (OK != (s=sys_getmachine(&machine))) {
252 panic("MEM","Couldn't get machine information.",s);
253 }
254 if (! machine.protected) {
255 m_geom[MEM_DEV].dv_size = cvul64(0x100000); /* 1M for 8086 systems */
256 } else {
257 m_geom[MEM_DEV].dv_size = cvul64(0xFFFFFFFF); /* 4G-1 for 386 systems */
258 }
259}
260
261/*===========================================================================*
262 * m_ioctl *
263 *===========================================================================*/
264PRIVATE int m_ioctl(dp, m_ptr)
265struct driver *dp; /* pointer to driver structure */
266message *m_ptr; /* pointer to control message */
267{
268/* I/O controls for the memory driver. Currently there is one I/O control:
269 * - MIOCRAMSIZE: to set the size of the RAM disk.
270 */
271 struct device *dv;
272 if ((dv = m_prepare(m_ptr->DEVICE)) == NIL_DEV) return(ENXIO);
273
274 switch (m_ptr->REQUEST) {
275 case MIOCRAMSIZE: {
276 /* FS wants to create a new RAM disk with the given size. */
277 phys_bytes ramdev_size;
278 phys_bytes ramdev_base;
279 int s;
280
281 if (m_ptr->PROC_NR != FS_PROC_NR) {
282 report("MEM", "warning, MIOCRAMSIZE called by", m_ptr->PROC_NR);
283 return(EPERM);
284 }
285
286 /* Try to allocate a piece of memory for the RAM disk. */
287 ramdev_size = m_ptr->POSITION;
288 if (allocmem(ramdev_size, &ramdev_base) < 0) {
289 report("MEM", "warning, allocmem failed", errno);
290 return(ENOMEM);
291 }
292 dv->dv_base = cvul64(ramdev_base);
293 dv->dv_size = cvul64(ramdev_size);
294
295 if (OK != (s=sys_segctl(&m_seg[RAM_DEV], (u16_t *) &s, (vir_bytes *) &s,
296 ramdev_base, ramdev_size))) {
297 panic("MEM","Couldn't install remote segment.",s);
298 }
299 break;
300 }
301
302 default:
303 return(do_diocntl(&m_dtab, m_ptr));
304 }
305 return(OK);
306}
307
308/*===========================================================================*
309 * m_geometry *
310 *===========================================================================*/
311PRIVATE void m_geometry(entry)
312struct partition *entry;
313{
314 /* Memory devices don't have a geometry, but the outside world insists. */
315 entry->cylinders = div64u(m_geom[m_device].dv_size, SECTOR_SIZE) / (64 * 32);
316 entry->heads = 64;
317 entry->sectors = 32;
318}
Note: See TracBrowser for help on using the repository browser.