I"±	<p>I hate xv6, a stupid, useless education-oriented system. In this article, I will generally talk about how to implement system call to this operating system.</p>

<h2 id="xv6-systemcall">Xv6 Systemcall</h2>
<p>To invoke a system call, we have to first define a user mode function to be the interface of the kernel instruction  in file <em>user.h</em>.</p>

<div class="language-c highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kt">void</span> <span class="nf">function</span> <span class="p">(</span><span class="kt">void</span><span class="p">);</span>
</code></pre></div></div>

<p>This interface-like function will then pass the function name, in this case function, to <em>usys.S</em>. When using user mode function in programs, <em>usys.S</em> will generate a reference to SYS_function and push system call number of this function into %eax. After that, system can know from <em>syscall.c</em> and determining whether this system call is available. We must define same name system function and add it into <em>syscall.h</em> and <em>syscall.c</em>.</p>

<div class="language-c highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="cp">#define SYS_function ##		// ## is the system call number
</span><span class="p">[</span><span class="n">SYS_function</span><span class="p">]</span>  <span class="n">sys_function</span>	<span class="c1">// real system function name</span>
<span class="k">extern</span> <span class="kt">int</span> <span class="nf">sys_function</span><span class="p">(</span><span class="kt">void</span><span class="p">);</span>	<span class="c1">// real system function declaration</span>
</code></pre></div></div>

<p>After adding these sentences to syscall files, we can implement real function in specific place where you want to make the function works well.</p>

<p>Sometimes, we need to pass variables among system calls. In this case, variablesâ€™ values are not necessary and even canâ€™t be pass directly into system_function. When invoke a system call function, all variables of this system call will be pushed into current processâ€™ stack. In file <em>syscall.c</em>, multiple functions are provided to get these variables from the process. I wonâ€™t waste time on explaining how to use these functions especially when elegant and detailed comments were written in source codes. However, I will explain concepts and how process organized and works in xv6 in future articles.</p>
:ET