Path: blob/master/tools/testing/selftests/kvm/guest_print_test.c
38189 views
// SPDX-License-Identifier: GPL-2.01/*2* A test for GUEST_PRINTF3*4* Copyright 2022, Google, Inc. and/or its affiliates.5*/6#include <fcntl.h>7#include <stdio.h>8#include <stdlib.h>9#include <string.h>10#include <sys/ioctl.h>1112#include "test_util.h"13#include "kvm_util.h"14#include "processor.h"15#include "ucall_common.h"1617struct guest_vals {18uint64_t a;19uint64_t b;20uint64_t type;21};2223static struct guest_vals vals;2425/* GUEST_PRINTF()/GUEST_ASSERT_FMT() does not support float or double. */26#define TYPE_LIST \27TYPE(test_type_i64, I64, "%ld", int64_t) \28TYPE(test_type_u64, U64u, "%lu", uint64_t) \29TYPE(test_type_x64, U64x, "0x%lx", uint64_t) \30TYPE(test_type_X64, U64X, "0x%lX", uint64_t) \31TYPE(test_type_u32, U32u, "%u", uint32_t) \32TYPE(test_type_x32, U32x, "0x%x", uint32_t) \33TYPE(test_type_X32, U32X, "0x%X", uint32_t) \34TYPE(test_type_int, INT, "%d", int) \35TYPE(test_type_char, CHAR, "%c", char) \36TYPE(test_type_str, STR, "'%s'", const char *) \37TYPE(test_type_ptr, PTR, "%p", uintptr_t)3839enum args_type {40#define TYPE(fn, ext, fmt_t, T) TYPE_##ext,41TYPE_LIST42#undef TYPE43};4445static void run_test(struct kvm_vcpu *vcpu, const char *expected_printf,46const char *expected_assert);4748#define BUILD_TYPE_STRINGS_AND_HELPER(fn, ext, fmt_t, T) \49const char *PRINTF_FMT_##ext = "Got params a = " fmt_t " and b = " fmt_t; \50const char *ASSERT_FMT_##ext = "Expected " fmt_t ", got " fmt_t " instead"; \51static void fn(struct kvm_vcpu *vcpu, T a, T b) \52{ \53char expected_printf[UCALL_BUFFER_LEN]; \54char expected_assert[UCALL_BUFFER_LEN]; \55\56snprintf(expected_printf, UCALL_BUFFER_LEN, PRINTF_FMT_##ext, a, b); \57snprintf(expected_assert, UCALL_BUFFER_LEN, ASSERT_FMT_##ext, a, b); \58vals = (struct guest_vals){ (uint64_t)a, (uint64_t)b, TYPE_##ext }; \59sync_global_to_guest(vcpu->vm, vals); \60run_test(vcpu, expected_printf, expected_assert); \61}6263#define TYPE(fn, ext, fmt_t, T) \64BUILD_TYPE_STRINGS_AND_HELPER(fn, ext, fmt_t, T)65TYPE_LIST66#undef TYPE6768static void guest_code(void)69{70while (1) {71switch (vals.type) {72#define TYPE(fn, ext, fmt_t, T) \73case TYPE_##ext: \74GUEST_PRINTF(PRINTF_FMT_##ext, vals.a, vals.b); \75__GUEST_ASSERT(vals.a == vals.b, \76ASSERT_FMT_##ext, vals.a, vals.b); \77break;78TYPE_LIST79#undef TYPE80default:81GUEST_SYNC(vals.type);82}8384GUEST_DONE();85}86}8788/*89* Unfortunately this gets a little messy because 'assert_msg' doesn't90* just contains the matching string, it also contains additional assert91* info. Fortunately the part that matches should be at the very end of92* 'assert_msg'.93*/94static void ucall_abort(const char *assert_msg, const char *expected_assert_msg)95{96int len_str = strlen(assert_msg);97int len_substr = strlen(expected_assert_msg);98int offset = len_str - len_substr;99100TEST_ASSERT(len_substr <= len_str,101"Expected '%s' to be a substring of '%s'",102assert_msg, expected_assert_msg);103104TEST_ASSERT(strcmp(&assert_msg[offset], expected_assert_msg) == 0,105"Unexpected mismatch. Expected: '%s', got: '%s'",106expected_assert_msg, &assert_msg[offset]);107}108109/*110* Open code vcpu_run(), sans the UCALL_ABORT handling, so that intentional111* guest asserts guest can be verified instead of being reported as failures.112*/113static void do_vcpu_run(struct kvm_vcpu *vcpu)114{115int r;116117do {118r = __vcpu_run(vcpu);119} while (r == -1 && errno == EINTR);120121TEST_ASSERT(!r, KVM_IOCTL_ERROR(KVM_RUN, r));122}123124static void run_test(struct kvm_vcpu *vcpu, const char *expected_printf,125const char *expected_assert)126{127struct kvm_run *run = vcpu->run;128struct ucall uc;129130while (1) {131do_vcpu_run(vcpu);132133TEST_ASSERT(run->exit_reason == UCALL_EXIT_REASON,134"Unexpected exit reason: %u (%s),",135run->exit_reason, exit_reason_str(run->exit_reason));136137switch (get_ucall(vcpu, &uc)) {138case UCALL_SYNC:139TEST_FAIL("Unknown 'args_type' = %lu", uc.args[1]);140break;141case UCALL_PRINTF:142TEST_ASSERT(strcmp(uc.buffer, expected_printf) == 0,143"Unexpected mismatch. Expected: '%s', got: '%s'",144expected_printf, uc.buffer);145break;146case UCALL_ABORT:147ucall_abort(uc.buffer, expected_assert);148break;149case UCALL_DONE:150return;151default:152TEST_FAIL("Unknown ucall %lu", uc.cmd);153}154}155}156157static void guest_code_limits(void)158{159char test_str[UCALL_BUFFER_LEN + 10];160161memset(test_str, 'a', sizeof(test_str));162test_str[sizeof(test_str) - 1] = 0;163164GUEST_PRINTF("%s", test_str);165}166167static void test_limits(void)168{169struct kvm_vcpu *vcpu;170struct kvm_run *run;171struct kvm_vm *vm;172struct ucall uc;173174vm = vm_create_with_one_vcpu(&vcpu, guest_code_limits);175run = vcpu->run;176do_vcpu_run(vcpu);177178TEST_ASSERT(run->exit_reason == UCALL_EXIT_REASON,179"Unexpected exit reason: %u (%s),",180run->exit_reason, exit_reason_str(run->exit_reason));181182TEST_ASSERT(get_ucall(vcpu, &uc) == UCALL_ABORT,183"Unexpected ucall command: %lu, Expected: %u (UCALL_ABORT)",184uc.cmd, UCALL_ABORT);185186kvm_vm_free(vm);187}188189int main(int argc, char *argv[])190{191struct kvm_vcpu *vcpu;192struct kvm_vm *vm;193194vm = vm_create_with_one_vcpu(&vcpu, guest_code);195196test_type_i64(vcpu, -1, -1);197test_type_i64(vcpu, -1, 1);198test_type_i64(vcpu, 0x1234567890abcdef, 0x1234567890abcdef);199test_type_i64(vcpu, 0x1234567890abcdef, 0x1234567890abcdee);200201test_type_u64(vcpu, 0x1234567890abcdef, 0x1234567890abcdef);202test_type_u64(vcpu, 0x1234567890abcdef, 0x1234567890abcdee);203test_type_x64(vcpu, 0x1234567890abcdef, 0x1234567890abcdef);204test_type_x64(vcpu, 0x1234567890abcdef, 0x1234567890abcdee);205test_type_X64(vcpu, 0x1234567890abcdef, 0x1234567890abcdef);206test_type_X64(vcpu, 0x1234567890abcdef, 0x1234567890abcdee);207208test_type_u32(vcpu, 0x90abcdef, 0x90abcdef);209test_type_u32(vcpu, 0x90abcdef, 0x90abcdee);210test_type_x32(vcpu, 0x90abcdef, 0x90abcdef);211test_type_x32(vcpu, 0x90abcdef, 0x90abcdee);212test_type_X32(vcpu, 0x90abcdef, 0x90abcdef);213test_type_X32(vcpu, 0x90abcdef, 0x90abcdee);214215test_type_int(vcpu, -1, -1);216test_type_int(vcpu, -1, 1);217test_type_int(vcpu, 1, 1);218219test_type_char(vcpu, 'a', 'a');220test_type_char(vcpu, 'a', 'A');221test_type_char(vcpu, 'a', 'b');222223test_type_str(vcpu, "foo", "foo");224test_type_str(vcpu, "foo", "bar");225226test_type_ptr(vcpu, 0x1234567890abcdef, 0x1234567890abcdef);227test_type_ptr(vcpu, 0x1234567890abcdef, 0x1234567890abcdee);228229kvm_vm_free(vm);230231test_limits();232233return 0;234}235236237