priv.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * handling privileged instructions
  4. *
  5. * Copyright IBM Corp. 2008, 2020
  6. *
  7. * Author(s): Carsten Otte <[email protected]>
  8. * Christian Borntraeger <[email protected]>
  9. */
  10. #include <linux/kvm.h>
  11. #include <linux/gfp.h>
  12. #include <linux/errno.h>
  13. #include <linux/mm_types.h>
  14. #include <linux/pgtable.h>
  15. #include <asm/asm-offsets.h>
  16. #include <asm/facility.h>
  17. #include <asm/current.h>
  18. #include <asm/debug.h>
  19. #include <asm/ebcdic.h>
  20. #include <asm/sysinfo.h>
  21. #include <asm/page-states.h>
  22. #include <asm/gmap.h>
  23. #include <asm/io.h>
  24. #include <asm/ptrace.h>
  25. #include <asm/sclp.h>
  26. #include <asm/ap.h>
  27. #include "gaccess.h"
  28. #include "kvm-s390.h"
  29. #include "trace.h"
  30. static int handle_ri(struct kvm_vcpu *vcpu)
  31. {
  32. vcpu->stat.instruction_ri++;
  33. if (test_kvm_facility(vcpu->kvm, 64)) {
  34. VCPU_EVENT(vcpu, 3, "%s", "ENABLE: RI (lazy)");
  35. vcpu->arch.sie_block->ecb3 |= ECB3_RI;
  36. kvm_s390_retry_instr(vcpu);
  37. return 0;
  38. } else
  39. return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
  40. }
  41. int kvm_s390_handle_aa(struct kvm_vcpu *vcpu)
  42. {
  43. if ((vcpu->arch.sie_block->ipa & 0xf) <= 4)
  44. return handle_ri(vcpu);
  45. else
  46. return -EOPNOTSUPP;
  47. }
  48. static int handle_gs(struct kvm_vcpu *vcpu)
  49. {
  50. vcpu->stat.instruction_gs++;
  51. if (test_kvm_facility(vcpu->kvm, 133)) {
  52. VCPU_EVENT(vcpu, 3, "%s", "ENABLE: GS (lazy)");
  53. preempt_disable();
  54. __ctl_set_bit(2, 4);
  55. current->thread.gs_cb = (struct gs_cb *)&vcpu->run->s.regs.gscb;
  56. restore_gs_cb(current->thread.gs_cb);
  57. preempt_enable();
  58. vcpu->arch.sie_block->ecb |= ECB_GS;
  59. vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT;
  60. vcpu->arch.gs_enabled = 1;
  61. kvm_s390_retry_instr(vcpu);
  62. return 0;
  63. } else
  64. return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
  65. }
  66. int kvm_s390_handle_e3(struct kvm_vcpu *vcpu)
  67. {
  68. int code = vcpu->arch.sie_block->ipb & 0xff;
  69. if (code == 0x49 || code == 0x4d)
  70. return handle_gs(vcpu);
  71. else
  72. return -EOPNOTSUPP;
  73. }
  74. /* Handle SCK (SET CLOCK) interception */
  75. static int handle_set_clock(struct kvm_vcpu *vcpu)
  76. {
  77. struct kvm_s390_vm_tod_clock gtod = { 0 };
  78. int rc;
  79. u8 ar;
  80. u64 op2;
  81. vcpu->stat.instruction_sck++;
  82. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  83. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  84. op2 = kvm_s390_get_base_disp_s(vcpu, &ar);
  85. if (op2 & 7) /* Operand must be on a doubleword boundary */
  86. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  87. rc = read_guest(vcpu, op2, ar, &gtod.tod, sizeof(gtod.tod));
  88. if (rc)
  89. return kvm_s390_inject_prog_cond(vcpu, rc);
  90. VCPU_EVENT(vcpu, 3, "SCK: setting guest TOD to 0x%llx", gtod.tod);
  91. /*
  92. * To set the TOD clock the kvm lock must be taken, but the vcpu lock
  93. * is already held in handle_set_clock. The usual lock order is the
  94. * opposite. As SCK is deprecated and should not be used in several
  95. * cases, for example when the multiple epoch facility or TOD clock
  96. * steering facility is installed (see Principles of Operation), a
  97. * slow path can be used. If the lock can not be taken via try_lock,
  98. * the instruction will be retried via -EAGAIN at a later point in
  99. * time.
  100. */
  101. if (!kvm_s390_try_set_tod_clock(vcpu->kvm, &gtod)) {
  102. kvm_s390_retry_instr(vcpu);
  103. return -EAGAIN;
  104. }
  105. kvm_s390_set_psw_cc(vcpu, 0);
  106. return 0;
  107. }
  108. static int handle_set_prefix(struct kvm_vcpu *vcpu)
  109. {
  110. u64 operand2;
  111. u32 address;
  112. int rc;
  113. u8 ar;
  114. vcpu->stat.instruction_spx++;
  115. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  116. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  117. operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
  118. /* must be word boundary */
  119. if (operand2 & 3)
  120. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  121. /* get the value */
  122. rc = read_guest(vcpu, operand2, ar, &address, sizeof(address));
  123. if (rc)
  124. return kvm_s390_inject_prog_cond(vcpu, rc);
  125. address &= 0x7fffe000u;
  126. /*
  127. * Make sure the new value is valid memory. We only need to check the
  128. * first page, since address is 8k aligned and memory pieces are always
  129. * at least 1MB aligned and have at least a size of 1MB.
  130. */
  131. if (kvm_is_error_gpa(vcpu->kvm, address))
  132. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  133. kvm_s390_set_prefix(vcpu, address);
  134. trace_kvm_s390_handle_prefix(vcpu, 1, address);
  135. return 0;
  136. }
  137. static int handle_store_prefix(struct kvm_vcpu *vcpu)
  138. {
  139. u64 operand2;
  140. u32 address;
  141. int rc;
  142. u8 ar;
  143. vcpu->stat.instruction_stpx++;
  144. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  145. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  146. operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
  147. /* must be word boundary */
  148. if (operand2 & 3)
  149. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  150. address = kvm_s390_get_prefix(vcpu);
  151. /* get the value */
  152. rc = write_guest(vcpu, operand2, ar, &address, sizeof(address));
  153. if (rc)
  154. return kvm_s390_inject_prog_cond(vcpu, rc);
  155. VCPU_EVENT(vcpu, 3, "STPX: storing prefix 0x%x into 0x%llx", address, operand2);
  156. trace_kvm_s390_handle_prefix(vcpu, 0, address);
  157. return 0;
  158. }
  159. static int handle_store_cpu_address(struct kvm_vcpu *vcpu)
  160. {
  161. u16 vcpu_id = vcpu->vcpu_id;
  162. u64 ga;
  163. int rc;
  164. u8 ar;
  165. vcpu->stat.instruction_stap++;
  166. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  167. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  168. ga = kvm_s390_get_base_disp_s(vcpu, &ar);
  169. if (ga & 1)
  170. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  171. rc = write_guest(vcpu, ga, ar, &vcpu_id, sizeof(vcpu_id));
  172. if (rc)
  173. return kvm_s390_inject_prog_cond(vcpu, rc);
  174. VCPU_EVENT(vcpu, 3, "STAP: storing cpu address (%u) to 0x%llx", vcpu_id, ga);
  175. trace_kvm_s390_handle_stap(vcpu, ga);
  176. return 0;
  177. }
  178. int kvm_s390_skey_check_enable(struct kvm_vcpu *vcpu)
  179. {
  180. int rc;
  181. trace_kvm_s390_skey_related_inst(vcpu);
  182. /* Already enabled? */
  183. if (vcpu->arch.skey_enabled)
  184. return 0;
  185. rc = s390_enable_skey();
  186. VCPU_EVENT(vcpu, 3, "enabling storage keys for guest: %d", rc);
  187. if (rc)
  188. return rc;
  189. if (kvm_s390_test_cpuflags(vcpu, CPUSTAT_KSS))
  190. kvm_s390_clear_cpuflags(vcpu, CPUSTAT_KSS);
  191. if (!vcpu->kvm->arch.use_skf)
  192. vcpu->arch.sie_block->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE;
  193. else
  194. vcpu->arch.sie_block->ictl &= ~(ICTL_ISKE | ICTL_SSKE | ICTL_RRBE);
  195. vcpu->arch.skey_enabled = true;
  196. return 0;
  197. }
  198. static int try_handle_skey(struct kvm_vcpu *vcpu)
  199. {
  200. int rc;
  201. rc = kvm_s390_skey_check_enable(vcpu);
  202. if (rc)
  203. return rc;
  204. if (vcpu->kvm->arch.use_skf) {
  205. /* with storage-key facility, SIE interprets it for us */
  206. kvm_s390_retry_instr(vcpu);
  207. VCPU_EVENT(vcpu, 4, "%s", "retrying storage key operation");
  208. return -EAGAIN;
  209. }
  210. return 0;
  211. }
  212. static int handle_iske(struct kvm_vcpu *vcpu)
  213. {
  214. unsigned long gaddr, vmaddr;
  215. unsigned char key;
  216. int reg1, reg2;
  217. bool unlocked;
  218. int rc;
  219. vcpu->stat.instruction_iske++;
  220. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  221. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  222. rc = try_handle_skey(vcpu);
  223. if (rc)
  224. return rc != -EAGAIN ? rc : 0;
  225. kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
  226. gaddr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
  227. gaddr = kvm_s390_logical_to_effective(vcpu, gaddr);
  228. gaddr = kvm_s390_real_to_abs(vcpu, gaddr);
  229. vmaddr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(gaddr));
  230. if (kvm_is_error_hva(vmaddr))
  231. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  232. retry:
  233. unlocked = false;
  234. mmap_read_lock(current->mm);
  235. rc = get_guest_storage_key(current->mm, vmaddr, &key);
  236. if (rc) {
  237. rc = fixup_user_fault(current->mm, vmaddr,
  238. FAULT_FLAG_WRITE, &unlocked);
  239. if (!rc) {
  240. mmap_read_unlock(current->mm);
  241. goto retry;
  242. }
  243. }
  244. mmap_read_unlock(current->mm);
  245. if (rc == -EFAULT)
  246. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  247. if (rc < 0)
  248. return rc;
  249. vcpu->run->s.regs.gprs[reg1] &= ~0xff;
  250. vcpu->run->s.regs.gprs[reg1] |= key;
  251. return 0;
  252. }
  253. static int handle_rrbe(struct kvm_vcpu *vcpu)
  254. {
  255. unsigned long vmaddr, gaddr;
  256. int reg1, reg2;
  257. bool unlocked;
  258. int rc;
  259. vcpu->stat.instruction_rrbe++;
  260. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  261. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  262. rc = try_handle_skey(vcpu);
  263. if (rc)
  264. return rc != -EAGAIN ? rc : 0;
  265. kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
  266. gaddr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
  267. gaddr = kvm_s390_logical_to_effective(vcpu, gaddr);
  268. gaddr = kvm_s390_real_to_abs(vcpu, gaddr);
  269. vmaddr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(gaddr));
  270. if (kvm_is_error_hva(vmaddr))
  271. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  272. retry:
  273. unlocked = false;
  274. mmap_read_lock(current->mm);
  275. rc = reset_guest_reference_bit(current->mm, vmaddr);
  276. if (rc < 0) {
  277. rc = fixup_user_fault(current->mm, vmaddr,
  278. FAULT_FLAG_WRITE, &unlocked);
  279. if (!rc) {
  280. mmap_read_unlock(current->mm);
  281. goto retry;
  282. }
  283. }
  284. mmap_read_unlock(current->mm);
  285. if (rc == -EFAULT)
  286. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  287. if (rc < 0)
  288. return rc;
  289. kvm_s390_set_psw_cc(vcpu, rc);
  290. return 0;
  291. }
  292. #define SSKE_NQ 0x8
  293. #define SSKE_MR 0x4
  294. #define SSKE_MC 0x2
  295. #define SSKE_MB 0x1
  296. static int handle_sske(struct kvm_vcpu *vcpu)
  297. {
  298. unsigned char m3 = vcpu->arch.sie_block->ipb >> 28;
  299. unsigned long start, end;
  300. unsigned char key, oldkey;
  301. int reg1, reg2;
  302. bool unlocked;
  303. int rc;
  304. vcpu->stat.instruction_sske++;
  305. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  306. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  307. rc = try_handle_skey(vcpu);
  308. if (rc)
  309. return rc != -EAGAIN ? rc : 0;
  310. if (!test_kvm_facility(vcpu->kvm, 8))
  311. m3 &= ~SSKE_MB;
  312. if (!test_kvm_facility(vcpu->kvm, 10))
  313. m3 &= ~(SSKE_MC | SSKE_MR);
  314. if (!test_kvm_facility(vcpu->kvm, 14))
  315. m3 &= ~SSKE_NQ;
  316. kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
  317. key = vcpu->run->s.regs.gprs[reg1] & 0xfe;
  318. start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
  319. start = kvm_s390_logical_to_effective(vcpu, start);
  320. if (m3 & SSKE_MB) {
  321. /* start already designates an absolute address */
  322. end = (start + _SEGMENT_SIZE) & ~(_SEGMENT_SIZE - 1);
  323. } else {
  324. start = kvm_s390_real_to_abs(vcpu, start);
  325. end = start + PAGE_SIZE;
  326. }
  327. while (start != end) {
  328. unsigned long vmaddr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(start));
  329. unlocked = false;
  330. if (kvm_is_error_hva(vmaddr))
  331. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  332. mmap_read_lock(current->mm);
  333. rc = cond_set_guest_storage_key(current->mm, vmaddr, key, &oldkey,
  334. m3 & SSKE_NQ, m3 & SSKE_MR,
  335. m3 & SSKE_MC);
  336. if (rc < 0) {
  337. rc = fixup_user_fault(current->mm, vmaddr,
  338. FAULT_FLAG_WRITE, &unlocked);
  339. rc = !rc ? -EAGAIN : rc;
  340. }
  341. mmap_read_unlock(current->mm);
  342. if (rc == -EFAULT)
  343. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  344. if (rc == -EAGAIN)
  345. continue;
  346. if (rc < 0)
  347. return rc;
  348. start += PAGE_SIZE;
  349. }
  350. if (m3 & (SSKE_MC | SSKE_MR)) {
  351. if (m3 & SSKE_MB) {
  352. /* skey in reg1 is unpredictable */
  353. kvm_s390_set_psw_cc(vcpu, 3);
  354. } else {
  355. kvm_s390_set_psw_cc(vcpu, rc);
  356. vcpu->run->s.regs.gprs[reg1] &= ~0xff00UL;
  357. vcpu->run->s.regs.gprs[reg1] |= (u64) oldkey << 8;
  358. }
  359. }
  360. if (m3 & SSKE_MB) {
  361. if (psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_BITS_AMODE_64BIT)
  362. vcpu->run->s.regs.gprs[reg2] &= ~PAGE_MASK;
  363. else
  364. vcpu->run->s.regs.gprs[reg2] &= ~0xfffff000UL;
  365. end = kvm_s390_logical_to_effective(vcpu, end);
  366. vcpu->run->s.regs.gprs[reg2] |= end;
  367. }
  368. return 0;
  369. }
  370. static int handle_ipte_interlock(struct kvm_vcpu *vcpu)
  371. {
  372. vcpu->stat.instruction_ipte_interlock++;
  373. if (psw_bits(vcpu->arch.sie_block->gpsw).pstate)
  374. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  375. wait_event(vcpu->kvm->arch.ipte_wq, !ipte_lock_held(vcpu->kvm));
  376. kvm_s390_retry_instr(vcpu);
  377. VCPU_EVENT(vcpu, 4, "%s", "retrying ipte interlock operation");
  378. return 0;
  379. }
  380. static int handle_test_block(struct kvm_vcpu *vcpu)
  381. {
  382. gpa_t addr;
  383. int reg2;
  384. vcpu->stat.instruction_tb++;
  385. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  386. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  387. kvm_s390_get_regs_rre(vcpu, NULL, &reg2);
  388. addr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
  389. addr = kvm_s390_logical_to_effective(vcpu, addr);
  390. if (kvm_s390_check_low_addr_prot_real(vcpu, addr))
  391. return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
  392. addr = kvm_s390_real_to_abs(vcpu, addr);
  393. if (kvm_is_error_gpa(vcpu->kvm, addr))
  394. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  395. /*
  396. * We don't expect errors on modern systems, and do not care
  397. * about storage keys (yet), so let's just clear the page.
  398. */
  399. if (kvm_clear_guest(vcpu->kvm, addr, PAGE_SIZE))
  400. return -EFAULT;
  401. kvm_s390_set_psw_cc(vcpu, 0);
  402. vcpu->run->s.regs.gprs[0] = 0;
  403. return 0;
  404. }
  405. static int handle_tpi(struct kvm_vcpu *vcpu)
  406. {
  407. struct kvm_s390_interrupt_info *inti;
  408. unsigned long len;
  409. u32 tpi_data[3];
  410. int rc;
  411. u64 addr;
  412. u8 ar;
  413. vcpu->stat.instruction_tpi++;
  414. addr = kvm_s390_get_base_disp_s(vcpu, &ar);
  415. if (addr & 3)
  416. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  417. inti = kvm_s390_get_io_int(vcpu->kvm, vcpu->arch.sie_block->gcr[6], 0);
  418. if (!inti) {
  419. kvm_s390_set_psw_cc(vcpu, 0);
  420. return 0;
  421. }
  422. tpi_data[0] = inti->io.subchannel_id << 16 | inti->io.subchannel_nr;
  423. tpi_data[1] = inti->io.io_int_parm;
  424. tpi_data[2] = inti->io.io_int_word;
  425. if (addr) {
  426. /*
  427. * Store the two-word I/O interruption code into the
  428. * provided area.
  429. */
  430. len = sizeof(tpi_data) - 4;
  431. rc = write_guest(vcpu, addr, ar, &tpi_data, len);
  432. if (rc) {
  433. rc = kvm_s390_inject_prog_cond(vcpu, rc);
  434. goto reinject_interrupt;
  435. }
  436. } else {
  437. /*
  438. * Store the three-word I/O interruption code into
  439. * the appropriate lowcore area.
  440. */
  441. len = sizeof(tpi_data);
  442. if (write_guest_lc(vcpu, __LC_SUBCHANNEL_ID, &tpi_data, len)) {
  443. /* failed writes to the low core are not recoverable */
  444. rc = -EFAULT;
  445. goto reinject_interrupt;
  446. }
  447. }
  448. /* irq was successfully handed to the guest */
  449. kfree(inti);
  450. kvm_s390_set_psw_cc(vcpu, 1);
  451. return 0;
  452. reinject_interrupt:
  453. /*
  454. * If we encounter a problem storing the interruption code, the
  455. * instruction is suppressed from the guest's view: reinject the
  456. * interrupt.
  457. */
  458. if (kvm_s390_reinject_io_int(vcpu->kvm, inti)) {
  459. kfree(inti);
  460. rc = -EFAULT;
  461. }
  462. /* don't set the cc, a pgm irq was injected or we drop to user space */
  463. return rc ? -EFAULT : 0;
  464. }
  465. static int handle_tsch(struct kvm_vcpu *vcpu)
  466. {
  467. struct kvm_s390_interrupt_info *inti = NULL;
  468. const u64 isc_mask = 0xffUL << 24; /* all iscs set */
  469. vcpu->stat.instruction_tsch++;
  470. /* a valid schid has at least one bit set */
  471. if (vcpu->run->s.regs.gprs[1])
  472. inti = kvm_s390_get_io_int(vcpu->kvm, isc_mask,
  473. vcpu->run->s.regs.gprs[1]);
  474. /*
  475. * Prepare exit to userspace.
  476. * We indicate whether we dequeued a pending I/O interrupt
  477. * so that userspace can re-inject it if the instruction gets
  478. * a program check. While this may re-order the pending I/O
  479. * interrupts, this is no problem since the priority is kept
  480. * intact.
  481. */
  482. vcpu->run->exit_reason = KVM_EXIT_S390_TSCH;
  483. vcpu->run->s390_tsch.dequeued = !!inti;
  484. if (inti) {
  485. vcpu->run->s390_tsch.subchannel_id = inti->io.subchannel_id;
  486. vcpu->run->s390_tsch.subchannel_nr = inti->io.subchannel_nr;
  487. vcpu->run->s390_tsch.io_int_parm = inti->io.io_int_parm;
  488. vcpu->run->s390_tsch.io_int_word = inti->io.io_int_word;
  489. }
  490. vcpu->run->s390_tsch.ipb = vcpu->arch.sie_block->ipb;
  491. kfree(inti);
  492. return -EREMOTE;
  493. }
  494. static int handle_io_inst(struct kvm_vcpu *vcpu)
  495. {
  496. VCPU_EVENT(vcpu, 4, "%s", "I/O instruction");
  497. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  498. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  499. if (vcpu->kvm->arch.css_support) {
  500. /*
  501. * Most I/O instructions will be handled by userspace.
  502. * Exceptions are tpi and the interrupt portion of tsch.
  503. */
  504. if (vcpu->arch.sie_block->ipa == 0xb236)
  505. return handle_tpi(vcpu);
  506. if (vcpu->arch.sie_block->ipa == 0xb235)
  507. return handle_tsch(vcpu);
  508. /* Handle in userspace. */
  509. vcpu->stat.instruction_io_other++;
  510. return -EOPNOTSUPP;
  511. } else {
  512. /*
  513. * Set condition code 3 to stop the guest from issuing channel
  514. * I/O instructions.
  515. */
  516. kvm_s390_set_psw_cc(vcpu, 3);
  517. return 0;
  518. }
  519. }
  520. /*
  521. * handle_pqap: Handling pqap interception
  522. * @vcpu: the vcpu having issue the pqap instruction
  523. *
  524. * We now support PQAP/AQIC instructions and we need to correctly
  525. * answer the guest even if no dedicated driver's hook is available.
  526. *
  527. * The intercepting code calls a dedicated callback for this instruction
  528. * if a driver did register one in the CRYPTO satellite of the
  529. * SIE block.
  530. *
  531. * If no callback is available, the queues are not available, return this
  532. * response code to the caller and set CC to 3.
  533. * Else return the response code returned by the callback.
  534. */
  535. static int handle_pqap(struct kvm_vcpu *vcpu)
  536. {
  537. struct ap_queue_status status = {};
  538. crypto_hook pqap_hook;
  539. unsigned long reg0;
  540. int ret;
  541. uint8_t fc;
  542. /* Verify that the AP instruction are available */
  543. if (!ap_instructions_available())
  544. return -EOPNOTSUPP;
  545. /* Verify that the guest is allowed to use AP instructions */
  546. if (!(vcpu->arch.sie_block->eca & ECA_APIE))
  547. return -EOPNOTSUPP;
  548. /*
  549. * The only possibly intercepted functions when AP instructions are
  550. * available for the guest are AQIC and TAPQ with the t bit set
  551. * since we do not set IC.3 (FIII) we currently will only intercept
  552. * the AQIC function code.
  553. * Note: running nested under z/VM can result in intercepts for other
  554. * function codes, e.g. PQAP(QCI). We do not support this and bail out.
  555. */
  556. reg0 = vcpu->run->s.regs.gprs[0];
  557. fc = (reg0 >> 24) & 0xff;
  558. if (fc != 0x03)
  559. return -EOPNOTSUPP;
  560. /* PQAP instruction is allowed for guest kernel only */
  561. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  562. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  563. /* Common PQAP instruction specification exceptions */
  564. /* bits 41-47 must all be zeros */
  565. if (reg0 & 0x007f0000UL)
  566. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  567. /* APFT not install and T bit set */
  568. if (!test_kvm_facility(vcpu->kvm, 15) && (reg0 & 0x00800000UL))
  569. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  570. /* APXA not installed and APID greater 64 or APQI greater 16 */
  571. if (!(vcpu->kvm->arch.crypto.crycbd & 0x02) && (reg0 & 0x0000c0f0UL))
  572. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  573. /* AQIC function code specific exception */
  574. /* facility 65 not present for AQIC function code */
  575. if (!test_kvm_facility(vcpu->kvm, 65))
  576. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  577. /*
  578. * If the hook callback is registered, there will be a pointer to the
  579. * hook function pointer in the kvm_s390_crypto structure. Lock the
  580. * owner, retrieve the hook function pointer and call the hook.
  581. */
  582. down_read(&vcpu->kvm->arch.crypto.pqap_hook_rwsem);
  583. if (vcpu->kvm->arch.crypto.pqap_hook) {
  584. pqap_hook = *vcpu->kvm->arch.crypto.pqap_hook;
  585. ret = pqap_hook(vcpu);
  586. if (!ret && vcpu->run->s.regs.gprs[1] & 0x00ff0000)
  587. kvm_s390_set_psw_cc(vcpu, 3);
  588. up_read(&vcpu->kvm->arch.crypto.pqap_hook_rwsem);
  589. return ret;
  590. }
  591. up_read(&vcpu->kvm->arch.crypto.pqap_hook_rwsem);
  592. /*
  593. * A vfio_driver must register a hook.
  594. * No hook means no driver to enable the SIE CRYCB and no queues.
  595. * We send this response to the guest.
  596. */
  597. status.response_code = 0x01;
  598. memcpy(&vcpu->run->s.regs.gprs[1], &status, sizeof(status));
  599. kvm_s390_set_psw_cc(vcpu, 3);
  600. return 0;
  601. }
  602. static int handle_stfl(struct kvm_vcpu *vcpu)
  603. {
  604. int rc;
  605. unsigned int fac;
  606. vcpu->stat.instruction_stfl++;
  607. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  608. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  609. /*
  610. * We need to shift the lower 32 facility bits (bit 0-31) from a u64
  611. * into a u32 memory representation. They will remain bits 0-31.
  612. */
  613. fac = *vcpu->kvm->arch.model.fac_list >> 32;
  614. rc = write_guest_lc(vcpu, offsetof(struct lowcore, stfl_fac_list),
  615. &fac, sizeof(fac));
  616. if (rc)
  617. return rc;
  618. VCPU_EVENT(vcpu, 3, "STFL: store facility list 0x%x", fac);
  619. trace_kvm_s390_handle_stfl(vcpu, fac);
  620. return 0;
  621. }
  622. #define PSW_MASK_ADDR_MODE (PSW_MASK_EA | PSW_MASK_BA)
  623. #define PSW_MASK_UNASSIGNED 0xb80800fe7fffffffUL
  624. #define PSW_ADDR_24 0x0000000000ffffffUL
  625. #define PSW_ADDR_31 0x000000007fffffffUL
  626. int is_valid_psw(psw_t *psw)
  627. {
  628. if (psw->mask & PSW_MASK_UNASSIGNED)
  629. return 0;
  630. if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_BA) {
  631. if (psw->addr & ~PSW_ADDR_31)
  632. return 0;
  633. }
  634. if (!(psw->mask & PSW_MASK_ADDR_MODE) && (psw->addr & ~PSW_ADDR_24))
  635. return 0;
  636. if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_EA)
  637. return 0;
  638. if (psw->addr & 1)
  639. return 0;
  640. return 1;
  641. }
  642. int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu)
  643. {
  644. psw_t *gpsw = &vcpu->arch.sie_block->gpsw;
  645. psw_compat_t new_psw;
  646. u64 addr;
  647. int rc;
  648. u8 ar;
  649. vcpu->stat.instruction_lpsw++;
  650. if (gpsw->mask & PSW_MASK_PSTATE)
  651. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  652. addr = kvm_s390_get_base_disp_s(vcpu, &ar);
  653. if (addr & 7)
  654. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  655. rc = read_guest(vcpu, addr, ar, &new_psw, sizeof(new_psw));
  656. if (rc)
  657. return kvm_s390_inject_prog_cond(vcpu, rc);
  658. if (!(new_psw.mask & PSW32_MASK_BASE))
  659. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  660. gpsw->mask = (new_psw.mask & ~PSW32_MASK_BASE) << 32;
  661. gpsw->mask |= new_psw.addr & PSW32_ADDR_AMODE;
  662. gpsw->addr = new_psw.addr & ~PSW32_ADDR_AMODE;
  663. if (!is_valid_psw(gpsw))
  664. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  665. return 0;
  666. }
  667. static int handle_lpswe(struct kvm_vcpu *vcpu)
  668. {
  669. psw_t new_psw;
  670. u64 addr;
  671. int rc;
  672. u8 ar;
  673. vcpu->stat.instruction_lpswe++;
  674. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  675. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  676. addr = kvm_s390_get_base_disp_s(vcpu, &ar);
  677. if (addr & 7)
  678. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  679. rc = read_guest(vcpu, addr, ar, &new_psw, sizeof(new_psw));
  680. if (rc)
  681. return kvm_s390_inject_prog_cond(vcpu, rc);
  682. vcpu->arch.sie_block->gpsw = new_psw;
  683. if (!is_valid_psw(&vcpu->arch.sie_block->gpsw))
  684. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  685. return 0;
  686. }
  687. static int handle_stidp(struct kvm_vcpu *vcpu)
  688. {
  689. u64 stidp_data = vcpu->kvm->arch.model.cpuid;
  690. u64 operand2;
  691. int rc;
  692. u8 ar;
  693. vcpu->stat.instruction_stidp++;
  694. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  695. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  696. operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
  697. if (operand2 & 7)
  698. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  699. rc = write_guest(vcpu, operand2, ar, &stidp_data, sizeof(stidp_data));
  700. if (rc)
  701. return kvm_s390_inject_prog_cond(vcpu, rc);
  702. VCPU_EVENT(vcpu, 3, "STIDP: store cpu id 0x%llx", stidp_data);
  703. return 0;
  704. }
  705. static void handle_stsi_3_2_2(struct kvm_vcpu *vcpu, struct sysinfo_3_2_2 *mem)
  706. {
  707. int cpus = 0;
  708. int n;
  709. cpus = atomic_read(&vcpu->kvm->online_vcpus);
  710. /* deal with other level 3 hypervisors */
  711. if (stsi(mem, 3, 2, 2))
  712. mem->count = 0;
  713. if (mem->count < 8)
  714. mem->count++;
  715. for (n = mem->count - 1; n > 0 ; n--)
  716. memcpy(&mem->vm[n], &mem->vm[n - 1], sizeof(mem->vm[0]));
  717. memset(&mem->vm[0], 0, sizeof(mem->vm[0]));
  718. mem->vm[0].cpus_total = cpus;
  719. mem->vm[0].cpus_configured = cpus;
  720. mem->vm[0].cpus_standby = 0;
  721. mem->vm[0].cpus_reserved = 0;
  722. mem->vm[0].caf = 1000;
  723. memcpy(mem->vm[0].name, "KVMguest", 8);
  724. ASCEBC(mem->vm[0].name, 8);
  725. memcpy(mem->vm[0].cpi, "KVM/Linux ", 16);
  726. ASCEBC(mem->vm[0].cpi, 16);
  727. }
  728. static void insert_stsi_usr_data(struct kvm_vcpu *vcpu, u64 addr, u8 ar,
  729. u8 fc, u8 sel1, u16 sel2)
  730. {
  731. vcpu->run->exit_reason = KVM_EXIT_S390_STSI;
  732. vcpu->run->s390_stsi.addr = addr;
  733. vcpu->run->s390_stsi.ar = ar;
  734. vcpu->run->s390_stsi.fc = fc;
  735. vcpu->run->s390_stsi.sel1 = sel1;
  736. vcpu->run->s390_stsi.sel2 = sel2;
  737. }
  738. static int handle_stsi(struct kvm_vcpu *vcpu)
  739. {
  740. int fc = (vcpu->run->s.regs.gprs[0] & 0xf0000000) >> 28;
  741. int sel1 = vcpu->run->s.regs.gprs[0] & 0xff;
  742. int sel2 = vcpu->run->s.regs.gprs[1] & 0xffff;
  743. unsigned long mem = 0;
  744. u64 operand2;
  745. int rc = 0;
  746. u8 ar;
  747. vcpu->stat.instruction_stsi++;
  748. VCPU_EVENT(vcpu, 3, "STSI: fc: %u sel1: %u sel2: %u", fc, sel1, sel2);
  749. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  750. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  751. /* Bailout forbidden function codes */
  752. if (fc > 3 && fc != 15)
  753. goto out_no_data;
  754. /*
  755. * fc 15 is provided only with
  756. * - PTF/CPU topology support through facility 15
  757. * - KVM_CAP_S390_USER_STSI
  758. */
  759. if (fc == 15 && (!test_kvm_facility(vcpu->kvm, 11) ||
  760. !vcpu->kvm->arch.user_stsi))
  761. goto out_no_data;
  762. if (vcpu->run->s.regs.gprs[0] & 0x0fffff00
  763. || vcpu->run->s.regs.gprs[1] & 0xffff0000)
  764. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  765. if (fc == 0) {
  766. vcpu->run->s.regs.gprs[0] = 3 << 28;
  767. kvm_s390_set_psw_cc(vcpu, 0);
  768. return 0;
  769. }
  770. operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
  771. if (!kvm_s390_pv_cpu_is_protected(vcpu) && (operand2 & 0xfff))
  772. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  773. switch (fc) {
  774. case 1: /* same handling for 1 and 2 */
  775. case 2:
  776. mem = get_zeroed_page(GFP_KERNEL_ACCOUNT);
  777. if (!mem)
  778. goto out_no_data;
  779. if (stsi((void *) mem, fc, sel1, sel2))
  780. goto out_no_data;
  781. break;
  782. case 3:
  783. if (sel1 != 2 || sel2 != 2)
  784. goto out_no_data;
  785. mem = get_zeroed_page(GFP_KERNEL_ACCOUNT);
  786. if (!mem)
  787. goto out_no_data;
  788. handle_stsi_3_2_2(vcpu, (void *) mem);
  789. break;
  790. case 15: /* fc 15 is fully handled in userspace */
  791. insert_stsi_usr_data(vcpu, operand2, ar, fc, sel1, sel2);
  792. trace_kvm_s390_handle_stsi(vcpu, fc, sel1, sel2, operand2);
  793. return -EREMOTE;
  794. }
  795. if (kvm_s390_pv_cpu_is_protected(vcpu)) {
  796. memcpy((void *)sida_origin(vcpu->arch.sie_block), (void *)mem,
  797. PAGE_SIZE);
  798. rc = 0;
  799. } else {
  800. rc = write_guest(vcpu, operand2, ar, (void *)mem, PAGE_SIZE);
  801. }
  802. if (rc) {
  803. rc = kvm_s390_inject_prog_cond(vcpu, rc);
  804. goto out;
  805. }
  806. if (vcpu->kvm->arch.user_stsi) {
  807. insert_stsi_usr_data(vcpu, operand2, ar, fc, sel1, sel2);
  808. rc = -EREMOTE;
  809. }
  810. trace_kvm_s390_handle_stsi(vcpu, fc, sel1, sel2, operand2);
  811. free_page(mem);
  812. kvm_s390_set_psw_cc(vcpu, 0);
  813. vcpu->run->s.regs.gprs[0] = 0;
  814. return rc;
  815. out_no_data:
  816. kvm_s390_set_psw_cc(vcpu, 3);
  817. out:
  818. free_page(mem);
  819. return rc;
  820. }
  821. int kvm_s390_handle_b2(struct kvm_vcpu *vcpu)
  822. {
  823. switch (vcpu->arch.sie_block->ipa & 0x00ff) {
  824. case 0x02:
  825. return handle_stidp(vcpu);
  826. case 0x04:
  827. return handle_set_clock(vcpu);
  828. case 0x10:
  829. return handle_set_prefix(vcpu);
  830. case 0x11:
  831. return handle_store_prefix(vcpu);
  832. case 0x12:
  833. return handle_store_cpu_address(vcpu);
  834. case 0x14:
  835. return kvm_s390_handle_vsie(vcpu);
  836. case 0x21:
  837. case 0x50:
  838. return handle_ipte_interlock(vcpu);
  839. case 0x29:
  840. return handle_iske(vcpu);
  841. case 0x2a:
  842. return handle_rrbe(vcpu);
  843. case 0x2b:
  844. return handle_sske(vcpu);
  845. case 0x2c:
  846. return handle_test_block(vcpu);
  847. case 0x30:
  848. case 0x31:
  849. case 0x32:
  850. case 0x33:
  851. case 0x34:
  852. case 0x35:
  853. case 0x36:
  854. case 0x37:
  855. case 0x38:
  856. case 0x39:
  857. case 0x3a:
  858. case 0x3b:
  859. case 0x3c:
  860. case 0x5f:
  861. case 0x74:
  862. case 0x76:
  863. return handle_io_inst(vcpu);
  864. case 0x56:
  865. return handle_sthyi(vcpu);
  866. case 0x7d:
  867. return handle_stsi(vcpu);
  868. case 0xaf:
  869. return handle_pqap(vcpu);
  870. case 0xb1:
  871. return handle_stfl(vcpu);
  872. case 0xb2:
  873. return handle_lpswe(vcpu);
  874. default:
  875. return -EOPNOTSUPP;
  876. }
  877. }
  878. static int handle_epsw(struct kvm_vcpu *vcpu)
  879. {
  880. int reg1, reg2;
  881. vcpu->stat.instruction_epsw++;
  882. kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
  883. /* This basically extracts the mask half of the psw. */
  884. vcpu->run->s.regs.gprs[reg1] &= 0xffffffff00000000UL;
  885. vcpu->run->s.regs.gprs[reg1] |= vcpu->arch.sie_block->gpsw.mask >> 32;
  886. if (reg2) {
  887. vcpu->run->s.regs.gprs[reg2] &= 0xffffffff00000000UL;
  888. vcpu->run->s.regs.gprs[reg2] |=
  889. vcpu->arch.sie_block->gpsw.mask & 0x00000000ffffffffUL;
  890. }
  891. return 0;
  892. }
  893. #define PFMF_RESERVED 0xfffc0101UL
  894. #define PFMF_SK 0x00020000UL
  895. #define PFMF_CF 0x00010000UL
  896. #define PFMF_UI 0x00008000UL
  897. #define PFMF_FSC 0x00007000UL
  898. #define PFMF_NQ 0x00000800UL
  899. #define PFMF_MR 0x00000400UL
  900. #define PFMF_MC 0x00000200UL
  901. #define PFMF_KEY 0x000000feUL
  902. static int handle_pfmf(struct kvm_vcpu *vcpu)
  903. {
  904. bool mr = false, mc = false, nq;
  905. int reg1, reg2;
  906. unsigned long start, end;
  907. unsigned char key;
  908. vcpu->stat.instruction_pfmf++;
  909. kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
  910. if (!test_kvm_facility(vcpu->kvm, 8))
  911. return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
  912. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  913. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  914. if (vcpu->run->s.regs.gprs[reg1] & PFMF_RESERVED)
  915. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  916. /* Only provide non-quiescing support if enabled for the guest */
  917. if (vcpu->run->s.regs.gprs[reg1] & PFMF_NQ &&
  918. !test_kvm_facility(vcpu->kvm, 14))
  919. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  920. /* Only provide conditional-SSKE support if enabled for the guest */
  921. if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK &&
  922. test_kvm_facility(vcpu->kvm, 10)) {
  923. mr = vcpu->run->s.regs.gprs[reg1] & PFMF_MR;
  924. mc = vcpu->run->s.regs.gprs[reg1] & PFMF_MC;
  925. }
  926. nq = vcpu->run->s.regs.gprs[reg1] & PFMF_NQ;
  927. key = vcpu->run->s.regs.gprs[reg1] & PFMF_KEY;
  928. start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
  929. start = kvm_s390_logical_to_effective(vcpu, start);
  930. if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
  931. if (kvm_s390_check_low_addr_prot_real(vcpu, start))
  932. return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
  933. }
  934. switch (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
  935. case 0x00000000:
  936. /* only 4k frames specify a real address */
  937. start = kvm_s390_real_to_abs(vcpu, start);
  938. end = (start + PAGE_SIZE) & ~(PAGE_SIZE - 1);
  939. break;
  940. case 0x00001000:
  941. end = (start + _SEGMENT_SIZE) & ~(_SEGMENT_SIZE - 1);
  942. break;
  943. case 0x00002000:
  944. /* only support 2G frame size if EDAT2 is available and we are
  945. not in 24-bit addressing mode */
  946. if (!test_kvm_facility(vcpu->kvm, 78) ||
  947. psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_BITS_AMODE_24BIT)
  948. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  949. end = (start + _REGION3_SIZE) & ~(_REGION3_SIZE - 1);
  950. break;
  951. default:
  952. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  953. }
  954. while (start != end) {
  955. unsigned long vmaddr;
  956. bool unlocked = false;
  957. /* Translate guest address to host address */
  958. vmaddr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(start));
  959. if (kvm_is_error_hva(vmaddr))
  960. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  961. if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
  962. if (kvm_clear_guest(vcpu->kvm, start, PAGE_SIZE))
  963. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  964. }
  965. if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK) {
  966. int rc = kvm_s390_skey_check_enable(vcpu);
  967. if (rc)
  968. return rc;
  969. mmap_read_lock(current->mm);
  970. rc = cond_set_guest_storage_key(current->mm, vmaddr,
  971. key, NULL, nq, mr, mc);
  972. if (rc < 0) {
  973. rc = fixup_user_fault(current->mm, vmaddr,
  974. FAULT_FLAG_WRITE, &unlocked);
  975. rc = !rc ? -EAGAIN : rc;
  976. }
  977. mmap_read_unlock(current->mm);
  978. if (rc == -EFAULT)
  979. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  980. if (rc == -EAGAIN)
  981. continue;
  982. if (rc < 0)
  983. return rc;
  984. }
  985. start += PAGE_SIZE;
  986. }
  987. if (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
  988. if (psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_BITS_AMODE_64BIT) {
  989. vcpu->run->s.regs.gprs[reg2] = end;
  990. } else {
  991. vcpu->run->s.regs.gprs[reg2] &= ~0xffffffffUL;
  992. end = kvm_s390_logical_to_effective(vcpu, end);
  993. vcpu->run->s.regs.gprs[reg2] |= end;
  994. }
  995. }
  996. return 0;
  997. }
  998. /*
  999. * Must be called with relevant read locks held (kvm->mm->mmap_lock, kvm->srcu)
  1000. */
  1001. static inline int __do_essa(struct kvm_vcpu *vcpu, const int orc)
  1002. {
  1003. int r1, r2, nappended, entries;
  1004. unsigned long gfn, hva, res, pgstev, ptev;
  1005. unsigned long *cbrlo;
  1006. /*
  1007. * We don't need to set SD.FPF.SK to 1 here, because if we have a
  1008. * machine check here we either handle it or crash
  1009. */
  1010. kvm_s390_get_regs_rre(vcpu, &r1, &r2);
  1011. gfn = vcpu->run->s.regs.gprs[r2] >> PAGE_SHIFT;
  1012. hva = gfn_to_hva(vcpu->kvm, gfn);
  1013. entries = (vcpu->arch.sie_block->cbrlo & ~PAGE_MASK) >> 3;
  1014. if (kvm_is_error_hva(hva))
  1015. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  1016. nappended = pgste_perform_essa(vcpu->kvm->mm, hva, orc, &ptev, &pgstev);
  1017. if (nappended < 0) {
  1018. res = orc ? 0x10 : 0;
  1019. vcpu->run->s.regs.gprs[r1] = res; /* Exception Indication */
  1020. return 0;
  1021. }
  1022. res = (pgstev & _PGSTE_GPS_USAGE_MASK) >> 22;
  1023. /*
  1024. * Set the block-content state part of the result. 0 means resident, so
  1025. * nothing to do if the page is valid. 2 is for preserved pages
  1026. * (non-present and non-zero), and 3 for zero pages (non-present and
  1027. * zero).
  1028. */
  1029. if (ptev & _PAGE_INVALID) {
  1030. res |= 2;
  1031. if (pgstev & _PGSTE_GPS_ZERO)
  1032. res |= 1;
  1033. }
  1034. if (pgstev & _PGSTE_GPS_NODAT)
  1035. res |= 0x20;
  1036. vcpu->run->s.regs.gprs[r1] = res;
  1037. /*
  1038. * It is possible that all the normal 511 slots were full, in which case
  1039. * we will now write in the 512th slot, which is reserved for host use.
  1040. * In both cases we let the normal essa handling code process all the
  1041. * slots, including the reserved one, if needed.
  1042. */
  1043. if (nappended > 0) {
  1044. cbrlo = phys_to_virt(vcpu->arch.sie_block->cbrlo & PAGE_MASK);
  1045. cbrlo[entries] = gfn << PAGE_SHIFT;
  1046. }
  1047. if (orc) {
  1048. struct kvm_memory_slot *ms = gfn_to_memslot(vcpu->kvm, gfn);
  1049. /* Increment only if we are really flipping the bit */
  1050. if (ms && !test_and_set_bit(gfn - ms->base_gfn, kvm_second_dirty_bitmap(ms)))
  1051. atomic64_inc(&vcpu->kvm->arch.cmma_dirty_pages);
  1052. }
  1053. return nappended;
  1054. }
  1055. static int handle_essa(struct kvm_vcpu *vcpu)
  1056. {
  1057. /* entries expected to be 1FF */
  1058. int entries = (vcpu->arch.sie_block->cbrlo & ~PAGE_MASK) >> 3;
  1059. unsigned long *cbrlo;
  1060. struct gmap *gmap;
  1061. int i, orc;
  1062. VCPU_EVENT(vcpu, 4, "ESSA: release %d pages", entries);
  1063. gmap = vcpu->arch.gmap;
  1064. vcpu->stat.instruction_essa++;
  1065. if (!vcpu->kvm->arch.use_cmma)
  1066. return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
  1067. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  1068. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  1069. /* Check for invalid operation request code */
  1070. orc = (vcpu->arch.sie_block->ipb & 0xf0000000) >> 28;
  1071. /* ORCs 0-6 are always valid */
  1072. if (orc > (test_kvm_facility(vcpu->kvm, 147) ? ESSA_SET_STABLE_NODAT
  1073. : ESSA_SET_STABLE_IF_RESIDENT))
  1074. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  1075. if (!vcpu->kvm->arch.migration_mode) {
  1076. /*
  1077. * CMMA is enabled in the KVM settings, but is disabled in
  1078. * the SIE block and in the mm_context, and we are not doing
  1079. * a migration. Enable CMMA in the mm_context.
  1080. * Since we need to take a write lock to write to the context
  1081. * to avoid races with storage keys handling, we check if the
  1082. * value really needs to be written to; if the value is
  1083. * already correct, we do nothing and avoid the lock.
  1084. */
  1085. if (vcpu->kvm->mm->context.uses_cmm == 0) {
  1086. mmap_write_lock(vcpu->kvm->mm);
  1087. vcpu->kvm->mm->context.uses_cmm = 1;
  1088. mmap_write_unlock(vcpu->kvm->mm);
  1089. }
  1090. /*
  1091. * If we are here, we are supposed to have CMMA enabled in
  1092. * the SIE block. Enabling CMMA works on a per-CPU basis,
  1093. * while the context use_cmma flag is per process.
  1094. * It's possible that the context flag is enabled and the
  1095. * SIE flag is not, so we set the flag always; if it was
  1096. * already set, nothing changes, otherwise we enable it
  1097. * on this CPU too.
  1098. */
  1099. vcpu->arch.sie_block->ecb2 |= ECB2_CMMA;
  1100. /* Retry the ESSA instruction */
  1101. kvm_s390_retry_instr(vcpu);
  1102. } else {
  1103. int srcu_idx;
  1104. mmap_read_lock(vcpu->kvm->mm);
  1105. srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
  1106. i = __do_essa(vcpu, orc);
  1107. srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
  1108. mmap_read_unlock(vcpu->kvm->mm);
  1109. if (i < 0)
  1110. return i;
  1111. /* Account for the possible extra cbrl entry */
  1112. entries += i;
  1113. }
  1114. vcpu->arch.sie_block->cbrlo &= PAGE_MASK; /* reset nceo */
  1115. cbrlo = phys_to_virt(vcpu->arch.sie_block->cbrlo);
  1116. mmap_read_lock(gmap->mm);
  1117. for (i = 0; i < entries; ++i)
  1118. __gmap_zap(gmap, cbrlo[i]);
  1119. mmap_read_unlock(gmap->mm);
  1120. return 0;
  1121. }
  1122. int kvm_s390_handle_b9(struct kvm_vcpu *vcpu)
  1123. {
  1124. switch (vcpu->arch.sie_block->ipa & 0x00ff) {
  1125. case 0x8a:
  1126. case 0x8e:
  1127. case 0x8f:
  1128. return handle_ipte_interlock(vcpu);
  1129. case 0x8d:
  1130. return handle_epsw(vcpu);
  1131. case 0xab:
  1132. return handle_essa(vcpu);
  1133. case 0xaf:
  1134. return handle_pfmf(vcpu);
  1135. default:
  1136. return -EOPNOTSUPP;
  1137. }
  1138. }
  1139. int kvm_s390_handle_lctl(struct kvm_vcpu *vcpu)
  1140. {
  1141. int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
  1142. int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
  1143. int reg, rc, nr_regs;
  1144. u32 ctl_array[16];
  1145. u64 ga;
  1146. u8 ar;
  1147. vcpu->stat.instruction_lctl++;
  1148. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  1149. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  1150. ga = kvm_s390_get_base_disp_rs(vcpu, &ar);
  1151. if (ga & 3)
  1152. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  1153. VCPU_EVENT(vcpu, 4, "LCTL: r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
  1154. trace_kvm_s390_handle_lctl(vcpu, 0, reg1, reg3, ga);
  1155. nr_regs = ((reg3 - reg1) & 0xf) + 1;
  1156. rc = read_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u32));
  1157. if (rc)
  1158. return kvm_s390_inject_prog_cond(vcpu, rc);
  1159. reg = reg1;
  1160. nr_regs = 0;
  1161. do {
  1162. vcpu->arch.sie_block->gcr[reg] &= 0xffffffff00000000ul;
  1163. vcpu->arch.sie_block->gcr[reg] |= ctl_array[nr_regs++];
  1164. if (reg == reg3)
  1165. break;
  1166. reg = (reg + 1) % 16;
  1167. } while (1);
  1168. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  1169. return 0;
  1170. }
  1171. int kvm_s390_handle_stctl(struct kvm_vcpu *vcpu)
  1172. {
  1173. int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
  1174. int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
  1175. int reg, rc, nr_regs;
  1176. u32 ctl_array[16];
  1177. u64 ga;
  1178. u8 ar;
  1179. vcpu->stat.instruction_stctl++;
  1180. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  1181. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  1182. ga = kvm_s390_get_base_disp_rs(vcpu, &ar);
  1183. if (ga & 3)
  1184. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  1185. VCPU_EVENT(vcpu, 4, "STCTL r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
  1186. trace_kvm_s390_handle_stctl(vcpu, 0, reg1, reg3, ga);
  1187. reg = reg1;
  1188. nr_regs = 0;
  1189. do {
  1190. ctl_array[nr_regs++] = vcpu->arch.sie_block->gcr[reg];
  1191. if (reg == reg3)
  1192. break;
  1193. reg = (reg + 1) % 16;
  1194. } while (1);
  1195. rc = write_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u32));
  1196. return rc ? kvm_s390_inject_prog_cond(vcpu, rc) : 0;
  1197. }
  1198. static int handle_lctlg(struct kvm_vcpu *vcpu)
  1199. {
  1200. int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
  1201. int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
  1202. int reg, rc, nr_regs;
  1203. u64 ctl_array[16];
  1204. u64 ga;
  1205. u8 ar;
  1206. vcpu->stat.instruction_lctlg++;
  1207. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  1208. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  1209. ga = kvm_s390_get_base_disp_rsy(vcpu, &ar);
  1210. if (ga & 7)
  1211. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  1212. VCPU_EVENT(vcpu, 4, "LCTLG: r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
  1213. trace_kvm_s390_handle_lctl(vcpu, 1, reg1, reg3, ga);
  1214. nr_regs = ((reg3 - reg1) & 0xf) + 1;
  1215. rc = read_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u64));
  1216. if (rc)
  1217. return kvm_s390_inject_prog_cond(vcpu, rc);
  1218. reg = reg1;
  1219. nr_regs = 0;
  1220. do {
  1221. vcpu->arch.sie_block->gcr[reg] = ctl_array[nr_regs++];
  1222. if (reg == reg3)
  1223. break;
  1224. reg = (reg + 1) % 16;
  1225. } while (1);
  1226. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  1227. return 0;
  1228. }
  1229. static int handle_stctg(struct kvm_vcpu *vcpu)
  1230. {
  1231. int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
  1232. int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
  1233. int reg, rc, nr_regs;
  1234. u64 ctl_array[16];
  1235. u64 ga;
  1236. u8 ar;
  1237. vcpu->stat.instruction_stctg++;
  1238. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  1239. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  1240. ga = kvm_s390_get_base_disp_rsy(vcpu, &ar);
  1241. if (ga & 7)
  1242. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  1243. VCPU_EVENT(vcpu, 4, "STCTG r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
  1244. trace_kvm_s390_handle_stctl(vcpu, 1, reg1, reg3, ga);
  1245. reg = reg1;
  1246. nr_regs = 0;
  1247. do {
  1248. ctl_array[nr_regs++] = vcpu->arch.sie_block->gcr[reg];
  1249. if (reg == reg3)
  1250. break;
  1251. reg = (reg + 1) % 16;
  1252. } while (1);
  1253. rc = write_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u64));
  1254. return rc ? kvm_s390_inject_prog_cond(vcpu, rc) : 0;
  1255. }
  1256. int kvm_s390_handle_eb(struct kvm_vcpu *vcpu)
  1257. {
  1258. switch (vcpu->arch.sie_block->ipb & 0x000000ff) {
  1259. case 0x25:
  1260. return handle_stctg(vcpu);
  1261. case 0x2f:
  1262. return handle_lctlg(vcpu);
  1263. case 0x60:
  1264. case 0x61:
  1265. case 0x62:
  1266. return handle_ri(vcpu);
  1267. default:
  1268. return -EOPNOTSUPP;
  1269. }
  1270. }
  1271. static int handle_tprot(struct kvm_vcpu *vcpu)
  1272. {
  1273. u64 address, operand2;
  1274. unsigned long gpa;
  1275. u8 access_key;
  1276. bool writable;
  1277. int ret, cc;
  1278. u8 ar;
  1279. vcpu->stat.instruction_tprot++;
  1280. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  1281. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  1282. kvm_s390_get_base_disp_sse(vcpu, &address, &operand2, &ar, NULL);
  1283. access_key = (operand2 & 0xf0) >> 4;
  1284. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
  1285. ipte_lock(vcpu->kvm);
  1286. ret = guest_translate_address_with_key(vcpu, address, ar, &gpa,
  1287. GACC_STORE, access_key);
  1288. if (ret == 0) {
  1289. gfn_to_hva_prot(vcpu->kvm, gpa_to_gfn(gpa), &writable);
  1290. } else if (ret == PGM_PROTECTION) {
  1291. writable = false;
  1292. /* Write protected? Try again with read-only... */
  1293. ret = guest_translate_address_with_key(vcpu, address, ar, &gpa,
  1294. GACC_FETCH, access_key);
  1295. }
  1296. if (ret >= 0) {
  1297. cc = -1;
  1298. /* Fetching permitted; storing permitted */
  1299. if (ret == 0 && writable)
  1300. cc = 0;
  1301. /* Fetching permitted; storing not permitted */
  1302. else if (ret == 0 && !writable)
  1303. cc = 1;
  1304. /* Fetching not permitted; storing not permitted */
  1305. else if (ret == PGM_PROTECTION)
  1306. cc = 2;
  1307. /* Translation not available */
  1308. else if (ret != PGM_ADDRESSING && ret != PGM_TRANSLATION_SPEC)
  1309. cc = 3;
  1310. if (cc != -1) {
  1311. kvm_s390_set_psw_cc(vcpu, cc);
  1312. ret = 0;
  1313. } else {
  1314. ret = kvm_s390_inject_program_int(vcpu, ret);
  1315. }
  1316. }
  1317. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
  1318. ipte_unlock(vcpu->kvm);
  1319. return ret;
  1320. }
  1321. int kvm_s390_handle_e5(struct kvm_vcpu *vcpu)
  1322. {
  1323. switch (vcpu->arch.sie_block->ipa & 0x00ff) {
  1324. case 0x01:
  1325. return handle_tprot(vcpu);
  1326. default:
  1327. return -EOPNOTSUPP;
  1328. }
  1329. }
  1330. static int handle_sckpf(struct kvm_vcpu *vcpu)
  1331. {
  1332. u32 value;
  1333. vcpu->stat.instruction_sckpf++;
  1334. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  1335. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  1336. if (vcpu->run->s.regs.gprs[0] & 0x00000000ffff0000)
  1337. return kvm_s390_inject_program_int(vcpu,
  1338. PGM_SPECIFICATION);
  1339. value = vcpu->run->s.regs.gprs[0] & 0x000000000000ffff;
  1340. vcpu->arch.sie_block->todpr = value;
  1341. return 0;
  1342. }
  1343. static int handle_ptff(struct kvm_vcpu *vcpu)
  1344. {
  1345. vcpu->stat.instruction_ptff++;
  1346. /* we don't emulate any control instructions yet */
  1347. kvm_s390_set_psw_cc(vcpu, 3);
  1348. return 0;
  1349. }
  1350. int kvm_s390_handle_01(struct kvm_vcpu *vcpu)
  1351. {
  1352. switch (vcpu->arch.sie_block->ipa & 0x00ff) {
  1353. case 0x04:
  1354. return handle_ptff(vcpu);
  1355. case 0x07:
  1356. return handle_sckpf(vcpu);
  1357. default:
  1358. return -EOPNOTSUPP;
  1359. }
  1360. }