acrn-hypervisor/hypervisor/common/vm_event.c
Wu Zhou 925e3d95b4 hv: add max_len for sbuf_put param
sbuf_put copies sbuf->ele_size of data, and puts into ring. Currently
this function assumes that data size from caller is no less than
sbuf->ele_size.

But as sbuf->ele_size is usually setup by some sources outside of the HV
(e.g., the service VM), it is not meant to be trusted. So caller should
provide the max length of the data for safety reason. sbuf_put() will
return UINT32_MAX if max_len of data is less than element size.

Additionally, a helper function sbuf_put_many() is added for putting
multiple entries.

Tracked-On: #8547
Signed-off-by: Wu Zhou <wu.zhou@intel.com>
Reviewed-by: Junjie Mao <junjie.mao@intel.com>
2024-02-20 11:52:02 +08:00

49 lines
994 B
C

/*
* Copyright (C) 2020-2023 Intel Corporation.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <errno.h>
#include <util.h>
#include <acrn_hv_defs.h>
#include <asm/guest/vm.h>
#include <vm_event.h>
#include <sbuf.h>
int32_t init_vm_event(struct acrn_vm *vm, uint64_t *hva)
{
struct shared_buf *sbuf = (struct shared_buf *)hva;
int ret = -1;
stac();
if (sbuf != NULL) {
if (sbuf->magic == SBUF_MAGIC) {
vm->sw.vm_event_sbuf = sbuf;
spinlock_init(&vm->vm_event_lock);
ret = 0;
}
}
clac();
return ret;
}
int32_t send_vm_event(struct acrn_vm *vm, struct vm_event *event)
{
struct shared_buf *sbuf = (struct shared_buf *)vm->sw.vm_event_sbuf;
int32_t ret = -ENODEV;
uint32_t size_sent;
if (sbuf != NULL) {
spinlock_obtain(&vm->vm_event_lock);
size_sent = sbuf_put(sbuf, (uint8_t *)event, sizeof(*event));
spinlock_release(&vm->vm_event_lock);
if (size_sent == sizeof(struct vm_event)) {
arch_fire_hsm_interrupt();
ret = 0;
}
}
return ret;
}