]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/amd64/amd64/mp_machdep.c
Merge compiler-rt trunk r366426, resolve conflicts, and add
[FreeBSD/FreeBSD.git] / sys / amd64 / amd64 / mp_machdep.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 1996, by Steve Passe
5  * Copyright (c) 2003, by Peter Wemm
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. The name of the developer may NOT be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include "opt_cpu.h"
33 #include "opt_ddb.h"
34 #include "opt_kstack_pages.h"
35 #include "opt_sched.h"
36 #include "opt_smp.h"
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/bus.h>
41 #include <sys/cpuset.h>
42 #include <sys/domainset.h>
43 #ifdef GPROF 
44 #include <sys/gmon.h>
45 #endif
46 #include <sys/kernel.h>
47 #include <sys/ktr.h>
48 #include <sys/lock.h>
49 #include <sys/malloc.h>
50 #include <sys/memrange.h>
51 #include <sys/mutex.h>
52 #include <sys/pcpu.h>
53 #include <sys/proc.h>
54 #include <sys/sched.h>
55 #include <sys/smp.h>
56 #include <sys/sysctl.h>
57
58 #include <vm/vm.h>
59 #include <vm/vm_param.h>
60 #include <vm/pmap.h>
61 #include <vm/vm_kern.h>
62 #include <vm/vm_extern.h>
63 #include <vm/vm_page.h>
64 #include <vm/vm_phys.h>
65
66 #include <x86/apicreg.h>
67 #include <machine/clock.h>
68 #include <machine/cputypes.h>
69 #include <machine/cpufunc.h>
70 #include <x86/mca.h>
71 #include <machine/md_var.h>
72 #include <machine/pcb.h>
73 #include <machine/psl.h>
74 #include <machine/smp.h>
75 #include <machine/specialreg.h>
76 #include <machine/tss.h>
77 #include <x86/ucode.h>
78 #include <machine/cpu.h>
79 #include <x86/init.h>
80
81 #include <contrib/dev/acpica/include/acpi.h>
82 #include <dev/acpica/acpivar.h>
83
84 #define WARMBOOT_TARGET         0
85 #define WARMBOOT_OFF            (KERNBASE + 0x0467)
86 #define WARMBOOT_SEG            (KERNBASE + 0x0469)
87
88 #define CMOS_REG                (0x70)
89 #define CMOS_DATA               (0x71)
90 #define BIOS_RESET              (0x0f)
91 #define BIOS_WARM               (0x0a)
92
93 #define GiB(v)                  (v ## ULL << 30)
94
95 #define AP_BOOTPT_SZ            (PAGE_SIZE * 3)
96
97 extern  struct pcpu __pcpu[];
98
99 /* Temporary variables for init_secondary()  */
100 char *doublefault_stack;
101 char *mce_stack;
102 char *nmi_stack;
103 char *dbg_stack;
104
105 /*
106  * Local data and functions.
107  */
108
109 static int      start_ap(int apic_id);
110
111 static bool
112 is_kernel_paddr(vm_paddr_t pa)
113 {
114
115         return (pa >= trunc_2mpage(btext - KERNBASE) &&
116            pa < round_page(_end - KERNBASE));
117 }
118
119 static bool
120 is_mpboot_good(vm_paddr_t start, vm_paddr_t end)
121 {
122
123         return (start + AP_BOOTPT_SZ <= GiB(4) && atop(end) < Maxmem);
124 }
125
126 /*
127  * Calculate usable address in base memory for AP trampoline code.
128  */
129 void
130 mp_bootaddress(vm_paddr_t *physmap, unsigned int *physmap_idx)
131 {
132         vm_paddr_t start, end;
133         unsigned int i;
134         bool allocated;
135
136         alloc_ap_trampoline(physmap, physmap_idx);
137
138         /*
139          * Find a memory region big enough below the 4GB boundary to
140          * store the initial page tables.  Region must be mapped by
141          * the direct map.
142          *
143          * Note that it needs to be aligned to a page boundary.
144          */
145         allocated = false;
146         for (i = *physmap_idx; i <= *physmap_idx; i -= 2) {
147                 /*
148                  * First, try to chomp at the start of the physmap region.
149                  * Kernel binary might claim it already.
150                  */
151                 start = round_page(physmap[i]);
152                 end = start + AP_BOOTPT_SZ;
153                 if (start < end && end <= physmap[i + 1] &&
154                     is_mpboot_good(start, end) &&
155                     !is_kernel_paddr(start) && !is_kernel_paddr(end - 1)) {
156                         allocated = true;
157                         physmap[i] = end;
158                         break;
159                 }
160
161                 /*
162                  * Second, try to chomp at the end.  Again, check
163                  * against kernel.
164                  */
165                 end = trunc_page(physmap[i + 1]);
166                 start = end - AP_BOOTPT_SZ;
167                 if (start < end && start >= physmap[i] &&
168                     is_mpboot_good(start, end) &&
169                     !is_kernel_paddr(start) && !is_kernel_paddr(end - 1)) {
170                         allocated = true;
171                         physmap[i + 1] = start;
172                         break;
173                 }
174         }
175         if (allocated) {
176                 mptramp_pagetables = start;
177                 if (physmap[i] == physmap[i + 1] && *physmap_idx != 0) {
178                         memmove(&physmap[i], &physmap[i + 2],
179                             sizeof(*physmap) * (*physmap_idx - i + 2));
180                         *physmap_idx -= 2;
181                 }
182         } else {
183                 mptramp_pagetables = trunc_page(boot_address) - AP_BOOTPT_SZ;
184                 if (bootverbose)
185                         printf(
186 "Cannot find enough space for the initial AP page tables, placing them at %#x",
187                             mptramp_pagetables);
188         }
189 }
190
191 /*
192  * Initialize the IPI handlers and start up the AP's.
193  */
194 void
195 cpu_mp_start(void)
196 {
197         int i;
198
199         /* Initialize the logical ID to APIC ID table. */
200         for (i = 0; i < MAXCPU; i++) {
201                 cpu_apic_ids[i] = -1;
202         }
203
204         /* Install an inter-CPU IPI for TLB invalidation */
205         if (pmap_pcid_enabled) {
206                 if (invpcid_works) {
207                         setidt(IPI_INVLTLB, pti ?
208                             IDTVEC(invltlb_invpcid_pti_pti) :
209                             IDTVEC(invltlb_invpcid_nopti), SDT_SYSIGT,
210                             SEL_KPL, 0);
211                         setidt(IPI_INVLPG, pti ? IDTVEC(invlpg_invpcid_pti) :
212                             IDTVEC(invlpg_invpcid), SDT_SYSIGT, SEL_KPL, 0);
213                         setidt(IPI_INVLRNG, pti ? IDTVEC(invlrng_invpcid_pti) :
214                             IDTVEC(invlrng_invpcid), SDT_SYSIGT, SEL_KPL, 0);
215                 } else {
216                         setidt(IPI_INVLTLB, pti ? IDTVEC(invltlb_pcid_pti) :
217                             IDTVEC(invltlb_pcid), SDT_SYSIGT, SEL_KPL, 0);
218                         setidt(IPI_INVLPG, pti ? IDTVEC(invlpg_pcid_pti) :
219                             IDTVEC(invlpg_pcid), SDT_SYSIGT, SEL_KPL, 0);
220                         setidt(IPI_INVLRNG, pti ? IDTVEC(invlrng_pcid_pti) :
221                             IDTVEC(invlrng_pcid), SDT_SYSIGT, SEL_KPL, 0);
222                 }
223         } else {
224                 setidt(IPI_INVLTLB, pti ? IDTVEC(invltlb_pti) : IDTVEC(invltlb),
225                     SDT_SYSIGT, SEL_KPL, 0);
226                 setidt(IPI_INVLPG, pti ? IDTVEC(invlpg_pti) : IDTVEC(invlpg),
227                     SDT_SYSIGT, SEL_KPL, 0);
228                 setidt(IPI_INVLRNG, pti ? IDTVEC(invlrng_pti) : IDTVEC(invlrng),
229                     SDT_SYSIGT, SEL_KPL, 0);
230         }
231
232         /* Install an inter-CPU IPI for cache invalidation. */
233         setidt(IPI_INVLCACHE, pti ? IDTVEC(invlcache_pti) : IDTVEC(invlcache),
234             SDT_SYSIGT, SEL_KPL, 0);
235
236         /* Install an inter-CPU IPI for all-CPU rendezvous */
237         setidt(IPI_RENDEZVOUS, pti ? IDTVEC(rendezvous_pti) :
238             IDTVEC(rendezvous), SDT_SYSIGT, SEL_KPL, 0);
239
240         /* Install generic inter-CPU IPI handler */
241         setidt(IPI_BITMAP_VECTOR, pti ? IDTVEC(ipi_intr_bitmap_handler_pti) :
242             IDTVEC(ipi_intr_bitmap_handler), SDT_SYSIGT, SEL_KPL, 0);
243
244         /* Install an inter-CPU IPI for CPU stop/restart */
245         setidt(IPI_STOP, pti ? IDTVEC(cpustop_pti) : IDTVEC(cpustop),
246             SDT_SYSIGT, SEL_KPL, 0);
247
248         /* Install an inter-CPU IPI for CPU suspend/resume */
249         setidt(IPI_SUSPEND, pti ? IDTVEC(cpususpend_pti) : IDTVEC(cpususpend),
250             SDT_SYSIGT, SEL_KPL, 0);
251
252         /* Set boot_cpu_id if needed. */
253         if (boot_cpu_id == -1) {
254                 boot_cpu_id = PCPU_GET(apic_id);
255                 cpu_info[boot_cpu_id].cpu_bsp = 1;
256         } else
257                 KASSERT(boot_cpu_id == PCPU_GET(apic_id),
258                     ("BSP's APIC ID doesn't match boot_cpu_id"));
259
260         /* Probe logical/physical core configuration. */
261         topo_probe();
262
263         assign_cpu_ids();
264
265         /* Start each Application Processor */
266         init_ops.start_all_aps();
267
268         set_interrupt_apic_ids();
269 }
270
271
272 /*
273  * AP CPU's call this to initialize themselves.
274  */
275 void
276 init_secondary(void)
277 {
278         struct pcpu *pc;
279         struct nmi_pcpu *np;
280         u_int64_t cr0;
281         int cpu, gsel_tss, x;
282         struct region_descriptor ap_gdt;
283
284         /* Set by the startup code for us to use */
285         cpu = bootAP;
286
287         /* Update microcode before doing anything else. */
288         ucode_load_ap(cpu);
289
290         /* Init tss */
291         common_tss[cpu] = common_tss[0];
292         common_tss[cpu].tss_iobase = sizeof(struct amd64tss) +
293             IOPERM_BITMAP_SIZE;
294         common_tss[cpu].tss_ist1 = (long)&doublefault_stack[PAGE_SIZE];
295
296         /* The NMI stack runs on IST2. */
297         np = ((struct nmi_pcpu *) &nmi_stack[PAGE_SIZE]) - 1;
298         common_tss[cpu].tss_ist2 = (long) np;
299
300         /* The MC# stack runs on IST3. */
301         np = ((struct nmi_pcpu *) &mce_stack[PAGE_SIZE]) - 1;
302         common_tss[cpu].tss_ist3 = (long) np;
303
304         /* The DB# stack runs on IST4. */
305         np = ((struct nmi_pcpu *) &dbg_stack[PAGE_SIZE]) - 1;
306         common_tss[cpu].tss_ist4 = (long) np;
307
308         /* Prepare private GDT */
309         gdt_segs[GPROC0_SEL].ssd_base = (long) &common_tss[cpu];
310         for (x = 0; x < NGDT; x++) {
311                 if (x != GPROC0_SEL && x != (GPROC0_SEL + 1) &&
312                     x != GUSERLDT_SEL && x != (GUSERLDT_SEL + 1))
313                         ssdtosd(&gdt_segs[x], &gdt[NGDT * cpu + x]);
314         }
315         ssdtosyssd(&gdt_segs[GPROC0_SEL],
316             (struct system_segment_descriptor *)&gdt[NGDT * cpu + GPROC0_SEL]);
317         ap_gdt.rd_limit = NGDT * sizeof(gdt[0]) - 1;
318         ap_gdt.rd_base =  (long) &gdt[NGDT * cpu];
319         lgdt(&ap_gdt);                  /* does magic intra-segment return */
320
321         /* Get per-cpu data */
322         pc = &__pcpu[cpu];
323
324         /* prime data page for it to use */
325         pcpu_init(pc, cpu, sizeof(struct pcpu));
326         dpcpu_init(dpcpu, cpu);
327         pc->pc_apic_id = cpu_apic_ids[cpu];
328         pc->pc_prvspace = pc;
329         pc->pc_curthread = 0;
330         pc->pc_tssp = &common_tss[cpu];
331         pc->pc_commontssp = &common_tss[cpu];
332         pc->pc_rsp0 = 0;
333         pc->pc_pti_rsp0 = (((vm_offset_t)&pc->pc_pti_stack +
334             PC_PTI_STACK_SZ * sizeof(uint64_t)) & ~0xful);
335         pc->pc_tss = (struct system_segment_descriptor *)&gdt[NGDT * cpu +
336             GPROC0_SEL];
337         pc->pc_fs32p = &gdt[NGDT * cpu + GUFS32_SEL];
338         pc->pc_gs32p = &gdt[NGDT * cpu + GUGS32_SEL];
339         pc->pc_ldt = (struct system_segment_descriptor *)&gdt[NGDT * cpu +
340             GUSERLDT_SEL];
341         /* See comment in pmap_bootstrap(). */
342         pc->pc_pcid_next = PMAP_PCID_KERN + 2;
343         pc->pc_pcid_gen = 1;
344         common_tss[cpu].tss_rsp0 = 0;
345
346         /* Save the per-cpu pointer for use by the NMI handler. */
347         np = ((struct nmi_pcpu *) &nmi_stack[PAGE_SIZE]) - 1;
348         np->np_pcpu = (register_t) pc;
349
350         /* Save the per-cpu pointer for use by the MC# handler. */
351         np = ((struct nmi_pcpu *) &mce_stack[PAGE_SIZE]) - 1;
352         np->np_pcpu = (register_t) pc;
353
354         /* Save the per-cpu pointer for use by the DB# handler. */
355         np = ((struct nmi_pcpu *) &dbg_stack[PAGE_SIZE]) - 1;
356         np->np_pcpu = (register_t) pc;
357
358         wrmsr(MSR_FSBASE, 0);           /* User value */
359         wrmsr(MSR_GSBASE, (u_int64_t)pc);
360         wrmsr(MSR_KGSBASE, (u_int64_t)pc);      /* XXX User value while we're in the kernel */
361         fix_cpuid();
362
363         lidt(&r_idt);
364
365         gsel_tss = GSEL(GPROC0_SEL, SEL_KPL);
366         ltr(gsel_tss);
367
368         /*
369          * Set to a known state:
370          * Set by mpboot.s: CR0_PG, CR0_PE
371          * Set by cpu_setregs: CR0_NE, CR0_MP, CR0_TS, CR0_WP, CR0_AM
372          */
373         cr0 = rcr0();
374         cr0 &= ~(CR0_CD | CR0_NW | CR0_EM);
375         load_cr0(cr0);
376
377         amd64_conf_fast_syscall();
378
379         /* signal our startup to the BSP. */
380         mp_naps++;
381
382         /* Spin until the BSP releases the AP's. */
383         while (atomic_load_acq_int(&aps_ready) == 0)
384                 ia32_pause();
385
386         init_secondary_tail();
387 }
388
389 /*******************************************************************
390  * local functions and data
391  */
392
393 #ifdef NUMA
394 static void
395 mp_realloc_pcpu(int cpuid, int domain)
396 {
397         vm_page_t m;
398         vm_offset_t oa, na;
399
400         oa = (vm_offset_t)&__pcpu[cpuid];
401         if (_vm_phys_domain(pmap_kextract(oa)) == domain)
402                 return;
403         m = vm_page_alloc_domain(NULL, 0, domain,
404             VM_ALLOC_NORMAL | VM_ALLOC_NOOBJ | VM_ALLOC_ZERO);
405         na = PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m));
406         pagecopy((void *)oa, (void *)na);
407         pmap_enter(kernel_pmap, oa, m, VM_PROT_READ | VM_PROT_WRITE, 0, 0);
408         /* XXX old pcpu page leaked. */
409 }
410 #endif
411
412 /*
413  * start each AP in our list
414  */
415 int
416 native_start_all_aps(void)
417 {
418         u_int64_t *pt4, *pt3, *pt2;
419         u_int32_t mpbioswarmvec;
420         int apic_id, cpu, domain, i;
421         u_char mpbiosreason;
422
423         mtx_init(&ap_boot_mtx, "ap boot", NULL, MTX_SPIN);
424
425         /* copy the AP 1st level boot code */
426         bcopy(mptramp_start, (void *)PHYS_TO_DMAP(boot_address), bootMP_size);
427
428         /* Locate the page tables, they'll be below the trampoline */
429         pt4 = (uint64_t *)PHYS_TO_DMAP(mptramp_pagetables);
430         pt3 = pt4 + (PAGE_SIZE) / sizeof(u_int64_t);
431         pt2 = pt3 + (PAGE_SIZE) / sizeof(u_int64_t);
432
433         /* Create the initial 1GB replicated page tables */
434         for (i = 0; i < 512; i++) {
435                 /* Each slot of the level 4 pages points to the same level 3 page */
436                 pt4[i] = (u_int64_t)(uintptr_t)(mptramp_pagetables + PAGE_SIZE);
437                 pt4[i] |= PG_V | PG_RW | PG_U;
438
439                 /* Each slot of the level 3 pages points to the same level 2 page */
440                 pt3[i] = (u_int64_t)(uintptr_t)(mptramp_pagetables + (2 * PAGE_SIZE));
441                 pt3[i] |= PG_V | PG_RW | PG_U;
442
443                 /* The level 2 page slots are mapped with 2MB pages for 1GB. */
444                 pt2[i] = i * (2 * 1024 * 1024);
445                 pt2[i] |= PG_V | PG_RW | PG_PS | PG_U;
446         }
447
448         /* save the current value of the warm-start vector */
449         mpbioswarmvec = *((u_int32_t *) WARMBOOT_OFF);
450         outb(CMOS_REG, BIOS_RESET);
451         mpbiosreason = inb(CMOS_DATA);
452
453         /* setup a vector to our boot code */
454         *((volatile u_short *) WARMBOOT_OFF) = WARMBOOT_TARGET;
455         *((volatile u_short *) WARMBOOT_SEG) = (boot_address >> 4);
456         outb(CMOS_REG, BIOS_RESET);
457         outb(CMOS_DATA, BIOS_WARM);     /* 'warm-start' */
458
459         /* Relocate pcpu areas to the correct domain. */
460 #ifdef NUMA
461         if (vm_ndomains > 1)
462                 for (cpu = 1; cpu < mp_ncpus; cpu++) {
463                         apic_id = cpu_apic_ids[cpu];
464                         domain = acpi_pxm_get_cpu_locality(apic_id);
465                         mp_realloc_pcpu(cpu, domain);
466                 }
467 #endif
468
469         /* start each AP */
470         domain = 0;
471         for (cpu = 1; cpu < mp_ncpus; cpu++) {
472                 apic_id = cpu_apic_ids[cpu];
473 #ifdef NUMA
474                 if (vm_ndomains > 1)
475                         domain = acpi_pxm_get_cpu_locality(apic_id);
476 #endif
477                 /* allocate and set up an idle stack data page */
478                 bootstacks[cpu] = (void *)kmem_malloc_domainset(
479                     DOMAINSET_FIXED(domain), kstack_pages * PAGE_SIZE,
480                     M_WAITOK | M_ZERO);
481                 doublefault_stack = (char *)kmem_malloc_domainset(
482                     DOMAINSET_FIXED(domain), PAGE_SIZE, M_WAITOK | M_ZERO);
483                 mce_stack = (char *)kmem_malloc_domainset(
484                     DOMAINSET_FIXED(domain), PAGE_SIZE, M_WAITOK | M_ZERO);
485                 nmi_stack = (char *)kmem_malloc_domainset(
486                     DOMAINSET_FIXED(domain), PAGE_SIZE, M_WAITOK | M_ZERO);
487                 dbg_stack = (char *)kmem_malloc_domainset(
488                     DOMAINSET_FIXED(domain), PAGE_SIZE, M_WAITOK | M_ZERO);
489                 dpcpu = (void *)kmem_malloc_domainset(DOMAINSET_FIXED(domain),
490                     DPCPU_SIZE, M_WAITOK | M_ZERO);
491
492                 bootSTK = (char *)bootstacks[cpu] +
493                     kstack_pages * PAGE_SIZE - 8;
494                 bootAP = cpu;
495
496                 /* attempt to start the Application Processor */
497                 if (!start_ap(apic_id)) {
498                         /* restore the warmstart vector */
499                         *(u_int32_t *) WARMBOOT_OFF = mpbioswarmvec;
500                         panic("AP #%d (PHY# %d) failed!", cpu, apic_id);
501                 }
502
503                 CPU_SET(cpu, &all_cpus);        /* record AP in CPU map */
504         }
505
506         /* restore the warmstart vector */
507         *(u_int32_t *) WARMBOOT_OFF = mpbioswarmvec;
508
509         outb(CMOS_REG, BIOS_RESET);
510         outb(CMOS_DATA, mpbiosreason);
511
512         /* number of APs actually started */
513         return mp_naps;
514 }
515
516
517 /*
518  * This function starts the AP (application processor) identified
519  * by the APIC ID 'physicalCpu'.  It does quite a "song and dance"
520  * to accomplish this.  This is necessary because of the nuances
521  * of the different hardware we might encounter.  It isn't pretty,
522  * but it seems to work.
523  */
524 static int
525 start_ap(int apic_id)
526 {
527         int vector, ms;
528         int cpus;
529
530         /* calculate the vector */
531         vector = (boot_address >> 12) & 0xff;
532
533         /* used as a watchpoint to signal AP startup */
534         cpus = mp_naps;
535
536         ipi_startup(apic_id, vector);
537
538         /* Wait up to 5 seconds for it to start. */
539         for (ms = 0; ms < 5000; ms++) {
540                 if (mp_naps > cpus)
541                         return 1;       /* return SUCCESS */
542                 DELAY(1000);
543         }
544         return 0;               /* return FAILURE */
545 }
546
547 void
548 invltlb_invpcid_handler(void)
549 {
550         struct invpcid_descr d;
551         uint32_t generation;
552
553 #ifdef COUNT_XINVLTLB_HITS
554         xhits_gbl[PCPU_GET(cpuid)]++;
555 #endif /* COUNT_XINVLTLB_HITS */
556 #ifdef COUNT_IPIS
557         (*ipi_invltlb_counts[PCPU_GET(cpuid)])++;
558 #endif /* COUNT_IPIS */
559
560         generation = smp_tlb_generation;
561         d.pcid = smp_tlb_pmap->pm_pcids[PCPU_GET(cpuid)].pm_pcid;
562         d.pad = 0;
563         d.addr = 0;
564         invpcid(&d, smp_tlb_pmap == kernel_pmap ? INVPCID_CTXGLOB :
565             INVPCID_CTX);
566         PCPU_SET(smp_tlb_done, generation);
567 }
568
569 void
570 invltlb_invpcid_pti_handler(void)
571 {
572         struct invpcid_descr d;
573         uint32_t generation;
574
575 #ifdef COUNT_XINVLTLB_HITS
576         xhits_gbl[PCPU_GET(cpuid)]++;
577 #endif /* COUNT_XINVLTLB_HITS */
578 #ifdef COUNT_IPIS
579         (*ipi_invltlb_counts[PCPU_GET(cpuid)])++;
580 #endif /* COUNT_IPIS */
581
582         generation = smp_tlb_generation;
583         d.pcid = smp_tlb_pmap->pm_pcids[PCPU_GET(cpuid)].pm_pcid;
584         d.pad = 0;
585         d.addr = 0;
586         if (smp_tlb_pmap == kernel_pmap) {
587                 /*
588                  * This invalidation actually needs to clear kernel
589                  * mappings from the TLB in the current pmap, but
590                  * since we were asked for the flush in the kernel
591                  * pmap, achieve it by performing global flush.
592                  */
593                 invpcid(&d, INVPCID_CTXGLOB);
594         } else {
595                 invpcid(&d, INVPCID_CTX);
596                 d.pcid |= PMAP_PCID_USER_PT;
597                 invpcid(&d, INVPCID_CTX);
598         }
599         PCPU_SET(smp_tlb_done, generation);
600 }
601
602 void
603 invltlb_pcid_handler(void)
604 {
605         uint64_t kcr3, ucr3;
606         uint32_t generation, pcid;
607   
608 #ifdef COUNT_XINVLTLB_HITS
609         xhits_gbl[PCPU_GET(cpuid)]++;
610 #endif /* COUNT_XINVLTLB_HITS */
611 #ifdef COUNT_IPIS
612         (*ipi_invltlb_counts[PCPU_GET(cpuid)])++;
613 #endif /* COUNT_IPIS */
614
615         generation = smp_tlb_generation;        /* Overlap with serialization */
616         if (smp_tlb_pmap == kernel_pmap) {
617                 invltlb_glob();
618         } else {
619                 /*
620                  * The current pmap might not be equal to
621                  * smp_tlb_pmap.  The clearing of the pm_gen in
622                  * pmap_invalidate_all() takes care of TLB
623                  * invalidation when switching to the pmap on this
624                  * CPU.
625                  */
626                 if (PCPU_GET(curpmap) == smp_tlb_pmap) {
627                         pcid = smp_tlb_pmap->pm_pcids[PCPU_GET(cpuid)].pm_pcid;
628                         kcr3 = smp_tlb_pmap->pm_cr3 | pcid;
629                         ucr3 = smp_tlb_pmap->pm_ucr3;
630                         if (ucr3 != PMAP_NO_CR3) {
631                                 ucr3 |= PMAP_PCID_USER_PT | pcid;
632                                 pmap_pti_pcid_invalidate(ucr3, kcr3);
633                         } else
634                                 load_cr3(kcr3);
635                 }
636         }
637         PCPU_SET(smp_tlb_done, generation);
638 }
639
640 void
641 invlpg_invpcid_handler(void)
642 {
643         struct invpcid_descr d;
644         uint32_t generation;
645
646 #ifdef COUNT_XINVLTLB_HITS
647         xhits_pg[PCPU_GET(cpuid)]++;
648 #endif /* COUNT_XINVLTLB_HITS */
649 #ifdef COUNT_IPIS
650         (*ipi_invlpg_counts[PCPU_GET(cpuid)])++;
651 #endif /* COUNT_IPIS */
652
653         generation = smp_tlb_generation;        /* Overlap with serialization */
654         invlpg(smp_tlb_addr1);
655         if (smp_tlb_pmap->pm_ucr3 != PMAP_NO_CR3) {
656                 d.pcid = smp_tlb_pmap->pm_pcids[PCPU_GET(cpuid)].pm_pcid |
657                     PMAP_PCID_USER_PT;
658                 d.pad = 0;
659                 d.addr = smp_tlb_addr1;
660                 invpcid(&d, INVPCID_ADDR);
661         }
662         PCPU_SET(smp_tlb_done, generation);
663 }
664
665 void
666 invlpg_pcid_handler(void)
667 {
668         uint64_t kcr3, ucr3;
669         uint32_t generation;
670         uint32_t pcid;
671
672 #ifdef COUNT_XINVLTLB_HITS
673         xhits_pg[PCPU_GET(cpuid)]++;
674 #endif /* COUNT_XINVLTLB_HITS */
675 #ifdef COUNT_IPIS
676         (*ipi_invlpg_counts[PCPU_GET(cpuid)])++;
677 #endif /* COUNT_IPIS */
678
679         generation = smp_tlb_generation;        /* Overlap with serialization */
680         invlpg(smp_tlb_addr1);
681         if (smp_tlb_pmap == PCPU_GET(curpmap) &&
682             (ucr3 = smp_tlb_pmap->pm_ucr3) != PMAP_NO_CR3) {
683                 pcid = smp_tlb_pmap->pm_pcids[PCPU_GET(cpuid)].pm_pcid;
684                 kcr3 = smp_tlb_pmap->pm_cr3 | pcid | CR3_PCID_SAVE;
685                 ucr3 |= pcid | PMAP_PCID_USER_PT | CR3_PCID_SAVE;
686                 pmap_pti_pcid_invlpg(ucr3, kcr3, smp_tlb_addr1);
687         }
688         PCPU_SET(smp_tlb_done, generation);
689 }
690
691 void
692 invlrng_invpcid_handler(void)
693 {
694         struct invpcid_descr d;
695         vm_offset_t addr, addr2;
696         uint32_t generation;
697
698 #ifdef COUNT_XINVLTLB_HITS
699         xhits_rng[PCPU_GET(cpuid)]++;
700 #endif /* COUNT_XINVLTLB_HITS */
701 #ifdef COUNT_IPIS
702         (*ipi_invlrng_counts[PCPU_GET(cpuid)])++;
703 #endif /* COUNT_IPIS */
704
705         addr = smp_tlb_addr1;
706         addr2 = smp_tlb_addr2;
707         generation = smp_tlb_generation;        /* Overlap with serialization */
708         do {
709                 invlpg(addr);
710                 addr += PAGE_SIZE;
711         } while (addr < addr2);
712         if (smp_tlb_pmap->pm_ucr3 != PMAP_NO_CR3) {
713                 d.pcid = smp_tlb_pmap->pm_pcids[PCPU_GET(cpuid)].pm_pcid |
714                     PMAP_PCID_USER_PT;
715                 d.pad = 0;
716                 d.addr = smp_tlb_addr1;
717                 do {
718                         invpcid(&d, INVPCID_ADDR);
719                         d.addr += PAGE_SIZE;
720                 } while (d.addr < addr2);
721         }
722         PCPU_SET(smp_tlb_done, generation);
723 }
724
725 void
726 invlrng_pcid_handler(void)
727 {
728         vm_offset_t addr, addr2;
729         uint64_t kcr3, ucr3;
730         uint32_t generation;
731         uint32_t pcid;
732
733 #ifdef COUNT_XINVLTLB_HITS
734         xhits_rng[PCPU_GET(cpuid)]++;
735 #endif /* COUNT_XINVLTLB_HITS */
736 #ifdef COUNT_IPIS
737         (*ipi_invlrng_counts[PCPU_GET(cpuid)])++;
738 #endif /* COUNT_IPIS */
739
740         addr = smp_tlb_addr1;
741         addr2 = smp_tlb_addr2;
742         generation = smp_tlb_generation;        /* Overlap with serialization */
743         do {
744                 invlpg(addr);
745                 addr += PAGE_SIZE;
746         } while (addr < addr2);
747         if (smp_tlb_pmap == PCPU_GET(curpmap) &&
748             (ucr3 = smp_tlb_pmap->pm_ucr3) != PMAP_NO_CR3) {
749                 pcid = smp_tlb_pmap->pm_pcids[PCPU_GET(cpuid)].pm_pcid;
750                 kcr3 = smp_tlb_pmap->pm_cr3 | pcid | CR3_PCID_SAVE;
751                 ucr3 |= pcid | PMAP_PCID_USER_PT | CR3_PCID_SAVE;
752                 pmap_pti_pcid_invlrng(ucr3, kcr3, smp_tlb_addr1, addr2);
753         }
754         PCPU_SET(smp_tlb_done, generation);
755 }