gaccess.c 24 KB

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