blob: b264a2862092919449875df055e595171d95cf19 [file] [log] [blame]
Andrey Smetanin50efe822015-11-10 15:52:43 +03001/*
2 * QEMU KVM Hyper-V support
3 *
4 * Copyright (C) 2015 Andrey Smetanin <asmetanin@virtuozzo.com>
5 *
6 * Authors:
7 * Andrey Smetanin <asmetanin@virtuozzo.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
11 *
12 */
13
Peter Maydellb6a0aa02016-01-26 18:17:03 +000014#include "qemu/osdep.h"
Roman Kagan267e0712018-09-21 11:22:11 +030015#include "qemu/main-loop.h"
Andrey Smetanin50efe822015-11-10 15:52:43 +030016#include "hyperv.h"
Roman Kagan701189e2018-09-21 11:20:39 +030017#include "hw/hyperv/hyperv.h"
Roman Kagan5e953812017-07-13 23:15:21 +030018#include "hyperv-proto.h"
Andrey Smetanin50efe822015-11-10 15:52:43 +030019
Roman Kagan606c34b2018-09-21 11:22:09 +030020int hyperv_x86_synic_add(X86CPU *cpu)
21{
22 hyperv_synic_add(CPU(cpu));
23 return 0;
24}
25
26void hyperv_x86_synic_reset(X86CPU *cpu)
27{
28 hyperv_synic_reset(CPU(cpu));
29}
30
31void hyperv_x86_synic_update(X86CPU *cpu)
32{
33 CPUX86State *env = &cpu->env;
34 bool enable = env->msr_hv_synic_control & HV_SYNIC_ENABLE;
35 hwaddr msg_page_addr = (env->msr_hv_synic_msg_page & HV_SIMP_ENABLE) ?
36 (env->msr_hv_synic_msg_page & TARGET_PAGE_MASK) : 0;
37 hwaddr event_page_addr = (env->msr_hv_synic_evt_page & HV_SIEFP_ENABLE) ?
38 (env->msr_hv_synic_evt_page & TARGET_PAGE_MASK) : 0;
39 hyperv_synic_update(CPU(cpu), enable, msg_page_addr, event_page_addr);
40}
41
Roman Kagan267e0712018-09-21 11:22:11 +030042static void async_synic_update(CPUState *cs, run_on_cpu_data data)
43{
44 qemu_mutex_lock_iothread();
45 hyperv_x86_synic_update(X86_CPU(cs));
46 qemu_mutex_unlock_iothread();
47}
48
Andrey Smetanin50efe822015-11-10 15:52:43 +030049int kvm_hv_handle_exit(X86CPU *cpu, struct kvm_hyperv_exit *exit)
50{
51 CPUX86State *env = &cpu->env;
52
53 switch (exit->type) {
54 case KVM_EXIT_HYPERV_SYNIC:
55 if (!cpu->hyperv_synic) {
56 return -1;
57 }
58
Andrey Smetanin50efe822015-11-10 15:52:43 +030059 switch (exit->u.synic.msr) {
60 case HV_X64_MSR_SCONTROL:
61 env->msr_hv_synic_control = exit->u.synic.control;
62 break;
63 case HV_X64_MSR_SIMP:
64 env->msr_hv_synic_msg_page = exit->u.synic.msg_page;
65 break;
66 case HV_X64_MSR_SIEFP:
67 env->msr_hv_synic_evt_page = exit->u.synic.evt_page;
68 break;
69 default:
70 return -1;
71 }
Roman Kagan606c34b2018-09-21 11:22:09 +030072
Roman Kagan267e0712018-09-21 11:22:11 +030073 /*
74 * this will run in this cpu thread before it returns to KVM, but in a
75 * safe environment (i.e. when all cpus are quiescent) -- this is
76 * necessary because memory hierarchy is being changed
77 */
78 async_safe_run_on_cpu(CPU(cpu), async_synic_update, RUN_ON_CPU_NULL);
Roman Kagan606c34b2018-09-21 11:22:09 +030079
Andrey Smetanin50efe822015-11-10 15:52:43 +030080 return 0;
Andrey Smetanin1b0d9b02016-02-24 13:22:48 +030081 case KVM_EXIT_HYPERV_HCALL: {
Roman Kagane6ea9f42018-09-21 11:22:14 +030082 uint16_t code = exit->u.hcall.input & 0xffff;
83 bool fast = exit->u.hcall.input & HV_HYPERCALL_FAST;
84 uint64_t param = exit->u.hcall.params[0];
Andrey Smetanin1b0d9b02016-02-24 13:22:48 +030085
Andrey Smetanin1b0d9b02016-02-24 13:22:48 +030086 switch (code) {
Roman Kagan76036a52018-09-21 11:22:16 +030087 case HV_POST_MESSAGE:
88 exit->u.hcall.result = hyperv_hcall_post_message(param, fast);
89 break;
Roman Kagan5e953812017-07-13 23:15:21 +030090 case HV_SIGNAL_EVENT:
Roman Kagane6ea9f42018-09-21 11:22:14 +030091 exit->u.hcall.result = hyperv_hcall_signal_event(param, fast);
92 break;
Andrey Smetanin1b0d9b02016-02-24 13:22:48 +030093 default:
94 exit->u.hcall.result = HV_STATUS_INVALID_HYPERCALL_CODE;
Andrey Smetanin1b0d9b02016-02-24 13:22:48 +030095 }
Roman Kagane6ea9f42018-09-21 11:22:14 +030096 return 0;
Andrey Smetanin1b0d9b02016-02-24 13:22:48 +030097 }
Andrey Smetanin50efe822015-11-10 15:52:43 +030098 default:
99 return -1;
100 }
101}