]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/amd64/amd64/mp_machdep.c
Integrate tools/regression/mqueue into the FreeBSD test suite as
[FreeBSD/FreeBSD.git] / sys / amd64 / amd64 / mp_machdep.c
1 /*-
2  * Copyright (c) 1996, by Steve Passe
3  * Copyright (c) 2003, by Peter Wemm
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. The name of the developer may NOT be used to endorse or promote products
12  *    derived from this software without specific prior written permission.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include "opt_cpu.h"
31 #include "opt_ddb.h"
32 #include "opt_kstack_pages.h"
33 #include "opt_sched.h"
34 #include "opt_smp.h"
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/bus.h>
39 #include <sys/cpuset.h>
40 #ifdef GPROF 
41 #include <sys/gmon.h>
42 #endif
43 #include <sys/kernel.h>
44 #include <sys/ktr.h>
45 #include <sys/lock.h>
46 #include <sys/malloc.h>
47 #include <sys/memrange.h>
48 #include <sys/mutex.h>
49 #include <sys/pcpu.h>
50 #include <sys/proc.h>
51 #include <sys/sched.h>
52 #include <sys/smp.h>
53 #include <sys/sysctl.h>
54
55 #include <vm/vm.h>
56 #include <vm/vm_param.h>
57 #include <vm/pmap.h>
58 #include <vm/vm_kern.h>
59 #include <vm/vm_extern.h>
60
61 #include <x86/apicreg.h>
62 #include <machine/clock.h>
63 #include <machine/cputypes.h>
64 #include <machine/cpufunc.h>
65 #include <x86/mca.h>
66 #include <machine/md_var.h>
67 #include <machine/pcb.h>
68 #include <machine/psl.h>
69 #include <machine/smp.h>
70 #include <machine/specialreg.h>
71 #include <machine/tss.h>
72 #include <machine/cpu.h>
73 #include <x86/init.h>
74
75 #define WARMBOOT_TARGET         0
76 #define WARMBOOT_OFF            (KERNBASE + 0x0467)
77 #define WARMBOOT_SEG            (KERNBASE + 0x0469)
78
79 #define CMOS_REG                (0x70)
80 #define CMOS_DATA               (0x71)
81 #define BIOS_RESET              (0x0f)
82 #define BIOS_WARM               (0x0a)
83
84 extern  struct pcpu __pcpu[];
85
86 /* Temporary variables for init_secondary()  */
87 char *doublefault_stack;
88 char *nmi_stack;
89
90 /* Variables needed for SMP tlb shootdown. */
91 vm_offset_t smp_tlb_addr2;
92 struct invpcid_descr smp_tlb_invpcid;
93 volatile int smp_tlb_wait;
94 uint64_t pcid_cr3;
95 pmap_t smp_tlb_pmap;
96 extern int invpcid_works;
97
98 extern inthand_t IDTVEC(fast_syscall), IDTVEC(fast_syscall32);
99
100 /*
101  * Local data and functions.
102  */
103
104 static int      start_ap(int apic_id);
105
106 static u_int    bootMP_size;
107 static u_int    boot_address;
108
109 /*
110  * Calculate usable address in base memory for AP trampoline code.
111  */
112 u_int
113 mp_bootaddress(u_int basemem)
114 {
115
116         bootMP_size = mptramp_end - mptramp_start;
117         boot_address = trunc_page(basemem * 1024); /* round down to 4k boundary */
118         if (((basemem * 1024) - boot_address) < bootMP_size)
119                 boot_address -= PAGE_SIZE;      /* not enough, lower by 4k */
120         /* 3 levels of page table pages */
121         mptramp_pagetables = boot_address - (PAGE_SIZE * 3);
122
123         return mptramp_pagetables;
124 }
125
126 /*
127  * Initialize the IPI handlers and start up the AP's.
128  */
129 void
130 cpu_mp_start(void)
131 {
132         int i;
133
134         /* Initialize the logical ID to APIC ID table. */
135         for (i = 0; i < MAXCPU; i++) {
136                 cpu_apic_ids[i] = -1;
137                 cpu_ipi_pending[i] = 0;
138         }
139
140         /* Install an inter-CPU IPI for TLB invalidation */
141         if (pmap_pcid_enabled) {
142                 setidt(IPI_INVLTLB, IDTVEC(invltlb_pcid), SDT_SYSIGT,
143                     SEL_KPL, 0);
144                 setidt(IPI_INVLPG, IDTVEC(invlpg_pcid), SDT_SYSIGT,
145                     SEL_KPL, 0);
146         } else {
147                 setidt(IPI_INVLTLB, IDTVEC(invltlb), SDT_SYSIGT, SEL_KPL, 0);
148                 setidt(IPI_INVLPG, IDTVEC(invlpg), SDT_SYSIGT, SEL_KPL, 0);
149         }
150         setidt(IPI_INVLRNG, IDTVEC(invlrng), SDT_SYSIGT, SEL_KPL, 0);
151
152         /* Install an inter-CPU IPI for cache invalidation. */
153         setidt(IPI_INVLCACHE, IDTVEC(invlcache), SDT_SYSIGT, SEL_KPL, 0);
154
155         /* Install an inter-CPU IPI for all-CPU rendezvous */
156         setidt(IPI_RENDEZVOUS, IDTVEC(rendezvous), SDT_SYSIGT, SEL_KPL, 0);
157
158         /* Install generic inter-CPU IPI handler */
159         setidt(IPI_BITMAP_VECTOR, IDTVEC(ipi_intr_bitmap_handler),
160                SDT_SYSIGT, SEL_KPL, 0);
161
162         /* Install an inter-CPU IPI for CPU stop/restart */
163         setidt(IPI_STOP, IDTVEC(cpustop), SDT_SYSIGT, SEL_KPL, 0);
164
165         /* Install an inter-CPU IPI for CPU suspend/resume */
166         setidt(IPI_SUSPEND, IDTVEC(cpususpend), SDT_SYSIGT, SEL_KPL, 0);
167
168         /* Set boot_cpu_id if needed. */
169         if (boot_cpu_id == -1) {
170                 boot_cpu_id = PCPU_GET(apic_id);
171                 cpu_info[boot_cpu_id].cpu_bsp = 1;
172         } else
173                 KASSERT(boot_cpu_id == PCPU_GET(apic_id),
174                     ("BSP's APIC ID doesn't match boot_cpu_id"));
175
176         /* Probe logical/physical core configuration. */
177         topo_probe();
178
179         assign_cpu_ids();
180
181         /* Start each Application Processor */
182         init_ops.start_all_aps();
183
184         set_interrupt_apic_ids();
185 }
186
187
188 /*
189  * AP CPU's call this to initialize themselves.
190  */
191 void
192 init_secondary(void)
193 {
194         struct pcpu *pc;
195         struct nmi_pcpu *np;
196         u_int64_t msr, cr0;
197         int cpu, gsel_tss, x;
198         struct region_descriptor ap_gdt;
199
200         /* Set by the startup code for us to use */
201         cpu = bootAP;
202
203         /* Init tss */
204         common_tss[cpu] = common_tss[0];
205         common_tss[cpu].tss_rsp0 = 0;   /* not used until after switch */
206         common_tss[cpu].tss_iobase = sizeof(struct amd64tss) +
207             IOPERM_BITMAP_SIZE;
208         common_tss[cpu].tss_ist1 = (long)&doublefault_stack[PAGE_SIZE];
209
210         /* The NMI stack runs on IST2. */
211         np = ((struct nmi_pcpu *) &nmi_stack[PAGE_SIZE]) - 1;
212         common_tss[cpu].tss_ist2 = (long) np;
213
214         /* Prepare private GDT */
215         gdt_segs[GPROC0_SEL].ssd_base = (long) &common_tss[cpu];
216         for (x = 0; x < NGDT; x++) {
217                 if (x != GPROC0_SEL && x != (GPROC0_SEL + 1) &&
218                     x != GUSERLDT_SEL && x != (GUSERLDT_SEL + 1))
219                         ssdtosd(&gdt_segs[x], &gdt[NGDT * cpu + x]);
220         }
221         ssdtosyssd(&gdt_segs[GPROC0_SEL],
222             (struct system_segment_descriptor *)&gdt[NGDT * cpu + GPROC0_SEL]);
223         ap_gdt.rd_limit = NGDT * sizeof(gdt[0]) - 1;
224         ap_gdt.rd_base =  (long) &gdt[NGDT * cpu];
225         lgdt(&ap_gdt);                  /* does magic intra-segment return */
226
227         /* Get per-cpu data */
228         pc = &__pcpu[cpu];
229
230         /* prime data page for it to use */
231         pcpu_init(pc, cpu, sizeof(struct pcpu));
232         dpcpu_init(dpcpu, cpu);
233         pc->pc_apic_id = cpu_apic_ids[cpu];
234         pc->pc_prvspace = pc;
235         pc->pc_curthread = 0;
236         pc->pc_tssp = &common_tss[cpu];
237         pc->pc_commontssp = &common_tss[cpu];
238         pc->pc_rsp0 = 0;
239         pc->pc_tss = (struct system_segment_descriptor *)&gdt[NGDT * cpu +
240             GPROC0_SEL];
241         pc->pc_fs32p = &gdt[NGDT * cpu + GUFS32_SEL];
242         pc->pc_gs32p = &gdt[NGDT * cpu + GUGS32_SEL];
243         pc->pc_ldt = (struct system_segment_descriptor *)&gdt[NGDT * cpu +
244             GUSERLDT_SEL];
245
246         /* Save the per-cpu pointer for use by the NMI handler. */
247         np->np_pcpu = (register_t) pc;
248
249         wrmsr(MSR_FSBASE, 0);           /* User value */
250         wrmsr(MSR_GSBASE, (u_int64_t)pc);
251         wrmsr(MSR_KGSBASE, (u_int64_t)pc);      /* XXX User value while we're in the kernel */
252
253         lidt(&r_idt);
254
255         gsel_tss = GSEL(GPROC0_SEL, SEL_KPL);
256         ltr(gsel_tss);
257
258         /*
259          * Set to a known state:
260          * Set by mpboot.s: CR0_PG, CR0_PE
261          * Set by cpu_setregs: CR0_NE, CR0_MP, CR0_TS, CR0_WP, CR0_AM
262          */
263         cr0 = rcr0();
264         cr0 &= ~(CR0_CD | CR0_NW | CR0_EM);
265         load_cr0(cr0);
266
267         /* Set up the fast syscall stuff */
268         msr = rdmsr(MSR_EFER) | EFER_SCE;
269         wrmsr(MSR_EFER, msr);
270         wrmsr(MSR_LSTAR, (u_int64_t)IDTVEC(fast_syscall));
271         wrmsr(MSR_CSTAR, (u_int64_t)IDTVEC(fast_syscall32));
272         msr = ((u_int64_t)GSEL(GCODE_SEL, SEL_KPL) << 32) |
273               ((u_int64_t)GSEL(GUCODE32_SEL, SEL_UPL) << 48);
274         wrmsr(MSR_STAR, msr);
275         wrmsr(MSR_SF_MASK, PSL_NT|PSL_T|PSL_I|PSL_C|PSL_D);
276
277         /* signal our startup to the BSP. */
278         mp_naps++;
279
280         /* Spin until the BSP releases the AP's. */
281         while (!aps_ready)
282                 ia32_pause();
283
284         init_secondary_tail();
285 }
286
287 /*******************************************************************
288  * local functions and data
289  */
290
291 /*
292  * start each AP in our list
293  */
294 int
295 native_start_all_aps(void)
296 {
297         vm_offset_t va = boot_address + KERNBASE;
298         u_int64_t *pt4, *pt3, *pt2;
299         u_int32_t mpbioswarmvec;
300         int apic_id, cpu, i;
301         u_char mpbiosreason;
302
303         mtx_init(&ap_boot_mtx, "ap boot", NULL, MTX_SPIN);
304
305         /* install the AP 1st level boot code */
306         pmap_kenter(va, boot_address);
307         pmap_invalidate_page(kernel_pmap, va);
308         bcopy(mptramp_start, (void *)va, bootMP_size);
309
310         /* Locate the page tables, they'll be below the trampoline */
311         pt4 = (u_int64_t *)(uintptr_t)(mptramp_pagetables + KERNBASE);
312         pt3 = pt4 + (PAGE_SIZE) / sizeof(u_int64_t);
313         pt2 = pt3 + (PAGE_SIZE) / sizeof(u_int64_t);
314
315         /* Create the initial 1GB replicated page tables */
316         for (i = 0; i < 512; i++) {
317                 /* Each slot of the level 4 pages points to the same level 3 page */
318                 pt4[i] = (u_int64_t)(uintptr_t)(mptramp_pagetables + PAGE_SIZE);
319                 pt4[i] |= PG_V | PG_RW | PG_U;
320
321                 /* Each slot of the level 3 pages points to the same level 2 page */
322                 pt3[i] = (u_int64_t)(uintptr_t)(mptramp_pagetables + (2 * PAGE_SIZE));
323                 pt3[i] |= PG_V | PG_RW | PG_U;
324
325                 /* The level 2 page slots are mapped with 2MB pages for 1GB. */
326                 pt2[i] = i * (2 * 1024 * 1024);
327                 pt2[i] |= PG_V | PG_RW | PG_PS | PG_U;
328         }
329
330         /* save the current value of the warm-start vector */
331         mpbioswarmvec = *((u_int32_t *) WARMBOOT_OFF);
332         outb(CMOS_REG, BIOS_RESET);
333         mpbiosreason = inb(CMOS_DATA);
334
335         /* setup a vector to our boot code */
336         *((volatile u_short *) WARMBOOT_OFF) = WARMBOOT_TARGET;
337         *((volatile u_short *) WARMBOOT_SEG) = (boot_address >> 4);
338         outb(CMOS_REG, BIOS_RESET);
339         outb(CMOS_DATA, BIOS_WARM);     /* 'warm-start' */
340
341         /* start each AP */
342         for (cpu = 1; cpu < mp_ncpus; cpu++) {
343                 apic_id = cpu_apic_ids[cpu];
344
345                 /* allocate and set up an idle stack data page */
346                 bootstacks[cpu] = (void *)kmem_malloc(kernel_arena,
347                     KSTACK_PAGES * PAGE_SIZE, M_WAITOK | M_ZERO);
348                 doublefault_stack = (char *)kmem_malloc(kernel_arena,
349                     PAGE_SIZE, M_WAITOK | M_ZERO);
350                 nmi_stack = (char *)kmem_malloc(kernel_arena, PAGE_SIZE,
351                     M_WAITOK | M_ZERO);
352                 dpcpu = (void *)kmem_malloc(kernel_arena, DPCPU_SIZE,
353                     M_WAITOK | M_ZERO);
354
355                 bootSTK = (char *)bootstacks[cpu] + KSTACK_PAGES * PAGE_SIZE - 8;
356                 bootAP = cpu;
357
358                 /* attempt to start the Application Processor */
359                 if (!start_ap(apic_id)) {
360                         /* restore the warmstart vector */
361                         *(u_int32_t *) WARMBOOT_OFF = mpbioswarmvec;
362                         panic("AP #%d (PHY# %d) failed!", cpu, apic_id);
363                 }
364
365                 CPU_SET(cpu, &all_cpus);        /* record AP in CPU map */
366         }
367
368         /* restore the warmstart vector */
369         *(u_int32_t *) WARMBOOT_OFF = mpbioswarmvec;
370
371         outb(CMOS_REG, BIOS_RESET);
372         outb(CMOS_DATA, mpbiosreason);
373
374         /* number of APs actually started */
375         return mp_naps;
376 }
377
378
379 /*
380  * This function starts the AP (application processor) identified
381  * by the APIC ID 'physicalCpu'.  It does quite a "song and dance"
382  * to accomplish this.  This is necessary because of the nuances
383  * of the different hardware we might encounter.  It isn't pretty,
384  * but it seems to work.
385  */
386 static int
387 start_ap(int apic_id)
388 {
389         int vector, ms;
390         int cpus;
391
392         /* calculate the vector */
393         vector = (boot_address >> 12) & 0xff;
394
395         /* used as a watchpoint to signal AP startup */
396         cpus = mp_naps;
397
398         ipi_startup(apic_id, vector);
399
400         /* Wait up to 5 seconds for it to start. */
401         for (ms = 0; ms < 5000; ms++) {
402                 if (mp_naps > cpus)
403                         return 1;       /* return SUCCESS */
404                 DELAY(1000);
405         }
406         return 0;               /* return FAILURE */
407 }
408
409 /*
410  * Flush the TLB on all other CPU's
411  */
412 static void
413 smp_tlb_shootdown(u_int vector, pmap_t pmap, vm_offset_t addr1,
414     vm_offset_t addr2)
415 {
416         u_int ncpu;
417
418         ncpu = mp_ncpus - 1;    /* does not shootdown self */
419         if (ncpu < 1)
420                 return;         /* no other cpus */
421         if (!(read_rflags() & PSL_I))
422                 panic("%s: interrupts disabled", __func__);
423         mtx_lock_spin(&smp_ipi_mtx);
424         smp_tlb_invpcid.addr = addr1;
425         if (pmap == NULL) {
426                 smp_tlb_invpcid.pcid = 0;
427         } else {
428                 smp_tlb_invpcid.pcid = pmap->pm_pcid;
429                 pcid_cr3 = pmap->pm_cr3;
430         }
431         smp_tlb_addr2 = addr2;
432         smp_tlb_pmap = pmap;
433         atomic_store_rel_int(&smp_tlb_wait, 0);
434         ipi_all_but_self(vector);
435         while (smp_tlb_wait < ncpu)
436                 ia32_pause();
437         mtx_unlock_spin(&smp_ipi_mtx);
438 }
439
440 static void
441 smp_targeted_tlb_shootdown(cpuset_t mask, u_int vector, pmap_t pmap,
442     vm_offset_t addr1, vm_offset_t addr2)
443 {
444         int cpu, ncpu, othercpus;
445
446         othercpus = mp_ncpus - 1;
447         if (CPU_ISFULLSET(&mask)) {
448                 if (othercpus < 1)
449                         return;
450         } else {
451                 CPU_CLR(PCPU_GET(cpuid), &mask);
452                 if (CPU_EMPTY(&mask))
453                         return;
454         }
455         if (!(read_rflags() & PSL_I))
456                 panic("%s: interrupts disabled", __func__);
457         mtx_lock_spin(&smp_ipi_mtx);
458         smp_tlb_invpcid.addr = addr1;
459         if (pmap == NULL) {
460                 smp_tlb_invpcid.pcid = 0;
461         } else {
462                 smp_tlb_invpcid.pcid = pmap->pm_pcid;
463                 pcid_cr3 = pmap->pm_cr3;
464         }
465         smp_tlb_addr2 = addr2;
466         smp_tlb_pmap = pmap;
467         atomic_store_rel_int(&smp_tlb_wait, 0);
468         if (CPU_ISFULLSET(&mask)) {
469                 ncpu = othercpus;
470                 ipi_all_but_self(vector);
471         } else {
472                 ncpu = 0;
473                 while ((cpu = CPU_FFS(&mask)) != 0) {
474                         cpu--;
475                         CPU_CLR(cpu, &mask);
476                         CTR3(KTR_SMP, "%s: cpu: %d ipi: %x", __func__,
477                             cpu, vector);
478                         ipi_send_cpu(cpu, vector);
479                         ncpu++;
480                 }
481         }
482         while (smp_tlb_wait < ncpu)
483                 ia32_pause();
484         mtx_unlock_spin(&smp_ipi_mtx);
485 }
486
487 void
488 smp_invlpg(pmap_t pmap, vm_offset_t addr)
489 {
490
491         if (smp_started) {
492                 smp_tlb_shootdown(IPI_INVLPG, pmap, addr, 0);
493 #ifdef COUNT_XINVLTLB_HITS
494                 ipi_page++;
495 #endif
496         }
497 }
498
499 void
500 smp_invlpg_range(pmap_t pmap, vm_offset_t addr1, vm_offset_t addr2)
501 {
502
503         if (smp_started) {
504                 smp_tlb_shootdown(IPI_INVLRNG, pmap, addr1, addr2);
505 #ifdef COUNT_XINVLTLB_HITS
506                 ipi_range++;
507                 ipi_range_size += (addr2 - addr1) / PAGE_SIZE;
508 #endif
509         }
510 }
511
512 void
513 smp_masked_invltlb(cpuset_t mask, pmap_t pmap)
514 {
515
516         if (smp_started) {
517                 smp_targeted_tlb_shootdown(mask, IPI_INVLTLB, pmap, 0, 0);
518 #ifdef COUNT_XINVLTLB_HITS
519                 ipi_masked_global++;
520 #endif
521         }
522 }
523
524 void
525 smp_masked_invlpg(cpuset_t mask, pmap_t pmap, vm_offset_t addr)
526 {
527
528         if (smp_started) {
529                 smp_targeted_tlb_shootdown(mask, IPI_INVLPG, pmap, addr, 0);
530 #ifdef COUNT_XINVLTLB_HITS
531                 ipi_masked_page++;
532 #endif
533         }
534 }
535
536 void
537 smp_masked_invlpg_range(cpuset_t mask, pmap_t pmap, vm_offset_t addr1,
538     vm_offset_t addr2)
539 {
540
541         if (smp_started) {
542                 smp_targeted_tlb_shootdown(mask, IPI_INVLRNG, pmap, addr1,
543                     addr2);
544 #ifdef COUNT_XINVLTLB_HITS
545                 ipi_masked_range++;
546                 ipi_masked_range_size += (addr2 - addr1) / PAGE_SIZE;
547 #endif
548         }
549 }
550
551 void
552 smp_cache_flush(void)
553 {
554
555         if (smp_started)
556                 smp_tlb_shootdown(IPI_INVLCACHE, NULL, 0, 0);
557 }
558
559 void
560 smp_invltlb(pmap_t pmap)
561
562
563         if (smp_started) {
564                 smp_tlb_shootdown(IPI_INVLTLB, pmap, 0, 0);
565 #ifdef COUNT_XINVLTLB_HITS
566                 ipi_global++;
567 #endif
568         }
569 }
570
571 /*
572  * Handlers for TLB related IPIs
573  */
574 void
575 invltlb_handler(void)
576 {
577 #ifdef COUNT_XINVLTLB_HITS
578         xhits_gbl[PCPU_GET(cpuid)]++;
579 #endif /* COUNT_XINVLTLB_HITS */
580 #ifdef COUNT_IPIS
581         (*ipi_invltlb_counts[PCPU_GET(cpuid)])++;
582 #endif /* COUNT_IPIS */
583
584         invltlb();
585         atomic_add_int(&smp_tlb_wait, 1);
586 }
587
588 void
589 invltlb_pcid_handler(void)
590 {
591         uint64_t cr3;
592         u_int cpuid;
593 #ifdef COUNT_XINVLTLB_HITS
594         xhits_gbl[PCPU_GET(cpuid)]++;
595 #endif /* COUNT_XINVLTLB_HITS */
596 #ifdef COUNT_IPIS
597         (*ipi_invltlb_counts[PCPU_GET(cpuid)])++;
598 #endif /* COUNT_IPIS */
599
600         if (smp_tlb_invpcid.pcid != (uint64_t)-1 &&
601             smp_tlb_invpcid.pcid != 0) {
602                 if (invpcid_works) {
603                         invpcid(&smp_tlb_invpcid, INVPCID_CTX);
604                 } else {
605                         /* Otherwise reload %cr3 twice. */
606                         cr3 = rcr3();
607                         if (cr3 != pcid_cr3) {
608                                 load_cr3(pcid_cr3);
609                                 cr3 |= CR3_PCID_SAVE;
610                         }
611                         load_cr3(cr3);
612                 }
613         } else {
614                 invltlb_globpcid();
615         }
616         if (smp_tlb_pmap != NULL) {
617                 cpuid = PCPU_GET(cpuid);
618                 if (!CPU_ISSET(cpuid, &smp_tlb_pmap->pm_active))
619                         CPU_CLR_ATOMIC(cpuid, &smp_tlb_pmap->pm_save);
620         }
621
622         atomic_add_int(&smp_tlb_wait, 1);
623 }
624
625 void
626 invlpg_handler(void)
627 {
628 #ifdef COUNT_XINVLTLB_HITS
629         xhits_pg[PCPU_GET(cpuid)]++;
630 #endif /* COUNT_XINVLTLB_HITS */
631 #ifdef COUNT_IPIS
632         (*ipi_invlpg_counts[PCPU_GET(cpuid)])++;
633 #endif /* COUNT_IPIS */
634
635         invlpg(smp_tlb_invpcid.addr);
636         atomic_add_int(&smp_tlb_wait, 1);
637 }
638
639 void
640 invlpg_pcid_handler(void)
641 {
642         uint64_t cr3;
643 #ifdef COUNT_XINVLTLB_HITS
644         xhits_pg[PCPU_GET(cpuid)]++;
645 #endif /* COUNT_XINVLTLB_HITS */
646 #ifdef COUNT_IPIS
647         (*ipi_invlpg_counts[PCPU_GET(cpuid)])++;
648 #endif /* COUNT_IPIS */
649
650         if (smp_tlb_invpcid.pcid == (uint64_t)-1) {
651                 invltlb_globpcid();
652         } else if (smp_tlb_invpcid.pcid == 0) {
653                 invlpg(smp_tlb_invpcid.addr);
654         } else if (invpcid_works) {
655                 invpcid(&smp_tlb_invpcid, INVPCID_ADDR);
656         } else {
657                 /*
658                  * PCID supported, but INVPCID is not.
659                  * Temporarily switch to the target address
660                  * space and do INVLPG.
661                  */
662                 cr3 = rcr3();
663                 if (cr3 != pcid_cr3)
664                         load_cr3(pcid_cr3 | CR3_PCID_SAVE);
665                 invlpg(smp_tlb_invpcid.addr);
666                 load_cr3(cr3 | CR3_PCID_SAVE);
667         }
668
669         atomic_add_int(&smp_tlb_wait, 1);
670 }
671
672 static inline void
673 invlpg_range(vm_offset_t start, vm_offset_t end)
674 {
675
676         do {
677                 invlpg(start);
678                 start += PAGE_SIZE;
679         } while (start < end);
680 }
681
682 void
683 invlrng_handler(void)
684 {
685         struct invpcid_descr d;
686         vm_offset_t addr;
687         uint64_t cr3;
688         u_int cpuid;
689 #ifdef COUNT_XINVLTLB_HITS
690         xhits_rng[PCPU_GET(cpuid)]++;
691 #endif /* COUNT_XINVLTLB_HITS */
692 #ifdef COUNT_IPIS
693         (*ipi_invlrng_counts[PCPU_GET(cpuid)])++;
694 #endif /* COUNT_IPIS */
695
696         addr = smp_tlb_invpcid.addr;
697         if (pmap_pcid_enabled) {
698                 if (smp_tlb_invpcid.pcid == 0) {
699                         /*
700                          * kernel pmap - use invlpg to invalidate
701                          * global mapping.
702                          */
703                         invlpg_range(addr, smp_tlb_addr2);
704                 } else if (smp_tlb_invpcid.pcid == (uint64_t)-1) {
705                         invltlb_globpcid();
706                         if (smp_tlb_pmap != NULL) {
707                                 cpuid = PCPU_GET(cpuid);
708                                 if (!CPU_ISSET(cpuid, &smp_tlb_pmap->pm_active))
709                                         CPU_CLR_ATOMIC(cpuid,
710                                             &smp_tlb_pmap->pm_save);
711                         }
712                 } else if (invpcid_works) {
713                         d = smp_tlb_invpcid;
714                         do {
715                                 invpcid(&d, INVPCID_ADDR);
716                                 d.addr += PAGE_SIZE;
717                         } while (d.addr <= smp_tlb_addr2);
718                 } else {
719                         cr3 = rcr3();
720                         if (cr3 != pcid_cr3)
721                                 load_cr3(pcid_cr3 | CR3_PCID_SAVE);
722                         invlpg_range(addr, smp_tlb_addr2);
723                         load_cr3(cr3 | CR3_PCID_SAVE);
724                 }
725         } else {
726                 invlpg_range(addr, smp_tlb_addr2);
727         }
728
729         atomic_add_int(&smp_tlb_wait, 1);
730 }