1 /*
2 * Copyright (C) 2022 Huawei Technologies Co., Ltd.
3 * Licensed under the Mulan PSL v2.
4 * You can use this software according to the terms and conditions of the Mulan PSL v2.
5 * You may obtain a copy of Mulan PSL v2 at:
6 * http://license.coscl.org.cn/MulanPSL2
7 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
8 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
9 * PURPOSE.
10 * See the Mulan PSL v2 for more details.
11 */
12
13 #include <stdio.h>
14 #include <ipclib.h>
15 #include <drv.h>
16 #include <fileio.h>
17 #include <timer.h>
18 #include <inttypes.h>
19 #include <tee_log.h>
20 #include <usrsyscall_irq.h>
21 #include "teesmcmgr.h"
22 #include "tee_crypto_api.h"
23 #include "global_task.h"
24 #include "init.h"
25 #include <sched.h>
26 #include <mem_ops.h>
27
28 #define GT_CHANNEL_NUM 2
29
wait_for_kill(void)30 static void wait_for_kill(void)
31 {
32 while (true)
33 (void)sched_yield();
34 }
35
gtask_init(void)36 static void gtask_init(void)
37 {
38 int32_t ret;
39 struct reg_items_st reg_items = { true, true, true };
40
41 ret = ipc_create_channel("TEEGlobalTask", GT_CHANNEL_NUM, NULL, reg_items);
42 if (ret != 0) {
43 tloge("GTASK: create ipc chnl failed: %d\n", ret);
44 wait_for_kill();
45 }
46
47 ret = init_main();
48 if (ret != 0) {
49 tloge("GTASK: init failed: %d\n", ret);
50 wait_for_kill();
51 }
52 }
53
gtask_init_fileio(void)54 static void gtask_init_fileio(void)
55 {
56 int32_t ret;
57
58 ret = fileio_init();
59 if (ret != 0) {
60 tloge("GTASK: fileio_init failed: %d\n", ret);
61 wait_for_kill();
62 }
63 }
64
gtask_init_timer_irqmgr(void)65 static void gtask_init_timer_irqmgr(void)
66 {
67 init_sysctrl_hdlr();
68 #if (!defined CONFIG_OFF_DRV_TIMER)
69 int32_t ret;
70
71 ret = tee_timer_init();
72 if (ret != 0) {
73 tloge("GTASK: tee_timer_init failed: %d\n", ret);
74 wait_for_kill();
75 }
76 #endif
77 }
78
gtask_set_priority(void)79 static void gtask_set_priority(void)
80 {
81 int32_t ret;
82 ret = set_priority(PRIO_TEE_GT);
83 if (ret < 0) {
84 tee_abort("GTASK: failed to set priority to PRIO_TEE_GT: %x\n", ret);
85 wait_for_kill();
86 }
87 }
88
gtask_run_and_destory(void)89 static void gtask_run_and_destory(void)
90 {
91 /* smcmgr threads must be created after setting stack guard */
92 gtask_set_priority();
93
94 gtask_main();
95
96 tloge("Gtask error. teesmcmgr error is expected\n");
97 init_shell();
98 }
99
main(void)100 int main(void)
101 {
102 tlogi("GTASK: Starting up...\n");
103
104 /*
105 * gtask will init something :
106 * hm_mmgr_client, cs_client, ipc_channels, main, ccmgr
107 */
108 mem_ops_init();
109 gtask_init();
110
111 gtask_init_timer_irqmgr();
112
113 gtask_init_fileio();
114
115 /*
116 * gtask will set affinity to use all cpus
117 * gtask will set high priority
118 * gtask will extend utable to use more utilities
119 * use gtask_main to run
120 * finally gtask will destory itself
121 */
122 gtask_run_and_destory();
123
124 return 0;
125 }
126