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