Skip to content

Commit

Permalink
KVM: selftests: Add string formatting options to ucall
Browse files Browse the repository at this point in the history
Add more flexibility to guest debugging and testing by adding
GUEST_PRINTF() and GUEST_ASSERT_FMT() to the ucall framework.

Add a sized buffer to the ucall structure to hold the formatted string,
i.e. to allow the guest to easily resolve the string, and thus avoid the
ugly pattern of the host side having to make assumptions about the desired
format, as well as having to pass around a large number of parameters.

The buffer size was chosen to accommodate most use cases, and based on
similar usage.  E.g. printf() uses the same size buffer in
arch/x86/boot/printf.c.  And 1KiB ought to be enough for anybody.

Signed-off-by: Aaron Lewis <aaronlewis@google.com>
[sean: massage changelog, wrap macro param in ()]
Link: https://lore.kernel.org/r/20230729003643.1053367-8-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
  • Loading branch information
suomilewis authored and sean-jc committed Aug 2, 2023
1 parent 215a681 commit 57e5c1f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
7 changes: 7 additions & 0 deletions tools/testing/selftests/kvm/include/ucall_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@ enum {
UCALL_NONE,
UCALL_SYNC,
UCALL_ABORT,
UCALL_PRINTF,
UCALL_DONE,
UCALL_UNHANDLED,
};

#define UCALL_MAX_ARGS 7
#define UCALL_BUFFER_LEN 1024

struct ucall {
uint64_t cmd;
uint64_t args[UCALL_MAX_ARGS];
char buffer[UCALL_BUFFER_LEN];

/* Host virtual address of this struct. */
struct ucall *hva;
Expand All @@ -32,6 +35,7 @@ void ucall_arch_do_ucall(vm_vaddr_t uc);
void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu);

void ucall(uint64_t cmd, int nargs, ...);
void ucall_fmt(uint64_t cmd, const char *fmt, ...);
uint64_t get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc);
void ucall_init(struct kvm_vm *vm, vm_paddr_t mmio_gpa);
int ucall_nr_pages_required(uint64_t page_size);
Expand All @@ -47,8 +51,11 @@ int ucall_nr_pages_required(uint64_t page_size);
#define GUEST_SYNC_ARGS(stage, arg1, arg2, arg3, arg4) \
ucall(UCALL_SYNC, 6, "hello", stage, arg1, arg2, arg3, arg4)
#define GUEST_SYNC(stage) ucall(UCALL_SYNC, 2, "hello", stage)
#define GUEST_PRINTF(_fmt, _args...) ucall_fmt(UCALL_PRINTF, _fmt, ##_args)
#define GUEST_DONE() ucall(UCALL_DONE, 0)

#define REPORT_GUEST_PRINTF(ucall) pr_info("%s", (ucall).buffer)

enum guest_assert_builtin_args {
GUEST_ERROR_STRING,
GUEST_FILE,
Expand Down
17 changes: 17 additions & 0 deletions tools/testing/selftests/kvm/lib/ucall_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,23 @@ static void ucall_free(struct ucall *uc)
clear_bit(uc - ucall_pool->ucalls, ucall_pool->in_use);
}

void ucall_fmt(uint64_t cmd, const char *fmt, ...)
{
struct ucall *uc;
va_list va;

uc = ucall_alloc();
uc->cmd = cmd;

va_start(va, fmt);
guest_vsnprintf(uc->buffer, UCALL_BUFFER_LEN, fmt, va);
va_end(va);

ucall_arch_do_ucall((vm_vaddr_t)uc->hva);

ucall_free(uc);
}

void ucall(uint64_t cmd, int nargs, ...)
{
struct ucall *uc;
Expand Down

0 comments on commit 57e5c1f

Please sign in to comment.