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
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>
18 #include <linux/errno.h>
19 #include <linux/sched.h>
20 #include <linux/kernel.h>
22 #include <linux/smp.h>
23 #include <linux/stddef.h>
24 #include <linux/unistd.h>
25 #include <linux/ptrace.h>
26 #include <linux/slab.h>
27 #include <linux/elf.h>
28 #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>
35 #include <asm/pgtable.h>
36 #include <asm/uaccess.h>
37 #include <asm/system.h>
39 #include <asm/processor.h>
40 #include <asm/platform.h>
43 #include <asm/atomic.h>
44 #include <asm/asm-offsets.h>
47 extern void ret_from_fork(void);
49 struct task_struct *current_set[NR_CPUS] = {&init_task, };
51 void (*pm_power_off)(void) = NULL;
52 EXPORT_SYMBOL(pm_power_off);
56 * Powermanagement idle function, if any is provided by the platform.
63 /* endless idle loop with no priority at all */
65 while (!need_resched())
67 preempt_enable_no_resched();
74 * Free current thread data structures etc..
77 void exit_thread(void)
81 void flush_thread(void)
88 * The stack layout for the new thread looks like this:
90 * +------------------------+ <- sp in childregs (= tos)
92 * +------------------------+ <- thread.sp = sp in dummy-frame
93 * | dummy-frame | (saved in dummy-frame spill-area)
94 * +------------------------+
96 * We create a dummy frame to return to ret_from_fork:
97 * a0 points to ret_from_fork (simulating a call4)
98 * sp points to itself (thread.sp)
101 * Note: This is a pristine frame, so we don't need any spill region on top of
105 int copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
106 unsigned long unused,
107 struct task_struct * p, struct pt_regs * regs)
109 struct pt_regs *childregs;
111 int user_mode = user_mode(regs);
113 /* Set up new TSS. */
114 tos = (unsigned long)task_stack_page(p) + THREAD_SIZE;
116 childregs = (struct pt_regs*)(tos - PT_USER_SIZE);
118 childregs = (struct pt_regs*)tos - 1;
122 /* Create a call4 dummy-frame: a0 = 0, a1 = childregs. */
123 *((int*)childregs - 3) = (unsigned long)childregs;
124 *((int*)childregs - 4) = 0;
126 childregs->areg[1] = tos;
127 childregs->areg[2] = 0;
128 p->set_child_tid = p->clear_child_tid = NULL;
129 p->thread.ra = MAKE_RA_FOR_CALL((unsigned long)ret_from_fork, 0x1);
130 p->thread.sp = (unsigned long)childregs;
131 if (user_mode(regs)) {
133 int len = childregs->wmask & ~0xf;
134 childregs->areg[1] = usp;
135 memcpy(&childregs->areg[XCHAL_NUM_AREGS - len/4],
136 ®s->areg[XCHAL_NUM_AREGS - len/4], len);
138 if (clone_flags & CLONE_SETTLS)
139 childregs->areg[2] = childregs->areg[6];
142 /* In kernel space, we start a new thread with a new stack. */
143 childregs->wmask = 1;
150 * These bracket the sleeping functions..
153 unsigned long get_wchan(struct task_struct *p)
155 unsigned long sp, pc;
156 unsigned long stack_page = (unsigned long) task_stack_page(p);
159 if (!p || p == current || p->state == TASK_RUNNING)
163 pc = MAKE_PC_FROM_RA(p->thread.ra, p->thread.sp);
166 if (sp < stack_page + sizeof(struct task_struct) ||
167 sp >= (stack_page + THREAD_SIZE) ||
170 if (!in_sched_functions(pc))
173 /* Stack layout: sp-4: ra, sp-3: sp' */
175 pc = MAKE_PC_FROM_RA(*(unsigned long*)sp - 4, sp);
176 sp = *(unsigned long *)sp - 3;
177 } while (count++ < 16);
182 * do_copy_regs() gathers information from 'struct pt_regs' and
183 * 'current->thread.areg[]' to fill in the xtensa_gregset_t
186 * xtensa_gregset_t and 'struct pt_regs' are vastly different formats
187 * of processor registers. Besides different ordering,
188 * xtensa_gregset_t contains non-live register information that
189 * 'struct pt_regs' does not. Exception handling (primarily) uses
190 * 'struct pt_regs'. Core files and ptrace use xtensa_gregset_t.
194 void do_copy_regs (xtensa_gregset_t *elfregs, struct pt_regs *regs,
195 struct task_struct *tsk)
199 elfregs->xchal_config_id0 = XCHAL_HW_CONFIGID0;
200 elfregs->xchal_config_id1 = XCHAL_HW_CONFIGID1;
202 __asm__ __volatile__ ("rsr %0, 176\n" : "=a" (i));
204 __asm__ __volatile__ ("rsr %0, 208\n" : "=a" (i));
207 /* Note: PS.EXCM is not set while user task is running; its
208 * being set in regs->ps is for exception handling convenience.
211 elfregs->pc = regs->pc;
212 elfregs->ps = (regs->ps & ~(1 << PS_EXCM_BIT));
213 elfregs->exccause = regs->exccause;
214 elfregs->excvaddr = regs->excvaddr;
215 elfregs->windowbase = regs->windowbase;
216 elfregs->windowstart = regs->windowstart;
217 elfregs->lbeg = regs->lbeg;
218 elfregs->lend = regs->lend;
219 elfregs->lcount = regs->lcount;
220 elfregs->sar = regs->sar;
221 elfregs->syscall = regs->syscall;
223 /* Copy register file.
224 * The layout looks like this:
226 * | a0 ... a15 | Z ... Z | arX ... arY |
227 * current window unused saved frames
230 memset (elfregs->ar, 0, sizeof(elfregs->ar));
232 wb_offset = regs->windowbase * 4;
233 n = (regs->wmask&1)? 4 : (regs->wmask&2)? 8 : (regs->wmask&4)? 12 : 16;
235 for (i = 0; i < n; i++)
236 elfregs->ar[(wb_offset + i) % XCHAL_NUM_AREGS] = regs->areg[i];
238 n = (regs->wmask >> 4) * 4;
240 for (i = XCHAL_NUM_AREGS - n; n > 0; i++, n--)
241 elfregs->ar[(wb_offset + i) % XCHAL_NUM_AREGS] = regs->areg[i];
244 void xtensa_elf_core_copy_regs (xtensa_gregset_t *elfregs, struct pt_regs *regs)
246 do_copy_regs ((xtensa_gregset_t *)elfregs, regs, current);
250 /* The inverse of do_copy_regs(). No error or sanity checking. */
252 void do_restore_regs (xtensa_gregset_t *elfregs, struct pt_regs *regs,
253 struct task_struct *tsk)
257 /* Note: PS.EXCM is not set while user task is running; it
258 * needs to be set in regs->ps is for exception handling convenience.
261 regs->pc = elfregs->pc;
262 regs->ps = (elfregs->ps | (1 << PS_EXCM_BIT));
263 regs->exccause = elfregs->exccause;
264 regs->excvaddr = elfregs->excvaddr;
265 regs->windowbase = elfregs->windowbase;
266 regs->windowstart = elfregs->windowstart;
267 regs->lbeg = elfregs->lbeg;
268 regs->lend = elfregs->lend;
269 regs->lcount = elfregs->lcount;
270 regs->sar = elfregs->sar;
271 regs->syscall = elfregs->syscall;
273 /* Clear everything. */
275 memset (regs->areg, 0, sizeof(regs->areg));
277 /* Copy regs from live window frame. */
279 wb_offset = regs->windowbase * 4;
280 n = (regs->wmask&1)? 4 : (regs->wmask&2)? 8 : (regs->wmask&4)? 12 : 16;
282 for (i = 0; i < n; i++)
283 regs->areg[(wb_offset+i) % XCHAL_NUM_AREGS] = elfregs->ar[i];
285 n = (regs->wmask >> 4) * 4;
287 for (i = XCHAL_NUM_AREGS - n; n > 0; i++, n--)
288 regs->areg[(wb_offset+i) % XCHAL_NUM_AREGS] = elfregs->ar[i];
292 * do_save_fpregs() gathers information from 'struct pt_regs' and
293 * 'current->thread' to fill in the elf_fpregset_t structure.
295 * Core files and ptrace use elf_fpregset_t.
298 void do_save_fpregs (elf_fpregset_t *fpregs, struct pt_regs *regs,
299 struct task_struct *tsk)
303 extern unsigned char _xtensa_reginfo_tables[];
304 extern unsigned _xtensa_reginfo_table_size;
308 /* Before dumping coprocessor state from memory,
309 * ensure any live coprocessor contents for this
310 * task are first saved to memory:
312 local_irq_save(flags);
314 for (i = 0; i < XCHAL_CP_MAX; i++) {
315 if (tsk == coprocessor_info[i].owner) {
316 enable_coprocessor(i);
317 save_coprocessor_registers(
318 tsk->thread.cp_save+coprocessor_info[i].offset,i);
319 disable_coprocessor(i);
323 local_irq_restore(flags);
325 /* Now dump coprocessor & extra state: */
326 memcpy((unsigned char*)fpregs,
327 _xtensa_reginfo_tables, _xtensa_reginfo_table_size);
328 memcpy((unsigned char*)fpregs + _xtensa_reginfo_table_size,
329 tsk->thread.cp_save, XTENSA_CP_EXTRA_SIZE);
334 * The inverse of do_save_fpregs().
335 * Copies coprocessor and extra state from fpregs into regs and tsk->thread.
336 * Returns 0 on success, non-zero if layout doesn't match.
339 int do_restore_fpregs (elf_fpregset_t *fpregs, struct pt_regs *regs,
340 struct task_struct *tsk)
344 extern unsigned char _xtensa_reginfo_tables[];
345 extern unsigned _xtensa_reginfo_table_size;
349 /* Make sure save area layouts match.
350 * FIXME: in the future we could allow restoring from
351 * a different layout of the same registers, by comparing
352 * fpregs' table with _xtensa_reginfo_tables and matching
353 * entries and copying registers one at a time.
354 * Not too sure yet whether that's very useful.
357 if( memcmp((unsigned char*)fpregs,
358 _xtensa_reginfo_tables, _xtensa_reginfo_table_size) ) {
362 /* Before restoring coprocessor state from memory,
363 * ensure any live coprocessor contents for this
364 * task are first invalidated.
367 local_irq_save(flags);
369 for (i = 0; i < XCHAL_CP_MAX; i++) {
370 if (tsk == coprocessor_info[i].owner) {
371 enable_coprocessor(i);
372 save_coprocessor_registers(
373 tsk->thread.cp_save+coprocessor_info[i].offset,i);
374 coprocessor_info[i].owner = 0;
375 disable_coprocessor(i);
379 local_irq_restore(flags);
381 /* Now restore coprocessor & extra state: */
383 memcpy(tsk->thread.cp_save,
384 (unsigned char*)fpregs + _xtensa_reginfo_table_size,
385 XTENSA_CP_EXTRA_SIZE);
390 * Fill in the CP structure for a core dump for a particular task.
394 dump_task_fpu(struct pt_regs *regs, struct task_struct *task, elf_fpregset_t *r)
396 return 0; /* no coprocessors active on this processor */
400 * Fill in the CP structure for a core dump.
401 * This includes any FPU coprocessor.
402 * Here, we dump all coprocessors, and other ("extra") custom state.
404 * This function is called by elf_core_dump() in fs/binfmt_elf.c
405 * (in which case 'regs' comes from calls to do_coredump, see signals.c).
407 int dump_fpu(struct pt_regs *regs, elf_fpregset_t *r)
409 return dump_task_fpu(regs, current, r);
413 long xtensa_clone(unsigned long clone_flags, unsigned long newsp,
414 void __user *parent_tid, void *child_tls,
415 void __user *child_tid, long a5,
416 struct pt_regs *regs)
419 newsp = regs->areg[1];
420 return do_fork(clone_flags, newsp, regs, 0, parent_tid, child_tid);
424 * * xtensa_execve() executes a new program.
428 long xtensa_execve(char __user *name, char __user * __user *argv,
429 char __user * __user *envp,
430 long a3, long a4, long a5,
431 struct pt_regs *regs)
436 filename = getname(name);
437 error = PTR_ERR(filename);
438 if (IS_ERR(filename))
440 // FIXME: release coprocessor??
441 error = do_execve(filename, argv, envp, regs);
444 current->ptrace &= ~PT_DTRACE;
445 task_unlock(current);