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#include "los_task.h"
32#include "securec.h"
33#include "los_config.h"
34#include "los_debug.h"
35#include "los_hook.h"
36#include "los_interrupt.h"
37#include "los_memory.h"
38#include "los_mpu.h"
39#include "los_sched.h"
40#include "los_mux.h"
41#include "los_sem.h"
42#include "los_timer.h"
43#include "los_arch_context.h"
44#include "los_box.h"
45#include "los_syscall.h"
46
47int SysUserTaskCreate(unsigned long entry, unsigned long userArea, unsigned long userSp, BOOL joinable)
48{
49    UINT32 ret, taskID;
50    TSK_INIT_PARAM_S taskInitParam = { 0 };
51    taskInitParam.pfnTaskEntry = (TSK_ENTRY_FUNC)entry;
52    taskInitParam.uwStackSize = 0x1000;
53    taskInitParam.pcName = "UserTask";
54    taskInitParam.usTaskPrio = OS_TASK_PRIORITY_LOWEST;
55    taskInitParam.stackAddr = userSp;
56    if (joinable) {
57        taskInitParam.uwResved |= LOS_TASK_ATTR_JOINABLE;
58    }
59    ret = LOS_TaskCreateOnly(&taskID, &taskInitParam);
60    if (ret != LOS_OK) {
61        return -EINVAL;
62    }
63
64    OsUserTaskInit(taskID, entry, userArea, userSp);
65    return taskID;
66}
67
68int SysSchedSetScheduler(unsigned int tid, int policy, int priority)
69{
70    if ((tid == 0) || (tid > LOSCFG_BASE_CORE_TSK_LIMIT)) {
71        return EINVAL;
72    }
73
74    if (policy != 0) {
75        return EINVAL;
76    }
77
78    if ((priority <= 0) || (priority > OS_TASK_PRIORITY_LOWEST)) {
79        return EINVAL;
80    }
81
82    UINT32 ret = LOS_TaskPriSet((UINT32)tid, (UINT16)priority);
83    if (ret != LOS_OK) {
84        return EINVAL;
85    }
86
87    return 0;
88}
89
90int *SysSchedGetArea(unsigned int tid)
91{
92    unsigned int intSave;
93    int *area = NULL;
94
95    if ((tid == 0) || (tid > LOSCFG_BASE_CORE_TSK_LIMIT)) {
96        return NULL;
97    }
98
99    intSave = LOS_IntLock();
100    area = (int *)OsGetUserTaskCB(tid)->userArea;
101    LOS_IntRestore(intSave);
102    return area;
103}
104
105int SysSetThreadArea(const char *area)
106{
107    unsigned int intSave;
108
109    intSave = LOS_IntLock();
110    LosTaskCB *runTask = g_losTask.runTask;
111    OsGetUserTaskCB(runTask->taskID)->userArea = (unsigned long)(uintptr_t)area;
112    LOS_IntRestore(intSave);
113    return 0;
114}
115
116char *SysGetThreadArea(void)
117{
118    unsigned int intSave;
119    char *area = NULL;
120
121    intSave = LOS_IntLock();
122    LosTaskCB *runTask = g_losTask.runTask;
123    area = (char *)OsGetUserTaskCB(runTask->taskID)->userArea;
124    LOS_IntRestore(intSave);
125    return area;
126}
127