gaccess.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191
  1. /*
  2. * guest access functions
  3. *
  4. * Copyright IBM Corp. 2014
  5. *
  6. */
  7. #include <linux/vmalloc.h>
  8. #include <linux/mm_types.h>
  9. #include <linux/err.h>
  10. #include <asm/pgtable.h>
  11. #include <asm/gmap.h>
  12. #include "kvm-s390.h"
  13. #include "gaccess.h"
  14. #include <asm/switch_to.h>
  15. union asce {
  16. unsigned long val;
  17. struct {
  18. unsigned long origin : 52; /* Region- or Segment-Table Origin */
  19. unsigned long : 2;
  20. unsigned long g : 1; /* Subspace Group Control */
  21. unsigned long p : 1; /* Private Space Control */
  22. unsigned long s : 1; /* Storage-Alteration-Event Control */
  23. unsigned long x : 1; /* Space-Switch-Event Control */
  24. unsigned long r : 1; /* Real-Space Control */
  25. unsigned long : 1;
  26. unsigned long dt : 2; /* Designation-Type Control */
  27. unsigned long tl : 2; /* Region- or Segment-Table Length */
  28. };
  29. };
  30. enum {
  31. ASCE_TYPE_SEGMENT = 0,
  32. ASCE_TYPE_REGION3 = 1,
  33. ASCE_TYPE_REGION2 = 2,
  34. ASCE_TYPE_REGION1 = 3
  35. };
  36. union region1_table_entry {
  37. unsigned long val;
  38. struct {
  39. unsigned long rto: 52;/* Region-Table Origin */
  40. unsigned long : 2;
  41. unsigned long p : 1; /* DAT-Protection Bit */
  42. unsigned long : 1;
  43. unsigned long tf : 2; /* Region-Second-Table Offset */
  44. unsigned long i : 1; /* Region-Invalid Bit */
  45. unsigned long : 1;
  46. unsigned long tt : 2; /* Table-Type Bits */
  47. unsigned long tl : 2; /* Region-Second-Table Length */
  48. };
  49. };
  50. union region2_table_entry {
  51. unsigned long val;
  52. struct {
  53. unsigned long rto: 52;/* Region-Table Origin */
  54. unsigned long : 2;
  55. unsigned long p : 1; /* DAT-Protection Bit */
  56. unsigned long : 1;
  57. unsigned long tf : 2; /* Region-Third-Table Offset */
  58. unsigned long i : 1; /* Region-Invalid Bit */
  59. unsigned long : 1;
  60. unsigned long tt : 2; /* Table-Type Bits */
  61. unsigned long tl : 2; /* Region-Third-Table Length */
  62. };
  63. };
  64. struct region3_table_entry_fc0 {
  65. unsigned long sto: 52;/* Segment-Table Origin */
  66. unsigned long : 1;
  67. unsigned long fc : 1; /* Format-Control */
  68. unsigned long p : 1; /* DAT-Protection Bit */
  69. unsigned long : 1;
  70. unsigned long tf : 2; /* Segment-Table Offset */
  71. unsigned long i : 1; /* Region-Invalid Bit */
  72. unsigned long cr : 1; /* Common-Region Bit */
  73. unsigned long tt : 2; /* Table-Type Bits */
  74. unsigned long tl : 2; /* Segment-Table Length */
  75. };
  76. struct region3_table_entry_fc1 {
  77. unsigned long rfaa : 33; /* Region-Frame Absolute Address */
  78. unsigned long : 14;
  79. unsigned long av : 1; /* ACCF-Validity Control */
  80. unsigned long acc: 4; /* Access-Control Bits */
  81. unsigned long f : 1; /* Fetch-Protection Bit */
  82. unsigned long fc : 1; /* Format-Control */
  83. unsigned long p : 1; /* DAT-Protection Bit */
  84. unsigned long co : 1; /* Change-Recording Override */
  85. unsigned long : 2;
  86. unsigned long i : 1; /* Region-Invalid Bit */
  87. unsigned long cr : 1; /* Common-Region Bit */
  88. unsigned long tt : 2; /* Table-Type Bits */
  89. unsigned long : 2;
  90. };
  91. union region3_table_entry {
  92. unsigned long val;
  93. struct region3_table_entry_fc0 fc0;
  94. struct region3_table_entry_fc1 fc1;
  95. struct {
  96. unsigned long : 53;
  97. unsigned long fc : 1; /* Format-Control */
  98. unsigned long : 4;
  99. unsigned long i : 1; /* Region-Invalid Bit */
  100. unsigned long cr : 1; /* Common-Region Bit */
  101. unsigned long tt : 2; /* Table-Type Bits */
  102. unsigned long : 2;
  103. };
  104. };
  105. struct segment_entry_fc0 {
  106. unsigned long pto: 53;/* Page-Table Origin */
  107. unsigned long fc : 1; /* Format-Control */
  108. unsigned long p : 1; /* DAT-Protection Bit */
  109. unsigned long : 3;
  110. unsigned long i : 1; /* Segment-Invalid Bit */
  111. unsigned long cs : 1; /* Common-Segment Bit */
  112. unsigned long tt : 2; /* Table-Type Bits */
  113. unsigned long : 2;
  114. };
  115. struct segment_entry_fc1 {
  116. unsigned long sfaa : 44; /* Segment-Frame Absolute Address */
  117. unsigned long : 3;
  118. unsigned long av : 1; /* ACCF-Validity Control */
  119. unsigned long acc: 4; /* Access-Control Bits */
  120. unsigned long f : 1; /* Fetch-Protection Bit */
  121. unsigned long fc : 1; /* Format-Control */
  122. unsigned long p : 1; /* DAT-Protection Bit */
  123. unsigned long co : 1; /* Change-Recording Override */
  124. unsigned long : 2;
  125. unsigned long i : 1; /* Segment-Invalid Bit */
  126. unsigned long cs : 1; /* Common-Segment Bit */
  127. unsigned long tt : 2; /* Table-Type Bits */
  128. unsigned long : 2;
  129. };
  130. union segment_table_entry {
  131. unsigned long val;
  132. struct segment_entry_fc0 fc0;
  133. struct segment_entry_fc1 fc1;
  134. struct {
  135. unsigned long : 53;
  136. unsigned long fc : 1; /* Format-Control */
  137. unsigned long : 4;
  138. unsigned long i : 1; /* Segment-Invalid Bit */
  139. unsigned long cs : 1; /* Common-Segment Bit */
  140. unsigned long tt : 2; /* Table-Type Bits */
  141. unsigned long : 2;
  142. };
  143. };
  144. enum {
  145. TABLE_TYPE_SEGMENT = 0,
  146. TABLE_TYPE_REGION3 = 1,
  147. TABLE_TYPE_REGION2 = 2,
  148. TABLE_TYPE_REGION1 = 3
  149. };
  150. union page_table_entry {
  151. unsigned long val;
  152. struct {
  153. unsigned long pfra : 52; /* Page-Frame Real Address */
  154. unsigned long z : 1; /* Zero Bit */
  155. unsigned long i : 1; /* Page-Invalid Bit */
  156. unsigned long p : 1; /* DAT-Protection Bit */
  157. unsigned long : 9;
  158. };
  159. };
  160. /*
  161. * vaddress union in order to easily decode a virtual address into its
  162. * region first index, region second index etc. parts.
  163. */
  164. union vaddress {
  165. unsigned long addr;
  166. struct {
  167. unsigned long rfx : 11;
  168. unsigned long rsx : 11;
  169. unsigned long rtx : 11;
  170. unsigned long sx : 11;
  171. unsigned long px : 8;
  172. unsigned long bx : 12;
  173. };
  174. struct {
  175. unsigned long rfx01 : 2;
  176. unsigned long : 9;
  177. unsigned long rsx01 : 2;
  178. unsigned long : 9;
  179. unsigned long rtx01 : 2;
  180. unsigned long : 9;
  181. unsigned long sx01 : 2;
  182. unsigned long : 29;
  183. };
  184. };
  185. /*
  186. * raddress union which will contain the result (real or absolute address)
  187. * after a page table walk. The rfaa, sfaa and pfra members are used to
  188. * simply assign them the value of a region, segment or page table entry.
  189. */
  190. union raddress {
  191. unsigned long addr;
  192. unsigned long rfaa : 33; /* Region-Frame Absolute Address */
  193. unsigned long sfaa : 44; /* Segment-Frame Absolute Address */
  194. unsigned long pfra : 52; /* Page-Frame Real Address */
  195. };
  196. union alet {
  197. u32 val;
  198. struct {
  199. u32 reserved : 7;
  200. u32 p : 1;
  201. u32 alesn : 8;
  202. u32 alen : 16;
  203. };
  204. };
  205. union ald {
  206. u32 val;
  207. struct {
  208. u32 : 1;
  209. u32 alo : 24;
  210. u32 all : 7;
  211. };
  212. };
  213. struct ale {
  214. unsigned long i : 1; /* ALEN-Invalid Bit */
  215. unsigned long : 5;
  216. unsigned long fo : 1; /* Fetch-Only Bit */
  217. unsigned long p : 1; /* Private Bit */
  218. unsigned long alesn : 8; /* Access-List-Entry Sequence Number */
  219. unsigned long aleax : 16; /* Access-List-Entry Authorization Index */
  220. unsigned long : 32;
  221. unsigned long : 1;
  222. unsigned long asteo : 25; /* ASN-Second-Table-Entry Origin */
  223. unsigned long : 6;
  224. unsigned long astesn : 32; /* ASTE Sequence Number */
  225. } __packed;
  226. struct aste {
  227. unsigned long i : 1; /* ASX-Invalid Bit */
  228. unsigned long ato : 29; /* Authority-Table Origin */
  229. unsigned long : 1;
  230. unsigned long b : 1; /* Base-Space Bit */
  231. unsigned long ax : 16; /* Authorization Index */
  232. unsigned long atl : 12; /* Authority-Table Length */
  233. unsigned long : 2;
  234. unsigned long ca : 1; /* Controlled-ASN Bit */
  235. unsigned long ra : 1; /* Reusable-ASN Bit */
  236. unsigned long asce : 64; /* Address-Space-Control Element */
  237. unsigned long ald : 32;
  238. unsigned long astesn : 32;
  239. /* .. more fields there */
  240. } __packed;
  241. int ipte_lock_held(struct kvm_vcpu *vcpu)
  242. {
  243. if (vcpu->arch.sie_block->eca & ECA_SII) {
  244. int rc;
  245. read_lock(&vcpu->kvm->arch.sca_lock);
  246. rc = kvm_s390_get_ipte_control(vcpu->kvm)->kh != 0;
  247. read_unlock(&vcpu->kvm->arch.sca_lock);
  248. return rc;
  249. }
  250. return vcpu->kvm->arch.ipte_lock_count != 0;
  251. }
  252. static void ipte_lock_simple(struct kvm_vcpu *vcpu)
  253. {
  254. union ipte_control old, new, *ic;
  255. mutex_lock(&vcpu->kvm->arch.ipte_mutex);
  256. vcpu->kvm->arch.ipte_lock_count++;
  257. if (vcpu->kvm->arch.ipte_lock_count > 1)
  258. goto out;
  259. retry:
  260. read_lock(&vcpu->kvm->arch.sca_lock);
  261. ic = kvm_s390_get_ipte_control(vcpu->kvm);
  262. do {
  263. old = READ_ONCE(*ic);
  264. if (old.k) {
  265. read_unlock(&vcpu->kvm->arch.sca_lock);
  266. cond_resched();
  267. goto retry;
  268. }
  269. new = old;
  270. new.k = 1;
  271. } while (cmpxchg(&ic->val, old.val, new.val) != old.val);
  272. read_unlock(&vcpu->kvm->arch.sca_lock);
  273. out:
  274. mutex_unlock(&vcpu->kvm->arch.ipte_mutex);
  275. }
  276. static void ipte_unlock_simple(struct kvm_vcpu *vcpu)
  277. {
  278. union ipte_control old, new, *ic;
  279. mutex_lock(&vcpu->kvm->arch.ipte_mutex);
  280. vcpu->kvm->arch.ipte_lock_count--;
  281. if (vcpu->kvm->arch.ipte_lock_count)
  282. goto out;
  283. read_lock(&vcpu->kvm->arch.sca_lock);
  284. ic = kvm_s390_get_ipte_control(vcpu->kvm);
  285. do {
  286. old = READ_ONCE(*ic);
  287. new = old;
  288. new.k = 0;
  289. } while (cmpxchg(&ic->val, old.val, new.val) != old.val);
  290. read_unlock(&vcpu->kvm->arch.sca_lock);
  291. wake_up(&vcpu->kvm->arch.ipte_wq);
  292. out:
  293. mutex_unlock(&vcpu->kvm->arch.ipte_mutex);
  294. }
  295. static void ipte_lock_siif(struct kvm_vcpu *vcpu)
  296. {
  297. union ipte_control old, new, *ic;
  298. retry:
  299. read_lock(&vcpu->kvm->arch.sca_lock);
  300. ic = kvm_s390_get_ipte_control(vcpu->kvm);
  301. do {
  302. old = READ_ONCE(*ic);
  303. if (old.kg) {
  304. read_unlock(&vcpu->kvm->arch.sca_lock);
  305. cond_resched();
  306. goto retry;
  307. }
  308. new = old;
  309. new.k = 1;
  310. new.kh++;
  311. } while (cmpxchg(&ic->val, old.val, new.val) != old.val);
  312. read_unlock(&vcpu->kvm->arch.sca_lock);
  313. }
  314. static void ipte_unlock_siif(struct kvm_vcpu *vcpu)
  315. {
  316. union ipte_control old, new, *ic;
  317. read_lock(&vcpu->kvm->arch.sca_lock);
  318. ic = kvm_s390_get_ipte_control(vcpu->kvm);
  319. do {
  320. old = READ_ONCE(*ic);
  321. new = old;
  322. new.kh--;
  323. if (!new.kh)
  324. new.k = 0;
  325. } while (cmpxchg(&ic->val, old.val, new.val) != old.val);
  326. read_unlock(&vcpu->kvm->arch.sca_lock);
  327. if (!new.kh)
  328. wake_up(&vcpu->kvm->arch.ipte_wq);
  329. }
  330. void ipte_lock(struct kvm_vcpu *vcpu)
  331. {
  332. if (vcpu->arch.sie_block->eca & ECA_SII)
  333. ipte_lock_siif(vcpu);
  334. else
  335. ipte_lock_simple(vcpu);
  336. }
  337. void ipte_unlock(struct kvm_vcpu *vcpu)
  338. {
  339. if (vcpu->arch.sie_block->eca & ECA_SII)
  340. ipte_unlock_siif(vcpu);
  341. else
  342. ipte_unlock_simple(vcpu);
  343. }
  344. static int ar_translation(struct kvm_vcpu *vcpu, union asce *asce, u8 ar,
  345. enum gacc_mode mode)
  346. {
  347. union alet alet;
  348. struct ale ale;
  349. struct aste aste;
  350. unsigned long ald_addr, authority_table_addr;
  351. union ald ald;
  352. int eax, rc;
  353. u8 authority_table;
  354. if (ar >= NUM_ACRS)
  355. return -EINVAL;
  356. save_access_regs(vcpu->run->s.regs.acrs);
  357. alet.val = vcpu->run->s.regs.acrs[ar];
  358. if (ar == 0 || alet.val == 0) {
  359. asce->val = vcpu->arch.sie_block->gcr[1];
  360. return 0;
  361. } else if (alet.val == 1) {
  362. asce->val = vcpu->arch.sie_block->gcr[7];
  363. return 0;
  364. }
  365. if (alet.reserved)
  366. return PGM_ALET_SPECIFICATION;
  367. if (alet.p)
  368. ald_addr = vcpu->arch.sie_block->gcr[5];
  369. else
  370. ald_addr = vcpu->arch.sie_block->gcr[2];
  371. ald_addr &= 0x7fffffc0;
  372. rc = read_guest_real(vcpu, ald_addr + 16, &ald.val, sizeof(union ald));
  373. if (rc)
  374. return rc;
  375. if (alet.alen / 8 > ald.all)
  376. return PGM_ALEN_TRANSLATION;
  377. if (0x7fffffff - ald.alo * 128 < alet.alen * 16)
  378. return PGM_ADDRESSING;
  379. rc = read_guest_real(vcpu, ald.alo * 128 + alet.alen * 16, &ale,
  380. sizeof(struct ale));
  381. if (rc)
  382. return rc;
  383. if (ale.i == 1)
  384. return PGM_ALEN_TRANSLATION;
  385. if (ale.alesn != alet.alesn)
  386. return PGM_ALE_SEQUENCE;
  387. rc = read_guest_real(vcpu, ale.asteo * 64, &aste, sizeof(struct aste));
  388. if (rc)
  389. return rc;
  390. if (aste.i)
  391. return PGM_ASTE_VALIDITY;
  392. if (aste.astesn != ale.astesn)
  393. return PGM_ASTE_SEQUENCE;
  394. if (ale.p == 1) {
  395. eax = (vcpu->arch.sie_block->gcr[8] >> 16) & 0xffff;
  396. if (ale.aleax != eax) {
  397. if (eax / 16 > aste.atl)
  398. return PGM_EXTENDED_AUTHORITY;
  399. authority_table_addr = aste.ato * 4 + eax / 4;
  400. rc = read_guest_real(vcpu, authority_table_addr,
  401. &authority_table,
  402. sizeof(u8));
  403. if (rc)
  404. return rc;
  405. if ((authority_table & (0x40 >> ((eax & 3) * 2))) == 0)
  406. return PGM_EXTENDED_AUTHORITY;
  407. }
  408. }
  409. if (ale.fo == 1 && mode == GACC_STORE)
  410. return PGM_PROTECTION;
  411. asce->val = aste.asce;
  412. return 0;
  413. }
  414. struct trans_exc_code_bits {
  415. unsigned long addr : 52; /* Translation-exception Address */
  416. unsigned long fsi : 2; /* Access Exception Fetch/Store Indication */
  417. unsigned long : 2;
  418. unsigned long b56 : 1;
  419. unsigned long : 3;
  420. unsigned long b60 : 1;
  421. unsigned long b61 : 1;
  422. unsigned long as : 2; /* ASCE Identifier */
  423. };
  424. enum {
  425. FSI_UNKNOWN = 0, /* Unknown wether fetch or store */
  426. FSI_STORE = 1, /* Exception was due to store operation */
  427. FSI_FETCH = 2 /* Exception was due to fetch operation */
  428. };
  429. enum prot_type {
  430. PROT_TYPE_LA = 0,
  431. PROT_TYPE_KEYC = 1,
  432. PROT_TYPE_ALC = 2,
  433. PROT_TYPE_DAT = 3,
  434. };
  435. static int trans_exc(struct kvm_vcpu *vcpu, int code, unsigned long gva,
  436. u8 ar, enum gacc_mode mode, enum prot_type prot)
  437. {
  438. struct kvm_s390_pgm_info *pgm = &vcpu->arch.pgm;
  439. struct trans_exc_code_bits *tec;
  440. memset(pgm, 0, sizeof(*pgm));
  441. pgm->code = code;
  442. tec = (struct trans_exc_code_bits *)&pgm->trans_exc_code;
  443. switch (code) {
  444. case PGM_PROTECTION:
  445. switch (prot) {
  446. case PROT_TYPE_LA:
  447. tec->b56 = 1;
  448. break;
  449. case PROT_TYPE_KEYC:
  450. tec->b60 = 1;
  451. break;
  452. case PROT_TYPE_ALC:
  453. tec->b60 = 1;
  454. /* FALL THROUGH */
  455. case PROT_TYPE_DAT:
  456. tec->b61 = 1;
  457. break;
  458. }
  459. /* FALL THROUGH */
  460. case PGM_ASCE_TYPE:
  461. case PGM_PAGE_TRANSLATION:
  462. case PGM_REGION_FIRST_TRANS:
  463. case PGM_REGION_SECOND_TRANS:
  464. case PGM_REGION_THIRD_TRANS:
  465. case PGM_SEGMENT_TRANSLATION:
  466. /*
  467. * op_access_id only applies to MOVE_PAGE -> set bit 61
  468. * exc_access_id has to be set to 0 for some instructions. Both
  469. * cases have to be handled by the caller.
  470. */
  471. tec->addr = gva >> PAGE_SHIFT;
  472. tec->fsi = mode == GACC_STORE ? FSI_STORE : FSI_FETCH;
  473. tec->as = psw_bits(vcpu->arch.sie_block->gpsw).as;
  474. /* FALL THROUGH */
  475. case PGM_ALEN_TRANSLATION:
  476. case PGM_ALE_SEQUENCE:
  477. case PGM_ASTE_VALIDITY:
  478. case PGM_ASTE_SEQUENCE:
  479. case PGM_EXTENDED_AUTHORITY:
  480. /*
  481. * We can always store exc_access_id, as it is
  482. * undefined for non-ar cases. It is undefined for
  483. * most DAT protection exceptions.
  484. */
  485. pgm->exc_access_id = ar;
  486. break;
  487. }
  488. return code;
  489. }
  490. static int get_vcpu_asce(struct kvm_vcpu *vcpu, union asce *asce,
  491. unsigned long ga, u8 ar, enum gacc_mode mode)
  492. {
  493. int rc;
  494. struct psw_bits psw = psw_bits(vcpu->arch.sie_block->gpsw);
  495. if (!psw.t) {
  496. asce->val = 0;
  497. asce->r = 1;
  498. return 0;
  499. }
  500. if (mode == GACC_IFETCH)
  501. psw.as = psw.as == PSW_AS_HOME ? PSW_AS_HOME : PSW_AS_PRIMARY;
  502. switch (psw.as) {
  503. case PSW_AS_PRIMARY:
  504. asce->val = vcpu->arch.sie_block->gcr[1];
  505. return 0;
  506. case PSW_AS_SECONDARY:
  507. asce->val = vcpu->arch.sie_block->gcr[7];
  508. return 0;
  509. case PSW_AS_HOME:
  510. asce->val = vcpu->arch.sie_block->gcr[13];
  511. return 0;
  512. case PSW_AS_ACCREG:
  513. rc = ar_translation(vcpu, asce, ar, mode);
  514. if (rc > 0)
  515. return trans_exc(vcpu, rc, ga, ar, mode, PROT_TYPE_ALC);
  516. return rc;
  517. }
  518. return 0;
  519. }
  520. static int deref_table(struct kvm *kvm, unsigned long gpa, unsigned long *val)
  521. {
  522. return kvm_read_guest(kvm, gpa, val, sizeof(*val));
  523. }
  524. /**
  525. * guest_translate - translate a guest virtual into a guest absolute address
  526. * @vcpu: virtual cpu
  527. * @gva: guest virtual address
  528. * @gpa: points to where guest physical (absolute) address should be stored
  529. * @asce: effective asce
  530. * @mode: indicates the access mode to be used
  531. *
  532. * Translate a guest virtual address into a guest absolute address by means
  533. * of dynamic address translation as specified by the architecture.
  534. * If the resulting absolute address is not available in the configuration
  535. * an addressing exception is indicated and @gpa will not be changed.
  536. *
  537. * Returns: - zero on success; @gpa contains the resulting absolute address
  538. * - a negative value if guest access failed due to e.g. broken
  539. * guest mapping
  540. * - a positve value if an access exception happened. In this case
  541. * the returned value is the program interruption code as defined
  542. * by the architecture
  543. */
  544. static unsigned long guest_translate(struct kvm_vcpu *vcpu, unsigned long gva,
  545. unsigned long *gpa, const union asce asce,
  546. enum gacc_mode mode)
  547. {
  548. union vaddress vaddr = {.addr = gva};
  549. union raddress raddr = {.addr = gva};
  550. union page_table_entry pte;
  551. int dat_protection = 0;
  552. union ctlreg0 ctlreg0;
  553. unsigned long ptr;
  554. int edat1, edat2;
  555. ctlreg0.val = vcpu->arch.sie_block->gcr[0];
  556. edat1 = ctlreg0.edat && test_kvm_facility(vcpu->kvm, 8);
  557. edat2 = edat1 && test_kvm_facility(vcpu->kvm, 78);
  558. if (asce.r)
  559. goto real_address;
  560. ptr = asce.origin * 4096;
  561. switch (asce.dt) {
  562. case ASCE_TYPE_REGION1:
  563. if (vaddr.rfx01 > asce.tl)
  564. return PGM_REGION_FIRST_TRANS;
  565. ptr += vaddr.rfx * 8;
  566. break;
  567. case ASCE_TYPE_REGION2:
  568. if (vaddr.rfx)
  569. return PGM_ASCE_TYPE;
  570. if (vaddr.rsx01 > asce.tl)
  571. return PGM_REGION_SECOND_TRANS;
  572. ptr += vaddr.rsx * 8;
  573. break;
  574. case ASCE_TYPE_REGION3:
  575. if (vaddr.rfx || vaddr.rsx)
  576. return PGM_ASCE_TYPE;
  577. if (vaddr.rtx01 > asce.tl)
  578. return PGM_REGION_THIRD_TRANS;
  579. ptr += vaddr.rtx * 8;
  580. break;
  581. case ASCE_TYPE_SEGMENT:
  582. if (vaddr.rfx || vaddr.rsx || vaddr.rtx)
  583. return PGM_ASCE_TYPE;
  584. if (vaddr.sx01 > asce.tl)
  585. return PGM_SEGMENT_TRANSLATION;
  586. ptr += vaddr.sx * 8;
  587. break;
  588. }
  589. switch (asce.dt) {
  590. case ASCE_TYPE_REGION1: {
  591. union region1_table_entry rfte;
  592. if (kvm_is_error_gpa(vcpu->kvm, ptr))
  593. return PGM_ADDRESSING;
  594. if (deref_table(vcpu->kvm, ptr, &rfte.val))
  595. return -EFAULT;
  596. if (rfte.i)
  597. return PGM_REGION_FIRST_TRANS;
  598. if (rfte.tt != TABLE_TYPE_REGION1)
  599. return PGM_TRANSLATION_SPEC;
  600. if (vaddr.rsx01 < rfte.tf || vaddr.rsx01 > rfte.tl)
  601. return PGM_REGION_SECOND_TRANS;
  602. if (edat1)
  603. dat_protection |= rfte.p;
  604. ptr = rfte.rto * 4096 + vaddr.rsx * 8;
  605. }
  606. /* fallthrough */
  607. case ASCE_TYPE_REGION2: {
  608. union region2_table_entry rste;
  609. if (kvm_is_error_gpa(vcpu->kvm, ptr))
  610. return PGM_ADDRESSING;
  611. if (deref_table(vcpu->kvm, ptr, &rste.val))
  612. return -EFAULT;
  613. if (rste.i)
  614. return PGM_REGION_SECOND_TRANS;
  615. if (rste.tt != TABLE_TYPE_REGION2)
  616. return PGM_TRANSLATION_SPEC;
  617. if (vaddr.rtx01 < rste.tf || vaddr.rtx01 > rste.tl)
  618. return PGM_REGION_THIRD_TRANS;
  619. if (edat1)
  620. dat_protection |= rste.p;
  621. ptr = rste.rto * 4096 + vaddr.rtx * 8;
  622. }
  623. /* fallthrough */
  624. case ASCE_TYPE_REGION3: {
  625. union region3_table_entry rtte;
  626. if (kvm_is_error_gpa(vcpu->kvm, ptr))
  627. return PGM_ADDRESSING;
  628. if (deref_table(vcpu->kvm, ptr, &rtte.val))
  629. return -EFAULT;
  630. if (rtte.i)
  631. return PGM_REGION_THIRD_TRANS;
  632. if (rtte.tt != TABLE_TYPE_REGION3)
  633. return PGM_TRANSLATION_SPEC;
  634. if (rtte.cr && asce.p && edat2)
  635. return PGM_TRANSLATION_SPEC;
  636. if (rtte.fc && edat2) {
  637. dat_protection |= rtte.fc1.p;
  638. raddr.rfaa = rtte.fc1.rfaa;
  639. goto absolute_address;
  640. }
  641. if (vaddr.sx01 < rtte.fc0.tf)
  642. return PGM_SEGMENT_TRANSLATION;
  643. if (vaddr.sx01 > rtte.fc0.tl)
  644. return PGM_SEGMENT_TRANSLATION;
  645. if (edat1)
  646. dat_protection |= rtte.fc0.p;
  647. ptr = rtte.fc0.sto * 4096 + vaddr.sx * 8;
  648. }
  649. /* fallthrough */
  650. case ASCE_TYPE_SEGMENT: {
  651. union segment_table_entry ste;
  652. if (kvm_is_error_gpa(vcpu->kvm, ptr))
  653. return PGM_ADDRESSING;
  654. if (deref_table(vcpu->kvm, ptr, &ste.val))
  655. return -EFAULT;
  656. if (ste.i)
  657. return PGM_SEGMENT_TRANSLATION;
  658. if (ste.tt != TABLE_TYPE_SEGMENT)
  659. return PGM_TRANSLATION_SPEC;
  660. if (ste.cs && asce.p)
  661. return PGM_TRANSLATION_SPEC;
  662. if (ste.fc && edat1) {
  663. dat_protection |= ste.fc1.p;
  664. raddr.sfaa = ste.fc1.sfaa;
  665. goto absolute_address;
  666. }
  667. dat_protection |= ste.fc0.p;
  668. ptr = ste.fc0.pto * 2048 + vaddr.px * 8;
  669. }
  670. }
  671. if (kvm_is_error_gpa(vcpu->kvm, ptr))
  672. return PGM_ADDRESSING;
  673. if (deref_table(vcpu->kvm, ptr, &pte.val))
  674. return -EFAULT;
  675. if (pte.i)
  676. return PGM_PAGE_TRANSLATION;
  677. if (pte.z)
  678. return PGM_TRANSLATION_SPEC;
  679. dat_protection |= pte.p;
  680. raddr.pfra = pte.pfra;
  681. real_address:
  682. raddr.addr = kvm_s390_real_to_abs(vcpu, raddr.addr);
  683. absolute_address:
  684. if (mode == GACC_STORE && dat_protection)
  685. return PGM_PROTECTION;
  686. if (kvm_is_error_gpa(vcpu->kvm, raddr.addr))
  687. return PGM_ADDRESSING;
  688. *gpa = raddr.addr;
  689. return 0;
  690. }
  691. static inline int is_low_address(unsigned long ga)
  692. {
  693. /* Check for address ranges 0..511 and 4096..4607 */
  694. return (ga & ~0x11fful) == 0;
  695. }
  696. static int low_address_protection_enabled(struct kvm_vcpu *vcpu,
  697. const union asce asce)
  698. {
  699. union ctlreg0 ctlreg0 = {.val = vcpu->arch.sie_block->gcr[0]};
  700. psw_t *psw = &vcpu->arch.sie_block->gpsw;
  701. if (!ctlreg0.lap)
  702. return 0;
  703. if (psw_bits(*psw).t && asce.p)
  704. return 0;
  705. return 1;
  706. }
  707. static int guest_page_range(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar,
  708. unsigned long *pages, unsigned long nr_pages,
  709. const union asce asce, enum gacc_mode mode)
  710. {
  711. psw_t *psw = &vcpu->arch.sie_block->gpsw;
  712. int lap_enabled, rc = 0;
  713. lap_enabled = low_address_protection_enabled(vcpu, asce);
  714. while (nr_pages) {
  715. ga = kvm_s390_logical_to_effective(vcpu, ga);
  716. if (mode == GACC_STORE && lap_enabled && is_low_address(ga))
  717. return trans_exc(vcpu, PGM_PROTECTION, ga, ar, mode,
  718. PROT_TYPE_LA);
  719. ga &= PAGE_MASK;
  720. if (psw_bits(*psw).t) {
  721. rc = guest_translate(vcpu, ga, pages, asce, mode);
  722. if (rc < 0)
  723. return rc;
  724. } else {
  725. *pages = kvm_s390_real_to_abs(vcpu, ga);
  726. if (kvm_is_error_gpa(vcpu->kvm, *pages))
  727. rc = PGM_ADDRESSING;
  728. }
  729. if (rc)
  730. return trans_exc(vcpu, rc, ga, ar, mode, PROT_TYPE_DAT);
  731. ga += PAGE_SIZE;
  732. pages++;
  733. nr_pages--;
  734. }
  735. return 0;
  736. }
  737. int access_guest(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar, void *data,
  738. unsigned long len, enum gacc_mode mode)
  739. {
  740. psw_t *psw = &vcpu->arch.sie_block->gpsw;
  741. unsigned long _len, nr_pages, gpa, idx;
  742. unsigned long pages_array[2];
  743. unsigned long *pages;
  744. int need_ipte_lock;
  745. union asce asce;
  746. int rc;
  747. if (!len)
  748. return 0;
  749. ga = kvm_s390_logical_to_effective(vcpu, ga);
  750. rc = get_vcpu_asce(vcpu, &asce, ga, ar, mode);
  751. if (rc)
  752. return rc;
  753. nr_pages = (((ga & ~PAGE_MASK) + len - 1) >> PAGE_SHIFT) + 1;
  754. pages = pages_array;
  755. if (nr_pages > ARRAY_SIZE(pages_array))
  756. pages = vmalloc(nr_pages * sizeof(unsigned long));
  757. if (!pages)
  758. return -ENOMEM;
  759. need_ipte_lock = psw_bits(*psw).t && !asce.r;
  760. if (need_ipte_lock)
  761. ipte_lock(vcpu);
  762. rc = guest_page_range(vcpu, ga, ar, pages, nr_pages, asce, mode);
  763. for (idx = 0; idx < nr_pages && !rc; idx++) {
  764. gpa = *(pages + idx) + (ga & ~PAGE_MASK);
  765. _len = min(PAGE_SIZE - (gpa & ~PAGE_MASK), len);
  766. if (mode == GACC_STORE)
  767. rc = kvm_write_guest(vcpu->kvm, gpa, data, _len);
  768. else
  769. rc = kvm_read_guest(vcpu->kvm, gpa, data, _len);
  770. len -= _len;
  771. ga += _len;
  772. data += _len;
  773. }
  774. if (need_ipte_lock)
  775. ipte_unlock(vcpu);
  776. if (nr_pages > ARRAY_SIZE(pages_array))
  777. vfree(pages);
  778. return rc;
  779. }
  780. int access_guest_real(struct kvm_vcpu *vcpu, unsigned long gra,
  781. void *data, unsigned long len, enum gacc_mode mode)
  782. {
  783. unsigned long _len, gpa;
  784. int rc = 0;
  785. while (len && !rc) {
  786. gpa = kvm_s390_real_to_abs(vcpu, gra);
  787. _len = min(PAGE_SIZE - (gpa & ~PAGE_MASK), len);
  788. if (mode)
  789. rc = write_guest_abs(vcpu, gpa, data, _len);
  790. else
  791. rc = read_guest_abs(vcpu, gpa, data, _len);
  792. len -= _len;
  793. gra += _len;
  794. data += _len;
  795. }
  796. return rc;
  797. }
  798. /**
  799. * guest_translate_address - translate guest logical into guest absolute address
  800. *
  801. * Parameter semantics are the same as the ones from guest_translate.
  802. * The memory contents at the guest address are not changed.
  803. *
  804. * Note: The IPTE lock is not taken during this function, so the caller
  805. * has to take care of this.
  806. */
  807. int guest_translate_address(struct kvm_vcpu *vcpu, unsigned long gva, u8 ar,
  808. unsigned long *gpa, enum gacc_mode mode)
  809. {
  810. psw_t *psw = &vcpu->arch.sie_block->gpsw;
  811. union asce asce;
  812. int rc;
  813. gva = kvm_s390_logical_to_effective(vcpu, gva);
  814. rc = get_vcpu_asce(vcpu, &asce, gva, ar, mode);
  815. if (rc)
  816. return rc;
  817. if (is_low_address(gva) && low_address_protection_enabled(vcpu, asce)) {
  818. if (mode == GACC_STORE)
  819. return trans_exc(vcpu, PGM_PROTECTION, gva, 0,
  820. mode, PROT_TYPE_LA);
  821. }
  822. if (psw_bits(*psw).t && !asce.r) { /* Use DAT? */
  823. rc = guest_translate(vcpu, gva, gpa, asce, mode);
  824. if (rc > 0)
  825. return trans_exc(vcpu, rc, gva, 0, mode, PROT_TYPE_DAT);
  826. } else {
  827. *gpa = kvm_s390_real_to_abs(vcpu, gva);
  828. if (kvm_is_error_gpa(vcpu->kvm, *gpa))
  829. return trans_exc(vcpu, rc, gva, PGM_ADDRESSING, mode, 0);
  830. }
  831. return rc;
  832. }
  833. /**
  834. * check_gva_range - test a range of guest virtual addresses for accessibility
  835. */
  836. int check_gva_range(struct kvm_vcpu *vcpu, unsigned long gva, u8 ar,
  837. unsigned long length, enum gacc_mode mode)
  838. {
  839. unsigned long gpa;
  840. unsigned long currlen;
  841. int rc = 0;
  842. ipte_lock(vcpu);
  843. while (length > 0 && !rc) {
  844. currlen = min(length, PAGE_SIZE - (gva % PAGE_SIZE));
  845. rc = guest_translate_address(vcpu, gva, ar, &gpa, mode);
  846. gva += currlen;
  847. length -= currlen;
  848. }
  849. ipte_unlock(vcpu);
  850. return rc;
  851. }
  852. /**
  853. * kvm_s390_check_low_addr_prot_real - check for low-address protection
  854. * @gra: Guest real address
  855. *
  856. * Checks whether an address is subject to low-address protection and set
  857. * up vcpu->arch.pgm accordingly if necessary.
  858. *
  859. * Return: 0 if no protection exception, or PGM_PROTECTION if protected.
  860. */
  861. int kvm_s390_check_low_addr_prot_real(struct kvm_vcpu *vcpu, unsigned long gra)
  862. {
  863. union ctlreg0 ctlreg0 = {.val = vcpu->arch.sie_block->gcr[0]};
  864. if (!ctlreg0.lap || !is_low_address(gra))
  865. return 0;
  866. return trans_exc(vcpu, PGM_PROTECTION, gra, 0, GACC_STORE, PROT_TYPE_LA);
  867. }
  868. /**
  869. * kvm_s390_shadow_tables - walk the guest page table and create shadow tables
  870. * @sg: pointer to the shadow guest address space structure
  871. * @saddr: faulting address in the shadow gmap
  872. * @pgt: pointer to the page table address result
  873. * @fake: pgt references contiguous guest memory block, not a pgtable
  874. */
  875. static int kvm_s390_shadow_tables(struct gmap *sg, unsigned long saddr,
  876. unsigned long *pgt, int *dat_protection,
  877. int *fake)
  878. {
  879. struct gmap *parent;
  880. union asce asce;
  881. union vaddress vaddr;
  882. unsigned long ptr;
  883. int rc;
  884. *fake = 0;
  885. *dat_protection = 0;
  886. parent = sg->parent;
  887. vaddr.addr = saddr;
  888. asce.val = sg->orig_asce;
  889. ptr = asce.origin * 4096;
  890. if (asce.r) {
  891. *fake = 1;
  892. asce.dt = ASCE_TYPE_REGION1;
  893. }
  894. switch (asce.dt) {
  895. case ASCE_TYPE_REGION1:
  896. if (vaddr.rfx01 > asce.tl && !asce.r)
  897. return PGM_REGION_FIRST_TRANS;
  898. break;
  899. case ASCE_TYPE_REGION2:
  900. if (vaddr.rfx)
  901. return PGM_ASCE_TYPE;
  902. if (vaddr.rsx01 > asce.tl)
  903. return PGM_REGION_SECOND_TRANS;
  904. break;
  905. case ASCE_TYPE_REGION3:
  906. if (vaddr.rfx || vaddr.rsx)
  907. return PGM_ASCE_TYPE;
  908. if (vaddr.rtx01 > asce.tl)
  909. return PGM_REGION_THIRD_TRANS;
  910. break;
  911. case ASCE_TYPE_SEGMENT:
  912. if (vaddr.rfx || vaddr.rsx || vaddr.rtx)
  913. return PGM_ASCE_TYPE;
  914. if (vaddr.sx01 > asce.tl)
  915. return PGM_SEGMENT_TRANSLATION;
  916. break;
  917. }
  918. switch (asce.dt) {
  919. case ASCE_TYPE_REGION1: {
  920. union region1_table_entry rfte;
  921. if (*fake) {
  922. /* offset in 16EB guest memory block */
  923. ptr = ptr + ((unsigned long) vaddr.rsx << 53UL);
  924. rfte.val = ptr;
  925. goto shadow_r2t;
  926. }
  927. rc = gmap_read_table(parent, ptr + vaddr.rfx * 8, &rfte.val);
  928. if (rc)
  929. return rc;
  930. if (rfte.i)
  931. return PGM_REGION_FIRST_TRANS;
  932. if (rfte.tt != TABLE_TYPE_REGION1)
  933. return PGM_TRANSLATION_SPEC;
  934. if (vaddr.rsx01 < rfte.tf || vaddr.rsx01 > rfte.tl)
  935. return PGM_REGION_SECOND_TRANS;
  936. if (sg->edat_level >= 1)
  937. *dat_protection |= rfte.p;
  938. ptr = rfte.rto << 12UL;
  939. shadow_r2t:
  940. rc = gmap_shadow_r2t(sg, saddr, rfte.val, *fake);
  941. if (rc)
  942. return rc;
  943. /* fallthrough */
  944. }
  945. case ASCE_TYPE_REGION2: {
  946. union region2_table_entry rste;
  947. if (*fake) {
  948. /* offset in 8PB guest memory block */
  949. ptr = ptr + ((unsigned long) vaddr.rtx << 42UL);
  950. rste.val = ptr;
  951. goto shadow_r3t;
  952. }
  953. rc = gmap_read_table(parent, ptr + vaddr.rsx * 8, &rste.val);
  954. if (rc)
  955. return rc;
  956. if (rste.i)
  957. return PGM_REGION_SECOND_TRANS;
  958. if (rste.tt != TABLE_TYPE_REGION2)
  959. return PGM_TRANSLATION_SPEC;
  960. if (vaddr.rtx01 < rste.tf || vaddr.rtx01 > rste.tl)
  961. return PGM_REGION_THIRD_TRANS;
  962. if (sg->edat_level >= 1)
  963. *dat_protection |= rste.p;
  964. ptr = rste.rto << 12UL;
  965. shadow_r3t:
  966. rste.p |= *dat_protection;
  967. rc = gmap_shadow_r3t(sg, saddr, rste.val, *fake);
  968. if (rc)
  969. return rc;
  970. /* fallthrough */
  971. }
  972. case ASCE_TYPE_REGION3: {
  973. union region3_table_entry rtte;
  974. if (*fake) {
  975. /* offset in 4TB guest memory block */
  976. ptr = ptr + ((unsigned long) vaddr.sx << 31UL);
  977. rtte.val = ptr;
  978. goto shadow_sgt;
  979. }
  980. rc = gmap_read_table(parent, ptr + vaddr.rtx * 8, &rtte.val);
  981. if (rc)
  982. return rc;
  983. if (rtte.i)
  984. return PGM_REGION_THIRD_TRANS;
  985. if (rtte.tt != TABLE_TYPE_REGION3)
  986. return PGM_TRANSLATION_SPEC;
  987. if (rtte.cr && asce.p && sg->edat_level >= 2)
  988. return PGM_TRANSLATION_SPEC;
  989. if (rtte.fc && sg->edat_level >= 2) {
  990. *dat_protection |= rtte.fc0.p;
  991. *fake = 1;
  992. ptr = rtte.fc1.rfaa << 31UL;
  993. rtte.val = ptr;
  994. goto shadow_sgt;
  995. }
  996. if (vaddr.sx01 < rtte.fc0.tf || vaddr.sx01 > rtte.fc0.tl)
  997. return PGM_SEGMENT_TRANSLATION;
  998. if (sg->edat_level >= 1)
  999. *dat_protection |= rtte.fc0.p;
  1000. ptr = rtte.fc0.sto << 12UL;
  1001. shadow_sgt:
  1002. rtte.fc0.p |= *dat_protection;
  1003. rc = gmap_shadow_sgt(sg, saddr, rtte.val, *fake);
  1004. if (rc)
  1005. return rc;
  1006. /* fallthrough */
  1007. }
  1008. case ASCE_TYPE_SEGMENT: {
  1009. union segment_table_entry ste;
  1010. if (*fake) {
  1011. /* offset in 2G guest memory block */
  1012. ptr = ptr + ((unsigned long) vaddr.sx << 20UL);
  1013. ste.val = ptr;
  1014. goto shadow_pgt;
  1015. }
  1016. rc = gmap_read_table(parent, ptr + vaddr.sx * 8, &ste.val);
  1017. if (rc)
  1018. return rc;
  1019. if (ste.i)
  1020. return PGM_SEGMENT_TRANSLATION;
  1021. if (ste.tt != TABLE_TYPE_SEGMENT)
  1022. return PGM_TRANSLATION_SPEC;
  1023. if (ste.cs && asce.p)
  1024. return PGM_TRANSLATION_SPEC;
  1025. *dat_protection |= ste.fc0.p;
  1026. if (ste.fc && sg->edat_level >= 1) {
  1027. *fake = 1;
  1028. ptr = ste.fc1.sfaa << 20UL;
  1029. ste.val = ptr;
  1030. goto shadow_pgt;
  1031. }
  1032. ptr = ste.fc0.pto << 11UL;
  1033. shadow_pgt:
  1034. ste.fc0.p |= *dat_protection;
  1035. rc = gmap_shadow_pgt(sg, saddr, ste.val, *fake);
  1036. if (rc)
  1037. return rc;
  1038. }
  1039. }
  1040. /* Return the parent address of the page table */
  1041. *pgt = ptr;
  1042. return 0;
  1043. }
  1044. /**
  1045. * kvm_s390_shadow_fault - handle fault on a shadow page table
  1046. * @vcpu: virtual cpu
  1047. * @sg: pointer to the shadow guest address space structure
  1048. * @saddr: faulting address in the shadow gmap
  1049. *
  1050. * Returns: - 0 if the shadow fault was successfully resolved
  1051. * - > 0 (pgm exception code) on exceptions while faulting
  1052. * - -EAGAIN if the caller can retry immediately
  1053. * - -EFAULT when accessing invalid guest addresses
  1054. * - -ENOMEM if out of memory
  1055. */
  1056. int kvm_s390_shadow_fault(struct kvm_vcpu *vcpu, struct gmap *sg,
  1057. unsigned long saddr)
  1058. {
  1059. union vaddress vaddr;
  1060. union page_table_entry pte;
  1061. unsigned long pgt;
  1062. int dat_protection, fake;
  1063. int rc;
  1064. down_read(&sg->mm->mmap_sem);
  1065. /*
  1066. * We don't want any guest-2 tables to change - so the parent
  1067. * tables/pointers we read stay valid - unshadowing is however
  1068. * always possible - only guest_table_lock protects us.
  1069. */
  1070. ipte_lock(vcpu);
  1071. rc = gmap_shadow_pgt_lookup(sg, saddr, &pgt, &dat_protection, &fake);
  1072. if (rc)
  1073. rc = kvm_s390_shadow_tables(sg, saddr, &pgt, &dat_protection,
  1074. &fake);
  1075. vaddr.addr = saddr;
  1076. if (fake) {
  1077. /* offset in 1MB guest memory block */
  1078. pte.val = pgt + ((unsigned long) vaddr.px << 12UL);
  1079. goto shadow_page;
  1080. }
  1081. if (!rc)
  1082. rc = gmap_read_table(sg->parent, pgt + vaddr.px * 8, &pte.val);
  1083. if (!rc && pte.i)
  1084. rc = PGM_PAGE_TRANSLATION;
  1085. if (!rc && pte.z)
  1086. rc = PGM_TRANSLATION_SPEC;
  1087. shadow_page:
  1088. pte.p |= dat_protection;
  1089. if (!rc)
  1090. rc = gmap_shadow_page(sg, saddr, __pte(pte.val));
  1091. ipte_unlock(vcpu);
  1092. up_read(&sg->mm->mmap_sem);
  1093. return rc;
  1094. }