Chinaunix
标题:
insmod 某个驱动报错问题
[打印本页]
作者:
idi0t
时间:
2013-11-05 16:59
标题:
insmod 某个驱动报错问题
在内核版看到一个获取物理地址的源码,有兴趣就编译了下,结果却卡在了insmod上。
2.6.19的内核 root权限下:
insmod getpgd.ko
insmod: error inserting 'getpgd.ko' : -1 Operation not permitted.
而我insmod其它驱动的时候却没问题,所以怀疑是不是源码里的一些代码不能执行造成的。
可初学,不知道如何下手,希望大家指点下。
源码如下:
/*******************************************************************************
Copyright(c) 2008-2012
This program is free software; you can redistribute it and/or modify it
under the terms and conditions of the GNU General Public License,
version 2, as published by the Free Software Foundation.
This program is distributed in the hope it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
The full GNU General Public License is included in this distribution in
the file called "COPYING".
Version: 0.1
Date: 2012-04-21 10:25:55 CST
Contact Information:
Tony <tingw.liu@gmail.com>
Home, Qingdao, China.
*******************************************************************************/
#include <linux/module.h>
#include <asm/pgtable.h>
#include <linux/version.h>
#include <asm/page.h>
#include <linux/gfp.h>
#include <linux/page-flags.h>
#include <linux/sched.h>//find_task_by_vpid
#include <linux/mm.h>//find_vma
#include <asm/uaccess.h>
#include <asm/page.h>
#include <asm/e820.h>
#include <asm/pgtable_types.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Tony <tingw.liu@gmail.com>");
MODULE_DESCRIPTION("CONVERT USER VIRTUAL ADDRESS TO PHYADDRESS");
static int pid;
static unsigned long va;
module_param(pid, int, 0644);
module_param(va, ulong, 0644);
static int find_pgd_init(void)
{
unsigned long pa=0;
struct task_struct *pcb_tmp=NULL;
struct page *ppage = NULL;
unsigned long pfn = 0;
pgd_t *pgd_tmp = NULL;
pud_t *pud_tmp = NULL;
pmd_t *pmd_tmp = NULL;
pte_t *pte_tmp = NULL;
printk(KERN_ALERT "PAGE_OFFSET=0x%lx\n", PAGE_OFFSET);
printk(KERN_ALERT "PGDIR_SHIFT=%d\n", PGDIR_SHIFT);
printk(KERN_ALERT "PUD_SHIFT=%d\n", PUD_SHIFT);
printk(KERN_ALERT "PMD_SHIFT=%d\n", PMD_SHIFT);
printk(KERN_ALERT "PAGE_SHIFT=%d\n", PAGE_SHIFT);
printk(KERN_ALERT "PTRS_PRE_PGD=%d\n", PTRS_PER_PGD);
printk(KERN_ALERT "PTRS_PRE_PUD=%d\n", PTRS_PER_PUD);
printk(KERN_ALERT "PTRS_PRE_PMD=%d\n", PTRS_PER_PMD);
printk(KERN_ALERT "PTRS_PRE_PTE=%d\n", PTRS_PER_PTE);
printk(KERN_ALERT "PAGE_MASK=0x%lx\n", PAGE_MASK);
if (!(pcb_tmp = pid_task(find_vpid(pid), PIDTYPE_PID))) {
printk(KERN_ALERT "Can't find the task %d.\n", pid);
return -1;
}
printk(KERN_ALERT "pgd=0x%p\n", pcb_tmp->mm->pgd);
if (!find_vma(pcb_tmp->mm, va)) {
printk(KERN_ALERT "virt_addr 0x%p not available.\n", va);
return -1;
}
pgd_tmp = pgd_offset(pcb_tmp->mm, va);
printk(KERN_ALERT "pgd_tmp=0x%p\n", pgd_tmp);
printk(KERN_ALERT "pgd_val(*pgd_tmp)=0x%p\n", pgd_val(*pgd_tmp));
if (pgd_none(*pgd_tmp) || pgd_bad(*pgd_tmp)) {
printk(KERN_ALERT "Not mapped in pgd.\n");
return -1;
}
pud_tmp = pud_offset(pgd_tmp,va);
if (pud_none(*pud_tmp) || pud_bad(*pud_tmp)) {
printk(KERN_ALERT "Not mapped in pud.\n");
return -1;
}
pmd_tmp = pmd_offset(pud_tmp,va);
if (pmd_none(*pmd_tmp) || pmd_bad(*pmd_tmp)) {
printk(KERN_ALERT "Not mapped in pmd.\n");
return -1;
}
/*FIXME:
Do I need to check Large Page ? PSE bit.
if(pmd_large(*pmd_tmp) == 1){
pa = (pmd_val(*pmd_tmp) & PMD_MASK) | (va & ~PMD_MASK);
}
*/
pte_tmp = pte_offset_kernel(pmd_tmp, va);
if (!pte_tmp || pte_none(*pte_tmp)) {
printk(KERN_ALERT "Not mapped in pte.\n");
return 0;
}
if (!pte_present(*pte_tmp)) {
printk(KERN_ALERT "pte not in RAM,maybe swaped.\n");
return 0;
}
ppage = pte_page(*pte_tmp);
pfn = page_to_pfn(ppage);
pa = page_to_phys(ppage);
pa = pa|(va&~PAGE_MASK);
printk(KERN_ALERT "virt_addr 0x%lx in RAM is 0x%lx.\n",va,pa);
printk(KERN_ALERT "content in 0x%lx is 0x%lx.\n",pa,*(unsigned int*)(__va(pa)));
return 0;
}
static void find_pgd_exit(void)
{
printk(KERN_ALERT "Goodbey.\n");
}
module_init(find_pgd_init);
module_exit(find_pgd_exit);
复制代码
作者:
guocslock
时间:
2013-11-05 18:08
dmesg有什么线索吗?
作者:
idi0t
时间:
2013-11-05 22:29
回复
2#
guocslock
dmesg 就是找不到pid。
重启了就好了,奇怪了, pid和via不知道怎么传不进去.初学很多不懂,以后学习深入了再回过来看看
作者:
历史的哭泣
时间:
2013-11-06 16:02
初学弄这么多代码,换个简单先玩玩,把框架整明白了,在逐渐加入新的东西。我感觉static int xx()这个地方就有问题。
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2