xref: /kernel/liteos_a/syscall/los_syscall.c (revision 0d163575)
1/*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 *    conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 *    of conditions and the following disclaimer in the documentation and/or other materials
13 *    provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 *    to endorse or promote products derived from this software without specific prior written
17 *    permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#define _GNU_SOURCE
33#ifdef LOSCFG_FS_VFS
34#include "fs/file.h"
35#endif
36#include "los_init.h"
37#include "los_signal.h"
38#include "los_syscall.h"
39#include "los_task_pri.h"
40#include "los_process_pri.h"
41#include "los_hw_pri.h"
42#include "los_printf.h"
43#include "time.h"
44#include "utime.h"
45#include "poll.h"
46#include "mqueue.h"
47#include "los_futex_pri.h"
48#include "sys/times.h"
49#include "dirent.h"
50#include "fcntl.h"
51#include "unistd.h"
52
53#include "sys/mount.h"
54#include "sys/resource.h"
55#include "sys/mman.h"
56#include "sys/uio.h"
57#include "sys/prctl.h"
58#include "sys/socket.h"
59#include "sys/utsname.h"
60#ifdef LOSCFG_SHELL
61#include "shmsg.h"
62#endif
63#ifdef LOSCFG_SECURITY_CAPABILITY
64#include "capability_api.h"
65#endif
66#include "sys/shm.h"
67
68
69#define SYS_CALL_NUM    (__NR_syscallend + 1)
70#define NARG_BITS       4
71#define NARG_MASK       0x0F
72#define NARG_PER_BYTE   2
73
74typedef UINT32 (*SyscallFun1)(UINT32);
75typedef UINT32 (*SyscallFun3)(UINT32, UINT32, UINT32);
76typedef UINT32 (*SyscallFun5)(UINT32, UINT32, UINT32, UINT32, UINT32);
77typedef UINT32 (*SyscallFun7)(UINT32, UINT32, UINT32, UINT32, UINT32, UINT32, UINT32);
78
79static UINTPTR g_syscallHandle[SYS_CALL_NUM] = {0};
80static UINT8 g_syscallNArgs[(SYS_CALL_NUM + 1) / NARG_PER_BYTE] = {0};
81
82void OsSyscallHandleInit(void)
83{
84#define SYSCALL_HAND_DEF(id, fun, rType, nArg)                                             \
85    if ((id) < SYS_CALL_NUM) {                                                             \
86        g_syscallHandle[(id)] = (UINTPTR)(fun);                                            \
87        g_syscallNArgs[(id) / NARG_PER_BYTE] |= ((id) & 1) ? (nArg) << NARG_BITS : (nArg); \
88    }                                                                                      \
89
90    #include "syscall_lookup.h"
91#undef SYSCALL_HAND_DEF
92}
93
94LOS_MODULE_INIT(OsSyscallHandleInit, LOS_INIT_LEVEL_KMOD_EXTENDED);
95
96/* The SYSCALL ID is in R7 on entry.  Parameters follow in R0..R6 */
97VOID OsArmA32SyscallHandle(TaskContext *regs)
98{
99    UINT32 ret;
100    UINT8 nArgs;
101    UINTPTR handle;
102    UINT32 cmd = regs->reserved2;
103
104    if (cmd >= SYS_CALL_NUM) {
105        PRINT_ERR("Syscall ID: error %d !!!\n", cmd);
106        return;
107    }
108
109    handle = g_syscallHandle[cmd];
110    nArgs = g_syscallNArgs[cmd / NARG_PER_BYTE]; /* 4bit per nargs */
111    nArgs = (cmd & 1) ? (nArgs >> NARG_BITS) : (nArgs & NARG_MASK);
112    if ((handle == 0) || (nArgs > ARG_NUM_7)) {
113        PRINT_ERR("Unsupported syscall ID: %d nArgs: %d\n", cmd, nArgs);
114        regs->R0 = -ENOSYS;
115        return;
116    }
117
118    OsSigIntLock();
119    switch (nArgs) {
120        case ARG_NUM_0:
121        case ARG_NUM_1:
122            ret = (*(SyscallFun1)handle)(regs->R0);
123            break;
124        case ARG_NUM_2:
125        case ARG_NUM_3:
126            ret = (*(SyscallFun3)handle)(regs->R0, regs->R1, regs->R2);
127            break;
128        case ARG_NUM_4:
129        case ARG_NUM_5:
130            ret = (*(SyscallFun5)handle)(regs->R0, regs->R1, regs->R2, regs->R3, regs->R4);
131            break;
132        default:
133            ret = (*(SyscallFun7)handle)(regs->R0, regs->R1, regs->R2, regs->R3, regs->R4, regs->R5, regs->R6);
134    }
135
136    regs->R0 = ret;
137    OsSigIntUnlock();
138
139    return;
140}
141