blob: 570307c918469d83a5c2f875b7e67a006ae1e600 [file] [log] [blame]
Chris Zankel5a0015d2005-06-23 22:01:16 -07001/*
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
Chris Zankel5a0015d2005-06-23 22:01:16 -070018#include <linux/errno.h>
19#include <linux/sched.h>
20#include <linux/kernel.h>
21#include <linux/mm.h>
22#include <linux/smp.h>
Chris Zankel5a0015d2005-06-23 22:01:16 -070023#include <linux/stddef.h>
24#include <linux/unistd.h>
25#include <linux/ptrace.h>
Chris Zankel5a0015d2005-06-23 22:01:16 -070026#include <linux/elf.h>
Max Filippovc91e02b2016-01-24 10:32:10 +030027#include <linux/hw_breakpoint.h>
Chris Zankel5a0015d2005-06-23 22:01:16 -070028#include <linux/init.h>
29#include <linux/prctl.h>
30#include <linux/init_task.h>
31#include <linux/module.h>
32#include <linux/mqueue.h>
Chris Zankel73089cb2007-08-04 09:27:30 -070033#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Frederic Weisbecker11ad47a2012-08-22 17:27:34 +020035#include <linux/rcupdate.h>
Chris Zankel5a0015d2005-06-23 22:01:16 -070036
37#include <asm/pgtable.h>
38#include <asm/uaccess.h>
Chris Zankel5a0015d2005-06-23 22:01:16 -070039#include <asm/io.h>
40#include <asm/processor.h>
41#include <asm/platform.h>
42#include <asm/mmu.h>
43#include <asm/irq.h>
Arun Sharma600634972011-07-26 16:09:06 -070044#include <linux/atomic.h>
Sam Ravnborg0013a852005-09-09 20:57:26 +020045#include <asm/asm-offsets.h>
Chris Zankel173d66812006-12-10 02:18:48 -080046#include <asm/regs.h>
Max Filippovc91e02b2016-01-24 10:32:10 +030047#include <asm/hw_breakpoint.h>
Chris Zankel5a0015d2005-06-23 22:01:16 -070048
49extern void ret_from_fork(void);
Max Filippov3306a722012-10-25 11:10:50 +040050extern void ret_from_kernel_thread(void);
Chris Zankel5a0015d2005-06-23 22:01:16 -070051
Chris Zankel5a0015d2005-06-23 22:01:16 -070052struct task_struct *current_set[NR_CPUS] = {&init_task, };
53
Adrian Bunk47f3fc92006-03-06 15:42:47 -080054void (*pm_power_off)(void) = NULL;
55EXPORT_SYMBOL(pm_power_off);
56
Chris Zankel5a0015d2005-06-23 22:01:16 -070057
Chris Zankelc658eac2008-02-12 13:17:07 -080058#if XTENSA_HAVE_COPROCESSORS
59
60void coprocessor_release_all(struct thread_info *ti)
61{
62 unsigned long cpenable;
63 int i;
64
65 /* Make sure we don't switch tasks during this operation. */
66
67 preempt_disable();
68
69 /* Walk through all cp owners and release it for the requested one. */
70
71 cpenable = ti->cpenable;
72
73 for (i = 0; i < XCHAL_CP_MAX; i++) {
74 if (coprocessor_owner[i] == ti) {
75 coprocessor_owner[i] = 0;
76 cpenable &= ~(1 << i);
77 }
78 }
79
80 ti->cpenable = cpenable;
81 coprocessor_clear_cpenable();
82
83 preempt_enable();
84}
85
86void coprocessor_flush_all(struct thread_info *ti)
87{
Max Filippovc26e3c62018-11-26 13:29:41 -080088 unsigned long cpenable, old_cpenable;
Chris Zankelc658eac2008-02-12 13:17:07 -080089 int i;
90
91 preempt_disable();
92
Max Filippovc26e3c62018-11-26 13:29:41 -080093 RSR_CPENABLE(old_cpenable);
Chris Zankelc658eac2008-02-12 13:17:07 -080094 cpenable = ti->cpenable;
Max Filippovc26e3c62018-11-26 13:29:41 -080095 WSR_CPENABLE(cpenable);
Chris Zankelc658eac2008-02-12 13:17:07 -080096
97 for (i = 0; i < XCHAL_CP_MAX; i++) {
98 if ((cpenable & 1) != 0 && coprocessor_owner[i] == ti)
99 coprocessor_flush(ti, i);
100 cpenable >>= 1;
101 }
Max Filippovc26e3c62018-11-26 13:29:41 -0800102 WSR_CPENABLE(old_cpenable);
Chris Zankelc658eac2008-02-12 13:17:07 -0800103
104 preempt_enable();
105}
106
107#endif
108
109
Chris Zankel5a0015d2005-06-23 22:01:16 -0700110/*
111 * Powermanagement idle function, if any is provided by the platform.
112 */
Thomas Gleixnerf4e2e9a2013-03-21 22:50:04 +0100113void arch_cpu_idle(void)
Chris Zankel5a0015d2005-06-23 22:01:16 -0700114{
Thomas Gleixnerf4e2e9a2013-03-21 22:50:04 +0100115 platform_idle();
Chris Zankel5a0015d2005-06-23 22:01:16 -0700116}
117
118/*
Chris Zankelc658eac2008-02-12 13:17:07 -0800119 * This is called when the thread calls exit().
Chris Zankel5a0015d2005-06-23 22:01:16 -0700120 */
Jiri Slabye6464692016-05-20 17:00:20 -0700121void exit_thread(struct task_struct *tsk)
Chris Zankel5a0015d2005-06-23 22:01:16 -0700122{
Chris Zankelc658eac2008-02-12 13:17:07 -0800123#if XTENSA_HAVE_COPROCESSORS
Jiri Slabye6464692016-05-20 17:00:20 -0700124 coprocessor_release_all(task_thread_info(tsk));
Chris Zankelc658eac2008-02-12 13:17:07 -0800125#endif
Chris Zankel5a0015d2005-06-23 22:01:16 -0700126}
127
Chris Zankelc658eac2008-02-12 13:17:07 -0800128/*
129 * Flush thread state. This is called when a thread does an execve()
130 * Note that we flush coprocessor registers for the case execve fails.
131 */
Chris Zankel5a0015d2005-06-23 22:01:16 -0700132void flush_thread(void)
133{
Chris Zankelc658eac2008-02-12 13:17:07 -0800134#if XTENSA_HAVE_COPROCESSORS
135 struct thread_info *ti = current_thread_info();
136 coprocessor_flush_all(ti);
137 coprocessor_release_all(ti);
138#endif
Max Filippovc91e02b2016-01-24 10:32:10 +0300139 flush_ptrace_hw_breakpoint(current);
Chris Zankelc658eac2008-02-12 13:17:07 -0800140}
141
142/*
Suresh Siddha55ccf3f2012-05-16 15:03:51 -0700143 * this gets called so that we can store coprocessor state into memory and
144 * copy the current task into the new thread.
Chris Zankelc658eac2008-02-12 13:17:07 -0800145 */
Suresh Siddha55ccf3f2012-05-16 15:03:51 -0700146int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
Chris Zankelc658eac2008-02-12 13:17:07 -0800147{
148#if XTENSA_HAVE_COPROCESSORS
Suresh Siddha55ccf3f2012-05-16 15:03:51 -0700149 coprocessor_flush_all(task_thread_info(src));
Chris Zankelc658eac2008-02-12 13:17:07 -0800150#endif
Suresh Siddha55ccf3f2012-05-16 15:03:51 -0700151 *dst = *src;
152 return 0;
Chris Zankel5a0015d2005-06-23 22:01:16 -0700153}
154
155/*
156 * Copy thread.
157 *
Max Filippov3306a722012-10-25 11:10:50 +0400158 * There are two modes in which this function is called:
159 * 1) Userspace thread creation,
160 * regs != NULL, usp_thread_fn is userspace stack pointer.
161 * It is expected to copy parent regs (in case CLONE_VM is not set
162 * in the clone_flags) and set up passed usp in the childregs.
163 * 2) Kernel thread creation,
164 * regs == NULL, usp_thread_fn is the function to run in the new thread
165 * and thread_fn_arg is its parameter.
166 * childregs are not used for the kernel threads.
167 *
Chris Zankel5a0015d2005-06-23 22:01:16 -0700168 * The stack layout for the new thread looks like this:
169 *
Max Filippov3306a722012-10-25 11:10:50 +0400170 * +------------------------+
Chris Zankel5a0015d2005-06-23 22:01:16 -0700171 * | childregs |
172 * +------------------------+ <- thread.sp = sp in dummy-frame
173 * | dummy-frame | (saved in dummy-frame spill-area)
174 * +------------------------+
175 *
Max Filippov3306a722012-10-25 11:10:50 +0400176 * We create a dummy frame to return to either ret_from_fork or
177 * ret_from_kernel_thread:
178 * a0 points to ret_from_fork/ret_from_kernel_thread (simulating a call4)
Chris Zankel5a0015d2005-06-23 22:01:16 -0700179 * sp points to itself (thread.sp)
Max Filippov3306a722012-10-25 11:10:50 +0400180 * a2, a3 are unused for userspace threads,
181 * a2 points to thread_fn, a3 holds thread_fn arg for kernel threads.
Chris Zankel5a0015d2005-06-23 22:01:16 -0700182 *
183 * Note: This is a pristine frame, so we don't need any spill region on top of
184 * childregs.
Marc Gauthier84ed3052012-10-15 03:55:35 +0400185 *
186 * The fun part: if we're keeping the same VM (i.e. cloning a thread,
187 * not an entire process), we're normally given a new usp, and we CANNOT share
188 * any live address register windows. If we just copy those live frames over,
189 * the two threads (parent and child) will overflow the same frames onto the
190 * parent stack at different times, likely corrupting the parent stack (esp.
191 * if the parent returns from functions that called clone() and calls new
192 * ones, before the child overflows its now old copies of its parent windows).
193 * One solution is to spill windows to the parent stack, but that's fairly
194 * involved. Much simpler to just not copy those live frames across.
Chris Zankel5a0015d2005-06-23 22:01:16 -0700195 */
196
Max Filippov3306a722012-10-25 11:10:50 +0400197int copy_thread(unsigned long clone_flags, unsigned long usp_thread_fn,
Al Viroafa86fc2012-10-22 22:51:14 -0400198 unsigned long thread_fn_arg, struct task_struct *p)
Chris Zankel5a0015d2005-06-23 22:01:16 -0700199{
Max Filippov3306a722012-10-25 11:10:50 +0400200 struct pt_regs *childregs = task_pt_regs(p);
Chris Zankel5a0015d2005-06-23 22:01:16 -0700201
Chris Zankel39070cb2012-10-17 23:08:20 -0700202#if (XTENSA_HAVE_COPROCESSORS || XTENSA_HAVE_IO_PORTS)
203 struct thread_info *ti;
204#endif
205
Chris Zankel5a0015d2005-06-23 22:01:16 -0700206 /* Create a call4 dummy-frame: a0 = 0, a1 = childregs. */
207 *((int*)childregs - 3) = (unsigned long)childregs;
208 *((int*)childregs - 4) = 0;
209
Chris Zankel5a0015d2005-06-23 22:01:16 -0700210 p->thread.sp = (unsigned long)childregs;
Chris Zankelc658eac2008-02-12 13:17:07 -0800211
Max Filippov3306a722012-10-25 11:10:50 +0400212 if (!(p->flags & PF_KTHREAD)) {
213 struct pt_regs *regs = current_pt_regs();
214 unsigned long usp = usp_thread_fn ?
215 usp_thread_fn : regs->areg[1];
Chris Zankel5a0015d2005-06-23 22:01:16 -0700216
Max Filippov3306a722012-10-25 11:10:50 +0400217 p->thread.ra = MAKE_RA_FOR_CALL(
218 (unsigned long)ret_from_fork, 0x1);
219
220 /* This does not copy all the regs.
221 * In a bout of brilliance or madness,
222 * ARs beyond a0-a15 exist past the end of the struct.
223 */
224 *childregs = *regs;
Chris Zankel5a0015d2005-06-23 22:01:16 -0700225 childregs->areg[1] = usp;
Max Filippov3306a722012-10-25 11:10:50 +0400226 childregs->areg[2] = 0;
Chris Zankel6ebe7da2012-10-24 13:15:21 -0700227
228 /* When sharing memory with the parent thread, the child
229 usually starts on a pristine stack, so we have to reset
230 windowbase, windowstart and wmask.
231 (Note that such a new thread is required to always create
232 an initial call4 frame)
233 The exception is vfork, where the new thread continues to
234 run on the parent's stack until it calls execve. This could
235 be a call8 or call12, which requires a legal stack frame
236 of the previous caller for the overflow handlers to work.
237 (Note that it's always legal to overflow live registers).
238 In this case, ensure to spill at least the stack pointer
239 of that frame. */
240
Marc Gauthier84ed3052012-10-15 03:55:35 +0400241 if (clone_flags & CLONE_VM) {
Chris Zankel6ebe7da2012-10-24 13:15:21 -0700242 /* check that caller window is live and same stack */
243 int len = childregs->wmask & ~0xf;
244 if (regs->areg[1] == usp && len != 0) {
245 int callinc = (regs->areg[0] >> 30) & 3;
246 int caller_ars = XCHAL_NUM_AREGS - callinc * 4;
247 put_user(regs->areg[caller_ars+1],
248 (unsigned __user*)(usp - 12));
249 }
250 childregs->wmask = 1;
251 childregs->windowstart = 1;
252 childregs->windowbase = 0;
Marc Gauthier84ed3052012-10-15 03:55:35 +0400253 } else {
254 int len = childregs->wmask & ~0xf;
255 memcpy(&childregs->areg[XCHAL_NUM_AREGS - len/4],
256 &regs->areg[XCHAL_NUM_AREGS - len/4], len);
257 }
Chris Zankelc50842d2013-02-23 19:35:57 -0800258
259 /* The thread pointer is passed in the '4th argument' (= a5) */
Chris Zankel5a0015d2005-06-23 22:01:16 -0700260 if (clone_flags & CLONE_SETTLS)
Chris Zankelc50842d2013-02-23 19:35:57 -0800261 childregs->threadptr = childregs->areg[5];
Chris Zankel5a0015d2005-06-23 22:01:16 -0700262 } else {
Max Filippov3306a722012-10-25 11:10:50 +0400263 p->thread.ra = MAKE_RA_FOR_CALL(
264 (unsigned long)ret_from_kernel_thread, 1);
265
266 /* pass parameters to ret_from_kernel_thread:
267 * a2 = thread_fn, a3 = thread_fn arg
268 */
269 *((int *)childregs - 1) = thread_fn_arg;
270 *((int *)childregs - 2) = usp_thread_fn;
271
272 /* Childregs are only used when we're going to userspace
273 * in which case start_thread will set them up.
274 */
Chris Zankel5a0015d2005-06-23 22:01:16 -0700275 }
Chris Zankelc658eac2008-02-12 13:17:07 -0800276
277#if (XTENSA_HAVE_COPROCESSORS || XTENSA_HAVE_IO_PORTS)
278 ti = task_thread_info(p);
279 ti->cpenable = 0;
280#endif
281
Max Filippovc91e02b2016-01-24 10:32:10 +0300282 clear_ptrace_hw_breakpoint(p);
283
Chris Zankel5a0015d2005-06-23 22:01:16 -0700284 return 0;
285}
286
287
288/*
Chris Zankel5a0015d2005-06-23 22:01:16 -0700289 * These bracket the sleeping functions..
290 */
291
292unsigned long get_wchan(struct task_struct *p)
293{
294 unsigned long sp, pc;
Al Viro04fe6fa2006-01-12 01:05:50 -0800295 unsigned long stack_page = (unsigned long) task_stack_page(p);
Chris Zankel5a0015d2005-06-23 22:01:16 -0700296 int count = 0;
297
298 if (!p || p == current || p->state == TASK_RUNNING)
299 return 0;
300
301 sp = p->thread.sp;
302 pc = MAKE_PC_FROM_RA(p->thread.ra, p->thread.sp);
303
304 do {
305 if (sp < stack_page + sizeof(struct task_struct) ||
306 sp >= (stack_page + THREAD_SIZE) ||
307 pc == 0)
308 return 0;
309 if (!in_sched_functions(pc))
310 return pc;
311
312 /* Stack layout: sp-4: ra, sp-3: sp' */
313
314 pc = MAKE_PC_FROM_RA(*(unsigned long*)sp - 4, sp);
315 sp = *(unsigned long *)sp - 3;
316 } while (count++ < 16);
317 return 0;
318}
319
320/*
Chris Zankel5a0015d2005-06-23 22:01:16 -0700321 * xtensa_gregset_t and 'struct pt_regs' are vastly different formats
322 * of processor registers. Besides different ordering,
323 * xtensa_gregset_t contains non-live register information that
324 * 'struct pt_regs' does not. Exception handling (primarily) uses
325 * 'struct pt_regs'. Core files and ptrace use xtensa_gregset_t.
326 *
327 */
328
Chris Zankelc658eac2008-02-12 13:17:07 -0800329void xtensa_elf_core_copy_regs (xtensa_gregset_t *elfregs, struct pt_regs *regs)
Chris Zankel5a0015d2005-06-23 22:01:16 -0700330{
Chris Zankelc658eac2008-02-12 13:17:07 -0800331 unsigned long wb, ws, wm;
332 int live, last;
333
334 wb = regs->windowbase;
335 ws = regs->windowstart;
336 wm = regs->wmask;
337 ws = ((ws >> wb) | (ws << (WSBITS - wb))) & ((1 << WSBITS) - 1);
338
339 /* Don't leak any random bits. */
340
Alan Cox688bb412012-07-11 14:02:50 -0700341 memset(elfregs, 0, sizeof(*elfregs));
Chris Zankelc658eac2008-02-12 13:17:07 -0800342
Chris Zankel5a0015d2005-06-23 22:01:16 -0700343 /* Note: PS.EXCM is not set while user task is running; its
344 * being set in regs->ps is for exception handling convenience.
345 */
346
347 elfregs->pc = regs->pc;
Chris Zankel173d66812006-12-10 02:18:48 -0800348 elfregs->ps = (regs->ps & ~(1 << PS_EXCM_BIT));
Chris Zankel5a0015d2005-06-23 22:01:16 -0700349 elfregs->lbeg = regs->lbeg;
350 elfregs->lend = regs->lend;
351 elfregs->lcount = regs->lcount;
352 elfregs->sar = regs->sar;
Chris Zankelc658eac2008-02-12 13:17:07 -0800353 elfregs->windowstart = ws;
Chris Zankel5a0015d2005-06-23 22:01:16 -0700354
Chris Zankelc658eac2008-02-12 13:17:07 -0800355 live = (wm & 2) ? 4 : (wm & 4) ? 8 : (wm & 8) ? 12 : 16;
356 last = XCHAL_NUM_AREGS - (wm >> 4) * 4;
357 memcpy(elfregs->a, regs->areg, live * 4);
358 memcpy(elfregs->a + last, regs->areg + last, (wm >> 4) * 16);
Chris Zankel5a0015d2005-06-23 22:01:16 -0700359}
360
Chris Zankelc658eac2008-02-12 13:17:07 -0800361int dump_fpu(void)
Chris Zankel5a0015d2005-06-23 22:01:16 -0700362{
Chris Zankel5a0015d2005-06-23 22:01:16 -0700363 return 0;
364}