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