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