xref: /kvmtool/kvm-cpu.c (revision af7b08685968ff8f6ffdf5cac1ef067688bce8c6)
1 #include "kvm/kvm-cpu.h"
2 
3 #include "kvm/symbol.h"
4 #include "kvm/util.h"
5 #include "kvm/kvm.h"
6 
7 #include <sys/ioctl.h>
8 #include <sys/mman.h>
9 #include <signal.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <errno.h>
13 #include <stdio.h>
14 
15 extern struct kvm_cpu *kvm_cpus[KVM_NR_CPUS];
16 extern __thread struct kvm_cpu *current_kvm_cpu;
17 
18 void kvm_cpu__enable_singlestep(struct kvm_cpu *vcpu)
19 {
20 	struct kvm_guest_debug debug = {
21 		.control	= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_SINGLESTEP,
22 	};
23 
24 	if (ioctl(vcpu->vcpu_fd, KVM_SET_GUEST_DEBUG, &debug) < 0)
25 		pr_warning("KVM_SET_GUEST_DEBUG failed");
26 }
27 
28 void kvm_cpu__run(struct kvm_cpu *vcpu)
29 {
30 	int err;
31 
32 	err = ioctl(vcpu->vcpu_fd, KVM_RUN, 0);
33 	if (err && (errno != EINTR && errno != EAGAIN))
34 		die_perror("KVM_RUN failed");
35 }
36 
37 static void kvm_cpu_signal_handler(int signum)
38 {
39 	if (signum == SIGKVMEXIT) {
40 		if (current_kvm_cpu && current_kvm_cpu->is_running) {
41 			current_kvm_cpu->is_running = false;
42 			pthread_kill(pthread_self(), SIGKVMEXIT);
43 		}
44 	} else if (signum == SIGKVMPAUSE) {
45 		current_kvm_cpu->paused = 1;
46 	}
47 }
48 
49 static void kvm_cpu__handle_coalesced_mmio(struct kvm_cpu *cpu)
50 {
51 	if (cpu->ring) {
52 		while (cpu->ring->first != cpu->ring->last) {
53 			struct kvm_coalesced_mmio *m;
54 			m = &cpu->ring->coalesced_mmio[cpu->ring->first];
55 			kvm__emulate_mmio(cpu->kvm,
56 					m->phys_addr,
57 					m->data,
58 					m->len,
59 					1);
60 			cpu->ring->first = (cpu->ring->first + 1) % KVM_COALESCED_MMIO_MAX;
61 		}
62 	}
63 }
64 
65 void kvm_cpu__reboot(void)
66 {
67 	int i;
68 
69 	for (i = 0; i < KVM_NR_CPUS; i++)
70 		if (kvm_cpus[i])
71 			pthread_kill(kvm_cpus[i]->thread, SIGKVMEXIT);
72 }
73 
74 int kvm_cpu__start(struct kvm_cpu *cpu)
75 {
76 	sigset_t sigset;
77 
78 	sigemptyset(&sigset);
79 	sigaddset(&sigset, SIGALRM);
80 
81 	pthread_sigmask(SIG_BLOCK, &sigset, NULL);
82 
83 	signal(SIGKVMEXIT, kvm_cpu_signal_handler);
84 	signal(SIGKVMPAUSE, kvm_cpu_signal_handler);
85 
86 	kvm_cpu__reset_vcpu(cpu);
87 
88 	if (cpu->kvm->single_step)
89 		kvm_cpu__enable_singlestep(cpu);
90 
91 	while (cpu->is_running) {
92 		if (cpu->paused) {
93 			kvm__notify_paused();
94 			cpu->paused = 0;
95 		}
96 
97 		kvm_cpu__run(cpu);
98 
99 		switch (cpu->kvm_run->exit_reason) {
100 		case KVM_EXIT_UNKNOWN:
101 			break;
102 		case KVM_EXIT_DEBUG:
103 			kvm_cpu__show_registers(cpu);
104 			kvm_cpu__show_code(cpu);
105 			break;
106 		case KVM_EXIT_IO: {
107 			bool ret;
108 
109 			ret = kvm__emulate_io(cpu->kvm,
110 					cpu->kvm_run->io.port,
111 					(u8 *)cpu->kvm_run +
112 					cpu->kvm_run->io.data_offset,
113 					cpu->kvm_run->io.direction,
114 					cpu->kvm_run->io.size,
115 					cpu->kvm_run->io.count);
116 
117 			if (!ret)
118 				goto panic_kvm;
119 			break;
120 		}
121 		case KVM_EXIT_MMIO: {
122 			bool ret;
123 
124 			ret = kvm__emulate_mmio(cpu->kvm,
125 					cpu->kvm_run->mmio.phys_addr,
126 					cpu->kvm_run->mmio.data,
127 					cpu->kvm_run->mmio.len,
128 					cpu->kvm_run->mmio.is_write);
129 
130 			if (!ret)
131 				goto panic_kvm;
132 			break;
133 		}
134 		case KVM_EXIT_INTR:
135 			if (cpu->is_running)
136 				break;
137 			goto exit_kvm;
138 		case KVM_EXIT_SHUTDOWN:
139 			goto exit_kvm;
140 		default:
141 			goto panic_kvm;
142 		}
143 		kvm_cpu__handle_coalesced_mmio(cpu);
144 	}
145 
146 exit_kvm:
147 	return 0;
148 
149 panic_kvm:
150 	return 1;
151 }
152