arm.c 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2012 - Virtual Open Systems and Columbia University
  4. * Author: Christoffer Dall <c.dall@virtualopensystems.com>
  5. */
  6. #include <linux/bug.h>
  7. #include <linux/cpu_pm.h>
  8. #include <linux/errno.h>
  9. #include <linux/err.h>
  10. #include <linux/kvm_host.h>
  11. #include <linux/list.h>
  12. #include <linux/module.h>
  13. #include <linux/vmalloc.h>
  14. #include <linux/fs.h>
  15. #include <linux/mman.h>
  16. #include <linux/sched.h>
  17. #include <linux/kvm.h>
  18. #include <linux/kvm_irqfd.h>
  19. #include <linux/irqbypass.h>
  20. #include <linux/sched/stat.h>
  21. #include <linux/psci.h>
  22. #include <trace/events/kvm.h>
  23. #define CREATE_TRACE_POINTS
  24. #include "trace_arm.h"
  25. #include <linux/uaccess.h>
  26. #include <asm/ptrace.h>
  27. #include <asm/mman.h>
  28. #include <asm/tlbflush.h>
  29. #include <asm/cacheflush.h>
  30. #include <asm/cpufeature.h>
  31. #include <asm/virt.h>
  32. #include <asm/kvm_arm.h>
  33. #include <asm/kvm_asm.h>
  34. #include <asm/kvm_emulate.h>
  35. #include <asm/kvm_mmu.h>
  36. #include <asm/kvm_nested.h>
  37. #include <asm/kvm_pkvm.h>
  38. #include <asm/kvm_ptrauth.h>
  39. #include <asm/sections.h>
  40. #include <asm/stacktrace/nvhe.h>
  41. #include <kvm/arm_hypercalls.h>
  42. #include <kvm/arm_pmu.h>
  43. #include <kvm/arm_psci.h>
  44. #include "sys_regs.h"
  45. static enum kvm_mode kvm_mode = KVM_MODE_DEFAULT;
  46. enum kvm_wfx_trap_policy {
  47. KVM_WFX_NOTRAP_SINGLE_TASK, /* Default option */
  48. KVM_WFX_NOTRAP,
  49. KVM_WFX_TRAP,
  50. };
  51. static enum kvm_wfx_trap_policy kvm_wfi_trap_policy __read_mostly = KVM_WFX_NOTRAP_SINGLE_TASK;
  52. static enum kvm_wfx_trap_policy kvm_wfe_trap_policy __read_mostly = KVM_WFX_NOTRAP_SINGLE_TASK;
  53. /*
  54. * Tracks KVM IOCTLs and their associated KVM capabilities.
  55. */
  56. struct kvm_ioctl_cap_map {
  57. unsigned int ioctl;
  58. long ext;
  59. };
  60. /* Make KVM_CAP_NR_VCPUS the reference for features we always supported */
  61. #define KVM_CAP_ARM_BASIC KVM_CAP_NR_VCPUS
  62. /*
  63. * Sorted by ioctl to allow for potential binary search,
  64. * though linear scan is sufficient for this size.
  65. */
  66. static const struct kvm_ioctl_cap_map vm_ioctl_caps[] = {
  67. { KVM_CREATE_IRQCHIP, KVM_CAP_IRQCHIP },
  68. { KVM_ARM_SET_DEVICE_ADDR, KVM_CAP_ARM_SET_DEVICE_ADDR },
  69. { KVM_ARM_MTE_COPY_TAGS, KVM_CAP_ARM_MTE },
  70. { KVM_SET_DEVICE_ATTR, KVM_CAP_DEVICE_CTRL },
  71. { KVM_GET_DEVICE_ATTR, KVM_CAP_DEVICE_CTRL },
  72. { KVM_HAS_DEVICE_ATTR, KVM_CAP_DEVICE_CTRL },
  73. { KVM_ARM_SET_COUNTER_OFFSET, KVM_CAP_COUNTER_OFFSET },
  74. { KVM_ARM_GET_REG_WRITABLE_MASKS, KVM_CAP_ARM_SUPPORTED_REG_MASK_RANGES },
  75. { KVM_ARM_PREFERRED_TARGET, KVM_CAP_ARM_BASIC },
  76. };
  77. /*
  78. * Set *ext to the capability.
  79. * Return 0 if found, or -EINVAL if no IOCTL matches.
  80. */
  81. long kvm_get_cap_for_kvm_ioctl(unsigned int ioctl, long *ext)
  82. {
  83. int i;
  84. for (i = 0; i < ARRAY_SIZE(vm_ioctl_caps); i++) {
  85. if (vm_ioctl_caps[i].ioctl == ioctl) {
  86. *ext = vm_ioctl_caps[i].ext;
  87. return 0;
  88. }
  89. }
  90. return -EINVAL;
  91. }
  92. DECLARE_KVM_HYP_PER_CPU(unsigned long, kvm_hyp_vector);
  93. DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_base);
  94. DECLARE_KVM_NVHE_PER_CPU(struct kvm_nvhe_init_params, kvm_init_params);
  95. DECLARE_KVM_NVHE_PER_CPU(struct kvm_cpu_context, kvm_hyp_ctxt);
  96. static bool vgic_present, kvm_arm_initialised;
  97. static DEFINE_PER_CPU(unsigned char, kvm_hyp_initialized);
  98. bool is_kvm_arm_initialised(void)
  99. {
  100. return kvm_arm_initialised;
  101. }
  102. int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
  103. {
  104. return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
  105. }
  106. int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
  107. struct kvm_enable_cap *cap)
  108. {
  109. int r = -EINVAL;
  110. if (cap->flags)
  111. return -EINVAL;
  112. if (is_protected_kvm_enabled() && !kvm_pkvm_ext_allowed(kvm, cap->cap))
  113. return -EINVAL;
  114. switch (cap->cap) {
  115. case KVM_CAP_ARM_NISV_TO_USER:
  116. r = 0;
  117. set_bit(KVM_ARCH_FLAG_RETURN_NISV_IO_ABORT_TO_USER,
  118. &kvm->arch.flags);
  119. break;
  120. case KVM_CAP_ARM_MTE:
  121. mutex_lock(&kvm->lock);
  122. if (system_supports_mte() && !kvm->created_vcpus) {
  123. r = 0;
  124. set_bit(KVM_ARCH_FLAG_MTE_ENABLED, &kvm->arch.flags);
  125. }
  126. mutex_unlock(&kvm->lock);
  127. break;
  128. case KVM_CAP_ARM_SYSTEM_SUSPEND:
  129. r = 0;
  130. set_bit(KVM_ARCH_FLAG_SYSTEM_SUSPEND_ENABLED, &kvm->arch.flags);
  131. break;
  132. case KVM_CAP_ARM_EAGER_SPLIT_CHUNK_SIZE:
  133. mutex_lock(&kvm->slots_lock);
  134. /*
  135. * To keep things simple, allow changing the chunk
  136. * size only when no memory slots have been created.
  137. */
  138. if (kvm_are_all_memslots_empty(kvm)) {
  139. u64 new_cap = cap->args[0];
  140. if (!new_cap || kvm_is_block_size_supported(new_cap)) {
  141. r = 0;
  142. kvm->arch.mmu.split_page_chunk_size = new_cap;
  143. }
  144. }
  145. mutex_unlock(&kvm->slots_lock);
  146. break;
  147. case KVM_CAP_ARM_WRITABLE_IMP_ID_REGS:
  148. mutex_lock(&kvm->lock);
  149. if (!kvm->created_vcpus) {
  150. r = 0;
  151. set_bit(KVM_ARCH_FLAG_WRITABLE_IMP_ID_REGS, &kvm->arch.flags);
  152. }
  153. mutex_unlock(&kvm->lock);
  154. break;
  155. case KVM_CAP_ARM_SEA_TO_USER:
  156. r = 0;
  157. set_bit(KVM_ARCH_FLAG_EXIT_SEA, &kvm->arch.flags);
  158. break;
  159. default:
  160. break;
  161. }
  162. return r;
  163. }
  164. static int kvm_arm_default_max_vcpus(void)
  165. {
  166. return vgic_present ? kvm_vgic_get_max_vcpus() : KVM_MAX_VCPUS;
  167. }
  168. /**
  169. * kvm_arch_init_vm - initializes a VM data structure
  170. * @kvm: pointer to the KVM struct
  171. * @type: kvm device type
  172. */
  173. int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
  174. {
  175. int ret;
  176. mutex_init(&kvm->arch.config_lock);
  177. #ifdef CONFIG_LOCKDEP
  178. /* Clue in lockdep that the config_lock must be taken inside kvm->lock */
  179. mutex_lock(&kvm->lock);
  180. mutex_lock(&kvm->arch.config_lock);
  181. mutex_unlock(&kvm->arch.config_lock);
  182. mutex_unlock(&kvm->lock);
  183. #endif
  184. kvm_init_nested(kvm);
  185. ret = kvm_share_hyp(kvm, kvm + 1);
  186. if (ret)
  187. return ret;
  188. if (!zalloc_cpumask_var(&kvm->arch.supported_cpus, GFP_KERNEL_ACCOUNT)) {
  189. ret = -ENOMEM;
  190. goto err_unshare_kvm;
  191. }
  192. cpumask_copy(kvm->arch.supported_cpus, cpu_possible_mask);
  193. ret = kvm_init_stage2_mmu(kvm, &kvm->arch.mmu, type);
  194. if (ret)
  195. goto err_free_cpumask;
  196. if (is_protected_kvm_enabled()) {
  197. /*
  198. * If any failures occur after this is successful, make sure to
  199. * call __pkvm_unreserve_vm to unreserve the VM in hyp.
  200. */
  201. ret = pkvm_init_host_vm(kvm);
  202. if (ret)
  203. goto err_free_cpumask;
  204. }
  205. kvm_vgic_early_init(kvm);
  206. kvm_timer_init_vm(kvm);
  207. /* The maximum number of VCPUs is limited by the host's GIC model */
  208. kvm->max_vcpus = kvm_arm_default_max_vcpus();
  209. kvm_arm_init_hypercalls(kvm);
  210. bitmap_zero(kvm->arch.vcpu_features, KVM_VCPU_MAX_FEATURES);
  211. return 0;
  212. err_free_cpumask:
  213. free_cpumask_var(kvm->arch.supported_cpus);
  214. err_unshare_kvm:
  215. kvm_unshare_hyp(kvm, kvm + 1);
  216. return ret;
  217. }
  218. vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
  219. {
  220. return VM_FAULT_SIGBUS;
  221. }
  222. void kvm_arch_create_vm_debugfs(struct kvm *kvm)
  223. {
  224. kvm_sys_regs_create_debugfs(kvm);
  225. kvm_s2_ptdump_create_debugfs(kvm);
  226. }
  227. static void kvm_destroy_mpidr_data(struct kvm *kvm)
  228. {
  229. struct kvm_mpidr_data *data;
  230. mutex_lock(&kvm->arch.config_lock);
  231. data = rcu_dereference_protected(kvm->arch.mpidr_data,
  232. lockdep_is_held(&kvm->arch.config_lock));
  233. if (data) {
  234. rcu_assign_pointer(kvm->arch.mpidr_data, NULL);
  235. synchronize_rcu();
  236. kfree(data);
  237. }
  238. mutex_unlock(&kvm->arch.config_lock);
  239. }
  240. /**
  241. * kvm_arch_destroy_vm - destroy the VM data structure
  242. * @kvm: pointer to the KVM struct
  243. */
  244. void kvm_arch_destroy_vm(struct kvm *kvm)
  245. {
  246. bitmap_free(kvm->arch.pmu_filter);
  247. free_cpumask_var(kvm->arch.supported_cpus);
  248. kvm_vgic_destroy(kvm);
  249. if (is_protected_kvm_enabled())
  250. pkvm_destroy_hyp_vm(kvm);
  251. kvm_destroy_mpidr_data(kvm);
  252. kfree(kvm->arch.sysreg_masks);
  253. kvm_destroy_vcpus(kvm);
  254. kvm_unshare_hyp(kvm, kvm + 1);
  255. kvm_arm_teardown_hypercalls(kvm);
  256. }
  257. static bool kvm_has_full_ptr_auth(void)
  258. {
  259. bool apa, gpa, api, gpi, apa3, gpa3;
  260. u64 isar1, isar2, val;
  261. /*
  262. * Check that:
  263. *
  264. * - both Address and Generic auth are implemented for a given
  265. * algorithm (Q5, IMPDEF or Q3)
  266. * - only a single algorithm is implemented.
  267. */
  268. if (!system_has_full_ptr_auth())
  269. return false;
  270. isar1 = read_sanitised_ftr_reg(SYS_ID_AA64ISAR1_EL1);
  271. isar2 = read_sanitised_ftr_reg(SYS_ID_AA64ISAR2_EL1);
  272. apa = !!FIELD_GET(ID_AA64ISAR1_EL1_APA_MASK, isar1);
  273. val = FIELD_GET(ID_AA64ISAR1_EL1_GPA_MASK, isar1);
  274. gpa = (val == ID_AA64ISAR1_EL1_GPA_IMP);
  275. api = !!FIELD_GET(ID_AA64ISAR1_EL1_API_MASK, isar1);
  276. val = FIELD_GET(ID_AA64ISAR1_EL1_GPI_MASK, isar1);
  277. gpi = (val == ID_AA64ISAR1_EL1_GPI_IMP);
  278. apa3 = !!FIELD_GET(ID_AA64ISAR2_EL1_APA3_MASK, isar2);
  279. val = FIELD_GET(ID_AA64ISAR2_EL1_GPA3_MASK, isar2);
  280. gpa3 = (val == ID_AA64ISAR2_EL1_GPA3_IMP);
  281. return (apa == gpa && api == gpi && apa3 == gpa3 &&
  282. (apa + api + apa3) == 1);
  283. }
  284. int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
  285. {
  286. int r;
  287. if (is_protected_kvm_enabled() && !kvm_pkvm_ext_allowed(kvm, ext))
  288. return 0;
  289. switch (ext) {
  290. case KVM_CAP_IRQCHIP:
  291. r = vgic_present;
  292. break;
  293. case KVM_CAP_IOEVENTFD:
  294. case KVM_CAP_USER_MEMORY:
  295. case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
  296. case KVM_CAP_ONE_REG:
  297. case KVM_CAP_ARM_PSCI:
  298. case KVM_CAP_ARM_PSCI_0_2:
  299. case KVM_CAP_READONLY_MEM:
  300. case KVM_CAP_MP_STATE:
  301. case KVM_CAP_IMMEDIATE_EXIT:
  302. case KVM_CAP_VCPU_EVENTS:
  303. case KVM_CAP_ARM_IRQ_LINE_LAYOUT_2:
  304. case KVM_CAP_ARM_NISV_TO_USER:
  305. case KVM_CAP_ARM_INJECT_EXT_DABT:
  306. case KVM_CAP_SET_GUEST_DEBUG:
  307. case KVM_CAP_VCPU_ATTRIBUTES:
  308. case KVM_CAP_PTP_KVM:
  309. case KVM_CAP_ARM_SYSTEM_SUSPEND:
  310. case KVM_CAP_IRQFD_RESAMPLE:
  311. case KVM_CAP_COUNTER_OFFSET:
  312. case KVM_CAP_ARM_WRITABLE_IMP_ID_REGS:
  313. case KVM_CAP_ARM_SEA_TO_USER:
  314. r = 1;
  315. break;
  316. case KVM_CAP_SET_GUEST_DEBUG2:
  317. return KVM_GUESTDBG_VALID_MASK;
  318. case KVM_CAP_ARM_SET_DEVICE_ADDR:
  319. r = 1;
  320. break;
  321. case KVM_CAP_NR_VCPUS:
  322. /*
  323. * ARM64 treats KVM_CAP_NR_CPUS differently from all other
  324. * architectures, as it does not always bound it to
  325. * KVM_CAP_MAX_VCPUS. It should not matter much because
  326. * this is just an advisory value.
  327. */
  328. r = min_t(unsigned int, num_online_cpus(),
  329. kvm_arm_default_max_vcpus());
  330. break;
  331. case KVM_CAP_MAX_VCPUS:
  332. case KVM_CAP_MAX_VCPU_ID:
  333. if (kvm)
  334. r = kvm->max_vcpus;
  335. else
  336. r = kvm_arm_default_max_vcpus();
  337. break;
  338. case KVM_CAP_MSI_DEVID:
  339. if (!kvm)
  340. r = -EINVAL;
  341. else
  342. r = kvm->arch.vgic.msis_require_devid;
  343. break;
  344. case KVM_CAP_ARM_USER_IRQ:
  345. /*
  346. * 1: EL1_VTIMER, EL1_PTIMER, and PMU.
  347. * (bump this number if adding more devices)
  348. */
  349. r = 1;
  350. break;
  351. case KVM_CAP_ARM_MTE:
  352. r = system_supports_mte();
  353. break;
  354. case KVM_CAP_STEAL_TIME:
  355. r = kvm_arm_pvtime_supported();
  356. break;
  357. case KVM_CAP_ARM_EL1_32BIT:
  358. r = cpus_have_final_cap(ARM64_HAS_32BIT_EL1);
  359. break;
  360. case KVM_CAP_ARM_EL2:
  361. r = cpus_have_final_cap(ARM64_HAS_NESTED_VIRT);
  362. break;
  363. case KVM_CAP_ARM_EL2_E2H0:
  364. r = cpus_have_final_cap(ARM64_HAS_HCR_NV1);
  365. break;
  366. case KVM_CAP_GUEST_DEBUG_HW_BPS:
  367. r = get_num_brps();
  368. break;
  369. case KVM_CAP_GUEST_DEBUG_HW_WPS:
  370. r = get_num_wrps();
  371. break;
  372. case KVM_CAP_ARM_PMU_V3:
  373. r = kvm_supports_guest_pmuv3();
  374. break;
  375. case KVM_CAP_ARM_INJECT_SERROR_ESR:
  376. r = cpus_have_final_cap(ARM64_HAS_RAS_EXTN);
  377. break;
  378. case KVM_CAP_ARM_VM_IPA_SIZE:
  379. r = get_kvm_ipa_limit();
  380. break;
  381. case KVM_CAP_ARM_SVE:
  382. r = system_supports_sve();
  383. break;
  384. case KVM_CAP_ARM_PTRAUTH_ADDRESS:
  385. case KVM_CAP_ARM_PTRAUTH_GENERIC:
  386. r = kvm_has_full_ptr_auth();
  387. break;
  388. case KVM_CAP_ARM_EAGER_SPLIT_CHUNK_SIZE:
  389. if (kvm)
  390. r = kvm->arch.mmu.split_page_chunk_size;
  391. else
  392. r = KVM_ARM_EAGER_SPLIT_CHUNK_SIZE_DEFAULT;
  393. break;
  394. case KVM_CAP_ARM_SUPPORTED_BLOCK_SIZES:
  395. r = kvm_supported_block_sizes();
  396. break;
  397. case KVM_CAP_ARM_SUPPORTED_REG_MASK_RANGES:
  398. r = BIT(0);
  399. break;
  400. case KVM_CAP_ARM_CACHEABLE_PFNMAP_SUPPORTED:
  401. if (!kvm)
  402. r = -EINVAL;
  403. else
  404. r = kvm_supports_cacheable_pfnmap();
  405. break;
  406. default:
  407. r = 0;
  408. }
  409. return r;
  410. }
  411. long kvm_arch_dev_ioctl(struct file *filp,
  412. unsigned int ioctl, unsigned long arg)
  413. {
  414. return -EINVAL;
  415. }
  416. struct kvm *kvm_arch_alloc_vm(void)
  417. {
  418. size_t sz = sizeof(struct kvm);
  419. if (!has_vhe())
  420. return kzalloc(sz, GFP_KERNEL_ACCOUNT);
  421. return kvzalloc(sz, GFP_KERNEL_ACCOUNT);
  422. }
  423. int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
  424. {
  425. if (irqchip_in_kernel(kvm) && vgic_initialized(kvm))
  426. return -EBUSY;
  427. if (id >= kvm->max_vcpus)
  428. return -EINVAL;
  429. return 0;
  430. }
  431. int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
  432. {
  433. int err;
  434. spin_lock_init(&vcpu->arch.mp_state_lock);
  435. #ifdef CONFIG_LOCKDEP
  436. /* Inform lockdep that the config_lock is acquired after vcpu->mutex */
  437. mutex_lock(&vcpu->mutex);
  438. mutex_lock(&vcpu->kvm->arch.config_lock);
  439. mutex_unlock(&vcpu->kvm->arch.config_lock);
  440. mutex_unlock(&vcpu->mutex);
  441. #endif
  442. /* Force users to call KVM_ARM_VCPU_INIT */
  443. vcpu_clear_flag(vcpu, VCPU_INITIALIZED);
  444. vcpu->arch.mmu_page_cache.gfp_zero = __GFP_ZERO;
  445. /* Set up the timer */
  446. kvm_timer_vcpu_init(vcpu);
  447. kvm_pmu_vcpu_init(vcpu);
  448. kvm_arm_pvtime_vcpu_init(&vcpu->arch);
  449. vcpu->arch.hw_mmu = &vcpu->kvm->arch.mmu;
  450. /*
  451. * This vCPU may have been created after mpidr_data was initialized.
  452. * Throw out the pre-computed mappings if that is the case which forces
  453. * KVM to fall back to iteratively searching the vCPUs.
  454. */
  455. kvm_destroy_mpidr_data(vcpu->kvm);
  456. err = kvm_vgic_vcpu_init(vcpu);
  457. if (err)
  458. return err;
  459. err = kvm_share_hyp(vcpu, vcpu + 1);
  460. if (err)
  461. kvm_vgic_vcpu_destroy(vcpu);
  462. return err;
  463. }
  464. void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
  465. {
  466. }
  467. void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
  468. {
  469. if (!is_protected_kvm_enabled())
  470. kvm_mmu_free_memory_cache(&vcpu->arch.mmu_page_cache);
  471. else
  472. free_hyp_memcache(&vcpu->arch.pkvm_memcache);
  473. kvm_timer_vcpu_terminate(vcpu);
  474. kvm_pmu_vcpu_destroy(vcpu);
  475. kvm_vgic_vcpu_destroy(vcpu);
  476. kvm_arm_vcpu_destroy(vcpu);
  477. }
  478. void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu)
  479. {
  480. }
  481. void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu)
  482. {
  483. }
  484. static void vcpu_set_pauth_traps(struct kvm_vcpu *vcpu)
  485. {
  486. if (vcpu_has_ptrauth(vcpu) && !is_protected_kvm_enabled()) {
  487. /*
  488. * Either we're running an L2 guest, and the API/APK bits come
  489. * from L1's HCR_EL2, or API/APK are both set.
  490. */
  491. if (unlikely(is_nested_ctxt(vcpu))) {
  492. u64 val;
  493. val = __vcpu_sys_reg(vcpu, HCR_EL2);
  494. val &= (HCR_API | HCR_APK);
  495. vcpu->arch.hcr_el2 &= ~(HCR_API | HCR_APK);
  496. vcpu->arch.hcr_el2 |= val;
  497. } else {
  498. vcpu->arch.hcr_el2 |= (HCR_API | HCR_APK);
  499. }
  500. /*
  501. * Save the host keys if there is any chance for the guest
  502. * to use pauth, as the entry code will reload the guest
  503. * keys in that case.
  504. */
  505. if (vcpu->arch.hcr_el2 & (HCR_API | HCR_APK)) {
  506. struct kvm_cpu_context *ctxt;
  507. ctxt = this_cpu_ptr_hyp_sym(kvm_hyp_ctxt);
  508. ptrauth_save_keys(ctxt);
  509. }
  510. }
  511. }
  512. static bool kvm_vcpu_should_clear_twi(struct kvm_vcpu *vcpu)
  513. {
  514. if (unlikely(kvm_wfi_trap_policy != KVM_WFX_NOTRAP_SINGLE_TASK))
  515. return kvm_wfi_trap_policy == KVM_WFX_NOTRAP;
  516. return single_task_running() &&
  517. vcpu->kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3 &&
  518. (atomic_read(&vcpu->arch.vgic_cpu.vgic_v3.its_vpe.vlpi_count) ||
  519. vcpu->kvm->arch.vgic.nassgireq);
  520. }
  521. static bool kvm_vcpu_should_clear_twe(struct kvm_vcpu *vcpu)
  522. {
  523. if (unlikely(kvm_wfe_trap_policy != KVM_WFX_NOTRAP_SINGLE_TASK))
  524. return kvm_wfe_trap_policy == KVM_WFX_NOTRAP;
  525. return single_task_running();
  526. }
  527. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  528. {
  529. struct kvm_s2_mmu *mmu;
  530. int *last_ran;
  531. if (is_protected_kvm_enabled())
  532. goto nommu;
  533. if (vcpu_has_nv(vcpu))
  534. kvm_vcpu_load_hw_mmu(vcpu);
  535. mmu = vcpu->arch.hw_mmu;
  536. last_ran = this_cpu_ptr(mmu->last_vcpu_ran);
  537. /*
  538. * Ensure a VMID is allocated for the MMU before programming VTTBR_EL2,
  539. * which happens eagerly in VHE.
  540. *
  541. * Also, the VMID allocator only preserves VMIDs that are active at the
  542. * time of rollover, so KVM might need to grab a new VMID for the MMU if
  543. * this is called from kvm_sched_in().
  544. */
  545. kvm_arm_vmid_update(&mmu->vmid);
  546. /*
  547. * We guarantee that both TLBs and I-cache are private to each
  548. * vcpu. If detecting that a vcpu from the same VM has
  549. * previously run on the same physical CPU, call into the
  550. * hypervisor code to nuke the relevant contexts.
  551. *
  552. * We might get preempted before the vCPU actually runs, but
  553. * over-invalidation doesn't affect correctness.
  554. */
  555. if (*last_ran != vcpu->vcpu_idx) {
  556. kvm_call_hyp(__kvm_flush_cpu_context, mmu);
  557. *last_ran = vcpu->vcpu_idx;
  558. }
  559. nommu:
  560. vcpu->cpu = cpu;
  561. /*
  562. * The timer must be loaded before the vgic to correctly set up physical
  563. * interrupt deactivation in nested state (e.g. timer interrupt).
  564. */
  565. kvm_timer_vcpu_load(vcpu);
  566. kvm_vgic_load(vcpu);
  567. kvm_vcpu_load_debug(vcpu);
  568. kvm_vcpu_load_fgt(vcpu);
  569. if (has_vhe())
  570. kvm_vcpu_load_vhe(vcpu);
  571. kvm_arch_vcpu_load_fp(vcpu);
  572. kvm_vcpu_pmu_restore_guest(vcpu);
  573. if (kvm_arm_is_pvtime_enabled(&vcpu->arch))
  574. kvm_make_request(KVM_REQ_RECORD_STEAL, vcpu);
  575. if (kvm_vcpu_should_clear_twe(vcpu))
  576. vcpu->arch.hcr_el2 &= ~HCR_TWE;
  577. else
  578. vcpu->arch.hcr_el2 |= HCR_TWE;
  579. if (kvm_vcpu_should_clear_twi(vcpu))
  580. vcpu->arch.hcr_el2 &= ~HCR_TWI;
  581. else
  582. vcpu->arch.hcr_el2 |= HCR_TWI;
  583. vcpu_set_pauth_traps(vcpu);
  584. if (is_protected_kvm_enabled()) {
  585. kvm_call_hyp_nvhe(__pkvm_vcpu_load,
  586. vcpu->kvm->arch.pkvm.handle,
  587. vcpu->vcpu_idx, vcpu->arch.hcr_el2);
  588. kvm_call_hyp(__vgic_v3_restore_vmcr_aprs,
  589. &vcpu->arch.vgic_cpu.vgic_v3);
  590. }
  591. if (!cpumask_test_cpu(cpu, vcpu->kvm->arch.supported_cpus))
  592. vcpu_set_on_unsupported_cpu(vcpu);
  593. }
  594. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
  595. {
  596. if (is_protected_kvm_enabled()) {
  597. kvm_call_hyp(__vgic_v3_save_aprs, &vcpu->arch.vgic_cpu.vgic_v3);
  598. kvm_call_hyp_nvhe(__pkvm_vcpu_put);
  599. }
  600. kvm_vcpu_put_debug(vcpu);
  601. kvm_arch_vcpu_put_fp(vcpu);
  602. if (has_vhe())
  603. kvm_vcpu_put_vhe(vcpu);
  604. kvm_timer_vcpu_put(vcpu);
  605. kvm_vgic_put(vcpu);
  606. kvm_vcpu_pmu_restore_host(vcpu);
  607. if (vcpu_has_nv(vcpu))
  608. kvm_vcpu_put_hw_mmu(vcpu);
  609. kvm_arm_vmid_clear_active();
  610. vcpu_clear_on_unsupported_cpu(vcpu);
  611. vcpu->cpu = -1;
  612. }
  613. static void __kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu)
  614. {
  615. WRITE_ONCE(vcpu->arch.mp_state.mp_state, KVM_MP_STATE_STOPPED);
  616. kvm_make_request(KVM_REQ_SLEEP, vcpu);
  617. kvm_vcpu_kick(vcpu);
  618. }
  619. void kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu)
  620. {
  621. spin_lock(&vcpu->arch.mp_state_lock);
  622. __kvm_arm_vcpu_power_off(vcpu);
  623. spin_unlock(&vcpu->arch.mp_state_lock);
  624. }
  625. bool kvm_arm_vcpu_stopped(struct kvm_vcpu *vcpu)
  626. {
  627. return READ_ONCE(vcpu->arch.mp_state.mp_state) == KVM_MP_STATE_STOPPED;
  628. }
  629. static void kvm_arm_vcpu_suspend(struct kvm_vcpu *vcpu)
  630. {
  631. WRITE_ONCE(vcpu->arch.mp_state.mp_state, KVM_MP_STATE_SUSPENDED);
  632. kvm_make_request(KVM_REQ_SUSPEND, vcpu);
  633. kvm_vcpu_kick(vcpu);
  634. }
  635. static bool kvm_arm_vcpu_suspended(struct kvm_vcpu *vcpu)
  636. {
  637. return READ_ONCE(vcpu->arch.mp_state.mp_state) == KVM_MP_STATE_SUSPENDED;
  638. }
  639. int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
  640. struct kvm_mp_state *mp_state)
  641. {
  642. *mp_state = READ_ONCE(vcpu->arch.mp_state);
  643. return 0;
  644. }
  645. int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
  646. struct kvm_mp_state *mp_state)
  647. {
  648. int ret = 0;
  649. spin_lock(&vcpu->arch.mp_state_lock);
  650. switch (mp_state->mp_state) {
  651. case KVM_MP_STATE_RUNNABLE:
  652. WRITE_ONCE(vcpu->arch.mp_state, *mp_state);
  653. break;
  654. case KVM_MP_STATE_STOPPED:
  655. __kvm_arm_vcpu_power_off(vcpu);
  656. break;
  657. case KVM_MP_STATE_SUSPENDED:
  658. kvm_arm_vcpu_suspend(vcpu);
  659. break;
  660. default:
  661. ret = -EINVAL;
  662. }
  663. spin_unlock(&vcpu->arch.mp_state_lock);
  664. return ret;
  665. }
  666. /**
  667. * kvm_arch_vcpu_runnable - determine if the vcpu can be scheduled
  668. * @v: The VCPU pointer
  669. *
  670. * If the guest CPU is not waiting for interrupts or an interrupt line is
  671. * asserted, the CPU is by definition runnable.
  672. */
  673. int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
  674. {
  675. bool irq_lines = *vcpu_hcr(v) & (HCR_VI | HCR_VF | HCR_VSE);
  676. return ((irq_lines || kvm_vgic_vcpu_pending_irq(v))
  677. && !kvm_arm_vcpu_stopped(v) && !v->arch.pause);
  678. }
  679. bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
  680. {
  681. return vcpu_mode_priv(vcpu);
  682. }
  683. #ifdef CONFIG_GUEST_PERF_EVENTS
  684. unsigned long kvm_arch_vcpu_get_ip(struct kvm_vcpu *vcpu)
  685. {
  686. return *vcpu_pc(vcpu);
  687. }
  688. #endif
  689. static void kvm_init_mpidr_data(struct kvm *kvm)
  690. {
  691. struct kvm_mpidr_data *data = NULL;
  692. unsigned long c, mask, nr_entries;
  693. u64 aff_set = 0, aff_clr = ~0UL;
  694. struct kvm_vcpu *vcpu;
  695. mutex_lock(&kvm->arch.config_lock);
  696. if (rcu_access_pointer(kvm->arch.mpidr_data) ||
  697. atomic_read(&kvm->online_vcpus) == 1)
  698. goto out;
  699. kvm_for_each_vcpu(c, vcpu, kvm) {
  700. u64 aff = kvm_vcpu_get_mpidr_aff(vcpu);
  701. aff_set |= aff;
  702. aff_clr &= aff;
  703. }
  704. /*
  705. * A significant bit can be either 0 or 1, and will only appear in
  706. * aff_set. Use aff_clr to weed out the useless stuff.
  707. */
  708. mask = aff_set ^ aff_clr;
  709. nr_entries = BIT_ULL(hweight_long(mask));
  710. /*
  711. * Don't let userspace fool us. If we need more than a single page
  712. * to describe the compressed MPIDR array, just fall back to the
  713. * iterative method. Single vcpu VMs do not need this either.
  714. */
  715. if (struct_size(data, cmpidr_to_idx, nr_entries) <= PAGE_SIZE)
  716. data = kzalloc_flex(*data, cmpidr_to_idx, nr_entries,
  717. GFP_KERNEL_ACCOUNT);
  718. if (!data)
  719. goto out;
  720. data->mpidr_mask = mask;
  721. kvm_for_each_vcpu(c, vcpu, kvm) {
  722. u64 aff = kvm_vcpu_get_mpidr_aff(vcpu);
  723. u16 index = kvm_mpidr_index(data, aff);
  724. data->cmpidr_to_idx[index] = c;
  725. }
  726. rcu_assign_pointer(kvm->arch.mpidr_data, data);
  727. out:
  728. mutex_unlock(&kvm->arch.config_lock);
  729. }
  730. /*
  731. * Handle both the initialisation that is being done when the vcpu is
  732. * run for the first time, as well as the updates that must be
  733. * performed each time we get a new thread dealing with this vcpu.
  734. */
  735. int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu)
  736. {
  737. struct kvm *kvm = vcpu->kvm;
  738. int ret;
  739. if (!kvm_vcpu_initialized(vcpu))
  740. return -ENOEXEC;
  741. if (!kvm_arm_vcpu_is_finalized(vcpu))
  742. return -EPERM;
  743. if (likely(vcpu_has_run_once(vcpu)))
  744. return 0;
  745. kvm_init_mpidr_data(kvm);
  746. if (likely(irqchip_in_kernel(kvm))) {
  747. /*
  748. * Map the VGIC hardware resources before running a vcpu the
  749. * first time on this VM.
  750. */
  751. ret = kvm_vgic_map_resources(kvm);
  752. if (ret)
  753. return ret;
  754. }
  755. ret = kvm_finalize_sys_regs(vcpu);
  756. if (ret)
  757. return ret;
  758. if (vcpu_has_nv(vcpu)) {
  759. ret = kvm_vcpu_allocate_vncr_tlb(vcpu);
  760. if (ret)
  761. return ret;
  762. ret = kvm_vgic_vcpu_nv_init(vcpu);
  763. if (ret)
  764. return ret;
  765. }
  766. /*
  767. * This needs to happen after any restriction has been applied
  768. * to the feature set.
  769. */
  770. kvm_calculate_traps(vcpu);
  771. ret = kvm_timer_enable(vcpu);
  772. if (ret)
  773. return ret;
  774. if (kvm_vcpu_has_pmu(vcpu)) {
  775. ret = kvm_arm_pmu_v3_enable(vcpu);
  776. if (ret)
  777. return ret;
  778. }
  779. if (is_protected_kvm_enabled()) {
  780. ret = pkvm_create_hyp_vm(kvm);
  781. if (ret)
  782. return ret;
  783. ret = pkvm_create_hyp_vcpu(vcpu);
  784. if (ret)
  785. return ret;
  786. }
  787. mutex_lock(&kvm->arch.config_lock);
  788. set_bit(KVM_ARCH_FLAG_HAS_RAN_ONCE, &kvm->arch.flags);
  789. mutex_unlock(&kvm->arch.config_lock);
  790. return ret;
  791. }
  792. bool kvm_arch_intc_initialized(struct kvm *kvm)
  793. {
  794. return vgic_initialized(kvm);
  795. }
  796. void kvm_arm_halt_guest(struct kvm *kvm)
  797. {
  798. unsigned long i;
  799. struct kvm_vcpu *vcpu;
  800. kvm_for_each_vcpu(i, vcpu, kvm)
  801. vcpu->arch.pause = true;
  802. kvm_make_all_cpus_request(kvm, KVM_REQ_SLEEP);
  803. }
  804. void kvm_arm_resume_guest(struct kvm *kvm)
  805. {
  806. unsigned long i;
  807. struct kvm_vcpu *vcpu;
  808. kvm_for_each_vcpu(i, vcpu, kvm) {
  809. vcpu->arch.pause = false;
  810. __kvm_vcpu_wake_up(vcpu);
  811. }
  812. }
  813. static void kvm_vcpu_sleep(struct kvm_vcpu *vcpu)
  814. {
  815. struct rcuwait *wait = kvm_arch_vcpu_get_wait(vcpu);
  816. rcuwait_wait_event(wait,
  817. (!kvm_arm_vcpu_stopped(vcpu)) && (!vcpu->arch.pause),
  818. TASK_INTERRUPTIBLE);
  819. if (kvm_arm_vcpu_stopped(vcpu) || vcpu->arch.pause) {
  820. /* Awaken to handle a signal, request we sleep again later. */
  821. kvm_make_request(KVM_REQ_SLEEP, vcpu);
  822. }
  823. /*
  824. * Make sure we will observe a potential reset request if we've
  825. * observed a change to the power state. Pairs with the smp_wmb() in
  826. * kvm_psci_vcpu_on().
  827. */
  828. smp_rmb();
  829. }
  830. /**
  831. * kvm_vcpu_wfi - emulate Wait-For-Interrupt behavior
  832. * @vcpu: The VCPU pointer
  833. *
  834. * Suspend execution of a vCPU until a valid wake event is detected, i.e. until
  835. * the vCPU is runnable. The vCPU may or may not be scheduled out, depending
  836. * on when a wake event arrives, e.g. there may already be a pending wake event.
  837. */
  838. void kvm_vcpu_wfi(struct kvm_vcpu *vcpu)
  839. {
  840. /*
  841. * Sync back the state of the GIC CPU interface so that we have
  842. * the latest PMR and group enables. This ensures that
  843. * kvm_arch_vcpu_runnable has up-to-date data to decide whether
  844. * we have pending interrupts, e.g. when determining if the
  845. * vCPU should block.
  846. *
  847. * For the same reason, we want to tell GICv4 that we need
  848. * doorbells to be signalled, should an interrupt become pending.
  849. */
  850. preempt_disable();
  851. vcpu_set_flag(vcpu, IN_WFI);
  852. kvm_vgic_put(vcpu);
  853. preempt_enable();
  854. kvm_vcpu_halt(vcpu);
  855. vcpu_clear_flag(vcpu, IN_WFIT);
  856. preempt_disable();
  857. vcpu_clear_flag(vcpu, IN_WFI);
  858. kvm_vgic_load(vcpu);
  859. preempt_enable();
  860. }
  861. static int kvm_vcpu_suspend(struct kvm_vcpu *vcpu)
  862. {
  863. if (!kvm_arm_vcpu_suspended(vcpu))
  864. return 1;
  865. kvm_vcpu_wfi(vcpu);
  866. /*
  867. * The suspend state is sticky; we do not leave it until userspace
  868. * explicitly marks the vCPU as runnable. Request that we suspend again
  869. * later.
  870. */
  871. kvm_make_request(KVM_REQ_SUSPEND, vcpu);
  872. /*
  873. * Check to make sure the vCPU is actually runnable. If so, exit to
  874. * userspace informing it of the wakeup condition.
  875. */
  876. if (kvm_arch_vcpu_runnable(vcpu)) {
  877. memset(&vcpu->run->system_event, 0, sizeof(vcpu->run->system_event));
  878. vcpu->run->system_event.type = KVM_SYSTEM_EVENT_WAKEUP;
  879. vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
  880. return 0;
  881. }
  882. /*
  883. * Otherwise, we were unblocked to process a different event, such as a
  884. * pending signal. Return 1 and allow kvm_arch_vcpu_ioctl_run() to
  885. * process the event.
  886. */
  887. return 1;
  888. }
  889. /**
  890. * check_vcpu_requests - check and handle pending vCPU requests
  891. * @vcpu: the VCPU pointer
  892. *
  893. * Return: 1 if we should enter the guest
  894. * 0 if we should exit to userspace
  895. * < 0 if we should exit to userspace, where the return value indicates
  896. * an error
  897. */
  898. static int check_vcpu_requests(struct kvm_vcpu *vcpu)
  899. {
  900. if (kvm_request_pending(vcpu)) {
  901. if (kvm_check_request(KVM_REQ_VM_DEAD, vcpu))
  902. return -EIO;
  903. if (kvm_check_request(KVM_REQ_SLEEP, vcpu))
  904. kvm_vcpu_sleep(vcpu);
  905. if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu))
  906. kvm_reset_vcpu(vcpu);
  907. /*
  908. * Clear IRQ_PENDING requests that were made to guarantee
  909. * that a VCPU sees new virtual interrupts.
  910. */
  911. kvm_check_request(KVM_REQ_IRQ_PENDING, vcpu);
  912. /* Process interrupts deactivated through a trap */
  913. if (kvm_check_request(KVM_REQ_VGIC_PROCESS_UPDATE, vcpu))
  914. kvm_vgic_process_async_update(vcpu);
  915. if (kvm_check_request(KVM_REQ_RECORD_STEAL, vcpu))
  916. kvm_update_stolen_time(vcpu);
  917. if (kvm_check_request(KVM_REQ_RELOAD_GICv4, vcpu)) {
  918. /* The distributor enable bits were changed */
  919. preempt_disable();
  920. vgic_v4_put(vcpu);
  921. vgic_v4_load(vcpu);
  922. preempt_enable();
  923. }
  924. if (kvm_check_request(KVM_REQ_RELOAD_PMU, vcpu))
  925. kvm_vcpu_reload_pmu(vcpu);
  926. if (kvm_check_request(KVM_REQ_RESYNC_PMU_EL0, vcpu))
  927. kvm_vcpu_pmu_restore_guest(vcpu);
  928. if (kvm_check_request(KVM_REQ_SUSPEND, vcpu))
  929. return kvm_vcpu_suspend(vcpu);
  930. if (kvm_dirty_ring_check_request(vcpu))
  931. return 0;
  932. check_nested_vcpu_requests(vcpu);
  933. }
  934. return 1;
  935. }
  936. static bool vcpu_mode_is_bad_32bit(struct kvm_vcpu *vcpu)
  937. {
  938. if (likely(!vcpu_mode_is_32bit(vcpu)))
  939. return false;
  940. if (vcpu_has_nv(vcpu))
  941. return true;
  942. return !kvm_supports_32bit_el0();
  943. }
  944. /**
  945. * kvm_vcpu_exit_request - returns true if the VCPU should *not* enter the guest
  946. * @vcpu: The VCPU pointer
  947. * @ret: Pointer to write optional return code
  948. *
  949. * Returns: true if the VCPU needs to return to a preemptible + interruptible
  950. * and skip guest entry.
  951. *
  952. * This function disambiguates between two different types of exits: exits to a
  953. * preemptible + interruptible kernel context and exits to userspace. For an
  954. * exit to userspace, this function will write the return code to ret and return
  955. * true. For an exit to preemptible + interruptible kernel context (i.e. check
  956. * for pending work and re-enter), return true without writing to ret.
  957. */
  958. static bool kvm_vcpu_exit_request(struct kvm_vcpu *vcpu, int *ret)
  959. {
  960. struct kvm_run *run = vcpu->run;
  961. /*
  962. * If we're using a userspace irqchip, then check if we need
  963. * to tell a userspace irqchip about timer or PMU level
  964. * changes and if so, exit to userspace (the actual level
  965. * state gets updated in kvm_timer_update_run and
  966. * kvm_pmu_update_run below).
  967. */
  968. if (unlikely(!irqchip_in_kernel(vcpu->kvm))) {
  969. if (kvm_timer_should_notify_user(vcpu) ||
  970. kvm_pmu_should_notify_user(vcpu)) {
  971. *ret = -EINTR;
  972. run->exit_reason = KVM_EXIT_INTR;
  973. return true;
  974. }
  975. }
  976. if (unlikely(vcpu_on_unsupported_cpu(vcpu))) {
  977. run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  978. run->fail_entry.hardware_entry_failure_reason = KVM_EXIT_FAIL_ENTRY_CPU_UNSUPPORTED;
  979. run->fail_entry.cpu = smp_processor_id();
  980. *ret = 0;
  981. return true;
  982. }
  983. return kvm_request_pending(vcpu) ||
  984. xfer_to_guest_mode_work_pending();
  985. }
  986. /*
  987. * Actually run the vCPU, entering an RCU extended quiescent state (EQS) while
  988. * the vCPU is running.
  989. *
  990. * This must be noinstr as instrumentation may make use of RCU, and this is not
  991. * safe during the EQS.
  992. */
  993. static int noinstr kvm_arm_vcpu_enter_exit(struct kvm_vcpu *vcpu)
  994. {
  995. int ret;
  996. guest_state_enter_irqoff();
  997. ret = kvm_call_hyp_ret(__kvm_vcpu_run, vcpu);
  998. guest_state_exit_irqoff();
  999. return ret;
  1000. }
  1001. /**
  1002. * kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code
  1003. * @vcpu: The VCPU pointer
  1004. *
  1005. * This function is called through the VCPU_RUN ioctl called from user space. It
  1006. * will execute VM code in a loop until the time slice for the process is used
  1007. * or some emulation is needed from user space in which case the function will
  1008. * return with return value 0 and with the kvm_run structure filled in with the
  1009. * required data for the requested emulation.
  1010. */
  1011. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
  1012. {
  1013. struct kvm_run *run = vcpu->run;
  1014. int ret;
  1015. if (run->exit_reason == KVM_EXIT_MMIO) {
  1016. ret = kvm_handle_mmio_return(vcpu);
  1017. if (ret <= 0)
  1018. return ret;
  1019. }
  1020. vcpu_load(vcpu);
  1021. if (!vcpu->wants_to_run) {
  1022. ret = -EINTR;
  1023. goto out;
  1024. }
  1025. kvm_sigset_activate(vcpu);
  1026. ret = 1;
  1027. run->exit_reason = KVM_EXIT_UNKNOWN;
  1028. run->flags = 0;
  1029. while (ret > 0) {
  1030. /*
  1031. * Check conditions before entering the guest
  1032. */
  1033. ret = kvm_xfer_to_guest_mode_handle_work(vcpu);
  1034. if (!ret)
  1035. ret = 1;
  1036. if (ret > 0)
  1037. ret = check_vcpu_requests(vcpu);
  1038. /*
  1039. * Preparing the interrupts to be injected also
  1040. * involves poking the GIC, which must be done in a
  1041. * non-preemptible context.
  1042. */
  1043. preempt_disable();
  1044. kvm_nested_flush_hwstate(vcpu);
  1045. if (kvm_vcpu_has_pmu(vcpu))
  1046. kvm_pmu_flush_hwstate(vcpu);
  1047. local_irq_disable();
  1048. kvm_vgic_flush_hwstate(vcpu);
  1049. kvm_pmu_update_vcpu_events(vcpu);
  1050. /*
  1051. * Ensure we set mode to IN_GUEST_MODE after we disable
  1052. * interrupts and before the final VCPU requests check.
  1053. * See the comment in kvm_vcpu_exiting_guest_mode() and
  1054. * Documentation/virt/kvm/vcpu-requests.rst
  1055. */
  1056. smp_store_mb(vcpu->mode, IN_GUEST_MODE);
  1057. if (ret <= 0 || kvm_vcpu_exit_request(vcpu, &ret)) {
  1058. vcpu->mode = OUTSIDE_GUEST_MODE;
  1059. isb(); /* Ensure work in x_flush_hwstate is committed */
  1060. if (kvm_vcpu_has_pmu(vcpu))
  1061. kvm_pmu_sync_hwstate(vcpu);
  1062. if (unlikely(!irqchip_in_kernel(vcpu->kvm)))
  1063. kvm_timer_sync_user(vcpu);
  1064. kvm_vgic_sync_hwstate(vcpu);
  1065. local_irq_enable();
  1066. preempt_enable();
  1067. continue;
  1068. }
  1069. kvm_arch_vcpu_ctxflush_fp(vcpu);
  1070. /**************************************************************
  1071. * Enter the guest
  1072. */
  1073. trace_kvm_entry(*vcpu_pc(vcpu));
  1074. guest_timing_enter_irqoff();
  1075. ret = kvm_arm_vcpu_enter_exit(vcpu);
  1076. vcpu->mode = OUTSIDE_GUEST_MODE;
  1077. vcpu->stat.exits++;
  1078. /*
  1079. * Back from guest
  1080. *************************************************************/
  1081. /*
  1082. * We must sync the PMU state before the vgic state so
  1083. * that the vgic can properly sample the updated state of the
  1084. * interrupt line.
  1085. */
  1086. if (kvm_vcpu_has_pmu(vcpu))
  1087. kvm_pmu_sync_hwstate(vcpu);
  1088. /*
  1089. * Sync the vgic state before syncing the timer state because
  1090. * the timer code needs to know if the virtual timer
  1091. * interrupts are active.
  1092. */
  1093. kvm_vgic_sync_hwstate(vcpu);
  1094. /*
  1095. * Sync the timer hardware state before enabling interrupts as
  1096. * we don't want vtimer interrupts to race with syncing the
  1097. * timer virtual interrupt state.
  1098. */
  1099. if (unlikely(!irqchip_in_kernel(vcpu->kvm)))
  1100. kvm_timer_sync_user(vcpu);
  1101. if (is_hyp_ctxt(vcpu))
  1102. kvm_timer_sync_nested(vcpu);
  1103. kvm_arch_vcpu_ctxsync_fp(vcpu);
  1104. /*
  1105. * We must ensure that any pending interrupts are taken before
  1106. * we exit guest timing so that timer ticks are accounted as
  1107. * guest time. Transiently unmask interrupts so that any
  1108. * pending interrupts are taken.
  1109. *
  1110. * Per ARM DDI 0487G.b section D1.13.4, an ISB (or other
  1111. * context synchronization event) is necessary to ensure that
  1112. * pending interrupts are taken.
  1113. */
  1114. if (ARM_EXCEPTION_CODE(ret) == ARM_EXCEPTION_IRQ) {
  1115. local_irq_enable();
  1116. isb();
  1117. local_irq_disable();
  1118. }
  1119. guest_timing_exit_irqoff();
  1120. local_irq_enable();
  1121. trace_kvm_exit(ret, kvm_vcpu_trap_get_class(vcpu), *vcpu_pc(vcpu));
  1122. /* Exit types that need handling before we can be preempted */
  1123. handle_exit_early(vcpu, ret);
  1124. kvm_nested_sync_hwstate(vcpu);
  1125. preempt_enable();
  1126. /*
  1127. * The ARMv8 architecture doesn't give the hypervisor
  1128. * a mechanism to prevent a guest from dropping to AArch32 EL0
  1129. * if implemented by the CPU. If we spot the guest in such
  1130. * state and that we decided it wasn't supposed to do so (like
  1131. * with the asymmetric AArch32 case), return to userspace with
  1132. * a fatal error.
  1133. */
  1134. if (vcpu_mode_is_bad_32bit(vcpu)) {
  1135. /*
  1136. * As we have caught the guest red-handed, decide that
  1137. * it isn't fit for purpose anymore by making the vcpu
  1138. * invalid. The VMM can try and fix it by issuing a
  1139. * KVM_ARM_VCPU_INIT if it really wants to.
  1140. */
  1141. vcpu_clear_flag(vcpu, VCPU_INITIALIZED);
  1142. ret = ARM_EXCEPTION_IL;
  1143. }
  1144. ret = handle_exit(vcpu, ret);
  1145. }
  1146. /* Tell userspace about in-kernel device output levels */
  1147. if (unlikely(!irqchip_in_kernel(vcpu->kvm))) {
  1148. kvm_timer_update_run(vcpu);
  1149. kvm_pmu_update_run(vcpu);
  1150. }
  1151. kvm_sigset_deactivate(vcpu);
  1152. out:
  1153. /*
  1154. * In the unlikely event that we are returning to userspace
  1155. * with pending exceptions or PC adjustment, commit these
  1156. * adjustments in order to give userspace a consistent view of
  1157. * the vcpu state. Note that this relies on __kvm_adjust_pc()
  1158. * being preempt-safe on VHE.
  1159. */
  1160. if (unlikely(vcpu_get_flag(vcpu, PENDING_EXCEPTION) ||
  1161. vcpu_get_flag(vcpu, INCREMENT_PC)))
  1162. kvm_call_hyp(__kvm_adjust_pc, vcpu);
  1163. vcpu_put(vcpu);
  1164. return ret;
  1165. }
  1166. static int vcpu_interrupt_line(struct kvm_vcpu *vcpu, int number, bool level)
  1167. {
  1168. int bit_index;
  1169. bool set;
  1170. unsigned long *hcr;
  1171. if (number == KVM_ARM_IRQ_CPU_IRQ)
  1172. bit_index = __ffs(HCR_VI);
  1173. else /* KVM_ARM_IRQ_CPU_FIQ */
  1174. bit_index = __ffs(HCR_VF);
  1175. hcr = vcpu_hcr(vcpu);
  1176. if (level)
  1177. set = test_and_set_bit(bit_index, hcr);
  1178. else
  1179. set = test_and_clear_bit(bit_index, hcr);
  1180. /*
  1181. * If we didn't change anything, no need to wake up or kick other CPUs
  1182. */
  1183. if (set == level)
  1184. return 0;
  1185. /*
  1186. * The vcpu irq_lines field was updated, wake up sleeping VCPUs and
  1187. * trigger a world-switch round on the running physical CPU to set the
  1188. * virtual IRQ/FIQ fields in the HCR appropriately.
  1189. */
  1190. kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
  1191. kvm_vcpu_kick(vcpu);
  1192. return 0;
  1193. }
  1194. int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
  1195. bool line_status)
  1196. {
  1197. u32 irq = irq_level->irq;
  1198. unsigned int irq_type, vcpu_id, irq_num;
  1199. struct kvm_vcpu *vcpu = NULL;
  1200. bool level = irq_level->level;
  1201. irq_type = (irq >> KVM_ARM_IRQ_TYPE_SHIFT) & KVM_ARM_IRQ_TYPE_MASK;
  1202. vcpu_id = (irq >> KVM_ARM_IRQ_VCPU_SHIFT) & KVM_ARM_IRQ_VCPU_MASK;
  1203. vcpu_id += ((irq >> KVM_ARM_IRQ_VCPU2_SHIFT) & KVM_ARM_IRQ_VCPU2_MASK) * (KVM_ARM_IRQ_VCPU_MASK + 1);
  1204. irq_num = (irq >> KVM_ARM_IRQ_NUM_SHIFT) & KVM_ARM_IRQ_NUM_MASK;
  1205. trace_kvm_irq_line(irq_type, vcpu_id, irq_num, irq_level->level);
  1206. switch (irq_type) {
  1207. case KVM_ARM_IRQ_TYPE_CPU:
  1208. if (irqchip_in_kernel(kvm))
  1209. return -ENXIO;
  1210. vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id);
  1211. if (!vcpu)
  1212. return -EINVAL;
  1213. if (irq_num > KVM_ARM_IRQ_CPU_FIQ)
  1214. return -EINVAL;
  1215. return vcpu_interrupt_line(vcpu, irq_num, level);
  1216. case KVM_ARM_IRQ_TYPE_PPI:
  1217. if (!irqchip_in_kernel(kvm))
  1218. return -ENXIO;
  1219. vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id);
  1220. if (!vcpu)
  1221. return -EINVAL;
  1222. if (irq_num < VGIC_NR_SGIS || irq_num >= VGIC_NR_PRIVATE_IRQS)
  1223. return -EINVAL;
  1224. return kvm_vgic_inject_irq(kvm, vcpu, irq_num, level, NULL);
  1225. case KVM_ARM_IRQ_TYPE_SPI:
  1226. if (!irqchip_in_kernel(kvm))
  1227. return -ENXIO;
  1228. if (irq_num < VGIC_NR_PRIVATE_IRQS)
  1229. return -EINVAL;
  1230. return kvm_vgic_inject_irq(kvm, NULL, irq_num, level, NULL);
  1231. }
  1232. return -EINVAL;
  1233. }
  1234. static unsigned long system_supported_vcpu_features(void)
  1235. {
  1236. unsigned long features = KVM_VCPU_VALID_FEATURES;
  1237. if (!cpus_have_final_cap(ARM64_HAS_32BIT_EL1))
  1238. clear_bit(KVM_ARM_VCPU_EL1_32BIT, &features);
  1239. if (!kvm_supports_guest_pmuv3())
  1240. clear_bit(KVM_ARM_VCPU_PMU_V3, &features);
  1241. if (!system_supports_sve())
  1242. clear_bit(KVM_ARM_VCPU_SVE, &features);
  1243. if (!kvm_has_full_ptr_auth()) {
  1244. clear_bit(KVM_ARM_VCPU_PTRAUTH_ADDRESS, &features);
  1245. clear_bit(KVM_ARM_VCPU_PTRAUTH_GENERIC, &features);
  1246. }
  1247. if (!cpus_have_final_cap(ARM64_HAS_NESTED_VIRT))
  1248. clear_bit(KVM_ARM_VCPU_HAS_EL2, &features);
  1249. return features;
  1250. }
  1251. static int kvm_vcpu_init_check_features(struct kvm_vcpu *vcpu,
  1252. const struct kvm_vcpu_init *init)
  1253. {
  1254. unsigned long features = init->features[0];
  1255. int i;
  1256. if (features & ~KVM_VCPU_VALID_FEATURES)
  1257. return -ENOENT;
  1258. for (i = 1; i < ARRAY_SIZE(init->features); i++) {
  1259. if (init->features[i])
  1260. return -ENOENT;
  1261. }
  1262. if (features & ~system_supported_vcpu_features())
  1263. return -EINVAL;
  1264. /*
  1265. * For now make sure that both address/generic pointer authentication
  1266. * features are requested by the userspace together.
  1267. */
  1268. if (test_bit(KVM_ARM_VCPU_PTRAUTH_ADDRESS, &features) !=
  1269. test_bit(KVM_ARM_VCPU_PTRAUTH_GENERIC, &features))
  1270. return -EINVAL;
  1271. if (!test_bit(KVM_ARM_VCPU_EL1_32BIT, &features))
  1272. return 0;
  1273. /* MTE is incompatible with AArch32 */
  1274. if (kvm_has_mte(vcpu->kvm))
  1275. return -EINVAL;
  1276. /* NV is incompatible with AArch32 */
  1277. if (test_bit(KVM_ARM_VCPU_HAS_EL2, &features))
  1278. return -EINVAL;
  1279. return 0;
  1280. }
  1281. static bool kvm_vcpu_init_changed(struct kvm_vcpu *vcpu,
  1282. const struct kvm_vcpu_init *init)
  1283. {
  1284. unsigned long features = init->features[0];
  1285. return !bitmap_equal(vcpu->kvm->arch.vcpu_features, &features,
  1286. KVM_VCPU_MAX_FEATURES);
  1287. }
  1288. static int kvm_setup_vcpu(struct kvm_vcpu *vcpu)
  1289. {
  1290. struct kvm *kvm = vcpu->kvm;
  1291. int ret = 0;
  1292. /*
  1293. * When the vCPU has a PMU, but no PMU is set for the guest
  1294. * yet, set the default one.
  1295. */
  1296. if (kvm_vcpu_has_pmu(vcpu) && !kvm->arch.arm_pmu)
  1297. ret = kvm_arm_set_default_pmu(kvm);
  1298. /* Prepare for nested if required */
  1299. if (!ret && vcpu_has_nv(vcpu))
  1300. ret = kvm_vcpu_init_nested(vcpu);
  1301. return ret;
  1302. }
  1303. static int __kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
  1304. const struct kvm_vcpu_init *init)
  1305. {
  1306. unsigned long features = init->features[0];
  1307. struct kvm *kvm = vcpu->kvm;
  1308. int ret = -EINVAL;
  1309. mutex_lock(&kvm->arch.config_lock);
  1310. if (test_bit(KVM_ARCH_FLAG_VCPU_FEATURES_CONFIGURED, &kvm->arch.flags) &&
  1311. kvm_vcpu_init_changed(vcpu, init))
  1312. goto out_unlock;
  1313. bitmap_copy(kvm->arch.vcpu_features, &features, KVM_VCPU_MAX_FEATURES);
  1314. ret = kvm_setup_vcpu(vcpu);
  1315. if (ret)
  1316. goto out_unlock;
  1317. /* Now we know what it is, we can reset it. */
  1318. kvm_reset_vcpu(vcpu);
  1319. set_bit(KVM_ARCH_FLAG_VCPU_FEATURES_CONFIGURED, &kvm->arch.flags);
  1320. vcpu_set_flag(vcpu, VCPU_INITIALIZED);
  1321. ret = 0;
  1322. out_unlock:
  1323. mutex_unlock(&kvm->arch.config_lock);
  1324. return ret;
  1325. }
  1326. static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
  1327. const struct kvm_vcpu_init *init)
  1328. {
  1329. int ret;
  1330. if (init->target != KVM_ARM_TARGET_GENERIC_V8 &&
  1331. init->target != kvm_target_cpu())
  1332. return -EINVAL;
  1333. ret = kvm_vcpu_init_check_features(vcpu, init);
  1334. if (ret)
  1335. return ret;
  1336. if (!kvm_vcpu_initialized(vcpu))
  1337. return __kvm_vcpu_set_target(vcpu, init);
  1338. if (kvm_vcpu_init_changed(vcpu, init))
  1339. return -EINVAL;
  1340. kvm_reset_vcpu(vcpu);
  1341. return 0;
  1342. }
  1343. static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu,
  1344. struct kvm_vcpu_init *init)
  1345. {
  1346. bool power_off = false;
  1347. int ret;
  1348. /*
  1349. * Treat the power-off vCPU feature as ephemeral. Clear the bit to avoid
  1350. * reflecting it in the finalized feature set, thus limiting its scope
  1351. * to a single KVM_ARM_VCPU_INIT call.
  1352. */
  1353. if (init->features[0] & BIT(KVM_ARM_VCPU_POWER_OFF)) {
  1354. init->features[0] &= ~BIT(KVM_ARM_VCPU_POWER_OFF);
  1355. power_off = true;
  1356. }
  1357. ret = kvm_vcpu_set_target(vcpu, init);
  1358. if (ret)
  1359. return ret;
  1360. /*
  1361. * Ensure a rebooted VM will fault in RAM pages and detect if the
  1362. * guest MMU is turned off and flush the caches as needed.
  1363. *
  1364. * S2FWB enforces all memory accesses to RAM being cacheable,
  1365. * ensuring that the data side is always coherent. We still
  1366. * need to invalidate the I-cache though, as FWB does *not*
  1367. * imply CTR_EL0.DIC.
  1368. */
  1369. if (vcpu_has_run_once(vcpu)) {
  1370. if (!cpus_have_final_cap(ARM64_HAS_STAGE2_FWB))
  1371. stage2_unmap_vm(vcpu->kvm);
  1372. else
  1373. icache_inval_all_pou();
  1374. }
  1375. vcpu_reset_hcr(vcpu);
  1376. /*
  1377. * Handle the "start in power-off" case.
  1378. */
  1379. spin_lock(&vcpu->arch.mp_state_lock);
  1380. if (power_off)
  1381. __kvm_arm_vcpu_power_off(vcpu);
  1382. else
  1383. WRITE_ONCE(vcpu->arch.mp_state.mp_state, KVM_MP_STATE_RUNNABLE);
  1384. spin_unlock(&vcpu->arch.mp_state_lock);
  1385. return 0;
  1386. }
  1387. static int kvm_arm_vcpu_set_attr(struct kvm_vcpu *vcpu,
  1388. struct kvm_device_attr *attr)
  1389. {
  1390. int ret = -ENXIO;
  1391. switch (attr->group) {
  1392. default:
  1393. ret = kvm_arm_vcpu_arch_set_attr(vcpu, attr);
  1394. break;
  1395. }
  1396. return ret;
  1397. }
  1398. static int kvm_arm_vcpu_get_attr(struct kvm_vcpu *vcpu,
  1399. struct kvm_device_attr *attr)
  1400. {
  1401. int ret = -ENXIO;
  1402. switch (attr->group) {
  1403. default:
  1404. ret = kvm_arm_vcpu_arch_get_attr(vcpu, attr);
  1405. break;
  1406. }
  1407. return ret;
  1408. }
  1409. static int kvm_arm_vcpu_has_attr(struct kvm_vcpu *vcpu,
  1410. struct kvm_device_attr *attr)
  1411. {
  1412. int ret = -ENXIO;
  1413. switch (attr->group) {
  1414. default:
  1415. ret = kvm_arm_vcpu_arch_has_attr(vcpu, attr);
  1416. break;
  1417. }
  1418. return ret;
  1419. }
  1420. static int kvm_arm_vcpu_get_events(struct kvm_vcpu *vcpu,
  1421. struct kvm_vcpu_events *events)
  1422. {
  1423. memset(events, 0, sizeof(*events));
  1424. return __kvm_arm_vcpu_get_events(vcpu, events);
  1425. }
  1426. static int kvm_arm_vcpu_set_events(struct kvm_vcpu *vcpu,
  1427. struct kvm_vcpu_events *events)
  1428. {
  1429. int i;
  1430. /* check whether the reserved field is zero */
  1431. for (i = 0; i < ARRAY_SIZE(events->reserved); i++)
  1432. if (events->reserved[i])
  1433. return -EINVAL;
  1434. /* check whether the pad field is zero */
  1435. for (i = 0; i < ARRAY_SIZE(events->exception.pad); i++)
  1436. if (events->exception.pad[i])
  1437. return -EINVAL;
  1438. return __kvm_arm_vcpu_set_events(vcpu, events);
  1439. }
  1440. long kvm_arch_vcpu_ioctl(struct file *filp,
  1441. unsigned int ioctl, unsigned long arg)
  1442. {
  1443. struct kvm_vcpu *vcpu = filp->private_data;
  1444. void __user *argp = (void __user *)arg;
  1445. struct kvm_device_attr attr;
  1446. long r;
  1447. switch (ioctl) {
  1448. case KVM_ARM_VCPU_INIT: {
  1449. struct kvm_vcpu_init init;
  1450. r = -EFAULT;
  1451. if (copy_from_user(&init, argp, sizeof(init)))
  1452. break;
  1453. r = kvm_arch_vcpu_ioctl_vcpu_init(vcpu, &init);
  1454. break;
  1455. }
  1456. case KVM_SET_ONE_REG:
  1457. case KVM_GET_ONE_REG: {
  1458. struct kvm_one_reg reg;
  1459. r = -ENOEXEC;
  1460. if (unlikely(!kvm_vcpu_initialized(vcpu)))
  1461. break;
  1462. r = -EFAULT;
  1463. if (copy_from_user(&reg, argp, sizeof(reg)))
  1464. break;
  1465. /*
  1466. * We could owe a reset due to PSCI. Handle the pending reset
  1467. * here to ensure userspace register accesses are ordered after
  1468. * the reset.
  1469. */
  1470. if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu))
  1471. kvm_reset_vcpu(vcpu);
  1472. if (ioctl == KVM_SET_ONE_REG)
  1473. r = kvm_arm_set_reg(vcpu, &reg);
  1474. else
  1475. r = kvm_arm_get_reg(vcpu, &reg);
  1476. break;
  1477. }
  1478. case KVM_GET_REG_LIST: {
  1479. struct kvm_reg_list __user *user_list = argp;
  1480. struct kvm_reg_list reg_list;
  1481. unsigned n;
  1482. r = -ENOEXEC;
  1483. if (unlikely(!kvm_vcpu_initialized(vcpu)))
  1484. break;
  1485. r = -EPERM;
  1486. if (!kvm_arm_vcpu_is_finalized(vcpu))
  1487. break;
  1488. r = -EFAULT;
  1489. if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
  1490. break;
  1491. n = reg_list.n;
  1492. reg_list.n = kvm_arm_num_regs(vcpu);
  1493. if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
  1494. break;
  1495. r = -E2BIG;
  1496. if (n < reg_list.n)
  1497. break;
  1498. r = kvm_arm_copy_reg_indices(vcpu, user_list->reg);
  1499. break;
  1500. }
  1501. case KVM_SET_DEVICE_ATTR: {
  1502. r = -EFAULT;
  1503. if (copy_from_user(&attr, argp, sizeof(attr)))
  1504. break;
  1505. r = kvm_arm_vcpu_set_attr(vcpu, &attr);
  1506. break;
  1507. }
  1508. case KVM_GET_DEVICE_ATTR: {
  1509. r = -EFAULT;
  1510. if (copy_from_user(&attr, argp, sizeof(attr)))
  1511. break;
  1512. r = kvm_arm_vcpu_get_attr(vcpu, &attr);
  1513. break;
  1514. }
  1515. case KVM_HAS_DEVICE_ATTR: {
  1516. r = -EFAULT;
  1517. if (copy_from_user(&attr, argp, sizeof(attr)))
  1518. break;
  1519. r = kvm_arm_vcpu_has_attr(vcpu, &attr);
  1520. break;
  1521. }
  1522. case KVM_GET_VCPU_EVENTS: {
  1523. struct kvm_vcpu_events events;
  1524. if (!kvm_vcpu_initialized(vcpu))
  1525. return -ENOEXEC;
  1526. if (kvm_arm_vcpu_get_events(vcpu, &events))
  1527. return -EINVAL;
  1528. if (copy_to_user(argp, &events, sizeof(events)))
  1529. return -EFAULT;
  1530. return 0;
  1531. }
  1532. case KVM_SET_VCPU_EVENTS: {
  1533. struct kvm_vcpu_events events;
  1534. if (!kvm_vcpu_initialized(vcpu))
  1535. return -ENOEXEC;
  1536. if (copy_from_user(&events, argp, sizeof(events)))
  1537. return -EFAULT;
  1538. return kvm_arm_vcpu_set_events(vcpu, &events);
  1539. }
  1540. case KVM_ARM_VCPU_FINALIZE: {
  1541. int what;
  1542. if (!kvm_vcpu_initialized(vcpu))
  1543. return -ENOEXEC;
  1544. if (get_user(what, (const int __user *)argp))
  1545. return -EFAULT;
  1546. return kvm_arm_vcpu_finalize(vcpu, what);
  1547. }
  1548. default:
  1549. r = -EINVAL;
  1550. }
  1551. return r;
  1552. }
  1553. long kvm_arch_vcpu_unlocked_ioctl(struct file *filp, unsigned int ioctl,
  1554. unsigned long arg)
  1555. {
  1556. return -ENOIOCTLCMD;
  1557. }
  1558. void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
  1559. {
  1560. }
  1561. static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
  1562. struct kvm_arm_device_addr *dev_addr)
  1563. {
  1564. switch (FIELD_GET(KVM_ARM_DEVICE_ID_MASK, dev_addr->id)) {
  1565. case KVM_ARM_DEVICE_VGIC_V2:
  1566. if (!vgic_present)
  1567. return -ENXIO;
  1568. return kvm_set_legacy_vgic_v2_addr(kvm, dev_addr);
  1569. default:
  1570. return -ENODEV;
  1571. }
  1572. }
  1573. static int kvm_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr)
  1574. {
  1575. switch (attr->group) {
  1576. case KVM_ARM_VM_SMCCC_CTRL:
  1577. return kvm_vm_smccc_has_attr(kvm, attr);
  1578. default:
  1579. return -ENXIO;
  1580. }
  1581. }
  1582. static int kvm_vm_set_attr(struct kvm *kvm, struct kvm_device_attr *attr)
  1583. {
  1584. switch (attr->group) {
  1585. case KVM_ARM_VM_SMCCC_CTRL:
  1586. return kvm_vm_smccc_set_attr(kvm, attr);
  1587. default:
  1588. return -ENXIO;
  1589. }
  1590. }
  1591. int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
  1592. {
  1593. struct kvm *kvm = filp->private_data;
  1594. void __user *argp = (void __user *)arg;
  1595. struct kvm_device_attr attr;
  1596. if (is_protected_kvm_enabled() && !kvm_pkvm_ioctl_allowed(kvm, ioctl))
  1597. return -EINVAL;
  1598. switch (ioctl) {
  1599. case KVM_CREATE_IRQCHIP: {
  1600. int ret;
  1601. if (!vgic_present)
  1602. return -ENXIO;
  1603. mutex_lock(&kvm->lock);
  1604. ret = kvm_vgic_create(kvm, KVM_DEV_TYPE_ARM_VGIC_V2);
  1605. mutex_unlock(&kvm->lock);
  1606. return ret;
  1607. }
  1608. case KVM_ARM_SET_DEVICE_ADDR: {
  1609. struct kvm_arm_device_addr dev_addr;
  1610. if (copy_from_user(&dev_addr, argp, sizeof(dev_addr)))
  1611. return -EFAULT;
  1612. return kvm_vm_ioctl_set_device_addr(kvm, &dev_addr);
  1613. }
  1614. case KVM_ARM_PREFERRED_TARGET: {
  1615. struct kvm_vcpu_init init = {
  1616. .target = KVM_ARM_TARGET_GENERIC_V8,
  1617. };
  1618. if (copy_to_user(argp, &init, sizeof(init)))
  1619. return -EFAULT;
  1620. return 0;
  1621. }
  1622. case KVM_ARM_MTE_COPY_TAGS: {
  1623. struct kvm_arm_copy_mte_tags copy_tags;
  1624. if (copy_from_user(&copy_tags, argp, sizeof(copy_tags)))
  1625. return -EFAULT;
  1626. return kvm_vm_ioctl_mte_copy_tags(kvm, &copy_tags);
  1627. }
  1628. case KVM_ARM_SET_COUNTER_OFFSET: {
  1629. struct kvm_arm_counter_offset offset;
  1630. if (copy_from_user(&offset, argp, sizeof(offset)))
  1631. return -EFAULT;
  1632. return kvm_vm_ioctl_set_counter_offset(kvm, &offset);
  1633. }
  1634. case KVM_HAS_DEVICE_ATTR: {
  1635. if (copy_from_user(&attr, argp, sizeof(attr)))
  1636. return -EFAULT;
  1637. return kvm_vm_has_attr(kvm, &attr);
  1638. }
  1639. case KVM_SET_DEVICE_ATTR: {
  1640. if (copy_from_user(&attr, argp, sizeof(attr)))
  1641. return -EFAULT;
  1642. return kvm_vm_set_attr(kvm, &attr);
  1643. }
  1644. case KVM_ARM_GET_REG_WRITABLE_MASKS: {
  1645. struct reg_mask_range range;
  1646. if (copy_from_user(&range, argp, sizeof(range)))
  1647. return -EFAULT;
  1648. return kvm_vm_ioctl_get_reg_writable_masks(kvm, &range);
  1649. }
  1650. default:
  1651. return -EINVAL;
  1652. }
  1653. }
  1654. static unsigned long nvhe_percpu_size(void)
  1655. {
  1656. return (unsigned long)CHOOSE_NVHE_SYM(__per_cpu_end) -
  1657. (unsigned long)CHOOSE_NVHE_SYM(__per_cpu_start);
  1658. }
  1659. static unsigned long nvhe_percpu_order(void)
  1660. {
  1661. unsigned long size = nvhe_percpu_size();
  1662. return size ? get_order(size) : 0;
  1663. }
  1664. static size_t pkvm_host_sve_state_order(void)
  1665. {
  1666. return get_order(pkvm_host_sve_state_size());
  1667. }
  1668. /* A lookup table holding the hypervisor VA for each vector slot */
  1669. static void *hyp_spectre_vector_selector[BP_HARDEN_EL2_SLOTS];
  1670. static void kvm_init_vector_slot(void *base, enum arm64_hyp_spectre_vector slot)
  1671. {
  1672. hyp_spectre_vector_selector[slot] = __kvm_vector_slot2addr(base, slot);
  1673. }
  1674. static int kvm_init_vector_slots(void)
  1675. {
  1676. int err;
  1677. void *base;
  1678. base = kern_hyp_va(kvm_ksym_ref(__kvm_hyp_vector));
  1679. kvm_init_vector_slot(base, HYP_VECTOR_DIRECT);
  1680. base = kern_hyp_va(kvm_ksym_ref(__bp_harden_hyp_vecs));
  1681. kvm_init_vector_slot(base, HYP_VECTOR_SPECTRE_DIRECT);
  1682. if (kvm_system_needs_idmapped_vectors() &&
  1683. !is_protected_kvm_enabled()) {
  1684. err = create_hyp_exec_mappings(__pa_symbol(__bp_harden_hyp_vecs),
  1685. __BP_HARDEN_HYP_VECS_SZ, &base);
  1686. if (err)
  1687. return err;
  1688. }
  1689. kvm_init_vector_slot(base, HYP_VECTOR_INDIRECT);
  1690. kvm_init_vector_slot(base, HYP_VECTOR_SPECTRE_INDIRECT);
  1691. return 0;
  1692. }
  1693. static void __init cpu_prepare_hyp_mode(int cpu, u32 hyp_va_bits)
  1694. {
  1695. struct kvm_nvhe_init_params *params = per_cpu_ptr_nvhe_sym(kvm_init_params, cpu);
  1696. unsigned long tcr;
  1697. /*
  1698. * Calculate the raw per-cpu offset without a translation from the
  1699. * kernel's mapping to the linear mapping, and store it in tpidr_el2
  1700. * so that we can use adr_l to access per-cpu variables in EL2.
  1701. * Also drop the KASAN tag which gets in the way...
  1702. */
  1703. params->tpidr_el2 = (unsigned long)kasan_reset_tag(per_cpu_ptr_nvhe_sym(__per_cpu_start, cpu)) -
  1704. (unsigned long)kvm_ksym_ref(CHOOSE_NVHE_SYM(__per_cpu_start));
  1705. params->mair_el2 = read_sysreg(mair_el1);
  1706. tcr = read_sysreg(tcr_el1);
  1707. if (cpus_have_final_cap(ARM64_KVM_HVHE)) {
  1708. tcr &= ~(TCR_HD | TCR_HA | TCR_A1 | TCR_T0SZ_MASK);
  1709. tcr |= TCR_EPD1_MASK;
  1710. } else {
  1711. unsigned long ips = FIELD_GET(TCR_IPS_MASK, tcr);
  1712. tcr &= TCR_EL2_MASK;
  1713. tcr |= TCR_EL2_RES1 | FIELD_PREP(TCR_EL2_PS_MASK, ips);
  1714. if (lpa2_is_enabled())
  1715. tcr |= TCR_EL2_DS;
  1716. }
  1717. tcr |= TCR_T0SZ(hyp_va_bits);
  1718. params->tcr_el2 = tcr;
  1719. params->pgd_pa = kvm_mmu_get_httbr();
  1720. if (is_protected_kvm_enabled())
  1721. params->hcr_el2 = HCR_HOST_NVHE_PROTECTED_FLAGS;
  1722. else
  1723. params->hcr_el2 = HCR_HOST_NVHE_FLAGS;
  1724. if (system_supports_mte())
  1725. params->hcr_el2 |= HCR_ATA;
  1726. else
  1727. params->hcr_el2 |= HCR_TID5;
  1728. if (cpus_have_final_cap(ARM64_KVM_HVHE))
  1729. params->hcr_el2 |= HCR_E2H;
  1730. params->vttbr = params->vtcr = 0;
  1731. /*
  1732. * Flush the init params from the data cache because the struct will
  1733. * be read while the MMU is off.
  1734. */
  1735. kvm_flush_dcache_to_poc(params, sizeof(*params));
  1736. }
  1737. static void hyp_install_host_vector(void)
  1738. {
  1739. struct kvm_nvhe_init_params *params;
  1740. struct arm_smccc_res res;
  1741. /* Switch from the HYP stub to our own HYP init vector */
  1742. __hyp_set_vectors(kvm_get_idmap_vector());
  1743. /*
  1744. * Call initialization code, and switch to the full blown HYP code.
  1745. * If the cpucaps haven't been finalized yet, something has gone very
  1746. * wrong, and hyp will crash and burn when it uses any
  1747. * cpus_have_*_cap() wrapper.
  1748. */
  1749. BUG_ON(!system_capabilities_finalized());
  1750. params = this_cpu_ptr_nvhe_sym(kvm_init_params);
  1751. arm_smccc_1_1_hvc(KVM_HOST_SMCCC_FUNC(__kvm_hyp_init), virt_to_phys(params), &res);
  1752. WARN_ON(res.a0 != SMCCC_RET_SUCCESS);
  1753. }
  1754. static void cpu_init_hyp_mode(void)
  1755. {
  1756. hyp_install_host_vector();
  1757. /*
  1758. * Disabling SSBD on a non-VHE system requires us to enable SSBS
  1759. * at EL2.
  1760. */
  1761. if (this_cpu_has_cap(ARM64_SSBS) &&
  1762. arm64_get_spectre_v4_state() == SPECTRE_VULNERABLE) {
  1763. kvm_call_hyp_nvhe(__kvm_enable_ssbs);
  1764. }
  1765. }
  1766. static void cpu_hyp_reset(void)
  1767. {
  1768. if (!is_kernel_in_hyp_mode())
  1769. __hyp_reset_vectors();
  1770. }
  1771. /*
  1772. * EL2 vectors can be mapped and rerouted in a number of ways,
  1773. * depending on the kernel configuration and CPU present:
  1774. *
  1775. * - If the CPU is affected by Spectre-v2, the hardening sequence is
  1776. * placed in one of the vector slots, which is executed before jumping
  1777. * to the real vectors.
  1778. *
  1779. * - If the CPU also has the ARM64_SPECTRE_V3A cap, the slot
  1780. * containing the hardening sequence is mapped next to the idmap page,
  1781. * and executed before jumping to the real vectors.
  1782. *
  1783. * - If the CPU only has the ARM64_SPECTRE_V3A cap, then an
  1784. * empty slot is selected, mapped next to the idmap page, and
  1785. * executed before jumping to the real vectors.
  1786. *
  1787. * Note that ARM64_SPECTRE_V3A is somewhat incompatible with
  1788. * VHE, as we don't have hypervisor-specific mappings. If the system
  1789. * is VHE and yet selects this capability, it will be ignored.
  1790. */
  1791. static void cpu_set_hyp_vector(void)
  1792. {
  1793. struct bp_hardening_data *data = this_cpu_ptr(&bp_hardening_data);
  1794. void *vector = hyp_spectre_vector_selector[data->slot];
  1795. if (!is_protected_kvm_enabled())
  1796. *this_cpu_ptr_hyp_sym(kvm_hyp_vector) = (unsigned long)vector;
  1797. else
  1798. kvm_call_hyp_nvhe(__pkvm_cpu_set_vector, data->slot);
  1799. }
  1800. static void cpu_hyp_init_context(void)
  1801. {
  1802. kvm_init_host_cpu_context(host_data_ptr(host_ctxt));
  1803. kvm_init_host_debug_data();
  1804. if (!is_kernel_in_hyp_mode())
  1805. cpu_init_hyp_mode();
  1806. }
  1807. static void cpu_hyp_init_features(void)
  1808. {
  1809. cpu_set_hyp_vector();
  1810. if (is_kernel_in_hyp_mode()) {
  1811. kvm_timer_init_vhe();
  1812. kvm_debug_init_vhe();
  1813. }
  1814. if (vgic_present)
  1815. kvm_vgic_init_cpu_hardware();
  1816. }
  1817. static void cpu_hyp_reinit(void)
  1818. {
  1819. cpu_hyp_reset();
  1820. cpu_hyp_init_context();
  1821. cpu_hyp_init_features();
  1822. }
  1823. static void cpu_hyp_init(void *discard)
  1824. {
  1825. if (!__this_cpu_read(kvm_hyp_initialized)) {
  1826. cpu_hyp_reinit();
  1827. __this_cpu_write(kvm_hyp_initialized, 1);
  1828. }
  1829. }
  1830. static void cpu_hyp_uninit(void *discard)
  1831. {
  1832. if (!is_protected_kvm_enabled() && __this_cpu_read(kvm_hyp_initialized)) {
  1833. cpu_hyp_reset();
  1834. __this_cpu_write(kvm_hyp_initialized, 0);
  1835. }
  1836. }
  1837. int kvm_arch_enable_virtualization_cpu(void)
  1838. {
  1839. /*
  1840. * Most calls to this function are made with migration
  1841. * disabled, but not with preemption disabled. The former is
  1842. * enough to ensure correctness, but most of the helpers
  1843. * expect the later and will throw a tantrum otherwise.
  1844. */
  1845. preempt_disable();
  1846. cpu_hyp_init(NULL);
  1847. kvm_vgic_cpu_up();
  1848. kvm_timer_cpu_up();
  1849. preempt_enable();
  1850. return 0;
  1851. }
  1852. void kvm_arch_disable_virtualization_cpu(void)
  1853. {
  1854. kvm_timer_cpu_down();
  1855. kvm_vgic_cpu_down();
  1856. if (!is_protected_kvm_enabled())
  1857. cpu_hyp_uninit(NULL);
  1858. }
  1859. #ifdef CONFIG_CPU_PM
  1860. static int hyp_init_cpu_pm_notifier(struct notifier_block *self,
  1861. unsigned long cmd,
  1862. void *v)
  1863. {
  1864. /*
  1865. * kvm_hyp_initialized is left with its old value over
  1866. * PM_ENTER->PM_EXIT. It is used to indicate PM_EXIT should
  1867. * re-enable hyp.
  1868. */
  1869. switch (cmd) {
  1870. case CPU_PM_ENTER:
  1871. if (__this_cpu_read(kvm_hyp_initialized))
  1872. /*
  1873. * don't update kvm_hyp_initialized here
  1874. * so that the hyp will be re-enabled
  1875. * when we resume. See below.
  1876. */
  1877. cpu_hyp_reset();
  1878. return NOTIFY_OK;
  1879. case CPU_PM_ENTER_FAILED:
  1880. case CPU_PM_EXIT:
  1881. if (__this_cpu_read(kvm_hyp_initialized))
  1882. /* The hyp was enabled before suspend. */
  1883. cpu_hyp_reinit();
  1884. return NOTIFY_OK;
  1885. default:
  1886. return NOTIFY_DONE;
  1887. }
  1888. }
  1889. static struct notifier_block hyp_init_cpu_pm_nb = {
  1890. .notifier_call = hyp_init_cpu_pm_notifier,
  1891. };
  1892. static void __init hyp_cpu_pm_init(void)
  1893. {
  1894. if (!is_protected_kvm_enabled())
  1895. cpu_pm_register_notifier(&hyp_init_cpu_pm_nb);
  1896. }
  1897. static void __init hyp_cpu_pm_exit(void)
  1898. {
  1899. if (!is_protected_kvm_enabled())
  1900. cpu_pm_unregister_notifier(&hyp_init_cpu_pm_nb);
  1901. }
  1902. #else
  1903. static inline void __init hyp_cpu_pm_init(void)
  1904. {
  1905. }
  1906. static inline void __init hyp_cpu_pm_exit(void)
  1907. {
  1908. }
  1909. #endif
  1910. static void __init init_cpu_logical_map(void)
  1911. {
  1912. unsigned int cpu;
  1913. /*
  1914. * Copy the MPIDR <-> logical CPU ID mapping to hyp.
  1915. * Only copy the set of online CPUs whose features have been checked
  1916. * against the finalized system capabilities. The hypervisor will not
  1917. * allow any other CPUs from the `possible` set to boot.
  1918. */
  1919. for_each_online_cpu(cpu)
  1920. hyp_cpu_logical_map[cpu] = cpu_logical_map(cpu);
  1921. }
  1922. #define init_psci_0_1_impl_state(config, what) \
  1923. config.psci_0_1_ ## what ## _implemented = psci_ops.what
  1924. static bool __init init_psci_relay(void)
  1925. {
  1926. /*
  1927. * If PSCI has not been initialized, protected KVM cannot install
  1928. * itself on newly booted CPUs.
  1929. */
  1930. if (!psci_ops.get_version) {
  1931. kvm_err("Cannot initialize protected mode without PSCI\n");
  1932. return false;
  1933. }
  1934. kvm_host_psci_config.version = psci_ops.get_version();
  1935. kvm_host_psci_config.smccc_version = arm_smccc_get_version();
  1936. if (kvm_host_psci_config.version == PSCI_VERSION(0, 1)) {
  1937. kvm_host_psci_config.function_ids_0_1 = get_psci_0_1_function_ids();
  1938. init_psci_0_1_impl_state(kvm_host_psci_config, cpu_suspend);
  1939. init_psci_0_1_impl_state(kvm_host_psci_config, cpu_on);
  1940. init_psci_0_1_impl_state(kvm_host_psci_config, cpu_off);
  1941. init_psci_0_1_impl_state(kvm_host_psci_config, migrate);
  1942. }
  1943. return true;
  1944. }
  1945. static int __init init_subsystems(void)
  1946. {
  1947. int err = 0;
  1948. /*
  1949. * Enable hardware so that subsystem initialisation can access EL2.
  1950. */
  1951. on_each_cpu(cpu_hyp_init, NULL, 1);
  1952. /*
  1953. * Register CPU lower-power notifier
  1954. */
  1955. hyp_cpu_pm_init();
  1956. /*
  1957. * Init HYP view of VGIC
  1958. */
  1959. err = kvm_vgic_hyp_init();
  1960. switch (err) {
  1961. case 0:
  1962. vgic_present = true;
  1963. break;
  1964. case -ENODEV:
  1965. case -ENXIO:
  1966. /*
  1967. * No VGIC? No pKVM for you.
  1968. *
  1969. * Protected mode assumes that VGICv3 is present, so no point
  1970. * in trying to hobble along if vgic initialization fails.
  1971. */
  1972. if (is_protected_kvm_enabled())
  1973. goto out;
  1974. /*
  1975. * Otherwise, userspace could choose to implement a GIC for its
  1976. * guest on non-cooperative hardware.
  1977. */
  1978. vgic_present = false;
  1979. err = 0;
  1980. break;
  1981. default:
  1982. goto out;
  1983. }
  1984. if (kvm_mode == KVM_MODE_NV &&
  1985. !(vgic_present && (kvm_vgic_global_state.type == VGIC_V3 ||
  1986. kvm_vgic_global_state.has_gcie_v3_compat))) {
  1987. kvm_err("NV support requires GICv3 or GICv5 with legacy support, giving up\n");
  1988. err = -EINVAL;
  1989. goto out;
  1990. }
  1991. /*
  1992. * Init HYP architected timer support
  1993. */
  1994. err = kvm_timer_hyp_init(vgic_present);
  1995. if (err)
  1996. goto out;
  1997. kvm_register_perf_callbacks();
  1998. out:
  1999. if (err)
  2000. hyp_cpu_pm_exit();
  2001. if (err || !is_protected_kvm_enabled())
  2002. on_each_cpu(cpu_hyp_uninit, NULL, 1);
  2003. return err;
  2004. }
  2005. static void __init teardown_subsystems(void)
  2006. {
  2007. kvm_unregister_perf_callbacks();
  2008. hyp_cpu_pm_exit();
  2009. }
  2010. static void __init teardown_hyp_mode(void)
  2011. {
  2012. bool free_sve = system_supports_sve() && is_protected_kvm_enabled();
  2013. int cpu;
  2014. free_hyp_pgds();
  2015. for_each_possible_cpu(cpu) {
  2016. if (per_cpu(kvm_hyp_initialized, cpu))
  2017. continue;
  2018. free_pages(per_cpu(kvm_arm_hyp_stack_base, cpu), NVHE_STACK_SHIFT - PAGE_SHIFT);
  2019. if (!kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu])
  2020. continue;
  2021. if (free_sve) {
  2022. struct cpu_sve_state *sve_state;
  2023. sve_state = per_cpu_ptr_nvhe_sym(kvm_host_data, cpu)->sve_state;
  2024. free_pages((unsigned long) sve_state, pkvm_host_sve_state_order());
  2025. }
  2026. free_pages(kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu], nvhe_percpu_order());
  2027. }
  2028. }
  2029. static int __init do_pkvm_init(u32 hyp_va_bits)
  2030. {
  2031. void *per_cpu_base = kvm_ksym_ref(kvm_nvhe_sym(kvm_arm_hyp_percpu_base));
  2032. int ret;
  2033. preempt_disable();
  2034. cpu_hyp_init_context();
  2035. ret = kvm_call_hyp_nvhe(__pkvm_init, hyp_mem_base, hyp_mem_size,
  2036. num_possible_cpus(), kern_hyp_va(per_cpu_base),
  2037. hyp_va_bits);
  2038. cpu_hyp_init_features();
  2039. /*
  2040. * The stub hypercalls are now disabled, so set our local flag to
  2041. * prevent a later re-init attempt in kvm_arch_enable_virtualization_cpu().
  2042. */
  2043. __this_cpu_write(kvm_hyp_initialized, 1);
  2044. preempt_enable();
  2045. return ret;
  2046. }
  2047. static u64 get_hyp_id_aa64pfr0_el1(void)
  2048. {
  2049. /*
  2050. * Track whether the system isn't affected by spectre/meltdown in the
  2051. * hypervisor's view of id_aa64pfr0_el1, used for protected VMs.
  2052. * Although this is per-CPU, we make it global for simplicity, e.g., not
  2053. * to have to worry about vcpu migration.
  2054. *
  2055. * Unlike for non-protected VMs, userspace cannot override this for
  2056. * protected VMs.
  2057. */
  2058. u64 val = read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1);
  2059. val &= ~(ID_AA64PFR0_EL1_CSV2 |
  2060. ID_AA64PFR0_EL1_CSV3);
  2061. val |= FIELD_PREP(ID_AA64PFR0_EL1_CSV2,
  2062. arm64_get_spectre_v2_state() == SPECTRE_UNAFFECTED);
  2063. val |= FIELD_PREP(ID_AA64PFR0_EL1_CSV3,
  2064. arm64_get_meltdown_state() == SPECTRE_UNAFFECTED);
  2065. return val;
  2066. }
  2067. static void kvm_hyp_init_symbols(void)
  2068. {
  2069. kvm_nvhe_sym(id_aa64pfr0_el1_sys_val) = get_hyp_id_aa64pfr0_el1();
  2070. kvm_nvhe_sym(id_aa64pfr1_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64PFR1_EL1);
  2071. kvm_nvhe_sym(id_aa64isar0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64ISAR0_EL1);
  2072. kvm_nvhe_sym(id_aa64isar1_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64ISAR1_EL1);
  2073. kvm_nvhe_sym(id_aa64isar2_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64ISAR2_EL1);
  2074. kvm_nvhe_sym(id_aa64mmfr0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1);
  2075. kvm_nvhe_sym(id_aa64mmfr1_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR1_EL1);
  2076. kvm_nvhe_sym(id_aa64mmfr2_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR2_EL1);
  2077. kvm_nvhe_sym(id_aa64smfr0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64SMFR0_EL1);
  2078. kvm_nvhe_sym(__icache_flags) = __icache_flags;
  2079. kvm_nvhe_sym(kvm_arm_vmid_bits) = kvm_arm_vmid_bits;
  2080. /* Propagate the FGT state to the nVHE side */
  2081. kvm_nvhe_sym(hfgrtr_masks) = hfgrtr_masks;
  2082. kvm_nvhe_sym(hfgwtr_masks) = hfgwtr_masks;
  2083. kvm_nvhe_sym(hfgitr_masks) = hfgitr_masks;
  2084. kvm_nvhe_sym(hdfgrtr_masks) = hdfgrtr_masks;
  2085. kvm_nvhe_sym(hdfgwtr_masks) = hdfgwtr_masks;
  2086. kvm_nvhe_sym(hafgrtr_masks) = hafgrtr_masks;
  2087. kvm_nvhe_sym(hfgrtr2_masks) = hfgrtr2_masks;
  2088. kvm_nvhe_sym(hfgwtr2_masks) = hfgwtr2_masks;
  2089. kvm_nvhe_sym(hfgitr2_masks) = hfgitr2_masks;
  2090. kvm_nvhe_sym(hdfgrtr2_masks)= hdfgrtr2_masks;
  2091. kvm_nvhe_sym(hdfgwtr2_masks)= hdfgwtr2_masks;
  2092. /*
  2093. * Flush entire BSS since part of its data containing init symbols is read
  2094. * while the MMU is off.
  2095. */
  2096. kvm_flush_dcache_to_poc(kvm_ksym_ref(__hyp_bss_start),
  2097. kvm_ksym_ref(__hyp_bss_end) - kvm_ksym_ref(__hyp_bss_start));
  2098. }
  2099. static int __init kvm_hyp_init_protection(u32 hyp_va_bits)
  2100. {
  2101. void *addr = phys_to_virt(hyp_mem_base);
  2102. int ret;
  2103. ret = create_hyp_mappings(addr, addr + hyp_mem_size, PAGE_HYP);
  2104. if (ret)
  2105. return ret;
  2106. ret = do_pkvm_init(hyp_va_bits);
  2107. if (ret)
  2108. return ret;
  2109. free_hyp_pgds();
  2110. return 0;
  2111. }
  2112. static int init_pkvm_host_sve_state(void)
  2113. {
  2114. int cpu;
  2115. if (!system_supports_sve())
  2116. return 0;
  2117. /* Allocate pages for host sve state in protected mode. */
  2118. for_each_possible_cpu(cpu) {
  2119. struct page *page = alloc_pages(GFP_KERNEL, pkvm_host_sve_state_order());
  2120. if (!page)
  2121. return -ENOMEM;
  2122. per_cpu_ptr_nvhe_sym(kvm_host_data, cpu)->sve_state = page_address(page);
  2123. }
  2124. /*
  2125. * Don't map the pages in hyp since these are only used in protected
  2126. * mode, which will (re)create its own mapping when initialized.
  2127. */
  2128. return 0;
  2129. }
  2130. /*
  2131. * Finalizes the initialization of hyp mode, once everything else is initialized
  2132. * and the initialziation process cannot fail.
  2133. */
  2134. static void finalize_init_hyp_mode(void)
  2135. {
  2136. int cpu;
  2137. if (system_supports_sve() && is_protected_kvm_enabled()) {
  2138. for_each_possible_cpu(cpu) {
  2139. struct cpu_sve_state *sve_state;
  2140. sve_state = per_cpu_ptr_nvhe_sym(kvm_host_data, cpu)->sve_state;
  2141. per_cpu_ptr_nvhe_sym(kvm_host_data, cpu)->sve_state =
  2142. kern_hyp_va(sve_state);
  2143. }
  2144. }
  2145. }
  2146. static void pkvm_hyp_init_ptrauth(void)
  2147. {
  2148. struct kvm_cpu_context *hyp_ctxt;
  2149. int cpu;
  2150. for_each_possible_cpu(cpu) {
  2151. hyp_ctxt = per_cpu_ptr_nvhe_sym(kvm_hyp_ctxt, cpu);
  2152. hyp_ctxt->sys_regs[APIAKEYLO_EL1] = get_random_long();
  2153. hyp_ctxt->sys_regs[APIAKEYHI_EL1] = get_random_long();
  2154. hyp_ctxt->sys_regs[APIBKEYLO_EL1] = get_random_long();
  2155. hyp_ctxt->sys_regs[APIBKEYHI_EL1] = get_random_long();
  2156. hyp_ctxt->sys_regs[APDAKEYLO_EL1] = get_random_long();
  2157. hyp_ctxt->sys_regs[APDAKEYHI_EL1] = get_random_long();
  2158. hyp_ctxt->sys_regs[APDBKEYLO_EL1] = get_random_long();
  2159. hyp_ctxt->sys_regs[APDBKEYHI_EL1] = get_random_long();
  2160. hyp_ctxt->sys_regs[APGAKEYLO_EL1] = get_random_long();
  2161. hyp_ctxt->sys_regs[APGAKEYHI_EL1] = get_random_long();
  2162. }
  2163. }
  2164. /* Inits Hyp-mode on all online CPUs */
  2165. static int __init init_hyp_mode(void)
  2166. {
  2167. u32 hyp_va_bits = kvm_hyp_va_bits();
  2168. int cpu;
  2169. int err = -ENOMEM;
  2170. /*
  2171. * The protected Hyp-mode cannot be initialized if the memory pool
  2172. * allocation has failed.
  2173. */
  2174. if (is_protected_kvm_enabled() && !hyp_mem_base)
  2175. goto out_err;
  2176. /*
  2177. * Allocate Hyp PGD and setup Hyp identity mapping
  2178. */
  2179. err = kvm_mmu_init(hyp_va_bits);
  2180. if (err)
  2181. goto out_err;
  2182. /*
  2183. * Allocate stack pages for Hypervisor-mode
  2184. */
  2185. for_each_possible_cpu(cpu) {
  2186. unsigned long stack_base;
  2187. stack_base = __get_free_pages(GFP_KERNEL, NVHE_STACK_SHIFT - PAGE_SHIFT);
  2188. if (!stack_base) {
  2189. err = -ENOMEM;
  2190. goto out_err;
  2191. }
  2192. per_cpu(kvm_arm_hyp_stack_base, cpu) = stack_base;
  2193. }
  2194. /*
  2195. * Allocate and initialize pages for Hypervisor-mode percpu regions.
  2196. */
  2197. for_each_possible_cpu(cpu) {
  2198. struct page *page;
  2199. void *page_addr;
  2200. page = alloc_pages(GFP_KERNEL, nvhe_percpu_order());
  2201. if (!page) {
  2202. err = -ENOMEM;
  2203. goto out_err;
  2204. }
  2205. page_addr = page_address(page);
  2206. memcpy(page_addr, CHOOSE_NVHE_SYM(__per_cpu_start), nvhe_percpu_size());
  2207. kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu] = (unsigned long)page_addr;
  2208. }
  2209. /*
  2210. * Map the Hyp-code called directly from the host
  2211. */
  2212. err = create_hyp_mappings(kvm_ksym_ref(__hyp_text_start),
  2213. kvm_ksym_ref(__hyp_text_end), PAGE_HYP_EXEC);
  2214. if (err) {
  2215. kvm_err("Cannot map world-switch code\n");
  2216. goto out_err;
  2217. }
  2218. err = create_hyp_mappings(kvm_ksym_ref(__hyp_data_start),
  2219. kvm_ksym_ref(__hyp_data_end), PAGE_HYP);
  2220. if (err) {
  2221. kvm_err("Cannot map .hyp.data section\n");
  2222. goto out_err;
  2223. }
  2224. err = create_hyp_mappings(kvm_ksym_ref(__hyp_rodata_start),
  2225. kvm_ksym_ref(__hyp_rodata_end), PAGE_HYP_RO);
  2226. if (err) {
  2227. kvm_err("Cannot map .hyp.rodata section\n");
  2228. goto out_err;
  2229. }
  2230. err = create_hyp_mappings(kvm_ksym_ref(__start_rodata),
  2231. kvm_ksym_ref(__end_rodata), PAGE_HYP_RO);
  2232. if (err) {
  2233. kvm_err("Cannot map rodata section\n");
  2234. goto out_err;
  2235. }
  2236. /*
  2237. * .hyp.bss is guaranteed to be placed at the beginning of the .bss
  2238. * section thanks to an assertion in the linker script. Map it RW and
  2239. * the rest of .bss RO.
  2240. */
  2241. err = create_hyp_mappings(kvm_ksym_ref(__hyp_bss_start),
  2242. kvm_ksym_ref(__hyp_bss_end), PAGE_HYP);
  2243. if (err) {
  2244. kvm_err("Cannot map hyp bss section: %d\n", err);
  2245. goto out_err;
  2246. }
  2247. err = create_hyp_mappings(kvm_ksym_ref(__hyp_bss_end),
  2248. kvm_ksym_ref(__bss_stop), PAGE_HYP_RO);
  2249. if (err) {
  2250. kvm_err("Cannot map bss section\n");
  2251. goto out_err;
  2252. }
  2253. /*
  2254. * Map the Hyp stack pages
  2255. */
  2256. for_each_possible_cpu(cpu) {
  2257. struct kvm_nvhe_init_params *params = per_cpu_ptr_nvhe_sym(kvm_init_params, cpu);
  2258. char *stack_base = (char *)per_cpu(kvm_arm_hyp_stack_base, cpu);
  2259. err = create_hyp_stack(__pa(stack_base), &params->stack_hyp_va);
  2260. if (err) {
  2261. kvm_err("Cannot map hyp stack\n");
  2262. goto out_err;
  2263. }
  2264. /*
  2265. * Save the stack PA in nvhe_init_params. This will be needed
  2266. * to recreate the stack mapping in protected nVHE mode.
  2267. * __hyp_pa() won't do the right thing there, since the stack
  2268. * has been mapped in the flexible private VA space.
  2269. */
  2270. params->stack_pa = __pa(stack_base);
  2271. }
  2272. for_each_possible_cpu(cpu) {
  2273. char *percpu_begin = (char *)kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu];
  2274. char *percpu_end = percpu_begin + nvhe_percpu_size();
  2275. /* Map Hyp percpu pages */
  2276. err = create_hyp_mappings(percpu_begin, percpu_end, PAGE_HYP);
  2277. if (err) {
  2278. kvm_err("Cannot map hyp percpu region\n");
  2279. goto out_err;
  2280. }
  2281. /* Prepare the CPU initialization parameters */
  2282. cpu_prepare_hyp_mode(cpu, hyp_va_bits);
  2283. }
  2284. kvm_hyp_init_symbols();
  2285. if (is_protected_kvm_enabled()) {
  2286. if (IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL) &&
  2287. cpus_have_final_cap(ARM64_HAS_ADDRESS_AUTH))
  2288. pkvm_hyp_init_ptrauth();
  2289. init_cpu_logical_map();
  2290. if (!init_psci_relay()) {
  2291. err = -ENODEV;
  2292. goto out_err;
  2293. }
  2294. err = init_pkvm_host_sve_state();
  2295. if (err)
  2296. goto out_err;
  2297. err = kvm_hyp_init_protection(hyp_va_bits);
  2298. if (err) {
  2299. kvm_err("Failed to init hyp memory protection\n");
  2300. goto out_err;
  2301. }
  2302. }
  2303. return 0;
  2304. out_err:
  2305. teardown_hyp_mode();
  2306. kvm_err("error initializing Hyp mode: %d\n", err);
  2307. return err;
  2308. }
  2309. struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr)
  2310. {
  2311. struct kvm_vcpu *vcpu = NULL;
  2312. struct kvm_mpidr_data *data;
  2313. unsigned long i;
  2314. mpidr &= MPIDR_HWID_BITMASK;
  2315. rcu_read_lock();
  2316. data = rcu_dereference(kvm->arch.mpidr_data);
  2317. if (data) {
  2318. u16 idx = kvm_mpidr_index(data, mpidr);
  2319. vcpu = kvm_get_vcpu(kvm, data->cmpidr_to_idx[idx]);
  2320. if (mpidr != kvm_vcpu_get_mpidr_aff(vcpu))
  2321. vcpu = NULL;
  2322. }
  2323. rcu_read_unlock();
  2324. if (vcpu)
  2325. return vcpu;
  2326. kvm_for_each_vcpu(i, vcpu, kvm) {
  2327. if (mpidr == kvm_vcpu_get_mpidr_aff(vcpu))
  2328. return vcpu;
  2329. }
  2330. return NULL;
  2331. }
  2332. bool kvm_arch_irqchip_in_kernel(struct kvm *kvm)
  2333. {
  2334. return irqchip_in_kernel(kvm);
  2335. }
  2336. int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
  2337. struct irq_bypass_producer *prod)
  2338. {
  2339. struct kvm_kernel_irqfd *irqfd =
  2340. container_of(cons, struct kvm_kernel_irqfd, consumer);
  2341. struct kvm_kernel_irq_routing_entry *irq_entry = &irqfd->irq_entry;
  2342. /*
  2343. * The only thing we have a chance of directly-injecting is LPIs. Maybe
  2344. * one day...
  2345. */
  2346. if (irq_entry->type != KVM_IRQ_ROUTING_MSI)
  2347. return 0;
  2348. return kvm_vgic_v4_set_forwarding(irqfd->kvm, prod->irq,
  2349. &irqfd->irq_entry);
  2350. }
  2351. void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
  2352. struct irq_bypass_producer *prod)
  2353. {
  2354. struct kvm_kernel_irqfd *irqfd =
  2355. container_of(cons, struct kvm_kernel_irqfd, consumer);
  2356. struct kvm_kernel_irq_routing_entry *irq_entry = &irqfd->irq_entry;
  2357. if (irq_entry->type != KVM_IRQ_ROUTING_MSI)
  2358. return;
  2359. kvm_vgic_v4_unset_forwarding(irqfd->kvm, prod->irq);
  2360. }
  2361. void kvm_arch_update_irqfd_routing(struct kvm_kernel_irqfd *irqfd,
  2362. struct kvm_kernel_irq_routing_entry *old,
  2363. struct kvm_kernel_irq_routing_entry *new)
  2364. {
  2365. if (old->type == KVM_IRQ_ROUTING_MSI &&
  2366. new->type == KVM_IRQ_ROUTING_MSI &&
  2367. !memcmp(&old->msi, &new->msi, sizeof(new->msi)))
  2368. return;
  2369. /*
  2370. * Remapping the vLPI requires taking the its_lock mutex to resolve
  2371. * the new translation. We're in spinlock land at this point, so no
  2372. * chance of resolving the translation.
  2373. *
  2374. * Unmap the vLPI and fall back to software LPI injection.
  2375. */
  2376. return kvm_vgic_v4_unset_forwarding(irqfd->kvm, irqfd->producer->irq);
  2377. }
  2378. void kvm_arch_irq_bypass_stop(struct irq_bypass_consumer *cons)
  2379. {
  2380. struct kvm_kernel_irqfd *irqfd =
  2381. container_of(cons, struct kvm_kernel_irqfd, consumer);
  2382. kvm_arm_halt_guest(irqfd->kvm);
  2383. }
  2384. void kvm_arch_irq_bypass_start(struct irq_bypass_consumer *cons)
  2385. {
  2386. struct kvm_kernel_irqfd *irqfd =
  2387. container_of(cons, struct kvm_kernel_irqfd, consumer);
  2388. kvm_arm_resume_guest(irqfd->kvm);
  2389. }
  2390. /* Initialize Hyp-mode and memory mappings on all CPUs */
  2391. static __init int kvm_arm_init(void)
  2392. {
  2393. int err;
  2394. bool in_hyp_mode;
  2395. if (!is_hyp_mode_available()) {
  2396. kvm_info("HYP mode not available\n");
  2397. return -ENODEV;
  2398. }
  2399. if (kvm_get_mode() == KVM_MODE_NONE) {
  2400. kvm_info("KVM disabled from command line\n");
  2401. return -ENODEV;
  2402. }
  2403. err = kvm_sys_reg_table_init();
  2404. if (err) {
  2405. kvm_info("Error initializing system register tables");
  2406. return err;
  2407. }
  2408. in_hyp_mode = is_kernel_in_hyp_mode();
  2409. if (cpus_have_final_cap(ARM64_WORKAROUND_DEVICE_LOAD_ACQUIRE) ||
  2410. cpus_have_final_cap(ARM64_WORKAROUND_1508412))
  2411. kvm_info("Guests without required CPU erratum workarounds can deadlock system!\n" \
  2412. "Only trusted guests should be used on this system.\n");
  2413. err = kvm_set_ipa_limit();
  2414. if (err)
  2415. return err;
  2416. err = kvm_arm_init_sve();
  2417. if (err)
  2418. return err;
  2419. err = kvm_arm_vmid_alloc_init();
  2420. if (err) {
  2421. kvm_err("Failed to initialize VMID allocator.\n");
  2422. return err;
  2423. }
  2424. if (!in_hyp_mode) {
  2425. err = init_hyp_mode();
  2426. if (err)
  2427. goto out_err;
  2428. }
  2429. err = kvm_init_vector_slots();
  2430. if (err) {
  2431. kvm_err("Cannot initialise vector slots\n");
  2432. goto out_hyp;
  2433. }
  2434. err = init_subsystems();
  2435. if (err)
  2436. goto out_hyp;
  2437. kvm_info("%s%sVHE%s mode initialized successfully\n",
  2438. in_hyp_mode ? "" : (is_protected_kvm_enabled() ?
  2439. "Protected " : "Hyp "),
  2440. in_hyp_mode ? "" : (cpus_have_final_cap(ARM64_KVM_HVHE) ?
  2441. "h" : "n"),
  2442. cpus_have_final_cap(ARM64_HAS_NESTED_VIRT) ? "+NV2": "");
  2443. /*
  2444. * FIXME: Do something reasonable if kvm_init() fails after pKVM
  2445. * hypervisor protection is finalized.
  2446. */
  2447. err = kvm_init(sizeof(struct kvm_vcpu), 0, THIS_MODULE);
  2448. if (err)
  2449. goto out_subs;
  2450. /*
  2451. * This should be called after initialization is done and failure isn't
  2452. * possible anymore.
  2453. */
  2454. if (!in_hyp_mode)
  2455. finalize_init_hyp_mode();
  2456. kvm_arm_initialised = true;
  2457. return 0;
  2458. out_subs:
  2459. teardown_subsystems();
  2460. out_hyp:
  2461. if (!in_hyp_mode)
  2462. teardown_hyp_mode();
  2463. out_err:
  2464. kvm_arm_vmid_alloc_free();
  2465. return err;
  2466. }
  2467. static int __init early_kvm_mode_cfg(char *arg)
  2468. {
  2469. if (!arg)
  2470. return -EINVAL;
  2471. if (strcmp(arg, "none") == 0) {
  2472. kvm_mode = KVM_MODE_NONE;
  2473. return 0;
  2474. }
  2475. if (!is_hyp_mode_available()) {
  2476. pr_warn_once("KVM is not available. Ignoring kvm-arm.mode\n");
  2477. return 0;
  2478. }
  2479. if (strcmp(arg, "protected") == 0) {
  2480. if (!is_kernel_in_hyp_mode())
  2481. kvm_mode = KVM_MODE_PROTECTED;
  2482. else
  2483. pr_warn_once("Protected KVM not available with VHE\n");
  2484. return 0;
  2485. }
  2486. if (strcmp(arg, "nvhe") == 0 && !WARN_ON(is_kernel_in_hyp_mode())) {
  2487. kvm_mode = KVM_MODE_DEFAULT;
  2488. return 0;
  2489. }
  2490. if (strcmp(arg, "nested") == 0 && !WARN_ON(!is_kernel_in_hyp_mode())) {
  2491. kvm_mode = KVM_MODE_NV;
  2492. return 0;
  2493. }
  2494. return -EINVAL;
  2495. }
  2496. early_param("kvm-arm.mode", early_kvm_mode_cfg);
  2497. static int __init early_kvm_wfx_trap_policy_cfg(char *arg, enum kvm_wfx_trap_policy *p)
  2498. {
  2499. if (!arg)
  2500. return -EINVAL;
  2501. if (strcmp(arg, "trap") == 0) {
  2502. *p = KVM_WFX_TRAP;
  2503. return 0;
  2504. }
  2505. if (strcmp(arg, "notrap") == 0) {
  2506. *p = KVM_WFX_NOTRAP;
  2507. return 0;
  2508. }
  2509. return -EINVAL;
  2510. }
  2511. static int __init early_kvm_wfi_trap_policy_cfg(char *arg)
  2512. {
  2513. return early_kvm_wfx_trap_policy_cfg(arg, &kvm_wfi_trap_policy);
  2514. }
  2515. early_param("kvm-arm.wfi_trap_policy", early_kvm_wfi_trap_policy_cfg);
  2516. static int __init early_kvm_wfe_trap_policy_cfg(char *arg)
  2517. {
  2518. return early_kvm_wfx_trap_policy_cfg(arg, &kvm_wfe_trap_policy);
  2519. }
  2520. early_param("kvm-arm.wfe_trap_policy", early_kvm_wfe_trap_policy_cfg);
  2521. enum kvm_mode kvm_get_mode(void)
  2522. {
  2523. return kvm_mode;
  2524. }
  2525. module_init(kvm_arm_init);