13d8536b4Sopenharmony_ci/*
23d8536b4Sopenharmony_ci * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
33d8536b4Sopenharmony_ci * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
43d8536b4Sopenharmony_ci *
53d8536b4Sopenharmony_ci * Redistribution and use in source and binary forms, with or without modification,
63d8536b4Sopenharmony_ci * are permitted provided that the following conditions are met:
73d8536b4Sopenharmony_ci *
83d8536b4Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright notice, this list of
93d8536b4Sopenharmony_ci *    conditions and the following disclaimer.
103d8536b4Sopenharmony_ci *
113d8536b4Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright notice, this list
123d8536b4Sopenharmony_ci *    of conditions and the following disclaimer in the documentation and/or other materials
133d8536b4Sopenharmony_ci *    provided with the distribution.
143d8536b4Sopenharmony_ci *
153d8536b4Sopenharmony_ci * 3. Neither the name of the copyright holder nor the names of its contributors may be used
163d8536b4Sopenharmony_ci *    to endorse or promote products derived from this software without specific prior written
173d8536b4Sopenharmony_ci *    permission.
183d8536b4Sopenharmony_ci *
193d8536b4Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
203d8536b4Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
213d8536b4Sopenharmony_ci * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
223d8536b4Sopenharmony_ci * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
233d8536b4Sopenharmony_ci * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
243d8536b4Sopenharmony_ci * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
253d8536b4Sopenharmony_ci * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
263d8536b4Sopenharmony_ci * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
273d8536b4Sopenharmony_ci * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
283d8536b4Sopenharmony_ci * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
293d8536b4Sopenharmony_ci * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
303d8536b4Sopenharmony_ci */
313d8536b4Sopenharmony_ci
323d8536b4Sopenharmony_ci#include "los_context.h"
333d8536b4Sopenharmony_ci#include "securec.h"
343d8536b4Sopenharmony_ci#include "los_arch_regs.h"
353d8536b4Sopenharmony_ci#include "los_arch_context.h"
363d8536b4Sopenharmony_ci#include "los_arch_interrupt.h"
373d8536b4Sopenharmony_ci#include "los_task.h"
383d8536b4Sopenharmony_ci#include "los_sched.h"
393d8536b4Sopenharmony_ci#include "los_interrupt.h"
403d8536b4Sopenharmony_ci#include "los_arch_timer.h"
413d8536b4Sopenharmony_ci#include "los_timer.h"
423d8536b4Sopenharmony_ci#include "los_debug.h"
433d8536b4Sopenharmony_ci
443d8536b4Sopenharmony_ciSTATIC UINT32 g_sysNeedSched = FALSE;
453d8536b4Sopenharmony_ci
463d8536b4Sopenharmony_ciUINT32 g_stackDefault[] = {
473d8536b4Sopenharmony_ci    0x00000000,     /* REG_OFF_PC */
483d8536b4Sopenharmony_ci    0x00000000,     /* REG_OFF_PS */
493d8536b4Sopenharmony_ci    0x00000A00,     /* REG_OFF_AR00 */
503d8536b4Sopenharmony_ci    0x00000A01,     /* REG_OFF_AR01 */
513d8536b4Sopenharmony_ci    0x00000A02,     /* REG_OFF_AR02 */
523d8536b4Sopenharmony_ci    0x00000A03,     /* REG_OFF_AR03 */
533d8536b4Sopenharmony_ci    0x00000A04,     /* REG_OFF_AR04 */
543d8536b4Sopenharmony_ci    0x00000A05,     /* REG_OFF_AR05 */
553d8536b4Sopenharmony_ci    0x00000A06,     /* REG_OFF_AR06 */
563d8536b4Sopenharmony_ci    0x00000A07,     /* REG_OFF_AR07 */
573d8536b4Sopenharmony_ci    0x00000A08,     /* REG_OFF_AR08 */
583d8536b4Sopenharmony_ci    0x00000A09,     /* REG_OFF_AR09 */
593d8536b4Sopenharmony_ci    0x00000A10,     /* REG_OFF_AR10 */
603d8536b4Sopenharmony_ci    0x00000A11,     /* REG_OFF_AR11 */
613d8536b4Sopenharmony_ci    0x00000A12,     /* REG_OFF_AR12 */
623d8536b4Sopenharmony_ci    0x00000A13,     /* REG_OFF_AR13 */
633d8536b4Sopenharmony_ci    0x00000A14,     /* REG_OFF_AR14 */
643d8536b4Sopenharmony_ci    0x00000A15,     /* REG_OFF_AR15 */
653d8536b4Sopenharmony_ci    0x00000000,     /* REG_OFF_RESERVED */
663d8536b4Sopenharmony_ci    0x00000000,     /* REG_OFF_EXCCAUSE */
673d8536b4Sopenharmony_ci    0x00000000,     /* REG_OFF_EXCVASSR */
683d8536b4Sopenharmony_ci    0x00000000,     /* REG_OFF_LCOUNT */
693d8536b4Sopenharmony_ci    0x00000000,     /* REG_OFF_LEND */
703d8536b4Sopenharmony_ci    0x00000000,     /* REG_OFF_LBEG */
713d8536b4Sopenharmony_ci#if (defined (__FPU_PRESENT) && (__FPU_PRESENT == 1U))
723d8536b4Sopenharmony_ci    0x00000000,     /* REG_OFF_TMP0 */
733d8536b4Sopenharmony_ci    0x00000001,     /* REG_OFF_CPENABLE | CONTEXT_OFF_CPSTORED */
743d8536b4Sopenharmony_ci    0x00000000,     /* REG_OFF_FCR */
753d8536b4Sopenharmony_ci    0x00000000,     /* REG_OFF_FSR */
763d8536b4Sopenharmony_ci    0x00000000,     /* REG_OFF_F0 */
773d8536b4Sopenharmony_ci    0x00000000,     /* REG_OFF_F1 */
783d8536b4Sopenharmony_ci    0x00000000,     /* REG_OFF_F2 */
793d8536b4Sopenharmony_ci    0x00000000,     /* REG_OFF_F3 */
803d8536b4Sopenharmony_ci    0x00000000,     /* REG_OFF_F4 */
813d8536b4Sopenharmony_ci    0x00000000,     /* REG_OFF_F5 */
823d8536b4Sopenharmony_ci    0x00000000,     /* REG_OFF_F6 */
833d8536b4Sopenharmony_ci    0x00000000,     /* REG_OFF_F7 */
843d8536b4Sopenharmony_ci    0x00000000,     /* REG_OFF_F8 */
853d8536b4Sopenharmony_ci    0x00000000,     /* REG_OFF_F9 */
863d8536b4Sopenharmony_ci    0x00000000,     /* REG_OFF_F10 */
873d8536b4Sopenharmony_ci    0x00000000,     /* REG_OFF_F11 */
883d8536b4Sopenharmony_ci    0x00000000,     /* REG_OFF_F12 */
893d8536b4Sopenharmony_ci    0x00000000,     /* REG_OFF_F13 */
903d8536b4Sopenharmony_ci    0x00000000,     /* REG_OFF_F14 */
913d8536b4Sopenharmony_ci    0x00000000,     /* REG_OFF_F15 */
923d8536b4Sopenharmony_ci#endif
933d8536b4Sopenharmony_ci    0x00000000,     /* REG_OFF_SPILL_RESERVED */
943d8536b4Sopenharmony_ci    0x00000000,     /* REG_OFF_SPILL_RESERVED */
953d8536b4Sopenharmony_ci    0x00000000,     /* REG_OFF_SPILL_RESERVED */
963d8536b4Sopenharmony_ci    0x00000000,     /* REG_OFF_SPILL_RESERVED */
973d8536b4Sopenharmony_ci};
983d8536b4Sopenharmony_ci
993d8536b4Sopenharmony_ciLITE_OS_SEC_TEXT_INIT VOID ArchInit(VOID)
1003d8536b4Sopenharmony_ci{
1013d8536b4Sopenharmony_ci    HalHwiInit();
1023d8536b4Sopenharmony_ci}
1033d8536b4Sopenharmony_ci
1043d8536b4Sopenharmony_ciLITE_OS_SEC_TEXT_MINOR VOID ArchSysExit(VOID)
1053d8536b4Sopenharmony_ci{
1063d8536b4Sopenharmony_ci    (VOID)LOS_IntLock();
1073d8536b4Sopenharmony_ci    while (1) {
1083d8536b4Sopenharmony_ci    }
1093d8536b4Sopenharmony_ci}
1103d8536b4Sopenharmony_ci
1113d8536b4Sopenharmony_ciLITE_OS_SEC_TEXT_INIT VOID *ArchTskStackInit(UINT32 taskID, UINT32 stackSize, VOID *topStack)
1123d8536b4Sopenharmony_ci{
1133d8536b4Sopenharmony_ci    errno_t result;
1143d8536b4Sopenharmony_ci    TaskContext *context = (TaskContext *)((UINTPTR)topStack + stackSize - sizeof(TaskContext));
1153d8536b4Sopenharmony_ci
1163d8536b4Sopenharmony_ci    /* initialize the task context */
1173d8536b4Sopenharmony_ci    result = memcpy_s(context, sizeof(TaskContext), g_stackDefault, sizeof(TaskContext));
1183d8536b4Sopenharmony_ci    if (result != EOK) {
1193d8536b4Sopenharmony_ci        PRINT_ERR("[%s] memcpy_s failed!\n", __func__);
1203d8536b4Sopenharmony_ci    }
1213d8536b4Sopenharmony_ci
1223d8536b4Sopenharmony_ci    context->pc       = (UINT32)(UINTPTR)OsTaskEntry;
1233d8536b4Sopenharmony_ci    context->regA[INDEX_OF_SP]  = (UINTPTR)topStack + stackSize; /* endStack */
1243d8536b4Sopenharmony_ci    context->regA[INDEX_OF_ARGS0]  = taskID; /* argument1 */
1253d8536b4Sopenharmony_ci    context->ps       = SPREG_PS_STACK_CROSS | WOE_ENABLE | 1 << BIT_CALLINC; /* set to kernel stack */
1263d8536b4Sopenharmony_ci
1273d8536b4Sopenharmony_ci    return (VOID *)context;
1283d8536b4Sopenharmony_ci}
1293d8536b4Sopenharmony_ci
1303d8536b4Sopenharmony_ciVOID HalStartToRun(VOID)
1313d8536b4Sopenharmony_ci{
1323d8536b4Sopenharmony_ci    __asm__ volatile ("call0 OsStartToRun");
1333d8536b4Sopenharmony_ci}
1343d8536b4Sopenharmony_ci
1353d8536b4Sopenharmony_ciLITE_OS_SEC_TEXT_INIT UINT32 ArchStartSchedule(VOID)
1363d8536b4Sopenharmony_ci{
1373d8536b4Sopenharmony_ci    (VOID)LOS_IntLock();
1383d8536b4Sopenharmony_ci    OsSchedStart();
1393d8536b4Sopenharmony_ci    HalStartToRun();
1403d8536b4Sopenharmony_ci    return LOS_OK;
1413d8536b4Sopenharmony_ci}
1423d8536b4Sopenharmony_ci
1433d8536b4Sopenharmony_ciVOID ArchTaskSchedule(VOID)
1443d8536b4Sopenharmony_ci{
1453d8536b4Sopenharmony_ci    UINT32 intSave;
1463d8536b4Sopenharmony_ci
1473d8536b4Sopenharmony_ci    if (OS_INT_ACTIVE) {
1483d8536b4Sopenharmony_ci        g_sysNeedSched = TRUE;
1493d8536b4Sopenharmony_ci        return;
1503d8536b4Sopenharmony_ci    }
1513d8536b4Sopenharmony_ci
1523d8536b4Sopenharmony_ci    intSave = LOS_IntLock();
1533d8536b4Sopenharmony_ci    g_sysNeedSched = FALSE;
1543d8536b4Sopenharmony_ci    BOOL isSwitch = OsSchedTaskSwitch();
1553d8536b4Sopenharmony_ci    if (isSwitch) {
1563d8536b4Sopenharmony_ci        HalTaskContextSwitch();
1573d8536b4Sopenharmony_ci    }
1583d8536b4Sopenharmony_ci
1593d8536b4Sopenharmony_ci    LOS_IntRestore(intSave);
1603d8536b4Sopenharmony_ci    return;
1613d8536b4Sopenharmony_ci}
1623d8536b4Sopenharmony_ci
1633d8536b4Sopenharmony_ciVOID HalIrqEndCheckNeedSched(VOID)
1643d8536b4Sopenharmony_ci{
1653d8536b4Sopenharmony_ci    if (g_sysNeedSched && g_taskScheduled && LOS_CHECK_SCHEDULE) {
1663d8536b4Sopenharmony_ci        ArchTaskSchedule();
1673d8536b4Sopenharmony_ci    }
1683d8536b4Sopenharmony_ci}
169