-
Notifications
You must be signed in to change notification settings - Fork 71
/
main.c
277 lines (227 loc) · 6.16 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
* KVM API Sample.
* author: Xu He Jie [email protected]
*/
#include <stdio.h>
#include <memory.h>
#include <sys/mman.h>
#include <pthread.h>
#include <linux/kvm.h>
#include <fcntl.h>
#include <stdlib.h>
#include <assert.h>
#define KVM_DEVICE "/dev/kvm"
#define RAM_SIZE 512000000
#define CODE_START 0x1000
#define BINARY_FILE "test.bin"
struct kvm {
int dev_fd;
int vm_fd;
__u64 ram_size;
__u64 ram_start;
int kvm_version;
struct kvm_userspace_memory_region mem;
struct vcpu *vcpus;
int vcpu_number;
};
struct vcpu {
int vcpu_id;
int vcpu_fd;
pthread_t vcpu_thread;
struct kvm_run *kvm_run;
int kvm_run_mmap_size;
struct kvm_regs regs;
struct kvm_sregs sregs;
void *(*vcpu_thread_func)(void *);
};
void kvm_reset_vcpu (struct vcpu *vcpu) {
if (ioctl(vcpu->vcpu_fd, KVM_GET_SREGS, &(vcpu->sregs)) < 0) {
perror("can not get sregs\n");
exit(1);
}
vcpu->sregs.cs.selector = CODE_START;
vcpu->sregs.cs.base = CODE_START * 16;
vcpu->sregs.ss.selector = CODE_START;
vcpu->sregs.ss.base = CODE_START * 16;
vcpu->sregs.ds.selector = CODE_START;
vcpu->sregs.ds.base = CODE_START *16;
vcpu->sregs.es.selector = CODE_START;
vcpu->sregs.es.base = CODE_START * 16;
vcpu->sregs.fs.selector = CODE_START;
vcpu->sregs.fs.base = CODE_START * 16;
vcpu->sregs.gs.selector = CODE_START;
if (ioctl(vcpu->vcpu_fd, KVM_SET_SREGS, &vcpu->sregs) < 0) {
perror("can not set sregs");
exit(1);
}
vcpu->regs.rflags = 0x0000000000000002ULL;
vcpu->regs.rip = 0;
vcpu->regs.rsp = 0xffffffff;
vcpu->regs.rbp= 0;
if (ioctl(vcpu->vcpu_fd, KVM_SET_REGS, &(vcpu->regs)) < 0) {
perror("KVM SET REGS\n");
exit(1);
}
}
void *kvm_cpu_thread(void *data) {
struct kvm *kvm = (struct kvm *)data;
int ret = 0;
kvm_reset_vcpu(kvm->vcpus);
while (1) {
printf("KVM start run\n");
ret = ioctl(kvm->vcpus->vcpu_fd, KVM_RUN, 0);
if (ret < 0) {
fprintf(stderr, "KVM_RUN failed\n");
exit(1);
}
switch (kvm->vcpus->kvm_run->exit_reason) {
case KVM_EXIT_UNKNOWN:
printf("KVM_EXIT_UNKNOWN\n");
break;
case KVM_EXIT_DEBUG:
printf("KVM_EXIT_DEBUG\n");
break;
case KVM_EXIT_IO:
printf("KVM_EXIT_IO\n");
printf("out port: %d, data: %d\n",
kvm->vcpus->kvm_run->io.port,
*(int *)((char *)(kvm->vcpus->kvm_run) + kvm->vcpus->kvm_run->io.data_offset)
);
sleep(1);
break;
case KVM_EXIT_MMIO:
printf("KVM_EXIT_MMIO\n");
break;
case KVM_EXIT_INTR:
printf("KVM_EXIT_INTR\n");
break;
case KVM_EXIT_SHUTDOWN:
printf("KVM_EXIT_SHUTDOWN\n");
goto exit_kvm;
break;
default:
printf("KVM PANIC\n");
goto exit_kvm;
}
}
exit_kvm:
return 0;
}
void load_binary(struct kvm *kvm) {
int fd = open(BINARY_FILE, O_RDONLY);
if (fd < 0) {
fprintf(stderr, "can not open binary file\n");
exit(1);
}
int ret = 0;
char *p = (char *)kvm->ram_start;
while(1) {
ret = read(fd, p, 4096);
if (ret <= 0) {
break;
}
printf("read size: %d", ret);
p += ret;
}
}
struct kvm *kvm_init(void) {
struct kvm *kvm = malloc(sizeof(struct kvm));
kvm->dev_fd = open(KVM_DEVICE, O_RDWR);
if (kvm->dev_fd < 0) {
perror("open kvm device fault: ");
return NULL;
}
kvm->kvm_version = ioctl(kvm->dev_fd, KVM_GET_API_VERSION, 0);
return kvm;
}
void kvm_clean(struct kvm *kvm) {
assert (kvm != NULL);
close(kvm->dev_fd);
free(kvm);
}
int kvm_create_vm(struct kvm *kvm, int ram_size) {
int ret = 0;
kvm->vm_fd = ioctl(kvm->dev_fd, KVM_CREATE_VM, 0);
if (kvm->vm_fd < 0) {
perror("can not create vm");
return -1;
}
kvm->ram_size = ram_size;
kvm->ram_start = (__u64)mmap(NULL, kvm->ram_size,
PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE,
-1, 0);
if ((void *)kvm->ram_start == MAP_FAILED) {
perror("can not mmap ram");
return -1;
}
kvm->mem.slot = 0;
kvm->mem.guest_phys_addr = 0;
kvm->mem.memory_size = kvm->ram_size;
kvm->mem.userspace_addr = kvm->ram_start;
ret = ioctl(kvm->vm_fd, KVM_SET_USER_MEMORY_REGION, &(kvm->mem));
if (ret < 0) {
perror("can not set user memory region");
return ret;
}
return ret;
}
void kvm_clean_vm(struct kvm *kvm) {
close(kvm->vm_fd);
munmap((void *)kvm->ram_start, kvm->ram_size);
}
struct vcpu *kvm_init_vcpu(struct kvm *kvm, int vcpu_id, void *(*fn)(void *)) {
struct vcpu *vcpu = malloc(sizeof(struct vcpu));
vcpu->vcpu_id = 0;
vcpu->vcpu_fd = ioctl(kvm->vm_fd, KVM_CREATE_VCPU, vcpu->vcpu_id);
if (vcpu->vcpu_fd < 0) {
perror("can not create vcpu");
return NULL;
}
vcpu->kvm_run_mmap_size = ioctl(kvm->dev_fd, KVM_GET_VCPU_MMAP_SIZE, 0);
if (vcpu->kvm_run_mmap_size < 0) {
perror("can not get vcpu mmsize");
return NULL;
}
printf("%d\n", vcpu->kvm_run_mmap_size);
vcpu->kvm_run = mmap(NULL, vcpu->kvm_run_mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED, vcpu->vcpu_fd, 0);
if (vcpu->kvm_run == MAP_FAILED) {
perror("can not mmap kvm_run");
return NULL;
}
vcpu->vcpu_thread_func = fn;
return vcpu;
}
void kvm_clean_vcpu(struct vcpu *vcpu) {
munmap(vcpu->kvm_run, vcpu->kvm_run_mmap_size);
close(vcpu->vcpu_fd);
}
void kvm_run_vm(struct kvm *kvm) {
int i = 0;
for (i = 0; i < kvm->vcpu_number; i++) {
if (pthread_create(&(kvm->vcpus->vcpu_thread), (const pthread_attr_t *)NULL, kvm->vcpus[i].vcpu_thread_func, kvm) != 0) {
perror("can not create kvm thread");
exit(1);
}
}
pthread_join(kvm->vcpus->vcpu_thread, NULL);
}
int main(int argc, char **argv) {
int ret = 0;
struct kvm *kvm = kvm_init();
if (kvm == NULL) {
fprintf(stderr, "kvm init fauilt\n");
return -1;
}
if (kvm_create_vm(kvm, RAM_SIZE) < 0) {
fprintf(stderr, "create vm fault\n");
return -1;
}
load_binary(kvm);
// only support one vcpu now
kvm->vcpu_number = 1;
kvm->vcpus = kvm_init_vcpu(kvm, 0, kvm_cpu_thread);
kvm_run_vm(kvm);
kvm_clean_vm(kvm);
kvm_clean_vcpu(kvm->vcpus);
kvm_clean(kvm);
}