powerpc.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412
  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_MAX_VCPUS:
  488. r = KVM_MAX_VCPUS;
  489. break;
  490. #ifdef CONFIG_PPC_BOOK3S_64
  491. case KVM_CAP_PPC_GET_SMMU_INFO:
  492. r = 1;
  493. break;
  494. #endif
  495. default:
  496. r = 0;
  497. break;
  498. }
  499. return r;
  500. }
  501. long kvm_arch_dev_ioctl(struct file *filp,
  502. unsigned int ioctl, unsigned long arg)
  503. {
  504. return -EINVAL;
  505. }
  506. void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
  507. struct kvm_memory_slot *dont)
  508. {
  509. kvmppc_core_free_memslot(kvm, free, dont);
  510. }
  511. int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
  512. unsigned long npages)
  513. {
  514. return kvmppc_core_create_memslot(kvm, slot, npages);
  515. }
  516. int kvm_arch_prepare_memory_region(struct kvm *kvm,
  517. struct kvm_memory_slot *memslot,
  518. const struct kvm_userspace_memory_region *mem,
  519. enum kvm_mr_change change)
  520. {
  521. return kvmppc_core_prepare_memory_region(kvm, memslot, mem);
  522. }
  523. void kvm_arch_commit_memory_region(struct kvm *kvm,
  524. const struct kvm_userspace_memory_region *mem,
  525. const struct kvm_memory_slot *old,
  526. const struct kvm_memory_slot *new,
  527. enum kvm_mr_change change)
  528. {
  529. kvmppc_core_commit_memory_region(kvm, mem, old, new);
  530. }
  531. void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
  532. struct kvm_memory_slot *slot)
  533. {
  534. kvmppc_core_flush_memslot(kvm, slot);
  535. }
  536. struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
  537. {
  538. struct kvm_vcpu *vcpu;
  539. vcpu = kvmppc_core_vcpu_create(kvm, id);
  540. if (!IS_ERR(vcpu)) {
  541. vcpu->arch.wqp = &vcpu->wq;
  542. kvmppc_create_vcpu_debugfs(vcpu, id);
  543. }
  544. return vcpu;
  545. }
  546. void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
  547. {
  548. }
  549. void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
  550. {
  551. /* Make sure we're not using the vcpu anymore */
  552. hrtimer_cancel(&vcpu->arch.dec_timer);
  553. kvmppc_remove_vcpu_debugfs(vcpu);
  554. switch (vcpu->arch.irq_type) {
  555. case KVMPPC_IRQ_MPIC:
  556. kvmppc_mpic_disconnect_vcpu(vcpu->arch.mpic, vcpu);
  557. break;
  558. case KVMPPC_IRQ_XICS:
  559. kvmppc_xics_free_icp(vcpu);
  560. break;
  561. }
  562. kvmppc_core_vcpu_free(vcpu);
  563. }
  564. void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
  565. {
  566. kvm_arch_vcpu_free(vcpu);
  567. }
  568. int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
  569. {
  570. return kvmppc_core_pending_dec(vcpu);
  571. }
  572. static enum hrtimer_restart kvmppc_decrementer_wakeup(struct hrtimer *timer)
  573. {
  574. struct kvm_vcpu *vcpu;
  575. vcpu = container_of(timer, struct kvm_vcpu, arch.dec_timer);
  576. kvmppc_decrementer_func(vcpu);
  577. return HRTIMER_NORESTART;
  578. }
  579. int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
  580. {
  581. int ret;
  582. hrtimer_init(&vcpu->arch.dec_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
  583. vcpu->arch.dec_timer.function = kvmppc_decrementer_wakeup;
  584. vcpu->arch.dec_expires = ~(u64)0;
  585. #ifdef CONFIG_KVM_EXIT_TIMING
  586. mutex_init(&vcpu->arch.exit_timing_lock);
  587. #endif
  588. ret = kvmppc_subarch_vcpu_init(vcpu);
  589. return ret;
  590. }
  591. void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
  592. {
  593. kvmppc_mmu_destroy(vcpu);
  594. kvmppc_subarch_vcpu_uninit(vcpu);
  595. }
  596. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  597. {
  598. #ifdef CONFIG_BOOKE
  599. /*
  600. * vrsave (formerly usprg0) isn't used by Linux, but may
  601. * be used by the guest.
  602. *
  603. * On non-booke this is associated with Altivec and
  604. * is handled by code in book3s.c.
  605. */
  606. mtspr(SPRN_VRSAVE, vcpu->arch.vrsave);
  607. #endif
  608. kvmppc_core_vcpu_load(vcpu, cpu);
  609. }
  610. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
  611. {
  612. kvmppc_core_vcpu_put(vcpu);
  613. #ifdef CONFIG_BOOKE
  614. vcpu->arch.vrsave = mfspr(SPRN_VRSAVE);
  615. #endif
  616. }
  617. static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu,
  618. struct kvm_run *run)
  619. {
  620. u64 uninitialized_var(gpr);
  621. if (run->mmio.len > sizeof(gpr)) {
  622. printk(KERN_ERR "bad MMIO length: %d\n", run->mmio.len);
  623. return;
  624. }
  625. if (!vcpu->arch.mmio_host_swabbed) {
  626. switch (run->mmio.len) {
  627. case 8: gpr = *(u64 *)run->mmio.data; break;
  628. case 4: gpr = *(u32 *)run->mmio.data; break;
  629. case 2: gpr = *(u16 *)run->mmio.data; break;
  630. case 1: gpr = *(u8 *)run->mmio.data; break;
  631. }
  632. } else {
  633. switch (run->mmio.len) {
  634. case 8: gpr = swab64(*(u64 *)run->mmio.data); break;
  635. case 4: gpr = swab32(*(u32 *)run->mmio.data); break;
  636. case 2: gpr = swab16(*(u16 *)run->mmio.data); break;
  637. case 1: gpr = *(u8 *)run->mmio.data; break;
  638. }
  639. }
  640. if (vcpu->arch.mmio_sign_extend) {
  641. switch (run->mmio.len) {
  642. #ifdef CONFIG_PPC64
  643. case 4:
  644. gpr = (s64)(s32)gpr;
  645. break;
  646. #endif
  647. case 2:
  648. gpr = (s64)(s16)gpr;
  649. break;
  650. case 1:
  651. gpr = (s64)(s8)gpr;
  652. break;
  653. }
  654. }
  655. kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
  656. switch (vcpu->arch.io_gpr & KVM_MMIO_REG_EXT_MASK) {
  657. case KVM_MMIO_REG_GPR:
  658. kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
  659. break;
  660. case KVM_MMIO_REG_FPR:
  661. VCPU_FPR(vcpu, vcpu->arch.io_gpr & KVM_MMIO_REG_MASK) = gpr;
  662. break;
  663. #ifdef CONFIG_PPC_BOOK3S
  664. case KVM_MMIO_REG_QPR:
  665. vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
  666. break;
  667. case KVM_MMIO_REG_FQPR:
  668. VCPU_FPR(vcpu, vcpu->arch.io_gpr & KVM_MMIO_REG_MASK) = gpr;
  669. vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
  670. break;
  671. #endif
  672. default:
  673. BUG();
  674. }
  675. }
  676. int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
  677. unsigned int rt, unsigned int bytes,
  678. int is_default_endian)
  679. {
  680. int idx, ret;
  681. bool host_swabbed;
  682. /* Pity C doesn't have a logical XOR operator */
  683. if (kvmppc_need_byteswap(vcpu)) {
  684. host_swabbed = is_default_endian;
  685. } else {
  686. host_swabbed = !is_default_endian;
  687. }
  688. if (bytes > sizeof(run->mmio.data)) {
  689. printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
  690. run->mmio.len);
  691. }
  692. run->mmio.phys_addr = vcpu->arch.paddr_accessed;
  693. run->mmio.len = bytes;
  694. run->mmio.is_write = 0;
  695. vcpu->arch.io_gpr = rt;
  696. vcpu->arch.mmio_host_swabbed = host_swabbed;
  697. vcpu->mmio_needed = 1;
  698. vcpu->mmio_is_write = 0;
  699. vcpu->arch.mmio_sign_extend = 0;
  700. idx = srcu_read_lock(&vcpu->kvm->srcu);
  701. ret = kvm_io_bus_read(vcpu, KVM_MMIO_BUS, run->mmio.phys_addr,
  702. bytes, &run->mmio.data);
  703. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  704. if (!ret) {
  705. kvmppc_complete_mmio_load(vcpu, run);
  706. vcpu->mmio_needed = 0;
  707. return EMULATE_DONE;
  708. }
  709. return EMULATE_DO_MMIO;
  710. }
  711. EXPORT_SYMBOL_GPL(kvmppc_handle_load);
  712. /* Same as above, but sign extends */
  713. int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu,
  714. unsigned int rt, unsigned int bytes,
  715. int is_default_endian)
  716. {
  717. int r;
  718. vcpu->arch.mmio_sign_extend = 1;
  719. r = kvmppc_handle_load(run, vcpu, rt, bytes, is_default_endian);
  720. return r;
  721. }
  722. int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
  723. u64 val, unsigned int bytes, int is_default_endian)
  724. {
  725. void *data = run->mmio.data;
  726. int idx, ret;
  727. bool host_swabbed;
  728. /* Pity C doesn't have a logical XOR operator */
  729. if (kvmppc_need_byteswap(vcpu)) {
  730. host_swabbed = is_default_endian;
  731. } else {
  732. host_swabbed = !is_default_endian;
  733. }
  734. if (bytes > sizeof(run->mmio.data)) {
  735. printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
  736. run->mmio.len);
  737. }
  738. run->mmio.phys_addr = vcpu->arch.paddr_accessed;
  739. run->mmio.len = bytes;
  740. run->mmio.is_write = 1;
  741. vcpu->mmio_needed = 1;
  742. vcpu->mmio_is_write = 1;
  743. /* Store the value at the lowest bytes in 'data'. */
  744. if (!host_swabbed) {
  745. switch (bytes) {
  746. case 8: *(u64 *)data = val; break;
  747. case 4: *(u32 *)data = val; break;
  748. case 2: *(u16 *)data = val; break;
  749. case 1: *(u8 *)data = val; break;
  750. }
  751. } else {
  752. switch (bytes) {
  753. case 8: *(u64 *)data = swab64(val); break;
  754. case 4: *(u32 *)data = swab32(val); break;
  755. case 2: *(u16 *)data = swab16(val); break;
  756. case 1: *(u8 *)data = val; break;
  757. }
  758. }
  759. idx = srcu_read_lock(&vcpu->kvm->srcu);
  760. ret = kvm_io_bus_write(vcpu, KVM_MMIO_BUS, run->mmio.phys_addr,
  761. bytes, &run->mmio.data);
  762. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  763. if (!ret) {
  764. vcpu->mmio_needed = 0;
  765. return EMULATE_DONE;
  766. }
  767. return EMULATE_DO_MMIO;
  768. }
  769. EXPORT_SYMBOL_GPL(kvmppc_handle_store);
  770. int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
  771. {
  772. int r = 0;
  773. union kvmppc_one_reg val;
  774. int size;
  775. size = one_reg_size(reg->id);
  776. if (size > sizeof(val))
  777. return -EINVAL;
  778. r = kvmppc_get_one_reg(vcpu, reg->id, &val);
  779. if (r == -EINVAL) {
  780. r = 0;
  781. switch (reg->id) {
  782. #ifdef CONFIG_ALTIVEC
  783. case KVM_REG_PPC_VR0 ... KVM_REG_PPC_VR31:
  784. if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
  785. r = -ENXIO;
  786. break;
  787. }
  788. vcpu->arch.vr.vr[reg->id - KVM_REG_PPC_VR0] = val.vval;
  789. break;
  790. case KVM_REG_PPC_VSCR:
  791. if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
  792. r = -ENXIO;
  793. break;
  794. }
  795. vcpu->arch.vr.vscr.u[3] = set_reg_val(reg->id, val);
  796. break;
  797. case KVM_REG_PPC_VRSAVE:
  798. if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
  799. r = -ENXIO;
  800. break;
  801. }
  802. vcpu->arch.vrsave = set_reg_val(reg->id, val);
  803. break;
  804. #endif /* CONFIG_ALTIVEC */
  805. default:
  806. r = -EINVAL;
  807. break;
  808. }
  809. }
  810. if (r)
  811. return r;
  812. if (copy_to_user((char __user *)(unsigned long)reg->addr, &val, size))
  813. r = -EFAULT;
  814. return r;
  815. }
  816. int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
  817. {
  818. int r;
  819. union kvmppc_one_reg val;
  820. int size;
  821. size = one_reg_size(reg->id);
  822. if (size > sizeof(val))
  823. return -EINVAL;
  824. if (copy_from_user(&val, (char __user *)(unsigned long)reg->addr, size))
  825. return -EFAULT;
  826. r = kvmppc_set_one_reg(vcpu, reg->id, &val);
  827. if (r == -EINVAL) {
  828. r = 0;
  829. switch (reg->id) {
  830. #ifdef CONFIG_ALTIVEC
  831. case KVM_REG_PPC_VR0 ... KVM_REG_PPC_VR31:
  832. if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
  833. r = -ENXIO;
  834. break;
  835. }
  836. val.vval = vcpu->arch.vr.vr[reg->id - KVM_REG_PPC_VR0];
  837. break;
  838. case KVM_REG_PPC_VSCR:
  839. if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
  840. r = -ENXIO;
  841. break;
  842. }
  843. val = get_reg_val(reg->id, vcpu->arch.vr.vscr.u[3]);
  844. break;
  845. case KVM_REG_PPC_VRSAVE:
  846. val = get_reg_val(reg->id, vcpu->arch.vrsave);
  847. break;
  848. #endif /* CONFIG_ALTIVEC */
  849. default:
  850. r = -EINVAL;
  851. break;
  852. }
  853. }
  854. return r;
  855. }
  856. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
  857. {
  858. int r;
  859. sigset_t sigsaved;
  860. if (vcpu->sigset_active)
  861. sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
  862. if (vcpu->mmio_needed) {
  863. if (!vcpu->mmio_is_write)
  864. kvmppc_complete_mmio_load(vcpu, run);
  865. vcpu->mmio_needed = 0;
  866. } else if (vcpu->arch.osi_needed) {
  867. u64 *gprs = run->osi.gprs;
  868. int i;
  869. for (i = 0; i < 32; i++)
  870. kvmppc_set_gpr(vcpu, i, gprs[i]);
  871. vcpu->arch.osi_needed = 0;
  872. } else if (vcpu->arch.hcall_needed) {
  873. int i;
  874. kvmppc_set_gpr(vcpu, 3, run->papr_hcall.ret);
  875. for (i = 0; i < 9; ++i)
  876. kvmppc_set_gpr(vcpu, 4 + i, run->papr_hcall.args[i]);
  877. vcpu->arch.hcall_needed = 0;
  878. #ifdef CONFIG_BOOKE
  879. } else if (vcpu->arch.epr_needed) {
  880. kvmppc_set_epr(vcpu, run->epr.epr);
  881. vcpu->arch.epr_needed = 0;
  882. #endif
  883. }
  884. r = kvmppc_vcpu_run(run, vcpu);
  885. if (vcpu->sigset_active)
  886. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  887. return r;
  888. }
  889. int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
  890. {
  891. if (irq->irq == KVM_INTERRUPT_UNSET) {
  892. kvmppc_core_dequeue_external(vcpu);
  893. return 0;
  894. }
  895. kvmppc_core_queue_external(vcpu, irq);
  896. kvm_vcpu_kick(vcpu);
  897. return 0;
  898. }
  899. static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
  900. struct kvm_enable_cap *cap)
  901. {
  902. int r;
  903. if (cap->flags)
  904. return -EINVAL;
  905. switch (cap->cap) {
  906. case KVM_CAP_PPC_OSI:
  907. r = 0;
  908. vcpu->arch.osi_enabled = true;
  909. break;
  910. case KVM_CAP_PPC_PAPR:
  911. r = 0;
  912. vcpu->arch.papr_enabled = true;
  913. break;
  914. case KVM_CAP_PPC_EPR:
  915. r = 0;
  916. if (cap->args[0])
  917. vcpu->arch.epr_flags |= KVMPPC_EPR_USER;
  918. else
  919. vcpu->arch.epr_flags &= ~KVMPPC_EPR_USER;
  920. break;
  921. #ifdef CONFIG_BOOKE
  922. case KVM_CAP_PPC_BOOKE_WATCHDOG:
  923. r = 0;
  924. vcpu->arch.watchdog_enabled = true;
  925. break;
  926. #endif
  927. #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
  928. case KVM_CAP_SW_TLB: {
  929. struct kvm_config_tlb cfg;
  930. void __user *user_ptr = (void __user *)(uintptr_t)cap->args[0];
  931. r = -EFAULT;
  932. if (copy_from_user(&cfg, user_ptr, sizeof(cfg)))
  933. break;
  934. r = kvm_vcpu_ioctl_config_tlb(vcpu, &cfg);
  935. break;
  936. }
  937. #endif
  938. #ifdef CONFIG_KVM_MPIC
  939. case KVM_CAP_IRQ_MPIC: {
  940. struct fd f;
  941. struct kvm_device *dev;
  942. r = -EBADF;
  943. f = fdget(cap->args[0]);
  944. if (!f.file)
  945. break;
  946. r = -EPERM;
  947. dev = kvm_device_from_filp(f.file);
  948. if (dev)
  949. r = kvmppc_mpic_connect_vcpu(dev, vcpu, cap->args[1]);
  950. fdput(f);
  951. break;
  952. }
  953. #endif
  954. #ifdef CONFIG_KVM_XICS
  955. case KVM_CAP_IRQ_XICS: {
  956. struct fd f;
  957. struct kvm_device *dev;
  958. r = -EBADF;
  959. f = fdget(cap->args[0]);
  960. if (!f.file)
  961. break;
  962. r = -EPERM;
  963. dev = kvm_device_from_filp(f.file);
  964. if (dev)
  965. r = kvmppc_xics_connect_vcpu(dev, vcpu, cap->args[1]);
  966. fdput(f);
  967. break;
  968. }
  969. #endif /* CONFIG_KVM_XICS */
  970. default:
  971. r = -EINVAL;
  972. break;
  973. }
  974. if (!r)
  975. r = kvmppc_sanity_check(vcpu);
  976. return r;
  977. }
  978. int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
  979. struct kvm_mp_state *mp_state)
  980. {
  981. return -EINVAL;
  982. }
  983. int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
  984. struct kvm_mp_state *mp_state)
  985. {
  986. return -EINVAL;
  987. }
  988. long kvm_arch_vcpu_ioctl(struct file *filp,
  989. unsigned int ioctl, unsigned long arg)
  990. {
  991. struct kvm_vcpu *vcpu = filp->private_data;
  992. void __user *argp = (void __user *)arg;
  993. long r;
  994. switch (ioctl) {
  995. case KVM_INTERRUPT: {
  996. struct kvm_interrupt irq;
  997. r = -EFAULT;
  998. if (copy_from_user(&irq, argp, sizeof(irq)))
  999. goto out;
  1000. r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
  1001. goto out;
  1002. }
  1003. case KVM_ENABLE_CAP:
  1004. {
  1005. struct kvm_enable_cap cap;
  1006. r = -EFAULT;
  1007. if (copy_from_user(&cap, argp, sizeof(cap)))
  1008. goto out;
  1009. r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
  1010. break;
  1011. }
  1012. case KVM_SET_ONE_REG:
  1013. case KVM_GET_ONE_REG:
  1014. {
  1015. struct kvm_one_reg reg;
  1016. r = -EFAULT;
  1017. if (copy_from_user(&reg, argp, sizeof(reg)))
  1018. goto out;
  1019. if (ioctl == KVM_SET_ONE_REG)
  1020. r = kvm_vcpu_ioctl_set_one_reg(vcpu, &reg);
  1021. else
  1022. r = kvm_vcpu_ioctl_get_one_reg(vcpu, &reg);
  1023. break;
  1024. }
  1025. #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
  1026. case KVM_DIRTY_TLB: {
  1027. struct kvm_dirty_tlb dirty;
  1028. r = -EFAULT;
  1029. if (copy_from_user(&dirty, argp, sizeof(dirty)))
  1030. goto out;
  1031. r = kvm_vcpu_ioctl_dirty_tlb(vcpu, &dirty);
  1032. break;
  1033. }
  1034. #endif
  1035. default:
  1036. r = -EINVAL;
  1037. }
  1038. out:
  1039. return r;
  1040. }
  1041. int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
  1042. {
  1043. return VM_FAULT_SIGBUS;
  1044. }
  1045. static int kvm_vm_ioctl_get_pvinfo(struct kvm_ppc_pvinfo *pvinfo)
  1046. {
  1047. u32 inst_nop = 0x60000000;
  1048. #ifdef CONFIG_KVM_BOOKE_HV
  1049. u32 inst_sc1 = 0x44000022;
  1050. pvinfo->hcall[0] = cpu_to_be32(inst_sc1);
  1051. pvinfo->hcall[1] = cpu_to_be32(inst_nop);
  1052. pvinfo->hcall[2] = cpu_to_be32(inst_nop);
  1053. pvinfo->hcall[3] = cpu_to_be32(inst_nop);
  1054. #else
  1055. u32 inst_lis = 0x3c000000;
  1056. u32 inst_ori = 0x60000000;
  1057. u32 inst_sc = 0x44000002;
  1058. u32 inst_imm_mask = 0xffff;
  1059. /*
  1060. * The hypercall to get into KVM from within guest context is as
  1061. * follows:
  1062. *
  1063. * lis r0, r0, KVM_SC_MAGIC_R0@h
  1064. * ori r0, KVM_SC_MAGIC_R0@l
  1065. * sc
  1066. * nop
  1067. */
  1068. pvinfo->hcall[0] = cpu_to_be32(inst_lis | ((KVM_SC_MAGIC_R0 >> 16) & inst_imm_mask));
  1069. pvinfo->hcall[1] = cpu_to_be32(inst_ori | (KVM_SC_MAGIC_R0 & inst_imm_mask));
  1070. pvinfo->hcall[2] = cpu_to_be32(inst_sc);
  1071. pvinfo->hcall[3] = cpu_to_be32(inst_nop);
  1072. #endif
  1073. pvinfo->flags = KVM_PPC_PVINFO_FLAGS_EV_IDLE;
  1074. return 0;
  1075. }
  1076. int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
  1077. bool line_status)
  1078. {
  1079. if (!irqchip_in_kernel(kvm))
  1080. return -ENXIO;
  1081. irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
  1082. irq_event->irq, irq_event->level,
  1083. line_status);
  1084. return 0;
  1085. }
  1086. static int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
  1087. struct kvm_enable_cap *cap)
  1088. {
  1089. int r;
  1090. if (cap->flags)
  1091. return -EINVAL;
  1092. switch (cap->cap) {
  1093. #ifdef CONFIG_KVM_BOOK3S_64_HANDLER
  1094. case KVM_CAP_PPC_ENABLE_HCALL: {
  1095. unsigned long hcall = cap->args[0];
  1096. r = -EINVAL;
  1097. if (hcall > MAX_HCALL_OPCODE || (hcall & 3) ||
  1098. cap->args[1] > 1)
  1099. break;
  1100. if (!kvmppc_book3s_hcall_implemented(kvm, hcall))
  1101. break;
  1102. if (cap->args[1])
  1103. set_bit(hcall / 4, kvm->arch.enabled_hcalls);
  1104. else
  1105. clear_bit(hcall / 4, kvm->arch.enabled_hcalls);
  1106. r = 0;
  1107. break;
  1108. }
  1109. #endif
  1110. default:
  1111. r = -EINVAL;
  1112. break;
  1113. }
  1114. return r;
  1115. }
  1116. long kvm_arch_vm_ioctl(struct file *filp,
  1117. unsigned int ioctl, unsigned long arg)
  1118. {
  1119. struct kvm *kvm __maybe_unused = filp->private_data;
  1120. void __user *argp = (void __user *)arg;
  1121. long r;
  1122. switch (ioctl) {
  1123. case KVM_PPC_GET_PVINFO: {
  1124. struct kvm_ppc_pvinfo pvinfo;
  1125. memset(&pvinfo, 0, sizeof(pvinfo));
  1126. r = kvm_vm_ioctl_get_pvinfo(&pvinfo);
  1127. if (copy_to_user(argp, &pvinfo, sizeof(pvinfo))) {
  1128. r = -EFAULT;
  1129. goto out;
  1130. }
  1131. break;
  1132. }
  1133. case KVM_ENABLE_CAP:
  1134. {
  1135. struct kvm_enable_cap cap;
  1136. r = -EFAULT;
  1137. if (copy_from_user(&cap, argp, sizeof(cap)))
  1138. goto out;
  1139. r = kvm_vm_ioctl_enable_cap(kvm, &cap);
  1140. break;
  1141. }
  1142. #ifdef CONFIG_PPC_BOOK3S_64
  1143. case KVM_CREATE_SPAPR_TCE: {
  1144. struct kvm_create_spapr_tce create_tce;
  1145. r = -EFAULT;
  1146. if (copy_from_user(&create_tce, argp, sizeof(create_tce)))
  1147. goto out;
  1148. r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce);
  1149. goto out;
  1150. }
  1151. case KVM_PPC_GET_SMMU_INFO: {
  1152. struct kvm_ppc_smmu_info info;
  1153. struct kvm *kvm = filp->private_data;
  1154. memset(&info, 0, sizeof(info));
  1155. r = kvm->arch.kvm_ops->get_smmu_info(kvm, &info);
  1156. if (r >= 0 && copy_to_user(argp, &info, sizeof(info)))
  1157. r = -EFAULT;
  1158. break;
  1159. }
  1160. case KVM_PPC_RTAS_DEFINE_TOKEN: {
  1161. struct kvm *kvm = filp->private_data;
  1162. r = kvm_vm_ioctl_rtas_define_token(kvm, argp);
  1163. break;
  1164. }
  1165. default: {
  1166. struct kvm *kvm = filp->private_data;
  1167. r = kvm->arch.kvm_ops->arch_vm_ioctl(filp, ioctl, arg);
  1168. }
  1169. #else /* CONFIG_PPC_BOOK3S_64 */
  1170. default:
  1171. r = -ENOTTY;
  1172. #endif
  1173. }
  1174. out:
  1175. return r;
  1176. }
  1177. static unsigned long lpid_inuse[BITS_TO_LONGS(KVMPPC_NR_LPIDS)];
  1178. static unsigned long nr_lpids;
  1179. long kvmppc_alloc_lpid(void)
  1180. {
  1181. long lpid;
  1182. do {
  1183. lpid = find_first_zero_bit(lpid_inuse, KVMPPC_NR_LPIDS);
  1184. if (lpid >= nr_lpids) {
  1185. pr_err("%s: No LPIDs free\n", __func__);
  1186. return -ENOMEM;
  1187. }
  1188. } while (test_and_set_bit(lpid, lpid_inuse));
  1189. return lpid;
  1190. }
  1191. EXPORT_SYMBOL_GPL(kvmppc_alloc_lpid);
  1192. void kvmppc_claim_lpid(long lpid)
  1193. {
  1194. set_bit(lpid, lpid_inuse);
  1195. }
  1196. EXPORT_SYMBOL_GPL(kvmppc_claim_lpid);
  1197. void kvmppc_free_lpid(long lpid)
  1198. {
  1199. clear_bit(lpid, lpid_inuse);
  1200. }
  1201. EXPORT_SYMBOL_GPL(kvmppc_free_lpid);
  1202. void kvmppc_init_lpid(unsigned long nr_lpids_param)
  1203. {
  1204. nr_lpids = min_t(unsigned long, KVMPPC_NR_LPIDS, nr_lpids_param);
  1205. memset(lpid_inuse, 0, sizeof(lpid_inuse));
  1206. }
  1207. EXPORT_SYMBOL_GPL(kvmppc_init_lpid);
  1208. int kvm_arch_init(void *opaque)
  1209. {
  1210. return 0;
  1211. }
  1212. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ppc_instr);