1 /* 2 * Test manual mapping of dataspace content. 3 * 4 * Copyright (C) 2020, 2021, 2022, 2023 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 <systypes/env.h> 26 #include <systypes/fcntl.h> 27 28 #include <stdio.h> 29 #include <string.h> 30 #include <stdlib.h> 31 32 #include <fsclient/file.h> 33 #include <mem/memory_utils.h> 34 35 #include "dataspace_client.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(ENV_FILESYSTEM_SERVER_NAME); 60 61 /* Invoke the open method to receive the file reference. */ 62 63 file_t file; 64 long err = file_open(&file, filename, O_RDWR, server); 65 66 if (err) 67 { 68 printf("Could not obtain file: %s\n", l4sys_errtostr(err)); 69 return 1; 70 } 71 72 /* A region of the file is mapped but not attached. */ 73 74 err = file_mmap_only(&file, 0, page(10), 0, 0); 75 76 if (err) 77 { 78 printf("Could not map file region: %s\n", l4sys_errtostr(err)); 79 return 1; 80 } 81 82 /* Fix up the file data structure manually. */ 83 84 file.memory = (char *) 0x2000000; 85 86 /* Explicitly map the file into this address space, without region mapper 87 usage. The receive window base must be a multiple of its size. */ 88 89 client_Dataspace dataspace(file.ref); 90 l4_snd_fpage_t region = {0, l4_fpage((l4_addr_t) file.memory, L4_PAGESHIFT + 4, 0)}; 91 92 printf("region = {%lx, {%lx, %d}}\n", region.snd_base, l4_fpage_memaddr(region.fpage), l4_fpage_size(region.fpage)); 93 94 err = dataspace.map(0, 0, L4_FPAGE_RO, ®ion); 95 96 if (err) 97 { 98 printf("Could not invoke map successfully: %s\n", l4sys_errtostr(err)); 99 return 1; 100 } 101 102 for (unsigned long offset = 0; offset < file_populated_span(&file); offset += step) 103 { 104 unsigned long remaining = file_populated_span(&file) - offset; 105 unsigned long sample_remaining = remaining < sample ? remaining : sample; 106 107 printf("%ld bytes from %p...\n", sample_remaining, (file.memory + offset)); 108 strncpy(buf, (file.memory + offset), sample_remaining); 109 buf[sample_remaining] = '\0'; 110 printf("%s\n", buf); 111 } 112 113 printf("End of test.\n"); 114 return 0; 115 } 116 117 // vim: tabstop=2 expandtab shiftwidth=2