| [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>
 | 
|---|
| [79ea5af] | 30 | #include <vfs/vfs.h>
 | 
|---|
| [dd5f703] | 31 | #include <stdlib.h>
 | 
|---|
 | 32 | #include <as.h>
 | 
|---|
 | 33 | #include <ns.h>
 | 
|---|
 | 34 | #include <async.h>
 | 
|---|
 | 35 | #include <errno.h>
 | 
|---|
 | 36 | #include "../tester.h"
 | 
|---|
 | 37 | 
 | 
|---|
| [e755b3f] | 38 | #define TEST_FILE       "/tmp/testfile"
 | 
|---|
 | 39 | 
 | 
|---|
 | 40 | const char text[] = "Hello world!";
 | 
|---|
 | 41 | 
 | 
|---|
 | 42 | int fd;
 | 
|---|
 | 43 | 
 | 
|---|
| [dd5f703] | 44 | static void *create_paged_area(size_t size)
 | 
|---|
 | 45 | {
 | 
|---|
| [8e3498b] | 46 |         size_t nwr;
 | 
|---|
| [b7fd2a0] | 47 |         errno_t rc;
 | 
|---|
| [8e3498b] | 48 | 
 | 
|---|
| [e755b3f] | 49 |         TPRINTF("Creating temporary file...\n");
 | 
|---|
 | 50 | 
 | 
|---|
| [f77c1c9] | 51 |         rc = vfs_lookup_open(TEST_FILE, WALK_REGULAR | WALK_MAY_CREATE,
 | 
|---|
 | 52 |             MODE_READ | MODE_WRITE, &fd);
 | 
|---|
 | 53 |         if (rc != EOK)
 | 
|---|
| [e755b3f] | 54 |                 return NULL;
 | 
|---|
| [79ea5af] | 55 |         (void) vfs_unlink_path(TEST_FILE);
 | 
|---|
| [58898d1d] | 56 | 
 | 
|---|
| [1433ecda] | 57 |         rc = vfs_write(fd, (aoff64_t []) { 0 }, text, sizeof(text), &nwr);
 | 
|---|
| [8e3498b] | 58 |         if (rc != EOK) {
 | 
|---|
| [9c4cf0d] | 59 |                 vfs_put(fd);
 | 
|---|
| [e755b3f] | 60 |                 return NULL;
 | 
|---|
 | 61 |         }
 | 
|---|
 | 62 | 
 | 
|---|
| [dd5f703] | 63 |         async_sess_t *vfs_pager_sess;
 | 
|---|
 | 64 | 
 | 
|---|
 | 65 |         TPRINTF("Connecting to VFS pager...\n");
 | 
|---|
 | 66 | 
 | 
|---|
 | 67 |         vfs_pager_sess = service_connect_blocking(SERVICE_VFS, INTERFACE_PAGER, 0);
 | 
|---|
 | 68 | 
 | 
|---|
| [e755b3f] | 69 |         if (!vfs_pager_sess) {
 | 
|---|
| [9c4cf0d] | 70 |                 vfs_put(fd);
 | 
|---|
| [dd5f703] | 71 |                 return NULL;
 | 
|---|
| [e755b3f] | 72 |         }
 | 
|---|
| [a35b458] | 73 | 
 | 
|---|
| [dd5f703] | 74 |         TPRINTF("Creating AS area...\n");
 | 
|---|
| [a35b458] | 75 | 
 | 
|---|
| [dd5f703] | 76 |         void *result = async_as_area_create(AS_AREA_ANY, size,
 | 
|---|
| [e755b3f] | 77 |             AS_AREA_READ | AS_AREA_CACHEABLE, vfs_pager_sess, fd, 0, 0);
 | 
|---|
 | 78 |         if (result == AS_MAP_FAILED) {
 | 
|---|
| [9c4cf0d] | 79 |                 vfs_put(fd);
 | 
|---|
| [dd5f703] | 80 |                 return NULL;
 | 
|---|
| [e755b3f] | 81 |         }
 | 
|---|
| [a35b458] | 82 | 
 | 
|---|
| [dd5f703] | 83 |         return result;
 | 
|---|
 | 84 | }
 | 
|---|
 | 85 | 
 | 
|---|
 | 86 | static void touch_area(void *area, size_t size)
 | 
|---|
 | 87 | {
 | 
|---|
 | 88 |         TPRINTF("Touching (faulting-in) AS area...\n");
 | 
|---|
| [a35b458] | 89 | 
 | 
|---|
| [dd5f703] | 90 |         volatile char *ptr = (char *) area;
 | 
|---|
| [a35b458] | 91 | 
 | 
|---|
| [dd5f703] | 92 |         char ch;
 | 
|---|
 | 93 |         while ((ch = *ptr++))
 | 
|---|
 | 94 |                 putchar(ch);
 | 
|---|
 | 95 | }
 | 
|---|
 | 96 | 
 | 
|---|
 | 97 | const char *test_pager1(void)
 | 
|---|
 | 98 | {
 | 
|---|
 | 99 |         size_t buffer_len = PAGE_SIZE;
 | 
|---|
 | 100 |         void *buffer = create_paged_area(buffer_len);
 | 
|---|
| [e755b3f] | 101 |         if (!buffer)
 | 
|---|
| [dd5f703] | 102 |                 return "Cannot allocate memory";
 | 
|---|
| [a35b458] | 103 | 
 | 
|---|
| [dd5f703] | 104 |         touch_area(buffer, buffer_len);
 | 
|---|
| [e755b3f] | 105 | 
 | 
|---|
| [1b20da0] | 106 |         as_area_destroy(buffer);
 | 
|---|
| [9c4cf0d] | 107 |         vfs_put(fd);
 | 
|---|
| [a35b458] | 108 | 
 | 
|---|
| [dd5f703] | 109 |         return NULL;
 | 
|---|
 | 110 | }
 | 
|---|