厚德博学 敬业乐群
@sky
全部博文(252)
2015年(2)
2014年(1)
2013年(1)
2012年(16)
2011年(42)
2010年(67)
2009年(87)
2008年(36)
25742040
shijiulo
niuxlinu
ebayboy
hayand66
大鬼不动
acer1025
醉鬼的故
小雅贝贝
XINGCHEN
wzy_yzw
十的9次
zds05
bjywxc
zlhc1
smile124
cynthia
格伯纳
分类:
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; }
上一篇:内核空间映射内存到用户空间
下一篇:端口tagged与untagged含义详解
登录 注册