Home | History | Annotate | Line # | Download | only in kern
kern_cpu.c revision 1.21.6.7
      1 /*	$NetBSD: kern_cpu.c,v 1.21.6.7 2009/01/17 13:29:18 mjf Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2007, 2008 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 #include <sys/cdefs.h>
     59 __KERNEL_RCSID(0, "$NetBSD: kern_cpu.c,v 1.21.6.7 2009/01/17 13:29:18 mjf Exp $");
     60 
     61 #include <sys/param.h>
     62 #include <sys/systm.h>
     63 #include <sys/idle.h>
     64 #include <sys/sched.h>
     65 #include <sys/intr.h>
     66 #include <sys/conf.h>
     67 #include <sys/cpu.h>
     68 #include <sys/cpuio.h>
     69 #include <sys/proc.h>
     70 #include <sys/percpu.h>
     71 #include <sys/kernel.h>
     72 #include <sys/kauth.h>
     73 #include <sys/xcall.h>
     74 #include <sys/pool.h>
     75 #include <sys/kmem.h>
     76 #include <sys/select.h>
     77 #include <sys/namei.h>
     78 #include <sys/callout.h>
     79 
     80 #include <uvm/uvm_extern.h>
     81 
     82 void	cpuctlattach(int);
     83 
     84 static void	cpu_xc_online(struct cpu_info *);
     85 static void	cpu_xc_offline(struct cpu_info *);
     86 
     87 dev_type_ioctl(cpuctl_ioctl);
     88 
     89 const struct cdevsw cpuctl_cdevsw = {
     90 	nullopen, nullclose, nullread, nullwrite, cpuctl_ioctl,
     91 	nullstop, notty, nopoll, nommap, nokqfilter,
     92 	D_OTHER | D_MPSAFE
     93 };
     94 
     95 kmutex_t cpu_lock;
     96 int	ncpu;
     97 int	ncpuonline;
     98 bool	mp_online;
     99 struct	cpuqueue cpu_queue = CIRCLEQ_HEAD_INITIALIZER(cpu_queue);
    100 
    101 static struct cpu_info *cpu_infos[MAXCPUS];
    102 
    103 int
    104 mi_cpu_attach(struct cpu_info *ci)
    105 {
    106 	int error;
    107 
    108 	ci->ci_index = ncpu;
    109 	cpu_infos[cpu_index(ci)] = ci;
    110 	CIRCLEQ_INSERT_TAIL(&cpu_queue, ci, ci_data.cpu_qchain);
    111 	TAILQ_INIT(&ci->ci_data.cpu_ld_locks);
    112 	__cpu_simple_lock_init(&ci->ci_data.cpu_ld_lock);
    113 
    114 	sched_cpuattach(ci);
    115 
    116 	error = create_idle_lwp(ci);
    117 	if (error != 0) {
    118 		/* XXX revert sched_cpuattach */
    119 		return error;
    120 	}
    121 
    122 	if (ci == curcpu())
    123 		ci->ci_data.cpu_onproc = curlwp;
    124 	else
    125 		ci->ci_data.cpu_onproc = ci->ci_data.cpu_idlelwp;
    126 
    127 	percpu_init_cpu(ci);
    128 	softint_init(ci);
    129 	callout_init_cpu(ci);
    130 	xc_init_cpu(ci);
    131 	pool_cache_cpu_init(ci);
    132 	selsysinit(ci);
    133 	cache_cpu_init(ci);
    134 	TAILQ_INIT(&ci->ci_data.cpu_biodone);
    135 	ncpu++;
    136 	ncpuonline++;
    137 
    138 	return 0;
    139 }
    140 
    141 void
    142 cpuctlattach(int dummy)
    143 {
    144 	int maj = cdevsw_lookup_major(&cpuctl_cdevsw);
    145 	device_register_name(makedev(maj, 0), NULL, true, DEV_OTHER, "cpuctl");
    146 }
    147 
    148 int
    149 cpuctl_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
    150 {
    151 	CPU_INFO_ITERATOR cii;
    152 	cpustate_t *cs;
    153 	struct cpu_info *ci;
    154 	int error, i;
    155 	u_int id;
    156 
    157 	error = 0;
    158 
    159 	mutex_enter(&cpu_lock);
    160 	switch (cmd) {
    161 	case IOC_CPU_SETSTATE:
    162 		cs = data;
    163 		error = kauth_authorize_system(l->l_cred,
    164 		    KAUTH_SYSTEM_CPU, KAUTH_REQ_SYSTEM_CPU_SETSTATE, cs, NULL,
    165 		    NULL);
    166 		if (error != 0)
    167 			break;
    168 		if (cs->cs_id >= __arraycount(cpu_infos) ||
    169 		    (ci = cpu_lookup(cs->cs_id)) == NULL) {
    170 			error = ESRCH;
    171 			break;
    172 		}
    173 		if (!cs->cs_intr) {
    174 			error = EOPNOTSUPP;
    175 			break;
    176 		}
    177 		error = cpu_setstate(ci, cs->cs_online);
    178 		break;
    179 
    180 	case IOC_CPU_GETSTATE:
    181 		cs = data;
    182 		id = cs->cs_id;
    183 		memset(cs, 0, sizeof(*cs));
    184 		cs->cs_id = id;
    185 		if (cs->cs_id >= __arraycount(cpu_infos) ||
    186 		    (ci = cpu_lookup(id)) == NULL) {
    187 			error = ESRCH;
    188 			break;
    189 		}
    190 		if ((ci->ci_schedstate.spc_flags & SPCF_OFFLINE) != 0)
    191 			cs->cs_online = false;
    192 		else
    193 			cs->cs_online = true;
    194 		cs->cs_intr = true;
    195 		cs->cs_lastmod = ci->ci_schedstate.spc_lastmod;
    196 		break;
    197 
    198 	case IOC_CPU_MAPID:
    199 		i = 0;
    200 		for (CPU_INFO_FOREACH(cii, ci)) {
    201 			if (i++ == *(int *)data)
    202 				break;
    203 		}
    204 		if (ci == NULL)
    205 			error = ESRCH;
    206 		else
    207 			*(int *)data = cpu_index(ci);
    208 		break;
    209 
    210 	case IOC_CPU_GETCOUNT:
    211 		*(int *)data = ncpu;
    212 		break;
    213 
    214 	default:
    215 		error = ENOTTY;
    216 		break;
    217 	}
    218 	mutex_exit(&cpu_lock);
    219 
    220 	return error;
    221 }
    222 
    223 struct cpu_info *
    224 cpu_lookup(u_int idx)
    225 {
    226 	struct cpu_info *ci = cpu_infos[idx];
    227 
    228 	KASSERT(idx < __arraycount(cpu_infos));
    229 	KASSERT(ci == NULL || cpu_index(ci) == idx);
    230 
    231 	return ci;
    232 }
    233 
    234 static void
    235 cpu_xc_offline(struct cpu_info *ci)
    236 {
    237 	struct schedstate_percpu *spc, *mspc = NULL;
    238 	struct cpu_info *target_ci;
    239 	struct lwp *l;
    240 	CPU_INFO_ITERATOR cii;
    241 	int s;
    242 
    243 	/*
    244 	 * Thread which sent unicast (separate context) is holding
    245 	 * the cpu_lock for us.
    246 	 */
    247 	spc = &ci->ci_schedstate;
    248 	s = splsched();
    249 	spc->spc_flags |= SPCF_OFFLINE;
    250 	splx(s);
    251 
    252 	/* Take the first available CPU for the migration */
    253 	for (CPU_INFO_FOREACH(cii, target_ci)) {
    254 		mspc = &target_ci->ci_schedstate;
    255 		if ((mspc->spc_flags & SPCF_OFFLINE) == 0)
    256 			break;
    257 	}
    258 	KASSERT(target_ci != NULL);
    259 
    260 	/*
    261 	 * Migrate all non-bound threads to the other CPU.  Note that this
    262 	 * runs from the xcall thread, thus handling of LSONPROC is not needed.
    263 	 */
    264 	mutex_enter(proc_lock);
    265 	LIST_FOREACH(l, &alllwp, l_list) {
    266 		struct cpu_info *mci;
    267 
    268 		lwp_lock(l);
    269 		if (l->l_cpu != ci || (l->l_pflag & (LP_BOUND | LP_INTR))) {
    270 			lwp_unlock(l);
    271 			continue;
    272 		}
    273 		/* Normal case - no affinity */
    274 		if ((l->l_flag & LW_AFFINITY) == 0) {
    275 			lwp_migrate(l, target_ci);
    276 			continue;
    277 		}
    278 		/* Affinity is set, find an online CPU in the set */
    279 		KASSERT(l->l_affinity != NULL);
    280 		for (CPU_INFO_FOREACH(cii, mci)) {
    281 			mspc = &mci->ci_schedstate;
    282 			if ((mspc->spc_flags & SPCF_OFFLINE) == 0 &&
    283 			    kcpuset_isset(cpu_index(mci), l->l_affinity))
    284 				break;
    285 		}
    286 		if (mci == NULL) {
    287 			lwp_unlock(l);
    288 			mutex_exit(proc_lock);
    289 			goto fail;
    290 		}
    291 		lwp_migrate(l, mci);
    292 	}
    293 	mutex_exit(proc_lock);
    294 
    295 #ifdef __HAVE_MD_CPU_OFFLINE
    296 	cpu_offline_md();
    297 #endif
    298 	return;
    299 fail:
    300 	/* Just unset the SPCF_OFFLINE flag, caller will check */
    301 	s = splsched();
    302 	spc->spc_flags &= ~SPCF_OFFLINE;
    303 	splx(s);
    304 }
    305 
    306 static void
    307 cpu_xc_online(struct cpu_info *ci)
    308 {
    309 	struct schedstate_percpu *spc;
    310 	int s;
    311 
    312 	spc = &ci->ci_schedstate;
    313 	s = splsched();
    314 	spc->spc_flags &= ~SPCF_OFFLINE;
    315 	splx(s);
    316 }
    317 
    318 int
    319 cpu_setstate(struct cpu_info *ci, bool online)
    320 {
    321 	struct schedstate_percpu *spc;
    322 	CPU_INFO_ITERATOR cii;
    323 	struct cpu_info *ci2;
    324 	uint64_t where;
    325 	xcfunc_t func;
    326 	int nonline;
    327 
    328 	spc = &ci->ci_schedstate;
    329 
    330 	KASSERT(mutex_owned(&cpu_lock));
    331 
    332 	if (online) {
    333 		if ((spc->spc_flags & SPCF_OFFLINE) == 0)
    334 			return 0;
    335 		func = (xcfunc_t)cpu_xc_online;
    336 		ncpuonline++;
    337 	} else {
    338 		if ((spc->spc_flags & SPCF_OFFLINE) != 0)
    339 			return 0;
    340 		nonline = 0;
    341 		/*
    342 		 * Ensure that at least one CPU within the processor set
    343 		 * stays online.  Revisit this later.
    344 		 */
    345 		for (CPU_INFO_FOREACH(cii, ci2)) {
    346 			if ((ci2->ci_schedstate.spc_flags & SPCF_OFFLINE) != 0)
    347 				continue;
    348 			if (ci2->ci_schedstate.spc_psid != spc->spc_psid)
    349 				continue;
    350 			nonline++;
    351 		}
    352 		if (nonline == 1)
    353 			return EBUSY;
    354 		func = (xcfunc_t)cpu_xc_offline;
    355 		ncpuonline--;
    356 	}
    357 
    358 	where = xc_unicast(0, func, ci, NULL, ci);
    359 	xc_wait(where);
    360 	if (online) {
    361 		KASSERT((spc->spc_flags & SPCF_OFFLINE) == 0);
    362 	} else if ((spc->spc_flags & SPCF_OFFLINE) == 0) {
    363 		/* If was not set offline, then it is busy */
    364 		return EBUSY;
    365 	}
    366 
    367 	spc->spc_lastmod = time_second;
    368 	return 0;
    369 }
    370 
    371 bool
    372 cpu_softintr_p(void)
    373 {
    374 
    375 	return (curlwp->l_pflag & LP_INTR) != 0;
    376 }
    377