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.

kvm.c 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /*
  2. * Support for decoding of KVM_* ioctl commands.
  3. *
  4. * Copyright (c) 2017 Masatake YAMATO <yamato@redhat.com>
  5. * Copyright (c) 2017 Red Hat, Inc.
  6. * Copyright (c) 2017-2019 The strace developers.
  7. * All rights reserved.
  8. *
  9. * SPDX-License-Identifier: LGPL-2.1-or-later
  10. */
  11. #include "defs.h"
  12. #ifdef HAVE_LINUX_KVM_H
  13. # include <linux/kvm.h>
  14. # include "print_fields.h"
  15. # include "arch_kvm.c"
  16. # include "xmalloc.h"
  17. # include "mmap_cache.h"
  18. struct vcpu_info {
  19. struct vcpu_info *next;
  20. int fd;
  21. int cpuid;
  22. long mmap_addr;
  23. unsigned long mmap_len;
  24. bool resolved;
  25. };
  26. static bool dump_kvm_run_structure;
  27. static struct vcpu_info *
  28. vcpu_find(struct tcb *const tcp, int fd)
  29. {
  30. for (struct vcpu_info *vcpu_info = tcp->vcpu_info_list;
  31. vcpu_info;
  32. vcpu_info = vcpu_info->next)
  33. if (vcpu_info->fd == fd)
  34. return vcpu_info;
  35. return NULL;
  36. }
  37. static struct vcpu_info *
  38. vcpu_alloc(struct tcb *const tcp, int fd, int cpuid)
  39. {
  40. struct vcpu_info *vcpu_info = xzalloc(sizeof(*vcpu_info));
  41. vcpu_info->fd = fd;
  42. vcpu_info->cpuid = cpuid;
  43. vcpu_info->next = tcp->vcpu_info_list;
  44. tcp->vcpu_info_list = vcpu_info;
  45. return vcpu_info;
  46. }
  47. void
  48. kvm_vcpu_info_free(struct tcb *tcp)
  49. {
  50. struct vcpu_info *head, *next;
  51. for (head = tcp->vcpu_info_list; head; head = next) {
  52. next = head->next;
  53. free(head);
  54. }
  55. tcp->vcpu_info_list = NULL;
  56. }
  57. static void
  58. vcpu_register(struct tcb *const tcp, int fd, int cpuid)
  59. {
  60. if (fd < 0)
  61. return;
  62. struct vcpu_info *vcpu_info = vcpu_find(tcp, fd);
  63. if (!vcpu_info) {
  64. vcpu_alloc(tcp, fd, cpuid);
  65. } else if (vcpu_info->cpuid != cpuid) {
  66. vcpu_info->cpuid = cpuid;
  67. vcpu_info->resolved = false;
  68. }
  69. }
  70. static bool
  71. is_map_for_file(struct mmap_cache_entry_t *map_info, void *data)
  72. {
  73. /* major version for anon inode may be given in get_anon_bdev()
  74. * in linux kernel.
  75. *
  76. * *p = MKDEV(0, dev & MINORMASK);
  77. *-----------------^
  78. */
  79. return map_info->binary_filename &&
  80. map_info->major == 0 &&
  81. strcmp(map_info->binary_filename, data) == 0;
  82. }
  83. static unsigned long
  84. map_len(struct mmap_cache_entry_t *map_info)
  85. {
  86. return map_info->start_addr < map_info->end_addr
  87. ? map_info->end_addr - map_info->start_addr
  88. : 0;
  89. }
  90. # define VCPU_DENTRY_PREFIX "anon_inode:kvm-vcpu:"
  91. static struct vcpu_info*
  92. vcpu_get_info(struct tcb *const tcp, int fd)
  93. {
  94. struct vcpu_info *vcpu_info = vcpu_find(tcp, fd);
  95. struct mmap_cache_entry_t *map_info;
  96. const char *cpuid_str;
  97. enum mmap_cache_rebuild_result mc_stat =
  98. mmap_cache_rebuild_if_invalid(tcp, __func__);
  99. if (mc_stat == MMAP_CACHE_REBUILD_NOCACHE)
  100. return NULL;
  101. if (vcpu_info && vcpu_info->resolved) {
  102. if (mc_stat == MMAP_CACHE_REBUILD_READY)
  103. return vcpu_info;
  104. else {
  105. map_info = mmap_cache_search(tcp, vcpu_info->mmap_addr);
  106. if (map_info) {
  107. cpuid_str =
  108. STR_STRIP_PREFIX(map_info->binary_filename,
  109. VCPU_DENTRY_PREFIX);
  110. if (cpuid_str != map_info->binary_filename) {
  111. int cpuid = string_to_uint(cpuid_str);
  112. if (cpuid < 0)
  113. return NULL;
  114. if (vcpu_info->cpuid == cpuid)
  115. return vcpu_info;
  116. }
  117. }
  118. /* The vcpu vma may be mremap'ed. */
  119. vcpu_info->resolved = false;
  120. }
  121. }
  122. /* Slow path: !vcpu_info || !vcpu_info->resolved */
  123. char path[PATH_MAX + 1];
  124. cpuid_str = path;
  125. if (getfdpath(tcp, fd, path, sizeof(path)) >= 0)
  126. cpuid_str = STR_STRIP_PREFIX(path, VCPU_DENTRY_PREFIX);
  127. if (cpuid_str == path)
  128. map_info = NULL;
  129. else
  130. map_info = mmap_cache_search_custom(tcp, is_map_for_file, path);
  131. if (map_info) {
  132. int cpuid = string_to_uint(cpuid_str);
  133. if (cpuid < 0)
  134. return NULL;
  135. if (!vcpu_info)
  136. vcpu_info = vcpu_alloc(tcp, fd, cpuid);
  137. else if (vcpu_info->cpuid != cpuid)
  138. vcpu_info->cpuid = cpuid;
  139. vcpu_info->mmap_addr = map_info->start_addr;
  140. vcpu_info->mmap_len = map_len(map_info);
  141. vcpu_info->resolved = true;
  142. return vcpu_info;
  143. }
  144. return NULL;
  145. }
  146. static int
  147. kvm_ioctl_create_vcpu(struct tcb *const tcp, const kernel_ulong_t arg)
  148. {
  149. uint32_t cpuid = arg;
  150. if (entering(tcp)) {
  151. tprintf(", %u", cpuid);
  152. if (dump_kvm_run_structure)
  153. return 0;
  154. } else if (!syserror(tcp)) {
  155. vcpu_register(tcp, tcp->u_rval, cpuid);
  156. }
  157. return RVAL_IOCTL_DECODED | RVAL_FD;
  158. }
  159. # ifdef HAVE_STRUCT_KVM_USERSPACE_MEMORY_REGION
  160. # include "xlat/kvm_mem_flags.h"
  161. static int
  162. kvm_ioctl_set_user_memory_region(struct tcb *const tcp, const kernel_ulong_t arg)
  163. {
  164. struct kvm_userspace_memory_region u_memory_region;
  165. tprints(", ");
  166. if (umove_or_printaddr(tcp, arg, &u_memory_region))
  167. return RVAL_IOCTL_DECODED;
  168. PRINT_FIELD_U("{", u_memory_region, slot);
  169. PRINT_FIELD_FLAGS(", ", u_memory_region, flags, kvm_mem_flags,
  170. "KVM_MEM_???");
  171. PRINT_FIELD_X(", ", u_memory_region, guest_phys_addr);
  172. PRINT_FIELD_U(", ", u_memory_region, memory_size);
  173. PRINT_FIELD_X(", ", u_memory_region, userspace_addr);
  174. tprints("}");
  175. return RVAL_IOCTL_DECODED;
  176. }
  177. # endif /* HAVE_STRUCT_KVM_USERSPACE_MEMORY_REGION */
  178. # ifdef HAVE_STRUCT_KVM_REGS
  179. static int
  180. kvm_ioctl_decode_regs(struct tcb *const tcp, const unsigned int code,
  181. const kernel_ulong_t arg)
  182. {
  183. struct kvm_regs regs;
  184. if (code == KVM_GET_REGS && entering(tcp))
  185. return 0;
  186. tprints(", ");
  187. if (!umove_or_printaddr(tcp, arg, &regs))
  188. arch_print_kvm_regs(tcp, arg, &regs);
  189. return RVAL_IOCTL_DECODED;
  190. }
  191. # endif /* HAVE_STRUCT_KVM_REGS */
  192. # ifdef HAVE_STRUCT_KVM_CPUID2
  193. # include "xlat/kvm_cpuid_flags.h"
  194. static bool
  195. print_kvm_cpuid_entry(struct tcb *const tcp,
  196. void* elem_buf, size_t elem_size, void* data)
  197. {
  198. const struct kvm_cpuid_entry2 *entry = elem_buf;
  199. PRINT_FIELD_X("{", *entry, function);
  200. PRINT_FIELD_X(", ", *entry, index);
  201. PRINT_FIELD_FLAGS(", ", *entry, flags, kvm_cpuid_flags,
  202. "KVM_CPUID_FLAG_???");
  203. PRINT_FIELD_X(", ", *entry, eax);
  204. PRINT_FIELD_X(", ", *entry, ebx);
  205. PRINT_FIELD_X(", ", *entry, ecx);
  206. PRINT_FIELD_X(", ", *entry, edx);
  207. tprints("}");
  208. return true;
  209. }
  210. static int
  211. kvm_ioctl_decode_cpuid2(struct tcb *const tcp, const unsigned int code,
  212. const kernel_ulong_t arg)
  213. {
  214. struct kvm_cpuid2 cpuid;
  215. if (entering(tcp) && (code == KVM_GET_SUPPORTED_CPUID
  216. # ifdef KVM_GET_EMULATED_CPUID
  217. || code == KVM_GET_EMULATED_CPUID
  218. # endif
  219. ))
  220. return 0;
  221. tprints(", ");
  222. if (!umove_or_printaddr(tcp, arg, &cpuid)) {
  223. PRINT_FIELD_U("{", cpuid, nent);
  224. tprints(", entries=");
  225. if (abbrev(tcp)) {
  226. tprints("[");
  227. if (cpuid.nent)
  228. tprints("...");
  229. tprints("]");
  230. } else {
  231. struct kvm_cpuid_entry2 entry;
  232. print_array(tcp, arg + sizeof(cpuid), cpuid.nent,
  233. &entry, sizeof(entry), tfetch_mem,
  234. print_kvm_cpuid_entry, NULL);
  235. }
  236. tprints("}");
  237. }
  238. return RVAL_IOCTL_DECODED;
  239. }
  240. # endif /* HAVE_STRUCT_KVM_CPUID2 */
  241. # ifdef HAVE_STRUCT_KVM_SREGS
  242. static int
  243. kvm_ioctl_decode_sregs(struct tcb *const tcp, const unsigned int code,
  244. const kernel_ulong_t arg)
  245. {
  246. struct kvm_sregs sregs;
  247. if (code == KVM_GET_SREGS && entering(tcp))
  248. return 0;
  249. tprints(", ");
  250. if (!umove_or_printaddr(tcp, arg, &sregs))
  251. arch_print_kvm_sregs(tcp, arg, &sregs);
  252. return RVAL_IOCTL_DECODED;
  253. }
  254. # endif /* HAVE_STRUCT_KVM_SREGS */
  255. # include "xlat/kvm_cap.h"
  256. static int
  257. kvm_ioctl_decode_check_extension(struct tcb *const tcp, const unsigned int code,
  258. const kernel_ulong_t arg)
  259. {
  260. tprints(", ");
  261. printxval64(kvm_cap, arg, "KVM_CAP_???");
  262. return RVAL_IOCTL_DECODED;
  263. }
  264. # include "xlat/kvm_exit_reason.h"
  265. static void
  266. kvm_ioctl_run_attach_auxstr(struct tcb *const tcp,
  267. struct vcpu_info *info)
  268. {
  269. static struct kvm_run vcpu_run_struct;
  270. if (info->mmap_len < sizeof(vcpu_run_struct))
  271. return;
  272. if (umove(tcp, info->mmap_addr, &vcpu_run_struct) < 0)
  273. return;
  274. tcp->auxstr = xlookup(kvm_exit_reason, vcpu_run_struct.exit_reason);
  275. if (!tcp->auxstr)
  276. tcp->auxstr = "KVM_EXIT_???";
  277. }
  278. static int
  279. kvm_ioctl_decode_run(struct tcb *const tcp)
  280. {
  281. if (entering(tcp))
  282. return 0;
  283. int r = RVAL_DECODED;
  284. if (syserror(tcp))
  285. return r;
  286. if (dump_kvm_run_structure) {
  287. tcp->auxstr = NULL;
  288. int fd = tcp->u_arg[0];
  289. struct vcpu_info *info = vcpu_get_info(tcp, fd);
  290. if (info) {
  291. kvm_ioctl_run_attach_auxstr(tcp, info);
  292. if (tcp->auxstr)
  293. r |= RVAL_STR;
  294. }
  295. }
  296. return r;
  297. }
  298. int
  299. kvm_ioctl(struct tcb *const tcp, const unsigned int code, const kernel_ulong_t arg)
  300. {
  301. switch (code) {
  302. case KVM_CREATE_VCPU:
  303. return kvm_ioctl_create_vcpu(tcp, arg);
  304. # ifdef HAVE_STRUCT_KVM_USERSPACE_MEMORY_REGION
  305. case KVM_SET_USER_MEMORY_REGION:
  306. return kvm_ioctl_set_user_memory_region(tcp, arg);
  307. # endif
  308. # ifdef HAVE_STRUCT_KVM_REGS
  309. case KVM_SET_REGS:
  310. case KVM_GET_REGS:
  311. return kvm_ioctl_decode_regs(tcp, code, arg);
  312. # endif
  313. # ifdef HAVE_STRUCT_KVM_SREGS
  314. case KVM_SET_SREGS:
  315. case KVM_GET_SREGS:
  316. return kvm_ioctl_decode_sregs(tcp, code, arg);
  317. # endif
  318. # ifdef HAVE_STRUCT_KVM_CPUID2
  319. case KVM_SET_CPUID2:
  320. case KVM_GET_SUPPORTED_CPUID:
  321. # ifdef KVM_GET_EMULATED_CPUID
  322. case KVM_GET_EMULATED_CPUID:
  323. # endif
  324. return kvm_ioctl_decode_cpuid2(tcp, code, arg);
  325. # endif
  326. case KVM_CHECK_EXTENSION:
  327. return kvm_ioctl_decode_check_extension(tcp, code, arg);
  328. case KVM_CREATE_VM:
  329. return RVAL_DECODED | RVAL_FD;
  330. case KVM_RUN:
  331. return kvm_ioctl_decode_run(tcp);
  332. case KVM_GET_VCPU_MMAP_SIZE:
  333. case KVM_GET_API_VERSION:
  334. default:
  335. return RVAL_DECODED;
  336. }
  337. }
  338. void
  339. kvm_run_structure_decoder_init(void)
  340. {
  341. dump_kvm_run_structure = true;
  342. mmap_cache_enable();
  343. }
  344. #endif /* HAVE_LINUX_KVM_H */