sigp.c 8.8 KB

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