xref: /netbsd-src/sys/kern/kern_cpu.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /*	$NetBSD: kern_cpu.c,v 1.92 2020/07/13 13:16:07 jruoho Exp $	*/
2 
3 /*-
4  * Copyright (c) 2007, 2008, 2009, 2010, 2012, 2019 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Andrew Doran.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*-
33  * Copyright (c)2007 YAMAMOTO Takashi,
34  * All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  *
45  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
46  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
49  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55  * SUCH DAMAGE.
56  */
57 
58 /*
59  * CPU related routines not shared with rump.
60  */
61 
62 #include <sys/cdefs.h>
63 __KERNEL_RCSID(0, "$NetBSD: kern_cpu.c,v 1.92 2020/07/13 13:16:07 jruoho Exp $");
64 
65 #ifdef _KERNEL_OPT
66 #include "opt_cpu_ucode.h"
67 #endif
68 
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/idle.h>
72 #include <sys/sched.h>
73 #include <sys/intr.h>
74 #include <sys/conf.h>
75 #include <sys/cpu.h>
76 #include <sys/cpuio.h>
77 #include <sys/proc.h>
78 #include <sys/percpu.h>
79 #include <sys/kernel.h>
80 #include <sys/kauth.h>
81 #include <sys/xcall.h>
82 #include <sys/pool.h>
83 #include <sys/kmem.h>
84 #include <sys/select.h>
85 #include <sys/namei.h>
86 #include <sys/callout.h>
87 #include <sys/pcu.h>
88 
89 #include <uvm/uvm_extern.h>
90 
91 #include "ioconf.h"
92 
93 /*
94  * If the port has stated that cpu_data is the first thing in cpu_info,
95  * verify that the claim is true. This will prevent them from getting out
96  * of sync.
97  */
98 #ifdef __HAVE_CPU_DATA_FIRST
99 CTASSERT(offsetof(struct cpu_info, ci_data) == 0);
100 #else
101 CTASSERT(offsetof(struct cpu_info, ci_data) != 0);
102 #endif
103 
104 int (*compat_cpuctl_ioctl)(struct lwp *, u_long, void *) = (void *)enosys;
105 
106 static void	cpu_xc_online(struct cpu_info *, void *);
107 static void	cpu_xc_offline(struct cpu_info *, void *);
108 
109 dev_type_ioctl(cpuctl_ioctl);
110 
111 const struct cdevsw cpuctl_cdevsw = {
112 	.d_open = nullopen,
113 	.d_close = nullclose,
114 	.d_read = nullread,
115 	.d_write = nullwrite,
116 	.d_ioctl = cpuctl_ioctl,
117 	.d_stop = nullstop,
118 	.d_tty = notty,
119 	.d_poll = nopoll,
120 	.d_mmap = nommap,
121 	.d_kqfilter = nokqfilter,
122 	.d_discard = nodiscard,
123 	.d_flag = D_OTHER | D_MPSAFE
124 };
125 
126 int
127 mi_cpu_attach(struct cpu_info *ci)
128 {
129 	int error;
130 
131 	KASSERT(maxcpus > 0);
132 
133 	if ((ci->ci_index = ncpu) >= maxcpus)
134 		panic("Too many CPUs.  Increase MAXCPUS?");
135 	kcpuset_set(kcpuset_attached, cpu_index(ci));
136 
137 	/*
138 	 * Create a convenience cpuset of just ourselves.
139 	 */
140 	kcpuset_create(&ci->ci_data.cpu_kcpuset, true);
141 	kcpuset_set(ci->ci_data.cpu_kcpuset, cpu_index(ci));
142 
143 	TAILQ_INIT(&ci->ci_data.cpu_ld_locks);
144 	__cpu_simple_lock_init(&ci->ci_data.cpu_ld_lock);
145 
146 	/* This is useful for eg, per-cpu evcnt */
147 	snprintf(ci->ci_data.cpu_name, sizeof(ci->ci_data.cpu_name), "cpu%d",
148 	    cpu_index(ci));
149 
150 	if (__predict_false(cpu_infos == NULL)) {
151 		size_t ci_bufsize = (maxcpus + 1) * sizeof(struct cpu_info *);
152 		cpu_infos = kmem_zalloc(ci_bufsize, KM_SLEEP);
153 	}
154 	cpu_infos[cpu_index(ci)] = ci;
155 
156 	sched_cpuattach(ci);
157 
158 	error = create_idle_lwp(ci);
159 	if (error != 0) {
160 		/* XXX revert sched_cpuattach */
161 		return error;
162 	}
163 
164 	if (ci == curcpu())
165 		ci->ci_onproc = curlwp;
166 	else
167 		ci->ci_onproc = ci->ci_data.cpu_idlelwp;
168 
169 	percpu_init_cpu(ci);
170 	softint_init(ci);
171 	callout_init_cpu(ci);
172 	xc_init_cpu(ci);
173 	pool_cache_cpu_init(ci);
174 	selsysinit(ci);
175 	cache_cpu_init(ci);
176 	TAILQ_INIT(&ci->ci_data.cpu_biodone);
177 	ncpu++;
178 	ncpuonline++;
179 
180 	return 0;
181 }
182 
183 void
184 cpuctlattach(int dummy __unused)
185 {
186 
187 	KASSERT(cpu_infos != NULL);
188 }
189 
190 int
191 cpuctl_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
192 {
193 	CPU_INFO_ITERATOR cii;
194 	cpustate_t *cs;
195 	struct cpu_info *ci;
196 	int error, i;
197 	u_int id;
198 
199 	error = 0;
200 
201 	mutex_enter(&cpu_lock);
202 	switch (cmd) {
203 	case IOC_CPU_SETSTATE:
204 		cs = data;
205 		error = kauth_authorize_system(l->l_cred,
206 		    KAUTH_SYSTEM_CPU, KAUTH_REQ_SYSTEM_CPU_SETSTATE, cs, NULL,
207 		    NULL);
208 		if (error != 0)
209 			break;
210 		if (cs->cs_id >= maxcpus ||
211 		    (ci = cpu_lookup(cs->cs_id)) == NULL) {
212 			error = ESRCH;
213 			break;
214 		}
215 		error = cpu_setintr(ci, cs->cs_intr);
216 		if (error)
217 			break;
218 		error = cpu_setstate(ci, cs->cs_online);
219 		break;
220 
221 	case IOC_CPU_GETSTATE:
222 		cs = data;
223 		id = cs->cs_id;
224 		memset(cs, 0, sizeof(*cs));
225 		cs->cs_id = id;
226 		if (cs->cs_id >= maxcpus ||
227 		    (ci = cpu_lookup(id)) == NULL) {
228 			error = ESRCH;
229 			break;
230 		}
231 		if ((ci->ci_schedstate.spc_flags & SPCF_OFFLINE) != 0)
232 			cs->cs_online = false;
233 		else
234 			cs->cs_online = true;
235 		if ((ci->ci_schedstate.spc_flags & SPCF_NOINTR) != 0)
236 			cs->cs_intr = false;
237 		else
238 			cs->cs_intr = true;
239 		cs->cs_lastmod = (int32_t)ci->ci_schedstate.spc_lastmod;
240 		cs->cs_lastmodhi = (int32_t)
241 		    (ci->ci_schedstate.spc_lastmod >> 32);
242 		cs->cs_intrcnt = cpu_intr_count(ci) + 1;
243 		cs->cs_hwid = ci->ci_cpuid;
244 		break;
245 
246 	case IOC_CPU_MAPID:
247 		i = 0;
248 		for (CPU_INFO_FOREACH(cii, ci)) {
249 			if (i++ == *(int *)data)
250 				break;
251 		}
252 		if (ci == NULL)
253 			error = ESRCH;
254 		else
255 			*(int *)data = cpu_index(ci);
256 		break;
257 
258 	case IOC_CPU_GETCOUNT:
259 		*(int *)data = ncpu;
260 		break;
261 
262 #ifdef CPU_UCODE
263 	case IOC_CPU_UCODE_GET_VERSION:
264 		error = cpu_ucode_get_version((struct cpu_ucode_version *)data);
265 		break;
266 
267 	case IOC_CPU_UCODE_APPLY:
268 		error = kauth_authorize_machdep(l->l_cred,
269 		    KAUTH_MACHDEP_CPU_UCODE_APPLY,
270 		    NULL, NULL, NULL, NULL);
271 		if (error != 0)
272 			break;
273 		error = cpu_ucode_apply((const struct cpu_ucode *)data);
274 		break;
275 #endif
276 
277 	default:
278 		error = (*compat_cpuctl_ioctl)(l, cmd, data);
279 		break;
280 	}
281 	mutex_exit(&cpu_lock);
282 
283 	return error;
284 }
285 
286 struct cpu_info *
287 cpu_lookup(u_int idx)
288 {
289 	struct cpu_info *ci;
290 
291 	/*
292 	 * cpu_infos is a NULL terminated array of MAXCPUS + 1 entries,
293 	 * so an index of MAXCPUS here is ok.  See mi_cpu_attach.
294 	 */
295 	KASSERT(idx <= maxcpus);
296 
297 	if (__predict_false(cpu_infos == NULL)) {
298 		KASSERT(idx == 0);
299 		return curcpu();
300 	}
301 
302 	ci = cpu_infos[idx];
303 	KASSERT(ci == NULL || cpu_index(ci) == idx);
304 	KASSERTMSG(idx < maxcpus || ci == NULL, "idx %d ci %p", idx, ci);
305 
306 	return ci;
307 }
308 
309 static void
310 cpu_xc_offline(struct cpu_info *ci, void *unused)
311 {
312 	struct schedstate_percpu *spc, *mspc = NULL;
313 	struct cpu_info *target_ci;
314 	struct lwp *l;
315 	CPU_INFO_ITERATOR cii;
316 	int s;
317 
318 	/*
319 	 * Thread that made the cross call (separate context) holds
320 	 * cpu_lock on our behalf.
321 	 */
322 	spc = &ci->ci_schedstate;
323 	s = splsched();
324 	spc->spc_flags |= SPCF_OFFLINE;
325 	splx(s);
326 
327 	/* Take the first available CPU for the migration. */
328 	for (CPU_INFO_FOREACH(cii, target_ci)) {
329 		mspc = &target_ci->ci_schedstate;
330 		if ((mspc->spc_flags & SPCF_OFFLINE) == 0)
331 			break;
332 	}
333 	KASSERT(target_ci != NULL);
334 
335 	/*
336 	 * Migrate all non-bound threads to the other CPU.  Note that this
337 	 * runs from the xcall thread, thus handling of LSONPROC is not needed.
338 	 */
339 	mutex_enter(&proc_lock);
340 	LIST_FOREACH(l, &alllwp, l_list) {
341 		struct cpu_info *mci;
342 
343 		lwp_lock(l);
344 		if (l->l_cpu != ci || (l->l_pflag & (LP_BOUND | LP_INTR))) {
345 			lwp_unlock(l);
346 			continue;
347 		}
348 		/* Regular case - no affinity. */
349 		if (l->l_affinity == NULL) {
350 			lwp_migrate(l, target_ci);
351 			continue;
352 		}
353 		/* Affinity is set, find an online CPU in the set. */
354 		for (CPU_INFO_FOREACH(cii, mci)) {
355 			mspc = &mci->ci_schedstate;
356 			if ((mspc->spc_flags & SPCF_OFFLINE) == 0 &&
357 			    kcpuset_isset(l->l_affinity, cpu_index(mci)))
358 				break;
359 		}
360 		if (mci == NULL) {
361 			lwp_unlock(l);
362 			mutex_exit(&proc_lock);
363 			goto fail;
364 		}
365 		lwp_migrate(l, mci);
366 	}
367 	mutex_exit(&proc_lock);
368 
369 #if PCU_UNIT_COUNT > 0
370 	pcu_save_all_on_cpu();
371 #endif
372 
373 #ifdef __HAVE_MD_CPU_OFFLINE
374 	cpu_offline_md();
375 #endif
376 	return;
377 fail:
378 	/* Just unset the SPCF_OFFLINE flag, caller will check */
379 	s = splsched();
380 	spc->spc_flags &= ~SPCF_OFFLINE;
381 	splx(s);
382 }
383 
384 static void
385 cpu_xc_online(struct cpu_info *ci, void *unused)
386 {
387 	struct schedstate_percpu *spc;
388 	int s;
389 
390 	spc = &ci->ci_schedstate;
391 	s = splsched();
392 	spc->spc_flags &= ~SPCF_OFFLINE;
393 	splx(s);
394 }
395 
396 int
397 cpu_setstate(struct cpu_info *ci, bool online)
398 {
399 	struct schedstate_percpu *spc;
400 	CPU_INFO_ITERATOR cii;
401 	struct cpu_info *ci2;
402 	uint64_t where;
403 	xcfunc_t func;
404 	int nonline;
405 
406 	spc = &ci->ci_schedstate;
407 
408 	KASSERT(mutex_owned(&cpu_lock));
409 
410 	if (online) {
411 		if ((spc->spc_flags & SPCF_OFFLINE) == 0)
412 			return 0;
413 		func = (xcfunc_t)cpu_xc_online;
414 	} else {
415 		if ((spc->spc_flags & SPCF_OFFLINE) != 0)
416 			return 0;
417 		nonline = 0;
418 		/*
419 		 * Ensure that at least one CPU within the processor set
420 		 * stays online.  Revisit this later.
421 		 */
422 		for (CPU_INFO_FOREACH(cii, ci2)) {
423 			if ((ci2->ci_schedstate.spc_flags & SPCF_OFFLINE) != 0)
424 				continue;
425 			if (ci2->ci_schedstate.spc_psid != spc->spc_psid)
426 				continue;
427 			nonline++;
428 		}
429 		if (nonline == 1)
430 			return EBUSY;
431 		func = (xcfunc_t)cpu_xc_offline;
432 	}
433 
434 	where = xc_unicast(0, func, ci, NULL, ci);
435 	xc_wait(where);
436 	if (online) {
437 		KASSERT((spc->spc_flags & SPCF_OFFLINE) == 0);
438 		ncpuonline++;
439 	} else {
440 		if ((spc->spc_flags & SPCF_OFFLINE) == 0) {
441 			/* If was not set offline, then it is busy */
442 			return EBUSY;
443 		}
444 		ncpuonline--;
445 	}
446 
447 	spc->spc_lastmod = time_second;
448 	return 0;
449 }
450 
451 #if defined(__HAVE_INTR_CONTROL)
452 static void
453 cpu_xc_intr(struct cpu_info *ci, void *unused)
454 {
455 	struct schedstate_percpu *spc;
456 	int s;
457 
458 	spc = &ci->ci_schedstate;
459 	s = splsched();
460 	spc->spc_flags &= ~SPCF_NOINTR;
461 	splx(s);
462 }
463 
464 static void
465 cpu_xc_nointr(struct cpu_info *ci, void *unused)
466 {
467 	struct schedstate_percpu *spc;
468 	int s;
469 
470 	spc = &ci->ci_schedstate;
471 	s = splsched();
472 	spc->spc_flags |= SPCF_NOINTR;
473 	splx(s);
474 }
475 
476 int
477 cpu_setintr(struct cpu_info *ci, bool intr)
478 {
479 	struct schedstate_percpu *spc;
480 	CPU_INFO_ITERATOR cii;
481 	struct cpu_info *ci2;
482 	uint64_t where;
483 	xcfunc_t func;
484 	int nintr;
485 
486 	spc = &ci->ci_schedstate;
487 
488 	KASSERT(mutex_owned(&cpu_lock));
489 
490 	if (intr) {
491 		if ((spc->spc_flags & SPCF_NOINTR) == 0)
492 			return 0;
493 		func = (xcfunc_t)cpu_xc_intr;
494 	} else {
495 		if (CPU_IS_PRIMARY(ci))
496 			return EINVAL;
497 		if ((spc->spc_flags & SPCF_NOINTR) != 0)
498 			return 0;
499 		/*
500 		 * Ensure that at least one CPU within the system
501 		 * is handing device interrupts.
502 		 */
503 		nintr = 0;
504 		for (CPU_INFO_FOREACH(cii, ci2)) {
505 			if ((ci2->ci_schedstate.spc_flags & SPCF_NOINTR) != 0)
506 				continue;
507 			if (ci2 == ci)
508 				continue;
509 			nintr++;
510 		}
511 		if (nintr == 0)
512 			return EBUSY;
513 		func = (xcfunc_t)cpu_xc_nointr;
514 	}
515 
516 	where = xc_unicast(0, func, ci, NULL, ci);
517 	xc_wait(where);
518 	if (intr) {
519 		KASSERT((spc->spc_flags & SPCF_NOINTR) == 0);
520 	} else if ((spc->spc_flags & SPCF_NOINTR) == 0) {
521 		/* If was not set offline, then it is busy */
522 		return EBUSY;
523 	}
524 
525 	/* Direct interrupts away from the CPU and record the change. */
526 	cpu_intr_redistribute();
527 	spc->spc_lastmod = time_second;
528 	return 0;
529 }
530 #else	/* __HAVE_INTR_CONTROL */
531 int
532 cpu_setintr(struct cpu_info *ci, bool intr)
533 {
534 
535 	return EOPNOTSUPP;
536 }
537 
538 u_int
539 cpu_intr_count(struct cpu_info *ci)
540 {
541 
542 	return 0;	/* 0 == "don't know" */
543 }
544 #endif	/* __HAVE_INTR_CONTROL */
545 
546 #ifdef CPU_UCODE
547 int
548 cpu_ucode_load(struct cpu_ucode_softc *sc, const char *fwname)
549 {
550 	firmware_handle_t fwh;
551 	int error;
552 
553 	if (sc->sc_blob != NULL) {
554 		firmware_free(sc->sc_blob, sc->sc_blobsize);
555 		sc->sc_blob = NULL;
556 		sc->sc_blobsize = 0;
557 	}
558 
559 	error = cpu_ucode_md_open(&fwh, sc->loader_version, fwname);
560 	if (error != 0) {
561 #ifdef DEBUG
562 		printf("ucode: firmware_open(%s) failed: %i\n", fwname, error);
563 #endif
564 		goto err0;
565 	}
566 
567 	sc->sc_blobsize = firmware_get_size(fwh);
568 	if (sc->sc_blobsize == 0) {
569 		error = EFTYPE;
570 		firmware_close(fwh);
571 		goto err0;
572 	}
573 	sc->sc_blob = firmware_malloc(sc->sc_blobsize);
574 	if (sc->sc_blob == NULL) {
575 		error = ENOMEM;
576 		firmware_close(fwh);
577 		goto err0;
578 	}
579 
580 	error = firmware_read(fwh, 0, sc->sc_blob, sc->sc_blobsize);
581 	firmware_close(fwh);
582 	if (error != 0)
583 		goto err1;
584 
585 	return 0;
586 
587 err1:
588 	firmware_free(sc->sc_blob, sc->sc_blobsize);
589 	sc->sc_blob = NULL;
590 	sc->sc_blobsize = 0;
591 err0:
592 	return error;
593 }
594 #endif
595