sigp.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. spin_lock(&fi->lock);
  79. li = fi->local_int[cpu_addr];
  80. if (li == NULL) {
  81. rc = 3; /* not operational */
  82. kfree(inti);
  83. goto unlock;
  84. }
  85. spin_lock_bh(&li->lock);
  86. list_add_tail(&inti->list, &li->list);
  87. atomic_set(&li->active, 1);
  88. atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
  89. if (waitqueue_active(&li->wq))
  90. wake_up_interruptible(&li->wq);
  91. spin_unlock_bh(&li->lock);
  92. rc = 0; /* order accepted */
  93. unlock:
  94. spin_unlock(&fi->lock);
  95. VCPU_EVENT(vcpu, 4, "sent sigp emerg to cpu %x", cpu_addr);
  96. return rc;
  97. }
  98. static int __inject_sigp_stop(struct kvm_s390_local_interrupt *li, int action)
  99. {
  100. struct kvm_s390_interrupt_info *inti;
  101. inti = kzalloc(sizeof(*inti), GFP_ATOMIC);
  102. if (!inti)
  103. return -ENOMEM;
  104. inti->type = KVM_S390_SIGP_STOP;
  105. spin_lock_bh(&li->lock);
  106. list_add_tail(&inti->list, &li->list);
  107. atomic_set(&li->active, 1);
  108. atomic_set_mask(CPUSTAT_STOP_INT, li->cpuflags);
  109. li->action_bits |= action;
  110. if (waitqueue_active(&li->wq))
  111. wake_up_interruptible(&li->wq);
  112. spin_unlock_bh(&li->lock);
  113. return 0; /* order accepted */
  114. }
  115. static int __sigp_stop(struct kvm_vcpu *vcpu, u16 cpu_addr, int action)
  116. {
  117. struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
  118. struct kvm_s390_local_interrupt *li;
  119. int rc;
  120. if (cpu_addr >= KVM_MAX_VCPUS)
  121. return 3; /* not operational */
  122. spin_lock(&fi->lock);
  123. li = fi->local_int[cpu_addr];
  124. if (li == NULL) {
  125. rc = 3; /* not operational */
  126. goto unlock;
  127. }
  128. rc = __inject_sigp_stop(li, action);
  129. unlock:
  130. spin_unlock(&fi->lock);
  131. VCPU_EVENT(vcpu, 4, "sent sigp stop to cpu %x", cpu_addr);
  132. return rc;
  133. }
  134. int kvm_s390_inject_sigp_stop(struct kvm_vcpu *vcpu, int action)
  135. {
  136. struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
  137. return __inject_sigp_stop(li, action);
  138. }
  139. static int __sigp_set_arch(struct kvm_vcpu *vcpu, u32 parameter)
  140. {
  141. int rc;
  142. switch (parameter & 0xff) {
  143. case 0:
  144. rc = 3; /* not operational */
  145. break;
  146. case 1:
  147. case 2:
  148. rc = 0; /* order accepted */
  149. break;
  150. default:
  151. rc = -EOPNOTSUPP;
  152. }
  153. return rc;
  154. }
  155. static int __sigp_set_prefix(struct kvm_vcpu *vcpu, u16 cpu_addr, u32 address,
  156. unsigned long *reg)
  157. {
  158. struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
  159. struct kvm_s390_local_interrupt *li = NULL;
  160. struct kvm_s390_interrupt_info *inti;
  161. int rc;
  162. u8 tmp;
  163. /* make sure that the new value is valid memory */
  164. address = address & 0x7fffe000u;
  165. if ((copy_from_user(&tmp, (void __user *)
  166. (address + vcpu->arch.sie_block->gmsor) , 1)) ||
  167. (copy_from_user(&tmp, (void __user *)(address +
  168. vcpu->arch.sie_block->gmsor + PAGE_SIZE), 1))) {
  169. *reg |= SIGP_STAT_INVALID_PARAMETER;
  170. return 1; /* invalid parameter */
  171. }
  172. inti = kzalloc(sizeof(*inti), GFP_KERNEL);
  173. if (!inti)
  174. return 2; /* busy */
  175. spin_lock(&fi->lock);
  176. if (cpu_addr < KVM_MAX_VCPUS)
  177. li = fi->local_int[cpu_addr];
  178. if (li == NULL) {
  179. rc = 1; /* incorrect state */
  180. *reg &= SIGP_STAT_INCORRECT_STATE;
  181. kfree(inti);
  182. goto out_fi;
  183. }
  184. spin_lock_bh(&li->lock);
  185. /* cpu must be in stopped state */
  186. if (atomic_read(li->cpuflags) & CPUSTAT_RUNNING) {
  187. rc = 1; /* incorrect state */
  188. *reg &= SIGP_STAT_INCORRECT_STATE;
  189. kfree(inti);
  190. goto out_li;
  191. }
  192. inti->type = KVM_S390_SIGP_SET_PREFIX;
  193. inti->prefix.address = address;
  194. list_add_tail(&inti->list, &li->list);
  195. atomic_set(&li->active, 1);
  196. if (waitqueue_active(&li->wq))
  197. wake_up_interruptible(&li->wq);
  198. rc = 0; /* order accepted */
  199. VCPU_EVENT(vcpu, 4, "set prefix of cpu %02x to %x", cpu_addr, address);
  200. out_li:
  201. spin_unlock_bh(&li->lock);
  202. out_fi:
  203. spin_unlock(&fi->lock);
  204. return rc;
  205. }
  206. int kvm_s390_handle_sigp(struct kvm_vcpu *vcpu)
  207. {
  208. int r1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
  209. int r3 = vcpu->arch.sie_block->ipa & 0x000f;
  210. int base2 = vcpu->arch.sie_block->ipb >> 28;
  211. int disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16);
  212. u32 parameter;
  213. u16 cpu_addr = vcpu->arch.guest_gprs[r3];
  214. u8 order_code;
  215. int rc;
  216. /* sigp in userspace can exit */
  217. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  218. return kvm_s390_inject_program_int(vcpu,
  219. PGM_PRIVILEGED_OPERATION);
  220. order_code = disp2;
  221. if (base2)
  222. order_code += vcpu->arch.guest_gprs[base2];
  223. if (r1 % 2)
  224. parameter = vcpu->arch.guest_gprs[r1];
  225. else
  226. parameter = vcpu->arch.guest_gprs[r1 + 1];
  227. switch (order_code) {
  228. case SIGP_SENSE:
  229. vcpu->stat.instruction_sigp_sense++;
  230. rc = __sigp_sense(vcpu, cpu_addr,
  231. &vcpu->arch.guest_gprs[r1]);
  232. break;
  233. case SIGP_EMERGENCY:
  234. vcpu->stat.instruction_sigp_emergency++;
  235. rc = __sigp_emergency(vcpu, cpu_addr);
  236. break;
  237. case SIGP_STOP:
  238. vcpu->stat.instruction_sigp_stop++;
  239. rc = __sigp_stop(vcpu, cpu_addr, ACTION_STOP_ON_STOP);
  240. break;
  241. case SIGP_STOP_STORE_STATUS:
  242. vcpu->stat.instruction_sigp_stop++;
  243. rc = __sigp_stop(vcpu, cpu_addr, ACTION_STORE_ON_STOP);
  244. break;
  245. case SIGP_SET_ARCH:
  246. vcpu->stat.instruction_sigp_arch++;
  247. rc = __sigp_set_arch(vcpu, parameter);
  248. break;
  249. case SIGP_SET_PREFIX:
  250. vcpu->stat.instruction_sigp_prefix++;
  251. rc = __sigp_set_prefix(vcpu, cpu_addr, parameter,
  252. &vcpu->arch.guest_gprs[r1]);
  253. break;
  254. case SIGP_RESTART:
  255. vcpu->stat.instruction_sigp_restart++;
  256. /* user space must know about restart */
  257. default:
  258. return -EOPNOTSUPP;
  259. }
  260. if (rc < 0)
  261. return rc;
  262. vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
  263. vcpu->arch.sie_block->gpsw.mask |= (rc & 3ul) << 44;
  264. return 0;
  265. }