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