免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2446 | 回复: 2
打印 上一主题 下一主题

内核使用户空间内存的问题。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-08-03 15:49 |只看该作者 |倒序浏览
我是参考onlyflyer文章里面的代码来写的,链接:http://bbs.chinaunix.net/thread-2143560-1-1.html。代码基本没怎么改。
insmod 和mknod加载驱动后,运行./user的时候出现OOPS。
我认为应该是下面的代码有问题:
       __DEBUG_MSG("before pte_offset_map\n");
        pte = pte_offset_map(pmd, vaddr);
        __DEBUG_MSG("after pte_offset_map\n");
       printf("test1");/////////////////////只能打印到这里
        pte_unmap(pte);
        if ( pte_none(*pte) )
        {
               printf("test2222");//////////////////////这个信息打印不出来。。
                __DEBUG_MSG("bad pte va: %X pte: %p pteval: %lX\n", vaddr, pte, pte_val(*pte));
                retval = -1;
                goto error;
        }
因为用printk打印信息可知   if ( pte_none(*pte) )  这句和之后出错。请大家帮忙看看什么情况阿。。谢谢。

完整代码如下(我后来改称用register_chrdev_alloc取代register_chrdev):
---------------------------------kernel.h--------------------------------------
/*
* kernel.h 公用头文件
*/
#ifndef KMAP_KERNEL_H
#define KMAP_KERNEL_H

#ifdef KMAP_KERNEL
        #define __DEBUG_MSG(a,x...) do{printk(KERN_ALERT"%s %s %d:"a,__FILE__,__FUNCTION__,__LINE__,##x);}while(0)
#else
        #define __DEBUG_MSG(a,x...) do{fprintf(stderr, "%s %s %d:"a, __FILE__, __FUNCTION__, __LINE__, ##x);}while(0)
#endif

#define DEVICE_NAME        "kmap"

#define KMAP_IOC_MAGIC        'q'
#define KMAP_IOC_SET_PID_AND_VADDR        _IOW(KMAP_IOC_MAGIC, 1, char *)

#ifdef KMAP_KERNEL
int init_module(void);
void cleanup_module(void);

static int kmap_open(struct inode *, struct file *);
static int kmap_ioctl(struct inode *, struct file *,
                                                unsigned int, unsigned long);
#endif

struct ioctl_arg
{
        pid_t pid;
        unsigned long vaddr;
};
#endif

------------------------------------kernel.c-------------------------------------
/*
*kernel.c 内核实现部分
*/
#include <linux/config.h>
#include <linux/types.h>
#include <linux/sched.h>
#include <linux/mm.h>
#include <linux/fcntl.h>
#include <linux/socket.h>
#include <linux/in.h>
#include <linux/inet.h>
#include <linux/netdevice.h>
#include <linux/if_packet.h>
#include <linux/wireless.h>
#include <linux/kmod.h>
#include <net/ip.h>
#include <net/protocol.h>
#include <linux/skbuff.h>
#include <net/sock.h>
#include <linux/errno.h>
#include <linux/timer.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/workqueue.h>

#include <linux/mempolicy.h>
#include <linux/rmap.h>
#include <linux/fs.h>
#include <linux/shm.h>
#include <linux/mm.h>
#include <linux/mman.h>
#include <linux/pagemap.h>
#include <linux/swap.h>
#include <linux/hugetlb.h>
#include <linux/mman.h>
#include <linux/slab.h>
#include <linux/swapops.h>

#include <asm/io.h>
#include <asm/uaccess.h>
#include <asm/tlb.h>
#include <asm/tlbflush.h>
#include <asm/pgtable.h>
#include <asm/pgalloc.h>
#include <asm/uaccess.h>
#include <asm/cacheflush.h>

#define        KMAP_KERNEL

#include "kernel.h"

static int major = 0;
static struct file_operations fops =
{
        .open = kmap_open,
        .ioctl = kmap_ioctl
};

int init_module(void)
{
        major = register_chrdev(0, DEVICE_NAME, &fops);
        if ( major < 0 )
        {
                printk(KERN_ALERT"register chrdev failed:%d\n", major);
                return major;
        }

        printk(KERN_ALERT"mknod /dev/%s c %d 0\n", DEVICE_NAME, major);
        return 0;
}

void cleanup_module(void)
{
        /*unregister the device*/
        int ret = unregister_chrdev(major, DEVICE_NAME);
        if ( ret < 0 )
        {
                __DEBUG_MSG("Error in unregister_chrdev:%d\n", ret);
        }
}

static int kmap_open(struct inode *inode, struct file *filp)
{
        __DEBUG_MSG("enter kmap_open\n");
        return 0;
}
static int kmap_ioctl(struct inode *inode, struct file *filp,
                                          unsigned int cmd , unsigned long arg)
{
        pid_t pid = 0;
        unsigned long vaddr = 0;
        unsigned long phy = 0;
        unsigned long laddr = 0;

        int err = 0, retval = 0;

        pgd_t *pgd = NULL;
        pud_t *pud = NULL;
        pmd_t *pmd = NULL;
        pte_t *pte = NULL;

        struct page *mypage;

        struct ioctl_arg k_arg;

        __memset_generic(&k_arg, 0, sizeof(struct ioctl_arg));

        /*get arg*/
        err = !access_ok(VERIFY_READ, arg, sizeof(struct ioctl_arg));
        if ( err )
        {
                __DEBUG_MSG("access vaild failed\n");
                return -EFAULT;
        }

        err = copy_from_user((void *)&k_arg, (const void *)arg,
                                                        sizeof(struct ioctl_arg));
        pid = k_arg.pid;
        vaddr = k_arg.vaddr;

        __DEBUG_MSG("pid:%d vaddr:%lu\n", pid, vaddr);
        /*
        *convert vaddr to kernel logic addr
        *1,get the current task by pid
        *2,get the phyaddr
        *3,convert phyaddr to kernel logic addr
        *4,write sth. in the buffer

        *NOTE:step 1 is not necessay,because now current is pointed to task
        */

        __DEBUG_MSG("The pid which user passed in is:%d\n"
                                " The current pid is:%d\n", pid, current->pid);
        /*get phyaddr*/
        pgd = pgd_offset(current->mm, vaddr);
        if ( pgd_none(*pgd) || pgd_bad(*pgd) )
        {
                __DEBUG_MSG("invalid pgd\n");
                retval = -1;
                goto error;
        }

        pud = pud_offset(pgd, vaddr);
        if ( pud_none(*pud) || pud_bad(*pud) )
        {
                __DEBUG_MSG("invalid pud\n");
                retval = -1;
                goto error;
        }

        pmd = pmd_offset(pud, vaddr);
        if ( pmd_none(*pmd) || pmd_bad(*pmd) )
        {
                __DEBUG_MSG("invalid pmd\n");
                retval = -1;
                goto error;
        }
        __DEBUG_MSG("before pte_offset_map\n");
        pte = pte_offset_map(pmd, vaddr);
        __DEBUG_MSG("after pte_offset_map\n");
        pte_unmap(pte);
        if ( pte_none(*pte) )
        {
                __DEBUG_MSG("bad pte va: %X pte: %p pteval: %lX\n", vaddr, pte, pte_val(*pte));
                retval = -1;
                goto error;
        }

        phy = (pte_val(*pte)) & PAGE_MASK;

        __DEBUG_MSG("the phy addr is %lu\n", phy);

        mypage = pte_page(*pte);
        set_bit(PG_locked, &mypage->flags);
        atomic_inc(&mypage->_count);

        /*convert phy to kernel logic addr*/
        laddr = __pa(phy);
        strcpy((char *)laddr, "hello kmap");

        error:
        return retval;
}
------------------------------------user.c-------------------------------------
/*
*user.c 用户空间部分
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>/*getpagesize*/
#include <errno.h>

#include "kernel.h"

#define CHAR_DEV_PATH        "/dev/"DEVICE_NAME

int main(int argc, char **argv)
{
        int fd = -1;
        pid_t pid = 0;
        unsigned int vaddr = 0;
        int result = 0;
        int page_size =0;
        struct ioctl_arg arg;
        int retval = 0;

        memset(&arg, 0, sizeof(struct ioctl_arg));

        /*get page size*/
        page_size = getpagesize();

        /*get pid*/
        pid = getpid();

        vaddr = (unsigned long)valloc(page_size);
        memset((char *)vaddr, 0, page_size);
        strcpy((char *)vaddr, "hello kmap");

        /*assemble arg*/
        __DEBUG_MSG("The pid and the vaddr is:%d, %lu\n", pid, vaddr);
        arg.pid = pid;
        arg.vaddr = vaddr;

        /*open the device*/
        fd = open(CHAR_DEV_PATH, O_RDWR);

        if ( fd < 0 )
        {
                __DEBUG_MSG("error when open device file:%s\n", strerror(errno));
                retval = -1;
                goto error;
        }

        /*pass the arg to kernel*/
        result = ioctl(fd, KMAP_IOC_SET_PID_AND_VADDR, &arg);
        if ( result )
        {
                __DEBUG_MSG("error when ioctl:%s\n", strerror(errno));
        }

        printf("after ioctl we get the memory:%s\n", (char *)vaddr);

        error:
        if ( fd != -1 )
        {
                close(fd);
        }
        return retval;
}

论坛徽章:
0
2 [报告]
发表于 2011-08-03 15:50 |只看该作者
原文中__pa(phy)的错误我也该过了。。。请大家帮忙看看阿

论坛徽章:
0
3 [报告]
发表于 2011-08-03 17:28 |只看该作者
救命啊,帮忙解答一下~~~
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP