Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1092264
  • 博文数量: 252
  • 博客积分: 4561
  • 博客等级: 上校
  • 技术积分: 2833
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-15 08:23
文章分类

全部博文(252)

文章存档

2015年(2)

2014年(1)

2013年(1)

2012年(16)

2011年(42)

2010年(67)

2009年(87)

2008年(36)

分类:

2010-12-27 10:03:29

#include <linux/completion.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/timer.h>
#include <linux/netdevice.h>
#include <linux/sched.h>
#include <linux/mm.h>
#include <linux/mm_types.h>
#include <linux/pagemap.h>
#include <linux/highmem.h>
#include <linux/io.h>

#include <net/net_namespace.h>

#define err(msg) printk("%s failed\n", msg)

static char *buffer;

static int lv_open(struct inode *inodp, struct file *filp)
{
    buffer = kmalloc(PAGE_SIZE, GFP_KERNEL | __GFP_ZERO);
    if (!buffer)
        goto out;

    SetPageReserved(virt_to_page(buffer));
    return 0;
out:
    return -ENODEV;
}

static int lv_release(struct inode *inodp, struct file *filp)
{
    printk(KERN_INFO "buffer: %s\n", buffer);
    ClearPageReserved(virt_to_page(buffer));
    kfree(buffer);
    return 0;
}

static int lv_mmap(struct file *filp, struct vm_area_struct *vma)
{
    unsigned long start = vma->vm_start;
    unsigned long size = vma->vm_end - vma->vm_start;
    
    if (size != PAGE_SIZE)
        goto out;

    if (remap_pfn_range(vma, start, __pa(buffer) >> PAGE_SHIFT, PAGE_SIZE, PAGE_SHARED))
    {
        err("remap_pfn_range");
        goto out;
    }

    return 0;
out:
    return -ENOMEM;
}

static struct file_operations fops =
{
    .open     = lv_open,
    .release = lv_release,
    .mmap     = lv_mmap,
};

static int main_init(void)
{
    if (register_chrdev(150, "lv", &fops) < 0)
    {
        err("register_chrdev");
        goto out;
    }
    
    printk("char device register ok.\n");
    return 0;
out:
    return -ENODEV;
}

static void main_exit(void)
{
    unregister_chrdev(150, "lv");
    printk("char device unregister ok.\n");
}

module_init(main_init);
module_exit(main_exit);
MODULE_LICENSE("GPL");


#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <sys/mman.h>

#define err(msg) perror(msg)

int main(void)
{
    int fd;
    char *addr;

    if ((fd = open("/root/wk/kernel/test/lv", O_RDWR)) == -1)
    {
        err("open");
        goto err;
    }
    
    addr = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    if (addr == MAP_FAILED)
    {
        err("mmap");
        goto err2;
    }
    strcpy(addr, "this is a mmap test");
    munmap(addr, 4096);
    close(fd);
    return 0;
err2:
    close(fd);
err:
    return -1;
}


阅读(1010) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~