Commit | Line | Data |
---|---|---|
5a0015d6 CZ |
1 | /* |
2 | * arch/xtensa/kernel/process.c | |
3 | * | |
4 | * Xtensa Processor version. | |
5 | * | |
6 | * This file is subject to the terms and conditions of the GNU General Public | |
7 | * License. See the file "COPYING" in the main directory of this archive | |
8 | * for more details. | |
9 | * | |
10 | * Copyright (C) 2001 - 2005 Tensilica Inc. | |
11 | * | |
12 | * Joe Taylor <joe@tensilica.com, joetylr@yahoo.com> | |
13 | * Chris Zankel <chris@zankel.net> | |
14 | * Marc Gauthier <marc@tensilica.com, marc@alumni.uwaterloo.ca> | |
15 | * Kevin Chea | |
16 | */ | |
17 | ||
5a0015d6 CZ |
18 | #include <linux/errno.h> |
19 | #include <linux/sched.h> | |
b17b0153 | 20 | #include <linux/sched/debug.h> |
29930025 | 21 | #include <linux/sched/task.h> |
68db0cf1 | 22 | #include <linux/sched/task_stack.h> |
5a0015d6 CZ |
23 | #include <linux/kernel.h> |
24 | #include <linux/mm.h> | |
25 | #include <linux/smp.h> | |
5a0015d6 CZ |
26 | #include <linux/stddef.h> |
27 | #include <linux/unistd.h> | |
28 | #include <linux/ptrace.h> | |
5a0015d6 | 29 | #include <linux/elf.h> |
c91e02bd | 30 | #include <linux/hw_breakpoint.h> |
5a0015d6 CZ |
31 | #include <linux/init.h> |
32 | #include <linux/prctl.h> | |
33 | #include <linux/init_task.h> | |
34 | #include <linux/module.h> | |
35 | #include <linux/mqueue.h> | |
73089cbf | 36 | #include <linux/fs.h> |
5a0e3ad6 | 37 | #include <linux/slab.h> |
11ad47a0 | 38 | #include <linux/rcupdate.h> |
5a0015d6 | 39 | |
7c0f6ba6 | 40 | #include <linux/uaccess.h> |
5a0015d6 CZ |
41 | #include <asm/io.h> |
42 | #include <asm/processor.h> | |
43 | #include <asm/platform.h> | |
44 | #include <asm/mmu.h> | |
45 | #include <asm/irq.h> | |
60063497 | 46 | #include <linux/atomic.h> |
0013a854 | 47 | #include <asm/asm-offsets.h> |
173d6681 | 48 | #include <asm/regs.h> |
c91e02bd | 49 | #include <asm/hw_breakpoint.h> |
11e969bc | 50 | #include <asm/traps.h> |
5a0015d6 CZ |
51 | |
52 | extern void ret_from_fork(void); | |
3306a726 | 53 | extern void ret_from_kernel_thread(void); |
5a0015d6 | 54 | |
47f3fc94 AB |
55 | void (*pm_power_off)(void) = NULL; |
56 | EXPORT_SYMBOL(pm_power_off); | |
57 | ||
5a0015d6 | 58 | |
050e9baa | 59 | #ifdef CONFIG_STACKPROTECTOR |
40d1a07b MF |
60 | #include <linux/stackprotector.h> |
61 | unsigned long __stack_chk_guard __read_mostly; | |
62 | EXPORT_SYMBOL(__stack_chk_guard); | |
63 | #endif | |
64 | ||
c658eac6 CZ |
65 | #if XTENSA_HAVE_COPROCESSORS |
66 | ||
11e969bc | 67 | void local_coprocessors_flush_release_all(void) |
c658eac6 | 68 | { |
11e969bc MF |
69 | struct thread_info **coprocessor_owner; |
70 | struct thread_info *unique_owner[XCHAL_CP_MAX]; | |
71 | int n = 0; | |
72 | int i, j; | |
c658eac6 | 73 | |
11e969bc MF |
74 | coprocessor_owner = this_cpu_ptr(&exc_table)->coprocessor_owner; |
75 | xtensa_set_sr(XCHAL_CP_MASK, cpenable); | |
c658eac6 | 76 | |
11e969bc MF |
77 | for (i = 0; i < XCHAL_CP_MAX; i++) { |
78 | struct thread_info *ti = coprocessor_owner[i]; | |
c658eac6 | 79 | |
11e969bc MF |
80 | if (ti) { |
81 | coprocessor_flush(ti, i); | |
c658eac6 | 82 | |
11e969bc MF |
83 | for (j = 0; j < n; j++) |
84 | if (unique_owner[j] == ti) | |
85 | break; | |
86 | if (j == n) | |
87 | unique_owner[n++] = ti; | |
c658eac6 | 88 | |
11e969bc | 89 | coprocessor_owner[i] = NULL; |
c658eac6 CZ |
90 | } |
91 | } | |
11e969bc MF |
92 | for (i = 0; i < n; i++) { |
93 | /* pairs with memw (1) in fast_coprocessor and memw in switch_to */ | |
94 | smp_wmb(); | |
95 | unique_owner[i]->cpenable = 0; | |
96 | } | |
97 | xtensa_set_sr(0, cpenable); | |
98 | } | |
c658eac6 | 99 | |
11e969bc MF |
100 | static void local_coprocessor_release_all(void *info) |
101 | { | |
102 | struct thread_info *ti = info; | |
103 | struct thread_info **coprocessor_owner; | |
104 | int i; | |
105 | ||
106 | coprocessor_owner = this_cpu_ptr(&exc_table)->coprocessor_owner; | |
107 | ||
108 | /* Walk through all cp owners and release it for the requested one. */ | |
109 | ||
110 | for (i = 0; i < XCHAL_CP_MAX; i++) { | |
111 | if (coprocessor_owner[i] == ti) | |
112 | coprocessor_owner[i] = NULL; | |
113 | } | |
114 | /* pairs with memw (1) in fast_coprocessor and memw in switch_to */ | |
115 | smp_wmb(); | |
116 | ti->cpenable = 0; | |
be38e4f2 MF |
117 | if (ti == current_thread_info()) |
118 | xtensa_set_sr(0, cpenable); | |
11e969bc | 119 | } |
c658eac6 | 120 | |
11e969bc MF |
121 | void coprocessor_release_all(struct thread_info *ti) |
122 | { | |
123 | if (ti->cpenable) { | |
124 | /* pairs with memw (2) in fast_coprocessor */ | |
125 | smp_rmb(); | |
126 | smp_call_function_single(ti->cp_owner_cpu, | |
127 | local_coprocessor_release_all, | |
128 | ti, true); | |
129 | } | |
c658eac6 CZ |
130 | } |
131 | ||
11e969bc | 132 | static void local_coprocessor_flush_all(void *info) |
c658eac6 | 133 | { |
11e969bc MF |
134 | struct thread_info *ti = info; |
135 | struct thread_info **coprocessor_owner; | |
136 | unsigned long old_cpenable; | |
c658eac6 CZ |
137 | int i; |
138 | ||
11e969bc MF |
139 | coprocessor_owner = this_cpu_ptr(&exc_table)->coprocessor_owner; |
140 | old_cpenable = xtensa_xsr(ti->cpenable, cpenable); | |
c658eac6 CZ |
141 | |
142 | for (i = 0; i < XCHAL_CP_MAX; i++) { | |
11e969bc | 143 | if (coprocessor_owner[i] == ti) |
c658eac6 | 144 | coprocessor_flush(ti, i); |
c658eac6 | 145 | } |
cad6fade | 146 | xtensa_set_sr(old_cpenable, cpenable); |
11e969bc MF |
147 | } |
148 | ||
149 | void coprocessor_flush_all(struct thread_info *ti) | |
150 | { | |
151 | if (ti->cpenable) { | |
152 | /* pairs with memw (2) in fast_coprocessor */ | |
153 | smp_rmb(); | |
154 | smp_call_function_single(ti->cp_owner_cpu, | |
155 | local_coprocessor_flush_all, | |
156 | ti, true); | |
157 | } | |
158 | } | |
159 | ||
160 | static void local_coprocessor_flush_release_all(void *info) | |
161 | { | |
162 | local_coprocessor_flush_all(info); | |
163 | local_coprocessor_release_all(info); | |
164 | } | |
c658eac6 | 165 | |
11e969bc MF |
166 | void coprocessor_flush_release_all(struct thread_info *ti) |
167 | { | |
168 | if (ti->cpenable) { | |
169 | /* pairs with memw (2) in fast_coprocessor */ | |
170 | smp_rmb(); | |
171 | smp_call_function_single(ti->cp_owner_cpu, | |
172 | local_coprocessor_flush_release_all, | |
173 | ti, true); | |
174 | } | |
c658eac6 CZ |
175 | } |
176 | ||
177 | #endif | |
178 | ||
179 | ||
5a0015d6 CZ |
180 | /* |
181 | * Powermanagement idle function, if any is provided by the platform. | |
182 | */ | |
f4e2e9a4 | 183 | void arch_cpu_idle(void) |
5a0015d6 | 184 | { |
f4e2e9a4 | 185 | platform_idle(); |
5a0015d6 CZ |
186 | } |
187 | ||
188 | /* | |
c658eac6 | 189 | * This is called when the thread calls exit(). |
5a0015d6 | 190 | */ |
e6464694 | 191 | void exit_thread(struct task_struct *tsk) |
5a0015d6 | 192 | { |
c658eac6 | 193 | #if XTENSA_HAVE_COPROCESSORS |
e6464694 | 194 | coprocessor_release_all(task_thread_info(tsk)); |
c658eac6 | 195 | #endif |
5a0015d6 CZ |
196 | } |
197 | ||
c658eac6 CZ |
198 | /* |
199 | * Flush thread state. This is called when a thread does an execve() | |
200 | * Note that we flush coprocessor registers for the case execve fails. | |
201 | */ | |
5a0015d6 CZ |
202 | void flush_thread(void) |
203 | { | |
c658eac6 CZ |
204 | #if XTENSA_HAVE_COPROCESSORS |
205 | struct thread_info *ti = current_thread_info(); | |
11e969bc | 206 | coprocessor_flush_release_all(ti); |
c658eac6 | 207 | #endif |
c91e02bd | 208 | flush_ptrace_hw_breakpoint(current); |
c658eac6 CZ |
209 | } |
210 | ||
211 | /* | |
55ccf3fe SS |
212 | * this gets called so that we can store coprocessor state into memory and |
213 | * copy the current task into the new thread. | |
c658eac6 | 214 | */ |
55ccf3fe | 215 | int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src) |
c658eac6 CZ |
216 | { |
217 | #if XTENSA_HAVE_COPROCESSORS | |
55ccf3fe | 218 | coprocessor_flush_all(task_thread_info(src)); |
c658eac6 | 219 | #endif |
55ccf3fe SS |
220 | *dst = *src; |
221 | return 0; | |
5a0015d6 CZ |
222 | } |
223 | ||
224 | /* | |
225 | * Copy thread. | |
226 | * | |
3306a726 MF |
227 | * There are two modes in which this function is called: |
228 | * 1) Userspace thread creation, | |
229 | * regs != NULL, usp_thread_fn is userspace stack pointer. | |
230 | * It is expected to copy parent regs (in case CLONE_VM is not set | |
231 | * in the clone_flags) and set up passed usp in the childregs. | |
232 | * 2) Kernel thread creation, | |
233 | * regs == NULL, usp_thread_fn is the function to run in the new thread | |
234 | * and thread_fn_arg is its parameter. | |
235 | * childregs are not used for the kernel threads. | |
236 | * | |
5a0015d6 CZ |
237 | * The stack layout for the new thread looks like this: |
238 | * | |
3306a726 | 239 | * +------------------------+ |
5a0015d6 CZ |
240 | * | childregs | |
241 | * +------------------------+ <- thread.sp = sp in dummy-frame | |
242 | * | dummy-frame | (saved in dummy-frame spill-area) | |
243 | * +------------------------+ | |
244 | * | |
3306a726 MF |
245 | * We create a dummy frame to return to either ret_from_fork or |
246 | * ret_from_kernel_thread: | |
247 | * a0 points to ret_from_fork/ret_from_kernel_thread (simulating a call4) | |
5a0015d6 | 248 | * sp points to itself (thread.sp) |
3306a726 MF |
249 | * a2, a3 are unused for userspace threads, |
250 | * a2 points to thread_fn, a3 holds thread_fn arg for kernel threads. | |
5a0015d6 CZ |
251 | * |
252 | * Note: This is a pristine frame, so we don't need any spill region on top of | |
253 | * childregs. | |
84ed3053 MG |
254 | * |
255 | * The fun part: if we're keeping the same VM (i.e. cloning a thread, | |
256 | * not an entire process), we're normally given a new usp, and we CANNOT share | |
257 | * any live address register windows. If we just copy those live frames over, | |
258 | * the two threads (parent and child) will overflow the same frames onto the | |
259 | * parent stack at different times, likely corrupting the parent stack (esp. | |
260 | * if the parent returns from functions that called clone() and calls new | |
261 | * ones, before the child overflows its now old copies of its parent windows). | |
262 | * One solution is to spill windows to the parent stack, but that's fairly | |
263 | * involved. Much simpler to just not copy those live frames across. | |
5a0015d6 CZ |
264 | */ |
265 | ||
714acdbd | 266 | int copy_thread(unsigned long clone_flags, unsigned long usp_thread_fn, |
c346b94f AA |
267 | unsigned long thread_fn_arg, struct task_struct *p, |
268 | unsigned long tls) | |
5a0015d6 | 269 | { |
3306a726 | 270 | struct pt_regs *childregs = task_pt_regs(p); |
5a0015d6 | 271 | |
39070cb8 CZ |
272 | #if (XTENSA_HAVE_COPROCESSORS || XTENSA_HAVE_IO_PORTS) |
273 | struct thread_info *ti; | |
274 | #endif | |
275 | ||
0b537257 | 276 | #if defined(__XTENSA_WINDOWED_ABI__) |
5a0015d6 | 277 | /* Create a call4 dummy-frame: a0 = 0, a1 = childregs. */ |
062b1c19 MF |
278 | SPILL_SLOT(childregs, 1) = (unsigned long)childregs; |
279 | SPILL_SLOT(childregs, 0) = 0; | |
5a0015d6 | 280 | |
5a0015d6 | 281 | p->thread.sp = (unsigned long)childregs; |
0b537257 MF |
282 | #elif defined(__XTENSA_CALL0_ABI__) |
283 | /* Reserve 16 bytes for the _switch_to stack frame. */ | |
284 | p->thread.sp = (unsigned long)childregs - 16; | |
285 | #else | |
286 | #error Unsupported Xtensa ABI | |
287 | #endif | |
c658eac6 | 288 | |
4727dc20 | 289 | if (!(p->flags & (PF_KTHREAD | PF_IO_WORKER))) { |
3306a726 MF |
290 | struct pt_regs *regs = current_pt_regs(); |
291 | unsigned long usp = usp_thread_fn ? | |
292 | usp_thread_fn : regs->areg[1]; | |
293 | ||
294 | p->thread.ra = MAKE_RA_FOR_CALL( | |
295 | (unsigned long)ret_from_fork, 0x1); | |
5a0015d6 | 296 | |
3306a726 | 297 | *childregs = *regs; |
5a0015d6 | 298 | childregs->areg[1] = usp; |
3306a726 | 299 | childregs->areg[2] = 0; |
6ebe7da2 CZ |
300 | |
301 | /* When sharing memory with the parent thread, the child | |
302 | usually starts on a pristine stack, so we have to reset | |
303 | windowbase, windowstart and wmask. | |
304 | (Note that such a new thread is required to always create | |
305 | an initial call4 frame) | |
306 | The exception is vfork, where the new thread continues to | |
307 | run on the parent's stack until it calls execve. This could | |
308 | be a call8 or call12, which requires a legal stack frame | |
309 | of the previous caller for the overflow handlers to work. | |
310 | (Note that it's always legal to overflow live registers). | |
311 | In this case, ensure to spill at least the stack pointer | |
312 | of that frame. */ | |
313 | ||
84ed3053 | 314 | if (clone_flags & CLONE_VM) { |
6ebe7da2 CZ |
315 | /* check that caller window is live and same stack */ |
316 | int len = childregs->wmask & ~0xf; | |
317 | if (regs->areg[1] == usp && len != 0) { | |
318 | int callinc = (regs->areg[0] >> 30) & 3; | |
319 | int caller_ars = XCHAL_NUM_AREGS - callinc * 4; | |
320 | put_user(regs->areg[caller_ars+1], | |
321 | (unsigned __user*)(usp - 12)); | |
322 | } | |
323 | childregs->wmask = 1; | |
324 | childregs->windowstart = 1; | |
325 | childregs->windowbase = 0; | |
84ed3053 | 326 | } |
c50842df | 327 | |
5a0015d6 | 328 | if (clone_flags & CLONE_SETTLS) |
c346b94f | 329 | childregs->threadptr = tls; |
5a0015d6 | 330 | } else { |
3306a726 MF |
331 | p->thread.ra = MAKE_RA_FOR_CALL( |
332 | (unsigned long)ret_from_kernel_thread, 1); | |
333 | ||
0b537257 MF |
334 | /* pass parameters to ret_from_kernel_thread: */ |
335 | #if defined(__XTENSA_WINDOWED_ABI__) | |
336 | /* | |
337 | * a2 = thread_fn, a3 = thread_fn arg. | |
338 | * Window underflow will load registers from the | |
339 | * spill slots on the stack on return from _switch_to. | |
3306a726 | 340 | */ |
062b1c19 | 341 | SPILL_SLOT(childregs, 2) = usp_thread_fn; |
0b537257 MF |
342 | SPILL_SLOT(childregs, 3) = thread_fn_arg; |
343 | #elif defined(__XTENSA_CALL0_ABI__) | |
344 | /* | |
345 | * a12 = thread_fn, a13 = thread_fn arg. | |
346 | * _switch_to epilogue will load registers from the stack. | |
347 | */ | |
348 | ((unsigned long *)p->thread.sp)[0] = usp_thread_fn; | |
349 | ((unsigned long *)p->thread.sp)[1] = thread_fn_arg; | |
350 | #else | |
351 | #error Unsupported Xtensa ABI | |
352 | #endif | |
3306a726 MF |
353 | |
354 | /* Childregs are only used when we're going to userspace | |
355 | * in which case start_thread will set them up. | |
356 | */ | |
5a0015d6 | 357 | } |
c658eac6 CZ |
358 | |
359 | #if (XTENSA_HAVE_COPROCESSORS || XTENSA_HAVE_IO_PORTS) | |
360 | ti = task_thread_info(p); | |
361 | ti->cpenable = 0; | |
362 | #endif | |
363 | ||
c91e02bd MF |
364 | clear_ptrace_hw_breakpoint(p); |
365 | ||
5a0015d6 CZ |
366 | return 0; |
367 | } | |
368 | ||
369 | ||
5a0015d6 CZ |
370 | /* |
371 | * These bracket the sleeping functions.. | |
372 | */ | |
373 | ||
42a20f86 | 374 | unsigned long __get_wchan(struct task_struct *p) |
5a0015d6 CZ |
375 | { |
376 | unsigned long sp, pc; | |
04fe6faf | 377 | unsigned long stack_page = (unsigned long) task_stack_page(p); |
5a0015d6 CZ |
378 | int count = 0; |
379 | ||
5a0015d6 CZ |
380 | sp = p->thread.sp; |
381 | pc = MAKE_PC_FROM_RA(p->thread.ra, p->thread.sp); | |
382 | ||
383 | do { | |
384 | if (sp < stack_page + sizeof(struct task_struct) || | |
385 | sp >= (stack_page + THREAD_SIZE) || | |
386 | pc == 0) | |
387 | return 0; | |
388 | if (!in_sched_functions(pc)) | |
389 | return pc; | |
390 | ||
391 | /* Stack layout: sp-4: ra, sp-3: sp' */ | |
392 | ||
d90b88fd MF |
393 | pc = MAKE_PC_FROM_RA(SPILL_SLOT(sp, 0), sp); |
394 | sp = SPILL_SLOT(sp, 1); | |
5a0015d6 CZ |
395 | } while (count++ < 16); |
396 | return 0; | |
397 | } |