Home | History | Annotate | Line # | Download | only in kern
kern_cpu.c revision 1.24
      1 /*	$NetBSD: kern_cpu.c,v 1.24 2008/04/11 15:31:34 ad 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  * 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.24 2008/04/11 15:31:34 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/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 
     87 #include <uvm/uvm_extern.h>
     88 
     89 void	cpuctlattach(int);
     90 
     91 static void	cpu_xc_online(struct cpu_info *);
     92 static void	cpu_xc_offline(struct cpu_info *);
     93 
     94 dev_type_ioctl(cpuctl_ioctl);
     95 
     96 const struct cdevsw cpuctl_cdevsw = {
     97 	nullopen, nullclose, nullread, nullwrite, cpuctl_ioctl,
     98 	nullstop, notty, nopoll, nommap, nokqfilter,
     99 	D_OTHER | D_MPSAFE
    100 };
    101 
    102 kmutex_t cpu_lock;
    103 int	ncpu;
    104 int	ncpuonline;
    105 bool	mp_online;
    106 struct	cpuqueue cpu_queue = CIRCLEQ_HEAD_INITIALIZER(cpu_queue);
    107 
    108 static struct cpu_info *cpu_infos[MAXCPUS];
    109 
    110 int
    111 mi_cpu_attach(struct cpu_info *ci)
    112 {
    113 	struct schedstate_percpu *spc = &ci->ci_schedstate;
    114 	int error;
    115 
    116 	ci->ci_index = ncpu;
    117 	cpu_infos[cpu_index(ci)] = ci;
    118 	CIRCLEQ_INSERT_TAIL(&cpu_queue, ci, ci_data.cpu_qchain);
    119 
    120 	KASSERT(sizeof(kmutex_t) <= CACHE_LINE_SIZE);
    121 	spc->spc_lwplock = kmem_alloc(CACHE_LINE_SIZE, KM_SLEEP);
    122 	mutex_init(spc->spc_lwplock, MUTEX_DEFAULT, IPL_SCHED);
    123 	sched_cpuattach(ci);
    124 	uvm_cpu_attach(ci);
    125 
    126 	error = create_idle_lwp(ci);
    127 	if (error != 0) {
    128 		/* XXX revert sched_cpuattach */
    129 		return error;
    130 	}
    131 
    132 	if (ci == curcpu())
    133 		ci->ci_data.cpu_onproc = curlwp;
    134 	else
    135 		ci->ci_data.cpu_onproc = ci->ci_data.cpu_idlelwp;
    136 
    137 	percpu_init_cpu(ci);
    138 	softint_init(ci);
    139 	xc_init_cpu(ci);
    140 	pool_cache_cpu_init(ci);
    141 	selsysinit(ci);
    142 	cache_cpu_init(ci);
    143 	TAILQ_INIT(&ci->ci_data.cpu_biodone);
    144 	ncpu++;
    145 	ncpuonline++;
    146 
    147 	return 0;
    148 }
    149 
    150 void
    151 cpuctlattach(int dummy)
    152 {
    153 
    154 }
    155 
    156 int
    157 cpuctl_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
    158 {
    159 	CPU_INFO_ITERATOR cii;
    160 	cpustate_t *cs;
    161 	struct cpu_info *ci;
    162 	int error, i;
    163 	u_int id;
    164 
    165 	error = 0;
    166 
    167 	mutex_enter(&cpu_lock);
    168 	switch (cmd) {
    169 	case IOC_CPU_SETSTATE:
    170 		cs = data;
    171 		error = kauth_authorize_system(l->l_cred,
    172 		    KAUTH_SYSTEM_CPU, KAUTH_REQ_SYSTEM_CPU_SETSTATE, cs, NULL,
    173 		    NULL);
    174 		if (error != 0)
    175 			break;
    176 		if ((ci = cpu_lookup(cs->cs_id)) == NULL) {
    177 			error = ESRCH;
    178 			break;
    179 		}
    180 		if (!cs->cs_intr) {
    181 			error = EOPNOTSUPP;
    182 			break;
    183 		}
    184 		error = cpu_setonline(ci, cs->cs_online);
    185 		break;
    186 
    187 	case IOC_CPU_GETSTATE:
    188 		cs = data;
    189 		id = cs->cs_id;
    190 		memset(cs, 0, sizeof(*cs));
    191 		cs->cs_id = id;
    192 		if ((ci = cpu_lookup(id)) == NULL) {
    193 			error = ESRCH;
    194 			break;
    195 		}
    196 		if ((ci->ci_schedstate.spc_flags & SPCF_OFFLINE) != 0)
    197 			cs->cs_online = false;
    198 		else
    199 			cs->cs_online = true;
    200 		cs->cs_intr = true;
    201 		cs->cs_lastmod = ci->ci_schedstate.spc_lastmod;
    202 		break;
    203 
    204 	case IOC_CPU_MAPID:
    205 		i = 0;
    206 		for (CPU_INFO_FOREACH(cii, ci)) {
    207 			if (i++ == *(int *)data)
    208 				break;
    209 		}
    210 		if (ci == NULL)
    211 			error = ESRCH;
    212 		else
    213 			*(int *)data = ci->ci_cpuid;
    214 		break;
    215 
    216 	case IOC_CPU_GETCOUNT:
    217 		*(int *)data = ncpu;
    218 		break;
    219 
    220 	default:
    221 		error = ENOTTY;
    222 		break;
    223 	}
    224 	mutex_exit(&cpu_lock);
    225 
    226 	return error;
    227 }
    228 
    229 struct cpu_info *
    230 cpu_lookup(cpuid_t id)
    231 {
    232 	CPU_INFO_ITERATOR cii;
    233 	struct cpu_info *ci;
    234 
    235 	for (CPU_INFO_FOREACH(cii, ci)) {
    236 		if (ci->ci_cpuid == id)
    237 			return ci;
    238 	}
    239 
    240 	return NULL;
    241 }
    242 
    243 struct cpu_info *
    244 cpu_lookup_byindex(u_int idx)
    245 {
    246 	struct cpu_info *ci = cpu_infos[idx];
    247 
    248 	KASSERT(idx < MAXCPUS);
    249 	KASSERT(ci == NULL || cpu_index(ci) == idx);
    250 
    251 	return ci;
    252 }
    253 
    254 static void
    255 cpu_xc_offline(struct cpu_info *ci)
    256 {
    257 	struct schedstate_percpu *spc, *mspc = NULL;
    258 	struct cpu_info *mci;
    259 	struct lwp *l;
    260 	CPU_INFO_ITERATOR cii;
    261 	int s;
    262 
    263 	spc = &ci->ci_schedstate;
    264 	s = splsched();
    265 	spc->spc_flags |= SPCF_OFFLINE;
    266 	splx(s);
    267 
    268 	/* Take the first available CPU for the migration */
    269 	for (CPU_INFO_FOREACH(cii, mci)) {
    270 		mspc = &mci->ci_schedstate;
    271 		if ((mspc->spc_flags & SPCF_OFFLINE) == 0)
    272 			break;
    273 	}
    274 	KASSERT(mci != NULL);
    275 
    276 	/*
    277 	 * Migrate all non-bound threads to the other CPU.
    278 	 * Please note, that this runs from the xcall thread, thus handling
    279 	 * of LSONPROC is not needed.
    280 	 */
    281 	mutex_enter(&proclist_lock);
    282 
    283 	/*
    284 	 * Note that threads on the runqueue might sleep after this, but
    285 	 * sched_takecpu() would migrate such threads to the appropriate CPU.
    286 	 */
    287 	LIST_FOREACH(l, &alllwp, l_list) {
    288 		lwp_lock(l);
    289 		if (l->l_cpu == ci && (l->l_stat == LSSLEEP ||
    290 		    l->l_stat == LSSTOP || l->l_stat == LSSUSPENDED)) {
    291 			KASSERT((l->l_flag & LW_RUNNING) == 0);
    292 			l->l_cpu = mci;
    293 		}
    294 		lwp_unlock(l);
    295 	}
    296 
    297 	/* Double-lock the run-queues */
    298 	spc_dlock(ci, mci);
    299 
    300 	/* Handle LSRUN and LSIDL cases */
    301 	LIST_FOREACH(l, &alllwp, l_list) {
    302 		if (l->l_cpu != ci || (l->l_flag & LW_BOUND))
    303 			continue;
    304 		if (l->l_stat == LSRUN && (l->l_flag & LW_INMEM) != 0) {
    305 			sched_dequeue(l);
    306 			l->l_cpu = mci;
    307 			lwp_setlock(l, mspc->spc_mutex);
    308 			sched_enqueue(l, false);
    309 		} else if (l->l_stat == LSRUN || l->l_stat == LSIDL) {
    310 			l->l_cpu = mci;
    311 			lwp_setlock(l, mspc->spc_mutex);
    312 		}
    313 	}
    314 	spc_dunlock(ci, mci);
    315 	mutex_exit(&proclist_lock);
    316 
    317 #ifdef __HAVE_MD_CPU_OFFLINE
    318 	cpu_offline_md();
    319 #endif
    320 }
    321 
    322 static void
    323 cpu_xc_online(struct cpu_info *ci)
    324 {
    325 	struct schedstate_percpu *spc;
    326 	int s;
    327 
    328 	spc = &ci->ci_schedstate;
    329 	s = splsched();
    330 	spc->spc_flags &= ~SPCF_OFFLINE;
    331 	splx(s);
    332 }
    333 
    334 int
    335 cpu_setonline(struct cpu_info *ci, bool online)
    336 {
    337 	struct schedstate_percpu *spc;
    338 	CPU_INFO_ITERATOR cii;
    339 	struct cpu_info *ci2;
    340 	uint64_t where;
    341 	xcfunc_t func;
    342 	int nonline;
    343 
    344 	spc = &ci->ci_schedstate;
    345 
    346 	KASSERT(mutex_owned(&cpu_lock));
    347 
    348 	if (online) {
    349 		if ((spc->spc_flags & SPCF_OFFLINE) == 0)
    350 			return 0;
    351 		func = (xcfunc_t)cpu_xc_online;
    352 		ncpuonline++;
    353 	} else {
    354 		if ((spc->spc_flags & SPCF_OFFLINE) != 0)
    355 			return 0;
    356 		nonline = 0;
    357 		for (CPU_INFO_FOREACH(cii, ci2)) {
    358 			nonline += ((ci2->ci_schedstate.spc_flags &
    359 			    SPCF_OFFLINE) == 0);
    360 		}
    361 		if (nonline == 1)
    362 			return EBUSY;
    363 		func = (xcfunc_t)cpu_xc_offline;
    364 		ncpuonline--;
    365 	}
    366 
    367 	where = xc_unicast(0, func, ci, NULL, ci);
    368 	xc_wait(where);
    369 	if (online) {
    370 		KASSERT((spc->spc_flags & SPCF_OFFLINE) == 0);
    371 	} else {
    372 		KASSERT(spc->spc_flags & SPCF_OFFLINE);
    373 	}
    374 	spc->spc_lastmod = time_second;
    375 
    376 	return 0;
    377 }
    378