xen.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
  4. * Copyright © 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  5. *
  6. * KVM Xen emulation
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #include "x86.h"
  10. #include "xen.h"
  11. #include "hyperv.h"
  12. #include "irq.h"
  13. #include <linux/eventfd.h>
  14. #include <linux/kvm_host.h>
  15. #include <linux/sched/stat.h>
  16. #include <trace/events/kvm.h>
  17. #include <xen/interface/xen.h>
  18. #include <xen/interface/vcpu.h>
  19. #include <xen/interface/version.h>
  20. #include <xen/interface/event_channel.h>
  21. #include <xen/interface/sched.h>
  22. #include <asm/xen/cpuid.h>
  23. #include <asm/pvclock.h>
  24. #include "cpuid.h"
  25. #include "trace.h"
  26. static int kvm_xen_set_evtchn(struct kvm_xen_evtchn *xe, struct kvm *kvm);
  27. static int kvm_xen_setattr_evtchn(struct kvm *kvm, struct kvm_xen_hvm_attr *data);
  28. static bool kvm_xen_hcall_evtchn_send(struct kvm_vcpu *vcpu, u64 param, u64 *r);
  29. DEFINE_STATIC_KEY_DEFERRED_FALSE(kvm_xen_enabled, HZ);
  30. static int kvm_xen_shared_info_init(struct kvm *kvm)
  31. {
  32. struct gfn_to_pfn_cache *gpc = &kvm->arch.xen.shinfo_cache;
  33. struct pvclock_wall_clock *wc;
  34. u32 *wc_sec_hi;
  35. u32 wc_version;
  36. u64 wall_nsec;
  37. int ret = 0;
  38. int idx = srcu_read_lock(&kvm->srcu);
  39. read_lock_irq(&gpc->lock);
  40. while (!kvm_gpc_check(gpc, PAGE_SIZE)) {
  41. read_unlock_irq(&gpc->lock);
  42. ret = kvm_gpc_refresh(gpc, PAGE_SIZE);
  43. if (ret)
  44. goto out;
  45. read_lock_irq(&gpc->lock);
  46. }
  47. /*
  48. * This code mirrors kvm_write_wall_clock() except that it writes
  49. * directly through the pfn cache and doesn't mark the page dirty.
  50. */
  51. wall_nsec = kvm_get_wall_clock_epoch(kvm);
  52. /* Paranoia checks on the 32-bit struct layout */
  53. BUILD_BUG_ON(offsetof(struct compat_shared_info, wc) != 0x900);
  54. BUILD_BUG_ON(offsetof(struct compat_shared_info, arch.wc_sec_hi) != 0x924);
  55. BUILD_BUG_ON(offsetof(struct pvclock_vcpu_time_info, version) != 0);
  56. #ifdef CONFIG_X86_64
  57. /* Paranoia checks on the 64-bit struct layout */
  58. BUILD_BUG_ON(offsetof(struct shared_info, wc) != 0xc00);
  59. BUILD_BUG_ON(offsetof(struct shared_info, wc_sec_hi) != 0xc0c);
  60. if (IS_ENABLED(CONFIG_64BIT) && kvm->arch.xen.long_mode) {
  61. struct shared_info *shinfo = gpc->khva;
  62. wc_sec_hi = &shinfo->wc_sec_hi;
  63. wc = &shinfo->wc;
  64. } else
  65. #endif
  66. {
  67. struct compat_shared_info *shinfo = gpc->khva;
  68. wc_sec_hi = &shinfo->arch.wc_sec_hi;
  69. wc = &shinfo->wc;
  70. }
  71. /* Increment and ensure an odd value */
  72. wc_version = wc->version = (wc->version + 1) | 1;
  73. smp_wmb();
  74. wc->nsec = do_div(wall_nsec, NSEC_PER_SEC);
  75. wc->sec = (u32)wall_nsec;
  76. *wc_sec_hi = wall_nsec >> 32;
  77. smp_wmb();
  78. wc->version = wc_version + 1;
  79. read_unlock_irq(&gpc->lock);
  80. kvm_make_all_cpus_request(kvm, KVM_REQ_MASTERCLOCK_UPDATE);
  81. out:
  82. srcu_read_unlock(&kvm->srcu, idx);
  83. return ret;
  84. }
  85. void kvm_xen_inject_timer_irqs(struct kvm_vcpu *vcpu)
  86. {
  87. if (atomic_read(&vcpu->arch.xen.timer_pending) > 0) {
  88. struct kvm_xen_evtchn e;
  89. e.vcpu_id = vcpu->vcpu_id;
  90. e.vcpu_idx = vcpu->vcpu_idx;
  91. e.port = vcpu->arch.xen.timer_virq;
  92. e.priority = KVM_IRQ_ROUTING_XEN_EVTCHN_PRIO_2LEVEL;
  93. kvm_xen_set_evtchn(&e, vcpu->kvm);
  94. vcpu->arch.xen.timer_expires = 0;
  95. atomic_set(&vcpu->arch.xen.timer_pending, 0);
  96. }
  97. }
  98. static enum hrtimer_restart xen_timer_callback(struct hrtimer *timer)
  99. {
  100. struct kvm_vcpu *vcpu = container_of(timer, struct kvm_vcpu,
  101. arch.xen.timer);
  102. struct kvm_xen_evtchn e;
  103. int rc;
  104. if (atomic_read(&vcpu->arch.xen.timer_pending))
  105. return HRTIMER_NORESTART;
  106. e.vcpu_id = vcpu->vcpu_id;
  107. e.vcpu_idx = vcpu->vcpu_idx;
  108. e.port = vcpu->arch.xen.timer_virq;
  109. e.priority = KVM_IRQ_ROUTING_XEN_EVTCHN_PRIO_2LEVEL;
  110. rc = kvm_xen_set_evtchn_fast(&e, vcpu->kvm);
  111. if (rc != -EWOULDBLOCK) {
  112. vcpu->arch.xen.timer_expires = 0;
  113. return HRTIMER_NORESTART;
  114. }
  115. atomic_inc(&vcpu->arch.xen.timer_pending);
  116. kvm_make_request(KVM_REQ_UNBLOCK, vcpu);
  117. kvm_vcpu_kick(vcpu);
  118. return HRTIMER_NORESTART;
  119. }
  120. static void kvm_xen_start_timer(struct kvm_vcpu *vcpu, u64 guest_abs,
  121. bool linux_wa)
  122. {
  123. int64_t kernel_now, delta;
  124. uint64_t guest_now;
  125. /*
  126. * The guest provides the requested timeout in absolute nanoseconds
  127. * of the KVM clock — as *it* sees it, based on the scaled TSC and
  128. * the pvclock information provided by KVM.
  129. *
  130. * The kernel doesn't support hrtimers based on CLOCK_MONOTONIC_RAW
  131. * so use CLOCK_MONOTONIC. In the timescales covered by timers, the
  132. * difference won't matter much as there is no cumulative effect.
  133. *
  134. * Calculate the time for some arbitrary point in time around "now"
  135. * in terms of both kvmclock and CLOCK_MONOTONIC. Calculate the
  136. * delta between the kvmclock "now" value and the guest's requested
  137. * timeout, apply the "Linux workaround" described below, and add
  138. * the resulting delta to the CLOCK_MONOTONIC "now" value, to get
  139. * the absolute CLOCK_MONOTONIC time at which the timer should
  140. * fire.
  141. */
  142. if (vcpu->arch.hv_clock.version && vcpu->kvm->arch.use_master_clock &&
  143. static_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
  144. uint64_t host_tsc, guest_tsc;
  145. if (!IS_ENABLED(CONFIG_64BIT) ||
  146. !kvm_get_monotonic_and_clockread(&kernel_now, &host_tsc)) {
  147. /*
  148. * Don't fall back to get_kvmclock_ns() because it's
  149. * broken; it has a systemic error in its results
  150. * because it scales directly from host TSC to
  151. * nanoseconds, and doesn't scale first to guest TSC
  152. * and *then* to nanoseconds as the guest does.
  153. *
  154. * There is a small error introduced here because time
  155. * continues to elapse between the ktime_get() and the
  156. * subsequent rdtsc(). But not the systemic drift due
  157. * to get_kvmclock_ns().
  158. */
  159. kernel_now = ktime_get(); /* This is CLOCK_MONOTONIC */
  160. host_tsc = rdtsc();
  161. }
  162. /* Calculate the guest kvmclock as the guest would do it. */
  163. guest_tsc = kvm_read_l1_tsc(vcpu, host_tsc);
  164. guest_now = __pvclock_read_cycles(&vcpu->arch.hv_clock,
  165. guest_tsc);
  166. } else {
  167. /*
  168. * Without CONSTANT_TSC, get_kvmclock_ns() is the only option.
  169. *
  170. * Also if the guest PV clock hasn't been set up yet, as is
  171. * likely to be the case during migration when the vCPU has
  172. * not been run yet. It would be possible to calculate the
  173. * scaling factors properly in that case but there's not much
  174. * point in doing so. The get_kvmclock_ns() drift accumulates
  175. * over time, so it's OK to use it at startup. Besides, on
  176. * migration there's going to be a little bit of skew in the
  177. * precise moment at which timers fire anyway. Often they'll
  178. * be in the "past" by the time the VM is running again after
  179. * migration.
  180. */
  181. guest_now = get_kvmclock_ns(vcpu->kvm);
  182. kernel_now = ktime_get();
  183. }
  184. delta = guest_abs - guest_now;
  185. /*
  186. * Xen has a 'Linux workaround' in do_set_timer_op() which checks for
  187. * negative absolute timeout values (caused by integer overflow), and
  188. * for values about 13 days in the future (2^50ns) which would be
  189. * caused by jiffies overflow. For those cases, Xen sets the timeout
  190. * 100ms in the future (not *too* soon, since if a guest really did
  191. * set a long timeout on purpose we don't want to keep churning CPU
  192. * time by waking it up). Emulate Xen's workaround when starting the
  193. * timer in response to __HYPERVISOR_set_timer_op.
  194. */
  195. if (linux_wa &&
  196. unlikely((int64_t)guest_abs < 0 ||
  197. (delta > 0 && (uint32_t) (delta >> 50) != 0))) {
  198. delta = 100 * NSEC_PER_MSEC;
  199. guest_abs = guest_now + delta;
  200. }
  201. /*
  202. * Avoid races with the old timer firing. Checking timer_expires
  203. * to avoid calling hrtimer_cancel() will only have false positives
  204. * so is fine.
  205. */
  206. if (vcpu->arch.xen.timer_expires)
  207. hrtimer_cancel(&vcpu->arch.xen.timer);
  208. atomic_set(&vcpu->arch.xen.timer_pending, 0);
  209. vcpu->arch.xen.timer_expires = guest_abs;
  210. if (delta <= 0)
  211. xen_timer_callback(&vcpu->arch.xen.timer);
  212. else
  213. hrtimer_start(&vcpu->arch.xen.timer,
  214. ktime_add_ns(kernel_now, delta),
  215. HRTIMER_MODE_ABS_HARD);
  216. }
  217. static void kvm_xen_stop_timer(struct kvm_vcpu *vcpu)
  218. {
  219. hrtimer_cancel(&vcpu->arch.xen.timer);
  220. vcpu->arch.xen.timer_expires = 0;
  221. atomic_set(&vcpu->arch.xen.timer_pending, 0);
  222. }
  223. static void kvm_xen_init_timer(struct kvm_vcpu *vcpu)
  224. {
  225. hrtimer_init(&vcpu->arch.xen.timer, CLOCK_MONOTONIC,
  226. HRTIMER_MODE_ABS_HARD);
  227. vcpu->arch.xen.timer.function = xen_timer_callback;
  228. }
  229. static void kvm_xen_update_runstate_guest(struct kvm_vcpu *v, bool atomic)
  230. {
  231. struct kvm_vcpu_xen *vx = &v->arch.xen;
  232. struct gfn_to_pfn_cache *gpc1 = &vx->runstate_cache;
  233. struct gfn_to_pfn_cache *gpc2 = &vx->runstate2_cache;
  234. size_t user_len, user_len1, user_len2;
  235. struct vcpu_runstate_info rs;
  236. unsigned long flags;
  237. size_t times_ofs;
  238. uint8_t *update_bit = NULL;
  239. uint64_t entry_time;
  240. uint64_t *rs_times;
  241. int *rs_state;
  242. /*
  243. * The only difference between 32-bit and 64-bit versions of the
  244. * runstate struct is the alignment of uint64_t in 32-bit, which
  245. * means that the 64-bit version has an additional 4 bytes of
  246. * padding after the first field 'state'. Let's be really really
  247. * paranoid about that, and matching it with our internal data
  248. * structures that we memcpy into it...
  249. */
  250. BUILD_BUG_ON(offsetof(struct vcpu_runstate_info, state) != 0);
  251. BUILD_BUG_ON(offsetof(struct compat_vcpu_runstate_info, state) != 0);
  252. BUILD_BUG_ON(sizeof(struct compat_vcpu_runstate_info) != 0x2c);
  253. #ifdef CONFIG_X86_64
  254. /*
  255. * The 64-bit structure has 4 bytes of padding before 'state_entry_time'
  256. * so each subsequent field is shifted by 4, and it's 4 bytes longer.
  257. */
  258. BUILD_BUG_ON(offsetof(struct vcpu_runstate_info, state_entry_time) !=
  259. offsetof(struct compat_vcpu_runstate_info, state_entry_time) + 4);
  260. BUILD_BUG_ON(offsetof(struct vcpu_runstate_info, time) !=
  261. offsetof(struct compat_vcpu_runstate_info, time) + 4);
  262. BUILD_BUG_ON(sizeof(struct vcpu_runstate_info) != 0x2c + 4);
  263. #endif
  264. /*
  265. * The state field is in the same place at the start of both structs,
  266. * and is the same size (int) as vx->current_runstate.
  267. */
  268. BUILD_BUG_ON(offsetof(struct vcpu_runstate_info, state) !=
  269. offsetof(struct compat_vcpu_runstate_info, state));
  270. BUILD_BUG_ON(sizeof_field(struct vcpu_runstate_info, state) !=
  271. sizeof(vx->current_runstate));
  272. BUILD_BUG_ON(sizeof_field(struct compat_vcpu_runstate_info, state) !=
  273. sizeof(vx->current_runstate));
  274. /*
  275. * The state_entry_time field is 64 bits in both versions, and the
  276. * XEN_RUNSTATE_UPDATE flag is in the top bit, which given that x86
  277. * is little-endian means that it's in the last *byte* of the word.
  278. * That detail is important later.
  279. */
  280. BUILD_BUG_ON(sizeof_field(struct vcpu_runstate_info, state_entry_time) !=
  281. sizeof(uint64_t));
  282. BUILD_BUG_ON(sizeof_field(struct compat_vcpu_runstate_info, state_entry_time) !=
  283. sizeof(uint64_t));
  284. BUILD_BUG_ON((XEN_RUNSTATE_UPDATE >> 56) != 0x80);
  285. /*
  286. * The time array is four 64-bit quantities in both versions, matching
  287. * the vx->runstate_times and immediately following state_entry_time.
  288. */
  289. BUILD_BUG_ON(offsetof(struct vcpu_runstate_info, state_entry_time) !=
  290. offsetof(struct vcpu_runstate_info, time) - sizeof(uint64_t));
  291. BUILD_BUG_ON(offsetof(struct compat_vcpu_runstate_info, state_entry_time) !=
  292. offsetof(struct compat_vcpu_runstate_info, time) - sizeof(uint64_t));
  293. BUILD_BUG_ON(sizeof_field(struct vcpu_runstate_info, time) !=
  294. sizeof_field(struct compat_vcpu_runstate_info, time));
  295. BUILD_BUG_ON(sizeof_field(struct vcpu_runstate_info, time) !=
  296. sizeof(vx->runstate_times));
  297. if (IS_ENABLED(CONFIG_64BIT) && v->kvm->arch.xen.long_mode) {
  298. user_len = sizeof(struct vcpu_runstate_info);
  299. times_ofs = offsetof(struct vcpu_runstate_info,
  300. state_entry_time);
  301. } else {
  302. user_len = sizeof(struct compat_vcpu_runstate_info);
  303. times_ofs = offsetof(struct compat_vcpu_runstate_info,
  304. state_entry_time);
  305. }
  306. /*
  307. * There are basically no alignment constraints. The guest can set it
  308. * up so it crosses from one page to the next, and at arbitrary byte
  309. * alignment (and the 32-bit ABI doesn't align the 64-bit integers
  310. * anyway, even if the overall struct had been 64-bit aligned).
  311. */
  312. if ((gpc1->gpa & ~PAGE_MASK) + user_len >= PAGE_SIZE) {
  313. user_len1 = PAGE_SIZE - (gpc1->gpa & ~PAGE_MASK);
  314. user_len2 = user_len - user_len1;
  315. } else {
  316. user_len1 = user_len;
  317. user_len2 = 0;
  318. }
  319. BUG_ON(user_len1 + user_len2 != user_len);
  320. retry:
  321. /*
  322. * Attempt to obtain the GPC lock on *both* (if there are two)
  323. * gfn_to_pfn caches that cover the region.
  324. */
  325. if (atomic) {
  326. local_irq_save(flags);
  327. if (!read_trylock(&gpc1->lock)) {
  328. local_irq_restore(flags);
  329. return;
  330. }
  331. } else {
  332. read_lock_irqsave(&gpc1->lock, flags);
  333. }
  334. while (!kvm_gpc_check(gpc1, user_len1)) {
  335. read_unlock_irqrestore(&gpc1->lock, flags);
  336. /* When invoked from kvm_sched_out() we cannot sleep */
  337. if (atomic)
  338. return;
  339. if (kvm_gpc_refresh(gpc1, user_len1))
  340. return;
  341. read_lock_irqsave(&gpc1->lock, flags);
  342. }
  343. if (likely(!user_len2)) {
  344. /*
  345. * Set up three pointers directly to the runstate_info
  346. * struct in the guest (via the GPC).
  347. *
  348. * • @rs_state → state field
  349. * • @rs_times → state_entry_time field.
  350. * • @update_bit → last byte of state_entry_time, which
  351. * contains the XEN_RUNSTATE_UPDATE bit.
  352. */
  353. rs_state = gpc1->khva;
  354. rs_times = gpc1->khva + times_ofs;
  355. if (v->kvm->arch.xen.runstate_update_flag)
  356. update_bit = ((void *)(&rs_times[1])) - 1;
  357. } else {
  358. /*
  359. * The guest's runstate_info is split across two pages and we
  360. * need to hold and validate both GPCs simultaneously. We can
  361. * declare a lock ordering GPC1 > GPC2 because nothing else
  362. * takes them more than one at a time. Set a subclass on the
  363. * gpc1 lock to make lockdep shut up about it.
  364. */
  365. lock_set_subclass(&gpc1->lock.dep_map, 1, _THIS_IP_);
  366. if (atomic) {
  367. if (!read_trylock(&gpc2->lock)) {
  368. read_unlock_irqrestore(&gpc1->lock, flags);
  369. return;
  370. }
  371. } else {
  372. read_lock(&gpc2->lock);
  373. }
  374. if (!kvm_gpc_check(gpc2, user_len2)) {
  375. read_unlock(&gpc2->lock);
  376. read_unlock_irqrestore(&gpc1->lock, flags);
  377. /* When invoked from kvm_sched_out() we cannot sleep */
  378. if (atomic)
  379. return;
  380. /*
  381. * Use kvm_gpc_activate() here because if the runstate
  382. * area was configured in 32-bit mode and only extends
  383. * to the second page now because the guest changed to
  384. * 64-bit mode, the second GPC won't have been set up.
  385. */
  386. if (kvm_gpc_activate(gpc2, gpc1->gpa + user_len1,
  387. user_len2))
  388. return;
  389. /*
  390. * We dropped the lock on GPC1 so we have to go all the
  391. * way back and revalidate that too.
  392. */
  393. goto retry;
  394. }
  395. /*
  396. * In this case, the runstate_info struct will be assembled on
  397. * the kernel stack (compat or not as appropriate) and will
  398. * be copied to GPC1/GPC2 with a dual memcpy. Set up the three
  399. * rs pointers accordingly.
  400. */
  401. rs_times = &rs.state_entry_time;
  402. /*
  403. * The rs_state pointer points to the start of what we'll
  404. * copy to the guest, which in the case of a compat guest
  405. * is the 32-bit field that the compiler thinks is padding.
  406. */
  407. rs_state = ((void *)rs_times) - times_ofs;
  408. /*
  409. * The update_bit is still directly in the guest memory,
  410. * via one GPC or the other.
  411. */
  412. if (v->kvm->arch.xen.runstate_update_flag) {
  413. if (user_len1 >= times_ofs + sizeof(uint64_t))
  414. update_bit = gpc1->khva + times_ofs +
  415. sizeof(uint64_t) - 1;
  416. else
  417. update_bit = gpc2->khva + times_ofs +
  418. sizeof(uint64_t) - 1 - user_len1;
  419. }
  420. #ifdef CONFIG_X86_64
  421. /*
  422. * Don't leak kernel memory through the padding in the 64-bit
  423. * version of the struct.
  424. */
  425. memset(&rs, 0, offsetof(struct vcpu_runstate_info, state_entry_time));
  426. #endif
  427. }
  428. /*
  429. * First, set the XEN_RUNSTATE_UPDATE bit in the top bit of the
  430. * state_entry_time field, directly in the guest. We need to set
  431. * that (and write-barrier) before writing to the rest of the
  432. * structure, and clear it last. Just as Xen does, we address the
  433. * single *byte* in which it resides because it might be in a
  434. * different cache line to the rest of the 64-bit word, due to
  435. * the (lack of) alignment constraints.
  436. */
  437. entry_time = vx->runstate_entry_time;
  438. if (update_bit) {
  439. entry_time |= XEN_RUNSTATE_UPDATE;
  440. *update_bit = (vx->runstate_entry_time | XEN_RUNSTATE_UPDATE) >> 56;
  441. smp_wmb();
  442. }
  443. /*
  444. * Now assemble the actual structure, either on our kernel stack
  445. * or directly in the guest according to how the rs_state and
  446. * rs_times pointers were set up above.
  447. */
  448. *rs_state = vx->current_runstate;
  449. rs_times[0] = entry_time;
  450. memcpy(rs_times + 1, vx->runstate_times, sizeof(vx->runstate_times));
  451. /* For the split case, we have to then copy it to the guest. */
  452. if (user_len2) {
  453. memcpy(gpc1->khva, rs_state, user_len1);
  454. memcpy(gpc2->khva, ((void *)rs_state) + user_len1, user_len2);
  455. }
  456. smp_wmb();
  457. /* Finally, clear the XEN_RUNSTATE_UPDATE bit. */
  458. if (update_bit) {
  459. entry_time &= ~XEN_RUNSTATE_UPDATE;
  460. *update_bit = entry_time >> 56;
  461. smp_wmb();
  462. }
  463. if (user_len2) {
  464. kvm_gpc_mark_dirty_in_slot(gpc2);
  465. read_unlock(&gpc2->lock);
  466. }
  467. kvm_gpc_mark_dirty_in_slot(gpc1);
  468. read_unlock_irqrestore(&gpc1->lock, flags);
  469. }
  470. void kvm_xen_update_runstate(struct kvm_vcpu *v, int state)
  471. {
  472. struct kvm_vcpu_xen *vx = &v->arch.xen;
  473. u64 now = get_kvmclock_ns(v->kvm);
  474. u64 delta_ns = now - vx->runstate_entry_time;
  475. u64 run_delay = current->sched_info.run_delay;
  476. if (unlikely(!vx->runstate_entry_time))
  477. vx->current_runstate = RUNSTATE_offline;
  478. /*
  479. * Time waiting for the scheduler isn't "stolen" if the
  480. * vCPU wasn't running anyway.
  481. */
  482. if (vx->current_runstate == RUNSTATE_running) {
  483. u64 steal_ns = run_delay - vx->last_steal;
  484. delta_ns -= steal_ns;
  485. vx->runstate_times[RUNSTATE_runnable] += steal_ns;
  486. }
  487. vx->last_steal = run_delay;
  488. vx->runstate_times[vx->current_runstate] += delta_ns;
  489. vx->current_runstate = state;
  490. vx->runstate_entry_time = now;
  491. if (vx->runstate_cache.active)
  492. kvm_xen_update_runstate_guest(v, state == RUNSTATE_runnable);
  493. }
  494. void kvm_xen_inject_vcpu_vector(struct kvm_vcpu *v)
  495. {
  496. struct kvm_lapic_irq irq = { };
  497. irq.dest_id = v->vcpu_id;
  498. irq.vector = v->arch.xen.upcall_vector;
  499. irq.dest_mode = APIC_DEST_PHYSICAL;
  500. irq.shorthand = APIC_DEST_NOSHORT;
  501. irq.delivery_mode = APIC_DM_FIXED;
  502. irq.level = 1;
  503. kvm_irq_delivery_to_apic(v->kvm, NULL, &irq, NULL);
  504. }
  505. /*
  506. * On event channel delivery, the vcpu_info may not have been accessible.
  507. * In that case, there are bits in vcpu->arch.xen.evtchn_pending_sel which
  508. * need to be marked into the vcpu_info (and evtchn_upcall_pending set).
  509. * Do so now that we can sleep in the context of the vCPU to bring the
  510. * page in, and refresh the pfn cache for it.
  511. */
  512. void kvm_xen_inject_pending_events(struct kvm_vcpu *v)
  513. {
  514. unsigned long evtchn_pending_sel = READ_ONCE(v->arch.xen.evtchn_pending_sel);
  515. struct gfn_to_pfn_cache *gpc = &v->arch.xen.vcpu_info_cache;
  516. unsigned long flags;
  517. if (!evtchn_pending_sel)
  518. return;
  519. /*
  520. * Yes, this is an open-coded loop. But that's just what put_user()
  521. * does anyway. Page it in and retry the instruction. We're just a
  522. * little more honest about it.
  523. */
  524. read_lock_irqsave(&gpc->lock, flags);
  525. while (!kvm_gpc_check(gpc, sizeof(struct vcpu_info))) {
  526. read_unlock_irqrestore(&gpc->lock, flags);
  527. if (kvm_gpc_refresh(gpc, sizeof(struct vcpu_info)))
  528. return;
  529. read_lock_irqsave(&gpc->lock, flags);
  530. }
  531. /* Now gpc->khva is a valid kernel address for the vcpu_info */
  532. if (IS_ENABLED(CONFIG_64BIT) && v->kvm->arch.xen.long_mode) {
  533. struct vcpu_info *vi = gpc->khva;
  534. asm volatile(LOCK_PREFIX "orq %0, %1\n"
  535. "notq %0\n"
  536. LOCK_PREFIX "andq %0, %2\n"
  537. : "=r" (evtchn_pending_sel),
  538. "+m" (vi->evtchn_pending_sel),
  539. "+m" (v->arch.xen.evtchn_pending_sel)
  540. : "0" (evtchn_pending_sel));
  541. WRITE_ONCE(vi->evtchn_upcall_pending, 1);
  542. } else {
  543. u32 evtchn_pending_sel32 = evtchn_pending_sel;
  544. struct compat_vcpu_info *vi = gpc->khva;
  545. asm volatile(LOCK_PREFIX "orl %0, %1\n"
  546. "notl %0\n"
  547. LOCK_PREFIX "andl %0, %2\n"
  548. : "=r" (evtchn_pending_sel32),
  549. "+m" (vi->evtchn_pending_sel),
  550. "+m" (v->arch.xen.evtchn_pending_sel)
  551. : "0" (evtchn_pending_sel32));
  552. WRITE_ONCE(vi->evtchn_upcall_pending, 1);
  553. }
  554. kvm_gpc_mark_dirty_in_slot(gpc);
  555. read_unlock_irqrestore(&gpc->lock, flags);
  556. /* For the per-vCPU lapic vector, deliver it as MSI. */
  557. if (v->arch.xen.upcall_vector)
  558. kvm_xen_inject_vcpu_vector(v);
  559. }
  560. int __kvm_xen_has_interrupt(struct kvm_vcpu *v)
  561. {
  562. struct gfn_to_pfn_cache *gpc = &v->arch.xen.vcpu_info_cache;
  563. unsigned long flags;
  564. u8 rc = 0;
  565. /*
  566. * If the global upcall vector (HVMIRQ_callback_vector) is set and
  567. * the vCPU's evtchn_upcall_pending flag is set, the IRQ is pending.
  568. */
  569. /* No need for compat handling here */
  570. BUILD_BUG_ON(offsetof(struct vcpu_info, evtchn_upcall_pending) !=
  571. offsetof(struct compat_vcpu_info, evtchn_upcall_pending));
  572. BUILD_BUG_ON(sizeof(rc) !=
  573. sizeof_field(struct vcpu_info, evtchn_upcall_pending));
  574. BUILD_BUG_ON(sizeof(rc) !=
  575. sizeof_field(struct compat_vcpu_info, evtchn_upcall_pending));
  576. read_lock_irqsave(&gpc->lock, flags);
  577. while (!kvm_gpc_check(gpc, sizeof(struct vcpu_info))) {
  578. read_unlock_irqrestore(&gpc->lock, flags);
  579. /*
  580. * This function gets called from kvm_vcpu_block() after setting the
  581. * task to TASK_INTERRUPTIBLE, to see if it needs to wake immediately
  582. * from a HLT. So we really mustn't sleep. If the page ended up absent
  583. * at that point, just return 1 in order to trigger an immediate wake,
  584. * and we'll end up getting called again from a context where we *can*
  585. * fault in the page and wait for it.
  586. */
  587. if (in_atomic() || !task_is_running(current))
  588. return 1;
  589. if (kvm_gpc_refresh(gpc, sizeof(struct vcpu_info))) {
  590. /*
  591. * If this failed, userspace has screwed up the
  592. * vcpu_info mapping. No interrupts for you.
  593. */
  594. return 0;
  595. }
  596. read_lock_irqsave(&gpc->lock, flags);
  597. }
  598. rc = ((struct vcpu_info *)gpc->khva)->evtchn_upcall_pending;
  599. read_unlock_irqrestore(&gpc->lock, flags);
  600. return rc;
  601. }
  602. int kvm_xen_hvm_set_attr(struct kvm *kvm, struct kvm_xen_hvm_attr *data)
  603. {
  604. int r = -ENOENT;
  605. switch (data->type) {
  606. case KVM_XEN_ATTR_TYPE_LONG_MODE:
  607. if (!IS_ENABLED(CONFIG_64BIT) && data->u.long_mode) {
  608. r = -EINVAL;
  609. } else {
  610. mutex_lock(&kvm->arch.xen.xen_lock);
  611. kvm->arch.xen.long_mode = !!data->u.long_mode;
  612. /*
  613. * Re-initialize shared_info to put the wallclock in the
  614. * correct place. Whilst it's not necessary to do this
  615. * unless the mode is actually changed, it does no harm
  616. * to make the call anyway.
  617. */
  618. r = kvm->arch.xen.shinfo_cache.active ?
  619. kvm_xen_shared_info_init(kvm) : 0;
  620. mutex_unlock(&kvm->arch.xen.xen_lock);
  621. }
  622. break;
  623. case KVM_XEN_ATTR_TYPE_SHARED_INFO:
  624. case KVM_XEN_ATTR_TYPE_SHARED_INFO_HVA: {
  625. int idx;
  626. mutex_lock(&kvm->arch.xen.xen_lock);
  627. idx = srcu_read_lock(&kvm->srcu);
  628. if (data->type == KVM_XEN_ATTR_TYPE_SHARED_INFO) {
  629. gfn_t gfn = data->u.shared_info.gfn;
  630. if (gfn == KVM_XEN_INVALID_GFN) {
  631. kvm_gpc_deactivate(&kvm->arch.xen.shinfo_cache);
  632. r = 0;
  633. } else {
  634. r = kvm_gpc_activate(&kvm->arch.xen.shinfo_cache,
  635. gfn_to_gpa(gfn), PAGE_SIZE);
  636. }
  637. } else {
  638. void __user * hva = u64_to_user_ptr(data->u.shared_info.hva);
  639. if (!PAGE_ALIGNED(hva)) {
  640. r = -EINVAL;
  641. } else if (!hva) {
  642. kvm_gpc_deactivate(&kvm->arch.xen.shinfo_cache);
  643. r = 0;
  644. } else {
  645. r = kvm_gpc_activate_hva(&kvm->arch.xen.shinfo_cache,
  646. (unsigned long)hva, PAGE_SIZE);
  647. }
  648. }
  649. srcu_read_unlock(&kvm->srcu, idx);
  650. if (!r && kvm->arch.xen.shinfo_cache.active)
  651. r = kvm_xen_shared_info_init(kvm);
  652. mutex_unlock(&kvm->arch.xen.xen_lock);
  653. break;
  654. }
  655. case KVM_XEN_ATTR_TYPE_UPCALL_VECTOR:
  656. if (data->u.vector && data->u.vector < 0x10)
  657. r = -EINVAL;
  658. else {
  659. mutex_lock(&kvm->arch.xen.xen_lock);
  660. kvm->arch.xen.upcall_vector = data->u.vector;
  661. mutex_unlock(&kvm->arch.xen.xen_lock);
  662. r = 0;
  663. }
  664. break;
  665. case KVM_XEN_ATTR_TYPE_EVTCHN:
  666. r = kvm_xen_setattr_evtchn(kvm, data);
  667. break;
  668. case KVM_XEN_ATTR_TYPE_XEN_VERSION:
  669. mutex_lock(&kvm->arch.xen.xen_lock);
  670. kvm->arch.xen.xen_version = data->u.xen_version;
  671. mutex_unlock(&kvm->arch.xen.xen_lock);
  672. r = 0;
  673. break;
  674. case KVM_XEN_ATTR_TYPE_RUNSTATE_UPDATE_FLAG:
  675. if (!sched_info_on()) {
  676. r = -EOPNOTSUPP;
  677. break;
  678. }
  679. mutex_lock(&kvm->arch.xen.xen_lock);
  680. kvm->arch.xen.runstate_update_flag = !!data->u.runstate_update_flag;
  681. mutex_unlock(&kvm->arch.xen.xen_lock);
  682. r = 0;
  683. break;
  684. default:
  685. break;
  686. }
  687. return r;
  688. }
  689. int kvm_xen_hvm_get_attr(struct kvm *kvm, struct kvm_xen_hvm_attr *data)
  690. {
  691. int r = -ENOENT;
  692. mutex_lock(&kvm->arch.xen.xen_lock);
  693. switch (data->type) {
  694. case KVM_XEN_ATTR_TYPE_LONG_MODE:
  695. data->u.long_mode = kvm->arch.xen.long_mode;
  696. r = 0;
  697. break;
  698. case KVM_XEN_ATTR_TYPE_SHARED_INFO:
  699. if (kvm_gpc_is_gpa_active(&kvm->arch.xen.shinfo_cache))
  700. data->u.shared_info.gfn = gpa_to_gfn(kvm->arch.xen.shinfo_cache.gpa);
  701. else
  702. data->u.shared_info.gfn = KVM_XEN_INVALID_GFN;
  703. r = 0;
  704. break;
  705. case KVM_XEN_ATTR_TYPE_SHARED_INFO_HVA:
  706. if (kvm_gpc_is_hva_active(&kvm->arch.xen.shinfo_cache))
  707. data->u.shared_info.hva = kvm->arch.xen.shinfo_cache.uhva;
  708. else
  709. data->u.shared_info.hva = 0;
  710. r = 0;
  711. break;
  712. case KVM_XEN_ATTR_TYPE_UPCALL_VECTOR:
  713. data->u.vector = kvm->arch.xen.upcall_vector;
  714. r = 0;
  715. break;
  716. case KVM_XEN_ATTR_TYPE_XEN_VERSION:
  717. data->u.xen_version = kvm->arch.xen.xen_version;
  718. r = 0;
  719. break;
  720. case KVM_XEN_ATTR_TYPE_RUNSTATE_UPDATE_FLAG:
  721. if (!sched_info_on()) {
  722. r = -EOPNOTSUPP;
  723. break;
  724. }
  725. data->u.runstate_update_flag = kvm->arch.xen.runstate_update_flag;
  726. r = 0;
  727. break;
  728. default:
  729. break;
  730. }
  731. mutex_unlock(&kvm->arch.xen.xen_lock);
  732. return r;
  733. }
  734. int kvm_xen_vcpu_set_attr(struct kvm_vcpu *vcpu, struct kvm_xen_vcpu_attr *data)
  735. {
  736. int idx, r = -ENOENT;
  737. mutex_lock(&vcpu->kvm->arch.xen.xen_lock);
  738. idx = srcu_read_lock(&vcpu->kvm->srcu);
  739. switch (data->type) {
  740. case KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO:
  741. case KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO_HVA:
  742. /* No compat necessary here. */
  743. BUILD_BUG_ON(sizeof(struct vcpu_info) !=
  744. sizeof(struct compat_vcpu_info));
  745. BUILD_BUG_ON(offsetof(struct vcpu_info, time) !=
  746. offsetof(struct compat_vcpu_info, time));
  747. if (data->type == KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO) {
  748. if (data->u.gpa == KVM_XEN_INVALID_GPA) {
  749. kvm_gpc_deactivate(&vcpu->arch.xen.vcpu_info_cache);
  750. r = 0;
  751. break;
  752. }
  753. r = kvm_gpc_activate(&vcpu->arch.xen.vcpu_info_cache,
  754. data->u.gpa, sizeof(struct vcpu_info));
  755. } else {
  756. if (data->u.hva == 0) {
  757. kvm_gpc_deactivate(&vcpu->arch.xen.vcpu_info_cache);
  758. r = 0;
  759. break;
  760. }
  761. r = kvm_gpc_activate_hva(&vcpu->arch.xen.vcpu_info_cache,
  762. data->u.hva, sizeof(struct vcpu_info));
  763. }
  764. if (!r)
  765. kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
  766. break;
  767. case KVM_XEN_VCPU_ATTR_TYPE_VCPU_TIME_INFO:
  768. if (data->u.gpa == KVM_XEN_INVALID_GPA) {
  769. kvm_gpc_deactivate(&vcpu->arch.xen.vcpu_time_info_cache);
  770. r = 0;
  771. break;
  772. }
  773. r = kvm_gpc_activate(&vcpu->arch.xen.vcpu_time_info_cache,
  774. data->u.gpa,
  775. sizeof(struct pvclock_vcpu_time_info));
  776. if (!r)
  777. kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
  778. break;
  779. case KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_ADDR: {
  780. size_t sz, sz1, sz2;
  781. if (!sched_info_on()) {
  782. r = -EOPNOTSUPP;
  783. break;
  784. }
  785. if (data->u.gpa == KVM_XEN_INVALID_GPA) {
  786. r = 0;
  787. deactivate_out:
  788. kvm_gpc_deactivate(&vcpu->arch.xen.runstate_cache);
  789. kvm_gpc_deactivate(&vcpu->arch.xen.runstate2_cache);
  790. break;
  791. }
  792. /*
  793. * If the guest switches to 64-bit mode after setting the runstate
  794. * address, that's actually OK. kvm_xen_update_runstate_guest()
  795. * will cope.
  796. */
  797. if (IS_ENABLED(CONFIG_64BIT) && vcpu->kvm->arch.xen.long_mode)
  798. sz = sizeof(struct vcpu_runstate_info);
  799. else
  800. sz = sizeof(struct compat_vcpu_runstate_info);
  801. /* How much fits in the (first) page? */
  802. sz1 = PAGE_SIZE - (data->u.gpa & ~PAGE_MASK);
  803. r = kvm_gpc_activate(&vcpu->arch.xen.runstate_cache,
  804. data->u.gpa, sz1);
  805. if (r)
  806. goto deactivate_out;
  807. /* Either map the second page, or deactivate the second GPC */
  808. if (sz1 >= sz) {
  809. kvm_gpc_deactivate(&vcpu->arch.xen.runstate2_cache);
  810. } else {
  811. sz2 = sz - sz1;
  812. BUG_ON((data->u.gpa + sz1) & ~PAGE_MASK);
  813. r = kvm_gpc_activate(&vcpu->arch.xen.runstate2_cache,
  814. data->u.gpa + sz1, sz2);
  815. if (r)
  816. goto deactivate_out;
  817. }
  818. kvm_xen_update_runstate_guest(vcpu, false);
  819. break;
  820. }
  821. case KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_CURRENT:
  822. if (!sched_info_on()) {
  823. r = -EOPNOTSUPP;
  824. break;
  825. }
  826. if (data->u.runstate.state > RUNSTATE_offline) {
  827. r = -EINVAL;
  828. break;
  829. }
  830. kvm_xen_update_runstate(vcpu, data->u.runstate.state);
  831. r = 0;
  832. break;
  833. case KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_DATA:
  834. if (!sched_info_on()) {
  835. r = -EOPNOTSUPP;
  836. break;
  837. }
  838. if (data->u.runstate.state > RUNSTATE_offline) {
  839. r = -EINVAL;
  840. break;
  841. }
  842. if (data->u.runstate.state_entry_time !=
  843. (data->u.runstate.time_running +
  844. data->u.runstate.time_runnable +
  845. data->u.runstate.time_blocked +
  846. data->u.runstate.time_offline)) {
  847. r = -EINVAL;
  848. break;
  849. }
  850. if (get_kvmclock_ns(vcpu->kvm) <
  851. data->u.runstate.state_entry_time) {
  852. r = -EINVAL;
  853. break;
  854. }
  855. vcpu->arch.xen.current_runstate = data->u.runstate.state;
  856. vcpu->arch.xen.runstate_entry_time =
  857. data->u.runstate.state_entry_time;
  858. vcpu->arch.xen.runstate_times[RUNSTATE_running] =
  859. data->u.runstate.time_running;
  860. vcpu->arch.xen.runstate_times[RUNSTATE_runnable] =
  861. data->u.runstate.time_runnable;
  862. vcpu->arch.xen.runstate_times[RUNSTATE_blocked] =
  863. data->u.runstate.time_blocked;
  864. vcpu->arch.xen.runstate_times[RUNSTATE_offline] =
  865. data->u.runstate.time_offline;
  866. vcpu->arch.xen.last_steal = current->sched_info.run_delay;
  867. r = 0;
  868. break;
  869. case KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_ADJUST:
  870. if (!sched_info_on()) {
  871. r = -EOPNOTSUPP;
  872. break;
  873. }
  874. if (data->u.runstate.state > RUNSTATE_offline &&
  875. data->u.runstate.state != (u64)-1) {
  876. r = -EINVAL;
  877. break;
  878. }
  879. /* The adjustment must add up */
  880. if (data->u.runstate.state_entry_time !=
  881. (data->u.runstate.time_running +
  882. data->u.runstate.time_runnable +
  883. data->u.runstate.time_blocked +
  884. data->u.runstate.time_offline)) {
  885. r = -EINVAL;
  886. break;
  887. }
  888. if (get_kvmclock_ns(vcpu->kvm) <
  889. (vcpu->arch.xen.runstate_entry_time +
  890. data->u.runstate.state_entry_time)) {
  891. r = -EINVAL;
  892. break;
  893. }
  894. vcpu->arch.xen.runstate_entry_time +=
  895. data->u.runstate.state_entry_time;
  896. vcpu->arch.xen.runstate_times[RUNSTATE_running] +=
  897. data->u.runstate.time_running;
  898. vcpu->arch.xen.runstate_times[RUNSTATE_runnable] +=
  899. data->u.runstate.time_runnable;
  900. vcpu->arch.xen.runstate_times[RUNSTATE_blocked] +=
  901. data->u.runstate.time_blocked;
  902. vcpu->arch.xen.runstate_times[RUNSTATE_offline] +=
  903. data->u.runstate.time_offline;
  904. if (data->u.runstate.state <= RUNSTATE_offline)
  905. kvm_xen_update_runstate(vcpu, data->u.runstate.state);
  906. else if (vcpu->arch.xen.runstate_cache.active)
  907. kvm_xen_update_runstate_guest(vcpu, false);
  908. r = 0;
  909. break;
  910. case KVM_XEN_VCPU_ATTR_TYPE_VCPU_ID:
  911. if (data->u.vcpu_id >= KVM_MAX_VCPUS)
  912. r = -EINVAL;
  913. else {
  914. vcpu->arch.xen.vcpu_id = data->u.vcpu_id;
  915. r = 0;
  916. }
  917. break;
  918. case KVM_XEN_VCPU_ATTR_TYPE_TIMER:
  919. if (data->u.timer.port &&
  920. data->u.timer.priority != KVM_IRQ_ROUTING_XEN_EVTCHN_PRIO_2LEVEL) {
  921. r = -EINVAL;
  922. break;
  923. }
  924. if (!vcpu->arch.xen.timer.function)
  925. kvm_xen_init_timer(vcpu);
  926. /* Stop the timer (if it's running) before changing the vector */
  927. kvm_xen_stop_timer(vcpu);
  928. vcpu->arch.xen.timer_virq = data->u.timer.port;
  929. /* Start the timer if the new value has a valid vector+expiry. */
  930. if (data->u.timer.port && data->u.timer.expires_ns)
  931. kvm_xen_start_timer(vcpu, data->u.timer.expires_ns, false);
  932. r = 0;
  933. break;
  934. case KVM_XEN_VCPU_ATTR_TYPE_UPCALL_VECTOR:
  935. if (data->u.vector && data->u.vector < 0x10)
  936. r = -EINVAL;
  937. else {
  938. vcpu->arch.xen.upcall_vector = data->u.vector;
  939. r = 0;
  940. }
  941. break;
  942. default:
  943. break;
  944. }
  945. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  946. mutex_unlock(&vcpu->kvm->arch.xen.xen_lock);
  947. return r;
  948. }
  949. int kvm_xen_vcpu_get_attr(struct kvm_vcpu *vcpu, struct kvm_xen_vcpu_attr *data)
  950. {
  951. int r = -ENOENT;
  952. mutex_lock(&vcpu->kvm->arch.xen.xen_lock);
  953. switch (data->type) {
  954. case KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO:
  955. if (kvm_gpc_is_gpa_active(&vcpu->arch.xen.vcpu_info_cache))
  956. data->u.gpa = vcpu->arch.xen.vcpu_info_cache.gpa;
  957. else
  958. data->u.gpa = KVM_XEN_INVALID_GPA;
  959. r = 0;
  960. break;
  961. case KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO_HVA:
  962. if (kvm_gpc_is_hva_active(&vcpu->arch.xen.vcpu_info_cache))
  963. data->u.hva = vcpu->arch.xen.vcpu_info_cache.uhva;
  964. else
  965. data->u.hva = 0;
  966. r = 0;
  967. break;
  968. case KVM_XEN_VCPU_ATTR_TYPE_VCPU_TIME_INFO:
  969. if (vcpu->arch.xen.vcpu_time_info_cache.active)
  970. data->u.gpa = vcpu->arch.xen.vcpu_time_info_cache.gpa;
  971. else
  972. data->u.gpa = KVM_XEN_INVALID_GPA;
  973. r = 0;
  974. break;
  975. case KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_ADDR:
  976. if (!sched_info_on()) {
  977. r = -EOPNOTSUPP;
  978. break;
  979. }
  980. if (vcpu->arch.xen.runstate_cache.active) {
  981. data->u.gpa = vcpu->arch.xen.runstate_cache.gpa;
  982. r = 0;
  983. }
  984. break;
  985. case KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_CURRENT:
  986. if (!sched_info_on()) {
  987. r = -EOPNOTSUPP;
  988. break;
  989. }
  990. data->u.runstate.state = vcpu->arch.xen.current_runstate;
  991. r = 0;
  992. break;
  993. case KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_DATA:
  994. if (!sched_info_on()) {
  995. r = -EOPNOTSUPP;
  996. break;
  997. }
  998. data->u.runstate.state = vcpu->arch.xen.current_runstate;
  999. data->u.runstate.state_entry_time =
  1000. vcpu->arch.xen.runstate_entry_time;
  1001. data->u.runstate.time_running =
  1002. vcpu->arch.xen.runstate_times[RUNSTATE_running];
  1003. data->u.runstate.time_runnable =
  1004. vcpu->arch.xen.runstate_times[RUNSTATE_runnable];
  1005. data->u.runstate.time_blocked =
  1006. vcpu->arch.xen.runstate_times[RUNSTATE_blocked];
  1007. data->u.runstate.time_offline =
  1008. vcpu->arch.xen.runstate_times[RUNSTATE_offline];
  1009. r = 0;
  1010. break;
  1011. case KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_ADJUST:
  1012. r = -EINVAL;
  1013. break;
  1014. case KVM_XEN_VCPU_ATTR_TYPE_VCPU_ID:
  1015. data->u.vcpu_id = vcpu->arch.xen.vcpu_id;
  1016. r = 0;
  1017. break;
  1018. case KVM_XEN_VCPU_ATTR_TYPE_TIMER:
  1019. /*
  1020. * Ensure a consistent snapshot of state is captured, with a
  1021. * timer either being pending, or the event channel delivered
  1022. * to the corresponding bit in the shared_info. Not still
  1023. * lurking in the timer_pending flag for deferred delivery.
  1024. * Purely as an optimisation, if the timer_expires field is
  1025. * zero, that means the timer isn't active (or even in the
  1026. * timer_pending flag) and there is no need to cancel it.
  1027. */
  1028. if (vcpu->arch.xen.timer_expires) {
  1029. hrtimer_cancel(&vcpu->arch.xen.timer);
  1030. kvm_xen_inject_timer_irqs(vcpu);
  1031. }
  1032. data->u.timer.port = vcpu->arch.xen.timer_virq;
  1033. data->u.timer.priority = KVM_IRQ_ROUTING_XEN_EVTCHN_PRIO_2LEVEL;
  1034. data->u.timer.expires_ns = vcpu->arch.xen.timer_expires;
  1035. /*
  1036. * The hrtimer may trigger and raise the IRQ immediately,
  1037. * while the returned state causes it to be set up and
  1038. * raised again on the destination system after migration.
  1039. * That's fine, as the guest won't even have had a chance
  1040. * to run and handle the interrupt. Asserting an already
  1041. * pending event channel is idempotent.
  1042. */
  1043. if (vcpu->arch.xen.timer_expires)
  1044. hrtimer_start_expires(&vcpu->arch.xen.timer,
  1045. HRTIMER_MODE_ABS_HARD);
  1046. r = 0;
  1047. break;
  1048. case KVM_XEN_VCPU_ATTR_TYPE_UPCALL_VECTOR:
  1049. data->u.vector = vcpu->arch.xen.upcall_vector;
  1050. r = 0;
  1051. break;
  1052. default:
  1053. break;
  1054. }
  1055. mutex_unlock(&vcpu->kvm->arch.xen.xen_lock);
  1056. return r;
  1057. }
  1058. int kvm_xen_write_hypercall_page(struct kvm_vcpu *vcpu, u64 data)
  1059. {
  1060. struct kvm *kvm = vcpu->kvm;
  1061. u32 page_num = data & ~PAGE_MASK;
  1062. u64 page_addr = data & PAGE_MASK;
  1063. bool lm = is_long_mode(vcpu);
  1064. int r = 0;
  1065. mutex_lock(&kvm->arch.xen.xen_lock);
  1066. if (kvm->arch.xen.long_mode != lm) {
  1067. kvm->arch.xen.long_mode = lm;
  1068. /*
  1069. * Re-initialize shared_info to put the wallclock in the
  1070. * correct place.
  1071. */
  1072. if (kvm->arch.xen.shinfo_cache.active &&
  1073. kvm_xen_shared_info_init(kvm))
  1074. r = 1;
  1075. }
  1076. mutex_unlock(&kvm->arch.xen.xen_lock);
  1077. if (r)
  1078. return r;
  1079. /*
  1080. * If Xen hypercall intercept is enabled, fill the hypercall
  1081. * page with VMCALL/VMMCALL instructions since that's what
  1082. * we catch. Else the VMM has provided the hypercall pages
  1083. * with instructions of its own choosing, so use those.
  1084. */
  1085. if (kvm_xen_hypercall_enabled(kvm)) {
  1086. u8 instructions[32];
  1087. int i;
  1088. if (page_num)
  1089. return 1;
  1090. /* mov imm32, %eax */
  1091. instructions[0] = 0xb8;
  1092. /* vmcall / vmmcall */
  1093. kvm_x86_call(patch_hypercall)(vcpu, instructions + 5);
  1094. /* ret */
  1095. instructions[8] = 0xc3;
  1096. /* int3 to pad */
  1097. memset(instructions + 9, 0xcc, sizeof(instructions) - 9);
  1098. for (i = 0; i < PAGE_SIZE / sizeof(instructions); i++) {
  1099. *(u32 *)&instructions[1] = i;
  1100. if (kvm_vcpu_write_guest(vcpu,
  1101. page_addr + (i * sizeof(instructions)),
  1102. instructions, sizeof(instructions)))
  1103. return 1;
  1104. }
  1105. } else {
  1106. /*
  1107. * Note, truncation is a non-issue as 'lm' is guaranteed to be
  1108. * false for a 32-bit kernel, i.e. when hva_t is only 4 bytes.
  1109. */
  1110. hva_t blob_addr = lm ? kvm->arch.xen_hvm_config.blob_addr_64
  1111. : kvm->arch.xen_hvm_config.blob_addr_32;
  1112. u8 blob_size = lm ? kvm->arch.xen_hvm_config.blob_size_64
  1113. : kvm->arch.xen_hvm_config.blob_size_32;
  1114. u8 *page;
  1115. int ret;
  1116. if (page_num >= blob_size)
  1117. return 1;
  1118. blob_addr += page_num * PAGE_SIZE;
  1119. page = memdup_user((u8 __user *)blob_addr, PAGE_SIZE);
  1120. if (IS_ERR(page))
  1121. return PTR_ERR(page);
  1122. ret = kvm_vcpu_write_guest(vcpu, page_addr, page, PAGE_SIZE);
  1123. kfree(page);
  1124. if (ret)
  1125. return 1;
  1126. }
  1127. return 0;
  1128. }
  1129. int kvm_xen_hvm_config(struct kvm *kvm, struct kvm_xen_hvm_config *xhc)
  1130. {
  1131. /* Only some feature flags need to be *enabled* by userspace */
  1132. u32 permitted_flags = KVM_XEN_HVM_CONFIG_INTERCEPT_HCALL |
  1133. KVM_XEN_HVM_CONFIG_EVTCHN_SEND |
  1134. KVM_XEN_HVM_CONFIG_PVCLOCK_TSC_UNSTABLE;
  1135. u32 old_flags;
  1136. if (xhc->flags & ~permitted_flags)
  1137. return -EINVAL;
  1138. /*
  1139. * With hypercall interception the kernel generates its own
  1140. * hypercall page so it must not be provided.
  1141. */
  1142. if ((xhc->flags & KVM_XEN_HVM_CONFIG_INTERCEPT_HCALL) &&
  1143. (xhc->blob_addr_32 || xhc->blob_addr_64 ||
  1144. xhc->blob_size_32 || xhc->blob_size_64))
  1145. return -EINVAL;
  1146. mutex_lock(&kvm->arch.xen.xen_lock);
  1147. if (xhc->msr && !kvm->arch.xen_hvm_config.msr)
  1148. static_branch_inc(&kvm_xen_enabled.key);
  1149. else if (!xhc->msr && kvm->arch.xen_hvm_config.msr)
  1150. static_branch_slow_dec_deferred(&kvm_xen_enabled);
  1151. old_flags = kvm->arch.xen_hvm_config.flags;
  1152. memcpy(&kvm->arch.xen_hvm_config, xhc, sizeof(*xhc));
  1153. mutex_unlock(&kvm->arch.xen.xen_lock);
  1154. if ((old_flags ^ xhc->flags) & KVM_XEN_HVM_CONFIG_PVCLOCK_TSC_UNSTABLE)
  1155. kvm_make_all_cpus_request(kvm, KVM_REQ_CLOCK_UPDATE);
  1156. return 0;
  1157. }
  1158. static int kvm_xen_hypercall_set_result(struct kvm_vcpu *vcpu, u64 result)
  1159. {
  1160. kvm_rax_write(vcpu, result);
  1161. return kvm_skip_emulated_instruction(vcpu);
  1162. }
  1163. static int kvm_xen_hypercall_complete_userspace(struct kvm_vcpu *vcpu)
  1164. {
  1165. struct kvm_run *run = vcpu->run;
  1166. if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.xen.hypercall_rip)))
  1167. return 1;
  1168. return kvm_xen_hypercall_set_result(vcpu, run->xen.u.hcall.result);
  1169. }
  1170. static inline int max_evtchn_port(struct kvm *kvm)
  1171. {
  1172. if (IS_ENABLED(CONFIG_64BIT) && kvm->arch.xen.long_mode)
  1173. return EVTCHN_2L_NR_CHANNELS;
  1174. else
  1175. return COMPAT_EVTCHN_2L_NR_CHANNELS;
  1176. }
  1177. static bool wait_pending_event(struct kvm_vcpu *vcpu, int nr_ports,
  1178. evtchn_port_t *ports)
  1179. {
  1180. struct kvm *kvm = vcpu->kvm;
  1181. struct gfn_to_pfn_cache *gpc = &kvm->arch.xen.shinfo_cache;
  1182. unsigned long *pending_bits;
  1183. unsigned long flags;
  1184. bool ret = true;
  1185. int idx, i;
  1186. idx = srcu_read_lock(&kvm->srcu);
  1187. read_lock_irqsave(&gpc->lock, flags);
  1188. if (!kvm_gpc_check(gpc, PAGE_SIZE))
  1189. goto out_rcu;
  1190. ret = false;
  1191. if (IS_ENABLED(CONFIG_64BIT) && kvm->arch.xen.long_mode) {
  1192. struct shared_info *shinfo = gpc->khva;
  1193. pending_bits = (unsigned long *)&shinfo->evtchn_pending;
  1194. } else {
  1195. struct compat_shared_info *shinfo = gpc->khva;
  1196. pending_bits = (unsigned long *)&shinfo->evtchn_pending;
  1197. }
  1198. for (i = 0; i < nr_ports; i++) {
  1199. if (test_bit(ports[i], pending_bits)) {
  1200. ret = true;
  1201. break;
  1202. }
  1203. }
  1204. out_rcu:
  1205. read_unlock_irqrestore(&gpc->lock, flags);
  1206. srcu_read_unlock(&kvm->srcu, idx);
  1207. return ret;
  1208. }
  1209. static bool kvm_xen_schedop_poll(struct kvm_vcpu *vcpu, bool longmode,
  1210. u64 param, u64 *r)
  1211. {
  1212. struct sched_poll sched_poll;
  1213. evtchn_port_t port, *ports;
  1214. struct x86_exception e;
  1215. int i;
  1216. if (!lapic_in_kernel(vcpu) ||
  1217. !(vcpu->kvm->arch.xen_hvm_config.flags & KVM_XEN_HVM_CONFIG_EVTCHN_SEND))
  1218. return false;
  1219. if (IS_ENABLED(CONFIG_64BIT) && !longmode) {
  1220. struct compat_sched_poll sp32;
  1221. /* Sanity check that the compat struct definition is correct */
  1222. BUILD_BUG_ON(sizeof(sp32) != 16);
  1223. if (kvm_read_guest_virt(vcpu, param, &sp32, sizeof(sp32), &e)) {
  1224. *r = -EFAULT;
  1225. return true;
  1226. }
  1227. /*
  1228. * This is a 32-bit pointer to an array of evtchn_port_t which
  1229. * are uint32_t, so once it's converted no further compat
  1230. * handling is needed.
  1231. */
  1232. sched_poll.ports = (void *)(unsigned long)(sp32.ports);
  1233. sched_poll.nr_ports = sp32.nr_ports;
  1234. sched_poll.timeout = sp32.timeout;
  1235. } else {
  1236. if (kvm_read_guest_virt(vcpu, param, &sched_poll,
  1237. sizeof(sched_poll), &e)) {
  1238. *r = -EFAULT;
  1239. return true;
  1240. }
  1241. }
  1242. if (unlikely(sched_poll.nr_ports > 1)) {
  1243. /* Xen (unofficially) limits number of pollers to 128 */
  1244. if (sched_poll.nr_ports > 128) {
  1245. *r = -EINVAL;
  1246. return true;
  1247. }
  1248. ports = kmalloc_array(sched_poll.nr_ports,
  1249. sizeof(*ports), GFP_KERNEL);
  1250. if (!ports) {
  1251. *r = -ENOMEM;
  1252. return true;
  1253. }
  1254. } else
  1255. ports = &port;
  1256. if (kvm_read_guest_virt(vcpu, (gva_t)sched_poll.ports, ports,
  1257. sched_poll.nr_ports * sizeof(*ports), &e)) {
  1258. *r = -EFAULT;
  1259. return true;
  1260. }
  1261. for (i = 0; i < sched_poll.nr_ports; i++) {
  1262. if (ports[i] >= max_evtchn_port(vcpu->kvm)) {
  1263. *r = -EINVAL;
  1264. goto out;
  1265. }
  1266. }
  1267. if (sched_poll.nr_ports == 1)
  1268. vcpu->arch.xen.poll_evtchn = port;
  1269. else
  1270. vcpu->arch.xen.poll_evtchn = -1;
  1271. set_bit(vcpu->vcpu_idx, vcpu->kvm->arch.xen.poll_mask);
  1272. if (!wait_pending_event(vcpu, sched_poll.nr_ports, ports)) {
  1273. vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
  1274. if (sched_poll.timeout)
  1275. mod_timer(&vcpu->arch.xen.poll_timer,
  1276. jiffies + nsecs_to_jiffies(sched_poll.timeout));
  1277. kvm_vcpu_halt(vcpu);
  1278. if (sched_poll.timeout)
  1279. del_timer(&vcpu->arch.xen.poll_timer);
  1280. vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
  1281. }
  1282. vcpu->arch.xen.poll_evtchn = 0;
  1283. *r = 0;
  1284. out:
  1285. /* Really, this is only needed in case of timeout */
  1286. clear_bit(vcpu->vcpu_idx, vcpu->kvm->arch.xen.poll_mask);
  1287. if (unlikely(sched_poll.nr_ports > 1))
  1288. kfree(ports);
  1289. return true;
  1290. }
  1291. static void cancel_evtchn_poll(struct timer_list *t)
  1292. {
  1293. struct kvm_vcpu *vcpu = from_timer(vcpu, t, arch.xen.poll_timer);
  1294. kvm_make_request(KVM_REQ_UNBLOCK, vcpu);
  1295. kvm_vcpu_kick(vcpu);
  1296. }
  1297. static bool kvm_xen_hcall_sched_op(struct kvm_vcpu *vcpu, bool longmode,
  1298. int cmd, u64 param, u64 *r)
  1299. {
  1300. switch (cmd) {
  1301. case SCHEDOP_poll:
  1302. if (kvm_xen_schedop_poll(vcpu, longmode, param, r))
  1303. return true;
  1304. fallthrough;
  1305. case SCHEDOP_yield:
  1306. kvm_vcpu_on_spin(vcpu, true);
  1307. *r = 0;
  1308. return true;
  1309. default:
  1310. break;
  1311. }
  1312. return false;
  1313. }
  1314. struct compat_vcpu_set_singleshot_timer {
  1315. uint64_t timeout_abs_ns;
  1316. uint32_t flags;
  1317. } __attribute__((packed));
  1318. static bool kvm_xen_hcall_vcpu_op(struct kvm_vcpu *vcpu, bool longmode, int cmd,
  1319. int vcpu_id, u64 param, u64 *r)
  1320. {
  1321. struct vcpu_set_singleshot_timer oneshot;
  1322. struct x86_exception e;
  1323. if (!kvm_xen_timer_enabled(vcpu))
  1324. return false;
  1325. switch (cmd) {
  1326. case VCPUOP_set_singleshot_timer:
  1327. if (vcpu->arch.xen.vcpu_id != vcpu_id) {
  1328. *r = -EINVAL;
  1329. return true;
  1330. }
  1331. /*
  1332. * The only difference for 32-bit compat is the 4 bytes of
  1333. * padding after the interesting part of the structure. So
  1334. * for a faithful emulation of Xen we have to *try* to copy
  1335. * the padding and return -EFAULT if we can't. Otherwise we
  1336. * might as well just have copied the 12-byte 32-bit struct.
  1337. */
  1338. BUILD_BUG_ON(offsetof(struct compat_vcpu_set_singleshot_timer, timeout_abs_ns) !=
  1339. offsetof(struct vcpu_set_singleshot_timer, timeout_abs_ns));
  1340. BUILD_BUG_ON(sizeof_field(struct compat_vcpu_set_singleshot_timer, timeout_abs_ns) !=
  1341. sizeof_field(struct vcpu_set_singleshot_timer, timeout_abs_ns));
  1342. BUILD_BUG_ON(offsetof(struct compat_vcpu_set_singleshot_timer, flags) !=
  1343. offsetof(struct vcpu_set_singleshot_timer, flags));
  1344. BUILD_BUG_ON(sizeof_field(struct compat_vcpu_set_singleshot_timer, flags) !=
  1345. sizeof_field(struct vcpu_set_singleshot_timer, flags));
  1346. if (kvm_read_guest_virt(vcpu, param, &oneshot, longmode ? sizeof(oneshot) :
  1347. sizeof(struct compat_vcpu_set_singleshot_timer), &e)) {
  1348. *r = -EFAULT;
  1349. return true;
  1350. }
  1351. kvm_xen_start_timer(vcpu, oneshot.timeout_abs_ns, false);
  1352. *r = 0;
  1353. return true;
  1354. case VCPUOP_stop_singleshot_timer:
  1355. if (vcpu->arch.xen.vcpu_id != vcpu_id) {
  1356. *r = -EINVAL;
  1357. return true;
  1358. }
  1359. kvm_xen_stop_timer(vcpu);
  1360. *r = 0;
  1361. return true;
  1362. }
  1363. return false;
  1364. }
  1365. static bool kvm_xen_hcall_set_timer_op(struct kvm_vcpu *vcpu, uint64_t timeout,
  1366. u64 *r)
  1367. {
  1368. if (!kvm_xen_timer_enabled(vcpu))
  1369. return false;
  1370. if (timeout)
  1371. kvm_xen_start_timer(vcpu, timeout, true);
  1372. else
  1373. kvm_xen_stop_timer(vcpu);
  1374. *r = 0;
  1375. return true;
  1376. }
  1377. int kvm_xen_hypercall(struct kvm_vcpu *vcpu)
  1378. {
  1379. bool longmode;
  1380. u64 input, params[6], r = -ENOSYS;
  1381. bool handled = false;
  1382. u8 cpl;
  1383. input = (u64)kvm_register_read(vcpu, VCPU_REGS_RAX);
  1384. /* Hyper-V hypercalls get bit 31 set in EAX */
  1385. if ((input & 0x80000000) &&
  1386. kvm_hv_hypercall_enabled(vcpu))
  1387. return kvm_hv_hypercall(vcpu);
  1388. longmode = is_64_bit_hypercall(vcpu);
  1389. if (!longmode) {
  1390. params[0] = (u32)kvm_rbx_read(vcpu);
  1391. params[1] = (u32)kvm_rcx_read(vcpu);
  1392. params[2] = (u32)kvm_rdx_read(vcpu);
  1393. params[3] = (u32)kvm_rsi_read(vcpu);
  1394. params[4] = (u32)kvm_rdi_read(vcpu);
  1395. params[5] = (u32)kvm_rbp_read(vcpu);
  1396. }
  1397. #ifdef CONFIG_X86_64
  1398. else {
  1399. params[0] = (u64)kvm_rdi_read(vcpu);
  1400. params[1] = (u64)kvm_rsi_read(vcpu);
  1401. params[2] = (u64)kvm_rdx_read(vcpu);
  1402. params[3] = (u64)kvm_r10_read(vcpu);
  1403. params[4] = (u64)kvm_r8_read(vcpu);
  1404. params[5] = (u64)kvm_r9_read(vcpu);
  1405. }
  1406. #endif
  1407. cpl = kvm_x86_call(get_cpl)(vcpu);
  1408. trace_kvm_xen_hypercall(cpl, input, params[0], params[1], params[2],
  1409. params[3], params[4], params[5]);
  1410. /*
  1411. * Only allow hypercall acceleration for CPL0. The rare hypercalls that
  1412. * are permitted in guest userspace can be handled by the VMM.
  1413. */
  1414. if (unlikely(cpl > 0))
  1415. goto handle_in_userspace;
  1416. switch (input) {
  1417. case __HYPERVISOR_xen_version:
  1418. if (params[0] == XENVER_version && vcpu->kvm->arch.xen.xen_version) {
  1419. r = vcpu->kvm->arch.xen.xen_version;
  1420. handled = true;
  1421. }
  1422. break;
  1423. case __HYPERVISOR_event_channel_op:
  1424. if (params[0] == EVTCHNOP_send)
  1425. handled = kvm_xen_hcall_evtchn_send(vcpu, params[1], &r);
  1426. break;
  1427. case __HYPERVISOR_sched_op:
  1428. handled = kvm_xen_hcall_sched_op(vcpu, longmode, params[0],
  1429. params[1], &r);
  1430. break;
  1431. case __HYPERVISOR_vcpu_op:
  1432. handled = kvm_xen_hcall_vcpu_op(vcpu, longmode, params[0], params[1],
  1433. params[2], &r);
  1434. break;
  1435. case __HYPERVISOR_set_timer_op: {
  1436. u64 timeout = params[0];
  1437. /* In 32-bit mode, the 64-bit timeout is in two 32-bit params. */
  1438. if (!longmode)
  1439. timeout |= params[1] << 32;
  1440. handled = kvm_xen_hcall_set_timer_op(vcpu, timeout, &r);
  1441. break;
  1442. }
  1443. default:
  1444. break;
  1445. }
  1446. if (handled)
  1447. return kvm_xen_hypercall_set_result(vcpu, r);
  1448. handle_in_userspace:
  1449. vcpu->run->exit_reason = KVM_EXIT_XEN;
  1450. vcpu->run->xen.type = KVM_EXIT_XEN_HCALL;
  1451. vcpu->run->xen.u.hcall.longmode = longmode;
  1452. vcpu->run->xen.u.hcall.cpl = cpl;
  1453. vcpu->run->xen.u.hcall.input = input;
  1454. vcpu->run->xen.u.hcall.params[0] = params[0];
  1455. vcpu->run->xen.u.hcall.params[1] = params[1];
  1456. vcpu->run->xen.u.hcall.params[2] = params[2];
  1457. vcpu->run->xen.u.hcall.params[3] = params[3];
  1458. vcpu->run->xen.u.hcall.params[4] = params[4];
  1459. vcpu->run->xen.u.hcall.params[5] = params[5];
  1460. vcpu->arch.xen.hypercall_rip = kvm_get_linear_rip(vcpu);
  1461. vcpu->arch.complete_userspace_io =
  1462. kvm_xen_hypercall_complete_userspace;
  1463. return 0;
  1464. }
  1465. static void kvm_xen_check_poller(struct kvm_vcpu *vcpu, int port)
  1466. {
  1467. int poll_evtchn = vcpu->arch.xen.poll_evtchn;
  1468. if ((poll_evtchn == port || poll_evtchn == -1) &&
  1469. test_and_clear_bit(vcpu->vcpu_idx, vcpu->kvm->arch.xen.poll_mask)) {
  1470. kvm_make_request(KVM_REQ_UNBLOCK, vcpu);
  1471. kvm_vcpu_kick(vcpu);
  1472. }
  1473. }
  1474. /*
  1475. * The return value from this function is propagated to kvm_set_irq() API,
  1476. * so it returns:
  1477. * < 0 Interrupt was ignored (masked or not delivered for other reasons)
  1478. * = 0 Interrupt was coalesced (previous irq is still pending)
  1479. * > 0 Number of CPUs interrupt was delivered to
  1480. *
  1481. * It is also called directly from kvm_arch_set_irq_inatomic(), where the
  1482. * only check on its return value is a comparison with -EWOULDBLOCK'.
  1483. */
  1484. int kvm_xen_set_evtchn_fast(struct kvm_xen_evtchn *xe, struct kvm *kvm)
  1485. {
  1486. struct gfn_to_pfn_cache *gpc = &kvm->arch.xen.shinfo_cache;
  1487. struct kvm_vcpu *vcpu;
  1488. unsigned long *pending_bits, *mask_bits;
  1489. unsigned long flags;
  1490. int port_word_bit;
  1491. bool kick_vcpu = false;
  1492. int vcpu_idx, idx, rc;
  1493. vcpu_idx = READ_ONCE(xe->vcpu_idx);
  1494. if (vcpu_idx >= 0)
  1495. vcpu = kvm_get_vcpu(kvm, vcpu_idx);
  1496. else {
  1497. vcpu = kvm_get_vcpu_by_id(kvm, xe->vcpu_id);
  1498. if (!vcpu)
  1499. return -EINVAL;
  1500. WRITE_ONCE(xe->vcpu_idx, vcpu->vcpu_idx);
  1501. }
  1502. if (xe->port >= max_evtchn_port(kvm))
  1503. return -EINVAL;
  1504. rc = -EWOULDBLOCK;
  1505. idx = srcu_read_lock(&kvm->srcu);
  1506. read_lock_irqsave(&gpc->lock, flags);
  1507. if (!kvm_gpc_check(gpc, PAGE_SIZE))
  1508. goto out_rcu;
  1509. if (IS_ENABLED(CONFIG_64BIT) && kvm->arch.xen.long_mode) {
  1510. struct shared_info *shinfo = gpc->khva;
  1511. pending_bits = (unsigned long *)&shinfo->evtchn_pending;
  1512. mask_bits = (unsigned long *)&shinfo->evtchn_mask;
  1513. port_word_bit = xe->port / 64;
  1514. } else {
  1515. struct compat_shared_info *shinfo = gpc->khva;
  1516. pending_bits = (unsigned long *)&shinfo->evtchn_pending;
  1517. mask_bits = (unsigned long *)&shinfo->evtchn_mask;
  1518. port_word_bit = xe->port / 32;
  1519. }
  1520. /*
  1521. * If this port wasn't already set, and if it isn't masked, then
  1522. * we try to set the corresponding bit in the in-kernel shadow of
  1523. * evtchn_pending_sel for the target vCPU. And if *that* wasn't
  1524. * already set, then we kick the vCPU in question to write to the
  1525. * *real* evtchn_pending_sel in its own guest vcpu_info struct.
  1526. */
  1527. if (test_and_set_bit(xe->port, pending_bits)) {
  1528. rc = 0; /* It was already raised */
  1529. } else if (test_bit(xe->port, mask_bits)) {
  1530. rc = -ENOTCONN; /* Masked */
  1531. kvm_xen_check_poller(vcpu, xe->port);
  1532. } else {
  1533. rc = 1; /* Delivered to the bitmap in shared_info. */
  1534. /* Now switch to the vCPU's vcpu_info to set the index and pending_sel */
  1535. read_unlock_irqrestore(&gpc->lock, flags);
  1536. gpc = &vcpu->arch.xen.vcpu_info_cache;
  1537. read_lock_irqsave(&gpc->lock, flags);
  1538. if (!kvm_gpc_check(gpc, sizeof(struct vcpu_info))) {
  1539. /*
  1540. * Could not access the vcpu_info. Set the bit in-kernel
  1541. * and prod the vCPU to deliver it for itself.
  1542. */
  1543. if (!test_and_set_bit(port_word_bit, &vcpu->arch.xen.evtchn_pending_sel))
  1544. kick_vcpu = true;
  1545. goto out_rcu;
  1546. }
  1547. if (IS_ENABLED(CONFIG_64BIT) && kvm->arch.xen.long_mode) {
  1548. struct vcpu_info *vcpu_info = gpc->khva;
  1549. if (!test_and_set_bit(port_word_bit, &vcpu_info->evtchn_pending_sel)) {
  1550. WRITE_ONCE(vcpu_info->evtchn_upcall_pending, 1);
  1551. kick_vcpu = true;
  1552. }
  1553. } else {
  1554. struct compat_vcpu_info *vcpu_info = gpc->khva;
  1555. if (!test_and_set_bit(port_word_bit,
  1556. (unsigned long *)&vcpu_info->evtchn_pending_sel)) {
  1557. WRITE_ONCE(vcpu_info->evtchn_upcall_pending, 1);
  1558. kick_vcpu = true;
  1559. }
  1560. }
  1561. /* For the per-vCPU lapic vector, deliver it as MSI. */
  1562. if (kick_vcpu && vcpu->arch.xen.upcall_vector) {
  1563. kvm_xen_inject_vcpu_vector(vcpu);
  1564. kick_vcpu = false;
  1565. }
  1566. }
  1567. out_rcu:
  1568. read_unlock_irqrestore(&gpc->lock, flags);
  1569. srcu_read_unlock(&kvm->srcu, idx);
  1570. if (kick_vcpu) {
  1571. kvm_make_request(KVM_REQ_UNBLOCK, vcpu);
  1572. kvm_vcpu_kick(vcpu);
  1573. }
  1574. return rc;
  1575. }
  1576. static int kvm_xen_set_evtchn(struct kvm_xen_evtchn *xe, struct kvm *kvm)
  1577. {
  1578. bool mm_borrowed = false;
  1579. int rc;
  1580. rc = kvm_xen_set_evtchn_fast(xe, kvm);
  1581. if (rc != -EWOULDBLOCK)
  1582. return rc;
  1583. if (current->mm != kvm->mm) {
  1584. /*
  1585. * If not on a thread which already belongs to this KVM,
  1586. * we'd better be in the irqfd workqueue.
  1587. */
  1588. if (WARN_ON_ONCE(current->mm))
  1589. return -EINVAL;
  1590. kthread_use_mm(kvm->mm);
  1591. mm_borrowed = true;
  1592. }
  1593. /*
  1594. * It is theoretically possible for the page to be unmapped
  1595. * and the MMU notifier to invalidate the shared_info before
  1596. * we even get to use it. In that case, this looks like an
  1597. * infinite loop. It was tempting to do it via the userspace
  1598. * HVA instead... but that just *hides* the fact that it's
  1599. * an infinite loop, because if a fault occurs and it waits
  1600. * for the page to come back, it can *still* immediately
  1601. * fault and have to wait again, repeatedly.
  1602. *
  1603. * Conversely, the page could also have been reinstated by
  1604. * another thread before we even obtain the mutex above, so
  1605. * check again *first* before remapping it.
  1606. */
  1607. do {
  1608. struct gfn_to_pfn_cache *gpc = &kvm->arch.xen.shinfo_cache;
  1609. int idx;
  1610. rc = kvm_xen_set_evtchn_fast(xe, kvm);
  1611. if (rc != -EWOULDBLOCK)
  1612. break;
  1613. idx = srcu_read_lock(&kvm->srcu);
  1614. rc = kvm_gpc_refresh(gpc, PAGE_SIZE);
  1615. srcu_read_unlock(&kvm->srcu, idx);
  1616. } while(!rc);
  1617. if (mm_borrowed)
  1618. kthread_unuse_mm(kvm->mm);
  1619. return rc;
  1620. }
  1621. /* This is the version called from kvm_set_irq() as the .set function */
  1622. static int evtchn_set_fn(struct kvm_kernel_irq_routing_entry *e, struct kvm *kvm,
  1623. int irq_source_id, int level, bool line_status)
  1624. {
  1625. if (!level)
  1626. return -EINVAL;
  1627. return kvm_xen_set_evtchn(&e->xen_evtchn, kvm);
  1628. }
  1629. /*
  1630. * Set up an event channel interrupt from the KVM IRQ routing table.
  1631. * Used for e.g. PIRQ from passed through physical devices.
  1632. */
  1633. int kvm_xen_setup_evtchn(struct kvm *kvm,
  1634. struct kvm_kernel_irq_routing_entry *e,
  1635. const struct kvm_irq_routing_entry *ue)
  1636. {
  1637. struct kvm_vcpu *vcpu;
  1638. if (ue->u.xen_evtchn.port >= max_evtchn_port(kvm))
  1639. return -EINVAL;
  1640. /* We only support 2 level event channels for now */
  1641. if (ue->u.xen_evtchn.priority != KVM_IRQ_ROUTING_XEN_EVTCHN_PRIO_2LEVEL)
  1642. return -EINVAL;
  1643. /*
  1644. * Xen gives us interesting mappings from vCPU index to APIC ID,
  1645. * which means kvm_get_vcpu_by_id() has to iterate over all vCPUs
  1646. * to find it. Do that once at setup time, instead of every time.
  1647. * But beware that on live update / live migration, the routing
  1648. * table might be reinstated before the vCPU threads have finished
  1649. * recreating their vCPUs.
  1650. */
  1651. vcpu = kvm_get_vcpu_by_id(kvm, ue->u.xen_evtchn.vcpu);
  1652. if (vcpu)
  1653. e->xen_evtchn.vcpu_idx = vcpu->vcpu_idx;
  1654. else
  1655. e->xen_evtchn.vcpu_idx = -1;
  1656. e->xen_evtchn.port = ue->u.xen_evtchn.port;
  1657. e->xen_evtchn.vcpu_id = ue->u.xen_evtchn.vcpu;
  1658. e->xen_evtchn.priority = ue->u.xen_evtchn.priority;
  1659. e->set = evtchn_set_fn;
  1660. return 0;
  1661. }
  1662. /*
  1663. * Explicit event sending from userspace with KVM_XEN_HVM_EVTCHN_SEND ioctl.
  1664. */
  1665. int kvm_xen_hvm_evtchn_send(struct kvm *kvm, struct kvm_irq_routing_xen_evtchn *uxe)
  1666. {
  1667. struct kvm_xen_evtchn e;
  1668. int ret;
  1669. if (!uxe->port || uxe->port >= max_evtchn_port(kvm))
  1670. return -EINVAL;
  1671. /* We only support 2 level event channels for now */
  1672. if (uxe->priority != KVM_IRQ_ROUTING_XEN_EVTCHN_PRIO_2LEVEL)
  1673. return -EINVAL;
  1674. e.port = uxe->port;
  1675. e.vcpu_id = uxe->vcpu;
  1676. e.vcpu_idx = -1;
  1677. e.priority = uxe->priority;
  1678. ret = kvm_xen_set_evtchn(&e, kvm);
  1679. /*
  1680. * None of that 'return 1 if it actually got delivered' nonsense.
  1681. * We don't care if it was masked (-ENOTCONN) either.
  1682. */
  1683. if (ret > 0 || ret == -ENOTCONN)
  1684. ret = 0;
  1685. return ret;
  1686. }
  1687. /*
  1688. * Support for *outbound* event channel events via the EVTCHNOP_send hypercall.
  1689. */
  1690. struct evtchnfd {
  1691. u32 send_port;
  1692. u32 type;
  1693. union {
  1694. struct kvm_xen_evtchn port;
  1695. struct {
  1696. u32 port; /* zero */
  1697. struct eventfd_ctx *ctx;
  1698. } eventfd;
  1699. } deliver;
  1700. };
  1701. /*
  1702. * Update target vCPU or priority for a registered sending channel.
  1703. */
  1704. static int kvm_xen_eventfd_update(struct kvm *kvm,
  1705. struct kvm_xen_hvm_attr *data)
  1706. {
  1707. u32 port = data->u.evtchn.send_port;
  1708. struct evtchnfd *evtchnfd;
  1709. int ret;
  1710. /* Protect writes to evtchnfd as well as the idr lookup. */
  1711. mutex_lock(&kvm->arch.xen.xen_lock);
  1712. evtchnfd = idr_find(&kvm->arch.xen.evtchn_ports, port);
  1713. ret = -ENOENT;
  1714. if (!evtchnfd)
  1715. goto out_unlock;
  1716. /* For an UPDATE, nothing may change except the priority/vcpu */
  1717. ret = -EINVAL;
  1718. if (evtchnfd->type != data->u.evtchn.type)
  1719. goto out_unlock;
  1720. /*
  1721. * Port cannot change, and if it's zero that was an eventfd
  1722. * which can't be changed either.
  1723. */
  1724. if (!evtchnfd->deliver.port.port ||
  1725. evtchnfd->deliver.port.port != data->u.evtchn.deliver.port.port)
  1726. goto out_unlock;
  1727. /* We only support 2 level event channels for now */
  1728. if (data->u.evtchn.deliver.port.priority != KVM_IRQ_ROUTING_XEN_EVTCHN_PRIO_2LEVEL)
  1729. goto out_unlock;
  1730. evtchnfd->deliver.port.priority = data->u.evtchn.deliver.port.priority;
  1731. if (evtchnfd->deliver.port.vcpu_id != data->u.evtchn.deliver.port.vcpu) {
  1732. evtchnfd->deliver.port.vcpu_id = data->u.evtchn.deliver.port.vcpu;
  1733. evtchnfd->deliver.port.vcpu_idx = -1;
  1734. }
  1735. ret = 0;
  1736. out_unlock:
  1737. mutex_unlock(&kvm->arch.xen.xen_lock);
  1738. return ret;
  1739. }
  1740. /*
  1741. * Configure the target (eventfd or local port delivery) for sending on
  1742. * a given event channel.
  1743. */
  1744. static int kvm_xen_eventfd_assign(struct kvm *kvm,
  1745. struct kvm_xen_hvm_attr *data)
  1746. {
  1747. u32 port = data->u.evtchn.send_port;
  1748. struct eventfd_ctx *eventfd = NULL;
  1749. struct evtchnfd *evtchnfd;
  1750. int ret = -EINVAL;
  1751. evtchnfd = kzalloc(sizeof(struct evtchnfd), GFP_KERNEL);
  1752. if (!evtchnfd)
  1753. return -ENOMEM;
  1754. switch(data->u.evtchn.type) {
  1755. case EVTCHNSTAT_ipi:
  1756. /* IPI must map back to the same port# */
  1757. if (data->u.evtchn.deliver.port.port != data->u.evtchn.send_port)
  1758. goto out_noeventfd; /* -EINVAL */
  1759. break;
  1760. case EVTCHNSTAT_interdomain:
  1761. if (data->u.evtchn.deliver.port.port) {
  1762. if (data->u.evtchn.deliver.port.port >= max_evtchn_port(kvm))
  1763. goto out_noeventfd; /* -EINVAL */
  1764. } else {
  1765. eventfd = eventfd_ctx_fdget(data->u.evtchn.deliver.eventfd.fd);
  1766. if (IS_ERR(eventfd)) {
  1767. ret = PTR_ERR(eventfd);
  1768. goto out_noeventfd;
  1769. }
  1770. }
  1771. break;
  1772. case EVTCHNSTAT_virq:
  1773. case EVTCHNSTAT_closed:
  1774. case EVTCHNSTAT_unbound:
  1775. case EVTCHNSTAT_pirq:
  1776. default: /* Unknown event channel type */
  1777. goto out; /* -EINVAL */
  1778. }
  1779. evtchnfd->send_port = data->u.evtchn.send_port;
  1780. evtchnfd->type = data->u.evtchn.type;
  1781. if (eventfd) {
  1782. evtchnfd->deliver.eventfd.ctx = eventfd;
  1783. } else {
  1784. /* We only support 2 level event channels for now */
  1785. if (data->u.evtchn.deliver.port.priority != KVM_IRQ_ROUTING_XEN_EVTCHN_PRIO_2LEVEL)
  1786. goto out; /* -EINVAL; */
  1787. evtchnfd->deliver.port.port = data->u.evtchn.deliver.port.port;
  1788. evtchnfd->deliver.port.vcpu_id = data->u.evtchn.deliver.port.vcpu;
  1789. evtchnfd->deliver.port.vcpu_idx = -1;
  1790. evtchnfd->deliver.port.priority = data->u.evtchn.deliver.port.priority;
  1791. }
  1792. mutex_lock(&kvm->arch.xen.xen_lock);
  1793. ret = idr_alloc(&kvm->arch.xen.evtchn_ports, evtchnfd, port, port + 1,
  1794. GFP_KERNEL);
  1795. mutex_unlock(&kvm->arch.xen.xen_lock);
  1796. if (ret >= 0)
  1797. return 0;
  1798. if (ret == -ENOSPC)
  1799. ret = -EEXIST;
  1800. out:
  1801. if (eventfd)
  1802. eventfd_ctx_put(eventfd);
  1803. out_noeventfd:
  1804. kfree(evtchnfd);
  1805. return ret;
  1806. }
  1807. static int kvm_xen_eventfd_deassign(struct kvm *kvm, u32 port)
  1808. {
  1809. struct evtchnfd *evtchnfd;
  1810. mutex_lock(&kvm->arch.xen.xen_lock);
  1811. evtchnfd = idr_remove(&kvm->arch.xen.evtchn_ports, port);
  1812. mutex_unlock(&kvm->arch.xen.xen_lock);
  1813. if (!evtchnfd)
  1814. return -ENOENT;
  1815. synchronize_srcu(&kvm->srcu);
  1816. if (!evtchnfd->deliver.port.port)
  1817. eventfd_ctx_put(evtchnfd->deliver.eventfd.ctx);
  1818. kfree(evtchnfd);
  1819. return 0;
  1820. }
  1821. static int kvm_xen_eventfd_reset(struct kvm *kvm)
  1822. {
  1823. struct evtchnfd *evtchnfd, **all_evtchnfds;
  1824. int i;
  1825. int n = 0;
  1826. mutex_lock(&kvm->arch.xen.xen_lock);
  1827. /*
  1828. * Because synchronize_srcu() cannot be called inside the
  1829. * critical section, first collect all the evtchnfd objects
  1830. * in an array as they are removed from evtchn_ports.
  1831. */
  1832. idr_for_each_entry(&kvm->arch.xen.evtchn_ports, evtchnfd, i)
  1833. n++;
  1834. all_evtchnfds = kmalloc_array(n, sizeof(struct evtchnfd *), GFP_KERNEL);
  1835. if (!all_evtchnfds) {
  1836. mutex_unlock(&kvm->arch.xen.xen_lock);
  1837. return -ENOMEM;
  1838. }
  1839. n = 0;
  1840. idr_for_each_entry(&kvm->arch.xen.evtchn_ports, evtchnfd, i) {
  1841. all_evtchnfds[n++] = evtchnfd;
  1842. idr_remove(&kvm->arch.xen.evtchn_ports, evtchnfd->send_port);
  1843. }
  1844. mutex_unlock(&kvm->arch.xen.xen_lock);
  1845. synchronize_srcu(&kvm->srcu);
  1846. while (n--) {
  1847. evtchnfd = all_evtchnfds[n];
  1848. if (!evtchnfd->deliver.port.port)
  1849. eventfd_ctx_put(evtchnfd->deliver.eventfd.ctx);
  1850. kfree(evtchnfd);
  1851. }
  1852. kfree(all_evtchnfds);
  1853. return 0;
  1854. }
  1855. static int kvm_xen_setattr_evtchn(struct kvm *kvm, struct kvm_xen_hvm_attr *data)
  1856. {
  1857. u32 port = data->u.evtchn.send_port;
  1858. if (data->u.evtchn.flags == KVM_XEN_EVTCHN_RESET)
  1859. return kvm_xen_eventfd_reset(kvm);
  1860. if (!port || port >= max_evtchn_port(kvm))
  1861. return -EINVAL;
  1862. if (data->u.evtchn.flags == KVM_XEN_EVTCHN_DEASSIGN)
  1863. return kvm_xen_eventfd_deassign(kvm, port);
  1864. if (data->u.evtchn.flags == KVM_XEN_EVTCHN_UPDATE)
  1865. return kvm_xen_eventfd_update(kvm, data);
  1866. if (data->u.evtchn.flags)
  1867. return -EINVAL;
  1868. return kvm_xen_eventfd_assign(kvm, data);
  1869. }
  1870. static bool kvm_xen_hcall_evtchn_send(struct kvm_vcpu *vcpu, u64 param, u64 *r)
  1871. {
  1872. struct evtchnfd *evtchnfd;
  1873. struct evtchn_send send;
  1874. struct x86_exception e;
  1875. /* Sanity check: this structure is the same for 32-bit and 64-bit */
  1876. BUILD_BUG_ON(sizeof(send) != 4);
  1877. if (kvm_read_guest_virt(vcpu, param, &send, sizeof(send), &e)) {
  1878. *r = -EFAULT;
  1879. return true;
  1880. }
  1881. /*
  1882. * evtchnfd is protected by kvm->srcu; the idr lookup instead
  1883. * is protected by RCU.
  1884. */
  1885. rcu_read_lock();
  1886. evtchnfd = idr_find(&vcpu->kvm->arch.xen.evtchn_ports, send.port);
  1887. rcu_read_unlock();
  1888. if (!evtchnfd)
  1889. return false;
  1890. if (evtchnfd->deliver.port.port) {
  1891. int ret = kvm_xen_set_evtchn(&evtchnfd->deliver.port, vcpu->kvm);
  1892. if (ret < 0 && ret != -ENOTCONN)
  1893. return false;
  1894. } else {
  1895. eventfd_signal(evtchnfd->deliver.eventfd.ctx);
  1896. }
  1897. *r = 0;
  1898. return true;
  1899. }
  1900. void kvm_xen_init_vcpu(struct kvm_vcpu *vcpu)
  1901. {
  1902. vcpu->arch.xen.vcpu_id = vcpu->vcpu_idx;
  1903. vcpu->arch.xen.poll_evtchn = 0;
  1904. timer_setup(&vcpu->arch.xen.poll_timer, cancel_evtchn_poll, 0);
  1905. kvm_gpc_init(&vcpu->arch.xen.runstate_cache, vcpu->kvm);
  1906. kvm_gpc_init(&vcpu->arch.xen.runstate2_cache, vcpu->kvm);
  1907. kvm_gpc_init(&vcpu->arch.xen.vcpu_info_cache, vcpu->kvm);
  1908. kvm_gpc_init(&vcpu->arch.xen.vcpu_time_info_cache, vcpu->kvm);
  1909. }
  1910. void kvm_xen_destroy_vcpu(struct kvm_vcpu *vcpu)
  1911. {
  1912. if (kvm_xen_timer_enabled(vcpu))
  1913. kvm_xen_stop_timer(vcpu);
  1914. kvm_gpc_deactivate(&vcpu->arch.xen.runstate_cache);
  1915. kvm_gpc_deactivate(&vcpu->arch.xen.runstate2_cache);
  1916. kvm_gpc_deactivate(&vcpu->arch.xen.vcpu_info_cache);
  1917. kvm_gpc_deactivate(&vcpu->arch.xen.vcpu_time_info_cache);
  1918. del_timer_sync(&vcpu->arch.xen.poll_timer);
  1919. }
  1920. void kvm_xen_update_tsc_info(struct kvm_vcpu *vcpu)
  1921. {
  1922. struct kvm_cpuid_entry2 *entry;
  1923. u32 function;
  1924. if (!vcpu->arch.xen.cpuid.base)
  1925. return;
  1926. function = vcpu->arch.xen.cpuid.base | XEN_CPUID_LEAF(3);
  1927. if (function > vcpu->arch.xen.cpuid.limit)
  1928. return;
  1929. entry = kvm_find_cpuid_entry_index(vcpu, function, 1);
  1930. if (entry) {
  1931. entry->ecx = vcpu->arch.hv_clock.tsc_to_system_mul;
  1932. entry->edx = vcpu->arch.hv_clock.tsc_shift;
  1933. }
  1934. entry = kvm_find_cpuid_entry_index(vcpu, function, 2);
  1935. if (entry)
  1936. entry->eax = vcpu->arch.hw_tsc_khz;
  1937. }
  1938. void kvm_xen_init_vm(struct kvm *kvm)
  1939. {
  1940. mutex_init(&kvm->arch.xen.xen_lock);
  1941. idr_init(&kvm->arch.xen.evtchn_ports);
  1942. kvm_gpc_init(&kvm->arch.xen.shinfo_cache, kvm);
  1943. }
  1944. void kvm_xen_destroy_vm(struct kvm *kvm)
  1945. {
  1946. struct evtchnfd *evtchnfd;
  1947. int i;
  1948. kvm_gpc_deactivate(&kvm->arch.xen.shinfo_cache);
  1949. idr_for_each_entry(&kvm->arch.xen.evtchn_ports, evtchnfd, i) {
  1950. if (!evtchnfd->deliver.port.port)
  1951. eventfd_ctx_put(evtchnfd->deliver.eventfd.ctx);
  1952. kfree(evtchnfd);
  1953. }
  1954. idr_destroy(&kvm->arch.xen.evtchn_ports);
  1955. if (kvm->arch.xen_hvm_config.msr)
  1956. static_branch_slow_dec_deferred(&kvm_xen_enabled);
  1957. }