• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <stdlib.h>
13 #include <stdio.h>
14 #include <pthread.h>
15 #include <securec.h>
16 #include <string.h>
17 #include <malloc.h>
18 #include <usrsyscall_smc.h>
19 #include <usrsyscall_irq.h>
20 #include "teesmcmgr.h"
21 #include <ipclib.h>
22 
23 static cref_t g_gtask_channel_hdlr;
24 static bool   g_is_gtask_alive;
25 
set_is_gtask_alive(bool value)26 void set_is_gtask_alive(bool value)
27 {
28     g_is_gtask_alive = value;
29 }
30 
get_is_gtask_alive(void)31 bool get_is_gtask_alive(void)
32 {
33     return g_is_gtask_alive;
34 }
35 
set_gtask_channel_hdlr(cref_t value)36 void set_gtask_channel_hdlr(cref_t value)
37 {
38     g_gtask_channel_hdlr = value;
39 }
40 
get_gtask_channel_hdlr(void)41 cref_t get_gtask_channel_hdlr(void)
42 {
43     return g_gtask_channel_hdlr;
44 }
45 
acquire_hdlr(void)46 static void acquire_hdlr(void)
47 {
48     init_teesmc_hdlr();
49     init_sysctrl_hdlr();
50     set_gtask_channel_hdlr(acquire_gtask_channel());
51     if (!check_ref_valid(g_gtask_channel_hdlr))
52         panic("acquire gtask channel failed\n");
53     set_is_gtask_alive(true);
54 }
55 
create_smc_thread(pthread_t * smc_thread)56 static void create_smc_thread(pthread_t *smc_thread)
57 {
58     int32_t ret;
59 
60     pthread_attr_t attr;
61     ret = pthread_attr_init(&attr);
62     if (ret != 0) {
63         error("fail to init smc thread\n");
64         exit(1);
65     }
66     void *stackaddr = malloc(SMCMGR_STACK_SIZE);
67     if (stackaddr == NULL) {
68         error("malloc stack space failed\n");
69         exit(1);
70     }
71     ret = pthread_attr_setstack(&attr, stackaddr, SMCMGR_STACK_SIZE);
72     if (ret != 0) {
73         error("smc thread set stack failed\n");
74         exit(1);
75     }
76 
77     if (pthread_create(smc_thread, &attr, tee_smc_thread, NULL) != 0) {
78         error("fail to create smc thread\n");
79         exit(1);
80     }
81 }
create_idle_thread(pthread_t * idle_thread)82 static void create_idle_thread(pthread_t *idle_thread)
83 {
84     int32_t ret;
85 
86     pthread_attr_t attr;
87     ret = pthread_attr_init(&attr);
88     if (ret != 0) {
89         error("fail to init idle thread\n");
90         exit(1);
91     }
92     void *stackaddr = malloc(SMCMGR_STACK_SIZE);
93     if (stackaddr == NULL) {
94         error("malloc stack space failed\n");
95         exit(1);
96     }
97     ret = pthread_attr_setstack(&attr, stackaddr, SMCMGR_STACK_SIZE);
98     if (ret != 0) {
99         error("idle thread set stack failed\n");
100         exit(1);
101     }
102 
103     if (pthread_create(idle_thread, &attr, tee_idle_thread, NULL) != 0) {
104         error("fail to create idle thread\n");
105         exit(1);
106     }
107 }
108 
main(void)109 int main(void)
110 {
111     int32_t rc;
112     pthread_t smc_thread = 0;
113     pthread_t idle_thread = 0;
114 
115     info(" --\n");
116     info("| TEE SMC Manager\n");
117     info(" --\n");
118 
119     acquire_hdlr();
120     create_smc_thread(&smc_thread);
121     create_idle_thread(&idle_thread);
122 
123     rc = pthread_join(idle_thread, NULL);
124     if (rc != 0) {
125         error("idle thread join failed\n");
126         exit(1);
127     }
128 
129     rc = pthread_join(smc_thread, NULL);
130     if (rc != 0) {
131         error("smc thread join failed\n");
132         exit(1);
133     }
134     panic("teesmcmgr exited unexpectedly\n");
135 
136     return 0;
137 }
138