sigp.c 12 KB

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