首页 > 解决方案 > 将开放系统调用映射到父函数?

问题描述

我有一个问题,我们需要在gemOS中实现开放系统调用的内部工作。问题如下在main函数中

int create_fd = open(filename, O_CREAT|O_RDWR, O_READ|O_WRITE);

在 Visual Studio Code 中,当我使用 ctrl + enter 检查打开的定义时,它引导我进入以下功能。让我们假设 Open 系统调用存在 O_CREAT 标志,然后相应地调用_syscall3

int open(char * filename, int flags, ...){
va_list ap;
long mode;
va_start(ap, flags);
mode = va_arg(ap, long);
va_end(ap);

if((flags & O_CREAT) == O_CREAT){
    return _syscall3(SYSCALL_OPEN, (u64)filename, flags, mode);
}
else{
    return _syscall2(SYSCALL_OPEN, (u64)filename, flags);
 }
}

当我检查_syscall3的定义时,它给了我以下代码。我无法理解 asm volatile 命令中写的内容。所以任何人都可以向我解释发生了什么。

static long _syscall3(int syscall_num, u64 arg1, u64 arg2, u64 arg3){
asm volatile (
    "int $0x80;"
    "leaveq;"
    "retq;"
    :::"memory"
  );

  return 0;   /*gcc shutup!*/
}

另外,当我尝试在 asm volatile 行之前打印一些东西时,该函数有点停止执行。顺便说一下,我们需要为此调用实现的功能如下。

  extern int do_regular_file_open(struct exec_context *ctx, char* filename, u64 flags, u64 mode){

/**  
*  TODO Implementation of file open, 
*  You should be creating file(use the alloc_file function to creat file), 
*  To create or Get inode use File system function calls, 
*  Handle mode and flags 
*  Validate file existence, Max File count is 16, Max Size is 4KB, etc
*  Incase of Error return valid Error code 
* */

return 100;
}

我试图将编写在主文件中的Open函数映射到这个do_regular_file_open,但由于 asm volatile 我无法理解发生了什么。

标签: clinuxlinux-kernel

解决方案


推荐阅读