我有我聲明/ 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