book3s_hv.c 102 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998
  1. /*
  2. * Copyright 2011 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
  3. * Copyright (C) 2009. SUSE Linux Products GmbH. All rights reserved.
  4. *
  5. * Authors:
  6. * Paul Mackerras <paulus@au1.ibm.com>
  7. * Alexander Graf <agraf@suse.de>
  8. * Kevin Wolf <mail@kevin-wolf.de>
  9. *
  10. * Description: KVM functions specific to running on Book 3S
  11. * processors in hypervisor mode (specifically POWER7 and later).
  12. *
  13. * This file is derived from arch/powerpc/kvm/book3s.c,
  14. * by Alexander Graf <agraf@suse.de>.
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License, version 2, as
  18. * published by the Free Software Foundation.
  19. */
  20. #include <linux/kvm_host.h>
  21. #include <linux/err.h>
  22. #include <linux/slab.h>
  23. #include <linux/preempt.h>
  24. #include <linux/sched/signal.h>
  25. #include <linux/sched/stat.h>
  26. #include <linux/delay.h>
  27. #include <linux/export.h>
  28. #include <linux/fs.h>
  29. #include <linux/anon_inodes.h>
  30. #include <linux/cpu.h>
  31. #include <linux/cpumask.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/page-flags.h>
  34. #include <linux/srcu.h>
  35. #include <linux/miscdevice.h>
  36. #include <linux/debugfs.h>
  37. #include <linux/gfp.h>
  38. #include <linux/vmalloc.h>
  39. #include <linux/highmem.h>
  40. #include <linux/hugetlb.h>
  41. #include <linux/kvm_irqfd.h>
  42. #include <linux/irqbypass.h>
  43. #include <linux/module.h>
  44. #include <linux/compiler.h>
  45. #include <linux/of.h>
  46. #include <asm/reg.h>
  47. #include <asm/cputable.h>
  48. #include <asm/cacheflush.h>
  49. #include <asm/tlbflush.h>
  50. #include <linux/uaccess.h>
  51. #include <asm/io.h>
  52. #include <asm/kvm_ppc.h>
  53. #include <asm/kvm_book3s.h>
  54. #include <asm/mmu_context.h>
  55. #include <asm/lppaca.h>
  56. #include <asm/processor.h>
  57. #include <asm/cputhreads.h>
  58. #include <asm/page.h>
  59. #include <asm/hvcall.h>
  60. #include <asm/switch_to.h>
  61. #include <asm/smp.h>
  62. #include <asm/dbell.h>
  63. #include <asm/hmi.h>
  64. #include <asm/pnv-pci.h>
  65. #include <asm/mmu.h>
  66. #include <asm/opal.h>
  67. #include <asm/xics.h>
  68. #include <asm/xive.h>
  69. #include "book3s.h"
  70. #define CREATE_TRACE_POINTS
  71. #include "trace_hv.h"
  72. /* #define EXIT_DEBUG */
  73. /* #define EXIT_DEBUG_SIMPLE */
  74. /* #define EXIT_DEBUG_INT */
  75. /* Used to indicate that a guest page fault needs to be handled */
  76. #define RESUME_PAGE_FAULT (RESUME_GUEST | RESUME_FLAG_ARCH1)
  77. /* Used to indicate that a guest passthrough interrupt needs to be handled */
  78. #define RESUME_PASSTHROUGH (RESUME_GUEST | RESUME_FLAG_ARCH2)
  79. /* Used as a "null" value for timebase values */
  80. #define TB_NIL (~(u64)0)
  81. static DECLARE_BITMAP(default_enabled_hcalls, MAX_HCALL_OPCODE/4 + 1);
  82. static int dynamic_mt_modes = 6;
  83. module_param(dynamic_mt_modes, int, S_IRUGO | S_IWUSR);
  84. MODULE_PARM_DESC(dynamic_mt_modes, "Set of allowed dynamic micro-threading modes: 0 (= none), 2, 4, or 6 (= 2 or 4)");
  85. static int target_smt_mode;
  86. module_param(target_smt_mode, int, S_IRUGO | S_IWUSR);
  87. MODULE_PARM_DESC(target_smt_mode, "Target threads per core (0 = max)");
  88. #ifdef CONFIG_KVM_XICS
  89. static struct kernel_param_ops module_param_ops = {
  90. .set = param_set_int,
  91. .get = param_get_int,
  92. };
  93. module_param_cb(kvm_irq_bypass, &module_param_ops, &kvm_irq_bypass,
  94. S_IRUGO | S_IWUSR);
  95. MODULE_PARM_DESC(kvm_irq_bypass, "Bypass passthrough interrupt optimization");
  96. module_param_cb(h_ipi_redirect, &module_param_ops, &h_ipi_redirect,
  97. S_IRUGO | S_IWUSR);
  98. MODULE_PARM_DESC(h_ipi_redirect, "Redirect H_IPI wakeup to a free host core");
  99. #endif
  100. static void kvmppc_end_cede(struct kvm_vcpu *vcpu);
  101. static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu);
  102. static inline struct kvm_vcpu *next_runnable_thread(struct kvmppc_vcore *vc,
  103. int *ip)
  104. {
  105. int i = *ip;
  106. struct kvm_vcpu *vcpu;
  107. while (++i < MAX_SMT_THREADS) {
  108. vcpu = READ_ONCE(vc->runnable_threads[i]);
  109. if (vcpu) {
  110. *ip = i;
  111. return vcpu;
  112. }
  113. }
  114. return NULL;
  115. }
  116. /* Used to traverse the list of runnable threads for a given vcore */
  117. #define for_each_runnable_thread(i, vcpu, vc) \
  118. for (i = -1; (vcpu = next_runnable_thread(vc, &i)); )
  119. static bool kvmppc_ipi_thread(int cpu)
  120. {
  121. unsigned long msg = PPC_DBELL_TYPE(PPC_DBELL_SERVER);
  122. /* On POWER9 we can use msgsnd to IPI any cpu */
  123. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  124. msg |= get_hard_smp_processor_id(cpu);
  125. smp_mb();
  126. __asm__ __volatile__ (PPC_MSGSND(%0) : : "r" (msg));
  127. return true;
  128. }
  129. /* On POWER8 for IPIs to threads in the same core, use msgsnd */
  130. if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
  131. preempt_disable();
  132. if (cpu_first_thread_sibling(cpu) ==
  133. cpu_first_thread_sibling(smp_processor_id())) {
  134. msg |= cpu_thread_in_core(cpu);
  135. smp_mb();
  136. __asm__ __volatile__ (PPC_MSGSND(%0) : : "r" (msg));
  137. preempt_enable();
  138. return true;
  139. }
  140. preempt_enable();
  141. }
  142. #if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
  143. if (cpu >= 0 && cpu < nr_cpu_ids) {
  144. if (paca[cpu].kvm_hstate.xics_phys) {
  145. xics_wake_cpu(cpu);
  146. return true;
  147. }
  148. opal_int_set_mfrr(get_hard_smp_processor_id(cpu), IPI_PRIORITY);
  149. return true;
  150. }
  151. #endif
  152. return false;
  153. }
  154. static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu *vcpu)
  155. {
  156. int cpu;
  157. struct swait_queue_head *wqp;
  158. wqp = kvm_arch_vcpu_wq(vcpu);
  159. if (swait_active(wqp)) {
  160. swake_up(wqp);
  161. ++vcpu->stat.halt_wakeup;
  162. }
  163. cpu = READ_ONCE(vcpu->arch.thread_cpu);
  164. if (cpu >= 0 && kvmppc_ipi_thread(cpu))
  165. return;
  166. /* CPU points to the first thread of the core */
  167. cpu = vcpu->cpu;
  168. if (cpu >= 0 && cpu < nr_cpu_ids && cpu_online(cpu))
  169. smp_send_reschedule(cpu);
  170. }
  171. /*
  172. * We use the vcpu_load/put functions to measure stolen time.
  173. * Stolen time is counted as time when either the vcpu is able to
  174. * run as part of a virtual core, but the task running the vcore
  175. * is preempted or sleeping, or when the vcpu needs something done
  176. * in the kernel by the task running the vcpu, but that task is
  177. * preempted or sleeping. Those two things have to be counted
  178. * separately, since one of the vcpu tasks will take on the job
  179. * of running the core, and the other vcpu tasks in the vcore will
  180. * sleep waiting for it to do that, but that sleep shouldn't count
  181. * as stolen time.
  182. *
  183. * Hence we accumulate stolen time when the vcpu can run as part of
  184. * a vcore using vc->stolen_tb, and the stolen time when the vcpu
  185. * needs its task to do other things in the kernel (for example,
  186. * service a page fault) in busy_stolen. We don't accumulate
  187. * stolen time for a vcore when it is inactive, or for a vcpu
  188. * when it is in state RUNNING or NOTREADY. NOTREADY is a bit of
  189. * a misnomer; it means that the vcpu task is not executing in
  190. * the KVM_VCPU_RUN ioctl, i.e. it is in userspace or elsewhere in
  191. * the kernel. We don't have any way of dividing up that time
  192. * between time that the vcpu is genuinely stopped, time that
  193. * the task is actively working on behalf of the vcpu, and time
  194. * that the task is preempted, so we don't count any of it as
  195. * stolen.
  196. *
  197. * Updates to busy_stolen are protected by arch.tbacct_lock;
  198. * updates to vc->stolen_tb are protected by the vcore->stoltb_lock
  199. * lock. The stolen times are measured in units of timebase ticks.
  200. * (Note that the != TB_NIL checks below are purely defensive;
  201. * they should never fail.)
  202. */
  203. static void kvmppc_core_start_stolen(struct kvmppc_vcore *vc)
  204. {
  205. unsigned long flags;
  206. spin_lock_irqsave(&vc->stoltb_lock, flags);
  207. vc->preempt_tb = mftb();
  208. spin_unlock_irqrestore(&vc->stoltb_lock, flags);
  209. }
  210. static void kvmppc_core_end_stolen(struct kvmppc_vcore *vc)
  211. {
  212. unsigned long flags;
  213. spin_lock_irqsave(&vc->stoltb_lock, flags);
  214. if (vc->preempt_tb != TB_NIL) {
  215. vc->stolen_tb += mftb() - vc->preempt_tb;
  216. vc->preempt_tb = TB_NIL;
  217. }
  218. spin_unlock_irqrestore(&vc->stoltb_lock, flags);
  219. }
  220. static void kvmppc_core_vcpu_load_hv(struct kvm_vcpu *vcpu, int cpu)
  221. {
  222. struct kvmppc_vcore *vc = vcpu->arch.vcore;
  223. unsigned long flags;
  224. /*
  225. * We can test vc->runner without taking the vcore lock,
  226. * because only this task ever sets vc->runner to this
  227. * vcpu, and once it is set to this vcpu, only this task
  228. * ever sets it to NULL.
  229. */
  230. if (vc->runner == vcpu && vc->vcore_state >= VCORE_SLEEPING)
  231. kvmppc_core_end_stolen(vc);
  232. spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
  233. if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST &&
  234. vcpu->arch.busy_preempt != TB_NIL) {
  235. vcpu->arch.busy_stolen += mftb() - vcpu->arch.busy_preempt;
  236. vcpu->arch.busy_preempt = TB_NIL;
  237. }
  238. spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
  239. }
  240. static void kvmppc_core_vcpu_put_hv(struct kvm_vcpu *vcpu)
  241. {
  242. struct kvmppc_vcore *vc = vcpu->arch.vcore;
  243. unsigned long flags;
  244. if (vc->runner == vcpu && vc->vcore_state >= VCORE_SLEEPING)
  245. kvmppc_core_start_stolen(vc);
  246. spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
  247. if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST)
  248. vcpu->arch.busy_preempt = mftb();
  249. spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
  250. }
  251. static void kvmppc_set_msr_hv(struct kvm_vcpu *vcpu, u64 msr)
  252. {
  253. /*
  254. * Check for illegal transactional state bit combination
  255. * and if we find it, force the TS field to a safe state.
  256. */
  257. if ((msr & MSR_TS_MASK) == MSR_TS_MASK)
  258. msr &= ~MSR_TS_MASK;
  259. vcpu->arch.shregs.msr = msr;
  260. kvmppc_end_cede(vcpu);
  261. }
  262. static void kvmppc_set_pvr_hv(struct kvm_vcpu *vcpu, u32 pvr)
  263. {
  264. vcpu->arch.pvr = pvr;
  265. }
  266. /* Dummy value used in computing PCR value below */
  267. #define PCR_ARCH_300 (PCR_ARCH_207 << 1)
  268. static int kvmppc_set_arch_compat(struct kvm_vcpu *vcpu, u32 arch_compat)
  269. {
  270. unsigned long host_pcr_bit = 0, guest_pcr_bit = 0;
  271. struct kvmppc_vcore *vc = vcpu->arch.vcore;
  272. /* We can (emulate) our own architecture version and anything older */
  273. if (cpu_has_feature(CPU_FTR_ARCH_300))
  274. host_pcr_bit = PCR_ARCH_300;
  275. else if (cpu_has_feature(CPU_FTR_ARCH_207S))
  276. host_pcr_bit = PCR_ARCH_207;
  277. else if (cpu_has_feature(CPU_FTR_ARCH_206))
  278. host_pcr_bit = PCR_ARCH_206;
  279. else
  280. host_pcr_bit = PCR_ARCH_205;
  281. /* Determine lowest PCR bit needed to run guest in given PVR level */
  282. guest_pcr_bit = host_pcr_bit;
  283. if (arch_compat) {
  284. switch (arch_compat) {
  285. case PVR_ARCH_205:
  286. guest_pcr_bit = PCR_ARCH_205;
  287. break;
  288. case PVR_ARCH_206:
  289. case PVR_ARCH_206p:
  290. guest_pcr_bit = PCR_ARCH_206;
  291. break;
  292. case PVR_ARCH_207:
  293. guest_pcr_bit = PCR_ARCH_207;
  294. break;
  295. case PVR_ARCH_300:
  296. guest_pcr_bit = PCR_ARCH_300;
  297. break;
  298. default:
  299. return -EINVAL;
  300. }
  301. }
  302. /* Check requested PCR bits don't exceed our capabilities */
  303. if (guest_pcr_bit > host_pcr_bit)
  304. return -EINVAL;
  305. spin_lock(&vc->lock);
  306. vc->arch_compat = arch_compat;
  307. /* Set all PCR bits for which guest_pcr_bit <= bit < host_pcr_bit */
  308. vc->pcr = host_pcr_bit - guest_pcr_bit;
  309. spin_unlock(&vc->lock);
  310. return 0;
  311. }
  312. static void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
  313. {
  314. int r;
  315. pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id);
  316. pr_err("pc = %.16lx msr = %.16llx trap = %x\n",
  317. vcpu->arch.pc, vcpu->arch.shregs.msr, vcpu->arch.trap);
  318. for (r = 0; r < 16; ++r)
  319. pr_err("r%2d = %.16lx r%d = %.16lx\n",
  320. r, kvmppc_get_gpr(vcpu, r),
  321. r+16, kvmppc_get_gpr(vcpu, r+16));
  322. pr_err("ctr = %.16lx lr = %.16lx\n",
  323. vcpu->arch.ctr, vcpu->arch.lr);
  324. pr_err("srr0 = %.16llx srr1 = %.16llx\n",
  325. vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1);
  326. pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
  327. vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1);
  328. pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
  329. vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3);
  330. pr_err("cr = %.8x xer = %.16lx dsisr = %.8x\n",
  331. vcpu->arch.cr, vcpu->arch.xer, vcpu->arch.shregs.dsisr);
  332. pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar);
  333. pr_err("fault dar = %.16lx dsisr = %.8x\n",
  334. vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
  335. pr_err("SLB (%d entries):\n", vcpu->arch.slb_max);
  336. for (r = 0; r < vcpu->arch.slb_max; ++r)
  337. pr_err(" ESID = %.16llx VSID = %.16llx\n",
  338. vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv);
  339. pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n",
  340. vcpu->arch.vcore->lpcr, vcpu->kvm->arch.sdr1,
  341. vcpu->arch.last_inst);
  342. }
  343. static struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id)
  344. {
  345. struct kvm_vcpu *ret;
  346. mutex_lock(&kvm->lock);
  347. ret = kvm_get_vcpu_by_id(kvm, id);
  348. mutex_unlock(&kvm->lock);
  349. return ret;
  350. }
  351. static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa)
  352. {
  353. vpa->__old_status |= LPPACA_OLD_SHARED_PROC;
  354. vpa->yield_count = cpu_to_be32(1);
  355. }
  356. static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v,
  357. unsigned long addr, unsigned long len)
  358. {
  359. /* check address is cacheline aligned */
  360. if (addr & (L1_CACHE_BYTES - 1))
  361. return -EINVAL;
  362. spin_lock(&vcpu->arch.vpa_update_lock);
  363. if (v->next_gpa != addr || v->len != len) {
  364. v->next_gpa = addr;
  365. v->len = addr ? len : 0;
  366. v->update_pending = 1;
  367. }
  368. spin_unlock(&vcpu->arch.vpa_update_lock);
  369. return 0;
  370. }
  371. /* Length for a per-processor buffer is passed in at offset 4 in the buffer */
  372. struct reg_vpa {
  373. u32 dummy;
  374. union {
  375. __be16 hword;
  376. __be32 word;
  377. } length;
  378. };
  379. static int vpa_is_registered(struct kvmppc_vpa *vpap)
  380. {
  381. if (vpap->update_pending)
  382. return vpap->next_gpa != 0;
  383. return vpap->pinned_addr != NULL;
  384. }
  385. static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
  386. unsigned long flags,
  387. unsigned long vcpuid, unsigned long vpa)
  388. {
  389. struct kvm *kvm = vcpu->kvm;
  390. unsigned long len, nb;
  391. void *va;
  392. struct kvm_vcpu *tvcpu;
  393. int err;
  394. int subfunc;
  395. struct kvmppc_vpa *vpap;
  396. tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
  397. if (!tvcpu)
  398. return H_PARAMETER;
  399. subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK;
  400. if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL ||
  401. subfunc == H_VPA_REG_SLB) {
  402. /* Registering new area - address must be cache-line aligned */
  403. if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa)
  404. return H_PARAMETER;
  405. /* convert logical addr to kernel addr and read length */
  406. va = kvmppc_pin_guest_page(kvm, vpa, &nb);
  407. if (va == NULL)
  408. return H_PARAMETER;
  409. if (subfunc == H_VPA_REG_VPA)
  410. len = be16_to_cpu(((struct reg_vpa *)va)->length.hword);
  411. else
  412. len = be32_to_cpu(((struct reg_vpa *)va)->length.word);
  413. kvmppc_unpin_guest_page(kvm, va, vpa, false);
  414. /* Check length */
  415. if (len > nb || len < sizeof(struct reg_vpa))
  416. return H_PARAMETER;
  417. } else {
  418. vpa = 0;
  419. len = 0;
  420. }
  421. err = H_PARAMETER;
  422. vpap = NULL;
  423. spin_lock(&tvcpu->arch.vpa_update_lock);
  424. switch (subfunc) {
  425. case H_VPA_REG_VPA: /* register VPA */
  426. if (len < sizeof(struct lppaca))
  427. break;
  428. vpap = &tvcpu->arch.vpa;
  429. err = 0;
  430. break;
  431. case H_VPA_REG_DTL: /* register DTL */
  432. if (len < sizeof(struct dtl_entry))
  433. break;
  434. len -= len % sizeof(struct dtl_entry);
  435. /* Check that they have previously registered a VPA */
  436. err = H_RESOURCE;
  437. if (!vpa_is_registered(&tvcpu->arch.vpa))
  438. break;
  439. vpap = &tvcpu->arch.dtl;
  440. err = 0;
  441. break;
  442. case H_VPA_REG_SLB: /* register SLB shadow buffer */
  443. /* Check that they have previously registered a VPA */
  444. err = H_RESOURCE;
  445. if (!vpa_is_registered(&tvcpu->arch.vpa))
  446. break;
  447. vpap = &tvcpu->arch.slb_shadow;
  448. err = 0;
  449. break;
  450. case H_VPA_DEREG_VPA: /* deregister VPA */
  451. /* Check they don't still have a DTL or SLB buf registered */
  452. err = H_RESOURCE;
  453. if (vpa_is_registered(&tvcpu->arch.dtl) ||
  454. vpa_is_registered(&tvcpu->arch.slb_shadow))
  455. break;
  456. vpap = &tvcpu->arch.vpa;
  457. err = 0;
  458. break;
  459. case H_VPA_DEREG_DTL: /* deregister DTL */
  460. vpap = &tvcpu->arch.dtl;
  461. err = 0;
  462. break;
  463. case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */
  464. vpap = &tvcpu->arch.slb_shadow;
  465. err = 0;
  466. break;
  467. }
  468. if (vpap) {
  469. vpap->next_gpa = vpa;
  470. vpap->len = len;
  471. vpap->update_pending = 1;
  472. }
  473. spin_unlock(&tvcpu->arch.vpa_update_lock);
  474. return err;
  475. }
  476. static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap)
  477. {
  478. struct kvm *kvm = vcpu->kvm;
  479. void *va;
  480. unsigned long nb;
  481. unsigned long gpa;
  482. /*
  483. * We need to pin the page pointed to by vpap->next_gpa,
  484. * but we can't call kvmppc_pin_guest_page under the lock
  485. * as it does get_user_pages() and down_read(). So we
  486. * have to drop the lock, pin the page, then get the lock
  487. * again and check that a new area didn't get registered
  488. * in the meantime.
  489. */
  490. for (;;) {
  491. gpa = vpap->next_gpa;
  492. spin_unlock(&vcpu->arch.vpa_update_lock);
  493. va = NULL;
  494. nb = 0;
  495. if (gpa)
  496. va = kvmppc_pin_guest_page(kvm, gpa, &nb);
  497. spin_lock(&vcpu->arch.vpa_update_lock);
  498. if (gpa == vpap->next_gpa)
  499. break;
  500. /* sigh... unpin that one and try again */
  501. if (va)
  502. kvmppc_unpin_guest_page(kvm, va, gpa, false);
  503. }
  504. vpap->update_pending = 0;
  505. if (va && nb < vpap->len) {
  506. /*
  507. * If it's now too short, it must be that userspace
  508. * has changed the mappings underlying guest memory,
  509. * so unregister the region.
  510. */
  511. kvmppc_unpin_guest_page(kvm, va, gpa, false);
  512. va = NULL;
  513. }
  514. if (vpap->pinned_addr)
  515. kvmppc_unpin_guest_page(kvm, vpap->pinned_addr, vpap->gpa,
  516. vpap->dirty);
  517. vpap->gpa = gpa;
  518. vpap->pinned_addr = va;
  519. vpap->dirty = false;
  520. if (va)
  521. vpap->pinned_end = va + vpap->len;
  522. }
  523. static void kvmppc_update_vpas(struct kvm_vcpu *vcpu)
  524. {
  525. if (!(vcpu->arch.vpa.update_pending ||
  526. vcpu->arch.slb_shadow.update_pending ||
  527. vcpu->arch.dtl.update_pending))
  528. return;
  529. spin_lock(&vcpu->arch.vpa_update_lock);
  530. if (vcpu->arch.vpa.update_pending) {
  531. kvmppc_update_vpa(vcpu, &vcpu->arch.vpa);
  532. if (vcpu->arch.vpa.pinned_addr)
  533. init_vpa(vcpu, vcpu->arch.vpa.pinned_addr);
  534. }
  535. if (vcpu->arch.dtl.update_pending) {
  536. kvmppc_update_vpa(vcpu, &vcpu->arch.dtl);
  537. vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr;
  538. vcpu->arch.dtl_index = 0;
  539. }
  540. if (vcpu->arch.slb_shadow.update_pending)
  541. kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow);
  542. spin_unlock(&vcpu->arch.vpa_update_lock);
  543. }
  544. /*
  545. * Return the accumulated stolen time for the vcore up until `now'.
  546. * The caller should hold the vcore lock.
  547. */
  548. static u64 vcore_stolen_time(struct kvmppc_vcore *vc, u64 now)
  549. {
  550. u64 p;
  551. unsigned long flags;
  552. spin_lock_irqsave(&vc->stoltb_lock, flags);
  553. p = vc->stolen_tb;
  554. if (vc->vcore_state != VCORE_INACTIVE &&
  555. vc->preempt_tb != TB_NIL)
  556. p += now - vc->preempt_tb;
  557. spin_unlock_irqrestore(&vc->stoltb_lock, flags);
  558. return p;
  559. }
  560. static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu,
  561. struct kvmppc_vcore *vc)
  562. {
  563. struct dtl_entry *dt;
  564. struct lppaca *vpa;
  565. unsigned long stolen;
  566. unsigned long core_stolen;
  567. u64 now;
  568. dt = vcpu->arch.dtl_ptr;
  569. vpa = vcpu->arch.vpa.pinned_addr;
  570. now = mftb();
  571. core_stolen = vcore_stolen_time(vc, now);
  572. stolen = core_stolen - vcpu->arch.stolen_logged;
  573. vcpu->arch.stolen_logged = core_stolen;
  574. spin_lock_irq(&vcpu->arch.tbacct_lock);
  575. stolen += vcpu->arch.busy_stolen;
  576. vcpu->arch.busy_stolen = 0;
  577. spin_unlock_irq(&vcpu->arch.tbacct_lock);
  578. if (!dt || !vpa)
  579. return;
  580. memset(dt, 0, sizeof(struct dtl_entry));
  581. dt->dispatch_reason = 7;
  582. dt->processor_id = cpu_to_be16(vc->pcpu + vcpu->arch.ptid);
  583. dt->timebase = cpu_to_be64(now + vc->tb_offset);
  584. dt->enqueue_to_dispatch_time = cpu_to_be32(stolen);
  585. dt->srr0 = cpu_to_be64(kvmppc_get_pc(vcpu));
  586. dt->srr1 = cpu_to_be64(vcpu->arch.shregs.msr);
  587. ++dt;
  588. if (dt == vcpu->arch.dtl.pinned_end)
  589. dt = vcpu->arch.dtl.pinned_addr;
  590. vcpu->arch.dtl_ptr = dt;
  591. /* order writing *dt vs. writing vpa->dtl_idx */
  592. smp_wmb();
  593. vpa->dtl_idx = cpu_to_be64(++vcpu->arch.dtl_index);
  594. vcpu->arch.dtl.dirty = true;
  595. }
  596. static bool kvmppc_power8_compatible(struct kvm_vcpu *vcpu)
  597. {
  598. if (vcpu->arch.vcore->arch_compat >= PVR_ARCH_207)
  599. return true;
  600. if ((!vcpu->arch.vcore->arch_compat) &&
  601. cpu_has_feature(CPU_FTR_ARCH_207S))
  602. return true;
  603. return false;
  604. }
  605. static int kvmppc_h_set_mode(struct kvm_vcpu *vcpu, unsigned long mflags,
  606. unsigned long resource, unsigned long value1,
  607. unsigned long value2)
  608. {
  609. switch (resource) {
  610. case H_SET_MODE_RESOURCE_SET_CIABR:
  611. if (!kvmppc_power8_compatible(vcpu))
  612. return H_P2;
  613. if (value2)
  614. return H_P4;
  615. if (mflags)
  616. return H_UNSUPPORTED_FLAG_START;
  617. /* Guests can't breakpoint the hypervisor */
  618. if ((value1 & CIABR_PRIV) == CIABR_PRIV_HYPER)
  619. return H_P3;
  620. vcpu->arch.ciabr = value1;
  621. return H_SUCCESS;
  622. case H_SET_MODE_RESOURCE_SET_DAWR:
  623. if (!kvmppc_power8_compatible(vcpu))
  624. return H_P2;
  625. if (mflags)
  626. return H_UNSUPPORTED_FLAG_START;
  627. if (value2 & DABRX_HYP)
  628. return H_P4;
  629. vcpu->arch.dawr = value1;
  630. vcpu->arch.dawrx = value2;
  631. return H_SUCCESS;
  632. default:
  633. return H_TOO_HARD;
  634. }
  635. }
  636. static int kvm_arch_vcpu_yield_to(struct kvm_vcpu *target)
  637. {
  638. struct kvmppc_vcore *vcore = target->arch.vcore;
  639. /*
  640. * We expect to have been called by the real mode handler
  641. * (kvmppc_rm_h_confer()) which would have directly returned
  642. * H_SUCCESS if the source vcore wasn't idle (e.g. if it may
  643. * have useful work to do and should not confer) so we don't
  644. * recheck that here.
  645. */
  646. spin_lock(&vcore->lock);
  647. if (target->arch.state == KVMPPC_VCPU_RUNNABLE &&
  648. vcore->vcore_state != VCORE_INACTIVE &&
  649. vcore->runner)
  650. target = vcore->runner;
  651. spin_unlock(&vcore->lock);
  652. return kvm_vcpu_yield_to(target);
  653. }
  654. static int kvmppc_get_yield_count(struct kvm_vcpu *vcpu)
  655. {
  656. int yield_count = 0;
  657. struct lppaca *lppaca;
  658. spin_lock(&vcpu->arch.vpa_update_lock);
  659. lppaca = (struct lppaca *)vcpu->arch.vpa.pinned_addr;
  660. if (lppaca)
  661. yield_count = be32_to_cpu(lppaca->yield_count);
  662. spin_unlock(&vcpu->arch.vpa_update_lock);
  663. return yield_count;
  664. }
  665. int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
  666. {
  667. unsigned long req = kvmppc_get_gpr(vcpu, 3);
  668. unsigned long target, ret = H_SUCCESS;
  669. int yield_count;
  670. struct kvm_vcpu *tvcpu;
  671. int idx, rc;
  672. if (req <= MAX_HCALL_OPCODE &&
  673. !test_bit(req/4, vcpu->kvm->arch.enabled_hcalls))
  674. return RESUME_HOST;
  675. switch (req) {
  676. case H_CEDE:
  677. break;
  678. case H_PROD:
  679. target = kvmppc_get_gpr(vcpu, 4);
  680. tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
  681. if (!tvcpu) {
  682. ret = H_PARAMETER;
  683. break;
  684. }
  685. tvcpu->arch.prodded = 1;
  686. smp_mb();
  687. if (tvcpu->arch.ceded)
  688. kvmppc_fast_vcpu_kick_hv(tvcpu);
  689. break;
  690. case H_CONFER:
  691. target = kvmppc_get_gpr(vcpu, 4);
  692. if (target == -1)
  693. break;
  694. tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
  695. if (!tvcpu) {
  696. ret = H_PARAMETER;
  697. break;
  698. }
  699. yield_count = kvmppc_get_gpr(vcpu, 5);
  700. if (kvmppc_get_yield_count(tvcpu) != yield_count)
  701. break;
  702. kvm_arch_vcpu_yield_to(tvcpu);
  703. break;
  704. case H_REGISTER_VPA:
  705. ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
  706. kvmppc_get_gpr(vcpu, 5),
  707. kvmppc_get_gpr(vcpu, 6));
  708. break;
  709. case H_RTAS:
  710. if (list_empty(&vcpu->kvm->arch.rtas_tokens))
  711. return RESUME_HOST;
  712. idx = srcu_read_lock(&vcpu->kvm->srcu);
  713. rc = kvmppc_rtas_hcall(vcpu);
  714. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  715. if (rc == -ENOENT)
  716. return RESUME_HOST;
  717. else if (rc == 0)
  718. break;
  719. /* Send the error out to userspace via KVM_RUN */
  720. return rc;
  721. case H_LOGICAL_CI_LOAD:
  722. ret = kvmppc_h_logical_ci_load(vcpu);
  723. if (ret == H_TOO_HARD)
  724. return RESUME_HOST;
  725. break;
  726. case H_LOGICAL_CI_STORE:
  727. ret = kvmppc_h_logical_ci_store(vcpu);
  728. if (ret == H_TOO_HARD)
  729. return RESUME_HOST;
  730. break;
  731. case H_SET_MODE:
  732. ret = kvmppc_h_set_mode(vcpu, kvmppc_get_gpr(vcpu, 4),
  733. kvmppc_get_gpr(vcpu, 5),
  734. kvmppc_get_gpr(vcpu, 6),
  735. kvmppc_get_gpr(vcpu, 7));
  736. if (ret == H_TOO_HARD)
  737. return RESUME_HOST;
  738. break;
  739. case H_XIRR:
  740. case H_CPPR:
  741. case H_EOI:
  742. case H_IPI:
  743. case H_IPOLL:
  744. case H_XIRR_X:
  745. if (kvmppc_xics_enabled(vcpu)) {
  746. if (xive_enabled()) {
  747. ret = H_NOT_AVAILABLE;
  748. return RESUME_GUEST;
  749. }
  750. ret = kvmppc_xics_hcall(vcpu, req);
  751. break;
  752. }
  753. return RESUME_HOST;
  754. case H_PUT_TCE:
  755. ret = kvmppc_h_put_tce(vcpu, kvmppc_get_gpr(vcpu, 4),
  756. kvmppc_get_gpr(vcpu, 5),
  757. kvmppc_get_gpr(vcpu, 6));
  758. if (ret == H_TOO_HARD)
  759. return RESUME_HOST;
  760. break;
  761. case H_PUT_TCE_INDIRECT:
  762. ret = kvmppc_h_put_tce_indirect(vcpu, kvmppc_get_gpr(vcpu, 4),
  763. kvmppc_get_gpr(vcpu, 5),
  764. kvmppc_get_gpr(vcpu, 6),
  765. kvmppc_get_gpr(vcpu, 7));
  766. if (ret == H_TOO_HARD)
  767. return RESUME_HOST;
  768. break;
  769. case H_STUFF_TCE:
  770. ret = kvmppc_h_stuff_tce(vcpu, kvmppc_get_gpr(vcpu, 4),
  771. kvmppc_get_gpr(vcpu, 5),
  772. kvmppc_get_gpr(vcpu, 6),
  773. kvmppc_get_gpr(vcpu, 7));
  774. if (ret == H_TOO_HARD)
  775. return RESUME_HOST;
  776. break;
  777. default:
  778. return RESUME_HOST;
  779. }
  780. kvmppc_set_gpr(vcpu, 3, ret);
  781. vcpu->arch.hcall_needed = 0;
  782. return RESUME_GUEST;
  783. }
  784. static int kvmppc_hcall_impl_hv(unsigned long cmd)
  785. {
  786. switch (cmd) {
  787. case H_CEDE:
  788. case H_PROD:
  789. case H_CONFER:
  790. case H_REGISTER_VPA:
  791. case H_SET_MODE:
  792. case H_LOGICAL_CI_LOAD:
  793. case H_LOGICAL_CI_STORE:
  794. #ifdef CONFIG_KVM_XICS
  795. case H_XIRR:
  796. case H_CPPR:
  797. case H_EOI:
  798. case H_IPI:
  799. case H_IPOLL:
  800. case H_XIRR_X:
  801. #endif
  802. return 1;
  803. }
  804. /* See if it's in the real-mode table */
  805. return kvmppc_hcall_impl_hv_realmode(cmd);
  806. }
  807. static int kvmppc_emulate_debug_inst(struct kvm_run *run,
  808. struct kvm_vcpu *vcpu)
  809. {
  810. u32 last_inst;
  811. if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst) !=
  812. EMULATE_DONE) {
  813. /*
  814. * Fetch failed, so return to guest and
  815. * try executing it again.
  816. */
  817. return RESUME_GUEST;
  818. }
  819. if (last_inst == KVMPPC_INST_SW_BREAKPOINT) {
  820. run->exit_reason = KVM_EXIT_DEBUG;
  821. run->debug.arch.address = kvmppc_get_pc(vcpu);
  822. return RESUME_HOST;
  823. } else {
  824. kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
  825. return RESUME_GUEST;
  826. }
  827. }
  828. static int kvmppc_handle_exit_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
  829. struct task_struct *tsk)
  830. {
  831. int r = RESUME_HOST;
  832. vcpu->stat.sum_exits++;
  833. /*
  834. * This can happen if an interrupt occurs in the last stages
  835. * of guest entry or the first stages of guest exit (i.e. after
  836. * setting paca->kvm_hstate.in_guest to KVM_GUEST_MODE_GUEST_HV
  837. * and before setting it to KVM_GUEST_MODE_HOST_HV).
  838. * That can happen due to a bug, or due to a machine check
  839. * occurring at just the wrong time.
  840. */
  841. if (vcpu->arch.shregs.msr & MSR_HV) {
  842. printk(KERN_EMERG "KVM trap in HV mode!\n");
  843. printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
  844. vcpu->arch.trap, kvmppc_get_pc(vcpu),
  845. vcpu->arch.shregs.msr);
  846. kvmppc_dump_regs(vcpu);
  847. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  848. run->hw.hardware_exit_reason = vcpu->arch.trap;
  849. return RESUME_HOST;
  850. }
  851. run->exit_reason = KVM_EXIT_UNKNOWN;
  852. run->ready_for_interrupt_injection = 1;
  853. switch (vcpu->arch.trap) {
  854. /* We're good on these - the host merely wanted to get our attention */
  855. case BOOK3S_INTERRUPT_HV_DECREMENTER:
  856. vcpu->stat.dec_exits++;
  857. r = RESUME_GUEST;
  858. break;
  859. case BOOK3S_INTERRUPT_EXTERNAL:
  860. case BOOK3S_INTERRUPT_H_DOORBELL:
  861. case BOOK3S_INTERRUPT_H_VIRT:
  862. vcpu->stat.ext_intr_exits++;
  863. r = RESUME_GUEST;
  864. break;
  865. /* HMI is hypervisor interrupt and host has handled it. Resume guest.*/
  866. case BOOK3S_INTERRUPT_HMI:
  867. case BOOK3S_INTERRUPT_PERFMON:
  868. r = RESUME_GUEST;
  869. break;
  870. case BOOK3S_INTERRUPT_MACHINE_CHECK:
  871. /*
  872. * Deliver a machine check interrupt to the guest.
  873. * We have to do this, even if the host has handled the
  874. * machine check, because machine checks use SRR0/1 and
  875. * the interrupt might have trashed guest state in them.
  876. */
  877. kvmppc_book3s_queue_irqprio(vcpu,
  878. BOOK3S_INTERRUPT_MACHINE_CHECK);
  879. r = RESUME_GUEST;
  880. break;
  881. case BOOK3S_INTERRUPT_PROGRAM:
  882. {
  883. ulong flags;
  884. /*
  885. * Normally program interrupts are delivered directly
  886. * to the guest by the hardware, but we can get here
  887. * as a result of a hypervisor emulation interrupt
  888. * (e40) getting turned into a 700 by BML RTAS.
  889. */
  890. flags = vcpu->arch.shregs.msr & 0x1f0000ull;
  891. kvmppc_core_queue_program(vcpu, flags);
  892. r = RESUME_GUEST;
  893. break;
  894. }
  895. case BOOK3S_INTERRUPT_SYSCALL:
  896. {
  897. /* hcall - punt to userspace */
  898. int i;
  899. /* hypercall with MSR_PR has already been handled in rmode,
  900. * and never reaches here.
  901. */
  902. run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
  903. for (i = 0; i < 9; ++i)
  904. run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
  905. run->exit_reason = KVM_EXIT_PAPR_HCALL;
  906. vcpu->arch.hcall_needed = 1;
  907. r = RESUME_HOST;
  908. break;
  909. }
  910. /*
  911. * We get these next two if the guest accesses a page which it thinks
  912. * it has mapped but which is not actually present, either because
  913. * it is for an emulated I/O device or because the corresonding
  914. * host page has been paged out. Any other HDSI/HISI interrupts
  915. * have been handled already.
  916. */
  917. case BOOK3S_INTERRUPT_H_DATA_STORAGE:
  918. r = RESUME_PAGE_FAULT;
  919. break;
  920. case BOOK3S_INTERRUPT_H_INST_STORAGE:
  921. vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
  922. vcpu->arch.fault_dsisr = 0;
  923. r = RESUME_PAGE_FAULT;
  924. break;
  925. /*
  926. * This occurs if the guest executes an illegal instruction.
  927. * If the guest debug is disabled, generate a program interrupt
  928. * to the guest. If guest debug is enabled, we need to check
  929. * whether the instruction is a software breakpoint instruction.
  930. * Accordingly return to Guest or Host.
  931. */
  932. case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
  933. if (vcpu->arch.emul_inst != KVM_INST_FETCH_FAILED)
  934. vcpu->arch.last_inst = kvmppc_need_byteswap(vcpu) ?
  935. swab32(vcpu->arch.emul_inst) :
  936. vcpu->arch.emul_inst;
  937. if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) {
  938. r = kvmppc_emulate_debug_inst(run, vcpu);
  939. } else {
  940. kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
  941. r = RESUME_GUEST;
  942. }
  943. break;
  944. /*
  945. * This occurs if the guest (kernel or userspace), does something that
  946. * is prohibited by HFSCR. We just generate a program interrupt to
  947. * the guest.
  948. */
  949. case BOOK3S_INTERRUPT_H_FAC_UNAVAIL:
  950. kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
  951. r = RESUME_GUEST;
  952. break;
  953. case BOOK3S_INTERRUPT_HV_RM_HARD:
  954. r = RESUME_PASSTHROUGH;
  955. break;
  956. default:
  957. kvmppc_dump_regs(vcpu);
  958. printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
  959. vcpu->arch.trap, kvmppc_get_pc(vcpu),
  960. vcpu->arch.shregs.msr);
  961. run->hw.hardware_exit_reason = vcpu->arch.trap;
  962. r = RESUME_HOST;
  963. break;
  964. }
  965. return r;
  966. }
  967. static int kvm_arch_vcpu_ioctl_get_sregs_hv(struct kvm_vcpu *vcpu,
  968. struct kvm_sregs *sregs)
  969. {
  970. int i;
  971. memset(sregs, 0, sizeof(struct kvm_sregs));
  972. sregs->pvr = vcpu->arch.pvr;
  973. for (i = 0; i < vcpu->arch.slb_max; i++) {
  974. sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
  975. sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
  976. }
  977. return 0;
  978. }
  979. static int kvm_arch_vcpu_ioctl_set_sregs_hv(struct kvm_vcpu *vcpu,
  980. struct kvm_sregs *sregs)
  981. {
  982. int i, j;
  983. /* Only accept the same PVR as the host's, since we can't spoof it */
  984. if (sregs->pvr != vcpu->arch.pvr)
  985. return -EINVAL;
  986. j = 0;
  987. for (i = 0; i < vcpu->arch.slb_nr; i++) {
  988. if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
  989. vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
  990. vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
  991. ++j;
  992. }
  993. }
  994. vcpu->arch.slb_max = j;
  995. return 0;
  996. }
  997. static void kvmppc_set_lpcr(struct kvm_vcpu *vcpu, u64 new_lpcr,
  998. bool preserve_top32)
  999. {
  1000. struct kvm *kvm = vcpu->kvm;
  1001. struct kvmppc_vcore *vc = vcpu->arch.vcore;
  1002. u64 mask;
  1003. mutex_lock(&kvm->lock);
  1004. spin_lock(&vc->lock);
  1005. /*
  1006. * If ILE (interrupt little-endian) has changed, update the
  1007. * MSR_LE bit in the intr_msr for each vcpu in this vcore.
  1008. */
  1009. if ((new_lpcr & LPCR_ILE) != (vc->lpcr & LPCR_ILE)) {
  1010. struct kvm_vcpu *vcpu;
  1011. int i;
  1012. kvm_for_each_vcpu(i, vcpu, kvm) {
  1013. if (vcpu->arch.vcore != vc)
  1014. continue;
  1015. if (new_lpcr & LPCR_ILE)
  1016. vcpu->arch.intr_msr |= MSR_LE;
  1017. else
  1018. vcpu->arch.intr_msr &= ~MSR_LE;
  1019. }
  1020. }
  1021. /*
  1022. * Userspace can only modify DPFD (default prefetch depth),
  1023. * ILE (interrupt little-endian) and TC (translation control).
  1024. * On POWER8 and POWER9 userspace can also modify AIL (alt. interrupt loc.).
  1025. */
  1026. mask = LPCR_DPFD | LPCR_ILE | LPCR_TC;
  1027. if (cpu_has_feature(CPU_FTR_ARCH_207S))
  1028. mask |= LPCR_AIL;
  1029. /* Broken 32-bit version of LPCR must not clear top bits */
  1030. if (preserve_top32)
  1031. mask &= 0xFFFFFFFF;
  1032. vc->lpcr = (vc->lpcr & ~mask) | (new_lpcr & mask);
  1033. spin_unlock(&vc->lock);
  1034. mutex_unlock(&kvm->lock);
  1035. }
  1036. static int kvmppc_get_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
  1037. union kvmppc_one_reg *val)
  1038. {
  1039. int r = 0;
  1040. long int i;
  1041. switch (id) {
  1042. case KVM_REG_PPC_DEBUG_INST:
  1043. *val = get_reg_val(id, KVMPPC_INST_SW_BREAKPOINT);
  1044. break;
  1045. case KVM_REG_PPC_HIOR:
  1046. *val = get_reg_val(id, 0);
  1047. break;
  1048. case KVM_REG_PPC_DABR:
  1049. *val = get_reg_val(id, vcpu->arch.dabr);
  1050. break;
  1051. case KVM_REG_PPC_DABRX:
  1052. *val = get_reg_val(id, vcpu->arch.dabrx);
  1053. break;
  1054. case KVM_REG_PPC_DSCR:
  1055. *val = get_reg_val(id, vcpu->arch.dscr);
  1056. break;
  1057. case KVM_REG_PPC_PURR:
  1058. *val = get_reg_val(id, vcpu->arch.purr);
  1059. break;
  1060. case KVM_REG_PPC_SPURR:
  1061. *val = get_reg_val(id, vcpu->arch.spurr);
  1062. break;
  1063. case KVM_REG_PPC_AMR:
  1064. *val = get_reg_val(id, vcpu->arch.amr);
  1065. break;
  1066. case KVM_REG_PPC_UAMOR:
  1067. *val = get_reg_val(id, vcpu->arch.uamor);
  1068. break;
  1069. case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
  1070. i = id - KVM_REG_PPC_MMCR0;
  1071. *val = get_reg_val(id, vcpu->arch.mmcr[i]);
  1072. break;
  1073. case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
  1074. i = id - KVM_REG_PPC_PMC1;
  1075. *val = get_reg_val(id, vcpu->arch.pmc[i]);
  1076. break;
  1077. case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
  1078. i = id - KVM_REG_PPC_SPMC1;
  1079. *val = get_reg_val(id, vcpu->arch.spmc[i]);
  1080. break;
  1081. case KVM_REG_PPC_SIAR:
  1082. *val = get_reg_val(id, vcpu->arch.siar);
  1083. break;
  1084. case KVM_REG_PPC_SDAR:
  1085. *val = get_reg_val(id, vcpu->arch.sdar);
  1086. break;
  1087. case KVM_REG_PPC_SIER:
  1088. *val = get_reg_val(id, vcpu->arch.sier);
  1089. break;
  1090. case KVM_REG_PPC_IAMR:
  1091. *val = get_reg_val(id, vcpu->arch.iamr);
  1092. break;
  1093. case KVM_REG_PPC_PSPB:
  1094. *val = get_reg_val(id, vcpu->arch.pspb);
  1095. break;
  1096. case KVM_REG_PPC_DPDES:
  1097. *val = get_reg_val(id, vcpu->arch.vcore->dpdes);
  1098. break;
  1099. case KVM_REG_PPC_VTB:
  1100. *val = get_reg_val(id, vcpu->arch.vcore->vtb);
  1101. break;
  1102. case KVM_REG_PPC_DAWR:
  1103. *val = get_reg_val(id, vcpu->arch.dawr);
  1104. break;
  1105. case KVM_REG_PPC_DAWRX:
  1106. *val = get_reg_val(id, vcpu->arch.dawrx);
  1107. break;
  1108. case KVM_REG_PPC_CIABR:
  1109. *val = get_reg_val(id, vcpu->arch.ciabr);
  1110. break;
  1111. case KVM_REG_PPC_CSIGR:
  1112. *val = get_reg_val(id, vcpu->arch.csigr);
  1113. break;
  1114. case KVM_REG_PPC_TACR:
  1115. *val = get_reg_val(id, vcpu->arch.tacr);
  1116. break;
  1117. case KVM_REG_PPC_TCSCR:
  1118. *val = get_reg_val(id, vcpu->arch.tcscr);
  1119. break;
  1120. case KVM_REG_PPC_PID:
  1121. *val = get_reg_val(id, vcpu->arch.pid);
  1122. break;
  1123. case KVM_REG_PPC_ACOP:
  1124. *val = get_reg_val(id, vcpu->arch.acop);
  1125. break;
  1126. case KVM_REG_PPC_WORT:
  1127. *val = get_reg_val(id, vcpu->arch.wort);
  1128. break;
  1129. case KVM_REG_PPC_TIDR:
  1130. *val = get_reg_val(id, vcpu->arch.tid);
  1131. break;
  1132. case KVM_REG_PPC_PSSCR:
  1133. *val = get_reg_val(id, vcpu->arch.psscr);
  1134. break;
  1135. case KVM_REG_PPC_VPA_ADDR:
  1136. spin_lock(&vcpu->arch.vpa_update_lock);
  1137. *val = get_reg_val(id, vcpu->arch.vpa.next_gpa);
  1138. spin_unlock(&vcpu->arch.vpa_update_lock);
  1139. break;
  1140. case KVM_REG_PPC_VPA_SLB:
  1141. spin_lock(&vcpu->arch.vpa_update_lock);
  1142. val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa;
  1143. val->vpaval.length = vcpu->arch.slb_shadow.len;
  1144. spin_unlock(&vcpu->arch.vpa_update_lock);
  1145. break;
  1146. case KVM_REG_PPC_VPA_DTL:
  1147. spin_lock(&vcpu->arch.vpa_update_lock);
  1148. val->vpaval.addr = vcpu->arch.dtl.next_gpa;
  1149. val->vpaval.length = vcpu->arch.dtl.len;
  1150. spin_unlock(&vcpu->arch.vpa_update_lock);
  1151. break;
  1152. case KVM_REG_PPC_TB_OFFSET:
  1153. *val = get_reg_val(id, vcpu->arch.vcore->tb_offset);
  1154. break;
  1155. case KVM_REG_PPC_LPCR:
  1156. case KVM_REG_PPC_LPCR_64:
  1157. *val = get_reg_val(id, vcpu->arch.vcore->lpcr);
  1158. break;
  1159. case KVM_REG_PPC_PPR:
  1160. *val = get_reg_val(id, vcpu->arch.ppr);
  1161. break;
  1162. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1163. case KVM_REG_PPC_TFHAR:
  1164. *val = get_reg_val(id, vcpu->arch.tfhar);
  1165. break;
  1166. case KVM_REG_PPC_TFIAR:
  1167. *val = get_reg_val(id, vcpu->arch.tfiar);
  1168. break;
  1169. case KVM_REG_PPC_TEXASR:
  1170. *val = get_reg_val(id, vcpu->arch.texasr);
  1171. break;
  1172. case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
  1173. i = id - KVM_REG_PPC_TM_GPR0;
  1174. *val = get_reg_val(id, vcpu->arch.gpr_tm[i]);
  1175. break;
  1176. case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
  1177. {
  1178. int j;
  1179. i = id - KVM_REG_PPC_TM_VSR0;
  1180. if (i < 32)
  1181. for (j = 0; j < TS_FPRWIDTH; j++)
  1182. val->vsxval[j] = vcpu->arch.fp_tm.fpr[i][j];
  1183. else {
  1184. if (cpu_has_feature(CPU_FTR_ALTIVEC))
  1185. val->vval = vcpu->arch.vr_tm.vr[i-32];
  1186. else
  1187. r = -ENXIO;
  1188. }
  1189. break;
  1190. }
  1191. case KVM_REG_PPC_TM_CR:
  1192. *val = get_reg_val(id, vcpu->arch.cr_tm);
  1193. break;
  1194. case KVM_REG_PPC_TM_XER:
  1195. *val = get_reg_val(id, vcpu->arch.xer_tm);
  1196. break;
  1197. case KVM_REG_PPC_TM_LR:
  1198. *val = get_reg_val(id, vcpu->arch.lr_tm);
  1199. break;
  1200. case KVM_REG_PPC_TM_CTR:
  1201. *val = get_reg_val(id, vcpu->arch.ctr_tm);
  1202. break;
  1203. case KVM_REG_PPC_TM_FPSCR:
  1204. *val = get_reg_val(id, vcpu->arch.fp_tm.fpscr);
  1205. break;
  1206. case KVM_REG_PPC_TM_AMR:
  1207. *val = get_reg_val(id, vcpu->arch.amr_tm);
  1208. break;
  1209. case KVM_REG_PPC_TM_PPR:
  1210. *val = get_reg_val(id, vcpu->arch.ppr_tm);
  1211. break;
  1212. case KVM_REG_PPC_TM_VRSAVE:
  1213. *val = get_reg_val(id, vcpu->arch.vrsave_tm);
  1214. break;
  1215. case KVM_REG_PPC_TM_VSCR:
  1216. if (cpu_has_feature(CPU_FTR_ALTIVEC))
  1217. *val = get_reg_val(id, vcpu->arch.vr_tm.vscr.u[3]);
  1218. else
  1219. r = -ENXIO;
  1220. break;
  1221. case KVM_REG_PPC_TM_DSCR:
  1222. *val = get_reg_val(id, vcpu->arch.dscr_tm);
  1223. break;
  1224. case KVM_REG_PPC_TM_TAR:
  1225. *val = get_reg_val(id, vcpu->arch.tar_tm);
  1226. break;
  1227. #endif
  1228. case KVM_REG_PPC_ARCH_COMPAT:
  1229. *val = get_reg_val(id, vcpu->arch.vcore->arch_compat);
  1230. break;
  1231. default:
  1232. r = -EINVAL;
  1233. break;
  1234. }
  1235. return r;
  1236. }
  1237. static int kvmppc_set_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
  1238. union kvmppc_one_reg *val)
  1239. {
  1240. int r = 0;
  1241. long int i;
  1242. unsigned long addr, len;
  1243. switch (id) {
  1244. case KVM_REG_PPC_HIOR:
  1245. /* Only allow this to be set to zero */
  1246. if (set_reg_val(id, *val))
  1247. r = -EINVAL;
  1248. break;
  1249. case KVM_REG_PPC_DABR:
  1250. vcpu->arch.dabr = set_reg_val(id, *val);
  1251. break;
  1252. case KVM_REG_PPC_DABRX:
  1253. vcpu->arch.dabrx = set_reg_val(id, *val) & ~DABRX_HYP;
  1254. break;
  1255. case KVM_REG_PPC_DSCR:
  1256. vcpu->arch.dscr = set_reg_val(id, *val);
  1257. break;
  1258. case KVM_REG_PPC_PURR:
  1259. vcpu->arch.purr = set_reg_val(id, *val);
  1260. break;
  1261. case KVM_REG_PPC_SPURR:
  1262. vcpu->arch.spurr = set_reg_val(id, *val);
  1263. break;
  1264. case KVM_REG_PPC_AMR:
  1265. vcpu->arch.amr = set_reg_val(id, *val);
  1266. break;
  1267. case KVM_REG_PPC_UAMOR:
  1268. vcpu->arch.uamor = set_reg_val(id, *val);
  1269. break;
  1270. case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
  1271. i = id - KVM_REG_PPC_MMCR0;
  1272. vcpu->arch.mmcr[i] = set_reg_val(id, *val);
  1273. break;
  1274. case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
  1275. i = id - KVM_REG_PPC_PMC1;
  1276. vcpu->arch.pmc[i] = set_reg_val(id, *val);
  1277. break;
  1278. case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
  1279. i = id - KVM_REG_PPC_SPMC1;
  1280. vcpu->arch.spmc[i] = set_reg_val(id, *val);
  1281. break;
  1282. case KVM_REG_PPC_SIAR:
  1283. vcpu->arch.siar = set_reg_val(id, *val);
  1284. break;
  1285. case KVM_REG_PPC_SDAR:
  1286. vcpu->arch.sdar = set_reg_val(id, *val);
  1287. break;
  1288. case KVM_REG_PPC_SIER:
  1289. vcpu->arch.sier = set_reg_val(id, *val);
  1290. break;
  1291. case KVM_REG_PPC_IAMR:
  1292. vcpu->arch.iamr = set_reg_val(id, *val);
  1293. break;
  1294. case KVM_REG_PPC_PSPB:
  1295. vcpu->arch.pspb = set_reg_val(id, *val);
  1296. break;
  1297. case KVM_REG_PPC_DPDES:
  1298. vcpu->arch.vcore->dpdes = set_reg_val(id, *val);
  1299. break;
  1300. case KVM_REG_PPC_VTB:
  1301. vcpu->arch.vcore->vtb = set_reg_val(id, *val);
  1302. break;
  1303. case KVM_REG_PPC_DAWR:
  1304. vcpu->arch.dawr = set_reg_val(id, *val);
  1305. break;
  1306. case KVM_REG_PPC_DAWRX:
  1307. vcpu->arch.dawrx = set_reg_val(id, *val) & ~DAWRX_HYP;
  1308. break;
  1309. case KVM_REG_PPC_CIABR:
  1310. vcpu->arch.ciabr = set_reg_val(id, *val);
  1311. /* Don't allow setting breakpoints in hypervisor code */
  1312. if ((vcpu->arch.ciabr & CIABR_PRIV) == CIABR_PRIV_HYPER)
  1313. vcpu->arch.ciabr &= ~CIABR_PRIV; /* disable */
  1314. break;
  1315. case KVM_REG_PPC_CSIGR:
  1316. vcpu->arch.csigr = set_reg_val(id, *val);
  1317. break;
  1318. case KVM_REG_PPC_TACR:
  1319. vcpu->arch.tacr = set_reg_val(id, *val);
  1320. break;
  1321. case KVM_REG_PPC_TCSCR:
  1322. vcpu->arch.tcscr = set_reg_val(id, *val);
  1323. break;
  1324. case KVM_REG_PPC_PID:
  1325. vcpu->arch.pid = set_reg_val(id, *val);
  1326. break;
  1327. case KVM_REG_PPC_ACOP:
  1328. vcpu->arch.acop = set_reg_val(id, *val);
  1329. break;
  1330. case KVM_REG_PPC_WORT:
  1331. vcpu->arch.wort = set_reg_val(id, *val);
  1332. break;
  1333. case KVM_REG_PPC_TIDR:
  1334. vcpu->arch.tid = set_reg_val(id, *val);
  1335. break;
  1336. case KVM_REG_PPC_PSSCR:
  1337. vcpu->arch.psscr = set_reg_val(id, *val) & PSSCR_GUEST_VIS;
  1338. break;
  1339. case KVM_REG_PPC_VPA_ADDR:
  1340. addr = set_reg_val(id, *val);
  1341. r = -EINVAL;
  1342. if (!addr && (vcpu->arch.slb_shadow.next_gpa ||
  1343. vcpu->arch.dtl.next_gpa))
  1344. break;
  1345. r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca));
  1346. break;
  1347. case KVM_REG_PPC_VPA_SLB:
  1348. addr = val->vpaval.addr;
  1349. len = val->vpaval.length;
  1350. r = -EINVAL;
  1351. if (addr && !vcpu->arch.vpa.next_gpa)
  1352. break;
  1353. r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len);
  1354. break;
  1355. case KVM_REG_PPC_VPA_DTL:
  1356. addr = val->vpaval.addr;
  1357. len = val->vpaval.length;
  1358. r = -EINVAL;
  1359. if (addr && (len < sizeof(struct dtl_entry) ||
  1360. !vcpu->arch.vpa.next_gpa))
  1361. break;
  1362. len -= len % sizeof(struct dtl_entry);
  1363. r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
  1364. break;
  1365. case KVM_REG_PPC_TB_OFFSET:
  1366. /* round up to multiple of 2^24 */
  1367. vcpu->arch.vcore->tb_offset =
  1368. ALIGN(set_reg_val(id, *val), 1UL << 24);
  1369. break;
  1370. case KVM_REG_PPC_LPCR:
  1371. kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), true);
  1372. break;
  1373. case KVM_REG_PPC_LPCR_64:
  1374. kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), false);
  1375. break;
  1376. case KVM_REG_PPC_PPR:
  1377. vcpu->arch.ppr = set_reg_val(id, *val);
  1378. break;
  1379. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1380. case KVM_REG_PPC_TFHAR:
  1381. vcpu->arch.tfhar = set_reg_val(id, *val);
  1382. break;
  1383. case KVM_REG_PPC_TFIAR:
  1384. vcpu->arch.tfiar = set_reg_val(id, *val);
  1385. break;
  1386. case KVM_REG_PPC_TEXASR:
  1387. vcpu->arch.texasr = set_reg_val(id, *val);
  1388. break;
  1389. case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
  1390. i = id - KVM_REG_PPC_TM_GPR0;
  1391. vcpu->arch.gpr_tm[i] = set_reg_val(id, *val);
  1392. break;
  1393. case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
  1394. {
  1395. int j;
  1396. i = id - KVM_REG_PPC_TM_VSR0;
  1397. if (i < 32)
  1398. for (j = 0; j < TS_FPRWIDTH; j++)
  1399. vcpu->arch.fp_tm.fpr[i][j] = val->vsxval[j];
  1400. else
  1401. if (cpu_has_feature(CPU_FTR_ALTIVEC))
  1402. vcpu->arch.vr_tm.vr[i-32] = val->vval;
  1403. else
  1404. r = -ENXIO;
  1405. break;
  1406. }
  1407. case KVM_REG_PPC_TM_CR:
  1408. vcpu->arch.cr_tm = set_reg_val(id, *val);
  1409. break;
  1410. case KVM_REG_PPC_TM_XER:
  1411. vcpu->arch.xer_tm = set_reg_val(id, *val);
  1412. break;
  1413. case KVM_REG_PPC_TM_LR:
  1414. vcpu->arch.lr_tm = set_reg_val(id, *val);
  1415. break;
  1416. case KVM_REG_PPC_TM_CTR:
  1417. vcpu->arch.ctr_tm = set_reg_val(id, *val);
  1418. break;
  1419. case KVM_REG_PPC_TM_FPSCR:
  1420. vcpu->arch.fp_tm.fpscr = set_reg_val(id, *val);
  1421. break;
  1422. case KVM_REG_PPC_TM_AMR:
  1423. vcpu->arch.amr_tm = set_reg_val(id, *val);
  1424. break;
  1425. case KVM_REG_PPC_TM_PPR:
  1426. vcpu->arch.ppr_tm = set_reg_val(id, *val);
  1427. break;
  1428. case KVM_REG_PPC_TM_VRSAVE:
  1429. vcpu->arch.vrsave_tm = set_reg_val(id, *val);
  1430. break;
  1431. case KVM_REG_PPC_TM_VSCR:
  1432. if (cpu_has_feature(CPU_FTR_ALTIVEC))
  1433. vcpu->arch.vr.vscr.u[3] = set_reg_val(id, *val);
  1434. else
  1435. r = - ENXIO;
  1436. break;
  1437. case KVM_REG_PPC_TM_DSCR:
  1438. vcpu->arch.dscr_tm = set_reg_val(id, *val);
  1439. break;
  1440. case KVM_REG_PPC_TM_TAR:
  1441. vcpu->arch.tar_tm = set_reg_val(id, *val);
  1442. break;
  1443. #endif
  1444. case KVM_REG_PPC_ARCH_COMPAT:
  1445. r = kvmppc_set_arch_compat(vcpu, set_reg_val(id, *val));
  1446. break;
  1447. default:
  1448. r = -EINVAL;
  1449. break;
  1450. }
  1451. return r;
  1452. }
  1453. /*
  1454. * On POWER9, threads are independent and can be in different partitions.
  1455. * Therefore we consider each thread to be a subcore.
  1456. * There is a restriction that all threads have to be in the same
  1457. * MMU mode (radix or HPT), unfortunately, but since we only support
  1458. * HPT guests on a HPT host so far, that isn't an impediment yet.
  1459. */
  1460. static int threads_per_vcore(void)
  1461. {
  1462. if (cpu_has_feature(CPU_FTR_ARCH_300))
  1463. return 1;
  1464. return threads_per_subcore;
  1465. }
  1466. static struct kvmppc_vcore *kvmppc_vcore_create(struct kvm *kvm, int core)
  1467. {
  1468. struct kvmppc_vcore *vcore;
  1469. vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
  1470. if (vcore == NULL)
  1471. return NULL;
  1472. spin_lock_init(&vcore->lock);
  1473. spin_lock_init(&vcore->stoltb_lock);
  1474. init_swait_queue_head(&vcore->wq);
  1475. vcore->preempt_tb = TB_NIL;
  1476. vcore->lpcr = kvm->arch.lpcr;
  1477. vcore->first_vcpuid = core * threads_per_vcore();
  1478. vcore->kvm = kvm;
  1479. INIT_LIST_HEAD(&vcore->preempt_list);
  1480. return vcore;
  1481. }
  1482. #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
  1483. static struct debugfs_timings_element {
  1484. const char *name;
  1485. size_t offset;
  1486. } timings[] = {
  1487. {"rm_entry", offsetof(struct kvm_vcpu, arch.rm_entry)},
  1488. {"rm_intr", offsetof(struct kvm_vcpu, arch.rm_intr)},
  1489. {"rm_exit", offsetof(struct kvm_vcpu, arch.rm_exit)},
  1490. {"guest", offsetof(struct kvm_vcpu, arch.guest_time)},
  1491. {"cede", offsetof(struct kvm_vcpu, arch.cede_time)},
  1492. };
  1493. #define N_TIMINGS (sizeof(timings) / sizeof(timings[0]))
  1494. struct debugfs_timings_state {
  1495. struct kvm_vcpu *vcpu;
  1496. unsigned int buflen;
  1497. char buf[N_TIMINGS * 100];
  1498. };
  1499. static int debugfs_timings_open(struct inode *inode, struct file *file)
  1500. {
  1501. struct kvm_vcpu *vcpu = inode->i_private;
  1502. struct debugfs_timings_state *p;
  1503. p = kzalloc(sizeof(*p), GFP_KERNEL);
  1504. if (!p)
  1505. return -ENOMEM;
  1506. kvm_get_kvm(vcpu->kvm);
  1507. p->vcpu = vcpu;
  1508. file->private_data = p;
  1509. return nonseekable_open(inode, file);
  1510. }
  1511. static int debugfs_timings_release(struct inode *inode, struct file *file)
  1512. {
  1513. struct debugfs_timings_state *p = file->private_data;
  1514. kvm_put_kvm(p->vcpu->kvm);
  1515. kfree(p);
  1516. return 0;
  1517. }
  1518. static ssize_t debugfs_timings_read(struct file *file, char __user *buf,
  1519. size_t len, loff_t *ppos)
  1520. {
  1521. struct debugfs_timings_state *p = file->private_data;
  1522. struct kvm_vcpu *vcpu = p->vcpu;
  1523. char *s, *buf_end;
  1524. struct kvmhv_tb_accumulator tb;
  1525. u64 count;
  1526. loff_t pos;
  1527. ssize_t n;
  1528. int i, loops;
  1529. bool ok;
  1530. if (!p->buflen) {
  1531. s = p->buf;
  1532. buf_end = s + sizeof(p->buf);
  1533. for (i = 0; i < N_TIMINGS; ++i) {
  1534. struct kvmhv_tb_accumulator *acc;
  1535. acc = (struct kvmhv_tb_accumulator *)
  1536. ((unsigned long)vcpu + timings[i].offset);
  1537. ok = false;
  1538. for (loops = 0; loops < 1000; ++loops) {
  1539. count = acc->seqcount;
  1540. if (!(count & 1)) {
  1541. smp_rmb();
  1542. tb = *acc;
  1543. smp_rmb();
  1544. if (count == acc->seqcount) {
  1545. ok = true;
  1546. break;
  1547. }
  1548. }
  1549. udelay(1);
  1550. }
  1551. if (!ok)
  1552. snprintf(s, buf_end - s, "%s: stuck\n",
  1553. timings[i].name);
  1554. else
  1555. snprintf(s, buf_end - s,
  1556. "%s: %llu %llu %llu %llu\n",
  1557. timings[i].name, count / 2,
  1558. tb_to_ns(tb.tb_total),
  1559. tb_to_ns(tb.tb_min),
  1560. tb_to_ns(tb.tb_max));
  1561. s += strlen(s);
  1562. }
  1563. p->buflen = s - p->buf;
  1564. }
  1565. pos = *ppos;
  1566. if (pos >= p->buflen)
  1567. return 0;
  1568. if (len > p->buflen - pos)
  1569. len = p->buflen - pos;
  1570. n = copy_to_user(buf, p->buf + pos, len);
  1571. if (n) {
  1572. if (n == len)
  1573. return -EFAULT;
  1574. len -= n;
  1575. }
  1576. *ppos = pos + len;
  1577. return len;
  1578. }
  1579. static ssize_t debugfs_timings_write(struct file *file, const char __user *buf,
  1580. size_t len, loff_t *ppos)
  1581. {
  1582. return -EACCES;
  1583. }
  1584. static const struct file_operations debugfs_timings_ops = {
  1585. .owner = THIS_MODULE,
  1586. .open = debugfs_timings_open,
  1587. .release = debugfs_timings_release,
  1588. .read = debugfs_timings_read,
  1589. .write = debugfs_timings_write,
  1590. .llseek = generic_file_llseek,
  1591. };
  1592. /* Create a debugfs directory for the vcpu */
  1593. static void debugfs_vcpu_init(struct kvm_vcpu *vcpu, unsigned int id)
  1594. {
  1595. char buf[16];
  1596. struct kvm *kvm = vcpu->kvm;
  1597. snprintf(buf, sizeof(buf), "vcpu%u", id);
  1598. if (IS_ERR_OR_NULL(kvm->arch.debugfs_dir))
  1599. return;
  1600. vcpu->arch.debugfs_dir = debugfs_create_dir(buf, kvm->arch.debugfs_dir);
  1601. if (IS_ERR_OR_NULL(vcpu->arch.debugfs_dir))
  1602. return;
  1603. vcpu->arch.debugfs_timings =
  1604. debugfs_create_file("timings", 0444, vcpu->arch.debugfs_dir,
  1605. vcpu, &debugfs_timings_ops);
  1606. }
  1607. #else /* CONFIG_KVM_BOOK3S_HV_EXIT_TIMING */
  1608. static void debugfs_vcpu_init(struct kvm_vcpu *vcpu, unsigned int id)
  1609. {
  1610. }
  1611. #endif /* CONFIG_KVM_BOOK3S_HV_EXIT_TIMING */
  1612. static struct kvm_vcpu *kvmppc_core_vcpu_create_hv(struct kvm *kvm,
  1613. unsigned int id)
  1614. {
  1615. struct kvm_vcpu *vcpu;
  1616. int err = -EINVAL;
  1617. int core;
  1618. struct kvmppc_vcore *vcore;
  1619. core = id / threads_per_vcore();
  1620. if (core >= KVM_MAX_VCORES)
  1621. goto out;
  1622. err = -ENOMEM;
  1623. vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  1624. if (!vcpu)
  1625. goto out;
  1626. err = kvm_vcpu_init(vcpu, kvm, id);
  1627. if (err)
  1628. goto free_vcpu;
  1629. vcpu->arch.shared = &vcpu->arch.shregs;
  1630. #ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
  1631. /*
  1632. * The shared struct is never shared on HV,
  1633. * so we can always use host endianness
  1634. */
  1635. #ifdef __BIG_ENDIAN__
  1636. vcpu->arch.shared_big_endian = true;
  1637. #else
  1638. vcpu->arch.shared_big_endian = false;
  1639. #endif
  1640. #endif
  1641. vcpu->arch.mmcr[0] = MMCR0_FC;
  1642. vcpu->arch.ctrl = CTRL_RUNLATCH;
  1643. /* default to host PVR, since we can't spoof it */
  1644. kvmppc_set_pvr_hv(vcpu, mfspr(SPRN_PVR));
  1645. spin_lock_init(&vcpu->arch.vpa_update_lock);
  1646. spin_lock_init(&vcpu->arch.tbacct_lock);
  1647. vcpu->arch.busy_preempt = TB_NIL;
  1648. vcpu->arch.intr_msr = MSR_SF | MSR_ME;
  1649. kvmppc_mmu_book3s_hv_init(vcpu);
  1650. vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
  1651. init_waitqueue_head(&vcpu->arch.cpu_run);
  1652. mutex_lock(&kvm->lock);
  1653. vcore = kvm->arch.vcores[core];
  1654. if (!vcore) {
  1655. vcore = kvmppc_vcore_create(kvm, core);
  1656. kvm->arch.vcores[core] = vcore;
  1657. kvm->arch.online_vcores++;
  1658. }
  1659. mutex_unlock(&kvm->lock);
  1660. if (!vcore)
  1661. goto free_vcpu;
  1662. spin_lock(&vcore->lock);
  1663. ++vcore->num_threads;
  1664. spin_unlock(&vcore->lock);
  1665. vcpu->arch.vcore = vcore;
  1666. vcpu->arch.ptid = vcpu->vcpu_id - vcore->first_vcpuid;
  1667. vcpu->arch.thread_cpu = -1;
  1668. vcpu->arch.prev_cpu = -1;
  1669. vcpu->arch.cpu_type = KVM_CPU_3S_64;
  1670. kvmppc_sanity_check(vcpu);
  1671. debugfs_vcpu_init(vcpu, id);
  1672. return vcpu;
  1673. free_vcpu:
  1674. kmem_cache_free(kvm_vcpu_cache, vcpu);
  1675. out:
  1676. return ERR_PTR(err);
  1677. }
  1678. static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa)
  1679. {
  1680. if (vpa->pinned_addr)
  1681. kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa,
  1682. vpa->dirty);
  1683. }
  1684. static void kvmppc_core_vcpu_free_hv(struct kvm_vcpu *vcpu)
  1685. {
  1686. spin_lock(&vcpu->arch.vpa_update_lock);
  1687. unpin_vpa(vcpu->kvm, &vcpu->arch.dtl);
  1688. unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow);
  1689. unpin_vpa(vcpu->kvm, &vcpu->arch.vpa);
  1690. spin_unlock(&vcpu->arch.vpa_update_lock);
  1691. kvm_vcpu_uninit(vcpu);
  1692. kmem_cache_free(kvm_vcpu_cache, vcpu);
  1693. }
  1694. static int kvmppc_core_check_requests_hv(struct kvm_vcpu *vcpu)
  1695. {
  1696. /* Indicate we want to get back into the guest */
  1697. return 1;
  1698. }
  1699. static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
  1700. {
  1701. unsigned long dec_nsec, now;
  1702. now = get_tb();
  1703. if (now > vcpu->arch.dec_expires) {
  1704. /* decrementer has already gone negative */
  1705. kvmppc_core_queue_dec(vcpu);
  1706. kvmppc_core_prepare_to_enter(vcpu);
  1707. return;
  1708. }
  1709. dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
  1710. / tb_ticks_per_sec;
  1711. hrtimer_start(&vcpu->arch.dec_timer, dec_nsec, HRTIMER_MODE_REL);
  1712. vcpu->arch.timer_running = 1;
  1713. }
  1714. static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
  1715. {
  1716. vcpu->arch.ceded = 0;
  1717. if (vcpu->arch.timer_running) {
  1718. hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
  1719. vcpu->arch.timer_running = 0;
  1720. }
  1721. }
  1722. extern void __kvmppc_vcore_entry(void);
  1723. static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
  1724. struct kvm_vcpu *vcpu)
  1725. {
  1726. u64 now;
  1727. if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
  1728. return;
  1729. spin_lock_irq(&vcpu->arch.tbacct_lock);
  1730. now = mftb();
  1731. vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) -
  1732. vcpu->arch.stolen_logged;
  1733. vcpu->arch.busy_preempt = now;
  1734. vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
  1735. spin_unlock_irq(&vcpu->arch.tbacct_lock);
  1736. --vc->n_runnable;
  1737. WRITE_ONCE(vc->runnable_threads[vcpu->arch.ptid], NULL);
  1738. }
  1739. static int kvmppc_grab_hwthread(int cpu)
  1740. {
  1741. struct paca_struct *tpaca;
  1742. long timeout = 10000;
  1743. tpaca = &paca[cpu];
  1744. /* Ensure the thread won't go into the kernel if it wakes */
  1745. tpaca->kvm_hstate.kvm_vcpu = NULL;
  1746. tpaca->kvm_hstate.kvm_vcore = NULL;
  1747. tpaca->kvm_hstate.napping = 0;
  1748. smp_wmb();
  1749. tpaca->kvm_hstate.hwthread_req = 1;
  1750. /*
  1751. * If the thread is already executing in the kernel (e.g. handling
  1752. * a stray interrupt), wait for it to get back to nap mode.
  1753. * The smp_mb() is to ensure that our setting of hwthread_req
  1754. * is visible before we look at hwthread_state, so if this
  1755. * races with the code at system_reset_pSeries and the thread
  1756. * misses our setting of hwthread_req, we are sure to see its
  1757. * setting of hwthread_state, and vice versa.
  1758. */
  1759. smp_mb();
  1760. while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
  1761. if (--timeout <= 0) {
  1762. pr_err("KVM: couldn't grab cpu %d\n", cpu);
  1763. return -EBUSY;
  1764. }
  1765. udelay(1);
  1766. }
  1767. return 0;
  1768. }
  1769. static void kvmppc_release_hwthread(int cpu)
  1770. {
  1771. struct paca_struct *tpaca;
  1772. tpaca = &paca[cpu];
  1773. tpaca->kvm_hstate.hwthread_req = 0;
  1774. tpaca->kvm_hstate.kvm_vcpu = NULL;
  1775. tpaca->kvm_hstate.kvm_vcore = NULL;
  1776. tpaca->kvm_hstate.kvm_split_mode = NULL;
  1777. }
  1778. static void do_nothing(void *x)
  1779. {
  1780. }
  1781. static void radix_flush_cpu(struct kvm *kvm, int cpu, struct kvm_vcpu *vcpu)
  1782. {
  1783. int i;
  1784. cpu = cpu_first_thread_sibling(cpu);
  1785. cpumask_set_cpu(cpu, &kvm->arch.need_tlb_flush);
  1786. /*
  1787. * Make sure setting of bit in need_tlb_flush precedes
  1788. * testing of cpu_in_guest bits. The matching barrier on
  1789. * the other side is the first smp_mb() in kvmppc_run_core().
  1790. */
  1791. smp_mb();
  1792. for (i = 0; i < threads_per_core; ++i)
  1793. if (cpumask_test_cpu(cpu + i, &kvm->arch.cpu_in_guest))
  1794. smp_call_function_single(cpu + i, do_nothing, NULL, 1);
  1795. }
  1796. static void kvmppc_start_thread(struct kvm_vcpu *vcpu, struct kvmppc_vcore *vc)
  1797. {
  1798. int cpu;
  1799. struct paca_struct *tpaca;
  1800. struct kvmppc_vcore *mvc = vc->master_vcore;
  1801. struct kvm *kvm = vc->kvm;
  1802. cpu = vc->pcpu;
  1803. if (vcpu) {
  1804. if (vcpu->arch.timer_running) {
  1805. hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
  1806. vcpu->arch.timer_running = 0;
  1807. }
  1808. cpu += vcpu->arch.ptid;
  1809. vcpu->cpu = mvc->pcpu;
  1810. vcpu->arch.thread_cpu = cpu;
  1811. /*
  1812. * With radix, the guest can do TLB invalidations itself,
  1813. * and it could choose to use the local form (tlbiel) if
  1814. * it is invalidating a translation that has only ever been
  1815. * used on one vcpu. However, that doesn't mean it has
  1816. * only ever been used on one physical cpu, since vcpus
  1817. * can move around between pcpus. To cope with this, when
  1818. * a vcpu moves from one pcpu to another, we need to tell
  1819. * any vcpus running on the same core as this vcpu previously
  1820. * ran to flush the TLB. The TLB is shared between threads,
  1821. * so we use a single bit in .need_tlb_flush for all 4 threads.
  1822. */
  1823. if (kvm_is_radix(kvm) && vcpu->arch.prev_cpu != cpu) {
  1824. if (vcpu->arch.prev_cpu >= 0 &&
  1825. cpu_first_thread_sibling(vcpu->arch.prev_cpu) !=
  1826. cpu_first_thread_sibling(cpu))
  1827. radix_flush_cpu(kvm, vcpu->arch.prev_cpu, vcpu);
  1828. vcpu->arch.prev_cpu = cpu;
  1829. }
  1830. cpumask_set_cpu(cpu, &kvm->arch.cpu_in_guest);
  1831. }
  1832. tpaca = &paca[cpu];
  1833. tpaca->kvm_hstate.kvm_vcpu = vcpu;
  1834. tpaca->kvm_hstate.ptid = cpu - mvc->pcpu;
  1835. /* Order stores to hstate.kvm_vcpu etc. before store to kvm_vcore */
  1836. smp_wmb();
  1837. tpaca->kvm_hstate.kvm_vcore = mvc;
  1838. if (cpu != smp_processor_id())
  1839. kvmppc_ipi_thread(cpu);
  1840. }
  1841. static void kvmppc_wait_for_nap(void)
  1842. {
  1843. int cpu = smp_processor_id();
  1844. int i, loops;
  1845. int n_threads = threads_per_vcore();
  1846. if (n_threads <= 1)
  1847. return;
  1848. for (loops = 0; loops < 1000000; ++loops) {
  1849. /*
  1850. * Check if all threads are finished.
  1851. * We set the vcore pointer when starting a thread
  1852. * and the thread clears it when finished, so we look
  1853. * for any threads that still have a non-NULL vcore ptr.
  1854. */
  1855. for (i = 1; i < n_threads; ++i)
  1856. if (paca[cpu + i].kvm_hstate.kvm_vcore)
  1857. break;
  1858. if (i == n_threads) {
  1859. HMT_medium();
  1860. return;
  1861. }
  1862. HMT_low();
  1863. }
  1864. HMT_medium();
  1865. for (i = 1; i < n_threads; ++i)
  1866. if (paca[cpu + i].kvm_hstate.kvm_vcore)
  1867. pr_err("KVM: CPU %d seems to be stuck\n", cpu + i);
  1868. }
  1869. /*
  1870. * Check that we are on thread 0 and that any other threads in
  1871. * this core are off-line. Then grab the threads so they can't
  1872. * enter the kernel.
  1873. */
  1874. static int on_primary_thread(void)
  1875. {
  1876. int cpu = smp_processor_id();
  1877. int thr;
  1878. /* Are we on a primary subcore? */
  1879. if (cpu_thread_in_subcore(cpu))
  1880. return 0;
  1881. thr = 0;
  1882. while (++thr < threads_per_subcore)
  1883. if (cpu_online(cpu + thr))
  1884. return 0;
  1885. /* Grab all hw threads so they can't go into the kernel */
  1886. for (thr = 1; thr < threads_per_subcore; ++thr) {
  1887. if (kvmppc_grab_hwthread(cpu + thr)) {
  1888. /* Couldn't grab one; let the others go */
  1889. do {
  1890. kvmppc_release_hwthread(cpu + thr);
  1891. } while (--thr > 0);
  1892. return 0;
  1893. }
  1894. }
  1895. return 1;
  1896. }
  1897. /*
  1898. * A list of virtual cores for each physical CPU.
  1899. * These are vcores that could run but their runner VCPU tasks are
  1900. * (or may be) preempted.
  1901. */
  1902. struct preempted_vcore_list {
  1903. struct list_head list;
  1904. spinlock_t lock;
  1905. };
  1906. static DEFINE_PER_CPU(struct preempted_vcore_list, preempted_vcores);
  1907. static void init_vcore_lists(void)
  1908. {
  1909. int cpu;
  1910. for_each_possible_cpu(cpu) {
  1911. struct preempted_vcore_list *lp = &per_cpu(preempted_vcores, cpu);
  1912. spin_lock_init(&lp->lock);
  1913. INIT_LIST_HEAD(&lp->list);
  1914. }
  1915. }
  1916. static void kvmppc_vcore_preempt(struct kvmppc_vcore *vc)
  1917. {
  1918. struct preempted_vcore_list *lp = this_cpu_ptr(&preempted_vcores);
  1919. vc->vcore_state = VCORE_PREEMPT;
  1920. vc->pcpu = smp_processor_id();
  1921. if (vc->num_threads < threads_per_vcore()) {
  1922. spin_lock(&lp->lock);
  1923. list_add_tail(&vc->preempt_list, &lp->list);
  1924. spin_unlock(&lp->lock);
  1925. }
  1926. /* Start accumulating stolen time */
  1927. kvmppc_core_start_stolen(vc);
  1928. }
  1929. static void kvmppc_vcore_end_preempt(struct kvmppc_vcore *vc)
  1930. {
  1931. struct preempted_vcore_list *lp;
  1932. kvmppc_core_end_stolen(vc);
  1933. if (!list_empty(&vc->preempt_list)) {
  1934. lp = &per_cpu(preempted_vcores, vc->pcpu);
  1935. spin_lock(&lp->lock);
  1936. list_del_init(&vc->preempt_list);
  1937. spin_unlock(&lp->lock);
  1938. }
  1939. vc->vcore_state = VCORE_INACTIVE;
  1940. }
  1941. /*
  1942. * This stores information about the virtual cores currently
  1943. * assigned to a physical core.
  1944. */
  1945. struct core_info {
  1946. int n_subcores;
  1947. int max_subcore_threads;
  1948. int total_threads;
  1949. int subcore_threads[MAX_SUBCORES];
  1950. struct kvm *subcore_vm[MAX_SUBCORES];
  1951. struct list_head vcs[MAX_SUBCORES];
  1952. };
  1953. /*
  1954. * This mapping means subcores 0 and 1 can use threads 0-3 and 4-7
  1955. * respectively in 2-way micro-threading (split-core) mode.
  1956. */
  1957. static int subcore_thread_map[MAX_SUBCORES] = { 0, 4, 2, 6 };
  1958. static void init_core_info(struct core_info *cip, struct kvmppc_vcore *vc)
  1959. {
  1960. int sub;
  1961. memset(cip, 0, sizeof(*cip));
  1962. cip->n_subcores = 1;
  1963. cip->max_subcore_threads = vc->num_threads;
  1964. cip->total_threads = vc->num_threads;
  1965. cip->subcore_threads[0] = vc->num_threads;
  1966. cip->subcore_vm[0] = vc->kvm;
  1967. for (sub = 0; sub < MAX_SUBCORES; ++sub)
  1968. INIT_LIST_HEAD(&cip->vcs[sub]);
  1969. list_add_tail(&vc->preempt_list, &cip->vcs[0]);
  1970. }
  1971. static bool subcore_config_ok(int n_subcores, int n_threads)
  1972. {
  1973. /* Can only dynamically split if unsplit to begin with */
  1974. if (n_subcores > 1 && threads_per_subcore < MAX_SMT_THREADS)
  1975. return false;
  1976. if (n_subcores > MAX_SUBCORES)
  1977. return false;
  1978. if (n_subcores > 1) {
  1979. if (!(dynamic_mt_modes & 2))
  1980. n_subcores = 4;
  1981. if (n_subcores > 2 && !(dynamic_mt_modes & 4))
  1982. return false;
  1983. }
  1984. return n_subcores * roundup_pow_of_two(n_threads) <= MAX_SMT_THREADS;
  1985. }
  1986. static void init_master_vcore(struct kvmppc_vcore *vc)
  1987. {
  1988. vc->master_vcore = vc;
  1989. vc->entry_exit_map = 0;
  1990. vc->in_guest = 0;
  1991. vc->napping_threads = 0;
  1992. vc->conferring_threads = 0;
  1993. }
  1994. static bool can_dynamic_split(struct kvmppc_vcore *vc, struct core_info *cip)
  1995. {
  1996. int n_threads = vc->num_threads;
  1997. int sub;
  1998. if (!cpu_has_feature(CPU_FTR_ARCH_207S))
  1999. return false;
  2000. if (n_threads < cip->max_subcore_threads)
  2001. n_threads = cip->max_subcore_threads;
  2002. if (!subcore_config_ok(cip->n_subcores + 1, n_threads))
  2003. return false;
  2004. cip->max_subcore_threads = n_threads;
  2005. sub = cip->n_subcores;
  2006. ++cip->n_subcores;
  2007. cip->total_threads += vc->num_threads;
  2008. cip->subcore_threads[sub] = vc->num_threads;
  2009. cip->subcore_vm[sub] = vc->kvm;
  2010. init_master_vcore(vc);
  2011. list_move_tail(&vc->preempt_list, &cip->vcs[sub]);
  2012. return true;
  2013. }
  2014. /*
  2015. * Work out whether it is possible to piggyback the execution of
  2016. * vcore *pvc onto the execution of the other vcores described in *cip.
  2017. */
  2018. static bool can_piggyback(struct kvmppc_vcore *pvc, struct core_info *cip,
  2019. int target_threads)
  2020. {
  2021. if (cip->total_threads + pvc->num_threads > target_threads)
  2022. return false;
  2023. return can_dynamic_split(pvc, cip);
  2024. }
  2025. static void prepare_threads(struct kvmppc_vcore *vc)
  2026. {
  2027. int i;
  2028. struct kvm_vcpu *vcpu;
  2029. for_each_runnable_thread(i, vcpu, vc) {
  2030. if (signal_pending(vcpu->arch.run_task))
  2031. vcpu->arch.ret = -EINTR;
  2032. else if (vcpu->arch.vpa.update_pending ||
  2033. vcpu->arch.slb_shadow.update_pending ||
  2034. vcpu->arch.dtl.update_pending)
  2035. vcpu->arch.ret = RESUME_GUEST;
  2036. else
  2037. continue;
  2038. kvmppc_remove_runnable(vc, vcpu);
  2039. wake_up(&vcpu->arch.cpu_run);
  2040. }
  2041. }
  2042. static void collect_piggybacks(struct core_info *cip, int target_threads)
  2043. {
  2044. struct preempted_vcore_list *lp = this_cpu_ptr(&preempted_vcores);
  2045. struct kvmppc_vcore *pvc, *vcnext;
  2046. spin_lock(&lp->lock);
  2047. list_for_each_entry_safe(pvc, vcnext, &lp->list, preempt_list) {
  2048. if (!spin_trylock(&pvc->lock))
  2049. continue;
  2050. prepare_threads(pvc);
  2051. if (!pvc->n_runnable) {
  2052. list_del_init(&pvc->preempt_list);
  2053. if (pvc->runner == NULL) {
  2054. pvc->vcore_state = VCORE_INACTIVE;
  2055. kvmppc_core_end_stolen(pvc);
  2056. }
  2057. spin_unlock(&pvc->lock);
  2058. continue;
  2059. }
  2060. if (!can_piggyback(pvc, cip, target_threads)) {
  2061. spin_unlock(&pvc->lock);
  2062. continue;
  2063. }
  2064. kvmppc_core_end_stolen(pvc);
  2065. pvc->vcore_state = VCORE_PIGGYBACK;
  2066. if (cip->total_threads >= target_threads)
  2067. break;
  2068. }
  2069. spin_unlock(&lp->lock);
  2070. }
  2071. static void post_guest_process(struct kvmppc_vcore *vc, bool is_master)
  2072. {
  2073. int still_running = 0, i;
  2074. u64 now;
  2075. long ret;
  2076. struct kvm_vcpu *vcpu;
  2077. spin_lock(&vc->lock);
  2078. now = get_tb();
  2079. for_each_runnable_thread(i, vcpu, vc) {
  2080. /* cancel pending dec exception if dec is positive */
  2081. if (now < vcpu->arch.dec_expires &&
  2082. kvmppc_core_pending_dec(vcpu))
  2083. kvmppc_core_dequeue_dec(vcpu);
  2084. trace_kvm_guest_exit(vcpu);
  2085. ret = RESUME_GUEST;
  2086. if (vcpu->arch.trap)
  2087. ret = kvmppc_handle_exit_hv(vcpu->arch.kvm_run, vcpu,
  2088. vcpu->arch.run_task);
  2089. vcpu->arch.ret = ret;
  2090. vcpu->arch.trap = 0;
  2091. if (is_kvmppc_resume_guest(vcpu->arch.ret)) {
  2092. if (vcpu->arch.pending_exceptions)
  2093. kvmppc_core_prepare_to_enter(vcpu);
  2094. if (vcpu->arch.ceded)
  2095. kvmppc_set_timer(vcpu);
  2096. else
  2097. ++still_running;
  2098. } else {
  2099. kvmppc_remove_runnable(vc, vcpu);
  2100. wake_up(&vcpu->arch.cpu_run);
  2101. }
  2102. }
  2103. list_del_init(&vc->preempt_list);
  2104. if (!is_master) {
  2105. if (still_running > 0) {
  2106. kvmppc_vcore_preempt(vc);
  2107. } else if (vc->runner) {
  2108. vc->vcore_state = VCORE_PREEMPT;
  2109. kvmppc_core_start_stolen(vc);
  2110. } else {
  2111. vc->vcore_state = VCORE_INACTIVE;
  2112. }
  2113. if (vc->n_runnable > 0 && vc->runner == NULL) {
  2114. /* make sure there's a candidate runner awake */
  2115. i = -1;
  2116. vcpu = next_runnable_thread(vc, &i);
  2117. wake_up(&vcpu->arch.cpu_run);
  2118. }
  2119. }
  2120. spin_unlock(&vc->lock);
  2121. }
  2122. /*
  2123. * Clear core from the list of active host cores as we are about to
  2124. * enter the guest. Only do this if it is the primary thread of the
  2125. * core (not if a subcore) that is entering the guest.
  2126. */
  2127. static inline int kvmppc_clear_host_core(unsigned int cpu)
  2128. {
  2129. int core;
  2130. if (!kvmppc_host_rm_ops_hv || cpu_thread_in_core(cpu))
  2131. return 0;
  2132. /*
  2133. * Memory barrier can be omitted here as we will do a smp_wmb()
  2134. * later in kvmppc_start_thread and we need ensure that state is
  2135. * visible to other CPUs only after we enter guest.
  2136. */
  2137. core = cpu >> threads_shift;
  2138. kvmppc_host_rm_ops_hv->rm_core[core].rm_state.in_host = 0;
  2139. return 0;
  2140. }
  2141. /*
  2142. * Advertise this core as an active host core since we exited the guest
  2143. * Only need to do this if it is the primary thread of the core that is
  2144. * exiting.
  2145. */
  2146. static inline int kvmppc_set_host_core(unsigned int cpu)
  2147. {
  2148. int core;
  2149. if (!kvmppc_host_rm_ops_hv || cpu_thread_in_core(cpu))
  2150. return 0;
  2151. /*
  2152. * Memory barrier can be omitted here because we do a spin_unlock
  2153. * immediately after this which provides the memory barrier.
  2154. */
  2155. core = cpu >> threads_shift;
  2156. kvmppc_host_rm_ops_hv->rm_core[core].rm_state.in_host = 1;
  2157. return 0;
  2158. }
  2159. /*
  2160. * Run a set of guest threads on a physical core.
  2161. * Called with vc->lock held.
  2162. */
  2163. static noinline void kvmppc_run_core(struct kvmppc_vcore *vc)
  2164. {
  2165. struct kvm_vcpu *vcpu;
  2166. int i;
  2167. int srcu_idx;
  2168. struct core_info core_info;
  2169. struct kvmppc_vcore *pvc, *vcnext;
  2170. struct kvm_split_mode split_info, *sip;
  2171. int split, subcore_size, active;
  2172. int sub;
  2173. bool thr0_done;
  2174. unsigned long cmd_bit, stat_bit;
  2175. int pcpu, thr;
  2176. int target_threads;
  2177. int controlled_threads;
  2178. /*
  2179. * Remove from the list any threads that have a signal pending
  2180. * or need a VPA update done
  2181. */
  2182. prepare_threads(vc);
  2183. /* if the runner is no longer runnable, let the caller pick a new one */
  2184. if (vc->runner->arch.state != KVMPPC_VCPU_RUNNABLE)
  2185. return;
  2186. /*
  2187. * Initialize *vc.
  2188. */
  2189. init_master_vcore(vc);
  2190. vc->preempt_tb = TB_NIL;
  2191. /*
  2192. * Number of threads that we will be controlling: the same as
  2193. * the number of threads per subcore, except on POWER9,
  2194. * where it's 1 because the threads are (mostly) independent.
  2195. */
  2196. controlled_threads = threads_per_vcore();
  2197. /*
  2198. * Make sure we are running on primary threads, and that secondary
  2199. * threads are offline. Also check if the number of threads in this
  2200. * guest are greater than the current system threads per guest.
  2201. */
  2202. if ((controlled_threads > 1) &&
  2203. ((vc->num_threads > threads_per_subcore) || !on_primary_thread())) {
  2204. for_each_runnable_thread(i, vcpu, vc) {
  2205. vcpu->arch.ret = -EBUSY;
  2206. kvmppc_remove_runnable(vc, vcpu);
  2207. wake_up(&vcpu->arch.cpu_run);
  2208. }
  2209. goto out;
  2210. }
  2211. /*
  2212. * See if we could run any other vcores on the physical core
  2213. * along with this one.
  2214. */
  2215. init_core_info(&core_info, vc);
  2216. pcpu = smp_processor_id();
  2217. target_threads = controlled_threads;
  2218. if (target_smt_mode && target_smt_mode < target_threads)
  2219. target_threads = target_smt_mode;
  2220. if (vc->num_threads < target_threads)
  2221. collect_piggybacks(&core_info, target_threads);
  2222. /* Decide on micro-threading (split-core) mode */
  2223. subcore_size = threads_per_subcore;
  2224. cmd_bit = stat_bit = 0;
  2225. split = core_info.n_subcores;
  2226. sip = NULL;
  2227. if (split > 1) {
  2228. /* threads_per_subcore must be MAX_SMT_THREADS (8) here */
  2229. if (split == 2 && (dynamic_mt_modes & 2)) {
  2230. cmd_bit = HID0_POWER8_1TO2LPAR;
  2231. stat_bit = HID0_POWER8_2LPARMODE;
  2232. } else {
  2233. split = 4;
  2234. cmd_bit = HID0_POWER8_1TO4LPAR;
  2235. stat_bit = HID0_POWER8_4LPARMODE;
  2236. }
  2237. subcore_size = MAX_SMT_THREADS / split;
  2238. sip = &split_info;
  2239. memset(&split_info, 0, sizeof(split_info));
  2240. split_info.rpr = mfspr(SPRN_RPR);
  2241. split_info.pmmar = mfspr(SPRN_PMMAR);
  2242. split_info.ldbar = mfspr(SPRN_LDBAR);
  2243. split_info.subcore_size = subcore_size;
  2244. for (sub = 0; sub < core_info.n_subcores; ++sub)
  2245. split_info.master_vcs[sub] =
  2246. list_first_entry(&core_info.vcs[sub],
  2247. struct kvmppc_vcore, preempt_list);
  2248. /* order writes to split_info before kvm_split_mode pointer */
  2249. smp_wmb();
  2250. }
  2251. pcpu = smp_processor_id();
  2252. for (thr = 0; thr < controlled_threads; ++thr)
  2253. paca[pcpu + thr].kvm_hstate.kvm_split_mode = sip;
  2254. /* Initiate micro-threading (split-core) if required */
  2255. if (cmd_bit) {
  2256. unsigned long hid0 = mfspr(SPRN_HID0);
  2257. hid0 |= cmd_bit | HID0_POWER8_DYNLPARDIS;
  2258. mb();
  2259. mtspr(SPRN_HID0, hid0);
  2260. isync();
  2261. for (;;) {
  2262. hid0 = mfspr(SPRN_HID0);
  2263. if (hid0 & stat_bit)
  2264. break;
  2265. cpu_relax();
  2266. }
  2267. }
  2268. kvmppc_clear_host_core(pcpu);
  2269. /* Start all the threads */
  2270. active = 0;
  2271. for (sub = 0; sub < core_info.n_subcores; ++sub) {
  2272. thr = subcore_thread_map[sub];
  2273. thr0_done = false;
  2274. active |= 1 << thr;
  2275. list_for_each_entry(pvc, &core_info.vcs[sub], preempt_list) {
  2276. pvc->pcpu = pcpu + thr;
  2277. for_each_runnable_thread(i, vcpu, pvc) {
  2278. kvmppc_start_thread(vcpu, pvc);
  2279. kvmppc_create_dtl_entry(vcpu, pvc);
  2280. trace_kvm_guest_enter(vcpu);
  2281. if (!vcpu->arch.ptid)
  2282. thr0_done = true;
  2283. active |= 1 << (thr + vcpu->arch.ptid);
  2284. }
  2285. /*
  2286. * We need to start the first thread of each subcore
  2287. * even if it doesn't have a vcpu.
  2288. */
  2289. if (pvc->master_vcore == pvc && !thr0_done)
  2290. kvmppc_start_thread(NULL, pvc);
  2291. thr += pvc->num_threads;
  2292. }
  2293. }
  2294. /*
  2295. * Ensure that split_info.do_nap is set after setting
  2296. * the vcore pointer in the PACA of the secondaries.
  2297. */
  2298. smp_mb();
  2299. if (cmd_bit)
  2300. split_info.do_nap = 1; /* ask secondaries to nap when done */
  2301. /*
  2302. * When doing micro-threading, poke the inactive threads as well.
  2303. * This gets them to the nap instruction after kvm_do_nap,
  2304. * which reduces the time taken to unsplit later.
  2305. */
  2306. if (split > 1)
  2307. for (thr = 1; thr < threads_per_subcore; ++thr)
  2308. if (!(active & (1 << thr)))
  2309. kvmppc_ipi_thread(pcpu + thr);
  2310. vc->vcore_state = VCORE_RUNNING;
  2311. preempt_disable();
  2312. trace_kvmppc_run_core(vc, 0);
  2313. for (sub = 0; sub < core_info.n_subcores; ++sub)
  2314. list_for_each_entry(pvc, &core_info.vcs[sub], preempt_list)
  2315. spin_unlock(&pvc->lock);
  2316. guest_enter();
  2317. srcu_idx = srcu_read_lock(&vc->kvm->srcu);
  2318. __kvmppc_vcore_entry();
  2319. srcu_read_unlock(&vc->kvm->srcu, srcu_idx);
  2320. spin_lock(&vc->lock);
  2321. /* prevent other vcpu threads from doing kvmppc_start_thread() now */
  2322. vc->vcore_state = VCORE_EXITING;
  2323. /* wait for secondary threads to finish writing their state to memory */
  2324. kvmppc_wait_for_nap();
  2325. /* Return to whole-core mode if we split the core earlier */
  2326. if (split > 1) {
  2327. unsigned long hid0 = mfspr(SPRN_HID0);
  2328. unsigned long loops = 0;
  2329. hid0 &= ~HID0_POWER8_DYNLPARDIS;
  2330. stat_bit = HID0_POWER8_2LPARMODE | HID0_POWER8_4LPARMODE;
  2331. mb();
  2332. mtspr(SPRN_HID0, hid0);
  2333. isync();
  2334. for (;;) {
  2335. hid0 = mfspr(SPRN_HID0);
  2336. if (!(hid0 & stat_bit))
  2337. break;
  2338. cpu_relax();
  2339. ++loops;
  2340. }
  2341. split_info.do_nap = 0;
  2342. }
  2343. /* Let secondaries go back to the offline loop */
  2344. for (i = 0; i < controlled_threads; ++i) {
  2345. kvmppc_release_hwthread(pcpu + i);
  2346. if (sip && sip->napped[i])
  2347. kvmppc_ipi_thread(pcpu + i);
  2348. cpumask_clear_cpu(pcpu + i, &vc->kvm->arch.cpu_in_guest);
  2349. }
  2350. kvmppc_set_host_core(pcpu);
  2351. spin_unlock(&vc->lock);
  2352. /* make sure updates to secondary vcpu structs are visible now */
  2353. smp_mb();
  2354. guest_exit();
  2355. for (sub = 0; sub < core_info.n_subcores; ++sub)
  2356. list_for_each_entry_safe(pvc, vcnext, &core_info.vcs[sub],
  2357. preempt_list)
  2358. post_guest_process(pvc, pvc == vc);
  2359. spin_lock(&vc->lock);
  2360. preempt_enable();
  2361. out:
  2362. vc->vcore_state = VCORE_INACTIVE;
  2363. trace_kvmppc_run_core(vc, 1);
  2364. }
  2365. /*
  2366. * Wait for some other vcpu thread to execute us, and
  2367. * wake us up when we need to handle something in the host.
  2368. */
  2369. static void kvmppc_wait_for_exec(struct kvmppc_vcore *vc,
  2370. struct kvm_vcpu *vcpu, int wait_state)
  2371. {
  2372. DEFINE_WAIT(wait);
  2373. prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
  2374. if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
  2375. spin_unlock(&vc->lock);
  2376. schedule();
  2377. spin_lock(&vc->lock);
  2378. }
  2379. finish_wait(&vcpu->arch.cpu_run, &wait);
  2380. }
  2381. static void grow_halt_poll_ns(struct kvmppc_vcore *vc)
  2382. {
  2383. /* 10us base */
  2384. if (vc->halt_poll_ns == 0 && halt_poll_ns_grow)
  2385. vc->halt_poll_ns = 10000;
  2386. else
  2387. vc->halt_poll_ns *= halt_poll_ns_grow;
  2388. }
  2389. static void shrink_halt_poll_ns(struct kvmppc_vcore *vc)
  2390. {
  2391. if (halt_poll_ns_shrink == 0)
  2392. vc->halt_poll_ns = 0;
  2393. else
  2394. vc->halt_poll_ns /= halt_poll_ns_shrink;
  2395. }
  2396. /*
  2397. * Check to see if any of the runnable vcpus on the vcore have pending
  2398. * exceptions or are no longer ceded
  2399. */
  2400. static int kvmppc_vcore_check_block(struct kvmppc_vcore *vc)
  2401. {
  2402. struct kvm_vcpu *vcpu;
  2403. int i;
  2404. for_each_runnable_thread(i, vcpu, vc) {
  2405. if (vcpu->arch.pending_exceptions || !vcpu->arch.ceded ||
  2406. vcpu->arch.prodded)
  2407. return 1;
  2408. }
  2409. return 0;
  2410. }
  2411. /*
  2412. * All the vcpus in this vcore are idle, so wait for a decrementer
  2413. * or external interrupt to one of the vcpus. vc->lock is held.
  2414. */
  2415. static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
  2416. {
  2417. ktime_t cur, start_poll, start_wait;
  2418. int do_sleep = 1;
  2419. u64 block_ns;
  2420. DECLARE_SWAITQUEUE(wait);
  2421. /* Poll for pending exceptions and ceded state */
  2422. cur = start_poll = ktime_get();
  2423. if (vc->halt_poll_ns) {
  2424. ktime_t stop = ktime_add_ns(start_poll, vc->halt_poll_ns);
  2425. ++vc->runner->stat.halt_attempted_poll;
  2426. vc->vcore_state = VCORE_POLLING;
  2427. spin_unlock(&vc->lock);
  2428. do {
  2429. if (kvmppc_vcore_check_block(vc)) {
  2430. do_sleep = 0;
  2431. break;
  2432. }
  2433. cur = ktime_get();
  2434. } while (single_task_running() && ktime_before(cur, stop));
  2435. spin_lock(&vc->lock);
  2436. vc->vcore_state = VCORE_INACTIVE;
  2437. if (!do_sleep) {
  2438. ++vc->runner->stat.halt_successful_poll;
  2439. goto out;
  2440. }
  2441. }
  2442. prepare_to_swait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
  2443. if (kvmppc_vcore_check_block(vc)) {
  2444. finish_swait(&vc->wq, &wait);
  2445. do_sleep = 0;
  2446. /* If we polled, count this as a successful poll */
  2447. if (vc->halt_poll_ns)
  2448. ++vc->runner->stat.halt_successful_poll;
  2449. goto out;
  2450. }
  2451. start_wait = ktime_get();
  2452. vc->vcore_state = VCORE_SLEEPING;
  2453. trace_kvmppc_vcore_blocked(vc, 0);
  2454. spin_unlock(&vc->lock);
  2455. schedule();
  2456. finish_swait(&vc->wq, &wait);
  2457. spin_lock(&vc->lock);
  2458. vc->vcore_state = VCORE_INACTIVE;
  2459. trace_kvmppc_vcore_blocked(vc, 1);
  2460. ++vc->runner->stat.halt_successful_wait;
  2461. cur = ktime_get();
  2462. out:
  2463. block_ns = ktime_to_ns(cur) - ktime_to_ns(start_poll);
  2464. /* Attribute wait time */
  2465. if (do_sleep) {
  2466. vc->runner->stat.halt_wait_ns +=
  2467. ktime_to_ns(cur) - ktime_to_ns(start_wait);
  2468. /* Attribute failed poll time */
  2469. if (vc->halt_poll_ns)
  2470. vc->runner->stat.halt_poll_fail_ns +=
  2471. ktime_to_ns(start_wait) -
  2472. ktime_to_ns(start_poll);
  2473. } else {
  2474. /* Attribute successful poll time */
  2475. if (vc->halt_poll_ns)
  2476. vc->runner->stat.halt_poll_success_ns +=
  2477. ktime_to_ns(cur) -
  2478. ktime_to_ns(start_poll);
  2479. }
  2480. /* Adjust poll time */
  2481. if (halt_poll_ns) {
  2482. if (block_ns <= vc->halt_poll_ns)
  2483. ;
  2484. /* We slept and blocked for longer than the max halt time */
  2485. else if (vc->halt_poll_ns && block_ns > halt_poll_ns)
  2486. shrink_halt_poll_ns(vc);
  2487. /* We slept and our poll time is too small */
  2488. else if (vc->halt_poll_ns < halt_poll_ns &&
  2489. block_ns < halt_poll_ns)
  2490. grow_halt_poll_ns(vc);
  2491. if (vc->halt_poll_ns > halt_poll_ns)
  2492. vc->halt_poll_ns = halt_poll_ns;
  2493. } else
  2494. vc->halt_poll_ns = 0;
  2495. trace_kvmppc_vcore_wakeup(do_sleep, block_ns);
  2496. }
  2497. static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
  2498. {
  2499. int n_ceded, i;
  2500. struct kvmppc_vcore *vc;
  2501. struct kvm_vcpu *v;
  2502. trace_kvmppc_run_vcpu_enter(vcpu);
  2503. kvm_run->exit_reason = 0;
  2504. vcpu->arch.ret = RESUME_GUEST;
  2505. vcpu->arch.trap = 0;
  2506. kvmppc_update_vpas(vcpu);
  2507. /*
  2508. * Synchronize with other threads in this virtual core
  2509. */
  2510. vc = vcpu->arch.vcore;
  2511. spin_lock(&vc->lock);
  2512. vcpu->arch.ceded = 0;
  2513. vcpu->arch.run_task = current;
  2514. vcpu->arch.kvm_run = kvm_run;
  2515. vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb());
  2516. vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
  2517. vcpu->arch.busy_preempt = TB_NIL;
  2518. WRITE_ONCE(vc->runnable_threads[vcpu->arch.ptid], vcpu);
  2519. ++vc->n_runnable;
  2520. /*
  2521. * This happens the first time this is called for a vcpu.
  2522. * If the vcore is already running, we may be able to start
  2523. * this thread straight away and have it join in.
  2524. */
  2525. if (!signal_pending(current)) {
  2526. if (vc->vcore_state == VCORE_PIGGYBACK) {
  2527. struct kvmppc_vcore *mvc = vc->master_vcore;
  2528. if (spin_trylock(&mvc->lock)) {
  2529. if (mvc->vcore_state == VCORE_RUNNING &&
  2530. !VCORE_IS_EXITING(mvc)) {
  2531. kvmppc_create_dtl_entry(vcpu, vc);
  2532. kvmppc_start_thread(vcpu, vc);
  2533. trace_kvm_guest_enter(vcpu);
  2534. }
  2535. spin_unlock(&mvc->lock);
  2536. }
  2537. } else if (vc->vcore_state == VCORE_RUNNING &&
  2538. !VCORE_IS_EXITING(vc)) {
  2539. kvmppc_create_dtl_entry(vcpu, vc);
  2540. kvmppc_start_thread(vcpu, vc);
  2541. trace_kvm_guest_enter(vcpu);
  2542. } else if (vc->vcore_state == VCORE_SLEEPING) {
  2543. swake_up(&vc->wq);
  2544. }
  2545. }
  2546. while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
  2547. !signal_pending(current)) {
  2548. if (vc->vcore_state == VCORE_PREEMPT && vc->runner == NULL)
  2549. kvmppc_vcore_end_preempt(vc);
  2550. if (vc->vcore_state != VCORE_INACTIVE) {
  2551. kvmppc_wait_for_exec(vc, vcpu, TASK_INTERRUPTIBLE);
  2552. continue;
  2553. }
  2554. for_each_runnable_thread(i, v, vc) {
  2555. kvmppc_core_prepare_to_enter(v);
  2556. if (signal_pending(v->arch.run_task)) {
  2557. kvmppc_remove_runnable(vc, v);
  2558. v->stat.signal_exits++;
  2559. v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
  2560. v->arch.ret = -EINTR;
  2561. wake_up(&v->arch.cpu_run);
  2562. }
  2563. }
  2564. if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
  2565. break;
  2566. n_ceded = 0;
  2567. for_each_runnable_thread(i, v, vc) {
  2568. if (!v->arch.pending_exceptions && !v->arch.prodded)
  2569. n_ceded += v->arch.ceded;
  2570. else
  2571. v->arch.ceded = 0;
  2572. }
  2573. vc->runner = vcpu;
  2574. if (n_ceded == vc->n_runnable) {
  2575. kvmppc_vcore_blocked(vc);
  2576. } else if (need_resched()) {
  2577. kvmppc_vcore_preempt(vc);
  2578. /* Let something else run */
  2579. cond_resched_lock(&vc->lock);
  2580. if (vc->vcore_state == VCORE_PREEMPT)
  2581. kvmppc_vcore_end_preempt(vc);
  2582. } else {
  2583. kvmppc_run_core(vc);
  2584. }
  2585. vc->runner = NULL;
  2586. }
  2587. while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
  2588. (vc->vcore_state == VCORE_RUNNING ||
  2589. vc->vcore_state == VCORE_EXITING ||
  2590. vc->vcore_state == VCORE_PIGGYBACK))
  2591. kvmppc_wait_for_exec(vc, vcpu, TASK_UNINTERRUPTIBLE);
  2592. if (vc->vcore_state == VCORE_PREEMPT && vc->runner == NULL)
  2593. kvmppc_vcore_end_preempt(vc);
  2594. if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
  2595. kvmppc_remove_runnable(vc, vcpu);
  2596. vcpu->stat.signal_exits++;
  2597. kvm_run->exit_reason = KVM_EXIT_INTR;
  2598. vcpu->arch.ret = -EINTR;
  2599. }
  2600. if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) {
  2601. /* Wake up some vcpu to run the core */
  2602. i = -1;
  2603. v = next_runnable_thread(vc, &i);
  2604. wake_up(&v->arch.cpu_run);
  2605. }
  2606. trace_kvmppc_run_vcpu_exit(vcpu, kvm_run);
  2607. spin_unlock(&vc->lock);
  2608. return vcpu->arch.ret;
  2609. }
  2610. static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu)
  2611. {
  2612. int r;
  2613. int srcu_idx;
  2614. if (!vcpu->arch.sane) {
  2615. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  2616. return -EINVAL;
  2617. }
  2618. kvmppc_core_prepare_to_enter(vcpu);
  2619. /* No need to go into the guest when all we'll do is come back out */
  2620. if (signal_pending(current)) {
  2621. run->exit_reason = KVM_EXIT_INTR;
  2622. return -EINTR;
  2623. }
  2624. atomic_inc(&vcpu->kvm->arch.vcpus_running);
  2625. /* Order vcpus_running vs. hpte_setup_done, see kvmppc_alloc_reset_hpt */
  2626. smp_mb();
  2627. /* On the first time here, set up HTAB and VRMA */
  2628. if (!kvm_is_radix(vcpu->kvm) && !vcpu->kvm->arch.hpte_setup_done) {
  2629. r = kvmppc_hv_setup_htab_rma(vcpu);
  2630. if (r)
  2631. goto out;
  2632. }
  2633. flush_all_to_thread(current);
  2634. vcpu->arch.wqp = &vcpu->arch.vcore->wq;
  2635. vcpu->arch.pgdir = current->mm->pgd;
  2636. vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
  2637. do {
  2638. r = kvmppc_run_vcpu(run, vcpu);
  2639. if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
  2640. !(vcpu->arch.shregs.msr & MSR_PR)) {
  2641. trace_kvm_hcall_enter(vcpu);
  2642. r = kvmppc_pseries_do_hcall(vcpu);
  2643. trace_kvm_hcall_exit(vcpu, r);
  2644. kvmppc_core_prepare_to_enter(vcpu);
  2645. } else if (r == RESUME_PAGE_FAULT) {
  2646. srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
  2647. r = kvmppc_book3s_hv_page_fault(run, vcpu,
  2648. vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
  2649. srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
  2650. } else if (r == RESUME_PASSTHROUGH) {
  2651. if (WARN_ON(xive_enabled()))
  2652. r = H_SUCCESS;
  2653. else
  2654. r = kvmppc_xics_rm_complete(vcpu, 0);
  2655. }
  2656. } while (is_kvmppc_resume_guest(r));
  2657. out:
  2658. vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
  2659. atomic_dec(&vcpu->kvm->arch.vcpus_running);
  2660. return r;
  2661. }
  2662. static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
  2663. int linux_psize)
  2664. {
  2665. struct mmu_psize_def *def = &mmu_psize_defs[linux_psize];
  2666. if (!def->shift)
  2667. return;
  2668. (*sps)->page_shift = def->shift;
  2669. (*sps)->slb_enc = def->sllp;
  2670. (*sps)->enc[0].page_shift = def->shift;
  2671. (*sps)->enc[0].pte_enc = def->penc[linux_psize];
  2672. /*
  2673. * Add 16MB MPSS support if host supports it
  2674. */
  2675. if (linux_psize != MMU_PAGE_16M && def->penc[MMU_PAGE_16M] != -1) {
  2676. (*sps)->enc[1].page_shift = 24;
  2677. (*sps)->enc[1].pte_enc = def->penc[MMU_PAGE_16M];
  2678. }
  2679. (*sps)++;
  2680. }
  2681. static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm *kvm,
  2682. struct kvm_ppc_smmu_info *info)
  2683. {
  2684. struct kvm_ppc_one_seg_page_size *sps;
  2685. /*
  2686. * Since we don't yet support HPT guests on a radix host,
  2687. * return an error if the host uses radix.
  2688. */
  2689. if (radix_enabled())
  2690. return -EINVAL;
  2691. info->flags = KVM_PPC_PAGE_SIZES_REAL;
  2692. if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
  2693. info->flags |= KVM_PPC_1T_SEGMENTS;
  2694. info->slb_size = mmu_slb_size;
  2695. /* We only support these sizes for now, and no muti-size segments */
  2696. sps = &info->sps[0];
  2697. kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K);
  2698. kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K);
  2699. kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M);
  2700. return 0;
  2701. }
  2702. /*
  2703. * Get (and clear) the dirty memory log for a memory slot.
  2704. */
  2705. static int kvm_vm_ioctl_get_dirty_log_hv(struct kvm *kvm,
  2706. struct kvm_dirty_log *log)
  2707. {
  2708. struct kvm_memslots *slots;
  2709. struct kvm_memory_slot *memslot;
  2710. int i, r;
  2711. unsigned long n;
  2712. unsigned long *buf;
  2713. struct kvm_vcpu *vcpu;
  2714. mutex_lock(&kvm->slots_lock);
  2715. r = -EINVAL;
  2716. if (log->slot >= KVM_USER_MEM_SLOTS)
  2717. goto out;
  2718. slots = kvm_memslots(kvm);
  2719. memslot = id_to_memslot(slots, log->slot);
  2720. r = -ENOENT;
  2721. if (!memslot->dirty_bitmap)
  2722. goto out;
  2723. /*
  2724. * Use second half of bitmap area because radix accumulates
  2725. * bits in the first half.
  2726. */
  2727. n = kvm_dirty_bitmap_bytes(memslot);
  2728. buf = memslot->dirty_bitmap + n / sizeof(long);
  2729. memset(buf, 0, n);
  2730. if (kvm_is_radix(kvm))
  2731. r = kvmppc_hv_get_dirty_log_radix(kvm, memslot, buf);
  2732. else
  2733. r = kvmppc_hv_get_dirty_log_hpt(kvm, memslot, buf);
  2734. if (r)
  2735. goto out;
  2736. /* Harvest dirty bits from VPA and DTL updates */
  2737. /* Note: we never modify the SLB shadow buffer areas */
  2738. kvm_for_each_vcpu(i, vcpu, kvm) {
  2739. spin_lock(&vcpu->arch.vpa_update_lock);
  2740. kvmppc_harvest_vpa_dirty(&vcpu->arch.vpa, memslot, buf);
  2741. kvmppc_harvest_vpa_dirty(&vcpu->arch.dtl, memslot, buf);
  2742. spin_unlock(&vcpu->arch.vpa_update_lock);
  2743. }
  2744. r = -EFAULT;
  2745. if (copy_to_user(log->dirty_bitmap, buf, n))
  2746. goto out;
  2747. r = 0;
  2748. out:
  2749. mutex_unlock(&kvm->slots_lock);
  2750. return r;
  2751. }
  2752. static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *free,
  2753. struct kvm_memory_slot *dont)
  2754. {
  2755. if (!dont || free->arch.rmap != dont->arch.rmap) {
  2756. vfree(free->arch.rmap);
  2757. free->arch.rmap = NULL;
  2758. }
  2759. }
  2760. static int kvmppc_core_create_memslot_hv(struct kvm_memory_slot *slot,
  2761. unsigned long npages)
  2762. {
  2763. /*
  2764. * For now, if radix_enabled() then we only support radix guests,
  2765. * and in that case we don't need the rmap array.
  2766. */
  2767. if (radix_enabled()) {
  2768. slot->arch.rmap = NULL;
  2769. return 0;
  2770. }
  2771. slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap));
  2772. if (!slot->arch.rmap)
  2773. return -ENOMEM;
  2774. return 0;
  2775. }
  2776. static int kvmppc_core_prepare_memory_region_hv(struct kvm *kvm,
  2777. struct kvm_memory_slot *memslot,
  2778. const struct kvm_userspace_memory_region *mem)
  2779. {
  2780. return 0;
  2781. }
  2782. static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm,
  2783. const struct kvm_userspace_memory_region *mem,
  2784. const struct kvm_memory_slot *old,
  2785. const struct kvm_memory_slot *new)
  2786. {
  2787. unsigned long npages = mem->memory_size >> PAGE_SHIFT;
  2788. struct kvm_memslots *slots;
  2789. struct kvm_memory_slot *memslot;
  2790. /*
  2791. * If we are making a new memslot, it might make
  2792. * some address that was previously cached as emulated
  2793. * MMIO be no longer emulated MMIO, so invalidate
  2794. * all the caches of emulated MMIO translations.
  2795. */
  2796. if (npages)
  2797. atomic64_inc(&kvm->arch.mmio_update);
  2798. if (npages && old->npages && !kvm_is_radix(kvm)) {
  2799. /*
  2800. * If modifying a memslot, reset all the rmap dirty bits.
  2801. * If this is a new memslot, we don't need to do anything
  2802. * since the rmap array starts out as all zeroes,
  2803. * i.e. no pages are dirty.
  2804. */
  2805. slots = kvm_memslots(kvm);
  2806. memslot = id_to_memslot(slots, mem->slot);
  2807. kvmppc_hv_get_dirty_log_hpt(kvm, memslot, NULL);
  2808. }
  2809. }
  2810. /*
  2811. * Update LPCR values in kvm->arch and in vcores.
  2812. * Caller must hold kvm->lock.
  2813. */
  2814. void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr, unsigned long mask)
  2815. {
  2816. long int i;
  2817. u32 cores_done = 0;
  2818. if ((kvm->arch.lpcr & mask) == lpcr)
  2819. return;
  2820. kvm->arch.lpcr = (kvm->arch.lpcr & ~mask) | lpcr;
  2821. for (i = 0; i < KVM_MAX_VCORES; ++i) {
  2822. struct kvmppc_vcore *vc = kvm->arch.vcores[i];
  2823. if (!vc)
  2824. continue;
  2825. spin_lock(&vc->lock);
  2826. vc->lpcr = (vc->lpcr & ~mask) | lpcr;
  2827. spin_unlock(&vc->lock);
  2828. if (++cores_done >= kvm->arch.online_vcores)
  2829. break;
  2830. }
  2831. }
  2832. static void kvmppc_mmu_destroy_hv(struct kvm_vcpu *vcpu)
  2833. {
  2834. return;
  2835. }
  2836. static void kvmppc_setup_partition_table(struct kvm *kvm)
  2837. {
  2838. unsigned long dw0, dw1;
  2839. if (!kvm_is_radix(kvm)) {
  2840. /* PS field - page size for VRMA */
  2841. dw0 = ((kvm->arch.vrma_slb_v & SLB_VSID_L) >> 1) |
  2842. ((kvm->arch.vrma_slb_v & SLB_VSID_LP) << 1);
  2843. /* HTABSIZE and HTABORG fields */
  2844. dw0 |= kvm->arch.sdr1;
  2845. /* Second dword as set by userspace */
  2846. dw1 = kvm->arch.process_table;
  2847. } else {
  2848. dw0 = PATB_HR | radix__get_tree_size() |
  2849. __pa(kvm->arch.pgtable) | RADIX_PGD_INDEX_SIZE;
  2850. dw1 = PATB_GR | kvm->arch.process_table;
  2851. }
  2852. mmu_partition_table_set_entry(kvm->arch.lpid, dw0, dw1);
  2853. }
  2854. static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
  2855. {
  2856. int err = 0;
  2857. struct kvm *kvm = vcpu->kvm;
  2858. unsigned long hva;
  2859. struct kvm_memory_slot *memslot;
  2860. struct vm_area_struct *vma;
  2861. unsigned long lpcr = 0, senc;
  2862. unsigned long psize, porder;
  2863. int srcu_idx;
  2864. mutex_lock(&kvm->lock);
  2865. if (kvm->arch.hpte_setup_done)
  2866. goto out; /* another vcpu beat us to it */
  2867. /* Allocate hashed page table (if not done already) and reset it */
  2868. if (!kvm->arch.hpt.virt) {
  2869. int order = KVM_DEFAULT_HPT_ORDER;
  2870. struct kvm_hpt_info info;
  2871. err = kvmppc_allocate_hpt(&info, order);
  2872. /* If we get here, it means userspace didn't specify a
  2873. * size explicitly. So, try successively smaller
  2874. * sizes if the default failed. */
  2875. while ((err == -ENOMEM) && --order >= PPC_MIN_HPT_ORDER)
  2876. err = kvmppc_allocate_hpt(&info, order);
  2877. if (err < 0) {
  2878. pr_err("KVM: Couldn't alloc HPT\n");
  2879. goto out;
  2880. }
  2881. kvmppc_set_hpt(kvm, &info);
  2882. }
  2883. /* Look up the memslot for guest physical address 0 */
  2884. srcu_idx = srcu_read_lock(&kvm->srcu);
  2885. memslot = gfn_to_memslot(kvm, 0);
  2886. /* We must have some memory at 0 by now */
  2887. err = -EINVAL;
  2888. if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
  2889. goto out_srcu;
  2890. /* Look up the VMA for the start of this memory slot */
  2891. hva = memslot->userspace_addr;
  2892. down_read(&current->mm->mmap_sem);
  2893. vma = find_vma(current->mm, hva);
  2894. if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
  2895. goto up_out;
  2896. psize = vma_kernel_pagesize(vma);
  2897. porder = __ilog2(psize);
  2898. up_read(&current->mm->mmap_sem);
  2899. /* We can handle 4k, 64k or 16M pages in the VRMA */
  2900. err = -EINVAL;
  2901. if (!(psize == 0x1000 || psize == 0x10000 ||
  2902. psize == 0x1000000))
  2903. goto out_srcu;
  2904. senc = slb_pgsize_encoding(psize);
  2905. kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
  2906. (VRMA_VSID << SLB_VSID_SHIFT_1T);
  2907. /* Create HPTEs in the hash page table for the VRMA */
  2908. kvmppc_map_vrma(vcpu, memslot, porder);
  2909. /* Update VRMASD field in the LPCR */
  2910. if (!cpu_has_feature(CPU_FTR_ARCH_300)) {
  2911. /* the -4 is to account for senc values starting at 0x10 */
  2912. lpcr = senc << (LPCR_VRMASD_SH - 4);
  2913. kvmppc_update_lpcr(kvm, lpcr, LPCR_VRMASD);
  2914. } else {
  2915. kvmppc_setup_partition_table(kvm);
  2916. }
  2917. /* Order updates to kvm->arch.lpcr etc. vs. hpte_setup_done */
  2918. smp_wmb();
  2919. kvm->arch.hpte_setup_done = 1;
  2920. err = 0;
  2921. out_srcu:
  2922. srcu_read_unlock(&kvm->srcu, srcu_idx);
  2923. out:
  2924. mutex_unlock(&kvm->lock);
  2925. return err;
  2926. up_out:
  2927. up_read(&current->mm->mmap_sem);
  2928. goto out_srcu;
  2929. }
  2930. #ifdef CONFIG_KVM_XICS
  2931. /*
  2932. * Allocate a per-core structure for managing state about which cores are
  2933. * running in the host versus the guest and for exchanging data between
  2934. * real mode KVM and CPU running in the host.
  2935. * This is only done for the first VM.
  2936. * The allocated structure stays even if all VMs have stopped.
  2937. * It is only freed when the kvm-hv module is unloaded.
  2938. * It's OK for this routine to fail, we just don't support host
  2939. * core operations like redirecting H_IPI wakeups.
  2940. */
  2941. void kvmppc_alloc_host_rm_ops(void)
  2942. {
  2943. struct kvmppc_host_rm_ops *ops;
  2944. unsigned long l_ops;
  2945. int cpu, core;
  2946. int size;
  2947. /* Not the first time here ? */
  2948. if (kvmppc_host_rm_ops_hv != NULL)
  2949. return;
  2950. ops = kzalloc(sizeof(struct kvmppc_host_rm_ops), GFP_KERNEL);
  2951. if (!ops)
  2952. return;
  2953. size = cpu_nr_cores() * sizeof(struct kvmppc_host_rm_core);
  2954. ops->rm_core = kzalloc(size, GFP_KERNEL);
  2955. if (!ops->rm_core) {
  2956. kfree(ops);
  2957. return;
  2958. }
  2959. get_online_cpus();
  2960. for (cpu = 0; cpu < nr_cpu_ids; cpu += threads_per_core) {
  2961. if (!cpu_online(cpu))
  2962. continue;
  2963. core = cpu >> threads_shift;
  2964. ops->rm_core[core].rm_state.in_host = 1;
  2965. }
  2966. ops->vcpu_kick = kvmppc_fast_vcpu_kick_hv;
  2967. /*
  2968. * Make the contents of the kvmppc_host_rm_ops structure visible
  2969. * to other CPUs before we assign it to the global variable.
  2970. * Do an atomic assignment (no locks used here), but if someone
  2971. * beats us to it, just free our copy and return.
  2972. */
  2973. smp_wmb();
  2974. l_ops = (unsigned long) ops;
  2975. if (cmpxchg64((unsigned long *)&kvmppc_host_rm_ops_hv, 0, l_ops)) {
  2976. put_online_cpus();
  2977. kfree(ops->rm_core);
  2978. kfree(ops);
  2979. return;
  2980. }
  2981. cpuhp_setup_state_nocalls(CPUHP_KVM_PPC_BOOK3S_PREPARE,
  2982. "ppc/kvm_book3s:prepare",
  2983. kvmppc_set_host_core,
  2984. kvmppc_clear_host_core);
  2985. put_online_cpus();
  2986. }
  2987. void kvmppc_free_host_rm_ops(void)
  2988. {
  2989. if (kvmppc_host_rm_ops_hv) {
  2990. cpuhp_remove_state_nocalls(CPUHP_KVM_PPC_BOOK3S_PREPARE);
  2991. kfree(kvmppc_host_rm_ops_hv->rm_core);
  2992. kfree(kvmppc_host_rm_ops_hv);
  2993. kvmppc_host_rm_ops_hv = NULL;
  2994. }
  2995. }
  2996. #endif
  2997. static int kvmppc_core_init_vm_hv(struct kvm *kvm)
  2998. {
  2999. unsigned long lpcr, lpid;
  3000. char buf[32];
  3001. int ret;
  3002. /* Allocate the guest's logical partition ID */
  3003. lpid = kvmppc_alloc_lpid();
  3004. if ((long)lpid < 0)
  3005. return -ENOMEM;
  3006. kvm->arch.lpid = lpid;
  3007. kvmppc_alloc_host_rm_ops();
  3008. /*
  3009. * Since we don't flush the TLB when tearing down a VM,
  3010. * and this lpid might have previously been used,
  3011. * make sure we flush on each core before running the new VM.
  3012. * On POWER9, the tlbie in mmu_partition_table_set_entry()
  3013. * does this flush for us.
  3014. */
  3015. if (!cpu_has_feature(CPU_FTR_ARCH_300))
  3016. cpumask_setall(&kvm->arch.need_tlb_flush);
  3017. /* Start out with the default set of hcalls enabled */
  3018. memcpy(kvm->arch.enabled_hcalls, default_enabled_hcalls,
  3019. sizeof(kvm->arch.enabled_hcalls));
  3020. if (!cpu_has_feature(CPU_FTR_ARCH_300))
  3021. kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
  3022. /* Init LPCR for virtual RMA mode */
  3023. kvm->arch.host_lpid = mfspr(SPRN_LPID);
  3024. kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
  3025. lpcr &= LPCR_PECE | LPCR_LPES;
  3026. lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
  3027. LPCR_VPM0 | LPCR_VPM1;
  3028. kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
  3029. (VRMA_VSID << SLB_VSID_SHIFT_1T);
  3030. /* On POWER8 turn on online bit to enable PURR/SPURR */
  3031. if (cpu_has_feature(CPU_FTR_ARCH_207S))
  3032. lpcr |= LPCR_ONL;
  3033. /*
  3034. * On POWER9, VPM0 bit is reserved (VPM0=1 behaviour is assumed)
  3035. * Set HVICE bit to enable hypervisor virtualization interrupts.
  3036. * Set HEIC to prevent OS interrupts to go to hypervisor (should
  3037. * be unnecessary but better safe than sorry in case we re-enable
  3038. * EE in HV mode with this LPCR still set)
  3039. */
  3040. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  3041. lpcr &= ~LPCR_VPM0;
  3042. lpcr |= LPCR_HVICE | LPCR_HEIC;
  3043. /*
  3044. * If xive is enabled, we route 0x500 interrupts directly
  3045. * to the guest.
  3046. */
  3047. if (xive_enabled())
  3048. lpcr |= LPCR_LPES;
  3049. }
  3050. /*
  3051. * For now, if the host uses radix, the guest must be radix.
  3052. */
  3053. if (radix_enabled()) {
  3054. kvm->arch.radix = 1;
  3055. lpcr &= ~LPCR_VPM1;
  3056. lpcr |= LPCR_UPRT | LPCR_GTSE | LPCR_HR;
  3057. ret = kvmppc_init_vm_radix(kvm);
  3058. if (ret) {
  3059. kvmppc_free_lpid(kvm->arch.lpid);
  3060. return ret;
  3061. }
  3062. kvmppc_setup_partition_table(kvm);
  3063. }
  3064. kvm->arch.lpcr = lpcr;
  3065. /* Initialization for future HPT resizes */
  3066. kvm->arch.resize_hpt = NULL;
  3067. /*
  3068. * Work out how many sets the TLB has, for the use of
  3069. * the TLB invalidation loop in book3s_hv_rmhandlers.S.
  3070. */
  3071. if (kvm_is_radix(kvm))
  3072. kvm->arch.tlb_sets = POWER9_TLB_SETS_RADIX; /* 128 */
  3073. else if (cpu_has_feature(CPU_FTR_ARCH_300))
  3074. kvm->arch.tlb_sets = POWER9_TLB_SETS_HASH; /* 256 */
  3075. else if (cpu_has_feature(CPU_FTR_ARCH_207S))
  3076. kvm->arch.tlb_sets = POWER8_TLB_SETS; /* 512 */
  3077. else
  3078. kvm->arch.tlb_sets = POWER7_TLB_SETS; /* 128 */
  3079. /*
  3080. * Track that we now have a HV mode VM active. This blocks secondary
  3081. * CPU threads from coming online.
  3082. * On POWER9, we only need to do this for HPT guests on a radix
  3083. * host, which is not yet supported.
  3084. */
  3085. if (!cpu_has_feature(CPU_FTR_ARCH_300))
  3086. kvm_hv_vm_activated();
  3087. /*
  3088. * Create a debugfs directory for the VM
  3089. */
  3090. snprintf(buf, sizeof(buf), "vm%d", current->pid);
  3091. kvm->arch.debugfs_dir = debugfs_create_dir(buf, kvm_debugfs_dir);
  3092. if (!IS_ERR_OR_NULL(kvm->arch.debugfs_dir))
  3093. kvmppc_mmu_debugfs_init(kvm);
  3094. return 0;
  3095. }
  3096. static void kvmppc_free_vcores(struct kvm *kvm)
  3097. {
  3098. long int i;
  3099. for (i = 0; i < KVM_MAX_VCORES; ++i)
  3100. kfree(kvm->arch.vcores[i]);
  3101. kvm->arch.online_vcores = 0;
  3102. }
  3103. static void kvmppc_core_destroy_vm_hv(struct kvm *kvm)
  3104. {
  3105. debugfs_remove_recursive(kvm->arch.debugfs_dir);
  3106. if (!cpu_has_feature(CPU_FTR_ARCH_300))
  3107. kvm_hv_vm_deactivated();
  3108. kvmppc_free_vcores(kvm);
  3109. kvmppc_free_lpid(kvm->arch.lpid);
  3110. if (kvm_is_radix(kvm))
  3111. kvmppc_free_radix(kvm);
  3112. else
  3113. kvmppc_free_hpt(&kvm->arch.hpt);
  3114. kvmppc_free_pimap(kvm);
  3115. }
  3116. /* We don't need to emulate any privileged instructions or dcbz */
  3117. static int kvmppc_core_emulate_op_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
  3118. unsigned int inst, int *advance)
  3119. {
  3120. return EMULATE_FAIL;
  3121. }
  3122. static int kvmppc_core_emulate_mtspr_hv(struct kvm_vcpu *vcpu, int sprn,
  3123. ulong spr_val)
  3124. {
  3125. return EMULATE_FAIL;
  3126. }
  3127. static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn,
  3128. ulong *spr_val)
  3129. {
  3130. return EMULATE_FAIL;
  3131. }
  3132. static int kvmppc_core_check_processor_compat_hv(void)
  3133. {
  3134. if (!cpu_has_feature(CPU_FTR_HVMODE) ||
  3135. !cpu_has_feature(CPU_FTR_ARCH_206))
  3136. return -EIO;
  3137. return 0;
  3138. }
  3139. #ifdef CONFIG_KVM_XICS
  3140. void kvmppc_free_pimap(struct kvm *kvm)
  3141. {
  3142. kfree(kvm->arch.pimap);
  3143. }
  3144. static struct kvmppc_passthru_irqmap *kvmppc_alloc_pimap(void)
  3145. {
  3146. return kzalloc(sizeof(struct kvmppc_passthru_irqmap), GFP_KERNEL);
  3147. }
  3148. static int kvmppc_set_passthru_irq(struct kvm *kvm, int host_irq, int guest_gsi)
  3149. {
  3150. struct irq_desc *desc;
  3151. struct kvmppc_irq_map *irq_map;
  3152. struct kvmppc_passthru_irqmap *pimap;
  3153. struct irq_chip *chip;
  3154. int i, rc = 0;
  3155. if (!kvm_irq_bypass)
  3156. return 1;
  3157. desc = irq_to_desc(host_irq);
  3158. if (!desc)
  3159. return -EIO;
  3160. mutex_lock(&kvm->lock);
  3161. pimap = kvm->arch.pimap;
  3162. if (pimap == NULL) {
  3163. /* First call, allocate structure to hold IRQ map */
  3164. pimap = kvmppc_alloc_pimap();
  3165. if (pimap == NULL) {
  3166. mutex_unlock(&kvm->lock);
  3167. return -ENOMEM;
  3168. }
  3169. kvm->arch.pimap = pimap;
  3170. }
  3171. /*
  3172. * For now, we only support interrupts for which the EOI operation
  3173. * is an OPAL call followed by a write to XIRR, since that's
  3174. * what our real-mode EOI code does, or a XIVE interrupt
  3175. */
  3176. chip = irq_data_get_irq_chip(&desc->irq_data);
  3177. if (!chip || !(is_pnv_opal_msi(chip) || is_xive_irq(chip))) {
  3178. pr_warn("kvmppc_set_passthru_irq_hv: Could not assign IRQ map for (%d,%d)\n",
  3179. host_irq, guest_gsi);
  3180. mutex_unlock(&kvm->lock);
  3181. return -ENOENT;
  3182. }
  3183. /*
  3184. * See if we already have an entry for this guest IRQ number.
  3185. * If it's mapped to a hardware IRQ number, that's an error,
  3186. * otherwise re-use this entry.
  3187. */
  3188. for (i = 0; i < pimap->n_mapped; i++) {
  3189. if (guest_gsi == pimap->mapped[i].v_hwirq) {
  3190. if (pimap->mapped[i].r_hwirq) {
  3191. mutex_unlock(&kvm->lock);
  3192. return -EINVAL;
  3193. }
  3194. break;
  3195. }
  3196. }
  3197. if (i == KVMPPC_PIRQ_MAPPED) {
  3198. mutex_unlock(&kvm->lock);
  3199. return -EAGAIN; /* table is full */
  3200. }
  3201. irq_map = &pimap->mapped[i];
  3202. irq_map->v_hwirq = guest_gsi;
  3203. irq_map->desc = desc;
  3204. /*
  3205. * Order the above two stores before the next to serialize with
  3206. * the KVM real mode handler.
  3207. */
  3208. smp_wmb();
  3209. irq_map->r_hwirq = desc->irq_data.hwirq;
  3210. if (i == pimap->n_mapped)
  3211. pimap->n_mapped++;
  3212. if (xive_enabled())
  3213. rc = kvmppc_xive_set_mapped(kvm, guest_gsi, desc);
  3214. else
  3215. kvmppc_xics_set_mapped(kvm, guest_gsi, desc->irq_data.hwirq);
  3216. if (rc)
  3217. irq_map->r_hwirq = 0;
  3218. mutex_unlock(&kvm->lock);
  3219. return 0;
  3220. }
  3221. static int kvmppc_clr_passthru_irq(struct kvm *kvm, int host_irq, int guest_gsi)
  3222. {
  3223. struct irq_desc *desc;
  3224. struct kvmppc_passthru_irqmap *pimap;
  3225. int i, rc = 0;
  3226. if (!kvm_irq_bypass)
  3227. return 0;
  3228. desc = irq_to_desc(host_irq);
  3229. if (!desc)
  3230. return -EIO;
  3231. mutex_lock(&kvm->lock);
  3232. if (!kvm->arch.pimap)
  3233. goto unlock;
  3234. pimap = kvm->arch.pimap;
  3235. for (i = 0; i < pimap->n_mapped; i++) {
  3236. if (guest_gsi == pimap->mapped[i].v_hwirq)
  3237. break;
  3238. }
  3239. if (i == pimap->n_mapped) {
  3240. mutex_unlock(&kvm->lock);
  3241. return -ENODEV;
  3242. }
  3243. if (xive_enabled())
  3244. rc = kvmppc_xive_clr_mapped(kvm, guest_gsi, pimap->mapped[i].desc);
  3245. else
  3246. kvmppc_xics_clr_mapped(kvm, guest_gsi, pimap->mapped[i].r_hwirq);
  3247. /* invalidate the entry (what do do on error from the above ?) */
  3248. pimap->mapped[i].r_hwirq = 0;
  3249. /*
  3250. * We don't free this structure even when the count goes to
  3251. * zero. The structure is freed when we destroy the VM.
  3252. */
  3253. unlock:
  3254. mutex_unlock(&kvm->lock);
  3255. return rc;
  3256. }
  3257. static int kvmppc_irq_bypass_add_producer_hv(struct irq_bypass_consumer *cons,
  3258. struct irq_bypass_producer *prod)
  3259. {
  3260. int ret = 0;
  3261. struct kvm_kernel_irqfd *irqfd =
  3262. container_of(cons, struct kvm_kernel_irqfd, consumer);
  3263. irqfd->producer = prod;
  3264. ret = kvmppc_set_passthru_irq(irqfd->kvm, prod->irq, irqfd->gsi);
  3265. if (ret)
  3266. pr_info("kvmppc_set_passthru_irq (irq %d, gsi %d) fails: %d\n",
  3267. prod->irq, irqfd->gsi, ret);
  3268. return ret;
  3269. }
  3270. static void kvmppc_irq_bypass_del_producer_hv(struct irq_bypass_consumer *cons,
  3271. struct irq_bypass_producer *prod)
  3272. {
  3273. int ret;
  3274. struct kvm_kernel_irqfd *irqfd =
  3275. container_of(cons, struct kvm_kernel_irqfd, consumer);
  3276. irqfd->producer = NULL;
  3277. /*
  3278. * When producer of consumer is unregistered, we change back to
  3279. * default external interrupt handling mode - KVM real mode
  3280. * will switch back to host.
  3281. */
  3282. ret = kvmppc_clr_passthru_irq(irqfd->kvm, prod->irq, irqfd->gsi);
  3283. if (ret)
  3284. pr_warn("kvmppc_clr_passthru_irq (irq %d, gsi %d) fails: %d\n",
  3285. prod->irq, irqfd->gsi, ret);
  3286. }
  3287. #endif
  3288. static long kvm_arch_vm_ioctl_hv(struct file *filp,
  3289. unsigned int ioctl, unsigned long arg)
  3290. {
  3291. struct kvm *kvm __maybe_unused = filp->private_data;
  3292. void __user *argp = (void __user *)arg;
  3293. long r;
  3294. switch (ioctl) {
  3295. case KVM_PPC_ALLOCATE_HTAB: {
  3296. u32 htab_order;
  3297. r = -EFAULT;
  3298. if (get_user(htab_order, (u32 __user *)argp))
  3299. break;
  3300. r = kvmppc_alloc_reset_hpt(kvm, htab_order);
  3301. if (r)
  3302. break;
  3303. r = 0;
  3304. break;
  3305. }
  3306. case KVM_PPC_GET_HTAB_FD: {
  3307. struct kvm_get_htab_fd ghf;
  3308. r = -EFAULT;
  3309. if (copy_from_user(&ghf, argp, sizeof(ghf)))
  3310. break;
  3311. r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf);
  3312. break;
  3313. }
  3314. case KVM_PPC_RESIZE_HPT_PREPARE: {
  3315. struct kvm_ppc_resize_hpt rhpt;
  3316. r = -EFAULT;
  3317. if (copy_from_user(&rhpt, argp, sizeof(rhpt)))
  3318. break;
  3319. r = kvm_vm_ioctl_resize_hpt_prepare(kvm, &rhpt);
  3320. break;
  3321. }
  3322. case KVM_PPC_RESIZE_HPT_COMMIT: {
  3323. struct kvm_ppc_resize_hpt rhpt;
  3324. r = -EFAULT;
  3325. if (copy_from_user(&rhpt, argp, sizeof(rhpt)))
  3326. break;
  3327. r = kvm_vm_ioctl_resize_hpt_commit(kvm, &rhpt);
  3328. break;
  3329. }
  3330. default:
  3331. r = -ENOTTY;
  3332. }
  3333. return r;
  3334. }
  3335. /*
  3336. * List of hcall numbers to enable by default.
  3337. * For compatibility with old userspace, we enable by default
  3338. * all hcalls that were implemented before the hcall-enabling
  3339. * facility was added. Note this list should not include H_RTAS.
  3340. */
  3341. static unsigned int default_hcall_list[] = {
  3342. H_REMOVE,
  3343. H_ENTER,
  3344. H_READ,
  3345. H_PROTECT,
  3346. H_BULK_REMOVE,
  3347. H_GET_TCE,
  3348. H_PUT_TCE,
  3349. H_SET_DABR,
  3350. H_SET_XDABR,
  3351. H_CEDE,
  3352. H_PROD,
  3353. H_CONFER,
  3354. H_REGISTER_VPA,
  3355. #ifdef CONFIG_KVM_XICS
  3356. H_EOI,
  3357. H_CPPR,
  3358. H_IPI,
  3359. H_IPOLL,
  3360. H_XIRR,
  3361. H_XIRR_X,
  3362. #endif
  3363. 0
  3364. };
  3365. static void init_default_hcalls(void)
  3366. {
  3367. int i;
  3368. unsigned int hcall;
  3369. for (i = 0; default_hcall_list[i]; ++i) {
  3370. hcall = default_hcall_list[i];
  3371. WARN_ON(!kvmppc_hcall_impl_hv(hcall));
  3372. __set_bit(hcall / 4, default_enabled_hcalls);
  3373. }
  3374. }
  3375. static int kvmhv_configure_mmu(struct kvm *kvm, struct kvm_ppc_mmuv3_cfg *cfg)
  3376. {
  3377. unsigned long lpcr;
  3378. int radix;
  3379. /* If not on a POWER9, reject it */
  3380. if (!cpu_has_feature(CPU_FTR_ARCH_300))
  3381. return -ENODEV;
  3382. /* If any unknown flags set, reject it */
  3383. if (cfg->flags & ~(KVM_PPC_MMUV3_RADIX | KVM_PPC_MMUV3_GTSE))
  3384. return -EINVAL;
  3385. /* We can't change a guest to/from radix yet */
  3386. radix = !!(cfg->flags & KVM_PPC_MMUV3_RADIX);
  3387. if (radix != kvm_is_radix(kvm))
  3388. return -EINVAL;
  3389. /* GR (guest radix) bit in process_table field must match */
  3390. if (!!(cfg->process_table & PATB_GR) != radix)
  3391. return -EINVAL;
  3392. /* Process table size field must be reasonable, i.e. <= 24 */
  3393. if ((cfg->process_table & PRTS_MASK) > 24)
  3394. return -EINVAL;
  3395. kvm->arch.process_table = cfg->process_table;
  3396. kvmppc_setup_partition_table(kvm);
  3397. lpcr = (cfg->flags & KVM_PPC_MMUV3_GTSE) ? LPCR_GTSE : 0;
  3398. kvmppc_update_lpcr(kvm, lpcr, LPCR_GTSE);
  3399. return 0;
  3400. }
  3401. static struct kvmppc_ops kvm_ops_hv = {
  3402. .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv,
  3403. .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,
  3404. .get_one_reg = kvmppc_get_one_reg_hv,
  3405. .set_one_reg = kvmppc_set_one_reg_hv,
  3406. .vcpu_load = kvmppc_core_vcpu_load_hv,
  3407. .vcpu_put = kvmppc_core_vcpu_put_hv,
  3408. .set_msr = kvmppc_set_msr_hv,
  3409. .vcpu_run = kvmppc_vcpu_run_hv,
  3410. .vcpu_create = kvmppc_core_vcpu_create_hv,
  3411. .vcpu_free = kvmppc_core_vcpu_free_hv,
  3412. .check_requests = kvmppc_core_check_requests_hv,
  3413. .get_dirty_log = kvm_vm_ioctl_get_dirty_log_hv,
  3414. .flush_memslot = kvmppc_core_flush_memslot_hv,
  3415. .prepare_memory_region = kvmppc_core_prepare_memory_region_hv,
  3416. .commit_memory_region = kvmppc_core_commit_memory_region_hv,
  3417. .unmap_hva = kvm_unmap_hva_hv,
  3418. .unmap_hva_range = kvm_unmap_hva_range_hv,
  3419. .age_hva = kvm_age_hva_hv,
  3420. .test_age_hva = kvm_test_age_hva_hv,
  3421. .set_spte_hva = kvm_set_spte_hva_hv,
  3422. .mmu_destroy = kvmppc_mmu_destroy_hv,
  3423. .free_memslot = kvmppc_core_free_memslot_hv,
  3424. .create_memslot = kvmppc_core_create_memslot_hv,
  3425. .init_vm = kvmppc_core_init_vm_hv,
  3426. .destroy_vm = kvmppc_core_destroy_vm_hv,
  3427. .get_smmu_info = kvm_vm_ioctl_get_smmu_info_hv,
  3428. .emulate_op = kvmppc_core_emulate_op_hv,
  3429. .emulate_mtspr = kvmppc_core_emulate_mtspr_hv,
  3430. .emulate_mfspr = kvmppc_core_emulate_mfspr_hv,
  3431. .fast_vcpu_kick = kvmppc_fast_vcpu_kick_hv,
  3432. .arch_vm_ioctl = kvm_arch_vm_ioctl_hv,
  3433. .hcall_implemented = kvmppc_hcall_impl_hv,
  3434. #ifdef CONFIG_KVM_XICS
  3435. .irq_bypass_add_producer = kvmppc_irq_bypass_add_producer_hv,
  3436. .irq_bypass_del_producer = kvmppc_irq_bypass_del_producer_hv,
  3437. #endif
  3438. .configure_mmu = kvmhv_configure_mmu,
  3439. .get_rmmu_info = kvmhv_get_rmmu_info,
  3440. };
  3441. static int kvm_init_subcore_bitmap(void)
  3442. {
  3443. int i, j;
  3444. int nr_cores = cpu_nr_cores();
  3445. struct sibling_subcore_state *sibling_subcore_state;
  3446. for (i = 0; i < nr_cores; i++) {
  3447. int first_cpu = i * threads_per_core;
  3448. int node = cpu_to_node(first_cpu);
  3449. /* Ignore if it is already allocated. */
  3450. if (paca[first_cpu].sibling_subcore_state)
  3451. continue;
  3452. sibling_subcore_state =
  3453. kmalloc_node(sizeof(struct sibling_subcore_state),
  3454. GFP_KERNEL, node);
  3455. if (!sibling_subcore_state)
  3456. return -ENOMEM;
  3457. memset(sibling_subcore_state, 0,
  3458. sizeof(struct sibling_subcore_state));
  3459. for (j = 0; j < threads_per_core; j++) {
  3460. int cpu = first_cpu + j;
  3461. paca[cpu].sibling_subcore_state = sibling_subcore_state;
  3462. }
  3463. }
  3464. return 0;
  3465. }
  3466. static int kvmppc_radix_possible(void)
  3467. {
  3468. return cpu_has_feature(CPU_FTR_ARCH_300) && radix_enabled();
  3469. }
  3470. static int kvmppc_book3s_init_hv(void)
  3471. {
  3472. int r;
  3473. /*
  3474. * FIXME!! Do we need to check on all cpus ?
  3475. */
  3476. r = kvmppc_core_check_processor_compat_hv();
  3477. if (r < 0)
  3478. return -ENODEV;
  3479. r = kvm_init_subcore_bitmap();
  3480. if (r)
  3481. return r;
  3482. /*
  3483. * We need a way of accessing the XICS interrupt controller,
  3484. * either directly, via paca[cpu].kvm_hstate.xics_phys, or
  3485. * indirectly, via OPAL.
  3486. */
  3487. #ifdef CONFIG_SMP
  3488. if (!xive_enabled() && !local_paca->kvm_hstate.xics_phys) {
  3489. struct device_node *np;
  3490. np = of_find_compatible_node(NULL, NULL, "ibm,opal-intc");
  3491. if (!np) {
  3492. pr_err("KVM-HV: Cannot determine method for accessing XICS\n");
  3493. return -ENODEV;
  3494. }
  3495. }
  3496. #endif
  3497. kvm_ops_hv.owner = THIS_MODULE;
  3498. kvmppc_hv_ops = &kvm_ops_hv;
  3499. init_default_hcalls();
  3500. init_vcore_lists();
  3501. r = kvmppc_mmu_hv_init();
  3502. if (r)
  3503. return r;
  3504. if (kvmppc_radix_possible())
  3505. r = kvmppc_radix_init();
  3506. return r;
  3507. }
  3508. static void kvmppc_book3s_exit_hv(void)
  3509. {
  3510. kvmppc_free_host_rm_ops();
  3511. if (kvmppc_radix_possible())
  3512. kvmppc_radix_exit();
  3513. kvmppc_hv_ops = NULL;
  3514. }
  3515. module_init(kvmppc_book3s_init_hv);
  3516. module_exit(kvmppc_book3s_exit_hv);
  3517. MODULE_LICENSE("GPL");
  3518. MODULE_ALIAS_MISCDEV(KVM_MINOR);
  3519. MODULE_ALIAS("devname:kvm");