1 /*
2 * Copyright 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 #include "DPUHandler.h"
18 #include "trusty/secure_dpu/SecureDpu.h"
19
20 #include <android-base/logging.h>
21 #include <errno.h>
22 #include <poll.h>
23 #include <stdio.h>
24 #include <sys/stat.h>
25 #include <sys/uio.h>
26 #include <unistd.h>
27 #include <trusty/tipc.h>
28
29 #include <BufferAllocator/BufferAllocatorWrapper.h>
30
31 #define countof(arr) (sizeof(arr) / sizeof(arr[0]))
32
33 namespace android {
34 namespace trusty {
35 namespace secure_dpu {
36
DPUHandler()37 DPUHandler::DPUHandler() : buf_allocator_(nullptr) {}
38
~DPUHandler()39 DPUHandler::~DPUHandler() {
40 tipc_close(dpu_handle_);
41 if (buf_allocator_) {
42 FreeDmabufHeapBufferAllocator(buf_allocator_);
43 }
44 }
45
Init(std::string device_name)46 android::base::Result<void> DPUHandler::Init(std::string device_name) {
47 buf_allocator_ = CreateDmabufHeapBufferAllocator();
48 if (!buf_allocator_) {
49 return base::Error() << "Failed to create buffer allocator";
50 }
51
52 dpu_handle_ = tipc_connect(device_name.c_str(),
53 SECURE_DPU_PORT_NAME);
54 if (dpu_handle_ < 0) {
55 return base::Error() << "Failed to connect to: " << device_name;
56 }
57 return {};
58 }
59
HandleStartSecureDisplay()60 android::base::Result<void> DPUHandler::HandleStartSecureDisplay() {
61 secure_dpu_resp rsp;
62 rsp.cmd = SECURE_DPU_CMD_START_SECURE_DISPLAY | SECURE_DPU_CMD_RESP_BIT;
63 rsp.status = SECURE_DPU_ERROR_OK;
64
65 auto write_len = write(dpu_handle_, &rsp, sizeof(rsp));
66 if (write_len != sizeof(rsp)) {
67 return base::Error() << "Failed to write command";
68 }
69 return {};
70 }
71
HandleStopSecureDisplay()72 android::base::Result<void> DPUHandler::HandleStopSecureDisplay() {
73 secure_dpu_resp rsp;
74 rsp.cmd = SECURE_DPU_CMD_STOP_SECURE_DISPLAY | SECURE_DPU_CMD_RESP_BIT;
75 rsp.status = SECURE_DPU_ERROR_OK;
76
77 auto write_len = write(dpu_handle_, &rsp, sizeof(rsp));
78 if (write_len != sizeof(rsp)) {
79 return base::Error() << "Failed to write command";
80 }
81 return {};
82 }
83
AllocateBuffer(size_t req_buffer_len,size_t * allocated_buffer_len,int * buf_fd)84 android::base::Result<void> DPUHandler::AllocateBuffer(size_t req_buffer_len,
85 size_t* allocated_buffer_len, int* buf_fd) {
86 auto dma_buf_fd =
87 DmabufHeapAlloc(buf_allocator_, "system", req_buffer_len, 0, 0 /* legacy align */);
88 if (dma_buf_fd < 0) {
89 return base::Error() << "Failed to allocate buffer."
90 << " rc = " << dma_buf_fd << " size = " << req_buffer_len;
91 }
92
93 *buf_fd = dma_buf_fd;
94 *allocated_buffer_len = req_buffer_len;
95 return {};
96 }
97
98 android::base::Result<void>
HandleAllocateBuffer(const secure_dpu_allocate_buffer_req * req)99 DPUHandler::HandleAllocateBuffer(const secure_dpu_allocate_buffer_req* req) {
100 size_t req_buffer_len = static_cast<size_t>(req->buffer_len);
101 LOG(DEBUG) << "Requested buffer length: " << req_buffer_len;
102
103 secure_dpu_resp rsp;
104 secure_dpu_allocate_buffer_resp msg_rsp;
105
106 iovec iov[] = {
107 {
108 .iov_base = &rsp,
109 .iov_len = sizeof(rsp),
110 },
111 {
112 .iov_base = &msg_rsp,
113 .iov_len = sizeof(msg_rsp),
114 },
115 };
116 trusty_shm shm;
117 size_t allocated_buffer_len = 0;
118 int buf_fd = kInvalidFd;
119 auto result = AllocateBuffer(req_buffer_len, &allocated_buffer_len, &buf_fd);
120 if (result.ok()) {
121 rsp.status = SECURE_DPU_ERROR_OK;
122 } else {
123 LOG(ERROR) << result.error();
124 rsp.status = SECURE_DPU_ERROR_FAIL;
125 }
126
127 rsp.cmd = SECURE_DPU_CMD_ALLOCATE_BUFFER | SECURE_DPU_CMD_RESP_BIT;
128
129 msg_rsp.buffer_len = allocated_buffer_len;
130 shm.fd = buf_fd;
131 shm.transfer = TRUSTY_SHARE;
132
133 auto rc = tipc_send(dpu_handle_, iov, countof(iov), &shm, 1);
134 if (buf_fd != kInvalidFd) close(buf_fd);
135 if (rc != sizeof(rsp) + sizeof(msg_rsp)) {
136 return base::Error() << "Failed to do tipc_send: " << rc;
137 }
138 return {};
139 }
140
HandleCmd(const void * in_buf,const size_t in_size)141 android::base::Result<void> DPUHandler::HandleCmd(const void* in_buf, const size_t in_size) {
142 if (in_size < sizeof(secure_dpu_req)) {
143 return base::Error() << "Invalid payload";
144 }
145 const secure_dpu_req* req = reinterpret_cast<const secure_dpu_req*>(in_buf);
146 switch (req->cmd) {
147 case SECURE_DPU_CMD_START_SECURE_DISPLAY: {
148 return HandleStartSecureDisplay();
149 }
150 case SECURE_DPU_CMD_STOP_SECURE_DISPLAY: {
151 return HandleStopSecureDisplay();
152 }
153 case SECURE_DPU_CMD_ALLOCATE_BUFFER: {
154 if (in_size != sizeof(secure_dpu_req) + sizeof(secure_dpu_allocate_buffer_req)) {
155 return base::Error() << "Invalid payload";
156 }
157 const secure_dpu_allocate_buffer_req* req_args =
158 reinterpret_cast<const secure_dpu_allocate_buffer_req*>((uint8_t*)in_buf +
159 sizeof(secure_dpu_req));
160 return HandleAllocateBuffer(req_args);
161 }
162 default:
163 LOG(ERROR) << "Unknown command: " << (uint32_t)req->cmd;
164 return base::Error() << "Unknown command";
165 }
166 return {};
167 }
168
Handle()169 android::base::Result<void> DPUHandler::Handle() {
170 uint8_t in_buf[SECURE_DPU_MAX_MSG_SIZE];
171
172 auto read_len = read(dpu_handle_, in_buf, sizeof(in_buf));
173 if (read_len < 0) {
174 return base::Error() << "Failed to read command";
175 }
176 auto result = HandleCmd(in_buf, read_len);
177 if (!result.ok()) {
178 return base::Error() << "Failed to handle command. "
179 << "Reason: " << result.error();
180 }
181 return {};
182 }
183
184 } // namespace secure_dpu
185 } // namespace trusty
186 } // namespace android
187