1 /*
2 * Copyright 2017 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 0x1000
35 #define STACK_BASE (LOAD_ADDRESS + 0x1000)
36 #define STACK_SIZE 0x1000
37 #define SUCCESS_ADDRESS 0x3000
38 #define KILL_ADDRESS 0x4000
39
40 /*
41 org 0x1000
42 bits 16
43
44 cli
45
46 ; Set entry 0x0 in the interrupt vector table
47 mov word [0x0], handle
48 mov word [0x2], 0x0
49
50 sti
51
52 ; Loop until interrupt is handled
53 loop:
54 cmp byte [si], 0x01
55 jne loop
56
57 cli
58
59 ; Signal that we are ready to end
60 end:
61 mov byte [es:0], 0x01
62 hlt
63
64 ; Handle the interrupt by halting
65 handle:
66 mov byte [si], 0x01
67 iret
68 */
69 const uint8_t g_code[] = {
70 0xfa, 0xc7, 0x06, 0x00, 0x00, 0x1b, 0x10, 0xc7, 0x06, 0x02, 0x00, 0x00,
71 0x00, 0xfb, 0x80, 0x3c, 0x01, 0x75, 0xfb, 0xfa, 0x26, 0xc6, 0x06, 0x00,
72 0x00, 0x01, 0xf4, 0xc6, 0x04, 0x01, 0xcf
73 };
74
75 struct vcpu_context {
76 struct crosvm_vcpu *vcpu;
77 int irqeventfd;
78 int kill_evt;
79 };
80
vcpu_thread(void * arg)81 void *vcpu_thread(void *arg) {
82 struct vcpu_context *ctx = arg;
83 struct crosvm_vcpu *vcpu = ctx->vcpu;
84 struct crosvm_vcpu_event evt;
85 uint64_t dummy = 1;
86 int i = 0;
87 int ret;
88 while (crosvm_vcpu_wait(vcpu, &evt) == 0) {
89 if (evt.kind == CROSVM_VCPU_EVENT_KIND_INIT) {
90 struct kvm_sregs sregs;
91 crosvm_vcpu_get_sregs(vcpu, &sregs);
92 sregs.cs.base = 0;
93 sregs.cs.selector = 0x0;
94 sregs.ss.base = 0;
95 sregs.ss.selector = 0x0;
96 sregs.es.base = KILL_ADDRESS;
97 sregs.es.selector = 0x0;
98 crosvm_vcpu_set_sregs(vcpu, &sregs);
99
100 struct kvm_regs regs;
101 crosvm_vcpu_get_regs(vcpu, ®s);
102 regs.rflags = 2;
103 regs.rip = LOAD_ADDRESS;
104 regs.rsp = STACK_BASE + STACK_SIZE;
105 regs.rsi = SUCCESS_ADDRESS;
106 crosvm_vcpu_set_regs(vcpu, ®s);
107
108 write(ctx->irqeventfd, &dummy, sizeof(dummy));
109 }
110
111 if (evt.kind == CROSVM_VCPU_EVENT_KIND_IO_ACCESS &&
112 evt.io_access.address_space == CROSVM_ADDRESS_SPACE_MMIO &&
113 evt.io_access.address == KILL_ADDRESS &&
114 evt.io_access.is_write &&
115 evt.io_access.length == 1 &&
116 evt.io_access.data[0] == 1)
117 {
118 write(ctx->kill_evt, &dummy, sizeof(dummy));
119 return NULL;
120 }
121
122 crosvm_vcpu_resume(vcpu);
123 }
124
125 return NULL;
126 }
127
main(int argc,char ** argv)128 int main(int argc, char** argv) {
129 int i;
130 uint64_t dummy = 1;
131 struct crosvm *crosvm;
132 int ret = crosvm_connect(&crosvm);
133 if (ret) {
134 fprintf(stderr, "failed to connect to crosvm: %d\n", ret);
135 return 1;
136 }
137
138 int kill_evt = crosvm_get_shutdown_eventfd(crosvm);
139 if (kill_evt < 0) {
140 fprintf(stderr, "failed to get kill eventfd: %d\n", kill_evt);
141 return 1;
142 }
143
144 crosvm_reserve_range(crosvm, CROSVM_ADDRESS_SPACE_MMIO, KILL_ADDRESS, 1);
145
146 struct crosvm_irq *irq;
147 ret = crosvm_create_irq_event(crosvm, 0, &irq);
148 if (ret) {
149 fprintf(stderr, "failed to create irq event: %d\n", ret);
150 return 1;
151 }
152
153 int irqeventfd = crosvm_irq_event_get_fd(irq);
154
155 int mem_size = 0x4000;
156 int mem_fd = syscall(SYS_memfd_create, "guest_mem", MFD_CLOEXEC | MFD_ALLOW_SEALING);
157 if (mem_fd < 0) {
158 fprintf(stderr, "failed to create guest memfd: %d\n", errno);
159 return 1;
160 }
161 ret = ftruncate(mem_fd, mem_size);
162 if (ret) {
163 fprintf(stderr, "failed to set size of guest memory: %d\n", errno);
164 return 1;
165 }
166 uint8_t *mem = mmap(NULL, mem_size, PROT_READ | PROT_WRITE, MAP_SHARED, mem_fd, 0);
167 if (mem == MAP_FAILED) {
168 fprintf(stderr, "failed to mmap guest memory: %d\n", errno);
169 return 1;
170 }
171 fcntl(mem_fd, F_ADD_SEALS, F_SEAL_SHRINK);
172 memcpy(mem + LOAD_ADDRESS, g_code, sizeof(g_code));
173
174 struct crosvm_memory *mem_obj;
175 ret = crosvm_create_memory(crosvm, mem_fd, 0, mem_size, 0, false, false, &mem_obj);
176 if (ret) {
177 fprintf(stderr, "failed to create memory in crosvm: %d\n", ret);
178 return 1;
179 }
180
181 struct crosvm_vcpu *vcpus[32];
182 struct vcpu_context ctxs[32];
183 pthread_t vcpu_threads[32];
184 uint32_t vcpu_count;
185 for (vcpu_count = 0; vcpu_count < 32; vcpu_count++) {
186 ret = crosvm_get_vcpu(crosvm, vcpu_count, &vcpus[vcpu_count]);
187 if (ret == -ENOENT)
188 break;
189
190 if (ret) {
191 fprintf(stderr, "error while getting all vcpus: %d\n", ret);
192 return 1;
193 }
194 ctxs[vcpu_count].vcpu = vcpus[vcpu_count];
195 ctxs[vcpu_count].irqeventfd = irqeventfd;
196 ctxs[vcpu_count].kill_evt = kill_evt;
197 pthread_create(&vcpu_threads[vcpu_count], NULL, vcpu_thread, &ctxs[vcpu_count]);
198 }
199
200 ret = crosvm_start(crosvm);
201 if (ret) {
202 fprintf(stderr, "failed to tell crosvm to start: %d\n", ret);
203 return 1;
204 }
205
206 ret = read(kill_evt, &dummy, sizeof(dummy));
207 if (ret == -1) {
208 fprintf(stderr, "failed to read kill eventfd: %d\n", errno);
209 return 1;
210 }
211
212 if (mem[SUCCESS_ADDRESS] != 0x01) {
213 fprintf(stderr, "interrupt was not handled: 0x%x\n", mem[SUCCESS_ADDRESS]);
214 return 1;
215 }
216
217 return 0;
218 }
219