1 /* $Id: process.c,v 1.26 2004/02/06 14:14:14 kkojima 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/slab.h>
19 #include <linux/a.out.h>
20 #include <linux/ptrace.h>
21 #include <linux/platform.h>
22 #include <linux/kallsyms.h>
25 #include <asm/uaccess.h>
26 #include <asm/mmu_context.h>
29 static int hlt_counter=0;
33 #define HARD_IDLE_TIMEOUT (HZ / 3)
35 void disable_hlt(void)
40 EXPORT_SYMBOL(disable_hlt);
47 EXPORT_SYMBOL(enable_hlt);
49 void default_idle(void)
51 /* endless idle loop with no priority at all */
53 while (!need_resched())
60 void cpu_idle(void *unused)
65 void machine_restart(char * __unused)
67 /* SR.BL=1 and invoke address error to let CPU reset (manual reset) */
68 asm volatile("ldc %0, sr\n\t"
69 "mov.l @%1, %0" : : "r" (0x10000000), "r" (0x80000001));
72 EXPORT_SYMBOL(machine_restart);
74 void machine_halt(void)
80 EXPORT_SYMBOL(machine_halt);
82 void machine_power_off(void)
86 EXPORT_SYMBOL(machine_power_off);
88 void show_regs(struct pt_regs * regs)
91 printk("Pid : %d, Comm: %20s\n", current->pid, current->comm);
92 print_symbol("PC is at %s\n", regs->pc);
93 printk("PC : %08lx SP : %08lx SR : %08lx ",
94 regs->pc, regs->regs[15], regs->sr);
96 printk("TEA : %08x ", ctrl_inl(MMU_TEA));
100 printk("%s\n", print_tainted());
102 printk("R0 : %08lx R1 : %08lx R2 : %08lx R3 : %08lx\n",
103 regs->regs[0],regs->regs[1],
104 regs->regs[2],regs->regs[3]);
105 printk("R4 : %08lx R5 : %08lx R6 : %08lx R7 : %08lx\n",
106 regs->regs[4],regs->regs[5],
107 regs->regs[6],regs->regs[7]);
108 printk("R8 : %08lx R9 : %08lx R10 : %08lx R11 : %08lx\n",
109 regs->regs[8],regs->regs[9],
110 regs->regs[10],regs->regs[11]);
111 printk("R12 : %08lx R13 : %08lx R14 : %08lx\n",
112 regs->regs[12],regs->regs[13],
114 printk("MACH: %08lx MACL: %08lx GBR : %08lx PR : %08lx\n",
115 regs->mach, regs->macl, regs->gbr, regs->pr);
118 * If we're in kernel mode, dump the stack too..
120 if (!user_mode(regs)) {
121 extern void show_task(unsigned long *sp);
122 unsigned long sp = regs->regs[15];
124 show_task((unsigned long *)sp);
129 * Create a kernel thread
133 * This is the mechanism for creating a new kernel thread.
136 extern void kernel_thread_helper(void);
138 "kernel_thread_helper:\n\t"
147 int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
148 { /* Don't use this in BL=1(cli). Or else, CPU resets! */
151 memset(®s, 0, sizeof(regs));
152 regs.regs[4] = (unsigned long) arg;
153 regs.regs[5] = (unsigned long) fn;
155 regs.pc = (unsigned long) kernel_thread_helper;
158 /* Ok, create the new process.. */
159 return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, ®s, 0, NULL, NULL);
163 * Free current thread data structures etc..
165 void exit_thread(void)
167 if (current->thread.ubc_pc) {
168 current->thread.ubc_pc = 0;
173 void flush_thread(void)
175 #if defined(CONFIG_CPU_SH4)
176 struct task_struct *tsk = current;
177 struct pt_regs *regs = (struct pt_regs *)
178 ((unsigned long)tsk->thread_info
179 + THREAD_SIZE - sizeof(struct pt_regs)
180 - sizeof(unsigned long));
182 /* Forget lazy FPU state */
183 clear_fpu(tsk, regs);
188 void release_thread(struct task_struct *dead_task)
193 /* Fill in the fpu structure for a core dump.. */
194 int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu)
198 #if defined(CONFIG_CPU_SH4)
199 struct task_struct *tsk = current;
201 fpvalid = tsk->used_math;
203 unlazy_fpu(tsk, regs);
204 memcpy(fpu, &tsk->thread.fpu.hard, sizeof(*fpu));
212 * Capture the user space registers if the task is not running (in user space)
214 int dump_task_regs(struct task_struct *tsk, elf_gregset_t *regs)
216 struct pt_regs ptregs;
218 ptregs = *(struct pt_regs *)
219 ((unsigned long)tsk->thread_info + THREAD_SIZE
220 - sizeof(struct pt_regs)
222 - sizeof(struct pt_dspregs)
224 - sizeof(unsigned long));
225 elf_core_copy_regs(regs, &ptregs);
231 dump_task_fpu (struct task_struct *tsk, elf_fpregset_t *fpu)
235 #if defined(CONFIG_CPU_SH4)
236 fpvalid = tsk->used_math;
238 struct pt_regs *regs = (struct pt_regs *)
239 ((unsigned long)tsk->thread_info
240 + THREAD_SIZE - sizeof(struct pt_regs)
241 - sizeof(unsigned long));
242 unlazy_fpu(tsk, regs);
243 memcpy(fpu, &tsk->thread.fpu.hard, sizeof(*fpu));
250 asmlinkage void ret_from_fork(void);
252 int copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
253 unsigned long unused,
254 struct task_struct *p, struct pt_regs *regs)
256 struct pt_regs *childregs;
257 #if defined(CONFIG_CPU_SH4)
258 struct task_struct *tsk = current;
260 unlazy_fpu(tsk, regs);
261 p->thread.fpu = tsk->thread.fpu;
262 p->used_math = tsk->used_math;
265 childregs = ((struct pt_regs *)
266 (THREAD_SIZE + (unsigned long) p->thread_info)
268 - sizeof(struct pt_dspregs)
270 - sizeof(unsigned long)) - 1;
273 if (user_mode(regs)) {
274 childregs->regs[15] = usp;
276 childregs->regs[15] = (unsigned long)p->thread_info + THREAD_SIZE;
278 if (clone_flags & CLONE_SETTLS) {
279 childregs->gbr = childregs->regs[0];
281 childregs->regs[0] = 0; /* Set return value for child */
282 p->set_child_tid = p->clear_child_tid = NULL;
284 p->thread.sp = (unsigned long) childregs;
285 p->thread.pc = (unsigned long) ret_from_fork;
287 p->thread.ubc_pc = 0;
293 * fill in the user structure for a core dump..
295 void dump_thread(struct pt_regs * regs, struct user * dump)
297 dump->magic = CMAGIC;
298 dump->start_code = current->mm->start_code;
299 dump->start_data = current->mm->start_data;
300 dump->start_stack = regs->regs[15] & ~(PAGE_SIZE - 1);
301 dump->u_tsize = (current->mm->end_code - dump->start_code) >> PAGE_SHIFT;
302 dump->u_dsize = (current->mm->brk + (PAGE_SIZE-1) - dump->start_data) >> PAGE_SHIFT;
303 dump->u_ssize = (current->mm->start_stack - dump->start_stack +
304 PAGE_SIZE - 1) >> PAGE_SHIFT;
305 /* Debug registers will come here. */
309 dump->u_fpvalid = dump_fpu(regs, &dump->fpu);
312 /* Tracing by user break controller. */
314 ubc_set_tracing(int asid, unsigned long pc)
316 ctrl_outl(pc, UBC_BARA);
318 /* We don't have any ASID settings for the SH-2! */
319 if (cpu_data->type != CPU_SH7604)
320 ctrl_outb(asid, UBC_BASRA);
322 ctrl_outl(0, UBC_BAMRA);
324 if (cpu_data->type == CPU_SH7729) {
325 ctrl_outw(BBR_INST | BBR_READ | BBR_CPU, UBC_BBRA);
326 ctrl_outl(BRCR_PCBA | BRCR_PCTE, UBC_BRCR);
328 ctrl_outw(BBR_INST | BBR_READ, UBC_BBRA);
329 ctrl_outw(BRCR_PCBA, UBC_BRCR);
334 * switch_to(x,y) should switch tasks from x to y.
337 struct task_struct *__switch_to(struct task_struct *prev, struct task_struct *next)
339 #if defined(CONFIG_CPU_SH4)
340 struct pt_regs *regs = (struct pt_regs *)
341 ((unsigned long)prev->thread_info
342 + THREAD_SIZE - sizeof(struct pt_regs)
343 - sizeof(unsigned long));
344 unlazy_fpu(prev, regs);
347 #ifdef CONFIG_PREEMPT
350 struct pt_regs *regs;
352 local_irq_save(flags);
353 regs = (struct pt_regs *)
354 ((unsigned long)prev->thread_info
355 + THREAD_SIZE - sizeof(struct pt_regs)
357 - sizeof(struct pt_dspregs)
359 - sizeof(unsigned long));
360 if (user_mode(regs) && regs->regs[15] >= 0xc0000000) {
361 int offset = (int)regs->regs[15];
363 /* Reset stack pointer: clear critical region mark */
364 regs->regs[15] = regs->regs[1];
365 if (regs->pc < regs->regs[0])
366 /* Go to rewind point */
367 regs->pc = regs->regs[0] + offset;
369 local_irq_restore(flags);
374 * Restore the kernel mode register
377 asm volatile("ldc %0, r7_bank"
379 : "r" (next->thread_info));
382 /* If no tasks are using the UBC, we're done */
383 if (ubc_usercnt == 0)
384 /* If no tasks are using the UBC, we're done */;
385 else if (next->thread.ubc_pc && next->mm) {
386 ubc_set_tracing(next->mm->context & MMU_CONTEXT_ASID_MASK,
387 next->thread.ubc_pc);
389 ctrl_outw(0, UBC_BBRA);
390 ctrl_outw(0, UBC_BBRB);
397 asmlinkage int sys_fork(unsigned long r4, unsigned long r5,
398 unsigned long r6, unsigned long r7,
402 return do_fork(SIGCHLD, regs.regs[15], ®s, 0, NULL, NULL);
404 /* fork almost works, enough to trick you into looking elsewhere :-( */
409 asmlinkage int sys_clone(unsigned long clone_flags, unsigned long newsp,
410 unsigned long parent_tidptr,
411 unsigned long child_tidptr,
415 newsp = regs.regs[15];
416 return do_fork(clone_flags & ~CLONE_IDLETASK, newsp, ®s, 0,
417 (int __user *)parent_tidptr, (int __user *)child_tidptr);
421 * This is trivial, and on the face of it looks like it
422 * could equally well be done in user mode.
424 * Not so, for quite unobvious reasons - register pressure.
425 * In user mode vfork() cannot have a stack frame, and if
426 * done by calling the "clone()" system call directly, you
427 * do not have enough call-clobbered registers to hold all
428 * the information you need.
430 asmlinkage int sys_vfork(unsigned long r4, unsigned long r5,
431 unsigned long r6, unsigned long r7,
434 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs.regs[15], ®s,
439 * sys_execve() executes a new program.
441 asmlinkage int sys_execve(char *ufilename, char **uargv,
442 char **uenvp, unsigned long r7,
448 filename = getname((char __user *)ufilename);
449 error = PTR_ERR(filename);
450 if (IS_ERR(filename))
453 error = do_execve(filename,
454 (char __user * __user *)uargv,
455 (char __user * __user *)uenvp,
458 current->ptrace &= ~PT_DTRACE;
464 unsigned long get_wchan(struct task_struct *p)
466 unsigned long schedule_frame;
469 if (!p || p == current || p->state == TASK_RUNNING)
473 * The same comment as on the Alpha applies here, too ...
475 pc = thread_saved_pc(p);
476 if (in_sched_functions(pc)) {
477 schedule_frame = ((unsigned long *)(long)p->thread.sp)[1];
478 return (unsigned long)((unsigned long *)schedule_frame)[1];
483 asmlinkage void break_point_trap(unsigned long r4, unsigned long r5,
484 unsigned long r6, unsigned long r7,
488 ctrl_outw(0, UBC_BBRA);
489 ctrl_outw(0, UBC_BBRB);
490 current->thread.ubc_pc = 0;
493 force_sig(SIGTRAP, current);
496 asmlinkage void break_point_trap_software(unsigned long r4, unsigned long r5,
497 unsigned long r6, unsigned long r7,
501 force_sig(SIGTRAP, current);