priv.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 unsigned int kvm_stfl(void)
  130. {
  131. asm volatile(
  132. " .insn s,0xb2b10000,0(0)\n" /* stfl */
  133. "0:\n"
  134. EX_TABLE(0b, 0b));
  135. return S390_lowcore.stfl_fac_list;
  136. }
  137. static int handle_stfl(struct kvm_vcpu *vcpu)
  138. {
  139. unsigned int facility_list = kvm_stfl();
  140. int rc;
  141. vcpu->stat.instruction_stfl++;
  142. facility_list &= ~(1UL<<24); /* no stfle */
  143. rc = copy_to_guest(vcpu, offsetof(struct _lowcore, stfl_fac_list),
  144. &facility_list, sizeof(facility_list));
  145. if (rc == -EFAULT)
  146. kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  147. else
  148. VCPU_EVENT(vcpu, 5, "store facility list value %x",
  149. facility_list);
  150. return 0;
  151. }
  152. static int handle_stidp(struct kvm_vcpu *vcpu)
  153. {
  154. int base2 = vcpu->arch.sie_block->ipb >> 28;
  155. int disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16);
  156. u64 operand2;
  157. int rc;
  158. vcpu->stat.instruction_stidp++;
  159. operand2 = disp2;
  160. if (base2)
  161. operand2 += vcpu->arch.guest_gprs[base2];
  162. if (operand2 & 7) {
  163. kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  164. goto out;
  165. }
  166. rc = put_guest_u64(vcpu, operand2, vcpu->arch.stidp_data);
  167. if (rc == -EFAULT) {
  168. kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  169. goto out;
  170. }
  171. VCPU_EVENT(vcpu, 5, "%s", "store cpu id");
  172. out:
  173. return 0;
  174. }
  175. static void handle_stsi_3_2_2(struct kvm_vcpu *vcpu, struct sysinfo_3_2_2 *mem)
  176. {
  177. struct float_interrupt *fi = &vcpu->kvm->arch.float_int;
  178. int cpus = 0;
  179. int n;
  180. spin_lock_bh(&fi->lock);
  181. for (n = 0; n < KVM_MAX_VCPUS; n++)
  182. if (fi->local_int[n])
  183. cpus++;
  184. spin_unlock_bh(&fi->lock);
  185. /* deal with other level 3 hypervisors */
  186. if (stsi(mem, 3, 2, 2) == -ENOSYS)
  187. mem->count = 0;
  188. if (mem->count < 8)
  189. mem->count++;
  190. for (n = mem->count - 1; n > 0 ; n--)
  191. memcpy(&mem->vm[n], &mem->vm[n - 1], sizeof(mem->vm[0]));
  192. mem->vm[0].cpus_total = cpus;
  193. mem->vm[0].cpus_configured = cpus;
  194. mem->vm[0].cpus_standby = 0;
  195. mem->vm[0].cpus_reserved = 0;
  196. mem->vm[0].caf = 1000;
  197. memcpy(mem->vm[0].name, "KVMguest", 8);
  198. ASCEBC(mem->vm[0].name, 8);
  199. memcpy(mem->vm[0].cpi, "KVM/Linux ", 16);
  200. ASCEBC(mem->vm[0].cpi, 16);
  201. }
  202. static int handle_stsi(struct kvm_vcpu *vcpu)
  203. {
  204. int fc = (vcpu->arch.guest_gprs[0] & 0xf0000000) >> 28;
  205. int sel1 = vcpu->arch.guest_gprs[0] & 0xff;
  206. int sel2 = vcpu->arch.guest_gprs[1] & 0xffff;
  207. int base2 = vcpu->arch.sie_block->ipb >> 28;
  208. int disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16);
  209. u64 operand2;
  210. unsigned long mem;
  211. vcpu->stat.instruction_stsi++;
  212. VCPU_EVENT(vcpu, 4, "stsi: fc: %x sel1: %x sel2: %x", fc, sel1, sel2);
  213. operand2 = disp2;
  214. if (base2)
  215. operand2 += vcpu->arch.guest_gprs[base2];
  216. if (operand2 & 0xfff && fc > 0)
  217. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  218. switch (fc) {
  219. case 0:
  220. vcpu->arch.guest_gprs[0] = 3 << 28;
  221. vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
  222. return 0;
  223. case 1: /* same handling for 1 and 2 */
  224. case 2:
  225. mem = get_zeroed_page(GFP_KERNEL);
  226. if (!mem)
  227. goto out_fail;
  228. if (stsi((void *) mem, fc, sel1, sel2) == -ENOSYS)
  229. goto out_mem;
  230. break;
  231. case 3:
  232. if (sel1 != 2 || sel2 != 2)
  233. goto out_fail;
  234. mem = get_zeroed_page(GFP_KERNEL);
  235. if (!mem)
  236. goto out_fail;
  237. handle_stsi_3_2_2(vcpu, (void *) mem);
  238. break;
  239. default:
  240. goto out_fail;
  241. }
  242. if (copy_to_guest_absolute(vcpu, operand2, (void *) mem, PAGE_SIZE)) {
  243. kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  244. goto out_mem;
  245. }
  246. free_page(mem);
  247. vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
  248. vcpu->arch.guest_gprs[0] = 0;
  249. return 0;
  250. out_mem:
  251. free_page(mem);
  252. out_fail:
  253. /* condition code 3 */
  254. vcpu->arch.sie_block->gpsw.mask |= 3ul << 44;
  255. return 0;
  256. }
  257. static intercept_handler_t priv_handlers[256] = {
  258. [0x02] = handle_stidp,
  259. [0x10] = handle_set_prefix,
  260. [0x11] = handle_store_prefix,
  261. [0x12] = handle_store_cpu_address,
  262. [0x29] = handle_skey,
  263. [0x2a] = handle_skey,
  264. [0x2b] = handle_skey,
  265. [0x34] = handle_stsch,
  266. [0x5f] = handle_chsc,
  267. [0x7d] = handle_stsi,
  268. [0xb1] = handle_stfl,
  269. };
  270. int kvm_s390_handle_priv(struct kvm_vcpu *vcpu)
  271. {
  272. intercept_handler_t handler;
  273. handler = priv_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
  274. if (handler)
  275. return handler(vcpu);
  276. return -ENOTSUPP;
  277. }