gpt4 book ai didi

c - 简单字符驱动崩溃

转载 作者:太空狗 更新时间:2023-10-29 11:33:56 28 4
gpt4 key购买 nike

我正在制作一个简单的简单字符驱动程序,它应该写入我的字符设备“/dev/coffee_bean”,当从中读取时,它应该显示字符串“Hi There!”在控制台中。我通过“cat/dev/coffee_bean”从设备读取,而不是我的系统崩溃并重置。下面是我的源代码。感谢您的帮助。

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/kdev_t.h>
#include <linux/types.h>
#include <linux/completion.h>
#include <linux/slab.h>
#include <asm/uaccess.h>
#include <linux/semaphore.h>
MODULE_LICENSE("Dual BSD/GPL");

#define DEVICE_NAME "coffee_grinds"
#define COUNT 4
#define FIRST_MINOR 0
#define CONST_QUANTUM 4000
#define CONST_QSET 4000

int test;

module_param(test, int, S_IRUGO);

struct my_char_structure{
struct cdev my_cdev;
struct semaphore sem;
unsigned int access_key;
unsigned long size;
};

static dev_t dev_num;

int dev_open(struct inode *in_node, struct file *filp){
struct my_char_structure *my_dev;

my_dev = container_of(in_node->i_cdev, struct my_char_structure, my_cdev);
filp->private_data = my_dev;
return 0;
}

int dev_release(struct inode *inode, struct file *filp){
return 0;
}

ssize_t dev_write(struct file *filp, const char __user *buff, size_t count, loff_t *offp){
struct my_char_structure *my_dev = filp->private_data;
ssize_t retval = -ENOMEM; /* value used in "goto out" statements */
char *my_string;
int counting;
printk(KERN_ALERT "Write was accessed, Lol");
if (down_interruptible(&my_dev->sem))
return -ERESTARTSYS;
my_string = kmalloc(count,GFP_KERNEL);
counting = copy_from_user(my_string,buff,count);
printk(KERN_ALERT "You wrote %s",my_string);
kfree(my_string);
up(&my_dev->sem);

printk(KERN_ALERT "We wrote %d bytes",counting);
return retval;
// Here is some experimental code
}

ssize_t dev_read(struct file *filp, char __user *buff, size_t count, loff_t *offp){
struct my_char_structure *my_dev = filp->private_data;
ssize_t retval = 0;
char *my_string;

printk(KERN_ALERT "Read was accessed Lol");

if (down_interruptible(&my_dev->sem))
return -ERESTARTSYS;
my_string = "Hi there!";
copy_to_user(buff,my_string,10);
up(&my_dev->sem);
return retval;

}

struct file_operations fops = {
.owner = THIS_MODULE,
.read = dev_read,
.write = dev_write,
.open = dev_open,
.release= dev_release,
};

int start_mod(void){
//Because we are dealing with a fictitious device, I want
//the driver to create my two devices with arbitrarly
//assigned major numbers.
static struct my_char_structure Dev;
static struct my_char_structure *my_dev = &Dev;
int err;

alloc_chrdev_region(&dev_num, FIRST_MINOR, COUNT, DEVICE_NAME);

sema_init(&(my_dev->sem),1);

cdev_init(&(my_dev->my_cdev), &fops);
my_dev->my_cdev.owner = THIS_MODULE;
my_dev->my_cdev.ops = &fops;// fops is my file operations struct

err = cdev_add(&my_dev->my_cdev, dev_num, COUNT);
if(err)
printk(KERN_ALERT "There was an error %d.",err);
printk(KERN_ALERT " insmod to major number %d",MAJOR(dev_num));

return 0;
}

void end_mod(void){

unregister_chrdev_region(dev_num, COUNT);

}

module_init(start_mod);
module_exit(end_mod);

最佳答案

在到达 dev_read 之前,事情可能会出错。您是否在控制台上看到了您的 KERN_ALERT 消息?

显然,这并不是您的源代码的全部内容,因为模块已初始化,字符设备已注册,并且还有其他功能,例如 open 例程。是什么让您认为错误存在于 dev_read 只是因为从设备读取会导致机器崩溃?

sizeof(my_string)sizeof(char *),它是 4 或 8。您正在获取指针的大小。如果您使用的是 64 位内核,那么当您将此调试得足够好以达到那种程度时,您最多只会得到没有 !Hi there。 :)

即很明显,您可能会受益于 C 基础知识的教程,例如数组和指针之间的区别。

关于c - 简单字符驱动崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9676556/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com