xref: /kvm-unit-tests/s390x/snippets/c/sie-dat.c (revision da49e2919f6b7e14af756e781105acaabd30f7ca)
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 <asm/mem.h>
13 #include "sie-dat.h"
14 
15 static uint8_t test_pages[GUEST_TEST_PAGE_COUNT * PAGE_SIZE] __attribute__((__aligned__(PAGE_SIZE)));
16 
17 static inline void force_exit(void)
18 {
19 	asm volatile("diag	0,0,0x44\n"
20 		     :
21 		     :
22 		     : "memory"
23 	);
24 }
25 
26 static inline void force_exit_value(uint64_t val)
27 {
28 	asm volatile("diag	%[val],0,0x9c\n"
29 		     :
30 		     : [val] "d"(val)
31 		     : "memory"
32 	);
33 }
34 
35 int main(void)
36 {
37 	uint8_t *invalid_ptr;
38 
39 	memset(test_pages, 0, sizeof(test_pages));
40 	/* tell the host the page's physical address (we're running DAT off) */
41 	force_exit_value((uint64_t)test_pages);
42 
43 	/* write some value to the page so the host can verify it */
44 	for (size_t i = 0; i < GUEST_TEST_PAGE_COUNT; i++)
45 		test_pages[i * PAGE_SIZE] = 42 + i;
46 
47 	/* indicate we've written all pages */
48 	force_exit();
49 
50 	/* the first unmapped address */
51 	invalid_ptr = OPAQUE_PTR(GUEST_TOTAL_PAGE_COUNT * PAGE_SIZE);
52 	*invalid_ptr = 42;
53 
54 	/* indicate we've written the non-allowed page (should never get here) */
55 	force_exit();
56 
57 	return 0;
58 }
59