1
0

plugin_ioevent.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 <stdint.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <sys/mman.h>
  15. #include <sys/syscall.h>
  16. #include <time.h>
  17. #include <unistd.h>
  18. #include "crosvm.h"
  19. #ifndef F_LINUX_SPECIFIC_BASE
  20. #define F_LINUX_SPECIFIC_BASE 1024
  21. #endif
  22. #ifndef F_ADD_SEALS
  23. #define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9)
  24. #endif
  25. #ifndef F_SEAL_SHRINK
  26. #define F_SEAL_SHRINK 0x0002
  27. #endif
  28. #define LOAD_ADDRESS 0x1000
  29. #define DATAMATCH_VAL 0x88
  30. #define KILL_ADDRESS 0x4000
  31. int g_kill_evt;
  32. void *vcpu_thread(void *arg) {
  33. struct crosvm_vcpu *vcpu = arg;
  34. struct crosvm_vcpu_event evt;
  35. int i = 0;
  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.rflags = 2;
  48. regs.rip = LOAD_ADDRESS;
  49. regs.rax = DATAMATCH_VAL;
  50. regs.rbx = DATAMATCH_VAL - 1;
  51. crosvm_vcpu_set_regs(vcpu, &regs);
  52. }
  53. if (evt.kind == CROSVM_VCPU_EVENT_KIND_IO_ACCESS &&
  54. evt.io_access.address_space == CROSVM_ADDRESS_SPACE_MMIO &&
  55. evt.io_access.address == KILL_ADDRESS &&
  56. evt.io_access.is_write &&
  57. evt.io_access.length == 1 &&
  58. evt.io_access.data[0] == 1)
  59. {
  60. uint64_t dummy = 1;
  61. write(g_kill_evt, &dummy, sizeof(dummy));
  62. return NULL;
  63. }
  64. crosvm_vcpu_resume(vcpu);
  65. }
  66. return NULL;
  67. }
  68. int main(int argc, char** argv) {
  69. const uint8_t code[] = {
  70. /*
  71. 0000 BAF803 mov dx,0x3f8
  72. 0003 88C3 mov bl,al
  73. 0005 EE out dx,al
  74. 0006 B000 mov al,0x0
  75. 0008 EE out dx,al
  76. 0009 88D8 mov al,bl
  77. 000B EE out dx,al
  78. 0014 26C606000001 mov byte [es:0x0],0x1
  79. 000C F4 hlt
  80. */
  81. 0xba, 0xf8, 0x03,
  82. 0x88, 0xc3,
  83. 0xee,
  84. 0xb0, 0x00,
  85. 0xee,
  86. 0x88, 0xd8,
  87. 0xee,
  88. 0x26, 0xc6, 0x06, 0x00, 0x00, 0x01,
  89. 0xf4,
  90. };
  91. struct crosvm *crosvm;
  92. int ret = crosvm_connect(&crosvm);
  93. if (ret) {
  94. fprintf(stderr, "failed to connect to crosvm: %d\n", ret);
  95. return 1;
  96. }
  97. g_kill_evt = crosvm_get_shutdown_eventfd(crosvm);
  98. if (g_kill_evt < 0) {
  99. fprintf(stderr, "failed to get kill eventfd: %d\n", g_kill_evt);
  100. return 1;
  101. }
  102. ret = crosvm_reserve_range(crosvm, CROSVM_ADDRESS_SPACE_MMIO, KILL_ADDRESS, 1);
  103. if (ret) {
  104. fprintf(stderr, "failed to reserve mmio range: %d\n", ret);
  105. return 1;
  106. }
  107. uint8_t datamatch = DATAMATCH_VAL;
  108. struct crosvm_io *io;
  109. ret = crosvm_create_io_event(crosvm, CROSVM_ADDRESS_SPACE_IOPORT, 0x3f8, 1, &datamatch, &io);
  110. if (ret) {
  111. fprintf(stderr, "failed to create ioevent: %d\n", ret);
  112. return 1;
  113. }
  114. int ioeventfd = crosvm_io_event_fd(io);
  115. int mem_size = 0x4000;
  116. int mem_fd = syscall(SYS_memfd_create, "guest_mem", MFD_CLOEXEC | MFD_ALLOW_SEALING);
  117. if (mem_fd < 0) {
  118. fprintf(stderr, "failed to create guest memfd: %d\n", errno);
  119. return 1;
  120. }
  121. ret = ftruncate(mem_fd, mem_size);
  122. if (ret) {
  123. fprintf(stderr, "failed to set size of guest memory: %d\n", errno);
  124. return 1;
  125. }
  126. uint8_t *mem = mmap(NULL, mem_size, PROT_READ | PROT_WRITE, MAP_SHARED, mem_fd, 0);
  127. if (mem == MAP_FAILED) {
  128. fprintf(stderr, "failed to mmap guest memory: %d\n", errno);
  129. return 1;
  130. }
  131. fcntl(mem_fd, F_ADD_SEALS, F_SEAL_SHRINK);
  132. memcpy(mem + LOAD_ADDRESS, code, sizeof(code));
  133. struct crosvm_memory *mem_obj;
  134. ret = crosvm_create_memory(crosvm, mem_fd, 0, mem_size, 0, false, false, &mem_obj);
  135. if (ret) {
  136. fprintf(stderr, "failed to create memory in crosvm: %d\n", ret);
  137. return 1;
  138. }
  139. /* get and creat a thread for each vcpu */
  140. struct crosvm_vcpu *vcpus[32];
  141. pthread_t vcpu_threads[32];
  142. uint32_t vcpu_count;
  143. for (vcpu_count = 0; vcpu_count < 32; vcpu_count++) {
  144. ret = crosvm_get_vcpu(crosvm, vcpu_count, &vcpus[vcpu_count]);
  145. if (ret == -ENOENT)
  146. break;
  147. if (ret) {
  148. fprintf(stderr, "error while getting all vcpus: %d\n", ret);
  149. return 1;
  150. }
  151. pthread_create(&vcpu_threads[vcpu_count], NULL, vcpu_thread, vcpus[vcpu_count]);
  152. }
  153. ret = crosvm_start(crosvm);
  154. if (ret) {
  155. fprintf(stderr, "failed to tell crosvm to start: %d\n", ret);
  156. return 1;
  157. }
  158. uint64_t dummy;
  159. read(g_kill_evt, &dummy, 8);
  160. ret = read(ioeventfd, &dummy, sizeof(dummy));
  161. if (ret == -1) {
  162. fprintf(stderr, "failed to read ioeventfd: %d\n", errno);
  163. return 1;
  164. }
  165. if (dummy != 2) {
  166. fprintf(stderr, "ioeventfd was not triggered the expected number of times: %d\n", dummy);
  167. return 1;
  168. }
  169. return 0;
  170. }