1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (C) 2023 SUSE LLC
4f08c3bdfSopenharmony_ci * Author: Nicolai Stange <nstange@suse.de>
5f08c3bdfSopenharmony_ci * LTP port: Martin Doucha <mdoucha@suse.cz>
6f08c3bdfSopenharmony_ci */
7f08c3bdfSopenharmony_ci
8f08c3bdfSopenharmony_ci/*\
9f08c3bdfSopenharmony_ci * Check that KVM correctly intercepts the CLGI instruction in a nested
10f08c3bdfSopenharmony_ci * virtual machine even when the parent guest disables intercept.
11f08c3bdfSopenharmony_ci * If KVM does not override the disabled intercept, it'll allow the nested VM
12f08c3bdfSopenharmony_ci * to hold the physical CPU indefinitely and potentially perform a denial
13f08c3bdfSopenharmony_ci * of service attack against the host kernel. CPU lockup fixed in:
14f08c3bdfSopenharmony_ci *
15f08c3bdfSopenharmony_ci *  commit 91b7130cb6606d8c6b3b77e54426b3f3a83f48b1
16f08c3bdfSopenharmony_ci *  Author: Paolo Bonzini <pbonzini@redhat.com>
17f08c3bdfSopenharmony_ci *  Date:   Fri May 22 12:28:52 2020 -0400
18f08c3bdfSopenharmony_ci *
19f08c3bdfSopenharmony_ci *  KVM: SVM: preserve VGIF across VMCB switch
20f08c3bdfSopenharmony_ci */
21f08c3bdfSopenharmony_ci
22f08c3bdfSopenharmony_ci#include "kvm_test.h"
23f08c3bdfSopenharmony_ci
24f08c3bdfSopenharmony_ci#ifdef COMPILE_PAYLOAD
25f08c3bdfSopenharmony_ci#if defined(__i386__) || defined(__x86_64__)
26f08c3bdfSopenharmony_ci
27f08c3bdfSopenharmony_ci#include "kvm_x86_svm.h"
28f08c3bdfSopenharmony_ci
29f08c3bdfSopenharmony_ci/* Disable global interrupts */
30f08c3bdfSopenharmony_cistatic int guest_clgi(void)
31f08c3bdfSopenharmony_ci{
32f08c3bdfSopenharmony_ci	int ret, *result = (int *)KVM_RESULT_BASEADDR;
33f08c3bdfSopenharmony_ci
34f08c3bdfSopenharmony_ci	/*
35f08c3bdfSopenharmony_ci	 * Make sure that result page is present in memory. CLGI may disable
36f08c3bdfSopenharmony_ci	 * page fault handling on the current CPU. The actual value
37f08c3bdfSopenharmony_ci	 * at that address is irrelevant.
38f08c3bdfSopenharmony_ci	 */
39f08c3bdfSopenharmony_ci	ret = *result;
40f08c3bdfSopenharmony_ci
41f08c3bdfSopenharmony_ci	/* Disable global interrupts */
42f08c3bdfSopenharmony_ci	asm ("clgi");
43f08c3bdfSopenharmony_ci
44f08c3bdfSopenharmony_ci	/* Signal host to kill the VM and wait */
45f08c3bdfSopenharmony_ci	tst_wait_host(NULL);
46f08c3bdfSopenharmony_ci	return ret;
47f08c3bdfSopenharmony_ci}
48f08c3bdfSopenharmony_ci
49f08c3bdfSopenharmony_civoid main(void)
50f08c3bdfSopenharmony_ci{
51f08c3bdfSopenharmony_ci	struct kvm_svm_vcpu *vcpu;
52f08c3bdfSopenharmony_ci
53f08c3bdfSopenharmony_ci	kvm_init_svm();
54f08c3bdfSopenharmony_ci	vcpu = kvm_create_svm_vcpu(guest_clgi, 1);
55f08c3bdfSopenharmony_ci	kvm_vmcb_set_intercept(vcpu->vmcb, SVM_INTERCEPT_CLGI, 0);
56f08c3bdfSopenharmony_ci	kvm_svm_vmrun(vcpu);
57f08c3bdfSopenharmony_ci
58f08c3bdfSopenharmony_ci	if (vcpu->vmcb->exitcode != SVM_EXIT_HLT)
59f08c3bdfSopenharmony_ci		tst_brk(TBROK, "Nested VM exited unexpectedly");
60f08c3bdfSopenharmony_ci}
61f08c3bdfSopenharmony_ci
62f08c3bdfSopenharmony_ci#else /* defined(__i386__) || defined(__x86_64__) */
63f08c3bdfSopenharmony_ciTST_TEST_TCONF("Test supported only on x86");
64f08c3bdfSopenharmony_ci#endif /* defined(__i386__) || defined(__x86_64__) */
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_ci#else /* COMPILE_PAYLOAD */
67f08c3bdfSopenharmony_ci
68f08c3bdfSopenharmony_ci#include <pthread.h>
69f08c3bdfSopenharmony_ci#include "tst_safe_pthread.h"
70f08c3bdfSopenharmony_ci#include "tst_safe_clocks.h"
71f08c3bdfSopenharmony_ci
72f08c3bdfSopenharmony_cistatic struct tst_kvm_instance test_vm = { .vm_fd = -1 };
73f08c3bdfSopenharmony_cistatic pthread_mutex_t mutex;
74f08c3bdfSopenharmony_cistatic int mutex_init;
75f08c3bdfSopenharmony_ci
76f08c3bdfSopenharmony_cistatic void sighandler(int sig LTP_ATTRIBUTE_UNUSED)
77f08c3bdfSopenharmony_ci{
78f08c3bdfSopenharmony_ci
79f08c3bdfSopenharmony_ci}
80f08c3bdfSopenharmony_ci
81f08c3bdfSopenharmony_cistatic void *vm_thread(void *arg)
82f08c3bdfSopenharmony_ci{
83f08c3bdfSopenharmony_ci	SAFE_PTHREAD_MUTEX_LOCK(&mutex);
84f08c3bdfSopenharmony_ci	tst_kvm_run_instance(&test_vm, EINTR);
85f08c3bdfSopenharmony_ci	SAFE_PTHREAD_MUTEX_UNLOCK(&mutex);
86f08c3bdfSopenharmony_ci	return arg;
87f08c3bdfSopenharmony_ci}
88f08c3bdfSopenharmony_ci
89f08c3bdfSopenharmony_cistatic void setup(void)
90f08c3bdfSopenharmony_ci{
91f08c3bdfSopenharmony_ci	struct sigaction sa = { .sa_handler = sighandler };
92f08c3bdfSopenharmony_ci	pthread_mutexattr_t attr;
93f08c3bdfSopenharmony_ci
94f08c3bdfSopenharmony_ci	SAFE_PTHREAD_MUTEXATTR_INIT(&attr);
95f08c3bdfSopenharmony_ci	SAFE_PTHREAD_MUTEXATTR_SETTYPE(&attr, PTHREAD_MUTEX_NORMAL);
96f08c3bdfSopenharmony_ci	SAFE_PTHREAD_MUTEX_INIT(&mutex, &attr);
97f08c3bdfSopenharmony_ci	mutex_init = 1;
98f08c3bdfSopenharmony_ci	SAFE_PTHREAD_MUTEXATTR_DESTROY(&attr);
99f08c3bdfSopenharmony_ci	SAFE_SIGACTION(SIGUSR1, &sa, NULL);
100f08c3bdfSopenharmony_ci}
101f08c3bdfSopenharmony_ci
102f08c3bdfSopenharmony_cistatic void run(void)
103f08c3bdfSopenharmony_ci{
104f08c3bdfSopenharmony_ci	struct timespec timeout;
105f08c3bdfSopenharmony_ci	pthread_t tid;
106f08c3bdfSopenharmony_ci	int ret;
107f08c3bdfSopenharmony_ci
108f08c3bdfSopenharmony_ci	tst_kvm_create_instance(&test_vm, DEFAULT_RAM_SIZE);
109f08c3bdfSopenharmony_ci
110f08c3bdfSopenharmony_ci	SAFE_PTHREAD_CREATE(&tid, NULL, vm_thread, NULL);
111f08c3bdfSopenharmony_ci	ret = tst_kvm_wait_guest(&test_vm, 2000);
112f08c3bdfSopenharmony_ci
113f08c3bdfSopenharmony_ci	if (ret == KVM_TEXIT) {
114f08c3bdfSopenharmony_ci		SAFE_PTHREAD_JOIN(tid, NULL);
115f08c3bdfSopenharmony_ci		tst_brk(TCONF, "Guest exited early");
116f08c3bdfSopenharmony_ci	}
117f08c3bdfSopenharmony_ci
118f08c3bdfSopenharmony_ci	if (ret)
119f08c3bdfSopenharmony_ci		tst_brk(TBROK, "Wait for guest initialization timed out");
120f08c3bdfSopenharmony_ci
121f08c3bdfSopenharmony_ci	SAFE_PTHREAD_KILL(tid, SIGUSR1);
122f08c3bdfSopenharmony_ci	SAFE_CLOCK_GETTIME(CLOCK_REALTIME, &timeout);
123f08c3bdfSopenharmony_ci	timeout.tv_sec += 2;
124f08c3bdfSopenharmony_ci
125f08c3bdfSopenharmony_ci	if (SAFE_PTHREAD_MUTEX_TIMEDLOCK(&mutex, &timeout)) {
126f08c3bdfSopenharmony_ci		tst_kvm_clear_guest_signal(&test_vm);
127f08c3bdfSopenharmony_ci		tst_res(TFAIL, "VM thread does not respond to signals");
128f08c3bdfSopenharmony_ci	} else {
129f08c3bdfSopenharmony_ci		SAFE_PTHREAD_MUTEX_UNLOCK(&mutex);
130f08c3bdfSopenharmony_ci		tst_res(TPASS, "VM thread was interrupted by signal");
131f08c3bdfSopenharmony_ci	}
132f08c3bdfSopenharmony_ci
133f08c3bdfSopenharmony_ci	SAFE_PTHREAD_JOIN(tid, NULL);
134f08c3bdfSopenharmony_ci	tst_kvm_destroy_instance(&test_vm);
135f08c3bdfSopenharmony_ci	tst_free_all();
136f08c3bdfSopenharmony_ci}
137f08c3bdfSopenharmony_ci
138f08c3bdfSopenharmony_cistatic void cleanup(void)
139f08c3bdfSopenharmony_ci{
140f08c3bdfSopenharmony_ci	/*
141f08c3bdfSopenharmony_ci	 * If the mutex is locked, the VM is likely still running, cannot
142f08c3bdfSopenharmony_ci	 * clean up anything
143f08c3bdfSopenharmony_ci	 */
144f08c3bdfSopenharmony_ci	if (!mutex_init || SAFE_PTHREAD_MUTEX_TRYLOCK(&mutex))
145f08c3bdfSopenharmony_ci		return;
146f08c3bdfSopenharmony_ci
147f08c3bdfSopenharmony_ci	if (!SAFE_PTHREAD_MUTEX_UNLOCK(&mutex))
148f08c3bdfSopenharmony_ci		SAFE_PTHREAD_MUTEX_DESTROY(&mutex);
149f08c3bdfSopenharmony_ci
150f08c3bdfSopenharmony_ci	tst_kvm_destroy_instance(&test_vm);
151f08c3bdfSopenharmony_ci}
152f08c3bdfSopenharmony_ci
153f08c3bdfSopenharmony_cistatic struct tst_test test = {
154f08c3bdfSopenharmony_ci	.test_all = run,
155f08c3bdfSopenharmony_ci	.setup = setup,
156f08c3bdfSopenharmony_ci	.cleanup = cleanup,
157f08c3bdfSopenharmony_ci	.min_cpus = 2,
158f08c3bdfSopenharmony_ci	.supported_archs = (const char *const []) {
159f08c3bdfSopenharmony_ci		"x86_64",
160f08c3bdfSopenharmony_ci		"x86",
161f08c3bdfSopenharmony_ci		NULL
162f08c3bdfSopenharmony_ci	},
163f08c3bdfSopenharmony_ci	.tags = (struct tst_tag[]){
164f08c3bdfSopenharmony_ci		{"linux-git", "91b7130cb660"},
165f08c3bdfSopenharmony_ci		{}
166f08c3bdfSopenharmony_ci	}
167f08c3bdfSopenharmony_ci};
168f08c3bdfSopenharmony_ci
169f08c3bdfSopenharmony_ci#endif /* COMPILE_PAYLOAD */
170