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/elf.h>
27 #include <linux/init.h>
28 #include <linux/prctl.h>
29 #include <linux/init_task.h>
30 #include <linux/module.h>
31 #include <linux/mqueue.h>
33 #include <linux/slab.h>
35 #include <asm/pgtable.h>
36 #include <asm/uaccess.h>
38 #include <asm/processor.h>
39 #include <asm/platform.h>
42 #include <linux/atomic.h>
43 #include <asm/asm-offsets.h>
46 extern void ret_from_fork(void);
48 struct task_struct
*current_set
[NR_CPUS
] = {&init_task
, };
50 void (*pm_power_off
)(void) = NULL
;
51 EXPORT_SYMBOL(pm_power_off
);
54 #if XTENSA_HAVE_COPROCESSORS
56 void coprocessor_release_all(struct thread_info
*ti
)
58 unsigned long cpenable
;
61 /* Make sure we don't switch tasks during this operation. */
65 /* Walk through all cp owners and release it for the requested one. */
67 cpenable
= ti
->cpenable
;
69 for (i
= 0; i
< XCHAL_CP_MAX
; i
++) {
70 if (coprocessor_owner
[i
] == ti
) {
71 coprocessor_owner
[i
] = 0;
72 cpenable
&= ~(1 << i
);
76 ti
->cpenable
= cpenable
;
77 coprocessor_clear_cpenable();
82 void coprocessor_flush_all(struct thread_info
*ti
)
84 unsigned long cpenable
;
89 cpenable
= ti
->cpenable
;
91 for (i
= 0; i
< XCHAL_CP_MAX
; i
++) {
92 if ((cpenable
& 1) != 0 && coprocessor_owner
[i
] == ti
)
93 coprocessor_flush(ti
, i
);
104 * Powermanagement idle function, if any is provided by the platform.
111 /* endless idle loop with no priority at all */
113 while (!need_resched())
115 schedule_preempt_disabled();
120 * This is called when the thread calls exit().
122 void exit_thread(void)
124 #if XTENSA_HAVE_COPROCESSORS
125 coprocessor_release_all(current_thread_info());
130 * Flush thread state. This is called when a thread does an execve()
131 * Note that we flush coprocessor registers for the case execve fails.
133 void flush_thread(void)
135 #if XTENSA_HAVE_COPROCESSORS
136 struct thread_info
*ti
= current_thread_info();
137 coprocessor_flush_all(ti
);
138 coprocessor_release_all(ti
);
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
));
158 * The stack layout for the new thread looks like this:
160 * +------------------------+ <- sp in childregs (= tos)
162 * +------------------------+ <- thread.sp = sp in dummy-frame
163 * | dummy-frame | (saved in dummy-frame spill-area)
164 * +------------------------+
166 * We create a dummy frame to return to ret_from_fork:
167 * a0 points to ret_from_fork (simulating a call4)
168 * sp points to itself (thread.sp)
171 * Note: This is a pristine frame, so we don't need any spill region on top of
175 int copy_thread(unsigned long clone_flags
, unsigned long usp
,
176 unsigned long unused
,
177 struct task_struct
* p
, struct pt_regs
* regs
)
179 struct pt_regs
*childregs
;
180 struct thread_info
*ti
;
182 int user_mode
= user_mode(regs
);
184 /* Set up new TSS. */
185 tos
= (unsigned long)task_stack_page(p
) + THREAD_SIZE
;
187 childregs
= (struct pt_regs
*)(tos
- PT_USER_SIZE
);
189 childregs
= (struct pt_regs
*)tos
- 1;
193 /* Create a call4 dummy-frame: a0 = 0, a1 = childregs. */
194 *((int*)childregs
- 3) = (unsigned long)childregs
;
195 *((int*)childregs
- 4) = 0;
197 childregs
->areg
[1] = tos
;
198 childregs
->areg
[2] = 0;
199 p
->set_child_tid
= p
->clear_child_tid
= NULL
;
200 p
->thread
.ra
= MAKE_RA_FOR_CALL((unsigned long)ret_from_fork
, 0x1);
201 p
->thread
.sp
= (unsigned long)childregs
;
203 if (user_mode(regs
)) {
205 int len
= childregs
->wmask
& ~0xf;
206 childregs
->areg
[1] = usp
;
207 memcpy(&childregs
->areg
[XCHAL_NUM_AREGS
- len
/4],
208 ®s
->areg
[XCHAL_NUM_AREGS
- len
/4], len
);
209 // FIXME: we need to set THREADPTR in thread_info...
210 if (clone_flags
& CLONE_SETTLS
)
211 childregs
->areg
[2] = childregs
->areg
[6];
214 /* In kernel space, we start a new thread with a new stack. */
215 childregs
->wmask
= 1;
218 #if (XTENSA_HAVE_COPROCESSORS || XTENSA_HAVE_IO_PORTS)
219 ti
= task_thread_info(p
);
228 * These bracket the sleeping functions..
231 unsigned long get_wchan(struct task_struct
*p
)
233 unsigned long sp
, pc
;
234 unsigned long stack_page
= (unsigned long) task_stack_page(p
);
237 if (!p
|| p
== current
|| p
->state
== TASK_RUNNING
)
241 pc
= MAKE_PC_FROM_RA(p
->thread
.ra
, p
->thread
.sp
);
244 if (sp
< stack_page
+ sizeof(struct task_struct
) ||
245 sp
>= (stack_page
+ THREAD_SIZE
) ||
248 if (!in_sched_functions(pc
))
251 /* Stack layout: sp-4: ra, sp-3: sp' */
253 pc
= MAKE_PC_FROM_RA(*(unsigned long*)sp
- 4, sp
);
254 sp
= *(unsigned long *)sp
- 3;
255 } while (count
++ < 16);
260 * xtensa_gregset_t and 'struct pt_regs' are vastly different formats
261 * of processor registers. Besides different ordering,
262 * xtensa_gregset_t contains non-live register information that
263 * 'struct pt_regs' does not. Exception handling (primarily) uses
264 * 'struct pt_regs'. Core files and ptrace use xtensa_gregset_t.
268 void xtensa_elf_core_copy_regs (xtensa_gregset_t
*elfregs
, struct pt_regs
*regs
)
270 unsigned long wb
, ws
, wm
;
273 wb
= regs
->windowbase
;
274 ws
= regs
->windowstart
;
276 ws
= ((ws
>> wb
) | (ws
<< (WSBITS
- wb
))) & ((1 << WSBITS
) - 1);
278 /* Don't leak any random bits. */
280 memset(elfregs
, 0, sizeof(*elfregs
));
282 /* Note: PS.EXCM is not set while user task is running; its
283 * being set in regs->ps is for exception handling convenience.
286 elfregs
->pc
= regs
->pc
;
287 elfregs
->ps
= (regs
->ps
& ~(1 << PS_EXCM_BIT
));
288 elfregs
->lbeg
= regs
->lbeg
;
289 elfregs
->lend
= regs
->lend
;
290 elfregs
->lcount
= regs
->lcount
;
291 elfregs
->sar
= regs
->sar
;
292 elfregs
->windowstart
= ws
;
294 live
= (wm
& 2) ? 4 : (wm
& 4) ? 8 : (wm
& 8) ? 12 : 16;
295 last
= XCHAL_NUM_AREGS
- (wm
>> 4) * 4;
296 memcpy(elfregs
->a
, regs
->areg
, live
* 4);
297 memcpy(elfregs
->a
+ last
, regs
->areg
+ last
, (wm
>> 4) * 16);
306 long xtensa_clone(unsigned long clone_flags
, unsigned long newsp
,
307 void __user
*parent_tid
, void *child_tls
,
308 void __user
*child_tid
, long a5
,
309 struct pt_regs
*regs
)
312 newsp
= regs
->areg
[1];
313 return do_fork(clone_flags
, newsp
, regs
, 0, parent_tid
, child_tid
);
317 * xtensa_execve() executes a new program.
321 long xtensa_execve(const char __user
*name
,
322 const char __user
*const __user
*argv
,
323 const char __user
*const __user
*envp
,
324 long a3
, long a4
, long a5
,
325 struct pt_regs
*regs
)
330 filename
= getname(name
);
331 error
= PTR_ERR(filename
);
332 if (IS_ERR(filename
))
334 error
= do_execve(filename
, argv
, envp
, regs
);