Path: blob/master/tools/testing/selftests/kvm/s390/sync_regs_test.c
38237 views
// SPDX-License-Identifier: GPL-2.0-only1/*2* Test for s390x KVM_CAP_SYNC_REGS3*4* Based on the same test for x86:5* Copyright (C) 2018, Google LLC.6*7* Adaptions for s390x:8* Copyright (C) 2019, Red Hat, Inc.9*10* Test expected behavior of the KVM_CAP_SYNC_REGS functionality.11*/12#include <fcntl.h>13#include <stdio.h>14#include <stdlib.h>15#include <string.h>16#include <sys/ioctl.h>1718#include "test_util.h"19#include "kvm_util.h"20#include "diag318_test_handler.h"21#include "kselftest.h"2223static void guest_code(void)24{25/*26* We embed diag 501 here instead of doing a ucall to avoid that27* the compiler has messed with r11 at the time of the ucall.28*/29asm volatile (30"0: diag 0,0,0x501\n"31" ahi 11,1\n"32" j 0b\n"33);34}3536#define REG_COMPARE(reg) \37TEST_ASSERT(left->reg == right->reg, \38"Register " #reg \39" values did not match: 0x%llx, 0x%llx", \40left->reg, right->reg)4142#define REG_COMPARE32(reg) \43TEST_ASSERT(left->reg == right->reg, \44"Register " #reg \45" values did not match: 0x%x, 0x%x", \46left->reg, right->reg)474849static void compare_regs(struct kvm_regs *left, struct kvm_sync_regs *right)50{51int i;5253for (i = 0; i < 16; i++)54REG_COMPARE(gprs[i]);55}5657static void compare_sregs(struct kvm_sregs *left, struct kvm_sync_regs *right)58{59int i;6061for (i = 0; i < 16; i++)62REG_COMPARE32(acrs[i]);6364for (i = 0; i < 16; i++)65REG_COMPARE(crs[i]);66}6768#undef REG_COMPARE6970#define TEST_SYNC_FIELDS (KVM_SYNC_GPRS|KVM_SYNC_ACRS|KVM_SYNC_CRS|KVM_SYNC_DIAG318)71#define INVALID_SYNC_FIELD 0x800000007273void test_read_invalid(struct kvm_vcpu *vcpu)74{75struct kvm_run *run = vcpu->run;76int rv;7778/* Request reading invalid register set from VCPU. */79run->kvm_valid_regs = INVALID_SYNC_FIELD;80rv = _vcpu_run(vcpu);81TEST_ASSERT(rv < 0 && errno == EINVAL,82"Invalid kvm_valid_regs did not cause expected KVM_RUN error: %d",83rv);84run->kvm_valid_regs = 0;8586run->kvm_valid_regs = INVALID_SYNC_FIELD | TEST_SYNC_FIELDS;87rv = _vcpu_run(vcpu);88TEST_ASSERT(rv < 0 && errno == EINVAL,89"Invalid kvm_valid_regs did not cause expected KVM_RUN error: %d",90rv);91run->kvm_valid_regs = 0;92}9394void test_set_invalid(struct kvm_vcpu *vcpu)95{96struct kvm_run *run = vcpu->run;97int rv;9899/* Request setting invalid register set into VCPU. */100run->kvm_dirty_regs = INVALID_SYNC_FIELD;101rv = _vcpu_run(vcpu);102TEST_ASSERT(rv < 0 && errno == EINVAL,103"Invalid kvm_dirty_regs did not cause expected KVM_RUN error: %d",104rv);105run->kvm_dirty_regs = 0;106107run->kvm_dirty_regs = INVALID_SYNC_FIELD | TEST_SYNC_FIELDS;108rv = _vcpu_run(vcpu);109TEST_ASSERT(rv < 0 && errno == EINVAL,110"Invalid kvm_dirty_regs did not cause expected KVM_RUN error: %d",111rv);112run->kvm_dirty_regs = 0;113}114115void test_req_and_verify_all_valid_regs(struct kvm_vcpu *vcpu)116{117struct kvm_run *run = vcpu->run;118struct kvm_sregs sregs;119struct kvm_regs regs;120int rv;121122/* Request and verify all valid register sets. */123run->kvm_valid_regs = TEST_SYNC_FIELDS;124rv = _vcpu_run(vcpu);125TEST_ASSERT(rv == 0, "vcpu_run failed: %d", rv);126TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_S390_SIEIC);127TEST_ASSERT(run->s390_sieic.icptcode == 4 &&128(run->s390_sieic.ipa >> 8) == 0x83 &&129(run->s390_sieic.ipb >> 16) == 0x501,130"Unexpected interception code: ic=%u, ipa=0x%x, ipb=0x%x",131run->s390_sieic.icptcode, run->s390_sieic.ipa,132run->s390_sieic.ipb);133134vcpu_regs_get(vcpu, ®s);135compare_regs(®s, &run->s.regs);136137vcpu_sregs_get(vcpu, &sregs);138compare_sregs(&sregs, &run->s.regs);139}140141void test_set_and_verify_various_reg_values(struct kvm_vcpu *vcpu)142{143struct kvm_run *run = vcpu->run;144struct kvm_sregs sregs;145struct kvm_regs regs;146int rv;147148/* Set and verify various register values */149run->s.regs.gprs[11] = 0xBAD1DEA;150run->s.regs.acrs[0] = 1 << 11;151152run->kvm_valid_regs = TEST_SYNC_FIELDS;153run->kvm_dirty_regs = KVM_SYNC_GPRS | KVM_SYNC_ACRS;154155if (get_diag318_info() > 0) {156run->s.regs.diag318 = get_diag318_info();157run->kvm_dirty_regs |= KVM_SYNC_DIAG318;158}159160rv = _vcpu_run(vcpu);161TEST_ASSERT(rv == 0, "vcpu_run failed: %d", rv);162TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_S390_SIEIC);163TEST_ASSERT(run->s.regs.gprs[11] == 0xBAD1DEA + 1,164"r11 sync regs value incorrect 0x%llx.",165run->s.regs.gprs[11]);166TEST_ASSERT(run->s.regs.acrs[0] == 1 << 11,167"acr0 sync regs value incorrect 0x%x.",168run->s.regs.acrs[0]);169TEST_ASSERT(run->s.regs.diag318 == get_diag318_info(),170"diag318 sync regs value incorrect 0x%llx.",171run->s.regs.diag318);172173vcpu_regs_get(vcpu, ®s);174compare_regs(®s, &run->s.regs);175176vcpu_sregs_get(vcpu, &sregs);177compare_sregs(&sregs, &run->s.regs);178}179180void test_clear_kvm_dirty_regs_bits(struct kvm_vcpu *vcpu)181{182struct kvm_run *run = vcpu->run;183int rv;184185/* Clear kvm_dirty_regs bits, verify new s.regs values are186* overwritten with existing guest values.187*/188run->kvm_valid_regs = TEST_SYNC_FIELDS;189run->kvm_dirty_regs = 0;190run->s.regs.gprs[11] = 0xDEADBEEF;191run->s.regs.diag318 = 0x4B1D;192rv = _vcpu_run(vcpu);193TEST_ASSERT(rv == 0, "vcpu_run failed: %d", rv);194TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_S390_SIEIC);195TEST_ASSERT(run->s.regs.gprs[11] != 0xDEADBEEF,196"r11 sync regs value incorrect 0x%llx.",197run->s.regs.gprs[11]);198TEST_ASSERT(run->s.regs.diag318 != 0x4B1D,199"diag318 sync regs value incorrect 0x%llx.",200run->s.regs.diag318);201}202203struct testdef {204const char *name;205void (*test)(struct kvm_vcpu *vcpu);206} testlist[] = {207{ "read invalid", test_read_invalid },208{ "set invalid", test_set_invalid },209{ "request+verify all valid regs", test_req_and_verify_all_valid_regs },210{ "set+verify various regs", test_set_and_verify_various_reg_values },211{ "clear kvm_dirty_regs bits", test_clear_kvm_dirty_regs_bits },212};213214int main(int argc, char *argv[])215{216struct kvm_vcpu *vcpu;217struct kvm_vm *vm;218int idx;219220TEST_REQUIRE(kvm_has_cap(KVM_CAP_SYNC_REGS));221222ksft_print_header();223224ksft_set_plan(ARRAY_SIZE(testlist));225226/* Create VM */227vm = vm_create_with_one_vcpu(&vcpu, guest_code);228229for (idx = 0; idx < ARRAY_SIZE(testlist); idx++) {230testlist[idx].test(vcpu);231ksft_test_result_pass("%s\n", testlist[idx].name);232}233234kvm_vm_free(vm);235236ksft_finished(); /* Print results and exit() accordingly */237}238239240