我有我声明/ Linux内核中的(3.2)定义的结构,并且我目前试图分配一个系统调用内部这些结构中的一个,并返回一个指向它的过程调用系统调用。系统调用分配内存用户
我怎样才能
#include
这个文件中的程序在内核之外(这个问题可能是我应该包括文件)?目前,我宣布结构include/linux/syscalls.h
和我创造了自己在kernel/mysystemcall.c
文件中定义它。如果我尝试在程序中使用该结构,我会得到error: dereferencing pointer to incomplete type
。我怎样才能真正从这个存储器中读取,因为如果我取消对它的引用,我得到一个分段错误?目前,我正在使用
kmalloc
来分配内存;是否有我需要打开以访问内存的标志,还是应该使用其他内容来分配此内存?
感谢您提供的任何帮助!
当前的系统调用的实现:
#include <linux/linkage.h>
#include <linux/sched.h>
#include <linux/slab.h>
struct threadinfo_struct {
int pid;
int nthreads;
int *tid;
};
asmlinkage struct threadinfo_struct *sys_threadinfo(void) {
struct threadinfo_struct *info = kmalloc(sizeof(struct threadinfo_struct), GFP_KERNEL);
info->pid = current->pid;
info->nthreads = -1;
info->tid = NULL;
return info;
}
当前测试代码(局外人内核):
#include <stdio.h>
#include <linux/unistd.h>
#include <sys/syscall.h>
#define sys_threadinfo 349
int main(void) {
int *ti = (int*) syscall(sys_threadinfo);
printf("Thread id: %d\n", *ti); // Causes a segfault
return 0;
}
编辑:我知道我可以有我的系统调用一个指针指向已分配内存,只是为用户填写值,但是这样做是优选的(教师偏好)。
看看系统的实施要求,其目的是为用户空间程序分配内存:'brk'和'mmap'。 ('mmap'也可以做其他事情,但是应该清楚哪个代码路径是哪个代码路径)。两者都在'mm/mmap.c'中定义。 – zwol
@Zack这是'SYSCALL_DEFINE1(BRK,无符号长,BRK)'和'SYSCALL_DEFINE1(old_mmap,结构mmap_arg_struct __user *,ARG)'定义,你是指什么?我可以看到mmap的使用'copy_from_user'(没有多少是清楚这里 - 新的Linux内核),是有办法,我可以给内存给用户,而无需使用像'copy_to_user'? – Darthfett
这些不是系统调用的*实现*;这些实际上只是接口声明(尽管名称中有DEFINE)。正如我所说,*实现*位于'mm/mmap.c'中。寻找名为'sys_brk','sys_mmap','do_brk'和'do_mmap'的函数。 – zwol