PageRenderTime 63ms CodeModel.GetById 34ms RepoModel.GetById 1ms app.codeStats 0ms

/arch/s390/kvm/priv.c

https://github.com/Mengqi/linux-2.6
C | 377 lines | 289 code | 54 blank | 34 comment | 47 complexity | 120beaf25c48dbdad537aec41b966f05 MD5 | raw file
  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->arch.guest_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. vcpu->arch.sie_block->prefix = address;
  51. vcpu->arch.sie_block->ihcpu = 0xffff;
  52. VCPU_EVENT(vcpu, 5, "setting prefix to %x", address);
  53. out:
  54. return 0;
  55. }
  56. static int handle_store_prefix(struct kvm_vcpu *vcpu)
  57. {
  58. int base2 = vcpu->arch.sie_block->ipb >> 28;
  59. int disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16);
  60. u64 operand2;
  61. u32 address;
  62. vcpu->stat.instruction_stpx++;
  63. operand2 = disp2;
  64. if (base2)
  65. operand2 += vcpu->arch.guest_gprs[base2];
  66. /* must be word boundary */
  67. if (operand2 & 3) {
  68. kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  69. goto out;
  70. }
  71. address = vcpu->arch.sie_block->prefix;
  72. address = address & 0x7fffe000u;
  73. /* get the value */
  74. if (put_guest_u32(vcpu, operand2, address)) {
  75. kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  76. goto out;
  77. }
  78. VCPU_EVENT(vcpu, 5, "storing prefix to %x", address);
  79. out:
  80. return 0;
  81. }
  82. static int handle_store_cpu_address(struct kvm_vcpu *vcpu)
  83. {
  84. int base2 = vcpu->arch.sie_block->ipb >> 28;
  85. int disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16);
  86. u64 useraddr;
  87. int rc;
  88. vcpu->stat.instruction_stap++;
  89. useraddr = disp2;
  90. if (base2)
  91. useraddr += vcpu->arch.guest_gprs[base2];
  92. if (useraddr & 1) {
  93. kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  94. goto out;
  95. }
  96. rc = put_guest_u16(vcpu, useraddr, vcpu->vcpu_id);
  97. if (rc == -EFAULT) {
  98. kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  99. goto out;
  100. }
  101. VCPU_EVENT(vcpu, 5, "storing cpu address to %llx", useraddr);
  102. out:
  103. return 0;
  104. }
  105. static int handle_skey(struct kvm_vcpu *vcpu)
  106. {
  107. vcpu->stat.instruction_storage_key++;
  108. vcpu->arch.sie_block->gpsw.addr -= 4;
  109. VCPU_EVENT(vcpu, 4, "%s", "retrying storage key operation");
  110. return 0;
  111. }
  112. static int handle_stsch(struct kvm_vcpu *vcpu)
  113. {
  114. vcpu->stat.instruction_stsch++;
  115. VCPU_EVENT(vcpu, 4, "%s", "store subchannel - 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_chsc(struct kvm_vcpu *vcpu)
  122. {
  123. vcpu->stat.instruction_chsc++;
  124. VCPU_EVENT(vcpu, 4, "%s", "channel subsystem call - CC3");
  125. /* condition code 3 */
  126. vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
  127. vcpu->arch.sie_block->gpsw.mask |= (3 & 3ul) << 44;
  128. return 0;
  129. }
  130. static int handle_stfl(struct kvm_vcpu *vcpu)
  131. {
  132. unsigned int facility_list;
  133. int rc;
  134. vcpu->stat.instruction_stfl++;
  135. /* only pass the facility bits, which we can handle */
  136. facility_list = S390_lowcore.stfl_fac_list & 0xff00fff3;
  137. rc = copy_to_guest(vcpu, offsetof(struct _lowcore, stfl_fac_list),
  138. &facility_list, sizeof(facility_list));
  139. if (rc == -EFAULT)
  140. kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  141. else
  142. VCPU_EVENT(vcpu, 5, "store facility list value %x",
  143. facility_list);
  144. return 0;
  145. }
  146. static int handle_stidp(struct kvm_vcpu *vcpu)
  147. {
  148. int base2 = vcpu->arch.sie_block->ipb >> 28;
  149. int disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16);
  150. u64 operand2;
  151. int rc;
  152. vcpu->stat.instruction_stidp++;
  153. operand2 = disp2;
  154. if (base2)
  155. operand2 += vcpu->arch.guest_gprs[base2];
  156. if (operand2 & 7) {
  157. kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  158. goto out;
  159. }
  160. rc = put_guest_u64(vcpu, operand2, vcpu->arch.stidp_data);
  161. if (rc == -EFAULT) {
  162. kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  163. goto out;
  164. }
  165. VCPU_EVENT(vcpu, 5, "%s", "store cpu id");
  166. out:
  167. return 0;
  168. }
  169. static void handle_stsi_3_2_2(struct kvm_vcpu *vcpu, struct sysinfo_3_2_2 *mem)
  170. {
  171. struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
  172. int cpus = 0;
  173. int n;
  174. spin_lock(&fi->lock);
  175. for (n = 0; n < KVM_MAX_VCPUS; n++)
  176. if (fi->local_int[n])
  177. cpus++;
  178. spin_unlock(&fi->lock);
  179. /* deal with other level 3 hypervisors */
  180. if (stsi(mem, 3, 2, 2) == -ENOSYS)
  181. mem->count = 0;
  182. if (mem->count < 8)
  183. mem->count++;
  184. for (n = mem->count - 1; n > 0 ; n--)
  185. memcpy(&mem->vm[n], &mem->vm[n - 1], 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->arch.guest_gprs[0] & 0xf0000000) >> 28;
  199. int sel1 = vcpu->arch.guest_gprs[0] & 0xff;
  200. int sel2 = vcpu->arch.guest_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->arch.guest_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->arch.guest_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->arch.guest_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->arch.guest_gprs[base1] : 0;
  291. u64 address2 = disp2 + base2 ? vcpu->arch.guest_gprs[base2] : 0;
  292. struct vm_area_struct *vma;
  293. vcpu->stat.instruction_tprot++;
  294. /* we only handle the Linux memory detection case:
  295. * access key == 0
  296. * guest DAT == off
  297. * everything else goes to userspace. */
  298. if (address2 & 0xf0)
  299. return -EOPNOTSUPP;
  300. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
  301. return -EOPNOTSUPP;
  302. down_read(&current->mm->mmap_sem);
  303. vma = find_vma(current->mm,
  304. (unsigned long) __guestaddr_to_user(vcpu, address1));
  305. if (!vma) {
  306. up_read(&current->mm->mmap_sem);
  307. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  308. }
  309. vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
  310. if (!(vma->vm_flags & VM_WRITE) && (vma->vm_flags & VM_READ))
  311. vcpu->arch.sie_block->gpsw.mask |= (1ul << 44);
  312. if (!(vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_READ))
  313. vcpu->arch.sie_block->gpsw.mask |= (2ul << 44);
  314. up_read(&current->mm->mmap_sem);
  315. return 0;
  316. }
  317. int kvm_s390_handle_e5(struct kvm_vcpu *vcpu)
  318. {
  319. /* For e5xx... instructions we only handle TPROT */
  320. if ((vcpu->arch.sie_block->ipa & 0x00ff) == 0x01)
  321. return handle_tprot(vcpu);
  322. return -EOPNOTSUPP;
  323. }