| [dd5f703] | 1 | /*
|
|---|
| 2 | * Copyright (c) 2016 Jakub Jermar
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | #include <stdio.h>
|
|---|
| 30 | #include <unistd.h>
|
|---|
| [e755b3f] | 31 | #include <fcntl.h>
|
|---|
| [dd5f703] | 32 | #include <stdlib.h>
|
|---|
| 33 | #include <malloc.h>
|
|---|
| 34 | #include <as.h>
|
|---|
| 35 | #include <ns.h>
|
|---|
| 36 | #include <async.h>
|
|---|
| 37 | #include <errno.h>
|
|---|
| 38 | #include "../tester.h"
|
|---|
| 39 |
|
|---|
| [e755b3f] | 40 | #define TEST_FILE "/tmp/testfile"
|
|---|
| 41 |
|
|---|
| 42 | const char text[] = "Hello world!";
|
|---|
| 43 |
|
|---|
| 44 | int fd;
|
|---|
| 45 |
|
|---|
| [dd5f703] | 46 | static void *create_paged_area(size_t size)
|
|---|
| 47 | {
|
|---|
| [e755b3f] | 48 | TPRINTF("Creating temporary file...\n");
|
|---|
| 49 |
|
|---|
| 50 | fd = open(TEST_FILE, O_CREAT);
|
|---|
| 51 | if (fd < 0)
|
|---|
| 52 | return NULL;
|
|---|
| 53 | (void) unlink(TEST_FILE);
|
|---|
| 54 | if (write(fd, text, sizeof(text)) != sizeof(text)) {
|
|---|
| 55 | close(fd);
|
|---|
| 56 | return NULL;
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| [dd5f703] | 59 | async_sess_t *vfs_pager_sess;
|
|---|
| 60 |
|
|---|
| 61 | TPRINTF("Connecting to VFS pager...\n");
|
|---|
| 62 |
|
|---|
| 63 | vfs_pager_sess = service_connect_blocking(SERVICE_VFS, INTERFACE_PAGER, 0);
|
|---|
| 64 |
|
|---|
| [e755b3f] | 65 | if (!vfs_pager_sess) {
|
|---|
| 66 | close(fd);
|
|---|
| [dd5f703] | 67 | return NULL;
|
|---|
| [e755b3f] | 68 | }
|
|---|
| [dd5f703] | 69 |
|
|---|
| 70 | TPRINTF("Creating AS area...\n");
|
|---|
| 71 |
|
|---|
| 72 | void *result = async_as_area_create(AS_AREA_ANY, size,
|
|---|
| [e755b3f] | 73 | AS_AREA_READ | AS_AREA_CACHEABLE, vfs_pager_sess, fd, 0, 0);
|
|---|
| 74 | if (result == AS_MAP_FAILED) {
|
|---|
| 75 | close(fd);
|
|---|
| [dd5f703] | 76 | return NULL;
|
|---|
| [e755b3f] | 77 | }
|
|---|
| [dd5f703] | 78 |
|
|---|
| 79 | return result;
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | static void touch_area(void *area, size_t size)
|
|---|
| 83 | {
|
|---|
| 84 | TPRINTF("Touching (faulting-in) AS area...\n");
|
|---|
| 85 |
|
|---|
| 86 | volatile char *ptr = (char *) area;
|
|---|
| 87 |
|
|---|
| 88 | char ch;
|
|---|
| 89 | while ((ch = *ptr++))
|
|---|
| 90 | putchar(ch);
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | const char *test_pager1(void)
|
|---|
| 94 | {
|
|---|
| 95 | size_t buffer_len = PAGE_SIZE;
|
|---|
| 96 | void *buffer = create_paged_area(buffer_len);
|
|---|
| [e755b3f] | 97 | if (!buffer)
|
|---|
| [dd5f703] | 98 | return "Cannot allocate memory";
|
|---|
| 99 |
|
|---|
| 100 | touch_area(buffer, buffer_len);
|
|---|
| [e755b3f] | 101 |
|
|---|
| 102 | as_area_destroy(buffer);
|
|---|
| 103 | close(fd);
|
|---|
| [dd5f703] | 104 |
|
|---|
| 105 | return NULL;
|
|---|
| 106 | }
|
|---|