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 <stdio.h>
13 #include <inttypes.h>
14 #include <securec.h>
15 #include <ipclib.h>
16 #include <usrsyscall_smc.h>
17 #include <usrsyscall_irq.h>
18 #include "teesmcmgr.h"
19 #include <sched.h>
20
21 #define NORMAL_MSG_ID 0xDEADBEEF
22 #define SMC_BUF_OPS (-1ULL)
23
24 struct gtask_msg {
25 uint32_t msg_id;
26 union {
27 char payload[PAY_LOAD_SIZE];
28 uint64_t kill_ca;
29 };
30 } __attribute__((packed));
31
teeapi_configure(void)32 static void teeapi_configure(void)
33 {
34 int32_t err;
35
36 err = set_priority(PRIO_TEE_SMCMGR);
37 if (err < 0)
38 panic("api set priority failed: %x\n", err);
39
40 err = disable_local_irq();
41 if (err < 0)
42 panic("hmex disable local irq failed: %x\n", err);
43 }
44
tee_smc_thread(void * arg)45 __attribute__((noreturn)) void *tee_smc_thread(void *arg)
46 {
47 (void)arg;
48 int32_t err;
49 errno_t ret_s;
50 struct cap_teesmc_buf smc_buf = {0};
51 struct gtask_msg normal_msg = {0};
52 static const char magic_msg[] = MAGIC_MSG;
53
54 normal_msg.msg_id = NORMAL_MSG_ID;
55 ret_s = memcpy_s(normal_msg.payload, sizeof(normal_msg.payload), magic_msg, sizeof(magic_msg));
56 if (ret_s != EOK)
57 panic("memory copy failed\n");
58
59 info("Start teesmc\n");
60 teeapi_configure();
61 (void)sched_yield();
62
63 while (1) {
64 debug("tee smc thread: wait for switch req\n");
65 smc_buf.ops = SMC_BUF_OPS;
66 err = smc_wait_switch_req(&smc_buf);
67 debug("tee smc thread: return from REE, err=%d, ops=%" PRIx64 "\n", err, smc_buf.ops);
68
69 if (err == 0) {
70 if (get_is_gtask_alive() == 0)
71 continue;
72
73 err = 0;
74 if (smc_buf.ops == CAP_TEESMC_OPS_NORMAL)
75 err = ipc_msg_notification(get_gtask_channel_hdlr(), NULL, 0);
76
77 if (err < 0)
78 error("failed to notify gtask, err=0x%x\n", err);
79 } else {
80 error("unexpected err=%x\n", err);
81 }
82 }
83 }
84