2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2001 Wind River Systems, Inc.
6 * Written by: John Baldwin <jhb@FreeBSD.org>
8 * Copyright (c) 2009 Jeffrey Roberson <jeff@freebsd.org>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the author nor the names of any co-contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * This module provides MI support for per-cpu data.
39 * Each architecture determines the mapping of logical CPU IDs to physical
40 * CPUs. The requirements of this mapping are as follows:
41 * - Logical CPU IDs must reside in the range 0 ... MAXCPU - 1.
42 * - The mapping is not required to be dense. That is, there may be
43 * gaps in the mappings.
44 * - The platform sets the value of MAXCPU in <machine/param.h>.
45 * - It is suggested, but not required, that in the non-SMP case, the
46 * platform define MAXCPU to be 1 and define the logical ID of the
50 #include <sys/cdefs.h>
51 __FBSDID("$FreeBSD$");
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/sysctl.h>
59 #include <sys/malloc.h>
67 static MALLOC_DEFINE(M_PCPU, "Per-cpu", "Per-cpu resource accouting.");
72 TAILQ_ENTRY(dpcpu_free) df_link;
75 DPCPU_DEFINE_STATIC(char, modspace[DPCPU_MODMIN] __aligned(__alignof(void *)));
76 static TAILQ_HEAD(, dpcpu_free) dpcpu_head = TAILQ_HEAD_INITIALIZER(dpcpu_head);
77 static struct sx dpcpu_lock;
78 uintptr_t dpcpu_off[MAXCPU];
79 struct pcpu *cpuid_to_pcpu[MAXCPU];
80 struct cpuhead cpuhead = STAILQ_HEAD_INITIALIZER(cpuhead);
83 * Initialize the MI portions of a struct pcpu.
86 pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
90 KASSERT(cpuid >= 0 && cpuid < MAXCPU,
91 ("pcpu_init: invalid cpuid %d", cpuid));
92 pcpu->pc_cpuid = cpuid;
93 cpuid_to_pcpu[cpuid] = pcpu;
94 STAILQ_INSERT_TAIL(&cpuhead, pcpu, pc_allcpu);
95 cpu_pcpu_init(pcpu, cpuid, size);
96 pcpu->pc_rm_queue.rmq_next = &pcpu->pc_rm_queue;
97 pcpu->pc_rm_queue.rmq_prev = &pcpu->pc_rm_queue;
98 pcpu->pc_zpcpu_offset = zpcpu_offset_cpu(cpuid);
102 dpcpu_init(void *dpcpu, int cpuid)
106 pcpu = pcpu_find(cpuid);
107 pcpu->pc_dynamic = (uintptr_t)dpcpu - DPCPU_START;
110 * Initialize defaults from our linker section.
112 memcpy(dpcpu, (void *)DPCPU_START, DPCPU_BYTES);
115 * Place it in the global pcpu offset array.
117 dpcpu_off[cpuid] = pcpu->pc_dynamic;
121 dpcpu_startup(void *dummy __unused)
123 struct dpcpu_free *df;
125 df = malloc(sizeof(*df), M_PCPU, M_WAITOK | M_ZERO);
126 df->df_start = (uintptr_t)&DPCPU_NAME(modspace);
127 df->df_len = DPCPU_MODMIN;
128 TAILQ_INSERT_HEAD(&dpcpu_head, df, df_link);
129 sx_init(&dpcpu_lock, "dpcpu alloc lock");
131 SYSINIT(dpcpu, SI_SUB_KLD, SI_ORDER_FIRST, dpcpu_startup, NULL);
134 * UMA_PCPU_ZONE zones, that are available for all kernel
135 * consumers. Right now 64 bit zone is used for counter(9)
136 * and int zone is used for mount point counters.
139 uma_zone_t pcpu_zone_int;
140 uma_zone_t pcpu_zone_64;
143 pcpu_zones_startup(void)
146 pcpu_zone_int = uma_zcreate("int pcpu", sizeof(int),
147 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_PCPU);
148 pcpu_zone_64 = uma_zcreate("64 pcpu", sizeof(uint64_t),
149 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_PCPU);
151 SYSINIT(pcpu_zones, SI_SUB_COUNTER, SI_ORDER_FIRST, pcpu_zones_startup, NULL);
154 * First-fit extent based allocator for allocating space in the per-cpu
155 * region reserved for modules. This is only intended for use by the
156 * kernel linkers to place module linker sets.
159 dpcpu_alloc(int size)
161 struct dpcpu_free *df;
165 size = roundup2(size, sizeof(void *));
166 sx_xlock(&dpcpu_lock);
167 TAILQ_FOREACH(df, &dpcpu_head, df_link) {
168 if (df->df_len < size)
170 if (df->df_len == size) {
171 s = (void *)df->df_start;
172 TAILQ_REMOVE(&dpcpu_head, df, df_link);
176 s = (void *)df->df_start;
178 df->df_start = df->df_start + size;
181 sx_xunlock(&dpcpu_lock);
187 * Free dynamic per-cpu space at module unload time.
190 dpcpu_free(void *s, int size)
192 struct dpcpu_free *df;
193 struct dpcpu_free *dn;
197 size = roundup2(size, sizeof(void *));
198 start = (uintptr_t)s;
201 * Free a region of space and merge it with as many neighbors as
202 * possible. Keeping the list sorted simplifies this operation.
204 sx_xlock(&dpcpu_lock);
205 TAILQ_FOREACH(df, &dpcpu_head, df_link) {
206 if (df->df_start > end)
209 * If we expand at the end of an entry we may have to
210 * merge it with the one following it as well.
212 if (df->df_start + df->df_len == start) {
214 dn = TAILQ_NEXT(df, df_link);
215 if (df->df_start + df->df_len == dn->df_start) {
216 df->df_len += dn->df_len;
217 TAILQ_REMOVE(&dpcpu_head, dn, df_link);
220 sx_xunlock(&dpcpu_lock);
223 if (df->df_start == end) {
224 df->df_start = start;
226 sx_xunlock(&dpcpu_lock);
230 dn = malloc(sizeof(*df), M_PCPU, M_WAITOK | M_ZERO);
231 dn->df_start = start;
234 TAILQ_INSERT_BEFORE(df, dn, df_link);
236 TAILQ_INSERT_TAIL(&dpcpu_head, dn, df_link);
237 sx_xunlock(&dpcpu_lock);
241 * Initialize the per-cpu storage from an updated linker-set region.
244 dpcpu_copy(void *s, int size)
251 dpcpu = dpcpu_off[i];
254 memcpy((void *)(dpcpu + (uintptr_t)s), s, size);
257 memcpy((void *)(dpcpu_off[0] + (uintptr_t)s), s, size);
262 * Destroy a struct pcpu.
265 pcpu_destroy(struct pcpu *pcpu)
268 STAILQ_REMOVE(&cpuhead, pcpu, pcpu, pc_allcpu);
269 cpuid_to_pcpu[pcpu->pc_cpuid] = NULL;
270 dpcpu_off[pcpu->pc_cpuid] = 0;
274 * Locate a struct pcpu by cpu id.
277 pcpu_find(u_int cpuid)
280 return (cpuid_to_pcpu[cpuid]);
284 sysctl_dpcpu_quad(SYSCTL_HANDLER_ARGS)
292 dpcpu = dpcpu_off[i];
295 count += *(int64_t *)(dpcpu + (uintptr_t)arg1);
297 return (SYSCTL_OUT(req, &count, sizeof(count)));
301 sysctl_dpcpu_long(SYSCTL_HANDLER_ARGS)
309 dpcpu = dpcpu_off[i];
312 count += *(long *)(dpcpu + (uintptr_t)arg1);
314 return (SYSCTL_OUT(req, &count, sizeof(count)));
318 sysctl_dpcpu_int(SYSCTL_HANDLER_ARGS)
326 dpcpu = dpcpu_off[i];
329 count += *(int *)(dpcpu + (uintptr_t)arg1);
331 return (SYSCTL_OUT(req, &count, sizeof(count)));
335 DB_SHOW_COMMAND(dpcpu_off, db_show_dpcpu_off)
340 db_printf("dpcpu_off[%2d] = 0x%jx (+ DPCPU_START = %p)\n",
341 id, (uintmax_t)dpcpu_off[id],
342 (void *)(uintptr_t)(dpcpu_off[id] + DPCPU_START));
347 show_pcpu(struct pcpu *pc)
351 db_printf("cpuid = %d\n", pc->pc_cpuid);
352 db_printf("dynamic pcpu = %p\n", (void *)pc->pc_dynamic);
353 db_printf("curthread = ");
354 td = pc->pc_curthread;
356 db_printf("%p: pid %d tid %d critnest %d \"%s\"\n", td,
357 td->td_proc->p_pid, td->td_tid, td->td_critnest,
361 db_printf("curpcb = %p\n", pc->pc_curpcb);
362 db_printf("fpcurthread = ");
363 td = pc->pc_fpcurthread;
365 db_printf("%p: pid %d \"%s\"\n", td, td->td_proc->p_pid,
369 db_printf("idlethread = ");
370 td = pc->pc_idlethread;
372 db_printf("%p: tid %d \"%s\"\n", td, td->td_tid, td->td_name);
378 db_printf("curvnet = %p\n", pc->pc_curthread->td_vnet);
382 db_printf("spin locks held:\n");
383 witness_list_locks(&pc->pc_spinlocks, db_printf);
387 DB_SHOW_COMMAND(pcpu, db_show_pcpu)
393 id = ((addr >> 4) % 16) * 10 + (addr % 16);
395 id = PCPU_GET(cpuid);
398 db_printf("CPU %d not found\n", id);
404 DB_SHOW_ALL_COMMAND(pcpu, db_show_cpu_all)
409 db_printf("Current CPU: %d\n\n", PCPU_GET(cpuid));
418 DB_SHOW_ALIAS(allpcpu, db_show_cpu_all);