gaccess.c 31 KB

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