sigp.c 7.8 KB

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