在 Linux 内核版本 6.2.1 中对自定义系统调用进行编程时出错

Error while programming a custom system call in the Linux kernel version 6.2.1

提问人:IndianMax 提问时间:11/13/2023 更新时间:11/13/2023 访问量:52

问:

在编写自定义系统调用时,我遇到了以下问题:

error: 'struct thread_info' has no member named 'pcb'

当执行 make 命令来构建内核时,会出现此问题。在我的代码中,我访问内核thread_info结构,以获取有关它的数据并将其传递给用户级别。

这是我的自定义系统调用:

#include <linux/syscalls.h>
#include <linux/pid.h> 
#include <linux/sched.h>
#include <linux/uaccess.h>
#include "custom_thread_info.h"

SYSCALL_DEFINE2(gimme_thread_info, int, pid, void*, data)
{
    struct task_struct *task;
    struct thread_info *info;

    task = pid_task(find_vpid(pid), PIDTYPE_PID);
    if (task == NULL) {
        printk("ERR: ESRCH");
        return -ESRCH; //  not found
    }

    info = task_thread_info(task);
    if (info == NULL) {
        printk("ERR: EINVAL");
        return -EINVAL; //  invalid arg
    }

    struct custom_thread_info *c_t_i = kmalloc(sizeof(struct custom_thread_info), GFP_KERNEL);
    if (c_t_i == NULL) {
        printk("ERR: ENOMEM");
        return -ENOMEM; // no memory
    }

    c_t_i->palcode_ksp = info->pcb->ksp;
    c_t_i->palcode_usp = info->pcb->usp;
    c_t_i->palcode_ptbr = info->pcb->ptbr;
    c_t_i->palcode_pcc = info->pcb->pcc;
    c_t_i->palcode_asn = info->pcb->asn;
    c_t_i->palcode_unique = info->pcb->unique;
    c_t_i->palcode_flags = info->pcb->flags;
    c_t_i->palcode_res1 = info->pcb->res1;
    c_t_i->palcode_res2 = info->pcb->res2;

    if (info->task != NULL) {
        c_t_i->task_state = info->task->__state;
        c_t_i->task_prio = info->task->prio;
    } else {
        printk("WAR: info->task is null");
        c_t_i->task_state = 0;
        c_t_i->task_prio = -1;
    }
    
    c_t_i->flags = info->flags;
    c_t_i->ieee_state = info->ieee_state;
    c_t_i->cpu = info->cpu;
    c_t_i->preempt_count = info->preempt_count;
    c_t_i->status = info->status;
    c_t_i->bpt_nsaved = info->bpt_nsaved;
    c_t_i->bpt_addr[0] = info->bpt_addr[0];
    c_t_i->bpt_addr[1] = info->bpt_addr[1];
    c_t_i->bpt_insn[0] = info->bpt_insn[0];
    c_t_i->bpt_insn[1] = info->bpt_insn[1];

    
    if (copy_to_user(data, c_t_i, sizeof(struct custom_thread_info)) != 0) {
        kfree(c_t_i);
        printk("ERR: EFAULT");
        return -EFAULT; //  invalid address
    }

    kfree(c_t_i);
    printk("INF: OK");
    return 0;
}

如您所知,当您调用以下命令时,会出现此问题:

info->pcb

我试着和chatGPT聊天,但没有运气。我的猜测是我没有包含一些额外的头文件。尽管如此,Linux 内核的 thread_info 结构确实包含一个 pcb 字段。我的假设还包括对一些受保护的内核结构的无知......

c struct linux 内核 操作系统 系统调用

评论

1赞 Tsyvarev 11/13/2023
您显示的引用指向 alpha 平台上结构定义。但你不太可能为该平台编程。如果您正在为 x86 编程,则其结构不包含 field: elixir.bootlin.com/linux/latest/source/arch/x86/include/asm/...thread_infopcb
0赞 IndianMax 11/13/2023
@Tsyvarev非常感谢!你的回答对我帮助很大......以后我会更加小心!

答: 暂无答案