近几天有个项目需要实现用户层与内核共享内存。在前几篇博客中找到的均是kernel2.4 的。实现过程中,改的痛苦。对kernel源码才,勉强搞定。
后来在此网站(http://www.scs.ch/~frey/linux/memorymap.html)发现原来有一个demo:
#include <linux/config.h>
#include <linux/version.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
#include <linux/mm.h>
#ifdef MODVERSIONS
#
#endif
#include <asm/io.h>
static dev_t mmap_dev;
static struct cdev mmap_cdev;
static int mmap_open(struct inode *inode, struct file *filp);
static int mmap_release(struct inode *inode, struct file *filp);
static int mmap_mmap(struct file *filp, struct vm_area_struct *vma);
static struct file_operations mmap_fops = {
};
// internal data
// length of the two memory areas
#define NPAGES 16
// pointer to the vmalloc'd area - alway page aligned
static int *vmalloc_area;
// pointer to the kmalloc'd area, rounded up to a page boundary
static int *kmalloc_area;
// original pointer for kmalloc'd area as returned by kmalloc
static void *kmalloc_ptr;
static int mmap_open(struct inode *inode, struct file *filp)
{
}
static int mmap_release(struct inode *inode, struct file *filp)
{
}
// helper function, mmap's the kmalloc'd area which is physically contiguous
int mmap_kmem(struct file *filp, struct vm_area_struct *vma)
{
}
// helper function, mmap's the vmalloc'd area which is not physically contiguous
int mmap_vmem(struct file *filp, struct vm_area_struct *vma)
{
}
static int mmap_mmap(struct file *filp, struct vm_area_struct *vma)
{
}
static int __init mmap_init(void)
{
}
static void __exit mmap_exit(void)
{
}
module_init(mmap_init);
module_exit(mmap_exit);
MODULE_DESCRIPTION("mmap demo driver");
MODULE_AUTHOR("Martin Frey <frey@scs.ch>");
MODULE_LICENSE("BSD");