2 * Check decoding of set_mempolicy syscall.
4 * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
31 #include <asm/unistd.h>
33 #ifdef __NR_set_mempolicy
41 # include "xlat/policies.h"
44 # define NLONGS(n) ((n + 8 * sizeof(long) - 2) \
48 print_nodes(const unsigned long maxnode, unsigned int offset)
50 unsigned int nlongs = NLONGS(maxnode);
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);
60 long rc = syscall(__NR_set_mempolicy, 0, nodemask, maxnode);
61 const char *errstr = sprintrc(rc);
63 fputs("set_mempolicy(MPOL_DEFAULT, ", stdout);
68 for (i = 0; i < nlongs + offset; ++i) {
72 if (i >= MAX_STRLEN) {
76 printf("%#0*lx", (int) sizeof(long) * 2 + 2,
79 printf("%p", nodemask + i);
86 printf("%p", nodemask);
91 printf(", %lu) = %s\n", maxnode, errstr);
95 test_offset(const unsigned int offset)
97 unsigned long maxnode = get_page_size() * 8;
99 print_nodes(maxnode, offset);
100 print_nodes(maxnode + 1, offset);
101 print_nodes(maxnode + 2, offset);
103 maxnode = sizeof(long) * 8;
104 print_nodes(0, offset);
105 print_nodes(1, offset);
106 print_nodes(2, offset);
107 print_nodes(maxnode - 1, offset);
108 print_nodes(maxnode , offset);
109 print_nodes(maxnode + 1, offset);
110 print_nodes(maxnode + 2, offset);
111 print_nodes(maxnode * 2 - 1, offset);
112 print_nodes(maxnode * 2 , offset);
113 print_nodes(maxnode * 2 + 1, offset);
114 print_nodes(maxnode * 2 + 2, offset);
115 print_nodes(maxnode * 3 - 1, offset);
116 print_nodes(maxnode * 3 , offset);
117 print_nodes(maxnode * 3 + 1, offset);
118 print_nodes(maxnode * 3 + 2, offset);
119 print_nodes(maxnode * 4 + 2, offset);
125 if (syscall(__NR_set_mempolicy, 0, 0, 0))
126 perror_msg_and_skip("set_mempolicy");
127 puts("set_mempolicy(MPOL_DEFAULT, NULL, 0) = 0");
129 const unsigned long *nodemask = (void *) 0xfacefeedfffffffeULL;
130 const unsigned long maxnode = (unsigned long) 0xcafef00dbadc0dedULL;
131 long rc = syscall(__NR_set_mempolicy, 1, nodemask, maxnode);
132 printf("set_mempolicy(MPOL_PREFERRED, %p, %lu) = %s\n",
133 nodemask, maxnode, sprintrc(rc));
138 puts("+++ exited with 0 +++");
144 SKIP_MAIN_UNDEFINED("__NR_set_mempolicy")