0

I have a question where we need to implement internal working of open system call in gemOS.The question is as follows in the main function following

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

In Visual Studio Code when I checked definition of open using ctrl + enter, it lead me to following function. Lets suppose that Open system call has O_CREAT flag present, then accordingly _syscall3 will be called.

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);
 }
}

When I checked definition of _syscall3 it gave me following code.I was not able to understand what is written inside asm volatile command. So can anyone explain me what is happening.

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

  return 0;   /*gcc shutup!*/
}

Also when I tried printing something before asm volatile line, the function kinda stop executing. By the way the function that we need to implement for this call is following.

  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;
}

I was trying to map Open function written in main file to this do_regular_file_open, but due to asm volatile I wasn't able to understand whats happening.

  • "I was not able to understand what is written inside asm volatile command" - All three commands are easily googled. 1. `int 80` performs a *syscall interrupt*, which is processed by the kernel, 2. `leaveq` "finalizes" execution of `_syscall3` function. 3. `retq` performs actual returning from the function. – Tsyvarev Oct 24 '20 at 14:32
  • @Tsyvarev Could you explain how it is calling ** do_regular_file_open ** using asm command? – ancad ancad Oct 24 '20 at 14:59
  • As I already said in my previous comment, `int 80` is an **interrupt** and it triggers transition into the **kernel** (priveledged) code. `do_regular_file_open` is a **kernel** function which is responsible for processing system call with `SYSCALL_OPEN` identifier. If you don't know what are "kernel space", "user space" and what "syscall interrupt" means, then you need to read some resources on this topic before writing OS code. – Tsyvarev Oct 24 '20 at 15:35
  • @Tsyvarev thanks for help. Actually this was my assignment, and my instructor never explained these things, he just boasts of giving tough assignments each year. – ancad ancad Oct 24 '20 at 15:45
  • The `_syscall3` is really poorly written. I guess it's assuming that the arguments to the function will be left in the registers where they arrived, and that the generated code can handle an early return, but none of this is guaranteed by the compiler. It will break utterly if inlined, for example. It needs to be using extended asm constraints properly, as explained in the i386 section of [this answer](https://stackoverflow.com/a/9508738/634919) – Nate Eldredge Oct 24 '20 at 16:28

0 Answers0