priv.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * priv.c - handling privileged instructions
  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/errno.h>
  15. #include <asm/current.h>
  16. #include <asm/debug.h>
  17. #include <asm/ebcdic.h>
  18. #include <asm/sysinfo.h>
  19. #include "gaccess.h"
  20. #include "kvm-s390.h"
  21. static int handle_set_prefix(struct kvm_vcpu *vcpu)
  22. {
  23. int base2 = vcpu->arch.sie_block->ipb >> 28;
  24. int disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16);
  25. u64 operand2;
  26. u32 address = 0;
  27. u8 tmp;
  28. vcpu->stat.instruction_spx++;
  29. operand2 = disp2;
  30. if (base2)
  31. operand2 += vcpu->arch.guest_gprs[base2];
  32. /* must be word boundary */
  33. if (operand2 & 3) {
  34. kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  35. goto out;
  36. }
  37. /* get the value */
  38. if (get_guest_u32(vcpu, operand2, &address)) {
  39. kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  40. goto out;
  41. }
  42. address = address & 0x7fffe000u;
  43. /* make sure that the new value is valid memory */
  44. if (copy_from_guest_absolute(vcpu, &tmp, address, 1) ||
  45. (copy_from_guest_absolute(vcpu, &tmp, address + PAGE_SIZE, 1))) {
  46. kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  47. goto out;
  48. }
  49. vcpu->arch.sie_block->prefix = address;
  50. vcpu->arch.sie_block->ihcpu = 0xffff;
  51. VCPU_EVENT(vcpu, 5, "setting prefix to %x", address);
  52. out:
  53. return 0;
  54. }
  55. static int handle_store_prefix(struct kvm_vcpu *vcpu)
  56. {
  57. int base2 = vcpu->arch.sie_block->ipb >> 28;
  58. int disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16);
  59. u64 operand2;
  60. u32 address;
  61. vcpu->stat.instruction_stpx++;
  62. operand2 = disp2;
  63. if (base2)
  64. operand2 += vcpu->arch.guest_gprs[base2];
  65. /* must be word boundary */
  66. if (operand2 & 3) {
  67. kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  68. goto out;
  69. }
  70. address = vcpu->arch.sie_block->prefix;
  71. address = address & 0x7fffe000u;
  72. /* get the value */
  73. if (put_guest_u32(vcpu, operand2, address)) {
  74. kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  75. goto out;
  76. }
  77. VCPU_EVENT(vcpu, 5, "storing prefix to %x", address);
  78. out:
  79. return 0;
  80. }
  81. static int handle_store_cpu_address(struct kvm_vcpu *vcpu)
  82. {
  83. int base2 = vcpu->arch.sie_block->ipb >> 28;
  84. int disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16);
  85. u64 useraddr;
  86. int rc;
  87. vcpu->stat.instruction_stap++;
  88. useraddr = disp2;
  89. if (base2)
  90. useraddr += vcpu->arch.guest_gprs[base2];
  91. if (useraddr & 1) {
  92. kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  93. goto out;
  94. }
  95. rc = put_guest_u16(vcpu, useraddr, vcpu->vcpu_id);
  96. if (rc == -EFAULT) {
  97. kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  98. goto out;
  99. }
  100. VCPU_EVENT(vcpu, 5, "storing cpu address to %lx", useraddr);
  101. out:
  102. return 0;
  103. }
  104. static int handle_skey(struct kvm_vcpu *vcpu)
  105. {
  106. vcpu->stat.instruction_storage_key++;
  107. vcpu->arch.sie_block->gpsw.addr -= 4;
  108. VCPU_EVENT(vcpu, 4, "%s", "retrying storage key operation");
  109. return 0;
  110. }
  111. static int handle_stsch(struct kvm_vcpu *vcpu)
  112. {
  113. vcpu->stat.instruction_stsch++;
  114. VCPU_EVENT(vcpu, 4, "%s", "store subchannel - CC3");
  115. /* condition code 3 */
  116. vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
  117. vcpu->arch.sie_block->gpsw.mask |= (3 & 3ul) << 44;
  118. return 0;
  119. }
  120. static int handle_chsc(struct kvm_vcpu *vcpu)
  121. {
  122. vcpu->stat.instruction_chsc++;
  123. VCPU_EVENT(vcpu, 4, "%s", "channel subsystem call - CC3");
  124. /* condition code 3 */
  125. vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
  126. vcpu->arch.sie_block->gpsw.mask |= (3 & 3ul) << 44;
  127. return 0;
  128. }
  129. static int handle_stfl(struct kvm_vcpu *vcpu)
  130. {
  131. unsigned int facility_list = stfl();
  132. int rc;
  133. vcpu->stat.instruction_stfl++;
  134. facility_list &= ~(1UL<<24); /* no stfle */
  135. rc = copy_to_guest(vcpu, offsetof(struct _lowcore, stfl_fac_list),
  136. &facility_list, sizeof(facility_list));
  137. if (rc == -EFAULT)
  138. kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  139. else
  140. VCPU_EVENT(vcpu, 5, "store facility list value %x",
  141. facility_list);
  142. return 0;
  143. }
  144. static int handle_stidp(struct kvm_vcpu *vcpu)
  145. {
  146. int base2 = vcpu->arch.sie_block->ipb >> 28;
  147. int disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16);
  148. u64 operand2;
  149. int rc;
  150. vcpu->stat.instruction_stidp++;
  151. operand2 = disp2;
  152. if (base2)
  153. operand2 += vcpu->arch.guest_gprs[base2];
  154. if (operand2 & 7) {
  155. kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  156. goto out;
  157. }
  158. rc = put_guest_u64(vcpu, operand2, vcpu->arch.stidp_data);
  159. if (rc == -EFAULT) {
  160. kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  161. goto out;
  162. }
  163. VCPU_EVENT(vcpu, 5, "%s", "store cpu id");
  164. out:
  165. return 0;
  166. }
  167. static void handle_stsi_3_2_2(struct kvm_vcpu *vcpu, struct sysinfo_3_2_2 *mem)
  168. {
  169. struct float_interrupt *fi = &vcpu->kvm->arch.float_int;
  170. int cpus = 0;
  171. int n;
  172. spin_lock_bh(&fi->lock);
  173. for (n = 0; n < KVM_MAX_VCPUS; n++)
  174. if (fi->local_int[n])
  175. cpus++;
  176. spin_unlock_bh(&fi->lock);
  177. /* deal with other level 3 hypervisors */
  178. if (stsi(mem, 3, 2, 2) == -ENOSYS)
  179. mem->count = 0;
  180. if (mem->count < 8)
  181. mem->count++;
  182. for (n = mem->count - 1; n > 0 ; n--)
  183. memcpy(&mem->vm[n], &mem->vm[n - 1], sizeof(mem->vm[0]));
  184. mem->vm[0].cpus_total = cpus;
  185. mem->vm[0].cpus_configured = cpus;
  186. mem->vm[0].cpus_standby = 0;
  187. mem->vm[0].cpus_reserved = 0;
  188. mem->vm[0].caf = 1000;
  189. memcpy(mem->vm[0].name, "KVMguest", 8);
  190. ASCEBC(mem->vm[0].name, 8);
  191. memcpy(mem->vm[0].cpi, "KVM/Linux ", 16);
  192. ASCEBC(mem->vm[0].cpi, 16);
  193. }
  194. static int handle_stsi(struct kvm_vcpu *vcpu)
  195. {
  196. int fc = (vcpu->arch.guest_gprs[0] & 0xf0000000) >> 28;
  197. int sel1 = vcpu->arch.guest_gprs[0] & 0xff;
  198. int sel2 = vcpu->arch.guest_gprs[1] & 0xffff;
  199. int base2 = vcpu->arch.sie_block->ipb >> 28;
  200. int disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16);
  201. u64 operand2;
  202. unsigned long mem;
  203. vcpu->stat.instruction_stsi++;
  204. VCPU_EVENT(vcpu, 4, "stsi: fc: %x sel1: %x sel2: %x", fc, sel1, sel2);
  205. operand2 = disp2;
  206. if (base2)
  207. operand2 += vcpu->arch.guest_gprs[base2];
  208. if (operand2 & 0xfff && fc > 0)
  209. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  210. switch (fc) {
  211. case 0:
  212. vcpu->arch.guest_gprs[0] = 3 << 28;
  213. vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
  214. return 0;
  215. case 1: /* same handling for 1 and 2 */
  216. case 2:
  217. mem = get_zeroed_page(GFP_KERNEL);
  218. if (!mem)
  219. goto out_fail;
  220. if (stsi((void *) mem, fc, sel1, sel2) == -ENOSYS)
  221. goto out_mem;
  222. break;
  223. case 3:
  224. if (sel1 != 2 || sel2 != 2)
  225. goto out_fail;
  226. mem = get_zeroed_page(GFP_KERNEL);
  227. if (!mem)
  228. goto out_fail;
  229. handle_stsi_3_2_2(vcpu, (void *) mem);
  230. break;
  231. default:
  232. goto out_fail;
  233. }
  234. if (copy_to_guest_absolute(vcpu, operand2, (void *) mem, PAGE_SIZE)) {
  235. kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  236. goto out_mem;
  237. }
  238. free_page(mem);
  239. vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
  240. vcpu->arch.guest_gprs[0] = 0;
  241. return 0;
  242. out_mem:
  243. free_page(mem);
  244. out_fail:
  245. /* condition code 3 */
  246. vcpu->arch.sie_block->gpsw.mask |= 3ul << 44;
  247. return 0;
  248. }
  249. static intercept_handler_t priv_handlers[256] = {
  250. [0x02] = handle_stidp,
  251. [0x10] = handle_set_prefix,
  252. [0x11] = handle_store_prefix,
  253. [0x12] = handle_store_cpu_address,
  254. [0x29] = handle_skey,
  255. [0x2a] = handle_skey,
  256. [0x2b] = handle_skey,
  257. [0x34] = handle_stsch,
  258. [0x5f] = handle_chsc,
  259. [0x7d] = handle_stsi,
  260. [0xb1] = handle_stfl,
  261. };
  262. int kvm_s390_handle_priv(struct kvm_vcpu *vcpu)
  263. {
  264. intercept_handler_t handler;
  265. handler = priv_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
  266. if (handler)
  267. return handler(vcpu);
  268. return -ENOTSUPP;
  269. }