1 /* $Id: process.c,v 1.28 2004/05/05 16:54:23 lethal Exp $
3 * linux/arch/sh/kernel/process.c
5 * Copyright (C) 1995 Linus Torvalds
7 * SuperH version: Copyright (C) 1999, 2000 Niibe Yutaka & Kaz Kojima
11 * This file handles the architecture-dependent parts of process handling..
14 #include <linux/module.h>
15 #include <linux/unistd.h>
17 #include <linux/elfcore.h>
18 #include <linux/a.out.h>
19 #include <linux/slab.h>
21 #include <linux/ptrace.h>
22 #include <linux/kallsyms.h>
23 #include <linux/kexec.h>
26 #include <asm/uaccess.h>
27 #include <asm/mmu_context.h>
30 static int hlt_counter=0;
34 #define HARD_IDLE_TIMEOUT (HZ / 3)
36 void (*pm_idle)(void);
38 void (*pm_power_off)(void);
39 EXPORT_SYMBOL(pm_power_off);
41 void disable_hlt(void)
46 EXPORT_SYMBOL(disable_hlt);
53 EXPORT_SYMBOL(enable_hlt);
55 void default_idle(void)
65 /* endless idle loop with no priority at all */
67 void (*idle)(void) = pm_idle;
72 while (!need_resched())
75 preempt_enable_no_resched();
81 void machine_restart(char * __unused)
86 image = xchg(&kexec_image, 0);
93 /* SR.BL=1 and invoke address error to let CPU reset (manual reset) */
94 asm volatile("ldc %0, sr\n\t"
95 "mov.l @%1, %0" : : "r" (0x10000000), "r" (0x80000001));
98 void machine_halt(void)
106 void machine_power_off(void)
112 void show_regs(struct pt_regs * regs)
115 printk("Pid : %d, Comm: %20s\n", current->pid, current->comm);
116 print_symbol("PC is at %s\n", regs->pc);
117 printk("PC : %08lx SP : %08lx SR : %08lx ",
118 regs->pc, regs->regs[15], regs->sr);
120 printk("TEA : %08x ", ctrl_inl(MMU_TEA));
124 printk("%s\n", print_tainted());
126 printk("R0 : %08lx R1 : %08lx R2 : %08lx R3 : %08lx\n",
127 regs->regs[0],regs->regs[1],
128 regs->regs[2],regs->regs[3]);
129 printk("R4 : %08lx R5 : %08lx R6 : %08lx R7 : %08lx\n",
130 regs->regs[4],regs->regs[5],
131 regs->regs[6],regs->regs[7]);
132 printk("R8 : %08lx R9 : %08lx R10 : %08lx R11 : %08lx\n",
133 regs->regs[8],regs->regs[9],
134 regs->regs[10],regs->regs[11]);
135 printk("R12 : %08lx R13 : %08lx R14 : %08lx\n",
136 regs->regs[12],regs->regs[13],
138 printk("MACH: %08lx MACL: %08lx GBR : %08lx PR : %08lx\n",
139 regs->mach, regs->macl, regs->gbr, regs->pr);
142 * If we're in kernel mode, dump the stack too..
144 if (!user_mode(regs)) {
145 extern void show_task(unsigned long *sp);
146 unsigned long sp = regs->regs[15];
148 show_task((unsigned long *)sp);
153 * Create a kernel thread
157 * This is the mechanism for creating a new kernel thread.
160 extern void kernel_thread_helper(void);
162 "kernel_thread_helper:\n\t"
171 int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
172 { /* Don't use this in BL=1(cli). Or else, CPU resets! */
175 memset(®s, 0, sizeof(regs));
176 regs.regs[4] = (unsigned long) arg;
177 regs.regs[5] = (unsigned long) fn;
179 regs.pc = (unsigned long) kernel_thread_helper;
182 /* Ok, create the new process.. */
183 return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, ®s, 0, NULL, NULL);
187 * Free current thread data structures etc..
189 void exit_thread(void)
191 if (current->thread.ubc_pc) {
192 current->thread.ubc_pc = 0;
197 void flush_thread(void)
199 #if defined(CONFIG_SH_FPU)
200 struct task_struct *tsk = current;
201 /* Forget lazy FPU state */
202 clear_fpu(tsk, task_pt_regs(tsk));
207 void release_thread(struct task_struct *dead_task)
212 /* Fill in the fpu structure for a core dump.. */
213 int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu)
217 #if defined(CONFIG_SH_FPU)
218 struct task_struct *tsk = current;
220 fpvalid = !!tsk_used_math(tsk);
222 unlazy_fpu(tsk, regs);
223 memcpy(fpu, &tsk->thread.fpu.hard, sizeof(*fpu));
231 * Capture the user space registers if the task is not running (in user space)
233 int dump_task_regs(struct task_struct *tsk, elf_gregset_t *regs)
235 struct pt_regs ptregs;
237 ptregs = *task_pt_regs(tsk);
238 elf_core_copy_regs(regs, &ptregs);
244 dump_task_fpu (struct task_struct *tsk, elf_fpregset_t *fpu)
248 #if defined(CONFIG_SH_FPU)
249 fpvalid = !!tsk_used_math(tsk);
251 unlazy_fpu(tsk, task_pt_regs(tsk));
252 memcpy(fpu, &tsk->thread.fpu.hard, sizeof(*fpu));
259 asmlinkage void ret_from_fork(void);
261 int copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
262 unsigned long unused,
263 struct task_struct *p, struct pt_regs *regs)
265 struct pt_regs *childregs;
266 #if defined(CONFIG_SH_FPU)
267 struct task_struct *tsk = current;
269 unlazy_fpu(tsk, regs);
270 p->thread.fpu = tsk->thread.fpu;
271 copy_to_stopped_child_used_math(p);
274 childregs = task_pt_regs(p);
277 if (user_mode(regs)) {
278 childregs->regs[15] = usp;
280 childregs->regs[15] = (unsigned long)task_stack_page(p) + THREAD_SIZE;
282 if (clone_flags & CLONE_SETTLS) {
283 childregs->gbr = childregs->regs[0];
285 childregs->regs[0] = 0; /* Set return value for child */
287 p->thread.sp = (unsigned long) childregs;
288 p->thread.pc = (unsigned long) ret_from_fork;
290 p->thread.ubc_pc = 0;
295 /* Tracing by user break controller. */
297 ubc_set_tracing(int asid, unsigned long pc)
299 ctrl_outl(pc, UBC_BARA);
301 /* We don't have any ASID settings for the SH-2! */
302 if (cpu_data->type != CPU_SH7604)
303 ctrl_outb(asid, UBC_BASRA);
305 ctrl_outl(0, UBC_BAMRA);
307 if (cpu_data->type == CPU_SH7729) {
308 ctrl_outw(BBR_INST | BBR_READ | BBR_CPU, UBC_BBRA);
309 ctrl_outl(BRCR_PCBA | BRCR_PCTE, UBC_BRCR);
311 ctrl_outw(BBR_INST | BBR_READ, UBC_BBRA);
312 ctrl_outw(BRCR_PCBA, UBC_BRCR);
317 * switch_to(x,y) should switch tasks from x to y.
320 struct task_struct *__switch_to(struct task_struct *prev, struct task_struct *next)
322 #if defined(CONFIG_SH_FPU)
323 unlazy_fpu(prev, task_pt_regs(prev));
326 #ifdef CONFIG_PREEMPT
329 struct pt_regs *regs;
331 local_irq_save(flags);
332 regs = task_pt_regs(prev);
333 if (user_mode(regs) && regs->regs[15] >= 0xc0000000) {
334 int offset = (int)regs->regs[15];
336 /* Reset stack pointer: clear critical region mark */
337 regs->regs[15] = regs->regs[1];
338 if (regs->pc < regs->regs[0])
339 /* Go to rewind point */
340 regs->pc = regs->regs[0] + offset;
342 local_irq_restore(flags);
347 * Restore the kernel mode register
350 asm volatile("ldc %0, r7_bank"
352 : "r" (task_thread_info(next)));
355 /* If no tasks are using the UBC, we're done */
356 if (ubc_usercnt == 0)
357 /* If no tasks are using the UBC, we're done */;
358 else if (next->thread.ubc_pc && next->mm) {
359 ubc_set_tracing(next->mm->context & MMU_CONTEXT_ASID_MASK,
360 next->thread.ubc_pc);
362 ctrl_outw(0, UBC_BBRA);
363 ctrl_outw(0, UBC_BBRB);
370 asmlinkage int sys_fork(unsigned long r4, unsigned long r5,
371 unsigned long r6, unsigned long r7,
375 return do_fork(SIGCHLD, regs.regs[15], ®s, 0, NULL, NULL);
377 /* fork almost works, enough to trick you into looking elsewhere :-( */
382 asmlinkage int sys_clone(unsigned long clone_flags, unsigned long newsp,
383 unsigned long parent_tidptr,
384 unsigned long child_tidptr,
388 newsp = regs.regs[15];
389 return do_fork(clone_flags, newsp, ®s, 0,
390 (int __user *)parent_tidptr, (int __user *)child_tidptr);
394 * This is trivial, and on the face of it looks like it
395 * could equally well be done in user mode.
397 * Not so, for quite unobvious reasons - register pressure.
398 * In user mode vfork() cannot have a stack frame, and if
399 * done by calling the "clone()" system call directly, you
400 * do not have enough call-clobbered registers to hold all
401 * the information you need.
403 asmlinkage int sys_vfork(unsigned long r4, unsigned long r5,
404 unsigned long r6, unsigned long r7,
407 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs.regs[15], ®s,
412 * sys_execve() executes a new program.
414 asmlinkage int sys_execve(char *ufilename, char **uargv,
415 char **uenvp, unsigned long r7,
421 filename = getname((char __user *)ufilename);
422 error = PTR_ERR(filename);
423 if (IS_ERR(filename))
426 error = do_execve(filename,
427 (char __user * __user *)uargv,
428 (char __user * __user *)uenvp,
432 current->ptrace &= ~PT_DTRACE;
433 task_unlock(current);
440 unsigned long get_wchan(struct task_struct *p)
442 unsigned long schedule_frame;
445 if (!p || p == current || p->state == TASK_RUNNING)
449 * The same comment as on the Alpha applies here, too ...
451 pc = thread_saved_pc(p);
452 if (in_sched_functions(pc)) {
453 schedule_frame = ((unsigned long *)(long)p->thread.sp)[1];
454 return (unsigned long)((unsigned long *)schedule_frame)[1];
459 asmlinkage void break_point_trap(unsigned long r4, unsigned long r5,
460 unsigned long r6, unsigned long r7,
464 ctrl_outw(0, UBC_BBRA);
465 ctrl_outw(0, UBC_BBRB);
466 current->thread.ubc_pc = 0;
469 force_sig(SIGTRAP, current);
472 asmlinkage void break_point_trap_software(unsigned long r4, unsigned long r5,
473 unsigned long r6, unsigned long r7,
477 force_sig(SIGTRAP, current);