paul@0 | 1 | /* |
paul@0 | 2 | * Test dataspace operations. |
paul@0 | 3 | * |
paul@0 | 4 | * Copyright (C) 2020, 2021 Paul Boddie <paul@boddie.org.uk> |
paul@0 | 5 | * |
paul@0 | 6 | * This program is free software; you can redistribute it and/or |
paul@0 | 7 | * modify it under the terms of the GNU General Public License as |
paul@0 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@0 | 9 | * the License, or (at your option) any later version. |
paul@0 | 10 | * |
paul@0 | 11 | * This program is distributed in the hope that it will be useful, |
paul@0 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@0 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@0 | 14 | * GNU General Public License for more details. |
paul@0 | 15 | * |
paul@0 | 16 | * You should have received a copy of the GNU General Public License |
paul@0 | 17 | * along with this program; if not, write to the Free Software |
paul@0 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@0 | 19 | * Boston, MA 02110-1301, USA |
paul@0 | 20 | */ |
paul@0 | 21 | |
paul@0 | 22 | #include <l4/re/env.h> |
paul@0 | 23 | #include <l4/sys/err.h> |
paul@0 | 24 | |
paul@0 | 25 | #include <stdio.h> |
paul@0 | 26 | #include <string.h> |
paul@0 | 27 | #include <stdlib.h> |
paul@0 | 28 | |
paul@0 | 29 | #include <ipc/mem_ipc.h> |
paul@0 | 30 | |
paul@10 | 31 | #include "dataspace_client.h" |
paul@10 | 32 | #include "opener_client.h" |
paul@10 | 33 | #include "opener_context_client.h" |
paul@7 | 34 | #include "mapped_file_client.h" |
paul@7 | 35 | #include "memory_utils.h" |
paul@7 | 36 | |
paul@0 | 37 | |
paul@0 | 38 | |
paul@12 | 39 | int main(int argc, char *argv[]) |
paul@0 | 40 | { |
paul@12 | 41 | if (argc < 4) |
paul@12 | 42 | { |
paul@12 | 43 | printf("Need filename, step and sample size.\n"); |
paul@12 | 44 | return 1; |
paul@12 | 45 | } |
paul@12 | 46 | |
paul@12 | 47 | /* Obtain filename and access parameters. */ |
paul@12 | 48 | |
paul@12 | 49 | char *filename = argv[1]; |
paul@12 | 50 | unsigned long step = atoi(argv[2]); |
paul@12 | 51 | unsigned long sample = atoi(argv[3]); |
paul@12 | 52 | |
paul@12 | 53 | /* Allocate a buffer for sampling from the file. */ |
paul@12 | 54 | |
paul@12 | 55 | char buf[sample + 1]; |
paul@12 | 56 | |
paul@12 | 57 | /* Obtain access to the filesystem. */ |
paul@12 | 58 | |
paul@0 | 59 | l4_cap_idx_t server = l4re_env_get_cap("server"); |
paul@10 | 60 | l4_cap_idx_t context_ref; |
paul@10 | 61 | client_Opener opener(server); |
paul@0 | 62 | |
paul@10 | 63 | long err = opener.context(&context_ref); |
paul@10 | 64 | |
paul@10 | 65 | if (err) |
paul@10 | 66 | { |
paul@10 | 67 | printf("Could not obtain context: %s\n", l4sys_errtostr(err)); |
paul@10 | 68 | return 1; |
paul@10 | 69 | } |
paul@10 | 70 | |
paul@10 | 71 | client_OpenerContext context(context_ref); |
paul@10 | 72 | client_Dataspace context_ds(context_ref); |
paul@10 | 73 | unsigned long size, flags; |
paul@10 | 74 | |
paul@10 | 75 | err = context_ds.info(&size, &flags); |
paul@10 | 76 | |
paul@10 | 77 | if (err) |
paul@10 | 78 | { |
paul@10 | 79 | printf("Could not obtain context info: %s\n", l4sys_errtostr(err)); |
paul@10 | 80 | return 1; |
paul@10 | 81 | } |
paul@10 | 82 | |
paul@10 | 83 | /* Map context memory to write the filename. */ |
paul@10 | 84 | |
paul@10 | 85 | char *memory; |
paul@10 | 86 | |
paul@10 | 87 | printf("Attach region of size %ld...\n", size); |
paul@10 | 88 | |
paul@10 | 89 | err = ipc_attach_dataspace(context_ref, size, (void **) &memory); |
paul@10 | 90 | |
paul@10 | 91 | if (err) |
paul@10 | 92 | { |
paul@10 | 93 | printf("Could not map memory: %s\n", l4sys_errtostr(err)); |
paul@10 | 94 | return 1; |
paul@10 | 95 | } |
paul@10 | 96 | |
paul@10 | 97 | printf("Mapped memory at %p\n", memory); |
paul@10 | 98 | |
paul@10 | 99 | /* Write the filename. */ |
paul@10 | 100 | |
paul@12 | 101 | strcpy(memory, filename); |
paul@10 | 102 | |
paul@10 | 103 | /* Invoke the open method to receive the file reference. */ |
paul@10 | 104 | |
paul@10 | 105 | l4_cap_idx_t file; |
paul@10 | 106 | |
paul@10 | 107 | err = context.open(L4_FPAGE_RW, &size, &file); |
paul@10 | 108 | |
paul@10 | 109 | if (err) |
paul@10 | 110 | { |
paul@10 | 111 | printf("Could not obtain file: %s\n", l4sys_errtostr(err)); |
paul@10 | 112 | return 1; |
paul@10 | 113 | } |
paul@10 | 114 | |
paul@10 | 115 | client_MappedFile obj(file); |
paul@7 | 116 | |
paul@0 | 117 | /* Some memory to be mapped. */ |
paul@0 | 118 | |
paul@7 | 119 | size_t start_pos, end_pos, data_end; |
paul@7 | 120 | |
paul@7 | 121 | printf("Map region from %ld to %ld...\n", 0L, page(10)); |
paul@7 | 122 | |
paul@10 | 123 | err = obj.mmap(0, page(10), &start_pos, &end_pos, &data_end); |
paul@7 | 124 | |
paul@7 | 125 | if (err) |
paul@7 | 126 | { |
paul@7 | 127 | printf("Could not map file region: %s\n", l4sys_errtostr(err)); |
paul@7 | 128 | return 1; |
paul@7 | 129 | } |
paul@7 | 130 | |
paul@12 | 131 | printf("Mapped region from %ld to %ld with content %ld.\n", |
paul@12 | 132 | start_pos, end_pos, data_end); |
paul@12 | 133 | |
paul@10 | 134 | size = end_pos - start_pos; |
paul@7 | 135 | |
paul@7 | 136 | printf("Attach region of size %ld...\n", size); |
paul@7 | 137 | |
paul@10 | 138 | err = ipc_attach_dataspace(file, size, (void **) &memory); |
paul@0 | 139 | |
paul@0 | 140 | if (err) |
paul@0 | 141 | { |
paul@0 | 142 | printf("Could not map memory: %s\n", l4sys_errtostr(err)); |
paul@0 | 143 | return 1; |
paul@0 | 144 | } |
paul@0 | 145 | |
paul@0 | 146 | printf("Mapped memory at %p\n", memory); |
paul@0 | 147 | |
paul@12 | 148 | if (data_end < size) |
paul@12 | 149 | size = data_end; |
paul@12 | 150 | |
paul@12 | 151 | for (unsigned long offset = 0; offset < size; offset += step) |
paul@0 | 152 | { |
paul@12 | 153 | unsigned long remaining = size - offset; |
paul@12 | 154 | unsigned long sample_remaining = remaining < sample ? remaining : sample; |
paul@12 | 155 | |
paul@12 | 156 | printf("%ld bytes from %p...\n", sample_remaining, (memory + offset)); |
paul@12 | 157 | strncpy(buf, (memory + offset), sample_remaining); |
paul@12 | 158 | buf[sample_remaining] = '\0'; |
paul@7 | 159 | printf("%s\n", buf); |
paul@0 | 160 | } |
paul@0 | 161 | |
paul@0 | 162 | return 0; |
paul@0 | 163 | } |