1 /* 2 * Copyright (c) 2001 Wind River Systems, Inc. 3 * All rights reserved. 4 * Written by: John Baldwin <jhb@FreeBSD.org> 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 4. Neither the name of the author nor the names of any co-contributors 15 * may be used to endorse or promote products derived from this software 16 * without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31 /* 32 * This module provides MI support for per-cpu data. 33 * 34 * Each architecture determines the mapping of logical CPU IDs to physical 35 * CPUs. The requirements of this mapping are as follows: 36 * - Logical CPU IDs must reside in the range 0 ... MAXCPU - 1. 37 * - The mapping is not required to be dense. That is, there may be 38 * gaps in the mappings. 39 * - The platform sets the value of MAXCPU in <machine/param.h>. 40 * - It is suggested, but not required, that in the non-SMP case, the 41 * platform define MAXCPU to be 1 and define the logical ID of the 42 * sole CPU as 0. 43 */ 44 45 #include <sys/cdefs.h> 46 __FBSDID("$FreeBSD$"); 47 48 #include "opt_ddb.h" 49 50 #include <sys/param.h> 51 #include <sys/systm.h> 52 #include <sys/linker_set.h> 53 #include <sys/lock.h> 54 #include <sys/pcpu.h> 55 #include <sys/proc.h> 56 #include <ddb/ddb.h> 57 58 static struct pcpu *cpuid_to_pcpu[MAXCPU]; 59 struct cpuhead cpuhead = SLIST_HEAD_INITIALIZER(cpuhead); 60 61 /* 62 * Initialize the MI portions of a struct pcpu. 63 */ 64 void 65 pcpu_init(struct pcpu *pcpu, int cpuid, size_t size) 66 { 67 68 bzero(pcpu, size); 69 KASSERT(cpuid >= 0 && cpuid < MAXCPU, 70 ("pcpu_init: invalid cpuid %d", cpuid)); 71 pcpu->pc_cpuid = cpuid; 72 pcpu->pc_cpumask = 1 << cpuid; 73 cpuid_to_pcpu[cpuid] = pcpu; 74 SLIST_INSERT_HEAD(&cpuhead, pcpu, pc_allcpu); 75 cpu_pcpu_init(pcpu, cpuid, size); 76 } 77 78 /* 79 * Destroy a struct pcpu. 80 */ 81 void 82 pcpu_destroy(struct pcpu *pcpu) 83 { 84 85 SLIST_REMOVE(&cpuhead, pcpu, pcpu, pc_allcpu); 86 cpuid_to_pcpu[pcpu->pc_cpuid] = NULL; 87 } 88 89 /* 90 * Locate a struct pcpu by cpu id. 91 */ 92 struct pcpu * 93 pcpu_find(u_int cpuid) 94 { 95 96 return (cpuid_to_pcpu[cpuid]); 97 } 98 99 #ifdef DDB 100 DB_SHOW_COMMAND(pcpu, db_show_pcpu) 101 { 102 struct pcpu *pc; 103 struct thread *td; 104 int id; 105 106 if (have_addr) 107 id = ((addr >> 4) % 16) * 10 + (addr % 16); 108 else 109 id = PCPU_GET(cpuid); 110 pc = pcpu_find(id); 111 if (pc == NULL) { 112 db_printf("CPU %d not found\n", id); 113 return; 114 } 115 db_printf("cpuid = %d\n", pc->pc_cpuid); 116 db_printf("curthread = "); 117 td = pc->pc_curthread; 118 if (td != NULL) 119 db_printf("%p: pid %d \"%s\"\n", td, td->td_proc->p_pid, 120 td->td_proc->p_comm); 121 else 122 db_printf("none\n"); 123 db_printf("curpcb = %p\n", pc->pc_curpcb); 124 db_printf("fpcurthread = "); 125 td = pc->pc_fpcurthread; 126 if (td != NULL) 127 db_printf("%p: pid %d \"%s\"\n", td, td->td_proc->p_pid, 128 td->td_proc->p_comm); 129 else 130 db_printf("none\n"); 131 db_printf("idlethread = "); 132 td = pc->pc_idlethread; 133 if (td != NULL) 134 db_printf("%p: pid %d \"%s\"\n", td, td->td_proc->p_pid, 135 td->td_proc->p_comm); 136 else 137 db_printf("none\n"); 138 db_show_mdpcpu(pc); 139 140 #ifdef WITNESS 141 db_printf("spin locks held:\n"); 142 witness_list_locks(&pc->pc_spinlocks); 143 #endif 144 } 145 #endif 146