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
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
44 #include <linux/kernel.h>
48 #define __percpu_prefix "%%"__stringify(__percpu_seg)":"
49 #define __my_cpu_offset this_cpu_read(this_cpu_off)
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
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
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(); \
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)) ? (val) : 0; \
137 switch (sizeof(var)) { \
140 asm("incb "__percpu_arg(0) : "+m" (var)); \
141 else if (pao_ID__ == -1) \
142 asm("decb "__percpu_arg(0) : "+m" (var)); \
144 asm("addb %1, "__percpu_arg(0) \
146 : "qi" ((pao_T__)(val))); \
150 asm("incw "__percpu_arg(0) : "+m" (var)); \
151 else if (pao_ID__ == -1) \
152 asm("decw "__percpu_arg(0) : "+m" (var)); \
154 asm("addw %1, "__percpu_arg(0) \
156 : "ri" ((pao_T__)(val))); \
160 asm("incl "__percpu_arg(0) : "+m" (var)); \
161 else if (pao_ID__ == -1) \
162 asm("decl "__percpu_arg(0) : "+m" (var)); \
164 asm("addl %1, "__percpu_arg(0) \
166 : "ri" ((pao_T__)(val))); \
170 asm("incq "__percpu_arg(0) : "+m" (var)); \
171 else if (pao_ID__ == -1) \
172 asm("decq "__percpu_arg(0) : "+m" (var)); \
174 asm("addq %1, "__percpu_arg(0) \
176 : "re" ((pao_T__)(val))); \
178 default: __bad_percpu_size(); \
182 #define percpu_from_op(op, var, constraint) \
184 typeof(var) pfo_ret__; \
185 switch (sizeof(var)) { \
187 asm(op "b "__percpu_arg(1)",%0" \
192 asm(op "w "__percpu_arg(1)",%0" \
197 asm(op "l "__percpu_arg(1)",%0" \
202 asm(op "q "__percpu_arg(1)",%0" \
206 default: __bad_percpu_size(); \
211 #define percpu_unary_op(op, var) \
213 switch (sizeof(var)) { \
215 asm(op "b "__percpu_arg(0) \
219 asm(op "w "__percpu_arg(0) \
223 asm(op "l "__percpu_arg(0) \
227 asm(op "q "__percpu_arg(0) \
230 default: __bad_percpu_size(); \
237 #define percpu_add_return_op(var, val) \
239 typeof(var) paro_ret__ = val; \
240 switch (sizeof(var)) { \
242 asm("xaddb %0, "__percpu_arg(1) \
243 : "+q" (paro_ret__), "+m" (var) \
247 asm("xaddw %0, "__percpu_arg(1) \
248 : "+r" (paro_ret__), "+m" (var) \
252 asm("xaddl %0, "__percpu_arg(1) \
253 : "+r" (paro_ret__), "+m" (var) \
257 asm("xaddq %0, "__percpu_arg(1) \
258 : "+re" (paro_ret__), "+m" (var) \
261 default: __bad_percpu_size(); \
272 #define percpu_xchg_op(var, nval) \
274 typeof(var) pxo_ret__; \
275 typeof(var) pxo_new__ = (nval); \
276 switch (sizeof(var)) { \
278 asm("\n\tmov "__percpu_arg(1)",%%al" \
279 "\n1:\tcmpxchgb %2, "__percpu_arg(1) \
281 : "=&a" (pxo_ret__), "+m" (var) \
286 asm("\n\tmov "__percpu_arg(1)",%%ax" \
287 "\n1:\tcmpxchgw %2, "__percpu_arg(1) \
289 : "=&a" (pxo_ret__), "+m" (var) \
294 asm("\n\tmov "__percpu_arg(1)",%%eax" \
295 "\n1:\tcmpxchgl %2, "__percpu_arg(1) \
297 : "=&a" (pxo_ret__), "+m" (var) \
302 asm("\n\tmov "__percpu_arg(1)",%%rax" \
303 "\n1:\tcmpxchgq %2, "__percpu_arg(1) \
305 : "=&a" (pxo_ret__), "+m" (var) \
309 default: __bad_percpu_size(); \
318 #define percpu_cmpxchg_op(var, oval, nval) \
320 typeof(var) pco_ret__; \
321 typeof(var) pco_old__ = (oval); \
322 typeof(var) pco_new__ = (nval); \
323 switch (sizeof(var)) { \
325 asm("cmpxchgb %2, "__percpu_arg(1) \
326 : "=a" (pco_ret__), "+m" (var) \
327 : "q" (pco_new__), "0" (pco_old__) \
331 asm("cmpxchgw %2, "__percpu_arg(1) \
332 : "=a" (pco_ret__), "+m" (var) \
333 : "r" (pco_new__), "0" (pco_old__) \
337 asm("cmpxchgl %2, "__percpu_arg(1) \
338 : "=a" (pco_ret__), "+m" (var) \
339 : "r" (pco_new__), "0" (pco_old__) \
343 asm("cmpxchgq %2, "__percpu_arg(1) \
344 : "=a" (pco_ret__), "+m" (var) \
345 : "r" (pco_new__), "0" (pco_old__) \
348 default: __bad_percpu_size(); \
362 #define this_cpu_read_stable(var) percpu_from_op("mov", var, "p" (&(var)))
364 #define __this_cpu_read_1(pcp) percpu_from_op("mov", (pcp), "m"(pcp))
365 #define __this_cpu_read_2(pcp) percpu_from_op("mov", (pcp), "m"(pcp))
366 #define __this_cpu_read_4(pcp) percpu_from_op("mov", (pcp), "m"(pcp))
368 #define __this_cpu_write_1(pcp, val) percpu_to_op("mov", (pcp), val)
369 #define __this_cpu_write_2(pcp, val) percpu_to_op("mov", (pcp), val)
370 #define __this_cpu_write_4(pcp, val) percpu_to_op("mov", (pcp), val)
371 #define __this_cpu_add_1(pcp, val) percpu_add_op((pcp), val)
372 #define __this_cpu_add_2(pcp, val) percpu_add_op((pcp), val)
373 #define __this_cpu_add_4(pcp, val) percpu_add_op((pcp), val)
374 #define __this_cpu_and_1(pcp, val) percpu_to_op("and", (pcp), val)
375 #define __this_cpu_and_2(pcp, val) percpu_to_op("and", (pcp), val)
376 #define __this_cpu_and_4(pcp, val) percpu_to_op("and", (pcp), val)
377 #define __this_cpu_or_1(pcp, val) percpu_to_op("or", (pcp), val)
378 #define __this_cpu_or_2(pcp, val) percpu_to_op("or", (pcp), val)
379 #define __this_cpu_or_4(pcp, val) percpu_to_op("or", (pcp), val)
380 #define __this_cpu_xor_1(pcp, val) percpu_to_op("xor", (pcp), val)
381 #define __this_cpu_xor_2(pcp, val) percpu_to_op("xor", (pcp), val)
382 #define __this_cpu_xor_4(pcp, val) percpu_to_op("xor", (pcp), val)
383 #define __this_cpu_xchg_1(pcp, val) percpu_xchg_op(pcp, val)
384 #define __this_cpu_xchg_2(pcp, val) percpu_xchg_op(pcp, val)
385 #define __this_cpu_xchg_4(pcp, val) percpu_xchg_op(pcp, val)
387 #define this_cpu_read_1(pcp) percpu_from_op("mov", (pcp), "m"(pcp))
388 #define this_cpu_read_2(pcp) percpu_from_op("mov", (pcp), "m"(pcp))
389 #define this_cpu_read_4(pcp) percpu_from_op("mov", (pcp), "m"(pcp))
390 #define this_cpu_write_1(pcp, val) percpu_to_op("mov", (pcp), val)
391 #define this_cpu_write_2(pcp, val) percpu_to_op("mov", (pcp), val)
392 #define this_cpu_write_4(pcp, val) percpu_to_op("mov", (pcp), val)
393 #define this_cpu_add_1(pcp, val) percpu_add_op((pcp), val)
394 #define this_cpu_add_2(pcp, val) percpu_add_op((pcp), val)
395 #define this_cpu_add_4(pcp, val) percpu_add_op((pcp), val)
396 #define this_cpu_and_1(pcp, val) percpu_to_op("and", (pcp), val)
397 #define this_cpu_and_2(pcp, val) percpu_to_op("and", (pcp), val)
398 #define this_cpu_and_4(pcp, val) percpu_to_op("and", (pcp), val)
399 #define this_cpu_or_1(pcp, val) percpu_to_op("or", (pcp), val)
400 #define this_cpu_or_2(pcp, val) percpu_to_op("or", (pcp), val)
401 #define this_cpu_or_4(pcp, val) percpu_to_op("or", (pcp), val)
402 #define this_cpu_xor_1(pcp, val) percpu_to_op("xor", (pcp), val)
403 #define this_cpu_xor_2(pcp, val) percpu_to_op("xor", (pcp), val)
404 #define this_cpu_xor_4(pcp, val) percpu_to_op("xor", (pcp), val)
405 #define this_cpu_xchg_1(pcp, nval) percpu_xchg_op(pcp, nval)
406 #define this_cpu_xchg_2(pcp, nval) percpu_xchg_op(pcp, nval)
407 #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)
426 #ifdef CONFIG_X86_CMPXCHG64
427 #define percpu_cmpxchg8b_double(pcp1, pcp2, o1, o2, n1, n2) \
430 typeof(pcp1) __o1 = (o1), __n1 = (n1); \
431 typeof(pcp2) __o2 = (o2), __n2 = (n2); \
432 asm volatile("cmpxchg8b "__percpu_arg(1)"\n\tsetz %0\n\t" \
433 : "=a" (__ret), "+m" (pcp1), "+m" (pcp2), "+d" (__o2) \
434 : "b" (__n1), "c" (__n2), "a" (__o1)); \
438 #define __this_cpu_cmpxchg_double_4 percpu_cmpxchg8b_double
439 #define this_cpu_cmpxchg_double_4 percpu_cmpxchg8b_double
447 #define __this_cpu_read_8(pcp) percpu_from_op("mov", (pcp), "m"(pcp))
448 #define __this_cpu_write_8(pcp, val) percpu_to_op("mov", (pcp), val)
449 #define __this_cpu_add_8(pcp, val) percpu_add_op((pcp), val)
450 #define __this_cpu_and_8(pcp, val) percpu_to_op("and", (pcp), val)
451 #define __this_cpu_or_8(pcp, val) percpu_to_op("or", (pcp), val)
452 #define __this_cpu_xor_8(pcp, val) percpu_to_op("xor", (pcp), val)
453 #define __this_cpu_add_return_8(pcp, val) percpu_add_return_op(pcp, val)
454 #define __this_cpu_xchg_8(pcp, nval) percpu_xchg_op(pcp, nval)
455 #define __this_cpu_cmpxchg_8(pcp, oval, nval) percpu_cmpxchg_op(pcp, oval, nval)
457 #define this_cpu_read_8(pcp) percpu_from_op("mov", (pcp), "m"(pcp))
458 #define this_cpu_write_8(pcp, val) percpu_to_op("mov", (pcp), val)
459 #define this_cpu_add_8(pcp, val) percpu_add_op((pcp), val)
460 #define this_cpu_and_8(pcp, val) percpu_to_op("and", (pcp), val)
461 #define this_cpu_or_8(pcp, val) percpu_to_op("or", (pcp), val)
462 #define this_cpu_xor_8(pcp, val) percpu_to_op("xor", (pcp), val)
463 #define this_cpu_add_return_8(pcp, val) percpu_add_return_op(pcp, val)
464 #define this_cpu_xchg_8(pcp, nval) percpu_xchg_op(pcp, nval)
465 #define this_cpu_cmpxchg_8(pcp, oval, nval) percpu_cmpxchg_op(pcp, oval, nval)
473 #define percpu_cmpxchg16b_double(pcp1, pcp2, o1, o2, n1, n2) \
476 typeof(pcp1) __o1 = (o1), __n1 = (n1); \
477 typeof(pcp2) __o2 = (o2), __n2 = (n2); \
478 alternative_io("leaq %P1,%%rsi\n\tcall this_cpu_cmpxchg16b_emu\n\t", \
479 "cmpxchg16b " __percpu_arg(1) "\n\tsetz %0\n\t", \
481 ASM_OUTPUT2("=a" (__ret), "+m" (pcp1), \
482 "+m" (pcp2), "+d" (__o2)), \
483 "b" (__n1), "c" (__n2), "a" (__o1) : "rsi"); \
487 #define __this_cpu_cmpxchg_double_8 percpu_cmpxchg16b_double
488 #define this_cpu_cmpxchg_double_8 percpu_cmpxchg16b_double
493 #define x86_test_and_clear_bit_percpu(bit, var) \
496 asm volatile("btr %2,"__percpu_arg(1)"\n\tsbbl %0,%0" \
497 : "=r" (old__), "+m" (var) \
514 static inline int x86_this_cpu_variable_test_bit(
int nr,
522 :
"m" (*(
unsigned long *)
addr),
"Ir" (nr));
527 #define x86_this_cpu_test_bit(nr, addr) \
528 (__builtin_constant_p((nr)) \
529 ? x86_this_cpu_constant_test_bit((nr), (addr)) \
530 : x86_this_cpu_variable_test_bit((nr), (addr)))
548 #define DEFINE_EARLY_PER_CPU(_type, _name, _initvalue) \
549 DEFINE_PER_CPU(_type, _name) = _initvalue; \
550 __typeof__(_type) _name##_early_map[NR_CPUS] __initdata = \
551 { [0 ... NR_CPUS-1] = _initvalue }; \
552 __typeof__(_type) *_name##_early_ptr __refdata = _name##_early_map
554 #define DEFINE_EARLY_PER_CPU_READ_MOSTLY(_type, _name, _initvalue) \
555 DEFINE_PER_CPU_READ_MOSTLY(_type, _name) = _initvalue; \
556 __typeof__(_type) _name##_early_map[NR_CPUS] __initdata = \
557 { [0 ... NR_CPUS-1] = _initvalue }; \
558 __typeof__(_type) *_name##_early_ptr __refdata = _name##_early_map
560 #define EXPORT_EARLY_PER_CPU_SYMBOL(_name) \
561 EXPORT_PER_CPU_SYMBOL(_name)
563 #define DECLARE_EARLY_PER_CPU(_type, _name) \
564 DECLARE_PER_CPU(_type, _name); \
565 extern __typeof__(_type) *_name##_early_ptr; \
566 extern __typeof__(_type) _name##_early_map[]
568 #define DECLARE_EARLY_PER_CPU_READ_MOSTLY(_type, _name) \
569 DECLARE_PER_CPU_READ_MOSTLY(_type, _name); \
570 extern __typeof__(_type) *_name##_early_ptr; \
571 extern __typeof__(_type) _name##_early_map[]
573 #define early_per_cpu_ptr(_name) (_name##_early_ptr)
574 #define early_per_cpu_map(_name, _idx) (_name##_early_map[_idx])
575 #define early_per_cpu(_name, _cpu) \
576 *(early_per_cpu_ptr(_name) ? \
577 &early_per_cpu_ptr(_name)[_cpu] : \
578 &per_cpu(_name, _cpu))
581 #define DEFINE_EARLY_PER_CPU(_type, _name, _initvalue) \
582 DEFINE_PER_CPU(_type, _name) = _initvalue
584 #define DEFINE_EARLY_PER_CPU_READ_MOSTLY(_type, _name, _initvalue) \
585 DEFINE_PER_CPU_READ_MOSTLY(_type, _name) = _initvalue
587 #define EXPORT_EARLY_PER_CPU_SYMBOL(_name) \
588 EXPORT_PER_CPU_SYMBOL(_name)
590 #define DECLARE_EARLY_PER_CPU(_type, _name) \
591 DECLARE_PER_CPU(_type, _name)
593 #define DECLARE_EARLY_PER_CPU_READ_MOSTLY(_type, _name) \
594 DECLARE_PER_CPU_READ_MOSTLY(_type, _name)
596 #define early_per_cpu(_name, _cpu) per_cpu(_name, _cpu)
597 #define early_per_cpu_ptr(_name) NULL