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 <cs.h>
13 #include <ipclib_hal.h>
14 #include <tee_log.h>
15 #include <securec.h>
16 #include <drv.h>
17
do_deal_with_msg(const dispatch_fn_t dispatch_fns[],unsigned n_dispatch_fns,void * msg_buff,struct src_msginfo * info,cref_t * msg_hdl)18 static int do_deal_with_msg(const dispatch_fn_t dispatch_fns[], unsigned n_dispatch_fns, void *msg_buff,
19 struct src_msginfo *info, cref_t *msg_hdl)
20 {
21 msg_header *msg_hdr = (msg_header *)(uintptr_t)msg_buff;
22 uint8_t class = msg_hdr->send.msg_class;
23 struct reply_cs_msg rmsg;
24 int32_t ret = 0;
25 (void)memset_s(&rmsg, sizeof(rmsg), 0, sizeof(rmsg));
26
27 bool flag = (class >= n_dispatch_fns) || (dispatch_fns[class] == NULL);
28 if (flag) {
29 rmsg.hdr.reply.ret_val = -EINVAL;
30 if (info->msg_type == MSG_TYPE_CALL) {
31 ret = ipc_msg_reply(*msg_hdl, &rmsg, sizeof(rmsg));
32 if (ret != 0)
33 tloge("cs reply msg error %d\n", ret);
34 }
35 } else {
36 ret = dispatch_fns[class](msg_buff, msg_hdl, info);
37 }
38 return ret;
39 }
40
cs_server_loop(cref_t channel,const dispatch_fn_t dispatch_fns[],unsigned n_dispatch_fns,int (* hook)(void),void * cur_thread)41 void cs_server_loop(cref_t channel, const dispatch_fn_t dispatch_fns[], unsigned n_dispatch_fns, int (*hook)(void), void *cur_thread)
42 {
43 (void)hook;
44 (void)cur_thread;
45 int32_t ret;
46 char msg_buf[SYSCAL_MSG_BUFFER_SIZE] = {0};
47 struct cs_req_msg *msg = (struct cs_req_msg *)msg_buf;
48 struct src_msginfo info = { 0 };
49 for (;;) {
50 cref_t msg_hdl = ipc_get_my_msghdl();
51
52 ret = ipc_msg_receive(channel, msg, sizeof(msg_buf), msg_hdl, &info, -1);
53 if (ret != 0) {
54 tloge("message receive failed, %llx\n", ret);
55 continue;
56 }
57
58 ret = do_deal_with_msg(dispatch_fns, n_dispatch_fns, msg, &info, &msg_hdl);
59 if (ret != 0) {
60 tloge("deal with msg error");
61 }
62 }
63 }