process.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  1. /*
  2. * process.c: handle interruption inject for guests.
  3. * Copyright (c) 2005, Intel Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  16. * Place - Suite 330, Boston, MA 02111-1307 USA.
  17. *
  18. * Shaofan Li (Susue Li) <susie.li@intel.com>
  19. * Xiaoyan Feng (Fleming Feng) <fleming.feng@intel.com>
  20. * Xuefei Xu (Anthony Xu) (Anthony.xu@intel.com)
  21. * Xiantao Zhang (xiantao.zhang@intel.com)
  22. */
  23. #include "vcpu.h"
  24. #include <asm/pal.h>
  25. #include <asm/sal.h>
  26. #include <asm/fpswa.h>
  27. #include <asm/kregs.h>
  28. #include <asm/tlb.h>
  29. fpswa_interface_t *vmm_fpswa_interface;
  30. #define IA64_VHPT_TRANS_VECTOR 0x0000
  31. #define IA64_INST_TLB_VECTOR 0x0400
  32. #define IA64_DATA_TLB_VECTOR 0x0800
  33. #define IA64_ALT_INST_TLB_VECTOR 0x0c00
  34. #define IA64_ALT_DATA_TLB_VECTOR 0x1000
  35. #define IA64_DATA_NESTED_TLB_VECTOR 0x1400
  36. #define IA64_INST_KEY_MISS_VECTOR 0x1800
  37. #define IA64_DATA_KEY_MISS_VECTOR 0x1c00
  38. #define IA64_DIRTY_BIT_VECTOR 0x2000
  39. #define IA64_INST_ACCESS_BIT_VECTOR 0x2400
  40. #define IA64_DATA_ACCESS_BIT_VECTOR 0x2800
  41. #define IA64_BREAK_VECTOR 0x2c00
  42. #define IA64_EXTINT_VECTOR 0x3000
  43. #define IA64_PAGE_NOT_PRESENT_VECTOR 0x5000
  44. #define IA64_KEY_PERMISSION_VECTOR 0x5100
  45. #define IA64_INST_ACCESS_RIGHTS_VECTOR 0x5200
  46. #define IA64_DATA_ACCESS_RIGHTS_VECTOR 0x5300
  47. #define IA64_GENEX_VECTOR 0x5400
  48. #define IA64_DISABLED_FPREG_VECTOR 0x5500
  49. #define IA64_NAT_CONSUMPTION_VECTOR 0x5600
  50. #define IA64_SPECULATION_VECTOR 0x5700 /* UNUSED */
  51. #define IA64_DEBUG_VECTOR 0x5900
  52. #define IA64_UNALIGNED_REF_VECTOR 0x5a00
  53. #define IA64_UNSUPPORTED_DATA_REF_VECTOR 0x5b00
  54. #define IA64_FP_FAULT_VECTOR 0x5c00
  55. #define IA64_FP_TRAP_VECTOR 0x5d00
  56. #define IA64_LOWERPRIV_TRANSFER_TRAP_VECTOR 0x5e00
  57. #define IA64_TAKEN_BRANCH_TRAP_VECTOR 0x5f00
  58. #define IA64_SINGLE_STEP_TRAP_VECTOR 0x6000
  59. /* SDM vol2 5.5 - IVA based interruption handling */
  60. #define INITIAL_PSR_VALUE_AT_INTERRUPTION (IA64_PSR_UP | IA64_PSR_MFL |\
  61. IA64_PSR_MFH | IA64_PSR_PK | IA64_PSR_DT | \
  62. IA64_PSR_RT | IA64_PSR_MC|IA64_PSR_IT)
  63. #define DOMN_PAL_REQUEST 0x110000
  64. #define DOMN_SAL_REQUEST 0x110001
  65. static u64 vec2off[68] = {0x0, 0x400, 0x800, 0xc00, 0x1000, 0x1400, 0x1800,
  66. 0x1c00, 0x2000, 0x2400, 0x2800, 0x2c00, 0x3000, 0x3400, 0x3800, 0x3c00,
  67. 0x4000, 0x4400, 0x4800, 0x4c00, 0x5000, 0x5100, 0x5200, 0x5300, 0x5400,
  68. 0x5500, 0x5600, 0x5700, 0x5800, 0x5900, 0x5a00, 0x5b00, 0x5c00, 0x5d00,
  69. 0x5e00, 0x5f00, 0x6000, 0x6100, 0x6200, 0x6300, 0x6400, 0x6500, 0x6600,
  70. 0x6700, 0x6800, 0x6900, 0x6a00, 0x6b00, 0x6c00, 0x6d00, 0x6e00, 0x6f00,
  71. 0x7000, 0x7100, 0x7200, 0x7300, 0x7400, 0x7500, 0x7600, 0x7700, 0x7800,
  72. 0x7900, 0x7a00, 0x7b00, 0x7c00, 0x7d00, 0x7e00, 0x7f00
  73. };
  74. static void collect_interruption(struct kvm_vcpu *vcpu)
  75. {
  76. u64 ipsr;
  77. u64 vdcr;
  78. u64 vifs;
  79. unsigned long vpsr;
  80. struct kvm_pt_regs *regs = vcpu_regs(vcpu);
  81. vpsr = vcpu_get_psr(vcpu);
  82. vcpu_bsw0(vcpu);
  83. if (vpsr & IA64_PSR_IC) {
  84. /* Sync mpsr id/da/dd/ss/ed bits to vipsr
  85. * since after guest do rfi, we still want these bits on in
  86. * mpsr
  87. */
  88. ipsr = regs->cr_ipsr;
  89. vpsr = vpsr | (ipsr & (IA64_PSR_ID | IA64_PSR_DA
  90. | IA64_PSR_DD | IA64_PSR_SS
  91. | IA64_PSR_ED));
  92. vcpu_set_ipsr(vcpu, vpsr);
  93. /* Currently, for trap, we do not advance IIP to next
  94. * instruction. That's because we assume caller already
  95. * set up IIP correctly
  96. */
  97. vcpu_set_iip(vcpu , regs->cr_iip);
  98. /* set vifs.v to zero */
  99. vifs = VCPU(vcpu, ifs);
  100. vifs &= ~IA64_IFS_V;
  101. vcpu_set_ifs(vcpu, vifs);
  102. vcpu_set_iipa(vcpu, VMX(vcpu, cr_iipa));
  103. }
  104. vdcr = VCPU(vcpu, dcr);
  105. /* Set guest psr
  106. * up/mfl/mfh/pk/dt/rt/mc/it keeps unchanged
  107. * be: set to the value of dcr.be
  108. * pp: set to the value of dcr.pp
  109. */
  110. vpsr &= INITIAL_PSR_VALUE_AT_INTERRUPTION;
  111. vpsr |= (vdcr & IA64_DCR_BE);
  112. /* VDCR pp bit position is different from VPSR pp bit */
  113. if (vdcr & IA64_DCR_PP) {
  114. vpsr |= IA64_PSR_PP;
  115. } else {
  116. vpsr &= ~IA64_PSR_PP;
  117. }
  118. vcpu_set_psr(vcpu, vpsr);
  119. }
  120. void inject_guest_interruption(struct kvm_vcpu *vcpu, u64 vec)
  121. {
  122. u64 viva;
  123. struct kvm_pt_regs *regs;
  124. union ia64_isr pt_isr;
  125. regs = vcpu_regs(vcpu);
  126. /* clear cr.isr.ir (incomplete register frame)*/
  127. pt_isr.val = VMX(vcpu, cr_isr);
  128. pt_isr.ir = 0;
  129. VMX(vcpu, cr_isr) = pt_isr.val;
  130. collect_interruption(vcpu);
  131. viva = vcpu_get_iva(vcpu);
  132. regs->cr_iip = viva + vec;
  133. }
  134. static u64 vcpu_get_itir_on_fault(struct kvm_vcpu *vcpu, u64 ifa)
  135. {
  136. union ia64_rr rr, rr1;
  137. rr.val = vcpu_get_rr(vcpu, ifa);
  138. rr1.val = 0;
  139. rr1.ps = rr.ps;
  140. rr1.rid = rr.rid;
  141. return (rr1.val);
  142. }
  143. /*
  144. * Set vIFA & vITIR & vIHA, when vPSR.ic =1
  145. * Parameter:
  146. * set_ifa: if true, set vIFA
  147. * set_itir: if true, set vITIR
  148. * set_iha: if true, set vIHA
  149. */
  150. void set_ifa_itir_iha(struct kvm_vcpu *vcpu, u64 vadr,
  151. int set_ifa, int set_itir, int set_iha)
  152. {
  153. long vpsr;
  154. u64 value;
  155. vpsr = VCPU(vcpu, vpsr);
  156. /* Vol2, Table 8-1 */
  157. if (vpsr & IA64_PSR_IC) {
  158. if (set_ifa)
  159. vcpu_set_ifa(vcpu, vadr);
  160. if (set_itir) {
  161. value = vcpu_get_itir_on_fault(vcpu, vadr);
  162. vcpu_set_itir(vcpu, value);
  163. }
  164. if (set_iha) {
  165. value = vcpu_thash(vcpu, vadr);
  166. vcpu_set_iha(vcpu, value);
  167. }
  168. }
  169. }
  170. /*
  171. * Data TLB Fault
  172. * @ Data TLB vector
  173. * Refer to SDM Vol2 Table 5-6 & 8-1
  174. */
  175. void dtlb_fault(struct kvm_vcpu *vcpu, u64 vadr)
  176. {
  177. /* If vPSR.ic, IFA, ITIR, IHA */
  178. set_ifa_itir_iha(vcpu, vadr, 1, 1, 1);
  179. inject_guest_interruption(vcpu, IA64_DATA_TLB_VECTOR);
  180. }
  181. /*
  182. * Instruction TLB Fault
  183. * @ Instruction TLB vector
  184. * Refer to SDM Vol2 Table 5-6 & 8-1
  185. */
  186. void itlb_fault(struct kvm_vcpu *vcpu, u64 vadr)
  187. {
  188. /* If vPSR.ic, IFA, ITIR, IHA */
  189. set_ifa_itir_iha(vcpu, vadr, 1, 1, 1);
  190. inject_guest_interruption(vcpu, IA64_INST_TLB_VECTOR);
  191. }
  192. /*
  193. * Data Nested TLB Fault
  194. * @ Data Nested TLB Vector
  195. * Refer to SDM Vol2 Table 5-6 & 8-1
  196. */
  197. void nested_dtlb(struct kvm_vcpu *vcpu)
  198. {
  199. inject_guest_interruption(vcpu, IA64_DATA_NESTED_TLB_VECTOR);
  200. }
  201. /*
  202. * Alternate Data TLB Fault
  203. * @ Alternate Data TLB vector
  204. * Refer to SDM Vol2 Table 5-6 & 8-1
  205. */
  206. void alt_dtlb(struct kvm_vcpu *vcpu, u64 vadr)
  207. {
  208. set_ifa_itir_iha(vcpu, vadr, 1, 1, 0);
  209. inject_guest_interruption(vcpu, IA64_ALT_DATA_TLB_VECTOR);
  210. }
  211. /*
  212. * Data TLB Fault
  213. * @ Data TLB vector
  214. * Refer to SDM Vol2 Table 5-6 & 8-1
  215. */
  216. void alt_itlb(struct kvm_vcpu *vcpu, u64 vadr)
  217. {
  218. set_ifa_itir_iha(vcpu, vadr, 1, 1, 0);
  219. inject_guest_interruption(vcpu, IA64_ALT_INST_TLB_VECTOR);
  220. }
  221. /* Deal with:
  222. * VHPT Translation Vector
  223. */
  224. static void _vhpt_fault(struct kvm_vcpu *vcpu, u64 vadr)
  225. {
  226. /* If vPSR.ic, IFA, ITIR, IHA*/
  227. set_ifa_itir_iha(vcpu, vadr, 1, 1, 1);
  228. inject_guest_interruption(vcpu, IA64_VHPT_TRANS_VECTOR);
  229. }
  230. /*
  231. * VHPT Instruction Fault
  232. * @ VHPT Translation vector
  233. * Refer to SDM Vol2 Table 5-6 & 8-1
  234. */
  235. void ivhpt_fault(struct kvm_vcpu *vcpu, u64 vadr)
  236. {
  237. _vhpt_fault(vcpu, vadr);
  238. }
  239. /*
  240. * VHPT Data Fault
  241. * @ VHPT Translation vector
  242. * Refer to SDM Vol2 Table 5-6 & 8-1
  243. */
  244. void dvhpt_fault(struct kvm_vcpu *vcpu, u64 vadr)
  245. {
  246. _vhpt_fault(vcpu, vadr);
  247. }
  248. /*
  249. * Deal with:
  250. * General Exception vector
  251. */
  252. void _general_exception(struct kvm_vcpu *vcpu)
  253. {
  254. inject_guest_interruption(vcpu, IA64_GENEX_VECTOR);
  255. }
  256. /*
  257. * Illegal Operation Fault
  258. * @ General Exception Vector
  259. * Refer to SDM Vol2 Table 5-6 & 8-1
  260. */
  261. void illegal_op(struct kvm_vcpu *vcpu)
  262. {
  263. _general_exception(vcpu);
  264. }
  265. /*
  266. * Illegal Dependency Fault
  267. * @ General Exception Vector
  268. * Refer to SDM Vol2 Table 5-6 & 8-1
  269. */
  270. void illegal_dep(struct kvm_vcpu *vcpu)
  271. {
  272. _general_exception(vcpu);
  273. }
  274. /*
  275. * Reserved Register/Field Fault
  276. * @ General Exception Vector
  277. * Refer to SDM Vol2 Table 5-6 & 8-1
  278. */
  279. void rsv_reg_field(struct kvm_vcpu *vcpu)
  280. {
  281. _general_exception(vcpu);
  282. }
  283. /*
  284. * Privileged Operation Fault
  285. * @ General Exception Vector
  286. * Refer to SDM Vol2 Table 5-6 & 8-1
  287. */
  288. void privilege_op(struct kvm_vcpu *vcpu)
  289. {
  290. _general_exception(vcpu);
  291. }
  292. /*
  293. * Unimplement Data Address Fault
  294. * @ General Exception Vector
  295. * Refer to SDM Vol2 Table 5-6 & 8-1
  296. */
  297. void unimpl_daddr(struct kvm_vcpu *vcpu)
  298. {
  299. _general_exception(vcpu);
  300. }
  301. /*
  302. * Privileged Register Fault
  303. * @ General Exception Vector
  304. * Refer to SDM Vol2 Table 5-6 & 8-1
  305. */
  306. void privilege_reg(struct kvm_vcpu *vcpu)
  307. {
  308. _general_exception(vcpu);
  309. }
  310. /* Deal with
  311. * Nat consumption vector
  312. * Parameter:
  313. * vaddr: Optional, if t == REGISTER
  314. */
  315. static void _nat_consumption_fault(struct kvm_vcpu *vcpu, u64 vadr,
  316. enum tlb_miss_type t)
  317. {
  318. /* If vPSR.ic && t == DATA/INST, IFA */
  319. if (t == DATA || t == INSTRUCTION) {
  320. /* IFA */
  321. set_ifa_itir_iha(vcpu, vadr, 1, 0, 0);
  322. }
  323. inject_guest_interruption(vcpu, IA64_NAT_CONSUMPTION_VECTOR);
  324. }
  325. /*
  326. * Instruction Nat Page Consumption Fault
  327. * @ Nat Consumption Vector
  328. * Refer to SDM Vol2 Table 5-6 & 8-1
  329. */
  330. void inat_page_consumption(struct kvm_vcpu *vcpu, u64 vadr)
  331. {
  332. _nat_consumption_fault(vcpu, vadr, INSTRUCTION);
  333. }
  334. /*
  335. * Register Nat Consumption Fault
  336. * @ Nat Consumption Vector
  337. * Refer to SDM Vol2 Table 5-6 & 8-1
  338. */
  339. void rnat_consumption(struct kvm_vcpu *vcpu)
  340. {
  341. _nat_consumption_fault(vcpu, 0, REGISTER);
  342. }
  343. /*
  344. * Data Nat Page Consumption Fault
  345. * @ Nat Consumption Vector
  346. * Refer to SDM Vol2 Table 5-6 & 8-1
  347. */
  348. void dnat_page_consumption(struct kvm_vcpu *vcpu, u64 vadr)
  349. {
  350. _nat_consumption_fault(vcpu, vadr, DATA);
  351. }
  352. /* Deal with
  353. * Page not present vector
  354. */
  355. static void __page_not_present(struct kvm_vcpu *vcpu, u64 vadr)
  356. {
  357. /* If vPSR.ic, IFA, ITIR */
  358. set_ifa_itir_iha(vcpu, vadr, 1, 1, 0);
  359. inject_guest_interruption(vcpu, IA64_PAGE_NOT_PRESENT_VECTOR);
  360. }
  361. void data_page_not_present(struct kvm_vcpu *vcpu, u64 vadr)
  362. {
  363. __page_not_present(vcpu, vadr);
  364. }
  365. void inst_page_not_present(struct kvm_vcpu *vcpu, u64 vadr)
  366. {
  367. __page_not_present(vcpu, vadr);
  368. }
  369. /* Deal with
  370. * Data access rights vector
  371. */
  372. void data_access_rights(struct kvm_vcpu *vcpu, u64 vadr)
  373. {
  374. /* If vPSR.ic, IFA, ITIR */
  375. set_ifa_itir_iha(vcpu, vadr, 1, 1, 0);
  376. inject_guest_interruption(vcpu, IA64_DATA_ACCESS_RIGHTS_VECTOR);
  377. }
  378. fpswa_ret_t vmm_fp_emulate(int fp_fault, void *bundle, unsigned long *ipsr,
  379. unsigned long *fpsr, unsigned long *isr, unsigned long *pr,
  380. unsigned long *ifs, struct kvm_pt_regs *regs)
  381. {
  382. fp_state_t fp_state;
  383. fpswa_ret_t ret;
  384. struct kvm_vcpu *vcpu = current_vcpu;
  385. uint64_t old_rr7 = ia64_get_rr(7UL<<61);
  386. if (!vmm_fpswa_interface)
  387. return (fpswa_ret_t) {-1, 0, 0, 0};
  388. memset(&fp_state, 0, sizeof(fp_state_t));
  389. /*
  390. * compute fp_state. only FP registers f6 - f11 are used by the
  391. * vmm, so set those bits in the mask and set the low volatile
  392. * pointer to point to these registers.
  393. */
  394. fp_state.bitmask_low64 = 0xfc0; /* bit6..bit11 */
  395. fp_state.fp_state_low_volatile = (fp_state_low_volatile_t *) &regs->f6;
  396. /*
  397. * unsigned long (*EFI_FPSWA) (
  398. * unsigned long trap_type,
  399. * void *Bundle,
  400. * unsigned long *pipsr,
  401. * unsigned long *pfsr,
  402. * unsigned long *pisr,
  403. * unsigned long *ppreds,
  404. * unsigned long *pifs,
  405. * void *fp_state);
  406. */
  407. /*Call host fpswa interface directly to virtualize
  408. *guest fpswa request!
  409. */
  410. ia64_set_rr(7UL << 61, vcpu->arch.host.rr[7]);
  411. ia64_srlz_d();
  412. ret = (*vmm_fpswa_interface->fpswa) (fp_fault, bundle,
  413. ipsr, fpsr, isr, pr, ifs, &fp_state);
  414. ia64_set_rr(7UL << 61, old_rr7);
  415. ia64_srlz_d();
  416. return ret;
  417. }
  418. /*
  419. * Handle floating-point assist faults and traps for domain.
  420. */
  421. unsigned long vmm_handle_fpu_swa(int fp_fault, struct kvm_pt_regs *regs,
  422. unsigned long isr)
  423. {
  424. struct kvm_vcpu *v = current_vcpu;
  425. IA64_BUNDLE bundle;
  426. unsigned long fault_ip;
  427. fpswa_ret_t ret;
  428. fault_ip = regs->cr_iip;
  429. /*
  430. * When the FP trap occurs, the trapping instruction is completed.
  431. * If ipsr.ri == 0, there is the trapping instruction in previous
  432. * bundle.
  433. */
  434. if (!fp_fault && (ia64_psr(regs)->ri == 0))
  435. fault_ip -= 16;
  436. if (fetch_code(v, fault_ip, &bundle))
  437. return -EAGAIN;
  438. if (!bundle.i64[0] && !bundle.i64[1])
  439. return -EACCES;
  440. ret = vmm_fp_emulate(fp_fault, &bundle, &regs->cr_ipsr, &regs->ar_fpsr,
  441. &isr, &regs->pr, &regs->cr_ifs, regs);
  442. return ret.status;
  443. }
  444. void reflect_interruption(u64 ifa, u64 isr, u64 iim,
  445. u64 vec, struct kvm_pt_regs *regs)
  446. {
  447. u64 vector;
  448. int status ;
  449. struct kvm_vcpu *vcpu = current_vcpu;
  450. u64 vpsr = VCPU(vcpu, vpsr);
  451. vector = vec2off[vec];
  452. if (!(vpsr & IA64_PSR_IC) && (vector != IA64_DATA_NESTED_TLB_VECTOR)) {
  453. panic_vm(vcpu, "Interruption with vector :0x%lx occurs "
  454. "with psr.ic = 0\n", vector);
  455. return;
  456. }
  457. switch (vec) {
  458. case 32: /*IA64_FP_FAULT_VECTOR*/
  459. status = vmm_handle_fpu_swa(1, regs, isr);
  460. if (!status) {
  461. vcpu_increment_iip(vcpu);
  462. return;
  463. } else if (-EAGAIN == status)
  464. return;
  465. break;
  466. case 33: /*IA64_FP_TRAP_VECTOR*/
  467. status = vmm_handle_fpu_swa(0, regs, isr);
  468. if (!status)
  469. return ;
  470. break;
  471. }
  472. VCPU(vcpu, isr) = isr;
  473. VCPU(vcpu, iipa) = regs->cr_iip;
  474. if (vector == IA64_BREAK_VECTOR || vector == IA64_SPECULATION_VECTOR)
  475. VCPU(vcpu, iim) = iim;
  476. else
  477. set_ifa_itir_iha(vcpu, ifa, 1, 1, 1);
  478. inject_guest_interruption(vcpu, vector);
  479. }
  480. static unsigned long kvm_trans_pal_call_args(struct kvm_vcpu *vcpu,
  481. unsigned long arg)
  482. {
  483. struct thash_data *data;
  484. unsigned long gpa, poff;
  485. if (!is_physical_mode(vcpu)) {
  486. /* Depends on caller to provide the DTR or DTC mapping.*/
  487. data = vtlb_lookup(vcpu, arg, D_TLB);
  488. if (data)
  489. gpa = data->page_flags & _PAGE_PPN_MASK;
  490. else {
  491. data = vhpt_lookup(arg);
  492. if (!data)
  493. return 0;
  494. gpa = data->gpaddr & _PAGE_PPN_MASK;
  495. }
  496. poff = arg & (PSIZE(data->ps) - 1);
  497. arg = PAGEALIGN(gpa, data->ps) | poff;
  498. }
  499. arg = kvm_gpa_to_mpa(arg << 1 >> 1);
  500. return (unsigned long)__va(arg);
  501. }
  502. static void set_pal_call_data(struct kvm_vcpu *vcpu)
  503. {
  504. struct exit_ctl_data *p = &vcpu->arch.exit_data;
  505. unsigned long gr28 = vcpu_get_gr(vcpu, 28);
  506. unsigned long gr29 = vcpu_get_gr(vcpu, 29);
  507. unsigned long gr30 = vcpu_get_gr(vcpu, 30);
  508. /*FIXME:For static and stacked convention, firmware
  509. * has put the parameters in gr28-gr31 before
  510. * break to vmm !!*/
  511. switch (gr28) {
  512. case PAL_PERF_MON_INFO:
  513. case PAL_HALT_INFO:
  514. p->u.pal_data.gr29 = kvm_trans_pal_call_args(vcpu, gr29);
  515. p->u.pal_data.gr30 = vcpu_get_gr(vcpu, 30);
  516. break;
  517. case PAL_BRAND_INFO:
  518. p->u.pal_data.gr29 = gr29;
  519. p->u.pal_data.gr30 = kvm_trans_pal_call_args(vcpu, gr30);
  520. break;
  521. default:
  522. p->u.pal_data.gr29 = gr29;
  523. p->u.pal_data.gr30 = vcpu_get_gr(vcpu, 30);
  524. }
  525. p->u.pal_data.gr28 = gr28;
  526. p->u.pal_data.gr31 = vcpu_get_gr(vcpu, 31);
  527. p->exit_reason = EXIT_REASON_PAL_CALL;
  528. }
  529. static void get_pal_call_result(struct kvm_vcpu *vcpu)
  530. {
  531. struct exit_ctl_data *p = &vcpu->arch.exit_data;
  532. if (p->exit_reason == EXIT_REASON_PAL_CALL) {
  533. vcpu_set_gr(vcpu, 8, p->u.pal_data.ret.status, 0);
  534. vcpu_set_gr(vcpu, 9, p->u.pal_data.ret.v0, 0);
  535. vcpu_set_gr(vcpu, 10, p->u.pal_data.ret.v1, 0);
  536. vcpu_set_gr(vcpu, 11, p->u.pal_data.ret.v2, 0);
  537. } else
  538. panic_vm(vcpu, "Mis-set for exit reason!\n");
  539. }
  540. static void set_sal_call_data(struct kvm_vcpu *vcpu)
  541. {
  542. struct exit_ctl_data *p = &vcpu->arch.exit_data;
  543. p->u.sal_data.in0 = vcpu_get_gr(vcpu, 32);
  544. p->u.sal_data.in1 = vcpu_get_gr(vcpu, 33);
  545. p->u.sal_data.in2 = vcpu_get_gr(vcpu, 34);
  546. p->u.sal_data.in3 = vcpu_get_gr(vcpu, 35);
  547. p->u.sal_data.in4 = vcpu_get_gr(vcpu, 36);
  548. p->u.sal_data.in5 = vcpu_get_gr(vcpu, 37);
  549. p->u.sal_data.in6 = vcpu_get_gr(vcpu, 38);
  550. p->u.sal_data.in7 = vcpu_get_gr(vcpu, 39);
  551. p->exit_reason = EXIT_REASON_SAL_CALL;
  552. }
  553. static void get_sal_call_result(struct kvm_vcpu *vcpu)
  554. {
  555. struct exit_ctl_data *p = &vcpu->arch.exit_data;
  556. if (p->exit_reason == EXIT_REASON_SAL_CALL) {
  557. vcpu_set_gr(vcpu, 8, p->u.sal_data.ret.r8, 0);
  558. vcpu_set_gr(vcpu, 9, p->u.sal_data.ret.r9, 0);
  559. vcpu_set_gr(vcpu, 10, p->u.sal_data.ret.r10, 0);
  560. vcpu_set_gr(vcpu, 11, p->u.sal_data.ret.r11, 0);
  561. } else
  562. panic_vm(vcpu, "Mis-set for exit reason!\n");
  563. }
  564. void kvm_ia64_handle_break(unsigned long ifa, struct kvm_pt_regs *regs,
  565. unsigned long isr, unsigned long iim)
  566. {
  567. struct kvm_vcpu *v = current_vcpu;
  568. long psr;
  569. if (ia64_psr(regs)->cpl == 0) {
  570. /* Allow hypercalls only when cpl = 0. */
  571. if (iim == DOMN_PAL_REQUEST) {
  572. local_irq_save(psr);
  573. set_pal_call_data(v);
  574. vmm_transition(v);
  575. get_pal_call_result(v);
  576. vcpu_increment_iip(v);
  577. local_irq_restore(psr);
  578. return;
  579. } else if (iim == DOMN_SAL_REQUEST) {
  580. local_irq_save(psr);
  581. set_sal_call_data(v);
  582. vmm_transition(v);
  583. get_sal_call_result(v);
  584. vcpu_increment_iip(v);
  585. local_irq_restore(psr);
  586. return;
  587. }
  588. }
  589. reflect_interruption(ifa, isr, iim, 11, regs);
  590. }
  591. void check_pending_irq(struct kvm_vcpu *vcpu)
  592. {
  593. int mask, h_pending, h_inservice;
  594. u64 isr;
  595. unsigned long vpsr;
  596. struct kvm_pt_regs *regs = vcpu_regs(vcpu);
  597. h_pending = highest_pending_irq(vcpu);
  598. if (h_pending == NULL_VECTOR) {
  599. update_vhpi(vcpu, NULL_VECTOR);
  600. return;
  601. }
  602. h_inservice = highest_inservice_irq(vcpu);
  603. vpsr = VCPU(vcpu, vpsr);
  604. mask = irq_masked(vcpu, h_pending, h_inservice);
  605. if ((vpsr & IA64_PSR_I) && IRQ_NO_MASKED == mask) {
  606. isr = vpsr & IA64_PSR_RI;
  607. update_vhpi(vcpu, h_pending);
  608. reflect_interruption(0, isr, 0, 12, regs); /* EXT IRQ */
  609. } else if (mask == IRQ_MASKED_BY_INSVC) {
  610. if (VCPU(vcpu, vhpi))
  611. update_vhpi(vcpu, NULL_VECTOR);
  612. } else {
  613. /* masked by vpsr.i or vtpr.*/
  614. update_vhpi(vcpu, h_pending);
  615. }
  616. }
  617. static void generate_exirq(struct kvm_vcpu *vcpu)
  618. {
  619. unsigned vpsr;
  620. uint64_t isr;
  621. struct kvm_pt_regs *regs = vcpu_regs(vcpu);
  622. vpsr = VCPU(vcpu, vpsr);
  623. isr = vpsr & IA64_PSR_RI;
  624. if (!(vpsr & IA64_PSR_IC))
  625. panic_vm(vcpu, "Trying to inject one IRQ with psr.ic=0\n");
  626. reflect_interruption(0, isr, 0, 12, regs); /* EXT IRQ */
  627. }
  628. void vhpi_detection(struct kvm_vcpu *vcpu)
  629. {
  630. uint64_t threshold, vhpi;
  631. union ia64_tpr vtpr;
  632. struct ia64_psr vpsr;
  633. vpsr = *(struct ia64_psr *)&VCPU(vcpu, vpsr);
  634. vtpr.val = VCPU(vcpu, tpr);
  635. threshold = ((!vpsr.i) << 5) | (vtpr.mmi << 4) | vtpr.mic;
  636. vhpi = VCPU(vcpu, vhpi);
  637. if (vhpi > threshold) {
  638. /* interrupt actived*/
  639. generate_exirq(vcpu);
  640. }
  641. }
  642. void leave_hypervisor_tail(void)
  643. {
  644. struct kvm_vcpu *v = current_vcpu;
  645. if (VMX(v, timer_check)) {
  646. VMX(v, timer_check) = 0;
  647. if (VMX(v, itc_check)) {
  648. if (vcpu_get_itc(v) > VCPU(v, itm)) {
  649. if (!(VCPU(v, itv) & (1 << 16))) {
  650. vcpu_pend_interrupt(v, VCPU(v, itv)
  651. & 0xff);
  652. VMX(v, itc_check) = 0;
  653. } else {
  654. v->arch.timer_pending = 1;
  655. }
  656. VMX(v, last_itc) = VCPU(v, itm) + 1;
  657. }
  658. }
  659. }
  660. rmb();
  661. if (v->arch.irq_new_pending) {
  662. v->arch.irq_new_pending = 0;
  663. VMX(v, irq_check) = 0;
  664. check_pending_irq(v);
  665. return;
  666. }
  667. if (VMX(v, irq_check)) {
  668. VMX(v, irq_check) = 0;
  669. vhpi_detection(v);
  670. }
  671. }
  672. static inline void handle_lds(struct kvm_pt_regs *regs)
  673. {
  674. regs->cr_ipsr |= IA64_PSR_ED;
  675. }
  676. void physical_tlb_miss(struct kvm_vcpu *vcpu, unsigned long vadr, int type)
  677. {
  678. unsigned long pte;
  679. union ia64_rr rr;
  680. rr.val = ia64_get_rr(vadr);
  681. pte = vadr & _PAGE_PPN_MASK;
  682. pte = pte | PHY_PAGE_WB;
  683. thash_vhpt_insert(vcpu, pte, (u64)(rr.ps << 2), vadr, type);
  684. return;
  685. }
  686. void kvm_page_fault(u64 vadr , u64 vec, struct kvm_pt_regs *regs)
  687. {
  688. unsigned long vpsr;
  689. int type;
  690. u64 vhpt_adr, gppa, pteval, rr, itir;
  691. union ia64_isr misr;
  692. union ia64_pta vpta;
  693. struct thash_data *data;
  694. struct kvm_vcpu *v = current_vcpu;
  695. vpsr = VCPU(v, vpsr);
  696. misr.val = VMX(v, cr_isr);
  697. type = vec;
  698. if (is_physical_mode(v) && (!(vadr << 1 >> 62))) {
  699. if (vec == 2) {
  700. if (__gpfn_is_io((vadr << 1) >> (PAGE_SHIFT + 1))) {
  701. emulate_io_inst(v, ((vadr << 1) >> 1), 4);
  702. return;
  703. }
  704. }
  705. physical_tlb_miss(v, vadr, type);
  706. return;
  707. }
  708. data = vtlb_lookup(v, vadr, type);
  709. if (data != 0) {
  710. if (type == D_TLB) {
  711. gppa = (vadr & ((1UL << data->ps) - 1))
  712. + (data->ppn >> (data->ps - 12) << data->ps);
  713. if (__gpfn_is_io(gppa >> PAGE_SHIFT)) {
  714. if (data->pl >= ((regs->cr_ipsr >>
  715. IA64_PSR_CPL0_BIT) & 3))
  716. emulate_io_inst(v, gppa, data->ma);
  717. else {
  718. vcpu_set_isr(v, misr.val);
  719. data_access_rights(v, vadr);
  720. }
  721. return ;
  722. }
  723. }
  724. thash_vhpt_insert(v, data->page_flags, data->itir, vadr, type);
  725. } else if (type == D_TLB) {
  726. if (misr.sp) {
  727. handle_lds(regs);
  728. return;
  729. }
  730. rr = vcpu_get_rr(v, vadr);
  731. itir = rr & (RR_RID_MASK | RR_PS_MASK);
  732. if (!vhpt_enabled(v, vadr, misr.rs ? RSE_REF : DATA_REF)) {
  733. if (vpsr & IA64_PSR_IC) {
  734. vcpu_set_isr(v, misr.val);
  735. alt_dtlb(v, vadr);
  736. } else {
  737. nested_dtlb(v);
  738. }
  739. return ;
  740. }
  741. vpta.val = vcpu_get_pta(v);
  742. /* avoid recursively walking (short format) VHPT */
  743. vhpt_adr = vcpu_thash(v, vadr);
  744. if (!guest_vhpt_lookup(vhpt_adr, &pteval)) {
  745. /* VHPT successfully read. */
  746. if (!(pteval & _PAGE_P)) {
  747. if (vpsr & IA64_PSR_IC) {
  748. vcpu_set_isr(v, misr.val);
  749. dtlb_fault(v, vadr);
  750. } else {
  751. nested_dtlb(v);
  752. }
  753. } else if ((pteval & _PAGE_MA_MASK) != _PAGE_MA_ST) {
  754. thash_purge_and_insert(v, pteval, itir,
  755. vadr, D_TLB);
  756. } else if (vpsr & IA64_PSR_IC) {
  757. vcpu_set_isr(v, misr.val);
  758. dtlb_fault(v, vadr);
  759. } else {
  760. nested_dtlb(v);
  761. }
  762. } else {
  763. /* Can't read VHPT. */
  764. if (vpsr & IA64_PSR_IC) {
  765. vcpu_set_isr(v, misr.val);
  766. dvhpt_fault(v, vadr);
  767. } else {
  768. nested_dtlb(v);
  769. }
  770. }
  771. } else if (type == I_TLB) {
  772. if (!(vpsr & IA64_PSR_IC))
  773. misr.ni = 1;
  774. if (!vhpt_enabled(v, vadr, INST_REF)) {
  775. vcpu_set_isr(v, misr.val);
  776. alt_itlb(v, vadr);
  777. return;
  778. }
  779. vpta.val = vcpu_get_pta(v);
  780. vhpt_adr = vcpu_thash(v, vadr);
  781. if (!guest_vhpt_lookup(vhpt_adr, &pteval)) {
  782. /* VHPT successfully read. */
  783. if (pteval & _PAGE_P) {
  784. if ((pteval & _PAGE_MA_MASK) == _PAGE_MA_ST) {
  785. vcpu_set_isr(v, misr.val);
  786. itlb_fault(v, vadr);
  787. return ;
  788. }
  789. rr = vcpu_get_rr(v, vadr);
  790. itir = rr & (RR_RID_MASK | RR_PS_MASK);
  791. thash_purge_and_insert(v, pteval, itir,
  792. vadr, I_TLB);
  793. } else {
  794. vcpu_set_isr(v, misr.val);
  795. inst_page_not_present(v, vadr);
  796. }
  797. } else {
  798. vcpu_set_isr(v, misr.val);
  799. ivhpt_fault(v, vadr);
  800. }
  801. }
  802. }
  803. void kvm_vexirq(struct kvm_vcpu *vcpu)
  804. {
  805. u64 vpsr, isr;
  806. struct kvm_pt_regs *regs;
  807. regs = vcpu_regs(vcpu);
  808. vpsr = VCPU(vcpu, vpsr);
  809. isr = vpsr & IA64_PSR_RI;
  810. reflect_interruption(0, isr, 0, 12, regs); /*EXT IRQ*/
  811. }
  812. void kvm_ia64_handle_irq(struct kvm_vcpu *v)
  813. {
  814. struct exit_ctl_data *p = &v->arch.exit_data;
  815. long psr;
  816. local_irq_save(psr);
  817. p->exit_reason = EXIT_REASON_EXTERNAL_INTERRUPT;
  818. vmm_transition(v);
  819. local_irq_restore(psr);
  820. VMX(v, timer_check) = 1;
  821. }
  822. static void ptc_ga_remote_func(struct kvm_vcpu *v, int pos)
  823. {
  824. u64 oldrid, moldrid, oldpsbits, vaddr;
  825. struct kvm_ptc_g *p = &v->arch.ptc_g_data[pos];
  826. vaddr = p->vaddr;
  827. oldrid = VMX(v, vrr[0]);
  828. VMX(v, vrr[0]) = p->rr;
  829. oldpsbits = VMX(v, psbits[0]);
  830. VMX(v, psbits[0]) = VMX(v, psbits[REGION_NUMBER(vaddr)]);
  831. moldrid = ia64_get_rr(0x0);
  832. ia64_set_rr(0x0, vrrtomrr(p->rr));
  833. ia64_srlz_d();
  834. vaddr = PAGEALIGN(vaddr, p->ps);
  835. thash_purge_entries_remote(v, vaddr, p->ps);
  836. VMX(v, vrr[0]) = oldrid;
  837. VMX(v, psbits[0]) = oldpsbits;
  838. ia64_set_rr(0x0, moldrid);
  839. ia64_dv_serialize_data();
  840. }
  841. static void vcpu_do_resume(struct kvm_vcpu *vcpu)
  842. {
  843. /*Re-init VHPT and VTLB once from resume*/
  844. vcpu->arch.vhpt.num = VHPT_NUM_ENTRIES;
  845. thash_init(&vcpu->arch.vhpt, VHPT_SHIFT);
  846. vcpu->arch.vtlb.num = VTLB_NUM_ENTRIES;
  847. thash_init(&vcpu->arch.vtlb, VTLB_SHIFT);
  848. ia64_set_pta(vcpu->arch.vhpt.pta.val);
  849. }
  850. static void vmm_sanity_check(struct kvm_vcpu *vcpu)
  851. {
  852. struct exit_ctl_data *p = &vcpu->arch.exit_data;
  853. if (!vmm_sanity && p->exit_reason != EXIT_REASON_DEBUG) {
  854. panic_vm(vcpu, "Failed to do vmm sanity check,"
  855. "it maybe caused by crashed vmm!!\n\n");
  856. }
  857. }
  858. static void kvm_do_resume_op(struct kvm_vcpu *vcpu)
  859. {
  860. vmm_sanity_check(vcpu); /*Guarantee vcpu running on healthy vmm!*/
  861. if (test_and_clear_bit(KVM_REQ_RESUME, &vcpu->requests)) {
  862. vcpu_do_resume(vcpu);
  863. return;
  864. }
  865. if (unlikely(test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))) {
  866. thash_purge_all(vcpu);
  867. return;
  868. }
  869. if (test_and_clear_bit(KVM_REQ_PTC_G, &vcpu->requests)) {
  870. while (vcpu->arch.ptc_g_count > 0)
  871. ptc_ga_remote_func(vcpu, --vcpu->arch.ptc_g_count);
  872. }
  873. }
  874. void vmm_transition(struct kvm_vcpu *vcpu)
  875. {
  876. ia64_call_vsa(PAL_VPS_SAVE, (unsigned long)vcpu->arch.vpd,
  877. 1, 0, 0, 0, 0, 0);
  878. vmm_trampoline(&vcpu->arch.guest, &vcpu->arch.host);
  879. ia64_call_vsa(PAL_VPS_RESTORE, (unsigned long)vcpu->arch.vpd,
  880. 1, 0, 0, 0, 0, 0);
  881. kvm_do_resume_op(vcpu);
  882. }
  883. void vmm_panic_handler(u64 vec)
  884. {
  885. struct kvm_vcpu *vcpu = current_vcpu;
  886. vmm_sanity = 0;
  887. panic_vm(vcpu, "Unexpected interruption occurs in VMM, vector:0x%lx\n",
  888. vec2off[vec]);
  889. }