kvm_host.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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. #ifndef __MIPS_KVM_HOST_H__
  10. #define __MIPS_KVM_HOST_H__
  11. #include <linux/mutex.h>
  12. #include <linux/hrtimer.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/types.h>
  15. #include <linux/kvm.h>
  16. #include <linux/kvm_types.h>
  17. #include <linux/threads.h>
  18. #include <linux/spinlock.h>
  19. #include <asm/inst.h>
  20. #include <asm/mipsregs.h>
  21. /* MIPS KVM register ids */
  22. #define MIPS_CP0_32(_R, _S) \
  23. (KVM_REG_MIPS_CP0 | KVM_REG_SIZE_U32 | (8 * (_R) + (_S)))
  24. #define MIPS_CP0_64(_R, _S) \
  25. (KVM_REG_MIPS_CP0 | KVM_REG_SIZE_U64 | (8 * (_R) + (_S)))
  26. #define KVM_REG_MIPS_CP0_INDEX MIPS_CP0_32(0, 0)
  27. #define KVM_REG_MIPS_CP0_ENTRYLO0 MIPS_CP0_64(2, 0)
  28. #define KVM_REG_MIPS_CP0_ENTRYLO1 MIPS_CP0_64(3, 0)
  29. #define KVM_REG_MIPS_CP0_CONTEXT MIPS_CP0_64(4, 0)
  30. #define KVM_REG_MIPS_CP0_USERLOCAL MIPS_CP0_64(4, 2)
  31. #define KVM_REG_MIPS_CP0_PAGEMASK MIPS_CP0_32(5, 0)
  32. #define KVM_REG_MIPS_CP0_PAGEGRAIN MIPS_CP0_32(5, 1)
  33. #define KVM_REG_MIPS_CP0_WIRED MIPS_CP0_32(6, 0)
  34. #define KVM_REG_MIPS_CP0_HWRENA MIPS_CP0_32(7, 0)
  35. #define KVM_REG_MIPS_CP0_BADVADDR MIPS_CP0_64(8, 0)
  36. #define KVM_REG_MIPS_CP0_COUNT MIPS_CP0_32(9, 0)
  37. #define KVM_REG_MIPS_CP0_ENTRYHI MIPS_CP0_64(10, 0)
  38. #define KVM_REG_MIPS_CP0_COMPARE MIPS_CP0_32(11, 0)
  39. #define KVM_REG_MIPS_CP0_STATUS MIPS_CP0_32(12, 0)
  40. #define KVM_REG_MIPS_CP0_CAUSE MIPS_CP0_32(13, 0)
  41. #define KVM_REG_MIPS_CP0_EPC MIPS_CP0_64(14, 0)
  42. #define KVM_REG_MIPS_CP0_PRID MIPS_CP0_32(15, 0)
  43. #define KVM_REG_MIPS_CP0_EBASE MIPS_CP0_64(15, 1)
  44. #define KVM_REG_MIPS_CP0_CONFIG MIPS_CP0_32(16, 0)
  45. #define KVM_REG_MIPS_CP0_CONFIG1 MIPS_CP0_32(16, 1)
  46. #define KVM_REG_MIPS_CP0_CONFIG2 MIPS_CP0_32(16, 2)
  47. #define KVM_REG_MIPS_CP0_CONFIG3 MIPS_CP0_32(16, 3)
  48. #define KVM_REG_MIPS_CP0_CONFIG4 MIPS_CP0_32(16, 4)
  49. #define KVM_REG_MIPS_CP0_CONFIG5 MIPS_CP0_32(16, 5)
  50. #define KVM_REG_MIPS_CP0_CONFIG7 MIPS_CP0_32(16, 7)
  51. #define KVM_REG_MIPS_CP0_XCONTEXT MIPS_CP0_64(20, 0)
  52. #define KVM_REG_MIPS_CP0_ERROREPC MIPS_CP0_64(30, 0)
  53. #define KVM_REG_MIPS_CP0_KSCRATCH1 MIPS_CP0_64(31, 2)
  54. #define KVM_REG_MIPS_CP0_KSCRATCH2 MIPS_CP0_64(31, 3)
  55. #define KVM_REG_MIPS_CP0_KSCRATCH3 MIPS_CP0_64(31, 4)
  56. #define KVM_REG_MIPS_CP0_KSCRATCH4 MIPS_CP0_64(31, 5)
  57. #define KVM_REG_MIPS_CP0_KSCRATCH5 MIPS_CP0_64(31, 6)
  58. #define KVM_REG_MIPS_CP0_KSCRATCH6 MIPS_CP0_64(31, 7)
  59. #define KVM_MAX_VCPUS 1
  60. #define KVM_USER_MEM_SLOTS 8
  61. /* memory slots that does not exposed to userspace */
  62. #define KVM_PRIVATE_MEM_SLOTS 0
  63. #define KVM_COALESCED_MMIO_PAGE_OFFSET 1
  64. #define KVM_HALT_POLL_NS_DEFAULT 500000
  65. /*
  66. * Special address that contains the comm page, used for reducing # of traps
  67. * This needs to be within 32Kb of 0x0 (so the zero register can be used), but
  68. * preferably not at 0x0 so that most kernel NULL pointer dereferences can be
  69. * caught.
  70. */
  71. #define KVM_GUEST_COMMPAGE_ADDR ((PAGE_SIZE > 0x8000) ? 0 : \
  72. (0x8000 - PAGE_SIZE))
  73. #define KVM_GUEST_KERNEL_MODE(vcpu) ((kvm_read_c0_guest_status(vcpu->arch.cop0) & (ST0_EXL | ST0_ERL)) || \
  74. ((kvm_read_c0_guest_status(vcpu->arch.cop0) & KSU_USER) == 0))
  75. #define KVM_GUEST_KUSEG 0x00000000UL
  76. #define KVM_GUEST_KSEG0 0x40000000UL
  77. #define KVM_GUEST_KSEG23 0x60000000UL
  78. #define KVM_GUEST_KSEGX(a) ((_ACAST32_(a)) & 0xe0000000)
  79. #define KVM_GUEST_CPHYSADDR(a) ((_ACAST32_(a)) & 0x1fffffff)
  80. #define KVM_GUEST_CKSEG0ADDR(a) (KVM_GUEST_CPHYSADDR(a) | KVM_GUEST_KSEG0)
  81. #define KVM_GUEST_CKSEG1ADDR(a) (KVM_GUEST_CPHYSADDR(a) | KVM_GUEST_KSEG1)
  82. #define KVM_GUEST_CKSEG23ADDR(a) (KVM_GUEST_CPHYSADDR(a) | KVM_GUEST_KSEG23)
  83. /*
  84. * Map an address to a certain kernel segment
  85. */
  86. #define KVM_GUEST_KSEG0ADDR(a) (KVM_GUEST_CPHYSADDR(a) | KVM_GUEST_KSEG0)
  87. #define KVM_GUEST_KSEG1ADDR(a) (KVM_GUEST_CPHYSADDR(a) | KVM_GUEST_KSEG1)
  88. #define KVM_GUEST_KSEG23ADDR(a) (KVM_GUEST_CPHYSADDR(a) | KVM_GUEST_KSEG23)
  89. #define KVM_INVALID_PAGE 0xdeadbeef
  90. #define KVM_INVALID_INST 0xdeadbeef
  91. #define KVM_INVALID_ADDR 0xdeadbeef
  92. /*
  93. * EVA has overlapping user & kernel address spaces, so user VAs may be >
  94. * PAGE_OFFSET. For this reason we can't use the default KVM_HVA_ERR_BAD of
  95. * PAGE_OFFSET.
  96. */
  97. #define KVM_HVA_ERR_BAD (-1UL)
  98. #define KVM_HVA_ERR_RO_BAD (-2UL)
  99. static inline bool kvm_is_error_hva(unsigned long addr)
  100. {
  101. return IS_ERR_VALUE(addr);
  102. }
  103. extern atomic_t kvm_mips_instance;
  104. struct kvm_vm_stat {
  105. ulong remote_tlb_flush;
  106. };
  107. struct kvm_vcpu_stat {
  108. u64 wait_exits;
  109. u64 cache_exits;
  110. u64 signal_exits;
  111. u64 int_exits;
  112. u64 cop_unusable_exits;
  113. u64 tlbmod_exits;
  114. u64 tlbmiss_ld_exits;
  115. u64 tlbmiss_st_exits;
  116. u64 addrerr_st_exits;
  117. u64 addrerr_ld_exits;
  118. u64 syscall_exits;
  119. u64 resvd_inst_exits;
  120. u64 break_inst_exits;
  121. u64 trap_inst_exits;
  122. u64 msa_fpe_exits;
  123. u64 fpe_exits;
  124. u64 msa_disabled_exits;
  125. u64 flush_dcache_exits;
  126. u64 halt_successful_poll;
  127. u64 halt_attempted_poll;
  128. u64 halt_poll_invalid;
  129. u64 halt_wakeup;
  130. };
  131. struct kvm_arch_memory_slot {
  132. };
  133. struct kvm_arch {
  134. /* Guest GVA->HPA page table */
  135. unsigned long *guest_pmap;
  136. unsigned long guest_pmap_npages;
  137. /* Wired host TLB used for the commpage */
  138. int commpage_tlb;
  139. };
  140. #define N_MIPS_COPROC_REGS 32
  141. #define N_MIPS_COPROC_SEL 8
  142. struct mips_coproc {
  143. unsigned long reg[N_MIPS_COPROC_REGS][N_MIPS_COPROC_SEL];
  144. #ifdef CONFIG_KVM_MIPS_DEBUG_COP0_COUNTERS
  145. unsigned long stat[N_MIPS_COPROC_REGS][N_MIPS_COPROC_SEL];
  146. #endif
  147. };
  148. /*
  149. * Coprocessor 0 register names
  150. */
  151. #define MIPS_CP0_TLB_INDEX 0
  152. #define MIPS_CP0_TLB_RANDOM 1
  153. #define MIPS_CP0_TLB_LOW 2
  154. #define MIPS_CP0_TLB_LO0 2
  155. #define MIPS_CP0_TLB_LO1 3
  156. #define MIPS_CP0_TLB_CONTEXT 4
  157. #define MIPS_CP0_TLB_PG_MASK 5
  158. #define MIPS_CP0_TLB_WIRED 6
  159. #define MIPS_CP0_HWRENA 7
  160. #define MIPS_CP0_BAD_VADDR 8
  161. #define MIPS_CP0_COUNT 9
  162. #define MIPS_CP0_TLB_HI 10
  163. #define MIPS_CP0_COMPARE 11
  164. #define MIPS_CP0_STATUS 12
  165. #define MIPS_CP0_CAUSE 13
  166. #define MIPS_CP0_EXC_PC 14
  167. #define MIPS_CP0_PRID 15
  168. #define MIPS_CP0_CONFIG 16
  169. #define MIPS_CP0_LLADDR 17
  170. #define MIPS_CP0_WATCH_LO 18
  171. #define MIPS_CP0_WATCH_HI 19
  172. #define MIPS_CP0_TLB_XCONTEXT 20
  173. #define MIPS_CP0_ECC 26
  174. #define MIPS_CP0_CACHE_ERR 27
  175. #define MIPS_CP0_TAG_LO 28
  176. #define MIPS_CP0_TAG_HI 29
  177. #define MIPS_CP0_ERROR_PC 30
  178. #define MIPS_CP0_DEBUG 23
  179. #define MIPS_CP0_DEPC 24
  180. #define MIPS_CP0_PERFCNT 25
  181. #define MIPS_CP0_ERRCTL 26
  182. #define MIPS_CP0_DATA_LO 28
  183. #define MIPS_CP0_DATA_HI 29
  184. #define MIPS_CP0_DESAVE 31
  185. #define MIPS_CP0_CONFIG_SEL 0
  186. #define MIPS_CP0_CONFIG1_SEL 1
  187. #define MIPS_CP0_CONFIG2_SEL 2
  188. #define MIPS_CP0_CONFIG3_SEL 3
  189. #define MIPS_CP0_CONFIG4_SEL 4
  190. #define MIPS_CP0_CONFIG5_SEL 5
  191. /* Resume Flags */
  192. #define RESUME_FLAG_DR (1<<0) /* Reload guest nonvolatile state? */
  193. #define RESUME_FLAG_HOST (1<<1) /* Resume host? */
  194. #define RESUME_GUEST 0
  195. #define RESUME_GUEST_DR RESUME_FLAG_DR
  196. #define RESUME_HOST RESUME_FLAG_HOST
  197. enum emulation_result {
  198. EMULATE_DONE, /* no further processing */
  199. EMULATE_DO_MMIO, /* kvm_run filled with MMIO request */
  200. EMULATE_FAIL, /* can't emulate this instruction */
  201. EMULATE_WAIT, /* WAIT instruction */
  202. EMULATE_PRIV_FAIL,
  203. };
  204. #define mips3_paddr_to_tlbpfn(x) \
  205. (((unsigned long)(x) >> MIPS3_PG_SHIFT) & MIPS3_PG_FRAME)
  206. #define mips3_tlbpfn_to_paddr(x) \
  207. ((unsigned long)((x) & MIPS3_PG_FRAME) << MIPS3_PG_SHIFT)
  208. #define MIPS3_PG_SHIFT 6
  209. #define MIPS3_PG_FRAME 0x3fffffc0
  210. #define VPN2_MASK 0xffffe000
  211. #define KVM_ENTRYHI_ASID MIPS_ENTRYHI_ASID
  212. #define TLB_IS_GLOBAL(x) ((x).tlb_lo[0] & (x).tlb_lo[1] & ENTRYLO_G)
  213. #define TLB_VPN2(x) ((x).tlb_hi & VPN2_MASK)
  214. #define TLB_ASID(x) ((x).tlb_hi & KVM_ENTRYHI_ASID)
  215. #define TLB_LO_IDX(x, va) (((va) >> PAGE_SHIFT) & 1)
  216. #define TLB_IS_VALID(x, va) ((x).tlb_lo[TLB_LO_IDX(x, va)] & ENTRYLO_V)
  217. #define TLB_HI_VPN2_HIT(x, y) ((TLB_VPN2(x) & ~(x).tlb_mask) == \
  218. ((y) & VPN2_MASK & ~(x).tlb_mask))
  219. #define TLB_HI_ASID_HIT(x, y) (TLB_IS_GLOBAL(x) || \
  220. TLB_ASID(x) == ((y) & KVM_ENTRYHI_ASID))
  221. struct kvm_mips_tlb {
  222. long tlb_mask;
  223. long tlb_hi;
  224. long tlb_lo[2];
  225. };
  226. #define KVM_MIPS_AUX_FPU 0x1
  227. #define KVM_MIPS_AUX_MSA 0x2
  228. #define KVM_MIPS_GUEST_TLB_SIZE 64
  229. struct kvm_vcpu_arch {
  230. void *guest_ebase;
  231. int (*vcpu_run)(struct kvm_run *run, struct kvm_vcpu *vcpu);
  232. unsigned long host_stack;
  233. unsigned long host_gp;
  234. /* Host CP0 registers used when handling exits from guest */
  235. unsigned long host_cp0_badvaddr;
  236. unsigned long host_cp0_epc;
  237. u32 host_cp0_cause;
  238. /* GPRS */
  239. unsigned long gprs[32];
  240. unsigned long hi;
  241. unsigned long lo;
  242. unsigned long pc;
  243. /* FPU State */
  244. struct mips_fpu_struct fpu;
  245. /* Which auxiliary state is loaded (KVM_MIPS_AUX_*) */
  246. unsigned int aux_inuse;
  247. /* COP0 State */
  248. struct mips_coproc *cop0;
  249. /* Host KSEG0 address of the EI/DI offset */
  250. void *kseg0_commpage;
  251. u32 io_gpr; /* GPR used as IO source/target */
  252. struct hrtimer comparecount_timer;
  253. /* Count timer control KVM register */
  254. u32 count_ctl;
  255. /* Count bias from the raw time */
  256. u32 count_bias;
  257. /* Frequency of timer in Hz */
  258. u32 count_hz;
  259. /* Dynamic nanosecond bias (multiple of count_period) to avoid overflow */
  260. s64 count_dyn_bias;
  261. /* Resume time */
  262. ktime_t count_resume;
  263. /* Period of timer tick in ns */
  264. u64 count_period;
  265. /* Bitmask of exceptions that are pending */
  266. unsigned long pending_exceptions;
  267. /* Bitmask of pending exceptions to be cleared */
  268. unsigned long pending_exceptions_clr;
  269. u32 pending_load_cause;
  270. /* Save/Restore the entryhi register when are are preempted/scheduled back in */
  271. unsigned long preempt_entryhi;
  272. /* S/W Based TLB for guest */
  273. struct kvm_mips_tlb guest_tlb[KVM_MIPS_GUEST_TLB_SIZE];
  274. /* Cached guest kernel/user ASIDs */
  275. u32 guest_user_asid[NR_CPUS];
  276. u32 guest_kernel_asid[NR_CPUS];
  277. struct mm_struct guest_kernel_mm, guest_user_mm;
  278. /* Guest ASID of last user mode execution */
  279. unsigned int last_user_gasid;
  280. int last_sched_cpu;
  281. /* WAIT executed */
  282. int wait;
  283. u8 fpu_enabled;
  284. u8 msa_enabled;
  285. u8 kscratch_enabled;
  286. };
  287. #define kvm_read_c0_guest_index(cop0) (cop0->reg[MIPS_CP0_TLB_INDEX][0])
  288. #define kvm_write_c0_guest_index(cop0, val) (cop0->reg[MIPS_CP0_TLB_INDEX][0] = val)
  289. #define kvm_read_c0_guest_entrylo0(cop0) (cop0->reg[MIPS_CP0_TLB_LO0][0])
  290. #define kvm_read_c0_guest_entrylo1(cop0) (cop0->reg[MIPS_CP0_TLB_LO1][0])
  291. #define kvm_read_c0_guest_context(cop0) (cop0->reg[MIPS_CP0_TLB_CONTEXT][0])
  292. #define kvm_write_c0_guest_context(cop0, val) (cop0->reg[MIPS_CP0_TLB_CONTEXT][0] = (val))
  293. #define kvm_read_c0_guest_userlocal(cop0) (cop0->reg[MIPS_CP0_TLB_CONTEXT][2])
  294. #define kvm_write_c0_guest_userlocal(cop0, val) (cop0->reg[MIPS_CP0_TLB_CONTEXT][2] = (val))
  295. #define kvm_read_c0_guest_pagemask(cop0) (cop0->reg[MIPS_CP0_TLB_PG_MASK][0])
  296. #define kvm_write_c0_guest_pagemask(cop0, val) (cop0->reg[MIPS_CP0_TLB_PG_MASK][0] = (val))
  297. #define kvm_read_c0_guest_wired(cop0) (cop0->reg[MIPS_CP0_TLB_WIRED][0])
  298. #define kvm_write_c0_guest_wired(cop0, val) (cop0->reg[MIPS_CP0_TLB_WIRED][0] = (val))
  299. #define kvm_read_c0_guest_hwrena(cop0) (cop0->reg[MIPS_CP0_HWRENA][0])
  300. #define kvm_write_c0_guest_hwrena(cop0, val) (cop0->reg[MIPS_CP0_HWRENA][0] = (val))
  301. #define kvm_read_c0_guest_badvaddr(cop0) (cop0->reg[MIPS_CP0_BAD_VADDR][0])
  302. #define kvm_write_c0_guest_badvaddr(cop0, val) (cop0->reg[MIPS_CP0_BAD_VADDR][0] = (val))
  303. #define kvm_read_c0_guest_count(cop0) (cop0->reg[MIPS_CP0_COUNT][0])
  304. #define kvm_write_c0_guest_count(cop0, val) (cop0->reg[MIPS_CP0_COUNT][0] = (val))
  305. #define kvm_read_c0_guest_entryhi(cop0) (cop0->reg[MIPS_CP0_TLB_HI][0])
  306. #define kvm_write_c0_guest_entryhi(cop0, val) (cop0->reg[MIPS_CP0_TLB_HI][0] = (val))
  307. #define kvm_read_c0_guest_compare(cop0) (cop0->reg[MIPS_CP0_COMPARE][0])
  308. #define kvm_write_c0_guest_compare(cop0, val) (cop0->reg[MIPS_CP0_COMPARE][0] = (val))
  309. #define kvm_read_c0_guest_status(cop0) (cop0->reg[MIPS_CP0_STATUS][0])
  310. #define kvm_write_c0_guest_status(cop0, val) (cop0->reg[MIPS_CP0_STATUS][0] = (val))
  311. #define kvm_read_c0_guest_intctl(cop0) (cop0->reg[MIPS_CP0_STATUS][1])
  312. #define kvm_write_c0_guest_intctl(cop0, val) (cop0->reg[MIPS_CP0_STATUS][1] = (val))
  313. #define kvm_read_c0_guest_cause(cop0) (cop0->reg[MIPS_CP0_CAUSE][0])
  314. #define kvm_write_c0_guest_cause(cop0, val) (cop0->reg[MIPS_CP0_CAUSE][0] = (val))
  315. #define kvm_read_c0_guest_epc(cop0) (cop0->reg[MIPS_CP0_EXC_PC][0])
  316. #define kvm_write_c0_guest_epc(cop0, val) (cop0->reg[MIPS_CP0_EXC_PC][0] = (val))
  317. #define kvm_read_c0_guest_prid(cop0) (cop0->reg[MIPS_CP0_PRID][0])
  318. #define kvm_write_c0_guest_prid(cop0, val) (cop0->reg[MIPS_CP0_PRID][0] = (val))
  319. #define kvm_read_c0_guest_ebase(cop0) (cop0->reg[MIPS_CP0_PRID][1])
  320. #define kvm_write_c0_guest_ebase(cop0, val) (cop0->reg[MIPS_CP0_PRID][1] = (val))
  321. #define kvm_read_c0_guest_config(cop0) (cop0->reg[MIPS_CP0_CONFIG][0])
  322. #define kvm_read_c0_guest_config1(cop0) (cop0->reg[MIPS_CP0_CONFIG][1])
  323. #define kvm_read_c0_guest_config2(cop0) (cop0->reg[MIPS_CP0_CONFIG][2])
  324. #define kvm_read_c0_guest_config3(cop0) (cop0->reg[MIPS_CP0_CONFIG][3])
  325. #define kvm_read_c0_guest_config4(cop0) (cop0->reg[MIPS_CP0_CONFIG][4])
  326. #define kvm_read_c0_guest_config5(cop0) (cop0->reg[MIPS_CP0_CONFIG][5])
  327. #define kvm_read_c0_guest_config7(cop0) (cop0->reg[MIPS_CP0_CONFIG][7])
  328. #define kvm_write_c0_guest_config(cop0, val) (cop0->reg[MIPS_CP0_CONFIG][0] = (val))
  329. #define kvm_write_c0_guest_config1(cop0, val) (cop0->reg[MIPS_CP0_CONFIG][1] = (val))
  330. #define kvm_write_c0_guest_config2(cop0, val) (cop0->reg[MIPS_CP0_CONFIG][2] = (val))
  331. #define kvm_write_c0_guest_config3(cop0, val) (cop0->reg[MIPS_CP0_CONFIG][3] = (val))
  332. #define kvm_write_c0_guest_config4(cop0, val) (cop0->reg[MIPS_CP0_CONFIG][4] = (val))
  333. #define kvm_write_c0_guest_config5(cop0, val) (cop0->reg[MIPS_CP0_CONFIG][5] = (val))
  334. #define kvm_write_c0_guest_config7(cop0, val) (cop0->reg[MIPS_CP0_CONFIG][7] = (val))
  335. #define kvm_read_c0_guest_errorepc(cop0) (cop0->reg[MIPS_CP0_ERROR_PC][0])
  336. #define kvm_write_c0_guest_errorepc(cop0, val) (cop0->reg[MIPS_CP0_ERROR_PC][0] = (val))
  337. #define kvm_read_c0_guest_kscratch1(cop0) (cop0->reg[MIPS_CP0_DESAVE][2])
  338. #define kvm_read_c0_guest_kscratch2(cop0) (cop0->reg[MIPS_CP0_DESAVE][3])
  339. #define kvm_read_c0_guest_kscratch3(cop0) (cop0->reg[MIPS_CP0_DESAVE][4])
  340. #define kvm_read_c0_guest_kscratch4(cop0) (cop0->reg[MIPS_CP0_DESAVE][5])
  341. #define kvm_read_c0_guest_kscratch5(cop0) (cop0->reg[MIPS_CP0_DESAVE][6])
  342. #define kvm_read_c0_guest_kscratch6(cop0) (cop0->reg[MIPS_CP0_DESAVE][7])
  343. #define kvm_write_c0_guest_kscratch1(cop0, val) (cop0->reg[MIPS_CP0_DESAVE][2] = (val))
  344. #define kvm_write_c0_guest_kscratch2(cop0, val) (cop0->reg[MIPS_CP0_DESAVE][3] = (val))
  345. #define kvm_write_c0_guest_kscratch3(cop0, val) (cop0->reg[MIPS_CP0_DESAVE][4] = (val))
  346. #define kvm_write_c0_guest_kscratch4(cop0, val) (cop0->reg[MIPS_CP0_DESAVE][5] = (val))
  347. #define kvm_write_c0_guest_kscratch5(cop0, val) (cop0->reg[MIPS_CP0_DESAVE][6] = (val))
  348. #define kvm_write_c0_guest_kscratch6(cop0, val) (cop0->reg[MIPS_CP0_DESAVE][7] = (val))
  349. /*
  350. * Some of the guest registers may be modified asynchronously (e.g. from a
  351. * hrtimer callback in hard irq context) and therefore need stronger atomicity
  352. * guarantees than other registers.
  353. */
  354. static inline void _kvm_atomic_set_c0_guest_reg(unsigned long *reg,
  355. unsigned long val)
  356. {
  357. unsigned long temp;
  358. do {
  359. __asm__ __volatile__(
  360. " .set "MIPS_ISA_ARCH_LEVEL" \n"
  361. " " __LL "%0, %1 \n"
  362. " or %0, %2 \n"
  363. " " __SC "%0, %1 \n"
  364. " .set mips0 \n"
  365. : "=&r" (temp), "+m" (*reg)
  366. : "r" (val));
  367. } while (unlikely(!temp));
  368. }
  369. static inline void _kvm_atomic_clear_c0_guest_reg(unsigned long *reg,
  370. unsigned long val)
  371. {
  372. unsigned long temp;
  373. do {
  374. __asm__ __volatile__(
  375. " .set "MIPS_ISA_ARCH_LEVEL" \n"
  376. " " __LL "%0, %1 \n"
  377. " and %0, %2 \n"
  378. " " __SC "%0, %1 \n"
  379. " .set mips0 \n"
  380. : "=&r" (temp), "+m" (*reg)
  381. : "r" (~val));
  382. } while (unlikely(!temp));
  383. }
  384. static inline void _kvm_atomic_change_c0_guest_reg(unsigned long *reg,
  385. unsigned long change,
  386. unsigned long val)
  387. {
  388. unsigned long temp;
  389. do {
  390. __asm__ __volatile__(
  391. " .set "MIPS_ISA_ARCH_LEVEL" \n"
  392. " " __LL "%0, %1 \n"
  393. " and %0, %2 \n"
  394. " or %0, %3 \n"
  395. " " __SC "%0, %1 \n"
  396. " .set mips0 \n"
  397. : "=&r" (temp), "+m" (*reg)
  398. : "r" (~change), "r" (val & change));
  399. } while (unlikely(!temp));
  400. }
  401. #define kvm_set_c0_guest_status(cop0, val) (cop0->reg[MIPS_CP0_STATUS][0] |= (val))
  402. #define kvm_clear_c0_guest_status(cop0, val) (cop0->reg[MIPS_CP0_STATUS][0] &= ~(val))
  403. /* Cause can be modified asynchronously from hardirq hrtimer callback */
  404. #define kvm_set_c0_guest_cause(cop0, val) \
  405. _kvm_atomic_set_c0_guest_reg(&cop0->reg[MIPS_CP0_CAUSE][0], val)
  406. #define kvm_clear_c0_guest_cause(cop0, val) \
  407. _kvm_atomic_clear_c0_guest_reg(&cop0->reg[MIPS_CP0_CAUSE][0], val)
  408. #define kvm_change_c0_guest_cause(cop0, change, val) \
  409. _kvm_atomic_change_c0_guest_reg(&cop0->reg[MIPS_CP0_CAUSE][0], \
  410. change, val)
  411. #define kvm_set_c0_guest_ebase(cop0, val) (cop0->reg[MIPS_CP0_PRID][1] |= (val))
  412. #define kvm_clear_c0_guest_ebase(cop0, val) (cop0->reg[MIPS_CP0_PRID][1] &= ~(val))
  413. #define kvm_change_c0_guest_ebase(cop0, change, val) \
  414. { \
  415. kvm_clear_c0_guest_ebase(cop0, change); \
  416. kvm_set_c0_guest_ebase(cop0, ((val) & (change))); \
  417. }
  418. /* Helpers */
  419. static inline bool kvm_mips_guest_can_have_fpu(struct kvm_vcpu_arch *vcpu)
  420. {
  421. return (!__builtin_constant_p(raw_cpu_has_fpu) || raw_cpu_has_fpu) &&
  422. vcpu->fpu_enabled;
  423. }
  424. static inline bool kvm_mips_guest_has_fpu(struct kvm_vcpu_arch *vcpu)
  425. {
  426. return kvm_mips_guest_can_have_fpu(vcpu) &&
  427. kvm_read_c0_guest_config1(vcpu->cop0) & MIPS_CONF1_FP;
  428. }
  429. static inline bool kvm_mips_guest_can_have_msa(struct kvm_vcpu_arch *vcpu)
  430. {
  431. return (!__builtin_constant_p(cpu_has_msa) || cpu_has_msa) &&
  432. vcpu->msa_enabled;
  433. }
  434. static inline bool kvm_mips_guest_has_msa(struct kvm_vcpu_arch *vcpu)
  435. {
  436. return kvm_mips_guest_can_have_msa(vcpu) &&
  437. kvm_read_c0_guest_config3(vcpu->cop0) & MIPS_CONF3_MSA;
  438. }
  439. struct kvm_mips_callbacks {
  440. int (*handle_cop_unusable)(struct kvm_vcpu *vcpu);
  441. int (*handle_tlb_mod)(struct kvm_vcpu *vcpu);
  442. int (*handle_tlb_ld_miss)(struct kvm_vcpu *vcpu);
  443. int (*handle_tlb_st_miss)(struct kvm_vcpu *vcpu);
  444. int (*handle_addr_err_st)(struct kvm_vcpu *vcpu);
  445. int (*handle_addr_err_ld)(struct kvm_vcpu *vcpu);
  446. int (*handle_syscall)(struct kvm_vcpu *vcpu);
  447. int (*handle_res_inst)(struct kvm_vcpu *vcpu);
  448. int (*handle_break)(struct kvm_vcpu *vcpu);
  449. int (*handle_trap)(struct kvm_vcpu *vcpu);
  450. int (*handle_msa_fpe)(struct kvm_vcpu *vcpu);
  451. int (*handle_fpe)(struct kvm_vcpu *vcpu);
  452. int (*handle_msa_disabled)(struct kvm_vcpu *vcpu);
  453. int (*vm_init)(struct kvm *kvm);
  454. int (*vcpu_init)(struct kvm_vcpu *vcpu);
  455. int (*vcpu_setup)(struct kvm_vcpu *vcpu);
  456. gpa_t (*gva_to_gpa)(gva_t gva);
  457. void (*queue_timer_int)(struct kvm_vcpu *vcpu);
  458. void (*dequeue_timer_int)(struct kvm_vcpu *vcpu);
  459. void (*queue_io_int)(struct kvm_vcpu *vcpu,
  460. struct kvm_mips_interrupt *irq);
  461. void (*dequeue_io_int)(struct kvm_vcpu *vcpu,
  462. struct kvm_mips_interrupt *irq);
  463. int (*irq_deliver)(struct kvm_vcpu *vcpu, unsigned int priority,
  464. u32 cause);
  465. int (*irq_clear)(struct kvm_vcpu *vcpu, unsigned int priority,
  466. u32 cause);
  467. unsigned long (*num_regs)(struct kvm_vcpu *vcpu);
  468. int (*copy_reg_indices)(struct kvm_vcpu *vcpu, u64 __user *indices);
  469. int (*get_one_reg)(struct kvm_vcpu *vcpu,
  470. const struct kvm_one_reg *reg, s64 *v);
  471. int (*set_one_reg)(struct kvm_vcpu *vcpu,
  472. const struct kvm_one_reg *reg, s64 v);
  473. int (*vcpu_get_regs)(struct kvm_vcpu *vcpu);
  474. int (*vcpu_set_regs)(struct kvm_vcpu *vcpu);
  475. };
  476. extern struct kvm_mips_callbacks *kvm_mips_callbacks;
  477. int kvm_mips_emulation_init(struct kvm_mips_callbacks **install_callbacks);
  478. /* Debug: dump vcpu state */
  479. int kvm_arch_vcpu_dump_regs(struct kvm_vcpu *vcpu);
  480. extern int kvm_mips_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu);
  481. /* Building of entry/exception code */
  482. int kvm_mips_entry_setup(void);
  483. void *kvm_mips_build_vcpu_run(void *addr);
  484. void *kvm_mips_build_exception(void *addr, void *handler);
  485. void *kvm_mips_build_exit(void *addr);
  486. /* FPU/MSA context management */
  487. void __kvm_save_fpu(struct kvm_vcpu_arch *vcpu);
  488. void __kvm_restore_fpu(struct kvm_vcpu_arch *vcpu);
  489. void __kvm_restore_fcsr(struct kvm_vcpu_arch *vcpu);
  490. void __kvm_save_msa(struct kvm_vcpu_arch *vcpu);
  491. void __kvm_restore_msa(struct kvm_vcpu_arch *vcpu);
  492. void __kvm_restore_msa_upper(struct kvm_vcpu_arch *vcpu);
  493. void __kvm_restore_msacsr(struct kvm_vcpu_arch *vcpu);
  494. void kvm_own_fpu(struct kvm_vcpu *vcpu);
  495. void kvm_own_msa(struct kvm_vcpu *vcpu);
  496. void kvm_drop_fpu(struct kvm_vcpu *vcpu);
  497. void kvm_lose_fpu(struct kvm_vcpu *vcpu);
  498. /* TLB handling */
  499. u32 kvm_get_kernel_asid(struct kvm_vcpu *vcpu);
  500. u32 kvm_get_user_asid(struct kvm_vcpu *vcpu);
  501. u32 kvm_get_commpage_asid (struct kvm_vcpu *vcpu);
  502. extern int kvm_mips_handle_kseg0_tlb_fault(unsigned long badbaddr,
  503. struct kvm_vcpu *vcpu);
  504. extern int kvm_mips_handle_commpage_tlb_fault(unsigned long badvaddr,
  505. struct kvm_vcpu *vcpu);
  506. extern int kvm_mips_handle_mapped_seg_tlb_fault(struct kvm_vcpu *vcpu,
  507. struct kvm_mips_tlb *tlb);
  508. extern enum emulation_result kvm_mips_handle_tlbmiss(u32 cause,
  509. u32 *opc,
  510. struct kvm_run *run,
  511. struct kvm_vcpu *vcpu);
  512. extern enum emulation_result kvm_mips_handle_tlbmod(u32 cause,
  513. u32 *opc,
  514. struct kvm_run *run,
  515. struct kvm_vcpu *vcpu);
  516. extern void kvm_mips_dump_host_tlbs(void);
  517. extern void kvm_mips_dump_guest_tlbs(struct kvm_vcpu *vcpu);
  518. extern int kvm_mips_host_tlb_write(struct kvm_vcpu *vcpu, unsigned long entryhi,
  519. unsigned long entrylo0,
  520. unsigned long entrylo1,
  521. int flush_dcache_mask);
  522. extern void kvm_mips_flush_host_tlb(int skip_kseg0);
  523. extern int kvm_mips_host_tlb_inv(struct kvm_vcpu *vcpu, unsigned long entryhi);
  524. extern int kvm_mips_guest_tlb_lookup(struct kvm_vcpu *vcpu,
  525. unsigned long entryhi);
  526. extern int kvm_mips_host_tlb_lookup(struct kvm_vcpu *vcpu, unsigned long vaddr);
  527. extern unsigned long kvm_mips_translate_guest_kseg0_to_hpa(struct kvm_vcpu *vcpu,
  528. unsigned long gva);
  529. extern void kvm_get_new_mmu_context(struct mm_struct *mm, unsigned long cpu,
  530. struct kvm_vcpu *vcpu);
  531. extern void kvm_local_flush_tlb_all(void);
  532. extern void kvm_mips_alloc_new_mmu_context(struct kvm_vcpu *vcpu);
  533. extern void kvm_mips_vcpu_load(struct kvm_vcpu *vcpu, int cpu);
  534. extern void kvm_mips_vcpu_put(struct kvm_vcpu *vcpu);
  535. /* Emulation */
  536. u32 kvm_get_inst(u32 *opc, struct kvm_vcpu *vcpu);
  537. enum emulation_result update_pc(struct kvm_vcpu *vcpu, u32 cause);
  538. extern enum emulation_result kvm_mips_emulate_inst(u32 cause,
  539. u32 *opc,
  540. struct kvm_run *run,
  541. struct kvm_vcpu *vcpu);
  542. extern enum emulation_result kvm_mips_emulate_syscall(u32 cause,
  543. u32 *opc,
  544. struct kvm_run *run,
  545. struct kvm_vcpu *vcpu);
  546. extern enum emulation_result kvm_mips_emulate_tlbmiss_ld(u32 cause,
  547. u32 *opc,
  548. struct kvm_run *run,
  549. struct kvm_vcpu *vcpu);
  550. extern enum emulation_result kvm_mips_emulate_tlbinv_ld(u32 cause,
  551. u32 *opc,
  552. struct kvm_run *run,
  553. struct kvm_vcpu *vcpu);
  554. extern enum emulation_result kvm_mips_emulate_tlbmiss_st(u32 cause,
  555. u32 *opc,
  556. struct kvm_run *run,
  557. struct kvm_vcpu *vcpu);
  558. extern enum emulation_result kvm_mips_emulate_tlbinv_st(u32 cause,
  559. u32 *opc,
  560. struct kvm_run *run,
  561. struct kvm_vcpu *vcpu);
  562. extern enum emulation_result kvm_mips_emulate_tlbmod(u32 cause,
  563. u32 *opc,
  564. struct kvm_run *run,
  565. struct kvm_vcpu *vcpu);
  566. extern enum emulation_result kvm_mips_emulate_fpu_exc(u32 cause,
  567. u32 *opc,
  568. struct kvm_run *run,
  569. struct kvm_vcpu *vcpu);
  570. extern enum emulation_result kvm_mips_handle_ri(u32 cause,
  571. u32 *opc,
  572. struct kvm_run *run,
  573. struct kvm_vcpu *vcpu);
  574. extern enum emulation_result kvm_mips_emulate_ri_exc(u32 cause,
  575. u32 *opc,
  576. struct kvm_run *run,
  577. struct kvm_vcpu *vcpu);
  578. extern enum emulation_result kvm_mips_emulate_bp_exc(u32 cause,
  579. u32 *opc,
  580. struct kvm_run *run,
  581. struct kvm_vcpu *vcpu);
  582. extern enum emulation_result kvm_mips_emulate_trap_exc(u32 cause,
  583. u32 *opc,
  584. struct kvm_run *run,
  585. struct kvm_vcpu *vcpu);
  586. extern enum emulation_result kvm_mips_emulate_msafpe_exc(u32 cause,
  587. u32 *opc,
  588. struct kvm_run *run,
  589. struct kvm_vcpu *vcpu);
  590. extern enum emulation_result kvm_mips_emulate_fpe_exc(u32 cause,
  591. u32 *opc,
  592. struct kvm_run *run,
  593. struct kvm_vcpu *vcpu);
  594. extern enum emulation_result kvm_mips_emulate_msadis_exc(u32 cause,
  595. u32 *opc,
  596. struct kvm_run *run,
  597. struct kvm_vcpu *vcpu);
  598. extern enum emulation_result kvm_mips_complete_mmio_load(struct kvm_vcpu *vcpu,
  599. struct kvm_run *run);
  600. u32 kvm_mips_read_count(struct kvm_vcpu *vcpu);
  601. void kvm_mips_write_count(struct kvm_vcpu *vcpu, u32 count);
  602. void kvm_mips_write_compare(struct kvm_vcpu *vcpu, u32 compare, bool ack);
  603. void kvm_mips_init_count(struct kvm_vcpu *vcpu);
  604. int kvm_mips_set_count_ctl(struct kvm_vcpu *vcpu, s64 count_ctl);
  605. int kvm_mips_set_count_resume(struct kvm_vcpu *vcpu, s64 count_resume);
  606. int kvm_mips_set_count_hz(struct kvm_vcpu *vcpu, s64 count_hz);
  607. void kvm_mips_count_enable_cause(struct kvm_vcpu *vcpu);
  608. void kvm_mips_count_disable_cause(struct kvm_vcpu *vcpu);
  609. enum hrtimer_restart kvm_mips_count_timeout(struct kvm_vcpu *vcpu);
  610. enum emulation_result kvm_mips_check_privilege(u32 cause,
  611. u32 *opc,
  612. struct kvm_run *run,
  613. struct kvm_vcpu *vcpu);
  614. enum emulation_result kvm_mips_emulate_cache(union mips_instruction inst,
  615. u32 *opc,
  616. u32 cause,
  617. struct kvm_run *run,
  618. struct kvm_vcpu *vcpu);
  619. enum emulation_result kvm_mips_emulate_CP0(union mips_instruction inst,
  620. u32 *opc,
  621. u32 cause,
  622. struct kvm_run *run,
  623. struct kvm_vcpu *vcpu);
  624. enum emulation_result kvm_mips_emulate_store(union mips_instruction inst,
  625. u32 cause,
  626. struct kvm_run *run,
  627. struct kvm_vcpu *vcpu);
  628. enum emulation_result kvm_mips_emulate_load(union mips_instruction inst,
  629. u32 cause,
  630. struct kvm_run *run,
  631. struct kvm_vcpu *vcpu);
  632. unsigned int kvm_mips_config1_wrmask(struct kvm_vcpu *vcpu);
  633. unsigned int kvm_mips_config3_wrmask(struct kvm_vcpu *vcpu);
  634. unsigned int kvm_mips_config4_wrmask(struct kvm_vcpu *vcpu);
  635. unsigned int kvm_mips_config5_wrmask(struct kvm_vcpu *vcpu);
  636. /* Dynamic binary translation */
  637. extern int kvm_mips_trans_cache_index(union mips_instruction inst,
  638. u32 *opc, struct kvm_vcpu *vcpu);
  639. extern int kvm_mips_trans_cache_va(union mips_instruction inst, u32 *opc,
  640. struct kvm_vcpu *vcpu);
  641. extern int kvm_mips_trans_mfc0(union mips_instruction inst, u32 *opc,
  642. struct kvm_vcpu *vcpu);
  643. extern int kvm_mips_trans_mtc0(union mips_instruction inst, u32 *opc,
  644. struct kvm_vcpu *vcpu);
  645. /* Misc */
  646. extern void kvm_mips_dump_stats(struct kvm_vcpu *vcpu);
  647. extern unsigned long kvm_mips_get_ramsize(struct kvm *kvm);
  648. static inline void kvm_arch_hardware_disable(void) {}
  649. static inline void kvm_arch_hardware_unsetup(void) {}
  650. static inline void kvm_arch_sync_events(struct kvm *kvm) {}
  651. static inline void kvm_arch_free_memslot(struct kvm *kvm,
  652. struct kvm_memory_slot *free, struct kvm_memory_slot *dont) {}
  653. static inline void kvm_arch_memslots_updated(struct kvm *kvm, struct kvm_memslots *slots) {}
  654. static inline void kvm_arch_flush_shadow_all(struct kvm *kvm) {}
  655. static inline void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
  656. struct kvm_memory_slot *slot) {}
  657. static inline void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu) {}
  658. static inline void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu) {}
  659. static inline void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu) {}
  660. static inline void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu) {}
  661. static inline void kvm_arch_vcpu_block_finish(struct kvm_vcpu *vcpu) {}
  662. #endif /* __MIPS_KVM_HOST_H__ */