xref: /kvm-unit-tests/s390x/selftest.c (revision 6afb94812d924a754e2d44f6c5de9e1859b2df28)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (c) 2017 Red Hat Inc
4  *
5  * Authors:
6  *  Thomas Huth <thuth@redhat.com>
7  *  David Hildenbrand <david@redhat.com>
8  */
9 #include <libcflat.h>
10 #include <util.h>
11 #include <alloc.h>
12 #include <asm/interrupt.h>
13 #include <asm/barrier.h>
14 #include <asm/pgtable.h>
15 
16 static void test_fp(void)
17 {
18 	double a = 3.0;
19 	double b = 2.0;
20 	double c;
21 
22 	asm volatile(
23 		"	ddb %1, %2\n"
24 		"	std %1, %0\n"
25 		: "=m" (c) : "f" (a), "m" (b));
26 
27 	report(c == 1.5, "3.0/2.0 == 1.5");
28 }
29 
30 static void test_pgm_int(void)
31 {
32 	expect_pgm_int();
33 	asm volatile("	.insn e,0x0000"); /* used for SW breakpoints in QEMU */
34 	check_pgm_int_code(PGM_INT_CODE_OPERATION);
35 
36 	expect_pgm_int();
37 	asm volatile("	stg %0,0(%0)\n" : : "a"(-1L));
38 	check_pgm_int_code(PGM_INT_CODE_ADDRESSING);
39 }
40 
41 static void test_malloc(void)
42 {
43 	int *tmp, *tmp2;
44 
45 	report_prefix_push("malloc");
46 
47 	report_prefix_push("ptr_0");
48 	tmp = malloc(sizeof(int));
49 	report((uintptr_t)tmp & 0xf000000000000000ul, "allocated memory");
50 	*tmp = 123456789;
51 	mb();
52 	report(*tmp == 123456789, "wrote allocated memory");
53 	report_prefix_pop();
54 
55 	report_prefix_push("ptr_1");
56 	tmp2 = malloc(sizeof(int));
57 	report((uintptr_t)tmp2 & 0xf000000000000000ul,
58 	       "allocated memory");
59 	*tmp2 = 123456789;
60 	mb();
61 	report((*tmp2 == 123456789), "wrote allocated memory");
62 	report_prefix_pop();
63 
64 	report(tmp != tmp2, "allocated memory addresses differ");
65 
66 	expect_pgm_int();
67 	disable_dat();
68 	*tmp = 987654321;
69 	enable_dat();
70 	check_pgm_int_code(PGM_INT_CODE_ADDRESSING);
71 
72 	free(tmp);
73 	free(tmp2);
74 	report_prefix_pop();
75 }
76 
77 int main(int argc, char**argv)
78 {
79 	report_prefix_push("selftest");
80 
81 	report_pass("true");
82 	report(argc == 3, "argc == 3");
83 	report(!strcmp(argv[0], "s390x/selftest.elf"), "argv[0] == PROGNAME");
84 	report(!strcmp(argv[1], "test"), "argv[1] == test");
85 	report(!strcmp(argv[2], "123"), "argv[2] == 123");
86 
87 	setup_vm();
88 
89 	test_fp();
90 	test_pgm_int();
91 	test_malloc();
92 
93 	return report_summary();
94 }
95