priv.c 11 KB

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