xref: /kvm-unit-tests/s390x/cmm.c (revision 2c96b77ec9d3b1fcec7525174e23a6240ee05949)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * CMM tests (ESSA)
4  *
5  * Copyright (c) 2018 IBM Corp
6  *
7  * Authors:
8  *  Janosch Frank <frankja@linux.vnet.ibm.com>
9  */
10 
11 #include <libcflat.h>
12 #include <asm/asm-offsets.h>
13 #include <asm/interrupt.h>
14 #include <asm/page.h>
15 
16 static uint8_t pagebuf[PAGE_SIZE] __attribute__((aligned(PAGE_SIZE)));
17 
18 static unsigned long essa(uint8_t state, unsigned long paddr)
19 {
20 	uint64_t extr_state;
21 
22 	asm volatile(".insn rrf,0xb9ab0000,%[extr_state],%[addr],%[new_state],0"
23 			: [extr_state] "=d" (extr_state)
24 			: [addr] "a" (paddr), [new_state] "i" (state));
25 	return (unsigned long)extr_state;
26 }
27 
28 static void test_params(void)
29 {
30 	report_prefix_push("invalid ORC 8");
31 	expect_pgm_int();
32 	essa(8, (unsigned long)pagebuf);
33 	check_pgm_int_code(PGM_INT_CODE_SPECIFICATION);
34 	report_prefix_pop();
35 }
36 
37 static void test_priv(void)
38 {
39 	report_prefix_push("privileged");
40 	expect_pgm_int();
41 	enter_pstate();
42 	essa(0, (unsigned long)pagebuf);
43 	check_pgm_int_code(PGM_INT_CODE_PRIVILEGED_OPERATION);
44 	report_prefix_pop();
45 }
46 
47 /* Unfortunately the availability is not indicated by stfl bits, but
48  * we have to try to execute it and test for an operation exception.
49  */
50 static bool test_availability(void)
51 {
52 	expect_pgm_int();
53 	essa(0, (unsigned long)pagebuf);
54 	return clear_pgm_int() == 0;
55 }
56 
57 int main(void)
58 {
59 	bool has_essa = test_availability();
60 
61 	report_prefix_push("cmm");
62 	if (!has_essa) {
63 		report_skip("ESSA is not available");
64 		goto done;
65 	}
66 
67 	test_priv();
68 	test_params();
69 done:
70 	report_prefix_pop();
71 	return report_summary();
72 }
73