|
kernel创建一设备分配内存, 并将内存地址通过d_mmap 映射, userland使用mmap!
参考/sys/dev/mem/memdev.c.
FreeBSD 7.1
kernel 代码:
#include <sys/cdefs.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/conf.h>
#include <sys/uio.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/module.h>
#include <sys/priv.h>
#include <sys/disk.h>
#include <vm/vm.h>
#include <vm/pmap.h>
//#include <sys/bus.h>
//#include <machine/bus.h>
/* For use with destroy_dev(9). */
static struct cdev *memio_dev;
static d_write_t memio_write;
static d_ioctl_t memio_ioctl;
static d_read_t memio_read;
static d_mmap_t memio_mmap;
#define MEMIO_MINOR 212
static struct cdevsw memio_cdevsw = {
.d_version = D_VERSION,
.d_read = memio_read,
.d_write = memio_write,
.d_ioctl = memio_ioctl,
.d_mmap = memio_mmap,
.d_name = \"memio\",
};
static void *zbuf;
static int
memio_read(struct cdev *dev __unused, struct uio *uio, int flags __unused)
{
int error = 0;
while (uio->uio_resid > 0 && error == 0)
error = uiomove(zbuf, MIN(uio->uio_resid, PAGE_SIZE), uio);
return error;
}
/* ARGSUSED */
static int
memio_write(struct cdev *dev __unused, struct uio *uio, int flags __unused)
{
int error = 0;
while( uio->uio_resid > 0 && error == 0 )
error = uiomove(zbuf, MIN(uio->uio_resid, PAGE_SIZE), uio);
return (0);
}
static int
memio_mmap(struct cdev *dev, vm_offset_t offset, vm_paddr_t *paddr,
int prot __unused)
{
*paddr = vtophys( zbuf );
return (0);
}
/* ARGSUSED */
static int
memio_ioctl(struct cdev *dev __unused, u_long cmd, caddr_t data __unused,
int flags __unused, struct thread *td)
{
int error;
if (cmd != DIOCSKERNELDUMP)
return (ENOIOCTL);
error = priv_check(td, PRIV_SETDUMPER);
if (error)
return (error);
return (set_dumper(NULL));
}
/* ARGSUSED */
static int
memio_modevent(module_t mod __unused, int type, void *data __unused)
{
switch(type) {
case MOD_LOAD:
if (bootverbose)
printf(\"memio: <memio device>\\n\");
zbuf = (void *)malloc(PAGE_SIZE, M_TEMP, M_WAITOK | M_ZERO);
memcpy( zbuf, \"hello from kernel\", 18 );
memio_dev = make_dev(&memio_cdevsw, MEMIO_MINOR, UID_ROOT,
GID_WHEEL, 0666, \"memio\");
uprintf( \"load memio module.\\n\" );
break;
case MOD_UNLOAD:
destroy_dev(memio_dev);
free(zbuf, M_TEMP);
uprintf( \"unload memio module.\\n\" );
break;
case MOD_SHUTDOWN:
break;
default:
return (EOPNOTSUPP);
}
return (0);
}
DEV_MODULE(memio, memio_modevent, NULL);
MODULE_VERSION(memio, 1);
Makefile
SRCS = main.c
KMOD = memio
KO = $(KMOD).ko
KLDMOD = t
.include <bsd.kmod.mk>
userland test代码
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/mman.h>
#define PAGE_SIZE 4096
int main ( int argc, char **argv )
{
int configfd;
configfd = open(\"/dev/memio\", O_RDWR);
if(configfd < 0) {
perror(\"open\");
return -1;
}
char * address = NULL;
address = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, configfd, 0);
if (address == MAP_FAILED) {
perror(\"mmap\");
return -1;
}
printf(\"address=%p\\n\", address );
printf(\"initial message: %s\\n\", address);
memcpy(address + 11 , \"*user*\", 6);
printf(\"changed message: %s\\n\", address);
close(configfd);
return 0;
} |
|