L4Re/departure

Annotated tests/dstest_file_mapping.cc

360:92c5f6aa8c36
2022-06-12 Paul Boddie Reintroduced PagerObject code generation required to initiate servers. mmap-region-flags
paul@353 1
/*
paul@353 2
 * Test dataspace operations.
paul@353 3
 *
paul@353 4
 * Copyright (C) 2020, 2021, 2022 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@353 25
#include <systypes/fcntl.h>
paul@353 26
paul@353 27
#include <stdio.h>
paul@353 28
#include <string.h>
paul@353 29
#include <stdlib.h>
paul@353 30
paul@353 31
#include <fsclient/file.h>
paul@353 32
#include <mem/memory_utils.h>
paul@353 33
paul@353 34
#include "dataspace_client.h"
paul@353 35
paul@353 36
paul@353 37
paul@353 38
int main(int argc, char *argv[])
paul@353 39
{
paul@353 40
  if (argc < 4)
paul@353 41
  {
paul@353 42
    printf("Need filename, step and sample size.\n");
paul@353 43
    return 1;
paul@353 44
  }
paul@353 45
paul@353 46
  /* Obtain filename and access parameters. */
paul@353 47
paul@353 48
  char *filename = argv[1];
paul@353 49
  unsigned long step = atoi(argv[2]);
paul@353 50
  unsigned long sample = atoi(argv[3]);
paul@353 51
paul@353 52
  /* Allocate a buffer for sampling from the file. */
paul@353 53
paul@353 54
  char buf[sample + 1];
paul@353 55
paul@353 56
  /* Obtain access to the filesystem. */
paul@353 57
paul@353 58
  l4_cap_idx_t server = l4re_env_get_cap("server");
paul@353 59
paul@353 60
  /* Invoke the open method to receive the file reference. */
paul@353 61
paul@353 62
  file_t file;
paul@353 63
  long err = file_open(&file, filename, O_RDWR, server);
paul@353 64
paul@353 65
  if (err)
paul@353 66
  {
paul@353 67
    printf("Could not obtain file: %s\n", l4sys_errtostr(err));
paul@353 68
    return 1;
paul@353 69
  }
paul@353 70
paul@357 71
  /* A region of the file is mapped but not attached. */
paul@353 72
paul@357 73
  err = file_mmap_only(&file, 0, page(10), 0, 0);
paul@353 74
paul@353 75
  if (err)
paul@353 76
  {
paul@353 77
    printf("Could not map file region: %s\n", l4sys_errtostr(err));
paul@353 78
    return 1;
paul@353 79
  }
paul@353 80
paul@353 81
  /* Fix up the file data structure manually. */
paul@353 82
paul@353 83
  file.memory = (char *) 0x2000000;
paul@353 84
paul@353 85
  /* Explicitly map the file into this address space, without region mapper
paul@353 86
     usage. The receive window base must be a multiple of its size. */
paul@353 87
paul@353 88
  client_Dataspace dataspace(file.ref);
paul@353 89
  l4_snd_fpage_t region = {0, l4_fpage((l4_addr_t) file.memory, L4_PAGESHIFT + 4, 0)};
paul@353 90
paul@353 91
  printf("region = {%lx, {%lx, %d}}\n", region.snd_base, l4_fpage_memaddr(region.fpage), l4_fpage_size(region.fpage));
paul@353 92
paul@353 93
  err = dataspace.map(0, 0, L4_FPAGE_RO, &region);
paul@353 94
paul@353 95
  if (err)
paul@353 96
  {
paul@353 97
    printf("Could not invoke map successfully: %s\n", l4sys_errtostr(err));
paul@353 98
    return 1;
paul@353 99
  }
paul@353 100
paul@353 101
  for (unsigned long offset = 0; offset < file_populated_span(&file); offset += step)
paul@353 102
  {
paul@353 103
    unsigned long remaining = file_populated_span(&file) - offset;
paul@353 104
    unsigned long sample_remaining = remaining < sample ? remaining : sample;
paul@353 105
paul@353 106
    printf("%ld bytes from %p...\n", sample_remaining, (file.memory + offset));
paul@353 107
    strncpy(buf, (file.memory + offset), sample_remaining);
paul@353 108
    buf[sample_remaining] = '\0';
paul@353 109
    printf("%s\n", buf);
paul@353 110
  }
paul@353 111
paul@353 112
  printf("File shown.\n");
paul@353 113
paul@353 114
  return 0;
paul@353 115
}
paul@353 116
paul@353 117
// vim: tabstop=2 expandtab shiftwidth=2