1 // SPDX-License-Identifier: GPL-2.0
3 * A sample program to run a User VM on the ACRN hypervisor
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.
8 * Guest VM code in guest16.s will be executed after the VM launched.
10 * Copyright (C) 2020 Intel Corporation. All rights reserved.
20 #include <sys/ioctl.h>
21 #include <linux/acrn.h>
23 #define GUEST_MEMORY_SIZE (1024*1024)
26 extern const unsigned char guest16[], guest16_end[];
27 static char io_request_page[4096] __attribute__((aligned(4096)));
28 static struct acrn_io_request *io_req_buf = (struct acrn_io_request *)io_request_page;
32 /* POST_STANDARD_VM_UUID1, refer to https://github.com/projectacrn/acrn-hypervisor/blob/master/hypervisor/include/common/vm_uuids.h */
33 guid_t vm_uuid = GUID_INIT(0x385479d2, 0xd625, 0xe811, 0x86, 0x4e, 0xcb, 0x7a, 0x18, 0xb3, 0x46, 0x43);
43 ioctl(hsm_fd, ACRN_IOCTL_PAUSE_VM, vmid);
44 ioctl(hsm_fd, ACRN_IOCTL_DESTROY_IOREQ_CLIENT, 0);
47 int main(int argc, char **argv)
50 struct acrn_vm_creation create_vm = {0};
51 struct acrn_vm_memmap ram_map = {0};
52 struct acrn_vcpu_regs regs;
53 struct acrn_io_request *io_req;
54 struct acrn_ioreq_notify __attribute__((aligned(8))) notify;
59 guest_memory = memalign(4096, GUEST_MEMORY_SIZE);
61 printf("No enough memory!\n");
64 hsm_fd = open("/dev/acrn_hsm", O_RDWR|O_CLOEXEC);
66 memcpy(&create_vm.uuid, &vm_uuid, 16);
67 create_vm.ioreq_buf = (__u64)io_req_buf;
68 ret = ioctl(hsm_fd, ACRN_IOCTL_CREATE_VM, &create_vm);
69 printf("Created VM! [%d]\n", ret);
70 vcpu_num = create_vm.vcpu_num;
71 vmid = create_vm.vmid;
73 /* setup guest memory */
74 ram_map.type = ACRN_MEMMAP_RAM;
75 ram_map.vma_base = (__u64)guest_memory;
76 ram_map.len = GUEST_MEMORY_SIZE;
77 ram_map.user_vm_pa = 0;
78 ram_map.attr = ACRN_MEM_ACCESS_RWX;
79 ret = ioctl(hsm_fd, ACRN_IOCTL_SET_MEMSEG, &ram_map);
80 printf("Set up VM memory! [%d]\n", ret);
82 memcpy(guest_memory, guest16, guest16_end-guest16);
84 /* setup vcpu registers */
85 memset(®s, 0, sizeof(regs));
87 regs.vcpu_regs.rip = 0;
90 regs.vcpu_regs.cr0 = 0x30U;
91 regs.vcpu_regs.cs_ar = 0x009FU;
92 regs.vcpu_regs.cs_sel = 0xF000U;
93 regs.vcpu_regs.cs_limit = 0xFFFFU;
94 regs.vcpu_regs.cs_base = 0 & 0xFFFF0000UL;
95 regs.vcpu_regs.rip = 0 & 0xFFFFUL;
97 ret = ioctl(hsm_fd, ACRN_IOCTL_SET_VCPU_REGS, ®s);
98 printf("Set up VM BSP registers! [%d]\n", ret);
100 /* create an ioreq client for this VM */
101 ret = ioctl(hsm_fd, ACRN_IOCTL_CREATE_IOREQ_CLIENT, 0);
102 printf("Created IO request client! [%d]\n", ret);
105 ret = ioctl(hsm_fd, ACRN_IOCTL_START_VM, vmid);
106 printf("Start VM! [%d]\n", ret);
108 signal(SIGINT, vm_exit);
110 ret = ioctl(hsm_fd, ACRN_IOCTL_ATTACH_IOREQ_CLIENT, 0);
112 for (vcpu_id = 0; vcpu_id < vcpu_num; vcpu_id++) {
113 io_req = &io_req_buf[vcpu_id];
114 if ((__sync_add_and_fetch(&io_req->processed, 0) == ACRN_IOREQ_STATE_PROCESSING)
115 && (!io_req->kernel_handled))
116 if (io_req->type == ACRN_IOREQ_TYPE_PORTIO) {
119 port = io_req->reqs.pio_request.address;
120 bytes = io_req->reqs.pio_request.size;
121 in = (io_req->reqs.pio_request.direction == ACRN_IOREQ_DIR_READ);
122 printf("Guest VM %s PIO[%x] with size[%x]\n", in ? "read" : "write", port, bytes);
125 notify.vcpu = vcpu_id;
126 ioctl(hsm_fd, ACRN_IOCTL_NOTIFY_REQUEST_FINISH, ¬ify);
131 ret = ioctl(hsm_fd, ACRN_IOCTL_DESTROY_VM, NULL);
132 printf("Destroy VM! [%d]\n", ret);