1
0

plugin_async_write.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Copyright 2019 The ChromiumOS Authors
  3. * Use of this source code is governed by a BSD-style license that can be
  4. * found in the LICENSE file.
  5. */
  6. #include <errno.h>
  7. #include <fcntl.h>
  8. #include <linux/memfd.h>
  9. #include <pthread.h>
  10. #include <signal.h>
  11. #include <stdint.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <sys/mman.h>
  16. #include <sys/syscall.h>
  17. #include <time.h>
  18. #include <unistd.h>
  19. #include "crosvm.h"
  20. #ifndef F_LINUX_SPECIFIC_BASE
  21. #define F_LINUX_SPECIFIC_BASE 1024
  22. #endif
  23. #ifndef F_ADD_SEALS
  24. #define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9)
  25. #endif
  26. #ifndef F_SEAL_SHRINK
  27. #define F_SEAL_SHRINK 0x0002
  28. #endif
  29. #define KILL_ADDRESS 0x3f9
  30. #define ASYNC_ADDRESS 0x500
  31. int g_kill_evt;
  32. int got_error = 0;
  33. void *vcpu_thread(void *arg) {
  34. struct crosvm_vcpu *vcpu = arg;
  35. struct crosvm_vcpu_event evt;
  36. while (crosvm_vcpu_wait(vcpu, &evt) == 0) {
  37. if (evt.kind == CROSVM_VCPU_EVENT_KIND_INIT) {
  38. struct kvm_sregs sregs;
  39. crosvm_vcpu_get_sregs(vcpu, &sregs);
  40. sregs.cs.base = 0;
  41. sregs.cs.selector = 0;
  42. sregs.es.base = KILL_ADDRESS;
  43. sregs.es.selector = 0;
  44. crosvm_vcpu_set_sregs(vcpu, &sregs);
  45. struct kvm_regs regs;
  46. crosvm_vcpu_get_regs(vcpu, &regs);
  47. regs.rip = 0x1000;
  48. regs.rax = 2;
  49. regs.rbx = 7;
  50. regs.rflags = 2;
  51. crosvm_vcpu_set_regs(vcpu, &regs);
  52. }
  53. if (evt.kind == CROSVM_VCPU_EVENT_KIND_IO_ACCESS) {
  54. if (evt.io_access.address_space == CROSVM_ADDRESS_SPACE_IOPORT &&
  55. evt.io_access.address == ASYNC_ADDRESS &&
  56. evt.io_access.is_write &&
  57. evt.io_access.length == 1) {
  58. int ret;
  59. if (!evt.io_access.no_resume) {
  60. fprintf(stderr, "should have been told not to resume\n");
  61. got_error = 1;
  62. }
  63. ret = crosvm_vcpu_wait(vcpu, &evt);
  64. if (ret == 0) {
  65. if (evt.kind != CROSVM_VCPU_EVENT_KIND_IO_ACCESS ||
  66. evt.io_access.address_space !=
  67. CROSVM_ADDRESS_SPACE_IOPORT ||
  68. evt.io_access.address != ASYNC_ADDRESS ||
  69. !evt.io_access.is_write ||
  70. !evt.io_access.no_resume ||
  71. evt.io_access.length != 1) {
  72. fprintf(stderr, "got unexpected wait #1 result\n");
  73. got_error = 1;
  74. }
  75. } else {
  76. fprintf(stderr, "crosvm_vcpu_wait() #1 failed: %d\n", ret);
  77. got_error = 1;
  78. }
  79. ret = crosvm_vcpu_wait(vcpu, &evt);
  80. if (ret == 0) {
  81. if (evt.kind != CROSVM_VCPU_EVENT_KIND_IO_ACCESS ||
  82. evt.io_access.address_space !=
  83. CROSVM_ADDRESS_SPACE_IOPORT ||
  84. evt.io_access.address != ASYNC_ADDRESS ||
  85. !evt.io_access.is_write ||
  86. !evt.io_access.no_resume ||
  87. evt.io_access.length != 1) {
  88. fprintf(stderr, "got unexpected wait #2 result\n");
  89. got_error = 1;
  90. }
  91. } else {
  92. fprintf(stderr, "crosvm_vcpu_wait() #2 failed: %d\n", ret);
  93. got_error = 1;
  94. }
  95. // skip the crosvm_vcpu_resume()
  96. continue;
  97. }
  98. if (evt.io_access.address_space == CROSVM_ADDRESS_SPACE_IOPORT &&
  99. evt.io_access.address == KILL_ADDRESS &&
  100. evt.io_access.is_write &&
  101. evt.io_access.length == 1 &&
  102. evt.io_access.data[0] == 1)
  103. {
  104. uint64_t dummy = 1;
  105. write(g_kill_evt, &dummy, sizeof(dummy));
  106. return NULL;
  107. }
  108. }
  109. crosvm_vcpu_resume(vcpu);
  110. }
  111. return NULL;
  112. }
  113. int main(int argc, char** argv) {
  114. const uint8_t code[] = {
  115. /*
  116. B007 mov al,0x7
  117. BA0005 mov dx,0x500
  118. EE out dx,al
  119. EE out dx,al
  120. EE out dx,al
  121. BAF903 mov dx,0x3f9
  122. B001 mov al,0x1
  123. EE out dx,al
  124. F4 hlt
  125. */
  126. 0xb0, 0x7,
  127. 0xba, (ASYNC_ADDRESS & 0xFF), ((ASYNC_ADDRESS >> 8) & 0xFF),
  128. 0xee,
  129. 0xee,
  130. 0xee,
  131. 0xba, (KILL_ADDRESS & 0xFF), ((KILL_ADDRESS >> 8) & 0xFF),
  132. 0xb0, 0x01,
  133. 0xee,
  134. 0xf4
  135. };
  136. struct crosvm *crosvm;
  137. int ret = crosvm_connect(&crosvm);
  138. if (ret) {
  139. fprintf(stderr, "failed to connect to crosvm: %d\n", ret);
  140. return 1;
  141. }
  142. /*
  143. * Not strictly necessary, but demonstrates we can have as many connections
  144. * as we please.
  145. */
  146. struct crosvm *extra_crosvm;
  147. ret = crosvm_new_connection(crosvm, &extra_crosvm);
  148. if (ret) {
  149. fprintf(stderr, "failed to make new socket: %d\n", ret);
  150. return 1;
  151. }
  152. /* We needs this eventfd to know when to exit before being killed. */
  153. g_kill_evt = crosvm_get_shutdown_eventfd(crosvm);
  154. if (g_kill_evt < 0) {
  155. fprintf(stderr, "failed to get kill eventfd: %d\n", g_kill_evt);
  156. return 1;
  157. }
  158. ret = crosvm_reserve_async_write_range(crosvm, CROSVM_ADDRESS_SPACE_IOPORT,
  159. ASYNC_ADDRESS, 1);
  160. if (ret) {
  161. fprintf(stderr, "failed to reserve async ioport range: %d\n", ret);
  162. return 1;
  163. }
  164. ret = crosvm_reserve_range(crosvm, CROSVM_ADDRESS_SPACE_IOPORT,
  165. KILL_ADDRESS, 1);
  166. if (ret) {
  167. fprintf(stderr, "failed to reserve kill ioport range: %d\n", ret);
  168. return 1;
  169. }
  170. int mem_size = 0x2000;
  171. int mem_fd = syscall(SYS_memfd_create, "guest_mem",
  172. MFD_CLOEXEC | MFD_ALLOW_SEALING);
  173. if (mem_fd < 0) {
  174. fprintf(stderr, "failed to create guest memfd: %d\n", errno);
  175. return 1;
  176. }
  177. ret = ftruncate(mem_fd, mem_size);
  178. if (ret) {
  179. fprintf(stderr, "failed to set size of guest memory: %d\n", errno);
  180. return 1;
  181. }
  182. uint8_t *mem = mmap(NULL, mem_size, PROT_READ | PROT_WRITE, MAP_SHARED,
  183. mem_fd, 0x1000);
  184. if (mem == MAP_FAILED) {
  185. fprintf(stderr, "failed to mmap guest memory: %d\n", errno);
  186. return 1;
  187. }
  188. fcntl(mem_fd, F_ADD_SEALS, F_SEAL_SHRINK);
  189. memcpy(mem, code, sizeof(code));
  190. struct crosvm_memory *mem_obj;
  191. ret = crosvm_create_memory(crosvm, mem_fd, 0x1000, 0x1000, 0x1000, false,
  192. false, &mem_obj);
  193. if (ret) {
  194. fprintf(stderr, "failed to create memory in crosvm: %d\n", ret);
  195. return 1;
  196. }
  197. /* get and creat a thread for each vcpu */
  198. struct crosvm_vcpu *vcpus[32];
  199. pthread_t vcpu_threads[32];
  200. uint32_t vcpu_count;
  201. for (vcpu_count = 0; vcpu_count < 32; vcpu_count++) {
  202. ret = crosvm_get_vcpu(crosvm, vcpu_count, &vcpus[vcpu_count]);
  203. if (ret == -ENOENT)
  204. break;
  205. if (ret) {
  206. fprintf(stderr, "error while getting all vcpus: %d\n", ret);
  207. return 1;
  208. }
  209. pthread_create(&vcpu_threads[vcpu_count], NULL, vcpu_thread,
  210. vcpus[vcpu_count]);
  211. }
  212. ret = crosvm_start(extra_crosvm);
  213. if (ret) {
  214. fprintf(stderr, "failed to tell crosvm to start: %d\n", ret);
  215. return 1;
  216. }
  217. /* Wait for crosvm to request that we exit otherwise we will be killed. */
  218. uint64_t dummy;
  219. read(g_kill_evt, &dummy, 8);
  220. ret = crosvm_destroy_memory(crosvm, &mem_obj);
  221. if (ret) {
  222. fprintf(stderr, "failed to destroy memory in crosvm: %d\n", ret);
  223. return 1;
  224. }
  225. ret = crosvm_reserve_async_write_range(crosvm, CROSVM_ADDRESS_SPACE_IOPORT,
  226. ASYNC_ADDRESS, 0);
  227. if (ret) {
  228. fprintf(stderr, "failed to unreserve async ioport range: %d\n", ret);
  229. return 1;
  230. }
  231. ret = crosvm_reserve_range(crosvm, CROSVM_ADDRESS_SPACE_IOPORT,
  232. KILL_ADDRESS, 0);
  233. if (ret) {
  234. fprintf(stderr, "failed to unreserve kill ioport range: %d\n", ret);
  235. return 1;
  236. }
  237. if (got_error) {
  238. fprintf(stderr, "vm ran to completion but with an error\n");
  239. return 1;
  240. }
  241. return 0;
  242. }