]>
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 <[email protected], [email protected]> | |
13 | * Chris Zankel <[email protected]> | |
14 | * Marc Gauthier <[email protected], [email protected]> | |
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 CZ |
39 | |
40 | #include <asm/pgtable.h> | |
7c0f6ba6 | 41 | #include <linux/uaccess.h> |
5a0015d6 CZ |
42 | #include <asm/io.h> |
43 | #include <asm/processor.h> | |
44 | #include <asm/platform.h> | |
45 | #include <asm/mmu.h> | |
46 | #include <asm/irq.h> | |
60063497 | 47 | #include <linux/atomic.h> |
0013a854 | 48 | #include <asm/asm-offsets.h> |
173d6681 | 49 | #include <asm/regs.h> |
c91e02bd | 50 | #include <asm/hw_breakpoint.h> |
5a0015d6 CZ |
51 | |
52 | extern void ret_from_fork(void); | |
3306a726 | 53 | extern void ret_from_kernel_thread(void); |
5a0015d6 | 54 | |
5a0015d6 CZ |
55 | struct task_struct *current_set[NR_CPUS] = {&init_task, }; |
56 | ||
47f3fc94 AB |
57 | void (*pm_power_off)(void) = NULL; |
58 | EXPORT_SYMBOL(pm_power_off); | |
59 | ||
5a0015d6 | 60 | |
050e9baa | 61 | #ifdef CONFIG_STACKPROTECTOR |
40d1a07b MF |
62 | #include <linux/stackprotector.h> |
63 | unsigned long __stack_chk_guard __read_mostly; | |
64 | EXPORT_SYMBOL(__stack_chk_guard); | |
65 | #endif | |
66 | ||
c658eac6 CZ |
67 | #if XTENSA_HAVE_COPROCESSORS |
68 | ||
69 | void coprocessor_release_all(struct thread_info *ti) | |
70 | { | |
71 | unsigned long cpenable; | |
72 | int i; | |
73 | ||
74 | /* Make sure we don't switch tasks during this operation. */ | |
75 | ||
76 | preempt_disable(); | |
77 | ||
78 | /* Walk through all cp owners and release it for the requested one. */ | |
79 | ||
80 | cpenable = ti->cpenable; | |
81 | ||
82 | for (i = 0; i < XCHAL_CP_MAX; i++) { | |
83 | if (coprocessor_owner[i] == ti) { | |
84 | coprocessor_owner[i] = 0; | |
85 | cpenable &= ~(1 << i); | |
86 | } | |
87 | } | |
88 | ||
89 | ti->cpenable = cpenable; | |
90 | coprocessor_clear_cpenable(); | |
91 | ||
92 | preempt_enable(); | |
93 | } | |
94 | ||
95 | void coprocessor_flush_all(struct thread_info *ti) | |
96 | { | |
97 | unsigned long cpenable; | |
98 | int i; | |
99 | ||
100 | preempt_disable(); | |
101 | ||
102 | cpenable = ti->cpenable; | |
103 | ||
104 | for (i = 0; i < XCHAL_CP_MAX; i++) { | |
105 | if ((cpenable & 1) != 0 && coprocessor_owner[i] == ti) | |
106 | coprocessor_flush(ti, i); | |
107 | cpenable >>= 1; | |
108 | } | |
109 | ||
110 | preempt_enable(); | |
111 | } | |
112 | ||
113 | #endif | |
114 | ||
115 | ||
5a0015d6 CZ |
116 | /* |
117 | * Powermanagement idle function, if any is provided by the platform. | |
118 | */ | |
f4e2e9a4 | 119 | void arch_cpu_idle(void) |
5a0015d6 | 120 | { |
f4e2e9a4 | 121 | platform_idle(); |
5a0015d6 CZ |
122 | } |
123 | ||
124 | /* | |
c658eac6 | 125 | * This is called when the thread calls exit(). |
5a0015d6 | 126 | */ |
e6464694 | 127 | void exit_thread(struct task_struct *tsk) |
5a0015d6 | 128 | { |
c658eac6 | 129 | #if XTENSA_HAVE_COPROCESSORS |
e6464694 | 130 | coprocessor_release_all(task_thread_info(tsk)); |
c658eac6 | 131 | #endif |
5a0015d6 CZ |
132 | } |
133 | ||
c658eac6 CZ |
134 | /* |
135 | * Flush thread state. This is called when a thread does an execve() | |
136 | * Note that we flush coprocessor registers for the case execve fails. | |
137 | */ | |
5a0015d6 CZ |
138 | void flush_thread(void) |
139 | { | |
c658eac6 CZ |
140 | #if XTENSA_HAVE_COPROCESSORS |
141 | struct thread_info *ti = current_thread_info(); | |
142 | coprocessor_flush_all(ti); | |
143 | coprocessor_release_all(ti); | |
144 | #endif | |
c91e02bd | 145 | flush_ptrace_hw_breakpoint(current); |
c658eac6 CZ |
146 | } |
147 | ||
148 | /* | |
55ccf3fe SS |
149 | * this gets called so that we can store coprocessor state into memory and |
150 | * copy the current task into the new thread. | |
c658eac6 | 151 | */ |
55ccf3fe | 152 | int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src) |
c658eac6 CZ |
153 | { |
154 | #if XTENSA_HAVE_COPROCESSORS | |
55ccf3fe | 155 | coprocessor_flush_all(task_thread_info(src)); |
c658eac6 | 156 | #endif |
55ccf3fe SS |
157 | *dst = *src; |
158 | return 0; | |
5a0015d6 CZ |
159 | } |
160 | ||
161 | /* | |
162 | * Copy thread. | |
163 | * | |
3306a726 MF |
164 | * There are two modes in which this function is called: |
165 | * 1) Userspace thread creation, | |
166 | * regs != NULL, usp_thread_fn is userspace stack pointer. | |
167 | * It is expected to copy parent regs (in case CLONE_VM is not set | |
168 | * in the clone_flags) and set up passed usp in the childregs. | |
169 | * 2) Kernel thread creation, | |
170 | * regs == NULL, usp_thread_fn is the function to run in the new thread | |
171 | * and thread_fn_arg is its parameter. | |
172 | * childregs are not used for the kernel threads. | |
173 | * | |
5a0015d6 CZ |
174 | * The stack layout for the new thread looks like this: |
175 | * | |
3306a726 | 176 | * +------------------------+ |
5a0015d6 CZ |
177 | * | childregs | |
178 | * +------------------------+ <- thread.sp = sp in dummy-frame | |
179 | * | dummy-frame | (saved in dummy-frame spill-area) | |
180 | * +------------------------+ | |
181 | * | |
3306a726 MF |
182 | * We create a dummy frame to return to either ret_from_fork or |
183 | * ret_from_kernel_thread: | |
184 | * a0 points to ret_from_fork/ret_from_kernel_thread (simulating a call4) | |
5a0015d6 | 185 | * sp points to itself (thread.sp) |
3306a726 MF |
186 | * a2, a3 are unused for userspace threads, |
187 | * a2 points to thread_fn, a3 holds thread_fn arg for kernel threads. | |
5a0015d6 CZ |
188 | * |
189 | * Note: This is a pristine frame, so we don't need any spill region on top of | |
190 | * childregs. | |
84ed3053 MG |
191 | * |
192 | * The fun part: if we're keeping the same VM (i.e. cloning a thread, | |
193 | * not an entire process), we're normally given a new usp, and we CANNOT share | |
194 | * any live address register windows. If we just copy those live frames over, | |
195 | * the two threads (parent and child) will overflow the same frames onto the | |
196 | * parent stack at different times, likely corrupting the parent stack (esp. | |
197 | * if the parent returns from functions that called clone() and calls new | |
198 | * ones, before the child overflows its now old copies of its parent windows). | |
199 | * One solution is to spill windows to the parent stack, but that's fairly | |
200 | * involved. Much simpler to just not copy those live frames across. | |
5a0015d6 CZ |
201 | */ |
202 | ||
3306a726 | 203 | int copy_thread(unsigned long clone_flags, unsigned long usp_thread_fn, |
afa86fc4 | 204 | unsigned long thread_fn_arg, struct task_struct *p) |
5a0015d6 | 205 | { |
3306a726 | 206 | struct pt_regs *childregs = task_pt_regs(p); |
5a0015d6 | 207 | |
39070cb8 CZ |
208 | #if (XTENSA_HAVE_COPROCESSORS || XTENSA_HAVE_IO_PORTS) |
209 | struct thread_info *ti; | |
210 | #endif | |
211 | ||
5a0015d6 | 212 | /* Create a call4 dummy-frame: a0 = 0, a1 = childregs. */ |
062b1c19 MF |
213 | SPILL_SLOT(childregs, 1) = (unsigned long)childregs; |
214 | SPILL_SLOT(childregs, 0) = 0; | |
5a0015d6 | 215 | |
5a0015d6 | 216 | p->thread.sp = (unsigned long)childregs; |
c658eac6 | 217 | |
3306a726 MF |
218 | if (!(p->flags & PF_KTHREAD)) { |
219 | struct pt_regs *regs = current_pt_regs(); | |
220 | unsigned long usp = usp_thread_fn ? | |
221 | usp_thread_fn : regs->areg[1]; | |
222 | ||
223 | p->thread.ra = MAKE_RA_FOR_CALL( | |
224 | (unsigned long)ret_from_fork, 0x1); | |
5a0015d6 | 225 | |
3306a726 MF |
226 | /* This does not copy all the regs. |
227 | * In a bout of brilliance or madness, | |
228 | * ARs beyond a0-a15 exist past the end of the struct. | |
229 | */ | |
230 | *childregs = *regs; | |
5a0015d6 | 231 | childregs->areg[1] = usp; |
3306a726 | 232 | childregs->areg[2] = 0; |
6ebe7da2 CZ |
233 | |
234 | /* When sharing memory with the parent thread, the child | |
235 | usually starts on a pristine stack, so we have to reset | |
236 | windowbase, windowstart and wmask. | |
237 | (Note that such a new thread is required to always create | |
238 | an initial call4 frame) | |
239 | The exception is vfork, where the new thread continues to | |
240 | run on the parent's stack until it calls execve. This could | |
241 | be a call8 or call12, which requires a legal stack frame | |
242 | of the previous caller for the overflow handlers to work. | |
243 | (Note that it's always legal to overflow live registers). | |
244 | In this case, ensure to spill at least the stack pointer | |
245 | of that frame. */ | |
246 | ||
84ed3053 | 247 | if (clone_flags & CLONE_VM) { |
6ebe7da2 CZ |
248 | /* check that caller window is live and same stack */ |
249 | int len = childregs->wmask & ~0xf; | |
250 | if (regs->areg[1] == usp && len != 0) { | |
251 | int callinc = (regs->areg[0] >> 30) & 3; | |
252 | int caller_ars = XCHAL_NUM_AREGS - callinc * 4; | |
253 | put_user(regs->areg[caller_ars+1], | |
254 | (unsigned __user*)(usp - 12)); | |
255 | } | |
256 | childregs->wmask = 1; | |
257 | childregs->windowstart = 1; | |
258 | childregs->windowbase = 0; | |
84ed3053 MG |
259 | } else { |
260 | int len = childregs->wmask & ~0xf; | |
261 | memcpy(&childregs->areg[XCHAL_NUM_AREGS - len/4], | |
262 | ®s->areg[XCHAL_NUM_AREGS - len/4], len); | |
263 | } | |
c50842df CZ |
264 | |
265 | /* The thread pointer is passed in the '4th argument' (= a5) */ | |
5a0015d6 | 266 | if (clone_flags & CLONE_SETTLS) |
c50842df | 267 | childregs->threadptr = childregs->areg[5]; |
5a0015d6 | 268 | } else { |
3306a726 MF |
269 | p->thread.ra = MAKE_RA_FOR_CALL( |
270 | (unsigned long)ret_from_kernel_thread, 1); | |
271 | ||
272 | /* pass parameters to ret_from_kernel_thread: | |
273 | * a2 = thread_fn, a3 = thread_fn arg | |
274 | */ | |
062b1c19 MF |
275 | SPILL_SLOT(childregs, 3) = thread_fn_arg; |
276 | SPILL_SLOT(childregs, 2) = usp_thread_fn; | |
3306a726 MF |
277 | |
278 | /* Childregs are only used when we're going to userspace | |
279 | * in which case start_thread will set them up. | |
280 | */ | |
5a0015d6 | 281 | } |
c658eac6 CZ |
282 | |
283 | #if (XTENSA_HAVE_COPROCESSORS || XTENSA_HAVE_IO_PORTS) | |
284 | ti = task_thread_info(p); | |
285 | ti->cpenable = 0; | |
286 | #endif | |
287 | ||
c91e02bd MF |
288 | clear_ptrace_hw_breakpoint(p); |
289 | ||
5a0015d6 CZ |
290 | return 0; |
291 | } | |
292 | ||
293 | ||
5a0015d6 CZ |
294 | /* |
295 | * These bracket the sleeping functions.. | |
296 | */ | |
297 | ||
298 | unsigned long get_wchan(struct task_struct *p) | |
299 | { | |
300 | unsigned long sp, pc; | |
04fe6faf | 301 | unsigned long stack_page = (unsigned long) task_stack_page(p); |
5a0015d6 CZ |
302 | int count = 0; |
303 | ||
304 | if (!p || p == current || p->state == TASK_RUNNING) | |
305 | return 0; | |
306 | ||
307 | sp = p->thread.sp; | |
308 | pc = MAKE_PC_FROM_RA(p->thread.ra, p->thread.sp); | |
309 | ||
310 | do { | |
311 | if (sp < stack_page + sizeof(struct task_struct) || | |
312 | sp >= (stack_page + THREAD_SIZE) || | |
313 | pc == 0) | |
314 | return 0; | |
315 | if (!in_sched_functions(pc)) | |
316 | return pc; | |
317 | ||
318 | /* Stack layout: sp-4: ra, sp-3: sp' */ | |
319 | ||
320 | pc = MAKE_PC_FROM_RA(*(unsigned long*)sp - 4, sp); | |
321 | sp = *(unsigned long *)sp - 3; | |
322 | } while (count++ < 16); | |
323 | return 0; | |
324 | } | |
325 | ||
326 | /* | |
5a0015d6 CZ |
327 | * xtensa_gregset_t and 'struct pt_regs' are vastly different formats |
328 | * of processor registers. Besides different ordering, | |
329 | * xtensa_gregset_t contains non-live register information that | |
330 | * 'struct pt_regs' does not. Exception handling (primarily) uses | |
331 | * 'struct pt_regs'. Core files and ptrace use xtensa_gregset_t. | |
332 | * | |
333 | */ | |
334 | ||
c658eac6 | 335 | void xtensa_elf_core_copy_regs (xtensa_gregset_t *elfregs, struct pt_regs *regs) |
5a0015d6 | 336 | { |
c658eac6 CZ |
337 | unsigned long wb, ws, wm; |
338 | int live, last; | |
339 | ||
340 | wb = regs->windowbase; | |
341 | ws = regs->windowstart; | |
342 | wm = regs->wmask; | |
343 | ws = ((ws >> wb) | (ws << (WSBITS - wb))) & ((1 << WSBITS) - 1); | |
344 | ||
345 | /* Don't leak any random bits. */ | |
346 | ||
688bb415 | 347 | memset(elfregs, 0, sizeof(*elfregs)); |
c658eac6 | 348 | |
5a0015d6 CZ |
349 | /* Note: PS.EXCM is not set while user task is running; its |
350 | * being set in regs->ps is for exception handling convenience. | |
351 | */ | |
352 | ||
353 | elfregs->pc = regs->pc; | |
173d6681 | 354 | elfregs->ps = (regs->ps & ~(1 << PS_EXCM_BIT)); |
5a0015d6 CZ |
355 | elfregs->lbeg = regs->lbeg; |
356 | elfregs->lend = regs->lend; | |
357 | elfregs->lcount = regs->lcount; | |
358 | elfregs->sar = regs->sar; | |
c658eac6 | 359 | elfregs->windowstart = ws; |
5a0015d6 | 360 | |
c658eac6 CZ |
361 | live = (wm & 2) ? 4 : (wm & 4) ? 8 : (wm & 8) ? 12 : 16; |
362 | last = XCHAL_NUM_AREGS - (wm >> 4) * 4; | |
363 | memcpy(elfregs->a, regs->areg, live * 4); | |
364 | memcpy(elfregs->a + last, regs->areg + last, (wm >> 4) * 16); | |
5a0015d6 CZ |
365 | } |
366 | ||
c658eac6 | 367 | int dump_fpu(void) |
5a0015d6 | 368 | { |
5a0015d6 CZ |
369 | return 0; |
370 | } |