priv.c 7.5 KB

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