xref: /openbmc/linux/tools/testing/selftests/kvm/include/aarch64/processor.h (revision 67bb66d32905627e29400e2cb7f87a7c4c8cf667)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * AArch64 processor specific defines
4  *
5  * Copyright (C) 2018, Red Hat, Inc.
6  */
7 #ifndef SELFTEST_KVM_PROCESSOR_H
8 #define SELFTEST_KVM_PROCESSOR_H
9 
10 #include "kvm_util.h"
11 #include <linux/stringify.h>
12 
13 
14 #define ARM64_CORE_REG(x) (KVM_REG_ARM64 | KVM_REG_SIZE_U64 | \
15 			   KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(x))
16 
17 #define CPACR_EL1               3, 0,  1, 0, 2
18 #define TCR_EL1                 3, 0,  2, 0, 2
19 #define MAIR_EL1                3, 0, 10, 2, 0
20 #define TTBR0_EL1               3, 0,  2, 0, 0
21 #define SCTLR_EL1               3, 0,  1, 0, 0
22 #define VBAR_EL1                3, 0, 12, 0, 0
23 
24 #define ID_AA64DFR0_EL1         3, 0,  0, 5, 0
25 
26 /*
27  * Default MAIR
28  *                  index   attribute
29  * DEVICE_nGnRnE      0     0000:0000
30  * DEVICE_nGnRE       1     0000:0100
31  * DEVICE_GRE         2     0000:1100
32  * NORMAL_NC          3     0100:0100
33  * NORMAL             4     1111:1111
34  * NORMAL_WT          5     1011:1011
35  */
36 #define DEFAULT_MAIR_EL1 ((0x00ul << (0 * 8)) | \
37 			  (0x04ul << (1 * 8)) | \
38 			  (0x0cul << (2 * 8)) | \
39 			  (0x44ul << (3 * 8)) | \
40 			  (0xfful << (4 * 8)) | \
41 			  (0xbbul << (5 * 8)))
42 
43 static inline void get_reg(struct kvm_vm *vm, uint32_t vcpuid, uint64_t id, uint64_t *addr)
44 {
45 	struct kvm_one_reg reg;
46 	reg.id = id;
47 	reg.addr = (uint64_t)addr;
48 	vcpu_ioctl(vm, vcpuid, KVM_GET_ONE_REG, &reg);
49 }
50 
51 static inline void set_reg(struct kvm_vm *vm, uint32_t vcpuid, uint64_t id, uint64_t val)
52 {
53 	struct kvm_one_reg reg;
54 	reg.id = id;
55 	reg.addr = (uint64_t)&val;
56 	vcpu_ioctl(vm, vcpuid, KVM_SET_ONE_REG, &reg);
57 }
58 
59 void aarch64_vcpu_setup(struct kvm_vm *vm, int vcpuid, struct kvm_vcpu_init *init);
60 void aarch64_vcpu_add_default(struct kvm_vm *vm, uint32_t vcpuid,
61 			      struct kvm_vcpu_init *init, void *guest_code);
62 
63 struct ex_regs {
64 	u64 regs[31];
65 	u64 sp;
66 	u64 pc;
67 	u64 pstate;
68 };
69 
70 #define VECTOR_NUM	16
71 
72 enum {
73 	VECTOR_SYNC_CURRENT_SP0,
74 	VECTOR_IRQ_CURRENT_SP0,
75 	VECTOR_FIQ_CURRENT_SP0,
76 	VECTOR_ERROR_CURRENT_SP0,
77 
78 	VECTOR_SYNC_CURRENT,
79 	VECTOR_IRQ_CURRENT,
80 	VECTOR_FIQ_CURRENT,
81 	VECTOR_ERROR_CURRENT,
82 
83 	VECTOR_SYNC_LOWER_64,
84 	VECTOR_IRQ_LOWER_64,
85 	VECTOR_FIQ_LOWER_64,
86 	VECTOR_ERROR_LOWER_64,
87 
88 	VECTOR_SYNC_LOWER_32,
89 	VECTOR_IRQ_LOWER_32,
90 	VECTOR_FIQ_LOWER_32,
91 	VECTOR_ERROR_LOWER_32,
92 };
93 
94 #define VECTOR_IS_SYNC(v) ((v) == VECTOR_SYNC_CURRENT_SP0 || \
95 			   (v) == VECTOR_SYNC_CURRENT     || \
96 			   (v) == VECTOR_SYNC_LOWER_64    || \
97 			   (v) == VECTOR_SYNC_LOWER_32)
98 
99 #define ESR_EC_NUM		64
100 #define ESR_EC_SHIFT		26
101 #define ESR_EC_MASK		(ESR_EC_NUM - 1)
102 
103 #define ESR_EC_SVC64		0x15
104 #define ESR_EC_HW_BP_CURRENT	0x31
105 #define ESR_EC_SSTEP_CURRENT	0x33
106 #define ESR_EC_WP_CURRENT	0x35
107 #define ESR_EC_BRK_INS		0x3c
108 
109 void vm_init_descriptor_tables(struct kvm_vm *vm);
110 void vcpu_init_descriptor_tables(struct kvm_vm *vm, uint32_t vcpuid);
111 
112 typedef void(*handler_fn)(struct ex_regs *);
113 void vm_install_exception_handler(struct kvm_vm *vm,
114 		int vector, handler_fn handler);
115 void vm_install_sync_handler(struct kvm_vm *vm,
116 		int vector, int ec, handler_fn handler);
117 
118 #define write_sysreg(reg, val)						  \
119 ({									  \
120 	u64 __val = (u64)(val);						  \
121 	asm volatile("msr " __stringify(reg) ", %x0" : : "rZ" (__val));	  \
122 })
123 
124 #define read_sysreg(reg)						  \
125 ({	u64 val;							  \
126 	asm volatile("mrs %0, "__stringify(reg) : "=r"(val) : : "memory");\
127 	val;								  \
128 })
129 
130 #define isb()	asm volatile("isb" : : : "memory")
131 
132 #endif /* SELFTEST_KVM_PROCESSOR_H */
133