]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/sys/pcpu.h
stand/powerpc: Only build loader.kboot for powerpc64
[FreeBSD/FreeBSD.git] / sys / sys / pcpu.h
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2001 Wind River Systems, Inc.
5  * All rights reserved.
6  * Written by: John Baldwin <jhb@FreeBSD.org>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * $FreeBSD$
33  */
34
35 #ifndef _SYS_PCPU_H_
36 #define _SYS_PCPU_H_
37
38 #ifdef LOCORE
39 #error "no assembler-serviceable parts inside"
40 #endif
41
42 #include <sys/param.h>
43 #include <sys/_cpuset.h>
44 #include <sys/_lock.h>
45 #include <sys/_mutex.h>
46 #include <sys/_sx.h>
47 #include <sys/queue.h>
48 #include <sys/_rmlock.h>
49 #include <sys/resource.h>
50 #include <machine/pcpu.h>
51
52 #define DPCPU_SETNAME           "set_pcpu"
53 #define DPCPU_SYMPREFIX         "pcpu_entry_"
54
55 #ifdef _KERNEL
56
57 /*
58  * Define a set for pcpu data.
59  */
60 extern uintptr_t *__start_set_pcpu;
61 __GLOBL(__start_set_pcpu);
62 extern uintptr_t *__stop_set_pcpu;
63 __GLOBL(__stop_set_pcpu);
64
65 /*
66  * Array of dynamic pcpu base offsets.  Indexed by id.
67  */
68 extern uintptr_t dpcpu_off[];
69
70 /*
71  * Convenience defines.
72  */
73 #define DPCPU_START             ((uintptr_t)&__start_set_pcpu)
74 #define DPCPU_STOP              ((uintptr_t)&__stop_set_pcpu)
75 #define DPCPU_BYTES             (DPCPU_STOP - DPCPU_START)
76 #define DPCPU_MODMIN            2048
77 #define DPCPU_SIZE              roundup2(DPCPU_BYTES, PAGE_SIZE)
78 #define DPCPU_MODSIZE           (DPCPU_SIZE - (DPCPU_BYTES - DPCPU_MODMIN))
79
80 /*
81  * Declaration and definition.
82  */
83 #define DPCPU_NAME(n)           pcpu_entry_##n
84 #define DPCPU_DECLARE(t, n)     extern t DPCPU_NAME(n)
85 /* struct _hack is to stop this from being used with the static keyword. */
86 #define DPCPU_DEFINE(t, n)      \
87     struct _hack; t DPCPU_NAME(n) __section(DPCPU_SETNAME) __used
88 #if defined(KLD_MODULE) && (defined(__aarch64__) || defined(__riscv))
89 /*
90  * On some architectures the compiler will use PC-relative load to
91  * find the address of DPCPU data with the static keyword. We then
92  * use this to find the offset of the data in a per-CPU region.
93  * This works for in the kernel as we can allocate the space ahead
94  * of time, however modules need to allocate a sepatate space and
95  * then use relocations to fix the address of the data. As
96  * PC-relative data doesn't have a relocation there is nothing for
97  * the kernel module linker to fix so data is accessed from the
98  * wrong location.
99  *
100  * This is a workaround until a better solution can be found.
101  *
102  * VNET_DEFINE_STATIC also has the same workaround.
103  */
104 #define DPCPU_DEFINE_STATIC(t, n)       \
105     t DPCPU_NAME(n) __section(DPCPU_SETNAME) __used
106 #else
107 #define DPCPU_DEFINE_STATIC(t, n)       \
108     static t DPCPU_NAME(n) __section(DPCPU_SETNAME) __used
109 #endif
110
111 /*
112  * Accessors with a given base.
113  */
114 #define _DPCPU_PTR(b, n)                                                \
115     (__typeof(DPCPU_NAME(n))*)((b) + (uintptr_t)&DPCPU_NAME(n))
116 #define _DPCPU_GET(b, n)        (*_DPCPU_PTR(b, n))
117 #define _DPCPU_SET(b, n, v)     (*_DPCPU_PTR(b, n) = v)
118
119 /*
120  * Accessors for the current cpu.
121  */
122 #define DPCPU_PTR(n)            _DPCPU_PTR(PCPU_GET(dynamic), n)
123 #define DPCPU_GET(n)            (*DPCPU_PTR(n))
124 #define DPCPU_SET(n, v)         (*DPCPU_PTR(n) = v)
125
126 /*
127  * Accessors for remote cpus.
128  */
129 #define DPCPU_ID_PTR(i, n)      _DPCPU_PTR(dpcpu_off[(i)], n)
130 #define DPCPU_ID_GET(i, n)      (*DPCPU_ID_PTR(i, n))
131 #define DPCPU_ID_SET(i, n, v)   (*DPCPU_ID_PTR(i, n) = v)
132
133 /*
134  * Utility macros.
135  */
136 #define DPCPU_SUM(n) __extension__                                      \
137 ({                                                                      \
138         u_int _i;                                                       \
139         __typeof(*DPCPU_PTR(n)) sum;                                    \
140                                                                         \
141         sum = 0;                                                        \
142         CPU_FOREACH(_i) {                                               \
143                 sum += *DPCPU_ID_PTR(_i, n);                            \
144         }                                                               \
145         sum;                                                            \
146 })
147
148 #define DPCPU_VARSUM(n, var) __extension__                              \
149 ({                                                                      \
150         u_int _i;                                                       \
151         __typeof((DPCPU_PTR(n))->var) sum;                              \
152                                                                         \
153         sum = 0;                                                        \
154         CPU_FOREACH(_i) {                                               \
155                 sum += (DPCPU_ID_PTR(_i, n))->var;                      \
156         }                                                               \
157         sum;                                                            \
158 })
159
160 #define DPCPU_ZERO(n) do {                                              \
161         u_int _i;                                                       \
162                                                                         \
163         CPU_FOREACH(_i) {                                               \
164                 bzero(DPCPU_ID_PTR(_i, n), sizeof(*DPCPU_PTR(n)));      \
165         }                                                               \
166 } while(0)
167
168 #endif /* _KERNEL */
169
170 /*
171  * This structure maps out the global data that needs to be kept on a
172  * per-cpu basis.  The members are accessed via the PCPU_GET/SET/PTR
173  * macros defined in <machine/pcpu.h>.  Machine dependent fields are
174  * defined in the PCPU_MD_FIELDS macro defined in <machine/pcpu.h>.
175  */
176 struct pcpu {
177         struct thread   *pc_curthread;          /* Current thread */
178         struct thread   *pc_idlethread;         /* Idle thread */
179         struct thread   *pc_fpcurthread;        /* Fp state owner */
180         struct thread   *pc_deadthread;         /* Zombie thread or NULL */
181         struct pcb      *pc_curpcb;             /* Current pcb */
182         uint64_t        pc_switchtime;          /* cpu_ticks() at last csw */
183         int             pc_switchticks;         /* `ticks' at last csw */
184         u_int           pc_cpuid;               /* This cpu number */
185         STAILQ_ENTRY(pcpu) pc_allcpu;
186         struct lock_list_entry *pc_spinlocks;
187         long            pc_cp_time[CPUSTATES];  /* statclock ticks */
188         struct device   *pc_device;
189         void            *pc_netisr;             /* netisr SWI cookie */
190         int             pc_unused1;             /* unused field */
191         int             pc_domain;              /* Memory domain. */
192         struct rm_queue pc_rm_queue;            /* rmlock list of trackers */
193         uintptr_t       pc_dynamic;             /* Dynamic per-cpu data area */
194         uint64_t        pc_early_dummy_counter; /* Startup time counter(9) */
195
196         /*
197          * Keep MD fields last, so that CPU-specific variations on a
198          * single architecture don't result in offset variations of
199          * the machine-independent fields of the pcpu.  Even though
200          * the pcpu structure is private to the kernel, some ports
201          * (e.g., lsof, part of gtop) define _KERNEL and include this
202          * header.  While strictly speaking this is wrong, there's no
203          * reason not to keep the offsets of the MI fields constant
204          * if only to make kernel debugging easier.
205          */
206         PCPU_MD_FIELDS;
207 } __aligned(CACHE_LINE_SIZE);
208
209 #ifdef _KERNEL
210
211 STAILQ_HEAD(cpuhead, pcpu);
212
213 extern struct cpuhead cpuhead;
214 extern struct pcpu *cpuid_to_pcpu[];
215
216 #define curcpu          PCPU_GET(cpuid)
217 #define curproc         (curthread->td_proc)
218 #ifndef curthread
219 #define curthread       PCPU_GET(curthread)
220 #endif
221 #define curvidata       PCPU_GET(vidata)
222
223 #define UMA_PCPU_ALLOC_SIZE             PAGE_SIZE
224
225 #ifdef CTASSERT
226 #if defined(__i386__) || defined(__amd64__)
227 /* Required for counters(9) to work on x86. */
228 CTASSERT(sizeof(struct pcpu) == UMA_PCPU_ALLOC_SIZE);
229 #else
230 /*
231  * To minimize memory waste in per-cpu UMA zones, size of struct pcpu
232  * should be denominator of PAGE_SIZE.
233  */
234 CTASSERT((PAGE_SIZE / sizeof(struct pcpu)) * sizeof(struct pcpu) == PAGE_SIZE);
235 #endif  /* UMA_PCPU_ALLOC_SIZE && x86 */
236 #endif  /* CTASSERT */
237
238 /* Accessor to elements allocated via UMA_ZONE_PCPU zone. */
239 static inline void *
240 zpcpu_get(void *base)
241 {
242
243         return ((char *)(base) + UMA_PCPU_ALLOC_SIZE * curcpu);
244 }
245
246 static inline void *
247 zpcpu_get_cpu(void *base, int cpu)
248 {
249
250         return ((char *)(base) + UMA_PCPU_ALLOC_SIZE * cpu);
251 }
252
253 /*
254  * Machine dependent callouts.  cpu_pcpu_init() is responsible for
255  * initializing machine dependent fields of struct pcpu, and
256  * db_show_mdpcpu() is responsible for handling machine dependent
257  * fields for the DDB 'show pcpu' command.
258  */
259 void    cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size);
260 void    db_show_mdpcpu(struct pcpu *pcpu);
261
262 void    *dpcpu_alloc(int size);
263 void    dpcpu_copy(void *s, int size);
264 void    dpcpu_free(void *s, int size);
265 void    dpcpu_init(void *dpcpu, int cpuid);
266 void    pcpu_destroy(struct pcpu *pcpu);
267 struct  pcpu *pcpu_find(u_int cpuid);
268 void    pcpu_init(struct pcpu *pcpu, int cpuid, size_t size);
269
270 #endif /* _KERNEL */
271
272 #endif /* !_SYS_PCPU_H_ */