powerpc.c 35 KB

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