gaccess.c 19 KB

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