paul@353 | 1 | /* |
paul@402 | 2 | * Test manual mapping of dataspace content. |
paul@353 | 3 | * |
paul@507 | 4 | * Copyright (C) 2020, 2021, 2022, 2023 Paul Boddie <paul@boddie.org.uk> |
paul@353 | 5 | * |
paul@353 | 6 | * This program is free software; you can redistribute it and/or |
paul@353 | 7 | * modify it under the terms of the GNU General Public License as |
paul@353 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@353 | 9 | * the License, or (at your option) any later version. |
paul@353 | 10 | * |
paul@353 | 11 | * This program is distributed in the hope that it will be useful, |
paul@353 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@353 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@353 | 14 | * GNU General Public License for more details. |
paul@353 | 15 | * |
paul@353 | 16 | * You should have received a copy of the GNU General Public License |
paul@353 | 17 | * along with this program; if not, write to the Free Software |
paul@353 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@353 | 19 | * Boston, MA 02110-1301, USA |
paul@353 | 20 | */ |
paul@353 | 21 | |
paul@353 | 22 | #include <l4/re/env.h> |
paul@353 | 23 | #include <l4/sys/err.h> |
paul@353 | 24 | |
paul@507 | 25 | #include <systypes/env.h> |
paul@353 | 26 | #include <systypes/fcntl.h> |
paul@353 | 27 | |
paul@353 | 28 | #include <stdio.h> |
paul@353 | 29 | #include <string.h> |
paul@353 | 30 | #include <stdlib.h> |
paul@353 | 31 | |
paul@353 | 32 | #include <fsclient/file.h> |
paul@353 | 33 | #include <mem/memory_utils.h> |
paul@353 | 34 | |
paul@353 | 35 | #include "dataspace_client.h" |
paul@353 | 36 | |
paul@353 | 37 | |
paul@353 | 38 | |
paul@353 | 39 | int main(int argc, char *argv[]) |
paul@353 | 40 | { |
paul@353 | 41 | if (argc < 4) |
paul@353 | 42 | { |
paul@353 | 43 | printf("Need filename, step and sample size.\n"); |
paul@353 | 44 | return 1; |
paul@353 | 45 | } |
paul@353 | 46 | |
paul@353 | 47 | /* Obtain filename and access parameters. */ |
paul@353 | 48 | |
paul@353 | 49 | char *filename = argv[1]; |
paul@353 | 50 | unsigned long step = atoi(argv[2]); |
paul@353 | 51 | unsigned long sample = atoi(argv[3]); |
paul@353 | 52 | |
paul@353 | 53 | /* Allocate a buffer for sampling from the file. */ |
paul@353 | 54 | |
paul@353 | 55 | char buf[sample + 1]; |
paul@353 | 56 | |
paul@353 | 57 | /* Obtain access to the filesystem. */ |
paul@353 | 58 | |
paul@507 | 59 | l4_cap_idx_t server = l4re_env_get_cap(ENV_FILESYSTEM_SERVER_NAME); |
paul@353 | 60 | |
paul@353 | 61 | /* Invoke the open method to receive the file reference. */ |
paul@353 | 62 | |
paul@353 | 63 | file_t file; |
paul@353 | 64 | long err = file_open(&file, filename, O_RDWR, server); |
paul@353 | 65 | |
paul@353 | 66 | if (err) |
paul@353 | 67 | { |
paul@353 | 68 | printf("Could not obtain file: %s\n", l4sys_errtostr(err)); |
paul@353 | 69 | return 1; |
paul@353 | 70 | } |
paul@353 | 71 | |
paul@357 | 72 | /* A region of the file is mapped but not attached. */ |
paul@353 | 73 | |
paul@357 | 74 | err = file_mmap_only(&file, 0, page(10), 0, 0); |
paul@353 | 75 | |
paul@353 | 76 | if (err) |
paul@353 | 77 | { |
paul@353 | 78 | printf("Could not map file region: %s\n", l4sys_errtostr(err)); |
paul@353 | 79 | return 1; |
paul@353 | 80 | } |
paul@353 | 81 | |
paul@353 | 82 | /* Fix up the file data structure manually. */ |
paul@353 | 83 | |
paul@353 | 84 | file.memory = (char *) 0x2000000; |
paul@353 | 85 | |
paul@353 | 86 | /* Explicitly map the file into this address space, without region mapper |
paul@353 | 87 | usage. The receive window base must be a multiple of its size. */ |
paul@353 | 88 | |
paul@353 | 89 | client_Dataspace dataspace(file.ref); |
paul@353 | 90 | l4_snd_fpage_t region = {0, l4_fpage((l4_addr_t) file.memory, L4_PAGESHIFT + 4, 0)}; |
paul@353 | 91 | |
paul@353 | 92 | printf("region = {%lx, {%lx, %d}}\n", region.snd_base, l4_fpage_memaddr(region.fpage), l4_fpage_size(region.fpage)); |
paul@353 | 93 | |
paul@353 | 94 | err = dataspace.map(0, 0, L4_FPAGE_RO, ®ion); |
paul@353 | 95 | |
paul@353 | 96 | if (err) |
paul@353 | 97 | { |
paul@353 | 98 | printf("Could not invoke map successfully: %s\n", l4sys_errtostr(err)); |
paul@353 | 99 | return 1; |
paul@353 | 100 | } |
paul@353 | 101 | |
paul@353 | 102 | for (unsigned long offset = 0; offset < file_populated_span(&file); offset += step) |
paul@353 | 103 | { |
paul@353 | 104 | unsigned long remaining = file_populated_span(&file) - offset; |
paul@353 | 105 | unsigned long sample_remaining = remaining < sample ? remaining : sample; |
paul@353 | 106 | |
paul@353 | 107 | printf("%ld bytes from %p...\n", sample_remaining, (file.memory + offset)); |
paul@353 | 108 | strncpy(buf, (file.memory + offset), sample_remaining); |
paul@353 | 109 | buf[sample_remaining] = '\0'; |
paul@353 | 110 | printf("%s\n", buf); |
paul@353 | 111 | } |
paul@353 | 112 | |
paul@402 | 113 | printf("End of test.\n"); |
paul@353 | 114 | return 0; |
paul@353 | 115 | } |
paul@353 | 116 | |
paul@353 | 117 | // vim: tabstop=2 expandtab shiftwidth=2 |