1 /*
2 * VGIC MMIO handling functions
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14 #include <linux/bitops.h>
15 #include <linux/bsearch.h>
16 #include <linux/kvm.h>
17 #include <linux/kvm_host.h>
18 #include <kvm/iodev.h>
19 #include <kvm/arm_vgic.h>
20
21 #include "vgic.h"
22 #include "vgic-mmio.h"
23
vgic_mmio_read_raz(struct kvm_vcpu * vcpu,gpa_t addr,unsigned int len)24 unsigned long vgic_mmio_read_raz(struct kvm_vcpu *vcpu,
25 gpa_t addr, unsigned int len)
26 {
27 return 0;
28 }
29
vgic_mmio_read_rao(struct kvm_vcpu * vcpu,gpa_t addr,unsigned int len)30 unsigned long vgic_mmio_read_rao(struct kvm_vcpu *vcpu,
31 gpa_t addr, unsigned int len)
32 {
33 return -1UL;
34 }
35
vgic_mmio_write_wi(struct kvm_vcpu * vcpu,gpa_t addr,unsigned int len,unsigned long val)36 void vgic_mmio_write_wi(struct kvm_vcpu *vcpu, gpa_t addr,
37 unsigned int len, unsigned long val)
38 {
39 /* Ignore */
40 }
41
42 /*
43 * Read accesses to both GICD_ICENABLER and GICD_ISENABLER return the value
44 * of the enabled bit, so there is only one function for both here.
45 */
vgic_mmio_read_enable(struct kvm_vcpu * vcpu,gpa_t addr,unsigned int len)46 unsigned long vgic_mmio_read_enable(struct kvm_vcpu *vcpu,
47 gpa_t addr, unsigned int len)
48 {
49 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
50 u32 value = 0;
51 int i;
52
53 /* Loop over all IRQs affected by this read */
54 for (i = 0; i < len * 8; i++) {
55 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
56
57 if (irq->enabled)
58 value |= (1U << i);
59
60 vgic_put_irq(vcpu->kvm, irq);
61 }
62
63 return value;
64 }
65
vgic_mmio_write_senable(struct kvm_vcpu * vcpu,gpa_t addr,unsigned int len,unsigned long val)66 void vgic_mmio_write_senable(struct kvm_vcpu *vcpu,
67 gpa_t addr, unsigned int len,
68 unsigned long val)
69 {
70 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
71 int i;
72
73 for_each_set_bit(i, &val, len * 8) {
74 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
75
76 spin_lock(&irq->irq_lock);
77 irq->enabled = true;
78 vgic_queue_irq_unlock(vcpu->kvm, irq);
79
80 vgic_put_irq(vcpu->kvm, irq);
81 }
82 }
83
vgic_mmio_write_cenable(struct kvm_vcpu * vcpu,gpa_t addr,unsigned int len,unsigned long val)84 void vgic_mmio_write_cenable(struct kvm_vcpu *vcpu,
85 gpa_t addr, unsigned int len,
86 unsigned long val)
87 {
88 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
89 int i;
90
91 for_each_set_bit(i, &val, len * 8) {
92 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
93
94 spin_lock(&irq->irq_lock);
95
96 irq->enabled = false;
97
98 spin_unlock(&irq->irq_lock);
99 vgic_put_irq(vcpu->kvm, irq);
100 }
101 }
102
vgic_mmio_read_pending(struct kvm_vcpu * vcpu,gpa_t addr,unsigned int len)103 unsigned long vgic_mmio_read_pending(struct kvm_vcpu *vcpu,
104 gpa_t addr, unsigned int len)
105 {
106 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
107 u32 value = 0;
108 int i;
109
110 /* Loop over all IRQs affected by this read */
111 for (i = 0; i < len * 8; i++) {
112 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
113
114 if (irq->pending)
115 value |= (1U << i);
116
117 vgic_put_irq(vcpu->kvm, irq);
118 }
119
120 return value;
121 }
122
vgic_mmio_write_spending(struct kvm_vcpu * vcpu,gpa_t addr,unsigned int len,unsigned long val)123 void vgic_mmio_write_spending(struct kvm_vcpu *vcpu,
124 gpa_t addr, unsigned int len,
125 unsigned long val)
126 {
127 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
128 int i;
129
130 for_each_set_bit(i, &val, len * 8) {
131 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
132
133 spin_lock(&irq->irq_lock);
134 irq->pending = true;
135 if (irq->config == VGIC_CONFIG_LEVEL)
136 irq->soft_pending = true;
137
138 vgic_queue_irq_unlock(vcpu->kvm, irq);
139 vgic_put_irq(vcpu->kvm, irq);
140 }
141 }
142
vgic_mmio_write_cpending(struct kvm_vcpu * vcpu,gpa_t addr,unsigned int len,unsigned long val)143 void vgic_mmio_write_cpending(struct kvm_vcpu *vcpu,
144 gpa_t addr, unsigned int len,
145 unsigned long val)
146 {
147 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
148 int i;
149
150 for_each_set_bit(i, &val, len * 8) {
151 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
152
153 spin_lock(&irq->irq_lock);
154
155 if (irq->config == VGIC_CONFIG_LEVEL) {
156 irq->soft_pending = false;
157 irq->pending = irq->line_level;
158 } else {
159 irq->pending = false;
160 }
161
162 spin_unlock(&irq->irq_lock);
163 vgic_put_irq(vcpu->kvm, irq);
164 }
165 }
166
vgic_mmio_read_active(struct kvm_vcpu * vcpu,gpa_t addr,unsigned int len)167 unsigned long vgic_mmio_read_active(struct kvm_vcpu *vcpu,
168 gpa_t addr, unsigned int len)
169 {
170 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
171 u32 value = 0;
172 int i;
173
174 /* Loop over all IRQs affected by this read */
175 for (i = 0; i < len * 8; i++) {
176 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
177
178 if (irq->active)
179 value |= (1U << i);
180
181 vgic_put_irq(vcpu->kvm, irq);
182 }
183
184 return value;
185 }
186
vgic_mmio_change_active(struct kvm_vcpu * vcpu,struct vgic_irq * irq,bool new_active_state)187 static void vgic_mmio_change_active(struct kvm_vcpu *vcpu, struct vgic_irq *irq,
188 bool new_active_state)
189 {
190 struct kvm_vcpu *requester_vcpu;
191 spin_lock(&irq->irq_lock);
192
193 /*
194 * The vcpu parameter here can mean multiple things depending on how
195 * this function is called; when handling a trap from the kernel it
196 * depends on the GIC version, and these functions are also called as
197 * part of save/restore from userspace.
198 *
199 * Therefore, we have to figure out the requester in a reliable way.
200 *
201 * When accessing VGIC state from user space, the requester_vcpu is
202 * NULL, which is fine, because we guarantee that no VCPUs are running
203 * when accessing VGIC state from user space so irq->vcpu->cpu is
204 * always -1.
205 */
206 requester_vcpu = kvm_arm_get_running_vcpu();
207
208 /*
209 * If this virtual IRQ was written into a list register, we
210 * have to make sure the CPU that runs the VCPU thread has
211 * synced back the LR state to the struct vgic_irq.
212 *
213 * As long as the conditions below are true, we know the VCPU thread
214 * may be on its way back from the guest (we kicked the VCPU thread in
215 * vgic_change_active_prepare) and still has to sync back this IRQ,
216 * so we release and re-acquire the spin_lock to let the other thread
217 * sync back the IRQ.
218 */
219 while (irq->vcpu && /* IRQ may have state in an LR somewhere */
220 irq->vcpu != requester_vcpu && /* Current thread is not the VCPU thread */
221 irq->vcpu->cpu != -1) /* VCPU thread is running */
222 cond_resched_lock(&irq->irq_lock);
223
224 irq->active = new_active_state;
225 if (new_active_state)
226 vgic_queue_irq_unlock(vcpu->kvm, irq);
227 else
228 spin_unlock(&irq->irq_lock);
229 }
230
231 /*
232 * If we are fiddling with an IRQ's active state, we have to make sure the IRQ
233 * is not queued on some running VCPU's LRs, because then the change to the
234 * active state can be overwritten when the VCPU's state is synced coming back
235 * from the guest.
236 *
237 * For shared interrupts, we have to stop all the VCPUs because interrupts can
238 * be migrated while we don't hold the IRQ locks and we don't want to be
239 * chasing moving targets.
240 *
241 * For private interrupts, we only have to make sure the single and only VCPU
242 * that can potentially queue the IRQ is stopped.
243 */
vgic_change_active_prepare(struct kvm_vcpu * vcpu,u32 intid)244 static void vgic_change_active_prepare(struct kvm_vcpu *vcpu, u32 intid)
245 {
246 if (intid < VGIC_NR_PRIVATE_IRQS)
247 kvm_arm_halt_vcpu(vcpu);
248 else
249 kvm_arm_halt_guest(vcpu->kvm);
250 }
251
252 /* See vgic_change_active_prepare */
vgic_change_active_finish(struct kvm_vcpu * vcpu,u32 intid)253 static void vgic_change_active_finish(struct kvm_vcpu *vcpu, u32 intid)
254 {
255 if (intid < VGIC_NR_PRIVATE_IRQS)
256 kvm_arm_resume_vcpu(vcpu);
257 else
258 kvm_arm_resume_guest(vcpu->kvm);
259 }
260
vgic_mmio_write_cactive(struct kvm_vcpu * vcpu,gpa_t addr,unsigned int len,unsigned long val)261 void vgic_mmio_write_cactive(struct kvm_vcpu *vcpu,
262 gpa_t addr, unsigned int len,
263 unsigned long val)
264 {
265 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
266 int i;
267
268 vgic_change_active_prepare(vcpu, intid);
269 for_each_set_bit(i, &val, len * 8) {
270 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
271 vgic_mmio_change_active(vcpu, irq, false);
272 vgic_put_irq(vcpu->kvm, irq);
273 }
274 vgic_change_active_finish(vcpu, intid);
275 }
276
vgic_mmio_write_sactive(struct kvm_vcpu * vcpu,gpa_t addr,unsigned int len,unsigned long val)277 void vgic_mmio_write_sactive(struct kvm_vcpu *vcpu,
278 gpa_t addr, unsigned int len,
279 unsigned long val)
280 {
281 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
282 int i;
283
284 vgic_change_active_prepare(vcpu, intid);
285 for_each_set_bit(i, &val, len * 8) {
286 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
287 vgic_mmio_change_active(vcpu, irq, true);
288 vgic_put_irq(vcpu->kvm, irq);
289 }
290 vgic_change_active_finish(vcpu, intid);
291 }
292
vgic_mmio_read_priority(struct kvm_vcpu * vcpu,gpa_t addr,unsigned int len)293 unsigned long vgic_mmio_read_priority(struct kvm_vcpu *vcpu,
294 gpa_t addr, unsigned int len)
295 {
296 u32 intid = VGIC_ADDR_TO_INTID(addr, 8);
297 int i;
298 u64 val = 0;
299
300 for (i = 0; i < len; i++) {
301 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
302
303 val |= (u64)irq->priority << (i * 8);
304
305 vgic_put_irq(vcpu->kvm, irq);
306 }
307
308 return val;
309 }
310
311 /*
312 * We currently don't handle changing the priority of an interrupt that
313 * is already pending on a VCPU. If there is a need for this, we would
314 * need to make this VCPU exit and re-evaluate the priorities, potentially
315 * leading to this interrupt getting presented now to the guest (if it has
316 * been masked by the priority mask before).
317 */
vgic_mmio_write_priority(struct kvm_vcpu * vcpu,gpa_t addr,unsigned int len,unsigned long val)318 void vgic_mmio_write_priority(struct kvm_vcpu *vcpu,
319 gpa_t addr, unsigned int len,
320 unsigned long val)
321 {
322 u32 intid = VGIC_ADDR_TO_INTID(addr, 8);
323 int i;
324
325 for (i = 0; i < len; i++) {
326 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
327
328 spin_lock(&irq->irq_lock);
329 /* Narrow the priority range to what we actually support */
330 irq->priority = (val >> (i * 8)) & GENMASK(7, 8 - VGIC_PRI_BITS);
331 spin_unlock(&irq->irq_lock);
332
333 vgic_put_irq(vcpu->kvm, irq);
334 }
335 }
336
vgic_mmio_read_config(struct kvm_vcpu * vcpu,gpa_t addr,unsigned int len)337 unsigned long vgic_mmio_read_config(struct kvm_vcpu *vcpu,
338 gpa_t addr, unsigned int len)
339 {
340 u32 intid = VGIC_ADDR_TO_INTID(addr, 2);
341 u32 value = 0;
342 int i;
343
344 for (i = 0; i < len * 4; i++) {
345 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
346
347 if (irq->config == VGIC_CONFIG_EDGE)
348 value |= (2U << (i * 2));
349
350 vgic_put_irq(vcpu->kvm, irq);
351 }
352
353 return value;
354 }
355
vgic_mmio_write_config(struct kvm_vcpu * vcpu,gpa_t addr,unsigned int len,unsigned long val)356 void vgic_mmio_write_config(struct kvm_vcpu *vcpu,
357 gpa_t addr, unsigned int len,
358 unsigned long val)
359 {
360 u32 intid = VGIC_ADDR_TO_INTID(addr, 2);
361 int i;
362
363 for (i = 0; i < len * 4; i++) {
364 struct vgic_irq *irq;
365
366 /*
367 * The configuration cannot be changed for SGIs in general,
368 * for PPIs this is IMPLEMENTATION DEFINED. The arch timer
369 * code relies on PPIs being level triggered, so we also
370 * make them read-only here.
371 */
372 if (intid + i < VGIC_NR_PRIVATE_IRQS)
373 continue;
374
375 irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
376 spin_lock(&irq->irq_lock);
377
378 if (test_bit(i * 2 + 1, &val)) {
379 irq->config = VGIC_CONFIG_EDGE;
380 } else {
381 irq->config = VGIC_CONFIG_LEVEL;
382 irq->pending = irq->line_level | irq->soft_pending;
383 }
384
385 spin_unlock(&irq->irq_lock);
386 vgic_put_irq(vcpu->kvm, irq);
387 }
388 }
389
match_region(const void * key,const void * elt)390 static int match_region(const void *key, const void *elt)
391 {
392 const unsigned int offset = (unsigned long)key;
393 const struct vgic_register_region *region = elt;
394
395 if (offset < region->reg_offset)
396 return -1;
397
398 if (offset >= region->reg_offset + region->len)
399 return 1;
400
401 return 0;
402 }
403
404 /* Find the proper register handler entry given a certain address offset. */
405 static const struct vgic_register_region *
vgic_find_mmio_region(const struct vgic_register_region * region,int nr_regions,unsigned int offset)406 vgic_find_mmio_region(const struct vgic_register_region *region, int nr_regions,
407 unsigned int offset)
408 {
409 return bsearch((void *)(uintptr_t)offset, region, nr_regions,
410 sizeof(region[0]), match_region);
411 }
412
413 /*
414 * kvm_mmio_read_buf() returns a value in a format where it can be converted
415 * to a byte array and be directly observed as the guest wanted it to appear
416 * in memory if it had done the store itself, which is LE for the GIC, as the
417 * guest knows the GIC is always LE.
418 *
419 * We convert this value to the CPUs native format to deal with it as a data
420 * value.
421 */
vgic_data_mmio_bus_to_host(const void * val,unsigned int len)422 unsigned long vgic_data_mmio_bus_to_host(const void *val, unsigned int len)
423 {
424 unsigned long data = kvm_mmio_read_buf(val, len);
425
426 switch (len) {
427 case 1:
428 return data;
429 case 2:
430 return le16_to_cpu(data);
431 case 4:
432 return le32_to_cpu(data);
433 default:
434 return le64_to_cpu(data);
435 }
436 }
437
438 /*
439 * kvm_mmio_write_buf() expects a value in a format such that if converted to
440 * a byte array it is observed as the guest would see it if it could perform
441 * the load directly. Since the GIC is LE, and the guest knows this, the
442 * guest expects a value in little endian format.
443 *
444 * We convert the data value from the CPUs native format to LE so that the
445 * value is returned in the proper format.
446 */
vgic_data_host_to_mmio_bus(void * buf,unsigned int len,unsigned long data)447 void vgic_data_host_to_mmio_bus(void *buf, unsigned int len,
448 unsigned long data)
449 {
450 switch (len) {
451 case 1:
452 break;
453 case 2:
454 data = cpu_to_le16(data);
455 break;
456 case 4:
457 data = cpu_to_le32(data);
458 break;
459 default:
460 data = cpu_to_le64(data);
461 }
462
463 kvm_mmio_write_buf(buf, len, data);
464 }
465
466 static
kvm_to_vgic_iodev(const struct kvm_io_device * dev)467 struct vgic_io_device *kvm_to_vgic_iodev(const struct kvm_io_device *dev)
468 {
469 return container_of(dev, struct vgic_io_device, dev);
470 }
471
check_region(const struct kvm * kvm,const struct vgic_register_region * region,gpa_t addr,int len)472 static bool check_region(const struct kvm *kvm,
473 const struct vgic_register_region *region,
474 gpa_t addr, int len)
475 {
476 int flags, nr_irqs = kvm->arch.vgic.nr_spis + VGIC_NR_PRIVATE_IRQS;
477
478 switch (len) {
479 case sizeof(u8):
480 flags = VGIC_ACCESS_8bit;
481 break;
482 case sizeof(u32):
483 flags = VGIC_ACCESS_32bit;
484 break;
485 case sizeof(u64):
486 flags = VGIC_ACCESS_64bit;
487 break;
488 default:
489 return false;
490 }
491
492 if ((region->access_flags & flags) && IS_ALIGNED(addr, len)) {
493 if (!region->bits_per_irq)
494 return true;
495
496 /* Do we access a non-allocated IRQ? */
497 return VGIC_ADDR_TO_INTID(addr, region->bits_per_irq) < nr_irqs;
498 }
499
500 return false;
501 }
502
dispatch_mmio_read(struct kvm_vcpu * vcpu,struct kvm_io_device * dev,gpa_t addr,int len,void * val)503 static int dispatch_mmio_read(struct kvm_vcpu *vcpu, struct kvm_io_device *dev,
504 gpa_t addr, int len, void *val)
505 {
506 struct vgic_io_device *iodev = kvm_to_vgic_iodev(dev);
507 const struct vgic_register_region *region;
508 unsigned long data = 0;
509
510 region = vgic_find_mmio_region(iodev->regions, iodev->nr_regions,
511 addr - iodev->base_addr);
512 if (!region || !check_region(vcpu->kvm, region, addr, len)) {
513 memset(val, 0, len);
514 return 0;
515 }
516
517 switch (iodev->iodev_type) {
518 case IODEV_CPUIF:
519 data = region->read(vcpu, addr, len);
520 break;
521 case IODEV_DIST:
522 data = region->read(vcpu, addr, len);
523 break;
524 case IODEV_REDIST:
525 data = region->read(iodev->redist_vcpu, addr, len);
526 break;
527 case IODEV_ITS:
528 data = region->its_read(vcpu->kvm, iodev->its, addr, len);
529 break;
530 }
531
532 vgic_data_host_to_mmio_bus(val, len, data);
533 return 0;
534 }
535
dispatch_mmio_write(struct kvm_vcpu * vcpu,struct kvm_io_device * dev,gpa_t addr,int len,const void * val)536 static int dispatch_mmio_write(struct kvm_vcpu *vcpu, struct kvm_io_device *dev,
537 gpa_t addr, int len, const void *val)
538 {
539 struct vgic_io_device *iodev = kvm_to_vgic_iodev(dev);
540 const struct vgic_register_region *region;
541 unsigned long data = vgic_data_mmio_bus_to_host(val, len);
542
543 region = vgic_find_mmio_region(iodev->regions, iodev->nr_regions,
544 addr - iodev->base_addr);
545 if (!region || !check_region(vcpu->kvm, region, addr, len))
546 return 0;
547
548 switch (iodev->iodev_type) {
549 case IODEV_CPUIF:
550 region->write(vcpu, addr, len, data);
551 break;
552 case IODEV_DIST:
553 region->write(vcpu, addr, len, data);
554 break;
555 case IODEV_REDIST:
556 region->write(iodev->redist_vcpu, addr, len, data);
557 break;
558 case IODEV_ITS:
559 region->its_write(vcpu->kvm, iodev->its, addr, len, data);
560 break;
561 }
562
563 return 0;
564 }
565
566 struct kvm_io_device_ops kvm_io_gic_ops = {
567 .read = dispatch_mmio_read,
568 .write = dispatch_mmio_write,
569 };
570
vgic_register_dist_iodev(struct kvm * kvm,gpa_t dist_base_address,enum vgic_type type)571 int vgic_register_dist_iodev(struct kvm *kvm, gpa_t dist_base_address,
572 enum vgic_type type)
573 {
574 struct vgic_io_device *io_device = &kvm->arch.vgic.dist_iodev;
575 int ret = 0;
576 unsigned int len;
577
578 switch (type) {
579 case VGIC_V2:
580 len = vgic_v2_init_dist_iodev(io_device);
581 break;
582 case VGIC_V3:
583 len = vgic_v3_init_dist_iodev(io_device);
584 break;
585 default:
586 BUG_ON(1);
587 }
588
589 io_device->base_addr = dist_base_address;
590 io_device->iodev_type = IODEV_DIST;
591 io_device->redist_vcpu = NULL;
592
593 mutex_lock(&kvm->slots_lock);
594 ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, dist_base_address,
595 len, &io_device->dev);
596 mutex_unlock(&kvm->slots_lock);
597
598 return ret;
599 }
600