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