• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2022 Huawei Device Co., Ltd. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification,
5  * are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice, this list of
8  *    conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11  *    of conditions and the following disclaimer in the documentation and/or other materials
12  *    provided with the distribution.
13  *
14  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
15  *    to endorse or promote products derived from this software without specific prior written
16  *    permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #define GNU_SOURCE
32 #include "los_syscall.h"
33 #include "los_context.h"
34 #include "los_task.h"
35 #include "los_debug.h"
36 #include "unistd.h"
37 #include "errno.h"
38 
39 #define SYS_CALL_NUM_LIMIT    (__NR_syscallend + 1)
40 #define SYS_CALL_NUM_REG_OFFSET 7
41 #define NARG_BITS       4
42 #define NARG_MASK       0x0F
43 #define NARG_PER_BYTE   2
44 
45 typedef UINT32 (*SyscallFun1)(UINT32);
46 typedef UINT32 (*SyscallFun2)(UINT32, UINT32);
47 typedef UINT32 (*SyscallFun3)(UINT32, UINT32, UINT32);
48 typedef UINT32 (*SyscallFun4)(UINT32, UINT32, UINT32, UINT32);
49 typedef UINT32 (*SyscallFun5)(UINT32, UINT32, UINT32, UINT32, UINT32);
50 typedef UINT32 (*SyscallFun7)(UINT32, UINT32, UINT32, UINT32, UINT32, UINT32, UINT32);
51 
52 static UINTPTR g_syscallHandle[SYS_CALL_NUM_LIMIT] = {0};
53 static UINT8 g_syscallNArgs[(SYS_CALL_NUM_LIMIT + 1) / NARG_PER_BYTE] = {0};
54 
OsSyscallHandleInit(void)55 void OsSyscallHandleInit(void)
56 {
57 #define SYSCALL_HAND_DEF(id, fun, rType, nArg)                                             \
58     if ((id) < SYS_CALL_NUM_LIMIT) {                                                       \
59         g_syscallHandle[(id)] = (UINTPTR)(fun);                                            \
60         g_syscallNArgs[(id) / NARG_PER_BYTE] |= ((id) & 1) ? (nArg) << NARG_BITS : (nArg); \
61     }                                                                                      \
62 
63 #include "syscall_lookup.h"
64 #undef SYSCALL_HAND_DEF
65 }
66 
67 /* The SYSCALL ID is in R7 on entry. Parameters follow in R0..R6 */
OsSyscallHandle(UINT32 * args)68 VOID OsSyscallHandle(UINT32 *args)
69 {
70     UINT32 ret;
71     UINT8 nArgs;
72     UINTPTR handle;
73     UINT32 svcNum = (UINT32)args[SYS_CALL_NUM_REG_OFFSET];
74 
75     if (svcNum >= SYS_CALL_NUM_LIMIT) {
76         PRINT_ERR("Syscall ID: error %d !!!\n", svcNum);
77         return;
78     }
79 
80     handle = g_syscallHandle[svcNum];
81     nArgs = g_syscallNArgs[svcNum / NARG_PER_BYTE]; /* 4bit per nargs */
82     nArgs = (svcNum & 1) ? (nArgs >> NARG_BITS) : (nArgs & NARG_MASK);
83     if ((handle == 0) || (nArgs > ARG_NUM_7)) {
84         PRINT_ERR("Unsupported syscall ID: %d nArgs: %d\n", svcNum, nArgs);
85         args[ARG_NUM_0] = -ENOSYS;
86         return;
87     }
88 
89     switch (nArgs) {
90         case ARG_NUM_0:
91         case ARG_NUM_1:
92             ret = (*(SyscallFun1)handle)(args[ARG_NUM_0]);
93             break;
94         case ARG_NUM_2:
95             ret = (*(SyscallFun2)handle)(args[ARG_NUM_0], args[ARG_NUM_1]);
96             break;
97         case ARG_NUM_3:
98             ret = (*(SyscallFun3)handle)(args[ARG_NUM_0], args[ARG_NUM_1], args[ARG_NUM_2]);
99             break;
100         case ARG_NUM_4:
101             ret = (*(SyscallFun4)handle)(args[ARG_NUM_0], args[ARG_NUM_1], args[ARG_NUM_2], args[ARG_NUM_3]);
102             break;
103         case ARG_NUM_5:
104             ret = (*(SyscallFun5)handle)(args[ARG_NUM_0], args[ARG_NUM_1], args[ARG_NUM_2], args[ARG_NUM_3], \
105                                          args[ARG_NUM_4]);
106             break;
107         default:
108             ret = (*(SyscallFun7)handle)(args[ARG_NUM_0], args[ARG_NUM_1], args[ARG_NUM_2], args[ARG_NUM_3], \
109                                          args[ARG_NUM_4], args[ARG_NUM_5], args[ARG_NUM_6]);
110     }
111 
112     args[ARG_NUM_0] = ret;
113 
114     return;
115 }