tools/testing/selftests/kvm/s390/ucontrol_test.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/kvm/s390/ucontrol_test.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/kvm/s390/ucontrol_test.c
Extension
.c
Size
22391 bytes
Lines
799
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

// SPDX-License-Identifier: GPL-2.0-only
/*
 * Test code for the s390x kvm ucontrol interface
 *
 * Copyright IBM Corp. 2024
 *
 * Authors:
 *  Christoph Schlameuss <schlameuss@linux.ibm.com>
 */
#include "debug_print.h"
#include "kselftest_harness.h"
#include "kvm_util.h"
#include "processor.h"
#include "sie.h"

#include <linux/capability.h>
#include <linux/sizes.h>

#define PGM_SEGMENT_TRANSLATION 0x10

#define VM_MEM_SIZE (4 * SZ_1M)
#define VM_MEM_EXT_SIZE (2 * SZ_1M)
#define VM_MEM_MAX_M ((VM_MEM_SIZE + VM_MEM_EXT_SIZE) / SZ_1M)

/* so directly declare capget to check caps without libcap */
int capget(cap_user_header_t header, cap_user_data_t data);

/**
 * In order to create user controlled virtual machines on S390,
 * check KVM_CAP_S390_UCONTROL and use the flag KVM_VM_S390_UCONTROL
 * as privileged user (SYS_ADMIN).
 */
void require_ucontrol_admin(void)
{
	struct __user_cap_data_struct data[_LINUX_CAPABILITY_U32S_3];
	struct __user_cap_header_struct hdr = {
		.version = _LINUX_CAPABILITY_VERSION_3,
	};
	int rc;

	rc = capget(&hdr, data);
	TEST_ASSERT_EQ(0, rc);
	TEST_REQUIRE((data->effective & CAP_TO_MASK(CAP_SYS_ADMIN)) > 0);

	TEST_REQUIRE(kvm_has_cap(KVM_CAP_S390_UCONTROL));
}

/* Test program setting some registers and looping */
extern char test_gprs_asm[];
asm("test_gprs_asm:\n"
	"xgr	%r0, %r0\n"
	"lgfi	%r1,1\n"
	"lgfi	%r2,2\n"
	"lgfi	%r3,3\n"
	"lgfi	%r4,4\n"
	"lgfi	%r5,5\n"
	"lgfi	%r6,6\n"
	"lgfi	%r7,7\n"
	"0:\n"
	"	diag	0,0,0x44\n"
	"	ahi	%r0,1\n"
	"	j	0b\n"
);

/* Test program manipulating memory */
extern char test_mem_asm[];
asm("test_mem_asm:\n"
	"xgr	%r0, %r0\n"

	"0:\n"
	"	ahi	%r0,1\n"
	"	st	%r1,0(%r5,%r6)\n"

	"	xgr	%r1,%r1\n"
	"	l	%r1,0(%r5,%r6)\n"
	"	ahi	%r0,1\n"
	"	diag	0,0,0x44\n"

	"	j	0b\n"
);

/* Test program manipulating storage keys */
extern char test_skey_asm[];
asm("test_skey_asm:\n"
	"xgr	%r0, %r0\n"

	"0:\n"
	"	ahi	%r0,1\n"
	"	st	%r1,0(%r5,%r6)\n"

Annotation

Implementation Notes