1# System Call<a name="EN-US_TOPIC_0000001123520159"></a> 2 3- [Basic Concepts](#section889710401734) 4- [Working Principles](#section195177541314) 5- [Development Guidelines](#section193492047135419) 6 - [How to Develop](#section7165741122210) 7 - [Development Example](#section107131418224) 8 9 10## Basic Concepts<a name="section889710401734"></a> 11 12The OpenHarmony LiteOS-A isolates the user space and kernel space. User-space programs cannot directly access kernel resources. System calls provide a channel for user-space programs to access kernel resources and interact with the kernel. 13 14## Working Principles<a name="section195177541314"></a> 15 16As shown in the following figure, a user program calls the System API \(a POSIX interface provided by the system\) to access kernel resources and interacts with the kernel. An SVC/SWI exception is triggered inside the POSIX interface to complete switching of the system from the user space to the kernel space. Then, the kernel Syscall Handler \(unified system call interface\) parses parameters received and distributes the parameters to the specific kernel functions for processing. 17 18**Figure 1** System call<a name="fig165662915310"></a> 19 20 21The Syscall Handler is implemented by the **OsArmA32SyscallHandle** function in **kernel/liteos\_a/syscall/los\_syscall.c**. This function is called when a system software interrupt occurs. The input parameters of system calls are parsed according to the list in **kernel/liteos\_a/syscall/syscall\_lookup.h** so that the specific kernel functions are executed. 22 23> **NOTE:** 24>- System calls implement basic interaction between user-space programs and the kernel. You are advised to use the POSIX APIs provided by the kernel instead of system call APIs. If you want to add system call APIs, see the development guide. 25>- For details about the system call APIs provided by the kernel for the user space, see **kernel/liteos\_a/syscall/syscall\_lookup.h**. For details about the system call functions provided by the kernel, see **kernel/liteos\_a/syscall/los\_syscall.h**. 26 27## Development Guidelines<a name="section193492047135419"></a> 28 29### How to Develop<a name="section7165741122210"></a> 30 31The typical development process of adding a system call API is as follows: 32 331. Determine and add the new system call number to the LibC library. 342. Add the declaration and implementation of the new user-space function API to the LibC library. 353. Add the new system call number and the declaration of the corresponding kernel processing function to the kernel system call header file. 364. Add the kernel processing function corresponding to the system call to the kernel. 37 38### Development Example<a name="section107131418224"></a> 39 40**Sample Code** 41 421. Add the system call number to **syscall.h.in** in the LibC library. 43 44 In the following example, **\_\_NR\_new\_syscall\_sample** specifies the system call number added. 45 46 ``` 47 ... 48 /* Current system call list */ 49 /* OHOS customized syscalls, not compatible with ARM EABI */ 50 #define __NR_OHOS_BEGIN 500 51 #define __NR_pthread_set_detach (__NR_OHOS_BEGIN + 0) 52 #define __NR_pthread_join (__NR_OHOS_BEGIN + 1) 53 #define __NR_pthread_deatch (__NR_OHOS_BEGIN + 2) 54 #define __NR_creat_user_thread (__NR_OHOS_BEGIN + 3) 55 #define __NR_processcreat (__NR_OHOS_BEGIN + 4) 56 #define __NR_processtart (__NR_OHOS_BEGIN + 5) 57 #define __NR_printf (__NR_OHOS_BEGIN + 6) 58 #define __NR_dumpmemory (__NR_OHOS_BEGIN + 13) 59 #define __NR_mkfifo (__NR_OHOS_BEGIN + 14) 60 #define __NR_mqclose (__NR_OHOS_BEGIN + 15) 61 #define __NR_realpath (__NR_OHOS_BEGIN + 16) 62 #define __NR_format (__NR_OHOS_BEGIN + 17) 63 #define __NR_shellexec (__NR_OHOS_BEGIN + 18) 64 #define __NR_ohoscapget (__NR_OHOS_BEGIN + 19) 65 #define __NR_ohoscapset (__NR_OHOS_BEGIN + 20) 66 67 #define __NR_new_syscall_sample (__NR_OHOS_BEGIN + 21) /* Add the new system call number: __NR_new_syscall_sample:521 */ 68 69 #define __NR_syscallend (__NR_OHOS_BEGIN + 22) 70 ... 71 ``` 72 732. Add the declaration and implementation of the new user-space API to the LibC library. 74 75 ``` 76 #include "stdio_impl.h" 77 #include "syscall.h" 78 ... 79 /* Add the implementation of the new user-space system call API.*/ 80 void newSyscallSample(int num) 81 { 82 printf("user mode: num = %d\n", num); 83 __syscall(SYS_new_syscall_sample, num); 84 return; 85 } 86 ``` 87 883. Add the system call number to the kernel system call header file. 89 90 In the **third\_party/musl/porting/liteos\_a/kernel/include/bits/syscall.h** file, **\_\_NR\_new\_syscall\_sample** specifies the new system call number . 91 92 ``` 93 ... 94 /* Current system call list */ 95 /* OHOS customized syscalls, not compatible with ARM EABI */ 96 #define __NR_OHOS_BEGIN 500 97 #define __NR_pthread_set_detach (__NR_OHOS_BEGIN + 0) 98 #define __NR_pthread_join (__NR_OHOS_BEGIN + 1) 99 #define __NR_pthread_deatch (__NR_OHOS_BEGIN + 2) 100 #define __NR_creat_user_thread (__NR_OHOS_BEGIN + 3) 101 #define __NR_processcreat (__NR_OHOS_BEGIN + 4) 102 #define __NR_processtart (__NR_OHOS_BEGIN + 5) 103 #define __NR_printf (__NR_OHOS_BEGIN + 6) 104 #define __NR_dumpmemory (__NR_OHOS_BEGIN + 13) 105 #define __NR_mkfifo (__NR_OHOS_BEGIN + 14) 106 #define __NR_mqclose (__NR_OHOS_BEGIN + 15) 107 #define __NR_realpath (__NR_OHOS_BEGIN + 16) 108 #define __NR_format (__NR_OHOS_BEGIN + 17) 109 #define __NR_shellexec (__NR_OHOS_BEGIN + 18) 110 #define __NR_ohoscapget (__NR_OHOS_BEGIN + 19) 111 #define __NR_ohoscapset (__NR_OHOS_BEGIN + 20) 112 113 #define __NR_new_syscall_sample (__NR_OHOS_BEGIN + 21) /* Add the new system call number: __NR_new_syscall_sample:521 */ 114 115 #define __NR_syscallend (__NR_OHOS_BEGIN + 22) 116 ... 117 ``` 118 119 In **kernel/liteos\_a/syscall/syscall\_lookup.h**, add the line **SYSCALL\_HAND\_DEF\(\_\_NR\_new\_syscall\_sample, SysNewSyscallSample, void, ARG\_NUM\_1\)**. 120 121 ``` 122 ... 123 /* Current system call list */ 124 SYSCALL_HAND_DEF(__NR_chown, SysChown, int, ARG_NUM_3) 125 SYSCALL_HAND_DEF(__NR_chown32, SysChown, int, ARG_NUM_3) 126 #ifdef LOSCFG_SECURITY_CAPABILITY 127 SYSCALL_HAND_DEF(__NR_ohoscapget, SysCapGet, UINT32, ARG_NUM_2) 128 SYSCALL_HAND_DEF(__NR_ohoscapset, SysCapSet, UINT32, ARG_NUM_1) 129 #endif 130 /* Add a system call. */ 131 SYSCALL_HAND_DEF(__NR_new_syscall_sample, SysNewSyscallSample, void, ARG_NUM_1) 132 ... 133 ``` 134 1354. Add a function corresponding to the new system call to the kernel. 136 137 In **kernel/liteos\_a/syscall/los\_syscall.h**, **SysNewSyscallSample** is the declaration of the kernel processing function corresponding to the new system call. 138 139 ``` 140 ... 141 /* List of the declaration of the current kernel processing functions for system calls. */ 142 extern int SysClockSettime64(clockid_t clockID, const struct timespec64 *tp); 143 extern int SysClockGettime64(clockid_t clockID, struct timespec64 *tp); 144 extern int SysClockGetres64(clockid_t clockID, struct timespec64 *tp); 145 extern int SysClockNanoSleep64(clockid_t clk, int flags, const struct timespec64 *req, struct timespec64 *rem); 146 extern int SysTimerGettime64(timer_t timerID, struct itimerspec64 *value); 147 extern int SysTimerSettime64(timer_t timerID, int flags, const struct itimerspec64 *value, struct itimerspec64 *oldValue); 148 /* Declaration of the kernel processing function for the new system call*/ 149 extern void SysNewSyscallSample(int num); 150 ... 151 ``` 152 153 The newly added kernel processing function is implemented as follows: 154 155 ``` 156 include "los_printf.h" 157 ... 158 /* Add the implementation of the kernel processing function for the new system call. */ 159 void SysNewSyscallSample(int num) 160 { 161 PRINTK("kernel mode: num = %d\n", num); 162 return; 163 } 164 ``` 165 166 167**Verification** 168 169The user-space program calls the **newSyscallSample\(10\)** API. The output is as follows: 170 171``` 172/* The output in both user-space and kernel-space APIs indicates that the new system call is enabled. */ 173user mode: num = 10 174kernel mode: num = 10 175``` 176 177