L4Re/departure

Annotated dstest_client.cc

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