summaryrefslogtreecommitdiffstats
path: root/sys/kern/init_smp.c
blob: 0751f153ab84728e89d4e40b041ada29fab5f313 (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
/*
 * Copyright (c) 1996, Peter Wemm <peter@freebsd.org>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $Id: init_smp.c,v 1.5 1997/05/04 02:08:09 peter Exp $
 */

#include "opt_smp.h"
#include "opt_smp_autostart.h"

#include <sys/param.h>
#include <sys/filedesc.h>
#include <sys/kernel.h>
#include <sys/sysctl.h>
#include <sys/proc.h>
#include <sys/resourcevar.h>
#include <sys/signalvar.h>
#include <sys/systm.h>
#include <sys/vnode.h>
#include <sys/sysent.h>
#include <sys/reboot.h>
#include <sys/sysproto.h>
#include <sys/vmmeter.h>
#include <sys/lock.h>

#include <machine/cpu.h>
#include <machine/smp.h>
#include <machine/smptests.h>	/** IGNORE_IDLEPROCS */

#include <vm/vm.h>
#include <vm/vm_param.h>
#include <vm/vm_prot.h>
#include <vm/pmap.h>
#include <vm/vm_map.h>
#include <sys/user.h>

int smp_active = 0;	/* are the APs allowed to run? */

static int
sysctl_smp_active SYSCTL_HANDLER_ARGS
{
	int error = 0;
	int new_val;

	error = SYSCTL_OUT(req, &smp_active, sizeof(int));

	if (error || !req->newptr)
		return (error);

	error = SYSCTL_IN(req, &new_val, sizeof(int));
	if (error)
		return (error);
	if (new_val < 1)
		return (EBUSY);
	if (new_val > mp_ncpus)
		return (EINVAL);
	smp_active = new_val;
	return (0);
}

SYSCTL_PROC(_kern, OID_AUTO, smp_active, CTLTYPE_INT|CTLFLAG_RW,
		0, 0, &sysctl_smp_active, "I", "");

int smp_cpus = 0;	/* how many cpu's running */
SYSCTL_INT(_kern, OID_AUTO, smp_cpus, CTLFLAG_RD, &smp_cpus, 0, "");

int idle_debug = 0;
SYSCTL_INT(_kern, OID_AUTO, idle_debug, CTLFLAG_RW, &idle_debug, 0, "");

int invltlb_ok = 0;	/* throttle smp_invltlb() till safe */

#if defined(IGNORE_IDLEPROCS)
int ignore_idleprocs = 1;
#else
int ignore_idleprocs = 0;
#endif
SYSCTL_INT(_kern, OID_AUTO, ignore_idleprocs, CTLFLAG_RW, &ignore_idleprocs,
	   0, "");

static void smp_kickoff __P((void *dummy));
SYSINIT(smpkick, SI_SUB_SMP, SI_ORDER_FIRST, smp_kickoff, NULL)

static void smp_idleloop __P((void *));

void	secondary_main __P((void));

static int idle_loops = 0;
void	boot_unlock __P((void));
 
struct proc *SMPidleproc[NCPU];
static int cpu_starting = -1;

static void
smp_kickoff(dummy)
	void *dummy;
{
	int		rval[2];	/* return from fork */
	struct proc	*p;
	int i;

	/*
	 * Create the appropriate number of cpu-idle-eaters
	 */
	for (i = 0; i < mp_ncpus; i++) {
		/* kernel thread*/
		if (fork(&proc0, NULL, rval))
			panic("cannot fork idle process");
		p = pfind(rval[0]);
		cpu_set_fork_handler(p, smp_idleloop, NULL);
		SMPidleproc[i] = p;
		p->p_flag |= P_INMEM | P_SYSTEM | P_IDLEPROC;
		sprintf(p->p_comm, "cpuidle%d", i);

		/*
		 * PRIO_IDLE is the last scheduled of the three
		 * classes and we choose the lowest priority possible
		 * for there.
		 */
		p->p_rtprio.type = RTP_PRIO_IDLE;
		p->p_rtprio.prio = RTP_PRIO_MAX;
	}

}


#define MSG_CPU_MADEIT \
	printf("SMP: TADA! CPU #%d made it into the scheduler!.\n", \
	       cpunumber())
#define MSG_NEXT_CPU \
	printf("SMP: %d of %d CPU's online. Unlocking next CPU..\n", \
	       smp_cpus, mp_ncpus)
#define MSG_FINAL_CPU \
	printf("SMP: All %d CPU's are online!\n", \
	       smp_cpus)
#define MSG_TOOMANY_CPU \
	printf("SMP: Hey! Too many cpu's started, %d of %d running!\n", \
	       smp_cpus, mp_ncpus)

/*
 * This is run by the secondary processor to kick things off.
 * It basically drops into the switch routine to pick the first
 * available process to run, which is probably an idle process.
 */

void
secondary_main()
{
	get_mplock();

	/*
	 * Record our ID so we know when we've released the mp_stk.
	 * We must remain single threaded through this.
	 */
	cpu_starting = cpunumber();
	smp_cpus++;

	printf("SMP: AP CPU #%d LAUNCHED!!  Starting Scheduling...\n",
		cpunumber());

	curproc = NULL;			/* ensure no context to save */
	cpu_switch(curproc);		/* start first process */
	panic("switch returned!");
}


/*
 * The main program loop for the idle process
 */

static void
smp_idleloop(dummy)
void *dummy;
{
	int dcnt = 0;

	/*
	 * This code is executed only on startup of the idleprocs
	 * The fact that this is executed is an indication that the
	 * idle procs are online and it's safe to kick off the first
	 * AP cpu.
	 */
	if ( ++idle_loops == mp_ncpus ) {
		printf("SMP: All idle procs online.\n");

#if defined(SMP_AUTOSTART)
		printf("SMP: *** AUTO *** starting 1st AP!\n");
		smp_cpus = 1;
		smp_active = mp_ncpus;	/* XXX */
		boot_unlock();
#else
		printf("You can now activate SMP processing, use: sysctl -w kern.smp_active=%d\n", mp_ncpus);
#endif /* SMP_AUTOSTART */
	}

	spl0();
	rel_mplock();

	while (1) {
		/*
		 * make the optimiser assume nothing about the
		 * which*qs variables
		 */
		__asm __volatile("" : : : "memory");
 
#if !defined(SMP_AUTOSTART)
		/*
		 * Alternate code to enable a lockstep startup
		 * via sysctl instead of automatically.
		 */
		if (smp_cpus == 0 && smp_active != 0) {
			get_mplock();
			printf("SMP: Starting 1st AP!\n");
			smp_cpus = 1;
			smp_active = mp_ncpus;	/* XXX */
			boot_unlock();
			rel_mplock();
		}
#endif /* !SMP_AUTOSTART */

		/*
		 * If smp_active is set to (say) 1, we want cpu id's
		 * 1,2,etc to freeze here.
		 */
		if (smp_active && smp_active <= cpunumber()) {
			get_mplock();
			printf("SMP: cpu#%d freezing\n", cpunumber());
			wakeup((caddr_t)&smp_active);
			rel_mplock();

			while (smp_active <= cpunumber()) {
				__asm __volatile("" : : : "memory");
			}
			get_mplock();
			printf("SMP: cpu#%d waking up!\n", cpunumber());
			rel_mplock();
		}
			
		if (whichqs || whichrtqs || (!ignore_idleprocs && whichidqs)) {
			/* grab lock for kernel "entry" */
			get_mplock();

			/* We need to retest due to the spin lock */
			__asm __volatile("" : : : "memory");

			if (whichqs || whichrtqs ||
					(!ignore_idleprocs && whichidqs)) {
				splhigh();
				if (curproc)
					setrunqueue(curproc);
				cnt.v_swtch++;
				cpu_switch(curproc);
				microtime(&runtime);

				if (cpu_starting != -1 &&
				    cpu_starting == cpunumber()) {
					/*
					 * TADA! we have arrived! unlock the
					 * next cpu now that we have released
					 * the single mp_stk.
					 */
					MSG_CPU_MADEIT;
					cpu_starting = -1;

					if (smp_cpus < mp_ncpus) {
						MSG_NEXT_CPU;
						boot_unlock();
					} else if (smp_cpus > mp_ncpus) {
						MSG_TOOMANY_CPU;
						panic("too many cpus");
					} else {
						MSG_FINAL_CPU;

						/*
						 * It's safe to send IPI's now
						 * that all CPUs are online.
						 */
						invltlb_ok = 1;
					}

					/* Init local apic for irq's */
					apic_initialize(0);
				}

				(void)spl0();
			}
			rel_mplock();
		} else {
			dcnt++;
			if (idle_debug && (dcnt % idle_debug) == 0) {
				get_mplock();
				printf("idleproc pid#%d on cpu#%d, lock %08x\n",
					curproc->p_pid, cpunumber(), mp_lock);
				rel_mplock();
			}
		}
	}
}
OpenPOWER on IntegriCloud