powerpc.c 35 KB

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