vgic.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  1. /*
  2. * Copyright (C) 2012 ARM Ltd.
  3. * Author: Marc Zyngier <marc.zyngier@arm.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #include <linux/kvm.h>
  19. #include <linux/kvm_host.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/io.h>
  22. #include <asm/kvm_emulate.h>
  23. /*
  24. * How the whole thing works (courtesy of Christoffer Dall):
  25. *
  26. * - At any time, the dist->irq_pending_on_cpu is the oracle that knows if
  27. * something is pending
  28. * - VGIC pending interrupts are stored on the vgic.irq_state vgic
  29. * bitmap (this bitmap is updated by both user land ioctls and guest
  30. * mmio ops, and other in-kernel peripherals such as the
  31. * arch. timers) and indicate the 'wire' state.
  32. * - Every time the bitmap changes, the irq_pending_on_cpu oracle is
  33. * recalculated
  34. * - To calculate the oracle, we need info for each cpu from
  35. * compute_pending_for_cpu, which considers:
  36. * - PPI: dist->irq_state & dist->irq_enable
  37. * - SPI: dist->irq_state & dist->irq_enable & dist->irq_spi_target
  38. * - irq_spi_target is a 'formatted' version of the GICD_ICFGR
  39. * registers, stored on each vcpu. We only keep one bit of
  40. * information per interrupt, making sure that only one vcpu can
  41. * accept the interrupt.
  42. * - The same is true when injecting an interrupt, except that we only
  43. * consider a single interrupt at a time. The irq_spi_cpu array
  44. * contains the target CPU for each SPI.
  45. *
  46. * The handling of level interrupts adds some extra complexity. We
  47. * need to track when the interrupt has been EOIed, so we can sample
  48. * the 'line' again. This is achieved as such:
  49. *
  50. * - When a level interrupt is moved onto a vcpu, the corresponding
  51. * bit in irq_active is set. As long as this bit is set, the line
  52. * will be ignored for further interrupts. The interrupt is injected
  53. * into the vcpu with the GICH_LR_EOI bit set (generate a
  54. * maintenance interrupt on EOI).
  55. * - When the interrupt is EOIed, the maintenance interrupt fires,
  56. * and clears the corresponding bit in irq_active. This allow the
  57. * interrupt line to be sampled again.
  58. */
  59. #define VGIC_ADDR_UNDEF (-1)
  60. #define IS_VGIC_ADDR_UNDEF(_x) ((_x) == VGIC_ADDR_UNDEF)
  61. #define ACCESS_READ_VALUE (1 << 0)
  62. #define ACCESS_READ_RAZ (0 << 0)
  63. #define ACCESS_READ_MASK(x) ((x) & (1 << 0))
  64. #define ACCESS_WRITE_IGNORED (0 << 1)
  65. #define ACCESS_WRITE_SETBIT (1 << 1)
  66. #define ACCESS_WRITE_CLEARBIT (2 << 1)
  67. #define ACCESS_WRITE_VALUE (3 << 1)
  68. #define ACCESS_WRITE_MASK(x) ((x) & (3 << 1))
  69. static void vgic_retire_disabled_irqs(struct kvm_vcpu *vcpu);
  70. static void vgic_update_state(struct kvm *kvm);
  71. static void vgic_dispatch_sgi(struct kvm_vcpu *vcpu, u32 reg);
  72. static u32 *vgic_bitmap_get_reg(struct vgic_bitmap *x,
  73. int cpuid, u32 offset)
  74. {
  75. offset >>= 2;
  76. if (!offset)
  77. return x->percpu[cpuid].reg;
  78. else
  79. return x->shared.reg + offset - 1;
  80. }
  81. static int vgic_bitmap_get_irq_val(struct vgic_bitmap *x,
  82. int cpuid, int irq)
  83. {
  84. if (irq < VGIC_NR_PRIVATE_IRQS)
  85. return test_bit(irq, x->percpu[cpuid].reg_ul);
  86. return test_bit(irq - VGIC_NR_PRIVATE_IRQS, x->shared.reg_ul);
  87. }
  88. static void vgic_bitmap_set_irq_val(struct vgic_bitmap *x, int cpuid,
  89. int irq, int val)
  90. {
  91. unsigned long *reg;
  92. if (irq < VGIC_NR_PRIVATE_IRQS) {
  93. reg = x->percpu[cpuid].reg_ul;
  94. } else {
  95. reg = x->shared.reg_ul;
  96. irq -= VGIC_NR_PRIVATE_IRQS;
  97. }
  98. if (val)
  99. set_bit(irq, reg);
  100. else
  101. clear_bit(irq, reg);
  102. }
  103. static unsigned long *vgic_bitmap_get_cpu_map(struct vgic_bitmap *x, int cpuid)
  104. {
  105. if (unlikely(cpuid >= VGIC_MAX_CPUS))
  106. return NULL;
  107. return x->percpu[cpuid].reg_ul;
  108. }
  109. static unsigned long *vgic_bitmap_get_shared_map(struct vgic_bitmap *x)
  110. {
  111. return x->shared.reg_ul;
  112. }
  113. static u32 *vgic_bytemap_get_reg(struct vgic_bytemap *x, int cpuid, u32 offset)
  114. {
  115. offset >>= 2;
  116. BUG_ON(offset > (VGIC_NR_IRQS / 4));
  117. if (offset < 4)
  118. return x->percpu[cpuid] + offset;
  119. else
  120. return x->shared + offset - 8;
  121. }
  122. #define VGIC_CFG_LEVEL 0
  123. #define VGIC_CFG_EDGE 1
  124. static bool vgic_irq_is_edge(struct kvm_vcpu *vcpu, int irq)
  125. {
  126. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  127. int irq_val;
  128. irq_val = vgic_bitmap_get_irq_val(&dist->irq_cfg, vcpu->vcpu_id, irq);
  129. return irq_val == VGIC_CFG_EDGE;
  130. }
  131. static int vgic_irq_is_enabled(struct kvm_vcpu *vcpu, int irq)
  132. {
  133. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  134. return vgic_bitmap_get_irq_val(&dist->irq_enabled, vcpu->vcpu_id, irq);
  135. }
  136. static int vgic_irq_is_active(struct kvm_vcpu *vcpu, int irq)
  137. {
  138. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  139. return vgic_bitmap_get_irq_val(&dist->irq_active, vcpu->vcpu_id, irq);
  140. }
  141. static void vgic_irq_set_active(struct kvm_vcpu *vcpu, int irq)
  142. {
  143. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  144. vgic_bitmap_set_irq_val(&dist->irq_active, vcpu->vcpu_id, irq, 1);
  145. }
  146. static void vgic_irq_clear_active(struct kvm_vcpu *vcpu, int irq)
  147. {
  148. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  149. vgic_bitmap_set_irq_val(&dist->irq_active, vcpu->vcpu_id, irq, 0);
  150. }
  151. static int vgic_dist_irq_is_pending(struct kvm_vcpu *vcpu, int irq)
  152. {
  153. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  154. return vgic_bitmap_get_irq_val(&dist->irq_state, vcpu->vcpu_id, irq);
  155. }
  156. static void vgic_dist_irq_set(struct kvm_vcpu *vcpu, int irq)
  157. {
  158. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  159. vgic_bitmap_set_irq_val(&dist->irq_state, vcpu->vcpu_id, irq, 1);
  160. }
  161. static void vgic_dist_irq_clear(struct kvm_vcpu *vcpu, int irq)
  162. {
  163. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  164. vgic_bitmap_set_irq_val(&dist->irq_state, vcpu->vcpu_id, irq, 0);
  165. }
  166. static void vgic_cpu_irq_set(struct kvm_vcpu *vcpu, int irq)
  167. {
  168. if (irq < VGIC_NR_PRIVATE_IRQS)
  169. set_bit(irq, vcpu->arch.vgic_cpu.pending_percpu);
  170. else
  171. set_bit(irq - VGIC_NR_PRIVATE_IRQS,
  172. vcpu->arch.vgic_cpu.pending_shared);
  173. }
  174. static void vgic_cpu_irq_clear(struct kvm_vcpu *vcpu, int irq)
  175. {
  176. if (irq < VGIC_NR_PRIVATE_IRQS)
  177. clear_bit(irq, vcpu->arch.vgic_cpu.pending_percpu);
  178. else
  179. clear_bit(irq - VGIC_NR_PRIVATE_IRQS,
  180. vcpu->arch.vgic_cpu.pending_shared);
  181. }
  182. static u32 mmio_data_read(struct kvm_exit_mmio *mmio, u32 mask)
  183. {
  184. return *((u32 *)mmio->data) & mask;
  185. }
  186. static void mmio_data_write(struct kvm_exit_mmio *mmio, u32 mask, u32 value)
  187. {
  188. *((u32 *)mmio->data) = value & mask;
  189. }
  190. /**
  191. * vgic_reg_access - access vgic register
  192. * @mmio: pointer to the data describing the mmio access
  193. * @reg: pointer to the virtual backing of vgic distributor data
  194. * @offset: least significant 2 bits used for word offset
  195. * @mode: ACCESS_ mode (see defines above)
  196. *
  197. * Helper to make vgic register access easier using one of the access
  198. * modes defined for vgic register access
  199. * (read,raz,write-ignored,setbit,clearbit,write)
  200. */
  201. static void vgic_reg_access(struct kvm_exit_mmio *mmio, u32 *reg,
  202. phys_addr_t offset, int mode)
  203. {
  204. int word_offset = (offset & 3) * 8;
  205. u32 mask = (1UL << (mmio->len * 8)) - 1;
  206. u32 regval;
  207. /*
  208. * Any alignment fault should have been delivered to the guest
  209. * directly (ARM ARM B3.12.7 "Prioritization of aborts").
  210. */
  211. if (reg) {
  212. regval = *reg;
  213. } else {
  214. BUG_ON(mode != (ACCESS_READ_RAZ | ACCESS_WRITE_IGNORED));
  215. regval = 0;
  216. }
  217. if (mmio->is_write) {
  218. u32 data = mmio_data_read(mmio, mask) << word_offset;
  219. switch (ACCESS_WRITE_MASK(mode)) {
  220. case ACCESS_WRITE_IGNORED:
  221. return;
  222. case ACCESS_WRITE_SETBIT:
  223. regval |= data;
  224. break;
  225. case ACCESS_WRITE_CLEARBIT:
  226. regval &= ~data;
  227. break;
  228. case ACCESS_WRITE_VALUE:
  229. regval = (regval & ~(mask << word_offset)) | data;
  230. break;
  231. }
  232. *reg = regval;
  233. } else {
  234. switch (ACCESS_READ_MASK(mode)) {
  235. case ACCESS_READ_RAZ:
  236. regval = 0;
  237. /* fall through */
  238. case ACCESS_READ_VALUE:
  239. mmio_data_write(mmio, mask, regval >> word_offset);
  240. }
  241. }
  242. }
  243. static bool handle_mmio_misc(struct kvm_vcpu *vcpu,
  244. struct kvm_exit_mmio *mmio, phys_addr_t offset)
  245. {
  246. u32 reg;
  247. u32 word_offset = offset & 3;
  248. switch (offset & ~3) {
  249. case 0: /* CTLR */
  250. reg = vcpu->kvm->arch.vgic.enabled;
  251. vgic_reg_access(mmio, &reg, word_offset,
  252. ACCESS_READ_VALUE | ACCESS_WRITE_VALUE);
  253. if (mmio->is_write) {
  254. vcpu->kvm->arch.vgic.enabled = reg & 1;
  255. vgic_update_state(vcpu->kvm);
  256. return true;
  257. }
  258. break;
  259. case 4: /* TYPER */
  260. reg = (atomic_read(&vcpu->kvm->online_vcpus) - 1) << 5;
  261. reg |= (VGIC_NR_IRQS >> 5) - 1;
  262. vgic_reg_access(mmio, &reg, word_offset,
  263. ACCESS_READ_VALUE | ACCESS_WRITE_IGNORED);
  264. break;
  265. case 8: /* IIDR */
  266. reg = 0x4B00043B;
  267. vgic_reg_access(mmio, &reg, word_offset,
  268. ACCESS_READ_VALUE | ACCESS_WRITE_IGNORED);
  269. break;
  270. }
  271. return false;
  272. }
  273. static bool handle_mmio_raz_wi(struct kvm_vcpu *vcpu,
  274. struct kvm_exit_mmio *mmio, phys_addr_t offset)
  275. {
  276. vgic_reg_access(mmio, NULL, offset,
  277. ACCESS_READ_RAZ | ACCESS_WRITE_IGNORED);
  278. return false;
  279. }
  280. static bool handle_mmio_set_enable_reg(struct kvm_vcpu *vcpu,
  281. struct kvm_exit_mmio *mmio,
  282. phys_addr_t offset)
  283. {
  284. u32 *reg = vgic_bitmap_get_reg(&vcpu->kvm->arch.vgic.irq_enabled,
  285. vcpu->vcpu_id, offset);
  286. vgic_reg_access(mmio, reg, offset,
  287. ACCESS_READ_VALUE | ACCESS_WRITE_SETBIT);
  288. if (mmio->is_write) {
  289. vgic_update_state(vcpu->kvm);
  290. return true;
  291. }
  292. return false;
  293. }
  294. static bool handle_mmio_clear_enable_reg(struct kvm_vcpu *vcpu,
  295. struct kvm_exit_mmio *mmio,
  296. phys_addr_t offset)
  297. {
  298. u32 *reg = vgic_bitmap_get_reg(&vcpu->kvm->arch.vgic.irq_enabled,
  299. vcpu->vcpu_id, offset);
  300. vgic_reg_access(mmio, reg, offset,
  301. ACCESS_READ_VALUE | ACCESS_WRITE_CLEARBIT);
  302. if (mmio->is_write) {
  303. if (offset < 4) /* Force SGI enabled */
  304. *reg |= 0xffff;
  305. vgic_retire_disabled_irqs(vcpu);
  306. vgic_update_state(vcpu->kvm);
  307. return true;
  308. }
  309. return false;
  310. }
  311. static bool handle_mmio_set_pending_reg(struct kvm_vcpu *vcpu,
  312. struct kvm_exit_mmio *mmio,
  313. phys_addr_t offset)
  314. {
  315. u32 *reg = vgic_bitmap_get_reg(&vcpu->kvm->arch.vgic.irq_state,
  316. vcpu->vcpu_id, offset);
  317. vgic_reg_access(mmio, reg, offset,
  318. ACCESS_READ_VALUE | ACCESS_WRITE_SETBIT);
  319. if (mmio->is_write) {
  320. vgic_update_state(vcpu->kvm);
  321. return true;
  322. }
  323. return false;
  324. }
  325. static bool handle_mmio_clear_pending_reg(struct kvm_vcpu *vcpu,
  326. struct kvm_exit_mmio *mmio,
  327. phys_addr_t offset)
  328. {
  329. u32 *reg = vgic_bitmap_get_reg(&vcpu->kvm->arch.vgic.irq_state,
  330. vcpu->vcpu_id, offset);
  331. vgic_reg_access(mmio, reg, offset,
  332. ACCESS_READ_VALUE | ACCESS_WRITE_CLEARBIT);
  333. if (mmio->is_write) {
  334. vgic_update_state(vcpu->kvm);
  335. return true;
  336. }
  337. return false;
  338. }
  339. static bool handle_mmio_priority_reg(struct kvm_vcpu *vcpu,
  340. struct kvm_exit_mmio *mmio,
  341. phys_addr_t offset)
  342. {
  343. u32 *reg = vgic_bytemap_get_reg(&vcpu->kvm->arch.vgic.irq_priority,
  344. vcpu->vcpu_id, offset);
  345. vgic_reg_access(mmio, reg, offset,
  346. ACCESS_READ_VALUE | ACCESS_WRITE_VALUE);
  347. return false;
  348. }
  349. #define GICD_ITARGETSR_SIZE 32
  350. #define GICD_CPUTARGETS_BITS 8
  351. #define GICD_IRQS_PER_ITARGETSR (GICD_ITARGETSR_SIZE / GICD_CPUTARGETS_BITS)
  352. static u32 vgic_get_target_reg(struct kvm *kvm, int irq)
  353. {
  354. struct vgic_dist *dist = &kvm->arch.vgic;
  355. struct kvm_vcpu *vcpu;
  356. int i, c;
  357. unsigned long *bmap;
  358. u32 val = 0;
  359. irq -= VGIC_NR_PRIVATE_IRQS;
  360. kvm_for_each_vcpu(c, vcpu, kvm) {
  361. bmap = vgic_bitmap_get_shared_map(&dist->irq_spi_target[c]);
  362. for (i = 0; i < GICD_IRQS_PER_ITARGETSR; i++)
  363. if (test_bit(irq + i, bmap))
  364. val |= 1 << (c + i * 8);
  365. }
  366. return val;
  367. }
  368. static void vgic_set_target_reg(struct kvm *kvm, u32 val, int irq)
  369. {
  370. struct vgic_dist *dist = &kvm->arch.vgic;
  371. struct kvm_vcpu *vcpu;
  372. int i, c;
  373. unsigned long *bmap;
  374. u32 target;
  375. irq -= VGIC_NR_PRIVATE_IRQS;
  376. /*
  377. * Pick the LSB in each byte. This ensures we target exactly
  378. * one vcpu per IRQ. If the byte is null, assume we target
  379. * CPU0.
  380. */
  381. for (i = 0; i < GICD_IRQS_PER_ITARGETSR; i++) {
  382. int shift = i * GICD_CPUTARGETS_BITS;
  383. target = ffs((val >> shift) & 0xffU);
  384. target = target ? (target - 1) : 0;
  385. dist->irq_spi_cpu[irq + i] = target;
  386. kvm_for_each_vcpu(c, vcpu, kvm) {
  387. bmap = vgic_bitmap_get_shared_map(&dist->irq_spi_target[c]);
  388. if (c == target)
  389. set_bit(irq + i, bmap);
  390. else
  391. clear_bit(irq + i, bmap);
  392. }
  393. }
  394. }
  395. static bool handle_mmio_target_reg(struct kvm_vcpu *vcpu,
  396. struct kvm_exit_mmio *mmio,
  397. phys_addr_t offset)
  398. {
  399. u32 reg;
  400. /* We treat the banked interrupts targets as read-only */
  401. if (offset < 32) {
  402. u32 roreg = 1 << vcpu->vcpu_id;
  403. roreg |= roreg << 8;
  404. roreg |= roreg << 16;
  405. vgic_reg_access(mmio, &roreg, offset,
  406. ACCESS_READ_VALUE | ACCESS_WRITE_IGNORED);
  407. return false;
  408. }
  409. reg = vgic_get_target_reg(vcpu->kvm, offset & ~3U);
  410. vgic_reg_access(mmio, &reg, offset,
  411. ACCESS_READ_VALUE | ACCESS_WRITE_VALUE);
  412. if (mmio->is_write) {
  413. vgic_set_target_reg(vcpu->kvm, reg, offset & ~3U);
  414. vgic_update_state(vcpu->kvm);
  415. return true;
  416. }
  417. return false;
  418. }
  419. static u32 vgic_cfg_expand(u16 val)
  420. {
  421. u32 res = 0;
  422. int i;
  423. /*
  424. * Turn a 16bit value like abcd...mnop into a 32bit word
  425. * a0b0c0d0...m0n0o0p0, which is what the HW cfg register is.
  426. */
  427. for (i = 0; i < 16; i++)
  428. res |= ((val >> i) & VGIC_CFG_EDGE) << (2 * i + 1);
  429. return res;
  430. }
  431. static u16 vgic_cfg_compress(u32 val)
  432. {
  433. u16 res = 0;
  434. int i;
  435. /*
  436. * Turn a 32bit word a0b0c0d0...m0n0o0p0 into 16bit value like
  437. * abcd...mnop which is what we really care about.
  438. */
  439. for (i = 0; i < 16; i++)
  440. res |= ((val >> (i * 2 + 1)) & VGIC_CFG_EDGE) << i;
  441. return res;
  442. }
  443. /*
  444. * The distributor uses 2 bits per IRQ for the CFG register, but the
  445. * LSB is always 0. As such, we only keep the upper bit, and use the
  446. * two above functions to compress/expand the bits
  447. */
  448. static bool handle_mmio_cfg_reg(struct kvm_vcpu *vcpu,
  449. struct kvm_exit_mmio *mmio, phys_addr_t offset)
  450. {
  451. u32 val;
  452. u32 *reg = vgic_bitmap_get_reg(&vcpu->kvm->arch.vgic.irq_cfg,
  453. vcpu->vcpu_id, offset >> 1);
  454. if (offset & 2)
  455. val = *reg >> 16;
  456. else
  457. val = *reg & 0xffff;
  458. val = vgic_cfg_expand(val);
  459. vgic_reg_access(mmio, &val, offset,
  460. ACCESS_READ_VALUE | ACCESS_WRITE_VALUE);
  461. if (mmio->is_write) {
  462. if (offset < 4) {
  463. *reg = ~0U; /* Force PPIs/SGIs to 1 */
  464. return false;
  465. }
  466. val = vgic_cfg_compress(val);
  467. if (offset & 2) {
  468. *reg &= 0xffff;
  469. *reg |= val << 16;
  470. } else {
  471. *reg &= 0xffff << 16;
  472. *reg |= val;
  473. }
  474. }
  475. return false;
  476. }
  477. static bool handle_mmio_sgi_reg(struct kvm_vcpu *vcpu,
  478. struct kvm_exit_mmio *mmio, phys_addr_t offset)
  479. {
  480. u32 reg;
  481. vgic_reg_access(mmio, &reg, offset,
  482. ACCESS_READ_RAZ | ACCESS_WRITE_VALUE);
  483. if (mmio->is_write) {
  484. vgic_dispatch_sgi(vcpu, reg);
  485. vgic_update_state(vcpu->kvm);
  486. return true;
  487. }
  488. return false;
  489. }
  490. /*
  491. * I would have liked to use the kvm_bus_io_*() API instead, but it
  492. * cannot cope with banked registers (only the VM pointer is passed
  493. * around, and we need the vcpu). One of these days, someone please
  494. * fix it!
  495. */
  496. struct mmio_range {
  497. phys_addr_t base;
  498. unsigned long len;
  499. bool (*handle_mmio)(struct kvm_vcpu *vcpu, struct kvm_exit_mmio *mmio,
  500. phys_addr_t offset);
  501. };
  502. static const struct mmio_range vgic_ranges[] = {
  503. {
  504. .base = GIC_DIST_CTRL,
  505. .len = 12,
  506. .handle_mmio = handle_mmio_misc,
  507. },
  508. {
  509. .base = GIC_DIST_IGROUP,
  510. .len = VGIC_NR_IRQS / 8,
  511. .handle_mmio = handle_mmio_raz_wi,
  512. },
  513. {
  514. .base = GIC_DIST_ENABLE_SET,
  515. .len = VGIC_NR_IRQS / 8,
  516. .handle_mmio = handle_mmio_set_enable_reg,
  517. },
  518. {
  519. .base = GIC_DIST_ENABLE_CLEAR,
  520. .len = VGIC_NR_IRQS / 8,
  521. .handle_mmio = handle_mmio_clear_enable_reg,
  522. },
  523. {
  524. .base = GIC_DIST_PENDING_SET,
  525. .len = VGIC_NR_IRQS / 8,
  526. .handle_mmio = handle_mmio_set_pending_reg,
  527. },
  528. {
  529. .base = GIC_DIST_PENDING_CLEAR,
  530. .len = VGIC_NR_IRQS / 8,
  531. .handle_mmio = handle_mmio_clear_pending_reg,
  532. },
  533. {
  534. .base = GIC_DIST_ACTIVE_SET,
  535. .len = VGIC_NR_IRQS / 8,
  536. .handle_mmio = handle_mmio_raz_wi,
  537. },
  538. {
  539. .base = GIC_DIST_ACTIVE_CLEAR,
  540. .len = VGIC_NR_IRQS / 8,
  541. .handle_mmio = handle_mmio_raz_wi,
  542. },
  543. {
  544. .base = GIC_DIST_PRI,
  545. .len = VGIC_NR_IRQS,
  546. .handle_mmio = handle_mmio_priority_reg,
  547. },
  548. {
  549. .base = GIC_DIST_TARGET,
  550. .len = VGIC_NR_IRQS,
  551. .handle_mmio = handle_mmio_target_reg,
  552. },
  553. {
  554. .base = GIC_DIST_CONFIG,
  555. .len = VGIC_NR_IRQS / 4,
  556. .handle_mmio = handle_mmio_cfg_reg,
  557. },
  558. {
  559. .base = GIC_DIST_SOFTINT,
  560. .len = 4,
  561. .handle_mmio = handle_mmio_sgi_reg,
  562. },
  563. {}
  564. };
  565. static const
  566. struct mmio_range *find_matching_range(const struct mmio_range *ranges,
  567. struct kvm_exit_mmio *mmio,
  568. phys_addr_t base)
  569. {
  570. const struct mmio_range *r = ranges;
  571. phys_addr_t addr = mmio->phys_addr - base;
  572. while (r->len) {
  573. if (addr >= r->base &&
  574. (addr + mmio->len) <= (r->base + r->len))
  575. return r;
  576. r++;
  577. }
  578. return NULL;
  579. }
  580. /**
  581. * vgic_handle_mmio - handle an in-kernel MMIO access
  582. * @vcpu: pointer to the vcpu performing the access
  583. * @run: pointer to the kvm_run structure
  584. * @mmio: pointer to the data describing the access
  585. *
  586. * returns true if the MMIO access has been performed in kernel space,
  587. * and false if it needs to be emulated in user space.
  588. */
  589. bool vgic_handle_mmio(struct kvm_vcpu *vcpu, struct kvm_run *run,
  590. struct kvm_exit_mmio *mmio)
  591. {
  592. const struct mmio_range *range;
  593. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  594. unsigned long base = dist->vgic_dist_base;
  595. bool updated_state;
  596. unsigned long offset;
  597. if (!irqchip_in_kernel(vcpu->kvm) ||
  598. mmio->phys_addr < base ||
  599. (mmio->phys_addr + mmio->len) > (base + KVM_VGIC_V2_DIST_SIZE))
  600. return false;
  601. /* We don't support ldrd / strd or ldm / stm to the emulated vgic */
  602. if (mmio->len > 4) {
  603. kvm_inject_dabt(vcpu, mmio->phys_addr);
  604. return true;
  605. }
  606. range = find_matching_range(vgic_ranges, mmio, base);
  607. if (unlikely(!range || !range->handle_mmio)) {
  608. pr_warn("Unhandled access %d %08llx %d\n",
  609. mmio->is_write, mmio->phys_addr, mmio->len);
  610. return false;
  611. }
  612. spin_lock(&vcpu->kvm->arch.vgic.lock);
  613. offset = mmio->phys_addr - range->base - base;
  614. updated_state = range->handle_mmio(vcpu, mmio, offset);
  615. spin_unlock(&vcpu->kvm->arch.vgic.lock);
  616. kvm_prepare_mmio(run, mmio);
  617. kvm_handle_mmio_return(vcpu, run);
  618. return true;
  619. }
  620. static void vgic_dispatch_sgi(struct kvm_vcpu *vcpu, u32 reg)
  621. {
  622. struct kvm *kvm = vcpu->kvm;
  623. struct vgic_dist *dist = &kvm->arch.vgic;
  624. int nrcpus = atomic_read(&kvm->online_vcpus);
  625. u8 target_cpus;
  626. int sgi, mode, c, vcpu_id;
  627. vcpu_id = vcpu->vcpu_id;
  628. sgi = reg & 0xf;
  629. target_cpus = (reg >> 16) & 0xff;
  630. mode = (reg >> 24) & 3;
  631. switch (mode) {
  632. case 0:
  633. if (!target_cpus)
  634. return;
  635. case 1:
  636. target_cpus = ((1 << nrcpus) - 1) & ~(1 << vcpu_id) & 0xff;
  637. break;
  638. case 2:
  639. target_cpus = 1 << vcpu_id;
  640. break;
  641. }
  642. kvm_for_each_vcpu(c, vcpu, kvm) {
  643. if (target_cpus & 1) {
  644. /* Flag the SGI as pending */
  645. vgic_dist_irq_set(vcpu, sgi);
  646. dist->irq_sgi_sources[c][sgi] |= 1 << vcpu_id;
  647. kvm_debug("SGI%d from CPU%d to CPU%d\n", sgi, vcpu_id, c);
  648. }
  649. target_cpus >>= 1;
  650. }
  651. }
  652. static int compute_pending_for_cpu(struct kvm_vcpu *vcpu)
  653. {
  654. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  655. unsigned long *pending, *enabled, *pend_percpu, *pend_shared;
  656. unsigned long pending_private, pending_shared;
  657. int vcpu_id;
  658. vcpu_id = vcpu->vcpu_id;
  659. pend_percpu = vcpu->arch.vgic_cpu.pending_percpu;
  660. pend_shared = vcpu->arch.vgic_cpu.pending_shared;
  661. pending = vgic_bitmap_get_cpu_map(&dist->irq_state, vcpu_id);
  662. enabled = vgic_bitmap_get_cpu_map(&dist->irq_enabled, vcpu_id);
  663. bitmap_and(pend_percpu, pending, enabled, VGIC_NR_PRIVATE_IRQS);
  664. pending = vgic_bitmap_get_shared_map(&dist->irq_state);
  665. enabled = vgic_bitmap_get_shared_map(&dist->irq_enabled);
  666. bitmap_and(pend_shared, pending, enabled, VGIC_NR_SHARED_IRQS);
  667. bitmap_and(pend_shared, pend_shared,
  668. vgic_bitmap_get_shared_map(&dist->irq_spi_target[vcpu_id]),
  669. VGIC_NR_SHARED_IRQS);
  670. pending_private = find_first_bit(pend_percpu, VGIC_NR_PRIVATE_IRQS);
  671. pending_shared = find_first_bit(pend_shared, VGIC_NR_SHARED_IRQS);
  672. return (pending_private < VGIC_NR_PRIVATE_IRQS ||
  673. pending_shared < VGIC_NR_SHARED_IRQS);
  674. }
  675. /*
  676. * Update the interrupt state and determine which CPUs have pending
  677. * interrupts. Must be called with distributor lock held.
  678. */
  679. static void vgic_update_state(struct kvm *kvm)
  680. {
  681. struct vgic_dist *dist = &kvm->arch.vgic;
  682. struct kvm_vcpu *vcpu;
  683. int c;
  684. if (!dist->enabled) {
  685. set_bit(0, &dist->irq_pending_on_cpu);
  686. return;
  687. }
  688. kvm_for_each_vcpu(c, vcpu, kvm) {
  689. if (compute_pending_for_cpu(vcpu)) {
  690. pr_debug("CPU%d has pending interrupts\n", c);
  691. set_bit(c, &dist->irq_pending_on_cpu);
  692. }
  693. }
  694. }
  695. #define LR_CPUID(lr) \
  696. (((lr) & GICH_LR_PHYSID_CPUID) >> GICH_LR_PHYSID_CPUID_SHIFT)
  697. #define MK_LR_PEND(src, irq) \
  698. (GICH_LR_PENDING_BIT | ((src) << GICH_LR_PHYSID_CPUID_SHIFT) | (irq))
  699. /*
  700. * An interrupt may have been disabled after being made pending on the
  701. * CPU interface (the classic case is a timer running while we're
  702. * rebooting the guest - the interrupt would kick as soon as the CPU
  703. * interface gets enabled, with deadly consequences).
  704. *
  705. * The solution is to examine already active LRs, and check the
  706. * interrupt is still enabled. If not, just retire it.
  707. */
  708. static void vgic_retire_disabled_irqs(struct kvm_vcpu *vcpu)
  709. {
  710. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  711. int lr;
  712. for_each_set_bit(lr, vgic_cpu->lr_used, vgic_cpu->nr_lr) {
  713. int irq = vgic_cpu->vgic_lr[lr] & GICH_LR_VIRTUALID;
  714. if (!vgic_irq_is_enabled(vcpu, irq)) {
  715. vgic_cpu->vgic_irq_lr_map[irq] = LR_EMPTY;
  716. clear_bit(lr, vgic_cpu->lr_used);
  717. vgic_cpu->vgic_lr[lr] &= ~GICH_LR_STATE;
  718. if (vgic_irq_is_active(vcpu, irq))
  719. vgic_irq_clear_active(vcpu, irq);
  720. }
  721. }
  722. }
  723. /*
  724. * Queue an interrupt to a CPU virtual interface. Return true on success,
  725. * or false if it wasn't possible to queue it.
  726. */
  727. static bool vgic_queue_irq(struct kvm_vcpu *vcpu, u8 sgi_source_id, int irq)
  728. {
  729. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  730. int lr;
  731. /* Sanitize the input... */
  732. BUG_ON(sgi_source_id & ~7);
  733. BUG_ON(sgi_source_id && irq >= VGIC_NR_SGIS);
  734. BUG_ON(irq >= VGIC_NR_IRQS);
  735. kvm_debug("Queue IRQ%d\n", irq);
  736. lr = vgic_cpu->vgic_irq_lr_map[irq];
  737. /* Do we have an active interrupt for the same CPUID? */
  738. if (lr != LR_EMPTY &&
  739. (LR_CPUID(vgic_cpu->vgic_lr[lr]) == sgi_source_id)) {
  740. kvm_debug("LR%d piggyback for IRQ%d %x\n",
  741. lr, irq, vgic_cpu->vgic_lr[lr]);
  742. BUG_ON(!test_bit(lr, vgic_cpu->lr_used));
  743. vgic_cpu->vgic_lr[lr] |= GICH_LR_PENDING_BIT;
  744. goto out;
  745. }
  746. /* Try to use another LR for this interrupt */
  747. lr = find_first_zero_bit((unsigned long *)vgic_cpu->lr_used,
  748. vgic_cpu->nr_lr);
  749. if (lr >= vgic_cpu->nr_lr)
  750. return false;
  751. kvm_debug("LR%d allocated for IRQ%d %x\n", lr, irq, sgi_source_id);
  752. vgic_cpu->vgic_lr[lr] = MK_LR_PEND(sgi_source_id, irq);
  753. vgic_cpu->vgic_irq_lr_map[irq] = lr;
  754. set_bit(lr, vgic_cpu->lr_used);
  755. out:
  756. if (!vgic_irq_is_edge(vcpu, irq))
  757. vgic_cpu->vgic_lr[lr] |= GICH_LR_EOI;
  758. return true;
  759. }
  760. static bool vgic_queue_sgi(struct kvm_vcpu *vcpu, int irq)
  761. {
  762. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  763. unsigned long sources;
  764. int vcpu_id = vcpu->vcpu_id;
  765. int c;
  766. sources = dist->irq_sgi_sources[vcpu_id][irq];
  767. for_each_set_bit(c, &sources, VGIC_MAX_CPUS) {
  768. if (vgic_queue_irq(vcpu, c, irq))
  769. clear_bit(c, &sources);
  770. }
  771. dist->irq_sgi_sources[vcpu_id][irq] = sources;
  772. /*
  773. * If the sources bitmap has been cleared it means that we
  774. * could queue all the SGIs onto link registers (see the
  775. * clear_bit above), and therefore we are done with them in
  776. * our emulated gic and can get rid of them.
  777. */
  778. if (!sources) {
  779. vgic_dist_irq_clear(vcpu, irq);
  780. vgic_cpu_irq_clear(vcpu, irq);
  781. return true;
  782. }
  783. return false;
  784. }
  785. static bool vgic_queue_hwirq(struct kvm_vcpu *vcpu, int irq)
  786. {
  787. if (vgic_irq_is_active(vcpu, irq))
  788. return true; /* level interrupt, already queued */
  789. if (vgic_queue_irq(vcpu, 0, irq)) {
  790. if (vgic_irq_is_edge(vcpu, irq)) {
  791. vgic_dist_irq_clear(vcpu, irq);
  792. vgic_cpu_irq_clear(vcpu, irq);
  793. } else {
  794. vgic_irq_set_active(vcpu, irq);
  795. }
  796. return true;
  797. }
  798. return false;
  799. }
  800. /*
  801. * Fill the list registers with pending interrupts before running the
  802. * guest.
  803. */
  804. static void __kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
  805. {
  806. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  807. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  808. int i, vcpu_id;
  809. int overflow = 0;
  810. vcpu_id = vcpu->vcpu_id;
  811. /*
  812. * We may not have any pending interrupt, or the interrupts
  813. * may have been serviced from another vcpu. In all cases,
  814. * move along.
  815. */
  816. if (!kvm_vgic_vcpu_pending_irq(vcpu)) {
  817. pr_debug("CPU%d has no pending interrupt\n", vcpu_id);
  818. goto epilog;
  819. }
  820. /* SGIs */
  821. for_each_set_bit(i, vgic_cpu->pending_percpu, VGIC_NR_SGIS) {
  822. if (!vgic_queue_sgi(vcpu, i))
  823. overflow = 1;
  824. }
  825. /* PPIs */
  826. for_each_set_bit_from(i, vgic_cpu->pending_percpu, VGIC_NR_PRIVATE_IRQS) {
  827. if (!vgic_queue_hwirq(vcpu, i))
  828. overflow = 1;
  829. }
  830. /* SPIs */
  831. for_each_set_bit(i, vgic_cpu->pending_shared, VGIC_NR_SHARED_IRQS) {
  832. if (!vgic_queue_hwirq(vcpu, i + VGIC_NR_PRIVATE_IRQS))
  833. overflow = 1;
  834. }
  835. epilog:
  836. if (overflow) {
  837. vgic_cpu->vgic_hcr |= GICH_HCR_UIE;
  838. } else {
  839. vgic_cpu->vgic_hcr &= ~GICH_HCR_UIE;
  840. /*
  841. * We're about to run this VCPU, and we've consumed
  842. * everything the distributor had in store for
  843. * us. Claim we don't have anything pending. We'll
  844. * adjust that if needed while exiting.
  845. */
  846. clear_bit(vcpu_id, &dist->irq_pending_on_cpu);
  847. }
  848. }
  849. static bool vgic_process_maintenance(struct kvm_vcpu *vcpu)
  850. {
  851. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  852. bool level_pending = false;
  853. kvm_debug("MISR = %08x\n", vgic_cpu->vgic_misr);
  854. /*
  855. * We do not need to take the distributor lock here, since the only
  856. * action we perform is clearing the irq_active_bit for an EOIed
  857. * level interrupt. There is a potential race with
  858. * the queuing of an interrupt in __kvm_vgic_flush_hwstate(), where we
  859. * check if the interrupt is already active. Two possibilities:
  860. *
  861. * - The queuing is occurring on the same vcpu: cannot happen,
  862. * as we're already in the context of this vcpu, and
  863. * executing the handler
  864. * - The interrupt has been migrated to another vcpu, and we
  865. * ignore this interrupt for this run. Big deal. It is still
  866. * pending though, and will get considered when this vcpu
  867. * exits.
  868. */
  869. if (vgic_cpu->vgic_misr & GICH_MISR_EOI) {
  870. /*
  871. * Some level interrupts have been EOIed. Clear their
  872. * active bit.
  873. */
  874. int lr, irq;
  875. for_each_set_bit(lr, (unsigned long *)vgic_cpu->vgic_eisr,
  876. vgic_cpu->nr_lr) {
  877. irq = vgic_cpu->vgic_lr[lr] & GICH_LR_VIRTUALID;
  878. vgic_irq_clear_active(vcpu, irq);
  879. vgic_cpu->vgic_lr[lr] &= ~GICH_LR_EOI;
  880. /* Any additional pending interrupt? */
  881. if (vgic_dist_irq_is_pending(vcpu, irq)) {
  882. vgic_cpu_irq_set(vcpu, irq);
  883. level_pending = true;
  884. } else {
  885. vgic_cpu_irq_clear(vcpu, irq);
  886. }
  887. }
  888. }
  889. if (vgic_cpu->vgic_misr & GICH_MISR_U)
  890. vgic_cpu->vgic_hcr &= ~GICH_HCR_UIE;
  891. return level_pending;
  892. }
  893. /*
  894. * Sync back the VGIC state after a guest run. We do not really touch
  895. * the distributor here (the irq_pending_on_cpu bit is safe to set),
  896. * so there is no need for taking its lock.
  897. */
  898. static void __kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
  899. {
  900. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  901. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  902. int lr, pending;
  903. bool level_pending;
  904. level_pending = vgic_process_maintenance(vcpu);
  905. /* Clear mappings for empty LRs */
  906. for_each_set_bit(lr, (unsigned long *)vgic_cpu->vgic_elrsr,
  907. vgic_cpu->nr_lr) {
  908. int irq;
  909. if (!test_and_clear_bit(lr, vgic_cpu->lr_used))
  910. continue;
  911. irq = vgic_cpu->vgic_lr[lr] & GICH_LR_VIRTUALID;
  912. BUG_ON(irq >= VGIC_NR_IRQS);
  913. vgic_cpu->vgic_irq_lr_map[irq] = LR_EMPTY;
  914. }
  915. /* Check if we still have something up our sleeve... */
  916. pending = find_first_zero_bit((unsigned long *)vgic_cpu->vgic_elrsr,
  917. vgic_cpu->nr_lr);
  918. if (level_pending || pending < vgic_cpu->nr_lr)
  919. set_bit(vcpu->vcpu_id, &dist->irq_pending_on_cpu);
  920. }
  921. void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
  922. {
  923. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  924. if (!irqchip_in_kernel(vcpu->kvm))
  925. return;
  926. spin_lock(&dist->lock);
  927. __kvm_vgic_flush_hwstate(vcpu);
  928. spin_unlock(&dist->lock);
  929. }
  930. void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
  931. {
  932. if (!irqchip_in_kernel(vcpu->kvm))
  933. return;
  934. __kvm_vgic_sync_hwstate(vcpu);
  935. }
  936. int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu)
  937. {
  938. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  939. if (!irqchip_in_kernel(vcpu->kvm))
  940. return 0;
  941. return test_bit(vcpu->vcpu_id, &dist->irq_pending_on_cpu);
  942. }
  943. static bool vgic_ioaddr_overlap(struct kvm *kvm)
  944. {
  945. phys_addr_t dist = kvm->arch.vgic.vgic_dist_base;
  946. phys_addr_t cpu = kvm->arch.vgic.vgic_cpu_base;
  947. if (IS_VGIC_ADDR_UNDEF(dist) || IS_VGIC_ADDR_UNDEF(cpu))
  948. return 0;
  949. if ((dist <= cpu && dist + KVM_VGIC_V2_DIST_SIZE > cpu) ||
  950. (cpu <= dist && cpu + KVM_VGIC_V2_CPU_SIZE > dist))
  951. return -EBUSY;
  952. return 0;
  953. }
  954. static int vgic_ioaddr_assign(struct kvm *kvm, phys_addr_t *ioaddr,
  955. phys_addr_t addr, phys_addr_t size)
  956. {
  957. int ret;
  958. if (!IS_VGIC_ADDR_UNDEF(*ioaddr))
  959. return -EEXIST;
  960. if (addr + size < addr)
  961. return -EINVAL;
  962. ret = vgic_ioaddr_overlap(kvm);
  963. if (ret)
  964. return ret;
  965. *ioaddr = addr;
  966. return ret;
  967. }
  968. int kvm_vgic_set_addr(struct kvm *kvm, unsigned long type, u64 addr)
  969. {
  970. int r = 0;
  971. struct vgic_dist *vgic = &kvm->arch.vgic;
  972. if (addr & ~KVM_PHYS_MASK)
  973. return -E2BIG;
  974. if (addr & ~PAGE_MASK)
  975. return -EINVAL;
  976. mutex_lock(&kvm->lock);
  977. switch (type) {
  978. case KVM_VGIC_V2_ADDR_TYPE_DIST:
  979. r = vgic_ioaddr_assign(kvm, &vgic->vgic_dist_base,
  980. addr, KVM_VGIC_V2_DIST_SIZE);
  981. break;
  982. case KVM_VGIC_V2_ADDR_TYPE_CPU:
  983. r = vgic_ioaddr_assign(kvm, &vgic->vgic_cpu_base,
  984. addr, KVM_VGIC_V2_CPU_SIZE);
  985. break;
  986. default:
  987. r = -ENODEV;
  988. }
  989. mutex_unlock(&kvm->lock);
  990. return r;
  991. }