xref: /openbmc/linux/samples/acrn/vm-sample.c (revision 8938c48f)
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 
23 #define GUEST_MEMORY_SIZE	(1024*1024)
24 void *guest_memory;
25 
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;
29 
30 __u16 vcpu_num;
31 __u16 vmid;
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);
34 
35 int hsm_fd;
36 int is_running = 1;
37 
38 void vm_exit(int sig)
39 {
40 	sig = sig;
41 
42 	is_running = 0;
43 	ioctl(hsm_fd, ACRN_IOCTL_PAUSE_VM, vmid);
44 	ioctl(hsm_fd, ACRN_IOCTL_DESTROY_IOREQ_CLIENT, 0);
45 }
46 
47 int main(int argc, char **argv)
48 {
49 	int vcpu_id, ret;
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;
55 
56 	argc = argc;
57 	argv = argv;
58 
59 	guest_memory = memalign(4096, GUEST_MEMORY_SIZE);
60 	if (!guest_memory) {
61 		printf("No enough memory!\n");
62 		return -1;
63 	}
64 	hsm_fd = open("/dev/acrn_hsm", O_RDWR|O_CLOEXEC);
65 
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;
72 
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);
81 
82 	memcpy(guest_memory, guest16, guest16_end-guest16);
83 
84 	/* setup vcpu registers */
85 	memset(&regs, 0, sizeof(regs));
86 	regs.vcpu_id = 0;
87 	regs.vcpu_regs.rip = 0;
88 
89 	/* CR0_ET | CR0_NE */
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;
96 
97 	ret = ioctl(hsm_fd, ACRN_IOCTL_SET_VCPU_REGS, &regs);
98 	printf("Set up VM BSP registers! [%d]\n", ret);
99 
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);
103 
104 	/* run vm */
105 	ret = ioctl(hsm_fd, ACRN_IOCTL_START_VM, vmid);
106 	printf("Start VM! [%d]\n", ret);
107 
108 	signal(SIGINT, vm_exit);
109 	while (is_running) {
110 		ret = ioctl(hsm_fd, ACRN_IOCTL_ATTACH_IOREQ_CLIENT, 0);
111 
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) {
117 					int bytes, port, in;
118 
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);
123 
124 					notify.vmid = vmid;
125 					notify.vcpu = vcpu_id;
126 					ioctl(hsm_fd, ACRN_IOCTL_NOTIFY_REQUEST_FINISH, &notify);
127 				}
128 		}
129 	}
130 
131 	ret = ioctl(hsm_fd, ACRN_IOCTL_DESTROY_VM, NULL);
132 	printf("Destroy VM! [%d]\n", ret);
133 	close(hsm_fd);
134 	free(guest_memory);
135 	return 0;
136 }
137