• 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 
13 #include <stdio.h>
14 #include <securec.h>
15 #include "teesmcmgr.h"
16 #include <usrsyscall_smc.h>
17 #include <ipclib.h>
18 #include <sched.h>
19 
20 #define GTASK_MSG_ID 0xDEADBEEF
21 #define RECV_BUF_SIZE 32
22 
23 struct gtask_msg {
24     uint32_t msg_id;
25     char payload[PAY_LOAD_SIZE];
26 } __attribute__((packed));
27 
28 const char *g_debug_prefix = "teesmcmgr";
29 static bool g_tz_started;
30 static bool g_send_to_gtask = false;
31 
send_to_gtask(void)32 static void send_to_gtask(void)
33 {
34     if (g_send_to_gtask)
35         return;
36 
37     int32_t err;
38     struct gtask_msg gtask_msg;
39     static const char magic_msg[] = MAGIC_MSG;
40     char recv_buf[RECV_BUF_SIZE] = {0};
41     gtask_msg.msg_id = GTASK_MSG_ID;
42     errno_t ret_s = memcpy_s(gtask_msg.payload, sizeof(gtask_msg.payload), magic_msg, sizeof(magic_msg));
43     if (ret_s != EOK)
44         panic("memory copy failed\n");
45 
46     /*
47      * Why we need this ipc_msg_call?
48      * As smcmgr send notification to gtask after check smc.ops is valid,
49      * but the first normal request 'set smc buffer' is send by raw_smc_send
50      * that smc.ops is set to be smc buffer's physical addr.
51      */
52     err = ipc_msg_call(get_gtask_channel_hdlr(), &gtask_msg, sizeof(struct gtask_msg), recv_buf, sizeof(recv_buf), -1);
53     if (err < 0)
54         panic("failed to send magic to gtask: %x\n", err);
55     debug("GT return %d\n", err);
56 
57     g_send_to_gtask = true;
58 }
59 
starttz_core(void)60 static void starttz_core(void)
61 {
62     g_tz_started = true;
63     int32_t err = smc_switch_req(CAP_TEESMC_REQ_STARTTZ);
64     if (err < 0)
65         panic("starttz failed: %x\n", err);
66 
67     send_to_gtask();
68 }
69 
tee_idle_thread(void * arg)70 __attribute__((noreturn)) void *tee_idle_thread(void *arg)
71 {
72     (void)arg;
73 
74     int32_t err = set_priority(PRIO_TEE_SMCMGR_IDLE);
75     if (err < 0)
76         panic("api set priority failed: %x\n", err);
77     (void)sched_yield();
78 
79     starttz_core();
80     error("StartTZ done\n");
81 
82     while (1) {
83         err = smc_switch_req(CAP_TEESMC_REQ_IDLE);
84         if (err != 0)
85             panic("something wrong");
86     }
87 }
88