xref: /kvm-unit-tests/s390x/selftest.c (revision fdab948bc134fb9989a8265380a55e809879418e)
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 = malloc(sizeof(int));
44 	int *tmp2 = malloc(sizeof(int));
45 
46 	*tmp = 123456789;
47 	*tmp2 = 123456789;
48 	mb();
49 
50 	report((uintptr_t)tmp & 0xf000000000000000ul, "malloc: got vaddr");
51 	report(*tmp == 123456789, "malloc: access works");
52 	report((uintptr_t)tmp2 & 0xf000000000000000ul,
53 	       "malloc: got 2nd vaddr");
54 	report((*tmp2 == 123456789), "malloc: access works");
55 	report(tmp != tmp2, "malloc: addresses differ");
56 
57 	expect_pgm_int();
58 	configure_dat(0);
59 	*tmp = 987654321;
60 	configure_dat(1);
61 	check_pgm_int_code(PGM_INT_CODE_ADDRESSING);
62 
63 	free(tmp);
64 	free(tmp2);
65 }
66 
67 int main(int argc, char**argv)
68 {
69 	report_prefix_push("selftest");
70 
71 	report(true, "true");
72 	report(argc == 3, "argc == 3");
73 	report(!strcmp(argv[0], "s390x/selftest.elf"), "argv[0] == PROGNAME");
74 	report(!strcmp(argv[1], "test"), "argv[1] == test");
75 	report(!strcmp(argv[2], "123"), "argv[2] == 123");
76 
77 	setup_vm();
78 
79 	test_fp();
80 	test_pgm_int();
81 	test_malloc();
82 
83 	return report_summary();
84 }
85