#include "qemu/osdep.h"
#include "qemu/main-loop.h"
+#include "qemu/module.h"
#include "qapi/error.h"
#include "exec/address-spaces.h"
+#include "exec/memory.h"
#include "sysemu/kvm.h"
#include "qemu/bitops.h"
#include "qemu/error-report.h"
+#include "qemu/lockable.h"
#include "qemu/queue.h"
#include "qemu/rcu.h"
#include "qemu/rcu_queue.h"
#include "hw/hyperv/hyperv.h"
+#include "qom/object.h"
+#include "target/i386/kvm/hyperv-proto.h"
+#include "target/i386/cpu.h"
+#include "exec/cpu-all.h"
-typedef struct SynICState {
+struct SynICState {
DeviceState parent_obj;
CPUState *cs;
- bool enabled;
+ bool sctl_enabled;
hwaddr msg_page_addr;
hwaddr event_page_addr;
MemoryRegion msg_page_mr;
MemoryRegion event_page_mr;
struct hyperv_message_page *msg_page;
struct hyperv_event_flags_page *event_page;
-} SynICState;
+
+ QemuMutex sint_routes_mutex;
+ QLIST_HEAD(, HvSintRoute) sint_routes;
+};
#define TYPE_SYNIC "hyperv-synic"
-#define SYNIC(obj) OBJECT_CHECK(SynICState, (obj), TYPE_SYNIC)
+OBJECT_DECLARE_SIMPLE_TYPE(SynICState, SYNIC)
+
+static bool synic_enabled;
+
+bool hyperv_is_synic_enabled(void)
+{
+ return synic_enabled;
+}
static SynICState *get_synic(CPUState *cs)
{
return SYNIC(object_resolve_path_component(OBJECT(cs), "synic"));
}
-static void synic_update(SynICState *synic, bool enable,
+static void synic_update(SynICState *synic, bool sctl_enable,
hwaddr msg_page_addr, hwaddr event_page_addr)
{
- synic->enabled = enable;
+ synic->sctl_enabled = sctl_enable;
if (synic->msg_page_addr != msg_page_addr) {
if (synic->msg_page_addr) {
memory_region_del_subregion(get_system_memory(),
}
}
-void hyperv_synic_update(CPUState *cs, bool enable,
+void hyperv_synic_update(CPUState *cs, bool sctl_enable,
hwaddr msg_page_addr, hwaddr event_page_addr)
{
SynICState *synic = get_synic(cs);
return;
}
- synic_update(synic, enable, msg_page_addr, event_page_addr);
+ synic_update(synic, sctl_enable, msg_page_addr, event_page_addr);
}
static void synic_realize(DeviceState *dev, Error **errp)
sizeof(*synic->event_page), &error_abort);
synic->msg_page = memory_region_get_ram_ptr(&synic->msg_page_mr);
synic->event_page = memory_region_get_ram_ptr(&synic->event_page_mr);
+ qemu_mutex_init(&synic->sint_routes_mutex);
+ QLIST_INIT(&synic->sint_routes);
g_free(msgp_name);
g_free(eventp_name);
}
+
static void synic_reset(DeviceState *dev)
{
SynICState *synic = SYNIC(dev);
memset(synic->msg_page, 0, sizeof(*synic->msg_page));
memset(synic->event_page, 0, sizeof(*synic->event_page));
synic_update(synic, false, 0, 0);
+ assert(QLIST_EMPTY(&synic->sint_routes));
}
static void synic_class_init(ObjectClass *klass, void *data)
obj = object_new(TYPE_SYNIC);
synic = SYNIC(obj);
synic->cs = cs;
- object_property_add_child(OBJECT(cs), "synic", obj, &error_abort);
+ object_property_add_child(OBJECT(cs), "synic", obj);
object_unref(obj);
- object_property_set_bool(obj, true, "realized", &error_abort);
+ qdev_realize(DEVICE(obj), NULL, &error_abort);
+ synic_enabled = true;
}
void hyperv_synic_reset(CPUState *cs)
SynICState *synic = get_synic(cs);
if (synic) {
- device_reset(DEVICE(synic));
+ device_cold_reset(DEVICE(synic));
}
}
HvSintStagedMessage *staged_msg;
unsigned refcount;
+ QLIST_ENTRY(HvSintRoute) link;
};
static CPUState *hyperv_find_vcpu(uint32_t vp_index)
HvSintRoute *sint_route = opaque;
HvSintStagedMessage *staged_msg = sint_route->staged_msg;
- if (atomic_read(&staged_msg->state) != HV_STAGED_MSG_POSTED) {
+ if (qatomic_read(&staged_msg->state) != HV_STAGED_MSG_POSTED) {
/* status nor ready yet (spurious ack from guest?), ignore */
return;
}
staged_msg->status = 0;
/* staged message processing finished, ready to start over */
- atomic_set(&staged_msg->state, HV_STAGED_MSG_FREE);
+ qatomic_set(&staged_msg->state, HV_STAGED_MSG_FREE);
/* drop the reference taken in hyperv_post_msg */
hyperv_sint_route_unref(sint_route);
}
assert(staged_msg->state == HV_STAGED_MSG_BUSY);
- if (!synic->enabled || !synic->msg_page_addr) {
+ if (!synic->msg_page_addr) {
staged_msg->status = -ENXIO;
goto posted;
}
memory_region_set_dirty(&synic->msg_page_mr, 0, sizeof(*synic->msg_page));
posted:
- atomic_set(&staged_msg->state, HV_STAGED_MSG_POSTED);
+ qatomic_set(&staged_msg->state, HV_STAGED_MSG_POSTED);
/*
* Notify the msg originator of the progress made; if the slot was busy we
* set msg_pending flag in it so it will be the guest who will do EOM and
assert(staged_msg);
/* grab the staging area */
- if (atomic_cmpxchg(&staged_msg->state, HV_STAGED_MSG_FREE,
+ if (qatomic_cmpxchg(&staged_msg->state, HV_STAGED_MSG_FREE,
HV_STAGED_MSG_BUSY) != HV_STAGED_MSG_FREE) {
return -EAGAIN;
}
if (eventno > HV_EVENT_FLAGS_COUNT) {
return -EINVAL;
}
- if (!synic->enabled || !synic->event_page_addr) {
+ if (!synic->sctl_enabled || !synic->event_page_addr) {
return -ENXIO;
}
set_mask = BIT_MASK(eventno);
flags = synic->event_page->slot[sint_route->sint].flags;
- if ((atomic_fetch_or(&flags[set_idx], set_mask) & set_mask) != set_mask) {
+ if ((qatomic_fetch_or(&flags[set_idx], set_mask) & set_mask) != set_mask) {
memory_region_set_dirty(&synic->event_page_mr, 0,
sizeof(*synic->event_page));
ret = hyperv_sint_route_set_sint(sint_route);
HvSintRoute *hyperv_sint_route_new(uint32_t vp_index, uint32_t sint,
HvSintMsgCb cb, void *cb_data)
{
- HvSintRoute *sint_route;
- EventNotifier *ack_notifier;
+ HvSintRoute *sint_route = NULL;
+ EventNotifier *ack_notifier = NULL;
int r, gsi;
CPUState *cs;
SynICState *synic;
+ bool ack_event_initialized = false;
cs = hyperv_find_vcpu(vp_index);
if (!cs) {
}
sint_route = g_new0(HvSintRoute, 1);
- r = event_notifier_init(&sint_route->sint_set_notifier, false);
- if (r) {
- goto err;
+ if (!sint_route) {
+ return NULL;
}
+ sint_route->synic = synic;
+ sint_route->sint = sint;
+ sint_route->refcount = 1;
ack_notifier = cb ? &sint_route->sint_ack_notifier : NULL;
if (ack_notifier) {
sint_route->staged_msg = g_new0(HvSintStagedMessage, 1);
+ if (!sint_route->staged_msg) {
+ goto cleanup_err_sint;
+ }
sint_route->staged_msg->cb = cb;
sint_route->staged_msg->cb_data = cb_data;
r = event_notifier_init(ack_notifier, false);
if (r) {
- goto err_sint_set_notifier;
+ goto cleanup_err_sint;
}
-
event_notifier_set_handler(ack_notifier, sint_ack_handler);
+ ack_event_initialized = true;
+ }
+
+ /* See if we are done or we need to setup a GSI for this SintRoute */
+ if (!synic->sctl_enabled) {
+ goto cleanup;
+ }
+
+ /* We need to setup a GSI for this SintRoute */
+ r = event_notifier_init(&sint_route->sint_set_notifier, false);
+ if (r) {
+ goto cleanup_err_sint;
}
gsi = kvm_irqchip_add_hv_sint_route(kvm_state, vp_index, sint);
if (gsi < 0) {
- goto err_gsi;
+ goto cleanup_err_sint_notifier;
}
r = kvm_irqchip_add_irqfd_notifier_gsi(kvm_state,
&sint_route->sint_set_notifier,
ack_notifier, gsi);
if (r) {
- goto err_irqfd;
+ goto cleanup_err_irqfd;
}
sint_route->gsi = gsi;
- sint_route->synic = synic;
- sint_route->sint = sint;
- sint_route->refcount = 1;
-
+cleanup:
+ qemu_mutex_lock(&synic->sint_routes_mutex);
+ QLIST_INSERT_HEAD(&synic->sint_routes, sint_route, link);
+ qemu_mutex_unlock(&synic->sint_routes_mutex);
return sint_route;
-err_irqfd:
+cleanup_err_irqfd:
kvm_irqchip_release_virq(kvm_state, gsi);
-err_gsi:
+
+cleanup_err_sint_notifier:
+ event_notifier_cleanup(&sint_route->sint_set_notifier);
+
+cleanup_err_sint:
if (ack_notifier) {
- event_notifier_set_handler(ack_notifier, NULL);
- event_notifier_cleanup(ack_notifier);
+ if (ack_event_initialized) {
+ event_notifier_set_handler(ack_notifier, NULL);
+ event_notifier_cleanup(ack_notifier);
+ }
+
g_free(sint_route->staged_msg);
}
-err_sint_set_notifier:
- event_notifier_cleanup(&sint_route->sint_set_notifier);
-err:
- g_free(sint_route);
+ g_free(sint_route);
return NULL;
}
void hyperv_sint_route_unref(HvSintRoute *sint_route)
{
+ SynICState *synic;
+
if (!sint_route) {
return;
}
return;
}
- kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state,
- &sint_route->sint_set_notifier,
- sint_route->gsi);
- kvm_irqchip_release_virq(kvm_state, sint_route->gsi);
+ synic = sint_route->synic;
+ qemu_mutex_lock(&synic->sint_routes_mutex);
+ QLIST_REMOVE(sint_route, link);
+ qemu_mutex_unlock(&synic->sint_routes_mutex);
+
+ if (sint_route->gsi) {
+ kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state,
+ &sint_route->sint_set_notifier,
+ sint_route->gsi);
+ kvm_irqchip_release_virq(kvm_state, sint_route->gsi);
+ event_notifier_cleanup(&sint_route->sint_set_notifier);
+ }
+
if (sint_route->staged_msg) {
event_notifier_set_handler(&sint_route->sint_ack_notifier, NULL);
event_notifier_cleanup(&sint_route->sint_ack_notifier);
g_free(sint_route->staged_msg);
}
- event_notifier_cleanup(&sint_route->sint_set_notifier);
g_free(sint_route);
}
int hyperv_sint_route_set_sint(HvSintRoute *sint_route)
{
+ if (!sint_route->gsi) {
+ return 0;
+ }
+
return event_notifier_set(&sint_route->sint_set_notifier);
}
int ret;
MsgHandler *mh;
- qemu_mutex_lock(&handlers_mutex);
+ QEMU_LOCK_GUARD(&handlers_mutex);
QLIST_FOREACH(mh, &msg_handlers, link) {
if (mh->conn_id == conn_id) {
if (handler) {
g_free_rcu(mh, rcu);
ret = 0;
}
- goto unlock;
+ return ret;
}
}
} else {
ret = -ENOENT;
}
-unlock:
- qemu_mutex_unlock(&handlers_mutex);
+
return ret;
}
}
ret = HV_STATUS_INVALID_CONNECTION_ID;
- rcu_read_lock();
- QLIST_FOREACH_RCU(mh, &msg_handlers, link) {
- if (mh->conn_id == (msg->connection_id & HV_CONNECTION_ID_MASK)) {
- ret = mh->handler(msg, mh->data);
- break;
+ WITH_RCU_READ_LOCK_GUARD() {
+ QLIST_FOREACH_RCU(mh, &msg_handlers, link) {
+ if (mh->conn_id == (msg->connection_id & HV_CONNECTION_ID_MASK)) {
+ ret = mh->handler(msg, mh->data);
+ break;
+ }
}
}
- rcu_read_unlock();
unmap:
cpu_physical_memory_unmap(msg, len, 0, 0);
int ret;
EventFlagHandler *handler;
- qemu_mutex_lock(&handlers_mutex);
+ QEMU_LOCK_GUARD(&handlers_mutex);
QLIST_FOREACH(handler, &event_flag_handlers, link) {
if (handler->conn_id == conn_id) {
if (notifier) {
g_free_rcu(handler, rcu);
ret = 0;
}
- goto unlock;
+ return ret;
}
}
} else {
ret = -ENOENT;
}
-unlock:
- qemu_mutex_unlock(&handlers_mutex);
+
return ret;
}
uint16_t hyperv_hcall_signal_event(uint64_t param, bool fast)
{
- uint16_t ret;
EventFlagHandler *handler;
if (unlikely(!fast)) {
return HV_STATUS_INVALID_HYPERCALL_INPUT;
}
- ret = HV_STATUS_INVALID_CONNECTION_ID;
- rcu_read_lock();
+ RCU_READ_LOCK_GUARD();
QLIST_FOREACH_RCU(handler, &event_flag_handlers, link) {
if (handler->conn_id == param) {
event_notifier_set(handler->notifier);
- ret = 0;
- break;
+ return 0;
}
}
- rcu_read_unlock();
+ return HV_STATUS_INVALID_CONNECTION_ID;
+}
+
+static HvSynDbgHandler hv_syndbg_handler;
+static void *hv_syndbg_context;
+
+void hyperv_set_syndbg_handler(HvSynDbgHandler handler, void *context)
+{
+ assert(!hv_syndbg_handler);
+ hv_syndbg_handler = handler;
+ hv_syndbg_context = context;
+}
+
+uint16_t hyperv_hcall_reset_dbg_session(uint64_t outgpa)
+{
+ uint16_t ret;
+ HvSynDbgMsg msg;
+ struct hyperv_reset_debug_session_output *reset_dbg_session = NULL;
+ hwaddr len;
+
+ if (!hv_syndbg_handler) {
+ ret = HV_STATUS_INVALID_HYPERCALL_CODE;
+ goto cleanup;
+ }
+
+ len = sizeof(*reset_dbg_session);
+ reset_dbg_session = cpu_physical_memory_map(outgpa, &len, 1);
+ if (!reset_dbg_session || len < sizeof(*reset_dbg_session)) {
+ ret = HV_STATUS_INSUFFICIENT_MEMORY;
+ goto cleanup;
+ }
+
+ msg.type = HV_SYNDBG_MSG_CONNECTION_INFO;
+ ret = hv_syndbg_handler(hv_syndbg_context, &msg);
+ if (ret) {
+ goto cleanup;
+ }
+
+ reset_dbg_session->host_ip = msg.u.connection_info.host_ip;
+ reset_dbg_session->host_port = msg.u.connection_info.host_port;
+ /* The following fields are only used as validation for KDVM */
+ memset(&reset_dbg_session->host_mac, 0,
+ sizeof(reset_dbg_session->host_mac));
+ reset_dbg_session->target_ip = msg.u.connection_info.host_ip;
+ reset_dbg_session->target_port = msg.u.connection_info.host_port;
+ memset(&reset_dbg_session->target_mac, 0,
+ sizeof(reset_dbg_session->target_mac));
+cleanup:
+ if (reset_dbg_session) {
+ cpu_physical_memory_unmap(reset_dbg_session,
+ sizeof(*reset_dbg_session), 1, len);
+ }
+
+ return ret;
+}
+
+uint16_t hyperv_hcall_retreive_dbg_data(uint64_t ingpa, uint64_t outgpa,
+ bool fast)
+{
+ uint16_t ret;
+ struct hyperv_retrieve_debug_data_input *debug_data_in = NULL;
+ struct hyperv_retrieve_debug_data_output *debug_data_out = NULL;
+ hwaddr in_len, out_len;
+ HvSynDbgMsg msg;
+
+ if (fast || !hv_syndbg_handler) {
+ ret = HV_STATUS_INVALID_HYPERCALL_CODE;
+ goto cleanup;
+ }
+
+ in_len = sizeof(*debug_data_in);
+ debug_data_in = cpu_physical_memory_map(ingpa, &in_len, 0);
+ if (!debug_data_in || in_len < sizeof(*debug_data_in)) {
+ ret = HV_STATUS_INSUFFICIENT_MEMORY;
+ goto cleanup;
+ }
+
+ out_len = sizeof(*debug_data_out);
+ debug_data_out = cpu_physical_memory_map(outgpa, &out_len, 1);
+ if (!debug_data_out || out_len < sizeof(*debug_data_out)) {
+ ret = HV_STATUS_INSUFFICIENT_MEMORY;
+ goto cleanup;
+ }
+
+ msg.type = HV_SYNDBG_MSG_RECV;
+ msg.u.recv.buf_gpa = outgpa + sizeof(*debug_data_out);
+ msg.u.recv.count = TARGET_PAGE_SIZE - sizeof(*debug_data_out);
+ msg.u.recv.options = debug_data_in->options;
+ msg.u.recv.timeout = debug_data_in->timeout;
+ msg.u.recv.is_raw = true;
+ ret = hv_syndbg_handler(hv_syndbg_context, &msg);
+ if (ret == HV_STATUS_NO_DATA) {
+ debug_data_out->retrieved_count = 0;
+ debug_data_out->remaining_count = debug_data_in->count;
+ goto cleanup;
+ } else if (ret != HV_STATUS_SUCCESS) {
+ goto cleanup;
+ }
+
+ debug_data_out->retrieved_count = msg.u.recv.retrieved_count;
+ debug_data_out->remaining_count =
+ debug_data_in->count - msg.u.recv.retrieved_count;
+cleanup:
+ if (debug_data_out) {
+ cpu_physical_memory_unmap(debug_data_out, sizeof(*debug_data_out), 1,
+ out_len);
+ }
+
+ if (debug_data_in) {
+ cpu_physical_memory_unmap(debug_data_in, sizeof(*debug_data_in), 0,
+ in_len);
+ }
+
+ return ret;
+}
+
+uint16_t hyperv_hcall_post_dbg_data(uint64_t ingpa, uint64_t outgpa, bool fast)
+{
+ uint16_t ret;
+ struct hyperv_post_debug_data_input *post_data_in = NULL;
+ struct hyperv_post_debug_data_output *post_data_out = NULL;
+ hwaddr in_len, out_len;
+ HvSynDbgMsg msg;
+
+ if (fast || !hv_syndbg_handler) {
+ ret = HV_STATUS_INVALID_HYPERCALL_CODE;
+ goto cleanup;
+ }
+
+ in_len = sizeof(*post_data_in);
+ post_data_in = cpu_physical_memory_map(ingpa, &in_len, 0);
+ if (!post_data_in || in_len < sizeof(*post_data_in)) {
+ ret = HV_STATUS_INSUFFICIENT_MEMORY;
+ goto cleanup;
+ }
+
+ if (post_data_in->count > TARGET_PAGE_SIZE - sizeof(*post_data_in)) {
+ ret = HV_STATUS_INVALID_PARAMETER;
+ goto cleanup;
+ }
+
+ out_len = sizeof(*post_data_out);
+ post_data_out = cpu_physical_memory_map(outgpa, &out_len, 1);
+ if (!post_data_out || out_len < sizeof(*post_data_out)) {
+ ret = HV_STATUS_INSUFFICIENT_MEMORY;
+ goto cleanup;
+ }
+
+ msg.type = HV_SYNDBG_MSG_SEND;
+ msg.u.send.buf_gpa = ingpa + sizeof(*post_data_in);
+ msg.u.send.count = post_data_in->count;
+ msg.u.send.is_raw = true;
+ ret = hv_syndbg_handler(hv_syndbg_context, &msg);
+ if (ret != HV_STATUS_SUCCESS) {
+ goto cleanup;
+ }
+
+ post_data_out->pending_count = msg.u.send.pending_count;
+ ret = post_data_out->pending_count ? HV_STATUS_INSUFFICIENT_BUFFERS :
+ HV_STATUS_SUCCESS;
+cleanup:
+ if (post_data_out) {
+ cpu_physical_memory_unmap(post_data_out,
+ sizeof(*post_data_out), 1, out_len);
+ }
+
+ if (post_data_in) {
+ cpu_physical_memory_unmap(post_data_in,
+ sizeof(*post_data_in), 0, in_len);
+ }
+
return ret;
}
+
+uint32_t hyperv_syndbg_send(uint64_t ingpa, uint32_t count)
+{
+ HvSynDbgMsg msg;
+
+ if (!hv_syndbg_handler) {
+ return HV_SYNDBG_STATUS_INVALID;
+ }
+
+ msg.type = HV_SYNDBG_MSG_SEND;
+ msg.u.send.buf_gpa = ingpa;
+ msg.u.send.count = count;
+ msg.u.send.is_raw = false;
+ if (hv_syndbg_handler(hv_syndbg_context, &msg)) {
+ return HV_SYNDBG_STATUS_INVALID;
+ }
+
+ return HV_SYNDBG_STATUS_SEND_SUCCESS;
+}
+
+uint32_t hyperv_syndbg_recv(uint64_t ingpa, uint32_t count)
+{
+ uint16_t ret;
+ HvSynDbgMsg msg;
+
+ if (!hv_syndbg_handler) {
+ return HV_SYNDBG_STATUS_INVALID;
+ }
+
+ msg.type = HV_SYNDBG_MSG_RECV;
+ msg.u.recv.buf_gpa = ingpa;
+ msg.u.recv.count = count;
+ msg.u.recv.options = 0;
+ msg.u.recv.timeout = 0;
+ msg.u.recv.is_raw = false;
+ ret = hv_syndbg_handler(hv_syndbg_context, &msg);
+ if (ret != HV_STATUS_SUCCESS) {
+ return 0;
+ }
+
+ return HV_SYNDBG_STATUS_SET_SIZE(HV_SYNDBG_STATUS_RECV_SUCCESS,
+ msg.u.recv.retrieved_count);
+}
+
+void hyperv_syndbg_set_pending_page(uint64_t ingpa)
+{
+ HvSynDbgMsg msg;
+
+ if (!hv_syndbg_handler) {
+ return;
+ }
+
+ msg.type = HV_SYNDBG_MSG_SET_PENDING_PAGE;
+ msg.u.pending_page.buf_gpa = ingpa;
+ hv_syndbg_handler(hv_syndbg_context, &msg);
+}
+
+uint64_t hyperv_syndbg_query_options(void)
+{
+ HvSynDbgMsg msg;
+
+ if (!hv_syndbg_handler) {
+ return 0;
+ }
+
+ msg.type = HV_SYNDBG_MSG_QUERY_OPTIONS;
+ if (hv_syndbg_handler(hv_syndbg_context, &msg) != HV_STATUS_SUCCESS) {
+ return 0;
+ }
+
+ return msg.u.query_options.options;
+}