kvm_host.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  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. /* Resume PC after MMIO completion */
  252. unsigned long io_pc;
  253. /* GPR used as IO source/target */
  254. u32 io_gpr;
  255. struct hrtimer comparecount_timer;
  256. /* Count timer control KVM register */
  257. u32 count_ctl;
  258. /* Count bias from the raw time */
  259. u32 count_bias;
  260. /* Frequency of timer in Hz */
  261. u32 count_hz;
  262. /* Dynamic nanosecond bias (multiple of count_period) to avoid overflow */
  263. s64 count_dyn_bias;
  264. /* Resume time */
  265. ktime_t count_resume;
  266. /* Period of timer tick in ns */
  267. u64 count_period;
  268. /* Bitmask of exceptions that are pending */
  269. unsigned long pending_exceptions;
  270. /* Bitmask of pending exceptions to be cleared */
  271. unsigned long pending_exceptions_clr;
  272. /* Save/Restore the entryhi register when are are preempted/scheduled back in */
  273. unsigned long preempt_entryhi;
  274. /* S/W Based TLB for guest */
  275. struct kvm_mips_tlb guest_tlb[KVM_MIPS_GUEST_TLB_SIZE];
  276. /* Cached guest kernel/user ASIDs */
  277. u32 guest_user_asid[NR_CPUS];
  278. u32 guest_kernel_asid[NR_CPUS];
  279. struct mm_struct guest_kernel_mm, guest_user_mm;
  280. /* Guest ASID of last user mode execution */
  281. unsigned int last_user_gasid;
  282. int last_sched_cpu;
  283. /* WAIT executed */
  284. int wait;
  285. u8 fpu_enabled;
  286. u8 msa_enabled;
  287. u8 kscratch_enabled;
  288. };
  289. #define kvm_read_c0_guest_index(cop0) (cop0->reg[MIPS_CP0_TLB_INDEX][0])
  290. #define kvm_write_c0_guest_index(cop0, val) (cop0->reg[MIPS_CP0_TLB_INDEX][0] = val)
  291. #define kvm_read_c0_guest_entrylo0(cop0) (cop0->reg[MIPS_CP0_TLB_LO0][0])
  292. #define kvm_read_c0_guest_entrylo1(cop0) (cop0->reg[MIPS_CP0_TLB_LO1][0])
  293. #define kvm_read_c0_guest_context(cop0) (cop0->reg[MIPS_CP0_TLB_CONTEXT][0])
  294. #define kvm_write_c0_guest_context(cop0, val) (cop0->reg[MIPS_CP0_TLB_CONTEXT][0] = (val))
  295. #define kvm_read_c0_guest_userlocal(cop0) (cop0->reg[MIPS_CP0_TLB_CONTEXT][2])
  296. #define kvm_write_c0_guest_userlocal(cop0, val) (cop0->reg[MIPS_CP0_TLB_CONTEXT][2] = (val))
  297. #define kvm_read_c0_guest_pagemask(cop0) (cop0->reg[MIPS_CP0_TLB_PG_MASK][0])
  298. #define kvm_write_c0_guest_pagemask(cop0, val) (cop0->reg[MIPS_CP0_TLB_PG_MASK][0] = (val))
  299. #define kvm_read_c0_guest_wired(cop0) (cop0->reg[MIPS_CP0_TLB_WIRED][0])
  300. #define kvm_write_c0_guest_wired(cop0, val) (cop0->reg[MIPS_CP0_TLB_WIRED][0] = (val))
  301. #define kvm_read_c0_guest_hwrena(cop0) (cop0->reg[MIPS_CP0_HWRENA][0])
  302. #define kvm_write_c0_guest_hwrena(cop0, val) (cop0->reg[MIPS_CP0_HWRENA][0] = (val))
  303. #define kvm_read_c0_guest_badvaddr(cop0) (cop0->reg[MIPS_CP0_BAD_VADDR][0])
  304. #define kvm_write_c0_guest_badvaddr(cop0, val) (cop0->reg[MIPS_CP0_BAD_VADDR][0] = (val))
  305. #define kvm_read_c0_guest_count(cop0) (cop0->reg[MIPS_CP0_COUNT][0])
  306. #define kvm_write_c0_guest_count(cop0, val) (cop0->reg[MIPS_CP0_COUNT][0] = (val))
  307. #define kvm_read_c0_guest_entryhi(cop0) (cop0->reg[MIPS_CP0_TLB_HI][0])
  308. #define kvm_write_c0_guest_entryhi(cop0, val) (cop0->reg[MIPS_CP0_TLB_HI][0] = (val))
  309. #define kvm_read_c0_guest_compare(cop0) (cop0->reg[MIPS_CP0_COMPARE][0])
  310. #define kvm_write_c0_guest_compare(cop0, val) (cop0->reg[MIPS_CP0_COMPARE][0] = (val))
  311. #define kvm_read_c0_guest_status(cop0) (cop0->reg[MIPS_CP0_STATUS][0])
  312. #define kvm_write_c0_guest_status(cop0, val) (cop0->reg[MIPS_CP0_STATUS][0] = (val))
  313. #define kvm_read_c0_guest_intctl(cop0) (cop0->reg[MIPS_CP0_STATUS][1])
  314. #define kvm_write_c0_guest_intctl(cop0, val) (cop0->reg[MIPS_CP0_STATUS][1] = (val))
  315. #define kvm_read_c0_guest_cause(cop0) (cop0->reg[MIPS_CP0_CAUSE][0])
  316. #define kvm_write_c0_guest_cause(cop0, val) (cop0->reg[MIPS_CP0_CAUSE][0] = (val))
  317. #define kvm_read_c0_guest_epc(cop0) (cop0->reg[MIPS_CP0_EXC_PC][0])
  318. #define kvm_write_c0_guest_epc(cop0, val) (cop0->reg[MIPS_CP0_EXC_PC][0] = (val))
  319. #define kvm_read_c0_guest_prid(cop0) (cop0->reg[MIPS_CP0_PRID][0])
  320. #define kvm_write_c0_guest_prid(cop0, val) (cop0->reg[MIPS_CP0_PRID][0] = (val))
  321. #define kvm_read_c0_guest_ebase(cop0) (cop0->reg[MIPS_CP0_PRID][1])
  322. #define kvm_write_c0_guest_ebase(cop0, val) (cop0->reg[MIPS_CP0_PRID][1] = (val))
  323. #define kvm_read_c0_guest_config(cop0) (cop0->reg[MIPS_CP0_CONFIG][0])
  324. #define kvm_read_c0_guest_config1(cop0) (cop0->reg[MIPS_CP0_CONFIG][1])
  325. #define kvm_read_c0_guest_config2(cop0) (cop0->reg[MIPS_CP0_CONFIG][2])
  326. #define kvm_read_c0_guest_config3(cop0) (cop0->reg[MIPS_CP0_CONFIG][3])
  327. #define kvm_read_c0_guest_config4(cop0) (cop0->reg[MIPS_CP0_CONFIG][4])
  328. #define kvm_read_c0_guest_config5(cop0) (cop0->reg[MIPS_CP0_CONFIG][5])
  329. #define kvm_read_c0_guest_config7(cop0) (cop0->reg[MIPS_CP0_CONFIG][7])
  330. #define kvm_write_c0_guest_config(cop0, val) (cop0->reg[MIPS_CP0_CONFIG][0] = (val))
  331. #define kvm_write_c0_guest_config1(cop0, val) (cop0->reg[MIPS_CP0_CONFIG][1] = (val))
  332. #define kvm_write_c0_guest_config2(cop0, val) (cop0->reg[MIPS_CP0_CONFIG][2] = (val))
  333. #define kvm_write_c0_guest_config3(cop0, val) (cop0->reg[MIPS_CP0_CONFIG][3] = (val))
  334. #define kvm_write_c0_guest_config4(cop0, val) (cop0->reg[MIPS_CP0_CONFIG][4] = (val))
  335. #define kvm_write_c0_guest_config5(cop0, val) (cop0->reg[MIPS_CP0_CONFIG][5] = (val))
  336. #define kvm_write_c0_guest_config7(cop0, val) (cop0->reg[MIPS_CP0_CONFIG][7] = (val))
  337. #define kvm_read_c0_guest_errorepc(cop0) (cop0->reg[MIPS_CP0_ERROR_PC][0])
  338. #define kvm_write_c0_guest_errorepc(cop0, val) (cop0->reg[MIPS_CP0_ERROR_PC][0] = (val))
  339. #define kvm_read_c0_guest_kscratch1(cop0) (cop0->reg[MIPS_CP0_DESAVE][2])
  340. #define kvm_read_c0_guest_kscratch2(cop0) (cop0->reg[MIPS_CP0_DESAVE][3])
  341. #define kvm_read_c0_guest_kscratch3(cop0) (cop0->reg[MIPS_CP0_DESAVE][4])
  342. #define kvm_read_c0_guest_kscratch4(cop0) (cop0->reg[MIPS_CP0_DESAVE][5])
  343. #define kvm_read_c0_guest_kscratch5(cop0) (cop0->reg[MIPS_CP0_DESAVE][6])
  344. #define kvm_read_c0_guest_kscratch6(cop0) (cop0->reg[MIPS_CP0_DESAVE][7])
  345. #define kvm_write_c0_guest_kscratch1(cop0, val) (cop0->reg[MIPS_CP0_DESAVE][2] = (val))
  346. #define kvm_write_c0_guest_kscratch2(cop0, val) (cop0->reg[MIPS_CP0_DESAVE][3] = (val))
  347. #define kvm_write_c0_guest_kscratch3(cop0, val) (cop0->reg[MIPS_CP0_DESAVE][4] = (val))
  348. #define kvm_write_c0_guest_kscratch4(cop0, val) (cop0->reg[MIPS_CP0_DESAVE][5] = (val))
  349. #define kvm_write_c0_guest_kscratch5(cop0, val) (cop0->reg[MIPS_CP0_DESAVE][6] = (val))
  350. #define kvm_write_c0_guest_kscratch6(cop0, val) (cop0->reg[MIPS_CP0_DESAVE][7] = (val))
  351. /*
  352. * Some of the guest registers may be modified asynchronously (e.g. from a
  353. * hrtimer callback in hard irq context) and therefore need stronger atomicity
  354. * guarantees than other registers.
  355. */
  356. static inline void _kvm_atomic_set_c0_guest_reg(unsigned long *reg,
  357. unsigned long val)
  358. {
  359. unsigned long temp;
  360. do {
  361. __asm__ __volatile__(
  362. " .set "MIPS_ISA_ARCH_LEVEL" \n"
  363. " " __LL "%0, %1 \n"
  364. " or %0, %2 \n"
  365. " " __SC "%0, %1 \n"
  366. " .set mips0 \n"
  367. : "=&r" (temp), "+m" (*reg)
  368. : "r" (val));
  369. } while (unlikely(!temp));
  370. }
  371. static inline void _kvm_atomic_clear_c0_guest_reg(unsigned long *reg,
  372. unsigned long val)
  373. {
  374. unsigned long temp;
  375. do {
  376. __asm__ __volatile__(
  377. " .set "MIPS_ISA_ARCH_LEVEL" \n"
  378. " " __LL "%0, %1 \n"
  379. " and %0, %2 \n"
  380. " " __SC "%0, %1 \n"
  381. " .set mips0 \n"
  382. : "=&r" (temp), "+m" (*reg)
  383. : "r" (~val));
  384. } while (unlikely(!temp));
  385. }
  386. static inline void _kvm_atomic_change_c0_guest_reg(unsigned long *reg,
  387. unsigned long change,
  388. unsigned long val)
  389. {
  390. unsigned long temp;
  391. do {
  392. __asm__ __volatile__(
  393. " .set "MIPS_ISA_ARCH_LEVEL" \n"
  394. " " __LL "%0, %1 \n"
  395. " and %0, %2 \n"
  396. " or %0, %3 \n"
  397. " " __SC "%0, %1 \n"
  398. " .set mips0 \n"
  399. : "=&r" (temp), "+m" (*reg)
  400. : "r" (~change), "r" (val & change));
  401. } while (unlikely(!temp));
  402. }
  403. #define kvm_set_c0_guest_status(cop0, val) (cop0->reg[MIPS_CP0_STATUS][0] |= (val))
  404. #define kvm_clear_c0_guest_status(cop0, val) (cop0->reg[MIPS_CP0_STATUS][0] &= ~(val))
  405. /* Cause can be modified asynchronously from hardirq hrtimer callback */
  406. #define kvm_set_c0_guest_cause(cop0, val) \
  407. _kvm_atomic_set_c0_guest_reg(&cop0->reg[MIPS_CP0_CAUSE][0], val)
  408. #define kvm_clear_c0_guest_cause(cop0, val) \
  409. _kvm_atomic_clear_c0_guest_reg(&cop0->reg[MIPS_CP0_CAUSE][0], val)
  410. #define kvm_change_c0_guest_cause(cop0, change, val) \
  411. _kvm_atomic_change_c0_guest_reg(&cop0->reg[MIPS_CP0_CAUSE][0], \
  412. change, val)
  413. #define kvm_set_c0_guest_ebase(cop0, val) (cop0->reg[MIPS_CP0_PRID][1] |= (val))
  414. #define kvm_clear_c0_guest_ebase(cop0, val) (cop0->reg[MIPS_CP0_PRID][1] &= ~(val))
  415. #define kvm_change_c0_guest_ebase(cop0, change, val) \
  416. { \
  417. kvm_clear_c0_guest_ebase(cop0, change); \
  418. kvm_set_c0_guest_ebase(cop0, ((val) & (change))); \
  419. }
  420. /* Helpers */
  421. static inline bool kvm_mips_guest_can_have_fpu(struct kvm_vcpu_arch *vcpu)
  422. {
  423. return (!__builtin_constant_p(raw_cpu_has_fpu) || raw_cpu_has_fpu) &&
  424. vcpu->fpu_enabled;
  425. }
  426. static inline bool kvm_mips_guest_has_fpu(struct kvm_vcpu_arch *vcpu)
  427. {
  428. return kvm_mips_guest_can_have_fpu(vcpu) &&
  429. kvm_read_c0_guest_config1(vcpu->cop0) & MIPS_CONF1_FP;
  430. }
  431. static inline bool kvm_mips_guest_can_have_msa(struct kvm_vcpu_arch *vcpu)
  432. {
  433. return (!__builtin_constant_p(cpu_has_msa) || cpu_has_msa) &&
  434. vcpu->msa_enabled;
  435. }
  436. static inline bool kvm_mips_guest_has_msa(struct kvm_vcpu_arch *vcpu)
  437. {
  438. return kvm_mips_guest_can_have_msa(vcpu) &&
  439. kvm_read_c0_guest_config3(vcpu->cop0) & MIPS_CONF3_MSA;
  440. }
  441. struct kvm_mips_callbacks {
  442. int (*handle_cop_unusable)(struct kvm_vcpu *vcpu);
  443. int (*handle_tlb_mod)(struct kvm_vcpu *vcpu);
  444. int (*handle_tlb_ld_miss)(struct kvm_vcpu *vcpu);
  445. int (*handle_tlb_st_miss)(struct kvm_vcpu *vcpu);
  446. int (*handle_addr_err_st)(struct kvm_vcpu *vcpu);
  447. int (*handle_addr_err_ld)(struct kvm_vcpu *vcpu);
  448. int (*handle_syscall)(struct kvm_vcpu *vcpu);
  449. int (*handle_res_inst)(struct kvm_vcpu *vcpu);
  450. int (*handle_break)(struct kvm_vcpu *vcpu);
  451. int (*handle_trap)(struct kvm_vcpu *vcpu);
  452. int (*handle_msa_fpe)(struct kvm_vcpu *vcpu);
  453. int (*handle_fpe)(struct kvm_vcpu *vcpu);
  454. int (*handle_msa_disabled)(struct kvm_vcpu *vcpu);
  455. int (*vm_init)(struct kvm *kvm);
  456. int (*vcpu_init)(struct kvm_vcpu *vcpu);
  457. int (*vcpu_setup)(struct kvm_vcpu *vcpu);
  458. gpa_t (*gva_to_gpa)(gva_t gva);
  459. void (*queue_timer_int)(struct kvm_vcpu *vcpu);
  460. void (*dequeue_timer_int)(struct kvm_vcpu *vcpu);
  461. void (*queue_io_int)(struct kvm_vcpu *vcpu,
  462. struct kvm_mips_interrupt *irq);
  463. void (*dequeue_io_int)(struct kvm_vcpu *vcpu,
  464. struct kvm_mips_interrupt *irq);
  465. int (*irq_deliver)(struct kvm_vcpu *vcpu, unsigned int priority,
  466. u32 cause);
  467. int (*irq_clear)(struct kvm_vcpu *vcpu, unsigned int priority,
  468. u32 cause);
  469. unsigned long (*num_regs)(struct kvm_vcpu *vcpu);
  470. int (*copy_reg_indices)(struct kvm_vcpu *vcpu, u64 __user *indices);
  471. int (*get_one_reg)(struct kvm_vcpu *vcpu,
  472. const struct kvm_one_reg *reg, s64 *v);
  473. int (*set_one_reg)(struct kvm_vcpu *vcpu,
  474. const struct kvm_one_reg *reg, s64 v);
  475. int (*vcpu_get_regs)(struct kvm_vcpu *vcpu);
  476. int (*vcpu_set_regs)(struct kvm_vcpu *vcpu);
  477. };
  478. extern struct kvm_mips_callbacks *kvm_mips_callbacks;
  479. int kvm_mips_emulation_init(struct kvm_mips_callbacks **install_callbacks);
  480. /* Debug: dump vcpu state */
  481. int kvm_arch_vcpu_dump_regs(struct kvm_vcpu *vcpu);
  482. extern int kvm_mips_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu);
  483. /* Building of entry/exception code */
  484. int kvm_mips_entry_setup(void);
  485. void *kvm_mips_build_vcpu_run(void *addr);
  486. void *kvm_mips_build_exception(void *addr, void *handler);
  487. void *kvm_mips_build_exit(void *addr);
  488. /* FPU/MSA context management */
  489. void __kvm_save_fpu(struct kvm_vcpu_arch *vcpu);
  490. void __kvm_restore_fpu(struct kvm_vcpu_arch *vcpu);
  491. void __kvm_restore_fcsr(struct kvm_vcpu_arch *vcpu);
  492. void __kvm_save_msa(struct kvm_vcpu_arch *vcpu);
  493. void __kvm_restore_msa(struct kvm_vcpu_arch *vcpu);
  494. void __kvm_restore_msa_upper(struct kvm_vcpu_arch *vcpu);
  495. void __kvm_restore_msacsr(struct kvm_vcpu_arch *vcpu);
  496. void kvm_own_fpu(struct kvm_vcpu *vcpu);
  497. void kvm_own_msa(struct kvm_vcpu *vcpu);
  498. void kvm_drop_fpu(struct kvm_vcpu *vcpu);
  499. void kvm_lose_fpu(struct kvm_vcpu *vcpu);
  500. /* TLB handling */
  501. u32 kvm_get_kernel_asid(struct kvm_vcpu *vcpu);
  502. u32 kvm_get_user_asid(struct kvm_vcpu *vcpu);
  503. u32 kvm_get_commpage_asid (struct kvm_vcpu *vcpu);
  504. extern int kvm_mips_handle_kseg0_tlb_fault(unsigned long badbaddr,
  505. struct kvm_vcpu *vcpu);
  506. extern int kvm_mips_handle_commpage_tlb_fault(unsigned long badvaddr,
  507. struct kvm_vcpu *vcpu);
  508. extern int kvm_mips_handle_mapped_seg_tlb_fault(struct kvm_vcpu *vcpu,
  509. struct kvm_mips_tlb *tlb);
  510. extern enum emulation_result kvm_mips_handle_tlbmiss(u32 cause,
  511. u32 *opc,
  512. struct kvm_run *run,
  513. struct kvm_vcpu *vcpu);
  514. extern enum emulation_result kvm_mips_handle_tlbmod(u32 cause,
  515. u32 *opc,
  516. struct kvm_run *run,
  517. struct kvm_vcpu *vcpu);
  518. extern void kvm_mips_dump_host_tlbs(void);
  519. extern void kvm_mips_dump_guest_tlbs(struct kvm_vcpu *vcpu);
  520. extern int kvm_mips_host_tlb_write(struct kvm_vcpu *vcpu, unsigned long entryhi,
  521. unsigned long entrylo0,
  522. unsigned long entrylo1,
  523. int flush_dcache_mask);
  524. extern void kvm_mips_flush_host_tlb(int skip_kseg0);
  525. extern int kvm_mips_host_tlb_inv(struct kvm_vcpu *vcpu, unsigned long entryhi);
  526. extern int kvm_mips_guest_tlb_lookup(struct kvm_vcpu *vcpu,
  527. unsigned long entryhi);
  528. extern int kvm_mips_host_tlb_lookup(struct kvm_vcpu *vcpu, unsigned long vaddr);
  529. extern unsigned long kvm_mips_translate_guest_kseg0_to_hpa(struct kvm_vcpu *vcpu,
  530. unsigned long gva);
  531. extern void kvm_get_new_mmu_context(struct mm_struct *mm, unsigned long cpu,
  532. struct kvm_vcpu *vcpu);
  533. extern void kvm_local_flush_tlb_all(void);
  534. extern void kvm_mips_alloc_new_mmu_context(struct kvm_vcpu *vcpu);
  535. extern void kvm_mips_vcpu_load(struct kvm_vcpu *vcpu, int cpu);
  536. extern void kvm_mips_vcpu_put(struct kvm_vcpu *vcpu);
  537. /* Emulation */
  538. u32 kvm_get_inst(u32 *opc, struct kvm_vcpu *vcpu);
  539. enum emulation_result update_pc(struct kvm_vcpu *vcpu, u32 cause);
  540. extern enum emulation_result kvm_mips_emulate_inst(u32 cause,
  541. u32 *opc,
  542. struct kvm_run *run,
  543. struct kvm_vcpu *vcpu);
  544. extern enum emulation_result kvm_mips_emulate_syscall(u32 cause,
  545. u32 *opc,
  546. struct kvm_run *run,
  547. struct kvm_vcpu *vcpu);
  548. extern enum emulation_result kvm_mips_emulate_tlbmiss_ld(u32 cause,
  549. u32 *opc,
  550. struct kvm_run *run,
  551. struct kvm_vcpu *vcpu);
  552. extern enum emulation_result kvm_mips_emulate_tlbinv_ld(u32 cause,
  553. u32 *opc,
  554. struct kvm_run *run,
  555. struct kvm_vcpu *vcpu);
  556. extern enum emulation_result kvm_mips_emulate_tlbmiss_st(u32 cause,
  557. u32 *opc,
  558. struct kvm_run *run,
  559. struct kvm_vcpu *vcpu);
  560. extern enum emulation_result kvm_mips_emulate_tlbinv_st(u32 cause,
  561. u32 *opc,
  562. struct kvm_run *run,
  563. struct kvm_vcpu *vcpu);
  564. extern enum emulation_result kvm_mips_emulate_tlbmod(u32 cause,
  565. u32 *opc,
  566. struct kvm_run *run,
  567. struct kvm_vcpu *vcpu);
  568. extern enum emulation_result kvm_mips_emulate_fpu_exc(u32 cause,
  569. u32 *opc,
  570. struct kvm_run *run,
  571. struct kvm_vcpu *vcpu);
  572. extern enum emulation_result kvm_mips_handle_ri(u32 cause,
  573. u32 *opc,
  574. struct kvm_run *run,
  575. struct kvm_vcpu *vcpu);
  576. extern enum emulation_result kvm_mips_emulate_ri_exc(u32 cause,
  577. u32 *opc,
  578. struct kvm_run *run,
  579. struct kvm_vcpu *vcpu);
  580. extern enum emulation_result kvm_mips_emulate_bp_exc(u32 cause,
  581. u32 *opc,
  582. struct kvm_run *run,
  583. struct kvm_vcpu *vcpu);
  584. extern enum emulation_result kvm_mips_emulate_trap_exc(u32 cause,
  585. u32 *opc,
  586. struct kvm_run *run,
  587. struct kvm_vcpu *vcpu);
  588. extern enum emulation_result kvm_mips_emulate_msafpe_exc(u32 cause,
  589. u32 *opc,
  590. struct kvm_run *run,
  591. struct kvm_vcpu *vcpu);
  592. extern enum emulation_result kvm_mips_emulate_fpe_exc(u32 cause,
  593. u32 *opc,
  594. struct kvm_run *run,
  595. struct kvm_vcpu *vcpu);
  596. extern enum emulation_result kvm_mips_emulate_msadis_exc(u32 cause,
  597. u32 *opc,
  598. struct kvm_run *run,
  599. struct kvm_vcpu *vcpu);
  600. extern enum emulation_result kvm_mips_complete_mmio_load(struct kvm_vcpu *vcpu,
  601. struct kvm_run *run);
  602. u32 kvm_mips_read_count(struct kvm_vcpu *vcpu);
  603. void kvm_mips_write_count(struct kvm_vcpu *vcpu, u32 count);
  604. void kvm_mips_write_compare(struct kvm_vcpu *vcpu, u32 compare, bool ack);
  605. void kvm_mips_init_count(struct kvm_vcpu *vcpu);
  606. int kvm_mips_set_count_ctl(struct kvm_vcpu *vcpu, s64 count_ctl);
  607. int kvm_mips_set_count_resume(struct kvm_vcpu *vcpu, s64 count_resume);
  608. int kvm_mips_set_count_hz(struct kvm_vcpu *vcpu, s64 count_hz);
  609. void kvm_mips_count_enable_cause(struct kvm_vcpu *vcpu);
  610. void kvm_mips_count_disable_cause(struct kvm_vcpu *vcpu);
  611. enum hrtimer_restart kvm_mips_count_timeout(struct kvm_vcpu *vcpu);
  612. enum emulation_result kvm_mips_check_privilege(u32 cause,
  613. u32 *opc,
  614. struct kvm_run *run,
  615. struct kvm_vcpu *vcpu);
  616. enum emulation_result kvm_mips_emulate_cache(union mips_instruction inst,
  617. u32 *opc,
  618. u32 cause,
  619. struct kvm_run *run,
  620. struct kvm_vcpu *vcpu);
  621. enum emulation_result kvm_mips_emulate_CP0(union mips_instruction inst,
  622. u32 *opc,
  623. u32 cause,
  624. struct kvm_run *run,
  625. struct kvm_vcpu *vcpu);
  626. enum emulation_result kvm_mips_emulate_store(union mips_instruction inst,
  627. u32 cause,
  628. struct kvm_run *run,
  629. struct kvm_vcpu *vcpu);
  630. enum emulation_result kvm_mips_emulate_load(union mips_instruction inst,
  631. u32 cause,
  632. struct kvm_run *run,
  633. struct kvm_vcpu *vcpu);
  634. unsigned int kvm_mips_config1_wrmask(struct kvm_vcpu *vcpu);
  635. unsigned int kvm_mips_config3_wrmask(struct kvm_vcpu *vcpu);
  636. unsigned int kvm_mips_config4_wrmask(struct kvm_vcpu *vcpu);
  637. unsigned int kvm_mips_config5_wrmask(struct kvm_vcpu *vcpu);
  638. /* Dynamic binary translation */
  639. extern int kvm_mips_trans_cache_index(union mips_instruction inst,
  640. u32 *opc, struct kvm_vcpu *vcpu);
  641. extern int kvm_mips_trans_cache_va(union mips_instruction inst, u32 *opc,
  642. struct kvm_vcpu *vcpu);
  643. extern int kvm_mips_trans_mfc0(union mips_instruction inst, u32 *opc,
  644. struct kvm_vcpu *vcpu);
  645. extern int kvm_mips_trans_mtc0(union mips_instruction inst, u32 *opc,
  646. struct kvm_vcpu *vcpu);
  647. /* Misc */
  648. extern void kvm_mips_dump_stats(struct kvm_vcpu *vcpu);
  649. extern unsigned long kvm_mips_get_ramsize(struct kvm *kvm);
  650. static inline void kvm_arch_hardware_disable(void) {}
  651. static inline void kvm_arch_hardware_unsetup(void) {}
  652. static inline void kvm_arch_sync_events(struct kvm *kvm) {}
  653. static inline void kvm_arch_free_memslot(struct kvm *kvm,
  654. struct kvm_memory_slot *free, struct kvm_memory_slot *dont) {}
  655. static inline void kvm_arch_memslots_updated(struct kvm *kvm, struct kvm_memslots *slots) {}
  656. static inline void kvm_arch_flush_shadow_all(struct kvm *kvm) {}
  657. static inline void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
  658. struct kvm_memory_slot *slot) {}
  659. static inline void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu) {}
  660. static inline void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu) {}
  661. static inline void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu) {}
  662. static inline void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu) {}
  663. static inline void kvm_arch_vcpu_block_finish(struct kvm_vcpu *vcpu) {}
  664. #endif /* __MIPS_KVM_HOST_H__ */