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(int argc, char *argv[]) 40 { 41 if (argc < 4) 42 { 43 printf("Need filename, step and sample size.\n"); 44 return 1; 45 } 46 47 /* Obtain filename and access parameters. */ 48 49 char *filename = argv[1]; 50 unsigned long step = atoi(argv[2]); 51 unsigned long sample = atoi(argv[3]); 52 53 /* Allocate a buffer for sampling from the file. */ 54 55 char buf[sample + 1]; 56 57 /* Obtain access to the filesystem. */ 58 59 l4_cap_idx_t server = l4re_env_get_cap("server"); 60 l4_cap_idx_t context_ref; 61 client_Opener opener(server); 62 63 long err = opener.context(&context_ref); 64 65 if (err) 66 { 67 printf("Could not obtain context: %s\n", l4sys_errtostr(err)); 68 return 1; 69 } 70 71 client_OpenerContext context(context_ref); 72 client_Dataspace context_ds(context_ref); 73 unsigned long size, flags; 74 75 err = context_ds.info(&size, &flags); 76 77 if (err) 78 { 79 printf("Could not obtain context info: %s\n", l4sys_errtostr(err)); 80 return 1; 81 } 82 83 /* Map context memory to write the filename. */ 84 85 char *memory; 86 87 printf("Attach region of size %ld...\n", size); 88 89 err = ipc_attach_dataspace(context_ref, size, (void **) &memory); 90 91 if (err) 92 { 93 printf("Could not map memory: %s\n", l4sys_errtostr(err)); 94 return 1; 95 } 96 97 printf("Mapped memory at %p\n", memory); 98 99 /* Write the filename. */ 100 101 strcpy(memory, filename); 102 103 /* Invoke the open method to receive the file reference. */ 104 105 l4_cap_idx_t file; 106 107 err = context.open(L4_FPAGE_RW, &size, &file); 108 109 if (err) 110 { 111 printf("Could not obtain file: %s\n", l4sys_errtostr(err)); 112 return 1; 113 } 114 115 client_MappedFile obj(file); 116 117 /* Some memory to be mapped. */ 118 119 size_t start_pos, end_pos, data_end; 120 121 printf("Map region from %ld to %ld...\n", 0L, page(10)); 122 123 err = obj.mmap(0, page(10), &start_pos, &end_pos, &data_end); 124 125 if (err) 126 { 127 printf("Could not map file region: %s\n", l4sys_errtostr(err)); 128 return 1; 129 } 130 131 printf("Mapped region from %ld to %ld with content %ld.\n", 132 start_pos, end_pos, data_end); 133 134 size = end_pos - start_pos; 135 136 printf("Attach region of size %ld...\n", size); 137 138 err = ipc_attach_dataspace(file, size, (void **) &memory); 139 140 if (err) 141 { 142 printf("Could not map memory: %s\n", l4sys_errtostr(err)); 143 return 1; 144 } 145 146 printf("Mapped memory at %p\n", memory); 147 148 if (data_end < size) 149 size = data_end; 150 151 for (unsigned long offset = 0; offset < size; offset += step) 152 { 153 unsigned long remaining = size - offset; 154 unsigned long sample_remaining = remaining < sample ? remaining : sample; 155 156 printf("%ld bytes from %p...\n", sample_remaining, (memory + offset)); 157 strncpy(buf, (memory + offset), sample_remaining); 158 buf[sample_remaining] = '\0'; 159 printf("%s\n", buf); 160 } 161 162 return 0; 163 }