Dmitry V. Levin | d10a955 | 2016-04-27 23:43:24 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Check decoding of set_mempolicy syscall. |
| 3 | * |
| 4 | * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org> |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * 1. Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in the |
| 14 | * documentation and/or other materials provided with the distribution. |
| 15 | * 3. The name of the author may not be used to endorse or promote products |
| 16 | * derived from this software without specific prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | */ |
| 29 | |
| 30 | #include "tests.h" |
Dmitry V. Levin | 6a2f43c | 2016-08-09 14:38:29 +0000 | [diff] [blame] | 31 | #include <asm/unistd.h> |
Dmitry V. Levin | d10a955 | 2016-04-27 23:43:24 +0000 | [diff] [blame] | 32 | |
| 33 | #ifdef __NR_set_mempolicy |
| 34 | |
| 35 | # include <errno.h> |
| 36 | # include <stdio.h> |
| 37 | # include <string.h> |
| 38 | # include <unistd.h> |
| 39 | |
| 40 | # include "xlat.h" |
| 41 | # include "xlat/policies.h" |
| 42 | |
| 43 | # define MAX_STRLEN 3 |
| 44 | # define NLONGS(n) ((n + 8 * sizeof(long) - 2) \ |
| 45 | / (8 * sizeof(long))) |
| 46 | |
| 47 | static void |
| 48 | print_nodes(const unsigned long maxnode, unsigned int offset) |
| 49 | { |
| 50 | unsigned int nlongs = NLONGS(maxnode); |
| 51 | if (nlongs <= offset) |
| 52 | nlongs = 0; |
| 53 | else |
| 54 | nlongs -= offset; |
| 55 | const unsigned int size = nlongs * sizeof(long); |
| 56 | unsigned long *const nodemask = |
| 57 | tail_alloc(size ? size : (offset ? 1 : 0)); |
| 58 | memset(nodemask, 0, size); |
Dmitry V. Levin | d10a955 | 2016-04-27 23:43:24 +0000 | [diff] [blame] | 59 | |
| 60 | long rc = syscall(__NR_set_mempolicy, 0, nodemask, maxnode); |
| 61 | int saved_errno = errno; |
| 62 | |
| 63 | fputs("set_mempolicy(MPOL_DEFAULT, ", stdout); |
| 64 | |
| 65 | if (nlongs) { |
| 66 | putc('[', stdout); |
| 67 | unsigned int i; |
| 68 | for (i = 0; i < nlongs + offset; ++i) { |
| 69 | if (i) |
| 70 | fputs(", ", stdout); |
| 71 | if (i < nlongs) { |
| 72 | if (i >= MAX_STRLEN) { |
| 73 | fputs("...", stdout); |
| 74 | break; |
| 75 | } |
| 76 | printf("%#0*lx", (int) sizeof(long) * 2 + 2, |
| 77 | nodemask[i]); |
| 78 | } else { |
| 79 | printf("%p", nodemask + i); |
| 80 | break; |
| 81 | } |
| 82 | } |
| 83 | putc(']', stdout); |
| 84 | } else { |
Dmitry V. Levin | 636b8c1 | 2016-05-07 23:02:36 +0000 | [diff] [blame] | 85 | if (maxnode) |
| 86 | printf("%p", nodemask); |
| 87 | else |
| 88 | printf("[]"); |
Dmitry V. Levin | d10a955 | 2016-04-27 23:43:24 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | printf(", %lu) = ", maxnode); |
| 92 | if (rc) { |
| 93 | errno = saved_errno; |
| 94 | printf("%ld %s (%m)\n", rc, errno2name()); |
| 95 | } else { |
| 96 | puts("0"); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | static void |
| 101 | test_offset(const unsigned int offset) |
| 102 | { |
| 103 | unsigned long maxnode = get_page_size() * 8; |
| 104 | |
| 105 | print_nodes(maxnode, offset); |
| 106 | print_nodes(maxnode + 1, offset); |
| 107 | print_nodes(maxnode + 2, offset); |
| 108 | |
| 109 | maxnode = sizeof(long) * 8; |
| 110 | print_nodes(0, offset); |
| 111 | print_nodes(1, offset); |
| 112 | print_nodes(2, offset); |
| 113 | print_nodes(maxnode - 1, offset); |
| 114 | print_nodes(maxnode , offset); |
| 115 | print_nodes(maxnode + 1, offset); |
| 116 | print_nodes(maxnode + 2, offset); |
| 117 | print_nodes(maxnode * 2 - 1, offset); |
| 118 | print_nodes(maxnode * 2 , offset); |
| 119 | print_nodes(maxnode * 2 + 1, offset); |
| 120 | print_nodes(maxnode * 2 + 2, offset); |
| 121 | print_nodes(maxnode * 3 - 1, offset); |
| 122 | print_nodes(maxnode * 3 , offset); |
| 123 | print_nodes(maxnode * 3 + 1, offset); |
| 124 | print_nodes(maxnode * 3 + 2, offset); |
| 125 | print_nodes(maxnode * 4 + 2, offset); |
| 126 | } |
| 127 | |
| 128 | int |
| 129 | main(void) |
| 130 | { |
| 131 | if (syscall(__NR_set_mempolicy, 0, 0, 0)) |
| 132 | perror_msg_and_skip("set_mempolicy"); |
| 133 | puts("set_mempolicy(MPOL_DEFAULT, NULL, 0) = 0"); |
| 134 | |
| 135 | const unsigned long *nodemask = (void *) 0xfacefeedfffffffe; |
| 136 | const unsigned long maxnode = (unsigned long) 0xcafef00dbadc0ded; |
| 137 | long rc = syscall(__NR_set_mempolicy, 1, nodemask, maxnode); |
| 138 | printf("set_mempolicy(MPOL_PREFERRED, %p, %lu) = %ld %s (%m)\n", |
| 139 | nodemask, maxnode, rc, errno2name()); |
| 140 | |
| 141 | test_offset(0); |
| 142 | test_offset(1); |
| 143 | |
| 144 | puts("+++ exited with 0 +++"); |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | #else |
| 149 | |
| 150 | SKIP_MAIN_UNDEFINED("__NR_set_mempolicy") |
| 151 | |
| 152 | #endif |