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