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