1
0

plugin_dirty_log.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 SI_ADDRESS 0x8000
  30. #define BL_VALUE 0x12
  31. #define KILL_ADDRESS 0x9000
  32. int g_kill_evt;
  33. void *vcpu_thread(void *arg) {
  34. struct crosvm_vcpu *vcpu = arg;
  35. struct crosvm_vcpu_event evt;
  36. int i = 0;
  37. while (crosvm_vcpu_wait(vcpu, &evt) == 0) {
  38. if (evt.kind == CROSVM_VCPU_EVENT_KIND_INIT) {
  39. struct kvm_sregs sregs;
  40. crosvm_vcpu_get_sregs(vcpu, &sregs);
  41. sregs.cs.base = 0;
  42. sregs.cs.selector = 0;
  43. sregs.es.base = KILL_ADDRESS;
  44. sregs.es.selector = 0;
  45. crosvm_vcpu_set_sregs(vcpu, &sregs);
  46. struct kvm_regs regs;
  47. crosvm_vcpu_get_regs(vcpu, &regs);
  48. regs.rflags = 2;
  49. regs.rip = LOAD_ADDRESS;
  50. regs.rbx = BL_VALUE;
  51. regs.rsi = SI_ADDRESS;
  52. crosvm_vcpu_set_regs(vcpu, &regs);
  53. }
  54. if (evt.kind == CROSVM_VCPU_EVENT_KIND_IO_ACCESS &&
  55. evt.io_access.address_space == CROSVM_ADDRESS_SPACE_MMIO &&
  56. evt.io_access.address == KILL_ADDRESS &&
  57. evt.io_access.is_write &&
  58. evt.io_access.length == 1 &&
  59. evt.io_access.data[0] == 1)
  60. {
  61. uint64_t dummy = 1;
  62. write(g_kill_evt, &dummy, sizeof(dummy));
  63. return NULL;
  64. }
  65. crosvm_vcpu_resume(vcpu);
  66. }
  67. return NULL;
  68. }
  69. int main(int argc, char** argv) {
  70. const uint8_t code[] = {
  71. /*
  72. 0000 881C mov [si],bl
  73. 0014 26C606000001 mov byte [es:0x0],0x1
  74. 0002 F4 hlt
  75. */
  76. 0x88, 0x1c, 0x26, 0xc6, 0x06, 0x00, 0x00, 0x01, 0xf4
  77. };
  78. struct crosvm *crosvm;
  79. int ret = crosvm_connect(&crosvm);
  80. if (ret) {
  81. fprintf(stderr, "failed to connect to crosvm: %d\n", ret);
  82. return 1;
  83. }
  84. g_kill_evt = crosvm_get_shutdown_eventfd(crosvm);
  85. if (g_kill_evt < 0) {
  86. fprintf(stderr, "failed to get kill eventfd: %d\n", g_kill_evt);
  87. return 1;
  88. }
  89. ret = crosvm_reserve_range(crosvm, CROSVM_ADDRESS_SPACE_MMIO, KILL_ADDRESS, 1);
  90. if (ret) {
  91. fprintf(stderr, "failed to reserve mmio range: %d\n", ret);
  92. return 1;
  93. }
  94. int mem_size = 0x9000;
  95. int mem_fd = syscall(SYS_memfd_create, "guest_mem", MFD_CLOEXEC | MFD_ALLOW_SEALING);
  96. if (mem_fd < 0) {
  97. fprintf(stderr, "failed to create guest memfd: %d\n", errno);
  98. return 1;
  99. }
  100. ret = ftruncate(mem_fd, mem_size);
  101. if (ret) {
  102. fprintf(stderr, "failed to set size of guest memory: %d\n", errno);
  103. return 1;
  104. }
  105. uint8_t *mem = mmap(NULL, mem_size, PROT_READ | PROT_WRITE, MAP_SHARED, mem_fd, 0);
  106. if (mem == MAP_FAILED) {
  107. fprintf(stderr, "failed to mmap guest memory: %d\n", errno);
  108. return 1;
  109. }
  110. fcntl(mem_fd, F_ADD_SEALS, F_SEAL_SHRINK);
  111. memcpy(mem + LOAD_ADDRESS, code, sizeof(code));
  112. struct crosvm_memory *mem_obj;
  113. ret = crosvm_create_memory(crosvm, mem_fd, 0, mem_size, 0, false, true, &mem_obj);
  114. if (ret) {
  115. fprintf(stderr, "failed to create memory in crosvm: %d\n", ret);
  116. return 1;
  117. }
  118. struct crosvm_vcpu *vcpus[32];
  119. pthread_t vcpu_threads[32];
  120. uint32_t vcpu_count;
  121. for (vcpu_count = 0; vcpu_count < 32; vcpu_count++) {
  122. ret = crosvm_get_vcpu(crosvm, vcpu_count, &vcpus[vcpu_count]);
  123. if (ret == -ENOENT)
  124. break;
  125. if (ret) {
  126. fprintf(stderr, "error while getting all vcpus: %d\n", ret);
  127. return 1;
  128. }
  129. pthread_create(&vcpu_threads[vcpu_count], NULL, vcpu_thread, vcpus[vcpu_count]);
  130. }
  131. ret = crosvm_start(crosvm);
  132. if (ret) {
  133. fprintf(stderr, "failed to tell crosvm to start: %d\n", ret);
  134. return 1;
  135. }
  136. uint64_t dummy;
  137. read(g_kill_evt, &dummy, 8);
  138. uint8_t dirty_log[2] = {0};
  139. ret = crosvm_memory_get_dirty_log(crosvm, mem_obj, dirty_log);
  140. if (ret) {
  141. fprintf(stderr, "failed to get dirty log: %d\n", ret);
  142. return 1;
  143. }
  144. if (dirty_log[1] != 0x1) {
  145. fprintf(stderr, "dirty log does not have expected bits: %x\n", dirty_log[1]);
  146. return 1;
  147. }
  148. uint64_t val = *(uint64_t*)(&mem[SI_ADDRESS]);
  149. if (val != BL_VALUE) {
  150. fprintf(stderr, "memory does not have expected value %d\n", val);
  151. return 1;
  152. }
  153. return 0;
  154. }