Dr. David Alan Gilbert | 0efd888 | 2016-05-10 11:49:03 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org> |
| 3 | * Copyright (c) 2016 Red Hat, Inc. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. 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 | * 3. 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 "tests.h" |
| 30 | #include <sys/syscall.h> |
| 31 | |
| 32 | #if defined __NR_userfaultfd && defined HAVE_LINUX_USERFAULTFD_H |
| 33 | |
| 34 | # include <fcntl.h> |
| 35 | # include <inttypes.h> |
| 36 | # include <stdint.h> |
| 37 | # include <stdio.h> |
| 38 | # include <string.h> |
| 39 | # include <unistd.h> |
| 40 | |
| 41 | # include <sys/ioctl.h> |
| 42 | # include <sys/mman.h> |
| 43 | # include <linux/ioctl.h> |
| 44 | # include <linux/userfaultfd.h> |
| 45 | |
| 46 | int |
| 47 | main(void) |
| 48 | { |
| 49 | int rc; |
| 50 | int fd = syscall(__NR_userfaultfd, O_NONBLOCK); |
| 51 | size_t pagesize = getpagesize(); |
| 52 | |
| 53 | if (fd < 0) |
| 54 | perror_msg_and_skip("userfaultfd"); |
| 55 | |
| 56 | /* ---- API ---- */ |
| 57 | (void) tail_alloc(1); |
| 58 | struct uffdio_api *api_struct = tail_alloc(sizeof(*api_struct)); |
| 59 | |
| 60 | /* With a bad fd */ |
| 61 | memset(api_struct, 0, sizeof(*api_struct)); |
| 62 | rc = ioctl(-1, UFFDIO_API, api_struct); |
| 63 | printf("ioctl(-1, UFFDIO_API, {api=0, features=0}) = %d %s (%m)\n", |
| 64 | rc, errno2name()); |
| 65 | /* With a bad pointer */ |
| 66 | rc = ioctl(fd, UFFDIO_API, NULL); |
| 67 | printf("ioctl(%d, UFFDIO_API, NULL) = %d %s (%m)\n", |
| 68 | fd, rc, errno2name()); |
| 69 | /* Normal call */ |
| 70 | api_struct->api = UFFD_API; |
| 71 | api_struct->features = 0; |
| 72 | rc = ioctl(fd, UFFDIO_API, api_struct); |
| 73 | printf("ioctl(%d, UFFDIO_API, {api=0xaa, features=0, " |
| 74 | "features.out=%#" PRIx64 ", " "ioctls=1<<_UFFDIO_REGISTER|" |
| 75 | "1<<_UFFDIO_UNREGISTER|1<<_UFFDIO_API", |
| 76 | fd, (uint64_t)api_struct->features); |
| 77 | api_struct->ioctls &= ~(1ull<<_UFFDIO_REGISTER| |
| 78 | 1ull<<_UFFDIO_UNREGISTER| |
| 79 | 1ull<<_UFFDIO_API); |
| 80 | if (api_struct->ioctls) |
| 81 | printf("|%#" PRIx64, (uint64_t)api_struct->ioctls); |
| 82 | printf("}) = %d\n", rc); |
| 83 | |
| 84 | /* For the rest of the tests we need some anonymous memory */ |
| 85 | void *area1 = mmap(NULL, pagesize, PROT_READ|PROT_WRITE, |
| 86 | MAP_PRIVATE|MAP_ANONYMOUS, |
| 87 | -1, 0); |
| 88 | if (area1 == MAP_FAILED) |
| 89 | perror_msg_and_fail("mmap area1"); |
| 90 | void *area2 = mmap(NULL, pagesize, PROT_READ|PROT_WRITE, |
| 91 | MAP_PRIVATE|MAP_ANONYMOUS, |
| 92 | -1, 0); |
| 93 | if (area2 == MAP_FAILED) |
| 94 | perror_msg_and_fail("mmap area2"); |
| 95 | madvise(area2, pagesize, MADV_DONTNEED); |
| 96 | *(char *)area1 = 42; |
| 97 | |
| 98 | /* ---- REGISTER ---- */ |
| 99 | (void) tail_alloc(1); |
| 100 | struct uffdio_register *register_struct = |
| 101 | tail_alloc(sizeof(*register_struct)); |
| 102 | memset(register_struct, 0, sizeof(*register_struct)); |
| 103 | |
| 104 | rc = ioctl(-1, UFFDIO_REGISTER, register_struct); |
| 105 | printf("ioctl(-1, UFFDIO_REGISTER, {range={start=0, len=0}, " |
| 106 | "mode=0}) = %d %s (%m)\n", rc, errno2name()); |
| 107 | |
| 108 | rc = ioctl(fd, UFFDIO_REGISTER, NULL); |
| 109 | printf("ioctl(%d, UFFDIO_REGISTER, NULL) = %d %s (%m)\n", |
| 110 | fd, rc, errno2name()); |
| 111 | |
| 112 | register_struct->range.start = (uint64_t)(uintptr_t)area2; |
| 113 | register_struct->range.len = pagesize; |
| 114 | register_struct->mode = UFFDIO_REGISTER_MODE_MISSING; |
| 115 | rc = ioctl(fd, UFFDIO_REGISTER, register_struct); |
| 116 | printf("ioctl(%d, UFFDIO_REGISTER, {range={start=%p, len=%#zx}, " |
| 117 | "mode=UFFDIO_REGISTER_MODE_MISSING, ioctls=" |
| 118 | "1<<_UFFDIO_WAKE|1<<_UFFDIO_COPY|1<<_UFFDIO_ZEROPAGE", |
| 119 | fd, area2, pagesize); |
| 120 | register_struct->ioctls &= ~(1ull<<_UFFDIO_WAKE| |
| 121 | 1ull<<_UFFDIO_COPY| |
| 122 | 1ull<<_UFFDIO_ZEROPAGE); |
| 123 | if (register_struct->ioctls) |
| 124 | printf("|%#" PRIx64, (uint64_t)register_struct->ioctls); |
| 125 | printf("}) = %d\n", rc); |
| 126 | |
| 127 | /* With area2 registered we can now do the atomic copies onto it |
| 128 | * but be careful not to access it in any other way otherwise |
| 129 | * userfaultfd will cause us to stall. |
| 130 | */ |
| 131 | /* ---- COPY ---- */ |
| 132 | (void) tail_alloc(1); |
| 133 | struct uffdio_copy *copy_struct = tail_alloc(sizeof(*copy_struct)); |
| 134 | |
| 135 | memset(copy_struct, 0, sizeof(*copy_struct)); |
| 136 | rc = ioctl(-1, UFFDIO_COPY, copy_struct); |
| 137 | printf("ioctl(-1, UFFDIO_COPY, {dst=0, src=0, len=0, mode=0" |
| 138 | "}) = %d %s (%m)\n", rc, errno2name()); |
| 139 | |
| 140 | rc = ioctl(fd, UFFDIO_COPY, NULL); |
| 141 | printf("ioctl(%d, UFFDIO_COPY, NULL) = %d %s (%m)\n", |
| 142 | fd, rc, errno2name()); |
| 143 | |
| 144 | copy_struct->dst = (uint64_t)(uintptr_t)area2; |
| 145 | copy_struct->src = (uint64_t)(uintptr_t)area1; |
| 146 | copy_struct->len = pagesize; |
| 147 | copy_struct->mode = UFFDIO_COPY_MODE_DONTWAKE; |
| 148 | rc = ioctl(fd, UFFDIO_COPY, copy_struct); |
| 149 | printf("ioctl(%d, UFFDIO_COPY, {dst=%p, src=%p, len=%#zx," |
| 150 | " mode=UFFDIO_COPY_MODE_DONTWAKE, copy=%#zx}) = %d\n", |
| 151 | fd, area2, area1, pagesize, pagesize, rc); |
| 152 | |
| 153 | /* ---- ZEROPAGE ---- */ |
| 154 | (void) tail_alloc(1); |
| 155 | struct uffdio_zeropage *zero_struct = tail_alloc(sizeof(*zero_struct)); |
| 156 | madvise(area2, pagesize, MADV_DONTNEED); |
| 157 | |
| 158 | memset(zero_struct, 0, sizeof(*zero_struct)); |
| 159 | rc = ioctl(-1, UFFDIO_ZEROPAGE, zero_struct); |
| 160 | printf("ioctl(-1, UFFDIO_ZEROPAGE, {range={start=0, len=0}, mode=0" |
| 161 | "}) = %d %s (%m)\n", rc, errno2name()); |
| 162 | |
| 163 | rc = ioctl(fd, UFFDIO_ZEROPAGE, NULL); |
| 164 | printf("ioctl(%d, UFFDIO_ZEROPAGE, NULL) = %d %s (%m)\n", |
| 165 | fd, rc, errno2name()); |
| 166 | |
| 167 | zero_struct->range.start = (uint64_t)(uintptr_t)area2; |
| 168 | zero_struct->range.len = pagesize; |
| 169 | zero_struct->mode = UFFDIO_ZEROPAGE_MODE_DONTWAKE; |
| 170 | rc = ioctl(fd, UFFDIO_ZEROPAGE, zero_struct); |
| 171 | printf("ioctl(%d, UFFDIO_ZEROPAGE, {range={start=%p, len=%#zx}," |
| 172 | " mode=UFFDIO_ZEROPAGE_MODE_DONTWAKE, zeropage=%#zx}) = %d\n", |
| 173 | fd, area2, pagesize, pagesize, rc); |
| 174 | |
| 175 | /* ---- WAKE ---- */ |
| 176 | (void) tail_alloc(1); |
| 177 | struct uffdio_range *range_struct = tail_alloc(sizeof(*range_struct)); |
| 178 | memset(range_struct, 0, sizeof(*range_struct)); |
| 179 | |
| 180 | rc = ioctl(-1, UFFDIO_WAKE, range_struct); |
| 181 | printf("ioctl(-1, UFFDIO_WAKE, {start=0, len=0}) = %d %s (%m)\n", |
| 182 | rc, errno2name()); |
| 183 | |
| 184 | rc = ioctl(fd, UFFDIO_WAKE, NULL); |
| 185 | printf("ioctl(%d, UFFDIO_WAKE, NULL) = %d %s (%m)\n", |
| 186 | fd, rc, errno2name()); |
| 187 | |
| 188 | range_struct->start = (uint64_t)(uintptr_t)area2; |
| 189 | range_struct->len = pagesize; |
| 190 | rc = ioctl(fd, UFFDIO_WAKE, range_struct); |
| 191 | printf("ioctl(%d, UFFDIO_WAKE, {start=%p, len=%#zx}) = %d\n", |
| 192 | fd, area2, pagesize, rc); |
| 193 | |
| 194 | /* ---- UNREGISTER ---- */ |
| 195 | memset(range_struct, 0, sizeof(*range_struct)); |
| 196 | |
| 197 | rc = ioctl(-1, UFFDIO_UNREGISTER, range_struct); |
| 198 | printf("ioctl(-1, UFFDIO_UNREGISTER, {start=0, len=0}) = %d %s (%m)\n", |
| 199 | rc, errno2name()); |
| 200 | |
| 201 | rc = ioctl(fd, UFFDIO_UNREGISTER, NULL); |
| 202 | printf("ioctl(%d, UFFDIO_UNREGISTER, NULL) = %d %s (%m)\n", |
| 203 | fd, rc, errno2name()); |
| 204 | |
| 205 | range_struct->start = (uint64_t)(uintptr_t)area2; |
| 206 | range_struct->len = pagesize; |
| 207 | rc = ioctl(fd, UFFDIO_UNREGISTER, range_struct); |
| 208 | printf("ioctl(%d, UFFDIO_UNREGISTER, {start=%p, len=%#zx}) = %d\n", |
| 209 | fd, area2, pagesize, rc); |
| 210 | puts("+++ exited with 0 +++"); |
| 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | #else |
| 215 | |
| 216 | SKIP_MAIN_UNDEFINED("__NR_userfaultfd && HAVE_LINUX_USERFAULTFD_H") |
| 217 | |
| 218 | #endif |