1
0

mini_plugin_template.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright 2018 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 {load_address}
  29. const uint8_t g_assembly_code[] = {{
  30. {assembly_code}
  31. }};
  32. /* These get defined by the code inserted below. */
  33. int setup_vm(struct crosvm *, void *mem);
  34. int handle_vpcu_init(struct crosvm_vcpu *, struct kvm_regs *, struct kvm_sregs *);
  35. int handle_vpcu_evt(struct crosvm_vcpu *, struct crosvm_vcpu_event evt);
  36. int check_result(struct crosvm *, void *mem);
  37. {src}
  38. struct vcpu_context {{
  39. struct crosvm_vcpu *vcpu;
  40. }};
  41. void *vcpu_thread(void *arg) {{
  42. struct vcpu_context *ctx = arg;
  43. struct crosvm_vcpu *vcpu = ctx->vcpu;
  44. struct crosvm_vcpu_event evt;
  45. int ret;
  46. while (crosvm_vcpu_wait(vcpu, &evt) == 0) {{
  47. if (evt.kind == CROSVM_VCPU_EVENT_KIND_INIT) {{
  48. struct kvm_regs regs;
  49. crosvm_vcpu_get_regs(vcpu, &regs);
  50. regs.rflags = 2;
  51. regs.rip = LOAD_ADDRESS;
  52. struct kvm_sregs sregs;
  53. crosvm_vcpu_get_sregs(vcpu, &sregs);
  54. sregs.cs.base = 0;
  55. sregs.cs.selector = 0;
  56. handle_vpcu_init(vcpu, &regs, &sregs);
  57. crosvm_vcpu_set_regs(vcpu, &regs);
  58. crosvm_vcpu_set_sregs(vcpu, &sregs);
  59. }} else {{
  60. ret = handle_vpcu_evt(vcpu, evt);
  61. if (ret)
  62. return NULL;
  63. }}
  64. crosvm_vcpu_resume(vcpu);
  65. }}
  66. return NULL;
  67. }}
  68. int main(int argc, char** argv) {{
  69. int i;
  70. uint64_t dummy = 1;
  71. struct crosvm *crosvm;
  72. int ret = crosvm_connect(&crosvm);
  73. if (ret) {{
  74. fprintf(stderr, "failed to connect to crosvm: %d\n", ret);
  75. return 1;
  76. }}
  77. int kill_evt = crosvm_get_shutdown_eventfd(crosvm);
  78. if (kill_evt < 0) {{
  79. fprintf(stderr, "failed to get kill eventfd: %d\n", kill_evt);
  80. return 1;
  81. }}
  82. int mem_size = {mem_size};
  83. int mem_fd = syscall(SYS_memfd_create, "guest_mem", MFD_CLOEXEC | MFD_ALLOW_SEALING);
  84. if (mem_fd < 0) {{
  85. fprintf(stderr, "failed to create guest memfd: %d\n", errno);
  86. return 1;
  87. }}
  88. ret = ftruncate(mem_fd, mem_size);
  89. if (ret) {{
  90. fprintf(stderr, "failed to set size of guest memory: %d\n", errno);
  91. return 1;
  92. }}
  93. uint8_t *mem = mmap(NULL, mem_size, PROT_READ | PROT_WRITE, MAP_SHARED, mem_fd, 0);
  94. if (mem == MAP_FAILED) {{
  95. fprintf(stderr, "failed to mmap guest memory: %d\n", errno);
  96. return 1;
  97. }}
  98. fcntl(mem_fd, F_ADD_SEALS, F_SEAL_SHRINK);
  99. memcpy(mem + LOAD_ADDRESS, g_assembly_code, sizeof(g_assembly_code));
  100. struct crosvm_memory *mem_obj;
  101. ret = crosvm_create_memory(crosvm, mem_fd, 0, mem_size, 0, false, false, &mem_obj);
  102. if (ret) {{
  103. fprintf(stderr, "failed to create memory in crosvm: %d\n", ret);
  104. return 1;
  105. }}
  106. ret = setup_vm(crosvm, mem);
  107. if (ret)
  108. return ret;
  109. struct crosvm_vcpu *vcpus[32];
  110. struct vcpu_context ctxs[32];
  111. pthread_t vcpu_threads[32];
  112. uint32_t vcpu_count;
  113. for (vcpu_count = 0; vcpu_count < 32; vcpu_count++) {{
  114. ret = crosvm_get_vcpu(crosvm, vcpu_count, &vcpus[vcpu_count]);
  115. if (ret == -ENOENT)
  116. break;
  117. if (ret) {{
  118. fprintf(stderr, "error while getting all vcpus: %d\n", ret);
  119. return 1;
  120. }}
  121. ctxs[vcpu_count].vcpu = vcpus[vcpu_count];
  122. pthread_create(&vcpu_threads[vcpu_count], NULL, vcpu_thread, &ctxs[vcpu_count]);
  123. }}
  124. ret = crosvm_start(crosvm);
  125. if (ret) {{
  126. fprintf(stderr, "failed to tell crosvm to start: %d\n", ret);
  127. return 1;
  128. }}
  129. ret = read(kill_evt, &dummy, sizeof(dummy));
  130. if (ret == -1) {{
  131. fprintf(stderr, "failed to read kill eventfd: %d\n", errno);
  132. return 1;
  133. }}
  134. return check_result(crosvm, mem);
  135. }}