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