linux - 插入我的内核模块时,INSMOD 退出并出现错误 "bad address",警告 "Kernel mismatch"

标签 linux kernel driver mismatch insmod

我正在为自定义设备开发这个模块,实际上,这是一个连接到 ISA 总线的 4*8 位 i-o 端口,地址为 0x0120 - 0x0123。该驱动程序基于 Alessandro Rubini 和 Jonathan Corbet 的“scull”。我的操作系统是 Ubuntu 10.04,内核是 2.6.32-74 generic,我使用内置的面向控制台的编译器 gcc。 使用“insmod”插入已编译的模块时,出现错误“地址错误”并且未加载模块。我尝试使用“printk”对其进行调试,发现我的模块成功获取了一系列 i-o 端口、主要和次要端口号,然后,当尝试执行“Reset_Port”功能时,它会生成错误“地址错误”并退出.

谁能告诉我,我做错了什么?

这是我的模块的 __exit 和 __init 函数

void __exit ET3201_exit(void)
{
        int i;
        dev_t devno = MKDEV(ET3201_major, ET3201_minor);

        /* Get rid of our char dev entries */
        if (ET3201_devices) {
                for (i = 0; i < ET3201_nr_devs; i++) {
                        ET3201_trim(ET3201_devices + i);
                        cdev_del(&ET3201_devices[i].cdev);
                }
                kfree(ET3201_devices);
        }

#ifdef ET3201_DEBUG /* use proc only if debugging */
        ET3201_remove_proc();
#endif

        /* cleanup_module is never called if registering failed */
        unregister_chrdev_region(devno, ET3201_nr_devs);
    if ( ! port ) release_region(BaseIO, 8);
    printk(KERN_INFO "Goodbye, cruel world - ET3201 is unloaded\n");

        /* and call the cleanup functions for friend devices */
        /*ET3201_access_cleanup();*/
}
/*----------------------------------------------------------------------------*/
/* Set up the char_dev structure for this device. */
static void ET3201_setup_cdev(struct ET3201_dev *dev, int index)
{
        int err, devno = MKDEV(ET3201_major, ET3201_minor + index);

        cdev_init(&dev->cdev, &ET3201_fops);
        dev->cdev.owner = THIS_MODULE;
        dev->cdev.ops = &ET3201_fops;
    dev->CAMAC_Module_Number = CAMAC_Nmod;
    dev->CAMAC_Command_Adress = CAMAC_Adcom;
    dev->Driver_Number = ET3201_minor + index;
        err = cdev_add (&dev->cdev, devno, 1);
        /* Fail gracefully if need be */
        if (err)
                printk(KERN_NOTICE "Error %d adding ET3201%d", err, index);
}
/*----------------------------------------------------------------------------*/
int __init ET3201_init(void)
{
    int result = 0;
    int i;
    dev_t dev = 0;
    BaseIO = Base;  
    /* Get a range of minor numbers to work with, asking for a dynamic
    major unless directed otherwise at load time. */
    if (ET3201_major) {
           dev = MKDEV(ET3201_major, ET3201_minor);
       result = register_chrdev_region(dev, ET3201_nr_devs, "ET3201");
    } else {
           result = alloc_chrdev_region(&dev, ET3201_minor, ET3201_nr_devs, "ET3201");
       ET3201_major = MAJOR(dev);
    }
    if (result < 0) {
       printk(KERN_WARNING "ET3201: can't get major %d\n", ET3201_major);
       return result;
    }     
    port = request_region(BaseIO, 8, "ET3201");
    if ( port == NULL ) {
                        printk(KERN_WARNING "ET3201 cannot reserve i-o ports %lu \n", BaseIO);
                        return -ENODEV; 
            goto fail;
                }
         /*
         * allocate the devices -- we can't have them static, as the number
         * can be specified at load time
         */
        ET3201_devices = kmalloc(ET3201_nr_devs * sizeof(struct ET3201_dev), GFP_KERNEL);
        if (! ET3201_devices) {
                result = -ENOMEM;
        printk(KERN_ALERT "ET3201: can't get memory \n");
                goto fail; /* Fail gracefully if need be */
        }
        memset(ET3201_devices, 0, ET3201_nr_devs * sizeof(struct ET3201_dev));

        /* Initialize each device. */
        for (i = 0; i < ET3201_nr_devs; i++) {
                ET3201_devices[i].quantum = ET3201_quantum;
                ET3201_devices[i].qset = ET3201_qset;
                init_MUTEX(&ET3201_devices[i].sem);
                ET3201_setup_cdev(&ET3201_devices[i], i);
        }

        /* At this point call the init function for any friend device */
        dev = MKDEV(ET3201_major, ET3201_minor + ET3201_nr_devs);
        /*dev += ET3201_access_init(dev);*/

    printk(KERN_INFO "ET3201 is initialized with major %d\n", ET3201_major);
    if ( port != NULL ){
        printk(KERN_INFO "ET3201 is trying to reset %d devices\n", ET3201_nr_devs);
        result = Reset_Port();
    }
        if ( result != 0 ) {
        printk(KERN_ALERT "ET3201: device cannot reset with result %d\n", result);
                result = -EFAULT;
                goto fail;  
        }
#ifdef ET3201_DEBUG /* only when debugging */
        ET3201_create_proc();
#endif

        return 0; /* succeed */

  fail:
        ET3201_exit();
    return result;
}
/*----------------------------------------------------------------------------*/
module_init(ET3201_init);
module_exit(ET3201_exit);
MODULE_LICENSE("GPL");
MODULE_ALIAS_MISCDEV(ET3201_minor);

接下来是 Reset_Port()

static int Reset_Port(void)
{
int result = -EIO;
int count;
if (port == NULL) goto fail;
for ( count = 0; count < ET3201_nr_devs; count++ )
{
outb(0x00, ports[count]);
}
wmb();          /*write memory barrier*/
LastOp = E_Reset;
result = 0;     /* success  */
fail:   
return result;
}
EXPORT_SYMBOL(Reset_Port);

现在,在修复“int Reset_Port(void)”之后,我遇到了另一个问题 -
'警告:modpost:发现 1 个部分不匹配。' 调试后我发现这是调用“ET3201_exit()”的结果 来自'module_init()' - 当我评论这个电话时,警告消失了。 令人惊讶的是,在受人尊敬的作者的“scull”驱动程序中进行了完全相同的调用 - 而且它有效。 问题:什么会导致此代码中的内核不匹配?

最佳答案

是的!该错误已修复 - 在声明“int Reset_Port(void)”后,模块已成功插入和删除。我认为(但这是错误的)所有可以从“module_init()”中调用的函数都不能声明为静态的。

关于linux - 插入我的内核模块时,INSMOD 退出并出现错误 "bad address",警告 "Kernel mismatch",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33335259/

相关文章:

linux - 如何使用ssh heredoc在bash中删除多个文件?

c - ISRS 没有接到电话

android - 官方 FTDI android 驱动程序 read() 不工作

c - 使用我的字符驱动程序时出现段错误

c - linux信号掩码函数中sig_atomic_t的用法

Linux SCHED_OTHER (CFS) 用户时间与 SCHED_RR 和 SCHED_FIFO 用户时间

c++ - 数字输入的输入验证

linux - 如何在内核中获取变量的地址?

linux - 检索父 pid

Java Selenium Firefox 驱动程序 - 文本框 Onchange 问题