Home | History | Annotate | Line # | Download | only in kern
kern_cpu.c revision 1.13
      1 /*	$NetBSD: kern_cpu.c,v 1.13 2007/11/06 00:42:41 ad Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2007 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  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*-
     40  * Copyright (c)2007 YAMAMOTO Takashi,
     41  * All rights reserved.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  *
     52  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     53  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     54  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     55  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     56  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     57  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     58  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     59  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     60  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     61  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     62  * SUCH DAMAGE.
     63  */
     64 
     65 #include <sys/cdefs.h>
     66 
     67 __KERNEL_RCSID(0, "$NetBSD: kern_cpu.c,v 1.13 2007/11/06 00:42:41 ad Exp $");
     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/kernel.h>
     79 #include <sys/kauth.h>
     80 #include <sys/xcall.h>
     81 #include <sys/pool.h>
     82 
     83 #include <uvm/uvm_extern.h>
     84 
     85 void	cpuctlattach(int);
     86 
     87 static void	cpu_xc_online(struct cpu_info *);
     88 static void	cpu_xc_offline(struct cpu_info *);
     89 
     90 dev_type_ioctl(cpuctl_ioctl);
     91 
     92 const struct cdevsw cpuctl_cdevsw = {
     93 	nullopen, nullclose, nullread, nullwrite, cpuctl_ioctl,
     94 	nullstop, notty, nopoll, nommap, nokqfilter,
     95 	D_OTHER | D_MPSAFE
     96 };
     97 
     98 kmutex_t cpu_lock;
     99 int	ncpu;
    100 int	ncpuonline;
    101 
    102 int
    103 mi_cpu_attach(struct cpu_info *ci)
    104 {
    105 	struct schedstate_percpu *spc = &ci->ci_schedstate;
    106 	int error;
    107 
    108 	ci->ci_index = ncpu;
    109 
    110 	mutex_init(&spc->spc_lwplock, MUTEX_SPIN, IPL_SCHED);
    111 	sched_cpuattach(ci);
    112 	uvm_cpu_attach(ci);
    113 
    114 	error = create_idle_lwp(ci);
    115 	if (error != 0) {
    116 		/* XXX revert sched_cpuattach */
    117 		return error;
    118 	}
    119 
    120 	if (ci == curcpu())
    121 		ci->ci_data.cpu_onproc = curlwp;
    122 	else
    123 		ci->ci_data.cpu_onproc = ci->ci_data.cpu_idlelwp;
    124 
    125 	softint_init(ci);
    126 	xc_init_cpu(ci);
    127 	TAILQ_INIT(&ci->ci_data.cpu_biodone);
    128 	ncpu++;
    129 	ncpuonline++;
    130 
    131 	return 0;
    132 }
    133 
    134 void
    135 cpuctlattach(int dummy)
    136 {
    137 
    138 }
    139 
    140 int
    141 cpuctl_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
    142 {
    143 	CPU_INFO_ITERATOR cii;
    144 	cpustate_t *cs;
    145 	struct cpu_info *ci;
    146 	int error, i;
    147 	u_int id;
    148 
    149 	error = 0;
    150 
    151 	mutex_enter(&cpu_lock);
    152 	switch (cmd) {
    153 	case IOC_CPU_SETSTATE:
    154 		error = kauth_authorize_generic(l->l_cred,
    155 		    KAUTH_GENERIC_ISSUSER, NULL);
    156 		if (error != 0)
    157 			break;
    158 		cs = data;
    159 		if ((ci = cpu_lookup(cs->cs_id)) == NULL) {
    160 			error = ESRCH;
    161 			break;
    162 		}
    163 		if (!cs->cs_intr) {
    164 			error = EOPNOTSUPP;
    165 			break;
    166 		}
    167 		error = cpu_setonline(ci, cs->cs_online);
    168 		break;
    169 
    170 	case IOC_CPU_GETSTATE:
    171 		cs = data;
    172 		id = cs->cs_id;
    173 		memset(cs, 0, sizeof(*cs));
    174 		cs->cs_id = id;
    175 		if ((ci = cpu_lookup(id)) == NULL) {
    176 			error = ESRCH;
    177 			break;
    178 		}
    179 		if ((ci->ci_schedstate.spc_flags & SPCF_OFFLINE) != 0)
    180 			cs->cs_online = false;
    181 		else
    182 			cs->cs_online = true;
    183 		cs->cs_intr = true;
    184 		cs->cs_lastmod = ci->ci_schedstate.spc_lastmod;
    185 		break;
    186 
    187 	case IOC_CPU_MAPID:
    188 		i = 0;
    189 		for (CPU_INFO_FOREACH(cii, ci)) {
    190 			if (i++ == *(int *)data)
    191 				break;
    192 		}
    193 		if (ci == NULL)
    194 			error = ESRCH;
    195 		else
    196 			*(int *)data = ci->ci_cpuid;
    197 		break;
    198 
    199 	case IOC_CPU_GETCOUNT:
    200 		*(int *)data = ncpu;
    201 		break;
    202 
    203 	default:
    204 		error = ENOTTY;
    205 		break;
    206 	}
    207 	mutex_exit(&cpu_lock);
    208 
    209 	return error;
    210 }
    211 
    212 struct cpu_info *
    213 cpu_lookup(cpuid_t id)
    214 {
    215 	CPU_INFO_ITERATOR cii;
    216 	struct cpu_info *ci;
    217 
    218 	for (CPU_INFO_FOREACH(cii, ci)) {
    219 		if (ci->ci_cpuid == id)
    220 			return ci;
    221 	}
    222 
    223 	return NULL;
    224 }
    225 
    226 static void
    227 cpu_xc_offline(struct cpu_info *ci)
    228 {
    229 	struct schedstate_percpu *spc, *mspc = NULL;
    230 	struct cpu_info *mci;
    231 	struct lwp *l;
    232 	CPU_INFO_ITERATOR cii;
    233 	int s;
    234 
    235 	spc = &ci->ci_schedstate;
    236 	s = splsched();
    237 	spc->spc_flags |= SPCF_OFFLINE;
    238 	splx(s);
    239 
    240 	/* Take the first available CPU for the migration */
    241 	for (CPU_INFO_FOREACH(cii, mci)) {
    242 		mspc = &mci->ci_schedstate;
    243 		if ((mspc->spc_flags & SPCF_OFFLINE) == 0)
    244 			break;
    245 	}
    246 	KASSERT(mci != NULL);
    247 
    248 	/*
    249 	 * Migrate all non-bound threads to the other CPU.
    250 	 * Please note, that this runs from the xcall thread, thus handling
    251 	 * of LSONPROC is not needed.
    252 	 */
    253 	mutex_enter(&proclist_lock);
    254 
    255 	/*
    256 	 * Note that threads on the runqueue might sleep after this, but
    257 	 * sched_takecpu() would migrate such threads to the appropriate CPU.
    258 	 */
    259 	LIST_FOREACH(l, &alllwp, l_list) {
    260 		lwp_lock(l);
    261 		if (l->l_cpu == ci && (l->l_stat == LSSLEEP ||
    262 		    l->l_stat == LSSTOP || l->l_stat == LSSUSPENDED)) {
    263 			KASSERT((l->l_flag & LW_RUNNING) == 0);
    264 			l->l_cpu = mci;
    265 		}
    266 		lwp_unlock(l);
    267 	}
    268 
    269 	/*
    270 	 * Runqueues are locked with the global lock if pointers match,
    271 	 * thus hold only one.  Otherwise, double-lock the runqueues.
    272 	 */
    273 	if (spc->spc_mutex == mspc->spc_mutex) {
    274 		spc_lock(ci);
    275 	} else if (ci < mci) {
    276 		spc_lock(ci);
    277 		spc_lock(mci);
    278 	} else {
    279 		spc_lock(mci);
    280 		spc_lock(ci);
    281 	}
    282 
    283 	/* Handle LSRUN and LSIDL cases */
    284 	LIST_FOREACH(l, &alllwp, l_list) {
    285 		if (l->l_cpu != ci || (l->l_flag & LW_BOUND))
    286 			continue;
    287 		if (l->l_stat == LSRUN && (l->l_flag & LW_INMEM) != 0) {
    288 			sched_dequeue(l);
    289 			l->l_cpu = mci;
    290 			lwp_setlock(l, mspc->spc_mutex);
    291 			sched_enqueue(l, false);
    292 		} else if (l->l_stat == LSRUN || l->l_stat == LSIDL) {
    293 			l->l_cpu = mci;
    294 			lwp_setlock(l, mspc->spc_mutex);
    295 		}
    296 	}
    297 	if (spc->spc_mutex == mspc->spc_mutex) {
    298 		spc_unlock(ci);
    299 	} else {
    300 		spc_unlock(ci);
    301 		spc_unlock(mci);
    302 	}
    303 
    304 	mutex_exit(&proclist_lock);
    305 }
    306 
    307 static void
    308 cpu_xc_online(struct cpu_info *ci)
    309 {
    310 	struct schedstate_percpu *spc;
    311 	int s;
    312 
    313 	spc = &ci->ci_schedstate;
    314 	s = splsched();
    315 	spc->spc_flags &= ~SPCF_OFFLINE;
    316 	splx(s);
    317 }
    318 
    319 int
    320 cpu_setonline(struct cpu_info *ci, bool online)
    321 {
    322 	struct schedstate_percpu *spc;
    323 	CPU_INFO_ITERATOR cii;
    324 	struct cpu_info *ci2;
    325 	uint64_t where;
    326 	xcfunc_t func;
    327 	int nonline;
    328 
    329 	spc = &ci->ci_schedstate;
    330 
    331 	KASSERT(mutex_owned(&cpu_lock));
    332 
    333 	if (online) {
    334 		if ((spc->spc_flags & SPCF_OFFLINE) == 0)
    335 			return 0;
    336 		func = (xcfunc_t)cpu_xc_online;
    337 		ncpuonline++;
    338 	} else {
    339 		if ((spc->spc_flags & SPCF_OFFLINE) != 0)
    340 			return 0;
    341 		nonline = 0;
    342 		for (CPU_INFO_FOREACH(cii, ci2)) {
    343 			nonline += ((ci2->ci_schedstate.spc_flags &
    344 			    SPCF_OFFLINE) == 0);
    345 		}
    346 		if (nonline == 1)
    347 			return EBUSY;
    348 		func = (xcfunc_t)cpu_xc_offline;
    349 		ncpuonline--;
    350 	}
    351 
    352 	where = xc_unicast(0, func, ci, NULL, ci);
    353 	xc_wait(where);
    354 	if (online) {
    355 		KASSERT((spc->spc_flags & SPCF_OFFLINE) == 0);
    356 	} else {
    357 		KASSERT(spc->spc_flags & SPCF_OFFLINE);
    358 	}
    359 	spc->spc_lastmod = time_second;
    360 
    361 	return 0;
    362 }
    363