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)
197 /* Note: PS.EXCM is not set while user task is running; its
198 * being set in regs->ps is for exception handling convenience.
201 elfregs->pc = regs->pc;
202 elfregs->ps = (regs->ps & ~(1 << PS_EXCM_BIT));
203 elfregs->lbeg = regs->lbeg;
204 elfregs->lend = regs->lend;
205 elfregs->lcount = regs->lcount;
206 elfregs->sar = regs->sar;
208 memcpy (elfregs->a, regs->areg, sizeof(elfregs->a));
211 void xtensa_elf_core_copy_regs (xtensa_gregset_t *elfregs, struct pt_regs *regs)
213 do_copy_regs ((xtensa_gregset_t *)elfregs, regs, current);
217 /* The inverse of do_copy_regs(). No error or sanity checking. */
219 void do_restore_regs (xtensa_gregset_t *elfregs, struct pt_regs *regs,
220 struct task_struct *tsk)
222 const unsigned long ps_mask = PS_CALLINC_MASK | PS_OWB_MASK;
225 /* Note: PS.EXCM is not set while user task is running; it
226 * needs to be set in regs->ps is for exception handling convenience.
229 ps = (regs->ps & ~ps_mask) | (elfregs->ps & ps_mask) | (1<<PS_EXCM_BIT);
231 regs->pc = elfregs->pc;
232 regs->lbeg = elfregs->lbeg;
233 regs->lend = elfregs->lend;
234 regs->lcount = elfregs->lcount;
235 regs->sar = elfregs->sar;
237 memcpy (regs->areg, elfregs->a, sizeof(regs->areg));
241 * do_save_fpregs() gathers information from 'struct pt_regs' and
242 * 'current->thread' to fill in the elf_fpregset_t structure.
244 * Core files and ptrace use elf_fpregset_t.
247 void do_save_fpregs (elf_fpregset_t *fpregs, struct pt_regs *regs,
248 struct task_struct *tsk)
252 extern unsigned char _xtensa_reginfo_tables[];
253 extern unsigned _xtensa_reginfo_table_size;
257 /* Before dumping coprocessor state from memory,
258 * ensure any live coprocessor contents for this
259 * task are first saved to memory:
261 local_irq_save(flags);
263 for (i = 0; i < XCHAL_CP_MAX; i++) {
264 if (tsk == coprocessor_info[i].owner) {
265 enable_coprocessor(i);
266 save_coprocessor_registers(
267 tsk->thread.cp_save+coprocessor_info[i].offset,i);
268 disable_coprocessor(i);
272 local_irq_restore(flags);
274 /* Now dump coprocessor & extra state: */
275 memcpy((unsigned char*)fpregs,
276 _xtensa_reginfo_tables, _xtensa_reginfo_table_size);
277 memcpy((unsigned char*)fpregs + _xtensa_reginfo_table_size,
278 tsk->thread.cp_save, XTENSA_CP_EXTRA_SIZE);
283 * The inverse of do_save_fpregs().
284 * Copies coprocessor and extra state from fpregs into regs and tsk->thread.
285 * Returns 0 on success, non-zero if layout doesn't match.
288 int do_restore_fpregs (elf_fpregset_t *fpregs, struct pt_regs *regs,
289 struct task_struct *tsk)
293 extern unsigned char _xtensa_reginfo_tables[];
294 extern unsigned _xtensa_reginfo_table_size;
298 /* Make sure save area layouts match.
299 * FIXME: in the future we could allow restoring from
300 * a different layout of the same registers, by comparing
301 * fpregs' table with _xtensa_reginfo_tables and matching
302 * entries and copying registers one at a time.
303 * Not too sure yet whether that's very useful.
306 if( memcmp((unsigned char*)fpregs,
307 _xtensa_reginfo_tables, _xtensa_reginfo_table_size) ) {
311 /* Before restoring coprocessor state from memory,
312 * ensure any live coprocessor contents for this
313 * task are first invalidated.
316 local_irq_save(flags);
318 for (i = 0; i < XCHAL_CP_MAX; i++) {
319 if (tsk == coprocessor_info[i].owner) {
320 enable_coprocessor(i);
321 save_coprocessor_registers(
322 tsk->thread.cp_save+coprocessor_info[i].offset,i);
323 coprocessor_info[i].owner = 0;
324 disable_coprocessor(i);
328 local_irq_restore(flags);
330 /* Now restore coprocessor & extra state: */
332 memcpy(tsk->thread.cp_save,
333 (unsigned char*)fpregs + _xtensa_reginfo_table_size,
334 XTENSA_CP_EXTRA_SIZE);
339 * Fill in the CP structure for a core dump for a particular task.
343 dump_task_fpu(struct pt_regs *regs, struct task_struct *task, elf_fpregset_t *r)
345 return 0; /* no coprocessors active on this processor */
349 * Fill in the CP structure for a core dump.
350 * This includes any FPU coprocessor.
351 * Here, we dump all coprocessors, and other ("extra") custom state.
353 * This function is called by elf_core_dump() in fs/binfmt_elf.c
354 * (in which case 'regs' comes from calls to do_coredump, see signals.c).
356 int dump_fpu(struct pt_regs *regs, elf_fpregset_t *r)
358 return dump_task_fpu(regs, current, r);
362 long xtensa_clone(unsigned long clone_flags, unsigned long newsp,
363 void __user *parent_tid, void *child_tls,
364 void __user *child_tid, long a5,
365 struct pt_regs *regs)
368 newsp = regs->areg[1];
369 return do_fork(clone_flags, newsp, regs, 0, parent_tid, child_tid);
373 * * xtensa_execve() executes a new program.
377 long xtensa_execve(char __user *name, char __user * __user *argv,
378 char __user * __user *envp,
379 long a3, long a4, long a5,
380 struct pt_regs *regs)
385 filename = getname(name);
386 error = PTR_ERR(filename);
387 if (IS_ERR(filename))
389 // FIXME: release coprocessor??
390 error = do_execve(filename, argv, envp, regs);
393 current->ptrace &= ~PT_DTRACE;
394 task_unlock(current);