plugin_irqfd.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 STACK_BASE (LOAD_ADDRESS + 0x1000)
  30. #define STACK_SIZE 0x1000
  31. #define SUCCESS_ADDRESS 0x3000
  32. #define KILL_ADDRESS 0x4000
  33. /*
  34. org 0x1000
  35. bits 16
  36. cli
  37. ; Set entry 0x0 in the interrupt vector table
  38. mov word [0x0], handle
  39. mov word [0x2], 0x0
  40. sti
  41. ; Loop until interrupt is handled
  42. loop:
  43. cmp byte [si], 0x01
  44. jne loop
  45. cli
  46. ; Signal that we are ready to end
  47. end:
  48. mov byte [es:0], 0x01
  49. hlt
  50. ; Handle the interrupt by halting
  51. handle:
  52. mov byte [si], 0x01
  53. iret
  54. */
  55. const uint8_t g_code[] = {
  56. 0xfa, 0xc7, 0x06, 0x00, 0x00, 0x1b, 0x10, 0xc7, 0x06, 0x02, 0x00, 0x00,
  57. 0x00, 0xfb, 0x80, 0x3c, 0x01, 0x75, 0xfb, 0xfa, 0x26, 0xc6, 0x06, 0x00,
  58. 0x00, 0x01, 0xf4, 0xc6, 0x04, 0x01, 0xcf
  59. };
  60. struct vcpu_context {
  61. struct crosvm_vcpu *vcpu;
  62. int irqeventfd;
  63. int kill_evt;
  64. };
  65. void *vcpu_thread(void *arg) {
  66. struct vcpu_context *ctx = arg;
  67. struct crosvm_vcpu *vcpu = ctx->vcpu;
  68. struct crosvm_vcpu_event evt;
  69. uint64_t dummy = 1;
  70. int i = 0;
  71. int ret;
  72. while (crosvm_vcpu_wait(vcpu, &evt) == 0) {
  73. if (evt.kind == CROSVM_VCPU_EVENT_KIND_INIT) {
  74. struct kvm_sregs sregs;
  75. crosvm_vcpu_get_sregs(vcpu, &sregs);
  76. sregs.cs.base = 0;
  77. sregs.cs.selector = 0x0;
  78. sregs.ss.base = 0;
  79. sregs.ss.selector = 0x0;
  80. sregs.es.base = KILL_ADDRESS;
  81. sregs.es.selector = 0x0;
  82. crosvm_vcpu_set_sregs(vcpu, &sregs);
  83. struct kvm_regs regs;
  84. crosvm_vcpu_get_regs(vcpu, &regs);
  85. regs.rflags = 2;
  86. regs.rip = LOAD_ADDRESS;
  87. regs.rsp = STACK_BASE + STACK_SIZE;
  88. regs.rsi = SUCCESS_ADDRESS;
  89. crosvm_vcpu_set_regs(vcpu, &regs);
  90. write(ctx->irqeventfd, &dummy, sizeof(dummy));
  91. }
  92. if (evt.kind == CROSVM_VCPU_EVENT_KIND_IO_ACCESS &&
  93. evt.io_access.address_space == CROSVM_ADDRESS_SPACE_MMIO &&
  94. evt.io_access.address == KILL_ADDRESS &&
  95. evt.io_access.is_write &&
  96. evt.io_access.length == 1 &&
  97. evt.io_access.data[0] == 1)
  98. {
  99. write(ctx->kill_evt, &dummy, sizeof(dummy));
  100. return NULL;
  101. }
  102. crosvm_vcpu_resume(vcpu);
  103. }
  104. return NULL;
  105. }
  106. int main(int argc, char** argv) {
  107. int i;
  108. uint64_t dummy = 1;
  109. struct crosvm *crosvm;
  110. int ret = crosvm_connect(&crosvm);
  111. if (ret) {
  112. fprintf(stderr, "failed to connect to crosvm: %d\n", ret);
  113. return 1;
  114. }
  115. int kill_evt = crosvm_get_shutdown_eventfd(crosvm);
  116. if (kill_evt < 0) {
  117. fprintf(stderr, "failed to get kill eventfd: %d\n", kill_evt);
  118. return 1;
  119. }
  120. crosvm_reserve_range(crosvm, CROSVM_ADDRESS_SPACE_MMIO, KILL_ADDRESS, 1);
  121. struct crosvm_irq *irq;
  122. ret = crosvm_create_irq_event(crosvm, 0, &irq);
  123. if (ret) {
  124. fprintf(stderr, "failed to create irq event: %d\n", ret);
  125. return 1;
  126. }
  127. int irqeventfd = crosvm_irq_event_get_fd(irq);
  128. int mem_size = 0x4000;
  129. int mem_fd = syscall(SYS_memfd_create, "guest_mem", MFD_CLOEXEC | MFD_ALLOW_SEALING);
  130. if (mem_fd < 0) {
  131. fprintf(stderr, "failed to create guest memfd: %d\n", errno);
  132. return 1;
  133. }
  134. ret = ftruncate(mem_fd, mem_size);
  135. if (ret) {
  136. fprintf(stderr, "failed to set size of guest memory: %d\n", errno);
  137. return 1;
  138. }
  139. uint8_t *mem = mmap(NULL, mem_size, PROT_READ | PROT_WRITE, MAP_SHARED, mem_fd, 0);
  140. if (mem == MAP_FAILED) {
  141. fprintf(stderr, "failed to mmap guest memory: %d\n", errno);
  142. return 1;
  143. }
  144. fcntl(mem_fd, F_ADD_SEALS, F_SEAL_SHRINK);
  145. memcpy(mem + LOAD_ADDRESS, g_code, sizeof(g_code));
  146. struct crosvm_memory *mem_obj;
  147. ret = crosvm_create_memory(crosvm, mem_fd, 0, mem_size, 0, false, false, &mem_obj);
  148. if (ret) {
  149. fprintf(stderr, "failed to create memory in crosvm: %d\n", ret);
  150. return 1;
  151. }
  152. struct crosvm_vcpu *vcpus[32];
  153. struct vcpu_context ctxs[32];
  154. pthread_t vcpu_threads[32];
  155. uint32_t vcpu_count;
  156. for (vcpu_count = 0; vcpu_count < 32; vcpu_count++) {
  157. ret = crosvm_get_vcpu(crosvm, vcpu_count, &vcpus[vcpu_count]);
  158. if (ret == -ENOENT)
  159. break;
  160. if (ret) {
  161. fprintf(stderr, "error while getting all vcpus: %d\n", ret);
  162. return 1;
  163. }
  164. ctxs[vcpu_count].vcpu = vcpus[vcpu_count];
  165. ctxs[vcpu_count].irqeventfd = irqeventfd;
  166. ctxs[vcpu_count].kill_evt = kill_evt;
  167. pthread_create(&vcpu_threads[vcpu_count], NULL, vcpu_thread, &ctxs[vcpu_count]);
  168. }
  169. ret = crosvm_start(crosvm);
  170. if (ret) {
  171. fprintf(stderr, "failed to tell crosvm to start: %d\n", ret);
  172. return 1;
  173. }
  174. ret = read(kill_evt, &dummy, sizeof(dummy));
  175. if (ret == -1) {
  176. fprintf(stderr, "failed to read kill eventfd: %d\n", errno);
  177. return 1;
  178. }
  179. if (mem[SUCCESS_ADDRESS] != 0x01) {
  180. fprintf(stderr, "interrupt was not handled: 0x%x\n", mem[SUCCESS_ADDRESS]);
  181. return 1;
  182. }
  183. return 0;
  184. }