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