]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/amd64/amd64/mp_machdep.c
Move instantiation of msgbufp from 9 MD files to subr_prf.c.
[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 *nmi_stack;
91
92 extern inthand_t IDTVEC(fast_syscall), IDTVEC(fast_syscall32);
93
94 /*
95  * Local data and functions.
96  */
97
98 static int      start_ap(int apic_id);
99
100 static u_int    bootMP_size;
101 static u_int    boot_address;
102
103 /*
104  * Calculate usable address in base memory for AP trampoline code.
105  */
106 u_int
107 mp_bootaddress(u_int basemem)
108 {
109
110         bootMP_size = mptramp_end - mptramp_start;
111         boot_address = trunc_page(basemem * 1024); /* round down to 4k boundary */
112         if (((basemem * 1024) - boot_address) < bootMP_size)
113                 boot_address -= PAGE_SIZE;      /* not enough, lower by 4k */
114         /* 3 levels of page table pages */
115         mptramp_pagetables = boot_address - (PAGE_SIZE * 3);
116
117         return mptramp_pagetables;
118 }
119
120 /*
121  * Initialize the IPI handlers and start up the AP's.
122  */
123 void
124 cpu_mp_start(void)
125 {
126         int i;
127
128         /* Initialize the logical ID to APIC ID table. */
129         for (i = 0; i < MAXCPU; i++) {
130                 cpu_apic_ids[i] = -1;
131                 cpu_ipi_pending[i] = 0;
132         }
133
134         /* Install an inter-CPU IPI for TLB invalidation */
135         if (pmap_pcid_enabled) {
136                 if (invpcid_works) {
137                         setidt(IPI_INVLTLB, IDTVEC(invltlb_invpcid),
138                             SDT_SYSIGT, SEL_KPL, 0);
139                 } else {
140                         setidt(IPI_INVLTLB, IDTVEC(invltlb_pcid), SDT_SYSIGT,
141                             SEL_KPL, 0);
142                 }
143         } else {
144                 setidt(IPI_INVLTLB, IDTVEC(invltlb), SDT_SYSIGT, SEL_KPL, 0);
145         }
146         setidt(IPI_INVLPG, IDTVEC(invlpg), SDT_SYSIGT, SEL_KPL, 0);
147         setidt(IPI_INVLRNG, IDTVEC(invlrng), SDT_SYSIGT, SEL_KPL, 0);
148
149         /* Install an inter-CPU IPI for cache invalidation. */
150         setidt(IPI_INVLCACHE, IDTVEC(invlcache), SDT_SYSIGT, SEL_KPL, 0);
151
152         /* Install an inter-CPU IPI for all-CPU rendezvous */
153         setidt(IPI_RENDEZVOUS, IDTVEC(rendezvous), SDT_SYSIGT, SEL_KPL, 0);
154
155         /* Install generic inter-CPU IPI handler */
156         setidt(IPI_BITMAP_VECTOR, IDTVEC(ipi_intr_bitmap_handler),
157                SDT_SYSIGT, SEL_KPL, 0);
158
159         /* Install an inter-CPU IPI for CPU stop/restart */
160         setidt(IPI_STOP, IDTVEC(cpustop), SDT_SYSIGT, SEL_KPL, 0);
161
162         /* Install an inter-CPU IPI for CPU suspend/resume */
163         setidt(IPI_SUSPEND, IDTVEC(cpususpend), SDT_SYSIGT, SEL_KPL, 0);
164
165         /* Set boot_cpu_id if needed. */
166         if (boot_cpu_id == -1) {
167                 boot_cpu_id = PCPU_GET(apic_id);
168                 cpu_info[boot_cpu_id].cpu_bsp = 1;
169         } else
170                 KASSERT(boot_cpu_id == PCPU_GET(apic_id),
171                     ("BSP's APIC ID doesn't match boot_cpu_id"));
172
173         /* Probe logical/physical core configuration. */
174         topo_probe();
175
176         assign_cpu_ids();
177
178         /* Start each Application Processor */
179         init_ops.start_all_aps();
180
181         set_interrupt_apic_ids();
182 }
183
184
185 /*
186  * AP CPU's call this to initialize themselves.
187  */
188 void
189 init_secondary(void)
190 {
191         struct pcpu *pc;
192         struct nmi_pcpu *np;
193         u_int64_t msr, cr0;
194         int cpu, gsel_tss, x;
195         struct region_descriptor ap_gdt;
196
197         /* Set by the startup code for us to use */
198         cpu = bootAP;
199
200         /* Init tss */
201         common_tss[cpu] = common_tss[0];
202         common_tss[cpu].tss_rsp0 = 0;   /* not used until after switch */
203         common_tss[cpu].tss_iobase = sizeof(struct amd64tss) +
204             IOPERM_BITMAP_SIZE;
205         common_tss[cpu].tss_ist1 = (long)&doublefault_stack[PAGE_SIZE];
206
207         /* The NMI stack runs on IST2. */
208         np = ((struct nmi_pcpu *) &nmi_stack[PAGE_SIZE]) - 1;
209         common_tss[cpu].tss_ist2 = (long) np;
210
211         /* Prepare private GDT */
212         gdt_segs[GPROC0_SEL].ssd_base = (long) &common_tss[cpu];
213         for (x = 0; x < NGDT; x++) {
214                 if (x != GPROC0_SEL && x != (GPROC0_SEL + 1) &&
215                     x != GUSERLDT_SEL && x != (GUSERLDT_SEL + 1))
216                         ssdtosd(&gdt_segs[x], &gdt[NGDT * cpu + x]);
217         }
218         ssdtosyssd(&gdt_segs[GPROC0_SEL],
219             (struct system_segment_descriptor *)&gdt[NGDT * cpu + GPROC0_SEL]);
220         ap_gdt.rd_limit = NGDT * sizeof(gdt[0]) - 1;
221         ap_gdt.rd_base =  (long) &gdt[NGDT * cpu];
222         lgdt(&ap_gdt);                  /* does magic intra-segment return */
223
224         /* Get per-cpu data */
225         pc = &__pcpu[cpu];
226
227         /* prime data page for it to use */
228         pcpu_init(pc, cpu, sizeof(struct pcpu));
229         dpcpu_init(dpcpu, cpu);
230         pc->pc_apic_id = cpu_apic_ids[cpu];
231         pc->pc_prvspace = pc;
232         pc->pc_curthread = 0;
233         pc->pc_tssp = &common_tss[cpu];
234         pc->pc_commontssp = &common_tss[cpu];
235         pc->pc_rsp0 = 0;
236         pc->pc_tss = (struct system_segment_descriptor *)&gdt[NGDT * cpu +
237             GPROC0_SEL];
238         pc->pc_fs32p = &gdt[NGDT * cpu + GUFS32_SEL];
239         pc->pc_gs32p = &gdt[NGDT * cpu + GUGS32_SEL];
240         pc->pc_ldt = (struct system_segment_descriptor *)&gdt[NGDT * cpu +
241             GUSERLDT_SEL];
242         pc->pc_curpmap = kernel_pmap;
243         pc->pc_pcid_gen = 1;
244         pc->pc_pcid_next = PMAP_PCID_KERN + 1;
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         fix_cpuid();
253
254         lidt(&r_idt);
255
256         gsel_tss = GSEL(GPROC0_SEL, SEL_KPL);
257         ltr(gsel_tss);
258
259         /*
260          * Set to a known state:
261          * Set by mpboot.s: CR0_PG, CR0_PE
262          * Set by cpu_setregs: CR0_NE, CR0_MP, CR0_TS, CR0_WP, CR0_AM
263          */
264         cr0 = rcr0();
265         cr0 &= ~(CR0_CD | CR0_NW | CR0_EM);
266         load_cr0(cr0);
267
268         /* Set up the fast syscall stuff */
269         msr = rdmsr(MSR_EFER) | EFER_SCE;
270         wrmsr(MSR_EFER, msr);
271         wrmsr(MSR_LSTAR, (u_int64_t)IDTVEC(fast_syscall));
272         wrmsr(MSR_CSTAR, (u_int64_t)IDTVEC(fast_syscall32));
273         msr = ((u_int64_t)GSEL(GCODE_SEL, SEL_KPL) << 32) |
274               ((u_int64_t)GSEL(GUCODE32_SEL, SEL_UPL) << 48);
275         wrmsr(MSR_STAR, msr);
276         wrmsr(MSR_SF_MASK, PSL_NT|PSL_T|PSL_I|PSL_C|PSL_D);
277
278         /* signal our startup to the BSP. */
279         mp_naps++;
280
281         /* Spin until the BSP releases the AP's. */
282         while (atomic_load_acq_int(&aps_ready) == 0)
283                 ia32_pause();
284
285         init_secondary_tail();
286 }
287
288 /*******************************************************************
289  * local functions and data
290  */
291
292 /*
293  * start each AP in our list
294  */
295 int
296 native_start_all_aps(void)
297 {
298         vm_offset_t va = boot_address + KERNBASE;
299         u_int64_t *pt4, *pt3, *pt2;
300         u_int32_t mpbioswarmvec;
301         int apic_id, cpu, i;
302         u_char mpbiosreason;
303
304         mtx_init(&ap_boot_mtx, "ap boot", NULL, MTX_SPIN);
305
306         /* install the AP 1st level boot code */
307         pmap_kenter(va, boot_address);
308         pmap_invalidate_page(kernel_pmap, va);
309         bcopy(mptramp_start, (void *)va, bootMP_size);
310
311         /* Locate the page tables, they'll be below the trampoline */
312         pt4 = (u_int64_t *)(uintptr_t)(mptramp_pagetables + KERNBASE);
313         pt3 = pt4 + (PAGE_SIZE) / sizeof(u_int64_t);
314         pt2 = pt3 + (PAGE_SIZE) / sizeof(u_int64_t);
315
316         /* Create the initial 1GB replicated page tables */
317         for (i = 0; i < 512; i++) {
318                 /* Each slot of the level 4 pages points to the same level 3 page */
319                 pt4[i] = (u_int64_t)(uintptr_t)(mptramp_pagetables + PAGE_SIZE);
320                 pt4[i] |= PG_V | PG_RW | PG_U;
321
322                 /* Each slot of the level 3 pages points to the same level 2 page */
323                 pt3[i] = (u_int64_t)(uintptr_t)(mptramp_pagetables + (2 * PAGE_SIZE));
324                 pt3[i] |= PG_V | PG_RW | PG_U;
325
326                 /* The level 2 page slots are mapped with 2MB pages for 1GB. */
327                 pt2[i] = i * (2 * 1024 * 1024);
328                 pt2[i] |= PG_V | PG_RW | PG_PS | PG_U;
329         }
330
331         /* save the current value of the warm-start vector */
332         mpbioswarmvec = *((u_int32_t *) WARMBOOT_OFF);
333         outb(CMOS_REG, BIOS_RESET);
334         mpbiosreason = inb(CMOS_DATA);
335
336         /* setup a vector to our boot code */
337         *((volatile u_short *) WARMBOOT_OFF) = WARMBOOT_TARGET;
338         *((volatile u_short *) WARMBOOT_SEG) = (boot_address >> 4);
339         outb(CMOS_REG, BIOS_RESET);
340         outb(CMOS_DATA, BIOS_WARM);     /* 'warm-start' */
341
342         /* start each AP */
343         for (cpu = 1; cpu < mp_ncpus; cpu++) {
344                 apic_id = cpu_apic_ids[cpu];
345
346                 /* allocate and set up an idle stack data page */
347                 bootstacks[cpu] = (void *)kmem_malloc(kernel_arena,
348                     kstack_pages * PAGE_SIZE, M_WAITOK | M_ZERO);
349                 doublefault_stack = (char *)kmem_malloc(kernel_arena,
350                     PAGE_SIZE, M_WAITOK | M_ZERO);
351                 nmi_stack = (char *)kmem_malloc(kernel_arena, PAGE_SIZE,
352                     M_WAITOK | M_ZERO);
353                 dpcpu = (void *)kmem_malloc(kernel_arena, DPCPU_SIZE,
354                     M_WAITOK | M_ZERO);
355
356                 bootSTK = (char *)bootstacks[cpu] + kstack_pages * PAGE_SIZE - 8;
357                 bootAP = cpu;
358
359                 /* attempt to start the Application Processor */
360                 if (!start_ap(apic_id)) {
361                         /* restore the warmstart vector */
362                         *(u_int32_t *) WARMBOOT_OFF = mpbioswarmvec;
363                         panic("AP #%d (PHY# %d) failed!", cpu, apic_id);
364                 }
365
366                 CPU_SET(cpu, &all_cpus);        /* record AP in CPU map */
367         }
368
369         /* restore the warmstart vector */
370         *(u_int32_t *) WARMBOOT_OFF = mpbioswarmvec;
371
372         outb(CMOS_REG, BIOS_RESET);
373         outb(CMOS_DATA, mpbiosreason);
374
375         /* number of APs actually started */
376         return mp_naps;
377 }
378
379
380 /*
381  * This function starts the AP (application processor) identified
382  * by the APIC ID 'physicalCpu'.  It does quite a "song and dance"
383  * to accomplish this.  This is necessary because of the nuances
384  * of the different hardware we might encounter.  It isn't pretty,
385  * but it seems to work.
386  */
387 static int
388 start_ap(int apic_id)
389 {
390         int vector, ms;
391         int cpus;
392
393         /* calculate the vector */
394         vector = (boot_address >> 12) & 0xff;
395
396         /* used as a watchpoint to signal AP startup */
397         cpus = mp_naps;
398
399         ipi_startup(apic_id, vector);
400
401         /* Wait up to 5 seconds for it to start. */
402         for (ms = 0; ms < 5000; ms++) {
403                 if (mp_naps > cpus)
404                         return 1;       /* return SUCCESS */
405                 DELAY(1000);
406         }
407         return 0;               /* return FAILURE */
408 }
409
410 void
411 invltlb_invpcid_handler(void)
412 {
413         struct invpcid_descr d;
414         uint32_t generation;
415
416 #ifdef COUNT_XINVLTLB_HITS
417         xhits_gbl[PCPU_GET(cpuid)]++;
418 #endif /* COUNT_XINVLTLB_HITS */
419 #ifdef COUNT_IPIS
420         (*ipi_invltlb_counts[PCPU_GET(cpuid)])++;
421 #endif /* COUNT_IPIS */
422
423         generation = smp_tlb_generation;
424         d.pcid = smp_tlb_pmap->pm_pcids[PCPU_GET(cpuid)].pm_pcid;
425         d.pad = 0;
426         d.addr = 0;
427         invpcid(&d, smp_tlb_pmap == kernel_pmap ? INVPCID_CTXGLOB :
428             INVPCID_CTX);
429         PCPU_SET(smp_tlb_done, generation);
430 }
431
432 void
433 invltlb_pcid_handler(void)
434 {
435         uint32_t generation;
436   
437 #ifdef COUNT_XINVLTLB_HITS
438         xhits_gbl[PCPU_GET(cpuid)]++;
439 #endif /* COUNT_XINVLTLB_HITS */
440 #ifdef COUNT_IPIS
441         (*ipi_invltlb_counts[PCPU_GET(cpuid)])++;
442 #endif /* COUNT_IPIS */
443
444         generation = smp_tlb_generation;        /* Overlap with serialization */
445         if (smp_tlb_pmap == kernel_pmap) {
446                 invltlb_glob();
447         } else {
448                 /*
449                  * The current pmap might not be equal to
450                  * smp_tlb_pmap.  The clearing of the pm_gen in
451                  * pmap_invalidate_all() takes care of TLB
452                  * invalidation when switching to the pmap on this
453                  * CPU.
454                  */
455                 if (PCPU_GET(curpmap) == smp_tlb_pmap) {
456                         load_cr3(smp_tlb_pmap->pm_cr3 |
457                             smp_tlb_pmap->pm_pcids[PCPU_GET(cpuid)].pm_pcid);
458                 }
459         }
460         PCPU_SET(smp_tlb_done, generation);
461 }