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