]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/x86/x86/intr_machdep.c
Fix panic from Intel CPU vulnerability mitigation.
[FreeBSD/FreeBSD.git] / sys / x86 / x86 / intr_machdep.c
1 /*-
2  * Copyright (c) 2003 John Baldwin <jhb@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28
29 /*
30  * Machine dependent interrupt code for x86.  For x86, we have to
31  * deal with different PICs.  Thus, we use the passed in vector to lookup
32  * an interrupt source associated with that vector.  The interrupt source
33  * describes which PIC the source belongs to and includes methods to handle
34  * that source.
35  */
36
37 #include "opt_atpic.h"
38 #include "opt_ddb.h"
39 #include "opt_smp.h"
40
41 #include <sys/param.h>
42 #include <sys/bus.h>
43 #include <sys/interrupt.h>
44 #include <sys/ktr.h>
45 #include <sys/kernel.h>
46 #include <sys/lock.h>
47 #include <sys/malloc.h>
48 #include <sys/mutex.h>
49 #include <sys/proc.h>
50 #include <sys/smp.h>
51 #include <sys/sx.h>
52 #include <sys/syslog.h>
53 #include <sys/systm.h>
54 #include <sys/vmmeter.h>
55 #include <machine/clock.h>
56 #include <machine/intr_machdep.h>
57 #include <machine/smp.h>
58 #ifdef DDB
59 #include <ddb/ddb.h>
60 #endif
61
62 #ifndef DEV_ATPIC
63 #include <machine/segments.h>
64 #include <machine/frame.h>
65 #include <dev/ic/i8259.h>
66 #include <x86/isa/icu.h>
67 #ifdef PC98
68 #include <pc98/cbus/cbus.h>
69 #else
70 #include <isa/isareg.h>
71 #endif
72 #endif
73
74 #define MAX_STRAY_LOG   5
75
76 typedef void (*mask_fn)(void *);
77
78 static int intrcnt_index;
79 static struct intsrc **interrupt_sources;
80 static struct sx intrsrc_lock;
81 static struct mtx intrpic_lock;
82 static struct mtx intrcnt_lock;
83 static TAILQ_HEAD(pics_head, pic) pics;
84 u_int num_io_irqs;
85
86 #if defined(SMP) && !defined(EARLY_AP_STARTUP)
87 static int assign_cpu;
88 #endif
89
90 u_long *intrcnt;
91 char *intrnames;
92 size_t sintrcnt = sizeof(intrcnt);
93 size_t sintrnames = sizeof(intrnames);
94 int nintrcnt;
95
96 static MALLOC_DEFINE(M_INTR, "intr", "Interrupt Sources");
97
98 static int      intr_assign_cpu(void *arg, int cpu);
99 static void     intr_disable_src(void *arg);
100 static void     intr_init(void *__dummy);
101 static int      intr_pic_registered(struct pic *pic);
102 static void     intrcnt_setname(const char *name, int index);
103 static void     intrcnt_updatename(struct intsrc *is);
104 static void     intrcnt_register(struct intsrc *is);
105
106 /*
107  * SYSINIT levels for SI_SUB_INTR:
108  *
109  * SI_ORDER_FIRST: Initialize locks and pics TAILQ, xen_hvm_cpu_init
110  * SI_ORDER_SECOND: Xen PICs
111  * SI_ORDER_THIRD: Add I/O APIC PICs, alloc MSI and Xen IRQ ranges
112  * SI_ORDER_FOURTH: Add 8259A PICs
113  * SI_ORDER_FOURTH + 1: Finalize interrupt count and add interrupt sources
114  * SI_ORDER_MIDDLE: SMP interrupt counters
115  * SI_ORDER_ANY: Enable interrupts on BSP
116  */
117
118 static int
119 intr_pic_registered(struct pic *pic)
120 {
121         struct pic *p;
122
123         TAILQ_FOREACH(p, &pics, pics) {
124                 if (p == pic)
125                         return (1);
126         }
127         return (0);
128 }
129
130 /*
131  * Register a new interrupt controller (PIC).  This is to support suspend
132  * and resume where we suspend/resume controllers rather than individual
133  * sources.  This also allows controllers with no active sources (such as
134  * 8259As in a system using the APICs) to participate in suspend and resume.
135  */
136 int
137 intr_register_pic(struct pic *pic)
138 {
139         int error;
140
141         mtx_lock(&intrpic_lock);
142         if (intr_pic_registered(pic))
143                 error = EBUSY;
144         else {
145                 TAILQ_INSERT_TAIL(&pics, pic, pics);
146                 error = 0;
147         }
148         mtx_unlock(&intrpic_lock);
149         return (error);
150 }
151
152 /*
153  * Allocate interrupt source arrays and register interrupt sources
154  * once the number of interrupts is known.
155  */
156 static void
157 intr_init_sources(void *arg)
158 {
159         struct pic *pic;
160
161         MPASS(num_io_irqs > 0);
162
163         interrupt_sources = mallocarray(num_io_irqs, sizeof(*interrupt_sources),
164             M_INTR, M_WAITOK | M_ZERO);
165
166         /*
167          * - 1 ??? dummy counter.
168          * - 2 counters for each I/O interrupt.
169          * - 1 counter for each CPU for lapic timer.
170          * - 1 counter for each CPU for the Hyper-V vmbus driver.
171          * - 8 counters for each CPU for IPI counters for SMP.
172          */
173         nintrcnt = 1 + num_io_irqs * 2 + mp_ncpus * 2;
174 #ifdef COUNT_IPIS
175         if (mp_ncpus > 1)
176                 nintrcnt += 8 * mp_ncpus;
177 #endif
178         intrcnt = mallocarray(nintrcnt, sizeof(u_long), M_INTR, M_WAITOK |
179             M_ZERO);
180         intrnames = mallocarray(nintrcnt, MAXCOMLEN + 1, M_INTR, M_WAITOK |
181             M_ZERO);
182         sintrcnt = nintrcnt * sizeof(u_long);
183         sintrnames = nintrcnt * (MAXCOMLEN + 1);
184
185         intrcnt_setname("???", 0);
186         intrcnt_index = 1;
187
188         /*
189          * NB: intrpic_lock is not held here to avoid LORs due to
190          * malloc() in intr_register_source().  However, we are still
191          * single-threaded at this point in startup so the list of
192          * PICs shouldn't change.
193          */
194         TAILQ_FOREACH(pic, &pics, pics) {
195                 if (pic->pic_register_sources != NULL)
196                         pic->pic_register_sources(pic);
197         }
198 }
199 SYSINIT(intr_init_sources, SI_SUB_INTR, SI_ORDER_FOURTH + 1, intr_init_sources,
200     NULL);
201
202 /*
203  * Register a new interrupt source with the global interrupt system.
204  * The global interrupts need to be disabled when this function is
205  * called.
206  */
207 int
208 intr_register_source(struct intsrc *isrc)
209 {
210         int error, vector;
211
212         KASSERT(intr_pic_registered(isrc->is_pic), ("unregistered PIC"));
213         vector = isrc->is_pic->pic_vector(isrc);
214         KASSERT(vector < num_io_irqs, ("IRQ %d too large (%u irqs)", vector,
215             num_io_irqs));
216         if (interrupt_sources[vector] != NULL)
217                 return (EEXIST);
218         error = intr_event_create(&isrc->is_event, isrc, 0, vector,
219             intr_disable_src, (mask_fn)isrc->is_pic->pic_enable_source,
220             (mask_fn)isrc->is_pic->pic_eoi_source, intr_assign_cpu, "irq%d:",
221             vector);
222         if (error)
223                 return (error);
224         sx_xlock(&intrsrc_lock);
225         if (interrupt_sources[vector] != NULL) {
226                 sx_xunlock(&intrsrc_lock);
227                 intr_event_destroy(isrc->is_event);
228                 return (EEXIST);
229         }
230         intrcnt_register(isrc);
231         interrupt_sources[vector] = isrc;
232         isrc->is_handlers = 0;
233         sx_xunlock(&intrsrc_lock);
234         return (0);
235 }
236
237 struct intsrc *
238 intr_lookup_source(int vector)
239 {
240
241         if (vector < 0 || vector >= num_io_irqs)
242                 return (NULL);
243         return (interrupt_sources[vector]);
244 }
245
246 int
247 intr_add_handler(const char *name, int vector, driver_filter_t filter,
248     driver_intr_t handler, void *arg, enum intr_type flags, void **cookiep)
249 {
250         struct intsrc *isrc;
251         int error;
252
253         isrc = intr_lookup_source(vector);
254         if (isrc == NULL)
255                 return (EINVAL);
256         error = intr_event_add_handler(isrc->is_event, name, filter, handler,
257             arg, intr_priority(flags), flags, cookiep);
258         if (error == 0) {
259                 sx_xlock(&intrsrc_lock);
260                 intrcnt_updatename(isrc);
261                 isrc->is_handlers++;
262                 if (isrc->is_handlers == 1) {
263                         isrc->is_pic->pic_enable_intr(isrc);
264                         isrc->is_pic->pic_enable_source(isrc);
265                 }
266                 sx_xunlock(&intrsrc_lock);
267         }
268         return (error);
269 }
270
271 int
272 intr_remove_handler(void *cookie)
273 {
274         struct intsrc *isrc;
275         int error;
276
277         isrc = intr_handler_source(cookie);
278         error = intr_event_remove_handler(cookie);
279         if (error == 0) {
280                 sx_xlock(&intrsrc_lock);
281                 isrc->is_handlers--;
282                 if (isrc->is_handlers == 0) {
283                         isrc->is_pic->pic_disable_source(isrc, PIC_NO_EOI);
284                         isrc->is_pic->pic_disable_intr(isrc);
285                 }
286                 intrcnt_updatename(isrc);
287                 sx_xunlock(&intrsrc_lock);
288         }
289         return (error);
290 }
291
292 int
293 intr_config_intr(int vector, enum intr_trigger trig, enum intr_polarity pol)
294 {
295         struct intsrc *isrc;
296
297         isrc = intr_lookup_source(vector);
298         if (isrc == NULL)
299                 return (EINVAL);
300         return (isrc->is_pic->pic_config_intr(isrc, trig, pol));
301 }
302
303 static void
304 intr_disable_src(void *arg)
305 {
306         struct intsrc *isrc;
307
308         isrc = arg;
309         isrc->is_pic->pic_disable_source(isrc, PIC_EOI);
310 }
311
312 void
313 intr_execute_handlers(struct intsrc *isrc, struct trapframe *frame)
314 {
315         struct intr_event *ie;
316         int vector;
317
318         /*
319          * We count software interrupts when we process them.  The
320          * code here follows previous practice, but there's an
321          * argument for counting hardware interrupts when they're
322          * processed too.
323          */
324         (*isrc->is_count)++;
325         PCPU_INC(cnt.v_intr);
326
327         ie = isrc->is_event;
328
329         /*
330          * XXX: We assume that IRQ 0 is only used for the ISA timer
331          * device (clk).
332          */
333         vector = isrc->is_pic->pic_vector(isrc);
334         if (vector == 0)
335                 clkintr_pending = 1;
336
337         /*
338          * For stray interrupts, mask and EOI the source, bump the
339          * stray count, and log the condition.
340          */
341         if (intr_event_handle(ie, frame) != 0) {
342                 isrc->is_pic->pic_disable_source(isrc, PIC_EOI);
343                 (*isrc->is_straycount)++;
344                 if (*isrc->is_straycount < MAX_STRAY_LOG)
345                         log(LOG_ERR, "stray irq%d\n", vector);
346                 else if (*isrc->is_straycount == MAX_STRAY_LOG)
347                         log(LOG_CRIT,
348                             "too many stray irq %d's: not logging anymore\n",
349                             vector);
350         }
351 }
352
353 void
354 intr_resume(bool suspend_cancelled)
355 {
356         struct pic *pic;
357
358 #ifndef DEV_ATPIC
359         atpic_reset();
360 #endif
361         mtx_lock(&intrpic_lock);
362         TAILQ_FOREACH(pic, &pics, pics) {
363                 if (pic->pic_resume != NULL)
364                         pic->pic_resume(pic, suspend_cancelled);
365         }
366         mtx_unlock(&intrpic_lock);
367 }
368
369 void
370 intr_suspend(void)
371 {
372         struct pic *pic;
373
374         mtx_lock(&intrpic_lock);
375         TAILQ_FOREACH_REVERSE(pic, &pics, pics_head, pics) {
376                 if (pic->pic_suspend != NULL)
377                         pic->pic_suspend(pic);
378         }
379         mtx_unlock(&intrpic_lock);
380 }
381
382 static int
383 intr_assign_cpu(void *arg, int cpu)
384 {
385 #ifdef SMP
386         struct intsrc *isrc;
387         int error;
388
389 #ifdef EARLY_AP_STARTUP
390         MPASS(mp_ncpus == 1 || smp_started);
391
392         /* Nothing to do if there is only a single CPU. */
393         if (mp_ncpus > 1 && cpu != NOCPU) {
394 #else
395         /*
396          * Don't do anything during early boot.  We will pick up the
397          * assignment once the APs are started.
398          */
399         if (assign_cpu && cpu != NOCPU) {
400 #endif
401                 isrc = arg;
402                 sx_xlock(&intrsrc_lock);
403                 error = isrc->is_pic->pic_assign_cpu(isrc, cpu_apic_ids[cpu]);
404                 sx_xunlock(&intrsrc_lock);
405         } else
406                 error = 0;
407         return (error);
408 #else
409         return (EOPNOTSUPP);
410 #endif
411 }
412
413 static void
414 intrcnt_setname(const char *name, int index)
415 {
416
417         snprintf(intrnames + (MAXCOMLEN + 1) * index, MAXCOMLEN + 1, "%-*s",
418             MAXCOMLEN, name);
419 }
420
421 static void
422 intrcnt_updatename(struct intsrc *is)
423 {
424
425         intrcnt_setname(is->is_event->ie_fullname, is->is_index);
426 }
427
428 static void
429 intrcnt_register(struct intsrc *is)
430 {
431         char straystr[MAXCOMLEN + 1];
432
433         KASSERT(is->is_event != NULL, ("%s: isrc with no event", __func__));
434         mtx_lock_spin(&intrcnt_lock);
435         MPASS(intrcnt_index + 2 <= nintrcnt);
436         is->is_index = intrcnt_index;
437         intrcnt_index += 2;
438         snprintf(straystr, MAXCOMLEN + 1, "stray irq%d",
439             is->is_pic->pic_vector(is));
440         intrcnt_updatename(is);
441         is->is_count = &intrcnt[is->is_index];
442         intrcnt_setname(straystr, is->is_index + 1);
443         is->is_straycount = &intrcnt[is->is_index + 1];
444         mtx_unlock_spin(&intrcnt_lock);
445 }
446
447 void
448 intrcnt_add(const char *name, u_long **countp)
449 {
450
451         mtx_lock_spin(&intrcnt_lock);
452         MPASS(intrcnt_index < nintrcnt);
453         *countp = &intrcnt[intrcnt_index];
454         intrcnt_setname(name, intrcnt_index);
455         intrcnt_index++;
456         mtx_unlock_spin(&intrcnt_lock);
457 }
458
459 static void
460 intr_init(void *dummy __unused)
461 {
462
463         TAILQ_INIT(&pics);
464         mtx_init(&intrpic_lock, "intrpic", NULL, MTX_DEF);
465         sx_init(&intrsrc_lock, "intrsrc");
466         mtx_init(&intrcnt_lock, "intrcnt", NULL, MTX_SPIN);
467 }
468 SYSINIT(intr_init, SI_SUB_INTR, SI_ORDER_FIRST, intr_init, NULL);
469
470 static void
471 intr_init_final(void *dummy __unused)
472 {
473
474         /*
475          * Enable interrupts on the BSP after all of the interrupt
476          * controllers are initialized.  Device interrupts are still
477          * disabled in the interrupt controllers until interrupt
478          * handlers are registered.  Interrupts are enabled on each AP
479          * after their first context switch.
480          */
481         enable_intr();
482 }
483 SYSINIT(intr_init_final, SI_SUB_INTR, SI_ORDER_ANY, intr_init_final, NULL);
484
485 #ifndef DEV_ATPIC
486 /* Initialize the two 8259A's to a known-good shutdown state. */
487 void
488 atpic_reset(void)
489 {
490
491         outb(IO_ICU1, ICW1_RESET | ICW1_IC4);
492         outb(IO_ICU1 + ICU_IMR_OFFSET, IDT_IO_INTS);
493         outb(IO_ICU1 + ICU_IMR_OFFSET, IRQ_MASK(ICU_SLAVEID));
494         outb(IO_ICU1 + ICU_IMR_OFFSET, MASTER_MODE);
495         outb(IO_ICU1 + ICU_IMR_OFFSET, 0xff);
496         outb(IO_ICU1, OCW3_SEL | OCW3_RR);
497
498         outb(IO_ICU2, ICW1_RESET | ICW1_IC4);
499         outb(IO_ICU2 + ICU_IMR_OFFSET, IDT_IO_INTS + 8);
500         outb(IO_ICU2 + ICU_IMR_OFFSET, ICU_SLAVEID);
501         outb(IO_ICU2 + ICU_IMR_OFFSET, SLAVE_MODE);
502         outb(IO_ICU2 + ICU_IMR_OFFSET, 0xff);
503         outb(IO_ICU2, OCW3_SEL | OCW3_RR);
504 }
505 #endif
506
507 /* Add a description to an active interrupt handler. */
508 int
509 intr_describe(u_int vector, void *ih, const char *descr)
510 {
511         struct intsrc *isrc;
512         int error;
513
514         isrc = intr_lookup_source(vector);
515         if (isrc == NULL)
516                 return (EINVAL);
517         error = intr_event_describe_handler(isrc->is_event, ih, descr);
518         if (error)
519                 return (error);
520         intrcnt_updatename(isrc);
521         return (0);
522 }
523
524 void
525 intr_reprogram(void)
526 {
527         struct intsrc *is;
528         u_int v;
529
530         sx_xlock(&intrsrc_lock);
531         for (v = 0; v < num_io_irqs; v++) {
532                 is = interrupt_sources[v];
533                 if (is == NULL)
534                         continue;
535                 if (is->is_pic->pic_reprogram_pin != NULL)
536                         is->is_pic->pic_reprogram_pin(is);
537         }
538         sx_xunlock(&intrsrc_lock);
539 }
540
541 #ifdef DDB
542 /*
543  * Dump data about interrupt handlers
544  */
545 DB_SHOW_COMMAND(irqs, db_show_irqs)
546 {
547         struct intsrc **isrc;
548         u_int i;
549         int verbose;
550
551         if (strcmp(modif, "v") == 0)
552                 verbose = 1;
553         else
554                 verbose = 0;
555         isrc = interrupt_sources;
556         for (i = 0; i < num_io_irqs && !db_pager_quit; i++, isrc++)
557                 if (*isrc != NULL)
558                         db_dump_intr_event((*isrc)->is_event, verbose);
559 }
560 #endif
561
562 #ifdef SMP
563 /*
564  * Support for balancing interrupt sources across CPUs.  For now we just
565  * allocate CPUs round-robin.
566  */
567
568 cpuset_t intr_cpus = CPUSET_T_INITIALIZER(0x1);
569 static int current_cpu;
570
571 /*
572  * Return the CPU that the next interrupt source should use.  For now
573  * this just returns the next local APIC according to round-robin.
574  */
575 u_int
576 intr_next_cpu(void)
577 {
578         u_int apic_id;
579
580 #ifdef EARLY_AP_STARTUP
581         MPASS(mp_ncpus == 1 || smp_started);
582         if (mp_ncpus == 1)
583                 return (PCPU_GET(apic_id));
584 #else
585         /* Leave all interrupts on the BSP during boot. */
586         if (!assign_cpu)
587                 return (PCPU_GET(apic_id));
588 #endif
589
590         mtx_lock_spin(&icu_lock);
591         apic_id = cpu_apic_ids[current_cpu];
592         do {
593                 current_cpu++;
594                 if (current_cpu > mp_maxid)
595                         current_cpu = 0;
596         } while (!CPU_ISSET(current_cpu, &intr_cpus));
597         mtx_unlock_spin(&icu_lock);
598         return (apic_id);
599 }
600
601 /* Attempt to bind the specified IRQ to the specified CPU. */
602 int
603 intr_bind(u_int vector, u_char cpu)
604 {
605         struct intsrc *isrc;
606
607         isrc = intr_lookup_source(vector);
608         if (isrc == NULL)
609                 return (EINVAL);
610         return (intr_event_bind(isrc->is_event, cpu));
611 }
612
613 /*
614  * Add a CPU to our mask of valid CPUs that can be destinations of
615  * interrupts.
616  */
617 void
618 intr_add_cpu(u_int cpu)
619 {
620
621         if (cpu >= MAXCPU)
622                 panic("%s: Invalid CPU ID", __func__);
623         if (bootverbose)
624                 printf("INTR: Adding local APIC %d as a target\n",
625                     cpu_apic_ids[cpu]);
626
627         CPU_SET(cpu, &intr_cpus);
628 }
629
630 #ifndef EARLY_AP_STARTUP
631 /*
632  * Distribute all the interrupt sources among the available CPUs once the
633  * AP's have been launched.
634  */
635 static void
636 intr_shuffle_irqs(void *arg __unused)
637 {
638         struct intsrc *isrc;
639         u_int i;
640
641         /* Don't bother on UP. */
642         if (mp_ncpus == 1)
643                 return;
644
645         /* Round-robin assign a CPU to each enabled source. */
646         sx_xlock(&intrsrc_lock);
647         assign_cpu = 1;
648         for (i = 0; i < num_io_irqs; i++) {
649                 isrc = interrupt_sources[i];
650                 if (isrc != NULL && isrc->is_handlers > 0) {
651                         /*
652                          * If this event is already bound to a CPU,
653                          * then assign the source to that CPU instead
654                          * of picking one via round-robin.  Note that
655                          * this is careful to only advance the
656                          * round-robin if the CPU assignment succeeds.
657                          */
658                         if (isrc->is_event->ie_cpu != NOCPU)
659                                 (void)isrc->is_pic->pic_assign_cpu(isrc,
660                                     cpu_apic_ids[isrc->is_event->ie_cpu]);
661                         else if (isrc->is_pic->pic_assign_cpu(isrc,
662                                 cpu_apic_ids[current_cpu]) == 0)
663                                 (void)intr_next_cpu();
664
665                 }
666         }
667         sx_xunlock(&intrsrc_lock);
668 }
669 SYSINIT(intr_shuffle_irqs, SI_SUB_SMP, SI_ORDER_SECOND, intr_shuffle_irqs,
670     NULL);
671 #endif
672 #else
673 /*
674  * Always route interrupts to the current processor in the UP case.
675  */
676 u_int
677 intr_next_cpu(void)
678 {
679
680         return (PCPU_GET(apic_id));
681 }
682 #endif