1# 系统调用 2 3## 基本概念 4 5OpenHarmony LiteOS-A实现了用户态与内核态的区分隔离,用户态程序不能直接访问内核资源,而系统调用则为用户态程序提供了一种访问内核资源、与内核进行交互的通道。 6 7## 运行机制 8 9 如图1所示,用户程序通过调用System API(系统API,通常是系统提供的POSIX接口)进行内核资源访问与交互请求,POSIX接口内部会触发SVC/SWI异常,完成系统从用户态到内核态的切换,然后对接到内核的Syscall Handler(系统调用统一处理接口)进行参数解析,最终分发至具体的内核处理函数。 10 11 **图1** 系统调用示意图 12 13  14Syscall Handler的具体实现在kernel/liteos_a/syscall/los_syscall.c中OsArmA32SyscallHandle函数,在进入系统软中断异常时会调用此函数,并且按照kernel/liteos_a/syscall/syscall_lookup.h中的清单进行系统调用的入参解析,执行各系统调用最终对应的内核处理函数。 15 16>  **说明:** 17> - 系统调用提供基础的用户态程序与内核的交互功能,不建议开发者直接使用系统调用接口,推荐使用内核提供的对外POSIX接口,若需要新增系统调用接口,详见开发指导。 18> 19> - 内核向用户态提供的系统调用接口清单[详见](https://gitee.com/openharmony/kernel_liteos_a/blob/master/syscall/syscall_lookup.h),内核相应的系统调用对接函数清单[详见](https://gitee.com/openharmony/kernel_liteos_a/blob/master/syscall/los_syscall.h)。 20 21## 开发指导 22 23### 开发流程 24 25新增系统调用的典型开发流程如下: 26 271. 在LibC库中确定并添加新增的系统调用号。 28 292. 在LibC库中新增用户态的函数接口声明及实现。 30 313. 在内核系统调用头文件中确定并添加新增的系统调用号及对应内核处理函数的声明。 32 334. 在内核中新增该系统调用对应的内核处理函数。 34 35### 编程实例 36 37**示例代码**: 38 391. 在LibC库syscall.h.in中新增系统调用号 40 如下所示,其中__NR_new_syscall_sample为新增系统调用号: 41 42 ``` 43 ... 44 /* 当前现有的系统调用清单 */ 45 /* OHOS customized syscalls, not compatible with ARM EABI */ 46 #define __NR_OHOS_BEGIN 500 47 #define __NR_pthread_set_detach (__NR_OHOS_BEGIN + 0) 48 #define __NR_pthread_join (__NR_OHOS_BEGIN + 1) 49 #define __NR_pthread_deatch (__NR_OHOS_BEGIN + 2) 50 #define __NR_create_user_thread (__NR_OHOS_BEGIN + 3) 51 #define __NR_processcreate (__NR_OHOS_BEGIN + 4) 52 #define __NR_processtart (__NR_OHOS_BEGIN + 5) 53 #define __NR_printf (__NR_OHOS_BEGIN + 6) 54 #define __NR_dumpmemory (__NR_OHOS_BEGIN + 13) 55 #define __NR_mkfifo (__NR_OHOS_BEGIN + 14) 56 #define __NR_mqclose (__NR_OHOS_BEGIN + 15) 57 #define __NR_realpath (__NR_OHOS_BEGIN + 16) 58 #define __NR_format (__NR_OHOS_BEGIN + 17) 59 #define __NR_shellexec (__NR_OHOS_BEGIN + 18) 60 #define __NR_ohoscapget (__NR_OHOS_BEGIN + 19) 61 #define __NR_ohoscapset (__NR_OHOS_BEGIN + 20) 62 63 #define __NR_new_syscall_sample (__NR_OHOS_BEGIN + 21) /* 新增的系统调用号 __NR_new_syscall_sample:521 */ 64 65 #define __NR_syscallend (__NR_OHOS_BEGIN + 22) 66 ... 67 ``` 68 692. 在LibC库中新增用户态接口的声明与实现。 70 71 ``` 72 #include "stdio_impl.h" 73 #include "syscall.h" 74 ... 75 /* 新增系统调用用户态的接口实现 */ 76 void newSyscallSample(int num) 77 { 78 printf("user mode: num = %d\n", num); 79 __syscall(SYS_new_syscall_sample, num); 80 return; 81 } 82 ``` 83 843. 在内核系统调用头文件中新增系统调用号 85 如下所示,在third_party/musl/porting/liteos_a/kernel/include/bits/syscall.h文件中,__NR_new_syscall_sample为新增系统调用号。 86 87 ``` 88 ... 89 /* 当前现有的系统调用清单 */ 90 /* OHOS customized syscalls, not compatible with ARM EABI */ 91 #define __NR_OHOS_BEGIN 500 92 #define __NR_pthread_set_detach (__NR_OHOS_BEGIN + 0) 93 #define __NR_pthread_join (__NR_OHOS_BEGIN + 1) 94 #define __NR_pthread_deatch (__NR_OHOS_BEGIN + 2) 95 #define __NR_create_user_thread (__NR_OHOS_BEGIN + 3) 96 #define __NR_processcreate (__NR_OHOS_BEGIN + 4) 97 #define __NR_processtart (__NR_OHOS_BEGIN + 5) 98 #define __NR_printf (__NR_OHOS_BEGIN + 6) 99 #define __NR_dumpmemory (__NR_OHOS_BEGIN + 13) 100 #define __NR_mkfifo (__NR_OHOS_BEGIN + 14) 101 #define __NR_mqclose (__NR_OHOS_BEGIN + 15) 102 #define __NR_realpath (__NR_OHOS_BEGIN + 16) 103 #define __NR_format (__NR_OHOS_BEGIN + 17) 104 #define __NR_shellexec (__NR_OHOS_BEGIN + 18) 105 #define __NR_ohoscapget (__NR_OHOS_BEGIN + 19) 106 #define __NR_ohoscapset (__NR_OHOS_BEGIN + 20) 107 108 #define __NR_new_syscall_sample (__NR_OHOS_BEGIN + 21) /* 新增的系统调用号 __NR_new_syscall_sample:521 */ 109 110 #define __NR_syscallend (__NR_OHOS_BEGIN + 22) 111 ... 112 ``` 113 114 在kernel/liteos_a/syscall/syscall_lookup.h中,增加一行SYSCALL_HAND_DEF(__NR_new_syscall_sample, SysNewSyscallSample, void, ARG_NUM_1): 115 116 ``` 117 ... 118 /* 当前现有的系统调用清单 */ 119 SYSCALL_HAND_DEF(__NR_chown, SysChown, int, ARG_NUM_3) 120 SYSCALL_HAND_DEF(__NR_chown32, SysChown, int, ARG_NUM_3) 121 #ifdef LOSCFG_SECURITY_CAPABILITY 122 SYSCALL_HAND_DEF(__NR_ohoscapget, SysCapGet, UINT32, ARG_NUM_2) 123 SYSCALL_HAND_DEF(__NR_ohoscapset, SysCapSet, UINT32, ARG_NUM_1) 124 #endif 125 /* 新增系统调用 */ 126 SYSCALL_HAND_DEF(__NR_new_syscall_sample, SysNewSyscallSample, void, ARG_NUM_1) 127 ... 128 ``` 129 1304. 在内核中新增内核该系统调用对应的处理函数 131 如下所示,在kernel/liteos_a/syscall/los_syscall.h中,SysNewSyscallSample为新增系统调用的内核处理函数声明: 132 133 ``` 134 ... 135 /* 当前现有的系统调用内核处理函数声明清单 */ 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 /* 新增的系统调用内核处理函数声明 */ 143 extern void SysNewSyscallSample(int num); 144 ... 145 ``` 146 147 新增的系统调用的内核处理函数实现如下: 148 149 ``` 150 include "los_printf.h" 151 ... 152 /* 新增系统调用内核处理函数的实现 */ 153 void SysNewSyscallSample(int num) 154 { 155 PRINTK("kernel mode: num = %d\n", num); 156 return; 157 } 158 ``` 159 160**结果验证:** 161 162用户态程序调用newSyscallSample(10)接口,得到输出结果如下: 163 164``` 165/* 用户态接口与内核态接口均有输出,证明系统调用已使能 */ 166user mode: num = 10 167kernel mode: num = 10 168``` 169