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