Linux kernel 写崩溃

Linux kernel 写崩溃,linux-kernel,kernel,ioctl,Linux Kernel,Kernel,Ioctl,我试图在内核空间中实现ioctl,以便在寄存器中写入一些日期,我在ioctl的cmd中遇到崩溃 下面是我的代码: 内核端: static struct file_operations fops = { .compat_ioctl = device_ioctl }; int device_ioctl(struct inode *inode, struct file *filep, unsigned int cmd, unsigned long arg) {

我试图在内核空间中实现ioctl,以便在寄存器中写入一些日期,我在ioctl的cmd中遇到崩溃

下面是我的代码:

内核端:

static struct file_operations fops = {
.compat_ioctl = device_ioctl
};

int device_ioctl(struct inode *inode, struct file *filep, 
                 unsigned int cmd, unsigned long arg)
{

    int len = 200;

    printk (KERN_INFO "In Device_ioctl !!\n");
    switch(cmd)
    {
    case IOCTL_WRITE_REG:
        write_ioctl((unsigned long *)arg);
        break;

    default:
        printk (KERN_INFO "default\n");
        return -ENOTTY;
    }
    printk (KERN_INFO "device_ioctl out\n");
    return len;
}
用户端

#define IOCTL_WRITE_REG _IOW(MAJOR_NUM, 1, int *)
void write_to_device(int write_fd)
{

    int retval;
    unsigned int to_write1 = 1;

    retval = ioctl(write_fd, IOCTL_WRITE_REG, &to_write1);
    if(retval < 0)
    {
        printf("fd: %d, write error: %d\n", write_fd, errno);
        exit(-1);
    }
}
#定义IOCTL_WRITE_REG_IOW(MAJOR_NUM,1,int*)
无效写入设备(int-write\u-fd)
{
内部检索;
无符号int到_write1=1;
retval=ioctl(write\u fd,ioctl\u write\u REG,&to\u write1);
如果(返回值<0)
{
printf(“fd:%d,写入错误:%d\n”,write\u fd,errno);
出口(-1);
}
}
它没有进入设备的ioctl功能,
我哪里出错了?

我注意到的几件事:

  • 您需要使用
    unlocked\u ioctl
    而不是
    compat\u ioctl
    compat_ioctl
    允许32位用户空间程序调用64位内核上的
    ioctl
    调用
  • ioctl处理程序函数的签名不正确(对于
    unlocked\u ioctl
    )。预期签名为:

    long (*unlocked_ioctl) (struct file * filep, unsigned int, unsigned long);
    
我还没有真正尝试编译这段代码,但我认为这应该可以:

static struct file_operations fops = {
    .unlocked_ioctl = device_ioctl
};

long device_ioctl(struct file *filep, 
                  unsigned int cmd,
                  unsigned long arg)
{

    int len = 200;

    printk (KERN_INFO "In Device_ioctl !!\n");
    switch(cmd)
    {
    case IOCTL_WRITE_REG:
        write_ioctl((unsigned long *)arg);
        break;

    default:
        printk (KERN_INFO "default\n");
        return -ENOTTY;
    }
    printk (KERN_INFO "device_ioctl out\n");
    return len;
}

我碰巧注意到的几件事:

  • 您需要使用
    unlocked\u ioctl
    而不是
    compat\u ioctl
    compat_ioctl
    允许32位用户空间程序调用64位内核上的
    ioctl
    调用
  • ioctl处理程序函数的签名不正确(对于
    unlocked\u ioctl
    )。预期签名为:

    long (*unlocked_ioctl) (struct file * filep, unsigned int, unsigned long);
    
我还没有真正尝试编译这段代码,但我认为这应该可以:

static struct file_operations fops = {
    .unlocked_ioctl = device_ioctl
};

long device_ioctl(struct file *filep, 
                  unsigned int cmd,
                  unsigned long arg)
{

    int len = 200;

    printk (KERN_INFO "In Device_ioctl !!\n");
    switch(cmd)
    {
    case IOCTL_WRITE_REG:
        write_ioctl((unsigned long *)arg);
        break;

    default:
        printk (KERN_INFO "default\n");
        return -ENOTTY;
    }
    printk (KERN_INFO "device_ioctl out\n");
    return len;
}

你需要使用
unlocked\u ioctl
而不是
compat\u ioctl
。你需要使用
unlocked\u ioctl
而不是
compat\u ioctl
。你能给我一个compat\u ioctl和closed\u ioctl的简单定义吗,这个32-64位转换中的appart?你能给我一个compat_ioctl和closed_ioctl的简单定义吗,这个32-64位转换中的appart?