1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/dstest_server.cc Tue Jan 19 23:26:49 2021 +0100
1.3 @@ -0,0 +1,125 @@
1.4 +/*
1.5 + * Test dataspace operations.
1.6 + *
1.7 + * Copyright (C) 2020 Paul Boddie <paul@boddie.org.uk>
1.8 + *
1.9 + * This program is free software; you can redistribute it and/or
1.10 + * modify it under the terms of the GNU General Public License as
1.11 + * published by the Free Software Foundation; either version 2 of
1.12 + * the License, or (at your option) any later version.
1.13 + *
1.14 + * This program is distributed in the hope that it will be useful,
1.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.17 + * GNU General Public License for more details.
1.18 + *
1.19 + * You should have received a copy of the GNU General Public License
1.20 + * along with this program; if not, write to the Free Software
1.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
1.22 + * Boston, MA 02110-1301, USA
1.23 + */
1.24 +
1.25 +#include <l4/re/env.h>
1.26 +#include <l4/sys/err.h>
1.27 +
1.28 +#include <stdio.h>
1.29 +#include <string.h>
1.30 +#include <stdlib.h>
1.31 +
1.32 +#include <ipc/server.h>
1.33 +
1.34 +#include "dataspace_server.h"
1.35 +#include "flexpage.h"
1.36 +
1.37 +
1.38 +
1.39 +/* Component interface. */
1.40 +
1.41 +class DataspaceServer : public Dataspace
1.42 +{
1.43 +protected:
1.44 + Region *_region;
1.45 + offset_t _start, _size;
1.46 +
1.47 +public:
1.48 + explicit DataspaceServer(char *addr, offset_t size)
1.49 + {
1.50 + _region = new Region((offset_t) addr, (offset_t) addr + size);
1.51 + _start = 0;
1.52 + _size = size;
1.53 +
1.54 + offset_t i;
1.55 + int j;
1.56 +
1.57 + for (i = 0, j = 0; i < size; i += PAGE_SIZE, j++)
1.58 + memset((void *) (addr + i), (int) 'a' + j, PAGE_SIZE);
1.59 + }
1.60 +
1.61 + virtual long map(unsigned long offset, l4_addr_t hot_spot, unsigned long flags, l4_snd_fpage_t *region)
1.62 + {
1.63 + Flexpage flexpage(_region);
1.64 + offset_t max_offset = _start + _size;
1.65 +
1.66 + printf("map(%ld, %lx, %lx, ...)\n", offset, hot_spot, flags);
1.67 +
1.68 + flexpage.reset(_start + offset);
1.69 +
1.70 + SendFlexpage send_flexpage = flexpage.to_send(offset, hot_spot, max_offset);
1.71 +
1.72 + printf("send %lx -> %lx -> {%lx, %d}\n", _region->start, flexpage.base_addr, send_flexpage.base_addr, send_flexpage.order);
1.73 +
1.74 + /* Send the flexpage explicitly. */
1.75 +
1.76 + region->fpage = l4_fpage(send_flexpage.base_addr, send_flexpage.order,
1.77 + (flags & L4RE_DS_MAP_FLAG_RW) ? L4_FPAGE_RW
1.78 + : L4_FPAGE_RO);
1.79 + region->snd_base = hot_spot;
1.80 +
1.81 + long err = complete_Dataspace_map(*region);
1.82 +
1.83 + if (err)
1.84 + return err;
1.85 +
1.86 + return IPC_MESSAGE_SENT;
1.87 + }
1.88 +};
1.89 +
1.90 +
1.91 +
1.92 +int main(void)
1.93 +{
1.94 + /* Some memory. */
1.95 +
1.96 + char *memory;
1.97 +
1.98 + if (posix_memalign((void **) &memory, 40960, 40960))
1.99 + {
1.100 + printf("Could not allocate memory.\n");
1.101 + return 1;
1.102 + }
1.103 +
1.104 + /* Dataspace encapsulation. */
1.105 +
1.106 + DataspaceServer obj(memory, 40960);
1.107 +
1.108 + /* Server capability. */
1.109 +
1.110 + l4_cap_idx_t server;
1.111 +
1.112 + /* Register a server associating it with the given object. */
1.113 +
1.114 + long err = ipc_server_bind("server", (l4_umword_t) &obj, &server);
1.115 +
1.116 + if (err)
1.117 + {
1.118 + printf("Could not bind server: %s\n", l4sys_errtostr(err));
1.119 + return 1;
1.120 + }
1.121 +
1.122 + /* Wait for messages, dispatching to the handler. */
1.123 +
1.124 + ipc_server_loop(Dataspace_expected_items, &obj,
1.125 + (ipc_server_handler_type) handle_Dataspace);
1.126 +
1.127 + return 0;
1.128 +}