xref: /freebsd/sys/kern/subr_pcpu.c (revision 685dc743dc3b5645e34836464128e1c0558b404b)
19454b2d8SWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4ba228f6dSJohn Baldwin  * Copyright (c) 2001 Wind River Systems, Inc.
5ba228f6dSJohn Baldwin  * All rights reserved.
6ba228f6dSJohn Baldwin  * Written by: John Baldwin <jhb@FreeBSD.org>
7ba228f6dSJohn Baldwin  *
850c202c5SJeff Roberson  * Copyright (c) 2009 Jeffrey Roberson <jeff@freebsd.org>
950c202c5SJeff Roberson  * All rights reserved.
1050c202c5SJeff Roberson  *
11ba228f6dSJohn Baldwin  * Redistribution and use in source and binary forms, with or without
12ba228f6dSJohn Baldwin  * modification, are permitted provided that the following conditions
13ba228f6dSJohn Baldwin  * are met:
14ba228f6dSJohn Baldwin  * 1. Redistributions of source code must retain the above copyright
15ba228f6dSJohn Baldwin  *    notice, this list of conditions and the following disclaimer.
16ba228f6dSJohn Baldwin  * 2. Redistributions in binary form must reproduce the above copyright
17ba228f6dSJohn Baldwin  *    notice, this list of conditions and the following disclaimer in the
18ba228f6dSJohn Baldwin  *    documentation and/or other materials provided with the distribution.
1969a28758SEd Maste  * 3. Neither the name of the author nor the names of any co-contributors
20ba228f6dSJohn Baldwin  *    may be used to endorse or promote products derived from this software
21ba228f6dSJohn Baldwin  *    without specific prior written permission.
22ba228f6dSJohn Baldwin  *
23ba228f6dSJohn Baldwin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24ba228f6dSJohn Baldwin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ba228f6dSJohn Baldwin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ba228f6dSJohn Baldwin  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27ba228f6dSJohn Baldwin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ba228f6dSJohn Baldwin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ba228f6dSJohn Baldwin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ba228f6dSJohn Baldwin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ba228f6dSJohn Baldwin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ba228f6dSJohn Baldwin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ba228f6dSJohn Baldwin  * SUCH DAMAGE.
34ba228f6dSJohn Baldwin  */
35ba228f6dSJohn Baldwin 
36ba228f6dSJohn Baldwin /*
37ba228f6dSJohn Baldwin  * This module provides MI support for per-cpu data.
38882bcf58SJohn Baldwin  *
39882bcf58SJohn Baldwin  * Each architecture determines the mapping of logical CPU IDs to physical
40882bcf58SJohn Baldwin  * CPUs.  The requirements of this mapping are as follows:
41882bcf58SJohn Baldwin  *  - Logical CPU IDs must reside in the range 0 ... MAXCPU - 1.
42882bcf58SJohn Baldwin  *  - The mapping is not required to be dense.  That is, there may be
43882bcf58SJohn Baldwin  *    gaps in the mappings.
44882bcf58SJohn Baldwin  *  - The platform sets the value of MAXCPU in <machine/param.h>.
45882bcf58SJohn Baldwin  *  - It is suggested, but not required, that in the non-SMP case, the
46882bcf58SJohn Baldwin  *    platform define MAXCPU to be 1 and define the logical ID of the
47882bcf58SJohn Baldwin  *    sole CPU as 0.
48ba228f6dSJohn Baldwin  */
49ba228f6dSJohn Baldwin 
50677b542eSDavid E. O'Brien #include <sys/cdefs.h>
510bbc8826SJohn Baldwin #include "opt_ddb.h"
520bbc8826SJohn Baldwin 
53ba228f6dSJohn Baldwin #include <sys/param.h>
54ba228f6dSJohn Baldwin #include <sys/systm.h>
5550c202c5SJeff Roberson #include <sys/sysctl.h>
560bbc8826SJohn Baldwin #include <sys/lock.h>
5750c202c5SJeff Roberson #include <sys/malloc.h>
58ba228f6dSJohn Baldwin #include <sys/pcpu.h>
590bbc8826SJohn Baldwin #include <sys/proc.h>
609b791585SRobert Watson #include <sys/smp.h>
6150c202c5SJeff Roberson #include <sys/sx.h>
6249fef6a2SGleb Smirnoff #include <vm/uma.h>
630bbc8826SJohn Baldwin #include <ddb/ddb.h>
64ba228f6dSJohn Baldwin 
65d745c852SEd Schouten static MALLOC_DEFINE(M_PCPU, "Per-cpu", "Per-cpu resource accouting.");
6650c202c5SJeff Roberson 
6750c202c5SJeff Roberson struct dpcpu_free {
6850c202c5SJeff Roberson 	uintptr_t	df_start;
6950c202c5SJeff Roberson 	int		df_len;
7050c202c5SJeff Roberson 	TAILQ_ENTRY(dpcpu_free) df_link;
7150c202c5SJeff Roberson };
7250c202c5SJeff Roberson 
73cd2106eaSAndrew Turner DPCPU_DEFINE_STATIC(char, modspace[DPCPU_MODMIN] __aligned(__alignof(void *)));
7450c202c5SJeff Roberson static TAILQ_HEAD(, dpcpu_free) dpcpu_head = TAILQ_HEAD_INITIALIZER(dpcpu_head);
7550c202c5SJeff Roberson static struct sx dpcpu_lock;
7650c202c5SJeff Roberson uintptr_t dpcpu_off[MAXCPU];
77f53d15feSStephan Uphoff struct pcpu *cpuid_to_pcpu[MAXCPU];
78d098f930SNathan Whitehorn struct cpuhead cpuhead = STAILQ_HEAD_INITIALIZER(cpuhead);
79ba228f6dSJohn Baldwin 
80ba228f6dSJohn Baldwin /*
810bbc8826SJohn Baldwin  * Initialize the MI portions of a struct pcpu.
82ba228f6dSJohn Baldwin  */
83ba228f6dSJohn Baldwin void
pcpu_init(struct pcpu * pcpu,int cpuid,size_t size)840bbc8826SJohn Baldwin pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
85ba228f6dSJohn Baldwin {
86ba228f6dSJohn Baldwin 
870bbc8826SJohn Baldwin 	bzero(pcpu, size);
880bbc8826SJohn Baldwin 	KASSERT(cpuid >= 0 && cpuid < MAXCPU,
890bbc8826SJohn Baldwin 	    ("pcpu_init: invalid cpuid %d", cpuid));
900bbc8826SJohn Baldwin 	pcpu->pc_cpuid = cpuid;
910bbc8826SJohn Baldwin 	cpuid_to_pcpu[cpuid] = pcpu;
92d098f930SNathan Whitehorn 	STAILQ_INSERT_TAIL(&cpuhead, pcpu, pc_allcpu);
930bbc8826SJohn Baldwin 	cpu_pcpu_init(pcpu, cpuid, size);
94f53d15feSStephan Uphoff 	pcpu->pc_rm_queue.rmq_next = &pcpu->pc_rm_queue;
95f53d15feSStephan Uphoff 	pcpu->pc_rm_queue.rmq_prev = &pcpu->pc_rm_queue;
963acb6572SMateusz Guzik 	pcpu->pc_zpcpu_offset = zpcpu_offset_cpu(cpuid);
9750c202c5SJeff Roberson }
98f53d15feSStephan Uphoff 
9950c202c5SJeff Roberson void
dpcpu_init(void * dpcpu,int cpuid)10050c202c5SJeff Roberson dpcpu_init(void *dpcpu, int cpuid)
10150c202c5SJeff Roberson {
10250c202c5SJeff Roberson 	struct pcpu *pcpu;
10350c202c5SJeff Roberson 
104*9d6ae1e3SColin Percival 	TSENTER();
10550c202c5SJeff Roberson 	pcpu = pcpu_find(cpuid);
10650c202c5SJeff Roberson 	pcpu->pc_dynamic = (uintptr_t)dpcpu - DPCPU_START;
10750c202c5SJeff Roberson 
10850c202c5SJeff Roberson 	/*
10950c202c5SJeff Roberson 	 * Initialize defaults from our linker section.
11050c202c5SJeff Roberson 	 */
11150c202c5SJeff Roberson 	memcpy(dpcpu, (void *)DPCPU_START, DPCPU_BYTES);
11250c202c5SJeff Roberson 
11350c202c5SJeff Roberson 	/*
11450c202c5SJeff Roberson 	 * Place it in the global pcpu offset array.
11550c202c5SJeff Roberson 	 */
11650c202c5SJeff Roberson 	dpcpu_off[cpuid] = pcpu->pc_dynamic;
117*9d6ae1e3SColin Percival 	TSEXIT();
11850c202c5SJeff Roberson }
11950c202c5SJeff Roberson 
12050c202c5SJeff Roberson static void
dpcpu_startup(void * dummy __unused)12150c202c5SJeff Roberson dpcpu_startup(void *dummy __unused)
12250c202c5SJeff Roberson {
12350c202c5SJeff Roberson 	struct dpcpu_free *df;
12450c202c5SJeff Roberson 
12550c202c5SJeff Roberson 	df = malloc(sizeof(*df), M_PCPU, M_WAITOK | M_ZERO);
12650c202c5SJeff Roberson 	df->df_start = (uintptr_t)&DPCPU_NAME(modspace);
127793f71bfSBjoern A. Zeeb 	df->df_len = DPCPU_MODMIN;
12850c202c5SJeff Roberson 	TAILQ_INSERT_HEAD(&dpcpu_head, df, df_link);
12950c202c5SJeff Roberson 	sx_init(&dpcpu_lock, "dpcpu alloc lock");
13050c202c5SJeff Roberson }
131891cf3edSEd Maste SYSINIT(dpcpu, SI_SUB_KLD, SI_ORDER_FIRST, dpcpu_startup, NULL);
13250c202c5SJeff Roberson 
13350c202c5SJeff Roberson /*
1342dee296aSMateusz Guzik  * UMA_ZONE_PCPU zones for general kernel use.
13549fef6a2SGleb Smirnoff  */
1362dee296aSMateusz Guzik uma_zone_t pcpu_zone_4;
1372dee296aSMateusz Guzik uma_zone_t pcpu_zone_8;
1383a440a42SMateusz Guzik uma_zone_t pcpu_zone_16;
1393a440a42SMateusz Guzik uma_zone_t pcpu_zone_32;
1403a440a42SMateusz Guzik uma_zone_t pcpu_zone_64;
14149fef6a2SGleb Smirnoff 
14249fef6a2SGleb Smirnoff static void
pcpu_zones_startup(void)14349fef6a2SGleb Smirnoff pcpu_zones_startup(void)
14449fef6a2SGleb Smirnoff {
14549fef6a2SGleb Smirnoff 
1463a440a42SMateusz Guzik 	pcpu_zone_4 = uma_zcreate("pcpu-4", 4,
147a8c8e44bSMateusz Guzik 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_PCPU);
1483a440a42SMateusz Guzik 	pcpu_zone_8 = uma_zcreate("pcpu-8", 8,
1493a440a42SMateusz Guzik 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_PCPU);
1503a440a42SMateusz Guzik 	pcpu_zone_16 = uma_zcreate("pcpu-16", 16,
1513a440a42SMateusz Guzik 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_PCPU);
1523a440a42SMateusz Guzik 	pcpu_zone_32 = uma_zcreate("pcpu-32", 32,
1533a440a42SMateusz Guzik 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_PCPU);
1543a440a42SMateusz Guzik 	pcpu_zone_64 = uma_zcreate("pcpu-64", 64,
15549fef6a2SGleb Smirnoff 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_PCPU);
15649fef6a2SGleb Smirnoff }
157fffcb56fSMark Johnston SYSINIT(pcpu_zones, SI_SUB_COUNTER, SI_ORDER_FIRST, pcpu_zones_startup, NULL);
15849fef6a2SGleb Smirnoff 
15949fef6a2SGleb Smirnoff /*
16050c202c5SJeff Roberson  * First-fit extent based allocator for allocating space in the per-cpu
16150c202c5SJeff Roberson  * region reserved for modules.  This is only intended for use by the
16250c202c5SJeff Roberson  * kernel linkers to place module linker sets.
16350c202c5SJeff Roberson  */
16450c202c5SJeff Roberson void *
dpcpu_alloc(int size)16550c202c5SJeff Roberson dpcpu_alloc(int size)
16650c202c5SJeff Roberson {
16750c202c5SJeff Roberson 	struct dpcpu_free *df;
16850c202c5SJeff Roberson 	void *s;
16950c202c5SJeff Roberson 
17050c202c5SJeff Roberson 	s = NULL;
17150c202c5SJeff Roberson 	size = roundup2(size, sizeof(void *));
17250c202c5SJeff Roberson 	sx_xlock(&dpcpu_lock);
17350c202c5SJeff Roberson 	TAILQ_FOREACH(df, &dpcpu_head, df_link) {
17450c202c5SJeff Roberson 		if (df->df_len < size)
17550c202c5SJeff Roberson 			continue;
17650c202c5SJeff Roberson 		if (df->df_len == size) {
17750c202c5SJeff Roberson 			s = (void *)df->df_start;
17850c202c5SJeff Roberson 			TAILQ_REMOVE(&dpcpu_head, df, df_link);
17950c202c5SJeff Roberson 			free(df, M_PCPU);
18050c202c5SJeff Roberson 			break;
18150c202c5SJeff Roberson 		}
18250c202c5SJeff Roberson 		s = (void *)df->df_start;
18350c202c5SJeff Roberson 		df->df_len -= size;
18450c202c5SJeff Roberson 		df->df_start = df->df_start + size;
18550c202c5SJeff Roberson 		break;
18650c202c5SJeff Roberson 	}
18750c202c5SJeff Roberson 	sx_xunlock(&dpcpu_lock);
18850c202c5SJeff Roberson 
18950c202c5SJeff Roberson 	return (s);
19050c202c5SJeff Roberson }
19150c202c5SJeff Roberson 
19250c202c5SJeff Roberson /*
19350c202c5SJeff Roberson  * Free dynamic per-cpu space at module unload time.
19450c202c5SJeff Roberson  */
19550c202c5SJeff Roberson void
dpcpu_free(void * s,int size)19650c202c5SJeff Roberson dpcpu_free(void *s, int size)
19750c202c5SJeff Roberson {
19850c202c5SJeff Roberson 	struct dpcpu_free *df;
19950c202c5SJeff Roberson 	struct dpcpu_free *dn;
20050c202c5SJeff Roberson 	uintptr_t start;
20150c202c5SJeff Roberson 	uintptr_t end;
20250c202c5SJeff Roberson 
20350c202c5SJeff Roberson 	size = roundup2(size, sizeof(void *));
20450c202c5SJeff Roberson 	start = (uintptr_t)s;
20550c202c5SJeff Roberson 	end = start + size;
20650c202c5SJeff Roberson 	/*
20750c202c5SJeff Roberson 	 * Free a region of space and merge it with as many neighbors as
20850c202c5SJeff Roberson 	 * possible.  Keeping the list sorted simplifies this operation.
20950c202c5SJeff Roberson 	 */
21050c202c5SJeff Roberson 	sx_xlock(&dpcpu_lock);
21150c202c5SJeff Roberson 	TAILQ_FOREACH(df, &dpcpu_head, df_link) {
21250c202c5SJeff Roberson 		if (df->df_start > end)
21350c202c5SJeff Roberson 			break;
21450c202c5SJeff Roberson 		/*
21550c202c5SJeff Roberson 		 * If we expand at the end of an entry we may have to
21650c202c5SJeff Roberson 		 * merge it with the one following it as well.
21750c202c5SJeff Roberson 		 */
21850c202c5SJeff Roberson 		if (df->df_start + df->df_len == start) {
21950c202c5SJeff Roberson 			df->df_len += size;
22050c202c5SJeff Roberson 			dn = TAILQ_NEXT(df, df_link);
22150c202c5SJeff Roberson 			if (df->df_start + df->df_len == dn->df_start) {
22250c202c5SJeff Roberson 				df->df_len += dn->df_len;
22350c202c5SJeff Roberson 				TAILQ_REMOVE(&dpcpu_head, dn, df_link);
22450c202c5SJeff Roberson 				free(dn, M_PCPU);
22550c202c5SJeff Roberson 			}
22650c202c5SJeff Roberson 			sx_xunlock(&dpcpu_lock);
22750c202c5SJeff Roberson 			return;
22850c202c5SJeff Roberson 		}
22950c202c5SJeff Roberson 		if (df->df_start == end) {
23050c202c5SJeff Roberson 			df->df_start = start;
23150c202c5SJeff Roberson 			df->df_len += size;
23250c202c5SJeff Roberson 			sx_xunlock(&dpcpu_lock);
23350c202c5SJeff Roberson 			return;
23450c202c5SJeff Roberson 		}
23550c202c5SJeff Roberson 	}
23650c202c5SJeff Roberson 	dn = malloc(sizeof(*df), M_PCPU, M_WAITOK | M_ZERO);
23750c202c5SJeff Roberson 	dn->df_start = start;
23850c202c5SJeff Roberson 	dn->df_len = size;
23950c202c5SJeff Roberson 	if (df)
24050c202c5SJeff Roberson 		TAILQ_INSERT_BEFORE(df, dn, df_link);
24150c202c5SJeff Roberson 	else
24250c202c5SJeff Roberson 		TAILQ_INSERT_TAIL(&dpcpu_head, dn, df_link);
24350c202c5SJeff Roberson 	sx_xunlock(&dpcpu_lock);
24450c202c5SJeff Roberson }
24550c202c5SJeff Roberson 
24650c202c5SJeff Roberson /*
24750c202c5SJeff Roberson  * Initialize the per-cpu storage from an updated linker-set region.
24850c202c5SJeff Roberson  */
24950c202c5SJeff Roberson void
dpcpu_copy(void * s,int size)25050c202c5SJeff Roberson dpcpu_copy(void *s, int size)
25150c202c5SJeff Roberson {
25250c202c5SJeff Roberson #ifdef SMP
25350c202c5SJeff Roberson 	uintptr_t dpcpu;
25450c202c5SJeff Roberson 	int i;
25550c202c5SJeff Roberson 
25696c85efbSNathan Whitehorn 	CPU_FOREACH(i) {
25750c202c5SJeff Roberson 		dpcpu = dpcpu_off[i];
25850c202c5SJeff Roberson 		if (dpcpu == 0)
25950c202c5SJeff Roberson 			continue;
26050c202c5SJeff Roberson 		memcpy((void *)(dpcpu + (uintptr_t)s), s, size);
26150c202c5SJeff Roberson 	}
26250c202c5SJeff Roberson #else
26350c202c5SJeff Roberson 	memcpy((void *)(dpcpu_off[0] + (uintptr_t)s), s, size);
26450c202c5SJeff Roberson #endif
265ba228f6dSJohn Baldwin }
266ba228f6dSJohn Baldwin 
267ba228f6dSJohn Baldwin /*
2680bbc8826SJohn Baldwin  * Destroy a struct pcpu.
269ba228f6dSJohn Baldwin  */
2700bbc8826SJohn Baldwin void
pcpu_destroy(struct pcpu * pcpu)2710bbc8826SJohn Baldwin pcpu_destroy(struct pcpu *pcpu)
272ba228f6dSJohn Baldwin {
273ba228f6dSJohn Baldwin 
274d098f930SNathan Whitehorn 	STAILQ_REMOVE(&cpuhead, pcpu, pcpu, pc_allcpu);
2750bbc8826SJohn Baldwin 	cpuid_to_pcpu[pcpu->pc_cpuid] = NULL;
27650c202c5SJeff Roberson 	dpcpu_off[pcpu->pc_cpuid] = 0;
277ba228f6dSJohn Baldwin }
2780bbc8826SJohn Baldwin 
2790bbc8826SJohn Baldwin /*
2800bbc8826SJohn Baldwin  * Locate a struct pcpu by cpu id.
2810bbc8826SJohn Baldwin  */
2820bbc8826SJohn Baldwin struct pcpu *
pcpu_find(u_int cpuid)2830bbc8826SJohn Baldwin pcpu_find(u_int cpuid)
2840bbc8826SJohn Baldwin {
2850bbc8826SJohn Baldwin 
2860bbc8826SJohn Baldwin 	return (cpuid_to_pcpu[cpuid]);
2870bbc8826SJohn Baldwin }
2880bbc8826SJohn Baldwin 
28950c202c5SJeff Roberson int
sysctl_dpcpu_quad(SYSCTL_HANDLER_ARGS)29050c202c5SJeff Roberson sysctl_dpcpu_quad(SYSCTL_HANDLER_ARGS)
29150c202c5SJeff Roberson {
29250c202c5SJeff Roberson 	uintptr_t dpcpu;
293e6e06a8bSJeff Roberson 	int64_t count;
29450c202c5SJeff Roberson 	int i;
29550c202c5SJeff Roberson 
29650c202c5SJeff Roberson 	count = 0;
29796c85efbSNathan Whitehorn 	CPU_FOREACH(i) {
29850c202c5SJeff Roberson 		dpcpu = dpcpu_off[i];
29950c202c5SJeff Roberson 		if (dpcpu == 0)
30050c202c5SJeff Roberson 			continue;
30150c202c5SJeff Roberson 		count += *(int64_t *)(dpcpu + (uintptr_t)arg1);
30250c202c5SJeff Roberson 	}
303e6e06a8bSJeff Roberson 	return (SYSCTL_OUT(req, &count, sizeof(count)));
304e6e06a8bSJeff Roberson }
305e6e06a8bSJeff Roberson 
306e6e06a8bSJeff Roberson int
sysctl_dpcpu_long(SYSCTL_HANDLER_ARGS)307e6e06a8bSJeff Roberson sysctl_dpcpu_long(SYSCTL_HANDLER_ARGS)
308e6e06a8bSJeff Roberson {
309e6e06a8bSJeff Roberson 	uintptr_t dpcpu;
310e6e06a8bSJeff Roberson 	long count;
311e6e06a8bSJeff Roberson 	int i;
312e6e06a8bSJeff Roberson 
313e6e06a8bSJeff Roberson 	count = 0;
31496c85efbSNathan Whitehorn 	CPU_FOREACH(i) {
315e6e06a8bSJeff Roberson 		dpcpu = dpcpu_off[i];
316e6e06a8bSJeff Roberson 		if (dpcpu == 0)
317e6e06a8bSJeff Roberson 			continue;
318e6e06a8bSJeff Roberson 		count += *(long *)(dpcpu + (uintptr_t)arg1);
319e6e06a8bSJeff Roberson 	}
32050c202c5SJeff Roberson 	return (SYSCTL_OUT(req, &count, sizeof(count)));
32150c202c5SJeff Roberson }
32250c202c5SJeff Roberson 
32350c202c5SJeff Roberson int
sysctl_dpcpu_int(SYSCTL_HANDLER_ARGS)32450c202c5SJeff Roberson sysctl_dpcpu_int(SYSCTL_HANDLER_ARGS)
32550c202c5SJeff Roberson {
32650c202c5SJeff Roberson 	uintptr_t dpcpu;
327e6e06a8bSJeff Roberson 	int count;
32850c202c5SJeff Roberson 	int i;
32950c202c5SJeff Roberson 
33050c202c5SJeff Roberson 	count = 0;
33196c85efbSNathan Whitehorn 	CPU_FOREACH(i) {
33250c202c5SJeff Roberson 		dpcpu = dpcpu_off[i];
33350c202c5SJeff Roberson 		if (dpcpu == 0)
33450c202c5SJeff Roberson 			continue;
33550c202c5SJeff Roberson 		count += *(int *)(dpcpu + (uintptr_t)arg1);
33650c202c5SJeff Roberson 	}
33750c202c5SJeff Roberson 	return (SYSCTL_OUT(req, &count, sizeof(count)));
33850c202c5SJeff Roberson }
33950c202c5SJeff Roberson 
3400bbc8826SJohn Baldwin #ifdef DDB
DB_SHOW_COMMAND_FLAGS(dpcpu_off,db_show_dpcpu_off,DB_CMD_MEMSAFE)341c84c5e00SMitchell Horne DB_SHOW_COMMAND_FLAGS(dpcpu_off, db_show_dpcpu_off, DB_CMD_MEMSAFE)
342f2140faeSBjoern A. Zeeb {
343f2140faeSBjoern A. Zeeb 	int id;
344f2140faeSBjoern A. Zeeb 
3453aa6d94eSJohn Baldwin 	CPU_FOREACH(id) {
346f2140faeSBjoern A. Zeeb 		db_printf("dpcpu_off[%2d] = 0x%jx (+ DPCPU_START = %p)\n",
347f2140faeSBjoern A. Zeeb 		    id, (uintmax_t)dpcpu_off[id],
348f2140faeSBjoern A. Zeeb 		    (void *)(uintptr_t)(dpcpu_off[id] + DPCPU_START));
349f2140faeSBjoern A. Zeeb 	}
350f2140faeSBjoern A. Zeeb }
3510bbc8826SJohn Baldwin 
3529b791585SRobert Watson static void
show_pcpu(struct pcpu * pc)3539b791585SRobert Watson show_pcpu(struct pcpu *pc)
3549b791585SRobert Watson {
3559b791585SRobert Watson 	struct thread *td;
3569b791585SRobert Watson 
3570bbc8826SJohn Baldwin 	db_printf("cpuid        = %d\n", pc->pc_cpuid);
35850c202c5SJeff Roberson 	db_printf("dynamic pcpu = %p\n", (void *)pc->pc_dynamic);
3590bbc8826SJohn Baldwin 	db_printf("curthread    = ");
3600bbc8826SJohn Baldwin 	td = pc->pc_curthread;
3610bbc8826SJohn Baldwin 	if (td != NULL)
36261a74c5cSJeff Roberson 		db_printf("%p: pid %d tid %d critnest %d \"%s\"\n", td,
36361a74c5cSJeff Roberson 		    td->td_proc->p_pid, td->td_tid, td->td_critnest,
36461a74c5cSJeff Roberson 		    td->td_name);
3650bbc8826SJohn Baldwin 	else
3660bbc8826SJohn Baldwin 		db_printf("none\n");
3670bbc8826SJohn Baldwin 	db_printf("curpcb       = %p\n", pc->pc_curpcb);
3680bbc8826SJohn Baldwin 	db_printf("fpcurthread  = ");
3690bbc8826SJohn Baldwin 	td = pc->pc_fpcurthread;
3700bbc8826SJohn Baldwin 	if (td != NULL)
3710bbc8826SJohn Baldwin 		db_printf("%p: pid %d \"%s\"\n", td, td->td_proc->p_pid,
372431f8906SJulian Elischer 		    td->td_name);
3730bbc8826SJohn Baldwin 	else
3740bbc8826SJohn Baldwin 		db_printf("none\n");
3750bbc8826SJohn Baldwin 	db_printf("idlethread   = ");
3760bbc8826SJohn Baldwin 	td = pc->pc_idlethread;
3770bbc8826SJohn Baldwin 	if (td != NULL)
378e826ef1eSJohn Baldwin 		db_printf("%p: tid %d \"%s\"\n", td, td->td_tid, td->td_name);
3790bbc8826SJohn Baldwin 	else
3800bbc8826SJohn Baldwin 		db_printf("none\n");
3810bbc8826SJohn Baldwin 	db_show_mdpcpu(pc);
3820bbc8826SJohn Baldwin 
38321ca7b57SMarko Zec #ifdef VIMAGE
38421ca7b57SMarko Zec 	db_printf("curvnet      = %p\n", pc->pc_curthread->td_vnet);
38521ca7b57SMarko Zec #endif
38621ca7b57SMarko Zec 
3870bbc8826SJohn Baldwin #ifdef WITNESS
3880bbc8826SJohn Baldwin 	db_printf("spin locks held:\n");
38998332c8cSAttilio Rao 	witness_list_locks(&pc->pc_spinlocks, db_printf);
3900bbc8826SJohn Baldwin #endif
3910bbc8826SJohn Baldwin }
3929b791585SRobert Watson 
DB_SHOW_COMMAND_FLAGS(pcpu,db_show_pcpu,DB_CMD_MEMSAFE)393c84c5e00SMitchell Horne DB_SHOW_COMMAND_FLAGS(pcpu, db_show_pcpu, DB_CMD_MEMSAFE)
3949b791585SRobert Watson {
3959b791585SRobert Watson 	struct pcpu *pc;
3969b791585SRobert Watson 	int id;
3979b791585SRobert Watson 
3989b791585SRobert Watson 	if (have_addr)
3999b791585SRobert Watson 		id = ((addr >> 4) % 16) * 10 + (addr % 16);
4009b791585SRobert Watson 	else
4019b791585SRobert Watson 		id = PCPU_GET(cpuid);
4029b791585SRobert Watson 	pc = pcpu_find(id);
4039b791585SRobert Watson 	if (pc == NULL) {
4049b791585SRobert Watson 		db_printf("CPU %d not found\n", id);
4059b791585SRobert Watson 		return;
4069b791585SRobert Watson 	}
4079b791585SRobert Watson 	show_pcpu(pc);
4089b791585SRobert Watson }
4099b791585SRobert Watson 
DB_SHOW_ALL_COMMAND(pcpu,db_show_cpu_all)41039297ba4SSam Leffler DB_SHOW_ALL_COMMAND(pcpu, db_show_cpu_all)
4119b791585SRobert Watson {
4129b791585SRobert Watson 	struct pcpu *pc;
4139b791585SRobert Watson 	int id;
4149b791585SRobert Watson 
4159b791585SRobert Watson 	db_printf("Current CPU: %d\n\n", PCPU_GET(cpuid));
416efe67753SNathan Whitehorn 	CPU_FOREACH(id) {
4179b791585SRobert Watson 		pc = pcpu_find(id);
4189b791585SRobert Watson 		if (pc != NULL) {
4199b791585SRobert Watson 			show_pcpu(pc);
4209b791585SRobert Watson 			db_printf("\n");
4219b791585SRobert Watson 		}
4229b791585SRobert Watson 	}
4239b791585SRobert Watson }
424c84c5e00SMitchell Horne DB_SHOW_ALIAS_FLAGS(allpcpu, db_show_cpu_all, DB_CMD_MEMSAFE);
4250bbc8826SJohn Baldwin #endif
426