1 /*
2  * Copyright 2019 The Hafnium Authors.
3  *
4  * Use of this source code is governed by a BSD-style
5  * license that can be found in the LICENSE file or at
6  * https://opensource.org/licenses/BSD-3-Clause.
7  */
8 
9 #include "primary_with_secondary.h"
10 #include "sysregs.h"
11 #include "test/vmapi/ffa.h"
12 
13 /**
14  * QEMU does not properly handle the trapping of certain system register
15  * accesses. This was fixed in a custom local build that we could use. If not
16  * using that build, limit testing to the subset QEMU handles correctly.
17  */
18 #define CUSTOM_QEMU_BUILD() 0
19 
TEAR_DOWN(debug_el1)20 TEAR_DOWN(debug_el1)
21 {
22 	EXPECT_FFA_ERROR(ffa_rx_release(), FFA_DENIED);
23 }
24 
TEST(debug_el1,secondary_basic)25 TEST(debug_el1, secondary_basic)
26 {
27 	struct ffa_value run_res;
28 	struct mailbox_buffers mb = set_up_mailbox();
29 
30 	SERVICE_SELECT(SERVICE_VM1, "debug_el1_secondary_basic", mb.send);
31 
32 	run_res = ffa_run(SERVICE_VM1, 0);
33 	EXPECT_EQ(run_res.func, FFA_YIELD_32);
34 }
35 
36 /**
37  * Attempts to access debug registers for read, without validating their value.
38  */
TEST(debug_el1,primary_basic)39 TEST(debug_el1, primary_basic)
40 {
41 	EXPECT_EQ(hf_vm_get_id(), HF_PRIMARY_VM_ID);
42 
43 	if (CUSTOM_QEMU_BUILD()) {
44 		TRY_READ(DBGAUTHSTATUS_EL1);
45 		TRY_READ(DBGCLAIMCLR_EL1);
46 		TRY_READ(DBGCLAIMSET_EL1);
47 		TRY_READ(DBGPRCR_EL1);
48 		TRY_READ(OSDTRRX_EL1);
49 		TRY_READ(OSDTRTX_EL1);
50 		TRY_READ(OSECCR_EL1);
51 
52 		TRY_READ(DBGBCR2_EL1);
53 		TRY_READ(DBGBCR3_EL1);
54 		TRY_READ(DBGBCR4_EL1);
55 		TRY_READ(DBGBCR5_EL1);
56 		TRY_READ(DBGBVR2_EL1);
57 		TRY_READ(DBGBVR3_EL1);
58 		TRY_READ(DBGBVR4_EL1);
59 		TRY_READ(DBGBVR5_EL1);
60 		TRY_READ(DBGWCR2_EL1);
61 		TRY_READ(DBGWCR3_EL1);
62 		TRY_READ(DBGWVR2_EL1);
63 		TRY_READ(DBGWVR3_EL1);
64 	}
65 
66 	/* The following is the subset currently supported by QEMU. */
67 	TRY_READ(MDCCINT_EL1);
68 	TRY_READ(MDRAR_EL1);
69 	TRY_READ(MDSCR_EL1);
70 	TRY_READ(OSDLR_EL1);
71 	TRY_READ(OSLSR_EL1);
72 
73 	TRY_READ(DBGBCR0_EL1);
74 	TRY_READ(DBGBCR1_EL1);
75 	TRY_READ(DBGBVR0_EL1);
76 	TRY_READ(DBGBVR1_EL1);
77 	TRY_READ(DBGWCR0_EL1);
78 	TRY_READ(DBGWCR1_EL1);
79 	TRY_READ(DBGWVR0_EL1);
80 	TRY_READ(DBGWVR1_EL1);
81 }
82 
83 /**
84  * Tests a few debug registers for read and write, and checks that the expected
85  * value is written/read.
86  */
TEST(debug_el1,primary_read_write)87 TEST(debug_el1, primary_read_write)
88 {
89 	EXPECT_EQ(hf_vm_get_id(), HF_PRIMARY_VM_ID);
90 
91 	CHECK_UPDATE(DBGBCR0_EL1, 0x0, 0x2);
92 	CHECK_UPDATE(DBGBVR0_EL1, 0xc4, 0xf0);
93 }
94