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 <thread> 30 31 #include <ipc/cap_alloc.h> 32 #include <ipc/mem_ipc.h> 33 34 #include "dataspace_client.h" 35 #include "opener_client.h" 36 #include "opener_context_client.h" 37 #include "mapped_file_client.h" 38 #include "memory_utils.h" 39 40 41 42 /* Test parameters affected by capability limits. */ 43 44 const unsigned long NUMBER_OF_FILES = 10; 45 const unsigned int START_LIMIT = 10; 46 const unsigned int ACTIVITY_ITERATIONS = 20; 47 48 /* Test parameters unaffected by any capability limits. */ 49 50 const unsigned int MAP_PAGES = 20; 51 const unsigned int REGION_ITERATIONS = 10; 52 53 54 55 /* An activity opening and reading from a file. */ 56 57 static long activity(l4_cap_idx_t context_ref, unsigned long fileid, unsigned int start_page) 58 { 59 client_OpenerContext context(context_ref); 60 unsigned long step = page(1); 61 unsigned long sample = page(1); 62 63 /* Allocate a buffer for sampling from the file. */ 64 65 char buf[sample + 1]; 66 67 /* Invoke the open method to receive the file reference. */ 68 69 unsigned long size; 70 l4_cap_idx_t file_ref; 71 72 long err = context.open(L4_FPAGE_RW, &size, &file_ref); 73 74 if (err) 75 { 76 printf("Could not obtain file for %ld @ page %d: %s\n", fileid, start_page, l4sys_errtostr(err)); 77 return err; 78 } 79 80 client_MappedFile file(file_ref); 81 82 /* Some memory to be mapped. */ 83 84 size_t start_pos, end_pos, data_end; 85 86 err = file.mmap(page(start_page), page(MAP_PAGES), &start_pos, &end_pos, &data_end); 87 88 if (err) 89 { 90 printf("Could not map file region for %ld @ page %d: %s\n", fileid, start_page, l4sys_errtostr(err)); 91 ipc_cap_free_um(file_ref); 92 return err; 93 } 94 95 size = end_pos - start_pos; 96 97 char *memory; 98 99 err = ipc_attach_dataspace(file_ref, size, (void **) &memory); 100 101 if (err) 102 { 103 printf("Could not map memory for %ld @ page %d: %s\n", fileid, start_page, l4sys_errtostr(err)); 104 ipc_cap_free_um(file_ref); 105 return err; 106 } 107 108 if (data_end < size) 109 size = data_end; 110 111 /* Read the region a number of times. */ 112 113 for (unsigned int read_counter = 0; read_counter < REGION_ITERATIONS; read_counter++) 114 { 115 for (unsigned long offset = 0; offset < size; offset += step) 116 { 117 unsigned long remaining = size - offset; 118 unsigned long sample_remaining = remaining < sample ? remaining : sample; 119 120 strncpy(buf, (memory + offset), sample_remaining); 121 buf[sample_remaining] = '\0'; 122 123 /* Test the data obtained. */ 124 125 unsigned long filepos = start_pos + offset; 126 unsigned long _fileid = 0, _filepos = 0; 127 char *sep = strchr(buf, ':'); 128 129 if (sep != NULL) 130 { 131 *sep = '\0'; sep++; 132 _fileid = atol(buf); _filepos = atol(sep); 133 } 134 135 if ((fileid != _fileid) || (filepos != _filepos)) 136 printf("! %ld:%ld is not %ld:%ld\n", _fileid, _filepos, fileid, filepos); 137 } 138 } 139 140 ipc_cap_free_um(file_ref); 141 ipc_detach_dataspace(memory); 142 143 return L4_EOK; 144 } 145 146 147 148 static long activity_iterate(l4_cap_idx_t context_ref, unsigned long fileid, unsigned int start_page) 149 { 150 long err; 151 152 /* Open the file, read pages, close the file, over and over. */ 153 154 for (unsigned int iteration = 0; iteration < ACTIVITY_ITERATIONS; iteration++) 155 { 156 err = activity(context_ref, fileid, start_page); 157 if (err) 158 break; 159 } 160 161 return err; 162 } 163 164 165 166 static long context_for_file(unsigned long fileid, l4_cap_idx_t *context_ref, char **filename) 167 { 168 /* Obtain access to the filesystem. */ 169 170 l4_cap_idx_t server = l4re_env_get_cap("server"); 171 client_Opener opener(server); 172 173 long err = opener.context(context_ref); 174 175 if (err) 176 { 177 printf("Could not obtain context: %s\n", l4sys_errtostr(err)); 178 return err; 179 } 180 181 client_Dataspace context_ds(*context_ref); 182 unsigned long size, flags; 183 184 err = context_ds.info(&size, &flags); 185 186 if (err) 187 { 188 printf("Could not obtain context info: %s\n", l4sys_errtostr(err)); 189 ipc_cap_free_um(*context_ref); 190 return err; 191 } 192 193 /* Map context memory to write the filename. */ 194 195 err = ipc_attach_dataspace(*context_ref, size, (void **) filename); 196 197 if (err) 198 { 199 printf("Could not map memory: %s\n", l4sys_errtostr(err)); 200 ipc_cap_free_um(*context_ref); 201 return err; 202 } 203 204 /* Write the filename. */ 205 206 sprintf(*filename, "%ld", fileid); 207 208 return L4_EOK; 209 } 210 211 212 213 int main(void) 214 { 215 /* Introduce concurrency control. */ 216 217 ipc_cap_alloc_init(); 218 219 std::thread *activities[NUMBER_OF_FILES * START_LIMIT]; 220 l4_cap_idx_t context_refs[NUMBER_OF_FILES]; 221 char *filenames[NUMBER_OF_FILES]; 222 223 /* Obtain opener contexts for the files. */ 224 225 unsigned long fileid; 226 227 for (fileid = 0; fileid < NUMBER_OF_FILES; fileid++) 228 { 229 long err = context_for_file(fileid, &context_refs[fileid], &filenames[fileid]); 230 231 if (err) 232 { 233 printf("Context allocation failed.\n"); 234 return 1; 235 } 236 } 237 238 /* Start threads accessing all the files. */ 239 240 int current = 0; 241 242 for (fileid = 0; fileid < NUMBER_OF_FILES; fileid++) 243 for (unsigned int start_page = 0; start_page < START_LIMIT; start_page++) 244 activities[current++] = new std::thread(activity_iterate, context_refs[fileid], fileid, start_page); 245 246 /* Wait for the threads. */ 247 248 int limit = current; 249 250 printf("Waiting for %d threads...\n", limit); 251 252 for (current = 0; current < limit; current++) 253 activities[current]->join(); 254 255 /* Discard the contexts. */ 256 257 for (fileid = 0; fileid < NUMBER_OF_FILES; fileid++) 258 { 259 ipc_cap_free_um(context_refs[fileid]); 260 ipc_detach_dataspace(filenames[fileid]); 261 } 262 263 printf("Activities completed.\n"); 264 265 return 0; 266 }