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