powerpc.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065
  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 <asm/switch_to.h>
  40. #include <asm/xive.h>
  41. #include "timing.h"
  42. #include "irq.h"
  43. #include "../mm/mmu_decl.h"
  44. #define CREATE_TRACE_POINTS
  45. #include "trace.h"
  46. struct kvmppc_ops *kvmppc_hv_ops;
  47. EXPORT_SYMBOL_GPL(kvmppc_hv_ops);
  48. struct kvmppc_ops *kvmppc_pr_ops;
  49. EXPORT_SYMBOL_GPL(kvmppc_pr_ops);
  50. int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
  51. {
  52. return !!(v->arch.pending_exceptions) || kvm_request_pending(v);
  53. }
  54. bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
  55. {
  56. return false;
  57. }
  58. int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
  59. {
  60. return 1;
  61. }
  62. /*
  63. * Common checks before entering the guest world. Call with interrupts
  64. * disabled.
  65. *
  66. * returns:
  67. *
  68. * == 1 if we're ready to go into guest state
  69. * <= 0 if we need to go back to the host with return value
  70. */
  71. int kvmppc_prepare_to_enter(struct kvm_vcpu *vcpu)
  72. {
  73. int r;
  74. WARN_ON(irqs_disabled());
  75. hard_irq_disable();
  76. while (true) {
  77. if (need_resched()) {
  78. local_irq_enable();
  79. cond_resched();
  80. hard_irq_disable();
  81. continue;
  82. }
  83. if (signal_pending(current)) {
  84. kvmppc_account_exit(vcpu, SIGNAL_EXITS);
  85. vcpu->run->exit_reason = KVM_EXIT_INTR;
  86. r = -EINTR;
  87. break;
  88. }
  89. vcpu->mode = IN_GUEST_MODE;
  90. /*
  91. * Reading vcpu->requests must happen after setting vcpu->mode,
  92. * so we don't miss a request because the requester sees
  93. * OUTSIDE_GUEST_MODE and assumes we'll be checking requests
  94. * before next entering the guest (and thus doesn't IPI).
  95. * This also orders the write to mode from any reads
  96. * to the page tables done while the VCPU is running.
  97. * Please see the comment in kvm_flush_remote_tlbs.
  98. */
  99. smp_mb();
  100. if (kvm_request_pending(vcpu)) {
  101. /* Make sure we process requests preemptable */
  102. local_irq_enable();
  103. trace_kvm_check_requests(vcpu);
  104. r = kvmppc_core_check_requests(vcpu);
  105. hard_irq_disable();
  106. if (r > 0)
  107. continue;
  108. break;
  109. }
  110. if (kvmppc_core_prepare_to_enter(vcpu)) {
  111. /* interrupts got enabled in between, so we
  112. are back at square 1 */
  113. continue;
  114. }
  115. guest_enter_irqoff();
  116. return 1;
  117. }
  118. /* return to host */
  119. local_irq_enable();
  120. return r;
  121. }
  122. EXPORT_SYMBOL_GPL(kvmppc_prepare_to_enter);
  123. #if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_KVM_BOOK3S_PR_POSSIBLE)
  124. static void kvmppc_swab_shared(struct kvm_vcpu *vcpu)
  125. {
  126. struct kvm_vcpu_arch_shared *shared = vcpu->arch.shared;
  127. int i;
  128. shared->sprg0 = swab64(shared->sprg0);
  129. shared->sprg1 = swab64(shared->sprg1);
  130. shared->sprg2 = swab64(shared->sprg2);
  131. shared->sprg3 = swab64(shared->sprg3);
  132. shared->srr0 = swab64(shared->srr0);
  133. shared->srr1 = swab64(shared->srr1);
  134. shared->dar = swab64(shared->dar);
  135. shared->msr = swab64(shared->msr);
  136. shared->dsisr = swab32(shared->dsisr);
  137. shared->int_pending = swab32(shared->int_pending);
  138. for (i = 0; i < ARRAY_SIZE(shared->sr); i++)
  139. shared->sr[i] = swab32(shared->sr[i]);
  140. }
  141. #endif
  142. int kvmppc_kvm_pv(struct kvm_vcpu *vcpu)
  143. {
  144. int nr = kvmppc_get_gpr(vcpu, 11);
  145. int r;
  146. unsigned long __maybe_unused param1 = kvmppc_get_gpr(vcpu, 3);
  147. unsigned long __maybe_unused param2 = kvmppc_get_gpr(vcpu, 4);
  148. unsigned long __maybe_unused param3 = kvmppc_get_gpr(vcpu, 5);
  149. unsigned long __maybe_unused param4 = kvmppc_get_gpr(vcpu, 6);
  150. unsigned long r2 = 0;
  151. if (!(kvmppc_get_msr(vcpu) & MSR_SF)) {
  152. /* 32 bit mode */
  153. param1 &= 0xffffffff;
  154. param2 &= 0xffffffff;
  155. param3 &= 0xffffffff;
  156. param4 &= 0xffffffff;
  157. }
  158. switch (nr) {
  159. case KVM_HCALL_TOKEN(KVM_HC_PPC_MAP_MAGIC_PAGE):
  160. {
  161. #if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_KVM_BOOK3S_PR_POSSIBLE)
  162. /* Book3S can be little endian, find it out here */
  163. int shared_big_endian = true;
  164. if (vcpu->arch.intr_msr & MSR_LE)
  165. shared_big_endian = false;
  166. if (shared_big_endian != vcpu->arch.shared_big_endian)
  167. kvmppc_swab_shared(vcpu);
  168. vcpu->arch.shared_big_endian = shared_big_endian;
  169. #endif
  170. if (!(param2 & MAGIC_PAGE_FLAG_NOT_MAPPED_NX)) {
  171. /*
  172. * Older versions of the Linux magic page code had
  173. * a bug where they would map their trampoline code
  174. * NX. If that's the case, remove !PR NX capability.
  175. */
  176. vcpu->arch.disable_kernel_nx = true;
  177. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  178. }
  179. vcpu->arch.magic_page_pa = param1 & ~0xfffULL;
  180. vcpu->arch.magic_page_ea = param2 & ~0xfffULL;
  181. #ifdef CONFIG_PPC_64K_PAGES
  182. /*
  183. * Make sure our 4k magic page is in the same window of a 64k
  184. * page within the guest and within the host's page.
  185. */
  186. if ((vcpu->arch.magic_page_pa & 0xf000) !=
  187. ((ulong)vcpu->arch.shared & 0xf000)) {
  188. void *old_shared = vcpu->arch.shared;
  189. ulong shared = (ulong)vcpu->arch.shared;
  190. void *new_shared;
  191. shared &= PAGE_MASK;
  192. shared |= vcpu->arch.magic_page_pa & 0xf000;
  193. new_shared = (void*)shared;
  194. memcpy(new_shared, old_shared, 0x1000);
  195. vcpu->arch.shared = new_shared;
  196. }
  197. #endif
  198. r2 = KVM_MAGIC_FEAT_SR | KVM_MAGIC_FEAT_MAS0_TO_SPRG7;
  199. r = EV_SUCCESS;
  200. break;
  201. }
  202. case KVM_HCALL_TOKEN(KVM_HC_FEATURES):
  203. r = EV_SUCCESS;
  204. #if defined(CONFIG_PPC_BOOK3S) || defined(CONFIG_KVM_E500V2)
  205. r2 |= (1 << KVM_FEATURE_MAGIC_PAGE);
  206. #endif
  207. /* Second return value is in r4 */
  208. break;
  209. case EV_HCALL_TOKEN(EV_IDLE):
  210. r = EV_SUCCESS;
  211. kvm_vcpu_block(vcpu);
  212. kvm_clear_request(KVM_REQ_UNHALT, vcpu);
  213. break;
  214. default:
  215. r = EV_UNIMPLEMENTED;
  216. break;
  217. }
  218. kvmppc_set_gpr(vcpu, 4, r2);
  219. return r;
  220. }
  221. EXPORT_SYMBOL_GPL(kvmppc_kvm_pv);
  222. int kvmppc_sanity_check(struct kvm_vcpu *vcpu)
  223. {
  224. int r = false;
  225. /* We have to know what CPU to virtualize */
  226. if (!vcpu->arch.pvr)
  227. goto out;
  228. /* PAPR only works with book3s_64 */
  229. if ((vcpu->arch.cpu_type != KVM_CPU_3S_64) && vcpu->arch.papr_enabled)
  230. goto out;
  231. /* HV KVM can only do PAPR mode for now */
  232. if (!vcpu->arch.papr_enabled && is_kvmppc_hv_enabled(vcpu->kvm))
  233. goto out;
  234. #ifdef CONFIG_KVM_BOOKE_HV
  235. if (!cpu_has_feature(CPU_FTR_EMB_HV))
  236. goto out;
  237. #endif
  238. r = true;
  239. out:
  240. vcpu->arch.sane = r;
  241. return r ? 0 : -EINVAL;
  242. }
  243. EXPORT_SYMBOL_GPL(kvmppc_sanity_check);
  244. int kvmppc_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu)
  245. {
  246. enum emulation_result er;
  247. int r;
  248. er = kvmppc_emulate_loadstore(vcpu);
  249. switch (er) {
  250. case EMULATE_DONE:
  251. /* Future optimization: only reload non-volatiles if they were
  252. * actually modified. */
  253. r = RESUME_GUEST_NV;
  254. break;
  255. case EMULATE_AGAIN:
  256. r = RESUME_GUEST;
  257. break;
  258. case EMULATE_DO_MMIO:
  259. run->exit_reason = KVM_EXIT_MMIO;
  260. /* We must reload nonvolatiles because "update" load/store
  261. * instructions modify register state. */
  262. /* Future optimization: only reload non-volatiles if they were
  263. * actually modified. */
  264. r = RESUME_HOST_NV;
  265. break;
  266. case EMULATE_FAIL:
  267. {
  268. u32 last_inst;
  269. kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst);
  270. /* XXX Deliver Program interrupt to guest. */
  271. pr_emerg("%s: emulation failed (%08x)\n", __func__, last_inst);
  272. r = RESUME_HOST;
  273. break;
  274. }
  275. default:
  276. WARN_ON(1);
  277. r = RESUME_GUEST;
  278. }
  279. return r;
  280. }
  281. EXPORT_SYMBOL_GPL(kvmppc_emulate_mmio);
  282. int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
  283. bool data)
  284. {
  285. ulong mp_pa = vcpu->arch.magic_page_pa & KVM_PAM & PAGE_MASK;
  286. struct kvmppc_pte pte;
  287. int r;
  288. vcpu->stat.st++;
  289. r = kvmppc_xlate(vcpu, *eaddr, data ? XLATE_DATA : XLATE_INST,
  290. XLATE_WRITE, &pte);
  291. if (r < 0)
  292. return r;
  293. *eaddr = pte.raddr;
  294. if (!pte.may_write)
  295. return -EPERM;
  296. /* Magic page override */
  297. if (kvmppc_supports_magic_page(vcpu) && mp_pa &&
  298. ((pte.raddr & KVM_PAM & PAGE_MASK) == mp_pa) &&
  299. !(kvmppc_get_msr(vcpu) & MSR_PR)) {
  300. void *magic = vcpu->arch.shared;
  301. magic += pte.eaddr & 0xfff;
  302. memcpy(magic, ptr, size);
  303. return EMULATE_DONE;
  304. }
  305. if (kvm_write_guest(vcpu->kvm, pte.raddr, ptr, size))
  306. return EMULATE_DO_MMIO;
  307. return EMULATE_DONE;
  308. }
  309. EXPORT_SYMBOL_GPL(kvmppc_st);
  310. int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
  311. bool data)
  312. {
  313. ulong mp_pa = vcpu->arch.magic_page_pa & KVM_PAM & PAGE_MASK;
  314. struct kvmppc_pte pte;
  315. int rc;
  316. vcpu->stat.ld++;
  317. rc = kvmppc_xlate(vcpu, *eaddr, data ? XLATE_DATA : XLATE_INST,
  318. XLATE_READ, &pte);
  319. if (rc)
  320. return rc;
  321. *eaddr = pte.raddr;
  322. if (!pte.may_read)
  323. return -EPERM;
  324. if (!data && !pte.may_execute)
  325. return -ENOEXEC;
  326. /* Magic page override */
  327. if (kvmppc_supports_magic_page(vcpu) && mp_pa &&
  328. ((pte.raddr & KVM_PAM & PAGE_MASK) == mp_pa) &&
  329. !(kvmppc_get_msr(vcpu) & MSR_PR)) {
  330. void *magic = vcpu->arch.shared;
  331. magic += pte.eaddr & 0xfff;
  332. memcpy(ptr, magic, size);
  333. return EMULATE_DONE;
  334. }
  335. if (kvm_read_guest(vcpu->kvm, pte.raddr, ptr, size))
  336. return EMULATE_DO_MMIO;
  337. return EMULATE_DONE;
  338. }
  339. EXPORT_SYMBOL_GPL(kvmppc_ld);
  340. int kvm_arch_hardware_enable(void)
  341. {
  342. return 0;
  343. }
  344. int kvm_arch_hardware_setup(void)
  345. {
  346. return 0;
  347. }
  348. void kvm_arch_check_processor_compat(void *rtn)
  349. {
  350. *(int *)rtn = kvmppc_core_check_processor_compat();
  351. }
  352. int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
  353. {
  354. struct kvmppc_ops *kvm_ops = NULL;
  355. /*
  356. * if we have both HV and PR enabled, default is HV
  357. */
  358. if (type == 0) {
  359. if (kvmppc_hv_ops)
  360. kvm_ops = kvmppc_hv_ops;
  361. else
  362. kvm_ops = kvmppc_pr_ops;
  363. if (!kvm_ops)
  364. goto err_out;
  365. } else if (type == KVM_VM_PPC_HV) {
  366. if (!kvmppc_hv_ops)
  367. goto err_out;
  368. kvm_ops = kvmppc_hv_ops;
  369. } else if (type == KVM_VM_PPC_PR) {
  370. if (!kvmppc_pr_ops)
  371. goto err_out;
  372. kvm_ops = kvmppc_pr_ops;
  373. } else
  374. goto err_out;
  375. if (kvm_ops->owner && !try_module_get(kvm_ops->owner))
  376. return -ENOENT;
  377. kvm->arch.kvm_ops = kvm_ops;
  378. return kvmppc_core_init_vm(kvm);
  379. err_out:
  380. return -EINVAL;
  381. }
  382. bool kvm_arch_has_vcpu_debugfs(void)
  383. {
  384. return false;
  385. }
  386. int kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu)
  387. {
  388. return 0;
  389. }
  390. void kvm_arch_destroy_vm(struct kvm *kvm)
  391. {
  392. unsigned int i;
  393. struct kvm_vcpu *vcpu;
  394. #ifdef CONFIG_KVM_XICS
  395. /*
  396. * We call kick_all_cpus_sync() to ensure that all
  397. * CPUs have executed any pending IPIs before we
  398. * continue and free VCPUs structures below.
  399. */
  400. if (is_kvmppc_hv_enabled(kvm))
  401. kick_all_cpus_sync();
  402. #endif
  403. kvm_for_each_vcpu(i, vcpu, kvm)
  404. kvm_arch_vcpu_free(vcpu);
  405. mutex_lock(&kvm->lock);
  406. for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
  407. kvm->vcpus[i] = NULL;
  408. atomic_set(&kvm->online_vcpus, 0);
  409. kvmppc_core_destroy_vm(kvm);
  410. mutex_unlock(&kvm->lock);
  411. /* drop the module reference */
  412. module_put(kvm->arch.kvm_ops->owner);
  413. }
  414. int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
  415. {
  416. int r;
  417. /* Assume we're using HV mode when the HV module is loaded */
  418. int hv_enabled = kvmppc_hv_ops ? 1 : 0;
  419. if (kvm) {
  420. /*
  421. * Hooray - we know which VM type we're running on. Depend on
  422. * that rather than the guess above.
  423. */
  424. hv_enabled = is_kvmppc_hv_enabled(kvm);
  425. }
  426. switch (ext) {
  427. #ifdef CONFIG_BOOKE
  428. case KVM_CAP_PPC_BOOKE_SREGS:
  429. case KVM_CAP_PPC_BOOKE_WATCHDOG:
  430. case KVM_CAP_PPC_EPR:
  431. #else
  432. case KVM_CAP_PPC_SEGSTATE:
  433. case KVM_CAP_PPC_HIOR:
  434. case KVM_CAP_PPC_PAPR:
  435. #endif
  436. case KVM_CAP_PPC_UNSET_IRQ:
  437. case KVM_CAP_PPC_IRQ_LEVEL:
  438. case KVM_CAP_ENABLE_CAP:
  439. case KVM_CAP_ENABLE_CAP_VM:
  440. case KVM_CAP_ONE_REG:
  441. case KVM_CAP_IOEVENTFD:
  442. case KVM_CAP_DEVICE_CTRL:
  443. case KVM_CAP_IMMEDIATE_EXIT:
  444. r = 1;
  445. break;
  446. case KVM_CAP_PPC_PAIRED_SINGLES:
  447. case KVM_CAP_PPC_OSI:
  448. case KVM_CAP_PPC_GET_PVINFO:
  449. #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
  450. case KVM_CAP_SW_TLB:
  451. #endif
  452. /* We support this only for PR */
  453. r = !hv_enabled;
  454. break;
  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. /* fallthrough */
  464. case KVM_CAP_SPAPR_TCE_VFIO:
  465. case KVM_CAP_PPC_RTAS:
  466. case KVM_CAP_PPC_FIXUP_HCALL:
  467. case KVM_CAP_PPC_ENABLE_HCALL:
  468. #ifdef CONFIG_KVM_XICS
  469. case KVM_CAP_IRQ_XICS:
  470. #endif
  471. r = 1;
  472. break;
  473. case KVM_CAP_PPC_ALLOC_HTAB:
  474. r = hv_enabled;
  475. break;
  476. #endif /* CONFIG_PPC_BOOK3S_64 */
  477. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  478. case KVM_CAP_PPC_SMT:
  479. r = 0;
  480. if (kvm) {
  481. if (kvm->arch.emul_smt_mode > 1)
  482. r = kvm->arch.emul_smt_mode;
  483. else
  484. r = kvm->arch.smt_mode;
  485. } else if (hv_enabled) {
  486. if (cpu_has_feature(CPU_FTR_ARCH_300))
  487. r = 1;
  488. else
  489. r = threads_per_subcore;
  490. }
  491. break;
  492. case KVM_CAP_PPC_SMT_POSSIBLE:
  493. r = 1;
  494. if (hv_enabled) {
  495. if (!cpu_has_feature(CPU_FTR_ARCH_300))
  496. r = ((threads_per_subcore << 1) - 1);
  497. else
  498. /* P9 can emulate dbells, so allow any mode */
  499. r = 8 | 4 | 2 | 1;
  500. }
  501. break;
  502. case KVM_CAP_PPC_RMA:
  503. r = 0;
  504. break;
  505. case KVM_CAP_PPC_HWRNG:
  506. r = kvmppc_hwrng_present();
  507. break;
  508. case KVM_CAP_PPC_MMU_RADIX:
  509. r = !!(hv_enabled && radix_enabled());
  510. break;
  511. case KVM_CAP_PPC_MMU_HASH_V3:
  512. r = !!(hv_enabled && cpu_has_feature(CPU_FTR_ARCH_300));
  513. break;
  514. #endif
  515. case KVM_CAP_SYNC_MMU:
  516. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  517. r = hv_enabled;
  518. #elif defined(KVM_ARCH_WANT_MMU_NOTIFIER)
  519. r = 1;
  520. #else
  521. r = 0;
  522. #endif
  523. break;
  524. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  525. case KVM_CAP_PPC_HTAB_FD:
  526. r = hv_enabled;
  527. break;
  528. #endif
  529. case KVM_CAP_NR_VCPUS:
  530. /*
  531. * Recommending a number of CPUs is somewhat arbitrary; we
  532. * return the number of present CPUs for -HV (since a host
  533. * will have secondary threads "offline"), and for other KVM
  534. * implementations just count online CPUs.
  535. */
  536. if (hv_enabled)
  537. r = num_present_cpus();
  538. else
  539. r = num_online_cpus();
  540. break;
  541. case KVM_CAP_NR_MEMSLOTS:
  542. r = KVM_USER_MEM_SLOTS;
  543. break;
  544. case KVM_CAP_MAX_VCPUS:
  545. r = KVM_MAX_VCPUS;
  546. break;
  547. #ifdef CONFIG_PPC_BOOK3S_64
  548. case KVM_CAP_PPC_GET_SMMU_INFO:
  549. r = 1;
  550. break;
  551. case KVM_CAP_SPAPR_MULTITCE:
  552. r = 1;
  553. break;
  554. case KVM_CAP_SPAPR_RESIZE_HPT:
  555. r = !!hv_enabled;
  556. break;
  557. #endif
  558. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  559. case KVM_CAP_PPC_FWNMI:
  560. r = hv_enabled;
  561. break;
  562. #endif
  563. case KVM_CAP_PPC_HTM:
  564. r = hv_enabled &&
  565. (cur_cpu_spec->cpu_user_features2 & PPC_FEATURE2_HTM_COMP);
  566. break;
  567. default:
  568. r = 0;
  569. break;
  570. }
  571. return r;
  572. }
  573. long kvm_arch_dev_ioctl(struct file *filp,
  574. unsigned int ioctl, unsigned long arg)
  575. {
  576. return -EINVAL;
  577. }
  578. void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
  579. struct kvm_memory_slot *dont)
  580. {
  581. kvmppc_core_free_memslot(kvm, free, dont);
  582. }
  583. int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
  584. unsigned long npages)
  585. {
  586. return kvmppc_core_create_memslot(kvm, slot, npages);
  587. }
  588. int kvm_arch_prepare_memory_region(struct kvm *kvm,
  589. struct kvm_memory_slot *memslot,
  590. const struct kvm_userspace_memory_region *mem,
  591. enum kvm_mr_change change)
  592. {
  593. return kvmppc_core_prepare_memory_region(kvm, memslot, mem);
  594. }
  595. void kvm_arch_commit_memory_region(struct kvm *kvm,
  596. const struct kvm_userspace_memory_region *mem,
  597. const struct kvm_memory_slot *old,
  598. const struct kvm_memory_slot *new,
  599. enum kvm_mr_change change)
  600. {
  601. kvmppc_core_commit_memory_region(kvm, mem, old, new);
  602. }
  603. void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
  604. struct kvm_memory_slot *slot)
  605. {
  606. kvmppc_core_flush_memslot(kvm, slot);
  607. }
  608. struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
  609. {
  610. struct kvm_vcpu *vcpu;
  611. vcpu = kvmppc_core_vcpu_create(kvm, id);
  612. if (!IS_ERR(vcpu)) {
  613. vcpu->arch.wqp = &vcpu->wq;
  614. kvmppc_create_vcpu_debugfs(vcpu, id);
  615. }
  616. return vcpu;
  617. }
  618. void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
  619. {
  620. }
  621. void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
  622. {
  623. /* Make sure we're not using the vcpu anymore */
  624. hrtimer_cancel(&vcpu->arch.dec_timer);
  625. kvmppc_remove_vcpu_debugfs(vcpu);
  626. switch (vcpu->arch.irq_type) {
  627. case KVMPPC_IRQ_MPIC:
  628. kvmppc_mpic_disconnect_vcpu(vcpu->arch.mpic, vcpu);
  629. break;
  630. case KVMPPC_IRQ_XICS:
  631. if (xive_enabled())
  632. kvmppc_xive_cleanup_vcpu(vcpu);
  633. else
  634. kvmppc_xics_free_icp(vcpu);
  635. break;
  636. }
  637. kvmppc_core_vcpu_free(vcpu);
  638. }
  639. void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
  640. {
  641. kvm_arch_vcpu_free(vcpu);
  642. }
  643. int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
  644. {
  645. return kvmppc_core_pending_dec(vcpu);
  646. }
  647. static enum hrtimer_restart kvmppc_decrementer_wakeup(struct hrtimer *timer)
  648. {
  649. struct kvm_vcpu *vcpu;
  650. vcpu = container_of(timer, struct kvm_vcpu, arch.dec_timer);
  651. kvmppc_decrementer_func(vcpu);
  652. return HRTIMER_NORESTART;
  653. }
  654. int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
  655. {
  656. int ret;
  657. hrtimer_init(&vcpu->arch.dec_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
  658. vcpu->arch.dec_timer.function = kvmppc_decrementer_wakeup;
  659. vcpu->arch.dec_expires = get_tb();
  660. #ifdef CONFIG_KVM_EXIT_TIMING
  661. mutex_init(&vcpu->arch.exit_timing_lock);
  662. #endif
  663. ret = kvmppc_subarch_vcpu_init(vcpu);
  664. return ret;
  665. }
  666. void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
  667. {
  668. kvmppc_mmu_destroy(vcpu);
  669. kvmppc_subarch_vcpu_uninit(vcpu);
  670. }
  671. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  672. {
  673. #ifdef CONFIG_BOOKE
  674. /*
  675. * vrsave (formerly usprg0) isn't used by Linux, but may
  676. * be used by the guest.
  677. *
  678. * On non-booke this is associated with Altivec and
  679. * is handled by code in book3s.c.
  680. */
  681. mtspr(SPRN_VRSAVE, vcpu->arch.vrsave);
  682. #endif
  683. kvmppc_core_vcpu_load(vcpu, cpu);
  684. }
  685. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
  686. {
  687. kvmppc_core_vcpu_put(vcpu);
  688. #ifdef CONFIG_BOOKE
  689. vcpu->arch.vrsave = mfspr(SPRN_VRSAVE);
  690. #endif
  691. }
  692. /*
  693. * irq_bypass_add_producer and irq_bypass_del_producer are only
  694. * useful if the architecture supports PCI passthrough.
  695. * irq_bypass_stop and irq_bypass_start are not needed and so
  696. * kvm_ops are not defined for them.
  697. */
  698. bool kvm_arch_has_irq_bypass(void)
  699. {
  700. return ((kvmppc_hv_ops && kvmppc_hv_ops->irq_bypass_add_producer) ||
  701. (kvmppc_pr_ops && kvmppc_pr_ops->irq_bypass_add_producer));
  702. }
  703. int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
  704. struct irq_bypass_producer *prod)
  705. {
  706. struct kvm_kernel_irqfd *irqfd =
  707. container_of(cons, struct kvm_kernel_irqfd, consumer);
  708. struct kvm *kvm = irqfd->kvm;
  709. if (kvm->arch.kvm_ops->irq_bypass_add_producer)
  710. return kvm->arch.kvm_ops->irq_bypass_add_producer(cons, prod);
  711. return 0;
  712. }
  713. void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
  714. struct irq_bypass_producer *prod)
  715. {
  716. struct kvm_kernel_irqfd *irqfd =
  717. container_of(cons, struct kvm_kernel_irqfd, consumer);
  718. struct kvm *kvm = irqfd->kvm;
  719. if (kvm->arch.kvm_ops->irq_bypass_del_producer)
  720. kvm->arch.kvm_ops->irq_bypass_del_producer(cons, prod);
  721. }
  722. #ifdef CONFIG_VSX
  723. static inline int kvmppc_get_vsr_dword_offset(int index)
  724. {
  725. int offset;
  726. if ((index != 0) && (index != 1))
  727. return -1;
  728. #ifdef __BIG_ENDIAN
  729. offset = index;
  730. #else
  731. offset = 1 - index;
  732. #endif
  733. return offset;
  734. }
  735. static inline int kvmppc_get_vsr_word_offset(int index)
  736. {
  737. int offset;
  738. if ((index > 3) || (index < 0))
  739. return -1;
  740. #ifdef __BIG_ENDIAN
  741. offset = index;
  742. #else
  743. offset = 3 - index;
  744. #endif
  745. return offset;
  746. }
  747. static inline void kvmppc_set_vsr_dword(struct kvm_vcpu *vcpu,
  748. u64 gpr)
  749. {
  750. union kvmppc_one_reg val;
  751. int offset = kvmppc_get_vsr_dword_offset(vcpu->arch.mmio_vsx_offset);
  752. int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
  753. if (offset == -1)
  754. return;
  755. if (vcpu->arch.mmio_vsx_tx_sx_enabled) {
  756. val.vval = VCPU_VSX_VR(vcpu, index);
  757. val.vsxval[offset] = gpr;
  758. VCPU_VSX_VR(vcpu, index) = val.vval;
  759. } else {
  760. VCPU_VSX_FPR(vcpu, index, offset) = gpr;
  761. }
  762. }
  763. static inline void kvmppc_set_vsr_dword_dump(struct kvm_vcpu *vcpu,
  764. u64 gpr)
  765. {
  766. union kvmppc_one_reg val;
  767. int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
  768. if (vcpu->arch.mmio_vsx_tx_sx_enabled) {
  769. val.vval = VCPU_VSX_VR(vcpu, index);
  770. val.vsxval[0] = gpr;
  771. val.vsxval[1] = gpr;
  772. VCPU_VSX_VR(vcpu, index) = val.vval;
  773. } else {
  774. VCPU_VSX_FPR(vcpu, index, 0) = gpr;
  775. VCPU_VSX_FPR(vcpu, index, 1) = gpr;
  776. }
  777. }
  778. static inline void kvmppc_set_vsr_word(struct kvm_vcpu *vcpu,
  779. u32 gpr32)
  780. {
  781. union kvmppc_one_reg val;
  782. int offset = kvmppc_get_vsr_word_offset(vcpu->arch.mmio_vsx_offset);
  783. int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
  784. int dword_offset, word_offset;
  785. if (offset == -1)
  786. return;
  787. if (vcpu->arch.mmio_vsx_tx_sx_enabled) {
  788. val.vval = VCPU_VSX_VR(vcpu, index);
  789. val.vsx32val[offset] = gpr32;
  790. VCPU_VSX_VR(vcpu, index) = val.vval;
  791. } else {
  792. dword_offset = offset / 2;
  793. word_offset = offset % 2;
  794. val.vsxval[0] = VCPU_VSX_FPR(vcpu, index, dword_offset);
  795. val.vsx32val[word_offset] = gpr32;
  796. VCPU_VSX_FPR(vcpu, index, dword_offset) = val.vsxval[0];
  797. }
  798. }
  799. #endif /* CONFIG_VSX */
  800. #ifdef CONFIG_ALTIVEC
  801. static inline void kvmppc_set_vmx_dword(struct kvm_vcpu *vcpu,
  802. u64 gpr)
  803. {
  804. int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
  805. u32 hi, lo;
  806. u32 di;
  807. #ifdef __BIG_ENDIAN
  808. hi = gpr >> 32;
  809. lo = gpr & 0xffffffff;
  810. #else
  811. lo = gpr >> 32;
  812. hi = gpr & 0xffffffff;
  813. #endif
  814. di = 2 - vcpu->arch.mmio_vmx_copy_nums; /* doubleword index */
  815. if (di > 1)
  816. return;
  817. if (vcpu->arch.mmio_host_swabbed)
  818. di = 1 - di;
  819. VCPU_VSX_VR(vcpu, index).u[di * 2] = hi;
  820. VCPU_VSX_VR(vcpu, index).u[di * 2 + 1] = lo;
  821. }
  822. #endif /* CONFIG_ALTIVEC */
  823. #ifdef CONFIG_PPC_FPU
  824. static inline u64 sp_to_dp(u32 fprs)
  825. {
  826. u64 fprd;
  827. preempt_disable();
  828. enable_kernel_fp();
  829. asm ("lfs%U1%X1 0,%1; stfd%U0%X0 0,%0" : "=m" (fprd) : "m" (fprs)
  830. : "fr0");
  831. preempt_enable();
  832. return fprd;
  833. }
  834. static inline u32 dp_to_sp(u64 fprd)
  835. {
  836. u32 fprs;
  837. preempt_disable();
  838. enable_kernel_fp();
  839. asm ("lfd%U1%X1 0,%1; stfs%U0%X0 0,%0" : "=m" (fprs) : "m" (fprd)
  840. : "fr0");
  841. preempt_enable();
  842. return fprs;
  843. }
  844. #else
  845. #define sp_to_dp(x) (x)
  846. #define dp_to_sp(x) (x)
  847. #endif /* CONFIG_PPC_FPU */
  848. static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu,
  849. struct kvm_run *run)
  850. {
  851. u64 uninitialized_var(gpr);
  852. if (run->mmio.len > sizeof(gpr)) {
  853. printk(KERN_ERR "bad MMIO length: %d\n", run->mmio.len);
  854. return;
  855. }
  856. if (!vcpu->arch.mmio_host_swabbed) {
  857. switch (run->mmio.len) {
  858. case 8: gpr = *(u64 *)run->mmio.data; break;
  859. case 4: gpr = *(u32 *)run->mmio.data; break;
  860. case 2: gpr = *(u16 *)run->mmio.data; break;
  861. case 1: gpr = *(u8 *)run->mmio.data; break;
  862. }
  863. } else {
  864. switch (run->mmio.len) {
  865. case 8: gpr = swab64(*(u64 *)run->mmio.data); break;
  866. case 4: gpr = swab32(*(u32 *)run->mmio.data); break;
  867. case 2: gpr = swab16(*(u16 *)run->mmio.data); break;
  868. case 1: gpr = *(u8 *)run->mmio.data; break;
  869. }
  870. }
  871. /* conversion between single and double precision */
  872. if ((vcpu->arch.mmio_sp64_extend) && (run->mmio.len == 4))
  873. gpr = sp_to_dp(gpr);
  874. if (vcpu->arch.mmio_sign_extend) {
  875. switch (run->mmio.len) {
  876. #ifdef CONFIG_PPC64
  877. case 4:
  878. gpr = (s64)(s32)gpr;
  879. break;
  880. #endif
  881. case 2:
  882. gpr = (s64)(s16)gpr;
  883. break;
  884. case 1:
  885. gpr = (s64)(s8)gpr;
  886. break;
  887. }
  888. }
  889. switch (vcpu->arch.io_gpr & KVM_MMIO_REG_EXT_MASK) {
  890. case KVM_MMIO_REG_GPR:
  891. kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
  892. break;
  893. case KVM_MMIO_REG_FPR:
  894. VCPU_FPR(vcpu, vcpu->arch.io_gpr & KVM_MMIO_REG_MASK) = gpr;
  895. break;
  896. #ifdef CONFIG_PPC_BOOK3S
  897. case KVM_MMIO_REG_QPR:
  898. vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
  899. break;
  900. case KVM_MMIO_REG_FQPR:
  901. VCPU_FPR(vcpu, vcpu->arch.io_gpr & KVM_MMIO_REG_MASK) = gpr;
  902. vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
  903. break;
  904. #endif
  905. #ifdef CONFIG_VSX
  906. case KVM_MMIO_REG_VSX:
  907. if (vcpu->arch.mmio_vsx_copy_type == KVMPPC_VSX_COPY_DWORD)
  908. kvmppc_set_vsr_dword(vcpu, gpr);
  909. else if (vcpu->arch.mmio_vsx_copy_type == KVMPPC_VSX_COPY_WORD)
  910. kvmppc_set_vsr_word(vcpu, gpr);
  911. else if (vcpu->arch.mmio_vsx_copy_type ==
  912. KVMPPC_VSX_COPY_DWORD_LOAD_DUMP)
  913. kvmppc_set_vsr_dword_dump(vcpu, gpr);
  914. break;
  915. #endif
  916. #ifdef CONFIG_ALTIVEC
  917. case KVM_MMIO_REG_VMX:
  918. kvmppc_set_vmx_dword(vcpu, gpr);
  919. break;
  920. #endif
  921. default:
  922. BUG();
  923. }
  924. }
  925. static int __kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
  926. unsigned int rt, unsigned int bytes,
  927. int is_default_endian, int sign_extend)
  928. {
  929. int idx, ret;
  930. bool host_swabbed;
  931. /* Pity C doesn't have a logical XOR operator */
  932. if (kvmppc_need_byteswap(vcpu)) {
  933. host_swabbed = is_default_endian;
  934. } else {
  935. host_swabbed = !is_default_endian;
  936. }
  937. if (bytes > sizeof(run->mmio.data)) {
  938. printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
  939. run->mmio.len);
  940. }
  941. run->mmio.phys_addr = vcpu->arch.paddr_accessed;
  942. run->mmio.len = bytes;
  943. run->mmio.is_write = 0;
  944. vcpu->arch.io_gpr = rt;
  945. vcpu->arch.mmio_host_swabbed = host_swabbed;
  946. vcpu->mmio_needed = 1;
  947. vcpu->mmio_is_write = 0;
  948. vcpu->arch.mmio_sign_extend = sign_extend;
  949. idx = srcu_read_lock(&vcpu->kvm->srcu);
  950. ret = kvm_io_bus_read(vcpu, KVM_MMIO_BUS, run->mmio.phys_addr,
  951. bytes, &run->mmio.data);
  952. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  953. if (!ret) {
  954. kvmppc_complete_mmio_load(vcpu, run);
  955. vcpu->mmio_needed = 0;
  956. return EMULATE_DONE;
  957. }
  958. return EMULATE_DO_MMIO;
  959. }
  960. int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
  961. unsigned int rt, unsigned int bytes,
  962. int is_default_endian)
  963. {
  964. return __kvmppc_handle_load(run, vcpu, rt, bytes, is_default_endian, 0);
  965. }
  966. EXPORT_SYMBOL_GPL(kvmppc_handle_load);
  967. /* Same as above, but sign extends */
  968. int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu,
  969. unsigned int rt, unsigned int bytes,
  970. int is_default_endian)
  971. {
  972. return __kvmppc_handle_load(run, vcpu, rt, bytes, is_default_endian, 1);
  973. }
  974. #ifdef CONFIG_VSX
  975. int kvmppc_handle_vsx_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
  976. unsigned int rt, unsigned int bytes,
  977. int is_default_endian, int mmio_sign_extend)
  978. {
  979. enum emulation_result emulated = EMULATE_DONE;
  980. /* Currently, mmio_vsx_copy_nums only allowed to be 4 or less */
  981. if (vcpu->arch.mmio_vsx_copy_nums > 4)
  982. return EMULATE_FAIL;
  983. while (vcpu->arch.mmio_vsx_copy_nums) {
  984. emulated = __kvmppc_handle_load(run, vcpu, rt, bytes,
  985. is_default_endian, mmio_sign_extend);
  986. if (emulated != EMULATE_DONE)
  987. break;
  988. vcpu->arch.paddr_accessed += run->mmio.len;
  989. vcpu->arch.mmio_vsx_copy_nums--;
  990. vcpu->arch.mmio_vsx_offset++;
  991. }
  992. return emulated;
  993. }
  994. #endif /* CONFIG_VSX */
  995. int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
  996. u64 val, unsigned int bytes, int is_default_endian)
  997. {
  998. void *data = run->mmio.data;
  999. int idx, ret;
  1000. bool host_swabbed;
  1001. /* Pity C doesn't have a logical XOR operator */
  1002. if (kvmppc_need_byteswap(vcpu)) {
  1003. host_swabbed = is_default_endian;
  1004. } else {
  1005. host_swabbed = !is_default_endian;
  1006. }
  1007. if (bytes > sizeof(run->mmio.data)) {
  1008. printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
  1009. run->mmio.len);
  1010. }
  1011. run->mmio.phys_addr = vcpu->arch.paddr_accessed;
  1012. run->mmio.len = bytes;
  1013. run->mmio.is_write = 1;
  1014. vcpu->mmio_needed = 1;
  1015. vcpu->mmio_is_write = 1;
  1016. if ((vcpu->arch.mmio_sp64_extend) && (bytes == 4))
  1017. val = dp_to_sp(val);
  1018. /* Store the value at the lowest bytes in 'data'. */
  1019. if (!host_swabbed) {
  1020. switch (bytes) {
  1021. case 8: *(u64 *)data = val; break;
  1022. case 4: *(u32 *)data = val; break;
  1023. case 2: *(u16 *)data = val; break;
  1024. case 1: *(u8 *)data = val; break;
  1025. }
  1026. } else {
  1027. switch (bytes) {
  1028. case 8: *(u64 *)data = swab64(val); break;
  1029. case 4: *(u32 *)data = swab32(val); break;
  1030. case 2: *(u16 *)data = swab16(val); break;
  1031. case 1: *(u8 *)data = val; break;
  1032. }
  1033. }
  1034. idx = srcu_read_lock(&vcpu->kvm->srcu);
  1035. ret = kvm_io_bus_write(vcpu, KVM_MMIO_BUS, run->mmio.phys_addr,
  1036. bytes, &run->mmio.data);
  1037. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  1038. if (!ret) {
  1039. vcpu->mmio_needed = 0;
  1040. return EMULATE_DONE;
  1041. }
  1042. return EMULATE_DO_MMIO;
  1043. }
  1044. EXPORT_SYMBOL_GPL(kvmppc_handle_store);
  1045. #ifdef CONFIG_VSX
  1046. static inline int kvmppc_get_vsr_data(struct kvm_vcpu *vcpu, int rs, u64 *val)
  1047. {
  1048. u32 dword_offset, word_offset;
  1049. union kvmppc_one_reg reg;
  1050. int vsx_offset = 0;
  1051. int copy_type = vcpu->arch.mmio_vsx_copy_type;
  1052. int result = 0;
  1053. switch (copy_type) {
  1054. case KVMPPC_VSX_COPY_DWORD:
  1055. vsx_offset =
  1056. kvmppc_get_vsr_dword_offset(vcpu->arch.mmio_vsx_offset);
  1057. if (vsx_offset == -1) {
  1058. result = -1;
  1059. break;
  1060. }
  1061. if (!vcpu->arch.mmio_vsx_tx_sx_enabled) {
  1062. *val = VCPU_VSX_FPR(vcpu, rs, vsx_offset);
  1063. } else {
  1064. reg.vval = VCPU_VSX_VR(vcpu, rs);
  1065. *val = reg.vsxval[vsx_offset];
  1066. }
  1067. break;
  1068. case KVMPPC_VSX_COPY_WORD:
  1069. vsx_offset =
  1070. kvmppc_get_vsr_word_offset(vcpu->arch.mmio_vsx_offset);
  1071. if (vsx_offset == -1) {
  1072. result = -1;
  1073. break;
  1074. }
  1075. if (!vcpu->arch.mmio_vsx_tx_sx_enabled) {
  1076. dword_offset = vsx_offset / 2;
  1077. word_offset = vsx_offset % 2;
  1078. reg.vsxval[0] = VCPU_VSX_FPR(vcpu, rs, dword_offset);
  1079. *val = reg.vsx32val[word_offset];
  1080. } else {
  1081. reg.vval = VCPU_VSX_VR(vcpu, rs);
  1082. *val = reg.vsx32val[vsx_offset];
  1083. }
  1084. break;
  1085. default:
  1086. result = -1;
  1087. break;
  1088. }
  1089. return result;
  1090. }
  1091. int kvmppc_handle_vsx_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
  1092. int rs, unsigned int bytes, int is_default_endian)
  1093. {
  1094. u64 val;
  1095. enum emulation_result emulated = EMULATE_DONE;
  1096. vcpu->arch.io_gpr = rs;
  1097. /* Currently, mmio_vsx_copy_nums only allowed to be 4 or less */
  1098. if (vcpu->arch.mmio_vsx_copy_nums > 4)
  1099. return EMULATE_FAIL;
  1100. while (vcpu->arch.mmio_vsx_copy_nums) {
  1101. if (kvmppc_get_vsr_data(vcpu, rs, &val) == -1)
  1102. return EMULATE_FAIL;
  1103. emulated = kvmppc_handle_store(run, vcpu,
  1104. val, bytes, is_default_endian);
  1105. if (emulated != EMULATE_DONE)
  1106. break;
  1107. vcpu->arch.paddr_accessed += run->mmio.len;
  1108. vcpu->arch.mmio_vsx_copy_nums--;
  1109. vcpu->arch.mmio_vsx_offset++;
  1110. }
  1111. return emulated;
  1112. }
  1113. static int kvmppc_emulate_mmio_vsx_loadstore(struct kvm_vcpu *vcpu,
  1114. struct kvm_run *run)
  1115. {
  1116. enum emulation_result emulated = EMULATE_FAIL;
  1117. int r;
  1118. vcpu->arch.paddr_accessed += run->mmio.len;
  1119. if (!vcpu->mmio_is_write) {
  1120. emulated = kvmppc_handle_vsx_load(run, vcpu, vcpu->arch.io_gpr,
  1121. run->mmio.len, 1, vcpu->arch.mmio_sign_extend);
  1122. } else {
  1123. emulated = kvmppc_handle_vsx_store(run, vcpu,
  1124. vcpu->arch.io_gpr, run->mmio.len, 1);
  1125. }
  1126. switch (emulated) {
  1127. case EMULATE_DO_MMIO:
  1128. run->exit_reason = KVM_EXIT_MMIO;
  1129. r = RESUME_HOST;
  1130. break;
  1131. case EMULATE_FAIL:
  1132. pr_info("KVM: MMIO emulation failed (VSX repeat)\n");
  1133. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  1134. run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
  1135. r = RESUME_HOST;
  1136. break;
  1137. default:
  1138. r = RESUME_GUEST;
  1139. break;
  1140. }
  1141. return r;
  1142. }
  1143. #endif /* CONFIG_VSX */
  1144. #ifdef CONFIG_ALTIVEC
  1145. /* handle quadword load access in two halves */
  1146. int kvmppc_handle_load128_by2x64(struct kvm_run *run, struct kvm_vcpu *vcpu,
  1147. unsigned int rt, int is_default_endian)
  1148. {
  1149. enum emulation_result emulated;
  1150. while (vcpu->arch.mmio_vmx_copy_nums) {
  1151. emulated = __kvmppc_handle_load(run, vcpu, rt, 8,
  1152. is_default_endian, 0);
  1153. if (emulated != EMULATE_DONE)
  1154. break;
  1155. vcpu->arch.paddr_accessed += run->mmio.len;
  1156. vcpu->arch.mmio_vmx_copy_nums--;
  1157. }
  1158. return emulated;
  1159. }
  1160. static inline int kvmppc_get_vmx_data(struct kvm_vcpu *vcpu, int rs, u64 *val)
  1161. {
  1162. vector128 vrs = VCPU_VSX_VR(vcpu, rs);
  1163. u32 di;
  1164. u64 w0, w1;
  1165. di = 2 - vcpu->arch.mmio_vmx_copy_nums; /* doubleword index */
  1166. if (di > 1)
  1167. return -1;
  1168. if (vcpu->arch.mmio_host_swabbed)
  1169. di = 1 - di;
  1170. w0 = vrs.u[di * 2];
  1171. w1 = vrs.u[di * 2 + 1];
  1172. #ifdef __BIG_ENDIAN
  1173. *val = (w0 << 32) | w1;
  1174. #else
  1175. *val = (w1 << 32) | w0;
  1176. #endif
  1177. return 0;
  1178. }
  1179. /* handle quadword store in two halves */
  1180. int kvmppc_handle_store128_by2x64(struct kvm_run *run, struct kvm_vcpu *vcpu,
  1181. unsigned int rs, int is_default_endian)
  1182. {
  1183. u64 val = 0;
  1184. enum emulation_result emulated = EMULATE_DONE;
  1185. vcpu->arch.io_gpr = rs;
  1186. while (vcpu->arch.mmio_vmx_copy_nums) {
  1187. if (kvmppc_get_vmx_data(vcpu, rs, &val) == -1)
  1188. return EMULATE_FAIL;
  1189. emulated = kvmppc_handle_store(run, vcpu, val, 8,
  1190. is_default_endian);
  1191. if (emulated != EMULATE_DONE)
  1192. break;
  1193. vcpu->arch.paddr_accessed += run->mmio.len;
  1194. vcpu->arch.mmio_vmx_copy_nums--;
  1195. }
  1196. return emulated;
  1197. }
  1198. static int kvmppc_emulate_mmio_vmx_loadstore(struct kvm_vcpu *vcpu,
  1199. struct kvm_run *run)
  1200. {
  1201. enum emulation_result emulated = EMULATE_FAIL;
  1202. int r;
  1203. vcpu->arch.paddr_accessed += run->mmio.len;
  1204. if (!vcpu->mmio_is_write) {
  1205. emulated = kvmppc_handle_load128_by2x64(run, vcpu,
  1206. vcpu->arch.io_gpr, 1);
  1207. } else {
  1208. emulated = kvmppc_handle_store128_by2x64(run, vcpu,
  1209. vcpu->arch.io_gpr, 1);
  1210. }
  1211. switch (emulated) {
  1212. case EMULATE_DO_MMIO:
  1213. run->exit_reason = KVM_EXIT_MMIO;
  1214. r = RESUME_HOST;
  1215. break;
  1216. case EMULATE_FAIL:
  1217. pr_info("KVM: MMIO emulation failed (VMX repeat)\n");
  1218. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  1219. run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
  1220. r = RESUME_HOST;
  1221. break;
  1222. default:
  1223. r = RESUME_GUEST;
  1224. break;
  1225. }
  1226. return r;
  1227. }
  1228. #endif /* CONFIG_ALTIVEC */
  1229. int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
  1230. {
  1231. int r = 0;
  1232. union kvmppc_one_reg val;
  1233. int size;
  1234. size = one_reg_size(reg->id);
  1235. if (size > sizeof(val))
  1236. return -EINVAL;
  1237. r = kvmppc_get_one_reg(vcpu, reg->id, &val);
  1238. if (r == -EINVAL) {
  1239. r = 0;
  1240. switch (reg->id) {
  1241. #ifdef CONFIG_ALTIVEC
  1242. case KVM_REG_PPC_VR0 ... KVM_REG_PPC_VR31:
  1243. if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
  1244. r = -ENXIO;
  1245. break;
  1246. }
  1247. val.vval = vcpu->arch.vr.vr[reg->id - KVM_REG_PPC_VR0];
  1248. break;
  1249. case KVM_REG_PPC_VSCR:
  1250. if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
  1251. r = -ENXIO;
  1252. break;
  1253. }
  1254. val = get_reg_val(reg->id, vcpu->arch.vr.vscr.u[3]);
  1255. break;
  1256. case KVM_REG_PPC_VRSAVE:
  1257. val = get_reg_val(reg->id, vcpu->arch.vrsave);
  1258. break;
  1259. #endif /* CONFIG_ALTIVEC */
  1260. default:
  1261. r = -EINVAL;
  1262. break;
  1263. }
  1264. }
  1265. if (r)
  1266. return r;
  1267. if (copy_to_user((char __user *)(unsigned long)reg->addr, &val, size))
  1268. r = -EFAULT;
  1269. return r;
  1270. }
  1271. int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
  1272. {
  1273. int r;
  1274. union kvmppc_one_reg val;
  1275. int size;
  1276. size = one_reg_size(reg->id);
  1277. if (size > sizeof(val))
  1278. return -EINVAL;
  1279. if (copy_from_user(&val, (char __user *)(unsigned long)reg->addr, size))
  1280. return -EFAULT;
  1281. r = kvmppc_set_one_reg(vcpu, reg->id, &val);
  1282. if (r == -EINVAL) {
  1283. r = 0;
  1284. switch (reg->id) {
  1285. #ifdef CONFIG_ALTIVEC
  1286. case KVM_REG_PPC_VR0 ... KVM_REG_PPC_VR31:
  1287. if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
  1288. r = -ENXIO;
  1289. break;
  1290. }
  1291. vcpu->arch.vr.vr[reg->id - KVM_REG_PPC_VR0] = val.vval;
  1292. break;
  1293. case KVM_REG_PPC_VSCR:
  1294. if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
  1295. r = -ENXIO;
  1296. break;
  1297. }
  1298. vcpu->arch.vr.vscr.u[3] = set_reg_val(reg->id, val);
  1299. break;
  1300. case KVM_REG_PPC_VRSAVE:
  1301. if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
  1302. r = -ENXIO;
  1303. break;
  1304. }
  1305. vcpu->arch.vrsave = set_reg_val(reg->id, val);
  1306. break;
  1307. #endif /* CONFIG_ALTIVEC */
  1308. default:
  1309. r = -EINVAL;
  1310. break;
  1311. }
  1312. }
  1313. return r;
  1314. }
  1315. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
  1316. {
  1317. int r;
  1318. if (vcpu->mmio_needed) {
  1319. vcpu->mmio_needed = 0;
  1320. if (!vcpu->mmio_is_write)
  1321. kvmppc_complete_mmio_load(vcpu, run);
  1322. #ifdef CONFIG_VSX
  1323. if (vcpu->arch.mmio_vsx_copy_nums > 0) {
  1324. vcpu->arch.mmio_vsx_copy_nums--;
  1325. vcpu->arch.mmio_vsx_offset++;
  1326. }
  1327. if (vcpu->arch.mmio_vsx_copy_nums > 0) {
  1328. r = kvmppc_emulate_mmio_vsx_loadstore(vcpu, run);
  1329. if (r == RESUME_HOST) {
  1330. vcpu->mmio_needed = 1;
  1331. return r;
  1332. }
  1333. }
  1334. #endif
  1335. #ifdef CONFIG_ALTIVEC
  1336. if (vcpu->arch.mmio_vmx_copy_nums > 0)
  1337. vcpu->arch.mmio_vmx_copy_nums--;
  1338. if (vcpu->arch.mmio_vmx_copy_nums > 0) {
  1339. r = kvmppc_emulate_mmio_vmx_loadstore(vcpu, run);
  1340. if (r == RESUME_HOST) {
  1341. vcpu->mmio_needed = 1;
  1342. return r;
  1343. }
  1344. }
  1345. #endif
  1346. } else if (vcpu->arch.osi_needed) {
  1347. u64 *gprs = run->osi.gprs;
  1348. int i;
  1349. for (i = 0; i < 32; i++)
  1350. kvmppc_set_gpr(vcpu, i, gprs[i]);
  1351. vcpu->arch.osi_needed = 0;
  1352. } else if (vcpu->arch.hcall_needed) {
  1353. int i;
  1354. kvmppc_set_gpr(vcpu, 3, run->papr_hcall.ret);
  1355. for (i = 0; i < 9; ++i)
  1356. kvmppc_set_gpr(vcpu, 4 + i, run->papr_hcall.args[i]);
  1357. vcpu->arch.hcall_needed = 0;
  1358. #ifdef CONFIG_BOOKE
  1359. } else if (vcpu->arch.epr_needed) {
  1360. kvmppc_set_epr(vcpu, run->epr.epr);
  1361. vcpu->arch.epr_needed = 0;
  1362. #endif
  1363. }
  1364. kvm_sigset_activate(vcpu);
  1365. if (run->immediate_exit)
  1366. r = -EINTR;
  1367. else
  1368. r = kvmppc_vcpu_run(run, vcpu);
  1369. kvm_sigset_deactivate(vcpu);
  1370. return r;
  1371. }
  1372. int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
  1373. {
  1374. if (irq->irq == KVM_INTERRUPT_UNSET) {
  1375. kvmppc_core_dequeue_external(vcpu);
  1376. return 0;
  1377. }
  1378. kvmppc_core_queue_external(vcpu, irq);
  1379. kvm_vcpu_kick(vcpu);
  1380. return 0;
  1381. }
  1382. static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
  1383. struct kvm_enable_cap *cap)
  1384. {
  1385. int r;
  1386. if (cap->flags)
  1387. return -EINVAL;
  1388. switch (cap->cap) {
  1389. case KVM_CAP_PPC_OSI:
  1390. r = 0;
  1391. vcpu->arch.osi_enabled = true;
  1392. break;
  1393. case KVM_CAP_PPC_PAPR:
  1394. r = 0;
  1395. vcpu->arch.papr_enabled = true;
  1396. break;
  1397. case KVM_CAP_PPC_EPR:
  1398. r = 0;
  1399. if (cap->args[0])
  1400. vcpu->arch.epr_flags |= KVMPPC_EPR_USER;
  1401. else
  1402. vcpu->arch.epr_flags &= ~KVMPPC_EPR_USER;
  1403. break;
  1404. #ifdef CONFIG_BOOKE
  1405. case KVM_CAP_PPC_BOOKE_WATCHDOG:
  1406. r = 0;
  1407. vcpu->arch.watchdog_enabled = true;
  1408. break;
  1409. #endif
  1410. #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
  1411. case KVM_CAP_SW_TLB: {
  1412. struct kvm_config_tlb cfg;
  1413. void __user *user_ptr = (void __user *)(uintptr_t)cap->args[0];
  1414. r = -EFAULT;
  1415. if (copy_from_user(&cfg, user_ptr, sizeof(cfg)))
  1416. break;
  1417. r = kvm_vcpu_ioctl_config_tlb(vcpu, &cfg);
  1418. break;
  1419. }
  1420. #endif
  1421. #ifdef CONFIG_KVM_MPIC
  1422. case KVM_CAP_IRQ_MPIC: {
  1423. struct fd f;
  1424. struct kvm_device *dev;
  1425. r = -EBADF;
  1426. f = fdget(cap->args[0]);
  1427. if (!f.file)
  1428. break;
  1429. r = -EPERM;
  1430. dev = kvm_device_from_filp(f.file);
  1431. if (dev)
  1432. r = kvmppc_mpic_connect_vcpu(dev, vcpu, cap->args[1]);
  1433. fdput(f);
  1434. break;
  1435. }
  1436. #endif
  1437. #ifdef CONFIG_KVM_XICS
  1438. case KVM_CAP_IRQ_XICS: {
  1439. struct fd f;
  1440. struct kvm_device *dev;
  1441. r = -EBADF;
  1442. f = fdget(cap->args[0]);
  1443. if (!f.file)
  1444. break;
  1445. r = -EPERM;
  1446. dev = kvm_device_from_filp(f.file);
  1447. if (dev) {
  1448. if (xive_enabled())
  1449. r = kvmppc_xive_connect_vcpu(dev, vcpu, cap->args[1]);
  1450. else
  1451. r = kvmppc_xics_connect_vcpu(dev, vcpu, cap->args[1]);
  1452. }
  1453. fdput(f);
  1454. break;
  1455. }
  1456. #endif /* CONFIG_KVM_XICS */
  1457. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  1458. case KVM_CAP_PPC_FWNMI:
  1459. r = -EINVAL;
  1460. if (!is_kvmppc_hv_enabled(vcpu->kvm))
  1461. break;
  1462. r = 0;
  1463. vcpu->kvm->arch.fwnmi_enabled = true;
  1464. break;
  1465. #endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */
  1466. default:
  1467. r = -EINVAL;
  1468. break;
  1469. }
  1470. if (!r)
  1471. r = kvmppc_sanity_check(vcpu);
  1472. return r;
  1473. }
  1474. bool kvm_arch_intc_initialized(struct kvm *kvm)
  1475. {
  1476. #ifdef CONFIG_KVM_MPIC
  1477. if (kvm->arch.mpic)
  1478. return true;
  1479. #endif
  1480. #ifdef CONFIG_KVM_XICS
  1481. if (kvm->arch.xics || kvm->arch.xive)
  1482. return true;
  1483. #endif
  1484. return false;
  1485. }
  1486. int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
  1487. struct kvm_mp_state *mp_state)
  1488. {
  1489. return -EINVAL;
  1490. }
  1491. int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
  1492. struct kvm_mp_state *mp_state)
  1493. {
  1494. return -EINVAL;
  1495. }
  1496. long kvm_arch_vcpu_ioctl(struct file *filp,
  1497. unsigned int ioctl, unsigned long arg)
  1498. {
  1499. struct kvm_vcpu *vcpu = filp->private_data;
  1500. void __user *argp = (void __user *)arg;
  1501. long r;
  1502. switch (ioctl) {
  1503. case KVM_INTERRUPT: {
  1504. struct kvm_interrupt irq;
  1505. r = -EFAULT;
  1506. if (copy_from_user(&irq, argp, sizeof(irq)))
  1507. goto out;
  1508. r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
  1509. goto out;
  1510. }
  1511. case KVM_ENABLE_CAP:
  1512. {
  1513. struct kvm_enable_cap cap;
  1514. r = -EFAULT;
  1515. if (copy_from_user(&cap, argp, sizeof(cap)))
  1516. goto out;
  1517. r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
  1518. break;
  1519. }
  1520. case KVM_SET_ONE_REG:
  1521. case KVM_GET_ONE_REG:
  1522. {
  1523. struct kvm_one_reg reg;
  1524. r = -EFAULT;
  1525. if (copy_from_user(&reg, argp, sizeof(reg)))
  1526. goto out;
  1527. if (ioctl == KVM_SET_ONE_REG)
  1528. r = kvm_vcpu_ioctl_set_one_reg(vcpu, &reg);
  1529. else
  1530. r = kvm_vcpu_ioctl_get_one_reg(vcpu, &reg);
  1531. break;
  1532. }
  1533. #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
  1534. case KVM_DIRTY_TLB: {
  1535. struct kvm_dirty_tlb dirty;
  1536. r = -EFAULT;
  1537. if (copy_from_user(&dirty, argp, sizeof(dirty)))
  1538. goto out;
  1539. r = kvm_vcpu_ioctl_dirty_tlb(vcpu, &dirty);
  1540. break;
  1541. }
  1542. #endif
  1543. default:
  1544. r = -EINVAL;
  1545. }
  1546. out:
  1547. return r;
  1548. }
  1549. int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
  1550. {
  1551. return VM_FAULT_SIGBUS;
  1552. }
  1553. static int kvm_vm_ioctl_get_pvinfo(struct kvm_ppc_pvinfo *pvinfo)
  1554. {
  1555. u32 inst_nop = 0x60000000;
  1556. #ifdef CONFIG_KVM_BOOKE_HV
  1557. u32 inst_sc1 = 0x44000022;
  1558. pvinfo->hcall[0] = cpu_to_be32(inst_sc1);
  1559. pvinfo->hcall[1] = cpu_to_be32(inst_nop);
  1560. pvinfo->hcall[2] = cpu_to_be32(inst_nop);
  1561. pvinfo->hcall[3] = cpu_to_be32(inst_nop);
  1562. #else
  1563. u32 inst_lis = 0x3c000000;
  1564. u32 inst_ori = 0x60000000;
  1565. u32 inst_sc = 0x44000002;
  1566. u32 inst_imm_mask = 0xffff;
  1567. /*
  1568. * The hypercall to get into KVM from within guest context is as
  1569. * follows:
  1570. *
  1571. * lis r0, r0, KVM_SC_MAGIC_R0@h
  1572. * ori r0, KVM_SC_MAGIC_R0@l
  1573. * sc
  1574. * nop
  1575. */
  1576. pvinfo->hcall[0] = cpu_to_be32(inst_lis | ((KVM_SC_MAGIC_R0 >> 16) & inst_imm_mask));
  1577. pvinfo->hcall[1] = cpu_to_be32(inst_ori | (KVM_SC_MAGIC_R0 & inst_imm_mask));
  1578. pvinfo->hcall[2] = cpu_to_be32(inst_sc);
  1579. pvinfo->hcall[3] = cpu_to_be32(inst_nop);
  1580. #endif
  1581. pvinfo->flags = KVM_PPC_PVINFO_FLAGS_EV_IDLE;
  1582. return 0;
  1583. }
  1584. int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
  1585. bool line_status)
  1586. {
  1587. if (!irqchip_in_kernel(kvm))
  1588. return -ENXIO;
  1589. irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
  1590. irq_event->irq, irq_event->level,
  1591. line_status);
  1592. return 0;
  1593. }
  1594. static int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
  1595. struct kvm_enable_cap *cap)
  1596. {
  1597. int r;
  1598. if (cap->flags)
  1599. return -EINVAL;
  1600. switch (cap->cap) {
  1601. #ifdef CONFIG_KVM_BOOK3S_64_HANDLER
  1602. case KVM_CAP_PPC_ENABLE_HCALL: {
  1603. unsigned long hcall = cap->args[0];
  1604. r = -EINVAL;
  1605. if (hcall > MAX_HCALL_OPCODE || (hcall & 3) ||
  1606. cap->args[1] > 1)
  1607. break;
  1608. if (!kvmppc_book3s_hcall_implemented(kvm, hcall))
  1609. break;
  1610. if (cap->args[1])
  1611. set_bit(hcall / 4, kvm->arch.enabled_hcalls);
  1612. else
  1613. clear_bit(hcall / 4, kvm->arch.enabled_hcalls);
  1614. r = 0;
  1615. break;
  1616. }
  1617. case KVM_CAP_PPC_SMT: {
  1618. unsigned long mode = cap->args[0];
  1619. unsigned long flags = cap->args[1];
  1620. r = -EINVAL;
  1621. if (kvm->arch.kvm_ops->set_smt_mode)
  1622. r = kvm->arch.kvm_ops->set_smt_mode(kvm, mode, flags);
  1623. break;
  1624. }
  1625. #endif
  1626. default:
  1627. r = -EINVAL;
  1628. break;
  1629. }
  1630. return r;
  1631. }
  1632. long kvm_arch_vm_ioctl(struct file *filp,
  1633. unsigned int ioctl, unsigned long arg)
  1634. {
  1635. struct kvm *kvm __maybe_unused = filp->private_data;
  1636. void __user *argp = (void __user *)arg;
  1637. long r;
  1638. switch (ioctl) {
  1639. case KVM_PPC_GET_PVINFO: {
  1640. struct kvm_ppc_pvinfo pvinfo;
  1641. memset(&pvinfo, 0, sizeof(pvinfo));
  1642. r = kvm_vm_ioctl_get_pvinfo(&pvinfo);
  1643. if (copy_to_user(argp, &pvinfo, sizeof(pvinfo))) {
  1644. r = -EFAULT;
  1645. goto out;
  1646. }
  1647. break;
  1648. }
  1649. case KVM_ENABLE_CAP:
  1650. {
  1651. struct kvm_enable_cap cap;
  1652. r = -EFAULT;
  1653. if (copy_from_user(&cap, argp, sizeof(cap)))
  1654. goto out;
  1655. r = kvm_vm_ioctl_enable_cap(kvm, &cap);
  1656. break;
  1657. }
  1658. #ifdef CONFIG_SPAPR_TCE_IOMMU
  1659. case KVM_CREATE_SPAPR_TCE_64: {
  1660. struct kvm_create_spapr_tce_64 create_tce_64;
  1661. r = -EFAULT;
  1662. if (copy_from_user(&create_tce_64, argp, sizeof(create_tce_64)))
  1663. goto out;
  1664. if (create_tce_64.flags) {
  1665. r = -EINVAL;
  1666. goto out;
  1667. }
  1668. r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce_64);
  1669. goto out;
  1670. }
  1671. case KVM_CREATE_SPAPR_TCE: {
  1672. struct kvm_create_spapr_tce create_tce;
  1673. struct kvm_create_spapr_tce_64 create_tce_64;
  1674. r = -EFAULT;
  1675. if (copy_from_user(&create_tce, argp, sizeof(create_tce)))
  1676. goto out;
  1677. create_tce_64.liobn = create_tce.liobn;
  1678. create_tce_64.page_shift = IOMMU_PAGE_SHIFT_4K;
  1679. create_tce_64.offset = 0;
  1680. create_tce_64.size = create_tce.window_size >>
  1681. IOMMU_PAGE_SHIFT_4K;
  1682. create_tce_64.flags = 0;
  1683. r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce_64);
  1684. goto out;
  1685. }
  1686. #endif
  1687. #ifdef CONFIG_PPC_BOOK3S_64
  1688. case KVM_PPC_GET_SMMU_INFO: {
  1689. struct kvm_ppc_smmu_info info;
  1690. struct kvm *kvm = filp->private_data;
  1691. memset(&info, 0, sizeof(info));
  1692. r = kvm->arch.kvm_ops->get_smmu_info(kvm, &info);
  1693. if (r >= 0 && copy_to_user(argp, &info, sizeof(info)))
  1694. r = -EFAULT;
  1695. break;
  1696. }
  1697. case KVM_PPC_RTAS_DEFINE_TOKEN: {
  1698. struct kvm *kvm = filp->private_data;
  1699. r = kvm_vm_ioctl_rtas_define_token(kvm, argp);
  1700. break;
  1701. }
  1702. case KVM_PPC_CONFIGURE_V3_MMU: {
  1703. struct kvm *kvm = filp->private_data;
  1704. struct kvm_ppc_mmuv3_cfg cfg;
  1705. r = -EINVAL;
  1706. if (!kvm->arch.kvm_ops->configure_mmu)
  1707. goto out;
  1708. r = -EFAULT;
  1709. if (copy_from_user(&cfg, argp, sizeof(cfg)))
  1710. goto out;
  1711. r = kvm->arch.kvm_ops->configure_mmu(kvm, &cfg);
  1712. break;
  1713. }
  1714. case KVM_PPC_GET_RMMU_INFO: {
  1715. struct kvm *kvm = filp->private_data;
  1716. struct kvm_ppc_rmmu_info info;
  1717. r = -EINVAL;
  1718. if (!kvm->arch.kvm_ops->get_rmmu_info)
  1719. goto out;
  1720. r = kvm->arch.kvm_ops->get_rmmu_info(kvm, &info);
  1721. if (r >= 0 && copy_to_user(argp, &info, sizeof(info)))
  1722. r = -EFAULT;
  1723. break;
  1724. }
  1725. default: {
  1726. struct kvm *kvm = filp->private_data;
  1727. r = kvm->arch.kvm_ops->arch_vm_ioctl(filp, ioctl, arg);
  1728. }
  1729. #else /* CONFIG_PPC_BOOK3S_64 */
  1730. default:
  1731. r = -ENOTTY;
  1732. #endif
  1733. }
  1734. out:
  1735. return r;
  1736. }
  1737. static unsigned long lpid_inuse[BITS_TO_LONGS(KVMPPC_NR_LPIDS)];
  1738. static unsigned long nr_lpids;
  1739. long kvmppc_alloc_lpid(void)
  1740. {
  1741. long lpid;
  1742. do {
  1743. lpid = find_first_zero_bit(lpid_inuse, KVMPPC_NR_LPIDS);
  1744. if (lpid >= nr_lpids) {
  1745. pr_err("%s: No LPIDs free\n", __func__);
  1746. return -ENOMEM;
  1747. }
  1748. } while (test_and_set_bit(lpid, lpid_inuse));
  1749. return lpid;
  1750. }
  1751. EXPORT_SYMBOL_GPL(kvmppc_alloc_lpid);
  1752. void kvmppc_claim_lpid(long lpid)
  1753. {
  1754. set_bit(lpid, lpid_inuse);
  1755. }
  1756. EXPORT_SYMBOL_GPL(kvmppc_claim_lpid);
  1757. void kvmppc_free_lpid(long lpid)
  1758. {
  1759. clear_bit(lpid, lpid_inuse);
  1760. }
  1761. EXPORT_SYMBOL_GPL(kvmppc_free_lpid);
  1762. void kvmppc_init_lpid(unsigned long nr_lpids_param)
  1763. {
  1764. nr_lpids = min_t(unsigned long, KVMPPC_NR_LPIDS, nr_lpids_param);
  1765. memset(lpid_inuse, 0, sizeof(lpid_inuse));
  1766. }
  1767. EXPORT_SYMBOL_GPL(kvmppc_init_lpid);
  1768. int kvm_arch_init(void *opaque)
  1769. {
  1770. return 0;
  1771. }
  1772. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ppc_instr);