priv.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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/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. static int handle_set_prefix(struct kvm_vcpu *vcpu)
  23. {
  24. int base2 = vcpu->arch.sie_block->ipb >> 28;
  25. int disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16);
  26. u64 operand2;
  27. u32 address = 0;
  28. u8 tmp;
  29. vcpu->stat.instruction_spx++;
  30. operand2 = disp2;
  31. if (base2)
  32. operand2 += vcpu->run->s.regs.gprs[base2];
  33. /* must be word boundary */
  34. if (operand2 & 3) {
  35. kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  36. goto out;
  37. }
  38. /* get the value */
  39. if (get_guest_u32(vcpu, operand2, &address)) {
  40. kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  41. goto out;
  42. }
  43. address = address & 0x7fffe000u;
  44. /* make sure that the new value is valid memory */
  45. if (copy_from_guest_absolute(vcpu, &tmp, address, 1) ||
  46. (copy_from_guest_absolute(vcpu, &tmp, address + PAGE_SIZE, 1))) {
  47. kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  48. goto out;
  49. }
  50. kvm_s390_set_prefix(vcpu, address);
  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->run->s.regs.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->run->s.regs.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 %llx", 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;
  132. int rc;
  133. vcpu->stat.instruction_stfl++;
  134. /* only pass the facility bits, which we can handle */
  135. facility_list = S390_lowcore.stfl_fac_list & 0xff00fff3;
  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->run->s.regs.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(&fi->lock);
  174. for (n = 0; n < KVM_MAX_VCPUS; n++)
  175. if (fi->local_int[n])
  176. cpus++;
  177. spin_unlock(&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. memset(&mem->vm[0], 0, sizeof(mem->vm[0]));
  186. mem->vm[0].cpus_total = cpus;
  187. mem->vm[0].cpus_configured = cpus;
  188. mem->vm[0].cpus_standby = 0;
  189. mem->vm[0].cpus_reserved = 0;
  190. mem->vm[0].caf = 1000;
  191. memcpy(mem->vm[0].name, "KVMguest", 8);
  192. ASCEBC(mem->vm[0].name, 8);
  193. memcpy(mem->vm[0].cpi, "KVM/Linux ", 16);
  194. ASCEBC(mem->vm[0].cpi, 16);
  195. }
  196. static int handle_stsi(struct kvm_vcpu *vcpu)
  197. {
  198. int fc = (vcpu->run->s.regs.gprs[0] & 0xf0000000) >> 28;
  199. int sel1 = vcpu->run->s.regs.gprs[0] & 0xff;
  200. int sel2 = vcpu->run->s.regs.gprs[1] & 0xffff;
  201. int base2 = vcpu->arch.sie_block->ipb >> 28;
  202. int disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16);
  203. u64 operand2;
  204. unsigned long mem;
  205. vcpu->stat.instruction_stsi++;
  206. VCPU_EVENT(vcpu, 4, "stsi: fc: %x sel1: %x sel2: %x", fc, sel1, sel2);
  207. operand2 = disp2;
  208. if (base2)
  209. operand2 += vcpu->run->s.regs.gprs[base2];
  210. if (operand2 & 0xfff && fc > 0)
  211. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  212. switch (fc) {
  213. case 0:
  214. vcpu->run->s.regs.gprs[0] = 3 << 28;
  215. vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
  216. return 0;
  217. case 1: /* same handling for 1 and 2 */
  218. case 2:
  219. mem = get_zeroed_page(GFP_KERNEL);
  220. if (!mem)
  221. goto out_fail;
  222. if (stsi((void *) mem, fc, sel1, sel2) == -ENOSYS)
  223. goto out_mem;
  224. break;
  225. case 3:
  226. if (sel1 != 2 || sel2 != 2)
  227. goto out_fail;
  228. mem = get_zeroed_page(GFP_KERNEL);
  229. if (!mem)
  230. goto out_fail;
  231. handle_stsi_3_2_2(vcpu, (void *) mem);
  232. break;
  233. default:
  234. goto out_fail;
  235. }
  236. if (copy_to_guest_absolute(vcpu, operand2, (void *) mem, PAGE_SIZE)) {
  237. kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  238. goto out_mem;
  239. }
  240. free_page(mem);
  241. vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
  242. vcpu->run->s.regs.gprs[0] = 0;
  243. return 0;
  244. out_mem:
  245. free_page(mem);
  246. out_fail:
  247. /* condition code 3 */
  248. vcpu->arch.sie_block->gpsw.mask |= 3ul << 44;
  249. return 0;
  250. }
  251. static intercept_handler_t priv_handlers[256] = {
  252. [0x02] = handle_stidp,
  253. [0x10] = handle_set_prefix,
  254. [0x11] = handle_store_prefix,
  255. [0x12] = handle_store_cpu_address,
  256. [0x29] = handle_skey,
  257. [0x2a] = handle_skey,
  258. [0x2b] = handle_skey,
  259. [0x34] = handle_stsch,
  260. [0x5f] = handle_chsc,
  261. [0x7d] = handle_stsi,
  262. [0xb1] = handle_stfl,
  263. };
  264. int kvm_s390_handle_b2(struct kvm_vcpu *vcpu)
  265. {
  266. intercept_handler_t handler;
  267. /*
  268. * a lot of B2 instructions are priviledged. We first check for
  269. * the privileged ones, that we can handle in the kernel. If the
  270. * kernel can handle this instruction, we check for the problem
  271. * state bit and (a) handle the instruction or (b) send a code 2
  272. * program check.
  273. * Anything else goes to userspace.*/
  274. handler = priv_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
  275. if (handler) {
  276. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  277. return kvm_s390_inject_program_int(vcpu,
  278. PGM_PRIVILEGED_OPERATION);
  279. else
  280. return handler(vcpu);
  281. }
  282. return -EOPNOTSUPP;
  283. }
  284. static int handle_tprot(struct kvm_vcpu *vcpu)
  285. {
  286. int base1 = (vcpu->arch.sie_block->ipb & 0xf0000000) >> 28;
  287. int disp1 = (vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16;
  288. int base2 = (vcpu->arch.sie_block->ipb & 0xf000) >> 12;
  289. int disp2 = vcpu->arch.sie_block->ipb & 0x0fff;
  290. u64 address1 = disp1 + base1 ? vcpu->run->s.regs.gprs[base1] : 0;
  291. u64 address2 = disp2 + base2 ? vcpu->run->s.regs.gprs[base2] : 0;
  292. struct vm_area_struct *vma;
  293. unsigned long user_address;
  294. vcpu->stat.instruction_tprot++;
  295. /* we only handle the Linux memory detection case:
  296. * access key == 0
  297. * guest DAT == off
  298. * everything else goes to userspace. */
  299. if (address2 & 0xf0)
  300. return -EOPNOTSUPP;
  301. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
  302. return -EOPNOTSUPP;
  303. /* we must resolve the address without holding the mmap semaphore.
  304. * This is ok since the userspace hypervisor is not supposed to change
  305. * the mapping while the guest queries the memory. Otherwise the guest
  306. * might crash or get wrong info anyway. */
  307. user_address = (unsigned long) __guestaddr_to_user(vcpu, address1);
  308. down_read(&current->mm->mmap_sem);
  309. vma = find_vma(current->mm, user_address);
  310. if (!vma) {
  311. up_read(&current->mm->mmap_sem);
  312. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  313. }
  314. vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
  315. if (!(vma->vm_flags & VM_WRITE) && (vma->vm_flags & VM_READ))
  316. vcpu->arch.sie_block->gpsw.mask |= (1ul << 44);
  317. if (!(vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_READ))
  318. vcpu->arch.sie_block->gpsw.mask |= (2ul << 44);
  319. up_read(&current->mm->mmap_sem);
  320. return 0;
  321. }
  322. int kvm_s390_handle_e5(struct kvm_vcpu *vcpu)
  323. {
  324. /* For e5xx... instructions we only handle TPROT */
  325. if ((vcpu->arch.sie_block->ipa & 0x00ff) == 0x01)
  326. return handle_tprot(vcpu);
  327. return -EOPNOTSUPP;
  328. }