summaryrefslogtreecommitdiff
path: root/i386/i386/mp_desc.c
blob: 4b5a78ea77525385698167952475552fa67d0bf4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/* 
 * Mach Operating System
 * Copyright (c) 1991,1990 Carnegie Mellon University
 * All Rights Reserved.
 * 
 * Permission to use, copy, modify and distribute this software and its
 * documentation is hereby granted, provided that both the copyright
 * notice and this permission notice appear in all copies of the
 * software, derivative works or modified versions, and any portions
 * thereof, and that both notices appear in supporting documentation.
 * 
 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
 * 
 * Carnegie Mellon requests users of this software to return to
 * 
 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
 *  School of Computer Science
 *  Carnegie Mellon University
 *  Pittsburgh PA 15213-3890
 * 
 * any improvements or extensions that they make and grant Carnegie Mellon
 * the rights to redistribute these changes.
 */

#include <kern/cpu_number.h>
#include <kern/debug.h>
#include <kern/printf.h>
#include <kern/smp.h>
#include <kern/startup.h>
#include <kern/kmutex.h>
#include <mach/machine.h>
#include <mach/xen.h>
#include <vm/vm_kern.h>

#include <i386/mp_desc.h>
#include <i386/lock.h>
#include <i386/apic.h>
#include <i386/locore.h>
#include <i386/gdt.h>
#include <i386at/idt.h>
#include <i386at/int_init.h>
#include <i386/cpu.h>
#include <i386/smp.h>

#include <i386at/model_dep.h>
#include <machine/ktss.h>
#include <machine/smp.h>
#include <machine/tss.h>
#include <machine/io_perm.h>
#include <machine/vm_param.h>

#include <i386at/acpi_parse_apic.h>
#include <string.h>

/*
 * The i386 needs an interrupt stack to keep the PCB stack from being
 * overrun by interrupts.  All interrupt stacks MUST lie at lower addresses
 * than any thread`s kernel stack.
 */

/*
 * Addresses of bottom and top of interrupt stacks.
 */
vm_offset_t	int_stack_top[NCPUS];
vm_offset_t	int_stack_base[NCPUS];

/* Interrupt stack allocation */
uint8_t solid_intstack[NCPUS*INTSTACK_SIZE] __aligned(NCPUS*INTSTACK_SIZE);

void
interrupt_stack_alloc(void)
{
	int i;

	/*
	 * Set up pointers to the top of the interrupt stack.
	 */

	for (i = 0; i < NCPUS; i++) {
		int_stack_base[i] = (vm_offset_t) &solid_intstack[i * INTSTACK_SIZE];
		int_stack_top[i] = (vm_offset_t) &solid_intstack[(i + 1) * INTSTACK_SIZE] - 4;
	}
}

#if	NCPUS > 1
/*
 * Flag to mark SMP init by BSP complete
 */
int bspdone;

extern void *apboot, *apbootend;
extern volatile ApicLocalUnit* lapic;

/*
 * Multiprocessor i386/i486 systems use a separate copy of the
 * GDT, IDT, LDT, and kernel TSS per processor.  The first three
 * are separate to avoid lock contention: the i386 uses locked
 * memory cycles to access the descriptor tables.  The TSS is
 * separate since each processor needs its own kernel stack,
 * and since using a TSS marks it busy.
 */

/*
 * Descriptor tables.
 */
struct mp_desc_table	*mp_desc_table[NCPUS] = { 0 };

/*
 * Pointer to TSS for access in load_context.
 */
struct task_tss		*mp_ktss[NCPUS] = { 0 };

/*
 * Pointer to GDT to reset the KTSS busy bit.
 */
struct real_descriptor	*mp_gdt[NCPUS] = { 0 };

/*
 * Boot-time tables, for initialization and master processor.
 */
extern struct real_gate		idt[IDTSZ];
extern struct real_descriptor	gdt[GDTSZ];
extern struct real_descriptor	ldt[LDTSZ];

/*
 * Allocate and initialize the per-processor descriptor tables.
 */

int
mp_desc_init(int mycpu)
{
	struct mp_desc_table *mpt;
	vm_offset_t mem;

	if (mycpu == 0) {
		/*
		 * Master CPU uses the tables built at boot time.
		 * Just set the TSS and GDT pointers.
		 */
		mp_ktss[mycpu] = (struct task_tss *) &ktss;
		mp_gdt[mycpu] = gdt;
		return 0;
	}
	else {
		/*
		 * Allocate tables for other CPUs
		 */
		if (!init_alloc_aligned(sizeof(struct mp_desc_table), &mem))
			panic("not enough memory for descriptor tables");
		mpt = (struct mp_desc_table *)phystokv(mem);

		mp_desc_table[mycpu] = mpt;
		mp_ktss[mycpu] = &mpt->ktss;
		mp_gdt[mycpu] = mpt->gdt;

		/*
		 * Zero the tables
		 */
		memset(mpt->idt, 0, sizeof(idt));
		memset(mpt->gdt, 0, sizeof(gdt));
		memset(mpt->ldt, 0, sizeof(ldt));
		memset(&mpt->ktss, 0, sizeof(struct task_tss));

		return mycpu;
	}
}

/* XXX should be adjusted per CPU speed */
int simple_lock_pause_loop = 100;

unsigned int simple_lock_pause_count = 0;	/* debugging */

void
simple_lock_pause(void)
{
	static volatile int dummy;
	int i;

	simple_lock_pause_count++;

	/*
	 * Used in loops that are trying to acquire locks out-of-order.
	 */

	for (i = 0; i < simple_lock_pause_loop; i++)
	    dummy++;	/* keep the compiler from optimizing the loop away */
}

kern_return_t
cpu_control(int cpu, const int *info, unsigned int count)
{
	printf("cpu_control(%d, %p, %d) not implemented\n",
	       cpu, info, count);
	return KERN_FAILURE;
}

void
interrupt_processor(int cpu)
{
	smp_pmap_update(apic_get_cpu_apic_id(cpu));
}

static void
paging_enable(void)
{
#ifndef MACH_HYP
    /* Turn paging on.
     * TODO: Why does setting the WP bit here cause a crash?
     */
#if PAE
    set_cr4(get_cr4() | CR4_PAE);
#endif
    set_cr0(get_cr0() | CR0_PG /* | CR0_WP */);
    set_cr0(get_cr0() & ~(CR0_CD | CR0_NW));
    if (CPU_HAS_FEATURE(CPU_FEATURE_PGE))
        set_cr4(get_cr4() | CR4_PGE);
#endif  /* MACH_HYP */
}

void
cpu_setup(int cpu)
{
    pmap_make_temporary_mapping();
    printf("AP=(%u) tempmap done\n", cpu);

    paging_enable();
    flush_instr_queue();
    printf("AP=(%u) paging done\n", cpu);

    mp_desc_init(cpu);
    printf("AP=(%u) mpdesc done\n", cpu);

    ap_gdt_init(cpu);
    printf("AP=(%u) gdt done\n", cpu);

    ap_idt_init(cpu);
    printf("AP=(%u) idt done\n", cpu);

    ap_int_init(cpu);
    printf("AP=(%u) int done\n", cpu);

    ap_ldt_init(cpu);
    printf("AP=(%u) ldt done\n", cpu);

    ap_ktss_init(cpu);
    printf("AP=(%u) ktss done\n", cpu);

    pmap_remove_temporary_mapping();
    printf("AP=(%u) remove tempmap done\n", cpu);

    pmap_set_page_dir();
    flush_tlb();
    printf("AP=(%u) reset page dir done\n", cpu);

    /* Initialize machine_slot fields with the cpu data */
    machine_slot[cpu].cpu_subtype = CPU_SUBTYPE_AT386;
    machine_slot[cpu].cpu_type = machine_slot[0].cpu_type;

    lapic_enable();
    cpu_launch_first_thread(THREAD_NULL);
}

void
cpu_ap_main()
{
    unsigned apic_id = (((ApicLocalUnit*)phystokv(lapic_addr))->apic_id.r >> 24) & 0xff;
    int cpu = apic_get_cpu_kernel_id(apic_id);

    do {
	cpu_pause();
    } while (bspdone != cpu);

    __sync_synchronize();

    cpu_setup(cpu);
}

kern_return_t
cpu_start(int cpu)
{
    assert(machine_slot[cpu].running != TRUE);

    uint16_t apic_id = apic_get_cpu_apic_id(cpu);

    printf("Trying to enable: %d\n", apic_id);

    smp_startup_cpu(apic_id, AP_BOOT_ADDR);

    printf("Started cpu %d (lapic id %04x)\n", cpu, apic_id);

    return KERN_SUCCESS;
}

void
start_other_cpus(void)
{
	int ncpus = smp_get_numcpus();

	//Copy cpu initialization assembly routine
	memcpy((void*)phystokv(AP_BOOT_ADDR), (void*) &apboot,
	       (uint32_t)&apbootend - (uint32_t)&apboot);

#ifndef APIC
	lapic_enable(); /* Enable lapic only once */
#endif
	unsigned cpu;

	splhigh();

	bspdone = 0;
	for (cpu = 1; cpu < ncpus; cpu++) {
		machine_slot[cpu].running = FALSE;

		//Start cpu
		printf("Starting AP %d\n", cpu);
		cpu_start(cpu);

		bspdone++;
		do {
			cpu_pause();
		} while (machine_slot[cpu].running == FALSE);

		__sync_synchronize();
	}
	printf("BSP: Completed SMP init\n");
}
#endif	/* NCPUS > 1 */