Mirror of strace – the linux syscall tracer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

move_pages.c 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Check decoding of move_pages syscall.
  3. *
  4. * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
  5. * Copyright (c) 2016-2018 The strace developers.
  6. * All rights reserved.
  7. *
  8. * SPDX-License-Identifier: GPL-2.0-or-later
  9. */
  10. #include "tests.h"
  11. #include <asm/unistd.h>
  12. #ifdef __NR_move_pages
  13. # include <errno.h>
  14. # include <stdio.h>
  15. # include <unistd.h>
  16. # define MAX_STRLEN 3
  17. static void
  18. print_page_array(const void **const pages,
  19. const unsigned long count,
  20. const unsigned int offset)
  21. {
  22. if (!count) {
  23. printf("%s", pages ? "[]" : "NULL");
  24. return;
  25. }
  26. if (count <= offset) {
  27. printf("%p", pages);
  28. return;
  29. }
  30. printf("[");
  31. unsigned long i;
  32. for (i = 0; i < count; ++i) {
  33. if (i)
  34. printf(", ");
  35. if (i + offset < count) {
  36. if (i >= MAX_STRLEN) {
  37. printf("...");
  38. break;
  39. }
  40. } else {
  41. printf("... /* %p */", pages + i);
  42. break;
  43. }
  44. const void *const addr = pages[i];
  45. if (addr)
  46. printf("%p", addr);
  47. else
  48. printf("NULL");
  49. }
  50. printf("]");
  51. }
  52. static void
  53. print_node_array(const int *const nodes,
  54. const unsigned long count,
  55. const unsigned int offset)
  56. {
  57. if (!count) {
  58. printf("%s", nodes ? "[]" : "NULL");
  59. return;
  60. }
  61. if (count <= offset) {
  62. printf("%p", nodes);
  63. return;
  64. }
  65. printf("[");
  66. unsigned long i;
  67. for (i = 0; i < count; ++i) {
  68. if (i)
  69. printf(", ");
  70. if (i + offset < count) {
  71. if (i >= MAX_STRLEN) {
  72. printf("...");
  73. break;
  74. }
  75. } else {
  76. printf("... /* %p */", nodes + i);
  77. break;
  78. }
  79. printf("%d", nodes[i]);
  80. }
  81. printf("]");
  82. }
  83. static void
  84. print_status_array(const int *const status, const unsigned long count)
  85. {
  86. if (!count) {
  87. printf("%s", status ? "[]" : "NULL");
  88. return;
  89. }
  90. printf("[");
  91. unsigned long i;
  92. for (i = 0; i < count; ++i) {
  93. if (i)
  94. printf(", ");
  95. if (i >= MAX_STRLEN) {
  96. printf("...");
  97. break;
  98. }
  99. if (status[i] >= 0) {
  100. printf("%d", status[i]);
  101. } else {
  102. # if !XLAT_RAW
  103. errno = -status[i];
  104. # endif
  105. # if XLAT_RAW
  106. printf("%d", status[i]);
  107. # elif XLAT_VERBOSE
  108. printf("%d /* -%s */", status[i], errno2name());
  109. # else
  110. printf("-%s", errno2name());
  111. # endif
  112. }
  113. }
  114. printf("]");
  115. }
  116. static void
  117. print_stat_pages(const unsigned long pid, const unsigned long count,
  118. const void **const pages, int *const status)
  119. {
  120. const unsigned long flags = (unsigned long) 0xfacefeed00000002ULL;
  121. long rc = syscall(__NR_move_pages,
  122. pid, count, pages, NULL, status, flags);
  123. const char *errstr = sprintrc(rc);
  124. printf("move_pages(%d, %lu, ", (int) pid, count);
  125. print_page_array(pages, count, 0);
  126. printf(", NULL, ");
  127. if (rc) {
  128. if (count)
  129. printf("%p", status);
  130. else
  131. printf("[]");
  132. } else {
  133. print_status_array(status, count);
  134. }
  135. # if XLAT_RAW
  136. printf(", 0x2) = %s\n", errstr);
  137. # elif XLAT_VERBOSE
  138. printf(", 0x2 /* MPOL_MF_MOVE */) = %s\n", errstr);
  139. # else /* XLAT_ABBREV */
  140. printf(", MPOL_MF_MOVE) = %s\n", errstr);
  141. # endif
  142. }
  143. static void
  144. print_move_pages(const unsigned long pid,
  145. unsigned long count,
  146. const unsigned int offset,
  147. const void **const pages,
  148. int *const nodes,
  149. int *const status)
  150. {
  151. const unsigned long flags = (unsigned long) 0xfacefeed00000004ULL;
  152. count += offset;
  153. long rc = syscall(__NR_move_pages,
  154. pid, count, pages, nodes, status, flags);
  155. const char *errstr = sprintrc(rc);
  156. printf("move_pages(%d, %lu, ", (int) pid, count);
  157. print_page_array(pages, count, offset);
  158. printf(", ");
  159. print_node_array(nodes, count, offset);
  160. printf(", ");
  161. if (count)
  162. printf("%p", status);
  163. else
  164. printf("[]");
  165. # if XLAT_RAW
  166. printf(", 0x4) = %s\n", errstr);
  167. # elif XLAT_VERBOSE
  168. printf(", 0x4 /* MPOL_MF_MOVE_ALL */) = %s\n", errstr);
  169. # else /* XLAT_ABBREV */
  170. printf(", MPOL_MF_MOVE_ALL) = %s\n", errstr);
  171. # endif
  172. }
  173. int
  174. main(void)
  175. {
  176. const unsigned long pid =
  177. (unsigned long) 0xfacefeed00000000ULL | getpid();
  178. unsigned long count = 1;
  179. const unsigned page_size = get_page_size();
  180. const void *const page = tail_alloc(page_size);
  181. const void *const efault = page + page_size;
  182. TAIL_ALLOC_OBJECT_VAR_PTR(const void *, pages);
  183. TAIL_ALLOC_OBJECT_VAR_PTR(int, nodes);
  184. TAIL_ALLOC_OBJECT_VAR_PTR(int, status);
  185. print_stat_pages(pid, 0, pages, status);
  186. print_move_pages(pid, 0, 0, pages, nodes, status);
  187. print_move_pages(pid, 0, 1, pages + 1, nodes + 1, status + 1);
  188. *pages = page;
  189. print_stat_pages(pid, count, pages, status);
  190. *nodes = 0xdeadbee1;
  191. print_move_pages(pid, count, 0, pages, nodes, status);
  192. print_move_pages(pid, count, 1, pages, nodes, status);
  193. ++count;
  194. --status;
  195. *(--pages) = efault;
  196. print_stat_pages(pid, count, pages, status);
  197. *(--nodes) = 0xdeadbee2;
  198. print_move_pages(pid, count, 0, pages, nodes, status);
  199. print_move_pages(pid, count, 1, pages, nodes, status);
  200. ++count;
  201. --status;
  202. *(--pages) = nodes;
  203. print_stat_pages(pid, count, pages, status);
  204. *(--nodes) = 0xdeadbee3;
  205. print_move_pages(pid, count, 0, pages, nodes, status);
  206. print_move_pages(pid, count, 1, pages, nodes, status);
  207. ++count;
  208. --status;
  209. *(--pages) = status;
  210. print_stat_pages(pid, count, pages, status);
  211. *(--nodes) = 0xdeadbee4;
  212. print_move_pages(pid, count, 0, pages, nodes, status);
  213. print_move_pages(pid, count, 1, pages, nodes, status);
  214. puts("+++ exited with 0 +++");
  215. return 0;
  216. }
  217. #else
  218. SKIP_MAIN_UNDEFINED("__NR_move_pages")
  219. #endif