xref: /freebsd/sys/kern/subr_pcpu.c (revision 9207b4cff7b8d483f4dd3c62266c2b58819eb7f9)
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  * $FreeBSD$
31  */
32 
33 /*
34  * This module provides MI support for per-cpu data.
35  *
36  * Each architecture determines the mapping of logical CPU IDs to physical
37  * CPUs.  The requirements of this mapping are as follows:
38  *  - Logical CPU IDs must reside in the range 0 ... MAXCPU - 1.
39  *  - The mapping is not required to be dense.  That is, there may be
40  *    gaps in the mappings.
41  *  - The platform sets the value of MAXCPU in <machine/param.h>.
42  *  - It is suggested, but not required, that in the non-SMP case, the
43  *    platform define MAXCPU to be 1 and define the logical ID of the
44  *    sole CPU as 0.
45  */
46 
47 #include "opt_ddb.h"
48 
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/linker_set.h>
52 #include <sys/lock.h>
53 #include <sys/pcpu.h>
54 #include <sys/proc.h>
55 #include <ddb/ddb.h>
56 
57 static struct pcpu *cpuid_to_pcpu[MAXCPU];
58 struct cpuhead cpuhead = SLIST_HEAD_INITIALIZER(cpuhead);
59 
60 /*
61  * Initialize the MI portions of a struct pcpu.
62  */
63 void
64 pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
65 {
66 
67 	bzero(pcpu, size);
68 	KASSERT(cpuid >= 0 && cpuid < MAXCPU,
69 	    ("pcpu_init: invalid cpuid %d", cpuid));
70 	pcpu->pc_cpuid = cpuid;
71 	cpuid_to_pcpu[cpuid] = pcpu;
72 	SLIST_INSERT_HEAD(&cpuhead, pcpu, pc_allcpu);
73 	cpu_pcpu_init(pcpu, cpuid, size);
74 }
75 
76 /*
77  * Destroy a struct pcpu.
78  */
79 void
80 pcpu_destroy(struct pcpu *pcpu)
81 {
82 
83 	SLIST_REMOVE(&cpuhead, pcpu, pcpu, pc_allcpu);
84 	cpuid_to_pcpu[pcpu->pc_cpuid] = NULL;
85 }
86 
87 /*
88  * Locate a struct pcpu by cpu id.
89  */
90 struct pcpu *
91 pcpu_find(u_int cpuid)
92 {
93 
94 	return (cpuid_to_pcpu[cpuid]);
95 }
96 
97 #ifdef DDB
98 DB_SHOW_COMMAND(pcpu, db_show_pcpu)
99 {
100 	struct pcpu *pc;
101 	struct thread *td;
102 	int id;
103 
104 	if (have_addr)
105 		id = ((addr >> 4) % 16) * 10 + (addr % 16);
106 	else
107 		id = PCPU_GET(cpuid);
108 	pc = pcpu_find(id);
109 	if (pc == NULL) {
110 		db_printf("CPU %d not found\n", id);
111 		return;
112 	}
113 	db_printf("cpuid        = %d\n", pc->pc_cpuid);
114 	db_printf("curthread    = ");
115 	td = pc->pc_curthread;
116 	if (td != NULL)
117 		db_printf("%p: pid %d \"%s\"\n", td, td->td_proc->p_pid,
118 		    td->td_proc->p_comm);
119 	else
120 		db_printf("none\n");
121 	db_printf("curpcb       = %p\n", pc->pc_curpcb);
122 	db_printf("fpcurthread  = ");
123 	td = pc->pc_fpcurthread;
124 	if (td != NULL)
125 		db_printf("%p: pid %d \"%s\"\n", td, td->td_proc->p_pid,
126 		    td->td_proc->p_comm);
127 	else
128 		db_printf("none\n");
129 	db_printf("idlethread   = ");
130 	td = pc->pc_idlethread;
131 	if (td != NULL)
132 		db_printf("%p: pid %d \"%s\"\n", td, td->td_proc->p_pid,
133 		    td->td_proc->p_comm);
134 	else
135 		db_printf("none\n");
136 	db_show_mdpcpu(pc);
137 
138 #ifdef WITNESS
139 	db_printf("spin locks held:\n");
140 	witness_list_locks(&pc->pc_spinlocks);
141 #endif
142 }
143 #endif
144