Home | History | Annotate | Line # | Download | only in kern
kern_cpu.c revision 1.2.10.3
      1  1.2.10.3  jmcneill /*	$NetBSD: kern_cpu.c,v 1.2.10.3 2007/09/03 16:48:47 jmcneill Exp $	*/
      2  1.2.10.1  jmcneill 
      3  1.2.10.1  jmcneill /*-
      4  1.2.10.1  jmcneill  * Copyright (c) 2007 The NetBSD Foundation, Inc.
      5  1.2.10.1  jmcneill  * All rights reserved.
      6  1.2.10.1  jmcneill  *
      7  1.2.10.1  jmcneill  * This code is derived from software contributed to The NetBSD Foundation
      8  1.2.10.1  jmcneill  * by Andrew Doran.
      9  1.2.10.1  jmcneill  *
     10  1.2.10.1  jmcneill  * Redistribution and use in source and binary forms, with or without
     11  1.2.10.1  jmcneill  * modification, are permitted provided that the following conditions
     12  1.2.10.1  jmcneill  * are met:
     13  1.2.10.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     14  1.2.10.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     15  1.2.10.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.2.10.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     17  1.2.10.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     18  1.2.10.1  jmcneill  * 3. All advertising materials mentioning features or use of this software
     19  1.2.10.1  jmcneill  *    must display the following acknowledgement:
     20  1.2.10.1  jmcneill  *        This product includes software developed by the NetBSD
     21  1.2.10.1  jmcneill  *        Foundation, Inc. and its contributors.
     22  1.2.10.1  jmcneill  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.2.10.1  jmcneill  *    contributors may be used to endorse or promote products derived
     24  1.2.10.1  jmcneill  *    from this software without specific prior written permission.
     25  1.2.10.1  jmcneill  *
     26  1.2.10.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.2.10.1  jmcneill  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.2.10.1  jmcneill  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.2.10.1  jmcneill  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.2.10.1  jmcneill  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.2.10.1  jmcneill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.2.10.1  jmcneill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.2.10.1  jmcneill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.2.10.1  jmcneill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.2.10.1  jmcneill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.2.10.1  jmcneill  * POSSIBILITY OF SUCH DAMAGE.
     37  1.2.10.1  jmcneill  */
     38       1.2      yamt 
     39       1.2      yamt /*-
     40       1.2      yamt  * Copyright (c)2007 YAMAMOTO Takashi,
     41       1.2      yamt  * All rights reserved.
     42       1.2      yamt  *
     43       1.2      yamt  * Redistribution and use in source and binary forms, with or without
     44       1.2      yamt  * modification, are permitted provided that the following conditions
     45       1.2      yamt  * are met:
     46       1.2      yamt  * 1. Redistributions of source code must retain the above copyright
     47       1.2      yamt  *    notice, this list of conditions and the following disclaimer.
     48       1.2      yamt  * 2. Redistributions in binary form must reproduce the above copyright
     49       1.2      yamt  *    notice, this list of conditions and the following disclaimer in the
     50       1.2      yamt  *    documentation and/or other materials provided with the distribution.
     51       1.2      yamt  *
     52       1.2      yamt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     53       1.2      yamt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     54       1.2      yamt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     55       1.2      yamt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     56       1.2      yamt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     57       1.2      yamt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     58       1.2      yamt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     59       1.2      yamt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     60       1.2      yamt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     61       1.2      yamt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     62       1.2      yamt  * SUCH DAMAGE.
     63       1.2      yamt  */
     64       1.2      yamt 
     65       1.2      yamt #include <sys/cdefs.h>
     66       1.2      yamt 
     67  1.2.10.3  jmcneill __KERNEL_RCSID(0, "$NetBSD: kern_cpu.c,v 1.2.10.3 2007/09/03 16:48:47 jmcneill Exp $");
     68       1.2      yamt 
     69       1.2      yamt #include <sys/param.h>
     70       1.2      yamt #include <sys/systm.h>
     71       1.2      yamt #include <sys/idle.h>
     72       1.2      yamt #include <sys/sched.h>
     73  1.2.10.1  jmcneill #include <sys/conf.h>
     74  1.2.10.1  jmcneill #include <sys/cpu.h>
     75  1.2.10.1  jmcneill #include <sys/cpuio.h>
     76  1.2.10.1  jmcneill #include <sys/proc.h>
     77  1.2.10.1  jmcneill #include <sys/kernel.h>
     78  1.2.10.1  jmcneill #include <sys/kauth.h>
     79  1.2.10.1  jmcneill 
     80  1.2.10.3  jmcneill #include <uvm/uvm_extern.h>
     81  1.2.10.3  jmcneill 
     82  1.2.10.1  jmcneill void	cpuctlattach(int);
     83  1.2.10.1  jmcneill 
     84  1.2.10.1  jmcneill dev_type_ioctl(cpuctl_ioctl);
     85  1.2.10.1  jmcneill 
     86  1.2.10.1  jmcneill const struct cdevsw cpuctl_cdevsw = {
     87  1.2.10.1  jmcneill 	nullopen, nullclose, nullread, nullwrite, cpuctl_ioctl,
     88  1.2.10.1  jmcneill 	nullstop, notty, nopoll, nommap, nokqfilter,
     89  1.2.10.1  jmcneill 	D_OTHER | D_MPSAFE
     90  1.2.10.1  jmcneill };
     91       1.2      yamt 
     92  1.2.10.1  jmcneill kmutex_t cpu_lock;
     93       1.2      yamt 
     94       1.2      yamt int
     95       1.2      yamt mi_cpu_attach(struct cpu_info *ci)
     96       1.2      yamt {
     97       1.2      yamt 	struct schedstate_percpu *spc = &ci->ci_schedstate;
     98       1.2      yamt 	int error;
     99       1.2      yamt 
    100  1.2.10.2  jmcneill 	ci->ci_index = ncpu;
    101  1.2.10.2  jmcneill 
    102       1.2      yamt 	mutex_init(&spc->spc_lwplock, MUTEX_SPIN, IPL_SCHED);
    103       1.2      yamt 	sched_cpuattach(ci);
    104  1.2.10.3  jmcneill 	uvm_cpu_attach(ci);
    105       1.2      yamt 
    106       1.2      yamt 	error = create_idle_lwp(ci);
    107       1.2      yamt 	if (error != 0) {
    108       1.2      yamt 		/* XXX revert sched_cpuattach */
    109       1.2      yamt 		return error;
    110       1.2      yamt 	}
    111       1.2      yamt 
    112       1.2      yamt 	ncpu++;
    113       1.2      yamt 
    114       1.2      yamt 	return 0;
    115       1.2      yamt }
    116  1.2.10.1  jmcneill 
    117  1.2.10.1  jmcneill void
    118  1.2.10.1  jmcneill cpuctlattach(int dummy)
    119  1.2.10.1  jmcneill {
    120  1.2.10.1  jmcneill 
    121  1.2.10.1  jmcneill }
    122  1.2.10.1  jmcneill 
    123  1.2.10.1  jmcneill int
    124  1.2.10.1  jmcneill cpuctl_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
    125  1.2.10.1  jmcneill {
    126  1.2.10.1  jmcneill 	CPU_INFO_ITERATOR cii;
    127  1.2.10.1  jmcneill 	cpustate_t *cs;
    128  1.2.10.1  jmcneill 	struct cpu_info *ci;
    129  1.2.10.1  jmcneill 	int error, i;
    130  1.2.10.1  jmcneill 	u_int id;
    131  1.2.10.1  jmcneill 
    132  1.2.10.1  jmcneill 	error = 0;
    133  1.2.10.1  jmcneill 
    134  1.2.10.1  jmcneill 	mutex_enter(&cpu_lock);
    135  1.2.10.1  jmcneill 	switch (cmd) {
    136  1.2.10.1  jmcneill 	case IOC_CPU_SETSTATE:
    137  1.2.10.1  jmcneill 		error = kauth_authorize_generic(l->l_cred,
    138  1.2.10.1  jmcneill 		    KAUTH_GENERIC_ISSUSER, NULL);
    139  1.2.10.1  jmcneill 		if (error != 0)
    140  1.2.10.1  jmcneill 			break;
    141  1.2.10.1  jmcneill 		cs = data;
    142  1.2.10.1  jmcneill 		if ((ci = cpu_lookup(cs->cs_id)) == NULL) {
    143  1.2.10.1  jmcneill 			error = ESRCH;
    144  1.2.10.1  jmcneill 			break;
    145  1.2.10.1  jmcneill 		}
    146  1.2.10.1  jmcneill 		if (!cs->cs_intr) {
    147  1.2.10.1  jmcneill 			error = EOPNOTSUPP;
    148  1.2.10.1  jmcneill 			break;
    149  1.2.10.1  jmcneill 		}
    150  1.2.10.1  jmcneill 		error = cpu_setonline(ci, cs->cs_online);
    151  1.2.10.1  jmcneill 		break;
    152  1.2.10.1  jmcneill 
    153  1.2.10.1  jmcneill 	case IOC_CPU_GETSTATE:
    154  1.2.10.1  jmcneill 		cs = data;
    155  1.2.10.1  jmcneill 		id = cs->cs_id;
    156  1.2.10.1  jmcneill 		memset(cs, sizeof(*cs), 0);
    157  1.2.10.1  jmcneill 		cs->cs_id = id;
    158  1.2.10.1  jmcneill 		if ((ci = cpu_lookup(id)) == NULL) {
    159  1.2.10.1  jmcneill 			error = ESRCH;
    160  1.2.10.1  jmcneill 			break;
    161  1.2.10.1  jmcneill 		}
    162  1.2.10.1  jmcneill 		if ((ci->ci_schedstate.spc_flags & SPCF_OFFLINE) != 0)
    163  1.2.10.1  jmcneill 			cs->cs_online = false;
    164  1.2.10.1  jmcneill 		else
    165  1.2.10.1  jmcneill 			cs->cs_online = true;
    166  1.2.10.1  jmcneill 		cs->cs_intr = true;
    167  1.2.10.1  jmcneill 		cs->cs_lastmod = ci->ci_schedstate.spc_lastmod;
    168  1.2.10.1  jmcneill 		break;
    169  1.2.10.1  jmcneill 
    170  1.2.10.1  jmcneill 	case IOC_CPU_MAPID:
    171  1.2.10.1  jmcneill 		i = 0;
    172  1.2.10.1  jmcneill 		for (CPU_INFO_FOREACH(cii, ci)) {
    173  1.2.10.1  jmcneill 			if (i++ == *(int *)data)
    174  1.2.10.1  jmcneill 				break;
    175  1.2.10.1  jmcneill 		}
    176  1.2.10.1  jmcneill 		if (ci == NULL)
    177  1.2.10.1  jmcneill 			error = ESRCH;
    178  1.2.10.1  jmcneill 		else
    179  1.2.10.1  jmcneill 			*(int *)data = ci->ci_cpuid;
    180  1.2.10.1  jmcneill 		break;
    181  1.2.10.1  jmcneill 
    182  1.2.10.1  jmcneill 	case IOC_CPU_GETCOUNT:
    183  1.2.10.1  jmcneill 		*(int *)data = ncpu;
    184  1.2.10.1  jmcneill 		break;
    185  1.2.10.1  jmcneill 
    186  1.2.10.1  jmcneill 	default:
    187  1.2.10.1  jmcneill 		error = ENOTTY;
    188  1.2.10.1  jmcneill 		break;
    189  1.2.10.1  jmcneill 	}
    190  1.2.10.1  jmcneill 	mutex_exit(&cpu_lock);
    191  1.2.10.1  jmcneill 
    192  1.2.10.1  jmcneill 	return error;
    193  1.2.10.1  jmcneill }
    194  1.2.10.1  jmcneill 
    195  1.2.10.1  jmcneill struct cpu_info *
    196  1.2.10.1  jmcneill cpu_lookup(cpuid_t id)
    197  1.2.10.1  jmcneill {
    198  1.2.10.1  jmcneill 	CPU_INFO_ITERATOR cii;
    199  1.2.10.1  jmcneill 	struct cpu_info *ci;
    200  1.2.10.1  jmcneill 
    201  1.2.10.1  jmcneill 	for (CPU_INFO_FOREACH(cii, ci)) {
    202  1.2.10.1  jmcneill 		if (ci->ci_cpuid == id)
    203  1.2.10.1  jmcneill 			return ci;
    204  1.2.10.1  jmcneill 	}
    205  1.2.10.1  jmcneill 
    206  1.2.10.1  jmcneill 	return NULL;
    207  1.2.10.1  jmcneill }
    208  1.2.10.1  jmcneill 
    209  1.2.10.1  jmcneill int
    210  1.2.10.1  jmcneill cpu_setonline(struct cpu_info *ci, bool online)
    211  1.2.10.1  jmcneill {
    212  1.2.10.1  jmcneill 	struct schedstate_percpu *spc;
    213  1.2.10.1  jmcneill 	CPU_INFO_ITERATOR cii;
    214  1.2.10.1  jmcneill 	struct cpu_info *ci2;
    215  1.2.10.1  jmcneill 	int nonline;
    216  1.2.10.1  jmcneill 
    217  1.2.10.1  jmcneill 	spc = &ci->ci_schedstate;
    218  1.2.10.1  jmcneill 
    219  1.2.10.1  jmcneill 	KASSERT(mutex_owned(&cpu_lock));
    220  1.2.10.1  jmcneill 
    221  1.2.10.1  jmcneill 	if (online) {
    222  1.2.10.1  jmcneill 		if ((spc->spc_flags & SPCF_OFFLINE) == 0)
    223  1.2.10.1  jmcneill 			return 0;
    224  1.2.10.1  jmcneill 		spc_lock(ci);
    225  1.2.10.1  jmcneill 		spc->spc_flags &= ~SPCF_OFFLINE;
    226  1.2.10.1  jmcneill 		cpu_need_resched(ci, true);
    227  1.2.10.1  jmcneill 		spc_unlock(ci);
    228  1.2.10.1  jmcneill 		spc->spc_lastmod = time_second;
    229  1.2.10.1  jmcneill 	} else {
    230  1.2.10.1  jmcneill 		if ((spc->spc_flags & SPCF_OFFLINE) != 0)
    231  1.2.10.1  jmcneill 			return 0;
    232  1.2.10.1  jmcneill 		nonline = 0;
    233  1.2.10.1  jmcneill 		for (CPU_INFO_FOREACH(cii, ci2)) {
    234  1.2.10.1  jmcneill 			nonline += ((ci2->ci_schedstate.spc_flags &
    235  1.2.10.1  jmcneill 			    SPCF_OFFLINE) == 0);
    236  1.2.10.1  jmcneill 		}
    237  1.2.10.1  jmcneill 		if (nonline == 1)
    238  1.2.10.1  jmcneill 			return EBUSY;
    239  1.2.10.1  jmcneill 		spc_lock(ci);
    240  1.2.10.1  jmcneill 		spc->spc_flags |= SPCF_OFFLINE;
    241  1.2.10.1  jmcneill 		cpu_need_resched(ci, true);
    242  1.2.10.1  jmcneill 		spc_unlock(ci);
    243  1.2.10.1  jmcneill 		do {
    244  1.2.10.1  jmcneill 			kpause("cpu", false, 1, NULL);
    245  1.2.10.1  jmcneill #ifdef MULTIPROCESOR
    246  1.2.10.1  jmcneill 		} while (ci->ci_curlwp != ci->ci_data.cpu_idlelwp);
    247  1.2.10.1  jmcneill #else
    248  1.2.10.1  jmcneill 		} while (0);
    249  1.2.10.1  jmcneill #endif
    250  1.2.10.1  jmcneill 		spc->spc_lastmod = time_second;
    251  1.2.10.1  jmcneill 	}
    252  1.2.10.1  jmcneill 
    253  1.2.10.1  jmcneill 	return 0;
    254  1.2.10.1  jmcneill }
    255