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