Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b026acb7 authored by Matthew Maurer's avatar Matthew Maurer Committed by Gerrit Code Review
Browse files

Merge changes I595cb4ee,Ie8623a70

* changes:
  trusty: tipc-test: Use dma_buf rather than memfd
  trusty: tipc-test: Test multi-page support
parents 09494764 8ca6ab01
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ cc_test {
    srcs: ["tipc_test.c"],
    shared_libs: [
        "libc",
        "libdmabufheap",
        "liblog",
        "libtrusty",
    ],
+27 −14
Original line number Diff line number Diff line
@@ -25,6 +25,8 @@
#include <sys/mman.h>
#include <sys/uio.h>

#include <BufferAllocator/BufferAllocatorWrapper.h>

#include <trusty/tipc.h>

#define TIPC_DEFAULT_DEVNAME "/dev/trusty-ipc-dev0"
@@ -86,7 +88,7 @@ static const char* usage_long =
        "   ta-access    - test ta-access flags\n"
        "   writev       - writev test\n"
        "   readv        - readv test\n"
        "   send-fd      - transmit memfd to trusty, use as shm\n"
        "   send-fd      - transmit dma_buf to trusty, use as shm\n"
        "\n";

static uint opt_repeat  = 1;
@@ -890,9 +892,12 @@ static int readv_test(uint repeat, uint msgsz, bool var)

static int send_fd_test(void) {
    int ret;
    int memfd = -1;
    int dma_buf = -1;
    int fd = -1;
    volatile char* buf = MAP_FAILED;
    BufferAllocator* allocator = NULL;

    const size_t num_pages = 10;

    fd = tipc_connect(dev_name, receiver_name);
    if (fd < 0) {
@@ -901,22 +906,24 @@ static int send_fd_test(void) {
        goto cleanup;
    }

    memfd = memfd_create("tipc-send-fd", 0);
    if (memfd < 0) {
        fprintf(stderr, "Failed to create memfd: %s\n", strerror(errno));
    allocator = CreateDmabufHeapBufferAllocator();
    if (!allocator) {
        fprintf(stderr, "Failed to create dma-buf allocator.\n");
        ret = -1;
        goto cleanup;
    }

    if (ftruncate(memfd, PAGE_SIZE) < 0) {
        fprintf(stderr, "Failed to resize memfd: %s\n", strerror(errno));
        ret = -1;
    size_t buf_size = PAGE_SIZE * num_pages;
    dma_buf = DmabufHeapAlloc(allocator, "system", buf_size, 0);
    if (dma_buf < 0) {
        ret = dma_buf;
        fprintf(stderr, "Failed to create dma-buf fd of size %zu err (%d)\n", buf_size, ret);
        goto cleanup;
    }

    buf = mmap(0, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, memfd, 0);
    buf = mmap(0, buf_size, PROT_READ | PROT_WRITE, MAP_SHARED, dma_buf, 0);
    if (buf == MAP_FAILED) {
        fprintf(stderr, "Failed to map memfd: %s\n", strerror(errno));
        fprintf(stderr, "Failed to map dma-buf: %s\n", strerror(errno));
        ret = -1;
        goto cleanup;
    }
@@ -924,13 +931,13 @@ static int send_fd_test(void) {
    strcpy((char*)buf, "From NS");

    struct trusty_shm shm = {
            .fd = memfd,
            .fd = dma_buf,
            .transfer = TRUSTY_SHARE,
    };

    ssize_t rc = tipc_send(fd, NULL, 0, &shm, 1);
    if (rc < 0) {
        fprintf(stderr, "tipc_send failed\n");
        fprintf(stderr, "tipc_send failed: %zd\n", rc);
        ret = rc;
        goto cleanup;
    }
@@ -938,13 +945,19 @@ static int send_fd_test(void) {
    read(fd, &c, 1);
    tipc_close(fd);

    ret = strcmp("Hello from Trusty!", (const char*)buf) ? (-1) : 0;
    ret = 0;
    for (size_t skip = 0; skip < num_pages; skip++) {
        ret |= strcmp("Hello from Trusty!", (const char*)&buf[skip * PAGE_SIZE]) ? (-1) : 0;
    }

cleanup:
    if (buf != MAP_FAILED) {
        munmap((char*)buf, PAGE_SIZE);
    }
    close(memfd);
    close(dma_buf);
    if (allocator) {
        FreeDmabufHeapBufferAllocator(allocator);
    }
    tipc_close(fd);
    return ret;
}