vmm: remove the leading '%' from the printc vmcall
[akaros.git] / user / vmm / vmexit.c
1 /* Copyright (c) 2015-2016 Google Inc.
2  * See LICENSE for details. */
3
4 #include <parlib/common.h>
5 #include <vmm/virtio.h>
6 #include <vmm/virtio_mmio.h>
7 #include <vmm/virtio_ids.h>
8 #include <vmm/virtio_config.h>
9 #include <vmm/mmio.h>
10 #include <vmm/vmm.h>
11 #include <parlib/arch/trap.h>
12 #include <parlib/bitmask.h>
13 #include <parlib/stdio.h>
14
15 static bool pir_notif_is_set(struct vmm_gpcore_init *gpci)
16 {
17         return GET_BITMASK_BIT(gpci->posted_irq_desc,
18                                VMX_POSTED_OUTSTANDING_NOTIF);
19 }
20
21 /* Returns true if the hardware will trigger an IRQ for the guest.  These
22  * virtual IRQs are only processed under certain situations, like vmentry, and
23  * posted IRQs.  See 'Evaluation of Pending Virtual Interrupts' in the SDM. */
24 static bool virtual_irq_is_pending(struct guest_thread *gth)
25 {
26         struct vmm_gpcore_init *gpci = gth_to_gpci(gth);
27         uint8_t rvi, vppr;
28
29         /* Currently, the lower 4 bits are various ways to block IRQs, e.g.
30          * blocking by STI.  The other bits are must be 0.  Presumably any new
31          * bits are types of IRQ blocking. */
32         if (gth_to_vmtf(gth)->tf_intrinfo1)
33                 return false;
34         vppr = read_mmreg32((uintptr_t)gth_to_gpci(gth)->vapic_addr + 0xa0);
35         rvi = gth_to_vmtf(gth)->tf_guest_intr_status & 0xff;
36         return (rvi & 0xf0) > (vppr & 0xf0);
37 }
38
39 /* Blocks a guest pcore / thread until it has an IRQ pending.  Syncs with
40  * vmm_interrupt_guest(). */
41 static void sleep_til_irq(struct guest_thread *gth)
42 {
43         struct vmm_gpcore_init *gpci = gth_to_gpci(gth);
44
45         /* The invariant is that if an IRQ is posted, but not delivered, we will
46          * not sleep.  Anyone who posts an IRQ must signal after setting it.
47          * vmm_interrupt_guest() does this.  If we use alternate sources of IRQ
48          * posting, we'll need to revist this.  For more details, see the notes
49          * in the kernel IPI-IRC fast path.
50          *
51          * Although vmm_interrupt_guest() only writes OUTSTANDING_NOTIF, it's
52          * possible that the hardware attempted to post the interrupt.  In SDM
53          * parlance, the processor could have "recognized" the virtual IRQ, but
54          * not delivered it yet.  This could happen if the guest had executed
55          * "sti", but not "hlt" yet.  The IRQ was posted and recognized, but not
56          * delivered ("sti blocking").  Then the guest executes "hlt", and
57          * vmexits.  OUTSTANDING_NOTIF will be clear in this case.  RVI should
58          * be set - at least to the vector we just sent, but possibly to a
59          * greater vector if multiple were sent.  RVI should only be cleared
60          * after virtual IRQs were actually delivered.  So checking
61          * OUTSTANDING_NOTIF and RVI should suffice.
62          *
63          * Note that when we see a notif or pending virtual IRQ, we don't
64          * actually deliver the IRQ, we'll just restart the guest and the
65          * hardware will deliver the virtual IRQ at the appropriate time.
66          *
67          * The more traditional race here is if the halt starts concurrently
68          * with the post; that's why we sync with the mutex to make sure there
69          * is an ordering between the actual halt (this function) and the
70          * posting. */
71         uth_mutex_lock(gth->halt_mtx);
72         while (!(pir_notif_is_set(gpci) || virtual_irq_is_pending(gth)))
73                 uth_cond_var_wait(gth->halt_cv, gth->halt_mtx);
74         uth_mutex_unlock(gth->halt_mtx);
75 }
76
77 enum {
78         CPUID_0B_LEVEL_SMT = 0,
79         CPUID_0B_LEVEL_CORE
80 };
81
82 static bool handle_cpuid(struct guest_thread *gth)
83 {
84         struct vm_trapframe *vm_tf = gth_to_vmtf(gth);
85         struct virtual_machine *vm = gth_to_vm(gth);
86         uint32_t level = vm_tf->tf_rcx & 0x0F;
87
88         if (vm_tf->tf_rax != 0x0B)
89                 return FALSE;
90
91         vm_tf->tf_rip += 2;
92         vm_tf->tf_rax = 0;
93         vm_tf->tf_rbx = 0;
94         vm_tf->tf_rcx = level;
95         vm_tf->tf_rdx = gth->gpc_id;
96         if (level == CPUID_0B_LEVEL_SMT) {
97                 vm_tf->tf_rax = 0;
98                 vm_tf->tf_rbx = 1;
99                 vm_tf->tf_rcx |= ((level + 1) << 8);
100         }
101         if (level == CPUID_0B_LEVEL_CORE) {
102                 uint32_t shift = LOG2_UP(vm->nr_gpcs);
103
104                 if (shift > 0x1F)
105                         shift = 0x1F;
106                 vm_tf->tf_rax = shift;
107                 vm_tf->tf_rbx = vm->nr_gpcs;
108                 vm_tf->tf_rcx |= ((level + 1) << 8);
109         }
110
111         return TRUE;
112 }
113
114 static bool handle_ept_fault(struct guest_thread *gth)
115 {
116         struct vm_trapframe *vm_tf = gth_to_vmtf(gth);
117         struct virtual_machine *vm = gth_to_vm(gth);
118         uint64_t gpa, *regp;
119         uint8_t regx;
120         int store, size;
121         int advance;
122         int ret;
123
124         if (vm_tf->tf_flags & VMCTX_FL_EPT_VMR_BACKED) {
125                 ret = ros_syscall(SYS_populate_va, vm_tf->tf_guest_pa, 1, 0, 0,
126                                   0, 0);
127                 if (ret <= 0)
128                         panic("[user] handle_ept_fault: populate_va failed: ret = %d\n",
129                               ret);
130                 return TRUE;
131         }
132         ret = decode(gth, &gpa, &regx, &regp, &store, &size, &advance);
133
134         if (ret < 0)
135                 return FALSE;
136         if (ret == VM_PAGE_FAULT) {
137                 /* We were unable to translate RIP due to an ept fault */
138                 vm_tf->tf_trap_inject = VM_TRAP_VALID
139                                       | VM_TRAP_ERROR_CODE
140                                       | VM_TRAP_HARDWARE
141                                       | HW_TRAP_PAGE_FAULT;
142                 return TRUE;
143         }
144
145         assert(size >= 0);
146         /* TODO use helpers for some of these addr checks.  the fee/fec ones
147          * might be wrong too. */
148         for (int i = 0; i < VIRTIO_MMIO_MAX_NUM_DEV; i++) {
149                 if (vm->virtio_mmio_devices[i] == NULL)
150                         continue;
151                 if (PG_ADDR(gpa) != vm->virtio_mmio_devices[i]->addr)
152                         continue;
153                 /* TODO: can the guest cause us to spawn off infinite threads?
154                  */
155                 if (store)
156                         virtio_mmio_wr(vm, vm->virtio_mmio_devices[i], gpa,
157                                        size, (uint32_t *)regp);
158                 else
159                         *regp = virtio_mmio_rd(vm, vm->virtio_mmio_devices[i],
160                                                gpa, size);
161                 vm_tf->tf_rip += advance;
162                 return TRUE;
163         }
164         if (PG_ADDR(gpa) == 0xfec00000) {
165                 do_ioapic(gth, gpa, regx, regp, store);
166         } else if (PG_ADDR(gpa) == 0) {
167                 memmove(regp, &vm->low4k[gpa], size);
168         } else {
169                 fprintf(stderr, "EPT violation: can't handle %p\n", gpa);
170                 fprintf(stderr, "RIP %p, exit reason 0x%x\n", vm_tf->tf_rip,
171                                 vm_tf->tf_exit_reason);
172                 fprintf(stderr, "Returning 0xffffffff\n");
173                 showstatus(stderr, gth);
174                 /* Just fill the whole register for now. */
175                 *regp = (uint64_t) -1;
176                 return FALSE;
177         }
178         vm_tf->tf_rip += advance;
179         return TRUE;
180 }
181
182 static bool handle_vmcall_printc(struct guest_thread *gth)
183 {
184         struct vm_trapframe *vm_tf = gth_to_vmtf(gth);
185         uint8_t byte;
186
187         byte = vm_tf->tf_rdi;
188         printf("%c", byte);
189         fflush(stdout);
190         return TRUE;
191 }
192
193 static bool handle_vmcall_smpboot(struct guest_thread *gth)
194 {
195         struct vm_trapframe *vm_tf = gth_to_vmtf(gth);
196         struct vm_trapframe *vm_tf_ap;
197         struct virtual_machine *vm = gth_to_vm(gth);
198         int cur_pcores = vm->up_gpcs;
199
200         /* Check if we're guest pcore 0. Only the BSP is allowed to start APs.
201          */
202         if (vm_tf->tf_guest_pcoreid != 0) {
203                 fprintf(stderr,
204                         "Only guest pcore 0 is allowed to start APs. core was %ld\n",
205                         vm_tf->tf_guest_pcoreid);
206                 return FALSE;
207         }
208
209         /* Check if we've reached the maximum, if yes, blow out. */
210         if (vm->nr_gpcs == cur_pcores) {
211                 fprintf(stderr,
212                         "guest tried to start up too many cores. max was %ld, current up %ld\n",
213                         vm->nr_gpcs, cur_pcores);
214                 return FALSE;
215         }
216
217         /* Start up secondary core. */
218         vm_tf_ap = gpcid_to_vmtf(vm, cur_pcores);
219         /* We use the BSP's CR3 for now. This should be fine because they
220          * change it later anyway. */
221         vm_tf_ap->tf_cr3 = vm_tf->tf_cr3;
222         vm_tf_ap->tf_rip = vm_tf->tf_rdi;
223         vm_tf_ap->tf_rsp = vm_tf->tf_rsi;
224         vm_tf_ap->tf_rflags = FL_RSVD_1;
225
226         vm->up_gpcs++;
227
228         start_guest_thread(gpcid_to_gth(vm, cur_pcores));
229
230         return TRUE;
231 }
232
233 static bool handle_vmcall_get_tscfreq(struct guest_thread *gth)
234 {
235         struct vm_trapframe *vm_tf = gth_to_vmtf(gth);
236         struct vm_trapframe *vm_tf_ap;
237         struct virtual_machine *vm = gth_to_vm(gth);
238
239         vm_tf->tf_rax = get_tsc_freq() / 1000;
240         return TRUE;
241 }
242
243 static bool handle_vmcall(struct guest_thread *gth)
244 {
245         struct vm_trapframe *vm_tf = gth_to_vmtf(gth);
246         struct virtual_machine *vm = gth_to_vm(gth);
247         bool retval = FALSE;
248
249         if (vm->vmcall)
250                 return vm->vmcall(gth, vm_tf);
251
252         switch (vm_tf->tf_rax) {
253         case VMCALL_PRINTC:
254                 retval = handle_vmcall_printc(gth);
255                 break;
256         case VMCALL_SMPBOOT:
257                 retval = handle_vmcall_smpboot(gth);
258                 break;
259         case VMCALL_GET_TSCFREQ:
260                 retval = handle_vmcall_get_tscfreq(gth);
261                 break;
262         case VMCALL_TRACE_TF:
263                 trace_printf("  rax  0x%016lx\n",      vm_tf->tf_r11);
264                 trace_printf("  rbx  0x%016lx\n",      vm_tf->tf_rbx);
265                 trace_printf("  rcx  0x%016lx\n",      vm_tf->tf_rcx);
266                 trace_printf("  rdx  0x%016lx\n",      vm_tf->tf_rdx);
267                 trace_printf("  rbp  0x%016lx\n",      vm_tf->tf_rbp);
268                 trace_printf("  rsi  0x%016lx\n",      vm_tf->tf_rsi);
269                 trace_printf("  rdi  0x%016lx\n",      vm_tf->tf_rdi);
270                 trace_printf("  r8   0x%016lx\n",      vm_tf->tf_r8);
271                 trace_printf("  r9   0x%016lx\n",      vm_tf->tf_r9);
272                 trace_printf("  r10  0x%016lx\n",      vm_tf->tf_r10);
273                 trace_printf("  r11  0x%016lx\n",      0xdeadbeef);
274                 trace_printf("  r12  0x%016lx\n",      vm_tf->tf_r12);
275                 trace_printf("  r13  0x%016lx\n",      vm_tf->tf_r13);
276                 trace_printf("  r14  0x%016lx\n",      vm_tf->tf_r14);
277                 trace_printf("  r15  0x%016lx\n",      vm_tf->tf_r15);
278                 trace_printf("  rip  0x%016lx\n",      vm_tf->tf_rip);
279                 trace_printf("  rflg 0x%016lx\n",      vm_tf->tf_rflags);
280                 trace_printf("  rsp  0x%016lx\n",      vm_tf->tf_rsp);
281                 trace_printf("  cr2  0x%016lx\n",      vm_tf->tf_cr2);
282                 trace_printf("  cr3  0x%016lx\n",      vm_tf->tf_cr3);
283                 trace_printf("Gpcore 0x%08x\n",        vm_tf->tf_guest_pcoreid);
284                 trace_printf("Flags  0x%08x\n",        vm_tf->tf_flags);
285                 trace_printf("Inject 0x%08x\n",        vm_tf->tf_trap_inject);
286                 trace_printf("ExitRs 0x%08x\n",        vm_tf->tf_exit_reason);
287                 trace_printf("ExitQl 0x%08x\n",        vm_tf->tf_exit_qual);
288                 trace_printf("Intr1  0x%016lx\n",      vm_tf->tf_intrinfo1);
289                 trace_printf("Intr2  0x%016lx\n",      vm_tf->tf_intrinfo2);
290                 trace_printf("GIntr  0x----%04x\n",
291                              vm_tf->tf_guest_intr_status);
292                 trace_printf("GVA    0x%016lx\n",      vm_tf->tf_guest_va);
293                 trace_printf("GPA    0x%016lx\n",      vm_tf->tf_guest_pa);
294                 retval = true;
295                 break;
296         }
297
298         if (retval)
299                 vm_tf->tf_rip += 3;
300
301         return retval;
302 }
303
304 static bool handle_io(struct guest_thread *gth)
305 {
306         struct vm_trapframe *vm_tf = gth_to_vmtf(gth);
307         int ret = io(gth);
308
309         if (ret < 0)
310                 return FALSE;
311         if (ret == VM_PAGE_FAULT) {
312                 /* We were unable to translate RIP due to an ept fault */
313                 vm_tf->tf_trap_inject = VM_TRAP_VALID
314                                       | VM_TRAP_ERROR_CODE
315                                       | VM_TRAP_HARDWARE
316                                       | HW_TRAP_PAGE_FAULT;
317         }
318         return TRUE;
319 }
320
321 static bool handle_msr(struct guest_thread *gth)
322 {
323         struct vm_trapframe *vm_tf = gth_to_vmtf(gth);
324
325         if (msrio(gth, gth_to_gpci(gth), vm_tf->tf_exit_reason)) {
326                 /* Use event injection through vmctl to send a general
327                  * protection fault vmctl.interrupt gets written to the VM-Entry
328                  * Interruption-Information Field by vmx */
329                 vm_tf->tf_trap_inject = VM_TRAP_VALID
330                                       | VM_TRAP_ERROR_CODE
331                                       | VM_TRAP_HARDWARE
332                                       | HW_TRAP_GP_FAULT;
333         } else {
334                 vm_tf->tf_rip += 2;
335         }
336         return TRUE;
337 }
338
339 static bool handle_apic_access(struct guest_thread *gth)
340 {
341         uint64_t gpa, *regp;
342         uint8_t regx;
343         int store, size;
344         int advance;
345         struct vm_trapframe *vm_tf = gth_to_vmtf(gth);
346
347         if (decode(gth, &gpa, &regx, &regp, &store, &size, &advance))
348                 return FALSE;
349         if (__apic_access(gth, gpa, regx, regp, store))
350                 return FALSE;
351         vm_tf->tf_rip += advance;
352         return TRUE;
353 }
354
355 static bool handle_halt(struct guest_thread *gth)
356 {
357         struct vm_trapframe *vm_tf = gth_to_vmtf(gth);
358         struct virtual_machine *vm = gth_to_vm(gth);
359
360         if (vm->halt_exit)
361                 return FALSE;
362         /* It's possible the guest disabled IRQs and halted, perhaps waiting on
363          * an NMI or something.  If we need to support that, we can change this.
364          */
365         sleep_til_irq(gth);
366         vm_tf->tf_rip += 1;
367         return TRUE;
368 }
369
370 /* The guest is told (via cpuid) that there is no monitor/mwait.  Callers of
371  * mwait are paravirtualized halts.
372  *
373  * We don't support monitor/mwait in software, so if they tried to mwait
374  * without break-on-interrupt and with interrupts disabled, they'll never
375  * wake up.  So we'll always break on interrupt. */
376 static bool handle_mwait(struct guest_thread *gth)
377 {
378         struct vm_trapframe *vm_tf = gth_to_vmtf(gth);
379         struct virtual_machine *vm = gth_to_vm(gth);
380
381         sleep_til_irq(gth);
382         vm_tf->tf_rip += 3;
383         return TRUE;
384 }
385
386 /* Is this a vmm specific thing?  or generic?
387  *
388  * what do we do when we want to kill the vm?  what are our other options? */
389 bool handle_vmexit(struct guest_thread *gth)
390 {
391         struct vm_trapframe *vm_tf = gth_to_vmtf(gth);
392
393         switch (vm_tf->tf_exit_reason) {
394         case EXIT_REASON_CPUID:
395                 return handle_cpuid(gth);
396         case EXIT_REASON_EPT_VIOLATION:
397                 return handle_ept_fault(gth);
398         case EXIT_REASON_VMCALL:
399                 return handle_vmcall(gth);
400         case EXIT_REASON_IO_INSTRUCTION:
401                 return handle_io(gth);
402         case EXIT_REASON_MSR_WRITE:
403         case EXIT_REASON_MSR_READ:
404                 return handle_msr(gth);
405         case EXIT_REASON_APIC_ACCESS:
406                 return handle_apic_access(gth);
407         case EXIT_REASON_HLT:
408                 return handle_halt(gth);
409         case EXIT_REASON_MWAIT_INSTRUCTION:
410                 return handle_mwait(gth);
411         case EXIT_REASON_EXTERNAL_INTERRUPT:
412         case EXIT_REASON_APIC_WRITE:
413                 /* TODO: just ignore these? */
414                 return TRUE;
415         default:
416                 return FALSE;
417         }
418 }