1 /*- 2 * Copyright (c) 2001 Wind River Systems, Inc. 3 * All rights reserved. 4 * Written by: John Baldwin <jhb@FreeBSD.org> 5 * 6 * Copyright (c) 2009 Jeffrey Roberson <jeff@freebsd.org> 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 4. Neither the name of the author nor the names of any co-contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 /* 35 * This module provides MI support for per-cpu data. 36 * 37 * Each architecture determines the mapping of logical CPU IDs to physical 38 * CPUs. The requirements of this mapping are as follows: 39 * - Logical CPU IDs must reside in the range 0 ... MAXCPU - 1. 40 * - The mapping is not required to be dense. That is, there may be 41 * gaps in the mappings. 42 * - The platform sets the value of MAXCPU in <machine/param.h>. 43 * - It is suggested, but not required, that in the non-SMP case, the 44 * platform define MAXCPU to be 1 and define the logical ID of the 45 * sole CPU as 0. 46 */ 47 48 #include <sys/cdefs.h> 49 __FBSDID("$FreeBSD$"); 50 51 #include "opt_ddb.h" 52 53 #include <sys/param.h> 54 #include <sys/systm.h> 55 #include <sys/sysctl.h> 56 #include <sys/lock.h> 57 #include <sys/malloc.h> 58 #include <sys/pcpu.h> 59 #include <sys/proc.h> 60 #include <sys/smp.h> 61 #include <sys/sx.h> 62 #include <ddb/ddb.h> 63 64 static MALLOC_DEFINE(M_PCPU, "Per-cpu", "Per-cpu resource accouting."); 65 66 struct dpcpu_free { 67 uintptr_t df_start; 68 int df_len; 69 TAILQ_ENTRY(dpcpu_free) df_link; 70 }; 71 72 static DPCPU_DEFINE(char, modspace[DPCPU_MODMIN]); 73 static TAILQ_HEAD(, dpcpu_free) dpcpu_head = TAILQ_HEAD_INITIALIZER(dpcpu_head); 74 static struct sx dpcpu_lock; 75 uintptr_t dpcpu_off[MAXCPU]; 76 struct pcpu *cpuid_to_pcpu[MAXCPU]; 77 struct cpuhead cpuhead = STAILQ_HEAD_INITIALIZER(cpuhead); 78 79 /* 80 * Initialize the MI portions of a struct pcpu. 81 */ 82 void 83 pcpu_init(struct pcpu *pcpu, int cpuid, size_t size) 84 { 85 86 bzero(pcpu, size); 87 KASSERT(cpuid >= 0 && cpuid < MAXCPU, 88 ("pcpu_init: invalid cpuid %d", cpuid)); 89 pcpu->pc_cpuid = cpuid; 90 cpuid_to_pcpu[cpuid] = pcpu; 91 STAILQ_INSERT_TAIL(&cpuhead, pcpu, pc_allcpu); 92 cpu_pcpu_init(pcpu, cpuid, size); 93 pcpu->pc_rm_queue.rmq_next = &pcpu->pc_rm_queue; 94 pcpu->pc_rm_queue.rmq_prev = &pcpu->pc_rm_queue; 95 } 96 97 void 98 dpcpu_init(void *dpcpu, int cpuid) 99 { 100 struct pcpu *pcpu; 101 102 pcpu = pcpu_find(cpuid); 103 pcpu->pc_dynamic = (uintptr_t)dpcpu - DPCPU_START; 104 105 /* 106 * Initialize defaults from our linker section. 107 */ 108 memcpy(dpcpu, (void *)DPCPU_START, DPCPU_BYTES); 109 110 /* 111 * Place it in the global pcpu offset array. 112 */ 113 dpcpu_off[cpuid] = pcpu->pc_dynamic; 114 } 115 116 static void 117 dpcpu_startup(void *dummy __unused) 118 { 119 struct dpcpu_free *df; 120 121 df = malloc(sizeof(*df), M_PCPU, M_WAITOK | M_ZERO); 122 df->df_start = (uintptr_t)&DPCPU_NAME(modspace); 123 df->df_len = DPCPU_MODMIN; 124 TAILQ_INSERT_HEAD(&dpcpu_head, df, df_link); 125 sx_init(&dpcpu_lock, "dpcpu alloc lock"); 126 } 127 SYSINIT(dpcpu, SI_SUB_KLD, SI_ORDER_FIRST, dpcpu_startup, 0); 128 129 /* 130 * First-fit extent based allocator for allocating space in the per-cpu 131 * region reserved for modules. This is only intended for use by the 132 * kernel linkers to place module linker sets. 133 */ 134 void * 135 dpcpu_alloc(int size) 136 { 137 struct dpcpu_free *df; 138 void *s; 139 140 s = NULL; 141 size = roundup2(size, sizeof(void *)); 142 sx_xlock(&dpcpu_lock); 143 TAILQ_FOREACH(df, &dpcpu_head, df_link) { 144 if (df->df_len < size) 145 continue; 146 if (df->df_len == size) { 147 s = (void *)df->df_start; 148 TAILQ_REMOVE(&dpcpu_head, df, df_link); 149 free(df, M_PCPU); 150 break; 151 } 152 s = (void *)df->df_start; 153 df->df_len -= size; 154 df->df_start = df->df_start + size; 155 break; 156 } 157 sx_xunlock(&dpcpu_lock); 158 159 return (s); 160 } 161 162 /* 163 * Free dynamic per-cpu space at module unload time. 164 */ 165 void 166 dpcpu_free(void *s, int size) 167 { 168 struct dpcpu_free *df; 169 struct dpcpu_free *dn; 170 uintptr_t start; 171 uintptr_t end; 172 173 size = roundup2(size, sizeof(void *)); 174 start = (uintptr_t)s; 175 end = start + size; 176 /* 177 * Free a region of space and merge it with as many neighbors as 178 * possible. Keeping the list sorted simplifies this operation. 179 */ 180 sx_xlock(&dpcpu_lock); 181 TAILQ_FOREACH(df, &dpcpu_head, df_link) { 182 if (df->df_start > end) 183 break; 184 /* 185 * If we expand at the end of an entry we may have to 186 * merge it with the one following it as well. 187 */ 188 if (df->df_start + df->df_len == start) { 189 df->df_len += size; 190 dn = TAILQ_NEXT(df, df_link); 191 if (df->df_start + df->df_len == dn->df_start) { 192 df->df_len += dn->df_len; 193 TAILQ_REMOVE(&dpcpu_head, dn, df_link); 194 free(dn, M_PCPU); 195 } 196 sx_xunlock(&dpcpu_lock); 197 return; 198 } 199 if (df->df_start == end) { 200 df->df_start = start; 201 df->df_len += size; 202 sx_xunlock(&dpcpu_lock); 203 return; 204 } 205 } 206 dn = malloc(sizeof(*df), M_PCPU, M_WAITOK | M_ZERO); 207 dn->df_start = start; 208 dn->df_len = size; 209 if (df) 210 TAILQ_INSERT_BEFORE(df, dn, df_link); 211 else 212 TAILQ_INSERT_TAIL(&dpcpu_head, dn, df_link); 213 sx_xunlock(&dpcpu_lock); 214 } 215 216 /* 217 * Initialize the per-cpu storage from an updated linker-set region. 218 */ 219 void 220 dpcpu_copy(void *s, int size) 221 { 222 #ifdef SMP 223 uintptr_t dpcpu; 224 int i; 225 226 for (i = 0; i < mp_ncpus; ++i) { 227 dpcpu = dpcpu_off[i]; 228 if (dpcpu == 0) 229 continue; 230 memcpy((void *)(dpcpu + (uintptr_t)s), s, size); 231 } 232 #else 233 memcpy((void *)(dpcpu_off[0] + (uintptr_t)s), s, size); 234 #endif 235 } 236 237 /* 238 * Destroy a struct pcpu. 239 */ 240 void 241 pcpu_destroy(struct pcpu *pcpu) 242 { 243 244 STAILQ_REMOVE(&cpuhead, pcpu, pcpu, pc_allcpu); 245 cpuid_to_pcpu[pcpu->pc_cpuid] = NULL; 246 dpcpu_off[pcpu->pc_cpuid] = 0; 247 } 248 249 /* 250 * Locate a struct pcpu by cpu id. 251 */ 252 struct pcpu * 253 pcpu_find(u_int cpuid) 254 { 255 256 return (cpuid_to_pcpu[cpuid]); 257 } 258 259 int 260 sysctl_dpcpu_quad(SYSCTL_HANDLER_ARGS) 261 { 262 uintptr_t dpcpu; 263 int64_t count; 264 int i; 265 266 count = 0; 267 for (i = 0; i < mp_ncpus; ++i) { 268 dpcpu = dpcpu_off[i]; 269 if (dpcpu == 0) 270 continue; 271 count += *(int64_t *)(dpcpu + (uintptr_t)arg1); 272 } 273 return (SYSCTL_OUT(req, &count, sizeof(count))); 274 } 275 276 int 277 sysctl_dpcpu_long(SYSCTL_HANDLER_ARGS) 278 { 279 uintptr_t dpcpu; 280 long count; 281 int i; 282 283 count = 0; 284 for (i = 0; i < mp_ncpus; ++i) { 285 dpcpu = dpcpu_off[i]; 286 if (dpcpu == 0) 287 continue; 288 count += *(long *)(dpcpu + (uintptr_t)arg1); 289 } 290 return (SYSCTL_OUT(req, &count, sizeof(count))); 291 } 292 293 int 294 sysctl_dpcpu_int(SYSCTL_HANDLER_ARGS) 295 { 296 uintptr_t dpcpu; 297 int count; 298 int i; 299 300 count = 0; 301 for (i = 0; i < mp_ncpus; ++i) { 302 dpcpu = dpcpu_off[i]; 303 if (dpcpu == 0) 304 continue; 305 count += *(int *)(dpcpu + (uintptr_t)arg1); 306 } 307 return (SYSCTL_OUT(req, &count, sizeof(count))); 308 } 309 310 #ifdef DDB 311 DB_SHOW_COMMAND(dpcpu_off, db_show_dpcpu_off) 312 { 313 int id; 314 315 CPU_FOREACH(id) { 316 db_printf("dpcpu_off[%2d] = 0x%jx (+ DPCPU_START = %p)\n", 317 id, (uintmax_t)dpcpu_off[id], 318 (void *)(uintptr_t)(dpcpu_off[id] + DPCPU_START)); 319 } 320 } 321 322 static void 323 show_pcpu(struct pcpu *pc) 324 { 325 struct thread *td; 326 327 db_printf("cpuid = %d\n", pc->pc_cpuid); 328 db_printf("dynamic pcpu = %p\n", (void *)pc->pc_dynamic); 329 db_printf("curthread = "); 330 td = pc->pc_curthread; 331 if (td != NULL) 332 db_printf("%p: pid %d \"%s\"\n", td, td->td_proc->p_pid, 333 td->td_name); 334 else 335 db_printf("none\n"); 336 db_printf("curpcb = %p\n", pc->pc_curpcb); 337 db_printf("fpcurthread = "); 338 td = pc->pc_fpcurthread; 339 if (td != NULL) 340 db_printf("%p: pid %d \"%s\"\n", td, td->td_proc->p_pid, 341 td->td_name); 342 else 343 db_printf("none\n"); 344 db_printf("idlethread = "); 345 td = pc->pc_idlethread; 346 if (td != NULL) 347 db_printf("%p: tid %d \"%s\"\n", td, td->td_tid, td->td_name); 348 else 349 db_printf("none\n"); 350 db_show_mdpcpu(pc); 351 352 #ifdef VIMAGE 353 db_printf("curvnet = %p\n", pc->pc_curthread->td_vnet); 354 #endif 355 356 #ifdef WITNESS 357 db_printf("spin locks held:\n"); 358 witness_list_locks(&pc->pc_spinlocks, db_printf); 359 #endif 360 } 361 362 DB_SHOW_COMMAND(pcpu, db_show_pcpu) 363 { 364 struct pcpu *pc; 365 int id; 366 367 if (have_addr) 368 id = ((addr >> 4) % 16) * 10 + (addr % 16); 369 else 370 id = PCPU_GET(cpuid); 371 pc = pcpu_find(id); 372 if (pc == NULL) { 373 db_printf("CPU %d not found\n", id); 374 return; 375 } 376 show_pcpu(pc); 377 } 378 379 DB_SHOW_ALL_COMMAND(pcpu, db_show_cpu_all) 380 { 381 struct pcpu *pc; 382 int id; 383 384 db_printf("Current CPU: %d\n\n", PCPU_GET(cpuid)); 385 for (id = 0; id <= mp_maxid; id++) { 386 pc = pcpu_find(id); 387 if (pc != NULL) { 388 show_pcpu(pc); 389 db_printf("\n"); 390 } 391 } 392 } 393 DB_SHOW_ALIAS(allpcpu, db_show_cpu_all); 394 #endif 395