xref: /kvm-unit-tests/s390x/snippets/c/sie-dat.c (revision dfc1fec2fbde04ad607e1aed560cf7059350c70f)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Snippet used by the sie-dat.c test to verify paging without MSO/MSL
4  *
5  * Copyright (c) 2023 IBM Corp
6  *
7  * Authors:
8  *  Nico Boehr <nrb@linux.ibm.com>
9  */
10 #include <libcflat.h>
11 #include <asm-generic/page.h>
12 #include "sie-dat.h"
13 
14 static uint8_t test_pages[GUEST_TEST_PAGE_COUNT * PAGE_SIZE] __attribute__((__aligned__(PAGE_SIZE)));
15 
16 static inline void force_exit(void)
17 {
18 	asm volatile("diag	0,0,0x44\n"
19 		     :
20 		     :
21 		     : "memory"
22 	);
23 }
24 
25 static inline void force_exit_value(uint64_t val)
26 {
27 	asm volatile("diag	%[val],0,0x9c\n"
28 		     :
29 		     : [val] "d"(val)
30 		     : "memory"
31 	);
32 }
33 
34 int main(void)
35 {
36 	uint8_t *invalid_ptr;
37 
38 	memset(test_pages, 0, sizeof(test_pages));
39 	/* tell the host the page's physical address (we're running DAT off) */
40 	force_exit_value((uint64_t)test_pages);
41 
42 	/* write some value to the page so the host can verify it */
43 	for (size_t i = 0; i < GUEST_TEST_PAGE_COUNT; i++)
44 		test_pages[i * PAGE_SIZE] = 42 + i;
45 
46 	/* indicate we've written all pages */
47 	force_exit();
48 
49 	/* the first unmapped address */
50 	invalid_ptr = (uint8_t *)(GUEST_TOTAL_PAGE_COUNT * PAGE_SIZE);
51 	*invalid_ptr = 42;
52 
53 	/* indicate we've written the non-allowed page (should never get here) */
54 	force_exit();
55 
56 	return 0;
57 }
58