• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018 The Chromium OS Authors. All rights reserved.
3  * Use of this source code is governed by a BSD-style license that can be
4  * found in the LICENSE file.
5  */
6 
7 #include <errno.h>
8 #include <fcntl.h>
9 #include <linux/memfd.h>
10 #include <pthread.h>
11 #include <stdint.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <sys/mman.h>
16 #include <sys/syscall.h>
17 #include <time.h>
18 #include <unistd.h>
19 
20 #include "crosvm.h"
21 
22 #ifndef F_LINUX_SPECIFIC_BASE
23 #define F_LINUX_SPECIFIC_BASE 1024
24 #endif
25 
26 #ifndef F_ADD_SEALS
27 #define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9)
28 #endif
29 
30 #ifndef F_SEAL_SHRINK
31 #define F_SEAL_SHRINK 0x0002
32 #endif
33 
34 #define LOAD_ADDRESS {load_address}
35 
36 const uint8_t g_assembly_code[] = {{
37     {assembly_code}
38 }};
39 
40 /* These get defined by the code inserted below. */
41 int setup_vm(struct crosvm *, void *mem);
42 int handle_vpcu_init(struct crosvm_vcpu *, struct kvm_regs *, struct kvm_sregs *);
43 int handle_vpcu_evt(struct crosvm_vcpu *, struct crosvm_vcpu_event evt);
44 int check_result(struct crosvm *, void *mem);
45 {src}
46 
47 struct vcpu_context {{
48     struct crosvm_vcpu *vcpu;
49 }};
50 
vcpu_thread(void * arg)51 void *vcpu_thread(void *arg) {{
52     struct vcpu_context *ctx = arg;
53     struct crosvm_vcpu *vcpu = ctx->vcpu;
54     struct crosvm_vcpu_event evt;
55     int ret;
56     while (crosvm_vcpu_wait(vcpu, &evt) == 0) {{
57         if (evt.kind == CROSVM_VCPU_EVENT_KIND_INIT) {{
58             struct kvm_regs regs;
59             crosvm_vcpu_get_regs(vcpu, &regs);
60             regs.rflags = 2;
61             regs.rip = LOAD_ADDRESS;
62 
63             struct kvm_sregs sregs;
64             crosvm_vcpu_get_sregs(vcpu, &sregs);
65             sregs.cs.base = 0;
66             sregs.cs.selector = 0;
67 
68             handle_vpcu_init(vcpu, &regs, &sregs);
69             crosvm_vcpu_set_regs(vcpu, &regs);
70             crosvm_vcpu_set_sregs(vcpu, &sregs);
71         }} else {{
72             ret = handle_vpcu_evt(vcpu, evt);
73             if (ret)
74                 return NULL;
75         }}
76 
77         crosvm_vcpu_resume(vcpu);
78     }}
79 
80     return NULL;
81 }}
82 
main(int argc,char ** argv)83 int main(int argc, char** argv) {{
84     int i;
85     uint64_t dummy = 1;
86     struct crosvm *crosvm;
87     int ret = crosvm_connect(&crosvm);
88     if (ret) {{
89         fprintf(stderr, "failed to connect to crosvm: %d\n", ret);
90         return 1;
91     }}
92 
93     int kill_evt = crosvm_get_shutdown_eventfd(crosvm);
94     if (kill_evt < 0) {{
95         fprintf(stderr, "failed to get kill eventfd: %d\n", kill_evt);
96         return 1;
97     }}
98 
99     int mem_size = {mem_size};
100     int mem_fd = syscall(SYS_memfd_create, "guest_mem", MFD_CLOEXEC | MFD_ALLOW_SEALING);
101     if (mem_fd < 0) {{
102         fprintf(stderr, "failed to create guest memfd: %d\n", errno);
103         return 1;
104     }}
105     ret = ftruncate(mem_fd, mem_size);
106     if (ret) {{
107         fprintf(stderr, "failed to set size of guest memory: %d\n", errno);
108         return 1;
109     }}
110     uint8_t *mem = mmap(NULL, mem_size, PROT_READ | PROT_WRITE, MAP_SHARED, mem_fd, 0);
111     if (mem == MAP_FAILED) {{
112         fprintf(stderr, "failed to mmap guest memory: %d\n", errno);
113         return 1;
114     }}
115     fcntl(mem_fd, F_ADD_SEALS, F_SEAL_SHRINK);
116     memcpy(mem + LOAD_ADDRESS, g_assembly_code, sizeof(g_assembly_code));
117 
118     struct crosvm_memory *mem_obj;
119     ret = crosvm_create_memory(crosvm, mem_fd, 0, mem_size, 0, false, false, &mem_obj);
120     if (ret) {{
121         fprintf(stderr, "failed to create memory in crosvm: %d\n", ret);
122         return 1;
123     }}
124 
125     ret = setup_vm(crosvm, mem);
126     if (ret)
127         return ret;
128 
129     struct crosvm_vcpu *vcpus[32];
130     struct vcpu_context ctxs[32];
131     pthread_t vcpu_threads[32];
132     uint32_t vcpu_count;
133     for (vcpu_count = 0; vcpu_count < 32; vcpu_count++) {{
134         ret = crosvm_get_vcpu(crosvm, vcpu_count, &vcpus[vcpu_count]);
135         if (ret == -ENOENT)
136             break;
137 
138         if (ret) {{
139             fprintf(stderr, "error while getting all vcpus: %d\n", ret);
140             return 1;
141         }}
142         ctxs[vcpu_count].vcpu = vcpus[vcpu_count];
143         pthread_create(&vcpu_threads[vcpu_count], NULL, vcpu_thread, &ctxs[vcpu_count]);
144     }}
145 
146     ret = crosvm_start(crosvm);
147     if (ret) {{
148         fprintf(stderr, "failed to tell crosvm to start: %d\n", ret);
149         return 1;
150     }}
151 
152     ret = read(kill_evt, &dummy, sizeof(dummy));
153     if (ret == -1) {{
154         fprintf(stderr, "failed to read kill eventfd: %d\n", errno);
155         return 1;
156     }}
157 
158     return check_result(crosvm, mem);
159 }}
160