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:per_cpu__this_cpu_off, reg; \
29 lea per_cpu__##var(reg), reg
30 #define PER_CPU_VAR(var) %__percpu_seg:per_cpu__##var
32 #define PER_CPU(var, reg) \
33 __percpu_mov_op $per_cpu__##var, reg
34 #define PER_CPU_VAR(var) per_cpu__##var
37 #ifdef CONFIG_X86_64_SMP
38 #define INIT_PER_CPU_VAR(var) init_per_cpu__##var
40 #define INIT_PER_CPU_VAR(var) per_cpu__##var
43 #else /* ...!ASSEMBLY */
45 #include <linux/stringify.h>
46 #include <asm/sections.h>
48 #define __addr_to_pcpu_ptr(addr) \
49 (void *)((unsigned long)(addr) - (unsigned long)pcpu_base_addr \
50 + (unsigned long)__per_cpu_start)
51 #define __pcpu_ptr_to_addr(ptr) \
52 (void *)((unsigned long)(ptr) + (unsigned long)pcpu_base_addr \
53 - (unsigned long)__per_cpu_start)
56 #define __percpu_arg(x) "%%"__stringify(__percpu_seg)":%P" #x
57 #define __my_cpu_offset percpu_read(this_cpu_off)
59 #define __percpu_arg(x) "%" #x
63 * Initialized pointers to per-cpu variables needed for the boot
64 * processor need to use these macros to get the proper address
65 * offset from __per_cpu_load on SMP.
67 * There also must be an entry in vmlinux_64.lds.S
69 #define DECLARE_INIT_PER_CPU(var) \
70 extern typeof(per_cpu_var(var)) init_per_cpu_var(var)
72 #ifdef CONFIG_X86_64_SMP
73 #define init_per_cpu_var(var) init_per_cpu__##var
75 #define init_per_cpu_var(var) per_cpu_var(var)
78 /* For arch-specific code, we can use direct single-insn ops (they
79 * don't give an lvalue though). */
80 extern void __bad_percpu_size(void);
82 #define percpu_to_op(op, var, val) \
84 typedef typeof(var) T__; \
89 switch (sizeof(var)) { \
91 asm(op "b %1,"__percpu_arg(0) \
96 asm(op "w %1,"__percpu_arg(0) \
101 asm(op "l %1,"__percpu_arg(0) \
103 : "ri" ((T__)val)); \
106 asm(op "q %1,"__percpu_arg(0) \
108 : "re" ((T__)val)); \
110 default: __bad_percpu_size(); \
114 #define percpu_from_op(op, var) \
117 switch (sizeof(var)) { \
119 asm(op "b "__percpu_arg(1)",%0" \
124 asm(op "w "__percpu_arg(1)",%0" \
129 asm(op "l "__percpu_arg(1)",%0" \
134 asm(op "q "__percpu_arg(1)",%0" \
138 default: __bad_percpu_size(); \
143 #define percpu_read(var) percpu_from_op("mov", per_cpu__##var)
144 #define percpu_write(var, val) percpu_to_op("mov", per_cpu__##var, val)
145 #define percpu_add(var, val) percpu_to_op("add", per_cpu__##var, val)
146 #define percpu_sub(var, val) percpu_to_op("sub", per_cpu__##var, val)
147 #define percpu_and(var, val) percpu_to_op("and", per_cpu__##var, val)
148 #define percpu_or(var, val) percpu_to_op("or", per_cpu__##var, val)
149 #define percpu_xor(var, val) percpu_to_op("xor", per_cpu__##var, val)
151 /* This is not atomic against other CPUs -- CPU preemption needs to be off */
152 #define x86_test_and_clear_bit_percpu(bit, var) \
155 asm volatile("btr %2,"__percpu_arg(1)"\n\tsbbl %0,%0" \
156 : "=r" (old__), "+m" (per_cpu__##var) \
161 #include <asm-generic/percpu.h>
163 /* We can use this directly for local CPU (faster). */
164 DECLARE_PER_CPU(unsigned long, this_cpu_off);
166 #endif /* !__ASSEMBLY__ */
171 * Define the "EARLY_PER_CPU" macros. These are used for some per_cpu
172 * variables that are initialized and accessed before there are per_cpu
176 #define DEFINE_EARLY_PER_CPU(_type, _name, _initvalue) \
177 DEFINE_PER_CPU(_type, _name) = _initvalue; \
178 __typeof__(_type) _name##_early_map[NR_CPUS] __initdata = \
179 { [0 ... NR_CPUS-1] = _initvalue }; \
180 __typeof__(_type) *_name##_early_ptr __refdata = _name##_early_map
182 #define EXPORT_EARLY_PER_CPU_SYMBOL(_name) \
183 EXPORT_PER_CPU_SYMBOL(_name)
185 #define DECLARE_EARLY_PER_CPU(_type, _name) \
186 DECLARE_PER_CPU(_type, _name); \
187 extern __typeof__(_type) *_name##_early_ptr; \
188 extern __typeof__(_type) _name##_early_map[]
190 #define early_per_cpu_ptr(_name) (_name##_early_ptr)
191 #define early_per_cpu_map(_name, _idx) (_name##_early_map[_idx])
192 #define early_per_cpu(_name, _cpu) \
193 *(early_per_cpu_ptr(_name) ? \
194 &early_per_cpu_ptr(_name)[_cpu] : \
195 &per_cpu(_name, _cpu))
197 #else /* !CONFIG_SMP */
198 #define DEFINE_EARLY_PER_CPU(_type, _name, _initvalue) \
199 DEFINE_PER_CPU(_type, _name) = _initvalue
201 #define EXPORT_EARLY_PER_CPU_SYMBOL(_name) \
202 EXPORT_PER_CPU_SYMBOL(_name)
204 #define DECLARE_EARLY_PER_CPU(_type, _name) \
205 DECLARE_PER_CPU(_type, _name)
207 #define early_per_cpu(_name, _cpu) per_cpu(_name, _cpu)
208 #define early_per_cpu_ptr(_name) NULL
209 /* no early_per_cpu_map() */
211 #endif /* !CONFIG_SMP */
213 #endif /* _ASM_X86_PERCPU_H */