Sanjay Lal | 740765c | 2012-11-21 18:34:00 -0800 | [diff] [blame] | 1 | /* |
| 2 | * This file is subject to the terms and conditions of the GNU General Public |
| 3 | * License. See the file "COPYING" in the main directory of this archive |
| 4 | * for more details. |
| 5 | * |
| 6 | * Copyright (C) 2012 MIPS Technologies, Inc. All rights reserved. |
| 7 | * Authors: Sanjay Lal <sanjayl@kymasys.com> |
| 8 | */ |
| 9 | |
| 10 | #ifndef __MIPS_KVM_HOST_H__ |
| 11 | #define __MIPS_KVM_HOST_H__ |
| 12 | |
James Hogan | c992a4f | 2017-03-14 10:15:31 +0000 | [diff] [blame] | 13 | #include <linux/cpumask.h> |
Sanjay Lal | 740765c | 2012-11-21 18:34:00 -0800 | [diff] [blame] | 14 | #include <linux/mutex.h> |
| 15 | #include <linux/hrtimer.h> |
| 16 | #include <linux/interrupt.h> |
| 17 | #include <linux/types.h> |
| 18 | #include <linux/kvm.h> |
| 19 | #include <linux/kvm_types.h> |
| 20 | #include <linux/threads.h> |
| 21 | #include <linux/spinlock.h> |
| 22 | |
James Hogan | 258f3a2 | 2016-06-15 19:29:47 +0100 | [diff] [blame] | 23 | #include <asm/inst.h> |
James Hogan | e6207bb | 2016-06-09 14:19:19 +0100 | [diff] [blame] | 24 | #include <asm/mipsregs.h> |
| 25 | |
Huacai Chen | f21db30 | 2020-05-23 15:56:37 +0800 | [diff] [blame] | 26 | #include <kvm/iodev.h> |
| 27 | |
James Hogan | 48a3c4e | 2014-05-29 10:16:28 +0100 | [diff] [blame] | 28 | /* MIPS KVM register ids */ |
| 29 | #define MIPS_CP0_32(_R, _S) \ |
James Hogan | 7bd4ace | 2014-12-02 15:47:04 +0000 | [diff] [blame] | 30 | (KVM_REG_MIPS_CP0 | KVM_REG_SIZE_U32 | (8 * (_R) + (_S))) |
James Hogan | 48a3c4e | 2014-05-29 10:16:28 +0100 | [diff] [blame] | 31 | |
| 32 | #define MIPS_CP0_64(_R, _S) \ |
James Hogan | 7bd4ace | 2014-12-02 15:47:04 +0000 | [diff] [blame] | 33 | (KVM_REG_MIPS_CP0 | KVM_REG_SIZE_U64 | (8 * (_R) + (_S))) |
James Hogan | 48a3c4e | 2014-05-29 10:16:28 +0100 | [diff] [blame] | 34 | |
| 35 | #define KVM_REG_MIPS_CP0_INDEX MIPS_CP0_32(0, 0) |
| 36 | #define KVM_REG_MIPS_CP0_ENTRYLO0 MIPS_CP0_64(2, 0) |
| 37 | #define KVM_REG_MIPS_CP0_ENTRYLO1 MIPS_CP0_64(3, 0) |
| 38 | #define KVM_REG_MIPS_CP0_CONTEXT MIPS_CP0_64(4, 0) |
James Hogan | dffe042 | 2017-03-14 10:15:34 +0000 | [diff] [blame] | 39 | #define KVM_REG_MIPS_CP0_CONTEXTCONFIG MIPS_CP0_32(4, 1) |
James Hogan | 48a3c4e | 2014-05-29 10:16:28 +0100 | [diff] [blame] | 40 | #define KVM_REG_MIPS_CP0_USERLOCAL MIPS_CP0_64(4, 2) |
James Hogan | dffe042 | 2017-03-14 10:15:34 +0000 | [diff] [blame] | 41 | #define KVM_REG_MIPS_CP0_XCONTEXTCONFIG MIPS_CP0_64(4, 3) |
James Hogan | 48a3c4e | 2014-05-29 10:16:28 +0100 | [diff] [blame] | 42 | #define KVM_REG_MIPS_CP0_PAGEMASK MIPS_CP0_32(5, 0) |
| 43 | #define KVM_REG_MIPS_CP0_PAGEGRAIN MIPS_CP0_32(5, 1) |
James Hogan | 4b7de02 | 2017-03-14 10:15:35 +0000 | [diff] [blame] | 44 | #define KVM_REG_MIPS_CP0_SEGCTL0 MIPS_CP0_64(5, 2) |
| 45 | #define KVM_REG_MIPS_CP0_SEGCTL1 MIPS_CP0_64(5, 3) |
| 46 | #define KVM_REG_MIPS_CP0_SEGCTL2 MIPS_CP0_64(5, 4) |
James Hogan | 5a2f352 | 2017-03-14 10:15:36 +0000 | [diff] [blame] | 47 | #define KVM_REG_MIPS_CP0_PWBASE MIPS_CP0_64(5, 5) |
| 48 | #define KVM_REG_MIPS_CP0_PWFIELD MIPS_CP0_64(5, 6) |
| 49 | #define KVM_REG_MIPS_CP0_PWSIZE MIPS_CP0_64(5, 7) |
James Hogan | 48a3c4e | 2014-05-29 10:16:28 +0100 | [diff] [blame] | 50 | #define KVM_REG_MIPS_CP0_WIRED MIPS_CP0_32(6, 0) |
James Hogan | 5a2f352 | 2017-03-14 10:15:36 +0000 | [diff] [blame] | 51 | #define KVM_REG_MIPS_CP0_PWCTL MIPS_CP0_32(6, 6) |
James Hogan | 48a3c4e | 2014-05-29 10:16:28 +0100 | [diff] [blame] | 52 | #define KVM_REG_MIPS_CP0_HWRENA MIPS_CP0_32(7, 0) |
| 53 | #define KVM_REG_MIPS_CP0_BADVADDR MIPS_CP0_64(8, 0) |
James Hogan | edc8926 | 2017-03-14 10:15:33 +0000 | [diff] [blame] | 54 | #define KVM_REG_MIPS_CP0_BADINSTR MIPS_CP0_32(8, 1) |
| 55 | #define KVM_REG_MIPS_CP0_BADINSTRP MIPS_CP0_32(8, 2) |
James Hogan | 48a3c4e | 2014-05-29 10:16:28 +0100 | [diff] [blame] | 56 | #define KVM_REG_MIPS_CP0_COUNT MIPS_CP0_32(9, 0) |
| 57 | #define KVM_REG_MIPS_CP0_ENTRYHI MIPS_CP0_64(10, 0) |
| 58 | #define KVM_REG_MIPS_CP0_COMPARE MIPS_CP0_32(11, 0) |
| 59 | #define KVM_REG_MIPS_CP0_STATUS MIPS_CP0_32(12, 0) |
James Hogan | ad58d4d | 2015-02-02 22:55:17 +0000 | [diff] [blame] | 60 | #define KVM_REG_MIPS_CP0_INTCTL MIPS_CP0_32(12, 1) |
James Hogan | 48a3c4e | 2014-05-29 10:16:28 +0100 | [diff] [blame] | 61 | #define KVM_REG_MIPS_CP0_CAUSE MIPS_CP0_32(13, 0) |
| 62 | #define KVM_REG_MIPS_CP0_EPC MIPS_CP0_64(14, 0) |
James Hogan | 1068eaa | 2014-06-26 13:56:52 +0100 | [diff] [blame] | 63 | #define KVM_REG_MIPS_CP0_PRID MIPS_CP0_32(15, 0) |
James Hogan | 48a3c4e | 2014-05-29 10:16:28 +0100 | [diff] [blame] | 64 | #define KVM_REG_MIPS_CP0_EBASE MIPS_CP0_64(15, 1) |
| 65 | #define KVM_REG_MIPS_CP0_CONFIG MIPS_CP0_32(16, 0) |
| 66 | #define KVM_REG_MIPS_CP0_CONFIG1 MIPS_CP0_32(16, 1) |
| 67 | #define KVM_REG_MIPS_CP0_CONFIG2 MIPS_CP0_32(16, 2) |
| 68 | #define KVM_REG_MIPS_CP0_CONFIG3 MIPS_CP0_32(16, 3) |
James Hogan | c771607 | 2014-06-26 15:11:29 +0100 | [diff] [blame] | 69 | #define KVM_REG_MIPS_CP0_CONFIG4 MIPS_CP0_32(16, 4) |
| 70 | #define KVM_REG_MIPS_CP0_CONFIG5 MIPS_CP0_32(16, 5) |
Huacai Chen | 8a5097e | 2020-05-23 15:56:39 +0800 | [diff] [blame] | 71 | #define KVM_REG_MIPS_CP0_CONFIG6 MIPS_CP0_32(16, 6) |
James Hogan | 48a3c4e | 2014-05-29 10:16:28 +0100 | [diff] [blame] | 72 | #define KVM_REG_MIPS_CP0_CONFIG7 MIPS_CP0_32(16, 7) |
James Hogan | d42a008 | 2017-03-14 10:15:38 +0000 | [diff] [blame] | 73 | #define KVM_REG_MIPS_CP0_MAARI MIPS_CP0_64(17, 2) |
James Hogan | 48a3c4e | 2014-05-29 10:16:28 +0100 | [diff] [blame] | 74 | #define KVM_REG_MIPS_CP0_XCONTEXT MIPS_CP0_64(20, 0) |
Huacai Chen | 8a5097e | 2020-05-23 15:56:39 +0800 | [diff] [blame] | 75 | #define KVM_REG_MIPS_CP0_DIAG MIPS_CP0_32(22, 0) |
James Hogan | 48a3c4e | 2014-05-29 10:16:28 +0100 | [diff] [blame] | 76 | #define KVM_REG_MIPS_CP0_ERROREPC MIPS_CP0_64(30, 0) |
James Hogan | 0510870 | 2016-06-15 19:29:56 +0100 | [diff] [blame] | 77 | #define KVM_REG_MIPS_CP0_KSCRATCH1 MIPS_CP0_64(31, 2) |
| 78 | #define KVM_REG_MIPS_CP0_KSCRATCH2 MIPS_CP0_64(31, 3) |
| 79 | #define KVM_REG_MIPS_CP0_KSCRATCH3 MIPS_CP0_64(31, 4) |
| 80 | #define KVM_REG_MIPS_CP0_KSCRATCH4 MIPS_CP0_64(31, 5) |
| 81 | #define KVM_REG_MIPS_CP0_KSCRATCH5 MIPS_CP0_64(31, 6) |
| 82 | #define KVM_REG_MIPS_CP0_KSCRATCH6 MIPS_CP0_64(31, 7) |
James Hogan | 48a3c4e | 2014-05-29 10:16:28 +0100 | [diff] [blame] | 83 | |
Sanjay Lal | 740765c | 2012-11-21 18:34:00 -0800 | [diff] [blame] | 84 | |
Huacai Chen | 210b4b9 | 2020-05-23 15:56:30 +0800 | [diff] [blame] | 85 | #define KVM_MAX_VCPUS 16 |
Sanjay Lal | 740765c | 2012-11-21 18:34:00 -0800 | [diff] [blame] | 86 | /* memory slots that does not exposed to userspace */ |
James Hogan | caa1faa | 2015-12-16 23:49:26 +0000 | [diff] [blame] | 87 | #define KVM_PRIVATE_MEM_SLOTS 0 |
Sanjay Lal | 740765c | 2012-11-21 18:34:00 -0800 | [diff] [blame] | 88 | |
David Hildenbrand | 920552b | 2015-09-18 12:34:53 +0200 | [diff] [blame] | 89 | #define KVM_HALT_POLL_NS_DEFAULT 500000 |
Sanjay Lal | 740765c | 2012-11-21 18:34:00 -0800 | [diff] [blame] | 90 | |
James Hogan | c992a4f | 2017-03-14 10:15:31 +0000 | [diff] [blame] | 91 | extern unsigned long GUESTID_MASK; |
| 92 | extern unsigned long GUESTID_FIRST_VERSION; |
| 93 | extern unsigned long GUESTID_VERSION_MASK; |
Sanjay Lal | 740765c | 2012-11-21 18:34:00 -0800 | [diff] [blame] | 94 | |
James Hogan | 2202794 | 2014-03-14 13:06:08 +0000 | [diff] [blame] | 95 | #define KVM_INVALID_ADDR 0xdeadbeef |
Sanjay Lal | 740765c | 2012-11-21 18:34:00 -0800 | [diff] [blame] | 96 | |
James Hogan | f6f7017 | 2016-08-01 09:07:52 +0100 | [diff] [blame] | 97 | /* |
| 98 | * EVA has overlapping user & kernel address spaces, so user VAs may be > |
| 99 | * PAGE_OFFSET. For this reason we can't use the default KVM_HVA_ERR_BAD of |
| 100 | * PAGE_OFFSET. |
| 101 | */ |
| 102 | |
| 103 | #define KVM_HVA_ERR_BAD (-1UL) |
| 104 | #define KVM_HVA_ERR_RO_BAD (-2UL) |
| 105 | |
| 106 | static inline bool kvm_is_error_hva(unsigned long addr) |
| 107 | { |
| 108 | return IS_ERR_VALUE(addr); |
| 109 | } |
| 110 | |
Sanjay Lal | 740765c | 2012-11-21 18:34:00 -0800 | [diff] [blame] | 111 | struct kvm_vm_stat { |
Jing Zhang | 0193cc9 | 2021-06-18 22:27:03 +0000 | [diff] [blame] | 112 | struct kvm_vm_stat_generic generic; |
Sanjay Lal | 740765c | 2012-11-21 18:34:00 -0800 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | struct kvm_vcpu_stat { |
Jing Zhang | 0193cc9 | 2021-06-18 22:27:03 +0000 | [diff] [blame] | 116 | struct kvm_vcpu_stat_generic generic; |
Suraj Jitindar Singh | 8a7e75d | 2016-08-02 14:03:22 +1000 | [diff] [blame] | 117 | u64 wait_exits; |
| 118 | u64 cache_exits; |
| 119 | u64 signal_exits; |
| 120 | u64 int_exits; |
| 121 | u64 cop_unusable_exits; |
| 122 | u64 tlbmod_exits; |
| 123 | u64 tlbmiss_ld_exits; |
| 124 | u64 tlbmiss_st_exits; |
| 125 | u64 addrerr_st_exits; |
| 126 | u64 addrerr_ld_exits; |
| 127 | u64 syscall_exits; |
| 128 | u64 resvd_inst_exits; |
| 129 | u64 break_inst_exits; |
| 130 | u64 trap_inst_exits; |
| 131 | u64 msa_fpe_exits; |
| 132 | u64 fpe_exits; |
| 133 | u64 msa_disabled_exits; |
| 134 | u64 flush_dcache_exits; |
James Hogan | a724492 | 2017-03-14 10:15:18 +0000 | [diff] [blame] | 135 | u64 vz_gpsi_exits; |
| 136 | u64 vz_gsfc_exits; |
| 137 | u64 vz_hc_exits; |
| 138 | u64 vz_grr_exits; |
| 139 | u64 vz_gva_exits; |
| 140 | u64 vz_ghfc_exits; |
| 141 | u64 vz_gpa_exits; |
| 142 | u64 vz_resvd_exits; |
Huacai Chen | 7f2a83f | 2020-05-23 15:56:38 +0800 | [diff] [blame] | 143 | #ifdef CONFIG_CPU_LOONGSON64 |
| 144 | u64 vz_cpucfg_exits; |
| 145 | #endif |
Sanjay Lal | 740765c | 2012-11-21 18:34:00 -0800 | [diff] [blame] | 146 | }; |
| 147 | |
Sanjay Lal | 740765c | 2012-11-21 18:34:00 -0800 | [diff] [blame] | 148 | struct kvm_arch_memory_slot { |
| 149 | }; |
| 150 | |
Huacai Chen | f21db30 | 2020-05-23 15:56:37 +0800 | [diff] [blame] | 151 | #ifdef CONFIG_CPU_LOONGSON64 |
| 152 | struct ipi_state { |
| 153 | uint32_t status; |
| 154 | uint32_t en; |
| 155 | uint32_t set; |
| 156 | uint32_t clear; |
| 157 | uint64_t buf[4]; |
| 158 | }; |
| 159 | |
| 160 | struct loongson_kvm_ipi; |
| 161 | |
| 162 | struct ipi_io_device { |
| 163 | int node_id; |
| 164 | struct loongson_kvm_ipi *ipi; |
| 165 | struct kvm_io_device device; |
| 166 | }; |
| 167 | |
| 168 | struct loongson_kvm_ipi { |
| 169 | spinlock_t lock; |
| 170 | struct kvm *kvm; |
| 171 | struct ipi_state ipistate[16]; |
| 172 | struct ipi_io_device dev_ipi[4]; |
| 173 | }; |
| 174 | #endif |
| 175 | |
Sanjay Lal | 740765c | 2012-11-21 18:34:00 -0800 | [diff] [blame] | 176 | struct kvm_arch { |
James Hogan | 06c158c | 2015-05-01 13:50:18 +0100 | [diff] [blame] | 177 | /* Guest physical mm */ |
| 178 | struct mm_struct gpa_mm; |
James Hogan | c992a4f | 2017-03-14 10:15:31 +0000 | [diff] [blame] | 179 | /* Mask of CPUs needing GPA ASID flush */ |
| 180 | cpumask_t asid_flush_mask; |
Huacai Chen | f21db30 | 2020-05-23 15:56:37 +0800 | [diff] [blame] | 181 | #ifdef CONFIG_CPU_LOONGSON64 |
| 182 | struct loongson_kvm_ipi ipi; |
| 183 | #endif |
Sanjay Lal | 740765c | 2012-11-21 18:34:00 -0800 | [diff] [blame] | 184 | }; |
| 185 | |
James Hogan | 2202794 | 2014-03-14 13:06:08 +0000 | [diff] [blame] | 186 | #define N_MIPS_COPROC_REGS 32 |
| 187 | #define N_MIPS_COPROC_SEL 8 |
Sanjay Lal | 740765c | 2012-11-21 18:34:00 -0800 | [diff] [blame] | 188 | |
| 189 | struct mips_coproc { |
| 190 | unsigned long reg[N_MIPS_COPROC_REGS][N_MIPS_COPROC_SEL]; |
| 191 | #ifdef CONFIG_KVM_MIPS_DEBUG_COP0_COUNTERS |
| 192 | unsigned long stat[N_MIPS_COPROC_REGS][N_MIPS_COPROC_SEL]; |
| 193 | #endif |
| 194 | }; |
| 195 | |
| 196 | /* |
| 197 | * Coprocessor 0 register names |
| 198 | */ |
James Hogan | 2202794 | 2014-03-14 13:06:08 +0000 | [diff] [blame] | 199 | #define MIPS_CP0_TLB_INDEX 0 |
| 200 | #define MIPS_CP0_TLB_RANDOM 1 |
| 201 | #define MIPS_CP0_TLB_LOW 2 |
| 202 | #define MIPS_CP0_TLB_LO0 2 |
| 203 | #define MIPS_CP0_TLB_LO1 3 |
| 204 | #define MIPS_CP0_TLB_CONTEXT 4 |
| 205 | #define MIPS_CP0_TLB_PG_MASK 5 |
| 206 | #define MIPS_CP0_TLB_WIRED 6 |
| 207 | #define MIPS_CP0_HWRENA 7 |
| 208 | #define MIPS_CP0_BAD_VADDR 8 |
| 209 | #define MIPS_CP0_COUNT 9 |
| 210 | #define MIPS_CP0_TLB_HI 10 |
| 211 | #define MIPS_CP0_COMPARE 11 |
| 212 | #define MIPS_CP0_STATUS 12 |
| 213 | #define MIPS_CP0_CAUSE 13 |
| 214 | #define MIPS_CP0_EXC_PC 14 |
| 215 | #define MIPS_CP0_PRID 15 |
| 216 | #define MIPS_CP0_CONFIG 16 |
| 217 | #define MIPS_CP0_LLADDR 17 |
| 218 | #define MIPS_CP0_WATCH_LO 18 |
| 219 | #define MIPS_CP0_WATCH_HI 19 |
| 220 | #define MIPS_CP0_TLB_XCONTEXT 20 |
Huacai Chen | 8a5097e | 2020-05-23 15:56:39 +0800 | [diff] [blame] | 221 | #define MIPS_CP0_DIAG 22 |
James Hogan | 2202794 | 2014-03-14 13:06:08 +0000 | [diff] [blame] | 222 | #define MIPS_CP0_ECC 26 |
| 223 | #define MIPS_CP0_CACHE_ERR 27 |
| 224 | #define MIPS_CP0_TAG_LO 28 |
| 225 | #define MIPS_CP0_TAG_HI 29 |
| 226 | #define MIPS_CP0_ERROR_PC 30 |
| 227 | #define MIPS_CP0_DEBUG 23 |
| 228 | #define MIPS_CP0_DEPC 24 |
| 229 | #define MIPS_CP0_PERFCNT 25 |
| 230 | #define MIPS_CP0_ERRCTL 26 |
| 231 | #define MIPS_CP0_DATA_LO 28 |
| 232 | #define MIPS_CP0_DATA_HI 29 |
| 233 | #define MIPS_CP0_DESAVE 31 |
Sanjay Lal | 740765c | 2012-11-21 18:34:00 -0800 | [diff] [blame] | 234 | |
James Hogan | 2202794 | 2014-03-14 13:06:08 +0000 | [diff] [blame] | 235 | #define MIPS_CP0_CONFIG_SEL 0 |
| 236 | #define MIPS_CP0_CONFIG1_SEL 1 |
| 237 | #define MIPS_CP0_CONFIG2_SEL 2 |
| 238 | #define MIPS_CP0_CONFIG3_SEL 3 |
James Hogan | c771607 | 2014-06-26 15:11:29 +0100 | [diff] [blame] | 239 | #define MIPS_CP0_CONFIG4_SEL 4 |
| 240 | #define MIPS_CP0_CONFIG5_SEL 5 |
Sanjay Lal | 740765c | 2012-11-21 18:34:00 -0800 | [diff] [blame] | 241 | |
James Hogan | c992a4f | 2017-03-14 10:15:31 +0000 | [diff] [blame] | 242 | #define MIPS_CP0_GUESTCTL2 10 |
| 243 | #define MIPS_CP0_GUESTCTL2_SEL 5 |
| 244 | #define MIPS_CP0_GTOFFSET 12 |
| 245 | #define MIPS_CP0_GTOFFSET_SEL 7 |
| 246 | |
Sanjay Lal | 740765c | 2012-11-21 18:34:00 -0800 | [diff] [blame] | 247 | /* Resume Flags */ |
James Hogan | 2202794 | 2014-03-14 13:06:08 +0000 | [diff] [blame] | 248 | #define RESUME_FLAG_DR (1<<0) /* Reload guest nonvolatile state? */ |
| 249 | #define RESUME_FLAG_HOST (1<<1) /* Resume host? */ |
Sanjay Lal | 740765c | 2012-11-21 18:34:00 -0800 | [diff] [blame] | 250 | |
James Hogan | 2202794 | 2014-03-14 13:06:08 +0000 | [diff] [blame] | 251 | #define RESUME_GUEST 0 |
| 252 | #define RESUME_GUEST_DR RESUME_FLAG_DR |
| 253 | #define RESUME_HOST RESUME_FLAG_HOST |
Sanjay Lal | 740765c | 2012-11-21 18:34:00 -0800 | [diff] [blame] | 254 | |
| 255 | enum emulation_result { |
| 256 | EMULATE_DONE, /* no further processing */ |
| 257 | EMULATE_DO_MMIO, /* kvm_run filled with MMIO request */ |
| 258 | EMULATE_FAIL, /* can't emulate this instruction */ |
| 259 | EMULATE_WAIT, /* WAIT instruction */ |
| 260 | EMULATE_PRIV_FAIL, |
James Hogan | 4cf74c9 | 2016-11-26 00:37:28 +0000 | [diff] [blame] | 261 | EMULATE_EXCEPT, /* A guest exception has been generated */ |
James Hogan | 955d8dc | 2017-03-14 10:15:14 +0000 | [diff] [blame] | 262 | EMULATE_HYPERCALL, /* HYPCALL instruction */ |
Sanjay Lal | 740765c | 2012-11-21 18:34:00 -0800 | [diff] [blame] | 263 | }; |
| 264 | |
Xing Li | 5816c76 | 2020-05-23 15:56:29 +0800 | [diff] [blame] | 265 | #if defined(CONFIG_64BIT) |
| 266 | #define VPN2_MASK GENMASK(cpu_vmbits - 1, 13) |
| 267 | #else |
James Hogan | 2202794 | 2014-03-14 13:06:08 +0000 | [diff] [blame] | 268 | #define VPN2_MASK 0xffffe000 |
Xing Li | 5816c76 | 2020-05-23 15:56:29 +0800 | [diff] [blame] | 269 | #endif |
Xing Li | fe2b73d | 2020-05-23 15:56:28 +0800 | [diff] [blame] | 270 | #define KVM_ENTRYHI_ASID cpu_asid_mask(&boot_cpu_data) |
James Hogan | e6207bb | 2016-06-09 14:19:19 +0100 | [diff] [blame] | 271 | #define TLB_IS_GLOBAL(x) ((x).tlb_lo[0] & (x).tlb_lo[1] & ENTRYLO_G) |
James Hogan | 2202794 | 2014-03-14 13:06:08 +0000 | [diff] [blame] | 272 | #define TLB_VPN2(x) ((x).tlb_hi & VPN2_MASK) |
Paul Burton | ca64c2b | 2016-05-06 14:36:20 +0100 | [diff] [blame] | 273 | #define TLB_ASID(x) ((x).tlb_hi & KVM_ENTRYHI_ASID) |
James Hogan | 19d194c | 2016-06-09 14:19:18 +0100 | [diff] [blame] | 274 | #define TLB_LO_IDX(x, va) (((va) >> PAGE_SHIFT) & 1) |
James Hogan | e6207bb | 2016-06-09 14:19:19 +0100 | [diff] [blame] | 275 | #define TLB_IS_VALID(x, va) ((x).tlb_lo[TLB_LO_IDX(x, va)] & ENTRYLO_V) |
James Hogan | 1880afd | 2016-11-28 23:04:52 +0000 | [diff] [blame] | 276 | #define TLB_IS_DIRTY(x, va) ((x).tlb_lo[TLB_LO_IDX(x, va)] & ENTRYLO_D) |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 277 | #define TLB_HI_VPN2_HIT(x, y) ((TLB_VPN2(x) & ~(x).tlb_mask) == \ |
| 278 | ((y) & VPN2_MASK & ~(x).tlb_mask)) |
| 279 | #define TLB_HI_ASID_HIT(x, y) (TLB_IS_GLOBAL(x) || \ |
Paul Burton | ca64c2b | 2016-05-06 14:36:20 +0100 | [diff] [blame] | 280 | TLB_ASID(x) == ((y) & KVM_ENTRYHI_ASID)) |
Sanjay Lal | 740765c | 2012-11-21 18:34:00 -0800 | [diff] [blame] | 281 | |
| 282 | struct kvm_mips_tlb { |
| 283 | long tlb_mask; |
| 284 | long tlb_hi; |
James Hogan | 9fbfb06 | 2016-06-09 14:19:17 +0100 | [diff] [blame] | 285 | long tlb_lo[2]; |
Sanjay Lal | 740765c | 2012-11-21 18:34:00 -0800 | [diff] [blame] | 286 | }; |
| 287 | |
James Hogan | f943176 | 2016-06-14 09:40:10 +0100 | [diff] [blame] | 288 | #define KVM_MIPS_AUX_FPU 0x1 |
| 289 | #define KVM_MIPS_AUX_MSA 0x2 |
James Hogan | 98e91b8 | 2014-11-18 14:09:12 +0000 | [diff] [blame] | 290 | |
Sanjay Lal | 740765c | 2012-11-21 18:34:00 -0800 | [diff] [blame] | 291 | struct kvm_vcpu_arch { |
James Hogan | 878edf0 | 2016-06-09 14:19:14 +0100 | [diff] [blame] | 292 | void *guest_ebase; |
Tianjia Zhang | 0b7aa58 | 2020-06-23 21:14:18 +0800 | [diff] [blame] | 293 | int (*vcpu_run)(struct kvm_vcpu *vcpu); |
James Hogan | 1934a3a | 2017-03-14 10:15:26 +0000 | [diff] [blame] | 294 | |
| 295 | /* Host registers preserved across guest mode execution */ |
Sanjay Lal | 740765c | 2012-11-21 18:34:00 -0800 | [diff] [blame] | 296 | unsigned long host_stack; |
| 297 | unsigned long host_gp; |
James Hogan | 1934a3a | 2017-03-14 10:15:26 +0000 | [diff] [blame] | 298 | unsigned long host_pgd; |
| 299 | unsigned long host_entryhi; |
Sanjay Lal | 740765c | 2012-11-21 18:34:00 -0800 | [diff] [blame] | 300 | |
| 301 | /* Host CP0 registers used when handling exits from guest */ |
| 302 | unsigned long host_cp0_badvaddr; |
Sanjay Lal | 740765c | 2012-11-21 18:34:00 -0800 | [diff] [blame] | 303 | unsigned long host_cp0_epc; |
James Hogan | 31cf749 | 2016-06-09 14:19:09 +0100 | [diff] [blame] | 304 | u32 host_cp0_cause; |
James Hogan | 1934a3a | 2017-03-14 10:15:26 +0000 | [diff] [blame] | 305 | u32 host_cp0_guestctl0; |
James Hogan | 6a97c77 | 2015-04-23 16:54:35 +0100 | [diff] [blame] | 306 | u32 host_cp0_badinstr; |
| 307 | u32 host_cp0_badinstrp; |
Sanjay Lal | 740765c | 2012-11-21 18:34:00 -0800 | [diff] [blame] | 308 | |
| 309 | /* GPRS */ |
| 310 | unsigned long gprs[32]; |
| 311 | unsigned long hi; |
| 312 | unsigned long lo; |
| 313 | unsigned long pc; |
| 314 | |
| 315 | /* FPU State */ |
| 316 | struct mips_fpu_struct fpu; |
James Hogan | f943176 | 2016-06-14 09:40:10 +0100 | [diff] [blame] | 317 | /* Which auxiliary state is loaded (KVM_MIPS_AUX_*) */ |
| 318 | unsigned int aux_inuse; |
Sanjay Lal | 740765c | 2012-11-21 18:34:00 -0800 | [diff] [blame] | 319 | |
| 320 | /* COP0 State */ |
| 321 | struct mips_coproc *cop0; |
| 322 | |
James Hogan | e1e575f6 | 2016-10-25 16:11:12 +0100 | [diff] [blame] | 323 | /* Resume PC after MMIO completion */ |
| 324 | unsigned long io_pc; |
| 325 | /* GPR used as IO source/target */ |
| 326 | u32 io_gpr; |
Sanjay Lal | 740765c | 2012-11-21 18:34:00 -0800 | [diff] [blame] | 327 | |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 328 | struct hrtimer comparecount_timer; |
James Hogan | f823934 | 2014-05-29 10:16:37 +0100 | [diff] [blame] | 329 | /* Count timer control KVM register */ |
James Hogan | bdb7ed8 | 2016-06-09 14:19:07 +0100 | [diff] [blame] | 330 | u32 count_ctl; |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 331 | /* Count bias from the raw time */ |
James Hogan | bdb7ed8 | 2016-06-09 14:19:07 +0100 | [diff] [blame] | 332 | u32 count_bias; |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 333 | /* Frequency of timer in Hz */ |
James Hogan | bdb7ed8 | 2016-06-09 14:19:07 +0100 | [diff] [blame] | 334 | u32 count_hz; |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 335 | /* Dynamic nanosecond bias (multiple of count_period) to avoid overflow */ |
| 336 | s64 count_dyn_bias; |
James Hogan | f823934 | 2014-05-29 10:16:37 +0100 | [diff] [blame] | 337 | /* Resume time */ |
| 338 | ktime_t count_resume; |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 339 | /* Period of timer tick in ns */ |
| 340 | u64 count_period; |
Sanjay Lal | 740765c | 2012-11-21 18:34:00 -0800 | [diff] [blame] | 341 | |
| 342 | /* Bitmask of exceptions that are pending */ |
| 343 | unsigned long pending_exceptions; |
| 344 | |
| 345 | /* Bitmask of pending exceptions to be cleared */ |
| 346 | unsigned long pending_exceptions_clr; |
| 347 | |
James Hogan | aba8592 | 2016-12-16 15:57:00 +0000 | [diff] [blame] | 348 | /* Cache some mmu pages needed inside spinlock regions */ |
| 349 | struct kvm_mmu_memory_cache mmu_page_cache; |
| 350 | |
James Hogan | c992a4f | 2017-03-14 10:15:31 +0000 | [diff] [blame] | 351 | /* vcpu's vzguestid is different on each host cpu in an smp system */ |
| 352 | u32 vzguestid[NR_CPUS]; |
| 353 | |
| 354 | /* wired guest TLB entries */ |
| 355 | struct kvm_mips_tlb *wired_tlb; |
| 356 | unsigned int wired_tlb_limit; |
| 357 | unsigned int wired_tlb_used; |
James Hogan | d42a008 | 2017-03-14 10:15:38 +0000 | [diff] [blame] | 358 | |
| 359 | /* emulated guest MAAR registers */ |
| 360 | unsigned long maar[6]; |
James Hogan | c992a4f | 2017-03-14 10:15:31 +0000 | [diff] [blame] | 361 | |
| 362 | /* Last CPU the VCPU state was loaded on */ |
Sanjay Lal | 740765c | 2012-11-21 18:34:00 -0800 | [diff] [blame] | 363 | int last_sched_cpu; |
James Hogan | c992a4f | 2017-03-14 10:15:31 +0000 | [diff] [blame] | 364 | /* Last CPU the VCPU actually executed guest code on */ |
| 365 | int last_exec_cpu; |
Sanjay Lal | 740765c | 2012-11-21 18:34:00 -0800 | [diff] [blame] | 366 | |
| 367 | /* WAIT executed */ |
| 368 | int wait; |
James Hogan | 98e91b8 | 2014-11-18 14:09:12 +0000 | [diff] [blame] | 369 | |
| 370 | u8 fpu_enabled; |
James Hogan | 539cb89fb | 2015-03-05 11:43:36 +0000 | [diff] [blame] | 371 | u8 msa_enabled; |
Sanjay Lal | 740765c | 2012-11-21 18:34:00 -0800 | [diff] [blame] | 372 | }; |
| 373 | |
James Hogan | c73c99b | 2014-05-29 10:16:33 +0100 | [diff] [blame] | 374 | static inline void _kvm_atomic_set_c0_guest_reg(unsigned long *reg, |
| 375 | unsigned long val) |
| 376 | { |
| 377 | unsigned long temp; |
| 378 | do { |
| 379 | __asm__ __volatile__( |
Paul Burton | 378ed6f | 2018-11-08 20:14:38 +0000 | [diff] [blame] | 380 | " .set push \n" |
James Hogan | d85ebff | 2016-07-04 19:35:10 +0100 | [diff] [blame] | 381 | " .set "MIPS_ISA_ARCH_LEVEL" \n" |
James Hogan | c73c99b | 2014-05-29 10:16:33 +0100 | [diff] [blame] | 382 | " " __LL "%0, %1 \n" |
| 383 | " or %0, %2 \n" |
| 384 | " " __SC "%0, %1 \n" |
Paul Burton | 378ed6f | 2018-11-08 20:14:38 +0000 | [diff] [blame] | 385 | " .set pop \n" |
James Hogan | c73c99b | 2014-05-29 10:16:33 +0100 | [diff] [blame] | 386 | : "=&r" (temp), "+m" (*reg) |
| 387 | : "r" (val)); |
| 388 | } while (unlikely(!temp)); |
| 389 | } |
| 390 | |
| 391 | static inline void _kvm_atomic_clear_c0_guest_reg(unsigned long *reg, |
| 392 | unsigned long val) |
| 393 | { |
| 394 | unsigned long temp; |
| 395 | do { |
| 396 | __asm__ __volatile__( |
Paul Burton | 378ed6f | 2018-11-08 20:14:38 +0000 | [diff] [blame] | 397 | " .set push \n" |
James Hogan | d85ebff | 2016-07-04 19:35:10 +0100 | [diff] [blame] | 398 | " .set "MIPS_ISA_ARCH_LEVEL" \n" |
James Hogan | c73c99b | 2014-05-29 10:16:33 +0100 | [diff] [blame] | 399 | " " __LL "%0, %1 \n" |
| 400 | " and %0, %2 \n" |
| 401 | " " __SC "%0, %1 \n" |
Paul Burton | 378ed6f | 2018-11-08 20:14:38 +0000 | [diff] [blame] | 402 | " .set pop \n" |
James Hogan | c73c99b | 2014-05-29 10:16:33 +0100 | [diff] [blame] | 403 | : "=&r" (temp), "+m" (*reg) |
| 404 | : "r" (~val)); |
| 405 | } while (unlikely(!temp)); |
| 406 | } |
| 407 | |
| 408 | static inline void _kvm_atomic_change_c0_guest_reg(unsigned long *reg, |
| 409 | unsigned long change, |
| 410 | unsigned long val) |
| 411 | { |
| 412 | unsigned long temp; |
| 413 | do { |
| 414 | __asm__ __volatile__( |
Paul Burton | 378ed6f | 2018-11-08 20:14:38 +0000 | [diff] [blame] | 415 | " .set push \n" |
James Hogan | d85ebff | 2016-07-04 19:35:10 +0100 | [diff] [blame] | 416 | " .set "MIPS_ISA_ARCH_LEVEL" \n" |
James Hogan | c73c99b | 2014-05-29 10:16:33 +0100 | [diff] [blame] | 417 | " " __LL "%0, %1 \n" |
| 418 | " and %0, %2 \n" |
| 419 | " or %0, %3 \n" |
| 420 | " " __SC "%0, %1 \n" |
Paul Burton | 378ed6f | 2018-11-08 20:14:38 +0000 | [diff] [blame] | 421 | " .set pop \n" |
James Hogan | c73c99b | 2014-05-29 10:16:33 +0100 | [diff] [blame] | 422 | : "=&r" (temp), "+m" (*reg) |
| 423 | : "r" (~change), "r" (val & change)); |
| 424 | } while (unlikely(!temp)); |
| 425 | } |
| 426 | |
James Hogan | a27660f | 2017-03-14 10:15:25 +0000 | [diff] [blame] | 427 | /* Guest register types, used in accessor build below */ |
| 428 | #define __KVMT32 u32 |
| 429 | #define __KVMTl unsigned long |
James Hogan | c73c99b | 2014-05-29 10:16:33 +0100 | [diff] [blame] | 430 | |
James Hogan | a27660f | 2017-03-14 10:15:25 +0000 | [diff] [blame] | 431 | /* |
| 432 | * __BUILD_KVM_$ops_SAVED(): kvm_$op_sw_gc0_$reg() |
| 433 | * These operate on the saved guest C0 state in RAM. |
| 434 | */ |
James Hogan | c73c99b | 2014-05-29 10:16:33 +0100 | [diff] [blame] | 435 | |
James Hogan | a27660f | 2017-03-14 10:15:25 +0000 | [diff] [blame] | 436 | /* Generate saved context simple accessors */ |
| 437 | #define __BUILD_KVM_RW_SAVED(name, type, _reg, sel) \ |
| 438 | static inline __KVMT##type kvm_read_sw_gc0_##name(struct mips_coproc *cop0) \ |
James Hogan | 2202794 | 2014-03-14 13:06:08 +0000 | [diff] [blame] | 439 | { \ |
James Hogan | a27660f | 2017-03-14 10:15:25 +0000 | [diff] [blame] | 440 | return cop0->reg[(_reg)][(sel)]; \ |
| 441 | } \ |
| 442 | static inline void kvm_write_sw_gc0_##name(struct mips_coproc *cop0, \ |
| 443 | __KVMT##type val) \ |
| 444 | { \ |
| 445 | cop0->reg[(_reg)][(sel)] = val; \ |
Sanjay Lal | 740765c | 2012-11-21 18:34:00 -0800 | [diff] [blame] | 446 | } |
| 447 | |
James Hogan | a27660f | 2017-03-14 10:15:25 +0000 | [diff] [blame] | 448 | /* Generate saved context bitwise modifiers */ |
| 449 | #define __BUILD_KVM_SET_SAVED(name, type, _reg, sel) \ |
| 450 | static inline void kvm_set_sw_gc0_##name(struct mips_coproc *cop0, \ |
| 451 | __KVMT##type val) \ |
| 452 | { \ |
| 453 | cop0->reg[(_reg)][(sel)] |= val; \ |
| 454 | } \ |
| 455 | static inline void kvm_clear_sw_gc0_##name(struct mips_coproc *cop0, \ |
| 456 | __KVMT##type val) \ |
| 457 | { \ |
| 458 | cop0->reg[(_reg)][(sel)] &= ~val; \ |
| 459 | } \ |
| 460 | static inline void kvm_change_sw_gc0_##name(struct mips_coproc *cop0, \ |
| 461 | __KVMT##type mask, \ |
| 462 | __KVMT##type val) \ |
| 463 | { \ |
| 464 | unsigned long _mask = mask; \ |
| 465 | cop0->reg[(_reg)][(sel)] &= ~_mask; \ |
| 466 | cop0->reg[(_reg)][(sel)] |= val & _mask; \ |
| 467 | } |
| 468 | |
| 469 | /* Generate saved context atomic bitwise modifiers */ |
| 470 | #define __BUILD_KVM_ATOMIC_SAVED(name, type, _reg, sel) \ |
| 471 | static inline void kvm_set_sw_gc0_##name(struct mips_coproc *cop0, \ |
| 472 | __KVMT##type val) \ |
| 473 | { \ |
| 474 | _kvm_atomic_set_c0_guest_reg(&cop0->reg[(_reg)][(sel)], val); \ |
| 475 | } \ |
| 476 | static inline void kvm_clear_sw_gc0_##name(struct mips_coproc *cop0, \ |
| 477 | __KVMT##type val) \ |
| 478 | { \ |
| 479 | _kvm_atomic_clear_c0_guest_reg(&cop0->reg[(_reg)][(sel)], val); \ |
| 480 | } \ |
| 481 | static inline void kvm_change_sw_gc0_##name(struct mips_coproc *cop0, \ |
| 482 | __KVMT##type mask, \ |
| 483 | __KVMT##type val) \ |
| 484 | { \ |
| 485 | _kvm_atomic_change_c0_guest_reg(&cop0->reg[(_reg)][(sel)], mask, \ |
| 486 | val); \ |
| 487 | } |
| 488 | |
| 489 | /* |
| 490 | * __BUILD_KVM_$ops_VZ(): kvm_$op_vz_gc0_$reg() |
| 491 | * These operate on the VZ guest C0 context in hardware. |
| 492 | */ |
| 493 | |
| 494 | /* Generate VZ guest context simple accessors */ |
| 495 | #define __BUILD_KVM_RW_VZ(name, type, _reg, sel) \ |
| 496 | static inline __KVMT##type kvm_read_vz_gc0_##name(struct mips_coproc *cop0) \ |
| 497 | { \ |
| 498 | return read_gc0_##name(); \ |
| 499 | } \ |
| 500 | static inline void kvm_write_vz_gc0_##name(struct mips_coproc *cop0, \ |
| 501 | __KVMT##type val) \ |
| 502 | { \ |
| 503 | write_gc0_##name(val); \ |
| 504 | } |
| 505 | |
| 506 | /* Generate VZ guest context bitwise modifiers */ |
| 507 | #define __BUILD_KVM_SET_VZ(name, type, _reg, sel) \ |
| 508 | static inline void kvm_set_vz_gc0_##name(struct mips_coproc *cop0, \ |
| 509 | __KVMT##type val) \ |
| 510 | { \ |
| 511 | set_gc0_##name(val); \ |
| 512 | } \ |
| 513 | static inline void kvm_clear_vz_gc0_##name(struct mips_coproc *cop0, \ |
| 514 | __KVMT##type val) \ |
| 515 | { \ |
| 516 | clear_gc0_##name(val); \ |
| 517 | } \ |
| 518 | static inline void kvm_change_vz_gc0_##name(struct mips_coproc *cop0, \ |
| 519 | __KVMT##type mask, \ |
| 520 | __KVMT##type val) \ |
| 521 | { \ |
| 522 | change_gc0_##name(mask, val); \ |
| 523 | } |
| 524 | |
| 525 | /* Generate VZ guest context save/restore to/from saved context */ |
| 526 | #define __BUILD_KVM_SAVE_VZ(name, _reg, sel) \ |
| 527 | static inline void kvm_restore_gc0_##name(struct mips_coproc *cop0) \ |
| 528 | { \ |
| 529 | write_gc0_##name(cop0->reg[(_reg)][(sel)]); \ |
| 530 | } \ |
| 531 | static inline void kvm_save_gc0_##name(struct mips_coproc *cop0) \ |
| 532 | { \ |
| 533 | cop0->reg[(_reg)][(sel)] = read_gc0_##name(); \ |
| 534 | } |
| 535 | |
| 536 | /* |
| 537 | * __BUILD_KVM_$ops_WRAP(): kvm_$op_$name1() -> kvm_$op_$name2() |
| 538 | * These wrap a set of operations to provide them with a different name. |
| 539 | */ |
| 540 | |
| 541 | /* Generate simple accessor wrapper */ |
| 542 | #define __BUILD_KVM_RW_WRAP(name1, name2, type) \ |
| 543 | static inline __KVMT##type kvm_read_##name1(struct mips_coproc *cop0) \ |
| 544 | { \ |
| 545 | return kvm_read_##name2(cop0); \ |
| 546 | } \ |
| 547 | static inline void kvm_write_##name1(struct mips_coproc *cop0, \ |
| 548 | __KVMT##type val) \ |
| 549 | { \ |
| 550 | kvm_write_##name2(cop0, val); \ |
| 551 | } |
| 552 | |
| 553 | /* Generate bitwise modifier wrapper */ |
| 554 | #define __BUILD_KVM_SET_WRAP(name1, name2, type) \ |
| 555 | static inline void kvm_set_##name1(struct mips_coproc *cop0, \ |
| 556 | __KVMT##type val) \ |
| 557 | { \ |
| 558 | kvm_set_##name2(cop0, val); \ |
| 559 | } \ |
| 560 | static inline void kvm_clear_##name1(struct mips_coproc *cop0, \ |
| 561 | __KVMT##type val) \ |
| 562 | { \ |
| 563 | kvm_clear_##name2(cop0, val); \ |
| 564 | } \ |
| 565 | static inline void kvm_change_##name1(struct mips_coproc *cop0, \ |
| 566 | __KVMT##type mask, \ |
| 567 | __KVMT##type val) \ |
| 568 | { \ |
| 569 | kvm_change_##name2(cop0, mask, val); \ |
| 570 | } |
| 571 | |
| 572 | /* |
| 573 | * __BUILD_KVM_$ops_SW(): kvm_$op_c0_guest_$reg() -> kvm_$op_sw_gc0_$reg() |
| 574 | * These generate accessors operating on the saved context in RAM, and wrap them |
| 575 | * with the common guest C0 accessors (for use by common emulation code). |
| 576 | */ |
| 577 | |
| 578 | #define __BUILD_KVM_RW_SW(name, type, _reg, sel) \ |
| 579 | __BUILD_KVM_RW_SAVED(name, type, _reg, sel) \ |
| 580 | __BUILD_KVM_RW_WRAP(c0_guest_##name, sw_gc0_##name, type) |
| 581 | |
| 582 | #define __BUILD_KVM_SET_SW(name, type, _reg, sel) \ |
| 583 | __BUILD_KVM_SET_SAVED(name, type, _reg, sel) \ |
| 584 | __BUILD_KVM_SET_WRAP(c0_guest_##name, sw_gc0_##name, type) |
| 585 | |
| 586 | #define __BUILD_KVM_ATOMIC_SW(name, type, _reg, sel) \ |
| 587 | __BUILD_KVM_ATOMIC_SAVED(name, type, _reg, sel) \ |
| 588 | __BUILD_KVM_SET_WRAP(c0_guest_##name, sw_gc0_##name, type) |
| 589 | |
James Hogan | a27660f | 2017-03-14 10:15:25 +0000 | [diff] [blame] | 590 | /* |
| 591 | * VZ (hardware assisted virtualisation) |
| 592 | * These macros use the active guest state in VZ mode (hardware registers), |
| 593 | */ |
| 594 | |
| 595 | /* |
| 596 | * __BUILD_KVM_$ops_HW(): kvm_$op_c0_guest_$reg() -> kvm_$op_vz_gc0_$reg() |
| 597 | * These generate accessors operating on the VZ guest context in hardware, and |
| 598 | * wrap them with the common guest C0 accessors (for use by common emulation |
| 599 | * code). |
| 600 | * |
| 601 | * Accessors operating on the saved context in RAM are also generated to allow |
| 602 | * convenient explicit saving and restoring of the state. |
| 603 | */ |
| 604 | |
| 605 | #define __BUILD_KVM_RW_HW(name, type, _reg, sel) \ |
| 606 | __BUILD_KVM_RW_SAVED(name, type, _reg, sel) \ |
| 607 | __BUILD_KVM_RW_VZ(name, type, _reg, sel) \ |
| 608 | __BUILD_KVM_RW_WRAP(c0_guest_##name, vz_gc0_##name, type) \ |
| 609 | __BUILD_KVM_SAVE_VZ(name, _reg, sel) |
| 610 | |
| 611 | #define __BUILD_KVM_SET_HW(name, type, _reg, sel) \ |
| 612 | __BUILD_KVM_SET_SAVED(name, type, _reg, sel) \ |
| 613 | __BUILD_KVM_SET_VZ(name, type, _reg, sel) \ |
| 614 | __BUILD_KVM_SET_WRAP(c0_guest_##name, vz_gc0_##name, type) |
| 615 | |
| 616 | /* |
| 617 | * We can't do atomic modifications of COP0 state if hardware can modify it. |
| 618 | * Races must be handled explicitly. |
| 619 | */ |
| 620 | #define __BUILD_KVM_ATOMIC_HW __BUILD_KVM_SET_HW |
| 621 | |
James Hogan | a27660f | 2017-03-14 10:15:25 +0000 | [diff] [blame] | 622 | /* |
| 623 | * Define accessors for CP0 registers that are accessible to the guest. These |
| 624 | * are primarily used by common emulation code, which may need to access the |
| 625 | * registers differently depending on the implementation. |
| 626 | * |
| 627 | * fns_hw/sw name type reg num select |
| 628 | */ |
| 629 | __BUILD_KVM_RW_HW(index, 32, MIPS_CP0_TLB_INDEX, 0) |
| 630 | __BUILD_KVM_RW_HW(entrylo0, l, MIPS_CP0_TLB_LO0, 0) |
| 631 | __BUILD_KVM_RW_HW(entrylo1, l, MIPS_CP0_TLB_LO1, 0) |
| 632 | __BUILD_KVM_RW_HW(context, l, MIPS_CP0_TLB_CONTEXT, 0) |
James Hogan | dffe042 | 2017-03-14 10:15:34 +0000 | [diff] [blame] | 633 | __BUILD_KVM_RW_HW(contextconfig, 32, MIPS_CP0_TLB_CONTEXT, 1) |
James Hogan | a27660f | 2017-03-14 10:15:25 +0000 | [diff] [blame] | 634 | __BUILD_KVM_RW_HW(userlocal, l, MIPS_CP0_TLB_CONTEXT, 2) |
James Hogan | dffe042 | 2017-03-14 10:15:34 +0000 | [diff] [blame] | 635 | __BUILD_KVM_RW_HW(xcontextconfig, l, MIPS_CP0_TLB_CONTEXT, 3) |
James Hogan | a27660f | 2017-03-14 10:15:25 +0000 | [diff] [blame] | 636 | __BUILD_KVM_RW_HW(pagemask, l, MIPS_CP0_TLB_PG_MASK, 0) |
| 637 | __BUILD_KVM_RW_HW(pagegrain, 32, MIPS_CP0_TLB_PG_MASK, 1) |
James Hogan | 4b7de02 | 2017-03-14 10:15:35 +0000 | [diff] [blame] | 638 | __BUILD_KVM_RW_HW(segctl0, l, MIPS_CP0_TLB_PG_MASK, 2) |
| 639 | __BUILD_KVM_RW_HW(segctl1, l, MIPS_CP0_TLB_PG_MASK, 3) |
| 640 | __BUILD_KVM_RW_HW(segctl2, l, MIPS_CP0_TLB_PG_MASK, 4) |
James Hogan | 5a2f352 | 2017-03-14 10:15:36 +0000 | [diff] [blame] | 641 | __BUILD_KVM_RW_HW(pwbase, l, MIPS_CP0_TLB_PG_MASK, 5) |
| 642 | __BUILD_KVM_RW_HW(pwfield, l, MIPS_CP0_TLB_PG_MASK, 6) |
| 643 | __BUILD_KVM_RW_HW(pwsize, l, MIPS_CP0_TLB_PG_MASK, 7) |
James Hogan | a27660f | 2017-03-14 10:15:25 +0000 | [diff] [blame] | 644 | __BUILD_KVM_RW_HW(wired, 32, MIPS_CP0_TLB_WIRED, 0) |
James Hogan | 5a2f352 | 2017-03-14 10:15:36 +0000 | [diff] [blame] | 645 | __BUILD_KVM_RW_HW(pwctl, 32, MIPS_CP0_TLB_WIRED, 6) |
James Hogan | a27660f | 2017-03-14 10:15:25 +0000 | [diff] [blame] | 646 | __BUILD_KVM_RW_HW(hwrena, 32, MIPS_CP0_HWRENA, 0) |
| 647 | __BUILD_KVM_RW_HW(badvaddr, l, MIPS_CP0_BAD_VADDR, 0) |
James Hogan | edc8926 | 2017-03-14 10:15:33 +0000 | [diff] [blame] | 648 | __BUILD_KVM_RW_HW(badinstr, 32, MIPS_CP0_BAD_VADDR, 1) |
| 649 | __BUILD_KVM_RW_HW(badinstrp, 32, MIPS_CP0_BAD_VADDR, 2) |
James Hogan | a27660f | 2017-03-14 10:15:25 +0000 | [diff] [blame] | 650 | __BUILD_KVM_RW_SW(count, 32, MIPS_CP0_COUNT, 0) |
| 651 | __BUILD_KVM_RW_HW(entryhi, l, MIPS_CP0_TLB_HI, 0) |
| 652 | __BUILD_KVM_RW_HW(compare, 32, MIPS_CP0_COMPARE, 0) |
| 653 | __BUILD_KVM_RW_HW(status, 32, MIPS_CP0_STATUS, 0) |
| 654 | __BUILD_KVM_RW_HW(intctl, 32, MIPS_CP0_STATUS, 1) |
| 655 | __BUILD_KVM_RW_HW(cause, 32, MIPS_CP0_CAUSE, 0) |
| 656 | __BUILD_KVM_RW_HW(epc, l, MIPS_CP0_EXC_PC, 0) |
| 657 | __BUILD_KVM_RW_SW(prid, 32, MIPS_CP0_PRID, 0) |
| 658 | __BUILD_KVM_RW_HW(ebase, l, MIPS_CP0_PRID, 1) |
| 659 | __BUILD_KVM_RW_HW(config, 32, MIPS_CP0_CONFIG, 0) |
| 660 | __BUILD_KVM_RW_HW(config1, 32, MIPS_CP0_CONFIG, 1) |
| 661 | __BUILD_KVM_RW_HW(config2, 32, MIPS_CP0_CONFIG, 2) |
| 662 | __BUILD_KVM_RW_HW(config3, 32, MIPS_CP0_CONFIG, 3) |
| 663 | __BUILD_KVM_RW_HW(config4, 32, MIPS_CP0_CONFIG, 4) |
| 664 | __BUILD_KVM_RW_HW(config5, 32, MIPS_CP0_CONFIG, 5) |
| 665 | __BUILD_KVM_RW_HW(config6, 32, MIPS_CP0_CONFIG, 6) |
| 666 | __BUILD_KVM_RW_HW(config7, 32, MIPS_CP0_CONFIG, 7) |
James Hogan | d42a008 | 2017-03-14 10:15:38 +0000 | [diff] [blame] | 667 | __BUILD_KVM_RW_SW(maari, l, MIPS_CP0_LLADDR, 2) |
James Hogan | c992a4f | 2017-03-14 10:15:31 +0000 | [diff] [blame] | 668 | __BUILD_KVM_RW_HW(xcontext, l, MIPS_CP0_TLB_XCONTEXT, 0) |
James Hogan | a27660f | 2017-03-14 10:15:25 +0000 | [diff] [blame] | 669 | __BUILD_KVM_RW_HW(errorepc, l, MIPS_CP0_ERROR_PC, 0) |
| 670 | __BUILD_KVM_RW_HW(kscratch1, l, MIPS_CP0_DESAVE, 2) |
| 671 | __BUILD_KVM_RW_HW(kscratch2, l, MIPS_CP0_DESAVE, 3) |
| 672 | __BUILD_KVM_RW_HW(kscratch3, l, MIPS_CP0_DESAVE, 4) |
| 673 | __BUILD_KVM_RW_HW(kscratch4, l, MIPS_CP0_DESAVE, 5) |
| 674 | __BUILD_KVM_RW_HW(kscratch5, l, MIPS_CP0_DESAVE, 6) |
| 675 | __BUILD_KVM_RW_HW(kscratch6, l, MIPS_CP0_DESAVE, 7) |
| 676 | |
| 677 | /* Bitwise operations (on HW state) */ |
| 678 | __BUILD_KVM_SET_HW(status, 32, MIPS_CP0_STATUS, 0) |
| 679 | /* Cause can be modified asynchronously from hardirq hrtimer callback */ |
| 680 | __BUILD_KVM_ATOMIC_HW(cause, 32, MIPS_CP0_CAUSE, 0) |
| 681 | __BUILD_KVM_SET_HW(ebase, l, MIPS_CP0_PRID, 1) |
| 682 | |
James Hogan | c992a4f | 2017-03-14 10:15:31 +0000 | [diff] [blame] | 683 | /* Bitwise operations (on saved state) */ |
| 684 | __BUILD_KVM_SET_SAVED(config, 32, MIPS_CP0_CONFIG, 0) |
| 685 | __BUILD_KVM_SET_SAVED(config1, 32, MIPS_CP0_CONFIG, 1) |
| 686 | __BUILD_KVM_SET_SAVED(config2, 32, MIPS_CP0_CONFIG, 2) |
| 687 | __BUILD_KVM_SET_SAVED(config3, 32, MIPS_CP0_CONFIG, 3) |
| 688 | __BUILD_KVM_SET_SAVED(config4, 32, MIPS_CP0_CONFIG, 4) |
| 689 | __BUILD_KVM_SET_SAVED(config5, 32, MIPS_CP0_CONFIG, 5) |
| 690 | |
James Hogan | 98e91b8 | 2014-11-18 14:09:12 +0000 | [diff] [blame] | 691 | /* Helpers */ |
| 692 | |
| 693 | static inline bool kvm_mips_guest_can_have_fpu(struct kvm_vcpu_arch *vcpu) |
| 694 | { |
James Hogan | 19451e5 | 2016-06-15 19:29:50 +0100 | [diff] [blame] | 695 | return (!__builtin_constant_p(raw_cpu_has_fpu) || raw_cpu_has_fpu) && |
James Hogan | 98e91b8 | 2014-11-18 14:09:12 +0000 | [diff] [blame] | 696 | vcpu->fpu_enabled; |
| 697 | } |
| 698 | |
| 699 | static inline bool kvm_mips_guest_has_fpu(struct kvm_vcpu_arch *vcpu) |
| 700 | { |
| 701 | return kvm_mips_guest_can_have_fpu(vcpu) && |
| 702 | kvm_read_c0_guest_config1(vcpu->cop0) & MIPS_CONF1_FP; |
| 703 | } |
Sanjay Lal | 740765c | 2012-11-21 18:34:00 -0800 | [diff] [blame] | 704 | |
James Hogan | 539cb89fb | 2015-03-05 11:43:36 +0000 | [diff] [blame] | 705 | static inline bool kvm_mips_guest_can_have_msa(struct kvm_vcpu_arch *vcpu) |
| 706 | { |
| 707 | return (!__builtin_constant_p(cpu_has_msa) || cpu_has_msa) && |
| 708 | vcpu->msa_enabled; |
| 709 | } |
| 710 | |
| 711 | static inline bool kvm_mips_guest_has_msa(struct kvm_vcpu_arch *vcpu) |
| 712 | { |
| 713 | return kvm_mips_guest_can_have_msa(vcpu) && |
| 714 | kvm_read_c0_guest_config3(vcpu->cop0) & MIPS_CONF3_MSA; |
| 715 | } |
| 716 | |
Sanjay Lal | 740765c | 2012-11-21 18:34:00 -0800 | [diff] [blame] | 717 | struct kvm_mips_callbacks { |
James Hogan | 2dca372 | 2014-05-29 10:16:40 +0100 | [diff] [blame] | 718 | int (*handle_cop_unusable)(struct kvm_vcpu *vcpu); |
| 719 | int (*handle_tlb_mod)(struct kvm_vcpu *vcpu); |
| 720 | int (*handle_tlb_ld_miss)(struct kvm_vcpu *vcpu); |
| 721 | int (*handle_tlb_st_miss)(struct kvm_vcpu *vcpu); |
| 722 | int (*handle_addr_err_st)(struct kvm_vcpu *vcpu); |
| 723 | int (*handle_addr_err_ld)(struct kvm_vcpu *vcpu); |
| 724 | int (*handle_syscall)(struct kvm_vcpu *vcpu); |
| 725 | int (*handle_res_inst)(struct kvm_vcpu *vcpu); |
| 726 | int (*handle_break)(struct kvm_vcpu *vcpu); |
James Hogan | 0a56042 | 2015-02-06 16:03:57 +0000 | [diff] [blame] | 727 | int (*handle_trap)(struct kvm_vcpu *vcpu); |
James Hogan | c2537ed | 2015-02-06 10:56:27 +0000 | [diff] [blame] | 728 | int (*handle_msa_fpe)(struct kvm_vcpu *vcpu); |
James Hogan | 1c0cd66 | 2015-02-06 10:56:27 +0000 | [diff] [blame] | 729 | int (*handle_fpe)(struct kvm_vcpu *vcpu); |
James Hogan | 98119ad | 2015-02-06 11:11:56 +0000 | [diff] [blame] | 730 | int (*handle_msa_disabled)(struct kvm_vcpu *vcpu); |
James Hogan | 28c1e76 | 2017-03-14 10:15:24 +0000 | [diff] [blame] | 731 | int (*handle_guest_exit)(struct kvm_vcpu *vcpu); |
James Hogan | edab4fe | 2017-03-14 10:15:23 +0000 | [diff] [blame] | 732 | int (*hardware_enable)(void); |
| 733 | void (*hardware_disable)(void); |
James Hogan | 607ef2f | 2017-03-14 10:15:22 +0000 | [diff] [blame] | 734 | int (*check_extension)(struct kvm *kvm, long ext); |
James Hogan | 2dca372 | 2014-05-29 10:16:40 +0100 | [diff] [blame] | 735 | int (*vcpu_init)(struct kvm_vcpu *vcpu); |
James Hogan | 630766b3 | 2016-09-08 23:00:24 +0100 | [diff] [blame] | 736 | void (*vcpu_uninit)(struct kvm_vcpu *vcpu); |
James Hogan | 2dca372 | 2014-05-29 10:16:40 +0100 | [diff] [blame] | 737 | int (*vcpu_setup)(struct kvm_vcpu *vcpu); |
Paolo Bonzini | 5194552f | 2021-03-31 09:38:16 +0200 | [diff] [blame] | 738 | void (*prepare_flush_shadow)(struct kvm *kvm); |
James Hogan | 2dca372 | 2014-05-29 10:16:40 +0100 | [diff] [blame] | 739 | gpa_t (*gva_to_gpa)(gva_t gva); |
| 740 | void (*queue_timer_int)(struct kvm_vcpu *vcpu); |
| 741 | void (*dequeue_timer_int)(struct kvm_vcpu *vcpu); |
| 742 | void (*queue_io_int)(struct kvm_vcpu *vcpu, |
| 743 | struct kvm_mips_interrupt *irq); |
| 744 | void (*dequeue_io_int)(struct kvm_vcpu *vcpu, |
| 745 | struct kvm_mips_interrupt *irq); |
| 746 | int (*irq_deliver)(struct kvm_vcpu *vcpu, unsigned int priority, |
James Hogan | bdb7ed8 | 2016-06-09 14:19:07 +0100 | [diff] [blame] | 747 | u32 cause); |
James Hogan | 2dca372 | 2014-05-29 10:16:40 +0100 | [diff] [blame] | 748 | int (*irq_clear)(struct kvm_vcpu *vcpu, unsigned int priority, |
James Hogan | bdb7ed8 | 2016-06-09 14:19:07 +0100 | [diff] [blame] | 749 | u32 cause); |
James Hogan | f5c43bd | 2016-06-15 19:29:49 +0100 | [diff] [blame] | 750 | unsigned long (*num_regs)(struct kvm_vcpu *vcpu); |
| 751 | int (*copy_reg_indices)(struct kvm_vcpu *vcpu, u64 __user *indices); |
James Hogan | f8be02d | 2014-05-29 10:16:29 +0100 | [diff] [blame] | 752 | int (*get_one_reg)(struct kvm_vcpu *vcpu, |
| 753 | const struct kvm_one_reg *reg, s64 *v); |
| 754 | int (*set_one_reg)(struct kvm_vcpu *vcpu, |
| 755 | const struct kvm_one_reg *reg, s64 v); |
James Hogan | a60b843 | 2016-11-12 00:00:13 +0000 | [diff] [blame] | 756 | int (*vcpu_load)(struct kvm_vcpu *vcpu, int cpu); |
| 757 | int (*vcpu_put)(struct kvm_vcpu *vcpu, int cpu); |
Tianjia Zhang | c34b26b | 2020-06-23 21:14:17 +0800 | [diff] [blame] | 758 | int (*vcpu_run)(struct kvm_vcpu *vcpu); |
| 759 | void (*vcpu_reenter)(struct kvm_vcpu *vcpu); |
Sanjay Lal | 740765c | 2012-11-21 18:34:00 -0800 | [diff] [blame] | 760 | }; |
| 761 | extern struct kvm_mips_callbacks *kvm_mips_callbacks; |
| 762 | int kvm_mips_emulation_init(struct kvm_mips_callbacks **install_callbacks); |
| 763 | |
| 764 | /* Debug: dump vcpu state */ |
| 765 | int kvm_arch_vcpu_dump_regs(struct kvm_vcpu *vcpu); |
| 766 | |
Tianjia Zhang | 0b7aa58 | 2020-06-23 21:14:18 +0800 | [diff] [blame] | 767 | extern int kvm_mips_handle_exit(struct kvm_vcpu *vcpu); |
James Hogan | 90e9311 | 2016-06-23 17:34:39 +0100 | [diff] [blame] | 768 | |
| 769 | /* Building of entry/exception code */ |
James Hogan | 1e5217f5 | 2016-06-23 17:34:45 +0100 | [diff] [blame] | 770 | int kvm_mips_entry_setup(void); |
James Hogan | 90e9311 | 2016-06-23 17:34:39 +0100 | [diff] [blame] | 771 | void *kvm_mips_build_vcpu_run(void *addr); |
James Hogan | a7cfa7a | 2016-09-10 23:56:46 +0100 | [diff] [blame] | 772 | void *kvm_mips_build_tlb_refill_exception(void *addr, void *handler); |
James Hogan | 1f9ca62 | 2016-06-23 17:34:46 +0100 | [diff] [blame] | 773 | void *kvm_mips_build_exception(void *addr, void *handler); |
James Hogan | 90e9311 | 2016-06-23 17:34:39 +0100 | [diff] [blame] | 774 | void *kvm_mips_build_exit(void *addr); |
Sanjay Lal | 740765c | 2012-11-21 18:34:00 -0800 | [diff] [blame] | 775 | |
James Hogan | 539cb89fb | 2015-03-05 11:43:36 +0000 | [diff] [blame] | 776 | /* FPU/MSA context management */ |
James Hogan | 98e91b8 | 2014-11-18 14:09:12 +0000 | [diff] [blame] | 777 | void __kvm_save_fpu(struct kvm_vcpu_arch *vcpu); |
| 778 | void __kvm_restore_fpu(struct kvm_vcpu_arch *vcpu); |
| 779 | void __kvm_restore_fcsr(struct kvm_vcpu_arch *vcpu); |
James Hogan | 539cb89fb | 2015-03-05 11:43:36 +0000 | [diff] [blame] | 780 | void __kvm_save_msa(struct kvm_vcpu_arch *vcpu); |
| 781 | void __kvm_restore_msa(struct kvm_vcpu_arch *vcpu); |
| 782 | void __kvm_restore_msa_upper(struct kvm_vcpu_arch *vcpu); |
| 783 | void __kvm_restore_msacsr(struct kvm_vcpu_arch *vcpu); |
James Hogan | 98e91b8 | 2014-11-18 14:09:12 +0000 | [diff] [blame] | 784 | void kvm_own_fpu(struct kvm_vcpu *vcpu); |
James Hogan | 539cb89fb | 2015-03-05 11:43:36 +0000 | [diff] [blame] | 785 | void kvm_own_msa(struct kvm_vcpu *vcpu); |
James Hogan | 98e91b8 | 2014-11-18 14:09:12 +0000 | [diff] [blame] | 786 | void kvm_drop_fpu(struct kvm_vcpu *vcpu); |
| 787 | void kvm_lose_fpu(struct kvm_vcpu *vcpu); |
| 788 | |
Sanjay Lal | 740765c | 2012-11-21 18:34:00 -0800 | [diff] [blame] | 789 | /* TLB handling */ |
James Hogan | c992a4f | 2017-03-14 10:15:31 +0000 | [diff] [blame] | 790 | int kvm_mips_handle_vz_root_tlb_fault(unsigned long badvaddr, |
| 791 | struct kvm_vcpu *vcpu, bool write_fault); |
Sanjay Lal | 740765c | 2012-11-21 18:34:00 -0800 | [diff] [blame] | 792 | |
James Hogan | 372582a | 2017-03-14 10:15:27 +0000 | [diff] [blame] | 793 | int kvm_vz_host_tlb_inv(struct kvm_vcpu *vcpu, unsigned long entryhi); |
| 794 | int kvm_vz_guest_tlb_lookup(struct kvm_vcpu *vcpu, unsigned long gva, |
| 795 | unsigned long *gpa); |
| 796 | void kvm_vz_local_flush_roottlb_all_guests(void); |
| 797 | void kvm_vz_local_flush_guesttlb_all(void); |
| 798 | void kvm_vz_save_guesttlb(struct kvm_mips_tlb *buf, unsigned int index, |
| 799 | unsigned int count); |
| 800 | void kvm_vz_load_guesttlb(const struct kvm_mips_tlb *buf, unsigned int index, |
| 801 | unsigned int count); |
Huacai Chen | 8a5097e | 2020-05-23 15:56:39 +0800 | [diff] [blame] | 802 | #ifdef CONFIG_CPU_LOONGSON64 |
| 803 | void kvm_loongson_clear_guest_vtlb(void); |
| 804 | void kvm_loongson_clear_guest_ftlb(void); |
| 805 | #endif |
James Hogan | a7ebb2e | 2016-11-15 00:06:05 +0000 | [diff] [blame] | 806 | |
James Hogan | a31b50d | 2016-12-16 15:57:00 +0000 | [diff] [blame] | 807 | /* MMU handling */ |
| 808 | |
James Hogan | 06c158c | 2015-05-01 13:50:18 +0100 | [diff] [blame] | 809 | bool kvm_mips_flush_gpa_pt(struct kvm *kvm, gfn_t start_gfn, gfn_t end_gfn); |
James Hogan | f0c0c33 | 2016-12-06 14:47:47 +0000 | [diff] [blame] | 810 | int kvm_mips_mkclean_gpa_pt(struct kvm *kvm, gfn_t start_gfn, gfn_t end_gfn); |
James Hogan | 06c158c | 2015-05-01 13:50:18 +0100 | [diff] [blame] | 811 | pgd_t *kvm_pgd_alloc(void); |
James Hogan | aba8592 | 2016-12-16 15:57:00 +0000 | [diff] [blame] | 812 | void kvm_mmu_free_memory_caches(struct kvm_vcpu *vcpu); |
Sanjay Lal | 740765c | 2012-11-21 18:34:00 -0800 | [diff] [blame] | 813 | |
James Hogan | 411740f | 2016-12-13 16:32:39 +0000 | [diff] [blame] | 814 | #define KVM_ARCH_WANT_MMU_NOTIFIER |
James Hogan | 411740f | 2016-12-13 16:32:39 +0000 | [diff] [blame] | 815 | |
Sanjay Lal | 740765c | 2012-11-21 18:34:00 -0800 | [diff] [blame] | 816 | /* Emulation */ |
James Hogan | bdb7ed8 | 2016-06-09 14:19:07 +0100 | [diff] [blame] | 817 | enum emulation_result update_pc(struct kvm_vcpu *vcpu, u32 cause); |
James Hogan | 6a97c77 | 2015-04-23 16:54:35 +0100 | [diff] [blame] | 818 | int kvm_get_badinstr(u32 *opc, struct kvm_vcpu *vcpu, u32 *out); |
| 819 | int kvm_get_badinstrp(u32 *opc, struct kvm_vcpu *vcpu, u32 *out); |
Sanjay Lal | 740765c | 2012-11-21 18:34:00 -0800 | [diff] [blame] | 820 | |
James Hogan | a1ecc54 | 2016-11-28 18:39:24 +0000 | [diff] [blame] | 821 | /** |
| 822 | * kvm_is_ifetch_fault() - Find whether a TLBL exception is due to ifetch fault. |
| 823 | * @vcpu: Virtual CPU. |
| 824 | * |
| 825 | * Returns: Whether the TLBL exception was likely due to an instruction |
| 826 | * fetch fault rather than a data load fault. |
| 827 | */ |
| 828 | static inline bool kvm_is_ifetch_fault(struct kvm_vcpu_arch *vcpu) |
| 829 | { |
| 830 | unsigned long badvaddr = vcpu->host_cp0_badvaddr; |
| 831 | unsigned long epc = msk_isa16_mode(vcpu->pc); |
| 832 | u32 cause = vcpu->host_cp0_cause; |
| 833 | |
| 834 | if (epc == badvaddr) |
| 835 | return true; |
| 836 | |
| 837 | /* |
| 838 | * Branches may be 32-bit or 16-bit instructions. |
| 839 | * This isn't exact, but we don't really support MIPS16 or microMIPS yet |
| 840 | * in KVM anyway. |
| 841 | */ |
| 842 | if ((cause & CAUSEF_BD) && badvaddr - epc <= 4) |
| 843 | return true; |
| 844 | |
| 845 | return false; |
| 846 | } |
| 847 | |
Tianjia Zhang | c34b26b | 2020-06-23 21:14:17 +0800 | [diff] [blame] | 848 | extern enum emulation_result kvm_mips_complete_mmio_load(struct kvm_vcpu *vcpu); |
Sanjay Lal | 740765c | 2012-11-21 18:34:00 -0800 | [diff] [blame] | 849 | |
James Hogan | bdb7ed8 | 2016-06-09 14:19:07 +0100 | [diff] [blame] | 850 | u32 kvm_mips_read_count(struct kvm_vcpu *vcpu); |
| 851 | void kvm_mips_write_count(struct kvm_vcpu *vcpu, u32 count); |
| 852 | void kvm_mips_write_compare(struct kvm_vcpu *vcpu, u32 compare, bool ack); |
James Hogan | a517c1a | 2017-03-14 10:15:21 +0000 | [diff] [blame] | 853 | void kvm_mips_init_count(struct kvm_vcpu *vcpu, unsigned long count_hz); |
James Hogan | f823934 | 2014-05-29 10:16:37 +0100 | [diff] [blame] | 854 | int kvm_mips_set_count_ctl(struct kvm_vcpu *vcpu, s64 count_ctl); |
| 855 | int kvm_mips_set_count_resume(struct kvm_vcpu *vcpu, s64 count_resume); |
James Hogan | f74a8e2 | 2014-05-29 10:16:38 +0100 | [diff] [blame] | 856 | int kvm_mips_set_count_hz(struct kvm_vcpu *vcpu, s64 count_hz); |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 857 | void kvm_mips_count_enable_cause(struct kvm_vcpu *vcpu); |
| 858 | void kvm_mips_count_disable_cause(struct kvm_vcpu *vcpu); |
| 859 | enum hrtimer_restart kvm_mips_count_timeout(struct kvm_vcpu *vcpu); |
Sanjay Lal | 740765c | 2012-11-21 18:34:00 -0800 | [diff] [blame] | 860 | |
James Hogan | f4474d5 | 2017-03-14 10:15:39 +0000 | [diff] [blame] | 861 | /* fairly internal functions requiring some care to use */ |
| 862 | int kvm_mips_count_disabled(struct kvm_vcpu *vcpu); |
| 863 | ktime_t kvm_mips_freeze_hrtimer(struct kvm_vcpu *vcpu, u32 *count); |
| 864 | int kvm_mips_restore_hrtimer(struct kvm_vcpu *vcpu, ktime_t before, |
| 865 | u32 count, int min_drift); |
| 866 | |
James Hogan | f4474d5 | 2017-03-14 10:15:39 +0000 | [diff] [blame] | 867 | void kvm_vz_acquire_htimer(struct kvm_vcpu *vcpu); |
| 868 | void kvm_vz_lose_htimer(struct kvm_vcpu *vcpu); |
James Hogan | f4474d5 | 2017-03-14 10:15:39 +0000 | [diff] [blame] | 869 | |
James Hogan | 258f3a2 | 2016-06-15 19:29:47 +0100 | [diff] [blame] | 870 | enum emulation_result kvm_mips_emulate_store(union mips_instruction inst, |
James Hogan | bdb7ed8 | 2016-06-09 14:19:07 +0100 | [diff] [blame] | 871 | u32 cause, |
Sanjay Lal | 740765c | 2012-11-21 18:34:00 -0800 | [diff] [blame] | 872 | struct kvm_vcpu *vcpu); |
James Hogan | 258f3a2 | 2016-06-15 19:29:47 +0100 | [diff] [blame] | 873 | enum emulation_result kvm_mips_emulate_load(union mips_instruction inst, |
James Hogan | bdb7ed8 | 2016-06-09 14:19:07 +0100 | [diff] [blame] | 874 | u32 cause, |
Sanjay Lal | 740765c | 2012-11-21 18:34:00 -0800 | [diff] [blame] | 875 | struct kvm_vcpu *vcpu); |
| 876 | |
James Hogan | c992a4f | 2017-03-14 10:15:31 +0000 | [diff] [blame] | 877 | /* COP0 */ |
| 878 | enum emulation_result kvm_mips_emul_wait(struct kvm_vcpu *vcpu); |
| 879 | |
James Hogan | 955d8dc | 2017-03-14 10:15:14 +0000 | [diff] [blame] | 880 | /* Hypercalls (hypcall.c) */ |
| 881 | |
| 882 | enum emulation_result kvm_mips_emul_hypcall(struct kvm_vcpu *vcpu, |
| 883 | union mips_instruction inst); |
| 884 | int kvm_mips_handle_hypcall(struct kvm_vcpu *vcpu); |
| 885 | |
Sanjay Lal | 740765c | 2012-11-21 18:34:00 -0800 | [diff] [blame] | 886 | /* Misc */ |
Deng-Cheng Zhu | d98403a | 2014-06-26 12:11:36 -0700 | [diff] [blame] | 887 | extern void kvm_mips_dump_stats(struct kvm_vcpu *vcpu); |
Sanjay Lal | 740765c | 2012-11-21 18:34:00 -0800 | [diff] [blame] | 888 | extern unsigned long kvm_mips_get_ramsize(struct kvm *kvm); |
Huacai Chen | f21db30 | 2020-05-23 15:56:37 +0800 | [diff] [blame] | 889 | extern int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, |
| 890 | struct kvm_mips_interrupt *irq); |
Sanjay Lal | 740765c | 2012-11-21 18:34:00 -0800 | [diff] [blame] | 891 | |
Radim Krčmář | 0865e63 | 2014-08-28 15:13:02 +0200 | [diff] [blame] | 892 | static inline void kvm_arch_hardware_unsetup(void) {} |
| 893 | static inline void kvm_arch_sync_events(struct kvm *kvm) {} |
| 894 | static inline void kvm_arch_free_memslot(struct kvm *kvm, |
Sean Christopherson | e96c81e | 2020-02-18 13:07:27 -0800 | [diff] [blame] | 895 | struct kvm_memory_slot *slot) {} |
Sean Christopherson | 1524825 | 2019-02-05 12:54:17 -0800 | [diff] [blame] | 896 | static inline void kvm_arch_memslots_updated(struct kvm *kvm, u64 gen) {} |
Radim Krčmář | 0865e63 | 2014-08-28 15:13:02 +0200 | [diff] [blame] | 897 | static inline void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu) {} |
Christoffer Dall | 3217f7c | 2015-08-27 16:41:15 +0200 | [diff] [blame] | 898 | static inline void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu) {} |
| 899 | static inline void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu) {} |
Christian Borntraeger | 3491caf | 2016-05-13 12:16:35 +0200 | [diff] [blame] | 900 | static inline void kvm_arch_vcpu_block_finish(struct kvm_vcpu *vcpu) {} |
Sanjay Lal | 740765c | 2012-11-21 18:34:00 -0800 | [diff] [blame] | 901 | |
Paolo Bonzini | 566a0be | 2021-04-02 11:44:56 +0200 | [diff] [blame] | 902 | #define __KVM_HAVE_ARCH_FLUSH_REMOTE_TLB |
| 903 | int kvm_arch_flush_remote_tlb(struct kvm *kvm); |
| 904 | |
Sanjay Lal | 740765c | 2012-11-21 18:34:00 -0800 | [diff] [blame] | 905 | #endif /* __MIPS_KVM_HOST_H__ */ |