Home | History | Annotate | Line # | Download | only in kern
kern_cpu.c revision 1.9
      1 /*	$NetBSD: kern_cpu.c,v 1.9 2007/10/15 14:12:55 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.9 2007/10/15 14:12:55 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 schedstate_percpu *);
     88 static void	cpu_xc_offline(struct schedstate_percpu *);
     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 	softint_init(ci);
    121 	xc_init_cpu(ci);
    122 	TAILQ_INIT(&ci->ci_data.cpu_biodone);
    123 	ncpu++;
    124 	ncpuonline++;
    125 
    126 	return 0;
    127 }
    128 
    129 void
    130 cpuctlattach(int dummy)
    131 {
    132 
    133 }
    134 
    135 int
    136 cpuctl_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
    137 {
    138 	CPU_INFO_ITERATOR cii;
    139 	cpustate_t *cs;
    140 	struct cpu_info *ci;
    141 	int error, i;
    142 	u_int id;
    143 
    144 	error = 0;
    145 
    146 	mutex_enter(&cpu_lock);
    147 	switch (cmd) {
    148 	case IOC_CPU_SETSTATE:
    149 		error = kauth_authorize_generic(l->l_cred,
    150 		    KAUTH_GENERIC_ISSUSER, NULL);
    151 		if (error != 0)
    152 			break;
    153 		cs = data;
    154 		if ((ci = cpu_lookup(cs->cs_id)) == NULL) {
    155 			error = ESRCH;
    156 			break;
    157 		}
    158 		if (!cs->cs_intr) {
    159 			error = EOPNOTSUPP;
    160 			break;
    161 		}
    162 		error = cpu_setonline(ci, cs->cs_online);
    163 		break;
    164 
    165 	case IOC_CPU_GETSTATE:
    166 		cs = data;
    167 		id = cs->cs_id;
    168 		memset(cs, sizeof(*cs), 0);
    169 		cs->cs_id = id;
    170 		if ((ci = cpu_lookup(id)) == NULL) {
    171 			error = ESRCH;
    172 			break;
    173 		}
    174 		if ((ci->ci_schedstate.spc_flags & SPCF_OFFLINE) != 0)
    175 			cs->cs_online = false;
    176 		else
    177 			cs->cs_online = true;
    178 		cs->cs_intr = true;
    179 		cs->cs_lastmod = ci->ci_schedstate.spc_lastmod;
    180 		break;
    181 
    182 	case IOC_CPU_MAPID:
    183 		i = 0;
    184 		for (CPU_INFO_FOREACH(cii, ci)) {
    185 			if (i++ == *(int *)data)
    186 				break;
    187 		}
    188 		if (ci == NULL)
    189 			error = ESRCH;
    190 		else
    191 			*(int *)data = ci->ci_cpuid;
    192 		break;
    193 
    194 	case IOC_CPU_GETCOUNT:
    195 		*(int *)data = ncpu;
    196 		break;
    197 
    198 	default:
    199 		error = ENOTTY;
    200 		break;
    201 	}
    202 	mutex_exit(&cpu_lock);
    203 
    204 	return error;
    205 }
    206 
    207 struct cpu_info *
    208 cpu_lookup(cpuid_t id)
    209 {
    210 	CPU_INFO_ITERATOR cii;
    211 	struct cpu_info *ci;
    212 
    213 	for (CPU_INFO_FOREACH(cii, ci)) {
    214 		if (ci->ci_cpuid == id)
    215 			return ci;
    216 	}
    217 
    218 	return NULL;
    219 }
    220 
    221 static void
    222 cpu_xc_offline(struct schedstate_percpu *spc)
    223 {
    224 	int s;
    225 
    226 	s = splsched();
    227 	spc->spc_flags |= SPCF_OFFLINE;
    228 	splx(s);
    229 }
    230 
    231 static void
    232 cpu_xc_online(struct schedstate_percpu *spc)
    233 {
    234 	int s;
    235 
    236 	s = splsched();
    237 	spc->spc_flags &= ~SPCF_OFFLINE;
    238 	splx(s);
    239 }
    240 
    241 int
    242 cpu_setonline(struct cpu_info *ci, bool online)
    243 {
    244 	struct schedstate_percpu *spc;
    245 	CPU_INFO_ITERATOR cii;
    246 	struct cpu_info *ci2;
    247 	uint64_t where;
    248 	xcfunc_t func;
    249 	int nonline;
    250 
    251 	spc = &ci->ci_schedstate;
    252 
    253 	KASSERT(mutex_owned(&cpu_lock));
    254 
    255 	if (online) {
    256 		if ((spc->spc_flags & SPCF_OFFLINE) == 0)
    257 			return 0;
    258 		func = (xcfunc_t)cpu_xc_online;
    259 		ncpuonline++;
    260 	} else {
    261 		if ((spc->spc_flags & SPCF_OFFLINE) != 0)
    262 			return 0;
    263 		nonline = 0;
    264 		for (CPU_INFO_FOREACH(cii, ci2)) {
    265 			nonline += ((ci2->ci_schedstate.spc_flags &
    266 			    SPCF_OFFLINE) == 0);
    267 		}
    268 		if (nonline == 1)
    269 			return EBUSY;
    270 		func = (xcfunc_t)cpu_xc_offline;
    271 		ncpuonline--;
    272 	}
    273 
    274 	where = xc_unicast(0, func, &ci->ci_schedstate, NULL, ci);
    275 	xc_wait(where);
    276 	spc->spc_lastmod = time_second;
    277 
    278 	return 0;
    279 }
    280