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