4 #include <linux/kernel.h>
5 #include <asm/segment.h>
6 #include <asm/cmpxchg.h>
10 #define __SAVE(reg,offset) "movq %%" #reg ",(14-" #offset ")*8(%%rsp)\n\t"
11 #define __RESTORE(reg,offset) "movq (14-" #offset ")*8(%%rsp),%%" #reg "\n\t"
13 /* frame pointer must be last for get_wchan */
14 #define SAVE_CONTEXT "pushf ; pushq %%rbp ; movq %%rsi,%%rbp\n\t"
15 #define RESTORE_CONTEXT "movq %%rbp,%%rsi ; popq %%rbp ; popf\t"
17 #define __EXTRA_CLOBBER \
18 ,"rcx","rbx","rdx","r8","r9","r10","r11","r12","r13","r14","r15"
20 /* Save restore flags to clear handle leaking NT */
21 #define switch_to(prev,next,last) \
22 asm volatile(SAVE_CONTEXT \
23 "movq %%rsp,%P[threadrsp](%[prev])\n\t" /* save RSP */ \
24 "movq %P[threadrsp](%[next]),%%rsp\n\t" /* restore RSP */ \
25 "call __switch_to\n\t" \
26 ".globl thread_return\n" \
27 "thread_return:\n\t" \
28 "movq %%gs:%P[pda_pcurrent],%%rsi\n\t" \
29 "movq %P[thread_info](%%rsi),%%r8\n\t" \
30 LOCK_PREFIX "btr %[tif_fork],%P[ti_flags](%%r8)\n\t" \
31 "movq %%rax,%%rdi\n\t" \
32 "jc ret_from_fork\n\t" \
35 : [next] "S" (next), [prev] "D" (prev), \
36 [threadrsp] "i" (offsetof(struct task_struct, thread.rsp)), \
37 [ti_flags] "i" (offsetof(struct thread_info, flags)),\
38 [tif_fork] "i" (TIF_FORK), \
39 [thread_info] "i" (offsetof(struct task_struct, stack)), \
40 [pda_pcurrent] "i" (offsetof(struct x8664_pda, pcurrent)) \
41 : "memory", "cc" __EXTRA_CLOBBER)
43 extern void load_gs_index(unsigned);
46 * Load a segment. Fall back on loading the zero
47 * segment if something goes wrong..
49 #define loadsegment(seg,value) \
52 "movl %k0,%%" #seg "\n" \
54 ".section .fixup,\"ax\"\n" \
56 "movl %1,%%" #seg "\n\t" \
59 ".section __ex_table,\"a\"\n\t" \
63 : :"r" (value), "r" (0))
66 * Clear and set 'TS' bit respectively
68 #define clts() __asm__ __volatile__ ("clts")
70 static inline unsigned long read_cr0(void)
73 asm volatile("movq %%cr0,%0" : "=r" (cr0));
77 static inline void write_cr0(unsigned long val)
79 asm volatile("movq %0,%%cr0" :: "r" (val));
82 static inline unsigned long read_cr2(void)
85 asm volatile("movq %%cr2,%0" : "=r" (cr2));
89 static inline void write_cr2(unsigned long val)
91 asm volatile("movq %0,%%cr2" :: "r" (val));
94 static inline unsigned long read_cr3(void)
97 asm volatile("movq %%cr3,%0" : "=r" (cr3));
101 static inline void write_cr3(unsigned long val)
103 asm volatile("movq %0,%%cr3" :: "r" (val) : "memory");
106 static inline unsigned long read_cr4(void)
109 asm volatile("movq %%cr4,%0" : "=r" (cr4));
113 static inline void write_cr4(unsigned long val)
115 asm volatile("movq %0,%%cr4" :: "r" (val) : "memory");
118 static inline unsigned long read_cr8(void)
121 asm volatile("movq %%cr8,%0" : "=r" (cr8));
125 static inline void write_cr8(unsigned long val)
127 asm volatile("movq %0,%%cr8" :: "r" (val) : "memory");
130 #define stts() write_cr0(8 | read_cr0())
133 __asm__ __volatile__ ("wbinvd": : :"memory")
135 #endif /* __KERNEL__ */
137 static inline void clflush(volatile void *__p)
139 asm volatile("clflush %0" : "+m" (*(char __force *)__p));
142 #define nop() __asm__ __volatile__ ("nop")
145 #define smp_mb() mb()
146 #define smp_rmb() barrier()
147 #define smp_wmb() barrier()
148 #define smp_read_barrier_depends() do {} while(0)
150 #define smp_mb() barrier()
151 #define smp_rmb() barrier()
152 #define smp_wmb() barrier()
153 #define smp_read_barrier_depends() do {} while(0)
158 * Force strict CPU ordering.
159 * And yes, this is required on UP too when we're talking
162 #define mb() asm volatile("mfence":::"memory")
163 #define rmb() asm volatile("lfence":::"memory")
164 #define wmb() asm volatile("sfence" ::: "memory")
166 #define read_barrier_depends() do {} while(0)
167 #define set_mb(var, value) do { (void) xchg(&var, value); } while (0)
169 #define warn_if_not_ulong(x) do { unsigned long foo; (void) (&(x) == &foo); } while (0)
171 #include <linux/irqflags.h>
173 void cpu_idle_wait(void);
175 extern unsigned long arch_align_stack(unsigned long sp);
176 extern void free_init_pages(char *what, unsigned long begin, unsigned long end);