paul@18 | 1 | /* |
paul@18 | 2 | * Test dataspace operations. |
paul@18 | 3 | * |
paul@18 | 4 | * Copyright (C) 2020, 2021 Paul Boddie <paul@boddie.org.uk> |
paul@18 | 5 | * |
paul@18 | 6 | * This program is free software; you can redistribute it and/or |
paul@18 | 7 | * modify it under the terms of the GNU General Public License as |
paul@18 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@18 | 9 | * the License, or (at your option) any later version. |
paul@18 | 10 | * |
paul@18 | 11 | * This program is distributed in the hope that it will be useful, |
paul@18 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@18 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@18 | 14 | * GNU General Public License for more details. |
paul@18 | 15 | * |
paul@18 | 16 | * You should have received a copy of the GNU General Public License |
paul@18 | 17 | * along with this program; if not, write to the Free Software |
paul@18 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@18 | 19 | * Boston, MA 02110-1301, USA |
paul@18 | 20 | */ |
paul@18 | 21 | |
paul@18 | 22 | #include <l4/re/env.h> |
paul@18 | 23 | #include <l4/sys/err.h> |
paul@18 | 24 | |
paul@18 | 25 | #include <stdio.h> |
paul@18 | 26 | #include <string.h> |
paul@18 | 27 | #include <stdlib.h> |
paul@18 | 28 | |
paul@18 | 29 | #include <thread> |
paul@18 | 30 | |
paul@18 | 31 | #include <ipc/cap_alloc.h> |
paul@18 | 32 | #include <ipc/mem_ipc.h> |
paul@29 | 33 | #include <ipc/thread.h> |
paul@18 | 34 | |
paul@18 | 35 | #include "dataspace_client.h" |
paul@18 | 36 | #include "opener_client.h" |
paul@18 | 37 | #include "opener_context_client.h" |
paul@18 | 38 | #include "mapped_file_client.h" |
paul@18 | 39 | #include "memory_utils.h" |
paul@18 | 40 | |
paul@18 | 41 | |
paul@18 | 42 | |
paul@21 | 43 | /* Test parameters affected by capability limits. */ |
paul@21 | 44 | |
paul@18 | 45 | const unsigned long NUMBER_OF_FILES = 10; |
paul@18 | 46 | const unsigned int START_LIMIT = 10; |
paul@21 | 47 | const unsigned int ACTIVITY_ITERATIONS = 20; |
paul@21 | 48 | |
paul@21 | 49 | /* Test parameters unaffected by any capability limits. */ |
paul@21 | 50 | |
paul@18 | 51 | const unsigned int MAP_PAGES = 20; |
paul@21 | 52 | const unsigned int REGION_ITERATIONS = 10; |
paul@18 | 53 | |
paul@18 | 54 | |
paul@18 | 55 | |
paul@30 | 56 | typedef struct |
paul@30 | 57 | { |
paul@30 | 58 | l4_cap_idx_t ref; |
paul@30 | 59 | char *memory; |
paul@30 | 60 | offset_t start_pos, end_pos, data_end, size; |
paul@30 | 61 | } file_t; |
paul@30 | 62 | |
paul@30 | 63 | |
paul@30 | 64 | |
paul@30 | 65 | static offset_t file_span(file_t *file) |
paul@30 | 66 | { |
paul@30 | 67 | offset_t size = file->end_pos - file->start_pos; |
paul@30 | 68 | |
paul@30 | 69 | return (file->data_end < size) ? file->data_end : size; |
paul@30 | 70 | } |
paul@30 | 71 | |
paul@30 | 72 | static void file_init(file_t *file) |
paul@30 | 73 | { |
paul@30 | 74 | file->memory = NULL; |
paul@30 | 75 | file->ref = L4_INVALID_CAP; |
paul@30 | 76 | } |
paul@30 | 77 | |
paul@30 | 78 | static long file_open(file_t *file, l4_cap_idx_t context_ref) |
paul@30 | 79 | { |
paul@30 | 80 | client_OpenerContext context(context_ref); |
paul@30 | 81 | file_init(file); |
paul@30 | 82 | return context.open(L4_FPAGE_RW, &file->size, &file->ref); |
paul@30 | 83 | } |
paul@30 | 84 | |
paul@30 | 85 | static void file_close(file_t *file) |
paul@30 | 86 | { |
paul@30 | 87 | if (l4_is_valid_cap(file->ref)) |
paul@30 | 88 | ipc_cap_free_um(file->ref); |
paul@30 | 89 | |
paul@30 | 90 | if (file->memory != NULL) |
paul@30 | 91 | ipc_detach_dataspace(file->memory); |
paul@30 | 92 | |
paul@30 | 93 | file_init(file); |
paul@30 | 94 | } |
paul@30 | 95 | |
paul@30 | 96 | static long file_mmap(file_t *file, offset_t position, offset_t length) |
paul@30 | 97 | { |
paul@30 | 98 | client_MappedFile mapped_file(file->ref); |
paul@30 | 99 | long err = mapped_file.mmap(position, length, &file->start_pos, &file->end_pos, &file->data_end); |
paul@30 | 100 | |
paul@30 | 101 | if (err) |
paul@30 | 102 | return err; |
paul@30 | 103 | |
paul@30 | 104 | return ipc_attach_dataspace(file->ref, file->end_pos - file->start_pos, (void **) &file->memory); |
paul@30 | 105 | } |
paul@30 | 106 | |
paul@30 | 107 | static long context_open(file_t *file, l4_cap_idx_t server) |
paul@30 | 108 | { |
paul@30 | 109 | client_Opener opener(server); |
paul@30 | 110 | unsigned long size, flags; |
paul@30 | 111 | long err; |
paul@30 | 112 | |
paul@30 | 113 | file_init(file); |
paul@30 | 114 | err = opener.context(&file->ref); |
paul@30 | 115 | |
paul@30 | 116 | if (err) |
paul@30 | 117 | return err; |
paul@30 | 118 | |
paul@30 | 119 | client_Dataspace context_ds(file->ref); |
paul@30 | 120 | |
paul@30 | 121 | err = context_ds.info(&size, &flags); |
paul@30 | 122 | |
paul@30 | 123 | if (err) |
paul@30 | 124 | return err; |
paul@30 | 125 | |
paul@30 | 126 | return ipc_attach_dataspace(file->ref, size, (void **) &file->memory); |
paul@30 | 127 | } |
paul@30 | 128 | |
paul@30 | 129 | |
paul@30 | 130 | |
paul@21 | 131 | /* An activity opening and reading from a file. */ |
paul@18 | 132 | |
paul@18 | 133 | static long activity(l4_cap_idx_t context_ref, unsigned long fileid, unsigned int start_page) |
paul@18 | 134 | { |
paul@18 | 135 | unsigned long step = page(1); |
paul@18 | 136 | unsigned long sample = page(1); |
paul@18 | 137 | |
paul@18 | 138 | /* Allocate a buffer for sampling from the file. */ |
paul@18 | 139 | |
paul@18 | 140 | char buf[sample + 1]; |
paul@18 | 141 | |
paul@18 | 142 | /* Invoke the open method to receive the file reference. */ |
paul@18 | 143 | |
paul@30 | 144 | file_t file; |
paul@18 | 145 | |
paul@30 | 146 | long err = file_open(&file, context_ref); |
paul@18 | 147 | |
paul@18 | 148 | if (err) |
paul@18 | 149 | { |
paul@18 | 150 | printf("Could not obtain file for %ld @ page %d: %s\n", fileid, start_page, l4sys_errtostr(err)); |
paul@18 | 151 | return err; |
paul@18 | 152 | } |
paul@18 | 153 | |
paul@18 | 154 | /* Some memory to be mapped. */ |
paul@18 | 155 | |
paul@30 | 156 | err = file_mmap(&file, page(start_page), page(MAP_PAGES)); |
paul@18 | 157 | |
paul@18 | 158 | if (err) |
paul@18 | 159 | { |
paul@18 | 160 | printf("Could not map file region for %ld @ page %d: %s\n", fileid, start_page, l4sys_errtostr(err)); |
paul@30 | 161 | file_close(&file); |
paul@18 | 162 | return err; |
paul@18 | 163 | } |
paul@18 | 164 | |
paul@21 | 165 | /* Read the region a number of times. */ |
paul@18 | 166 | |
paul@21 | 167 | for (unsigned int read_counter = 0; read_counter < REGION_ITERATIONS; read_counter++) |
paul@21 | 168 | { |
paul@30 | 169 | for (unsigned long offset = 0; offset < file_span(&file); offset += step) |
paul@21 | 170 | { |
paul@30 | 171 | unsigned long remaining = file_span(&file) - offset; |
paul@21 | 172 | unsigned long sample_remaining = remaining < sample ? remaining : sample; |
paul@18 | 173 | |
paul@30 | 174 | strncpy(buf, (file.memory + offset), sample_remaining); |
paul@21 | 175 | buf[sample_remaining] = '\0'; |
paul@21 | 176 | |
paul@21 | 177 | /* Test the data obtained. */ |
paul@18 | 178 | |
paul@30 | 179 | unsigned long filepos = file.start_pos + offset; |
paul@21 | 180 | unsigned long _fileid = 0, _filepos = 0; |
paul@21 | 181 | char *sep = strchr(buf, ':'); |
paul@18 | 182 | |
paul@21 | 183 | if (sep != NULL) |
paul@21 | 184 | { |
paul@21 | 185 | *sep = '\0'; sep++; |
paul@21 | 186 | _fileid = atol(buf); _filepos = atol(sep); |
paul@21 | 187 | } |
paul@21 | 188 | |
paul@21 | 189 | if ((fileid != _fileid) || (filepos != _filepos)) |
paul@21 | 190 | printf("! %ld:%ld is not %ld:%ld\n", _fileid, _filepos, fileid, filepos); |
paul@18 | 191 | } |
paul@18 | 192 | } |
paul@18 | 193 | |
paul@30 | 194 | file_close(&file); |
paul@18 | 195 | |
paul@18 | 196 | return L4_EOK; |
paul@18 | 197 | } |
paul@18 | 198 | |
paul@18 | 199 | |
paul@18 | 200 | |
paul@30 | 201 | static long activity_iterate(file_t *file, unsigned long fileid, unsigned int start_page) |
paul@18 | 202 | { |
paul@18 | 203 | long err; |
paul@18 | 204 | |
paul@18 | 205 | /* Open the file, read pages, close the file, over and over. */ |
paul@18 | 206 | |
paul@18 | 207 | for (unsigned int iteration = 0; iteration < ACTIVITY_ITERATIONS; iteration++) |
paul@18 | 208 | { |
paul@30 | 209 | err = activity(file->ref, fileid, start_page); |
paul@18 | 210 | if (err) |
paul@18 | 211 | break; |
paul@18 | 212 | } |
paul@18 | 213 | |
paul@18 | 214 | return err; |
paul@18 | 215 | } |
paul@18 | 216 | |
paul@18 | 217 | |
paul@18 | 218 | |
paul@30 | 219 | static long context_for_file(unsigned long fileid, file_t *file, l4_cap_idx_t server) |
paul@18 | 220 | { |
paul@30 | 221 | long err = context_open(file, server); |
paul@18 | 222 | |
paul@18 | 223 | if (err) |
paul@18 | 224 | { |
paul@18 | 225 | printf("Could not obtain context: %s\n", l4sys_errtostr(err)); |
paul@30 | 226 | file_close(file); |
paul@18 | 227 | return err; |
paul@18 | 228 | } |
paul@18 | 229 | |
paul@18 | 230 | /* Write the filename. */ |
paul@18 | 231 | |
paul@30 | 232 | sprintf(file->memory, "%ld", fileid); |
paul@18 | 233 | |
paul@18 | 234 | return L4_EOK; |
paul@18 | 235 | } |
paul@18 | 236 | |
paul@18 | 237 | |
paul@18 | 238 | |
paul@18 | 239 | int main(void) |
paul@18 | 240 | { |
paul@18 | 241 | /* Introduce concurrency control. */ |
paul@18 | 242 | |
paul@18 | 243 | ipc_cap_alloc_init(); |
paul@29 | 244 | ipc_mem_init(); |
paul@18 | 245 | |
paul@30 | 246 | /* Retain activity and context details. */ |
paul@30 | 247 | |
paul@28 | 248 | std::thread *activities[NUMBER_OF_FILES * START_LIMIT]; |
paul@30 | 249 | file_t contexts[NUMBER_OF_FILES]; |
paul@30 | 250 | |
paul@30 | 251 | /* Obtain access to the filesystem. */ |
paul@30 | 252 | |
paul@30 | 253 | l4_cap_idx_t server = l4re_env_get_cap("server"); |
paul@28 | 254 | |
paul@28 | 255 | /* Obtain opener contexts for the files. */ |
paul@18 | 256 | |
paul@18 | 257 | unsigned long fileid; |
paul@28 | 258 | |
paul@28 | 259 | for (fileid = 0; fileid < NUMBER_OF_FILES; fileid++) |
paul@28 | 260 | { |
paul@30 | 261 | long err = context_for_file(fileid, &contexts[fileid], server); |
paul@28 | 262 | |
paul@28 | 263 | if (err) |
paul@28 | 264 | { |
paul@28 | 265 | printf("Context allocation failed.\n"); |
paul@28 | 266 | return 1; |
paul@28 | 267 | } |
paul@28 | 268 | } |
paul@28 | 269 | |
paul@28 | 270 | /* Start threads accessing all the files. */ |
paul@28 | 271 | |
paul@18 | 272 | int current = 0; |
paul@18 | 273 | |
paul@18 | 274 | for (fileid = 0; fileid < NUMBER_OF_FILES; fileid++) |
paul@28 | 275 | for (unsigned int start_page = 0; start_page < START_LIMIT; start_page++) |
paul@30 | 276 | activities[current++] = new std::thread(activity_iterate, &contexts[fileid], fileid, start_page); |
paul@18 | 277 | |
paul@18 | 278 | /* Wait for the threads. */ |
paul@18 | 279 | |
paul@18 | 280 | int limit = current; |
paul@18 | 281 | |
paul@18 | 282 | printf("Waiting for %d threads...\n", limit); |
paul@18 | 283 | |
paul@18 | 284 | for (current = 0; current < limit; current++) |
paul@28 | 285 | activities[current]->join(); |
paul@28 | 286 | |
paul@28 | 287 | /* Discard the contexts. */ |
paul@28 | 288 | |
paul@28 | 289 | for (fileid = 0; fileid < NUMBER_OF_FILES; fileid++) |
paul@30 | 290 | file_close(&contexts[fileid]); |
paul@18 | 291 | |
paul@18 | 292 | printf("Activities completed.\n"); |
paul@18 | 293 | |
paul@18 | 294 | return 0; |
paul@18 | 295 | } |