summaryrefslogtreecommitdiff
path: root/lib/trusty/ql-tipc/ipc_dev.c
blob: 5924d448915ef24db5c675c4b339f6aa078d779e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include <trusty/trusty_dev.h>
#include <trusty/trusty_ipc.h>
#include <trusty/util.h>

#define NS_PTE_PHYSADDR(pte)       ((pte) & 0xFFFFFFFFF000ULL)

#define QL_TIPC_DEV_RESP     0x8000
#define QL_TIPC_DEV_CONNECT     0x1
#define QL_TIPC_DEV_GET_EVENT   0x2
#define QL_TIPC_DEV_SEND        0x3
#define QL_TIPC_DEV_RECV        0x4
#define QL_TIPC_DEV_DISCONNECT  0x5

#define LOCAL_LOG 0

struct trusty_ipc_cmd_hdr {
    uint16_t opcode;
    uint16_t flags;
    uint32_t status;
    uint32_t handle;
    uint32_t payload_len;
    uint8_t  payload[0];
};

struct trusty_ipc_wait_req {
    uint64_t reserved;
};

struct trusty_ipc_connect_req {
    uint64_t cookie;
    uint64_t reserved;
    uint8_t  name[0];
};

static size_t iovec_size(const struct trusty_ipc_iovec *iovs, size_t iovs_cnt)
{
    size_t i;
    size_t cb = 0;

    trusty_assert(iovs);

    for (i = 0; i < iovs_cnt; i++) {
        cb += iovs[i].len;
    }

    return cb;
}

static size_t iovec_to_buf(void *buf, size_t buf_len,
                           const struct trusty_ipc_iovec *iovs, size_t iovs_cnt)
{
    size_t i;
    size_t buf_pos = 0;

    trusty_assert(iovs);

    for (i = 0; i < iovs_cnt; i++) {
        size_t to_copy = (size_t)iovs[i].len;

        if (!to_copy)
            continue;

        if (to_copy > buf_len)
            to_copy = buf_len;

        trusty_memcpy((uint8_t *)buf + buf_pos, iovs[i].base, to_copy);

        buf_pos += to_copy;
        buf_len -= to_copy;

        if (buf_len == 0)
            break;
    }

    return buf_pos;
}

static size_t buf_to_iovec(const struct trusty_ipc_iovec *iovs, size_t iovs_cnt,
                           const void *buf, size_t buf_len)
{
    size_t i;
    size_t copied = 0;
    const uint8_t *buf_ptr = buf;

    trusty_assert(buf_ptr);
    trusty_assert(iovs);

    if (iovs_cnt == 0 || buf_len == 0)
        return 0;

    for (i = 0; i < iovs_cnt; i++) {
        size_t to_copy = buf_len;

        if (to_copy > iovs[i].len)
            to_copy = iovs[i].len;

        if (!to_copy)
            continue;

        trusty_memcpy(iovs[i].base, buf_ptr, to_copy);

        copied  += to_copy;
        buf_ptr += to_copy;
        buf_len -= to_copy;

        if (buf_len == 0)
            break;
    }

    return copied;
}

static int check_response(struct trusty_ipc_dev *dev,
                          volatile struct trusty_ipc_cmd_hdr *hdr, uint16_t cmd)
{
    if (hdr->opcode != (cmd | QL_TIPC_DEV_RESP)) {
        /* malformed response */
        trusty_error("%s: malformed response cmd: 0x%x\n",
                     __func__, hdr->opcode);
        return TRUSTY_ERR_SECOS_ERR;
    }

    if (hdr->status) {
        /* secure OS responded with error: TODO need error code */
        trusty_error("%s: cmd 0x%x: status = %d\n",
                     __func__, hdr->opcode, hdr->status);
        return TRUSTY_ERR_SECOS_ERR;
    }

    return TRUSTY_ERR_NONE;
}

int trusty_ipc_dev_create(struct trusty_ipc_dev **idev,
                          struct trusty_dev *tdev,
                          size_t buf_size)
{
    int rc;
    struct trusty_ipc_dev *dev;

    trusty_assert(idev);

    trusty_debug("%s: Create new Trusty IPC device (%zu)\n", __func__, buf_size);

    /* allocate device context */
    dev = trusty_calloc(1, sizeof(*dev));
    if (!dev) {
        trusty_error("%s: failed to allocate Trusty IPC device\n", __func__);
        return TRUSTY_ERR_NO_MEMORY;
    }
    dev->tdev = tdev;

    /* allocate shared buffer */
    dev->buf_size = buf_size;
    dev->buf_vaddr = trusty_membuf_alloc(&dev->buf_ns, buf_size);
    if (!dev->buf_vaddr) {
        trusty_error("%s: failed to allocate shared memory\n", __func__);
        rc = TRUSTY_ERR_NO_MEMORY;
        goto err_alloc_membuf;
    }

    /* call secure OS to register shared buffer */
    rc = trusty_dev_init_ipc(dev->tdev, &dev->buf_ns, dev->buf_size);
    if (rc != 0) {
        trusty_error("%s: failed (%d) to create Trusty IPC device\n",
                     __func__, rc);
        rc = TRUSTY_ERR_SECOS_ERR;
        goto err_create_sec_dev;
    }

    trusty_debug("%s: new Trusty IPC device (%p)\n", __func__, dev);

    *idev = dev;
    return TRUSTY_ERR_NONE;

err_create_sec_dev:
err_alloc_membuf:
    trusty_membuf_free(dev->buf_vaddr);
    trusty_free(dev);
    return rc;
}

void trusty_ipc_dev_shutdown(struct trusty_ipc_dev *dev)
{
    int rc;
    trusty_assert(dev);

    trusty_debug("%s: shutting down Trusty IPC device (%p)\n", __func__, dev);

    /* shutdown Trusty IPC device */
    rc = trusty_dev_shutdown_ipc(dev->tdev, &dev->buf_ns, dev->buf_size);
    trusty_assert(!rc);
    if (rc != 0) {
        trusty_error("%s: failed (%d) to shutdown Trusty IPC device\n",
                     __func__, rc);
    }
    trusty_membuf_free(dev->buf_vaddr);
    trusty_free(dev);
}

int trusty_ipc_dev_connect(struct trusty_ipc_dev *dev, const char *port,
                           uint64_t cookie)
{
    int rc;
    size_t port_len;
    volatile struct trusty_ipc_cmd_hdr *cmd;
    struct trusty_ipc_connect_req *req;

    trusty_assert(dev);
    trusty_assert(port);

    trusty_debug("%s: connecting to '%s'\n", __func__, port);

    /* check port name length */
    port_len = trusty_strlen(port) + 1;
    if (port_len > (dev->buf_size - sizeof(*cmd) + sizeof(*req))) {
        /* it would not fit into buffer */
        trusty_error("%s: port name is too long (%zu)\n", __func__, port_len);
        return TRUSTY_ERR_INVALID_ARGS;
    }

    /* prepare command */
    cmd = dev->buf_vaddr;
    trusty_memset((void *)cmd, 0, sizeof(*cmd));
    cmd->opcode = QL_TIPC_DEV_CONNECT;

    /* prepare payload  */
    req = (struct trusty_ipc_connect_req *)cmd->payload;
    trusty_memset((void *)req, 0, sizeof(*req));
    req->cookie = cookie;
    trusty_strcpy((char *)req->name, port);
    cmd->payload_len = sizeof(*req) + port_len;

    /* call secure os */
    rc = trusty_dev_exec_ipc(dev->tdev,
                             &dev->buf_ns, sizeof(*cmd) + cmd->payload_len);
    if (rc) {
        /* secure OS returned an error */
        trusty_error("%s: secure OS returned (%d)\n", __func__, rc);
        return TRUSTY_ERR_SECOS_ERR;
    }

    rc = check_response(dev, cmd, QL_TIPC_DEV_CONNECT);
    if (rc) {
        trusty_error("%s: connect cmd failed (%d)\n", __func__, rc);
        return rc;
    }

    /* success */
    return cmd->handle;
}

int trusty_ipc_dev_close(struct trusty_ipc_dev *dev, handle_t handle)
{
    int rc;
    volatile struct trusty_ipc_cmd_hdr *cmd;

    trusty_assert(dev);

    trusty_debug("%s: chan %d: closing\n", __func__, handle);

    /* prepare command */
    cmd = dev->buf_vaddr;
    trusty_memset((void *)cmd, 0, sizeof(*cmd));
    cmd->opcode = QL_TIPC_DEV_DISCONNECT;
    cmd->handle = handle;
    /* no payload */

    /* call into secure os */
    rc = trusty_dev_exec_ipc(dev->tdev,
                             &dev->buf_ns, sizeof(*cmd) + cmd->payload_len);
    if (rc) {
        trusty_error("%s: secure OS returned (%d)\n", __func__, rc);
        return TRUSTY_ERR_SECOS_ERR;
    }

    rc = check_response(dev, cmd, QL_TIPC_DEV_DISCONNECT);
    if (rc) {
        trusty_error("%s: disconnect cmd failed (%d)\n", __func__, rc);
        return rc;
    }

    trusty_debug("%s: chan %d: closed\n", __func__, handle);

    return TRUSTY_ERR_NONE;
}

int trusty_ipc_dev_get_event(struct trusty_ipc_dev *dev, handle_t chan,
                             struct trusty_ipc_event *event)
{
    int rc;
    volatile struct trusty_ipc_cmd_hdr *cmd;

    trusty_assert(dev);
    trusty_assert(event);

    /* prepare command */
    cmd = dev->buf_vaddr;
    trusty_memset((void *)cmd, 0, sizeof(*cmd));
    cmd->opcode = QL_TIPC_DEV_GET_EVENT;
    cmd->handle = chan;

    /* prepare payload  */
    trusty_memset((void *)cmd->payload, 0, sizeof(struct trusty_ipc_wait_req));
    cmd->payload_len = sizeof(struct trusty_ipc_wait_req);

    /* call into secure os */
    rc = trusty_dev_exec_ipc(dev->tdev,
                             &dev->buf_ns, sizeof(*cmd) + cmd->payload_len);
    if (rc) {
        trusty_error("%s: secure OS returned (%d)\n", __func__, rc);
        return TRUSTY_ERR_SECOS_ERR;
    }

    rc = check_response(dev, cmd, QL_TIPC_DEV_GET_EVENT);
    if (rc) {
        trusty_error("%s: get event cmd failed (%d)\n", __func__, rc);
        return rc;
    }

    if ((size_t)cmd->payload_len < sizeof(*event)) {
        trusty_error("%s: invalid response length (%zd)\n",
                     __func__, (size_t)cmd->payload_len);
        return TRUSTY_ERR_SECOS_ERR;
    }

    /* copy out event */
    trusty_memcpy(event, (const void *)cmd->payload, sizeof(*event));
    return TRUSTY_ERR_NONE;
}

int trusty_ipc_dev_send(struct trusty_ipc_dev *dev, handle_t chan,
                        const struct trusty_ipc_iovec *iovs, size_t iovs_cnt)
{
    int rc;
    size_t msg_size;
    volatile struct trusty_ipc_cmd_hdr *cmd;

    trusty_assert(dev);
    /* calc message length */
    msg_size = iovec_size(iovs, iovs_cnt);
    if (msg_size > dev->buf_size - sizeof(*cmd)) {
        /* msg is too big to fit provided buffer */
        trusty_error("%s: chan %d: msg is too long (%zu)\n", __func__,
                     chan, msg_size);
        return TRUSTY_ERR_MSG_TOO_BIG;
    }

    /* prepare command */
    cmd = dev->buf_vaddr;
    trusty_memset((void *)cmd, 0, sizeof(*cmd));
    cmd->opcode = QL_TIPC_DEV_SEND;
    cmd->handle = chan;

    /* copy in message data */
    cmd->payload_len = (uint32_t)msg_size;
    msg_size = iovec_to_buf(dev->buf_vaddr + sizeof(*cmd), dev->buf_size - sizeof(*cmd),
                          iovs,  iovs_cnt);
    trusty_assert(msg_size == (size_t)cmd->payload_len);

    /* call into secure os */
    rc = trusty_dev_exec_ipc(dev->tdev,
                             &dev->buf_ns, sizeof(*cmd) + cmd->payload_len);
    if (rc < 0) {
        trusty_error("%s: secure OS returned (%d)\n", __func__, rc);
        return TRUSTY_ERR_SECOS_ERR;
    }

    rc = check_response(dev, cmd, QL_TIPC_DEV_SEND);
    if (rc) {
        trusty_error("%s: send msg failed (%d)\n", __func__, rc);
    }

    return rc;
}


int trusty_ipc_dev_recv(struct trusty_ipc_dev *dev, handle_t chan,
                        const struct trusty_ipc_iovec *iovs, size_t iovs_cnt)
{
    int rc;
    size_t copied;
    volatile struct trusty_ipc_cmd_hdr *cmd;

    trusty_assert(dev);

    /* prepare command */
    cmd = dev->buf_vaddr;
    trusty_memset((void *)cmd, 0, sizeof(*cmd));
    cmd->opcode = QL_TIPC_DEV_RECV;
    cmd->handle = chan;
    /* no payload */

    /* call into secure os */
    rc = trusty_dev_exec_ipc(dev->tdev,
                             &dev->buf_ns, sizeof(*cmd) + cmd->payload_len);
    if (rc < 0) {
        trusty_error("%s: secure OS returned (%d)\n", __func__, rc);
        return TRUSTY_ERR_SECOS_ERR;
    }

    rc = check_response(dev, cmd, QL_TIPC_DEV_RECV);
    if (rc) {
        trusty_error("%s: recv cmd failed (%d)\n", __func__, rc);
        return rc;
    }

    /* copy data out to proper destination */
    copied = buf_to_iovec(iovs, iovs_cnt,
                          (const void *)cmd->payload, cmd->payload_len);
    if (copied != (size_t)cmd->payload_len) {
        /* msg is too big to fit provided buffer */
        trusty_error("%s: chan %d: buffer too small (%zu vs. %zu)\n",
                     __func__, chan, copied, (size_t)cmd->payload_len);
        return TRUSTY_ERR_MSG_TOO_BIG;
    }

    return (int)copied;
}

void trusty_ipc_dev_idle(struct trusty_ipc_dev *dev)
{
    trusty_idle(dev->tdev);
}