]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/amd64/amd64/mp_machdep.c
Eliminate the arena parameter to kmem_free(). Implicitly this corrects an
[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 #ifdef GPROF 
43 #include <sys/gmon.h>
44 #endif
45 #include <sys/kernel.h>
46 #include <sys/ktr.h>
47 #include <sys/lock.h>
48 #include <sys/malloc.h>
49 #include <sys/memrange.h>
50 #include <sys/mutex.h>
51 #include <sys/pcpu.h>
52 #include <sys/proc.h>
53 #include <sys/sched.h>
54 #include <sys/smp.h>
55 #include <sys/sysctl.h>
56
57 #include <vm/vm.h>
58 #include <vm/vm_param.h>
59 #include <vm/pmap.h>
60 #include <vm/vm_kern.h>
61 #include <vm/vm_extern.h>
62
63 #include <x86/apicreg.h>
64 #include <machine/clock.h>
65 #include <machine/cputypes.h>
66 #include <machine/cpufunc.h>
67 #include <x86/mca.h>
68 #include <machine/md_var.h>
69 #include <machine/pcb.h>
70 #include <machine/psl.h>
71 #include <machine/smp.h>
72 #include <machine/specialreg.h>
73 #include <machine/tss.h>
74 #include <x86/ucode.h>
75 #include <machine/cpu.h>
76 #include <x86/init.h>
77
78 #define WARMBOOT_TARGET         0
79 #define WARMBOOT_OFF            (KERNBASE + 0x0467)
80 #define WARMBOOT_SEG            (KERNBASE + 0x0469)
81
82 #define CMOS_REG                (0x70)
83 #define CMOS_DATA               (0x71)
84 #define BIOS_RESET              (0x0f)
85 #define BIOS_WARM               (0x0a)
86
87 #define GiB(v)                  (v ## ULL << 30)
88
89 extern  struct pcpu __pcpu[];
90
91 /* Temporary variables for init_secondary()  */
92 char *doublefault_stack;
93 char *mce_stack;
94 char *nmi_stack;
95 char *dbg_stack;
96
97 /*
98  * Local data and functions.
99  */
100
101 static int      start_ap(int apic_id);
102
103 /*
104  * Calculate usable address in base memory for AP trampoline code.
105  */
106 void
107 mp_bootaddress(vm_paddr_t *physmap, unsigned int *physmap_idx)
108 {
109         unsigned int i;
110         bool allocated;
111
112         alloc_ap_trampoline(physmap, physmap_idx);
113
114         allocated = false;
115         for (i = *physmap_idx; i <= *physmap_idx; i -= 2) {
116                 /*
117                  * Find a memory region big enough below the 4GB
118                  * boundary to store the initial page tables.  Region
119                  * must be mapped by the direct map.
120                  *
121                  * Note that it needs to be aligned to a page
122                  * boundary.
123                  */
124                 if (physmap[i] >= GiB(4) || physmap[i + 1] -
125                     round_page(physmap[i]) < PAGE_SIZE * 3 ||
126                     atop(physmap[i + 1]) > Maxmem)
127                         continue;
128
129                 allocated = true;
130                 mptramp_pagetables = round_page(physmap[i]);
131                 physmap[i] = round_page(physmap[i]) + (PAGE_SIZE * 3);
132                 if (physmap[i] == physmap[i + 1] && *physmap_idx != 0) {
133                         memmove(&physmap[i], &physmap[i + 2],
134                             sizeof(*physmap) * (*physmap_idx - i + 2));
135                         *physmap_idx -= 2;
136                 }
137                 break;
138         }
139
140         if (!allocated) {
141                 mptramp_pagetables = trunc_page(boot_address) - (PAGE_SIZE * 3);
142                 if (bootverbose)
143                         printf(
144 "Cannot find enough space for the initial AP page tables, placing them at %#x",
145                             mptramp_pagetables);
146         }
147 }
148
149 /*
150  * Initialize the IPI handlers and start up the AP's.
151  */
152 void
153 cpu_mp_start(void)
154 {
155         int i;
156
157         /* Initialize the logical ID to APIC ID table. */
158         for (i = 0; i < MAXCPU; i++) {
159                 cpu_apic_ids[i] = -1;
160                 cpu_ipi_pending[i] = 0;
161         }
162
163         /* Install an inter-CPU IPI for TLB invalidation */
164         if (pmap_pcid_enabled) {
165                 if (invpcid_works) {
166                         setidt(IPI_INVLTLB, pti ?
167                             IDTVEC(invltlb_invpcid_pti_pti) :
168                             IDTVEC(invltlb_invpcid_nopti), SDT_SYSIGT,
169                             SEL_KPL, 0);
170                         setidt(IPI_INVLPG, pti ? IDTVEC(invlpg_invpcid_pti) :
171                             IDTVEC(invlpg_invpcid), SDT_SYSIGT, SEL_KPL, 0);
172                         setidt(IPI_INVLRNG, pti ? IDTVEC(invlrng_invpcid_pti) :
173                             IDTVEC(invlrng_invpcid), SDT_SYSIGT, SEL_KPL, 0);
174                 } else {
175                         setidt(IPI_INVLTLB, pti ? IDTVEC(invltlb_pcid_pti) :
176                             IDTVEC(invltlb_pcid), SDT_SYSIGT, SEL_KPL, 0);
177                         setidt(IPI_INVLPG, pti ? IDTVEC(invlpg_pcid_pti) :
178                             IDTVEC(invlpg_pcid), SDT_SYSIGT, SEL_KPL, 0);
179                         setidt(IPI_INVLRNG, pti ? IDTVEC(invlrng_pcid_pti) :
180                             IDTVEC(invlrng_pcid), SDT_SYSIGT, SEL_KPL, 0);
181                 }
182         } else {
183                 setidt(IPI_INVLTLB, pti ? IDTVEC(invltlb_pti) : IDTVEC(invltlb),
184                     SDT_SYSIGT, SEL_KPL, 0);
185                 setidt(IPI_INVLPG, pti ? IDTVEC(invlpg_pti) : IDTVEC(invlpg),
186                     SDT_SYSIGT, SEL_KPL, 0);
187                 setidt(IPI_INVLRNG, pti ? IDTVEC(invlrng_pti) : IDTVEC(invlrng),
188                     SDT_SYSIGT, SEL_KPL, 0);
189         }
190
191         /* Install an inter-CPU IPI for cache invalidation. */
192         setidt(IPI_INVLCACHE, pti ? IDTVEC(invlcache_pti) : IDTVEC(invlcache),
193             SDT_SYSIGT, SEL_KPL, 0);
194
195         /* Install an inter-CPU IPI for all-CPU rendezvous */
196         setidt(IPI_RENDEZVOUS, pti ? IDTVEC(rendezvous_pti) :
197             IDTVEC(rendezvous), SDT_SYSIGT, SEL_KPL, 0);
198
199         /* Install generic inter-CPU IPI handler */
200         setidt(IPI_BITMAP_VECTOR, pti ? IDTVEC(ipi_intr_bitmap_handler_pti) :
201             IDTVEC(ipi_intr_bitmap_handler), SDT_SYSIGT, SEL_KPL, 0);
202
203         /* Install an inter-CPU IPI for CPU stop/restart */
204         setidt(IPI_STOP, pti ? IDTVEC(cpustop_pti) : IDTVEC(cpustop),
205             SDT_SYSIGT, SEL_KPL, 0);
206
207         /* Install an inter-CPU IPI for CPU suspend/resume */
208         setidt(IPI_SUSPEND, pti ? IDTVEC(cpususpend_pti) : IDTVEC(cpususpend),
209             SDT_SYSIGT, SEL_KPL, 0);
210
211         /* Set boot_cpu_id if needed. */
212         if (boot_cpu_id == -1) {
213                 boot_cpu_id = PCPU_GET(apic_id);
214                 cpu_info[boot_cpu_id].cpu_bsp = 1;
215         } else
216                 KASSERT(boot_cpu_id == PCPU_GET(apic_id),
217                     ("BSP's APIC ID doesn't match boot_cpu_id"));
218
219         /* Probe logical/physical core configuration. */
220         topo_probe();
221
222         assign_cpu_ids();
223
224         /* Start each Application Processor */
225         init_ops.start_all_aps();
226
227         set_interrupt_apic_ids();
228 }
229
230
231 /*
232  * AP CPU's call this to initialize themselves.
233  */
234 void
235 init_secondary(void)
236 {
237         struct pcpu *pc;
238         struct nmi_pcpu *np;
239         u_int64_t cr0;
240         int cpu, gsel_tss, x;
241         struct region_descriptor ap_gdt;
242
243         /* Set by the startup code for us to use */
244         cpu = bootAP;
245
246         /* Update microcode before doing anything else. */
247         ucode_load_ap(cpu);
248
249         /* Init tss */
250         common_tss[cpu] = common_tss[0];
251         common_tss[cpu].tss_iobase = sizeof(struct amd64tss) +
252             IOPERM_BITMAP_SIZE;
253         common_tss[cpu].tss_ist1 = (long)&doublefault_stack[PAGE_SIZE];
254
255         /* The NMI stack runs on IST2. */
256         np = ((struct nmi_pcpu *) &nmi_stack[PAGE_SIZE]) - 1;
257         common_tss[cpu].tss_ist2 = (long) np;
258
259         /* The MC# stack runs on IST3. */
260         np = ((struct nmi_pcpu *) &mce_stack[PAGE_SIZE]) - 1;
261         common_tss[cpu].tss_ist3 = (long) np;
262
263         /* The DB# stack runs on IST4. */
264         np = ((struct nmi_pcpu *) &dbg_stack[PAGE_SIZE]) - 1;
265         common_tss[cpu].tss_ist4 = (long) np;
266
267         /* Prepare private GDT */
268         gdt_segs[GPROC0_SEL].ssd_base = (long) &common_tss[cpu];
269         for (x = 0; x < NGDT; x++) {
270                 if (x != GPROC0_SEL && x != (GPROC0_SEL + 1) &&
271                     x != GUSERLDT_SEL && x != (GUSERLDT_SEL + 1))
272                         ssdtosd(&gdt_segs[x], &gdt[NGDT * cpu + x]);
273         }
274         ssdtosyssd(&gdt_segs[GPROC0_SEL],
275             (struct system_segment_descriptor *)&gdt[NGDT * cpu + GPROC0_SEL]);
276         ap_gdt.rd_limit = NGDT * sizeof(gdt[0]) - 1;
277         ap_gdt.rd_base =  (long) &gdt[NGDT * cpu];
278         lgdt(&ap_gdt);                  /* does magic intra-segment return */
279
280         /* Get per-cpu data */
281         pc = &__pcpu[cpu];
282
283         /* prime data page for it to use */
284         pcpu_init(pc, cpu, sizeof(struct pcpu));
285         dpcpu_init(dpcpu, cpu);
286         pc->pc_apic_id = cpu_apic_ids[cpu];
287         pc->pc_prvspace = pc;
288         pc->pc_curthread = 0;
289         pc->pc_tssp = &common_tss[cpu];
290         pc->pc_commontssp = &common_tss[cpu];
291         pc->pc_rsp0 = 0;
292         pc->pc_pti_rsp0 = (((vm_offset_t)&pc->pc_pti_stack +
293             PC_PTI_STACK_SZ * sizeof(uint64_t)) & ~0xful);
294         pc->pc_tss = (struct system_segment_descriptor *)&gdt[NGDT * cpu +
295             GPROC0_SEL];
296         pc->pc_fs32p = &gdt[NGDT * cpu + GUFS32_SEL];
297         pc->pc_gs32p = &gdt[NGDT * cpu + GUGS32_SEL];
298         pc->pc_ldt = (struct system_segment_descriptor *)&gdt[NGDT * cpu +
299             GUSERLDT_SEL];
300         /* See comment in pmap_bootstrap(). */
301         pc->pc_pcid_next = PMAP_PCID_KERN + 2;
302         pc->pc_pcid_gen = 1;
303         common_tss[cpu].tss_rsp0 = 0;
304
305         /* Save the per-cpu pointer for use by the NMI handler. */
306         np = ((struct nmi_pcpu *) &nmi_stack[PAGE_SIZE]) - 1;
307         np->np_pcpu = (register_t) pc;
308
309         /* Save the per-cpu pointer for use by the MC# handler. */
310         np = ((struct nmi_pcpu *) &mce_stack[PAGE_SIZE]) - 1;
311         np->np_pcpu = (register_t) pc;
312
313         /* Save the per-cpu pointer for use by the DB# handler. */
314         np = ((struct nmi_pcpu *) &dbg_stack[PAGE_SIZE]) - 1;
315         np->np_pcpu = (register_t) pc;
316
317         wrmsr(MSR_FSBASE, 0);           /* User value */
318         wrmsr(MSR_GSBASE, (u_int64_t)pc);
319         wrmsr(MSR_KGSBASE, (u_int64_t)pc);      /* XXX User value while we're in the kernel */
320         fix_cpuid();
321
322         lidt(&r_idt);
323
324         gsel_tss = GSEL(GPROC0_SEL, SEL_KPL);
325         ltr(gsel_tss);
326
327         /*
328          * Set to a known state:
329          * Set by mpboot.s: CR0_PG, CR0_PE
330          * Set by cpu_setregs: CR0_NE, CR0_MP, CR0_TS, CR0_WP, CR0_AM
331          */
332         cr0 = rcr0();
333         cr0 &= ~(CR0_CD | CR0_NW | CR0_EM);
334         load_cr0(cr0);
335
336         amd64_conf_fast_syscall();
337
338         /* signal our startup to the BSP. */
339         mp_naps++;
340
341         /* Spin until the BSP releases the AP's. */
342         while (atomic_load_acq_int(&aps_ready) == 0)
343                 ia32_pause();
344
345         init_secondary_tail();
346 }
347
348 /*******************************************************************
349  * local functions and data
350  */
351
352 /*
353  * start each AP in our list
354  */
355 int
356 native_start_all_aps(void)
357 {
358         u_int64_t *pt4, *pt3, *pt2;
359         u_int32_t mpbioswarmvec;
360         int apic_id, cpu, i;
361         u_char mpbiosreason;
362
363         mtx_init(&ap_boot_mtx, "ap boot", NULL, MTX_SPIN);
364
365         /* copy the AP 1st level boot code */
366         bcopy(mptramp_start, (void *)PHYS_TO_DMAP(boot_address), bootMP_size);
367
368         /* Locate the page tables, they'll be below the trampoline */
369         pt4 = (uint64_t *)PHYS_TO_DMAP(mptramp_pagetables);
370         pt3 = pt4 + (PAGE_SIZE) / sizeof(u_int64_t);
371         pt2 = pt3 + (PAGE_SIZE) / sizeof(u_int64_t);
372
373         /* Create the initial 1GB replicated page tables */
374         for (i = 0; i < 512; i++) {
375                 /* Each slot of the level 4 pages points to the same level 3 page */
376                 pt4[i] = (u_int64_t)(uintptr_t)(mptramp_pagetables + PAGE_SIZE);
377                 pt4[i] |= PG_V | PG_RW | PG_U;
378
379                 /* Each slot of the level 3 pages points to the same level 2 page */
380                 pt3[i] = (u_int64_t)(uintptr_t)(mptramp_pagetables + (2 * PAGE_SIZE));
381                 pt3[i] |= PG_V | PG_RW | PG_U;
382
383                 /* The level 2 page slots are mapped with 2MB pages for 1GB. */
384                 pt2[i] = i * (2 * 1024 * 1024);
385                 pt2[i] |= PG_V | PG_RW | PG_PS | PG_U;
386         }
387
388         /* save the current value of the warm-start vector */
389         mpbioswarmvec = *((u_int32_t *) WARMBOOT_OFF);
390         outb(CMOS_REG, BIOS_RESET);
391         mpbiosreason = inb(CMOS_DATA);
392
393         /* setup a vector to our boot code */
394         *((volatile u_short *) WARMBOOT_OFF) = WARMBOOT_TARGET;
395         *((volatile u_short *) WARMBOOT_SEG) = (boot_address >> 4);
396         outb(CMOS_REG, BIOS_RESET);
397         outb(CMOS_DATA, BIOS_WARM);     /* 'warm-start' */
398
399         /* start each AP */
400         for (cpu = 1; cpu < mp_ncpus; cpu++) {
401                 apic_id = cpu_apic_ids[cpu];
402
403                 /* allocate and set up an idle stack data page */
404                 bootstacks[cpu] = (void *)kmem_malloc(kstack_pages * PAGE_SIZE,
405                     M_WAITOK | M_ZERO);
406                 doublefault_stack = (char *)kmem_malloc(PAGE_SIZE, M_WAITOK |
407                     M_ZERO);
408                 mce_stack = (char *)kmem_malloc(PAGE_SIZE, M_WAITOK | M_ZERO);
409                 nmi_stack = (char *)kmem_malloc(PAGE_SIZE, M_WAITOK | M_ZERO);
410                 dbg_stack = (char *)kmem_malloc(PAGE_SIZE, M_WAITOK | M_ZERO);
411                 dpcpu = (void *)kmem_malloc(DPCPU_SIZE, M_WAITOK | M_ZERO);
412
413                 bootSTK = (char *)bootstacks[cpu] + kstack_pages * PAGE_SIZE - 8;
414                 bootAP = cpu;
415
416                 /* attempt to start the Application Processor */
417                 if (!start_ap(apic_id)) {
418                         /* restore the warmstart vector */
419                         *(u_int32_t *) WARMBOOT_OFF = mpbioswarmvec;
420                         panic("AP #%d (PHY# %d) failed!", cpu, apic_id);
421                 }
422
423                 CPU_SET(cpu, &all_cpus);        /* record AP in CPU map */
424         }
425
426         /* restore the warmstart vector */
427         *(u_int32_t *) WARMBOOT_OFF = mpbioswarmvec;
428
429         outb(CMOS_REG, BIOS_RESET);
430         outb(CMOS_DATA, mpbiosreason);
431
432         /* number of APs actually started */
433         return mp_naps;
434 }
435
436
437 /*
438  * This function starts the AP (application processor) identified
439  * by the APIC ID 'physicalCpu'.  It does quite a "song and dance"
440  * to accomplish this.  This is necessary because of the nuances
441  * of the different hardware we might encounter.  It isn't pretty,
442  * but it seems to work.
443  */
444 static int
445 start_ap(int apic_id)
446 {
447         int vector, ms;
448         int cpus;
449
450         /* calculate the vector */
451         vector = (boot_address >> 12) & 0xff;
452
453         /* used as a watchpoint to signal AP startup */
454         cpus = mp_naps;
455
456         ipi_startup(apic_id, vector);
457
458         /* Wait up to 5 seconds for it to start. */
459         for (ms = 0; ms < 5000; ms++) {
460                 if (mp_naps > cpus)
461                         return 1;       /* return SUCCESS */
462                 DELAY(1000);
463         }
464         return 0;               /* return FAILURE */
465 }
466
467 void
468 invltlb_invpcid_handler(void)
469 {
470         struct invpcid_descr d;
471         uint32_t generation;
472
473 #ifdef COUNT_XINVLTLB_HITS
474         xhits_gbl[PCPU_GET(cpuid)]++;
475 #endif /* COUNT_XINVLTLB_HITS */
476 #ifdef COUNT_IPIS
477         (*ipi_invltlb_counts[PCPU_GET(cpuid)])++;
478 #endif /* COUNT_IPIS */
479
480         generation = smp_tlb_generation;
481         d.pcid = smp_tlb_pmap->pm_pcids[PCPU_GET(cpuid)].pm_pcid;
482         d.pad = 0;
483         d.addr = 0;
484         invpcid(&d, smp_tlb_pmap == kernel_pmap ? INVPCID_CTXGLOB :
485             INVPCID_CTX);
486         PCPU_SET(smp_tlb_done, generation);
487 }
488
489 void
490 invltlb_invpcid_pti_handler(void)
491 {
492         struct invpcid_descr d;
493         uint32_t generation;
494
495 #ifdef COUNT_XINVLTLB_HITS
496         xhits_gbl[PCPU_GET(cpuid)]++;
497 #endif /* COUNT_XINVLTLB_HITS */
498 #ifdef COUNT_IPIS
499         (*ipi_invltlb_counts[PCPU_GET(cpuid)])++;
500 #endif /* COUNT_IPIS */
501
502         generation = smp_tlb_generation;
503         d.pcid = smp_tlb_pmap->pm_pcids[PCPU_GET(cpuid)].pm_pcid;
504         d.pad = 0;
505         d.addr = 0;
506         if (smp_tlb_pmap == kernel_pmap) {
507                 /*
508                  * This invalidation actually needs to clear kernel
509                  * mappings from the TLB in the current pmap, but
510                  * since we were asked for the flush in the kernel
511                  * pmap, achieve it by performing global flush.
512                  */
513                 invpcid(&d, INVPCID_CTXGLOB);
514         } else {
515                 invpcid(&d, INVPCID_CTX);
516                 d.pcid |= PMAP_PCID_USER_PT;
517                 invpcid(&d, INVPCID_CTX);
518         }
519         PCPU_SET(smp_tlb_done, generation);
520 }
521
522 void
523 invltlb_pcid_handler(void)
524 {
525         uint64_t kcr3, ucr3;
526         uint32_t generation, pcid;
527   
528 #ifdef COUNT_XINVLTLB_HITS
529         xhits_gbl[PCPU_GET(cpuid)]++;
530 #endif /* COUNT_XINVLTLB_HITS */
531 #ifdef COUNT_IPIS
532         (*ipi_invltlb_counts[PCPU_GET(cpuid)])++;
533 #endif /* COUNT_IPIS */
534
535         generation = smp_tlb_generation;        /* Overlap with serialization */
536         if (smp_tlb_pmap == kernel_pmap) {
537                 invltlb_glob();
538         } else {
539                 /*
540                  * The current pmap might not be equal to
541                  * smp_tlb_pmap.  The clearing of the pm_gen in
542                  * pmap_invalidate_all() takes care of TLB
543                  * invalidation when switching to the pmap on this
544                  * CPU.
545                  */
546                 if (PCPU_GET(curpmap) == smp_tlb_pmap) {
547                         pcid = smp_tlb_pmap->pm_pcids[PCPU_GET(cpuid)].pm_pcid;
548                         kcr3 = smp_tlb_pmap->pm_cr3 | pcid;
549                         ucr3 = smp_tlb_pmap->pm_ucr3;
550                         if (ucr3 != PMAP_NO_CR3) {
551                                 ucr3 |= PMAP_PCID_USER_PT | pcid;
552                                 pmap_pti_pcid_invalidate(ucr3, kcr3);
553                         } else
554                                 load_cr3(kcr3);
555                 }
556         }
557         PCPU_SET(smp_tlb_done, generation);
558 }
559
560 void
561 invlpg_invpcid_handler(void)
562 {
563         struct invpcid_descr d;
564         uint32_t generation;
565
566 #ifdef COUNT_XINVLTLB_HITS
567         xhits_pg[PCPU_GET(cpuid)]++;
568 #endif /* COUNT_XINVLTLB_HITS */
569 #ifdef COUNT_IPIS
570         (*ipi_invlpg_counts[PCPU_GET(cpuid)])++;
571 #endif /* COUNT_IPIS */
572
573         generation = smp_tlb_generation;        /* Overlap with serialization */
574         invlpg(smp_tlb_addr1);
575         if (smp_tlb_pmap->pm_ucr3 != PMAP_NO_CR3) {
576                 d.pcid = smp_tlb_pmap->pm_pcids[PCPU_GET(cpuid)].pm_pcid |
577                     PMAP_PCID_USER_PT;
578                 d.pad = 0;
579                 d.addr = smp_tlb_addr1;
580                 invpcid(&d, INVPCID_ADDR);
581         }
582         PCPU_SET(smp_tlb_done, generation);
583 }
584
585 void
586 invlpg_pcid_handler(void)
587 {
588         uint64_t kcr3, ucr3;
589         uint32_t generation;
590         uint32_t pcid;
591
592 #ifdef COUNT_XINVLTLB_HITS
593         xhits_pg[PCPU_GET(cpuid)]++;
594 #endif /* COUNT_XINVLTLB_HITS */
595 #ifdef COUNT_IPIS
596         (*ipi_invlpg_counts[PCPU_GET(cpuid)])++;
597 #endif /* COUNT_IPIS */
598
599         generation = smp_tlb_generation;        /* Overlap with serialization */
600         invlpg(smp_tlb_addr1);
601         if (smp_tlb_pmap == PCPU_GET(curpmap) &&
602             (ucr3 = smp_tlb_pmap->pm_ucr3) != PMAP_NO_CR3) {
603                 pcid = smp_tlb_pmap->pm_pcids[PCPU_GET(cpuid)].pm_pcid;
604                 kcr3 = smp_tlb_pmap->pm_cr3 | pcid | CR3_PCID_SAVE;
605                 ucr3 |= pcid | PMAP_PCID_USER_PT | CR3_PCID_SAVE;
606                 pmap_pti_pcid_invlpg(ucr3, kcr3, smp_tlb_addr1);
607         }
608         PCPU_SET(smp_tlb_done, generation);
609 }
610
611 void
612 invlrng_invpcid_handler(void)
613 {
614         struct invpcid_descr d;
615         vm_offset_t addr, addr2;
616         uint32_t generation;
617
618 #ifdef COUNT_XINVLTLB_HITS
619         xhits_rng[PCPU_GET(cpuid)]++;
620 #endif /* COUNT_XINVLTLB_HITS */
621 #ifdef COUNT_IPIS
622         (*ipi_invlrng_counts[PCPU_GET(cpuid)])++;
623 #endif /* COUNT_IPIS */
624
625         addr = smp_tlb_addr1;
626         addr2 = smp_tlb_addr2;
627         generation = smp_tlb_generation;        /* Overlap with serialization */
628         do {
629                 invlpg(addr);
630                 addr += PAGE_SIZE;
631         } while (addr < addr2);
632         if (smp_tlb_pmap->pm_ucr3 != PMAP_NO_CR3) {
633                 d.pcid = smp_tlb_pmap->pm_pcids[PCPU_GET(cpuid)].pm_pcid |
634                     PMAP_PCID_USER_PT;
635                 d.pad = 0;
636                 d.addr = smp_tlb_addr1;
637                 do {
638                         invpcid(&d, INVPCID_ADDR);
639                         d.addr += PAGE_SIZE;
640                 } while (d.addr < addr2);
641         }
642         PCPU_SET(smp_tlb_done, generation);
643 }
644
645 void
646 invlrng_pcid_handler(void)
647 {
648         vm_offset_t addr, addr2;
649         uint64_t kcr3, ucr3;
650         uint32_t generation;
651         uint32_t pcid;
652
653 #ifdef COUNT_XINVLTLB_HITS
654         xhits_rng[PCPU_GET(cpuid)]++;
655 #endif /* COUNT_XINVLTLB_HITS */
656 #ifdef COUNT_IPIS
657         (*ipi_invlrng_counts[PCPU_GET(cpuid)])++;
658 #endif /* COUNT_IPIS */
659
660         addr = smp_tlb_addr1;
661         addr2 = smp_tlb_addr2;
662         generation = smp_tlb_generation;        /* Overlap with serialization */
663         do {
664                 invlpg(addr);
665                 addr += PAGE_SIZE;
666         } while (addr < addr2);
667         if (smp_tlb_pmap == PCPU_GET(curpmap) &&
668             (ucr3 = smp_tlb_pmap->pm_ucr3) != PMAP_NO_CR3) {
669                 pcid = smp_tlb_pmap->pm_pcids[PCPU_GET(cpuid)].pm_pcid;
670                 kcr3 = smp_tlb_pmap->pm_cr3 | pcid | CR3_PCID_SAVE;
671                 ucr3 |= pcid | PMAP_PCID_USER_PT | CR3_PCID_SAVE;
672                 pmap_pti_pcid_invlrng(ucr3, kcr3, smp_tlb_addr1, addr2);
673         }
674         PCPU_SET(smp_tlb_done, generation);
675 }