13d8536b4Sopenharmony_ci/*
23d8536b4Sopenharmony_ci * Copyright (c) 2022-2022 Huawei Device Co., Ltd. All rights reserved.
33d8536b4Sopenharmony_ci *
43d8536b4Sopenharmony_ci * Redistribution and use in source and binary forms, with or without modification,
53d8536b4Sopenharmony_ci * are permitted provided that the following conditions are met:
63d8536b4Sopenharmony_ci *
73d8536b4Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright notice, this list of
83d8536b4Sopenharmony_ci *    conditions and the following disclaimer.
93d8536b4Sopenharmony_ci *
103d8536b4Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright notice, this list
113d8536b4Sopenharmony_ci *    of conditions and the following disclaimer in the documentation and/or other materials
123d8536b4Sopenharmony_ci *    provided with the distribution.
133d8536b4Sopenharmony_ci *
143d8536b4Sopenharmony_ci * 3. Neither the name of the copyright holder nor the names of its contributors may be used
153d8536b4Sopenharmony_ci *    to endorse or promote products derived from this software without specific prior written
163d8536b4Sopenharmony_ci *    permission.
173d8536b4Sopenharmony_ci *
183d8536b4Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
193d8536b4Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
203d8536b4Sopenharmony_ci * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
213d8536b4Sopenharmony_ci * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
223d8536b4Sopenharmony_ci * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
233d8536b4Sopenharmony_ci * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
243d8536b4Sopenharmony_ci * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
253d8536b4Sopenharmony_ci * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
263d8536b4Sopenharmony_ci * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
273d8536b4Sopenharmony_ci * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
283d8536b4Sopenharmony_ci * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
293d8536b4Sopenharmony_ci */
303d8536b4Sopenharmony_ci
313d8536b4Sopenharmony_ci#define GNU_SOURCE
323d8536b4Sopenharmony_ci#include "los_syscall.h"
333d8536b4Sopenharmony_ci#include "los_context.h"
343d8536b4Sopenharmony_ci#include "los_task.h"
353d8536b4Sopenharmony_ci#include "los_debug.h"
363d8536b4Sopenharmony_ci#include "unistd.h"
373d8536b4Sopenharmony_ci#include "errno.h"
383d8536b4Sopenharmony_ci
393d8536b4Sopenharmony_ci#define SYS_CALL_NUM_LIMIT    (__NR_syscallend + 1)
403d8536b4Sopenharmony_ci#define SYS_CALL_NUM_REG_OFFSET 7
413d8536b4Sopenharmony_ci#define NARG_BITS       4
423d8536b4Sopenharmony_ci#define NARG_MASK       0x0F
433d8536b4Sopenharmony_ci#define NARG_PER_BYTE   2
443d8536b4Sopenharmony_ci
453d8536b4Sopenharmony_citypedef UINT32 (*SyscallFun1)(UINT32);
463d8536b4Sopenharmony_citypedef UINT32 (*SyscallFun2)(UINT32, UINT32);
473d8536b4Sopenharmony_citypedef UINT32 (*SyscallFun3)(UINT32, UINT32, UINT32);
483d8536b4Sopenharmony_citypedef UINT32 (*SyscallFun4)(UINT32, UINT32, UINT32, UINT32);
493d8536b4Sopenharmony_citypedef UINT32 (*SyscallFun5)(UINT32, UINT32, UINT32, UINT32, UINT32);
503d8536b4Sopenharmony_citypedef UINT32 (*SyscallFun7)(UINT32, UINT32, UINT32, UINT32, UINT32, UINT32, UINT32);
513d8536b4Sopenharmony_ci
523d8536b4Sopenharmony_cistatic UINTPTR g_syscallHandle[SYS_CALL_NUM_LIMIT] = {0};
533d8536b4Sopenharmony_cistatic UINT8 g_syscallNArgs[(SYS_CALL_NUM_LIMIT + 1) / NARG_PER_BYTE] = {0};
543d8536b4Sopenharmony_ci
553d8536b4Sopenharmony_civoid OsSyscallHandleInit(void)
563d8536b4Sopenharmony_ci{
573d8536b4Sopenharmony_ci#define SYSCALL_HAND_DEF(id, fun, rType, nArg)                                             \
583d8536b4Sopenharmony_ci    if ((id) < SYS_CALL_NUM_LIMIT) {                                                       \
593d8536b4Sopenharmony_ci        g_syscallHandle[(id)] = (UINTPTR)(fun);                                            \
603d8536b4Sopenharmony_ci        g_syscallNArgs[(id) / NARG_PER_BYTE] |= ((id) & 1) ? (nArg) << NARG_BITS : (nArg); \
613d8536b4Sopenharmony_ci    }                                                                                      \
623d8536b4Sopenharmony_ci
633d8536b4Sopenharmony_ci#include "syscall_lookup.h"
643d8536b4Sopenharmony_ci#undef SYSCALL_HAND_DEF
653d8536b4Sopenharmony_ci}
663d8536b4Sopenharmony_ci
673d8536b4Sopenharmony_ci/* The SYSCALL ID is in R7 on entry. Parameters follow in R0..R6 */
683d8536b4Sopenharmony_ciVOID OsSyscallHandle(UINT32 *args)
693d8536b4Sopenharmony_ci{
703d8536b4Sopenharmony_ci    UINT32 ret;
713d8536b4Sopenharmony_ci    UINT8 nArgs;
723d8536b4Sopenharmony_ci    UINTPTR handle;
733d8536b4Sopenharmony_ci    UINT32 svcNum = (UINT32)args[SYS_CALL_NUM_REG_OFFSET];
743d8536b4Sopenharmony_ci
753d8536b4Sopenharmony_ci    if (svcNum >= SYS_CALL_NUM_LIMIT) {
763d8536b4Sopenharmony_ci        PRINT_ERR("Syscall ID: error %d !!!\n", svcNum);
773d8536b4Sopenharmony_ci        return;
783d8536b4Sopenharmony_ci    }
793d8536b4Sopenharmony_ci
803d8536b4Sopenharmony_ci    handle = g_syscallHandle[svcNum];
813d8536b4Sopenharmony_ci    nArgs = g_syscallNArgs[svcNum / NARG_PER_BYTE]; /* 4bit per nargs */
823d8536b4Sopenharmony_ci    nArgs = (svcNum & 1) ? (nArgs >> NARG_BITS) : (nArgs & NARG_MASK);
833d8536b4Sopenharmony_ci    if ((handle == 0) || (nArgs > ARG_NUM_7)) {
843d8536b4Sopenharmony_ci        PRINT_ERR("Unsupported syscall ID: %d nArgs: %d\n", svcNum, nArgs);
853d8536b4Sopenharmony_ci        args[ARG_NUM_0] = -ENOSYS;
863d8536b4Sopenharmony_ci        return;
873d8536b4Sopenharmony_ci    }
883d8536b4Sopenharmony_ci
893d8536b4Sopenharmony_ci    switch (nArgs) {
903d8536b4Sopenharmony_ci        case ARG_NUM_0:
913d8536b4Sopenharmony_ci        case ARG_NUM_1:
923d8536b4Sopenharmony_ci            ret = (*(SyscallFun1)handle)(args[ARG_NUM_0]);
933d8536b4Sopenharmony_ci            break;
943d8536b4Sopenharmony_ci        case ARG_NUM_2:
953d8536b4Sopenharmony_ci            ret = (*(SyscallFun2)handle)(args[ARG_NUM_0], args[ARG_NUM_1]);
963d8536b4Sopenharmony_ci            break;
973d8536b4Sopenharmony_ci        case ARG_NUM_3:
983d8536b4Sopenharmony_ci            ret = (*(SyscallFun3)handle)(args[ARG_NUM_0], args[ARG_NUM_1], args[ARG_NUM_2]);
993d8536b4Sopenharmony_ci            break;
1003d8536b4Sopenharmony_ci        case ARG_NUM_4:
1013d8536b4Sopenharmony_ci            ret = (*(SyscallFun4)handle)(args[ARG_NUM_0], args[ARG_NUM_1], args[ARG_NUM_2], args[ARG_NUM_3]);
1023d8536b4Sopenharmony_ci            break;
1033d8536b4Sopenharmony_ci        case ARG_NUM_5:
1043d8536b4Sopenharmony_ci            ret = (*(SyscallFun5)handle)(args[ARG_NUM_0], args[ARG_NUM_1], args[ARG_NUM_2], args[ARG_NUM_3], \
1053d8536b4Sopenharmony_ci                                         args[ARG_NUM_4]);
1063d8536b4Sopenharmony_ci            break;
1073d8536b4Sopenharmony_ci        default:
1083d8536b4Sopenharmony_ci            ret = (*(SyscallFun7)handle)(args[ARG_NUM_0], args[ARG_NUM_1], args[ARG_NUM_2], args[ARG_NUM_3], \
1093d8536b4Sopenharmony_ci                                         args[ARG_NUM_4], args[ARG_NUM_5], args[ARG_NUM_6]);
1103d8536b4Sopenharmony_ci    }
1113d8536b4Sopenharmony_ci
1123d8536b4Sopenharmony_ci    args[ARG_NUM_0] = ret;
1133d8536b4Sopenharmony_ci
1143d8536b4Sopenharmony_ci    return;
1153d8536b4Sopenharmony_ci}