sigp.c 9.7 KB

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