|
2 | 2 | #define _ASM_X86_SWITCH_TO_H |
3 | 3 |
|
4 | 4 | struct task_struct; /* one of the stranger aspects of C forward declarations */ |
| 5 | + |
| 6 | +struct task_struct *__switch_to_asm(struct task_struct *prev, |
| 7 | + struct task_struct *next); |
| 8 | + |
5 | 9 | __visible struct task_struct *__switch_to(struct task_struct *prev, |
6 | | - struct task_struct *next); |
| 10 | + struct task_struct *next); |
7 | 11 | struct tss_struct; |
8 | 12 | void __switch_to_xtra(struct task_struct *prev_p, struct task_struct *next_p, |
9 | 13 | struct tss_struct *tss); |
@@ -32,131 +36,30 @@ static inline void prepare_switch_to(struct task_struct *prev, |
32 | 36 |
|
33 | 37 | /* data that is pointed to by thread.sp */ |
34 | 38 | struct inactive_task_frame { |
| 39 | +#ifdef CONFIG_X86_64 |
| 40 | + unsigned long r15; |
| 41 | + unsigned long r14; |
| 42 | + unsigned long r13; |
| 43 | + unsigned long r12; |
| 44 | +#else |
| 45 | + unsigned long si; |
| 46 | + unsigned long di; |
| 47 | +#endif |
| 48 | + unsigned long bx; |
35 | 49 | unsigned long bp; |
| 50 | + unsigned long ret_addr; |
36 | 51 | }; |
37 | 52 |
|
38 | | -#ifdef CONFIG_X86_32 |
39 | | - |
40 | | -#ifdef CONFIG_CC_STACKPROTECTOR |
41 | | -#define __switch_canary \ |
42 | | - "movl %P[task_canary](%[next]), %%ebx\n\t" \ |
43 | | - "movl %%ebx, "__percpu_arg([stack_canary])"\n\t" |
44 | | -#define __switch_canary_oparam \ |
45 | | - , [stack_canary] "=m" (stack_canary.canary) |
46 | | -#define __switch_canary_iparam \ |
47 | | - , [task_canary] "i" (offsetof(struct task_struct, stack_canary)) |
48 | | -#else /* CC_STACKPROTECTOR */ |
49 | | -#define __switch_canary |
50 | | -#define __switch_canary_oparam |
51 | | -#define __switch_canary_iparam |
52 | | -#endif /* CC_STACKPROTECTOR */ |
| 53 | +struct fork_frame { |
| 54 | + struct inactive_task_frame frame; |
| 55 | + struct pt_regs regs; |
| 56 | +}; |
53 | 57 |
|
54 | | -/* |
55 | | - * Saving eflags is important. It switches not only IOPL between tasks, |
56 | | - * it also protects other tasks from NT leaking through sysenter etc. |
57 | | - */ |
58 | 58 | #define switch_to(prev, next, last) \ |
59 | 59 | do { \ |
60 | | - /* \ |
61 | | - * Context-switching clobbers all registers, so we clobber \ |
62 | | - * them explicitly, via unused output variables. \ |
63 | | - * (EAX and EBP is not listed because EBP is saved/restored \ |
64 | | - * explicitly for wchan access and EAX is the return value of \ |
65 | | - * __switch_to()) \ |
66 | | - */ \ |
67 | | - unsigned long ebx, ecx, edx, esi, edi; \ |
68 | | - \ |
69 | 60 | prepare_switch_to(prev, next); \ |
70 | 61 | \ |
71 | | - asm volatile("pushl %%ebp\n\t" /* save EBP */ \ |
72 | | - "movl %%esp,%[prev_sp]\n\t" /* save ESP */ \ |
73 | | - "movl %[next_sp],%%esp\n\t" /* restore ESP */ \ |
74 | | - "movl $1f,%[prev_ip]\n\t" /* save EIP */ \ |
75 | | - "pushl %[next_ip]\n\t" /* restore EIP */ \ |
76 | | - __switch_canary \ |
77 | | - "jmp __switch_to\n" /* regparm call */ \ |
78 | | - "1:\t" \ |
79 | | - "popl %%ebp\n\t" /* restore EBP */ \ |
80 | | - \ |
81 | | - /* output parameters */ \ |
82 | | - : [prev_sp] "=m" (prev->thread.sp), \ |
83 | | - [prev_ip] "=m" (prev->thread.ip), \ |
84 | | - "=a" (last), \ |
85 | | - \ |
86 | | - /* clobbered output registers: */ \ |
87 | | - "=b" (ebx), "=c" (ecx), "=d" (edx), \ |
88 | | - "=S" (esi), "=D" (edi) \ |
89 | | - \ |
90 | | - __switch_canary_oparam \ |
91 | | - \ |
92 | | - /* input parameters: */ \ |
93 | | - : [next_sp] "m" (next->thread.sp), \ |
94 | | - [next_ip] "m" (next->thread.ip), \ |
95 | | - \ |
96 | | - /* regparm parameters for __switch_to(): */ \ |
97 | | - [prev] "a" (prev), \ |
98 | | - [next] "d" (next) \ |
99 | | - \ |
100 | | - __switch_canary_iparam \ |
101 | | - \ |
102 | | - : /* reloaded segment registers */ \ |
103 | | - "memory"); \ |
| 62 | + ((last) = __switch_to_asm((prev), (next))); \ |
104 | 63 | } while (0) |
105 | 64 |
|
106 | | -#else /* CONFIG_X86_32 */ |
107 | | - |
108 | | -/* frame pointer must be last for get_wchan */ |
109 | | -#define SAVE_CONTEXT "pushq %%rbp ; movq %%rsi,%%rbp\n\t" |
110 | | -#define RESTORE_CONTEXT "movq %%rbp,%%rsi ; popq %%rbp\t" |
111 | | - |
112 | | -#define __EXTRA_CLOBBER \ |
113 | | - , "rcx", "rbx", "rdx", "r8", "r9", "r10", "r11", \ |
114 | | - "r12", "r13", "r14", "r15", "flags" |
115 | | - |
116 | | -#ifdef CONFIG_CC_STACKPROTECTOR |
117 | | -#define __switch_canary \ |
118 | | - "movq %P[task_canary](%%rsi),%%r8\n\t" \ |
119 | | - "movq %%r8,"__percpu_arg([gs_canary])"\n\t" |
120 | | -#define __switch_canary_oparam \ |
121 | | - , [gs_canary] "=m" (irq_stack_union.stack_canary) |
122 | | -#define __switch_canary_iparam \ |
123 | | - , [task_canary] "i" (offsetof(struct task_struct, stack_canary)) |
124 | | -#else /* CC_STACKPROTECTOR */ |
125 | | -#define __switch_canary |
126 | | -#define __switch_canary_oparam |
127 | | -#define __switch_canary_iparam |
128 | | -#endif /* CC_STACKPROTECTOR */ |
129 | | - |
130 | | -/* |
131 | | - * There is no need to save or restore flags, because flags are always |
132 | | - * clean in kernel mode, with the possible exception of IOPL. Kernel IOPL |
133 | | - * has no effect. |
134 | | - */ |
135 | | -#define switch_to(prev, next, last) \ |
136 | | - prepare_switch_to(prev, next); \ |
137 | | - \ |
138 | | - asm volatile(SAVE_CONTEXT \ |
139 | | - "movq %%rsp,%P[threadrsp](%[prev])\n\t" /* save RSP */ \ |
140 | | - "movq %P[threadrsp](%[next]),%%rsp\n\t" /* restore RSP */ \ |
141 | | - "call __switch_to\n\t" \ |
142 | | - "movq "__percpu_arg([current_task])",%%rsi\n\t" \ |
143 | | - __switch_canary \ |
144 | | - "movq %P[thread_info](%%rsi),%%r8\n\t" \ |
145 | | - "movq %%rax,%%rdi\n\t" \ |
146 | | - "testl %[_tif_fork],%P[ti_flags](%%r8)\n\t" \ |
147 | | - "jnz ret_from_fork\n\t" \ |
148 | | - RESTORE_CONTEXT \ |
149 | | - : "=a" (last) \ |
150 | | - __switch_canary_oparam \ |
151 | | - : [next] "S" (next), [prev] "D" (prev), \ |
152 | | - [threadrsp] "i" (offsetof(struct task_struct, thread.sp)), \ |
153 | | - [ti_flags] "i" (offsetof(struct thread_info, flags)), \ |
154 | | - [_tif_fork] "i" (_TIF_FORK), \ |
155 | | - [thread_info] "i" (offsetof(struct task_struct, stack)), \ |
156 | | - [current_task] "m" (current_task) \ |
157 | | - __switch_canary_iparam \ |
158 | | - : "memory", "cc" __EXTRA_CLOBBER) |
159 | | - |
160 | | -#endif /* CONFIG_X86_32 */ |
161 | | - |
162 | 65 | #endif /* _ASM_X86_SWITCH_TO_H */ |
0 commit comments