sigp.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * sigp.c - handlinge interprocessor communication
  3. *
  4. * Copyright IBM Corp. 2008
  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. */
  13. #include <linux/kvm.h>
  14. #include <linux/kvm_host.h>
  15. #include "gaccess.h"
  16. #include "kvm-s390.h"
  17. /* sigp order codes */
  18. #define SIGP_SENSE 0x01
  19. #define SIGP_EXTERNAL_CALL 0x02
  20. #define SIGP_EMERGENCY 0x03
  21. #define SIGP_START 0x04
  22. #define SIGP_STOP 0x05
  23. #define SIGP_RESTART 0x06
  24. #define SIGP_STOP_STORE_STATUS 0x09
  25. #define SIGP_INITIAL_CPU_RESET 0x0b
  26. #define SIGP_CPU_RESET 0x0c
  27. #define SIGP_SET_PREFIX 0x0d
  28. #define SIGP_STORE_STATUS_ADDR 0x0e
  29. #define SIGP_SET_ARCH 0x12
  30. /* cpu status bits */
  31. #define SIGP_STAT_EQUIPMENT_CHECK 0x80000000UL
  32. #define SIGP_STAT_INCORRECT_STATE 0x00000200UL
  33. #define SIGP_STAT_INVALID_PARAMETER 0x00000100UL
  34. #define SIGP_STAT_EXT_CALL_PENDING 0x00000080UL
  35. #define SIGP_STAT_STOPPED 0x00000040UL
  36. #define SIGP_STAT_OPERATOR_INTERV 0x00000020UL
  37. #define SIGP_STAT_CHECK_STOP 0x00000010UL
  38. #define SIGP_STAT_INOPERATIVE 0x00000004UL
  39. #define SIGP_STAT_INVALID_ORDER 0x00000002UL
  40. #define SIGP_STAT_RECEIVER_CHECK 0x00000001UL
  41. static int __sigp_sense(struct kvm_vcpu *vcpu, u16 cpu_addr, u64 *reg)
  42. {
  43. struct float_interrupt *fi = &vcpu->kvm->arch.float_int;
  44. int rc;
  45. if (cpu_addr >= KVM_MAX_VCPUS)
  46. return 3; /* not operational */
  47. spin_lock_bh(&fi->lock);
  48. if (fi->local_int[cpu_addr] == NULL)
  49. rc = 3; /* not operational */
  50. else if (atomic_read(fi->local_int[cpu_addr]->cpuflags)
  51. & CPUSTAT_RUNNING) {
  52. *reg &= 0xffffffff00000000UL;
  53. rc = 1; /* status stored */
  54. } else {
  55. *reg &= 0xffffffff00000000UL;
  56. *reg |= SIGP_STAT_STOPPED;
  57. rc = 1; /* status stored */
  58. }
  59. spin_unlock_bh(&fi->lock);
  60. VCPU_EVENT(vcpu, 4, "sensed status of cpu %x rc %x", cpu_addr, rc);
  61. return rc;
  62. }
  63. static int __sigp_emergency(struct kvm_vcpu *vcpu, u16 cpu_addr)
  64. {
  65. struct float_interrupt *fi = &vcpu->kvm->arch.float_int;
  66. struct local_interrupt *li;
  67. struct interrupt_info *inti;
  68. int rc;
  69. if (cpu_addr >= KVM_MAX_VCPUS)
  70. return 3; /* not operational */
  71. inti = kzalloc(sizeof(*inti), GFP_KERNEL);
  72. if (!inti)
  73. return -ENOMEM;
  74. inti->type = KVM_S390_INT_EMERGENCY;
  75. spin_lock_bh(&fi->lock);
  76. li = fi->local_int[cpu_addr];
  77. if (li == NULL) {
  78. rc = 3; /* not operational */
  79. kfree(inti);
  80. goto unlock;
  81. }
  82. spin_lock_bh(&li->lock);
  83. list_add_tail(&inti->list, &li->list);
  84. atomic_set(&li->active, 1);
  85. atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
  86. if (waitqueue_active(&li->wq))
  87. wake_up_interruptible(&li->wq);
  88. spin_unlock_bh(&li->lock);
  89. rc = 0; /* order accepted */
  90. unlock:
  91. spin_unlock_bh(&fi->lock);
  92. VCPU_EVENT(vcpu, 4, "sent sigp emerg to cpu %x", cpu_addr);
  93. return rc;
  94. }
  95. static int __sigp_stop(struct kvm_vcpu *vcpu, u16 cpu_addr, int store)
  96. {
  97. struct float_interrupt *fi = &vcpu->kvm->arch.float_int;
  98. struct local_interrupt *li;
  99. struct interrupt_info *inti;
  100. int rc;
  101. if (cpu_addr >= KVM_MAX_VCPUS)
  102. return 3; /* not operational */
  103. inti = kzalloc(sizeof(*inti), GFP_KERNEL);
  104. if (!inti)
  105. return -ENOMEM;
  106. inti->type = KVM_S390_SIGP_STOP;
  107. spin_lock_bh(&fi->lock);
  108. li = fi->local_int[cpu_addr];
  109. if (li == NULL) {
  110. rc = 3; /* not operational */
  111. kfree(inti);
  112. goto unlock;
  113. }
  114. spin_lock_bh(&li->lock);
  115. list_add_tail(&inti->list, &li->list);
  116. atomic_set(&li->active, 1);
  117. atomic_set_mask(CPUSTAT_STOP_INT, li->cpuflags);
  118. if (store)
  119. li->action_bits |= ACTION_STORE_ON_STOP;
  120. li->action_bits |= ACTION_STOP_ON_STOP;
  121. if (waitqueue_active(&li->wq))
  122. wake_up_interruptible(&li->wq);
  123. spin_unlock_bh(&li->lock);
  124. rc = 0; /* order accepted */
  125. unlock:
  126. spin_unlock_bh(&fi->lock);
  127. VCPU_EVENT(vcpu, 4, "sent sigp stop to cpu %x", cpu_addr);
  128. return rc;
  129. }
  130. static int __sigp_set_arch(struct kvm_vcpu *vcpu, u32 parameter)
  131. {
  132. int rc;
  133. switch (parameter & 0xff) {
  134. case 0:
  135. printk(KERN_WARNING "kvm: request to switch to ESA/390 mode"
  136. " not supported");
  137. rc = 3; /* not operational */
  138. break;
  139. case 1:
  140. case 2:
  141. rc = 0; /* order accepted */
  142. break;
  143. default:
  144. rc = -ENOTSUPP;
  145. }
  146. return rc;
  147. }
  148. static int __sigp_set_prefix(struct kvm_vcpu *vcpu, u16 cpu_addr, u32 address,
  149. u64 *reg)
  150. {
  151. struct float_interrupt *fi = &vcpu->kvm->arch.float_int;
  152. struct local_interrupt *li;
  153. struct interrupt_info *inti;
  154. int rc;
  155. u8 tmp;
  156. /* make sure that the new value is valid memory */
  157. address = address & 0x7fffe000u;
  158. if ((copy_from_guest(vcpu, &tmp,
  159. (u64) (address + vcpu->kvm->arch.guest_origin) , 1)) ||
  160. (copy_from_guest(vcpu, &tmp, (u64) (address +
  161. vcpu->kvm->arch.guest_origin + PAGE_SIZE), 1))) {
  162. *reg |= SIGP_STAT_INVALID_PARAMETER;
  163. return 1; /* invalid parameter */
  164. }
  165. inti = kzalloc(sizeof(*inti), GFP_KERNEL);
  166. if (!inti)
  167. return 2; /* busy */
  168. spin_lock_bh(&fi->lock);
  169. li = fi->local_int[cpu_addr];
  170. if ((cpu_addr >= KVM_MAX_VCPUS) || (li == NULL)) {
  171. rc = 1; /* incorrect state */
  172. *reg &= SIGP_STAT_INCORRECT_STATE;
  173. kfree(inti);
  174. goto out_fi;
  175. }
  176. spin_lock_bh(&li->lock);
  177. /* cpu must be in stopped state */
  178. if (atomic_read(li->cpuflags) & CPUSTAT_RUNNING) {
  179. rc = 1; /* incorrect state */
  180. *reg &= SIGP_STAT_INCORRECT_STATE;
  181. kfree(inti);
  182. goto out_li;
  183. }
  184. inti->type = KVM_S390_SIGP_SET_PREFIX;
  185. inti->prefix.address = address;
  186. list_add_tail(&inti->list, &li->list);
  187. atomic_set(&li->active, 1);
  188. if (waitqueue_active(&li->wq))
  189. wake_up_interruptible(&li->wq);
  190. rc = 0; /* order accepted */
  191. VCPU_EVENT(vcpu, 4, "set prefix of cpu %02x to %x", cpu_addr, address);
  192. out_li:
  193. spin_unlock_bh(&li->lock);
  194. out_fi:
  195. spin_unlock_bh(&fi->lock);
  196. return rc;
  197. }
  198. int kvm_s390_handle_sigp(struct kvm_vcpu *vcpu)
  199. {
  200. int r1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
  201. int r3 = vcpu->arch.sie_block->ipa & 0x000f;
  202. int base2 = vcpu->arch.sie_block->ipb >> 28;
  203. int disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16);
  204. u32 parameter;
  205. u16 cpu_addr = vcpu->arch.guest_gprs[r3];
  206. u8 order_code;
  207. int rc;
  208. order_code = disp2;
  209. if (base2)
  210. order_code += vcpu->arch.guest_gprs[base2];
  211. if (r1 % 2)
  212. parameter = vcpu->arch.guest_gprs[r1];
  213. else
  214. parameter = vcpu->arch.guest_gprs[r1 + 1];
  215. switch (order_code) {
  216. case SIGP_SENSE:
  217. vcpu->stat.instruction_sigp_sense++;
  218. rc = __sigp_sense(vcpu, cpu_addr,
  219. &vcpu->arch.guest_gprs[r1]);
  220. break;
  221. case SIGP_EMERGENCY:
  222. vcpu->stat.instruction_sigp_emergency++;
  223. rc = __sigp_emergency(vcpu, cpu_addr);
  224. break;
  225. case SIGP_STOP:
  226. vcpu->stat.instruction_sigp_stop++;
  227. rc = __sigp_stop(vcpu, cpu_addr, 0);
  228. break;
  229. case SIGP_STOP_STORE_STATUS:
  230. vcpu->stat.instruction_sigp_stop++;
  231. rc = __sigp_stop(vcpu, cpu_addr, 1);
  232. break;
  233. case SIGP_SET_ARCH:
  234. vcpu->stat.instruction_sigp_arch++;
  235. rc = __sigp_set_arch(vcpu, parameter);
  236. break;
  237. case SIGP_SET_PREFIX:
  238. vcpu->stat.instruction_sigp_prefix++;
  239. rc = __sigp_set_prefix(vcpu, cpu_addr, parameter,
  240. &vcpu->arch.guest_gprs[r1]);
  241. break;
  242. case SIGP_RESTART:
  243. vcpu->stat.instruction_sigp_restart++;
  244. /* user space must know about restart */
  245. default:
  246. return -ENOTSUPP;
  247. }
  248. if (rc < 0)
  249. return rc;
  250. vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
  251. vcpu->arch.sie_block->gpsw.mask |= (rc & 3ul) << 44;
  252. return 0;
  253. }