1 /* 2 * Test dataspace operations. 3 * 4 * Copyright (C) 2020, 2021 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/mem_ipc.h> 30 31 #include "dataspace_client.h" 32 #include "opener_client.h" 33 #include "opener_context_client.h" 34 #include "mapped_file_client.h" 35 #include "memory_utils.h" 36 37 38 39 int main(void) 40 { 41 l4_cap_idx_t server = l4re_env_get_cap("server"); 42 l4_cap_idx_t context_ref; 43 client_Opener opener(server); 44 45 long err = opener.context(&context_ref); 46 47 if (err) 48 { 49 printf("Could not obtain context: %s\n", l4sys_errtostr(err)); 50 return 1; 51 } 52 53 client_OpenerContext context(context_ref); 54 client_Dataspace context_ds(context_ref); 55 unsigned long size, flags; 56 57 err = context_ds.info(&size, &flags); 58 59 if (err) 60 { 61 printf("Could not obtain context info: %s\n", l4sys_errtostr(err)); 62 return 1; 63 } 64 65 /* Map context memory to write the filename. */ 66 67 char *memory; 68 69 printf("Attach region of size %ld...\n", size); 70 71 err = ipc_attach_dataspace(context_ref, size, (void **) &memory); 72 73 if (err) 74 { 75 printf("Could not map memory: %s\n", l4sys_errtostr(err)); 76 return 1; 77 } 78 79 printf("Mapped memory at %p\n", memory); 80 81 /* Write the filename. */ 82 83 strcpy(memory, "123"); 84 85 /* Invoke the open method to receive the file reference. */ 86 87 l4_cap_idx_t file; 88 89 err = context.open(L4_FPAGE_RW, &size, &file); 90 91 if (err) 92 { 93 printf("Could not obtain file: %s\n", l4sys_errtostr(err)); 94 return 1; 95 } 96 97 client_MappedFile obj(file); 98 99 /* Some memory to be mapped. */ 100 101 size_t start_pos, end_pos, data_end; 102 103 printf("Map region from %ld to %ld...\n", 0L, page(10)); 104 105 err = obj.mmap(0, page(10), &start_pos, &end_pos, &data_end); 106 107 if (err) 108 { 109 printf("Could not map file region: %s\n", l4sys_errtostr(err)); 110 return 1; 111 } 112 113 size = end_pos - start_pos; 114 115 printf("Attach region of size %ld...\n", size); 116 117 err = ipc_attach_dataspace(file, size, (void **) &memory); 118 119 if (err) 120 { 121 printf("Could not map memory: %s\n", l4sys_errtostr(err)); 122 return 1; 123 } 124 125 printf("Mapped memory at %p\n", memory); 126 127 for (unsigned long offset = 0; offset < size; offset += 1024) 128 { 129 char buf[11]; 130 printf("10 bytes from %p...\n", (memory + offset)); 131 strncpy(buf, (memory + offset), 10); 132 buf[10] = '\0'; 133 printf("%s\n", buf); 134 } 135 136 return 0; 137 }