gaccess.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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 = ACCESS_ONCE(*ic);
  213. while (old.k) {
  214. cond_resched();
  215. old = ACCESS_ONCE(*ic);
  216. }
  217. new = old;
  218. new.k = 1;
  219. } while (cmpxchg(&ic->val, old.val, new.val) != old.val);
  220. out:
  221. mutex_unlock(&ipte_mutex);
  222. }
  223. static void ipte_unlock_simple(struct kvm_vcpu *vcpu)
  224. {
  225. union ipte_control old, new, *ic;
  226. mutex_lock(&ipte_mutex);
  227. ipte_lock_count--;
  228. if (ipte_lock_count)
  229. goto out;
  230. ic = &vcpu->kvm->arch.sca->ipte_control;
  231. do {
  232. new = old = ACCESS_ONCE(*ic);
  233. new.k = 0;
  234. } while (cmpxchg(&ic->val, old.val, new.val) != old.val);
  235. wake_up(&vcpu->kvm->arch.ipte_wq);
  236. out:
  237. mutex_unlock(&ipte_mutex);
  238. }
  239. static void ipte_lock_siif(struct kvm_vcpu *vcpu)
  240. {
  241. union ipte_control old, new, *ic;
  242. ic = &vcpu->kvm->arch.sca->ipte_control;
  243. do {
  244. old = ACCESS_ONCE(*ic);
  245. while (old.kg) {
  246. cond_resched();
  247. old = ACCESS_ONCE(*ic);
  248. }
  249. new = old;
  250. new.k = 1;
  251. new.kh++;
  252. } while (cmpxchg(&ic->val, old.val, new.val) != old.val);
  253. }
  254. static void ipte_unlock_siif(struct kvm_vcpu *vcpu)
  255. {
  256. union ipte_control old, new, *ic;
  257. ic = &vcpu->kvm->arch.sca->ipte_control;
  258. do {
  259. new = old = ACCESS_ONCE(*ic);
  260. new.kh--;
  261. if (!new.kh)
  262. new.k = 0;
  263. } while (cmpxchg(&ic->val, old.val, new.val) != old.val);
  264. if (!new.kh)
  265. wake_up(&vcpu->kvm->arch.ipte_wq);
  266. }
  267. void ipte_lock(struct kvm_vcpu *vcpu)
  268. {
  269. if (vcpu->arch.sie_block->eca & 1)
  270. ipte_lock_siif(vcpu);
  271. else
  272. ipte_lock_simple(vcpu);
  273. }
  274. void ipte_unlock(struct kvm_vcpu *vcpu)
  275. {
  276. if (vcpu->arch.sie_block->eca & 1)
  277. ipte_unlock_siif(vcpu);
  278. else
  279. ipte_unlock_simple(vcpu);
  280. }
  281. static unsigned long get_vcpu_asce(struct kvm_vcpu *vcpu)
  282. {
  283. switch (psw_bits(vcpu->arch.sie_block->gpsw).as) {
  284. case PSW_AS_PRIMARY:
  285. return vcpu->arch.sie_block->gcr[1];
  286. case PSW_AS_SECONDARY:
  287. return vcpu->arch.sie_block->gcr[7];
  288. case PSW_AS_HOME:
  289. return vcpu->arch.sie_block->gcr[13];
  290. }
  291. return 0;
  292. }
  293. static int deref_table(struct kvm *kvm, unsigned long gpa, unsigned long *val)
  294. {
  295. return kvm_read_guest(kvm, gpa, val, sizeof(*val));
  296. }
  297. /**
  298. * guest_translate - translate a guest virtual into a guest absolute address
  299. * @vcpu: virtual cpu
  300. * @gva: guest virtual address
  301. * @gpa: points to where guest physical (absolute) address should be stored
  302. * @write: indicates if access is a write access
  303. *
  304. * Translate a guest virtual address into a guest absolute address by means
  305. * of dynamic address translation as specified by the architecuture.
  306. * If the resulting absolute address is not available in the configuration
  307. * an addressing exception is indicated and @gpa will not be changed.
  308. *
  309. * Returns: - zero on success; @gpa contains the resulting absolute address
  310. * - a negative value if guest access failed due to e.g. broken
  311. * guest mapping
  312. * - a positve value if an access exception happened. In this case
  313. * the returned value is the program interruption code as defined
  314. * by the architecture
  315. */
  316. static unsigned long guest_translate(struct kvm_vcpu *vcpu, unsigned long gva,
  317. unsigned long *gpa, int write)
  318. {
  319. union vaddress vaddr = {.addr = gva};
  320. union raddress raddr = {.addr = gva};
  321. union page_table_entry pte;
  322. int dat_protection = 0;
  323. union ctlreg0 ctlreg0;
  324. unsigned long ptr;
  325. int edat1, edat2;
  326. union asce asce;
  327. ctlreg0.val = vcpu->arch.sie_block->gcr[0];
  328. edat1 = ctlreg0.edat && test_vfacility(8);
  329. edat2 = edat1 && test_vfacility(78);
  330. asce.val = get_vcpu_asce(vcpu);
  331. if (asce.r)
  332. goto real_address;
  333. ptr = asce.origin * 4096;
  334. switch (asce.dt) {
  335. case ASCE_TYPE_REGION1:
  336. if (vaddr.rfx01 > asce.tl)
  337. return PGM_REGION_FIRST_TRANS;
  338. ptr += vaddr.rfx * 8;
  339. break;
  340. case ASCE_TYPE_REGION2:
  341. if (vaddr.rfx)
  342. return PGM_ASCE_TYPE;
  343. if (vaddr.rsx01 > asce.tl)
  344. return PGM_REGION_SECOND_TRANS;
  345. ptr += vaddr.rsx * 8;
  346. break;
  347. case ASCE_TYPE_REGION3:
  348. if (vaddr.rfx || vaddr.rsx)
  349. return PGM_ASCE_TYPE;
  350. if (vaddr.rtx01 > asce.tl)
  351. return PGM_REGION_THIRD_TRANS;
  352. ptr += vaddr.rtx * 8;
  353. break;
  354. case ASCE_TYPE_SEGMENT:
  355. if (vaddr.rfx || vaddr.rsx || vaddr.rtx)
  356. return PGM_ASCE_TYPE;
  357. if (vaddr.sx01 > asce.tl)
  358. return PGM_SEGMENT_TRANSLATION;
  359. ptr += vaddr.sx * 8;
  360. break;
  361. }
  362. switch (asce.dt) {
  363. case ASCE_TYPE_REGION1: {
  364. union region1_table_entry rfte;
  365. if (kvm_is_error_gpa(vcpu->kvm, ptr))
  366. return PGM_ADDRESSING;
  367. if (deref_table(vcpu->kvm, ptr, &rfte.val))
  368. return -EFAULT;
  369. if (rfte.i)
  370. return PGM_REGION_FIRST_TRANS;
  371. if (rfte.tt != TABLE_TYPE_REGION1)
  372. return PGM_TRANSLATION_SPEC;
  373. if (vaddr.rsx01 < rfte.tf || vaddr.rsx01 > rfte.tl)
  374. return PGM_REGION_SECOND_TRANS;
  375. if (edat1)
  376. dat_protection |= rfte.p;
  377. ptr = rfte.rto * 4096 + vaddr.rsx * 8;
  378. }
  379. /* fallthrough */
  380. case ASCE_TYPE_REGION2: {
  381. union region2_table_entry rste;
  382. if (kvm_is_error_gpa(vcpu->kvm, ptr))
  383. return PGM_ADDRESSING;
  384. if (deref_table(vcpu->kvm, ptr, &rste.val))
  385. return -EFAULT;
  386. if (rste.i)
  387. return PGM_REGION_SECOND_TRANS;
  388. if (rste.tt != TABLE_TYPE_REGION2)
  389. return PGM_TRANSLATION_SPEC;
  390. if (vaddr.rtx01 < rste.tf || vaddr.rtx01 > rste.tl)
  391. return PGM_REGION_THIRD_TRANS;
  392. if (edat1)
  393. dat_protection |= rste.p;
  394. ptr = rste.rto * 4096 + vaddr.rtx * 8;
  395. }
  396. /* fallthrough */
  397. case ASCE_TYPE_REGION3: {
  398. union region3_table_entry rtte;
  399. if (kvm_is_error_gpa(vcpu->kvm, ptr))
  400. return PGM_ADDRESSING;
  401. if (deref_table(vcpu->kvm, ptr, &rtte.val))
  402. return -EFAULT;
  403. if (rtte.i)
  404. return PGM_REGION_THIRD_TRANS;
  405. if (rtte.tt != TABLE_TYPE_REGION3)
  406. return PGM_TRANSLATION_SPEC;
  407. if (rtte.cr && asce.p && edat2)
  408. return PGM_TRANSLATION_SPEC;
  409. if (rtte.fc && edat2) {
  410. dat_protection |= rtte.fc1.p;
  411. raddr.rfaa = rtte.fc1.rfaa;
  412. goto absolute_address;
  413. }
  414. if (vaddr.sx01 < rtte.fc0.tf)
  415. return PGM_SEGMENT_TRANSLATION;
  416. if (vaddr.sx01 > rtte.fc0.tl)
  417. return PGM_SEGMENT_TRANSLATION;
  418. if (edat1)
  419. dat_protection |= rtte.fc0.p;
  420. ptr = rtte.fc0.sto * 4096 + vaddr.sx * 8;
  421. }
  422. /* fallthrough */
  423. case ASCE_TYPE_SEGMENT: {
  424. union segment_table_entry ste;
  425. if (kvm_is_error_gpa(vcpu->kvm, ptr))
  426. return PGM_ADDRESSING;
  427. if (deref_table(vcpu->kvm, ptr, &ste.val))
  428. return -EFAULT;
  429. if (ste.i)
  430. return PGM_SEGMENT_TRANSLATION;
  431. if (ste.tt != TABLE_TYPE_SEGMENT)
  432. return PGM_TRANSLATION_SPEC;
  433. if (ste.cs && asce.p)
  434. return PGM_TRANSLATION_SPEC;
  435. if (ste.fc && edat1) {
  436. dat_protection |= ste.fc1.p;
  437. raddr.sfaa = ste.fc1.sfaa;
  438. goto absolute_address;
  439. }
  440. dat_protection |= ste.fc0.p;
  441. ptr = ste.fc0.pto * 2048 + vaddr.px * 8;
  442. }
  443. }
  444. if (kvm_is_error_gpa(vcpu->kvm, ptr))
  445. return PGM_ADDRESSING;
  446. if (deref_table(vcpu->kvm, ptr, &pte.val))
  447. return -EFAULT;
  448. if (pte.i)
  449. return PGM_PAGE_TRANSLATION;
  450. if (pte.z)
  451. return PGM_TRANSLATION_SPEC;
  452. if (pte.co && !edat1)
  453. return PGM_TRANSLATION_SPEC;
  454. dat_protection |= pte.p;
  455. raddr.pfra = pte.pfra;
  456. real_address:
  457. raddr.addr = kvm_s390_real_to_abs(vcpu, raddr.addr);
  458. absolute_address:
  459. if (write && dat_protection)
  460. return PGM_PROTECTION;
  461. if (kvm_is_error_gpa(vcpu->kvm, raddr.addr))
  462. return PGM_ADDRESSING;
  463. *gpa = raddr.addr;
  464. return 0;
  465. }
  466. static inline int is_low_address(unsigned long ga)
  467. {
  468. /* Check for address ranges 0..511 and 4096..4607 */
  469. return (ga & ~0x11fful) == 0;
  470. }
  471. static int low_address_protection_enabled(struct kvm_vcpu *vcpu)
  472. {
  473. union ctlreg0 ctlreg0 = {.val = vcpu->arch.sie_block->gcr[0]};
  474. psw_t *psw = &vcpu->arch.sie_block->gpsw;
  475. union asce asce;
  476. if (!ctlreg0.lap)
  477. return 0;
  478. asce.val = get_vcpu_asce(vcpu);
  479. if (psw_bits(*psw).t && asce.p)
  480. return 0;
  481. return 1;
  482. }
  483. struct trans_exc_code_bits {
  484. unsigned long addr : 52; /* Translation-exception Address */
  485. unsigned long fsi : 2; /* Access Exception Fetch/Store Indication */
  486. unsigned long : 7;
  487. unsigned long b61 : 1;
  488. unsigned long as : 2; /* ASCE Identifier */
  489. };
  490. enum {
  491. FSI_UNKNOWN = 0, /* Unknown wether fetch or store */
  492. FSI_STORE = 1, /* Exception was due to store operation */
  493. FSI_FETCH = 2 /* Exception was due to fetch operation */
  494. };
  495. static int guest_page_range(struct kvm_vcpu *vcpu, unsigned long ga,
  496. unsigned long *pages, unsigned long nr_pages,
  497. int write)
  498. {
  499. struct kvm_s390_pgm_info *pgm = &vcpu->arch.pgm;
  500. psw_t *psw = &vcpu->arch.sie_block->gpsw;
  501. struct trans_exc_code_bits *tec_bits;
  502. int lap_enabled, rc;
  503. memset(pgm, 0, sizeof(*pgm));
  504. tec_bits = (struct trans_exc_code_bits *)&pgm->trans_exc_code;
  505. tec_bits->fsi = write ? FSI_STORE : FSI_FETCH;
  506. tec_bits->as = psw_bits(*psw).as;
  507. lap_enabled = low_address_protection_enabled(vcpu);
  508. while (nr_pages) {
  509. ga = kvm_s390_logical_to_effective(vcpu, ga);
  510. tec_bits->addr = ga >> PAGE_SHIFT;
  511. if (write && lap_enabled && is_low_address(ga)) {
  512. pgm->code = PGM_PROTECTION;
  513. return pgm->code;
  514. }
  515. ga &= PAGE_MASK;
  516. if (psw_bits(*psw).t) {
  517. rc = guest_translate(vcpu, ga, pages, write);
  518. if (rc < 0)
  519. return rc;
  520. if (rc == PGM_PROTECTION)
  521. tec_bits->b61 = 1;
  522. if (rc)
  523. pgm->code = rc;
  524. } else {
  525. *pages = kvm_s390_real_to_abs(vcpu, ga);
  526. if (kvm_is_error_gpa(vcpu->kvm, *pages))
  527. pgm->code = PGM_ADDRESSING;
  528. }
  529. if (pgm->code)
  530. return pgm->code;
  531. ga += PAGE_SIZE;
  532. pages++;
  533. nr_pages--;
  534. }
  535. return 0;
  536. }
  537. int access_guest(struct kvm_vcpu *vcpu, unsigned long ga, void *data,
  538. unsigned long len, int write)
  539. {
  540. psw_t *psw = &vcpu->arch.sie_block->gpsw;
  541. unsigned long _len, nr_pages, gpa, idx;
  542. unsigned long pages_array[2];
  543. unsigned long *pages;
  544. int need_ipte_lock;
  545. union asce asce;
  546. int rc;
  547. if (!len)
  548. return 0;
  549. /* Access register mode is not supported yet. */
  550. if (psw_bits(*psw).t && psw_bits(*psw).as == PSW_AS_ACCREG)
  551. return -EOPNOTSUPP;
  552. nr_pages = (((ga & ~PAGE_MASK) + len - 1) >> PAGE_SHIFT) + 1;
  553. pages = pages_array;
  554. if (nr_pages > ARRAY_SIZE(pages_array))
  555. pages = vmalloc(nr_pages * sizeof(unsigned long));
  556. if (!pages)
  557. return -ENOMEM;
  558. asce.val = get_vcpu_asce(vcpu);
  559. need_ipte_lock = psw_bits(*psw).t && !asce.r;
  560. if (need_ipte_lock)
  561. ipte_lock(vcpu);
  562. rc = guest_page_range(vcpu, ga, pages, nr_pages, write);
  563. for (idx = 0; idx < nr_pages && !rc; idx++) {
  564. gpa = *(pages + idx) + (ga & ~PAGE_MASK);
  565. _len = min(PAGE_SIZE - (gpa & ~PAGE_MASK), len);
  566. if (write)
  567. rc = kvm_write_guest(vcpu->kvm, gpa, data, _len);
  568. else
  569. rc = kvm_read_guest(vcpu->kvm, gpa, data, _len);
  570. len -= _len;
  571. ga += _len;
  572. data += _len;
  573. }
  574. if (need_ipte_lock)
  575. ipte_unlock(vcpu);
  576. if (nr_pages > ARRAY_SIZE(pages_array))
  577. vfree(pages);
  578. return rc;
  579. }
  580. int access_guest_real(struct kvm_vcpu *vcpu, unsigned long gra,
  581. void *data, unsigned long len, int write)
  582. {
  583. unsigned long _len, gpa;
  584. int rc = 0;
  585. while (len && !rc) {
  586. gpa = kvm_s390_real_to_abs(vcpu, gra);
  587. _len = min(PAGE_SIZE - (gpa & ~PAGE_MASK), len);
  588. if (write)
  589. rc = write_guest_abs(vcpu, gpa, data, _len);
  590. else
  591. rc = read_guest_abs(vcpu, gpa, data, _len);
  592. len -= _len;
  593. gra += _len;
  594. data += _len;
  595. }
  596. return rc;
  597. }
  598. /**
  599. * guest_translate_address - translate guest logical into guest absolute address
  600. *
  601. * Parameter semantics are the same as the ones from guest_translate.
  602. * The memory contents at the guest address are not changed.
  603. *
  604. * Note: The IPTE lock is not taken during this function, so the caller
  605. * has to take care of this.
  606. */
  607. int guest_translate_address(struct kvm_vcpu *vcpu, unsigned long gva,
  608. unsigned long *gpa, int write)
  609. {
  610. struct kvm_s390_pgm_info *pgm = &vcpu->arch.pgm;
  611. psw_t *psw = &vcpu->arch.sie_block->gpsw;
  612. struct trans_exc_code_bits *tec;
  613. union asce asce;
  614. int rc;
  615. /* Access register mode is not supported yet. */
  616. if (psw_bits(*psw).t && psw_bits(*psw).as == PSW_AS_ACCREG)
  617. return -EOPNOTSUPP;
  618. gva = kvm_s390_logical_to_effective(vcpu, gva);
  619. memset(pgm, 0, sizeof(*pgm));
  620. tec = (struct trans_exc_code_bits *)&pgm->trans_exc_code;
  621. tec->as = psw_bits(*psw).as;
  622. tec->fsi = write ? FSI_STORE : FSI_FETCH;
  623. tec->addr = gva >> PAGE_SHIFT;
  624. if (is_low_address(gva) && low_address_protection_enabled(vcpu)) {
  625. if (write) {
  626. rc = pgm->code = PGM_PROTECTION;
  627. return rc;
  628. }
  629. }
  630. asce.val = get_vcpu_asce(vcpu);
  631. if (psw_bits(*psw).t && !asce.r) { /* Use DAT? */
  632. rc = guest_translate(vcpu, gva, gpa, write);
  633. if (rc > 0) {
  634. if (rc == PGM_PROTECTION)
  635. tec->b61 = 1;
  636. pgm->code = rc;
  637. }
  638. } else {
  639. rc = 0;
  640. *gpa = kvm_s390_real_to_abs(vcpu, gva);
  641. if (kvm_is_error_gpa(vcpu->kvm, *gpa))
  642. rc = pgm->code = PGM_ADDRESSING;
  643. }
  644. return rc;
  645. }
  646. /**
  647. * kvm_s390_check_low_addr_protection - check for low-address protection
  648. * @ga: Guest address
  649. *
  650. * Checks whether an address is subject to low-address protection and set
  651. * up vcpu->arch.pgm accordingly if necessary.
  652. *
  653. * Return: 0 if no protection exception, or PGM_PROTECTION if protected.
  654. */
  655. int kvm_s390_check_low_addr_protection(struct kvm_vcpu *vcpu, unsigned long ga)
  656. {
  657. struct kvm_s390_pgm_info *pgm = &vcpu->arch.pgm;
  658. psw_t *psw = &vcpu->arch.sie_block->gpsw;
  659. struct trans_exc_code_bits *tec_bits;
  660. if (!is_low_address(ga) || !low_address_protection_enabled(vcpu))
  661. return 0;
  662. memset(pgm, 0, sizeof(*pgm));
  663. tec_bits = (struct trans_exc_code_bits *)&pgm->trans_exc_code;
  664. tec_bits->fsi = FSI_STORE;
  665. tec_bits->as = psw_bits(*psw).as;
  666. tec_bits->addr = ga >> PAGE_SHIFT;
  667. pgm->code = PGM_PROTECTION;
  668. return pgm->code;
  669. }