powerpc.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278
  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_instruction(run, 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, false, &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 kvm_arch_hardware_enable(void *garbage)
  271. {
  272. return 0;
  273. }
  274. void kvm_arch_hardware_disable(void *garbage)
  275. {
  276. }
  277. int kvm_arch_hardware_setup(void)
  278. {
  279. return 0;
  280. }
  281. void kvm_arch_hardware_unsetup(void)
  282. {
  283. }
  284. void kvm_arch_check_processor_compat(void *rtn)
  285. {
  286. *(int *)rtn = kvmppc_core_check_processor_compat();
  287. }
  288. int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
  289. {
  290. struct kvmppc_ops *kvm_ops = NULL;
  291. /*
  292. * if we have both HV and PR enabled, default is HV
  293. */
  294. if (type == 0) {
  295. if (kvmppc_hv_ops)
  296. kvm_ops = kvmppc_hv_ops;
  297. else
  298. kvm_ops = kvmppc_pr_ops;
  299. if (!kvm_ops)
  300. goto err_out;
  301. } else if (type == KVM_VM_PPC_HV) {
  302. if (!kvmppc_hv_ops)
  303. goto err_out;
  304. kvm_ops = kvmppc_hv_ops;
  305. } else if (type == KVM_VM_PPC_PR) {
  306. if (!kvmppc_pr_ops)
  307. goto err_out;
  308. kvm_ops = kvmppc_pr_ops;
  309. } else
  310. goto err_out;
  311. if (kvm_ops->owner && !try_module_get(kvm_ops->owner))
  312. return -ENOENT;
  313. kvm->arch.kvm_ops = kvm_ops;
  314. return kvmppc_core_init_vm(kvm);
  315. err_out:
  316. return -EINVAL;
  317. }
  318. void kvm_arch_destroy_vm(struct kvm *kvm)
  319. {
  320. unsigned int i;
  321. struct kvm_vcpu *vcpu;
  322. kvm_for_each_vcpu(i, vcpu, kvm)
  323. kvm_arch_vcpu_free(vcpu);
  324. mutex_lock(&kvm->lock);
  325. for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
  326. kvm->vcpus[i] = NULL;
  327. atomic_set(&kvm->online_vcpus, 0);
  328. kvmppc_core_destroy_vm(kvm);
  329. mutex_unlock(&kvm->lock);
  330. /* drop the module reference */
  331. module_put(kvm->arch.kvm_ops->owner);
  332. }
  333. void kvm_arch_sync_events(struct kvm *kvm)
  334. {
  335. }
  336. int kvm_dev_ioctl_check_extension(long ext)
  337. {
  338. int r;
  339. /* FIXME!!
  340. * Should some of this be vm ioctl ? is it possible now ?
  341. */
  342. int hv_enabled = kvmppc_hv_ops ? 1 : 0;
  343. switch (ext) {
  344. #ifdef CONFIG_BOOKE
  345. case KVM_CAP_PPC_BOOKE_SREGS:
  346. case KVM_CAP_PPC_BOOKE_WATCHDOG:
  347. case KVM_CAP_PPC_EPR:
  348. #else
  349. case KVM_CAP_PPC_SEGSTATE:
  350. case KVM_CAP_PPC_HIOR:
  351. case KVM_CAP_PPC_PAPR:
  352. #endif
  353. case KVM_CAP_PPC_UNSET_IRQ:
  354. case KVM_CAP_PPC_IRQ_LEVEL:
  355. case KVM_CAP_ENABLE_CAP:
  356. case KVM_CAP_ENABLE_CAP_VM:
  357. case KVM_CAP_ONE_REG:
  358. case KVM_CAP_IOEVENTFD:
  359. case KVM_CAP_DEVICE_CTRL:
  360. r = 1;
  361. break;
  362. case KVM_CAP_PPC_PAIRED_SINGLES:
  363. case KVM_CAP_PPC_OSI:
  364. case KVM_CAP_PPC_GET_PVINFO:
  365. #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
  366. case KVM_CAP_SW_TLB:
  367. #endif
  368. /* We support this only for PR */
  369. r = !hv_enabled;
  370. break;
  371. #ifdef CONFIG_KVM_MMIO
  372. case KVM_CAP_COALESCED_MMIO:
  373. r = KVM_COALESCED_MMIO_PAGE_OFFSET;
  374. break;
  375. #endif
  376. #ifdef CONFIG_KVM_MPIC
  377. case KVM_CAP_IRQ_MPIC:
  378. r = 1;
  379. break;
  380. #endif
  381. #ifdef CONFIG_PPC_BOOK3S_64
  382. case KVM_CAP_SPAPR_TCE:
  383. case KVM_CAP_PPC_ALLOC_HTAB:
  384. case KVM_CAP_PPC_RTAS:
  385. case KVM_CAP_PPC_FIXUP_HCALL:
  386. case KVM_CAP_PPC_ENABLE_HCALL:
  387. #ifdef CONFIG_KVM_XICS
  388. case KVM_CAP_IRQ_XICS:
  389. #endif
  390. r = 1;
  391. break;
  392. #endif /* CONFIG_PPC_BOOK3S_64 */
  393. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  394. case KVM_CAP_PPC_SMT:
  395. if (hv_enabled)
  396. r = threads_per_subcore;
  397. else
  398. r = 0;
  399. break;
  400. case KVM_CAP_PPC_RMA:
  401. r = hv_enabled;
  402. /* PPC970 requires an RMA */
  403. if (r && cpu_has_feature(CPU_FTR_ARCH_201))
  404. r = 2;
  405. break;
  406. #endif
  407. case KVM_CAP_SYNC_MMU:
  408. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  409. if (hv_enabled)
  410. r = cpu_has_feature(CPU_FTR_ARCH_206) ? 1 : 0;
  411. else
  412. r = 0;
  413. #elif defined(KVM_ARCH_WANT_MMU_NOTIFIER)
  414. r = 1;
  415. #else
  416. r = 0;
  417. #endif
  418. break;
  419. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  420. case KVM_CAP_PPC_HTAB_FD:
  421. r = hv_enabled;
  422. break;
  423. #endif
  424. case KVM_CAP_NR_VCPUS:
  425. /*
  426. * Recommending a number of CPUs is somewhat arbitrary; we
  427. * return the number of present CPUs for -HV (since a host
  428. * will have secondary threads "offline"), and for other KVM
  429. * implementations just count online CPUs.
  430. */
  431. if (hv_enabled)
  432. r = num_present_cpus();
  433. else
  434. r = num_online_cpus();
  435. break;
  436. case KVM_CAP_MAX_VCPUS:
  437. r = KVM_MAX_VCPUS;
  438. break;
  439. #ifdef CONFIG_PPC_BOOK3S_64
  440. case KVM_CAP_PPC_GET_SMMU_INFO:
  441. r = 1;
  442. break;
  443. #endif
  444. default:
  445. r = 0;
  446. break;
  447. }
  448. return r;
  449. }
  450. long kvm_arch_dev_ioctl(struct file *filp,
  451. unsigned int ioctl, unsigned long arg)
  452. {
  453. return -EINVAL;
  454. }
  455. void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
  456. struct kvm_memory_slot *dont)
  457. {
  458. kvmppc_core_free_memslot(kvm, free, dont);
  459. }
  460. int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
  461. unsigned long npages)
  462. {
  463. return kvmppc_core_create_memslot(kvm, slot, npages);
  464. }
  465. void kvm_arch_memslots_updated(struct kvm *kvm)
  466. {
  467. }
  468. int kvm_arch_prepare_memory_region(struct kvm *kvm,
  469. struct kvm_memory_slot *memslot,
  470. struct kvm_userspace_memory_region *mem,
  471. enum kvm_mr_change change)
  472. {
  473. return kvmppc_core_prepare_memory_region(kvm, memslot, mem);
  474. }
  475. void kvm_arch_commit_memory_region(struct kvm *kvm,
  476. struct kvm_userspace_memory_region *mem,
  477. const struct kvm_memory_slot *old,
  478. enum kvm_mr_change change)
  479. {
  480. kvmppc_core_commit_memory_region(kvm, mem, old);
  481. }
  482. void kvm_arch_flush_shadow_all(struct kvm *kvm)
  483. {
  484. }
  485. void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
  486. struct kvm_memory_slot *slot)
  487. {
  488. kvmppc_core_flush_memslot(kvm, slot);
  489. }
  490. struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
  491. {
  492. struct kvm_vcpu *vcpu;
  493. vcpu = kvmppc_core_vcpu_create(kvm, id);
  494. if (!IS_ERR(vcpu)) {
  495. vcpu->arch.wqp = &vcpu->wq;
  496. kvmppc_create_vcpu_debugfs(vcpu, id);
  497. }
  498. return vcpu;
  499. }
  500. int kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
  501. {
  502. return 0;
  503. }
  504. void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
  505. {
  506. /* Make sure we're not using the vcpu anymore */
  507. hrtimer_cancel(&vcpu->arch.dec_timer);
  508. tasklet_kill(&vcpu->arch.tasklet);
  509. kvmppc_remove_vcpu_debugfs(vcpu);
  510. switch (vcpu->arch.irq_type) {
  511. case KVMPPC_IRQ_MPIC:
  512. kvmppc_mpic_disconnect_vcpu(vcpu->arch.mpic, vcpu);
  513. break;
  514. case KVMPPC_IRQ_XICS:
  515. kvmppc_xics_free_icp(vcpu);
  516. break;
  517. }
  518. kvmppc_core_vcpu_free(vcpu);
  519. }
  520. void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
  521. {
  522. kvm_arch_vcpu_free(vcpu);
  523. }
  524. int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
  525. {
  526. return kvmppc_core_pending_dec(vcpu);
  527. }
  528. /*
  529. * low level hrtimer wake routine. Because this runs in hardirq context
  530. * we schedule a tasklet to do the real work.
  531. */
  532. enum hrtimer_restart kvmppc_decrementer_wakeup(struct hrtimer *timer)
  533. {
  534. struct kvm_vcpu *vcpu;
  535. vcpu = container_of(timer, struct kvm_vcpu, arch.dec_timer);
  536. tasklet_schedule(&vcpu->arch.tasklet);
  537. return HRTIMER_NORESTART;
  538. }
  539. int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
  540. {
  541. int ret;
  542. hrtimer_init(&vcpu->arch.dec_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
  543. tasklet_init(&vcpu->arch.tasklet, kvmppc_decrementer_func, (ulong)vcpu);
  544. vcpu->arch.dec_timer.function = kvmppc_decrementer_wakeup;
  545. vcpu->arch.dec_expires = ~(u64)0;
  546. #ifdef CONFIG_KVM_EXIT_TIMING
  547. mutex_init(&vcpu->arch.exit_timing_lock);
  548. #endif
  549. ret = kvmppc_subarch_vcpu_init(vcpu);
  550. return ret;
  551. }
  552. void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
  553. {
  554. kvmppc_mmu_destroy(vcpu);
  555. kvmppc_subarch_vcpu_uninit(vcpu);
  556. }
  557. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  558. {
  559. #ifdef CONFIG_BOOKE
  560. /*
  561. * vrsave (formerly usprg0) isn't used by Linux, but may
  562. * be used by the guest.
  563. *
  564. * On non-booke this is associated with Altivec and
  565. * is handled by code in book3s.c.
  566. */
  567. mtspr(SPRN_VRSAVE, vcpu->arch.vrsave);
  568. #endif
  569. kvmppc_core_vcpu_load(vcpu, cpu);
  570. }
  571. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
  572. {
  573. kvmppc_core_vcpu_put(vcpu);
  574. #ifdef CONFIG_BOOKE
  575. vcpu->arch.vrsave = mfspr(SPRN_VRSAVE);
  576. #endif
  577. }
  578. static void kvmppc_complete_dcr_load(struct kvm_vcpu *vcpu,
  579. struct kvm_run *run)
  580. {
  581. kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, run->dcr.data);
  582. }
  583. static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu,
  584. struct kvm_run *run)
  585. {
  586. u64 uninitialized_var(gpr);
  587. if (run->mmio.len > sizeof(gpr)) {
  588. printk(KERN_ERR "bad MMIO length: %d\n", run->mmio.len);
  589. return;
  590. }
  591. if (vcpu->arch.mmio_is_bigendian) {
  592. switch (run->mmio.len) {
  593. case 8: gpr = *(u64 *)run->mmio.data; break;
  594. case 4: gpr = *(u32 *)run->mmio.data; break;
  595. case 2: gpr = *(u16 *)run->mmio.data; break;
  596. case 1: gpr = *(u8 *)run->mmio.data; break;
  597. }
  598. } else {
  599. /* Convert BE data from userland back to LE. */
  600. switch (run->mmio.len) {
  601. case 4: gpr = ld_le32((u32 *)run->mmio.data); break;
  602. case 2: gpr = ld_le16((u16 *)run->mmio.data); break;
  603. case 1: gpr = *(u8 *)run->mmio.data; break;
  604. }
  605. }
  606. if (vcpu->arch.mmio_sign_extend) {
  607. switch (run->mmio.len) {
  608. #ifdef CONFIG_PPC64
  609. case 4:
  610. gpr = (s64)(s32)gpr;
  611. break;
  612. #endif
  613. case 2:
  614. gpr = (s64)(s16)gpr;
  615. break;
  616. case 1:
  617. gpr = (s64)(s8)gpr;
  618. break;
  619. }
  620. }
  621. kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
  622. switch (vcpu->arch.io_gpr & KVM_MMIO_REG_EXT_MASK) {
  623. case KVM_MMIO_REG_GPR:
  624. kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
  625. break;
  626. case KVM_MMIO_REG_FPR:
  627. VCPU_FPR(vcpu, vcpu->arch.io_gpr & KVM_MMIO_REG_MASK) = gpr;
  628. break;
  629. #ifdef CONFIG_PPC_BOOK3S
  630. case KVM_MMIO_REG_QPR:
  631. vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
  632. break;
  633. case KVM_MMIO_REG_FQPR:
  634. VCPU_FPR(vcpu, vcpu->arch.io_gpr & KVM_MMIO_REG_MASK) = gpr;
  635. vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
  636. break;
  637. #endif
  638. default:
  639. BUG();
  640. }
  641. }
  642. int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
  643. unsigned int rt, unsigned int bytes,
  644. int is_default_endian)
  645. {
  646. int idx, ret;
  647. int is_bigendian;
  648. if (kvmppc_need_byteswap(vcpu)) {
  649. /* Default endianness is "little endian". */
  650. is_bigendian = !is_default_endian;
  651. } else {
  652. /* Default endianness is "big endian". */
  653. is_bigendian = is_default_endian;
  654. }
  655. if (bytes > sizeof(run->mmio.data)) {
  656. printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
  657. run->mmio.len);
  658. }
  659. run->mmio.phys_addr = vcpu->arch.paddr_accessed;
  660. run->mmio.len = bytes;
  661. run->mmio.is_write = 0;
  662. vcpu->arch.io_gpr = rt;
  663. vcpu->arch.mmio_is_bigendian = is_bigendian;
  664. vcpu->mmio_needed = 1;
  665. vcpu->mmio_is_write = 0;
  666. vcpu->arch.mmio_sign_extend = 0;
  667. idx = srcu_read_lock(&vcpu->kvm->srcu);
  668. ret = kvm_io_bus_read(vcpu->kvm, KVM_MMIO_BUS, run->mmio.phys_addr,
  669. bytes, &run->mmio.data);
  670. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  671. if (!ret) {
  672. kvmppc_complete_mmio_load(vcpu, run);
  673. vcpu->mmio_needed = 0;
  674. return EMULATE_DONE;
  675. }
  676. return EMULATE_DO_MMIO;
  677. }
  678. EXPORT_SYMBOL_GPL(kvmppc_handle_load);
  679. /* Same as above, but sign extends */
  680. int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu,
  681. unsigned int rt, unsigned int bytes,
  682. int is_default_endian)
  683. {
  684. int r;
  685. vcpu->arch.mmio_sign_extend = 1;
  686. r = kvmppc_handle_load(run, vcpu, rt, bytes, is_default_endian);
  687. return r;
  688. }
  689. int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
  690. u64 val, unsigned int bytes, int is_default_endian)
  691. {
  692. void *data = run->mmio.data;
  693. int idx, ret;
  694. int is_bigendian;
  695. if (kvmppc_need_byteswap(vcpu)) {
  696. /* Default endianness is "little endian". */
  697. is_bigendian = !is_default_endian;
  698. } else {
  699. /* Default endianness is "big endian". */
  700. is_bigendian = is_default_endian;
  701. }
  702. if (bytes > sizeof(run->mmio.data)) {
  703. printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
  704. run->mmio.len);
  705. }
  706. run->mmio.phys_addr = vcpu->arch.paddr_accessed;
  707. run->mmio.len = bytes;
  708. run->mmio.is_write = 1;
  709. vcpu->mmio_needed = 1;
  710. vcpu->mmio_is_write = 1;
  711. /* Store the value at the lowest bytes in 'data'. */
  712. if (is_bigendian) {
  713. switch (bytes) {
  714. case 8: *(u64 *)data = val; break;
  715. case 4: *(u32 *)data = val; break;
  716. case 2: *(u16 *)data = val; break;
  717. case 1: *(u8 *)data = val; break;
  718. }
  719. } else {
  720. /* Store LE value into 'data'. */
  721. switch (bytes) {
  722. case 4: st_le32(data, val); break;
  723. case 2: st_le16(data, val); break;
  724. case 1: *(u8 *)data = val; break;
  725. }
  726. }
  727. idx = srcu_read_lock(&vcpu->kvm->srcu);
  728. ret = kvm_io_bus_write(vcpu->kvm, KVM_MMIO_BUS, run->mmio.phys_addr,
  729. bytes, &run->mmio.data);
  730. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  731. if (!ret) {
  732. vcpu->mmio_needed = 0;
  733. return EMULATE_DONE;
  734. }
  735. return EMULATE_DO_MMIO;
  736. }
  737. EXPORT_SYMBOL_GPL(kvmppc_handle_store);
  738. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
  739. {
  740. int r;
  741. sigset_t sigsaved;
  742. if (vcpu->sigset_active)
  743. sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
  744. if (vcpu->mmio_needed) {
  745. if (!vcpu->mmio_is_write)
  746. kvmppc_complete_mmio_load(vcpu, run);
  747. vcpu->mmio_needed = 0;
  748. } else if (vcpu->arch.dcr_needed) {
  749. if (!vcpu->arch.dcr_is_write)
  750. kvmppc_complete_dcr_load(vcpu, run);
  751. vcpu->arch.dcr_needed = 0;
  752. } else if (vcpu->arch.osi_needed) {
  753. u64 *gprs = run->osi.gprs;
  754. int i;
  755. for (i = 0; i < 32; i++)
  756. kvmppc_set_gpr(vcpu, i, gprs[i]);
  757. vcpu->arch.osi_needed = 0;
  758. } else if (vcpu->arch.hcall_needed) {
  759. int i;
  760. kvmppc_set_gpr(vcpu, 3, run->papr_hcall.ret);
  761. for (i = 0; i < 9; ++i)
  762. kvmppc_set_gpr(vcpu, 4 + i, run->papr_hcall.args[i]);
  763. vcpu->arch.hcall_needed = 0;
  764. #ifdef CONFIG_BOOKE
  765. } else if (vcpu->arch.epr_needed) {
  766. kvmppc_set_epr(vcpu, run->epr.epr);
  767. vcpu->arch.epr_needed = 0;
  768. #endif
  769. }
  770. r = kvmppc_vcpu_run(run, vcpu);
  771. if (vcpu->sigset_active)
  772. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  773. return r;
  774. }
  775. int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
  776. {
  777. if (irq->irq == KVM_INTERRUPT_UNSET) {
  778. kvmppc_core_dequeue_external(vcpu);
  779. return 0;
  780. }
  781. kvmppc_core_queue_external(vcpu, irq);
  782. kvm_vcpu_kick(vcpu);
  783. return 0;
  784. }
  785. static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
  786. struct kvm_enable_cap *cap)
  787. {
  788. int r;
  789. if (cap->flags)
  790. return -EINVAL;
  791. switch (cap->cap) {
  792. case KVM_CAP_PPC_OSI:
  793. r = 0;
  794. vcpu->arch.osi_enabled = true;
  795. break;
  796. case KVM_CAP_PPC_PAPR:
  797. r = 0;
  798. vcpu->arch.papr_enabled = true;
  799. break;
  800. case KVM_CAP_PPC_EPR:
  801. r = 0;
  802. if (cap->args[0])
  803. vcpu->arch.epr_flags |= KVMPPC_EPR_USER;
  804. else
  805. vcpu->arch.epr_flags &= ~KVMPPC_EPR_USER;
  806. break;
  807. #ifdef CONFIG_BOOKE
  808. case KVM_CAP_PPC_BOOKE_WATCHDOG:
  809. r = 0;
  810. vcpu->arch.watchdog_enabled = true;
  811. break;
  812. #endif
  813. #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
  814. case KVM_CAP_SW_TLB: {
  815. struct kvm_config_tlb cfg;
  816. void __user *user_ptr = (void __user *)(uintptr_t)cap->args[0];
  817. r = -EFAULT;
  818. if (copy_from_user(&cfg, user_ptr, sizeof(cfg)))
  819. break;
  820. r = kvm_vcpu_ioctl_config_tlb(vcpu, &cfg);
  821. break;
  822. }
  823. #endif
  824. #ifdef CONFIG_KVM_MPIC
  825. case KVM_CAP_IRQ_MPIC: {
  826. struct fd f;
  827. struct kvm_device *dev;
  828. r = -EBADF;
  829. f = fdget(cap->args[0]);
  830. if (!f.file)
  831. break;
  832. r = -EPERM;
  833. dev = kvm_device_from_filp(f.file);
  834. if (dev)
  835. r = kvmppc_mpic_connect_vcpu(dev, vcpu, cap->args[1]);
  836. fdput(f);
  837. break;
  838. }
  839. #endif
  840. #ifdef CONFIG_KVM_XICS
  841. case KVM_CAP_IRQ_XICS: {
  842. struct fd f;
  843. struct kvm_device *dev;
  844. r = -EBADF;
  845. f = fdget(cap->args[0]);
  846. if (!f.file)
  847. break;
  848. r = -EPERM;
  849. dev = kvm_device_from_filp(f.file);
  850. if (dev)
  851. r = kvmppc_xics_connect_vcpu(dev, vcpu, cap->args[1]);
  852. fdput(f);
  853. break;
  854. }
  855. #endif /* CONFIG_KVM_XICS */
  856. default:
  857. r = -EINVAL;
  858. break;
  859. }
  860. if (!r)
  861. r = kvmppc_sanity_check(vcpu);
  862. return r;
  863. }
  864. int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
  865. struct kvm_mp_state *mp_state)
  866. {
  867. return -EINVAL;
  868. }
  869. int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
  870. struct kvm_mp_state *mp_state)
  871. {
  872. return -EINVAL;
  873. }
  874. long kvm_arch_vcpu_ioctl(struct file *filp,
  875. unsigned int ioctl, unsigned long arg)
  876. {
  877. struct kvm_vcpu *vcpu = filp->private_data;
  878. void __user *argp = (void __user *)arg;
  879. long r;
  880. switch (ioctl) {
  881. case KVM_INTERRUPT: {
  882. struct kvm_interrupt irq;
  883. r = -EFAULT;
  884. if (copy_from_user(&irq, argp, sizeof(irq)))
  885. goto out;
  886. r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
  887. goto out;
  888. }
  889. case KVM_ENABLE_CAP:
  890. {
  891. struct kvm_enable_cap cap;
  892. r = -EFAULT;
  893. if (copy_from_user(&cap, argp, sizeof(cap)))
  894. goto out;
  895. r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
  896. break;
  897. }
  898. case KVM_SET_ONE_REG:
  899. case KVM_GET_ONE_REG:
  900. {
  901. struct kvm_one_reg reg;
  902. r = -EFAULT;
  903. if (copy_from_user(&reg, argp, sizeof(reg)))
  904. goto out;
  905. if (ioctl == KVM_SET_ONE_REG)
  906. r = kvm_vcpu_ioctl_set_one_reg(vcpu, &reg);
  907. else
  908. r = kvm_vcpu_ioctl_get_one_reg(vcpu, &reg);
  909. break;
  910. }
  911. #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
  912. case KVM_DIRTY_TLB: {
  913. struct kvm_dirty_tlb dirty;
  914. r = -EFAULT;
  915. if (copy_from_user(&dirty, argp, sizeof(dirty)))
  916. goto out;
  917. r = kvm_vcpu_ioctl_dirty_tlb(vcpu, &dirty);
  918. break;
  919. }
  920. #endif
  921. default:
  922. r = -EINVAL;
  923. }
  924. out:
  925. return r;
  926. }
  927. int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
  928. {
  929. return VM_FAULT_SIGBUS;
  930. }
  931. static int kvm_vm_ioctl_get_pvinfo(struct kvm_ppc_pvinfo *pvinfo)
  932. {
  933. u32 inst_nop = 0x60000000;
  934. #ifdef CONFIG_KVM_BOOKE_HV
  935. u32 inst_sc1 = 0x44000022;
  936. pvinfo->hcall[0] = cpu_to_be32(inst_sc1);
  937. pvinfo->hcall[1] = cpu_to_be32(inst_nop);
  938. pvinfo->hcall[2] = cpu_to_be32(inst_nop);
  939. pvinfo->hcall[3] = cpu_to_be32(inst_nop);
  940. #else
  941. u32 inst_lis = 0x3c000000;
  942. u32 inst_ori = 0x60000000;
  943. u32 inst_sc = 0x44000002;
  944. u32 inst_imm_mask = 0xffff;
  945. /*
  946. * The hypercall to get into KVM from within guest context is as
  947. * follows:
  948. *
  949. * lis r0, r0, KVM_SC_MAGIC_R0@h
  950. * ori r0, KVM_SC_MAGIC_R0@l
  951. * sc
  952. * nop
  953. */
  954. pvinfo->hcall[0] = cpu_to_be32(inst_lis | ((KVM_SC_MAGIC_R0 >> 16) & inst_imm_mask));
  955. pvinfo->hcall[1] = cpu_to_be32(inst_ori | (KVM_SC_MAGIC_R0 & inst_imm_mask));
  956. pvinfo->hcall[2] = cpu_to_be32(inst_sc);
  957. pvinfo->hcall[3] = cpu_to_be32(inst_nop);
  958. #endif
  959. pvinfo->flags = KVM_PPC_PVINFO_FLAGS_EV_IDLE;
  960. return 0;
  961. }
  962. int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
  963. bool line_status)
  964. {
  965. if (!irqchip_in_kernel(kvm))
  966. return -ENXIO;
  967. irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
  968. irq_event->irq, irq_event->level,
  969. line_status);
  970. return 0;
  971. }
  972. static int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
  973. struct kvm_enable_cap *cap)
  974. {
  975. int r;
  976. if (cap->flags)
  977. return -EINVAL;
  978. switch (cap->cap) {
  979. #ifdef CONFIG_KVM_BOOK3S_64_HANDLER
  980. case KVM_CAP_PPC_ENABLE_HCALL: {
  981. unsigned long hcall = cap->args[0];
  982. r = -EINVAL;
  983. if (hcall > MAX_HCALL_OPCODE || (hcall & 3) ||
  984. cap->args[1] > 1)
  985. break;
  986. if (!kvmppc_book3s_hcall_implemented(kvm, hcall))
  987. break;
  988. if (cap->args[1])
  989. set_bit(hcall / 4, kvm->arch.enabled_hcalls);
  990. else
  991. clear_bit(hcall / 4, kvm->arch.enabled_hcalls);
  992. r = 0;
  993. break;
  994. }
  995. #endif
  996. default:
  997. r = -EINVAL;
  998. break;
  999. }
  1000. return r;
  1001. }
  1002. long kvm_arch_vm_ioctl(struct file *filp,
  1003. unsigned int ioctl, unsigned long arg)
  1004. {
  1005. struct kvm *kvm __maybe_unused = filp->private_data;
  1006. void __user *argp = (void __user *)arg;
  1007. long r;
  1008. switch (ioctl) {
  1009. case KVM_PPC_GET_PVINFO: {
  1010. struct kvm_ppc_pvinfo pvinfo;
  1011. memset(&pvinfo, 0, sizeof(pvinfo));
  1012. r = kvm_vm_ioctl_get_pvinfo(&pvinfo);
  1013. if (copy_to_user(argp, &pvinfo, sizeof(pvinfo))) {
  1014. r = -EFAULT;
  1015. goto out;
  1016. }
  1017. break;
  1018. }
  1019. case KVM_ENABLE_CAP:
  1020. {
  1021. struct kvm_enable_cap cap;
  1022. r = -EFAULT;
  1023. if (copy_from_user(&cap, argp, sizeof(cap)))
  1024. goto out;
  1025. r = kvm_vm_ioctl_enable_cap(kvm, &cap);
  1026. break;
  1027. }
  1028. #ifdef CONFIG_PPC_BOOK3S_64
  1029. case KVM_CREATE_SPAPR_TCE: {
  1030. struct kvm_create_spapr_tce create_tce;
  1031. r = -EFAULT;
  1032. if (copy_from_user(&create_tce, argp, sizeof(create_tce)))
  1033. goto out;
  1034. r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce);
  1035. goto out;
  1036. }
  1037. case KVM_PPC_GET_SMMU_INFO: {
  1038. struct kvm_ppc_smmu_info info;
  1039. struct kvm *kvm = filp->private_data;
  1040. memset(&info, 0, sizeof(info));
  1041. r = kvm->arch.kvm_ops->get_smmu_info(kvm, &info);
  1042. if (r >= 0 && copy_to_user(argp, &info, sizeof(info)))
  1043. r = -EFAULT;
  1044. break;
  1045. }
  1046. case KVM_PPC_RTAS_DEFINE_TOKEN: {
  1047. struct kvm *kvm = filp->private_data;
  1048. r = kvm_vm_ioctl_rtas_define_token(kvm, argp);
  1049. break;
  1050. }
  1051. default: {
  1052. struct kvm *kvm = filp->private_data;
  1053. r = kvm->arch.kvm_ops->arch_vm_ioctl(filp, ioctl, arg);
  1054. }
  1055. #else /* CONFIG_PPC_BOOK3S_64 */
  1056. default:
  1057. r = -ENOTTY;
  1058. #endif
  1059. }
  1060. out:
  1061. return r;
  1062. }
  1063. static unsigned long lpid_inuse[BITS_TO_LONGS(KVMPPC_NR_LPIDS)];
  1064. static unsigned long nr_lpids;
  1065. long kvmppc_alloc_lpid(void)
  1066. {
  1067. long lpid;
  1068. do {
  1069. lpid = find_first_zero_bit(lpid_inuse, KVMPPC_NR_LPIDS);
  1070. if (lpid >= nr_lpids) {
  1071. pr_err("%s: No LPIDs free\n", __func__);
  1072. return -ENOMEM;
  1073. }
  1074. } while (test_and_set_bit(lpid, lpid_inuse));
  1075. return lpid;
  1076. }
  1077. EXPORT_SYMBOL_GPL(kvmppc_alloc_lpid);
  1078. void kvmppc_claim_lpid(long lpid)
  1079. {
  1080. set_bit(lpid, lpid_inuse);
  1081. }
  1082. EXPORT_SYMBOL_GPL(kvmppc_claim_lpid);
  1083. void kvmppc_free_lpid(long lpid)
  1084. {
  1085. clear_bit(lpid, lpid_inuse);
  1086. }
  1087. EXPORT_SYMBOL_GPL(kvmppc_free_lpid);
  1088. void kvmppc_init_lpid(unsigned long nr_lpids_param)
  1089. {
  1090. nr_lpids = min_t(unsigned long, KVMPPC_NR_LPIDS, nr_lpids_param);
  1091. memset(lpid_inuse, 0, sizeof(lpid_inuse));
  1092. }
  1093. EXPORT_SYMBOL_GPL(kvmppc_init_lpid);
  1094. int kvm_arch_init(void *opaque)
  1095. {
  1096. return 0;
  1097. }
  1098. void kvm_arch_exit(void)
  1099. {
  1100. }