sigp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /*
  2. * handling interprocessor communication
  3. *
  4. * Copyright IBM Corp. 2008, 2013
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License (version 2 only)
  8. * as published by the Free Software Foundation.
  9. *
  10. * Author(s): Carsten Otte <cotte@de.ibm.com>
  11. * Christian Borntraeger <borntraeger@de.ibm.com>
  12. * Christian Ehrhardt <ehrhardt@de.ibm.com>
  13. */
  14. #include <linux/kvm.h>
  15. #include <linux/kvm_host.h>
  16. #include <linux/slab.h>
  17. #include <asm/sigp.h>
  18. #include "gaccess.h"
  19. #include "kvm-s390.h"
  20. #include "trace.h"
  21. static int __sigp_sense(struct kvm_vcpu *vcpu, struct kvm_vcpu *dst_vcpu,
  22. u64 *reg)
  23. {
  24. struct kvm_s390_local_interrupt *li;
  25. int cpuflags;
  26. int rc;
  27. int ext_call_pending;
  28. li = &dst_vcpu->arch.local_int;
  29. cpuflags = atomic_read(li->cpuflags);
  30. ext_call_pending = kvm_s390_ext_call_pending(dst_vcpu);
  31. if (!(cpuflags & CPUSTAT_STOPPED) && !ext_call_pending)
  32. rc = SIGP_CC_ORDER_CODE_ACCEPTED;
  33. else {
  34. *reg &= 0xffffffff00000000UL;
  35. if (ext_call_pending)
  36. *reg |= SIGP_STATUS_EXT_CALL_PENDING;
  37. if (cpuflags & CPUSTAT_STOPPED)
  38. *reg |= SIGP_STATUS_STOPPED;
  39. rc = SIGP_CC_STATUS_STORED;
  40. }
  41. VCPU_EVENT(vcpu, 4, "sensed status of cpu %x rc %x", dst_vcpu->vcpu_id,
  42. rc);
  43. return rc;
  44. }
  45. static int __inject_sigp_emergency(struct kvm_vcpu *vcpu,
  46. struct kvm_vcpu *dst_vcpu)
  47. {
  48. struct kvm_s390_irq irq = {
  49. .type = KVM_S390_INT_EMERGENCY,
  50. .u.emerg.code = vcpu->vcpu_id,
  51. };
  52. int rc = 0;
  53. rc = kvm_s390_inject_vcpu(dst_vcpu, &irq);
  54. if (!rc)
  55. VCPU_EVENT(vcpu, 4, "sent sigp emerg to cpu %x",
  56. dst_vcpu->vcpu_id);
  57. return rc ? rc : SIGP_CC_ORDER_CODE_ACCEPTED;
  58. }
  59. static int __sigp_emergency(struct kvm_vcpu *vcpu, struct kvm_vcpu *dst_vcpu)
  60. {
  61. return __inject_sigp_emergency(vcpu, dst_vcpu);
  62. }
  63. static int __sigp_conditional_emergency(struct kvm_vcpu *vcpu,
  64. struct kvm_vcpu *dst_vcpu,
  65. u16 asn, u64 *reg)
  66. {
  67. const u64 psw_int_mask = PSW_MASK_IO | PSW_MASK_EXT;
  68. u16 p_asn, s_asn;
  69. psw_t *psw;
  70. bool idle;
  71. idle = is_vcpu_idle(vcpu);
  72. psw = &dst_vcpu->arch.sie_block->gpsw;
  73. p_asn = dst_vcpu->arch.sie_block->gcr[4] & 0xffff; /* Primary ASN */
  74. s_asn = dst_vcpu->arch.sie_block->gcr[3] & 0xffff; /* Secondary ASN */
  75. /* Inject the emergency signal? */
  76. if (!is_vcpu_stopped(vcpu)
  77. || (psw->mask & psw_int_mask) != psw_int_mask
  78. || (idle && psw->addr != 0)
  79. || (!idle && (asn == p_asn || asn == s_asn))) {
  80. return __inject_sigp_emergency(vcpu, dst_vcpu);
  81. } else {
  82. *reg &= 0xffffffff00000000UL;
  83. *reg |= SIGP_STATUS_INCORRECT_STATE;
  84. return SIGP_CC_STATUS_STORED;
  85. }
  86. }
  87. static int __sigp_external_call(struct kvm_vcpu *vcpu,
  88. struct kvm_vcpu *dst_vcpu, u64 *reg)
  89. {
  90. struct kvm_s390_irq irq = {
  91. .type = KVM_S390_INT_EXTERNAL_CALL,
  92. .u.extcall.code = vcpu->vcpu_id,
  93. };
  94. int rc;
  95. rc = kvm_s390_inject_vcpu(dst_vcpu, &irq);
  96. if (rc == -EBUSY) {
  97. *reg &= 0xffffffff00000000UL;
  98. *reg |= SIGP_STATUS_EXT_CALL_PENDING;
  99. return SIGP_CC_STATUS_STORED;
  100. } else if (rc == 0) {
  101. VCPU_EVENT(vcpu, 4, "sent sigp ext call to cpu %x",
  102. dst_vcpu->vcpu_id);
  103. }
  104. return rc ? rc : SIGP_CC_ORDER_CODE_ACCEPTED;
  105. }
  106. static int __sigp_stop(struct kvm_vcpu *vcpu, struct kvm_vcpu *dst_vcpu)
  107. {
  108. struct kvm_s390_irq irq = {
  109. .type = KVM_S390_SIGP_STOP,
  110. };
  111. int rc;
  112. rc = kvm_s390_inject_vcpu(dst_vcpu, &irq);
  113. if (rc == -EBUSY)
  114. rc = SIGP_CC_BUSY;
  115. else if (rc == 0)
  116. VCPU_EVENT(vcpu, 4, "sent sigp stop to cpu %x",
  117. dst_vcpu->vcpu_id);
  118. return rc;
  119. }
  120. static int __sigp_stop_and_store_status(struct kvm_vcpu *vcpu,
  121. struct kvm_vcpu *dst_vcpu, u64 *reg)
  122. {
  123. struct kvm_s390_irq irq = {
  124. .type = KVM_S390_SIGP_STOP,
  125. .u.stop.flags = KVM_S390_STOP_FLAG_STORE_STATUS,
  126. };
  127. int rc;
  128. rc = kvm_s390_inject_vcpu(dst_vcpu, &irq);
  129. if (rc == -EBUSY)
  130. rc = SIGP_CC_BUSY;
  131. else if (rc == 0)
  132. VCPU_EVENT(vcpu, 4, "sent sigp stop and store status to cpu %x",
  133. dst_vcpu->vcpu_id);
  134. return rc;
  135. }
  136. static int __sigp_set_arch(struct kvm_vcpu *vcpu, u32 parameter)
  137. {
  138. int rc;
  139. unsigned int i;
  140. struct kvm_vcpu *v;
  141. switch (parameter & 0xff) {
  142. case 0:
  143. rc = SIGP_CC_NOT_OPERATIONAL;
  144. break;
  145. case 1:
  146. case 2:
  147. kvm_for_each_vcpu(i, v, vcpu->kvm) {
  148. v->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
  149. kvm_clear_async_pf_completion_queue(v);
  150. }
  151. rc = SIGP_CC_ORDER_CODE_ACCEPTED;
  152. break;
  153. default:
  154. rc = -EOPNOTSUPP;
  155. }
  156. return rc;
  157. }
  158. static int __sigp_set_prefix(struct kvm_vcpu *vcpu, struct kvm_vcpu *dst_vcpu,
  159. u32 address, u64 *reg)
  160. {
  161. struct kvm_s390_irq irq = {
  162. .type = KVM_S390_SIGP_SET_PREFIX,
  163. .u.prefix.address = address & 0x7fffe000u,
  164. };
  165. int rc;
  166. /*
  167. * Make sure the new value is valid memory. We only need to check the
  168. * first page, since address is 8k aligned and memory pieces are always
  169. * at least 1MB aligned and have at least a size of 1MB.
  170. */
  171. if (kvm_is_error_gpa(vcpu->kvm, irq.u.prefix.address)) {
  172. *reg &= 0xffffffff00000000UL;
  173. *reg |= SIGP_STATUS_INVALID_PARAMETER;
  174. return SIGP_CC_STATUS_STORED;
  175. }
  176. rc = kvm_s390_inject_vcpu(dst_vcpu, &irq);
  177. if (rc == -EBUSY) {
  178. *reg &= 0xffffffff00000000UL;
  179. *reg |= SIGP_STATUS_INCORRECT_STATE;
  180. return SIGP_CC_STATUS_STORED;
  181. }
  182. return rc;
  183. }
  184. static int __sigp_store_status_at_addr(struct kvm_vcpu *vcpu,
  185. struct kvm_vcpu *dst_vcpu,
  186. u32 addr, u64 *reg)
  187. {
  188. int flags;
  189. int rc;
  190. flags = atomic_read(dst_vcpu->arch.local_int.cpuflags);
  191. if (!(flags & CPUSTAT_STOPPED)) {
  192. *reg &= 0xffffffff00000000UL;
  193. *reg |= SIGP_STATUS_INCORRECT_STATE;
  194. return SIGP_CC_STATUS_STORED;
  195. }
  196. addr &= 0x7ffffe00;
  197. rc = kvm_s390_store_status_unloaded(dst_vcpu, addr);
  198. if (rc == -EFAULT) {
  199. *reg &= 0xffffffff00000000UL;
  200. *reg |= SIGP_STATUS_INVALID_PARAMETER;
  201. rc = SIGP_CC_STATUS_STORED;
  202. }
  203. return rc;
  204. }
  205. static int __sigp_sense_running(struct kvm_vcpu *vcpu,
  206. struct kvm_vcpu *dst_vcpu, u64 *reg)
  207. {
  208. struct kvm_s390_local_interrupt *li;
  209. int rc;
  210. if (!test_kvm_facility(vcpu->kvm, 9)) {
  211. *reg &= 0xffffffff00000000UL;
  212. *reg |= SIGP_STATUS_INVALID_ORDER;
  213. return SIGP_CC_STATUS_STORED;
  214. }
  215. li = &dst_vcpu->arch.local_int;
  216. if (atomic_read(li->cpuflags) & CPUSTAT_RUNNING) {
  217. /* running */
  218. rc = SIGP_CC_ORDER_CODE_ACCEPTED;
  219. } else {
  220. /* not running */
  221. *reg &= 0xffffffff00000000UL;
  222. *reg |= SIGP_STATUS_NOT_RUNNING;
  223. rc = SIGP_CC_STATUS_STORED;
  224. }
  225. VCPU_EVENT(vcpu, 4, "sensed running status of cpu %x rc %x",
  226. dst_vcpu->vcpu_id, rc);
  227. return rc;
  228. }
  229. static int __prepare_sigp_re_start(struct kvm_vcpu *vcpu,
  230. struct kvm_vcpu *dst_vcpu, u8 order_code)
  231. {
  232. struct kvm_s390_local_interrupt *li = &dst_vcpu->arch.local_int;
  233. /* handle (RE)START in user space */
  234. int rc = -EOPNOTSUPP;
  235. /* make sure we don't race with STOP irq injection */
  236. spin_lock(&li->lock);
  237. if (kvm_s390_is_stop_irq_pending(dst_vcpu))
  238. rc = SIGP_CC_BUSY;
  239. spin_unlock(&li->lock);
  240. return rc;
  241. }
  242. static int __prepare_sigp_cpu_reset(struct kvm_vcpu *vcpu,
  243. struct kvm_vcpu *dst_vcpu, u8 order_code)
  244. {
  245. /* handle (INITIAL) CPU RESET in user space */
  246. return -EOPNOTSUPP;
  247. }
  248. static int __prepare_sigp_unknown(struct kvm_vcpu *vcpu,
  249. struct kvm_vcpu *dst_vcpu)
  250. {
  251. /* handle unknown orders in user space */
  252. return -EOPNOTSUPP;
  253. }
  254. static int handle_sigp_dst(struct kvm_vcpu *vcpu, u8 order_code,
  255. u16 cpu_addr, u32 parameter, u64 *status_reg)
  256. {
  257. int rc;
  258. struct kvm_vcpu *dst_vcpu = kvm_get_vcpu_by_id(vcpu->kvm, cpu_addr);
  259. if (!dst_vcpu)
  260. return SIGP_CC_NOT_OPERATIONAL;
  261. switch (order_code) {
  262. case SIGP_SENSE:
  263. vcpu->stat.instruction_sigp_sense++;
  264. rc = __sigp_sense(vcpu, dst_vcpu, status_reg);
  265. break;
  266. case SIGP_EXTERNAL_CALL:
  267. vcpu->stat.instruction_sigp_external_call++;
  268. rc = __sigp_external_call(vcpu, dst_vcpu, status_reg);
  269. break;
  270. case SIGP_EMERGENCY_SIGNAL:
  271. vcpu->stat.instruction_sigp_emergency++;
  272. rc = __sigp_emergency(vcpu, dst_vcpu);
  273. break;
  274. case SIGP_STOP:
  275. vcpu->stat.instruction_sigp_stop++;
  276. rc = __sigp_stop(vcpu, dst_vcpu);
  277. break;
  278. case SIGP_STOP_AND_STORE_STATUS:
  279. vcpu->stat.instruction_sigp_stop_store_status++;
  280. rc = __sigp_stop_and_store_status(vcpu, dst_vcpu, status_reg);
  281. break;
  282. case SIGP_STORE_STATUS_AT_ADDRESS:
  283. vcpu->stat.instruction_sigp_store_status++;
  284. rc = __sigp_store_status_at_addr(vcpu, dst_vcpu, parameter,
  285. status_reg);
  286. break;
  287. case SIGP_SET_PREFIX:
  288. vcpu->stat.instruction_sigp_prefix++;
  289. rc = __sigp_set_prefix(vcpu, dst_vcpu, parameter, status_reg);
  290. break;
  291. case SIGP_COND_EMERGENCY_SIGNAL:
  292. vcpu->stat.instruction_sigp_cond_emergency++;
  293. rc = __sigp_conditional_emergency(vcpu, dst_vcpu, parameter,
  294. status_reg);
  295. break;
  296. case SIGP_SENSE_RUNNING:
  297. vcpu->stat.instruction_sigp_sense_running++;
  298. rc = __sigp_sense_running(vcpu, dst_vcpu, status_reg);
  299. break;
  300. case SIGP_START:
  301. vcpu->stat.instruction_sigp_start++;
  302. rc = __prepare_sigp_re_start(vcpu, dst_vcpu, order_code);
  303. break;
  304. case SIGP_RESTART:
  305. vcpu->stat.instruction_sigp_restart++;
  306. rc = __prepare_sigp_re_start(vcpu, dst_vcpu, order_code);
  307. break;
  308. case SIGP_INITIAL_CPU_RESET:
  309. vcpu->stat.instruction_sigp_init_cpu_reset++;
  310. rc = __prepare_sigp_cpu_reset(vcpu, dst_vcpu, order_code);
  311. break;
  312. case SIGP_CPU_RESET:
  313. vcpu->stat.instruction_sigp_cpu_reset++;
  314. rc = __prepare_sigp_cpu_reset(vcpu, dst_vcpu, order_code);
  315. break;
  316. default:
  317. vcpu->stat.instruction_sigp_unknown++;
  318. rc = __prepare_sigp_unknown(vcpu, dst_vcpu);
  319. }
  320. if (rc == -EOPNOTSUPP)
  321. VCPU_EVENT(vcpu, 4,
  322. "sigp order %u -> cpu %x: handled in user space",
  323. order_code, dst_vcpu->vcpu_id);
  324. return rc;
  325. }
  326. static int handle_sigp_order_in_user_space(struct kvm_vcpu *vcpu, u8 order_code,
  327. u16 cpu_addr)
  328. {
  329. if (!vcpu->kvm->arch.user_sigp)
  330. return 0;
  331. switch (order_code) {
  332. case SIGP_SENSE:
  333. case SIGP_EXTERNAL_CALL:
  334. case SIGP_EMERGENCY_SIGNAL:
  335. case SIGP_COND_EMERGENCY_SIGNAL:
  336. case SIGP_SENSE_RUNNING:
  337. return 0;
  338. /* update counters as we're directly dropping to user space */
  339. case SIGP_STOP:
  340. vcpu->stat.instruction_sigp_stop++;
  341. break;
  342. case SIGP_STOP_AND_STORE_STATUS:
  343. vcpu->stat.instruction_sigp_stop_store_status++;
  344. break;
  345. case SIGP_STORE_STATUS_AT_ADDRESS:
  346. vcpu->stat.instruction_sigp_store_status++;
  347. break;
  348. case SIGP_STORE_ADDITIONAL_STATUS:
  349. vcpu->stat.instruction_sigp_store_adtl_status++;
  350. break;
  351. case SIGP_SET_PREFIX:
  352. vcpu->stat.instruction_sigp_prefix++;
  353. break;
  354. case SIGP_START:
  355. vcpu->stat.instruction_sigp_start++;
  356. break;
  357. case SIGP_RESTART:
  358. vcpu->stat.instruction_sigp_restart++;
  359. break;
  360. case SIGP_INITIAL_CPU_RESET:
  361. vcpu->stat.instruction_sigp_init_cpu_reset++;
  362. break;
  363. case SIGP_CPU_RESET:
  364. vcpu->stat.instruction_sigp_cpu_reset++;
  365. break;
  366. default:
  367. vcpu->stat.instruction_sigp_unknown++;
  368. }
  369. VCPU_EVENT(vcpu, 3, "SIGP: order %u for CPU %d handled in userspace",
  370. order_code, cpu_addr);
  371. return 1;
  372. }
  373. int kvm_s390_handle_sigp(struct kvm_vcpu *vcpu)
  374. {
  375. int r1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
  376. int r3 = vcpu->arch.sie_block->ipa & 0x000f;
  377. u32 parameter;
  378. u16 cpu_addr = vcpu->run->s.regs.gprs[r3];
  379. u8 order_code;
  380. int rc;
  381. /* sigp in userspace can exit */
  382. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  383. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  384. order_code = kvm_s390_get_base_disp_rs(vcpu, NULL);
  385. if (handle_sigp_order_in_user_space(vcpu, order_code, cpu_addr))
  386. return -EOPNOTSUPP;
  387. if (r1 % 2)
  388. parameter = vcpu->run->s.regs.gprs[r1];
  389. else
  390. parameter = vcpu->run->s.regs.gprs[r1 + 1];
  391. trace_kvm_s390_handle_sigp(vcpu, order_code, cpu_addr, parameter);
  392. switch (order_code) {
  393. case SIGP_SET_ARCHITECTURE:
  394. vcpu->stat.instruction_sigp_arch++;
  395. rc = __sigp_set_arch(vcpu, parameter);
  396. break;
  397. default:
  398. rc = handle_sigp_dst(vcpu, order_code, cpu_addr,
  399. parameter,
  400. &vcpu->run->s.regs.gprs[r1]);
  401. }
  402. if (rc < 0)
  403. return rc;
  404. kvm_s390_set_psw_cc(vcpu, rc);
  405. return 0;
  406. }
  407. /*
  408. * Handle SIGP partial execution interception.
  409. *
  410. * This interception will occur at the source cpu when a source cpu sends an
  411. * external call to a target cpu and the target cpu has the WAIT bit set in
  412. * its cpuflags. Interception will occurr after the interrupt indicator bits at
  413. * the target cpu have been set. All error cases will lead to instruction
  414. * interception, therefore nothing is to be checked or prepared.
  415. */
  416. int kvm_s390_handle_sigp_pei(struct kvm_vcpu *vcpu)
  417. {
  418. int r3 = vcpu->arch.sie_block->ipa & 0x000f;
  419. u16 cpu_addr = vcpu->run->s.regs.gprs[r3];
  420. struct kvm_vcpu *dest_vcpu;
  421. u8 order_code = kvm_s390_get_base_disp_rs(vcpu, NULL);
  422. trace_kvm_s390_handle_sigp_pei(vcpu, order_code, cpu_addr);
  423. if (order_code == SIGP_EXTERNAL_CALL) {
  424. dest_vcpu = kvm_get_vcpu_by_id(vcpu->kvm, cpu_addr);
  425. BUG_ON(dest_vcpu == NULL);
  426. kvm_s390_vcpu_wakeup(dest_vcpu);
  427. kvm_s390_set_psw_cc(vcpu, SIGP_CC_ORDER_CODE_ACCEPTED);
  428. return 0;
  429. }
  430. return -EOPNOTSUPP;
  431. }