2

我试图找到将数据从内核移动到用户空间的最快方法。现在我正在尝试 GKH 的 debugfs,但我正在努力让 blob 包装器工作。

这是我到目前为止得到的:

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/debugfs.h>

MODULE_AUTHOR("CREED0R");
MODULE_LICENSE("GPL");


struct dentry *dfs;
struct debugfs_blob_wrapper *myblob;

int my_init(void)
{
    int stats[10];
    int i;

    for (i = 0; i < 10; i++)
        stats[i] = i;

    myblob->data = (void *) stats;
    myblob->size = (unsigned long) 10;

    dfs = debugfs_create_blob("test", 0644, NULL, myblob);

    if (dfs == NULL) {
        printk("Could not create debugfs blob\n");
        return 1;
    }

    printk("DebugFS file created\n");

    return 0;
}


void my_exit(void)
{
    printk("DebugFS file deleted\n\n");
    debugfs_remove(dfs);
}


module_init(my_init);
module_exit(my_exit);

它可以构建,但是如果我运行 insmod,我的 qemu 实例会死得很惨。

不知道为什么会这样。我错过了什么?

4

1 回答 1

0

谢谢你的帮助。我只是忘记为 blob 结构获取内存,因此设置数据和大小指针不可避免地会杀死它。

这是正确的版本,将 32K' 的 u32 从内核泵入用户空间。它使用 2.6.32.38 内核构建:

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/debugfs.h>

#define SIZE 8192

MODULE_AUTHOR("CREED0R");
MODULE_LICENSE("GPL");

struct dentry *dfs;
struct debugfs_blob_wrapper *myblob;

u32 *stats; /* our blob of data */

int my_init(void)
{
    int i;
    int stats_size;   /* size of our data */
    int struct_size;  /* size of the blob struct */

    struct_size = sizeof(struct debugfs_blob_wrapper);
    stats_size  = SIZE * sizeof(u32);


    /* get mem for data */
    stats = kmalloc(stats_size, GFP_KERNEL);

    if (stats == NULL) {
        printk("Could not allocate mem for data\n");
        return -ENOMEM;
    }


    /* fill datablob with dummy data */
    for (i = 0; i < SIZE; i++)
        stats[i] = i;


    /* get mem for blob struct and init */
    myblob = (struct debugfs_blob_wrapper *) kmalloc(struct_size, GFP_KERNEL);

    if (myblob == NULL) {
        printk("Could not allocate mem for blob\n");
        kfree(stats);
        return -ENOMEM;
    }


    /* only set data pointer and data size */
    myblob->data = (void *) stats;
    myblob->size = (unsigned long) stats_size;


    /* create pseudo file under /sys/kernel/debug/ with name 'test' */
    dfs = debugfs_create_blob("test", 0644, NULL, myblob);

    if (dfs == NULL) {
        printk("Could not create debugfs blob\n");
        kfree(stats);
        kfree(myblob);
        return -EINVAL;
    }

    printk("DebugFS file created\n");

    return 0;
}


void my_exit(void)
{
    printk("DebugFS file deleted\n\n");

    kfree(myblob);
    kfree(stats);

    debugfs_remove(dfs);
}


module_init(my_init);
module_exit(my_exit);
于 2013-02-03T09:14:25.423 回答