]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - arch/i386/xen/events.c
xen: hack to prevent bad segment register reload
[mirror_ubuntu-jammy-kernel.git] / arch / i386 / xen / events.c
CommitLineData
e46cdb66
JF
1/*
2 * Xen event channels
3 *
4 * Xen models interrupts with abstract event channels. Because each
5 * domain gets 1024 event channels, but NR_IRQ is not that large, we
6 * must dynamically map irqs<->event channels. The event channels
7 * interface with the rest of the kernel by defining a xen interrupt
8 * chip. When an event is recieved, it is mapped to an irq and sent
9 * through the normal interrupt processing path.
10 *
11 * There are four kinds of events which can be mapped to an event
12 * channel:
13 *
14 * 1. Inter-domain notifications. This includes all the virtual
15 * device events, since they're driven by front-ends in another domain
16 * (typically dom0).
17 * 2. VIRQs, typically used for timers. These are per-cpu events.
18 * 3. IPIs.
19 * 4. Hardware interrupts. Not supported at present.
20 *
21 * Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007
22 */
23
24#include <linux/linkage.h>
25#include <linux/interrupt.h>
26#include <linux/irq.h>
27#include <linux/module.h>
28#include <linux/string.h>
29
30#include <asm/ptrace.h>
31#include <asm/irq.h>
32#include <asm/sync_bitops.h>
33#include <asm/xen/hypercall.h>
34
35#include <xen/events.h>
36#include <xen/interface/xen.h>
37#include <xen/interface/event_channel.h>
38
39#include "xen-ops.h"
40
41/*
42 * This lock protects updates to the following mapping and reference-count
43 * arrays. The lock does not need to be acquired to read the mapping tables.
44 */
45static DEFINE_SPINLOCK(irq_mapping_update_lock);
46
47/* IRQ <-> VIRQ mapping. */
48static DEFINE_PER_CPU(int, virq_to_irq[NR_VIRQS]) = {[0 ... NR_VIRQS-1] = -1};
49
f87e4cac
JF
50/* IRQ <-> IPI mapping */
51static DEFINE_PER_CPU(int, ipi_to_irq[XEN_NR_IPIS]) = {[0 ... XEN_NR_IPIS-1] = -1};
52
e46cdb66
JF
53/* Packed IRQ information: binding type, sub-type index, and event channel. */
54struct packed_irq
55{
56 unsigned short evtchn;
57 unsigned char index;
58 unsigned char type;
59};
60
61static struct packed_irq irq_info[NR_IRQS];
62
63/* Binding types. */
f87e4cac
JF
64enum {
65 IRQT_UNBOUND,
66 IRQT_PIRQ,
67 IRQT_VIRQ,
68 IRQT_IPI,
69 IRQT_EVTCHN
70};
e46cdb66
JF
71
72/* Convenient shorthand for packed representation of an unbound IRQ. */
73#define IRQ_UNBOUND mk_irq_info(IRQT_UNBOUND, 0, 0)
74
75static int evtchn_to_irq[NR_EVENT_CHANNELS] = {
76 [0 ... NR_EVENT_CHANNELS-1] = -1
77};
78static unsigned long cpu_evtchn_mask[NR_CPUS][NR_EVENT_CHANNELS/BITS_PER_LONG];
79static u8 cpu_evtchn[NR_EVENT_CHANNELS];
80
81/* Reference counts for bindings to IRQs. */
82static int irq_bindcount[NR_IRQS];
83
84/* Xen will never allocate port zero for any purpose. */
85#define VALID_EVTCHN(chn) ((chn) != 0)
86
87/*
88 * Force a proper event-channel callback from Xen after clearing the
89 * callback mask. We do this in a very simple manner, by making a call
90 * down into Xen. The pending flag will be checked by Xen on return.
91 */
92void force_evtchn_callback(void)
93{
94 (void)HYPERVISOR_xen_version(0, NULL);
95}
96EXPORT_SYMBOL_GPL(force_evtchn_callback);
97
98static struct irq_chip xen_dynamic_chip;
99
100/* Constructor for packed IRQ information. */
101static inline struct packed_irq mk_irq_info(u32 type, u32 index, u32 evtchn)
102{
103 return (struct packed_irq) { evtchn, index, type };
104}
105
106/*
107 * Accessors for packed IRQ information.
108 */
109static inline unsigned int evtchn_from_irq(int irq)
110{
111 return irq_info[irq].evtchn;
112}
113
114static inline unsigned int index_from_irq(int irq)
115{
116 return irq_info[irq].index;
117}
118
119static inline unsigned int type_from_irq(int irq)
120{
121 return irq_info[irq].type;
122}
123
124static inline unsigned long active_evtchns(unsigned int cpu,
125 struct shared_info *sh,
126 unsigned int idx)
127{
128 return (sh->evtchn_pending[idx] &
129 cpu_evtchn_mask[cpu][idx] &
130 ~sh->evtchn_mask[idx]);
131}
132
133static void bind_evtchn_to_cpu(unsigned int chn, unsigned int cpu)
134{
135 int irq = evtchn_to_irq[chn];
136
137 BUG_ON(irq == -1);
138#ifdef CONFIG_SMP
139 irq_desc[irq].affinity = cpumask_of_cpu(cpu);
140#endif
141
142 __clear_bit(chn, cpu_evtchn_mask[cpu_evtchn[chn]]);
143 __set_bit(chn, cpu_evtchn_mask[cpu]);
144
145 cpu_evtchn[chn] = cpu;
146}
147
148static void init_evtchn_cpu_bindings(void)
149{
150#ifdef CONFIG_SMP
151 int i;
152 /* By default all event channels notify CPU#0. */
153 for (i = 0; i < NR_IRQS; i++)
154 irq_desc[i].affinity = cpumask_of_cpu(0);
155#endif
156
157 memset(cpu_evtchn, 0, sizeof(cpu_evtchn));
158 memset(cpu_evtchn_mask[0], ~0, sizeof(cpu_evtchn_mask[0]));
159}
160
161static inline unsigned int cpu_from_evtchn(unsigned int evtchn)
162{
163 return cpu_evtchn[evtchn];
164}
165
166static inline void clear_evtchn(int port)
167{
168 struct shared_info *s = HYPERVISOR_shared_info;
169 sync_clear_bit(port, &s->evtchn_pending[0]);
170}
171
172static inline void set_evtchn(int port)
173{
174 struct shared_info *s = HYPERVISOR_shared_info;
175 sync_set_bit(port, &s->evtchn_pending[0]);
176}
177
178
179/**
180 * notify_remote_via_irq - send event to remote end of event channel via irq
181 * @irq: irq of event channel to send event to
182 *
183 * Unlike notify_remote_via_evtchn(), this is safe to use across
184 * save/restore. Notifications on a broken connection are silently
185 * dropped.
186 */
187void notify_remote_via_irq(int irq)
188{
189 int evtchn = evtchn_from_irq(irq);
190
191 if (VALID_EVTCHN(evtchn))
192 notify_remote_via_evtchn(evtchn);
193}
194EXPORT_SYMBOL_GPL(notify_remote_via_irq);
195
196static void mask_evtchn(int port)
197{
198 struct shared_info *s = HYPERVISOR_shared_info;
199 sync_set_bit(port, &s->evtchn_mask[0]);
200}
201
202static void unmask_evtchn(int port)
203{
204 struct shared_info *s = HYPERVISOR_shared_info;
205 unsigned int cpu = get_cpu();
206
207 BUG_ON(!irqs_disabled());
208
209 /* Slow path (hypercall) if this is a non-local port. */
210 if (unlikely(cpu != cpu_from_evtchn(port))) {
211 struct evtchn_unmask unmask = { .port = port };
212 (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask);
213 } else {
214 struct vcpu_info *vcpu_info = __get_cpu_var(xen_vcpu);
215
216 sync_clear_bit(port, &s->evtchn_mask[0]);
217
218 /*
219 * The following is basically the equivalent of
220 * 'hw_resend_irq'. Just like a real IO-APIC we 'lose
221 * the interrupt edge' if the channel is masked.
222 */
223 if (sync_test_bit(port, &s->evtchn_pending[0]) &&
224 !sync_test_and_set_bit(port / BITS_PER_LONG,
225 &vcpu_info->evtchn_pending_sel))
226 vcpu_info->evtchn_upcall_pending = 1;
227 }
228
229 put_cpu();
230}
231
232static int find_unbound_irq(void)
233{
234 int irq;
235
236 /* Only allocate from dynirq range */
237 for (irq = 0; irq < NR_IRQS; irq++)
238 if (irq_bindcount[irq] == 0)
239 break;
240
241 if (irq == NR_IRQS)
242 panic("No available IRQ to bind to: increase NR_IRQS!\n");
243
244 return irq;
245}
246
247static int bind_evtchn_to_irq(unsigned int evtchn)
248{
249 int irq;
250
251 spin_lock(&irq_mapping_update_lock);
252
253 irq = evtchn_to_irq[evtchn];
254
255 if (irq == -1) {
256 irq = find_unbound_irq();
257
258 dynamic_irq_init(irq);
259 set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
260 handle_level_irq, "event");
261
262 evtchn_to_irq[evtchn] = irq;
263 irq_info[irq] = mk_irq_info(IRQT_EVTCHN, 0, evtchn);
264 }
265
266 irq_bindcount[irq]++;
267
268 spin_unlock(&irq_mapping_update_lock);
269
270 return irq;
271}
272
f87e4cac
JF
273static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
274{
275 struct evtchn_bind_ipi bind_ipi;
276 int evtchn, irq;
277
278 spin_lock(&irq_mapping_update_lock);
279
280 irq = per_cpu(ipi_to_irq, cpu)[ipi];
281 if (irq == -1) {
282 irq = find_unbound_irq();
283 if (irq < 0)
284 goto out;
285
286 dynamic_irq_init(irq);
287 set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
288 handle_level_irq, "ipi");
289
290 bind_ipi.vcpu = cpu;
291 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
292 &bind_ipi) != 0)
293 BUG();
294 evtchn = bind_ipi.port;
295
296 evtchn_to_irq[evtchn] = irq;
297 irq_info[irq] = mk_irq_info(IRQT_IPI, ipi, evtchn);
298
299 per_cpu(ipi_to_irq, cpu)[ipi] = irq;
300
301 bind_evtchn_to_cpu(evtchn, cpu);
302 }
303
304 irq_bindcount[irq]++;
305
306 out:
307 spin_unlock(&irq_mapping_update_lock);
308 return irq;
309}
310
311
e46cdb66
JF
312static int bind_virq_to_irq(unsigned int virq, unsigned int cpu)
313{
314 struct evtchn_bind_virq bind_virq;
315 int evtchn, irq;
316
317 spin_lock(&irq_mapping_update_lock);
318
319 irq = per_cpu(virq_to_irq, cpu)[virq];
320
321 if (irq == -1) {
322 bind_virq.virq = virq;
323 bind_virq.vcpu = cpu;
324 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
325 &bind_virq) != 0)
326 BUG();
327 evtchn = bind_virq.port;
328
329 irq = find_unbound_irq();
330
331 dynamic_irq_init(irq);
332 set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
333 handle_level_irq, "virq");
334
335 evtchn_to_irq[evtchn] = irq;
336 irq_info[irq] = mk_irq_info(IRQT_VIRQ, virq, evtchn);
337
338 per_cpu(virq_to_irq, cpu)[virq] = irq;
339
340 bind_evtchn_to_cpu(evtchn, cpu);
341 }
342
343 irq_bindcount[irq]++;
344
345 spin_unlock(&irq_mapping_update_lock);
346
347 return irq;
348}
349
350static void unbind_from_irq(unsigned int irq)
351{
352 struct evtchn_close close;
353 int evtchn = evtchn_from_irq(irq);
354
355 spin_lock(&irq_mapping_update_lock);
356
357 if (VALID_EVTCHN(evtchn) && (--irq_bindcount[irq] == 0)) {
358 close.port = evtchn;
359 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
360 BUG();
361
362 switch (type_from_irq(irq)) {
363 case IRQT_VIRQ:
364 per_cpu(virq_to_irq, cpu_from_evtchn(evtchn))
365 [index_from_irq(irq)] = -1;
366 break;
367 default:
368 break;
369 }
370
371 /* Closed ports are implicitly re-bound to VCPU0. */
372 bind_evtchn_to_cpu(evtchn, 0);
373
374 evtchn_to_irq[evtchn] = -1;
375 irq_info[irq] = IRQ_UNBOUND;
376
377 dynamic_irq_init(irq);
378 }
379
380 spin_unlock(&irq_mapping_update_lock);
381}
382
383int bind_evtchn_to_irqhandler(unsigned int evtchn,
384 irqreturn_t (*handler)(int, void *),
385 unsigned long irqflags,
386 const char *devname, void *dev_id)
387{
388 unsigned int irq;
389 int retval;
390
391 irq = bind_evtchn_to_irq(evtchn);
392 retval = request_irq(irq, handler, irqflags, devname, dev_id);
393 if (retval != 0) {
394 unbind_from_irq(irq);
395 return retval;
396 }
397
398 return irq;
399}
400EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
401
402int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
403 irqreturn_t (*handler)(int, void *),
404 unsigned long irqflags, const char *devname, void *dev_id)
405{
406 unsigned int irq;
407 int retval;
408
409 irq = bind_virq_to_irq(virq, cpu);
410 retval = request_irq(irq, handler, irqflags, devname, dev_id);
411 if (retval != 0) {
412 unbind_from_irq(irq);
413 return retval;
414 }
415
416 return irq;
417}
418EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
419
f87e4cac
JF
420int bind_ipi_to_irqhandler(enum ipi_vector ipi,
421 unsigned int cpu,
422 irq_handler_t handler,
423 unsigned long irqflags,
424 const char *devname,
425 void *dev_id)
426{
427 int irq, retval;
428
429 irq = bind_ipi_to_irq(ipi, cpu);
430 if (irq < 0)
431 return irq;
432
433 retval = request_irq(irq, handler, irqflags, devname, dev_id);
434 if (retval != 0) {
435 unbind_from_irq(irq);
436 return retval;
437 }
438
439 return irq;
440}
441
e46cdb66
JF
442void unbind_from_irqhandler(unsigned int irq, void *dev_id)
443{
444 free_irq(irq, dev_id);
445 unbind_from_irq(irq);
446}
447EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
448
f87e4cac
JF
449void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
450{
451 int irq = per_cpu(ipi_to_irq, cpu)[vector];
452 BUG_ON(irq < 0);
453 notify_remote_via_irq(irq);
454}
455
456
e46cdb66
JF
457/*
458 * Search the CPUs pending events bitmasks. For each one found, map
459 * the event number to an irq, and feed it into do_IRQ() for
460 * handling.
461 *
462 * Xen uses a two-level bitmap to speed searching. The first level is
463 * a bitset of words which contain pending event bits. The second
464 * level is a bitset of pending events themselves.
465 */
466fastcall void xen_evtchn_do_upcall(struct pt_regs *regs)
467{
468 int cpu = get_cpu();
469 struct shared_info *s = HYPERVISOR_shared_info;
470 struct vcpu_info *vcpu_info = __get_cpu_var(xen_vcpu);
471 unsigned long pending_words;
472
473 vcpu_info->evtchn_upcall_pending = 0;
474
475 /* NB. No need for a barrier here -- XCHG is a barrier on x86. */
476 pending_words = xchg(&vcpu_info->evtchn_pending_sel, 0);
477 while (pending_words != 0) {
478 unsigned long pending_bits;
479 int word_idx = __ffs(pending_words);
480 pending_words &= ~(1UL << word_idx);
481
482 while ((pending_bits = active_evtchns(cpu, s, word_idx)) != 0) {
483 int bit_idx = __ffs(pending_bits);
484 int port = (word_idx * BITS_PER_LONG) + bit_idx;
485 int irq = evtchn_to_irq[port];
486
487 if (irq != -1) {
488 regs->orig_eax = ~irq;
489 do_IRQ(regs);
490 }
491 }
492 }
493
494 put_cpu();
495}
496
497/* Rebind an evtchn so that it gets delivered to a specific cpu */
498static void rebind_irq_to_cpu(unsigned irq, unsigned tcpu)
499{
500 struct evtchn_bind_vcpu bind_vcpu;
501 int evtchn = evtchn_from_irq(irq);
502
503 if (!VALID_EVTCHN(evtchn))
504 return;
505
506 /* Send future instances of this interrupt to other vcpu. */
507 bind_vcpu.port = evtchn;
508 bind_vcpu.vcpu = tcpu;
509
510 /*
511 * If this fails, it usually just indicates that we're dealing with a
512 * virq or IPI channel, which don't actually need to be rebound. Ignore
513 * it, but don't do the xenlinux-level rebind in that case.
514 */
515 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
516 bind_evtchn_to_cpu(evtchn, tcpu);
517}
518
519
520static void set_affinity_irq(unsigned irq, cpumask_t dest)
521{
522 unsigned tcpu = first_cpu(dest);
523 rebind_irq_to_cpu(irq, tcpu);
524}
525
526static void enable_dynirq(unsigned int irq)
527{
528 int evtchn = evtchn_from_irq(irq);
529
530 if (VALID_EVTCHN(evtchn))
531 unmask_evtchn(evtchn);
532}
533
534static void disable_dynirq(unsigned int irq)
535{
536 int evtchn = evtchn_from_irq(irq);
537
538 if (VALID_EVTCHN(evtchn))
539 mask_evtchn(evtchn);
540}
541
542static void ack_dynirq(unsigned int irq)
543{
544 int evtchn = evtchn_from_irq(irq);
545
546 move_native_irq(irq);
547
548 if (VALID_EVTCHN(evtchn))
549 clear_evtchn(evtchn);
550}
551
552static int retrigger_dynirq(unsigned int irq)
553{
554 int evtchn = evtchn_from_irq(irq);
555 int ret = 0;
556
557 if (VALID_EVTCHN(evtchn)) {
558 set_evtchn(evtchn);
559 ret = 1;
560 }
561
562 return ret;
563}
564
565static struct irq_chip xen_dynamic_chip __read_mostly = {
566 .name = "xen-dyn",
567 .mask = disable_dynirq,
568 .unmask = enable_dynirq,
569 .ack = ack_dynirq,
570 .set_affinity = set_affinity_irq,
571 .retrigger = retrigger_dynirq,
572};
573
574void __init xen_init_IRQ(void)
575{
576 int i;
577
578 init_evtchn_cpu_bindings();
579
580 /* No event channels are 'live' right now. */
581 for (i = 0; i < NR_EVENT_CHANNELS; i++)
582 mask_evtchn(i);
583
584 /* Dynamic IRQ space is currently unbound. Zero the refcnts. */
585 for (i = 0; i < NR_IRQS; i++)
586 irq_bindcount[i] = 0;
587
588 irq_ctx_init(smp_processor_id());
589}