1
0

plugin_vcpu_pause.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * Copyright 2017 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 <stdbool.h>
  11. #include <stdint.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <sys/eventfd.h>
  16. #include <sys/mman.h>
  17. #include <sys/syscall.h>
  18. #include <sys/types.h>
  19. #include <time.h>
  20. #include <unistd.h>
  21. #include "crosvm.h"
  22. #ifndef F_LINUX_SPECIFIC_BASE
  23. #define F_LINUX_SPECIFIC_BASE 1024
  24. #endif
  25. #ifndef F_ADD_SEALS
  26. #define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9)
  27. #endif
  28. #ifndef F_SEAL_SHRINK
  29. #define F_SEAL_SHRINK 0x0002
  30. #endif
  31. #define SERIAL_ADDRESS 0x3f8
  32. #define KILL_ADDRESS 0x3f9
  33. static char g_serial_out[16];
  34. static int g_next_evt;
  35. static int g_kill_evt;
  36. static bool g_paused;
  37. static bool g_exit_loop;
  38. static pthread_mutex_t g_pause_mutex = PTHREAD_MUTEX_INITIALIZER;
  39. static pthread_cond_t g_pause_cond = PTHREAD_COND_INITIALIZER;
  40. static volatile int count;
  41. static void *vcpu_thread_fn(void *arg) {
  42. struct crosvm_vcpu *vcpu = arg;
  43. struct crosvm_vcpu_event evt;
  44. int i = 0;
  45. while (crosvm_vcpu_wait(vcpu, &evt) == 0) {
  46. if (evt.kind == CROSVM_VCPU_EVENT_KIND_INIT) {
  47. struct kvm_sregs sregs;
  48. crosvm_vcpu_get_sregs(vcpu, &sregs);
  49. sregs.cs.base = 0;
  50. sregs.cs.selector = 0;
  51. sregs.es.base = KILL_ADDRESS;
  52. sregs.es.selector = 0;
  53. crosvm_vcpu_set_sregs(vcpu, &sregs);
  54. struct kvm_regs regs;
  55. crosvm_vcpu_get_regs(vcpu, &regs);
  56. regs.rip = 0x1000;
  57. regs.rax = 2;
  58. regs.rbx = 7;
  59. regs.rflags = 2;
  60. crosvm_vcpu_set_regs(vcpu, &regs);
  61. /* Signal the main thread that init is done */
  62. uint64_t dummy = 1;
  63. write(g_next_evt, &dummy, sizeof(dummy));
  64. }
  65. else if (evt.kind == CROSVM_VCPU_EVENT_KIND_IO_ACCESS &&
  66. evt.io_access.address_space == CROSVM_ADDRESS_SPACE_IOPORT &&
  67. evt.io_access.address == KILL_ADDRESS &&
  68. evt.io_access.is_write &&
  69. evt.io_access.length == 1 &&
  70. evt.io_access.data[0] == 1) {
  71. uint64_t dummy = 1;
  72. write(g_kill_evt, &dummy, sizeof(dummy));
  73. return NULL;
  74. }
  75. else if (evt.kind == CROSVM_VCPU_EVENT_KIND_PAUSED) {
  76. /* Signal that we paused */
  77. uint64_t dummy = 1;
  78. write(g_next_evt, &dummy, sizeof(dummy));
  79. /* Wait till we can continue again */
  80. pthread_mutex_lock(&g_pause_mutex);
  81. while (g_paused)
  82. pthread_cond_wait(&g_pause_cond, &g_pause_mutex);
  83. /* Kick the VM from infinite loop if requested */
  84. if (g_exit_loop) {
  85. struct kvm_regs regs;
  86. crosvm_vcpu_get_regs(vcpu, &regs);
  87. regs.rbx = 1;
  88. crosvm_vcpu_set_regs(vcpu, &regs);
  89. }
  90. /* Signal that we are no longer paused */
  91. write(g_next_evt, &dummy, sizeof(dummy));
  92. pthread_mutex_unlock(&g_pause_mutex);
  93. }
  94. crosvm_vcpu_resume(vcpu);
  95. }
  96. return NULL;
  97. }
  98. static int signal_pause(struct crosvm *crosvm) {
  99. pthread_mutex_lock(&g_pause_mutex);
  100. g_paused = true;
  101. pthread_mutex_unlock(&g_pause_mutex);
  102. return crosvm_pause_vcpus(crosvm, 1, NULL);
  103. }
  104. static void signal_unpause(struct crosvm *crosvm, bool exit_loop) {
  105. pthread_mutex_lock(&g_pause_mutex);
  106. g_paused = false;
  107. g_exit_loop = exit_loop;
  108. pthread_cond_broadcast(&g_pause_cond);
  109. pthread_mutex_unlock(&g_pause_mutex);
  110. }
  111. int main(int argc, char** argv) {
  112. const uint8_t code[] = {
  113. /*
  114. 0000 00D8 add al,bl
  115. 0002 80FB01 cmp bl, 0x1
  116. 0005 75F9 jne 0x0
  117. 0007 BAF903 mov dx,0x3f8
  118. 000A B001 mov al,0x1
  119. 000C EE out dx,al
  120. 000D F4 hlt
  121. */
  122. 0x00, 0xd8,
  123. 0x80, 0xfb, 0x01,
  124. 0x75, 0xf9,
  125. 0xba, 0xf9, 0x03,
  126. 0xb0, 0x01,
  127. 0xee,
  128. 0xf4
  129. };
  130. g_next_evt = eventfd(0, 0);
  131. if (g_next_evt == -1) {
  132. fprintf(stderr, "failed to create eventfd: %d\n", errno);
  133. return 1;
  134. }
  135. struct crosvm *crosvm;
  136. int ret = crosvm_connect(&crosvm);
  137. if (ret) {
  138. fprintf(stderr, "failed to connect to crosvm: %d\n", ret);
  139. return 1;
  140. }
  141. /* We needs this eventfd to know when to exit before being killed. */
  142. g_kill_evt = crosvm_get_shutdown_eventfd(crosvm);
  143. if (g_kill_evt < 0) {
  144. fprintf(stderr, "failed to get kill eventfd: %d\n", g_kill_evt);
  145. return 1;
  146. }
  147. ret = crosvm_reserve_range(crosvm, CROSVM_ADDRESS_SPACE_IOPORT,
  148. KILL_ADDRESS, 1);
  149. if (ret) {
  150. fprintf(stderr, "failed to reserve mmio range: %d\n", ret);
  151. return 1;
  152. }
  153. int mem_size = 0x2000;
  154. int mem_fd = syscall(SYS_memfd_create,
  155. "guest_mem", MFD_CLOEXEC | MFD_ALLOW_SEALING);
  156. if (mem_fd < 0) {
  157. fprintf(stderr, "failed to create guest memfd: %d\n", errno);
  158. return 1;
  159. }
  160. ret = ftruncate(mem_fd, mem_size);
  161. if (ret) {
  162. fprintf(stderr, "failed to set size of guest memory: %d\n", errno);
  163. return 1;
  164. }
  165. uint8_t *mem = mmap(NULL, mem_size, PROT_READ | PROT_WRITE, MAP_SHARED,
  166. mem_fd, 0x1000);
  167. if (mem == MAP_FAILED) {
  168. fprintf(stderr, "failed to mmap guest memory: %d\n", errno);
  169. return 1;
  170. }
  171. fcntl(mem_fd, F_ADD_SEALS, F_SEAL_SHRINK);
  172. memcpy(mem, code, sizeof(code));
  173. struct crosvm_memory *mem_obj;
  174. ret = crosvm_create_memory(crosvm, mem_fd, 0x1000, 0x1000, 0x1000,
  175. false, false, &mem_obj);
  176. if (ret) {
  177. fprintf(stderr, "failed to create memory in crosvm: %d\n", ret);
  178. return 1;
  179. }
  180. /* get and create a thread for the vcpu */
  181. struct crosvm_vcpu *vcpu;
  182. ret = crosvm_get_vcpu(crosvm, 0, &vcpu);
  183. if (ret) {
  184. fprintf(stderr, "error while getting vcpu: %d\n", ret);
  185. return 1;
  186. }
  187. pthread_t vcpu_thread;
  188. ret = pthread_create(&vcpu_thread, NULL, vcpu_thread_fn, vcpu);
  189. if (ret) {
  190. fprintf(stderr, "failed to createvcpu thread\n");
  191. return 1;
  192. }
  193. ret = crosvm_start(crosvm);
  194. if (ret) {
  195. fprintf(stderr, "failed to tell crosvm to start: %d\n", ret);
  196. return 1;
  197. }
  198. /* Wait till VCPU thread tells us that its initialization is done */
  199. uint64_t dummy;
  200. read(g_next_evt, &dummy, sizeof(dummy));
  201. ret = signal_pause(crosvm);
  202. if (ret) {
  203. fprintf(stderr, "failed to pause vcpus (1st time): %d\n", ret);
  204. return 1;
  205. }
  206. /* Wait till VCPU thread tells us it is paused */
  207. read(g_next_evt, &dummy, sizeof(dummy));
  208. /* Try pausing VCPUs 2nd time to make sure we do not deadlock */
  209. ret = signal_pause(crosvm);
  210. if (ret) {
  211. fprintf(stderr, "failed to pause vcpus (2nd time): %d\n", ret);
  212. return 1;
  213. }
  214. signal_unpause(crosvm, false);
  215. /* Wait until VCPU thread tells us that it is no longer paused */
  216. read(g_next_evt, &dummy, sizeof(dummy));
  217. /*
  218. * Try pausing VCPUs 3rd time to see if we will miss pause
  219. * request as we are exiting previous pause.
  220. */
  221. ret = signal_pause(crosvm);
  222. if (ret) {
  223. fprintf(stderr, "failed to pause vcpus (2nd time): %d\n", ret);
  224. return 1;
  225. }
  226. signal_unpause(crosvm, true);
  227. /* Wait for crosvm to request that we exit otherwise we will be killed. */
  228. read(g_kill_evt, &dummy, sizeof(dummy));
  229. ret = crosvm_destroy_memory(crosvm, &mem_obj);
  230. if (ret) {
  231. fprintf(stderr, "failed to destroy memory in crosvm: %d\n", ret);
  232. return 1;
  233. }
  234. ret = crosvm_reserve_range(crosvm, CROSVM_ADDRESS_SPACE_IOPORT,
  235. KILL_ADDRESS, 0);
  236. if (ret) {
  237. fprintf(stderr, "failed to unreserve mmio range: %d\n", ret);
  238. return 1;
  239. }
  240. return 0;
  241. }