1 #ifndef _ASM_X86_PERCPU_H
2 #define _ASM_X86_PERCPU_H
5 #define __percpu_seg gs
6 #define __percpu_mov_op movq
8 #define __percpu_seg fs
9 #define __percpu_mov_op movl
15 * PER_CPU finds an address of a per-cpu variable.
19 * reg - 32bit register
21 * The resulting address is stored in the "reg" argument.
24 * PER_CPU(cpu_gdt_descr, %ebx)
27 #define PER_CPU(var, reg) \
28 __percpu_mov_op %__percpu_seg:this_cpu_off, reg; \
30 #define PER_CPU_VAR(var) %__percpu_seg:var
32 #define PER_CPU(var, reg) __percpu_mov_op $var, reg
33 #define PER_CPU_VAR(var) var
36 #ifdef CONFIG_X86_64_SMP
37 #define INIT_PER_CPU_VAR(var) init_per_cpu__##var
39 #define INIT_PER_CPU_VAR(var) var
42 #else /* ...!ASSEMBLY */
44 #include <linux/kernel.h>
45 #include <linux/stringify.h>
48 #define __percpu_arg(x) "%%"__stringify(__percpu_seg)":%P" #x
49 #define __my_cpu_offset percpu_read(this_cpu_off)
51 #define __percpu_arg(x) "%P" #x
55 * Initialized pointers to per-cpu variables needed for the boot
56 * processor need to use these macros to get the proper address
57 * offset from __per_cpu_load on SMP.
59 * There also must be an entry in vmlinux_64.lds.S
61 #define DECLARE_INIT_PER_CPU(var) \
62 extern typeof(var) init_per_cpu_var(var)
64 #ifdef CONFIG_X86_64_SMP
65 #define init_per_cpu_var(var) init_per_cpu__##var
67 #define init_per_cpu_var(var) var
70 /* For arch-specific code, we can use direct single-insn ops (they
71 * don't give an lvalue though). */
72 extern void __bad_percpu_size(void);
74 #define percpu_to_op(op, var, val) \
76 typedef typeof(var) pto_T__; \
81 switch (sizeof(var)) { \
83 asm(op "b %1,"__percpu_arg(0) \
85 : "qi" ((pto_T__)(val))); \
88 asm(op "w %1,"__percpu_arg(0) \
90 : "ri" ((pto_T__)(val))); \
93 asm(op "l %1,"__percpu_arg(0) \
95 : "ri" ((pto_T__)(val))); \
98 asm(op "q %1,"__percpu_arg(0) \
100 : "re" ((pto_T__)(val))); \
102 default: __bad_percpu_size(); \
107 * Generate a percpu add to memory instruction and optimize code
108 * if a one is added or subtracted.
110 #define percpu_add_op(var, val) \
112 typedef typeof(var) pao_T__; \
113 const int pao_ID__ = (__builtin_constant_p(val) && \
114 ((val) == 1 || (val) == -1)) ? (val) : 0; \
119 switch (sizeof(var)) { \
122 asm("incb "__percpu_arg(0) : "+m" (var)); \
123 else if (pao_ID__ == -1) \
124 asm("decb "__percpu_arg(0) : "+m" (var)); \
126 asm("addb %1, "__percpu_arg(0) \
128 : "qi" ((pao_T__)(val))); \
132 asm("incw "__percpu_arg(0) : "+m" (var)); \
133 else if (pao_ID__ == -1) \
134 asm("decw "__percpu_arg(0) : "+m" (var)); \
136 asm("addw %1, "__percpu_arg(0) \
138 : "ri" ((pao_T__)(val))); \
142 asm("incl "__percpu_arg(0) : "+m" (var)); \
143 else if (pao_ID__ == -1) \
144 asm("decl "__percpu_arg(0) : "+m" (var)); \
146 asm("addl %1, "__percpu_arg(0) \
148 : "ri" ((pao_T__)(val))); \
152 asm("incq "__percpu_arg(0) : "+m" (var)); \
153 else if (pao_ID__ == -1) \
154 asm("decq "__percpu_arg(0) : "+m" (var)); \
156 asm("addq %1, "__percpu_arg(0) \
158 : "re" ((pao_T__)(val))); \
160 default: __bad_percpu_size(); \
164 #define percpu_from_op(op, var, constraint) \
166 typeof(var) pfo_ret__; \
167 switch (sizeof(var)) { \
169 asm(op "b "__percpu_arg(1)",%0" \
174 asm(op "w "__percpu_arg(1)",%0" \
179 asm(op "l "__percpu_arg(1)",%0" \
184 asm(op "q "__percpu_arg(1)",%0" \
188 default: __bad_percpu_size(); \
194 * percpu_read() makes gcc load the percpu variable every time it is
195 * accessed while percpu_read_stable() allows the value to be cached.
196 * percpu_read_stable() is more efficient and can be used if its value
197 * is guaranteed to be valid across cpus. The current users include
198 * get_current() and get_thread_info() both of which are actually
199 * per-thread variables implemented as per-cpu variables and thus
200 * stable for the duration of the respective task.
202 #define percpu_read(var) percpu_from_op("mov", var, "m" (var))
203 #define percpu_read_stable(var) percpu_from_op("mov", var, "p" (&(var)))
204 #define percpu_write(var, val) percpu_to_op("mov", var, val)
205 #define percpu_add(var, val) percpu_add_op(var, val)
206 #define percpu_sub(var, val) percpu_add_op(var, -(val))
207 #define percpu_and(var, val) percpu_to_op("and", var, val)
208 #define percpu_or(var, val) percpu_to_op("or", var, val)
209 #define percpu_xor(var, val) percpu_to_op("xor", var, val)
211 #define __this_cpu_read_1(pcp) percpu_from_op("mov", (pcp), "m"(pcp))
212 #define __this_cpu_read_2(pcp) percpu_from_op("mov", (pcp), "m"(pcp))
213 #define __this_cpu_read_4(pcp) percpu_from_op("mov", (pcp), "m"(pcp))
215 #define __this_cpu_write_1(pcp, val) percpu_to_op("mov", (pcp), val)
216 #define __this_cpu_write_2(pcp, val) percpu_to_op("mov", (pcp), val)
217 #define __this_cpu_write_4(pcp, val) percpu_to_op("mov", (pcp), val)
218 #define __this_cpu_add_1(pcp, val) percpu_add_op((pcp), val)
219 #define __this_cpu_add_2(pcp, val) percpu_add_op((pcp), val)
220 #define __this_cpu_add_4(pcp, val) percpu_add_op((pcp), val)
221 #define __this_cpu_and_1(pcp, val) percpu_to_op("and", (pcp), val)
222 #define __this_cpu_and_2(pcp, val) percpu_to_op("and", (pcp), val)
223 #define __this_cpu_and_4(pcp, val) percpu_to_op("and", (pcp), val)
224 #define __this_cpu_or_1(pcp, val) percpu_to_op("or", (pcp), val)
225 #define __this_cpu_or_2(pcp, val) percpu_to_op("or", (pcp), val)
226 #define __this_cpu_or_4(pcp, val) percpu_to_op("or", (pcp), val)
227 #define __this_cpu_xor_1(pcp, val) percpu_to_op("xor", (pcp), val)
228 #define __this_cpu_xor_2(pcp, val) percpu_to_op("xor", (pcp), val)
229 #define __this_cpu_xor_4(pcp, val) percpu_to_op("xor", (pcp), val)
231 #define this_cpu_read_1(pcp) percpu_from_op("mov", (pcp), "m"(pcp))
232 #define this_cpu_read_2(pcp) percpu_from_op("mov", (pcp), "m"(pcp))
233 #define this_cpu_read_4(pcp) percpu_from_op("mov", (pcp), "m"(pcp))
234 #define this_cpu_write_1(pcp, val) percpu_to_op("mov", (pcp), val)
235 #define this_cpu_write_2(pcp, val) percpu_to_op("mov", (pcp), val)
236 #define this_cpu_write_4(pcp, val) percpu_to_op("mov", (pcp), val)
237 #define this_cpu_add_1(pcp, val) percpu_add_op((pcp), val)
238 #define this_cpu_add_2(pcp, val) percpu_add_op((pcp), val)
239 #define this_cpu_add_4(pcp, val) percpu_add_op((pcp), val)
240 #define this_cpu_and_1(pcp, val) percpu_to_op("and", (pcp), val)
241 #define this_cpu_and_2(pcp, val) percpu_to_op("and", (pcp), val)
242 #define this_cpu_and_4(pcp, val) percpu_to_op("and", (pcp), val)
243 #define this_cpu_or_1(pcp, val) percpu_to_op("or", (pcp), val)
244 #define this_cpu_or_2(pcp, val) percpu_to_op("or", (pcp), val)
245 #define this_cpu_or_4(pcp, val) percpu_to_op("or", (pcp), val)
246 #define this_cpu_xor_1(pcp, val) percpu_to_op("xor", (pcp), val)
247 #define this_cpu_xor_2(pcp, val) percpu_to_op("xor", (pcp), val)
248 #define this_cpu_xor_4(pcp, val) percpu_to_op("xor", (pcp), val)
250 #define irqsafe_cpu_add_1(pcp, val) percpu_add_op((pcp), val)
251 #define irqsafe_cpu_add_2(pcp, val) percpu_add_op((pcp), val)
252 #define irqsafe_cpu_add_4(pcp, val) percpu_add_op((pcp), val)
253 #define irqsafe_cpu_and_1(pcp, val) percpu_to_op("and", (pcp), val)
254 #define irqsafe_cpu_and_2(pcp, val) percpu_to_op("and", (pcp), val)
255 #define irqsafe_cpu_and_4(pcp, val) percpu_to_op("and", (pcp), val)
256 #define irqsafe_cpu_or_1(pcp, val) percpu_to_op("or", (pcp), val)
257 #define irqsafe_cpu_or_2(pcp, val) percpu_to_op("or", (pcp), val)
258 #define irqsafe_cpu_or_4(pcp, val) percpu_to_op("or", (pcp), val)
259 #define irqsafe_cpu_xor_1(pcp, val) percpu_to_op("xor", (pcp), val)
260 #define irqsafe_cpu_xor_2(pcp, val) percpu_to_op("xor", (pcp), val)
261 #define irqsafe_cpu_xor_4(pcp, val) percpu_to_op("xor", (pcp), val)
264 * Per cpu atomic 64 bit operations are only available under 64 bit.
265 * 32 bit must fall back to generic operations.
268 #define __this_cpu_read_8(pcp) percpu_from_op("mov", (pcp), "m"(pcp))
269 #define __this_cpu_write_8(pcp, val) percpu_to_op("mov", (pcp), val)
270 #define __this_cpu_add_8(pcp, val) percpu_add_op((pcp), val)
271 #define __this_cpu_and_8(pcp, val) percpu_to_op("and", (pcp), val)
272 #define __this_cpu_or_8(pcp, val) percpu_to_op("or", (pcp), val)
273 #define __this_cpu_xor_8(pcp, val) percpu_to_op("xor", (pcp), val)
275 #define this_cpu_read_8(pcp) percpu_from_op("mov", (pcp), "m"(pcp))
276 #define this_cpu_write_8(pcp, val) percpu_to_op("mov", (pcp), val)
277 #define this_cpu_add_8(pcp, val) percpu_add_op((pcp), val)
278 #define this_cpu_and_8(pcp, val) percpu_to_op("and", (pcp), val)
279 #define this_cpu_or_8(pcp, val) percpu_to_op("or", (pcp), val)
280 #define this_cpu_xor_8(pcp, val) percpu_to_op("xor", (pcp), val)
282 #define irqsafe_cpu_add_8(pcp, val) percpu_add_op((pcp), val)
283 #define irqsafe_cpu_and_8(pcp, val) percpu_to_op("and", (pcp), val)
284 #define irqsafe_cpu_or_8(pcp, val) percpu_to_op("or", (pcp), val)
285 #define irqsafe_cpu_xor_8(pcp, val) percpu_to_op("xor", (pcp), val)
289 /* This is not atomic against other CPUs -- CPU preemption needs to be off */
290 #define x86_test_and_clear_bit_percpu(bit, var) \
293 asm volatile("btr %2,"__percpu_arg(1)"\n\tsbbl %0,%0" \
294 : "=r" (old__), "+m" (var) \
299 #include <asm-generic/percpu.h>
301 /* We can use this directly for local CPU (faster). */
302 DECLARE_PER_CPU(unsigned long, this_cpu_off);
304 #endif /* !__ASSEMBLY__ */
309 * Define the "EARLY_PER_CPU" macros. These are used for some per_cpu
310 * variables that are initialized and accessed before there are per_cpu
314 #define DEFINE_EARLY_PER_CPU(_type, _name, _initvalue) \
315 DEFINE_PER_CPU(_type, _name) = _initvalue; \
316 __typeof__(_type) _name##_early_map[NR_CPUS] __initdata = \
317 { [0 ... NR_CPUS-1] = _initvalue }; \
318 __typeof__(_type) *_name##_early_ptr __refdata = _name##_early_map
320 #define EXPORT_EARLY_PER_CPU_SYMBOL(_name) \
321 EXPORT_PER_CPU_SYMBOL(_name)
323 #define DECLARE_EARLY_PER_CPU(_type, _name) \
324 DECLARE_PER_CPU(_type, _name); \
325 extern __typeof__(_type) *_name##_early_ptr; \
326 extern __typeof__(_type) _name##_early_map[]
328 #define early_per_cpu_ptr(_name) (_name##_early_ptr)
329 #define early_per_cpu_map(_name, _idx) (_name##_early_map[_idx])
330 #define early_per_cpu(_name, _cpu) \
331 *(early_per_cpu_ptr(_name) ? \
332 &early_per_cpu_ptr(_name)[_cpu] : \
333 &per_cpu(_name, _cpu))
335 #else /* !CONFIG_SMP */
336 #define DEFINE_EARLY_PER_CPU(_type, _name, _initvalue) \
337 DEFINE_PER_CPU(_type, _name) = _initvalue
339 #define EXPORT_EARLY_PER_CPU_SYMBOL(_name) \
340 EXPORT_PER_CPU_SYMBOL(_name)
342 #define DECLARE_EARLY_PER_CPU(_type, _name) \
343 DECLARE_PER_CPU(_type, _name)
345 #define early_per_cpu(_name, _cpu) per_cpu(_name, _cpu)
346 #define early_per_cpu_ptr(_name) NULL
347 /* no early_per_cpu_map() */
349 #endif /* !CONFIG_SMP */
351 #endif /* _ASM_X86_PERCPU_H */