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_prefix "%%"__stringify(__percpu_seg)":"
49 #define __my_cpu_offset this_cpu_read(this_cpu_off)
52 * Compared to the generic __my_cpu_offset version, the following
53 * saves one instruction and avoids clobbering a temp register.
55 #define __this_cpu_ptr(ptr) \
57 unsigned long tcp_ptr__; \
58 __verify_pcpu_ptr(ptr); \
59 asm volatile("add " __percpu_arg(1) ", %0" \
61 : "m" (this_cpu_off), "0" (ptr)); \
62 (typeof(*(ptr)) __kernel __force *)tcp_ptr__; \
65 #define __percpu_prefix ""
68 #define __percpu_arg(x) __percpu_prefix "%P" #x
71 * Initialized pointers to per-cpu variables needed for the boot
72 * processor need to use these macros to get the proper address
73 * offset from __per_cpu_load on SMP.
75 * There also must be an entry in vmlinux_64.lds.S
77 #define DECLARE_INIT_PER_CPU(var) \
78 extern typeof(var) init_per_cpu_var(var)
80 #ifdef CONFIG_X86_64_SMP
81 #define init_per_cpu_var(var) init_per_cpu__##var
83 #define init_per_cpu_var(var) var
86 /* For arch-specific code, we can use direct single-insn ops (they
87 * don't give an lvalue though). */
88 extern void __bad_percpu_size(void);
90 #define percpu_to_op(op, var, val) \
92 typedef typeof(var) pto_T__; \
98 switch (sizeof(var)) { \
100 asm(op "b %1,"__percpu_arg(0) \
102 : "qi" ((pto_T__)(val))); \
105 asm(op "w %1,"__percpu_arg(0) \
107 : "ri" ((pto_T__)(val))); \
110 asm(op "l %1,"__percpu_arg(0) \
112 : "ri" ((pto_T__)(val))); \
115 asm(op "q %1,"__percpu_arg(0) \
117 : "re" ((pto_T__)(val))); \
119 default: __bad_percpu_size(); \
124 * Generate a percpu add to memory instruction and optimize code
125 * if one is added or subtracted.
127 #define percpu_add_op(var, val) \
129 typedef typeof(var) pao_T__; \
130 const int pao_ID__ = (__builtin_constant_p(val) && \
131 ((val) == 1 || (val) == -1)) ? \
138 switch (sizeof(var)) { \
141 asm("incb "__percpu_arg(0) : "+m" (var)); \
142 else if (pao_ID__ == -1) \
143 asm("decb "__percpu_arg(0) : "+m" (var)); \
145 asm("addb %1, "__percpu_arg(0) \
147 : "qi" ((pao_T__)(val))); \
151 asm("incw "__percpu_arg(0) : "+m" (var)); \
152 else if (pao_ID__ == -1) \
153 asm("decw "__percpu_arg(0) : "+m" (var)); \
155 asm("addw %1, "__percpu_arg(0) \
157 : "ri" ((pao_T__)(val))); \
161 asm("incl "__percpu_arg(0) : "+m" (var)); \
162 else if (pao_ID__ == -1) \
163 asm("decl "__percpu_arg(0) : "+m" (var)); \
165 asm("addl %1, "__percpu_arg(0) \
167 : "ri" ((pao_T__)(val))); \
171 asm("incq "__percpu_arg(0) : "+m" (var)); \
172 else if (pao_ID__ == -1) \
173 asm("decq "__percpu_arg(0) : "+m" (var)); \
175 asm("addq %1, "__percpu_arg(0) \
177 : "re" ((pao_T__)(val))); \
179 default: __bad_percpu_size(); \
183 #define percpu_from_op(op, var, constraint) \
185 typeof(var) pfo_ret__; \
186 switch (sizeof(var)) { \
188 asm(op "b "__percpu_arg(1)",%0" \
193 asm(op "w "__percpu_arg(1)",%0" \
198 asm(op "l "__percpu_arg(1)",%0" \
203 asm(op "q "__percpu_arg(1)",%0" \
207 default: __bad_percpu_size(); \
212 #define percpu_unary_op(op, var) \
214 switch (sizeof(var)) { \
216 asm(op "b "__percpu_arg(0) \
220 asm(op "w "__percpu_arg(0) \
224 asm(op "l "__percpu_arg(0) \
228 asm(op "q "__percpu_arg(0) \
231 default: __bad_percpu_size(); \
236 * Add return operation
238 #define percpu_add_return_op(var, val) \
240 typeof(var) paro_ret__ = val; \
241 switch (sizeof(var)) { \
243 asm("xaddb %0, "__percpu_arg(1) \
244 : "+q" (paro_ret__), "+m" (var) \
248 asm("xaddw %0, "__percpu_arg(1) \
249 : "+r" (paro_ret__), "+m" (var) \
253 asm("xaddl %0, "__percpu_arg(1) \
254 : "+r" (paro_ret__), "+m" (var) \
258 asm("xaddq %0, "__percpu_arg(1) \
259 : "+re" (paro_ret__), "+m" (var) \
262 default: __bad_percpu_size(); \
269 * xchg is implemented using cmpxchg without a lock prefix. xchg is
270 * expensive due to the implied lock prefix. The processor cannot prefetch
271 * cachelines if xchg is used.
273 #define percpu_xchg_op(var, nval) \
275 typeof(var) pxo_ret__; \
276 typeof(var) pxo_new__ = (nval); \
277 switch (sizeof(var)) { \
279 asm("\n\tmov "__percpu_arg(1)",%%al" \
280 "\n1:\tcmpxchgb %2, "__percpu_arg(1) \
282 : "=&a" (pxo_ret__), "+m" (var) \
287 asm("\n\tmov "__percpu_arg(1)",%%ax" \
288 "\n1:\tcmpxchgw %2, "__percpu_arg(1) \
290 : "=&a" (pxo_ret__), "+m" (var) \
295 asm("\n\tmov "__percpu_arg(1)",%%eax" \
296 "\n1:\tcmpxchgl %2, "__percpu_arg(1) \
298 : "=&a" (pxo_ret__), "+m" (var) \
303 asm("\n\tmov "__percpu_arg(1)",%%rax" \
304 "\n1:\tcmpxchgq %2, "__percpu_arg(1) \
306 : "=&a" (pxo_ret__), "+m" (var) \
310 default: __bad_percpu_size(); \
316 * cmpxchg has no such implied lock semantics as a result it is much
317 * more efficient for cpu local operations.
319 #define percpu_cmpxchg_op(var, oval, nval) \
321 typeof(var) pco_ret__; \
322 typeof(var) pco_old__ = (oval); \
323 typeof(var) pco_new__ = (nval); \
324 switch (sizeof(var)) { \
326 asm("cmpxchgb %2, "__percpu_arg(1) \
327 : "=a" (pco_ret__), "+m" (var) \
328 : "q" (pco_new__), "0" (pco_old__) \
332 asm("cmpxchgw %2, "__percpu_arg(1) \
333 : "=a" (pco_ret__), "+m" (var) \
334 : "r" (pco_new__), "0" (pco_old__) \
338 asm("cmpxchgl %2, "__percpu_arg(1) \
339 : "=a" (pco_ret__), "+m" (var) \
340 : "r" (pco_new__), "0" (pco_old__) \
344 asm("cmpxchgq %2, "__percpu_arg(1) \
345 : "=a" (pco_ret__), "+m" (var) \
346 : "r" (pco_new__), "0" (pco_old__) \
349 default: __bad_percpu_size(); \
355 * this_cpu_read() makes gcc load the percpu variable every time it is
356 * accessed while this_cpu_read_stable() allows the value to be cached.
357 * this_cpu_read_stable() is more efficient and can be used if its value
358 * is guaranteed to be valid across cpus. The current users include
359 * get_current() and get_thread_info() both of which are actually
360 * per-thread variables implemented as per-cpu variables and thus
361 * stable for the duration of the respective task.
363 #define this_cpu_read_stable(var) percpu_from_op("mov", var, "p" (&(var)))
365 #define __this_cpu_read_1(pcp) percpu_from_op("mov", (pcp), "m"(pcp))
366 #define __this_cpu_read_2(pcp) percpu_from_op("mov", (pcp), "m"(pcp))
367 #define __this_cpu_read_4(pcp) percpu_from_op("mov", (pcp), "m"(pcp))
369 #define __this_cpu_write_1(pcp, val) percpu_to_op("mov", (pcp), val)
370 #define __this_cpu_write_2(pcp, val) percpu_to_op("mov", (pcp), val)
371 #define __this_cpu_write_4(pcp, val) percpu_to_op("mov", (pcp), val)
372 #define __this_cpu_add_1(pcp, val) percpu_add_op((pcp), val)
373 #define __this_cpu_add_2(pcp, val) percpu_add_op((pcp), val)
374 #define __this_cpu_add_4(pcp, val) percpu_add_op((pcp), val)
375 #define __this_cpu_and_1(pcp, val) percpu_to_op("and", (pcp), val)
376 #define __this_cpu_and_2(pcp, val) percpu_to_op("and", (pcp), val)
377 #define __this_cpu_and_4(pcp, val) percpu_to_op("and", (pcp), val)
378 #define __this_cpu_or_1(pcp, val) percpu_to_op("or", (pcp), val)
379 #define __this_cpu_or_2(pcp, val) percpu_to_op("or", (pcp), val)
380 #define __this_cpu_or_4(pcp, val) percpu_to_op("or", (pcp), val)
381 #define __this_cpu_xor_1(pcp, val) percpu_to_op("xor", (pcp), val)
382 #define __this_cpu_xor_2(pcp, val) percpu_to_op("xor", (pcp), val)
383 #define __this_cpu_xor_4(pcp, val) percpu_to_op("xor", (pcp), val)
384 #define __this_cpu_xchg_1(pcp, val) percpu_xchg_op(pcp, val)
385 #define __this_cpu_xchg_2(pcp, val) percpu_xchg_op(pcp, val)
386 #define __this_cpu_xchg_4(pcp, val) percpu_xchg_op(pcp, val)
388 #define this_cpu_read_1(pcp) percpu_from_op("mov", (pcp), "m"(pcp))
389 #define this_cpu_read_2(pcp) percpu_from_op("mov", (pcp), "m"(pcp))
390 #define this_cpu_read_4(pcp) percpu_from_op("mov", (pcp), "m"(pcp))
391 #define this_cpu_write_1(pcp, val) percpu_to_op("mov", (pcp), val)
392 #define this_cpu_write_2(pcp, val) percpu_to_op("mov", (pcp), val)
393 #define this_cpu_write_4(pcp, val) percpu_to_op("mov", (pcp), val)
394 #define this_cpu_add_1(pcp, val) percpu_add_op((pcp), val)
395 #define this_cpu_add_2(pcp, val) percpu_add_op((pcp), val)
396 #define this_cpu_add_4(pcp, val) percpu_add_op((pcp), val)
397 #define this_cpu_and_1(pcp, val) percpu_to_op("and", (pcp), val)
398 #define this_cpu_and_2(pcp, val) percpu_to_op("and", (pcp), val)
399 #define this_cpu_and_4(pcp, val) percpu_to_op("and", (pcp), val)
400 #define this_cpu_or_1(pcp, val) percpu_to_op("or", (pcp), val)
401 #define this_cpu_or_2(pcp, val) percpu_to_op("or", (pcp), val)
402 #define this_cpu_or_4(pcp, val) percpu_to_op("or", (pcp), val)
403 #define this_cpu_xor_1(pcp, val) percpu_to_op("xor", (pcp), val)
404 #define this_cpu_xor_2(pcp, val) percpu_to_op("xor", (pcp), val)
405 #define this_cpu_xor_4(pcp, val) percpu_to_op("xor", (pcp), val)
406 #define this_cpu_xchg_1(pcp, nval) percpu_xchg_op(pcp, nval)
407 #define this_cpu_xchg_2(pcp, nval) percpu_xchg_op(pcp, nval)
408 #define this_cpu_xchg_4(pcp, nval) percpu_xchg_op(pcp, nval)
410 #define __this_cpu_add_return_1(pcp, val) percpu_add_return_op(pcp, val)
411 #define __this_cpu_add_return_2(pcp, val) percpu_add_return_op(pcp, val)
412 #define __this_cpu_add_return_4(pcp, val) percpu_add_return_op(pcp, val)
413 #define __this_cpu_cmpxchg_1(pcp, oval, nval) percpu_cmpxchg_op(pcp, oval, nval)
414 #define __this_cpu_cmpxchg_2(pcp, oval, nval) percpu_cmpxchg_op(pcp, oval, nval)
415 #define __this_cpu_cmpxchg_4(pcp, oval, nval) percpu_cmpxchg_op(pcp, oval, nval)
417 #define this_cpu_add_return_1(pcp, val) percpu_add_return_op(pcp, val)
418 #define this_cpu_add_return_2(pcp, val) percpu_add_return_op(pcp, val)
419 #define this_cpu_add_return_4(pcp, val) percpu_add_return_op(pcp, val)
420 #define this_cpu_cmpxchg_1(pcp, oval, nval) percpu_cmpxchg_op(pcp, oval, nval)
421 #define this_cpu_cmpxchg_2(pcp, oval, nval) percpu_cmpxchg_op(pcp, oval, nval)
422 #define this_cpu_cmpxchg_4(pcp, oval, nval) percpu_cmpxchg_op(pcp, oval, nval)
424 #ifdef CONFIG_X86_CMPXCHG64
425 #define percpu_cmpxchg8b_double(pcp1, pcp2, o1, o2, n1, n2) \
428 typeof(pcp1) __o1 = (o1), __n1 = (n1); \
429 typeof(pcp2) __o2 = (o2), __n2 = (n2); \
430 asm volatile("cmpxchg8b "__percpu_arg(1)"\n\tsetz %0\n\t" \
431 : "=a" (__ret), "+m" (pcp1), "+m" (pcp2), "+d" (__o2) \
432 : "b" (__n1), "c" (__n2), "a" (__o1)); \
436 #define __this_cpu_cmpxchg_double_4 percpu_cmpxchg8b_double
437 #define this_cpu_cmpxchg_double_4 percpu_cmpxchg8b_double
438 #endif /* CONFIG_X86_CMPXCHG64 */
441 * Per cpu atomic 64 bit operations are only available under 64 bit.
442 * 32 bit must fall back to generic operations.
445 #define __this_cpu_read_8(pcp) percpu_from_op("mov", (pcp), "m"(pcp))
446 #define __this_cpu_write_8(pcp, val) percpu_to_op("mov", (pcp), val)
447 #define __this_cpu_add_8(pcp, val) percpu_add_op((pcp), val)
448 #define __this_cpu_and_8(pcp, val) percpu_to_op("and", (pcp), val)
449 #define __this_cpu_or_8(pcp, val) percpu_to_op("or", (pcp), val)
450 #define __this_cpu_xor_8(pcp, val) percpu_to_op("xor", (pcp), val)
451 #define __this_cpu_add_return_8(pcp, val) percpu_add_return_op(pcp, val)
452 #define __this_cpu_xchg_8(pcp, nval) percpu_xchg_op(pcp, nval)
453 #define __this_cpu_cmpxchg_8(pcp, oval, nval) percpu_cmpxchg_op(pcp, oval, nval)
455 #define this_cpu_read_8(pcp) percpu_from_op("mov", (pcp), "m"(pcp))
456 #define this_cpu_write_8(pcp, val) percpu_to_op("mov", (pcp), val)
457 #define this_cpu_add_8(pcp, val) percpu_add_op((pcp), val)
458 #define this_cpu_and_8(pcp, val) percpu_to_op("and", (pcp), val)
459 #define this_cpu_or_8(pcp, val) percpu_to_op("or", (pcp), val)
460 #define this_cpu_xor_8(pcp, val) percpu_to_op("xor", (pcp), val)
461 #define this_cpu_add_return_8(pcp, val) percpu_add_return_op(pcp, val)
462 #define this_cpu_xchg_8(pcp, nval) percpu_xchg_op(pcp, nval)
463 #define this_cpu_cmpxchg_8(pcp, oval, nval) percpu_cmpxchg_op(pcp, oval, nval)
466 * Pretty complex macro to generate cmpxchg16 instruction. The instruction
467 * is not supported on early AMD64 processors so we must be able to emulate
468 * it in software. The address used in the cmpxchg16 instruction must be
469 * aligned to a 16 byte boundary.
471 #define percpu_cmpxchg16b_double(pcp1, pcp2, o1, o2, n1, n2) \
474 typeof(pcp1) __o1 = (o1), __n1 = (n1); \
475 typeof(pcp2) __o2 = (o2), __n2 = (n2); \
476 alternative_io("leaq %P1,%%rsi\n\tcall this_cpu_cmpxchg16b_emu\n\t", \
477 "cmpxchg16b " __percpu_arg(1) "\n\tsetz %0\n\t", \
479 ASM_OUTPUT2("=a" (__ret), "+m" (pcp1), \
480 "+m" (pcp2), "+d" (__o2)), \
481 "b" (__n1), "c" (__n2), "a" (__o1) : "rsi"); \
485 #define __this_cpu_cmpxchg_double_8 percpu_cmpxchg16b_double
486 #define this_cpu_cmpxchg_double_8 percpu_cmpxchg16b_double
490 /* This is not atomic against other CPUs -- CPU preemption needs to be off */
491 #define x86_test_and_clear_bit_percpu(bit, var) \
494 asm volatile("btr %2,"__percpu_arg(1)"\n\tsbbl %0,%0" \
495 : "=r" (old__), "+m" (var) \
500 static __always_inline
int x86_this_cpu_constant_test_bit(unsigned int nr
,
501 const unsigned long __percpu
*addr
)
503 unsigned long __percpu
*a
= (unsigned long *)addr
+ nr
/ BITS_PER_LONG
;
506 return ((1UL << (nr
% BITS_PER_LONG
)) & __this_cpu_read_8(*a
)) != 0;
508 return ((1UL << (nr
% BITS_PER_LONG
)) & __this_cpu_read_4(*a
)) != 0;
512 static inline int x86_this_cpu_variable_test_bit(int nr
,
513 const unsigned long __percpu
*addr
)
517 asm volatile("bt "__percpu_arg(2)",%1\n\t"
520 : "m" (*(unsigned long *)addr
), "Ir" (nr
));
525 #define x86_this_cpu_test_bit(nr, addr) \
526 (__builtin_constant_p((nr)) \
527 ? x86_this_cpu_constant_test_bit((nr), (addr)) \
528 : x86_this_cpu_variable_test_bit((nr), (addr)))
531 #include <asm-generic/percpu.h>
533 /* We can use this directly for local CPU (faster). */
534 DECLARE_PER_CPU(unsigned long, this_cpu_off
);
536 #endif /* !__ASSEMBLY__ */
541 * Define the "EARLY_PER_CPU" macros. These are used for some per_cpu
542 * variables that are initialized and accessed before there are per_cpu
546 #define DEFINE_EARLY_PER_CPU(_type, _name, _initvalue) \
547 DEFINE_PER_CPU(_type, _name) = _initvalue; \
548 __typeof__(_type) _name##_early_map[NR_CPUS] __initdata = \
549 { [0 ... NR_CPUS-1] = _initvalue }; \
550 __typeof__(_type) *_name##_early_ptr __refdata = _name##_early_map
552 #define DEFINE_EARLY_PER_CPU_READ_MOSTLY(_type, _name, _initvalue) \
553 DEFINE_PER_CPU_READ_MOSTLY(_type, _name) = _initvalue; \
554 __typeof__(_type) _name##_early_map[NR_CPUS] __initdata = \
555 { [0 ... NR_CPUS-1] = _initvalue }; \
556 __typeof__(_type) *_name##_early_ptr __refdata = _name##_early_map
558 #define EXPORT_EARLY_PER_CPU_SYMBOL(_name) \
559 EXPORT_PER_CPU_SYMBOL(_name)
561 #define DECLARE_EARLY_PER_CPU(_type, _name) \
562 DECLARE_PER_CPU(_type, _name); \
563 extern __typeof__(_type) *_name##_early_ptr; \
564 extern __typeof__(_type) _name##_early_map[]
566 #define DECLARE_EARLY_PER_CPU_READ_MOSTLY(_type, _name) \
567 DECLARE_PER_CPU_READ_MOSTLY(_type, _name); \
568 extern __typeof__(_type) *_name##_early_ptr; \
569 extern __typeof__(_type) _name##_early_map[]
571 #define early_per_cpu_ptr(_name) (_name##_early_ptr)
572 #define early_per_cpu_map(_name, _idx) (_name##_early_map[_idx])
573 #define early_per_cpu(_name, _cpu) \
574 *(early_per_cpu_ptr(_name) ? \
575 &early_per_cpu_ptr(_name)[_cpu] : \
576 &per_cpu(_name, _cpu))
578 #else /* !CONFIG_SMP */
579 #define DEFINE_EARLY_PER_CPU(_type, _name, _initvalue) \
580 DEFINE_PER_CPU(_type, _name) = _initvalue
582 #define DEFINE_EARLY_PER_CPU_READ_MOSTLY(_type, _name, _initvalue) \
583 DEFINE_PER_CPU_READ_MOSTLY(_type, _name) = _initvalue
585 #define EXPORT_EARLY_PER_CPU_SYMBOL(_name) \
586 EXPORT_PER_CPU_SYMBOL(_name)
588 #define DECLARE_EARLY_PER_CPU(_type, _name) \
589 DECLARE_PER_CPU(_type, _name)
591 #define DECLARE_EARLY_PER_CPU_READ_MOSTLY(_type, _name) \
592 DECLARE_PER_CPU_READ_MOSTLY(_type, _name)
594 #define early_per_cpu(_name, _cpu) per_cpu(_name, _cpu)
595 #define early_per_cpu_ptr(_name) NULL
596 /* no early_per_cpu_map() */
598 #endif /* !CONFIG_SMP */
600 #endif /* _ASM_X86_PERCPU_H */