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 <ipc/mem_ipc.h> 30 31 #include "mapped_file_client.h" 32 #include "memory_utils.h" 33 34 35 36 int main(void) 37 { 38 l4_cap_idx_t server = l4re_env_get_cap("server"); 39 40 client_MappedFile obj(server); 41 42 /* Some memory to be mapped. */ 43 44 size_t start_pos, end_pos, data_end; 45 46 printf("Map region from %ld to %ld...\n", 0L, page(10)); 47 48 long err = obj.mmap(0, page(10), &start_pos, &end_pos, &data_end); 49 50 if (err) 51 { 52 printf("Could not map file region: %s\n", l4sys_errtostr(err)); 53 return 1; 54 } 55 56 char *memory; 57 unsigned long size = end_pos - start_pos; 58 59 printf("Attach region of size %ld...\n", size); 60 61 err = ipc_attach_dataspace(server, size, (void **) &memory); 62 63 if (err) 64 { 65 printf("Could not map memory: %s\n", l4sys_errtostr(err)); 66 return 1; 67 } 68 69 printf("Mapped memory at %p\n", memory); 70 71 for (unsigned long offset = 0; offset < size; offset += 1024) 72 { 73 char buf[11]; 74 printf("10 bytes from %p...\n", (memory + offset)); 75 strncpy(buf, (memory + offset), 10); 76 buf[10] = '\0'; 77 printf("%s\n", buf); 78 } 79 80 return 0; 81 }