minix/drivers/memory/memory.c

477 lines
14 KiB
C
Raw Normal View History

2005-04-21 16:53:53 +02:00
/* This file contains the device dependent part of the drivers for the
* following special files:
* /dev/ram - RAM disk
* /dev/mem - absolute memory
* /dev/kmem - kernel virtual memory
* /dev/null - null device (data sink)
2005-04-29 17:36:43 +02:00
* /dev/boot - boot device loaded from boot image
* /dev/zero - null byte stream generator
2005-04-21 16:53:53 +02:00
*
* Changes:
2005-04-29 17:36:43 +02:00
* Apr 29, 2005 added null byte generator (Jorrit N. Herder)
* Apr 09, 2005 added support for boot device (Jorrit N. Herder)
2005-04-21 16:53:53 +02:00
* Jul 26, 2004 moved RAM driver to user-space (Jorrit N. Herder)
* Apr 20, 1992 device dependent/independent split (Kees J. Bot)
*/
#include "../drivers.h"
#include "../libdriver/driver.h"
#include <sys/ioc_memory.h>
#include <env.h>
#include <minix/ds.h>
#include "../../kernel/const.h"
#include "../../kernel/config.h"
#include "../../kernel/type.h"
2005-04-21 16:53:53 +02:00
#define MY_DS_NAME_BASE "dev:memory:ramdisk_base"
#define MY_DS_NAME_SIZE "dev:memory:ramdisk_size"
2005-09-30 14:57:30 +02:00
#include <sys/vm.h>
2005-07-18 17:40:24 +02:00
#include "assert.h"
#include "local.h"
#define NR_DEVS 7 /* number of minor devices */
2005-04-21 16:53:53 +02:00
2005-04-29 17:36:43 +02:00
PRIVATE struct device m_geom[NR_DEVS]; /* base and size of each device */
PRIVATE int m_seg[NR_DEVS]; /* segment index of each device */
2005-04-21 16:53:53 +02:00
PRIVATE int m_device; /* current device */
PRIVATE struct kinfo kinfo; /* kernel information */
PRIVATE struct machine machine; /* machine information */
extern int errno; /* error number for PM calls */
2005-04-21 16:53:53 +02:00
2005-09-11 19:09:11 +02:00
FORWARD _PROTOTYPE( char *m_name, (void) );
FORWARD _PROTOTYPE( struct device *m_prepare, (int device) );
FORWARD _PROTOTYPE( int m_transfer, (int proc_nr, int opcode, u64_t position,
2006-06-20 10:56:15 +02:00
iovec_t *iov, unsigned nr_req, int safe));
2005-09-11 19:09:11 +02:00
FORWARD _PROTOTYPE( int m_do_open, (struct driver *dp, message *m_ptr) );
2005-04-21 16:53:53 +02:00
FORWARD _PROTOTYPE( void m_init, (void) );
2006-06-20 10:56:15 +02:00
FORWARD _PROTOTYPE( int m_ioctl, (struct driver *dp, message *m_ptr, int safe));
2005-09-11 19:09:11 +02:00
FORWARD _PROTOTYPE( void m_geometry, (struct partition *entry) );
2005-04-21 16:53:53 +02:00
/* Entry points to this driver. */
PRIVATE struct driver m_dtab = {
m_name, /* current device's name */
m_do_open, /* open or mount */
do_nop, /* nothing on a close */
m_ioctl, /* specify ram disk geometry */
m_prepare, /* prepare for I/O on a given minor device */
m_transfer, /* do the I/O */
nop_cleanup, /* no need to clean up */
m_geometry, /* memory device "geometry" */
nop_signal, /* system signals */
2005-08-03 17:22:41 +02:00
nop_alarm,
nop_cancel,
nop_select,
NULL,
NULL
2005-04-21 16:53:53 +02:00
};
2005-04-29 17:36:43 +02:00
/* Buffer for the /dev/zero null byte feed. */
#define ZERO_BUF_SIZE 1024
PRIVATE char dev_zero[ZERO_BUF_SIZE];
2005-04-29 17:36:43 +02:00
#define click_to_round_k(n) \
((unsigned) ((((unsigned long) (n) << CLICK_SHIFT) + 512) / 1024))
2005-04-21 16:53:53 +02:00
/*===========================================================================*
* main *
*===========================================================================*/
2005-08-25 14:35:59 +02:00
PUBLIC int main(void)
2005-04-21 16:53:53 +02:00
{
/* Main program. Initialize the memory driver and start the main loop. */
struct sigaction sa;
sa.sa_handler = SIG_MESS;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
if (sigaction(SIGTERM,&sa,NULL)<0) panic("MEM","sigaction failed", errno);
m_init();
driver_task(&m_dtab);
2005-08-25 14:35:59 +02:00
return(OK);
2005-04-21 16:53:53 +02:00
}
/*===========================================================================*
* m_name *
*===========================================================================*/
PRIVATE char *m_name()
{
/* Return a name for the current device. */
static char name[] = "memory";
return name;
}
/*===========================================================================*
* m_prepare *
*===========================================================================*/
PRIVATE struct device *m_prepare(device)
int device;
{
/* Prepare for I/O on a device: check if the minor device number is ok. */
if (device < 0 || device >= NR_DEVS) return(NIL_DEV);
m_device = device;
return(&m_geom[device]);
}
/*===========================================================================*
* m_transfer *
*===========================================================================*/
PRIVATE int m_transfer(proc_nr, opcode, pos64, iov, nr_req, safe)
2005-04-21 16:53:53 +02:00
int proc_nr; /* process doing the request */
int opcode; /* DEV_GATHER_S or DEV_SCATTER_S */
u64_t pos64; /* offset on device to read or write */
2005-04-21 16:53:53 +02:00
iovec_t *iov; /* pointer to read or write request vector */
unsigned nr_req; /* length of request vector */
2006-06-20 10:56:15 +02:00
int safe; /* safe copies */
2005-04-21 16:53:53 +02:00
{
/* Read or write one the driver's minor devices. */
2005-08-25 14:35:59 +02:00
phys_bytes mem_phys;
2005-04-29 17:36:43 +02:00
int seg;
unsigned count, left, chunk;
2006-06-20 10:56:15 +02:00
vir_bytes user_vir, vir_offset = 0;
phys_bytes user_phys;
2005-04-21 16:53:53 +02:00
struct device *dv;
unsigned long dv_size;
2006-06-20 10:56:15 +02:00
int s, r;
off_t position;
2006-06-20 10:56:15 +02:00
static int n = 0;
2005-04-21 16:53:53 +02:00
if(!safe) {
printf("m_transfer: unsafe?\n");
return EPERM;
}
if (ex64hi(pos64) != 0)
return OK; /* Beyond EOF */
position= cv64ul(pos64);
2005-04-21 16:53:53 +02:00
/* Get minor device number and check for /dev/null. */
dv = &m_geom[m_device];
2005-04-21 16:53:53 +02:00
dv_size = cv64ul(dv->dv_size);
while (nr_req > 0) {
2005-04-29 17:36:43 +02:00
/* How much to transfer and where to / from. */
2005-04-21 16:53:53 +02:00
count = iov->iov_size;
user_vir = iov->iov_addr;
switch (m_device) {
2005-04-29 17:36:43 +02:00
/* No copying; ignore request. */
2005-04-21 16:53:53 +02:00
case NULL_DEV:
if (opcode == DEV_GATHER_S) return(OK); /* always at EOF */
2005-04-21 16:53:53 +02:00
break;
/* Virtual copying. For RAM disk, kernel memory and boot device. */
2005-04-29 17:36:43 +02:00
case RAM_DEV:
case KMEM_DEV:
case BOOT_DEV:
if (position >= dv_size) return(OK); /* check for EOF */
if (position + count > dv_size) count = dv_size - position;
seg = m_seg[m_device];
2005-04-29 17:36:43 +02:00
if (opcode == DEV_GATHER_S) { /* copy actual data */
2006-06-20 10:56:15 +02:00
r=sys_safecopyto(proc_nr, user_vir, vir_offset,
position, count, seg);
2005-04-29 17:36:43 +02:00
} else {
2006-06-20 10:56:15 +02:00
r=sys_safecopyfrom(proc_nr, user_vir, vir_offset,
position, count, seg);
}
if(r != OK) {
panic("MEM","I/O copy failed",r);
2005-04-29 17:36:43 +02:00
}
break;
/* Physical copying. Only used to access entire memory. */
case MEM_DEV:
if (position >= dv_size) {
printf("memory: read 0x%lx beyond physical memory of 0x%lx\n",
position, dv_size);
return(OK); /* check for EOF */
}
if (position + count > dv_size) {
printf("memory: truncating count from %d to ", count);
count = dv_size - position;
printf("%d (size %d)\n", count, dv_size);
}
2005-04-21 16:53:53 +02:00
mem_phys = cv64ul(dv->dv_base) + position;
if((r=sys_umap(proc_nr, GRANT_SEG, user_vir,
2006-06-20 10:56:15 +02:00
count + vir_offset, &user_phys)) != OK) {
panic("MEM","sys_umap failed in m_transfer",r);
}
2005-04-21 16:53:53 +02:00
if (opcode == DEV_GATHER_S) { /* copy data */
2005-04-29 17:36:43 +02:00
sys_physcopy(NONE, PHYS_SEG, mem_phys,
2006-06-20 10:56:15 +02:00
NONE, PHYS_SEG, user_phys + vir_offset, count);
2005-04-21 16:53:53 +02:00
} else {
2006-06-20 10:56:15 +02:00
sys_physcopy(NONE, PHYS_SEG, user_phys + vir_offset,
2005-04-29 17:36:43 +02:00
NONE, PHYS_SEG, mem_phys, count);
2005-04-21 16:53:53 +02:00
}
2005-04-29 17:36:43 +02:00
break;
/* Null byte stream generator. */
case ZERO_DEV:
if (opcode == DEV_GATHER_S) {
2006-06-20 10:56:15 +02:00
size_t suboffset = 0;
2005-04-29 17:36:43 +02:00
left = count;
while (left > 0) {
chunk = (left > ZERO_BUF_SIZE) ? ZERO_BUF_SIZE : left;
2006-06-20 10:56:15 +02:00
s=sys_safecopyto(proc_nr, user_vir,
vir_offset+suboffset, (vir_bytes) dev_zero, chunk, D);
if(s != OK)
report("MEM","sys_vircopy failed", s);
2005-04-29 17:36:43 +02:00
left -= chunk;
2006-06-20 10:56:15 +02:00
suboffset += chunk;
2005-04-29 17:36:43 +02:00
}
}
break;
case IMGRD_DEV:
if (position >= dv_size) return(OK); /* check for EOF */
if (position + count > dv_size) count = dv_size - position;
if (opcode == DEV_GATHER_S) { /* copy actual data */
2006-06-20 10:56:15 +02:00
s=sys_safecopyto(proc_nr, user_vir, vir_offset,
(vir_bytes)&imgrd[position], count, D);
} else {
2006-06-20 10:56:15 +02:00
s=sys_safecopyfrom(proc_nr, user_vir, vir_offset,
(vir_bytes)&imgrd[position], count, D);
}
break;
2005-04-29 17:36:43 +02:00
/* Unknown (illegal) minor device. */
default:
return(EINVAL);
2005-04-21 16:53:53 +02:00
}
/* Book the number of bytes transferred. */
position += count;
2006-06-20 10:56:15 +02:00
vir_offset += count;
if ((iov->iov_size -= count) == 0) { iov++; nr_req--; vir_offset = 0; }
2005-04-29 17:36:43 +02:00
2005-04-21 16:53:53 +02:00
}
return(OK);
}
2005-09-11 19:09:11 +02:00
/*===========================================================================*
* m_do_open *
*===========================================================================*/
2005-04-21 16:53:53 +02:00
PRIVATE int m_do_open(dp, m_ptr)
struct driver *dp;
message *m_ptr;
{
int r;
2005-10-20 22:26:02 +02:00
/* Check device number on open. */
2005-04-21 16:53:53 +02:00
if (m_prepare(m_ptr->DEVICE) == NIL_DEV) return(ENXIO);
if (m_device == MEM_DEV)
{
r = sys_enable_iop(m_ptr->IO_ENDPT);
if (r != OK)
{
printf("m_do_open: sys_enable_iop failed for %d: %d\n",
m_ptr->IO_ENDPT, r);
return r;
}
}
2005-04-21 16:53:53 +02:00
return(OK);
}
/*===========================================================================*
* m_init *
*===========================================================================*/
PRIVATE void m_init()
{
2005-04-29 17:36:43 +02:00
/* Initialize this task. All minor devices are initialized one by one. */
u32_t ramdev_size;
u32_t ramdev_base;
2005-10-20 22:26:02 +02:00
message m;
int i, s;
phys_bytes mem_top = 0;
/* Physical memory, to check validity of /dev/mem access. */
#define MAX_MEM_RANGES 10
struct memory mem_chunks[MAX_MEM_RANGES];
2005-04-21 16:53:53 +02:00
2005-04-29 17:36:43 +02:00
if (OK != (s=sys_getkinfo(&kinfo))) {
panic("MEM","Couldn't get kernel information.",s);
2005-04-21 16:53:53 +02:00
}
2005-04-22 15:25:17 +02:00
/* Obtain physical memory chunks for /dev/mem memory. */
if(env_memory_parse(mem_chunks, MAX_MEM_RANGES) != OK)
printf("memory driver: no memory layout, /dev/mem won't work\n");
else {
for(i = 0; i < MAX_MEM_RANGES; i++) {
phys_bytes top;
top = mem_chunks[i].base + mem_chunks[i].size;
if(top > mem_top)
mem_top = top;
}
}
2005-04-29 17:36:43 +02:00
/* Install remote segment for /dev/kmem memory. */
m_geom[KMEM_DEV].dv_base = cvul64(kinfo.kmem_base);
m_geom[KMEM_DEV].dv_size = cvul64(kinfo.kmem_size);
if (OK != (s=sys_segctl(&m_seg[KMEM_DEV], (u16_t *) &s, (vir_bytes *) &s,
kinfo.kmem_base, kinfo.kmem_size))) {
panic("MEM","Couldn't install remote segment.",s);
2005-04-29 17:36:43 +02:00
}
/* Install remote segment for /dev/boot memory, if enabled. */
m_geom[BOOT_DEV].dv_base = cvul64(kinfo.bootdev_base);
m_geom[BOOT_DEV].dv_size = cvul64(kinfo.bootdev_size);
if (kinfo.bootdev_base > 0) {
if (OK != (s=sys_segctl(&m_seg[BOOT_DEV], (u16_t *) &s, (vir_bytes *) &s,
kinfo.bootdev_base, kinfo.bootdev_size))) {
panic("MEM","Couldn't install remote segment.",s);
2005-04-29 17:36:43 +02:00
}
}
2005-04-22 15:25:17 +02:00
2005-10-20 22:26:02 +02:00
/* See if there are already RAM disk details at the Data Store server. */
if(ds_retrieve_u32(MY_DS_NAME_BASE, &ramdev_base) == OK &&
ds_retrieve_u32(MY_DS_NAME_SIZE, &ramdev_size) == OK) {
2005-10-20 22:26:02 +02:00
printf("MEM retrieved size %u and base %u from DS, status %d\n",
ramdev_size, ramdev_base, s);
if (OK != (s=sys_segctl(&m_seg[RAM_DEV], (u16_t *) &s,
(vir_bytes *) &s, ramdev_base, ramdev_size))) {
panic("MEM","Couldn't install remote segment.",s);
}
m_geom[RAM_DEV].dv_base = cvul64(ramdev_base);
m_geom[RAM_DEV].dv_size = cvul64(ramdev_size);
printf("MEM stored retrieved details as new RAM disk\n");
}
/* Ramdisk image built into the memory driver */
m_geom[IMGRD_DEV].dv_base= cvul64(0);
m_geom[IMGRD_DEV].dv_size= cvul64(imgrd_size);
/* Initialize /dev/zero. Simply write zeros into the buffer. */
2005-04-29 17:36:43 +02:00
for (i=0; i<ZERO_BUF_SIZE; i++) {
dev_zero[i] = '\0';
2005-04-29 17:36:43 +02:00
}
/* Set up memory range for /dev/mem. */
m_geom[MEM_DEV].dv_size = cvul64(mem_top);
2005-04-21 16:53:53 +02:00
}
/*===========================================================================*
* m_ioctl *
*===========================================================================*/
2006-06-20 10:56:15 +02:00
PRIVATE int m_ioctl(dp, m_ptr, safe)
struct driver *dp; /* pointer to driver structure */
message *m_ptr; /* pointer to control message */
2006-06-20 10:56:15 +02:00
int safe;
2005-04-21 16:53:53 +02:00
{
/* I/O controls for the memory driver. Currently there is one I/O control:
* - MIOCRAMSIZE: to set the size of the RAM disk.
*/
2005-04-21 16:53:53 +02:00
struct device *dv;
if(!safe) {
printf("m_transfer: unsafe?\n");
return EPERM;
}
2005-04-21 16:53:53 +02:00
switch (m_ptr->REQUEST) {
case MIOCRAMSIZE: {
/* Someone wants to create a new RAM disk with the given size. */
static int first_time= 1;
u32_t ramdev_size;
2005-04-29 17:36:43 +02:00
phys_bytes ramdev_base;
2005-10-20 22:26:02 +02:00
message m;
2005-04-21 16:53:53 +02:00
int s;
/* A ramdisk can be created only once, and only on RAM disk device. */
if (!first_time) return(EPERM);
2005-10-20 22:26:02 +02:00
if (m_ptr->DEVICE != RAM_DEV) return(EINVAL);
if ((dv = m_prepare(m_ptr->DEVICE)) == NIL_DEV) return(ENXIO);
2005-04-21 16:53:53 +02:00
#if 0
ramdev_size= m_ptr->POSITION;
#else
/* Get request structure */
2006-06-20 10:56:15 +02:00
s= sys_safecopyfrom(m_ptr->IO_ENDPT, (vir_bytes)m_ptr->IO_GRANT,
0, (vir_bytes)&ramdev_size, sizeof(ramdev_size), D);
if (s != OK)
return s;
#endif
2006-03-13 16:41:07 +01:00
#if DEBUG
printf("allocating ramdisk of size 0x%x\n", ramdev_size);
2006-03-13 16:41:07 +01:00
#endif
2005-04-29 17:36:43 +02:00
/* Try to allocate a piece of memory for the RAM disk. */
if (allocmem(ramdev_size, &ramdev_base) < 0) {
report("MEM", "warning, allocmem failed", errno);
return(ENOMEM);
}
2005-10-20 22:26:02 +02:00
/* Store the values we got in the data store so we can retrieve
* them later on, in the unfortunate event of a crash.
*/
if(ds_publish_u32(MY_DS_NAME_BASE, ramdev_base) != OK ||
ds_publish_u32(MY_DS_NAME_SIZE, ramdev_size) != OK) {
2005-10-20 22:26:02 +02:00
panic("MEM","Couldn't store RAM disk details at DS.",s);
}
#if DEBUG
printf("MEM stored size %u and base %u at DS, names %s and %s\n",
ramdev_size, ramdev_base, MY_DS_NAME_BASE, MY_DS_NAME_SIZE);
#endif
2005-10-20 22:26:02 +02:00
if (OK != (s=sys_segctl(&m_seg[RAM_DEV], (u16_t *) &s,
(vir_bytes *) &s, ramdev_base, ramdev_size))) {
panic("MEM","Couldn't install remote segment.",s);
2005-04-29 17:36:43 +02:00
}
2005-10-20 22:26:02 +02:00
dv->dv_base = cvul64(ramdev_base);
dv->dv_size = cvul64(ramdev_size);
2007-01-22 16:37:26 +01:00
first_time= 0;
2005-04-21 16:53:53 +02:00
break;
}
2005-09-30 14:57:30 +02:00
case MIOCMAP:
case MIOCUNMAP: {
int r, do_map;
struct mapreq mapreq;
if ((*dp->dr_prepare)(m_ptr->DEVICE) == NIL_DEV) return(ENXIO);
2005-09-30 14:57:30 +02:00
if (m_device != MEM_DEV)
return ENOTTY;
do_map= (m_ptr->REQUEST == MIOCMAP); /* else unmap */
/* Get request structure */
2006-06-20 10:56:15 +02:00
r= sys_safecopyfrom(m_ptr->IO_ENDPT, (vir_bytes)m_ptr->IO_GRANT,
0, (vir_bytes)&mapreq, sizeof(mapreq), D);
2005-09-30 14:57:30 +02:00
if (r != OK)
return r;
r= sys_vm_map(m_ptr->IO_ENDPT, do_map,
2005-09-30 14:57:30 +02:00
(phys_bytes)mapreq.base, mapreq.size, mapreq.offset);
return r;
}
2005-04-21 16:53:53 +02:00
default:
2006-06-20 10:56:15 +02:00
return(do_diocntl(&m_dtab, m_ptr, safe));
2005-04-21 16:53:53 +02:00
}
return(OK);
}
2005-09-11 19:09:11 +02:00
/*===========================================================================*
* m_geometry *
*===========================================================================*/
2005-04-21 16:53:53 +02:00
PRIVATE void m_geometry(entry)
struct partition *entry;
{
/* Memory devices don't have a geometry, but the outside world insists. */
entry->cylinders = div64u(m_geom[m_device].dv_size, SECTOR_SIZE) / (64 * 32);
entry->heads = 64;
entry->sectors = 32;
}