priv.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. * 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/gfp.h>
  15. #include <linux/errno.h>
  16. #include <asm/current.h>
  17. #include <asm/debug.h>
  18. #include <asm/ebcdic.h>
  19. #include <asm/sysinfo.h>
  20. #include "gaccess.h"
  21. #include "kvm-s390.h"
  22. #include "trace.h"
  23. static int handle_set_prefix(struct kvm_vcpu *vcpu)
  24. {
  25. u64 operand2;
  26. u32 address = 0;
  27. u8 tmp;
  28. vcpu->stat.instruction_spx++;
  29. operand2 = kvm_s390_get_base_disp_s(vcpu);
  30. /* must be word boundary */
  31. if (operand2 & 3) {
  32. kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  33. goto out;
  34. }
  35. /* get the value */
  36. if (get_guest_u32(vcpu, operand2, &address)) {
  37. kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  38. goto out;
  39. }
  40. address = address & 0x7fffe000u;
  41. /* make sure that the new value is valid memory */
  42. if (copy_from_guest_absolute(vcpu, &tmp, address, 1) ||
  43. (copy_from_guest_absolute(vcpu, &tmp, address + PAGE_SIZE, 1))) {
  44. kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  45. goto out;
  46. }
  47. kvm_s390_set_prefix(vcpu, address);
  48. VCPU_EVENT(vcpu, 5, "setting prefix to %x", address);
  49. trace_kvm_s390_handle_prefix(vcpu, 1, address);
  50. out:
  51. return 0;
  52. }
  53. static int handle_store_prefix(struct kvm_vcpu *vcpu)
  54. {
  55. u64 operand2;
  56. u32 address;
  57. vcpu->stat.instruction_stpx++;
  58. operand2 = kvm_s390_get_base_disp_s(vcpu);
  59. /* must be word boundary */
  60. if (operand2 & 3) {
  61. kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  62. goto out;
  63. }
  64. address = vcpu->arch.sie_block->prefix;
  65. address = address & 0x7fffe000u;
  66. /* get the value */
  67. if (put_guest_u32(vcpu, operand2, address)) {
  68. kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  69. goto out;
  70. }
  71. VCPU_EVENT(vcpu, 5, "storing prefix to %x", address);
  72. trace_kvm_s390_handle_prefix(vcpu, 0, address);
  73. out:
  74. return 0;
  75. }
  76. static int handle_store_cpu_address(struct kvm_vcpu *vcpu)
  77. {
  78. u64 useraddr;
  79. int rc;
  80. vcpu->stat.instruction_stap++;
  81. useraddr = kvm_s390_get_base_disp_s(vcpu);
  82. if (useraddr & 1) {
  83. kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  84. goto out;
  85. }
  86. rc = put_guest_u16(vcpu, useraddr, vcpu->vcpu_id);
  87. if (rc == -EFAULT) {
  88. kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  89. goto out;
  90. }
  91. VCPU_EVENT(vcpu, 5, "storing cpu address to %llx", useraddr);
  92. trace_kvm_s390_handle_stap(vcpu, useraddr);
  93. out:
  94. return 0;
  95. }
  96. static int handle_skey(struct kvm_vcpu *vcpu)
  97. {
  98. vcpu->stat.instruction_storage_key++;
  99. vcpu->arch.sie_block->gpsw.addr -= 4;
  100. VCPU_EVENT(vcpu, 4, "%s", "retrying storage key operation");
  101. return 0;
  102. }
  103. static int handle_stsch(struct kvm_vcpu *vcpu)
  104. {
  105. vcpu->stat.instruction_stsch++;
  106. VCPU_EVENT(vcpu, 4, "%s", "store subchannel - CC3");
  107. /* condition code 3 */
  108. vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
  109. vcpu->arch.sie_block->gpsw.mask |= (3 & 3ul) << 44;
  110. return 0;
  111. }
  112. static int handle_chsc(struct kvm_vcpu *vcpu)
  113. {
  114. vcpu->stat.instruction_chsc++;
  115. VCPU_EVENT(vcpu, 4, "%s", "channel subsystem call - CC3");
  116. /* condition code 3 */
  117. vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
  118. vcpu->arch.sie_block->gpsw.mask |= (3 & 3ul) << 44;
  119. return 0;
  120. }
  121. static int handle_stfl(struct kvm_vcpu *vcpu)
  122. {
  123. unsigned int facility_list;
  124. int rc;
  125. vcpu->stat.instruction_stfl++;
  126. /* only pass the facility bits, which we can handle */
  127. facility_list = S390_lowcore.stfl_fac_list & 0xff00fff3;
  128. rc = copy_to_guest(vcpu, offsetof(struct _lowcore, stfl_fac_list),
  129. &facility_list, sizeof(facility_list));
  130. if (rc == -EFAULT)
  131. kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  132. else {
  133. VCPU_EVENT(vcpu, 5, "store facility list value %x",
  134. facility_list);
  135. trace_kvm_s390_handle_stfl(vcpu, facility_list);
  136. }
  137. return 0;
  138. }
  139. static int handle_stidp(struct kvm_vcpu *vcpu)
  140. {
  141. u64 operand2;
  142. int rc;
  143. vcpu->stat.instruction_stidp++;
  144. operand2 = kvm_s390_get_base_disp_s(vcpu);
  145. if (operand2 & 7) {
  146. kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  147. goto out;
  148. }
  149. rc = put_guest_u64(vcpu, operand2, vcpu->arch.stidp_data);
  150. if (rc == -EFAULT) {
  151. kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  152. goto out;
  153. }
  154. VCPU_EVENT(vcpu, 5, "%s", "store cpu id");
  155. out:
  156. return 0;
  157. }
  158. static void handle_stsi_3_2_2(struct kvm_vcpu *vcpu, struct sysinfo_3_2_2 *mem)
  159. {
  160. struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
  161. int cpus = 0;
  162. int n;
  163. spin_lock(&fi->lock);
  164. for (n = 0; n < KVM_MAX_VCPUS; n++)
  165. if (fi->local_int[n])
  166. cpus++;
  167. spin_unlock(&fi->lock);
  168. /* deal with other level 3 hypervisors */
  169. if (stsi(mem, 3, 2, 2))
  170. mem->count = 0;
  171. if (mem->count < 8)
  172. mem->count++;
  173. for (n = mem->count - 1; n > 0 ; n--)
  174. memcpy(&mem->vm[n], &mem->vm[n - 1], sizeof(mem->vm[0]));
  175. mem->vm[0].cpus_total = cpus;
  176. mem->vm[0].cpus_configured = cpus;
  177. mem->vm[0].cpus_standby = 0;
  178. mem->vm[0].cpus_reserved = 0;
  179. mem->vm[0].caf = 1000;
  180. memcpy(mem->vm[0].name, "KVMguest", 8);
  181. ASCEBC(mem->vm[0].name, 8);
  182. memcpy(mem->vm[0].cpi, "KVM/Linux ", 16);
  183. ASCEBC(mem->vm[0].cpi, 16);
  184. }
  185. static int handle_stsi(struct kvm_vcpu *vcpu)
  186. {
  187. int fc = (vcpu->run->s.regs.gprs[0] & 0xf0000000) >> 28;
  188. int sel1 = vcpu->run->s.regs.gprs[0] & 0xff;
  189. int sel2 = vcpu->run->s.regs.gprs[1] & 0xffff;
  190. u64 operand2;
  191. unsigned long mem;
  192. vcpu->stat.instruction_stsi++;
  193. VCPU_EVENT(vcpu, 4, "stsi: fc: %x sel1: %x sel2: %x", fc, sel1, sel2);
  194. operand2 = kvm_s390_get_base_disp_s(vcpu);
  195. if (operand2 & 0xfff && fc > 0)
  196. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  197. switch (fc) {
  198. case 0:
  199. vcpu->run->s.regs.gprs[0] = 3 << 28;
  200. vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
  201. return 0;
  202. case 1: /* same handling for 1 and 2 */
  203. case 2:
  204. mem = get_zeroed_page(GFP_KERNEL);
  205. if (!mem)
  206. goto out_fail;
  207. if (stsi((void *) mem, fc, sel1, sel2))
  208. goto out_mem;
  209. break;
  210. case 3:
  211. if (sel1 != 2 || sel2 != 2)
  212. goto out_fail;
  213. mem = get_zeroed_page(GFP_KERNEL);
  214. if (!mem)
  215. goto out_fail;
  216. handle_stsi_3_2_2(vcpu, (void *) mem);
  217. break;
  218. default:
  219. goto out_fail;
  220. }
  221. if (copy_to_guest_absolute(vcpu, operand2, (void *) mem, PAGE_SIZE)) {
  222. kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  223. goto out_mem;
  224. }
  225. trace_kvm_s390_handle_stsi(vcpu, fc, sel1, sel2, operand2);
  226. free_page(mem);
  227. vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
  228. vcpu->run->s.regs.gprs[0] = 0;
  229. return 0;
  230. out_mem:
  231. free_page(mem);
  232. out_fail:
  233. /* condition code 3 */
  234. vcpu->arch.sie_block->gpsw.mask |= 3ul << 44;
  235. return 0;
  236. }
  237. static const intercept_handler_t priv_handlers[256] = {
  238. [0x02] = handle_stidp,
  239. [0x10] = handle_set_prefix,
  240. [0x11] = handle_store_prefix,
  241. [0x12] = handle_store_cpu_address,
  242. [0x29] = handle_skey,
  243. [0x2a] = handle_skey,
  244. [0x2b] = handle_skey,
  245. [0x34] = handle_stsch,
  246. [0x5f] = handle_chsc,
  247. [0x7d] = handle_stsi,
  248. [0xb1] = handle_stfl,
  249. };
  250. int kvm_s390_handle_b2(struct kvm_vcpu *vcpu)
  251. {
  252. intercept_handler_t handler;
  253. /*
  254. * a lot of B2 instructions are priviledged. We first check for
  255. * the privileged ones, that we can handle in the kernel. If the
  256. * kernel can handle this instruction, we check for the problem
  257. * state bit and (a) handle the instruction or (b) send a code 2
  258. * program check.
  259. * Anything else goes to userspace.*/
  260. handler = priv_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
  261. if (handler) {
  262. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  263. return kvm_s390_inject_program_int(vcpu,
  264. PGM_PRIVILEGED_OPERATION);
  265. else
  266. return handler(vcpu);
  267. }
  268. return -EOPNOTSUPP;
  269. }
  270. static int handle_tprot(struct kvm_vcpu *vcpu)
  271. {
  272. u64 address1, address2;
  273. struct vm_area_struct *vma;
  274. unsigned long user_address;
  275. vcpu->stat.instruction_tprot++;
  276. kvm_s390_get_base_disp_sse(vcpu, &address1, &address2);
  277. /* we only handle the Linux memory detection case:
  278. * access key == 0
  279. * guest DAT == off
  280. * everything else goes to userspace. */
  281. if (address2 & 0xf0)
  282. return -EOPNOTSUPP;
  283. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
  284. return -EOPNOTSUPP;
  285. /* we must resolve the address without holding the mmap semaphore.
  286. * This is ok since the userspace hypervisor is not supposed to change
  287. * the mapping while the guest queries the memory. Otherwise the guest
  288. * might crash or get wrong info anyway. */
  289. user_address = (unsigned long) __guestaddr_to_user(vcpu, address1);
  290. down_read(&current->mm->mmap_sem);
  291. vma = find_vma(current->mm, user_address);
  292. if (!vma) {
  293. up_read(&current->mm->mmap_sem);
  294. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  295. }
  296. vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
  297. if (!(vma->vm_flags & VM_WRITE) && (vma->vm_flags & VM_READ))
  298. vcpu->arch.sie_block->gpsw.mask |= (1ul << 44);
  299. if (!(vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_READ))
  300. vcpu->arch.sie_block->gpsw.mask |= (2ul << 44);
  301. up_read(&current->mm->mmap_sem);
  302. return 0;
  303. }
  304. int kvm_s390_handle_e5(struct kvm_vcpu *vcpu)
  305. {
  306. /* For e5xx... instructions we only handle TPROT */
  307. if ((vcpu->arch.sie_block->ipa & 0x00ff) == 0x01)
  308. return handle_tprot(vcpu);
  309. return -EOPNOTSUPP;
  310. }
  311. static int handle_sckpf(struct kvm_vcpu *vcpu)
  312. {
  313. u32 value;
  314. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  315. return kvm_s390_inject_program_int(vcpu,
  316. PGM_PRIVILEGED_OPERATION);
  317. if (vcpu->run->s.regs.gprs[0] & 0x00000000ffff0000)
  318. return kvm_s390_inject_program_int(vcpu,
  319. PGM_SPECIFICATION);
  320. value = vcpu->run->s.regs.gprs[0] & 0x000000000000ffff;
  321. vcpu->arch.sie_block->todpr = value;
  322. return 0;
  323. }
  324. static const intercept_handler_t x01_handlers[256] = {
  325. [0x07] = handle_sckpf,
  326. };
  327. int kvm_s390_handle_01(struct kvm_vcpu *vcpu)
  328. {
  329. intercept_handler_t handler;
  330. handler = x01_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
  331. if (handler)
  332. return handler(vcpu);
  333. return -EOPNOTSUPP;
  334. }