sigp.c 9.5 KB

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