powerpc.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License, version 2, as
  4. * published by the Free Software Foundation.
  5. *
  6. * This program is distributed in the hope that it will be useful,
  7. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. * GNU General Public License for more details.
  10. *
  11. * You should have received a copy of the GNU General Public License
  12. * along with this program; if not, write to the Free Software
  13. * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  14. *
  15. * Copyright IBM Corp. 2007
  16. *
  17. * Authors: Hollis Blanchard <hollisb@us.ibm.com>
  18. * Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
  19. */
  20. #include <linux/errno.h>
  21. #include <linux/err.h>
  22. #include <linux/kvm_host.h>
  23. #include <linux/vmalloc.h>
  24. #include <linux/hrtimer.h>
  25. #include <linux/fs.h>
  26. #include <linux/slab.h>
  27. #include <linux/file.h>
  28. #include <linux/module.h>
  29. #include <asm/cputable.h>
  30. #include <asm/uaccess.h>
  31. #include <asm/kvm_ppc.h>
  32. #include <asm/tlbflush.h>
  33. #include <asm/cputhreads.h>
  34. #include <asm/irqflags.h>
  35. #include "timing.h"
  36. #include "irq.h"
  37. #include "../mm/mmu_decl.h"
  38. #define CREATE_TRACE_POINTS
  39. #include "trace.h"
  40. struct kvmppc_ops *kvmppc_hv_ops;
  41. EXPORT_SYMBOL_GPL(kvmppc_hv_ops);
  42. struct kvmppc_ops *kvmppc_pr_ops;
  43. EXPORT_SYMBOL_GPL(kvmppc_pr_ops);
  44. int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
  45. {
  46. return !!(v->arch.pending_exceptions) ||
  47. v->requests;
  48. }
  49. int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
  50. {
  51. return 1;
  52. }
  53. /*
  54. * Common checks before entering the guest world. Call with interrupts
  55. * disabled.
  56. *
  57. * returns:
  58. *
  59. * == 1 if we're ready to go into guest state
  60. * <= 0 if we need to go back to the host with return value
  61. */
  62. int kvmppc_prepare_to_enter(struct kvm_vcpu *vcpu)
  63. {
  64. int r;
  65. WARN_ON(irqs_disabled());
  66. hard_irq_disable();
  67. while (true) {
  68. if (need_resched()) {
  69. local_irq_enable();
  70. cond_resched();
  71. hard_irq_disable();
  72. continue;
  73. }
  74. if (signal_pending(current)) {
  75. kvmppc_account_exit(vcpu, SIGNAL_EXITS);
  76. vcpu->run->exit_reason = KVM_EXIT_INTR;
  77. r = -EINTR;
  78. break;
  79. }
  80. vcpu->mode = IN_GUEST_MODE;
  81. /*
  82. * Reading vcpu->requests must happen after setting vcpu->mode,
  83. * so we don't miss a request because the requester sees
  84. * OUTSIDE_GUEST_MODE and assumes we'll be checking requests
  85. * before next entering the guest (and thus doesn't IPI).
  86. */
  87. smp_mb();
  88. if (vcpu->requests) {
  89. /* Make sure we process requests preemptable */
  90. local_irq_enable();
  91. trace_kvm_check_requests(vcpu);
  92. r = kvmppc_core_check_requests(vcpu);
  93. hard_irq_disable();
  94. if (r > 0)
  95. continue;
  96. break;
  97. }
  98. if (kvmppc_core_prepare_to_enter(vcpu)) {
  99. /* interrupts got enabled in between, so we
  100. are back at square 1 */
  101. continue;
  102. }
  103. __kvm_guest_enter();
  104. return 1;
  105. }
  106. /* return to host */
  107. local_irq_enable();
  108. return r;
  109. }
  110. EXPORT_SYMBOL_GPL(kvmppc_prepare_to_enter);
  111. #if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_KVM_BOOK3S_PR_POSSIBLE)
  112. static void kvmppc_swab_shared(struct kvm_vcpu *vcpu)
  113. {
  114. struct kvm_vcpu_arch_shared *shared = vcpu->arch.shared;
  115. int i;
  116. shared->sprg0 = swab64(shared->sprg0);
  117. shared->sprg1 = swab64(shared->sprg1);
  118. shared->sprg2 = swab64(shared->sprg2);
  119. shared->sprg3 = swab64(shared->sprg3);
  120. shared->srr0 = swab64(shared->srr0);
  121. shared->srr1 = swab64(shared->srr1);
  122. shared->dar = swab64(shared->dar);
  123. shared->msr = swab64(shared->msr);
  124. shared->dsisr = swab32(shared->dsisr);
  125. shared->int_pending = swab32(shared->int_pending);
  126. for (i = 0; i < ARRAY_SIZE(shared->sr); i++)
  127. shared->sr[i] = swab32(shared->sr[i]);
  128. }
  129. #endif
  130. int kvmppc_kvm_pv(struct kvm_vcpu *vcpu)
  131. {
  132. int nr = kvmppc_get_gpr(vcpu, 11);
  133. int r;
  134. unsigned long __maybe_unused param1 = kvmppc_get_gpr(vcpu, 3);
  135. unsigned long __maybe_unused param2 = kvmppc_get_gpr(vcpu, 4);
  136. unsigned long __maybe_unused param3 = kvmppc_get_gpr(vcpu, 5);
  137. unsigned long __maybe_unused param4 = kvmppc_get_gpr(vcpu, 6);
  138. unsigned long r2 = 0;
  139. if (!(kvmppc_get_msr(vcpu) & MSR_SF)) {
  140. /* 32 bit mode */
  141. param1 &= 0xffffffff;
  142. param2 &= 0xffffffff;
  143. param3 &= 0xffffffff;
  144. param4 &= 0xffffffff;
  145. }
  146. switch (nr) {
  147. case KVM_HCALL_TOKEN(KVM_HC_PPC_MAP_MAGIC_PAGE):
  148. {
  149. #if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_KVM_BOOK3S_PR_POSSIBLE)
  150. /* Book3S can be little endian, find it out here */
  151. int shared_big_endian = true;
  152. if (vcpu->arch.intr_msr & MSR_LE)
  153. shared_big_endian = false;
  154. if (shared_big_endian != vcpu->arch.shared_big_endian)
  155. kvmppc_swab_shared(vcpu);
  156. vcpu->arch.shared_big_endian = shared_big_endian;
  157. #endif
  158. if (!(param2 & MAGIC_PAGE_FLAG_NOT_MAPPED_NX)) {
  159. /*
  160. * Older versions of the Linux magic page code had
  161. * a bug where they would map their trampoline code
  162. * NX. If that's the case, remove !PR NX capability.
  163. */
  164. vcpu->arch.disable_kernel_nx = true;
  165. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  166. }
  167. vcpu->arch.magic_page_pa = param1 & ~0xfffULL;
  168. vcpu->arch.magic_page_ea = param2 & ~0xfffULL;
  169. #ifdef CONFIG_PPC_64K_PAGES
  170. /*
  171. * Make sure our 4k magic page is in the same window of a 64k
  172. * page within the guest and within the host's page.
  173. */
  174. if ((vcpu->arch.magic_page_pa & 0xf000) !=
  175. ((ulong)vcpu->arch.shared & 0xf000)) {
  176. void *old_shared = vcpu->arch.shared;
  177. ulong shared = (ulong)vcpu->arch.shared;
  178. void *new_shared;
  179. shared &= PAGE_MASK;
  180. shared |= vcpu->arch.magic_page_pa & 0xf000;
  181. new_shared = (void*)shared;
  182. memcpy(new_shared, old_shared, 0x1000);
  183. vcpu->arch.shared = new_shared;
  184. }
  185. #endif
  186. r2 = KVM_MAGIC_FEAT_SR | KVM_MAGIC_FEAT_MAS0_TO_SPRG7;
  187. r = EV_SUCCESS;
  188. break;
  189. }
  190. case KVM_HCALL_TOKEN(KVM_HC_FEATURES):
  191. r = EV_SUCCESS;
  192. #if defined(CONFIG_PPC_BOOK3S) || defined(CONFIG_KVM_E500V2)
  193. r2 |= (1 << KVM_FEATURE_MAGIC_PAGE);
  194. #endif
  195. /* Second return value is in r4 */
  196. break;
  197. case EV_HCALL_TOKEN(EV_IDLE):
  198. r = EV_SUCCESS;
  199. kvm_vcpu_block(vcpu);
  200. clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
  201. break;
  202. default:
  203. r = EV_UNIMPLEMENTED;
  204. break;
  205. }
  206. kvmppc_set_gpr(vcpu, 4, r2);
  207. return r;
  208. }
  209. EXPORT_SYMBOL_GPL(kvmppc_kvm_pv);
  210. int kvmppc_sanity_check(struct kvm_vcpu *vcpu)
  211. {
  212. int r = false;
  213. /* We have to know what CPU to virtualize */
  214. if (!vcpu->arch.pvr)
  215. goto out;
  216. /* PAPR only works with book3s_64 */
  217. if ((vcpu->arch.cpu_type != KVM_CPU_3S_64) && vcpu->arch.papr_enabled)
  218. goto out;
  219. /* HV KVM can only do PAPR mode for now */
  220. if (!vcpu->arch.papr_enabled && is_kvmppc_hv_enabled(vcpu->kvm))
  221. goto out;
  222. #ifdef CONFIG_KVM_BOOKE_HV
  223. if (!cpu_has_feature(CPU_FTR_EMB_HV))
  224. goto out;
  225. #endif
  226. r = true;
  227. out:
  228. vcpu->arch.sane = r;
  229. return r ? 0 : -EINVAL;
  230. }
  231. EXPORT_SYMBOL_GPL(kvmppc_sanity_check);
  232. int kvmppc_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu)
  233. {
  234. enum emulation_result er;
  235. int r;
  236. er = kvmppc_emulate_loadstore(vcpu);
  237. switch (er) {
  238. case EMULATE_DONE:
  239. /* Future optimization: only reload non-volatiles if they were
  240. * actually modified. */
  241. r = RESUME_GUEST_NV;
  242. break;
  243. case EMULATE_AGAIN:
  244. r = RESUME_GUEST;
  245. break;
  246. case EMULATE_DO_MMIO:
  247. run->exit_reason = KVM_EXIT_MMIO;
  248. /* We must reload nonvolatiles because "update" load/store
  249. * instructions modify register state. */
  250. /* Future optimization: only reload non-volatiles if they were
  251. * actually modified. */
  252. r = RESUME_HOST_NV;
  253. break;
  254. case EMULATE_FAIL:
  255. {
  256. u32 last_inst;
  257. kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst);
  258. /* XXX Deliver Program interrupt to guest. */
  259. pr_emerg("%s: emulation failed (%08x)\n", __func__, last_inst);
  260. r = RESUME_HOST;
  261. break;
  262. }
  263. default:
  264. WARN_ON(1);
  265. r = RESUME_GUEST;
  266. }
  267. return r;
  268. }
  269. EXPORT_SYMBOL_GPL(kvmppc_emulate_mmio);
  270. int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
  271. bool data)
  272. {
  273. ulong mp_pa = vcpu->arch.magic_page_pa & KVM_PAM & PAGE_MASK;
  274. struct kvmppc_pte pte;
  275. int r;
  276. vcpu->stat.st++;
  277. r = kvmppc_xlate(vcpu, *eaddr, data ? XLATE_DATA : XLATE_INST,
  278. XLATE_WRITE, &pte);
  279. if (r < 0)
  280. return r;
  281. *eaddr = pte.raddr;
  282. if (!pte.may_write)
  283. return -EPERM;
  284. /* Magic page override */
  285. if (kvmppc_supports_magic_page(vcpu) && mp_pa &&
  286. ((pte.raddr & KVM_PAM & PAGE_MASK) == mp_pa) &&
  287. !(kvmppc_get_msr(vcpu) & MSR_PR)) {
  288. void *magic = vcpu->arch.shared;
  289. magic += pte.eaddr & 0xfff;
  290. memcpy(magic, ptr, size);
  291. return EMULATE_DONE;
  292. }
  293. if (kvm_write_guest(vcpu->kvm, pte.raddr, ptr, size))
  294. return EMULATE_DO_MMIO;
  295. return EMULATE_DONE;
  296. }
  297. EXPORT_SYMBOL_GPL(kvmppc_st);
  298. int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
  299. bool data)
  300. {
  301. ulong mp_pa = vcpu->arch.magic_page_pa & KVM_PAM & PAGE_MASK;
  302. struct kvmppc_pte pte;
  303. int rc;
  304. vcpu->stat.ld++;
  305. rc = kvmppc_xlate(vcpu, *eaddr, data ? XLATE_DATA : XLATE_INST,
  306. XLATE_READ, &pte);
  307. if (rc)
  308. return rc;
  309. *eaddr = pte.raddr;
  310. if (!pte.may_read)
  311. return -EPERM;
  312. if (!data && !pte.may_execute)
  313. return -ENOEXEC;
  314. /* Magic page override */
  315. if (kvmppc_supports_magic_page(vcpu) && mp_pa &&
  316. ((pte.raddr & KVM_PAM & PAGE_MASK) == mp_pa) &&
  317. !(kvmppc_get_msr(vcpu) & MSR_PR)) {
  318. void *magic = vcpu->arch.shared;
  319. magic += pte.eaddr & 0xfff;
  320. memcpy(ptr, magic, size);
  321. return EMULATE_DONE;
  322. }
  323. if (kvm_read_guest(vcpu->kvm, pte.raddr, ptr, size))
  324. return EMULATE_DO_MMIO;
  325. return EMULATE_DONE;
  326. }
  327. EXPORT_SYMBOL_GPL(kvmppc_ld);
  328. int kvm_arch_hardware_enable(void)
  329. {
  330. return 0;
  331. }
  332. int kvm_arch_hardware_setup(void)
  333. {
  334. return 0;
  335. }
  336. void kvm_arch_check_processor_compat(void *rtn)
  337. {
  338. *(int *)rtn = kvmppc_core_check_processor_compat();
  339. }
  340. int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
  341. {
  342. struct kvmppc_ops *kvm_ops = NULL;
  343. /*
  344. * if we have both HV and PR enabled, default is HV
  345. */
  346. if (type == 0) {
  347. if (kvmppc_hv_ops)
  348. kvm_ops = kvmppc_hv_ops;
  349. else
  350. kvm_ops = kvmppc_pr_ops;
  351. if (!kvm_ops)
  352. goto err_out;
  353. } else if (type == KVM_VM_PPC_HV) {
  354. if (!kvmppc_hv_ops)
  355. goto err_out;
  356. kvm_ops = kvmppc_hv_ops;
  357. } else if (type == KVM_VM_PPC_PR) {
  358. if (!kvmppc_pr_ops)
  359. goto err_out;
  360. kvm_ops = kvmppc_pr_ops;
  361. } else
  362. goto err_out;
  363. if (kvm_ops->owner && !try_module_get(kvm_ops->owner))
  364. return -ENOENT;
  365. kvm->arch.kvm_ops = kvm_ops;
  366. return kvmppc_core_init_vm(kvm);
  367. err_out:
  368. return -EINVAL;
  369. }
  370. void kvm_arch_destroy_vm(struct kvm *kvm)
  371. {
  372. unsigned int i;
  373. struct kvm_vcpu *vcpu;
  374. kvm_for_each_vcpu(i, vcpu, kvm)
  375. kvm_arch_vcpu_free(vcpu);
  376. mutex_lock(&kvm->lock);
  377. for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
  378. kvm->vcpus[i] = NULL;
  379. atomic_set(&kvm->online_vcpus, 0);
  380. kvmppc_core_destroy_vm(kvm);
  381. mutex_unlock(&kvm->lock);
  382. /* drop the module reference */
  383. module_put(kvm->arch.kvm_ops->owner);
  384. }
  385. int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
  386. {
  387. int r;
  388. /* Assume we're using HV mode when the HV module is loaded */
  389. int hv_enabled = kvmppc_hv_ops ? 1 : 0;
  390. if (kvm) {
  391. /*
  392. * Hooray - we know which VM type we're running on. Depend on
  393. * that rather than the guess above.
  394. */
  395. hv_enabled = is_kvmppc_hv_enabled(kvm);
  396. }
  397. switch (ext) {
  398. #ifdef CONFIG_BOOKE
  399. case KVM_CAP_PPC_BOOKE_SREGS:
  400. case KVM_CAP_PPC_BOOKE_WATCHDOG:
  401. case KVM_CAP_PPC_EPR:
  402. #else
  403. case KVM_CAP_PPC_SEGSTATE:
  404. case KVM_CAP_PPC_HIOR:
  405. case KVM_CAP_PPC_PAPR:
  406. #endif
  407. case KVM_CAP_PPC_UNSET_IRQ:
  408. case KVM_CAP_PPC_IRQ_LEVEL:
  409. case KVM_CAP_ENABLE_CAP:
  410. case KVM_CAP_ENABLE_CAP_VM:
  411. case KVM_CAP_ONE_REG:
  412. case KVM_CAP_IOEVENTFD:
  413. case KVM_CAP_DEVICE_CTRL:
  414. r = 1;
  415. break;
  416. case KVM_CAP_PPC_PAIRED_SINGLES:
  417. case KVM_CAP_PPC_OSI:
  418. case KVM_CAP_PPC_GET_PVINFO:
  419. #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
  420. case KVM_CAP_SW_TLB:
  421. #endif
  422. /* We support this only for PR */
  423. r = !hv_enabled;
  424. break;
  425. #ifdef CONFIG_KVM_MMIO
  426. case KVM_CAP_COALESCED_MMIO:
  427. r = KVM_COALESCED_MMIO_PAGE_OFFSET;
  428. break;
  429. #endif
  430. #ifdef CONFIG_KVM_MPIC
  431. case KVM_CAP_IRQ_MPIC:
  432. r = 1;
  433. break;
  434. #endif
  435. #ifdef CONFIG_PPC_BOOK3S_64
  436. case KVM_CAP_SPAPR_TCE:
  437. case KVM_CAP_PPC_ALLOC_HTAB:
  438. case KVM_CAP_PPC_RTAS:
  439. case KVM_CAP_PPC_FIXUP_HCALL:
  440. case KVM_CAP_PPC_ENABLE_HCALL:
  441. #ifdef CONFIG_KVM_XICS
  442. case KVM_CAP_IRQ_XICS:
  443. #endif
  444. r = 1;
  445. break;
  446. #endif /* CONFIG_PPC_BOOK3S_64 */
  447. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  448. case KVM_CAP_PPC_SMT:
  449. if (hv_enabled)
  450. r = threads_per_subcore;
  451. else
  452. r = 0;
  453. break;
  454. case KVM_CAP_PPC_RMA:
  455. r = 0;
  456. break;
  457. case KVM_CAP_PPC_HWRNG:
  458. r = kvmppc_hwrng_present();
  459. break;
  460. #endif
  461. case KVM_CAP_SYNC_MMU:
  462. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  463. r = hv_enabled;
  464. #elif defined(KVM_ARCH_WANT_MMU_NOTIFIER)
  465. r = 1;
  466. #else
  467. r = 0;
  468. #endif
  469. break;
  470. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  471. case KVM_CAP_PPC_HTAB_FD:
  472. r = hv_enabled;
  473. break;
  474. #endif
  475. case KVM_CAP_NR_VCPUS:
  476. /*
  477. * Recommending a number of CPUs is somewhat arbitrary; we
  478. * return the number of present CPUs for -HV (since a host
  479. * will have secondary threads "offline"), and for other KVM
  480. * implementations just count online CPUs.
  481. */
  482. if (hv_enabled)
  483. r = num_present_cpus();
  484. else
  485. r = num_online_cpus();
  486. break;
  487. case KVM_CAP_NR_MEMSLOTS:
  488. r = KVM_USER_MEM_SLOTS;
  489. break;
  490. case KVM_CAP_MAX_VCPUS:
  491. r = KVM_MAX_VCPUS;
  492. break;
  493. #ifdef CONFIG_PPC_BOOK3S_64
  494. case KVM_CAP_PPC_GET_SMMU_INFO:
  495. r = 1;
  496. break;
  497. #endif
  498. default:
  499. r = 0;
  500. break;
  501. }
  502. return r;
  503. }
  504. long kvm_arch_dev_ioctl(struct file *filp,
  505. unsigned int ioctl, unsigned long arg)
  506. {
  507. return -EINVAL;
  508. }
  509. void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
  510. struct kvm_memory_slot *dont)
  511. {
  512. kvmppc_core_free_memslot(kvm, free, dont);
  513. }
  514. int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
  515. unsigned long npages)
  516. {
  517. return kvmppc_core_create_memslot(kvm, slot, npages);
  518. }
  519. int kvm_arch_prepare_memory_region(struct kvm *kvm,
  520. struct kvm_memory_slot *memslot,
  521. const struct kvm_userspace_memory_region *mem,
  522. enum kvm_mr_change change)
  523. {
  524. return kvmppc_core_prepare_memory_region(kvm, memslot, mem);
  525. }
  526. void kvm_arch_commit_memory_region(struct kvm *kvm,
  527. const struct kvm_userspace_memory_region *mem,
  528. const struct kvm_memory_slot *old,
  529. const struct kvm_memory_slot *new,
  530. enum kvm_mr_change change)
  531. {
  532. kvmppc_core_commit_memory_region(kvm, mem, old, new);
  533. }
  534. void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
  535. struct kvm_memory_slot *slot)
  536. {
  537. kvmppc_core_flush_memslot(kvm, slot);
  538. }
  539. struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
  540. {
  541. struct kvm_vcpu *vcpu;
  542. vcpu = kvmppc_core_vcpu_create(kvm, id);
  543. if (!IS_ERR(vcpu)) {
  544. vcpu->arch.wqp = &vcpu->wq;
  545. kvmppc_create_vcpu_debugfs(vcpu, id);
  546. }
  547. return vcpu;
  548. }
  549. void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
  550. {
  551. }
  552. void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
  553. {
  554. /* Make sure we're not using the vcpu anymore */
  555. hrtimer_cancel(&vcpu->arch.dec_timer);
  556. kvmppc_remove_vcpu_debugfs(vcpu);
  557. switch (vcpu->arch.irq_type) {
  558. case KVMPPC_IRQ_MPIC:
  559. kvmppc_mpic_disconnect_vcpu(vcpu->arch.mpic, vcpu);
  560. break;
  561. case KVMPPC_IRQ_XICS:
  562. kvmppc_xics_free_icp(vcpu);
  563. break;
  564. }
  565. kvmppc_core_vcpu_free(vcpu);
  566. }
  567. void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
  568. {
  569. kvm_arch_vcpu_free(vcpu);
  570. }
  571. int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
  572. {
  573. return kvmppc_core_pending_dec(vcpu);
  574. }
  575. static enum hrtimer_restart kvmppc_decrementer_wakeup(struct hrtimer *timer)
  576. {
  577. struct kvm_vcpu *vcpu;
  578. vcpu = container_of(timer, struct kvm_vcpu, arch.dec_timer);
  579. kvmppc_decrementer_func(vcpu);
  580. return HRTIMER_NORESTART;
  581. }
  582. int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
  583. {
  584. int ret;
  585. hrtimer_init(&vcpu->arch.dec_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
  586. vcpu->arch.dec_timer.function = kvmppc_decrementer_wakeup;
  587. vcpu->arch.dec_expires = ~(u64)0;
  588. #ifdef CONFIG_KVM_EXIT_TIMING
  589. mutex_init(&vcpu->arch.exit_timing_lock);
  590. #endif
  591. ret = kvmppc_subarch_vcpu_init(vcpu);
  592. return ret;
  593. }
  594. void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
  595. {
  596. kvmppc_mmu_destroy(vcpu);
  597. kvmppc_subarch_vcpu_uninit(vcpu);
  598. }
  599. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  600. {
  601. #ifdef CONFIG_BOOKE
  602. /*
  603. * vrsave (formerly usprg0) isn't used by Linux, but may
  604. * be used by the guest.
  605. *
  606. * On non-booke this is associated with Altivec and
  607. * is handled by code in book3s.c.
  608. */
  609. mtspr(SPRN_VRSAVE, vcpu->arch.vrsave);
  610. #endif
  611. kvmppc_core_vcpu_load(vcpu, cpu);
  612. }
  613. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
  614. {
  615. kvmppc_core_vcpu_put(vcpu);
  616. #ifdef CONFIG_BOOKE
  617. vcpu->arch.vrsave = mfspr(SPRN_VRSAVE);
  618. #endif
  619. }
  620. static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu,
  621. struct kvm_run *run)
  622. {
  623. u64 uninitialized_var(gpr);
  624. if (run->mmio.len > sizeof(gpr)) {
  625. printk(KERN_ERR "bad MMIO length: %d\n", run->mmio.len);
  626. return;
  627. }
  628. if (!vcpu->arch.mmio_host_swabbed) {
  629. switch (run->mmio.len) {
  630. case 8: gpr = *(u64 *)run->mmio.data; break;
  631. case 4: gpr = *(u32 *)run->mmio.data; break;
  632. case 2: gpr = *(u16 *)run->mmio.data; break;
  633. case 1: gpr = *(u8 *)run->mmio.data; break;
  634. }
  635. } else {
  636. switch (run->mmio.len) {
  637. case 8: gpr = swab64(*(u64 *)run->mmio.data); break;
  638. case 4: gpr = swab32(*(u32 *)run->mmio.data); break;
  639. case 2: gpr = swab16(*(u16 *)run->mmio.data); break;
  640. case 1: gpr = *(u8 *)run->mmio.data; break;
  641. }
  642. }
  643. if (vcpu->arch.mmio_sign_extend) {
  644. switch (run->mmio.len) {
  645. #ifdef CONFIG_PPC64
  646. case 4:
  647. gpr = (s64)(s32)gpr;
  648. break;
  649. #endif
  650. case 2:
  651. gpr = (s64)(s16)gpr;
  652. break;
  653. case 1:
  654. gpr = (s64)(s8)gpr;
  655. break;
  656. }
  657. }
  658. kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
  659. switch (vcpu->arch.io_gpr & KVM_MMIO_REG_EXT_MASK) {
  660. case KVM_MMIO_REG_GPR:
  661. kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
  662. break;
  663. case KVM_MMIO_REG_FPR:
  664. VCPU_FPR(vcpu, vcpu->arch.io_gpr & KVM_MMIO_REG_MASK) = gpr;
  665. break;
  666. #ifdef CONFIG_PPC_BOOK3S
  667. case KVM_MMIO_REG_QPR:
  668. vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
  669. break;
  670. case KVM_MMIO_REG_FQPR:
  671. VCPU_FPR(vcpu, vcpu->arch.io_gpr & KVM_MMIO_REG_MASK) = gpr;
  672. vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
  673. break;
  674. #endif
  675. default:
  676. BUG();
  677. }
  678. }
  679. int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
  680. unsigned int rt, unsigned int bytes,
  681. int is_default_endian)
  682. {
  683. int idx, ret;
  684. bool host_swabbed;
  685. /* Pity C doesn't have a logical XOR operator */
  686. if (kvmppc_need_byteswap(vcpu)) {
  687. host_swabbed = is_default_endian;
  688. } else {
  689. host_swabbed = !is_default_endian;
  690. }
  691. if (bytes > sizeof(run->mmio.data)) {
  692. printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
  693. run->mmio.len);
  694. }
  695. run->mmio.phys_addr = vcpu->arch.paddr_accessed;
  696. run->mmio.len = bytes;
  697. run->mmio.is_write = 0;
  698. vcpu->arch.io_gpr = rt;
  699. vcpu->arch.mmio_host_swabbed = host_swabbed;
  700. vcpu->mmio_needed = 1;
  701. vcpu->mmio_is_write = 0;
  702. vcpu->arch.mmio_sign_extend = 0;
  703. idx = srcu_read_lock(&vcpu->kvm->srcu);
  704. ret = kvm_io_bus_read(vcpu, KVM_MMIO_BUS, run->mmio.phys_addr,
  705. bytes, &run->mmio.data);
  706. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  707. if (!ret) {
  708. kvmppc_complete_mmio_load(vcpu, run);
  709. vcpu->mmio_needed = 0;
  710. return EMULATE_DONE;
  711. }
  712. return EMULATE_DO_MMIO;
  713. }
  714. EXPORT_SYMBOL_GPL(kvmppc_handle_load);
  715. /* Same as above, but sign extends */
  716. int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu,
  717. unsigned int rt, unsigned int bytes,
  718. int is_default_endian)
  719. {
  720. int r;
  721. vcpu->arch.mmio_sign_extend = 1;
  722. r = kvmppc_handle_load(run, vcpu, rt, bytes, is_default_endian);
  723. return r;
  724. }
  725. int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
  726. u64 val, unsigned int bytes, int is_default_endian)
  727. {
  728. void *data = run->mmio.data;
  729. int idx, ret;
  730. bool host_swabbed;
  731. /* Pity C doesn't have a logical XOR operator */
  732. if (kvmppc_need_byteswap(vcpu)) {
  733. host_swabbed = is_default_endian;
  734. } else {
  735. host_swabbed = !is_default_endian;
  736. }
  737. if (bytes > sizeof(run->mmio.data)) {
  738. printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
  739. run->mmio.len);
  740. }
  741. run->mmio.phys_addr = vcpu->arch.paddr_accessed;
  742. run->mmio.len = bytes;
  743. run->mmio.is_write = 1;
  744. vcpu->mmio_needed = 1;
  745. vcpu->mmio_is_write = 1;
  746. /* Store the value at the lowest bytes in 'data'. */
  747. if (!host_swabbed) {
  748. switch (bytes) {
  749. case 8: *(u64 *)data = val; break;
  750. case 4: *(u32 *)data = val; break;
  751. case 2: *(u16 *)data = val; break;
  752. case 1: *(u8 *)data = val; break;
  753. }
  754. } else {
  755. switch (bytes) {
  756. case 8: *(u64 *)data = swab64(val); break;
  757. case 4: *(u32 *)data = swab32(val); break;
  758. case 2: *(u16 *)data = swab16(val); break;
  759. case 1: *(u8 *)data = val; break;
  760. }
  761. }
  762. idx = srcu_read_lock(&vcpu->kvm->srcu);
  763. ret = kvm_io_bus_write(vcpu, KVM_MMIO_BUS, run->mmio.phys_addr,
  764. bytes, &run->mmio.data);
  765. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  766. if (!ret) {
  767. vcpu->mmio_needed = 0;
  768. return EMULATE_DONE;
  769. }
  770. return EMULATE_DO_MMIO;
  771. }
  772. EXPORT_SYMBOL_GPL(kvmppc_handle_store);
  773. int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
  774. {
  775. int r = 0;
  776. union kvmppc_one_reg val;
  777. int size;
  778. size = one_reg_size(reg->id);
  779. if (size > sizeof(val))
  780. return -EINVAL;
  781. r = kvmppc_get_one_reg(vcpu, reg->id, &val);
  782. if (r == -EINVAL) {
  783. r = 0;
  784. switch (reg->id) {
  785. #ifdef CONFIG_ALTIVEC
  786. case KVM_REG_PPC_VR0 ... KVM_REG_PPC_VR31:
  787. if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
  788. r = -ENXIO;
  789. break;
  790. }
  791. val.vval = vcpu->arch.vr.vr[reg->id - KVM_REG_PPC_VR0];
  792. break;
  793. case KVM_REG_PPC_VSCR:
  794. if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
  795. r = -ENXIO;
  796. break;
  797. }
  798. val = get_reg_val(reg->id, vcpu->arch.vr.vscr.u[3]);
  799. break;
  800. case KVM_REG_PPC_VRSAVE:
  801. val = get_reg_val(reg->id, vcpu->arch.vrsave);
  802. break;
  803. #endif /* CONFIG_ALTIVEC */
  804. default:
  805. r = -EINVAL;
  806. break;
  807. }
  808. }
  809. if (r)
  810. return r;
  811. if (copy_to_user((char __user *)(unsigned long)reg->addr, &val, size))
  812. r = -EFAULT;
  813. return r;
  814. }
  815. int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
  816. {
  817. int r;
  818. union kvmppc_one_reg val;
  819. int size;
  820. size = one_reg_size(reg->id);
  821. if (size > sizeof(val))
  822. return -EINVAL;
  823. if (copy_from_user(&val, (char __user *)(unsigned long)reg->addr, size))
  824. return -EFAULT;
  825. r = kvmppc_set_one_reg(vcpu, reg->id, &val);
  826. if (r == -EINVAL) {
  827. r = 0;
  828. switch (reg->id) {
  829. #ifdef CONFIG_ALTIVEC
  830. case KVM_REG_PPC_VR0 ... KVM_REG_PPC_VR31:
  831. if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
  832. r = -ENXIO;
  833. break;
  834. }
  835. vcpu->arch.vr.vr[reg->id - KVM_REG_PPC_VR0] = val.vval;
  836. break;
  837. case KVM_REG_PPC_VSCR:
  838. if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
  839. r = -ENXIO;
  840. break;
  841. }
  842. vcpu->arch.vr.vscr.u[3] = set_reg_val(reg->id, val);
  843. break;
  844. case KVM_REG_PPC_VRSAVE:
  845. if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
  846. r = -ENXIO;
  847. break;
  848. }
  849. vcpu->arch.vrsave = set_reg_val(reg->id, val);
  850. break;
  851. #endif /* CONFIG_ALTIVEC */
  852. default:
  853. r = -EINVAL;
  854. break;
  855. }
  856. }
  857. return r;
  858. }
  859. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
  860. {
  861. int r;
  862. sigset_t sigsaved;
  863. if (vcpu->sigset_active)
  864. sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
  865. if (vcpu->mmio_needed) {
  866. if (!vcpu->mmio_is_write)
  867. kvmppc_complete_mmio_load(vcpu, run);
  868. vcpu->mmio_needed = 0;
  869. } else if (vcpu->arch.osi_needed) {
  870. u64 *gprs = run->osi.gprs;
  871. int i;
  872. for (i = 0; i < 32; i++)
  873. kvmppc_set_gpr(vcpu, i, gprs[i]);
  874. vcpu->arch.osi_needed = 0;
  875. } else if (vcpu->arch.hcall_needed) {
  876. int i;
  877. kvmppc_set_gpr(vcpu, 3, run->papr_hcall.ret);
  878. for (i = 0; i < 9; ++i)
  879. kvmppc_set_gpr(vcpu, 4 + i, run->papr_hcall.args[i]);
  880. vcpu->arch.hcall_needed = 0;
  881. #ifdef CONFIG_BOOKE
  882. } else if (vcpu->arch.epr_needed) {
  883. kvmppc_set_epr(vcpu, run->epr.epr);
  884. vcpu->arch.epr_needed = 0;
  885. #endif
  886. }
  887. r = kvmppc_vcpu_run(run, vcpu);
  888. if (vcpu->sigset_active)
  889. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  890. return r;
  891. }
  892. int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
  893. {
  894. if (irq->irq == KVM_INTERRUPT_UNSET) {
  895. kvmppc_core_dequeue_external(vcpu);
  896. return 0;
  897. }
  898. kvmppc_core_queue_external(vcpu, irq);
  899. kvm_vcpu_kick(vcpu);
  900. return 0;
  901. }
  902. static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
  903. struct kvm_enable_cap *cap)
  904. {
  905. int r;
  906. if (cap->flags)
  907. return -EINVAL;
  908. switch (cap->cap) {
  909. case KVM_CAP_PPC_OSI:
  910. r = 0;
  911. vcpu->arch.osi_enabled = true;
  912. break;
  913. case KVM_CAP_PPC_PAPR:
  914. r = 0;
  915. vcpu->arch.papr_enabled = true;
  916. break;
  917. case KVM_CAP_PPC_EPR:
  918. r = 0;
  919. if (cap->args[0])
  920. vcpu->arch.epr_flags |= KVMPPC_EPR_USER;
  921. else
  922. vcpu->arch.epr_flags &= ~KVMPPC_EPR_USER;
  923. break;
  924. #ifdef CONFIG_BOOKE
  925. case KVM_CAP_PPC_BOOKE_WATCHDOG:
  926. r = 0;
  927. vcpu->arch.watchdog_enabled = true;
  928. break;
  929. #endif
  930. #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
  931. case KVM_CAP_SW_TLB: {
  932. struct kvm_config_tlb cfg;
  933. void __user *user_ptr = (void __user *)(uintptr_t)cap->args[0];
  934. r = -EFAULT;
  935. if (copy_from_user(&cfg, user_ptr, sizeof(cfg)))
  936. break;
  937. r = kvm_vcpu_ioctl_config_tlb(vcpu, &cfg);
  938. break;
  939. }
  940. #endif
  941. #ifdef CONFIG_KVM_MPIC
  942. case KVM_CAP_IRQ_MPIC: {
  943. struct fd f;
  944. struct kvm_device *dev;
  945. r = -EBADF;
  946. f = fdget(cap->args[0]);
  947. if (!f.file)
  948. break;
  949. r = -EPERM;
  950. dev = kvm_device_from_filp(f.file);
  951. if (dev)
  952. r = kvmppc_mpic_connect_vcpu(dev, vcpu, cap->args[1]);
  953. fdput(f);
  954. break;
  955. }
  956. #endif
  957. #ifdef CONFIG_KVM_XICS
  958. case KVM_CAP_IRQ_XICS: {
  959. struct fd f;
  960. struct kvm_device *dev;
  961. r = -EBADF;
  962. f = fdget(cap->args[0]);
  963. if (!f.file)
  964. break;
  965. r = -EPERM;
  966. dev = kvm_device_from_filp(f.file);
  967. if (dev)
  968. r = kvmppc_xics_connect_vcpu(dev, vcpu, cap->args[1]);
  969. fdput(f);
  970. break;
  971. }
  972. #endif /* CONFIG_KVM_XICS */
  973. default:
  974. r = -EINVAL;
  975. break;
  976. }
  977. if (!r)
  978. r = kvmppc_sanity_check(vcpu);
  979. return r;
  980. }
  981. int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
  982. struct kvm_mp_state *mp_state)
  983. {
  984. return -EINVAL;
  985. }
  986. int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
  987. struct kvm_mp_state *mp_state)
  988. {
  989. return -EINVAL;
  990. }
  991. long kvm_arch_vcpu_ioctl(struct file *filp,
  992. unsigned int ioctl, unsigned long arg)
  993. {
  994. struct kvm_vcpu *vcpu = filp->private_data;
  995. void __user *argp = (void __user *)arg;
  996. long r;
  997. switch (ioctl) {
  998. case KVM_INTERRUPT: {
  999. struct kvm_interrupt irq;
  1000. r = -EFAULT;
  1001. if (copy_from_user(&irq, argp, sizeof(irq)))
  1002. goto out;
  1003. r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
  1004. goto out;
  1005. }
  1006. case KVM_ENABLE_CAP:
  1007. {
  1008. struct kvm_enable_cap cap;
  1009. r = -EFAULT;
  1010. if (copy_from_user(&cap, argp, sizeof(cap)))
  1011. goto out;
  1012. r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
  1013. break;
  1014. }
  1015. case KVM_SET_ONE_REG:
  1016. case KVM_GET_ONE_REG:
  1017. {
  1018. struct kvm_one_reg reg;
  1019. r = -EFAULT;
  1020. if (copy_from_user(&reg, argp, sizeof(reg)))
  1021. goto out;
  1022. if (ioctl == KVM_SET_ONE_REG)
  1023. r = kvm_vcpu_ioctl_set_one_reg(vcpu, &reg);
  1024. else
  1025. r = kvm_vcpu_ioctl_get_one_reg(vcpu, &reg);
  1026. break;
  1027. }
  1028. #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
  1029. case KVM_DIRTY_TLB: {
  1030. struct kvm_dirty_tlb dirty;
  1031. r = -EFAULT;
  1032. if (copy_from_user(&dirty, argp, sizeof(dirty)))
  1033. goto out;
  1034. r = kvm_vcpu_ioctl_dirty_tlb(vcpu, &dirty);
  1035. break;
  1036. }
  1037. #endif
  1038. default:
  1039. r = -EINVAL;
  1040. }
  1041. out:
  1042. return r;
  1043. }
  1044. int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
  1045. {
  1046. return VM_FAULT_SIGBUS;
  1047. }
  1048. static int kvm_vm_ioctl_get_pvinfo(struct kvm_ppc_pvinfo *pvinfo)
  1049. {
  1050. u32 inst_nop = 0x60000000;
  1051. #ifdef CONFIG_KVM_BOOKE_HV
  1052. u32 inst_sc1 = 0x44000022;
  1053. pvinfo->hcall[0] = cpu_to_be32(inst_sc1);
  1054. pvinfo->hcall[1] = cpu_to_be32(inst_nop);
  1055. pvinfo->hcall[2] = cpu_to_be32(inst_nop);
  1056. pvinfo->hcall[3] = cpu_to_be32(inst_nop);
  1057. #else
  1058. u32 inst_lis = 0x3c000000;
  1059. u32 inst_ori = 0x60000000;
  1060. u32 inst_sc = 0x44000002;
  1061. u32 inst_imm_mask = 0xffff;
  1062. /*
  1063. * The hypercall to get into KVM from within guest context is as
  1064. * follows:
  1065. *
  1066. * lis r0, r0, KVM_SC_MAGIC_R0@h
  1067. * ori r0, KVM_SC_MAGIC_R0@l
  1068. * sc
  1069. * nop
  1070. */
  1071. pvinfo->hcall[0] = cpu_to_be32(inst_lis | ((KVM_SC_MAGIC_R0 >> 16) & inst_imm_mask));
  1072. pvinfo->hcall[1] = cpu_to_be32(inst_ori | (KVM_SC_MAGIC_R0 & inst_imm_mask));
  1073. pvinfo->hcall[2] = cpu_to_be32(inst_sc);
  1074. pvinfo->hcall[3] = cpu_to_be32(inst_nop);
  1075. #endif
  1076. pvinfo->flags = KVM_PPC_PVINFO_FLAGS_EV_IDLE;
  1077. return 0;
  1078. }
  1079. int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
  1080. bool line_status)
  1081. {
  1082. if (!irqchip_in_kernel(kvm))
  1083. return -ENXIO;
  1084. irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
  1085. irq_event->irq, irq_event->level,
  1086. line_status);
  1087. return 0;
  1088. }
  1089. static int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
  1090. struct kvm_enable_cap *cap)
  1091. {
  1092. int r;
  1093. if (cap->flags)
  1094. return -EINVAL;
  1095. switch (cap->cap) {
  1096. #ifdef CONFIG_KVM_BOOK3S_64_HANDLER
  1097. case KVM_CAP_PPC_ENABLE_HCALL: {
  1098. unsigned long hcall = cap->args[0];
  1099. r = -EINVAL;
  1100. if (hcall > MAX_HCALL_OPCODE || (hcall & 3) ||
  1101. cap->args[1] > 1)
  1102. break;
  1103. if (!kvmppc_book3s_hcall_implemented(kvm, hcall))
  1104. break;
  1105. if (cap->args[1])
  1106. set_bit(hcall / 4, kvm->arch.enabled_hcalls);
  1107. else
  1108. clear_bit(hcall / 4, kvm->arch.enabled_hcalls);
  1109. r = 0;
  1110. break;
  1111. }
  1112. #endif
  1113. default:
  1114. r = -EINVAL;
  1115. break;
  1116. }
  1117. return r;
  1118. }
  1119. long kvm_arch_vm_ioctl(struct file *filp,
  1120. unsigned int ioctl, unsigned long arg)
  1121. {
  1122. struct kvm *kvm __maybe_unused = filp->private_data;
  1123. void __user *argp = (void __user *)arg;
  1124. long r;
  1125. switch (ioctl) {
  1126. case KVM_PPC_GET_PVINFO: {
  1127. struct kvm_ppc_pvinfo pvinfo;
  1128. memset(&pvinfo, 0, sizeof(pvinfo));
  1129. r = kvm_vm_ioctl_get_pvinfo(&pvinfo);
  1130. if (copy_to_user(argp, &pvinfo, sizeof(pvinfo))) {
  1131. r = -EFAULT;
  1132. goto out;
  1133. }
  1134. break;
  1135. }
  1136. case KVM_ENABLE_CAP:
  1137. {
  1138. struct kvm_enable_cap cap;
  1139. r = -EFAULT;
  1140. if (copy_from_user(&cap, argp, sizeof(cap)))
  1141. goto out;
  1142. r = kvm_vm_ioctl_enable_cap(kvm, &cap);
  1143. break;
  1144. }
  1145. #ifdef CONFIG_PPC_BOOK3S_64
  1146. case KVM_CREATE_SPAPR_TCE: {
  1147. struct kvm_create_spapr_tce create_tce;
  1148. r = -EFAULT;
  1149. if (copy_from_user(&create_tce, argp, sizeof(create_tce)))
  1150. goto out;
  1151. r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce);
  1152. goto out;
  1153. }
  1154. case KVM_PPC_GET_SMMU_INFO: {
  1155. struct kvm_ppc_smmu_info info;
  1156. struct kvm *kvm = filp->private_data;
  1157. memset(&info, 0, sizeof(info));
  1158. r = kvm->arch.kvm_ops->get_smmu_info(kvm, &info);
  1159. if (r >= 0 && copy_to_user(argp, &info, sizeof(info)))
  1160. r = -EFAULT;
  1161. break;
  1162. }
  1163. case KVM_PPC_RTAS_DEFINE_TOKEN: {
  1164. struct kvm *kvm = filp->private_data;
  1165. r = kvm_vm_ioctl_rtas_define_token(kvm, argp);
  1166. break;
  1167. }
  1168. default: {
  1169. struct kvm *kvm = filp->private_data;
  1170. r = kvm->arch.kvm_ops->arch_vm_ioctl(filp, ioctl, arg);
  1171. }
  1172. #else /* CONFIG_PPC_BOOK3S_64 */
  1173. default:
  1174. r = -ENOTTY;
  1175. #endif
  1176. }
  1177. out:
  1178. return r;
  1179. }
  1180. static unsigned long lpid_inuse[BITS_TO_LONGS(KVMPPC_NR_LPIDS)];
  1181. static unsigned long nr_lpids;
  1182. long kvmppc_alloc_lpid(void)
  1183. {
  1184. long lpid;
  1185. do {
  1186. lpid = find_first_zero_bit(lpid_inuse, KVMPPC_NR_LPIDS);
  1187. if (lpid >= nr_lpids) {
  1188. pr_err("%s: No LPIDs free\n", __func__);
  1189. return -ENOMEM;
  1190. }
  1191. } while (test_and_set_bit(lpid, lpid_inuse));
  1192. return lpid;
  1193. }
  1194. EXPORT_SYMBOL_GPL(kvmppc_alloc_lpid);
  1195. void kvmppc_claim_lpid(long lpid)
  1196. {
  1197. set_bit(lpid, lpid_inuse);
  1198. }
  1199. EXPORT_SYMBOL_GPL(kvmppc_claim_lpid);
  1200. void kvmppc_free_lpid(long lpid)
  1201. {
  1202. clear_bit(lpid, lpid_inuse);
  1203. }
  1204. EXPORT_SYMBOL_GPL(kvmppc_free_lpid);
  1205. void kvmppc_init_lpid(unsigned long nr_lpids_param)
  1206. {
  1207. nr_lpids = min_t(unsigned long, KVMPPC_NR_LPIDS, nr_lpids_param);
  1208. memset(lpid_inuse, 0, sizeof(lpid_inuse));
  1209. }
  1210. EXPORT_SYMBOL_GPL(kvmppc_init_lpid);
  1211. int kvm_arch_init(void *opaque)
  1212. {
  1213. return 0;
  1214. }
  1215. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ppc_instr);