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