Merge tag 'pm-4.13-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm
[linux/fpc-iii.git] / arch / xtensa / kernel / process.c
blobff4f0ecb03dd1e9dfcab581eaa2afd55d7a1b374
1 /*
2 * arch/xtensa/kernel/process.c
4 * Xtensa Processor version.
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.
10 * Copyright (C) 2001 - 2005 Tensilica Inc.
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
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>
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>
52 extern void ret_from_fork(void);
53 extern void ret_from_kernel_thread(void);
55 struct task_struct *current_set[NR_CPUS] = {&init_task, };
57 void (*pm_power_off)(void) = NULL;
58 EXPORT_SYMBOL(pm_power_off);
61 #if XTENSA_HAVE_COPROCESSORS
63 void coprocessor_release_all(struct thread_info *ti)
65 unsigned long cpenable;
66 int i;
68 /* Make sure we don't switch tasks during this operation. */
70 preempt_disable();
72 /* Walk through all cp owners and release it for the requested one. */
74 cpenable = ti->cpenable;
76 for (i = 0; i < XCHAL_CP_MAX; i++) {
77 if (coprocessor_owner[i] == ti) {
78 coprocessor_owner[i] = 0;
79 cpenable &= ~(1 << i);
83 ti->cpenable = cpenable;
84 coprocessor_clear_cpenable();
86 preempt_enable();
89 void coprocessor_flush_all(struct thread_info *ti)
91 unsigned long cpenable;
92 int i;
94 preempt_disable();
96 cpenable = ti->cpenable;
98 for (i = 0; i < XCHAL_CP_MAX; i++) {
99 if ((cpenable & 1) != 0 && coprocessor_owner[i] == ti)
100 coprocessor_flush(ti, i);
101 cpenable >>= 1;
104 preempt_enable();
107 #endif
111 * Powermanagement idle function, if any is provided by the platform.
113 void arch_cpu_idle(void)
115 platform_idle();
119 * This is called when the thread calls exit().
121 void exit_thread(struct task_struct *tsk)
123 #if XTENSA_HAVE_COPROCESSORS
124 coprocessor_release_all(task_thread_info(tsk));
125 #endif
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.
132 void flush_thread(void)
134 #if XTENSA_HAVE_COPROCESSORS
135 struct thread_info *ti = current_thread_info();
136 coprocessor_flush_all(ti);
137 coprocessor_release_all(ti);
138 #endif
139 flush_ptrace_hw_breakpoint(current);
143 * this gets called so that we can store coprocessor state into memory and
144 * copy the current task into the new thread.
146 int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
148 #if XTENSA_HAVE_COPROCESSORS
149 coprocessor_flush_all(task_thread_info(src));
150 #endif
151 *dst = *src;
152 return 0;
156 * Copy thread.
158 * 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.
168 * The stack layout for the new thread looks like this:
170 * +------------------------+
171 * | childregs |
172 * +------------------------+ <- thread.sp = sp in dummy-frame
173 * | dummy-frame | (saved in dummy-frame spill-area)
174 * +------------------------+
176 * 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)
179 * sp points to itself (thread.sp)
180 * a2, a3 are unused for userspace threads,
181 * a2 points to thread_fn, a3 holds thread_fn arg for kernel threads.
183 * Note: This is a pristine frame, so we don't need any spill region on top of
184 * childregs.
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.
197 int copy_thread(unsigned long clone_flags, unsigned long usp_thread_fn,
198 unsigned long thread_fn_arg, struct task_struct *p)
200 struct pt_regs *childregs = task_pt_regs(p);
202 #if (XTENSA_HAVE_COPROCESSORS || XTENSA_HAVE_IO_PORTS)
203 struct thread_info *ti;
204 #endif
206 /* Create a call4 dummy-frame: a0 = 0, a1 = childregs. */
207 SPILL_SLOT(childregs, 1) = (unsigned long)childregs;
208 SPILL_SLOT(childregs, 0) = 0;
210 p->thread.sp = (unsigned long)childregs;
212 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];
217 p->thread.ra = MAKE_RA_FOR_CALL(
218 (unsigned long)ret_from_fork, 0x1);
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.
224 *childregs = *regs;
225 childregs->areg[1] = usp;
226 childregs->areg[2] = 0;
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. */
241 if (clone_flags & CLONE_VM) {
242 /* 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));
250 childregs->wmask = 1;
251 childregs->windowstart = 1;
252 childregs->windowbase = 0;
253 } 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);
259 /* The thread pointer is passed in the '4th argument' (= a5) */
260 if (clone_flags & CLONE_SETTLS)
261 childregs->threadptr = childregs->areg[5];
262 } else {
263 p->thread.ra = MAKE_RA_FOR_CALL(
264 (unsigned long)ret_from_kernel_thread, 1);
266 /* pass parameters to ret_from_kernel_thread:
267 * a2 = thread_fn, a3 = thread_fn arg
269 SPILL_SLOT(childregs, 3) = thread_fn_arg;
270 SPILL_SLOT(childregs, 2) = usp_thread_fn;
272 /* Childregs are only used when we're going to userspace
273 * in which case start_thread will set them up.
277 #if (XTENSA_HAVE_COPROCESSORS || XTENSA_HAVE_IO_PORTS)
278 ti = task_thread_info(p);
279 ti->cpenable = 0;
280 #endif
282 clear_ptrace_hw_breakpoint(p);
284 return 0;
289 * These bracket the sleeping functions..
292 unsigned long get_wchan(struct task_struct *p)
294 unsigned long sp, pc;
295 unsigned long stack_page = (unsigned long) task_stack_page(p);
296 int count = 0;
298 if (!p || p == current || p->state == TASK_RUNNING)
299 return 0;
301 sp = p->thread.sp;
302 pc = MAKE_PC_FROM_RA(p->thread.ra, p->thread.sp);
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;
312 /* Stack layout: sp-4: ra, sp-3: sp' */
314 pc = MAKE_PC_FROM_RA(*(unsigned long*)sp - 4, sp);
315 sp = *(unsigned long *)sp - 3;
316 } while (count++ < 16);
317 return 0;
321 * 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.
329 void xtensa_elf_core_copy_regs (xtensa_gregset_t *elfregs, struct pt_regs *regs)
331 unsigned long wb, ws, wm;
332 int live, last;
334 wb = regs->windowbase;
335 ws = regs->windowstart;
336 wm = regs->wmask;
337 ws = ((ws >> wb) | (ws << (WSBITS - wb))) & ((1 << WSBITS) - 1);
339 /* Don't leak any random bits. */
341 memset(elfregs, 0, sizeof(*elfregs));
343 /* Note: PS.EXCM is not set while user task is running; its
344 * being set in regs->ps is for exception handling convenience.
347 elfregs->pc = regs->pc;
348 elfregs->ps = (regs->ps & ~(1 << PS_EXCM_BIT));
349 elfregs->lbeg = regs->lbeg;
350 elfregs->lend = regs->lend;
351 elfregs->lcount = regs->lcount;
352 elfregs->sar = regs->sar;
353 elfregs->windowstart = ws;
355 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);
361 int dump_fpu(void)
363 return 0;