vm-sample.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * A sample program to run a User VM on the ACRN hypervisor
  4. *
  5. * This sample runs in a Service VM, which is a privileged VM of ACRN.
  6. * CONFIG_ACRN_HSM need to be enabled in the Service VM.
  7. *
  8. * Guest VM code in guest16.s will be executed after the VM launched.
  9. *
  10. * Copyright (C) 2020 Intel Corporation. All rights reserved.
  11. */
  12. #include <stdio.h>
  13. #include <stdint.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <malloc.h>
  17. #include <fcntl.h>
  18. #include <unistd.h>
  19. #include <signal.h>
  20. #include <sys/ioctl.h>
  21. #include <linux/acrn.h>
  22. #define GUEST_MEMORY_SIZE (1024*1024)
  23. void *guest_memory;
  24. extern const unsigned char guest16[], guest16_end[];
  25. static char io_request_page[4096] __attribute__((aligned(4096)));
  26. static struct acrn_io_request *io_req_buf = (struct acrn_io_request *)io_request_page;
  27. __u16 vcpu_num;
  28. __u16 vmid;
  29. /* POST_STANDARD_VM_UUID1, refer to https://github.com/projectacrn/acrn-hypervisor/blob/master/hypervisor/include/common/vm_uuids.h */
  30. guid_t vm_uuid = GUID_INIT(0x385479d2, 0xd625, 0xe811, 0x86, 0x4e, 0xcb, 0x7a, 0x18, 0xb3, 0x46, 0x43);
  31. int hsm_fd;
  32. int is_running = 1;
  33. void vm_exit(int sig)
  34. {
  35. sig = sig;
  36. is_running = 0;
  37. ioctl(hsm_fd, ACRN_IOCTL_PAUSE_VM, vmid);
  38. ioctl(hsm_fd, ACRN_IOCTL_DESTROY_IOREQ_CLIENT, 0);
  39. }
  40. int main(int argc, char **argv)
  41. {
  42. int vcpu_id, ret;
  43. struct acrn_vm_creation create_vm = {0};
  44. struct acrn_vm_memmap ram_map = {0};
  45. struct acrn_vcpu_regs regs;
  46. struct acrn_io_request *io_req;
  47. struct acrn_ioreq_notify __attribute__((aligned(8))) notify;
  48. argc = argc;
  49. argv = argv;
  50. guest_memory = memalign(4096, GUEST_MEMORY_SIZE);
  51. if (!guest_memory) {
  52. printf("No enough memory!\n");
  53. return -1;
  54. }
  55. hsm_fd = open("/dev/acrn_hsm", O_RDWR|O_CLOEXEC);
  56. memcpy(&create_vm.uuid, &vm_uuid, 16);
  57. create_vm.ioreq_buf = (__u64)io_req_buf;
  58. ret = ioctl(hsm_fd, ACRN_IOCTL_CREATE_VM, &create_vm);
  59. printf("Created VM! [%d]\n", ret);
  60. vcpu_num = create_vm.vcpu_num;
  61. vmid = create_vm.vmid;
  62. /* setup guest memory */
  63. ram_map.type = ACRN_MEMMAP_RAM;
  64. ram_map.vma_base = (__u64)guest_memory;
  65. ram_map.len = GUEST_MEMORY_SIZE;
  66. ram_map.user_vm_pa = 0;
  67. ram_map.attr = ACRN_MEM_ACCESS_RWX;
  68. ret = ioctl(hsm_fd, ACRN_IOCTL_SET_MEMSEG, &ram_map);
  69. printf("Set up VM memory! [%d]\n", ret);
  70. memcpy(guest_memory, guest16, guest16_end-guest16);
  71. /* setup vcpu registers */
  72. memset(&regs, 0, sizeof(regs));
  73. regs.vcpu_id = 0;
  74. regs.vcpu_regs.rip = 0;
  75. /* CR0_ET | CR0_NE */
  76. regs.vcpu_regs.cr0 = 0x30U;
  77. regs.vcpu_regs.cs_ar = 0x009FU;
  78. regs.vcpu_regs.cs_sel = 0xF000U;
  79. regs.vcpu_regs.cs_limit = 0xFFFFU;
  80. regs.vcpu_regs.cs_base = 0 & 0xFFFF0000UL;
  81. regs.vcpu_regs.rip = 0 & 0xFFFFUL;
  82. ret = ioctl(hsm_fd, ACRN_IOCTL_SET_VCPU_REGS, &regs);
  83. printf("Set up VM BSP registers! [%d]\n", ret);
  84. /* create an ioreq client for this VM */
  85. ret = ioctl(hsm_fd, ACRN_IOCTL_CREATE_IOREQ_CLIENT, 0);
  86. printf("Created IO request client! [%d]\n", ret);
  87. /* run vm */
  88. ret = ioctl(hsm_fd, ACRN_IOCTL_START_VM, vmid);
  89. printf("Start VM! [%d]\n", ret);
  90. signal(SIGINT, vm_exit);
  91. while (is_running) {
  92. ret = ioctl(hsm_fd, ACRN_IOCTL_ATTACH_IOREQ_CLIENT, 0);
  93. for (vcpu_id = 0; vcpu_id < vcpu_num; vcpu_id++) {
  94. io_req = &io_req_buf[vcpu_id];
  95. if ((__sync_add_and_fetch(&io_req->processed, 0) == ACRN_IOREQ_STATE_PROCESSING)
  96. && (!io_req->kernel_handled))
  97. if (io_req->type == ACRN_IOREQ_TYPE_PORTIO) {
  98. int bytes, port, in;
  99. port = io_req->reqs.pio_request.address;
  100. bytes = io_req->reqs.pio_request.size;
  101. in = (io_req->reqs.pio_request.direction == ACRN_IOREQ_DIR_READ);
  102. printf("Guest VM %s PIO[%x] with size[%x]\n", in ? "read" : "write", port, bytes);
  103. notify.vmid = vmid;
  104. notify.vcpu = vcpu_id;
  105. ioctl(hsm_fd, ACRN_IOCTL_NOTIFY_REQUEST_FINISH, &notify);
  106. }
  107. }
  108. }
  109. ret = ioctl(hsm_fd, ACRN_IOCTL_DESTROY_VM, NULL);
  110. printf("Destroy VM! [%d]\n", ret);
  111. close(hsm_fd);
  112. free(guest_memory);
  113. return 0;
  114. }