process.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  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. /*
  389. * Just let fpswa driver to use hardware fp registers.
  390. * No fp register is valid in memory.
  391. */
  392. memset(&fp_state, 0, sizeof(fp_state_t));
  393. /*
  394. * unsigned long (*EFI_FPSWA) (
  395. * unsigned long trap_type,
  396. * void *Bundle,
  397. * unsigned long *pipsr,
  398. * unsigned long *pfsr,
  399. * unsigned long *pisr,
  400. * unsigned long *ppreds,
  401. * unsigned long *pifs,
  402. * void *fp_state);
  403. */
  404. /*Call host fpswa interface directly to virtualize
  405. *guest fpswa request!
  406. */
  407. ia64_set_rr(7UL << 61, vcpu->arch.host.rr[7]);
  408. ia64_srlz_d();
  409. ret = (*vmm_fpswa_interface->fpswa) (fp_fault, bundle,
  410. ipsr, fpsr, isr, pr, ifs, &fp_state);
  411. ia64_set_rr(7UL << 61, old_rr7);
  412. ia64_srlz_d();
  413. return ret;
  414. }
  415. /*
  416. * Handle floating-point assist faults and traps for domain.
  417. */
  418. unsigned long vmm_handle_fpu_swa(int fp_fault, struct kvm_pt_regs *regs,
  419. unsigned long isr)
  420. {
  421. struct kvm_vcpu *v = current_vcpu;
  422. IA64_BUNDLE bundle;
  423. unsigned long fault_ip;
  424. fpswa_ret_t ret;
  425. fault_ip = regs->cr_iip;
  426. /*
  427. * When the FP trap occurs, the trapping instruction is completed.
  428. * If ipsr.ri == 0, there is the trapping instruction in previous
  429. * bundle.
  430. */
  431. if (!fp_fault && (ia64_psr(regs)->ri == 0))
  432. fault_ip -= 16;
  433. if (fetch_code(v, fault_ip, &bundle))
  434. return -EAGAIN;
  435. if (!bundle.i64[0] && !bundle.i64[1])
  436. return -EACCES;
  437. ret = vmm_fp_emulate(fp_fault, &bundle, &regs->cr_ipsr, &regs->ar_fpsr,
  438. &isr, &regs->pr, &regs->cr_ifs, regs);
  439. return ret.status;
  440. }
  441. void reflect_interruption(u64 ifa, u64 isr, u64 iim,
  442. u64 vec, struct kvm_pt_regs *regs)
  443. {
  444. u64 vector;
  445. int status ;
  446. struct kvm_vcpu *vcpu = current_vcpu;
  447. u64 vpsr = VCPU(vcpu, vpsr);
  448. vector = vec2off[vec];
  449. if (!(vpsr & IA64_PSR_IC) && (vector != IA64_DATA_NESTED_TLB_VECTOR)) {
  450. panic_vm(vcpu);
  451. return;
  452. }
  453. switch (vec) {
  454. case 32: /*IA64_FP_FAULT_VECTOR*/
  455. status = vmm_handle_fpu_swa(1, regs, isr);
  456. if (!status) {
  457. vcpu_increment_iip(vcpu);
  458. return;
  459. } else if (-EAGAIN == status)
  460. return;
  461. break;
  462. case 33: /*IA64_FP_TRAP_VECTOR*/
  463. status = vmm_handle_fpu_swa(0, regs, isr);
  464. if (!status)
  465. return ;
  466. else if (-EAGAIN == status) {
  467. vcpu_decrement_iip(vcpu);
  468. return ;
  469. }
  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 void set_pal_call_data(struct kvm_vcpu *vcpu)
  481. {
  482. struct exit_ctl_data *p = &vcpu->arch.exit_data;
  483. /*FIXME:For static and stacked convention, firmware
  484. * has put the parameters in gr28-gr31 before
  485. * break to vmm !!*/
  486. p->u.pal_data.gr28 = vcpu_get_gr(vcpu, 28);
  487. p->u.pal_data.gr29 = vcpu_get_gr(vcpu, 29);
  488. p->u.pal_data.gr30 = vcpu_get_gr(vcpu, 30);
  489. p->u.pal_data.gr31 = vcpu_get_gr(vcpu, 31);
  490. p->exit_reason = EXIT_REASON_PAL_CALL;
  491. }
  492. static void set_pal_call_result(struct kvm_vcpu *vcpu)
  493. {
  494. struct exit_ctl_data *p = &vcpu->arch.exit_data;
  495. if (p->exit_reason == EXIT_REASON_PAL_CALL) {
  496. vcpu_set_gr(vcpu, 8, p->u.pal_data.ret.status, 0);
  497. vcpu_set_gr(vcpu, 9, p->u.pal_data.ret.v0, 0);
  498. vcpu_set_gr(vcpu, 10, p->u.pal_data.ret.v1, 0);
  499. vcpu_set_gr(vcpu, 11, p->u.pal_data.ret.v2, 0);
  500. } else
  501. panic_vm(vcpu);
  502. }
  503. static void set_sal_call_data(struct kvm_vcpu *vcpu)
  504. {
  505. struct exit_ctl_data *p = &vcpu->arch.exit_data;
  506. p->u.sal_data.in0 = vcpu_get_gr(vcpu, 32);
  507. p->u.sal_data.in1 = vcpu_get_gr(vcpu, 33);
  508. p->u.sal_data.in2 = vcpu_get_gr(vcpu, 34);
  509. p->u.sal_data.in3 = vcpu_get_gr(vcpu, 35);
  510. p->u.sal_data.in4 = vcpu_get_gr(vcpu, 36);
  511. p->u.sal_data.in5 = vcpu_get_gr(vcpu, 37);
  512. p->u.sal_data.in6 = vcpu_get_gr(vcpu, 38);
  513. p->u.sal_data.in7 = vcpu_get_gr(vcpu, 39);
  514. p->exit_reason = EXIT_REASON_SAL_CALL;
  515. }
  516. static void set_sal_call_result(struct kvm_vcpu *vcpu)
  517. {
  518. struct exit_ctl_data *p = &vcpu->arch.exit_data;
  519. if (p->exit_reason == EXIT_REASON_SAL_CALL) {
  520. vcpu_set_gr(vcpu, 8, p->u.sal_data.ret.r8, 0);
  521. vcpu_set_gr(vcpu, 9, p->u.sal_data.ret.r9, 0);
  522. vcpu_set_gr(vcpu, 10, p->u.sal_data.ret.r10, 0);
  523. vcpu_set_gr(vcpu, 11, p->u.sal_data.ret.r11, 0);
  524. } else
  525. panic_vm(vcpu);
  526. }
  527. void kvm_ia64_handle_break(unsigned long ifa, struct kvm_pt_regs *regs,
  528. unsigned long isr, unsigned long iim)
  529. {
  530. struct kvm_vcpu *v = current_vcpu;
  531. if (ia64_psr(regs)->cpl == 0) {
  532. /* Allow hypercalls only when cpl = 0. */
  533. if (iim == DOMN_PAL_REQUEST) {
  534. set_pal_call_data(v);
  535. vmm_transition(v);
  536. set_pal_call_result(v);
  537. vcpu_increment_iip(v);
  538. return;
  539. } else if (iim == DOMN_SAL_REQUEST) {
  540. set_sal_call_data(v);
  541. vmm_transition(v);
  542. set_sal_call_result(v);
  543. vcpu_increment_iip(v);
  544. return;
  545. }
  546. }
  547. reflect_interruption(ifa, isr, iim, 11, regs);
  548. }
  549. void check_pending_irq(struct kvm_vcpu *vcpu)
  550. {
  551. int mask, h_pending, h_inservice;
  552. u64 isr;
  553. unsigned long vpsr;
  554. struct kvm_pt_regs *regs = vcpu_regs(vcpu);
  555. h_pending = highest_pending_irq(vcpu);
  556. if (h_pending == NULL_VECTOR) {
  557. update_vhpi(vcpu, NULL_VECTOR);
  558. return;
  559. }
  560. h_inservice = highest_inservice_irq(vcpu);
  561. vpsr = VCPU(vcpu, vpsr);
  562. mask = irq_masked(vcpu, h_pending, h_inservice);
  563. if ((vpsr & IA64_PSR_I) && IRQ_NO_MASKED == mask) {
  564. isr = vpsr & IA64_PSR_RI;
  565. update_vhpi(vcpu, h_pending);
  566. reflect_interruption(0, isr, 0, 12, regs); /* EXT IRQ */
  567. } else if (mask == IRQ_MASKED_BY_INSVC) {
  568. if (VCPU(vcpu, vhpi))
  569. update_vhpi(vcpu, NULL_VECTOR);
  570. } else {
  571. /* masked by vpsr.i or vtpr.*/
  572. update_vhpi(vcpu, h_pending);
  573. }
  574. }
  575. static void generate_exirq(struct kvm_vcpu *vcpu)
  576. {
  577. unsigned vpsr;
  578. uint64_t isr;
  579. struct kvm_pt_regs *regs = vcpu_regs(vcpu);
  580. vpsr = VCPU(vcpu, vpsr);
  581. isr = vpsr & IA64_PSR_RI;
  582. if (!(vpsr & IA64_PSR_IC))
  583. panic_vm(vcpu);
  584. reflect_interruption(0, isr, 0, 12, regs); /* EXT IRQ */
  585. }
  586. void vhpi_detection(struct kvm_vcpu *vcpu)
  587. {
  588. uint64_t threshold, vhpi;
  589. union ia64_tpr vtpr;
  590. struct ia64_psr vpsr;
  591. vpsr = *(struct ia64_psr *)&VCPU(vcpu, vpsr);
  592. vtpr.val = VCPU(vcpu, tpr);
  593. threshold = ((!vpsr.i) << 5) | (vtpr.mmi << 4) | vtpr.mic;
  594. vhpi = VCPU(vcpu, vhpi);
  595. if (vhpi > threshold) {
  596. /* interrupt actived*/
  597. generate_exirq(vcpu);
  598. }
  599. }
  600. void leave_hypervisor_tail(void)
  601. {
  602. struct kvm_vcpu *v = current_vcpu;
  603. if (VMX(v, timer_check)) {
  604. VMX(v, timer_check) = 0;
  605. if (VMX(v, itc_check)) {
  606. if (vcpu_get_itc(v) > VCPU(v, itm)) {
  607. if (!(VCPU(v, itv) & (1 << 16))) {
  608. vcpu_pend_interrupt(v, VCPU(v, itv)
  609. & 0xff);
  610. VMX(v, itc_check) = 0;
  611. } else {
  612. v->arch.timer_pending = 1;
  613. }
  614. VMX(v, last_itc) = VCPU(v, itm) + 1;
  615. }
  616. }
  617. }
  618. rmb();
  619. if (v->arch.irq_new_pending) {
  620. v->arch.irq_new_pending = 0;
  621. VMX(v, irq_check) = 0;
  622. check_pending_irq(v);
  623. return;
  624. }
  625. if (VMX(v, irq_check)) {
  626. VMX(v, irq_check) = 0;
  627. vhpi_detection(v);
  628. }
  629. }
  630. static inline void handle_lds(struct kvm_pt_regs *regs)
  631. {
  632. regs->cr_ipsr |= IA64_PSR_ED;
  633. }
  634. void physical_tlb_miss(struct kvm_vcpu *vcpu, unsigned long vadr, int type)
  635. {
  636. unsigned long pte;
  637. union ia64_rr rr;
  638. rr.val = ia64_get_rr(vadr);
  639. pte = vadr & _PAGE_PPN_MASK;
  640. pte = pte | PHY_PAGE_WB;
  641. thash_vhpt_insert(vcpu, pte, (u64)(rr.ps << 2), vadr, type);
  642. return;
  643. }
  644. void kvm_page_fault(u64 vadr , u64 vec, struct kvm_pt_regs *regs)
  645. {
  646. unsigned long vpsr;
  647. int type;
  648. u64 vhpt_adr, gppa, pteval, rr, itir;
  649. union ia64_isr misr;
  650. union ia64_pta vpta;
  651. struct thash_data *data;
  652. struct kvm_vcpu *v = current_vcpu;
  653. vpsr = VCPU(v, vpsr);
  654. misr.val = VMX(v, cr_isr);
  655. type = vec;
  656. if (is_physical_mode(v) && (!(vadr << 1 >> 62))) {
  657. if (vec == 2) {
  658. if (__gpfn_is_io((vadr << 1) >> (PAGE_SHIFT + 1))) {
  659. emulate_io_inst(v, ((vadr << 1) >> 1), 4);
  660. return;
  661. }
  662. }
  663. physical_tlb_miss(v, vadr, type);
  664. return;
  665. }
  666. data = vtlb_lookup(v, vadr, type);
  667. if (data != 0) {
  668. if (type == D_TLB) {
  669. gppa = (vadr & ((1UL << data->ps) - 1))
  670. + (data->ppn >> (data->ps - 12) << data->ps);
  671. if (__gpfn_is_io(gppa >> PAGE_SHIFT)) {
  672. if (data->pl >= ((regs->cr_ipsr >>
  673. IA64_PSR_CPL0_BIT) & 3))
  674. emulate_io_inst(v, gppa, data->ma);
  675. else {
  676. vcpu_set_isr(v, misr.val);
  677. data_access_rights(v, vadr);
  678. }
  679. return ;
  680. }
  681. }
  682. thash_vhpt_insert(v, data->page_flags, data->itir, vadr, type);
  683. } else if (type == D_TLB) {
  684. if (misr.sp) {
  685. handle_lds(regs);
  686. return;
  687. }
  688. rr = vcpu_get_rr(v, vadr);
  689. itir = rr & (RR_RID_MASK | RR_PS_MASK);
  690. if (!vhpt_enabled(v, vadr, misr.rs ? RSE_REF : DATA_REF)) {
  691. if (vpsr & IA64_PSR_IC) {
  692. vcpu_set_isr(v, misr.val);
  693. alt_dtlb(v, vadr);
  694. } else {
  695. nested_dtlb(v);
  696. }
  697. return ;
  698. }
  699. vpta.val = vcpu_get_pta(v);
  700. /* avoid recursively walking (short format) VHPT */
  701. vhpt_adr = vcpu_thash(v, vadr);
  702. if (!guest_vhpt_lookup(vhpt_adr, &pteval)) {
  703. /* VHPT successfully read. */
  704. if (!(pteval & _PAGE_P)) {
  705. if (vpsr & IA64_PSR_IC) {
  706. vcpu_set_isr(v, misr.val);
  707. dtlb_fault(v, vadr);
  708. } else {
  709. nested_dtlb(v);
  710. }
  711. } else if ((pteval & _PAGE_MA_MASK) != _PAGE_MA_ST) {
  712. thash_purge_and_insert(v, pteval, itir,
  713. vadr, D_TLB);
  714. } else if (vpsr & IA64_PSR_IC) {
  715. vcpu_set_isr(v, misr.val);
  716. dtlb_fault(v, vadr);
  717. } else {
  718. nested_dtlb(v);
  719. }
  720. } else {
  721. /* Can't read VHPT. */
  722. if (vpsr & IA64_PSR_IC) {
  723. vcpu_set_isr(v, misr.val);
  724. dvhpt_fault(v, vadr);
  725. } else {
  726. nested_dtlb(v);
  727. }
  728. }
  729. } else if (type == I_TLB) {
  730. if (!(vpsr & IA64_PSR_IC))
  731. misr.ni = 1;
  732. if (!vhpt_enabled(v, vadr, INST_REF)) {
  733. vcpu_set_isr(v, misr.val);
  734. alt_itlb(v, vadr);
  735. return;
  736. }
  737. vpta.val = vcpu_get_pta(v);
  738. vhpt_adr = vcpu_thash(v, vadr);
  739. if (!guest_vhpt_lookup(vhpt_adr, &pteval)) {
  740. /* VHPT successfully read. */
  741. if (pteval & _PAGE_P) {
  742. if ((pteval & _PAGE_MA_MASK) == _PAGE_MA_ST) {
  743. vcpu_set_isr(v, misr.val);
  744. itlb_fault(v, vadr);
  745. return ;
  746. }
  747. rr = vcpu_get_rr(v, vadr);
  748. itir = rr & (RR_RID_MASK | RR_PS_MASK);
  749. thash_purge_and_insert(v, pteval, itir,
  750. vadr, I_TLB);
  751. } else {
  752. vcpu_set_isr(v, misr.val);
  753. inst_page_not_present(v, vadr);
  754. }
  755. } else {
  756. vcpu_set_isr(v, misr.val);
  757. ivhpt_fault(v, vadr);
  758. }
  759. }
  760. }
  761. void kvm_vexirq(struct kvm_vcpu *vcpu)
  762. {
  763. u64 vpsr, isr;
  764. struct kvm_pt_regs *regs;
  765. regs = vcpu_regs(vcpu);
  766. vpsr = VCPU(vcpu, vpsr);
  767. isr = vpsr & IA64_PSR_RI;
  768. reflect_interruption(0, isr, 0, 12, regs); /*EXT IRQ*/
  769. }
  770. void kvm_ia64_handle_irq(struct kvm_vcpu *v)
  771. {
  772. struct exit_ctl_data *p = &v->arch.exit_data;
  773. long psr;
  774. local_irq_save(psr);
  775. p->exit_reason = EXIT_REASON_EXTERNAL_INTERRUPT;
  776. vmm_transition(v);
  777. local_irq_restore(psr);
  778. VMX(v, timer_check) = 1;
  779. }
  780. static void ptc_ga_remote_func(struct kvm_vcpu *v, int pos)
  781. {
  782. u64 oldrid, moldrid, oldpsbits, vaddr;
  783. struct kvm_ptc_g *p = &v->arch.ptc_g_data[pos];
  784. vaddr = p->vaddr;
  785. oldrid = VMX(v, vrr[0]);
  786. VMX(v, vrr[0]) = p->rr;
  787. oldpsbits = VMX(v, psbits[0]);
  788. VMX(v, psbits[0]) = VMX(v, psbits[REGION_NUMBER(vaddr)]);
  789. moldrid = ia64_get_rr(0x0);
  790. ia64_set_rr(0x0, vrrtomrr(p->rr));
  791. ia64_srlz_d();
  792. vaddr = PAGEALIGN(vaddr, p->ps);
  793. thash_purge_entries_remote(v, vaddr, p->ps);
  794. VMX(v, vrr[0]) = oldrid;
  795. VMX(v, psbits[0]) = oldpsbits;
  796. ia64_set_rr(0x0, moldrid);
  797. ia64_dv_serialize_data();
  798. }
  799. static void vcpu_do_resume(struct kvm_vcpu *vcpu)
  800. {
  801. /*Re-init VHPT and VTLB once from resume*/
  802. vcpu->arch.vhpt.num = VHPT_NUM_ENTRIES;
  803. thash_init(&vcpu->arch.vhpt, VHPT_SHIFT);
  804. vcpu->arch.vtlb.num = VTLB_NUM_ENTRIES;
  805. thash_init(&vcpu->arch.vtlb, VTLB_SHIFT);
  806. ia64_set_pta(vcpu->arch.vhpt.pta.val);
  807. }
  808. static void kvm_do_resume_op(struct kvm_vcpu *vcpu)
  809. {
  810. if (test_and_clear_bit(KVM_REQ_RESUME, &vcpu->requests)) {
  811. vcpu_do_resume(vcpu);
  812. return;
  813. }
  814. if (unlikely(test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))) {
  815. thash_purge_all(vcpu);
  816. return;
  817. }
  818. if (test_and_clear_bit(KVM_REQ_PTC_G, &vcpu->requests)) {
  819. while (vcpu->arch.ptc_g_count > 0)
  820. ptc_ga_remote_func(vcpu, --vcpu->arch.ptc_g_count);
  821. }
  822. }
  823. void vmm_transition(struct kvm_vcpu *vcpu)
  824. {
  825. ia64_call_vsa(PAL_VPS_SAVE, (unsigned long)vcpu->arch.vpd,
  826. 0, 0, 0, 0, 0, 0);
  827. vmm_trampoline(&vcpu->arch.guest, &vcpu->arch.host);
  828. ia64_call_vsa(PAL_VPS_RESTORE, (unsigned long)vcpu->arch.vpd,
  829. 0, 0, 0, 0, 0, 0);
  830. kvm_do_resume_op(vcpu);
  831. }