2 * Copyright (c) 2007 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * $DragonFly: src/sys/platform/vkernel/i386/mp.c,v 1.1 2007/06/18 18:57:12 josepht Exp $
38 #include <sys/interrupt.h>
40 #include <sys/types.h>
42 #include <vm/vm_extern.h>
43 #include <vm/vm_kern.h>
44 #include <vm/vm_object.h>
45 #include <vm/vm_page.h>
47 #include <machine/cpufunc.h>
48 #include <machine/globaldata.h>
49 #include <machine/md_var.h>
50 #include <machine/pmap.h>
51 #include <machine/smp.h>
52 #include <machine/tls.h>
57 extern pt_entry_t
*KPTphys
;
59 volatile u_int stopped_cpus
;
60 cpumask_t smp_active_mask
= 1; /* which cpus are ready for IPIs etc? */
61 static int mp_capable
;
62 static int boot_address
;
63 static cpumask_t smp_startup_mask
= 1; /* which cpus have been started */
64 int mp_naps
; /* # of Applications processors */
68 /* AP uses this during bootstrap. Do not staticize. */
73 /* XXX these need to go into the appropriate header file */
74 static int start_all_aps(u_int
);
75 void init_secondary(void);
76 void *start_ap(void *);
85 int ap_id
= *(int *)arg
;
86 kprintf("mycpu->gd_cpuid: %d\n", mycpu
->gd_cpuid
);
90 kprintf("mycpu->gd_cpuid: %d\n", mycpu
->gd_cpuid
);
93 kprintf("start_ap %d\n", ap_id
);
97 /* storage for AP thread IDs */
98 pthread_t ap_tids
[MAXCPU
];
105 /* XXX testing 2 cpus */
110 /* ncpus2 -- ncpus rounded down to the nearest power of 2 */
111 for (shift
= 0; (1 << shift
) <= ncpus
; ++shift
)
114 ncpus2_shift
= shift
;
116 ncpus2_mask
= ncpus2
- 1;
118 /* ncpus_fit -- ncpus rounded up to the nearest power of 2 */
119 if ((1 << shift
) < ncpus
)
121 ncpus_fit
= 1 << shift
;
122 ncpus_fit_mask
= ncpus_fit
- 1;
124 /* build our map of 'other' CPUs */
125 mycpu
->gd_other_cpus
= smp_startup_mask
& ~(1 << mycpu
->gd_cpuid
);
126 mycpu
->gd_ipiq
= (void *)kmem_alloc(&kernel_map
, sizeof(lwkt_ipiq
) * ncpus
);
127 bzero(mycpu
->gd_ipiq
, sizeof(lwkt_ipiq
) * ncpus
);
129 start_all_aps(boot_address
);
138 kprintf("DragonFly/MP: Multiprocessor\n");
139 kprintf(" cpu0 (BSP)\n");
141 for (x
= 1; x
<= mp_naps
; ++x
)
142 kprintf(" cpu%d (AP)\n", x
);
146 forward_fastint_remote(void *arg
)
148 panic("XXX forward_fastint_remote()");
152 cpu_send_ipiq(int dcpu
)
154 kprintf("cpu_send_ipiq(%d)\n", dcpu
);
155 /* panic("XXX cpu_send_ipiq()"); */
162 panic("XXX smp_invltlb()");
169 panic("XXX stop_cpus()");
173 restart_cpus(u_int map
)
175 panic("XXX restart_cpus()");
181 panic("XXX ap_init()");
188 struct mdglobaldata
*md
;
189 struct privatespace
*ps
;
191 ps
= &CPU_prvspace
[myid
];
194 gdt_segs
[GPRIV_SEL
].ssd_base
= (int)ps
;
195 gdt_segs
[GPROC0_SEL
].ssd_base
=
196 (int) &ps
->mdglobaldata
.gd_common_tss
;
198 ps
->mdglobaldata
.mi
.gd_prvspace
= ps
;
201 * Setup the %gs for cpu #n. The mycpu macro works after this
204 tls_set_fs(&CPU_prvspace
[myid
], sizeof(struct privatespace
));
207 for (x
= 0; x
< NGDT
; x
++) {
208 ssdtosd(&gdt_segs
[x
], &gdt
[myid
* NGDT
+ x
].sd
);
211 r_gdt
.rd_limit
= NGDT
* sizeof(gdt
[0]) - 1;
212 r_gdt
.rd_base
= (int) &gdt
[myid
* NGDT
];
213 lgdt(&r_gdt
); /* does magic intra-segment return */
218 mdcpu
->gd_currentldt
= _default_ldt
;
220 gsel_tss
= GSEL(GPROC0_SEL
, SEL_KPL
);
221 gdt
[myid
* NGDT
+ GPROC0_SEL
].sd
.sd_type
= SDT_SYS386TSS
;
224 md
= mdcpu
; /* loaded through %fs:0 (mdglobaldata.mi.gd_prvspace)*/
226 md
->gd_common_tss
.tss_esp0
= 0; /* not used until after switch */
227 md
->gd_common_tss
.tss_ss0
= GSEL(GDATA_SEL
, SEL_KPL
);
228 md
->gd_common_tss
.tss_ioopt
= (sizeof md
->gd_common_tss
) << 16;
230 md
->gd_tss_gdt
= &gdt
[myid
* NGDT
+ GPROC0_SEL
].sd
;
231 md
->gd_common_tssd
= *md
->gd_tss_gdt
;
236 * Set to a known state:
237 * Set by mpboot.s: CR0_PG, CR0_PE
238 * Set by cpu_setregs: CR0_NE, CR0_MP, CR0_TS, CR0_WP, CR0_AM
242 cr0
&= ~(CR0_CD
| CR0_NW
| CR0_EM
);
244 pmap_set_opt(); /* PSE/4MB pages, etc */
246 /* set up CPU registers and state */
249 /* set up FPU state on the AP */
250 npxinit(__INITIAL_NPXCW__
);
252 /* set up SSE registers */
259 start_all_aps(u_int boot_addr
)
262 struct mdglobaldata
*gd
;
263 struct privatespace
*ps
;
267 struct lwp_params params
;
271 /* store the mappings so we can populate gd_CMAP[0-2] and gd_PMAP3 */
274 for (x
= 1; x
<= mp_naps
; x
++)
278 /* Allocate space for the CPU's private space. */
279 va
= (vm_offset_t
)&CPU_prvspace
[x
];
280 for (i
= 0; i
< sizeof(struct mdglobaldata
); i
+= PAGE_SIZE
) {
281 va
=(vm_offset_t
)&CPU_prvspace
[x
].mdglobaldata
+ i
;
282 m
= vm_page_alloc(&kernel_object
, va
, VM_ALLOC_SYSTEM
);
283 pmap_kenter_quick(va
, m
->phys_addr
);
285 SMPpt2
[count
] = pmap_kpte(va
);
288 for (i
= 0; i
< sizeof(CPU_prvspace
[0].idlestack
); i
+= PAGE_SIZE
) {
289 va
=(vm_offset_t
)&CPU_prvspace
[x
].idlestack
+ i
;
290 m
= vm_page_alloc(&kernel_object
, va
, VM_ALLOC_SYSTEM
);
291 pmap_kenter_quick(va
, m
->phys_addr
);
295 /* first page of AP's private space */
296 pg
= x
* i386_btop(sizeof(struct privatespace
));
298 kprintf("pg: %d, PAGE_SIZE: %d\n", pg
, PAGE_SIZE
);
299 /* allocate new private data page(s) */
300 gd
= (struct mdglobaldata
*)kmem_alloc(&kernel_map
,
301 MDGLOBALDATA_BASEALLOC_SIZE
);
302 kprintf("added gd: %p, x: %d, sizeof(gd): %x\n", gd
, x
,
303 sizeof(struct mdglobaldata
));
304 /* wire it into the private page table page */
305 for (i
= 0; i
< MDGLOBALDATA_BASEALLOC_SIZE
; i
+= PAGE_SIZE
) {
306 SMPpt
[pg
+ i
/ PAGE_SIZE
] = (pt_entry_t
)
307 (PG_V
| PG_RW
| vtophys_pte((char *)gd
+ i
));
309 pg
+= MDGLOBALDATA_BASEALLOC_PAGES
;
311 SMPpt
[pg
+ 0] = 0; /* *gd_CMAP1 */
312 SMPpt
[pg
+ 1] = 0; /* *gd_CMAP2 */
313 SMPpt
[pg
+ 2] = 0; /* *gd_CMAP3 */
314 SMPpt
[pg
+ 3] = 0; /* *gd_PMAP1 */
316 /* allocate and set up an idle stack data page */
317 stack
= (char *)kmem_alloc(&kernel_map
, UPAGES
*PAGE_SIZE
);
318 for (i
= 0; i
< UPAGES
; i
++) {
319 SMPpt
[pg
+ 4 + i
] = (pt_entry_t
)
320 (PG_V
| PG_RW
| vtophys_pte(PAGE_SIZE
* i
+ stack
));
324 gd
= &CPU_prvspace
[x
].mdglobaldata
; /* official location */
325 bzero(gd
, sizeof(*gd
));
326 gd
->mi
.gd_prvspace
= ps
= &CPU_prvspace
[x
];
328 /* prime data page for it to use */
329 mi_gdinit(&gd
->mi
, x
);
332 gd
->gd_CMAP1
= &SMPpt
[pg
+ 0];
333 gd
->gd_CMAP2
= &SMPpt
[pg
+ 1];
334 gd
->gd_CMAP3
= &SMPpt
[pg
+ 2];
335 gd
->gd_PMAP1
= &SMPpt
[pg
+ 3];
337 gd
->gd_CMAP1
= SMPpt2
[0];
338 gd
->gd_CMAP2
= SMPpt2
[1];
339 gd
->gd_CMAP3
= SMPpt2
[2];
340 gd
->gd_PMAP1
= SMPpt2
[3];
341 gd
->gd_CADDR1
= ps
->CPAGE1
;
342 gd
->gd_CADDR2
= ps
->CPAGE2
;
343 gd
->gd_CADDR3
= ps
->CPAGE3
;
344 gd
->gd_PADDR1
= (unsigned *)ps
->PPAGE1
;
345 gd
->mi
.gd_ipiq
= (void *)kmem_alloc(&kernel_map
, sizeof(lwkt_ipiq
) * (mp_naps
+ 1));
346 bzero(gd
->mi
.gd_ipiq
, sizeof(lwkt_ipiq
) * (mp_naps
+ 1));
350 /* setup a vector to our boot code */
351 *((volatile u_short
*) WARMBOOT_OFF
) = WARMBOOT_TARGET
;
352 *((volatile u_short
*) WARMBOOT_SEG
) = (boot_addr
>> 4);
353 outb(CMOS_REG
, BIOS_RESET
);
354 outb(CMOS_DATA
, BIOS_WARM
); /* 'warm-start' */
358 * Setup the AP boot stack
360 bootSTK
= &ps
->idlestack
[UPAGES
*PAGE_SIZE
/2];
364 * Setup the AP's lwp, this is the 'cpu'
366 arg
= (int *)kmem_alloc(&kernel_map
, sizeof(int));
367 bzero(arg
, sizeof(int));
371 params
.func
= start_ap
;
373 params
.stack
= &CPU_prvspace
[x
].idlestack
;
374 params
.tid1
= &ap_tids
[x
];
375 params
.tid2
= &ap_tids
[x
];
378 kprintf("arg: %p, value = %d\n", arg
, *arg
);
379 pthread_create(&ap_tids
[x
], NULL
, start_ap
, arg
);