powerpc.c 32 KB

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