1
0

plugin_adder.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 SERIAL_ADDRESS 0x3f8
  29. #define KILL_ADDRESS 0x3f9
  30. char g_serial_out[16];
  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.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 == SERIAL_ADDRESS &&
  56. evt.io_access.is_write &&
  57. evt.io_access.length == 1) {
  58. g_serial_out[i] = evt.io_access.data[0];
  59. i++;
  60. }
  61. if (evt.io_access.address_space == CROSVM_ADDRESS_SPACE_IOPORT &&
  62. evt.io_access.address == KILL_ADDRESS &&
  63. evt.io_access.is_write &&
  64. evt.io_access.length == 1 &&
  65. evt.io_access.data[0] == 1)
  66. {
  67. uint64_t dummy = 1;
  68. write(g_kill_evt, &dummy, sizeof(dummy));
  69. return NULL;
  70. }
  71. }
  72. crosvm_vcpu_resume(vcpu);
  73. }
  74. return NULL;
  75. }
  76. int main(int argc, char** argv) {
  77. const uint8_t code[] = {
  78. /*
  79. 0000 BAF803 mov dx,0x3f8
  80. 0003 00D8 add al,bl
  81. 0005 0430 add al,0x30
  82. 0007 EE out dx,al
  83. 0008 B05C mov al,0x0a
  84. 000A EE out dx,al
  85. 000B BAF903 mov dx,0x3f9
  86. 000E B001 mov al,0x1
  87. 0010 EE out dx,al
  88. 0011 F4 hlt
  89. */
  90. 0xba, 0xf8, 0x03,
  91. 0x00, 0xd8,
  92. 0x04, '0',
  93. 0xee,
  94. 0xb0, '\n',
  95. 0xee,
  96. 0xba, 0xf9, 0x03,
  97. 0xb0, 0x01,
  98. 0xee,
  99. 0xf4
  100. };
  101. struct crosvm *crosvm;
  102. int ret = crosvm_connect(&crosvm);
  103. if (ret) {
  104. fprintf(stderr, "failed to connect to crosvm: %d\n", ret);
  105. return 1;
  106. }
  107. /*
  108. * Not strictly necessary, but demonstrates we can have as many connections
  109. * as we please.
  110. */
  111. struct crosvm *extra_crosvm;
  112. ret = crosvm_new_connection(crosvm, &extra_crosvm);
  113. if (ret) {
  114. fprintf(stderr, "failed to make new socket: %d\n", ret);
  115. return 1;
  116. }
  117. /* We needs this eventfd to know when to exit before being killed. */
  118. g_kill_evt = crosvm_get_shutdown_eventfd(crosvm);
  119. if (g_kill_evt < 0) {
  120. fprintf(stderr, "failed to get kill eventfd: %d\n", g_kill_evt);
  121. return 1;
  122. }
  123. ret = crosvm_reserve_range(crosvm, CROSVM_ADDRESS_SPACE_IOPORT, SERIAL_ADDRESS, 1);
  124. if (ret) {
  125. fprintf(stderr, "failed to reserve ioport range: %d\n", ret);
  126. return 1;
  127. }
  128. ret = crosvm_reserve_range(crosvm, CROSVM_ADDRESS_SPACE_IOPORT, KILL_ADDRESS, 1);
  129. if (ret) {
  130. fprintf(stderr, "failed to reserve mmio range: %d\n", ret);
  131. return 1;
  132. }
  133. int mem_size = 0x2000;
  134. int mem_fd = syscall(SYS_memfd_create, "guest_mem", MFD_CLOEXEC | MFD_ALLOW_SEALING);
  135. if (mem_fd < 0) {
  136. fprintf(stderr, "failed to create guest memfd: %d\n", errno);
  137. return 1;
  138. }
  139. ret = ftruncate(mem_fd, mem_size);
  140. if (ret) {
  141. fprintf(stderr, "failed to set size of guest memory: %d\n", errno);
  142. return 1;
  143. }
  144. uint8_t *mem = mmap(NULL, mem_size, PROT_READ | PROT_WRITE, MAP_SHARED, mem_fd, 0x1000);
  145. if (mem == MAP_FAILED) {
  146. fprintf(stderr, "failed to mmap guest memory: %d\n", errno);
  147. return 1;
  148. }
  149. fcntl(mem_fd, F_ADD_SEALS, F_SEAL_SHRINK);
  150. memcpy(mem, code, sizeof(code));
  151. struct crosvm_memory *mem_obj;
  152. ret = crosvm_create_memory(crosvm, mem_fd, 0x1000, 0x1000, 0x1000, false, false, &mem_obj);
  153. if (ret) {
  154. fprintf(stderr, "failed to create memory in crosvm: %d\n", ret);
  155. return 1;
  156. }
  157. /* get and creat a thread for each vcpu */
  158. struct crosvm_vcpu *vcpus[32];
  159. pthread_t vcpu_threads[32];
  160. uint32_t vcpu_count;
  161. for (vcpu_count = 0; vcpu_count < 32; vcpu_count++) {
  162. ret = crosvm_get_vcpu(crosvm, vcpu_count, &vcpus[vcpu_count]);
  163. if (ret == -ENOENT)
  164. break;
  165. if (ret) {
  166. fprintf(stderr, "error while getting all vcpus: %d\n", ret);
  167. return 1;
  168. }
  169. pthread_create(&vcpu_threads[vcpu_count], NULL, vcpu_thread, vcpus[vcpu_count]);
  170. }
  171. ret = crosvm_start(extra_crosvm);
  172. if (ret) {
  173. fprintf(stderr, "failed to tell crosvm to start: %d\n", ret);
  174. return 1;
  175. }
  176. /* Wait for crosvm to request that we exit otherwise we will be killed. */
  177. uint64_t dummy;
  178. read(g_kill_evt, &dummy, 8);
  179. ret = crosvm_destroy_memory(crosvm, &mem_obj);
  180. if (ret) {
  181. fprintf(stderr, "failed to destroy memory in crosvm: %d\n", ret);
  182. return 1;
  183. }
  184. ret = crosvm_reserve_range(crosvm, CROSVM_ADDRESS_SPACE_IOPORT, SERIAL_ADDRESS, 0);
  185. if (ret) {
  186. fprintf(stderr, "failed to unreserve ioport range: %d\n", ret);
  187. return 1;
  188. }
  189. ret = crosvm_reserve_range(crosvm, CROSVM_ADDRESS_SPACE_IOPORT, KILL_ADDRESS, 0);
  190. if (ret) {
  191. fprintf(stderr, "failed to unreserve mmio range: %d\n", ret);
  192. return 1;
  193. }
  194. return strcmp(g_serial_out, "9\n");
  195. }