sigp.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. * handling interprocessor communication
  3. *
  4. * Copyright IBM Corp. 2008, 2009
  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, u16 cpu_addr,
  22. u64 *reg)
  23. {
  24. struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
  25. int rc;
  26. if (cpu_addr >= KVM_MAX_VCPUS)
  27. return SIGP_CC_NOT_OPERATIONAL;
  28. spin_lock(&fi->lock);
  29. if (fi->local_int[cpu_addr] == NULL)
  30. rc = SIGP_CC_NOT_OPERATIONAL;
  31. else if (!(atomic_read(fi->local_int[cpu_addr]->cpuflags)
  32. & (CPUSTAT_ECALL_PEND | CPUSTAT_STOPPED)))
  33. rc = SIGP_CC_ORDER_CODE_ACCEPTED;
  34. else {
  35. *reg &= 0xffffffff00000000UL;
  36. if (atomic_read(fi->local_int[cpu_addr]->cpuflags)
  37. & CPUSTAT_ECALL_PEND)
  38. *reg |= SIGP_STATUS_EXT_CALL_PENDING;
  39. if (atomic_read(fi->local_int[cpu_addr]->cpuflags)
  40. & CPUSTAT_STOPPED)
  41. *reg |= SIGP_STATUS_STOPPED;
  42. rc = SIGP_CC_STATUS_STORED;
  43. }
  44. spin_unlock(&fi->lock);
  45. VCPU_EVENT(vcpu, 4, "sensed status of cpu %x rc %x", cpu_addr, rc);
  46. return rc;
  47. }
  48. static int __sigp_emergency(struct kvm_vcpu *vcpu, u16 cpu_addr)
  49. {
  50. struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
  51. struct kvm_s390_local_interrupt *li;
  52. struct kvm_s390_interrupt_info *inti;
  53. int rc;
  54. if (cpu_addr >= KVM_MAX_VCPUS)
  55. return SIGP_CC_NOT_OPERATIONAL;
  56. inti = kzalloc(sizeof(*inti), GFP_KERNEL);
  57. if (!inti)
  58. return -ENOMEM;
  59. inti->type = KVM_S390_INT_EMERGENCY;
  60. inti->emerg.code = vcpu->vcpu_id;
  61. spin_lock(&fi->lock);
  62. li = fi->local_int[cpu_addr];
  63. if (li == NULL) {
  64. rc = SIGP_CC_NOT_OPERATIONAL;
  65. kfree(inti);
  66. goto unlock;
  67. }
  68. spin_lock_bh(&li->lock);
  69. list_add_tail(&inti->list, &li->list);
  70. atomic_set(&li->active, 1);
  71. atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
  72. if (waitqueue_active(&li->wq))
  73. wake_up_interruptible(&li->wq);
  74. spin_unlock_bh(&li->lock);
  75. rc = SIGP_CC_ORDER_CODE_ACCEPTED;
  76. VCPU_EVENT(vcpu, 4, "sent sigp emerg to cpu %x", cpu_addr);
  77. unlock:
  78. spin_unlock(&fi->lock);
  79. return rc;
  80. }
  81. static int __sigp_external_call(struct kvm_vcpu *vcpu, u16 cpu_addr)
  82. {
  83. struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
  84. struct kvm_s390_local_interrupt *li;
  85. struct kvm_s390_interrupt_info *inti;
  86. int rc;
  87. if (cpu_addr >= KVM_MAX_VCPUS)
  88. return SIGP_CC_NOT_OPERATIONAL;
  89. inti = kzalloc(sizeof(*inti), GFP_KERNEL);
  90. if (!inti)
  91. return -ENOMEM;
  92. inti->type = KVM_S390_INT_EXTERNAL_CALL;
  93. inti->extcall.code = vcpu->vcpu_id;
  94. spin_lock(&fi->lock);
  95. li = fi->local_int[cpu_addr];
  96. if (li == NULL) {
  97. rc = SIGP_CC_NOT_OPERATIONAL;
  98. kfree(inti);
  99. goto unlock;
  100. }
  101. spin_lock_bh(&li->lock);
  102. list_add_tail(&inti->list, &li->list);
  103. atomic_set(&li->active, 1);
  104. atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
  105. if (waitqueue_active(&li->wq))
  106. wake_up_interruptible(&li->wq);
  107. spin_unlock_bh(&li->lock);
  108. rc = SIGP_CC_ORDER_CODE_ACCEPTED;
  109. VCPU_EVENT(vcpu, 4, "sent sigp ext call to cpu %x", cpu_addr);
  110. unlock:
  111. spin_unlock(&fi->lock);
  112. return rc;
  113. }
  114. static int __inject_sigp_stop(struct kvm_s390_local_interrupt *li, int action)
  115. {
  116. struct kvm_s390_interrupt_info *inti;
  117. inti = kzalloc(sizeof(*inti), GFP_ATOMIC);
  118. if (!inti)
  119. return -ENOMEM;
  120. inti->type = KVM_S390_SIGP_STOP;
  121. spin_lock_bh(&li->lock);
  122. if ((atomic_read(li->cpuflags) & CPUSTAT_STOPPED)) {
  123. kfree(inti);
  124. goto out;
  125. }
  126. list_add_tail(&inti->list, &li->list);
  127. atomic_set(&li->active, 1);
  128. atomic_set_mask(CPUSTAT_STOP_INT, li->cpuflags);
  129. li->action_bits |= action;
  130. if (waitqueue_active(&li->wq))
  131. wake_up_interruptible(&li->wq);
  132. out:
  133. spin_unlock_bh(&li->lock);
  134. return SIGP_CC_ORDER_CODE_ACCEPTED;
  135. }
  136. static int __sigp_stop(struct kvm_vcpu *vcpu, u16 cpu_addr, int action)
  137. {
  138. struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
  139. struct kvm_s390_local_interrupt *li;
  140. int rc;
  141. if (cpu_addr >= KVM_MAX_VCPUS)
  142. return SIGP_CC_NOT_OPERATIONAL;
  143. spin_lock(&fi->lock);
  144. li = fi->local_int[cpu_addr];
  145. if (li == NULL) {
  146. rc = SIGP_CC_NOT_OPERATIONAL;
  147. goto unlock;
  148. }
  149. rc = __inject_sigp_stop(li, action);
  150. unlock:
  151. spin_unlock(&fi->lock);
  152. VCPU_EVENT(vcpu, 4, "sent sigp stop to cpu %x", cpu_addr);
  153. return rc;
  154. }
  155. int kvm_s390_inject_sigp_stop(struct kvm_vcpu *vcpu, int action)
  156. {
  157. struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
  158. return __inject_sigp_stop(li, action);
  159. }
  160. static int __sigp_set_arch(struct kvm_vcpu *vcpu, u32 parameter)
  161. {
  162. int rc;
  163. switch (parameter & 0xff) {
  164. case 0:
  165. rc = SIGP_CC_NOT_OPERATIONAL;
  166. break;
  167. case 1:
  168. case 2:
  169. rc = SIGP_CC_ORDER_CODE_ACCEPTED;
  170. break;
  171. default:
  172. rc = -EOPNOTSUPP;
  173. }
  174. return rc;
  175. }
  176. static int __sigp_set_prefix(struct kvm_vcpu *vcpu, u16 cpu_addr, u32 address,
  177. u64 *reg)
  178. {
  179. struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
  180. struct kvm_s390_local_interrupt *li = NULL;
  181. struct kvm_s390_interrupt_info *inti;
  182. int rc;
  183. u8 tmp;
  184. /* make sure that the new value is valid memory */
  185. address = address & 0x7fffe000u;
  186. if (copy_from_guest_absolute(vcpu, &tmp, address, 1) ||
  187. copy_from_guest_absolute(vcpu, &tmp, address + PAGE_SIZE, 1)) {
  188. *reg &= 0xffffffff00000000UL;
  189. *reg |= SIGP_STATUS_INVALID_PARAMETER;
  190. return SIGP_CC_STATUS_STORED;
  191. }
  192. inti = kzalloc(sizeof(*inti), GFP_KERNEL);
  193. if (!inti)
  194. return SIGP_CC_BUSY;
  195. spin_lock(&fi->lock);
  196. if (cpu_addr < KVM_MAX_VCPUS)
  197. li = fi->local_int[cpu_addr];
  198. if (li == NULL) {
  199. *reg &= 0xffffffff00000000UL;
  200. *reg |= SIGP_STATUS_INCORRECT_STATE;
  201. rc = SIGP_CC_STATUS_STORED;
  202. kfree(inti);
  203. goto out_fi;
  204. }
  205. spin_lock_bh(&li->lock);
  206. /* cpu must be in stopped state */
  207. if (!(atomic_read(li->cpuflags) & CPUSTAT_STOPPED)) {
  208. *reg &= 0xffffffff00000000UL;
  209. *reg |= SIGP_STATUS_INCORRECT_STATE;
  210. rc = SIGP_CC_STATUS_STORED;
  211. kfree(inti);
  212. goto out_li;
  213. }
  214. inti->type = KVM_S390_SIGP_SET_PREFIX;
  215. inti->prefix.address = address;
  216. list_add_tail(&inti->list, &li->list);
  217. atomic_set(&li->active, 1);
  218. if (waitqueue_active(&li->wq))
  219. wake_up_interruptible(&li->wq);
  220. rc = SIGP_CC_ORDER_CODE_ACCEPTED;
  221. VCPU_EVENT(vcpu, 4, "set prefix of cpu %02x to %x", cpu_addr, address);
  222. out_li:
  223. spin_unlock_bh(&li->lock);
  224. out_fi:
  225. spin_unlock(&fi->lock);
  226. return rc;
  227. }
  228. static int __sigp_sense_running(struct kvm_vcpu *vcpu, u16 cpu_addr,
  229. u64 *reg)
  230. {
  231. int rc;
  232. struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
  233. if (cpu_addr >= KVM_MAX_VCPUS)
  234. return SIGP_CC_NOT_OPERATIONAL;
  235. spin_lock(&fi->lock);
  236. if (fi->local_int[cpu_addr] == NULL)
  237. rc = SIGP_CC_NOT_OPERATIONAL;
  238. else {
  239. if (atomic_read(fi->local_int[cpu_addr]->cpuflags)
  240. & CPUSTAT_RUNNING) {
  241. /* running */
  242. rc = SIGP_CC_ORDER_CODE_ACCEPTED;
  243. } else {
  244. /* not running */
  245. *reg &= 0xffffffff00000000UL;
  246. *reg |= SIGP_STATUS_NOT_RUNNING;
  247. rc = SIGP_CC_STATUS_STORED;
  248. }
  249. }
  250. spin_unlock(&fi->lock);
  251. VCPU_EVENT(vcpu, 4, "sensed running status of cpu %x rc %x", cpu_addr,
  252. rc);
  253. return rc;
  254. }
  255. static int __sigp_restart(struct kvm_vcpu *vcpu, u16 cpu_addr)
  256. {
  257. struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
  258. struct kvm_s390_local_interrupt *li;
  259. int rc = SIGP_CC_ORDER_CODE_ACCEPTED;
  260. if (cpu_addr >= KVM_MAX_VCPUS)
  261. return SIGP_CC_NOT_OPERATIONAL;
  262. spin_lock(&fi->lock);
  263. li = fi->local_int[cpu_addr];
  264. if (li == NULL) {
  265. rc = SIGP_CC_NOT_OPERATIONAL;
  266. goto out;
  267. }
  268. spin_lock_bh(&li->lock);
  269. if (li->action_bits & ACTION_STOP_ON_STOP)
  270. rc = SIGP_CC_BUSY;
  271. else
  272. VCPU_EVENT(vcpu, 4, "sigp restart %x to handle userspace",
  273. cpu_addr);
  274. spin_unlock_bh(&li->lock);
  275. out:
  276. spin_unlock(&fi->lock);
  277. return rc;
  278. }
  279. int kvm_s390_handle_sigp(struct kvm_vcpu *vcpu)
  280. {
  281. int r1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
  282. int r3 = vcpu->arch.sie_block->ipa & 0x000f;
  283. u32 parameter;
  284. u16 cpu_addr = vcpu->run->s.regs.gprs[r3];
  285. u8 order_code;
  286. int rc;
  287. /* sigp in userspace can exit */
  288. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  289. return kvm_s390_inject_program_int(vcpu,
  290. PGM_PRIVILEGED_OPERATION);
  291. order_code = kvm_s390_get_base_disp_rs(vcpu);
  292. if (r1 % 2)
  293. parameter = vcpu->run->s.regs.gprs[r1];
  294. else
  295. parameter = vcpu->run->s.regs.gprs[r1 + 1];
  296. trace_kvm_s390_handle_sigp(vcpu, order_code, cpu_addr, parameter);
  297. switch (order_code) {
  298. case SIGP_SENSE:
  299. vcpu->stat.instruction_sigp_sense++;
  300. rc = __sigp_sense(vcpu, cpu_addr,
  301. &vcpu->run->s.regs.gprs[r1]);
  302. break;
  303. case SIGP_EXTERNAL_CALL:
  304. vcpu->stat.instruction_sigp_external_call++;
  305. rc = __sigp_external_call(vcpu, cpu_addr);
  306. break;
  307. case SIGP_EMERGENCY_SIGNAL:
  308. vcpu->stat.instruction_sigp_emergency++;
  309. rc = __sigp_emergency(vcpu, cpu_addr);
  310. break;
  311. case SIGP_STOP:
  312. vcpu->stat.instruction_sigp_stop++;
  313. rc = __sigp_stop(vcpu, cpu_addr, ACTION_STOP_ON_STOP);
  314. break;
  315. case SIGP_STOP_AND_STORE_STATUS:
  316. vcpu->stat.instruction_sigp_stop++;
  317. rc = __sigp_stop(vcpu, cpu_addr, ACTION_STORE_ON_STOP |
  318. ACTION_STOP_ON_STOP);
  319. break;
  320. case SIGP_SET_ARCHITECTURE:
  321. vcpu->stat.instruction_sigp_arch++;
  322. rc = __sigp_set_arch(vcpu, parameter);
  323. break;
  324. case SIGP_SET_PREFIX:
  325. vcpu->stat.instruction_sigp_prefix++;
  326. rc = __sigp_set_prefix(vcpu, cpu_addr, parameter,
  327. &vcpu->run->s.regs.gprs[r1]);
  328. break;
  329. case SIGP_SENSE_RUNNING:
  330. vcpu->stat.instruction_sigp_sense_running++;
  331. rc = __sigp_sense_running(vcpu, cpu_addr,
  332. &vcpu->run->s.regs.gprs[r1]);
  333. break;
  334. case SIGP_RESTART:
  335. vcpu->stat.instruction_sigp_restart++;
  336. rc = __sigp_restart(vcpu, cpu_addr);
  337. if (rc == SIGP_CC_BUSY)
  338. break;
  339. /* user space must know about restart */
  340. default:
  341. return -EOPNOTSUPP;
  342. }
  343. if (rc < 0)
  344. return rc;
  345. vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
  346. vcpu->arch.sie_block->gpsw.mask |= (rc & 3ul) << 44;
  347. return 0;
  348. }