Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1070790
  • 博文数量: 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-23 15:55:18

#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 <net/net_namespace.h>

static unsigned char *virt_addr;

static int main_init(void)
{
    unsigned char *p;
    int i;

    virt_addr = (unsigned char *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 4);

    printk("physical address = %lx\n", virt_to_phys(virt_addr));

    for (p = virt_addr, i = 0; i < (2 << 4); i++, p += PAGE_SIZE)
    {
        SetPageReserved(virt_to_page(p));
    }

    return 0;
}

static void main_exit(void)
{
    unsigned char *p;
    int i;

    printk("%s\n", (char *)virt_addr);

    for (p = virt_addr, i = 0; i < (2 << 4); i++, p += PAGE_SIZE)
    {
        ClearPageReserved(virt_to_page(p));
    }

    free_pages((unsigned long)virt_addr, 4);
}

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("/dev/mem", O_RDWR)) == -1)
    {
        err("open");
        goto err;
    }
    
    addr = mmap(NULL, 4096 * (2 << 4), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0x1f170000);
    if (addr == MAP_FAILED)
    {
        err("mmap");
        goto err2;
    }

    strcpy(addr, "hello world");
    munmap(addr, 4096 * (2 << 4));
    close(fd);
    return 0;
err2:
    close(fd);
err:
    return -1;
}


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