vm-sample.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 needs 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 <fcntl.h>
  17. #include <unistd.h>
  18. #include <signal.h>
  19. #include <sys/ioctl.h>
  20. #include <linux/acrn.h>
  21. #define GUEST_MEMORY_SIZE (1024*1024)
  22. void *guest_memory;
  23. extern const unsigned char guest16[], guest16_end[];
  24. static char io_request_page[4096] __attribute__((aligned(4096)));
  25. static struct acrn_io_request *io_req_buf = (struct acrn_io_request *)io_request_page;
  26. __u16 vcpu_num;
  27. __u16 vmid;
  28. int hsm_fd;
  29. int is_running = 1;
  30. void vm_exit(int sig)
  31. {
  32. sig = sig;
  33. is_running = 0;
  34. ioctl(hsm_fd, ACRN_IOCTL_PAUSE_VM, vmid);
  35. ioctl(hsm_fd, ACRN_IOCTL_DESTROY_IOREQ_CLIENT, 0);
  36. }
  37. int main(int argc, char **argv)
  38. {
  39. int vcpu_id, ret;
  40. struct acrn_vm_creation create_vm = {0};
  41. struct acrn_vm_memmap ram_map = {0};
  42. struct acrn_vcpu_regs regs;
  43. struct acrn_io_request *io_req;
  44. struct acrn_ioreq_notify __attribute__((aligned(8))) notify;
  45. argc = argc;
  46. argv = argv;
  47. ret = posix_memalign(&guest_memory, 4096, GUEST_MEMORY_SIZE);
  48. if (ret < 0) {
  49. printf("Not enough memory!\n");
  50. return -1;
  51. }
  52. hsm_fd = open("/dev/acrn_hsm", O_RDWR|O_CLOEXEC);
  53. create_vm.ioreq_buf = (__u64)io_req_buf;
  54. ret = ioctl(hsm_fd, ACRN_IOCTL_CREATE_VM, &create_vm);
  55. printf("Created VM! [%d]\n", ret);
  56. vcpu_num = create_vm.vcpu_num;
  57. vmid = create_vm.vmid;
  58. /* setup guest memory */
  59. ram_map.type = ACRN_MEMMAP_RAM;
  60. ram_map.vma_base = (__u64)guest_memory;
  61. ram_map.len = GUEST_MEMORY_SIZE;
  62. ram_map.user_vm_pa = 0;
  63. ram_map.attr = ACRN_MEM_ACCESS_RWX;
  64. ret = ioctl(hsm_fd, ACRN_IOCTL_SET_MEMSEG, &ram_map);
  65. printf("Set up VM memory! [%d]\n", ret);
  66. memcpy(guest_memory, guest16, guest16_end-guest16);
  67. /* setup vcpu registers */
  68. memset(&regs, 0, sizeof(regs));
  69. regs.vcpu_id = 0;
  70. regs.vcpu_regs.rip = 0;
  71. /* CR0_ET | CR0_NE */
  72. regs.vcpu_regs.cr0 = 0x30U;
  73. regs.vcpu_regs.cs_ar = 0x009FU;
  74. regs.vcpu_regs.cs_sel = 0xF000U;
  75. regs.vcpu_regs.cs_limit = 0xFFFFU;
  76. regs.vcpu_regs.cs_base = 0 & 0xFFFF0000UL;
  77. regs.vcpu_regs.rip = 0 & 0xFFFFUL;
  78. ret = ioctl(hsm_fd, ACRN_IOCTL_SET_VCPU_REGS, &regs);
  79. printf("Set up VM BSP registers! [%d]\n", ret);
  80. /* create an ioreq client for this VM */
  81. ret = ioctl(hsm_fd, ACRN_IOCTL_CREATE_IOREQ_CLIENT, 0);
  82. printf("Created IO request client! [%d]\n", ret);
  83. /* run vm */
  84. ret = ioctl(hsm_fd, ACRN_IOCTL_START_VM, vmid);
  85. printf("Start VM! [%d]\n", ret);
  86. signal(SIGINT, vm_exit);
  87. while (is_running) {
  88. ret = ioctl(hsm_fd, ACRN_IOCTL_ATTACH_IOREQ_CLIENT, 0);
  89. for (vcpu_id = 0; vcpu_id < vcpu_num; vcpu_id++) {
  90. io_req = &io_req_buf[vcpu_id];
  91. if ((__sync_add_and_fetch(&io_req->processed, 0) == ACRN_IOREQ_STATE_PROCESSING)
  92. && (!io_req->kernel_handled))
  93. if (io_req->type == ACRN_IOREQ_TYPE_PORTIO) {
  94. int bytes, port, in;
  95. port = io_req->reqs.pio_request.address;
  96. bytes = io_req->reqs.pio_request.size;
  97. in = (io_req->reqs.pio_request.direction == ACRN_IOREQ_DIR_READ);
  98. printf("Guest VM %s PIO[%x] with size[%x]\n", in ? "read" : "write", port, bytes);
  99. notify.vmid = vmid;
  100. notify.vcpu = vcpu_id;
  101. ioctl(hsm_fd, ACRN_IOCTL_NOTIFY_REQUEST_FINISH, &notify);
  102. }
  103. }
  104. }
  105. ret = ioctl(hsm_fd, ACRN_IOCTL_DESTROY_VM, NULL);
  106. printf("Destroy VM! [%d]\n", ret);
  107. close(hsm_fd);
  108. free(guest_memory);
  109. return 0;
  110. }