Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * A sample program to run a User VM on the ACRN hypervisor
0004  *
0005  * This sample runs in a Service VM, which is a privileged VM of ACRN.
0006  * CONFIG_ACRN_HSM need to be enabled in the Service VM.
0007  *
0008  * Guest VM code in guest16.s will be executed after the VM launched.
0009  *
0010  * Copyright (C) 2020 Intel Corporation. All rights reserved.
0011  */
0012 #include <stdio.h>
0013 #include <stdint.h>
0014 #include <stdlib.h>
0015 #include <string.h>
0016 #include <malloc.h>
0017 #include <fcntl.h>
0018 #include <unistd.h>
0019 #include <signal.h>
0020 #include <sys/ioctl.h>
0021 #include <linux/acrn.h>
0022 
0023 #define GUEST_MEMORY_SIZE   (1024*1024)
0024 void *guest_memory;
0025 
0026 extern const unsigned char guest16[], guest16_end[];
0027 static char io_request_page[4096] __attribute__((aligned(4096)));
0028 static struct acrn_io_request *io_req_buf = (struct acrn_io_request *)io_request_page;
0029 
0030 __u16 vcpu_num;
0031 __u16 vmid;
0032 /* POST_STANDARD_VM_UUID1, refer to https://github.com/projectacrn/acrn-hypervisor/blob/master/hypervisor/include/common/vm_uuids.h */
0033 guid_t vm_uuid = GUID_INIT(0x385479d2, 0xd625, 0xe811, 0x86, 0x4e, 0xcb, 0x7a, 0x18, 0xb3, 0x46, 0x43);
0034 
0035 int hsm_fd;
0036 int is_running = 1;
0037 
0038 void vm_exit(int sig)
0039 {
0040     sig = sig;
0041 
0042     is_running = 0;
0043     ioctl(hsm_fd, ACRN_IOCTL_PAUSE_VM, vmid);
0044     ioctl(hsm_fd, ACRN_IOCTL_DESTROY_IOREQ_CLIENT, 0);
0045 }
0046 
0047 int main(int argc, char **argv)
0048 {
0049     int vcpu_id, ret;
0050     struct acrn_vm_creation create_vm = {0};
0051     struct acrn_vm_memmap ram_map = {0};
0052     struct acrn_vcpu_regs regs;
0053     struct acrn_io_request *io_req;
0054     struct acrn_ioreq_notify __attribute__((aligned(8))) notify;
0055 
0056     argc = argc;
0057     argv = argv;
0058 
0059     guest_memory = memalign(4096, GUEST_MEMORY_SIZE);
0060     if (!guest_memory) {
0061         printf("No enough memory!\n");
0062         return -1;
0063     }
0064     hsm_fd = open("/dev/acrn_hsm", O_RDWR|O_CLOEXEC);
0065 
0066     memcpy(&create_vm.uuid, &vm_uuid, 16);
0067     create_vm.ioreq_buf = (__u64)io_req_buf;
0068     ret = ioctl(hsm_fd, ACRN_IOCTL_CREATE_VM, &create_vm);
0069     printf("Created VM! [%d]\n", ret);
0070     vcpu_num = create_vm.vcpu_num;
0071     vmid = create_vm.vmid;
0072 
0073     /* setup guest memory */
0074     ram_map.type = ACRN_MEMMAP_RAM;
0075     ram_map.vma_base = (__u64)guest_memory;
0076     ram_map.len = GUEST_MEMORY_SIZE;
0077     ram_map.user_vm_pa = 0;
0078     ram_map.attr = ACRN_MEM_ACCESS_RWX;
0079     ret = ioctl(hsm_fd, ACRN_IOCTL_SET_MEMSEG, &ram_map);
0080     printf("Set up VM memory! [%d]\n", ret);
0081 
0082     memcpy(guest_memory, guest16, guest16_end-guest16);
0083 
0084     /* setup vcpu registers */
0085     memset(&regs, 0, sizeof(regs));
0086     regs.vcpu_id = 0;
0087     regs.vcpu_regs.rip = 0;
0088 
0089     /* CR0_ET | CR0_NE */
0090     regs.vcpu_regs.cr0 = 0x30U;
0091     regs.vcpu_regs.cs_ar = 0x009FU;
0092     regs.vcpu_regs.cs_sel = 0xF000U;
0093     regs.vcpu_regs.cs_limit = 0xFFFFU;
0094     regs.vcpu_regs.cs_base = 0 & 0xFFFF0000UL;
0095     regs.vcpu_regs.rip = 0 & 0xFFFFUL;
0096 
0097     ret = ioctl(hsm_fd, ACRN_IOCTL_SET_VCPU_REGS, &regs);
0098     printf("Set up VM BSP registers! [%d]\n", ret);
0099 
0100     /* create an ioreq client for this VM */
0101     ret = ioctl(hsm_fd, ACRN_IOCTL_CREATE_IOREQ_CLIENT, 0);
0102     printf("Created IO request client! [%d]\n", ret);
0103 
0104     /* run vm */
0105     ret = ioctl(hsm_fd, ACRN_IOCTL_START_VM, vmid);
0106     printf("Start VM! [%d]\n", ret);
0107 
0108     signal(SIGINT, vm_exit);
0109     while (is_running) {
0110         ret = ioctl(hsm_fd, ACRN_IOCTL_ATTACH_IOREQ_CLIENT, 0);
0111 
0112         for (vcpu_id = 0; vcpu_id < vcpu_num; vcpu_id++) {
0113             io_req = &io_req_buf[vcpu_id];
0114             if ((__sync_add_and_fetch(&io_req->processed, 0) == ACRN_IOREQ_STATE_PROCESSING)
0115                     && (!io_req->kernel_handled))
0116                 if (io_req->type == ACRN_IOREQ_TYPE_PORTIO) {
0117                     int bytes, port, in;
0118 
0119                     port = io_req->reqs.pio_request.address;
0120                     bytes = io_req->reqs.pio_request.size;
0121                     in = (io_req->reqs.pio_request.direction == ACRN_IOREQ_DIR_READ);
0122                     printf("Guest VM %s PIO[%x] with size[%x]\n", in ? "read" : "write", port, bytes);
0123 
0124                     notify.vmid = vmid;
0125                     notify.vcpu = vcpu_id;
0126                     ioctl(hsm_fd, ACRN_IOCTL_NOTIFY_REQUEST_FINISH, &notify);
0127                 }
0128         }
0129     }
0130 
0131     ret = ioctl(hsm_fd, ACRN_IOCTL_DESTROY_VM, NULL);
0132     printf("Destroy VM! [%d]\n", ret);
0133     close(hsm_fd);
0134     free(guest_memory);
0135     return 0;
0136 }