gaccess.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. /*
  2. * guest access functions
  3. *
  4. * Copyright IBM Corp. 2014
  5. *
  6. */
  7. #include <linux/vmalloc.h>
  8. #include <linux/err.h>
  9. #include <asm/pgtable.h>
  10. #include "kvm-s390.h"
  11. #include "gaccess.h"
  12. union asce {
  13. unsigned long val;
  14. struct {
  15. unsigned long origin : 52; /* Region- or Segment-Table Origin */
  16. unsigned long : 2;
  17. unsigned long g : 1; /* Subspace Group Control */
  18. unsigned long p : 1; /* Private Space Control */
  19. unsigned long s : 1; /* Storage-Alteration-Event Control */
  20. unsigned long x : 1; /* Space-Switch-Event Control */
  21. unsigned long r : 1; /* Real-Space Control */
  22. unsigned long : 1;
  23. unsigned long dt : 2; /* Designation-Type Control */
  24. unsigned long tl : 2; /* Region- or Segment-Table Length */
  25. };
  26. };
  27. enum {
  28. ASCE_TYPE_SEGMENT = 0,
  29. ASCE_TYPE_REGION3 = 1,
  30. ASCE_TYPE_REGION2 = 2,
  31. ASCE_TYPE_REGION1 = 3
  32. };
  33. union region1_table_entry {
  34. unsigned long val;
  35. struct {
  36. unsigned long rto: 52;/* Region-Table Origin */
  37. unsigned long : 2;
  38. unsigned long p : 1; /* DAT-Protection Bit */
  39. unsigned long : 1;
  40. unsigned long tf : 2; /* Region-Second-Table Offset */
  41. unsigned long i : 1; /* Region-Invalid Bit */
  42. unsigned long : 1;
  43. unsigned long tt : 2; /* Table-Type Bits */
  44. unsigned long tl : 2; /* Region-Second-Table Length */
  45. };
  46. };
  47. union region2_table_entry {
  48. unsigned long val;
  49. struct {
  50. unsigned long rto: 52;/* Region-Table Origin */
  51. unsigned long : 2;
  52. unsigned long p : 1; /* DAT-Protection Bit */
  53. unsigned long : 1;
  54. unsigned long tf : 2; /* Region-Third-Table Offset */
  55. unsigned long i : 1; /* Region-Invalid Bit */
  56. unsigned long : 1;
  57. unsigned long tt : 2; /* Table-Type Bits */
  58. unsigned long tl : 2; /* Region-Third-Table Length */
  59. };
  60. };
  61. struct region3_table_entry_fc0 {
  62. unsigned long sto: 52;/* Segment-Table Origin */
  63. unsigned long : 1;
  64. unsigned long fc : 1; /* Format-Control */
  65. unsigned long p : 1; /* DAT-Protection Bit */
  66. unsigned long : 1;
  67. unsigned long tf : 2; /* Segment-Table Offset */
  68. unsigned long i : 1; /* Region-Invalid Bit */
  69. unsigned long cr : 1; /* Common-Region Bit */
  70. unsigned long tt : 2; /* Table-Type Bits */
  71. unsigned long tl : 2; /* Segment-Table Length */
  72. };
  73. struct region3_table_entry_fc1 {
  74. unsigned long rfaa : 33; /* Region-Frame Absolute Address */
  75. unsigned long : 14;
  76. unsigned long av : 1; /* ACCF-Validity Control */
  77. unsigned long acc: 4; /* Access-Control Bits */
  78. unsigned long f : 1; /* Fetch-Protection Bit */
  79. unsigned long fc : 1; /* Format-Control */
  80. unsigned long p : 1; /* DAT-Protection Bit */
  81. unsigned long co : 1; /* Change-Recording Override */
  82. unsigned long : 2;
  83. unsigned long i : 1; /* Region-Invalid Bit */
  84. unsigned long cr : 1; /* Common-Region Bit */
  85. unsigned long tt : 2; /* Table-Type Bits */
  86. unsigned long : 2;
  87. };
  88. union region3_table_entry {
  89. unsigned long val;
  90. struct region3_table_entry_fc0 fc0;
  91. struct region3_table_entry_fc1 fc1;
  92. struct {
  93. unsigned long : 53;
  94. unsigned long fc : 1; /* Format-Control */
  95. unsigned long : 4;
  96. unsigned long i : 1; /* Region-Invalid Bit */
  97. unsigned long cr : 1; /* Common-Region Bit */
  98. unsigned long tt : 2; /* Table-Type Bits */
  99. unsigned long : 2;
  100. };
  101. };
  102. struct segment_entry_fc0 {
  103. unsigned long pto: 53;/* Page-Table Origin */
  104. unsigned long fc : 1; /* Format-Control */
  105. unsigned long p : 1; /* DAT-Protection Bit */
  106. unsigned long : 3;
  107. unsigned long i : 1; /* Segment-Invalid Bit */
  108. unsigned long cs : 1; /* Common-Segment Bit */
  109. unsigned long tt : 2; /* Table-Type Bits */
  110. unsigned long : 2;
  111. };
  112. struct segment_entry_fc1 {
  113. unsigned long sfaa : 44; /* Segment-Frame Absolute Address */
  114. unsigned long : 3;
  115. unsigned long av : 1; /* ACCF-Validity Control */
  116. unsigned long acc: 4; /* Access-Control Bits */
  117. unsigned long f : 1; /* Fetch-Protection Bit */
  118. unsigned long fc : 1; /* Format-Control */
  119. unsigned long p : 1; /* DAT-Protection Bit */
  120. unsigned long co : 1; /* Change-Recording Override */
  121. unsigned long : 2;
  122. unsigned long i : 1; /* Segment-Invalid Bit */
  123. unsigned long cs : 1; /* Common-Segment Bit */
  124. unsigned long tt : 2; /* Table-Type Bits */
  125. unsigned long : 2;
  126. };
  127. union segment_table_entry {
  128. unsigned long val;
  129. struct segment_entry_fc0 fc0;
  130. struct segment_entry_fc1 fc1;
  131. struct {
  132. unsigned long : 53;
  133. unsigned long fc : 1; /* Format-Control */
  134. unsigned long : 4;
  135. unsigned long i : 1; /* Segment-Invalid Bit */
  136. unsigned long cs : 1; /* Common-Segment Bit */
  137. unsigned long tt : 2; /* Table-Type Bits */
  138. unsigned long : 2;
  139. };
  140. };
  141. enum {
  142. TABLE_TYPE_SEGMENT = 0,
  143. TABLE_TYPE_REGION3 = 1,
  144. TABLE_TYPE_REGION2 = 2,
  145. TABLE_TYPE_REGION1 = 3
  146. };
  147. union page_table_entry {
  148. unsigned long val;
  149. struct {
  150. unsigned long pfra : 52; /* Page-Frame Real Address */
  151. unsigned long z : 1; /* Zero Bit */
  152. unsigned long i : 1; /* Page-Invalid Bit */
  153. unsigned long p : 1; /* DAT-Protection Bit */
  154. unsigned long co : 1; /* Change-Recording Override */
  155. unsigned long : 8;
  156. };
  157. };
  158. /*
  159. * vaddress union in order to easily decode a virtual address into its
  160. * region first index, region second index etc. parts.
  161. */
  162. union vaddress {
  163. unsigned long addr;
  164. struct {
  165. unsigned long rfx : 11;
  166. unsigned long rsx : 11;
  167. unsigned long rtx : 11;
  168. unsigned long sx : 11;
  169. unsigned long px : 8;
  170. unsigned long bx : 12;
  171. };
  172. struct {
  173. unsigned long rfx01 : 2;
  174. unsigned long : 9;
  175. unsigned long rsx01 : 2;
  176. unsigned long : 9;
  177. unsigned long rtx01 : 2;
  178. unsigned long : 9;
  179. unsigned long sx01 : 2;
  180. unsigned long : 29;
  181. };
  182. };
  183. /*
  184. * raddress union which will contain the result (real or absolute address)
  185. * after a page table walk. The rfaa, sfaa and pfra members are used to
  186. * simply assign them the value of a region, segment or page table entry.
  187. */
  188. union raddress {
  189. unsigned long addr;
  190. unsigned long rfaa : 33; /* Region-Frame Absolute Address */
  191. unsigned long sfaa : 44; /* Segment-Frame Absolute Address */
  192. unsigned long pfra : 52; /* Page-Frame Real Address */
  193. };
  194. static int ipte_lock_count;
  195. static DEFINE_MUTEX(ipte_mutex);
  196. int ipte_lock_held(struct kvm_vcpu *vcpu)
  197. {
  198. union ipte_control *ic = &vcpu->kvm->arch.sca->ipte_control;
  199. if (vcpu->arch.sie_block->eca & 1)
  200. return ic->kh != 0;
  201. return ipte_lock_count != 0;
  202. }
  203. static void ipte_lock_simple(struct kvm_vcpu *vcpu)
  204. {
  205. union ipte_control old, new, *ic;
  206. mutex_lock(&ipte_mutex);
  207. ipte_lock_count++;
  208. if (ipte_lock_count > 1)
  209. goto out;
  210. ic = &vcpu->kvm->arch.sca->ipte_control;
  211. do {
  212. old = *ic;
  213. barrier();
  214. while (old.k) {
  215. cond_resched();
  216. old = *ic;
  217. barrier();
  218. }
  219. new = old;
  220. new.k = 1;
  221. } while (cmpxchg(&ic->val, old.val, new.val) != old.val);
  222. out:
  223. mutex_unlock(&ipte_mutex);
  224. }
  225. static void ipte_unlock_simple(struct kvm_vcpu *vcpu)
  226. {
  227. union ipte_control old, new, *ic;
  228. mutex_lock(&ipte_mutex);
  229. ipte_lock_count--;
  230. if (ipte_lock_count)
  231. goto out;
  232. ic = &vcpu->kvm->arch.sca->ipte_control;
  233. do {
  234. old = *ic;
  235. barrier();
  236. new = old;
  237. new.k = 0;
  238. } while (cmpxchg(&ic->val, old.val, new.val) != old.val);
  239. wake_up(&vcpu->kvm->arch.ipte_wq);
  240. out:
  241. mutex_unlock(&ipte_mutex);
  242. }
  243. static void ipte_lock_siif(struct kvm_vcpu *vcpu)
  244. {
  245. union ipte_control old, new, *ic;
  246. ic = &vcpu->kvm->arch.sca->ipte_control;
  247. do {
  248. old = *ic;
  249. barrier();
  250. while (old.kg) {
  251. cond_resched();
  252. old = *ic;
  253. barrier();
  254. }
  255. new = old;
  256. new.k = 1;
  257. new.kh++;
  258. } while (cmpxchg(&ic->val, old.val, new.val) != old.val);
  259. }
  260. static void ipte_unlock_siif(struct kvm_vcpu *vcpu)
  261. {
  262. union ipte_control old, new, *ic;
  263. ic = &vcpu->kvm->arch.sca->ipte_control;
  264. do {
  265. old = *ic;
  266. barrier();
  267. new = old;
  268. new.kh--;
  269. if (!new.kh)
  270. new.k = 0;
  271. } while (cmpxchg(&ic->val, old.val, new.val) != old.val);
  272. if (!new.kh)
  273. wake_up(&vcpu->kvm->arch.ipte_wq);
  274. }
  275. void ipte_lock(struct kvm_vcpu *vcpu)
  276. {
  277. if (vcpu->arch.sie_block->eca & 1)
  278. ipte_lock_siif(vcpu);
  279. else
  280. ipte_lock_simple(vcpu);
  281. }
  282. void ipte_unlock(struct kvm_vcpu *vcpu)
  283. {
  284. if (vcpu->arch.sie_block->eca & 1)
  285. ipte_unlock_siif(vcpu);
  286. else
  287. ipte_unlock_simple(vcpu);
  288. }
  289. static unsigned long get_vcpu_asce(struct kvm_vcpu *vcpu)
  290. {
  291. switch (psw_bits(vcpu->arch.sie_block->gpsw).as) {
  292. case PSW_AS_PRIMARY:
  293. return vcpu->arch.sie_block->gcr[1];
  294. case PSW_AS_SECONDARY:
  295. return vcpu->arch.sie_block->gcr[7];
  296. case PSW_AS_HOME:
  297. return vcpu->arch.sie_block->gcr[13];
  298. }
  299. return 0;
  300. }
  301. static int deref_table(struct kvm *kvm, unsigned long gpa, unsigned long *val)
  302. {
  303. return kvm_read_guest(kvm, gpa, val, sizeof(*val));
  304. }
  305. /**
  306. * guest_translate - translate a guest virtual into a guest absolute address
  307. * @vcpu: virtual cpu
  308. * @gva: guest virtual address
  309. * @gpa: points to where guest physical (absolute) address should be stored
  310. * @write: indicates if access is a write access
  311. *
  312. * Translate a guest virtual address into a guest absolute address by means
  313. * of dynamic address translation as specified by the architecuture.
  314. * If the resulting absolute address is not available in the configuration
  315. * an addressing exception is indicated and @gpa will not be changed.
  316. *
  317. * Returns: - zero on success; @gpa contains the resulting absolute address
  318. * - a negative value if guest access failed due to e.g. broken
  319. * guest mapping
  320. * - a positve value if an access exception happened. In this case
  321. * the returned value is the program interruption code as defined
  322. * by the architecture
  323. */
  324. static unsigned long guest_translate(struct kvm_vcpu *vcpu, unsigned long gva,
  325. unsigned long *gpa, int write)
  326. {
  327. union vaddress vaddr = {.addr = gva};
  328. union raddress raddr = {.addr = gva};
  329. union page_table_entry pte;
  330. int dat_protection = 0;
  331. union ctlreg0 ctlreg0;
  332. unsigned long ptr;
  333. int edat1, edat2;
  334. union asce asce;
  335. ctlreg0.val = vcpu->arch.sie_block->gcr[0];
  336. edat1 = ctlreg0.edat && test_vfacility(8);
  337. edat2 = edat1 && test_vfacility(78);
  338. asce.val = get_vcpu_asce(vcpu);
  339. if (asce.r)
  340. goto real_address;
  341. ptr = asce.origin * 4096;
  342. switch (asce.dt) {
  343. case ASCE_TYPE_REGION1:
  344. if (vaddr.rfx01 > asce.tl)
  345. return PGM_REGION_FIRST_TRANS;
  346. ptr += vaddr.rfx * 8;
  347. break;
  348. case ASCE_TYPE_REGION2:
  349. if (vaddr.rfx)
  350. return PGM_ASCE_TYPE;
  351. if (vaddr.rsx01 > asce.tl)
  352. return PGM_REGION_SECOND_TRANS;
  353. ptr += vaddr.rsx * 8;
  354. break;
  355. case ASCE_TYPE_REGION3:
  356. if (vaddr.rfx || vaddr.rsx)
  357. return PGM_ASCE_TYPE;
  358. if (vaddr.rtx01 > asce.tl)
  359. return PGM_REGION_THIRD_TRANS;
  360. ptr += vaddr.rtx * 8;
  361. break;
  362. case ASCE_TYPE_SEGMENT:
  363. if (vaddr.rfx || vaddr.rsx || vaddr.rtx)
  364. return PGM_ASCE_TYPE;
  365. if (vaddr.sx01 > asce.tl)
  366. return PGM_SEGMENT_TRANSLATION;
  367. ptr += vaddr.sx * 8;
  368. break;
  369. }
  370. switch (asce.dt) {
  371. case ASCE_TYPE_REGION1: {
  372. union region1_table_entry rfte;
  373. if (kvm_is_error_gpa(vcpu->kvm, ptr))
  374. return PGM_ADDRESSING;
  375. if (deref_table(vcpu->kvm, ptr, &rfte.val))
  376. return -EFAULT;
  377. if (rfte.i)
  378. return PGM_REGION_FIRST_TRANS;
  379. if (rfte.tt != TABLE_TYPE_REGION1)
  380. return PGM_TRANSLATION_SPEC;
  381. if (vaddr.rsx01 < rfte.tf || vaddr.rsx01 > rfte.tl)
  382. return PGM_REGION_SECOND_TRANS;
  383. if (edat1)
  384. dat_protection |= rfte.p;
  385. ptr = rfte.rto * 4096 + vaddr.rsx * 8;
  386. }
  387. /* fallthrough */
  388. case ASCE_TYPE_REGION2: {
  389. union region2_table_entry rste;
  390. if (kvm_is_error_gpa(vcpu->kvm, ptr))
  391. return PGM_ADDRESSING;
  392. if (deref_table(vcpu->kvm, ptr, &rste.val))
  393. return -EFAULT;
  394. if (rste.i)
  395. return PGM_REGION_SECOND_TRANS;
  396. if (rste.tt != TABLE_TYPE_REGION2)
  397. return PGM_TRANSLATION_SPEC;
  398. if (vaddr.rtx01 < rste.tf || vaddr.rtx01 > rste.tl)
  399. return PGM_REGION_THIRD_TRANS;
  400. if (edat1)
  401. dat_protection |= rste.p;
  402. ptr = rste.rto * 4096 + vaddr.rtx * 8;
  403. }
  404. /* fallthrough */
  405. case ASCE_TYPE_REGION3: {
  406. union region3_table_entry rtte;
  407. if (kvm_is_error_gpa(vcpu->kvm, ptr))
  408. return PGM_ADDRESSING;
  409. if (deref_table(vcpu->kvm, ptr, &rtte.val))
  410. return -EFAULT;
  411. if (rtte.i)
  412. return PGM_REGION_THIRD_TRANS;
  413. if (rtte.tt != TABLE_TYPE_REGION3)
  414. return PGM_TRANSLATION_SPEC;
  415. if (rtte.cr && asce.p && edat2)
  416. return PGM_TRANSLATION_SPEC;
  417. if (rtte.fc && edat2) {
  418. dat_protection |= rtte.fc1.p;
  419. raddr.rfaa = rtte.fc1.rfaa;
  420. goto absolute_address;
  421. }
  422. if (vaddr.sx01 < rtte.fc0.tf)
  423. return PGM_SEGMENT_TRANSLATION;
  424. if (vaddr.sx01 > rtte.fc0.tl)
  425. return PGM_SEGMENT_TRANSLATION;
  426. if (edat1)
  427. dat_protection |= rtte.fc0.p;
  428. ptr = rtte.fc0.sto * 4096 + vaddr.sx * 8;
  429. }
  430. /* fallthrough */
  431. case ASCE_TYPE_SEGMENT: {
  432. union segment_table_entry ste;
  433. if (kvm_is_error_gpa(vcpu->kvm, ptr))
  434. return PGM_ADDRESSING;
  435. if (deref_table(vcpu->kvm, ptr, &ste.val))
  436. return -EFAULT;
  437. if (ste.i)
  438. return PGM_SEGMENT_TRANSLATION;
  439. if (ste.tt != TABLE_TYPE_SEGMENT)
  440. return PGM_TRANSLATION_SPEC;
  441. if (ste.cs && asce.p)
  442. return PGM_TRANSLATION_SPEC;
  443. if (ste.fc && edat1) {
  444. dat_protection |= ste.fc1.p;
  445. raddr.sfaa = ste.fc1.sfaa;
  446. goto absolute_address;
  447. }
  448. dat_protection |= ste.fc0.p;
  449. ptr = ste.fc0.pto * 2048 + vaddr.px * 8;
  450. }
  451. }
  452. if (kvm_is_error_gpa(vcpu->kvm, ptr))
  453. return PGM_ADDRESSING;
  454. if (deref_table(vcpu->kvm, ptr, &pte.val))
  455. return -EFAULT;
  456. if (pte.i)
  457. return PGM_PAGE_TRANSLATION;
  458. if (pte.z)
  459. return PGM_TRANSLATION_SPEC;
  460. if (pte.co && !edat1)
  461. return PGM_TRANSLATION_SPEC;
  462. dat_protection |= pte.p;
  463. raddr.pfra = pte.pfra;
  464. real_address:
  465. raddr.addr = kvm_s390_real_to_abs(vcpu, raddr.addr);
  466. absolute_address:
  467. if (write && dat_protection)
  468. return PGM_PROTECTION;
  469. if (kvm_is_error_gpa(vcpu->kvm, raddr.addr))
  470. return PGM_ADDRESSING;
  471. *gpa = raddr.addr;
  472. return 0;
  473. }
  474. static inline int is_low_address(unsigned long ga)
  475. {
  476. /* Check for address ranges 0..511 and 4096..4607 */
  477. return (ga & ~0x11fful) == 0;
  478. }
  479. static int low_address_protection_enabled(struct kvm_vcpu *vcpu)
  480. {
  481. union ctlreg0 ctlreg0 = {.val = vcpu->arch.sie_block->gcr[0]};
  482. psw_t *psw = &vcpu->arch.sie_block->gpsw;
  483. union asce asce;
  484. if (!ctlreg0.lap)
  485. return 0;
  486. asce.val = get_vcpu_asce(vcpu);
  487. if (psw_bits(*psw).t && asce.p)
  488. return 0;
  489. return 1;
  490. }
  491. struct trans_exc_code_bits {
  492. unsigned long addr : 52; /* Translation-exception Address */
  493. unsigned long fsi : 2; /* Access Exception Fetch/Store Indication */
  494. unsigned long : 7;
  495. unsigned long b61 : 1;
  496. unsigned long as : 2; /* ASCE Identifier */
  497. };
  498. enum {
  499. FSI_UNKNOWN = 0, /* Unknown wether fetch or store */
  500. FSI_STORE = 1, /* Exception was due to store operation */
  501. FSI_FETCH = 2 /* Exception was due to fetch operation */
  502. };
  503. static int guest_page_range(struct kvm_vcpu *vcpu, unsigned long ga,
  504. unsigned long *pages, unsigned long nr_pages,
  505. int write)
  506. {
  507. struct kvm_s390_pgm_info *pgm = &vcpu->arch.pgm;
  508. psw_t *psw = &vcpu->arch.sie_block->gpsw;
  509. struct trans_exc_code_bits *tec_bits;
  510. int lap_enabled, rc;
  511. memset(pgm, 0, sizeof(*pgm));
  512. tec_bits = (struct trans_exc_code_bits *)&pgm->trans_exc_code;
  513. tec_bits->fsi = write ? FSI_STORE : FSI_FETCH;
  514. tec_bits->as = psw_bits(*psw).as;
  515. lap_enabled = low_address_protection_enabled(vcpu);
  516. while (nr_pages) {
  517. ga = kvm_s390_logical_to_effective(vcpu, ga);
  518. tec_bits->addr = ga >> PAGE_SHIFT;
  519. if (write && lap_enabled && is_low_address(ga)) {
  520. pgm->code = PGM_PROTECTION;
  521. return pgm->code;
  522. }
  523. ga &= PAGE_MASK;
  524. if (psw_bits(*psw).t) {
  525. rc = guest_translate(vcpu, ga, pages, write);
  526. if (rc < 0)
  527. return rc;
  528. if (rc == PGM_PROTECTION)
  529. tec_bits->b61 = 1;
  530. if (rc)
  531. pgm->code = rc;
  532. } else {
  533. *pages = kvm_s390_real_to_abs(vcpu, ga);
  534. if (kvm_is_error_gpa(vcpu->kvm, *pages))
  535. pgm->code = PGM_ADDRESSING;
  536. }
  537. if (pgm->code)
  538. return pgm->code;
  539. ga += PAGE_SIZE;
  540. pages++;
  541. nr_pages--;
  542. }
  543. return 0;
  544. }
  545. int access_guest(struct kvm_vcpu *vcpu, unsigned long ga, void *data,
  546. unsigned long len, int write)
  547. {
  548. psw_t *psw = &vcpu->arch.sie_block->gpsw;
  549. unsigned long _len, nr_pages, gpa, idx;
  550. unsigned long pages_array[2];
  551. unsigned long *pages;
  552. int need_ipte_lock;
  553. union asce asce;
  554. int rc;
  555. if (!len)
  556. return 0;
  557. /* Access register mode is not supported yet. */
  558. if (psw_bits(*psw).t && psw_bits(*psw).as == PSW_AS_ACCREG)
  559. return -EOPNOTSUPP;
  560. nr_pages = (((ga & ~PAGE_MASK) + len - 1) >> PAGE_SHIFT) + 1;
  561. pages = pages_array;
  562. if (nr_pages > ARRAY_SIZE(pages_array))
  563. pages = vmalloc(nr_pages * sizeof(unsigned long));
  564. if (!pages)
  565. return -ENOMEM;
  566. asce.val = get_vcpu_asce(vcpu);
  567. need_ipte_lock = psw_bits(*psw).t && !asce.r;
  568. if (need_ipte_lock)
  569. ipte_lock(vcpu);
  570. rc = guest_page_range(vcpu, ga, pages, nr_pages, write);
  571. for (idx = 0; idx < nr_pages && !rc; idx++) {
  572. gpa = *(pages + idx) + (ga & ~PAGE_MASK);
  573. _len = min(PAGE_SIZE - (gpa & ~PAGE_MASK), len);
  574. if (write)
  575. rc = kvm_write_guest(vcpu->kvm, gpa, data, _len);
  576. else
  577. rc = kvm_read_guest(vcpu->kvm, gpa, data, _len);
  578. len -= _len;
  579. ga += _len;
  580. data += _len;
  581. }
  582. if (need_ipte_lock)
  583. ipte_unlock(vcpu);
  584. if (nr_pages > ARRAY_SIZE(pages_array))
  585. vfree(pages);
  586. return rc;
  587. }
  588. int access_guest_real(struct kvm_vcpu *vcpu, unsigned long gra,
  589. void *data, unsigned long len, int write)
  590. {
  591. unsigned long _len, gpa;
  592. int rc = 0;
  593. while (len && !rc) {
  594. gpa = kvm_s390_real_to_abs(vcpu, gra);
  595. _len = min(PAGE_SIZE - (gpa & ~PAGE_MASK), len);
  596. if (write)
  597. rc = write_guest_abs(vcpu, gpa, data, _len);
  598. else
  599. rc = read_guest_abs(vcpu, gpa, data, _len);
  600. len -= _len;
  601. gra += _len;
  602. data += _len;
  603. }
  604. return rc;
  605. }
  606. /**
  607. * guest_translate_address - translate guest logical into guest absolute address
  608. *
  609. * Parameter semantics are the same as the ones from guest_translate.
  610. * The memory contents at the guest address are not changed.
  611. *
  612. * Note: The IPTE lock is not taken during this function, so the caller
  613. * has to take care of this.
  614. */
  615. int guest_translate_address(struct kvm_vcpu *vcpu, unsigned long gva,
  616. unsigned long *gpa, int write)
  617. {
  618. struct kvm_s390_pgm_info *pgm = &vcpu->arch.pgm;
  619. psw_t *psw = &vcpu->arch.sie_block->gpsw;
  620. struct trans_exc_code_bits *tec;
  621. union asce asce;
  622. int rc;
  623. /* Access register mode is not supported yet. */
  624. if (psw_bits(*psw).t && psw_bits(*psw).as == PSW_AS_ACCREG)
  625. return -EOPNOTSUPP;
  626. gva = kvm_s390_logical_to_effective(vcpu, gva);
  627. memset(pgm, 0, sizeof(*pgm));
  628. tec = (struct trans_exc_code_bits *)&pgm->trans_exc_code;
  629. tec->as = psw_bits(*psw).as;
  630. tec->fsi = write ? FSI_STORE : FSI_FETCH;
  631. tec->addr = gva >> PAGE_SHIFT;
  632. if (is_low_address(gva) && low_address_protection_enabled(vcpu)) {
  633. if (write) {
  634. rc = pgm->code = PGM_PROTECTION;
  635. return rc;
  636. }
  637. }
  638. asce.val = get_vcpu_asce(vcpu);
  639. if (psw_bits(*psw).t && !asce.r) { /* Use DAT? */
  640. rc = guest_translate(vcpu, gva, gpa, write);
  641. if (rc > 0) {
  642. if (rc == PGM_PROTECTION)
  643. tec->b61 = 1;
  644. pgm->code = rc;
  645. }
  646. } else {
  647. rc = 0;
  648. *gpa = kvm_s390_real_to_abs(vcpu, gva);
  649. if (kvm_is_error_gpa(vcpu->kvm, *gpa))
  650. rc = pgm->code = PGM_ADDRESSING;
  651. }
  652. return rc;
  653. }
  654. /**
  655. * kvm_s390_check_low_addr_protection - check for low-address protection
  656. * @ga: Guest address
  657. *
  658. * Checks whether an address is subject to low-address protection and set
  659. * up vcpu->arch.pgm accordingly if necessary.
  660. *
  661. * Return: 0 if no protection exception, or PGM_PROTECTION if protected.
  662. */
  663. int kvm_s390_check_low_addr_protection(struct kvm_vcpu *vcpu, unsigned long ga)
  664. {
  665. struct kvm_s390_pgm_info *pgm = &vcpu->arch.pgm;
  666. psw_t *psw = &vcpu->arch.sie_block->gpsw;
  667. struct trans_exc_code_bits *tec_bits;
  668. if (!is_low_address(ga) || !low_address_protection_enabled(vcpu))
  669. return 0;
  670. memset(pgm, 0, sizeof(*pgm));
  671. tec_bits = (struct trans_exc_code_bits *)&pgm->trans_exc_code;
  672. tec_bits->fsi = FSI_STORE;
  673. tec_bits->as = psw_bits(*psw).as;
  674. tec_bits->addr = ga >> PAGE_SHIFT;
  675. pgm->code = PGM_PROTECTION;
  676. return pgm->code;
  677. }