paul@0 | 1 | /* |
paul@0 | 2 | * Test dataspace operations. |
paul@0 | 3 | * |
paul@0 | 4 | * Copyright (C) 2020 Paul Boddie <paul@boddie.org.uk> |
paul@0 | 5 | * |
paul@0 | 6 | * This program is free software; you can redistribute it and/or |
paul@0 | 7 | * modify it under the terms of the GNU General Public License as |
paul@0 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@0 | 9 | * the License, or (at your option) any later version. |
paul@0 | 10 | * |
paul@0 | 11 | * This program is distributed in the hope that it will be useful, |
paul@0 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@0 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@0 | 14 | * GNU General Public License for more details. |
paul@0 | 15 | * |
paul@0 | 16 | * You should have received a copy of the GNU General Public License |
paul@0 | 17 | * along with this program; if not, write to the Free Software |
paul@0 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@0 | 19 | * Boston, MA 02110-1301, USA |
paul@0 | 20 | */ |
paul@0 | 21 | |
paul@0 | 22 | #include <l4/re/env.h> |
paul@0 | 23 | #include <l4/sys/err.h> |
paul@0 | 24 | |
paul@0 | 25 | #include <stdio.h> |
paul@0 | 26 | #include <string.h> |
paul@0 | 27 | #include <stdlib.h> |
paul@0 | 28 | |
paul@0 | 29 | #include <ipc/server.h> |
paul@0 | 30 | |
paul@0 | 31 | #include "dataspace_server.h" |
paul@0 | 32 | #include "flexpage.h" |
paul@0 | 33 | |
paul@0 | 34 | |
paul@0 | 35 | |
paul@0 | 36 | /* Component interface. */ |
paul@0 | 37 | |
paul@0 | 38 | class DataspaceServer : public Dataspace |
paul@0 | 39 | { |
paul@0 | 40 | protected: |
paul@0 | 41 | Region *_region; |
paul@0 | 42 | offset_t _start, _size; |
paul@0 | 43 | |
paul@0 | 44 | public: |
paul@0 | 45 | explicit DataspaceServer(char *addr, offset_t size) |
paul@0 | 46 | { |
paul@0 | 47 | _region = new Region((offset_t) addr, (offset_t) addr + size); |
paul@0 | 48 | _start = 0; |
paul@0 | 49 | _size = size; |
paul@0 | 50 | |
paul@0 | 51 | offset_t i; |
paul@0 | 52 | int j; |
paul@0 | 53 | |
paul@0 | 54 | for (i = 0, j = 0; i < size; i += PAGE_SIZE, j++) |
paul@0 | 55 | memset((void *) (addr + i), (int) 'a' + j, PAGE_SIZE); |
paul@0 | 56 | } |
paul@0 | 57 | |
paul@0 | 58 | virtual long map(unsigned long offset, l4_addr_t hot_spot, unsigned long flags, l4_snd_fpage_t *region) |
paul@0 | 59 | { |
paul@0 | 60 | Flexpage flexpage(_region); |
paul@0 | 61 | offset_t max_offset = _start + _size; |
paul@0 | 62 | |
paul@0 | 63 | printf("map(%ld, %lx, %lx, ...)\n", offset, hot_spot, flags); |
paul@0 | 64 | |
paul@0 | 65 | flexpage.reset(_start + offset); |
paul@0 | 66 | |
paul@0 | 67 | SendFlexpage send_flexpage = flexpage.to_send(offset, hot_spot, max_offset); |
paul@0 | 68 | |
paul@0 | 69 | printf("send %lx -> %lx -> {%lx, %d}\n", _region->start, flexpage.base_addr, send_flexpage.base_addr, send_flexpage.order); |
paul@0 | 70 | |
paul@0 | 71 | /* Send the flexpage explicitly. */ |
paul@0 | 72 | |
paul@0 | 73 | region->fpage = l4_fpage(send_flexpage.base_addr, send_flexpage.order, |
paul@0 | 74 | (flags & L4RE_DS_MAP_FLAG_RW) ? L4_FPAGE_RW |
paul@0 | 75 | : L4_FPAGE_RO); |
paul@0 | 76 | region->snd_base = hot_spot; |
paul@0 | 77 | |
paul@0 | 78 | long err = complete_Dataspace_map(*region); |
paul@0 | 79 | |
paul@0 | 80 | if (err) |
paul@0 | 81 | return err; |
paul@0 | 82 | |
paul@0 | 83 | return IPC_MESSAGE_SENT; |
paul@0 | 84 | } |
paul@0 | 85 | }; |
paul@0 | 86 | |
paul@0 | 87 | |
paul@0 | 88 | |
paul@0 | 89 | int main(void) |
paul@0 | 90 | { |
paul@0 | 91 | /* Some memory. */ |
paul@0 | 92 | |
paul@0 | 93 | char *memory; |
paul@0 | 94 | |
paul@0 | 95 | if (posix_memalign((void **) &memory, 40960, 40960)) |
paul@0 | 96 | { |
paul@0 | 97 | printf("Could not allocate memory.\n"); |
paul@0 | 98 | return 1; |
paul@0 | 99 | } |
paul@0 | 100 | |
paul@0 | 101 | /* Dataspace encapsulation. */ |
paul@0 | 102 | |
paul@0 | 103 | DataspaceServer obj(memory, 40960); |
paul@0 | 104 | |
paul@0 | 105 | /* Server capability. */ |
paul@0 | 106 | |
paul@0 | 107 | l4_cap_idx_t server; |
paul@0 | 108 | |
paul@0 | 109 | /* Register a server associating it with the given object. */ |
paul@0 | 110 | |
paul@0 | 111 | long err = ipc_server_bind("server", (l4_umword_t) &obj, &server); |
paul@0 | 112 | |
paul@0 | 113 | if (err) |
paul@0 | 114 | { |
paul@0 | 115 | printf("Could not bind server: %s\n", l4sys_errtostr(err)); |
paul@0 | 116 | return 1; |
paul@0 | 117 | } |
paul@0 | 118 | |
paul@0 | 119 | /* Wait for messages, dispatching to the handler. */ |
paul@0 | 120 | |
paul@0 | 121 | ipc_server_loop(Dataspace_expected_items, &obj, |
paul@0 | 122 | (ipc_server_handler_type) handle_Dataspace); |
paul@0 | 123 | |
paul@0 | 124 | return 0; |
paul@0 | 125 | } |