User Tools

Site Tools


modifying_the_linux_kernel

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
modifying_the_linux_kernel [2020/09/07 14:49]
misterjei
modifying_the_linux_kernel [2021/09/17 11:36] (current)
misterjei [Adding a System Call]
Line 4: Line 4:
 ==== Adding a System Call ==== ==== Adding a System Call ====
 There are three major steps to adding a system call to the Linux kernel: There are three major steps to adding a system call to the Linux kernel:
-  -Adding a new entry to the system call table for the appropriate architecture +  -Entry to the system call table for the appropriate architecture 
-  -Marking the function so that its parameters do not reside in registers (only in the CPU stack) +  -Prototype of system call & marking so parameters do not reside in registers (only the CPU stack) 
-  -Adding the system call functions themselves+  -Definition of system call (implementation)
  
-=== 1. Adding an Entry to the System Call Table ===+=== 1. Entry in System Call Table ===
 Every system call must have an associated system call number which corresponds to its position in the system call table. This table is transformed / imported into the source as part of the build process. The correct table varies by architecture, but is in the following format/location in the source tree: Every system call must have an associated system call number which corresponds to its position in the system call table. This table is transformed / imported into the source as part of the build process. The correct table varies by architecture, but is in the following format/location in the source tree:
  
-''/arch/**//<archname>//**/entry/syscalls/syscall_**//<variant>//**.tbl''+''**//<kernelsourcedir>//**/arch/**//<archname>//**/entry/syscalls/syscall_**//<variant>//**.tbl''
  
 For example, in the x64 version of Linux, **//<archname>//** is ''__**x86**__'' and **//<variant>//** is ''__**64**__''. For example, in the x64 version of Linux, **//<archname>//** is ''__**x86**__'' and **//<variant>//** is ''__**64**__''.
Line 26: Line 26:
 <code>435 common sample_syscall __x64_sys_sample_syscall</code> <code>435 common sample_syscall __x64_sys_sample_syscall</code>
  
-=== 2. Marking System Call Function as Stack-Only === +=== 2. Prototype of the System Call === 
-The kernel has no "safety" checks like user-space programs; registers can easily (and often are) corrupted. Instead, we should tell the compiler to limit the function to the CPU stack only and avoid dependence on registers. This can do done using the asmlinkage flag in GCC (the compiler we use). This is done as part of the system call's prototype (which is used in C to declare a function which is defined elsewhere). If you explore the kernel,  you'll find some examples like this:+The kernel has no "safety" checks like user-space programs; registers can easily (and often are) corrupted. Instead, we should tell the compiler to limit the function to the CPU stack only and avoid dependence on registers. This can be done using the asmlinkage flag in GCC (the compiler we use) as part of the system call's prototype (which is used in C to declare a function which is defined elsewhere). The declaration must be in a place that makes it accessible to the definition. If you explore the kernel, you'll find some examples like this:
  
 <code>asmlinkage int sys_sample_syscall(int sample_param);</code> <code>asmlinkage int sys_sample_syscall(int sample_param);</code>
  
-=== 3. Adding System Call Functions === +=== 3. Definition of the System Call === 
-Finally, we need to add the system call functionsLike the **asmlinkage** flag, this can be +Finally, we need to add the system call definitionThise will need to be compiled into the kernel as well. The kernel has lots of examples along these lines, which is for a system call with one parameter (''int sample_param''): 
 + 
 +<code>SYSCALL_DEFINE1(sample_syscall, int, sample_param) 
 +
 +    return sample_param + 1; // Adds 1 to the parameter and returns it 
 +}</code> 
 + 
 +The ''SYSCALL_DEFINE**<#>**'' pattern supports between 0 (**''SYSCALL_DEFINE0''**) and 6 (**''SYSCALL_DEFINE6''**) parameters. 
modifying_the_linux_kernel.1599504593.txt.gz · Last modified: 2020/09/07 14:49 by misterjei