1 /*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define TLOG_TAG "receiver"
18
19 #include <assert.h>
20
21 #include <inttypes.h>
22 #include <lk/err_ptr.h>
23 #include <lk/macros.h>
24 #include <stddef.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/auxv.h>
29 #include <sys/mman.h>
30
31 #include <lib/tipc/tipc.h>
32 #include <lib/tipc/tipc_srv.h>
33 #include <lib/unittest/unittest.h>
34
35 #include <trusty/sys/mman.h>
36 #include <trusty_unittest.h>
37
38 /* Number of pages to expect from NS */
39 static const size_t num_pages = 10;
40
41 static struct tipc_port_acl receiver_port_acl = {
42 .flags = IPC_PORT_ALLOW_NS_CONNECT,
43 .uuid_num = 0,
44 .uuids = NULL,
45 .extra_data = NULL,
46 };
47
48 static struct tipc_port receiver_port = {
49 .name = "com.android.trusty.memref.receiver",
50 .msg_max_size = 1,
51 .msg_queue_len = 1,
52 .acl = &receiver_port_acl,
53 .priv = NULL,
54 };
55
receiver_on_message(const struct tipc_port * port,handle_t chan,void * ctx)56 static int receiver_on_message(const struct tipc_port* port,
57 handle_t chan,
58 void* ctx) {
59 assert(port == &receiver_port);
60 assert(ctx == NULL);
61 handle_t handle;
62 int rc;
63
64 struct ipc_msg msg = {
65 .iov = NULL,
66 .num_iov = 0,
67 .handles = &handle,
68 .num_handles = 1,
69 };
70
71 struct ipc_msg_info msg_inf;
72 rc = get_msg(chan, &msg_inf);
73 if (rc) {
74 return rc;
75 }
76
77 if (msg_inf.num_handles != 1) {
78 TLOGE("Message had no handles\n");
79 return -1;
80 }
81
82 rc = read_msg(chan, msg_inf.id, 0, &msg);
83 put_msg(chan, msg_inf.id);
84 if (rc < 0) {
85 TLOGE("Failed to read message\n");
86 return rc;
87 }
88
89 size_t page_size = getauxval(AT_PAGESZ);
90
91 char* out = mmap(0, page_size * num_pages, PROT_READ | PROT_WRITE, 0,
92 handle, 0);
93 if (out == MAP_FAILED) {
94 rc = (intptr_t)out;
95 TLOGE("Failed to mmap handle\n");
96 return rc;
97 }
98
99 for (size_t skip = 0; skip < num_pages; skip++) {
100 strcpy(&out[skip * page_size], "Hello from Trusty!");
101 }
102
103 rc = munmap((void*)out, page_size * num_pages);
104 if (rc != NO_ERROR) {
105 TLOGE("munmap() failed: %d\n", rc);
106 return rc;
107 }
108
109 close(handle);
110
111 // Send a message for sync
112 char c = 0;
113 tipc_send1(chan, &c, 1);
114
115 return rc;
116 }
117
118 static struct tipc_srv_ops receiver_ops = {
119 .on_message = receiver_on_message,
120 };
121
main(void)122 int main(void) {
123 struct tipc_hset* hset = tipc_hset_create();
124
125 if (IS_ERR(hset)) {
126 return PTR_ERR(hset);
127 }
128
129 int rc = tipc_add_service(hset, &receiver_port, 1, 1, &receiver_ops);
130 if (rc < 0) {
131 return rc;
132 }
133
134 rc = tipc_run_event_loop(hset);
135 TLOGE("receiver going down: (%d)\n", rc);
136 return rc;
137 }
138