2 * arch/sh/kernel/process.c
4 * This file handles the architecture-dependent parts of process handling..
6 * Copyright (C) 1995 Linus Torvalds
8 * SuperH version: Copyright (C) 1999, 2000 Niibe Yutaka & Kaz Kojima
9 * Copyright (C) 2006 Lineo Solutions Inc. support SH4A UBC
10 * Copyright (C) 2002 - 2007 Paul Mundt
12 #include <linux/module.h>
14 #include <linux/elfcore.h>
16 #include <linux/kallsyms.h>
17 #include <linux/kexec.h>
18 #include <linux/kdebug.h>
19 #include <linux/tick.h>
20 #include <linux/reboot.h>
22 #include <linux/preempt.h>
23 #include <asm/uaccess.h>
24 #include <asm/mmu_context.h>
25 #include <asm/pgalloc.h>
26 #include <asm/system.h>
29 #include <asm/syscalls.h>
31 static int hlt_counter;
34 void (*pm_idle)(void);
35 void (*pm_power_off)(void);
36 EXPORT_SYMBOL(pm_power_off);
38 static int __init nohlt_setup(char *__unused)
43 __setup("nohlt", nohlt_setup);
45 static int __init hlt_setup(char *__unused)
50 __setup("hlt", hlt_setup);
52 static void default_idle(void)
55 clear_thread_flag(TIF_POLLING_NRFLAG);
56 smp_mb__after_clear_bit();
58 while (!need_resched())
61 set_thread_flag(TIF_POLLING_NRFLAG);
63 while (!need_resched())
69 set_thread_flag(TIF_POLLING_NRFLAG);
71 /* endless idle loop with no priority at all */
73 void (*idle)(void) = pm_idle;
78 tick_nohz_stop_sched_tick(1);
79 while (!need_resched())
81 tick_nohz_restart_sched_tick();
83 preempt_enable_no_resched();
90 void machine_restart(char * __unused)
92 /* SR.BL=1 and invoke address error to let CPU reset (manual reset) */
93 asm volatile("ldc %0, sr\n\t"
94 "mov.l @%1, %0" : : "r" (0x10000000), "r" (0x80000001));
97 void machine_halt(void)
105 void machine_power_off(void)
111 void show_regs(struct pt_regs * regs)
114 printk("Pid : %d, Comm: %20s\n", task_pid_nr(current), current->comm);
115 printk("CPU : %d %s (%s %.*s)\n",
116 smp_processor_id(), print_tainted(), init_utsname()->release,
117 (int)strcspn(init_utsname()->version, " "),
118 init_utsname()->version);
120 print_symbol("PC is at %s\n", instruction_pointer(regs));
121 print_symbol("PR is at %s\n", regs->pr);
123 printk("PC : %08lx SP : %08lx SR : %08lx ",
124 regs->pc, regs->regs[15], regs->sr);
126 printk("TEA : %08x\n", ctrl_inl(MMU_TEA));
131 printk("R0 : %08lx R1 : %08lx R2 : %08lx R3 : %08lx\n",
132 regs->regs[0],regs->regs[1],
133 regs->regs[2],regs->regs[3]);
134 printk("R4 : %08lx R5 : %08lx R6 : %08lx R7 : %08lx\n",
135 regs->regs[4],regs->regs[5],
136 regs->regs[6],regs->regs[7]);
137 printk("R8 : %08lx R9 : %08lx R10 : %08lx R11 : %08lx\n",
138 regs->regs[8],regs->regs[9],
139 regs->regs[10],regs->regs[11]);
140 printk("R12 : %08lx R13 : %08lx R14 : %08lx\n",
141 regs->regs[12],regs->regs[13],
143 printk("MACH: %08lx MACL: %08lx GBR : %08lx PR : %08lx\n",
144 regs->mach, regs->macl, regs->gbr, regs->pr);
146 show_trace(NULL, (unsigned long *)regs->regs[15], regs);
150 * Create a kernel thread
154 * This is the mechanism for creating a new kernel thread.
157 extern void kernel_thread_helper(void);
159 "kernel_thread_helper:\n\t"
168 /* Don't use this in BL=1(cli). Or else, CPU resets! */
169 int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
173 memset(®s, 0, sizeof(regs));
174 regs.regs[4] = (unsigned long)arg;
175 regs.regs[5] = (unsigned long)fn;
177 regs.pc = (unsigned long)kernel_thread_helper;
180 /* Ok, create the new process.. */
181 return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0,
182 ®s, 0, NULL, NULL);
186 * Free current thread data structures etc..
188 void exit_thread(void)
190 if (current->thread.ubc_pc) {
191 current->thread.ubc_pc = 0;
196 void flush_thread(void)
198 #if defined(CONFIG_SH_FPU)
199 struct task_struct *tsk = current;
200 /* Forget lazy FPU state */
201 clear_fpu(tsk, task_pt_regs(tsk));
206 void release_thread(struct task_struct *dead_task)
211 /* Fill in the fpu structure for a core dump.. */
212 int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu)
216 #if defined(CONFIG_SH_FPU)
217 struct task_struct *tsk = current;
219 fpvalid = !!tsk_used_math(tsk);
221 unlazy_fpu(tsk, regs);
222 memcpy(fpu, &tsk->thread.fpu.hard, sizeof(*fpu));
229 asmlinkage void ret_from_fork(void);
231 int copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
232 unsigned long unused,
233 struct task_struct *p, struct pt_regs *regs)
235 struct thread_info *ti = task_thread_info(p);
236 struct pt_regs *childregs;
237 #if defined(CONFIG_SH_FPU)
238 struct task_struct *tsk = current;
240 unlazy_fpu(tsk, regs);
241 p->thread.fpu = tsk->thread.fpu;
242 copy_to_stopped_child_used_math(p);
245 childregs = task_pt_regs(p);
248 if (user_mode(regs)) {
249 childregs->regs[15] = usp;
250 ti->addr_limit = USER_DS;
252 childregs->regs[15] = (unsigned long)childregs;
253 ti->addr_limit = KERNEL_DS;
256 if (clone_flags & CLONE_SETTLS)
257 childregs->gbr = childregs->regs[0];
259 childregs->regs[0] = 0; /* Set return value for child */
261 p->thread.sp = (unsigned long) childregs;
262 p->thread.pc = (unsigned long) ret_from_fork;
264 p->thread.ubc_pc = 0;
269 /* Tracing by user break controller. */
270 static void ubc_set_tracing(int asid, unsigned long pc)
272 #if defined(CONFIG_CPU_SH4A)
275 val = (UBC_CBR_ID_INST | UBC_CBR_RW_READ | UBC_CBR_CE);
276 val |= (UBC_CBR_AIE | UBC_CBR_AIV_SET(asid));
278 ctrl_outl(val, UBC_CBR0);
279 ctrl_outl(pc, UBC_CAR0);
280 ctrl_outl(0x0, UBC_CAMR0);
281 ctrl_outl(0x0, UBC_CBCR);
283 val = (UBC_CRR_RES | UBC_CRR_PCB | UBC_CRR_BIE);
284 ctrl_outl(val, UBC_CRR0);
286 /* Read UBC register that we wrote last, for checking update */
287 val = ctrl_inl(UBC_CRR0);
289 #else /* CONFIG_CPU_SH4A */
290 ctrl_outl(pc, UBC_BARA);
293 ctrl_outb(asid, UBC_BASRA);
296 ctrl_outl(0, UBC_BAMRA);
298 if (current_cpu_data.type == CPU_SH7729 ||
299 current_cpu_data.type == CPU_SH7710 ||
300 current_cpu_data.type == CPU_SH7712) {
301 ctrl_outw(BBR_INST | BBR_READ | BBR_CPU, UBC_BBRA);
302 ctrl_outl(BRCR_PCBA | BRCR_PCTE, UBC_BRCR);
304 ctrl_outw(BBR_INST | BBR_READ, UBC_BBRA);
305 ctrl_outw(BRCR_PCBA, UBC_BRCR);
307 #endif /* CONFIG_CPU_SH4A */
311 * switch_to(x,y) should switch tasks from x to y.
314 struct task_struct *__switch_to(struct task_struct *prev,
315 struct task_struct *next)
317 #if defined(CONFIG_SH_FPU)
318 unlazy_fpu(prev, task_pt_regs(prev));
323 * Restore the kernel mode register
326 asm volatile("ldc %0, r7_bank"
328 : "r" (task_thread_info(next)));
331 /* If no tasks are using the UBC, we're done */
332 if (ubc_usercnt == 0)
333 /* If no tasks are using the UBC, we're done */;
334 else if (next->thread.ubc_pc && next->mm) {
337 asid |= cpu_asid(smp_processor_id(), next->mm);
339 ubc_set_tracing(asid, next->thread.ubc_pc);
341 #if defined(CONFIG_CPU_SH4A)
342 ctrl_outl(UBC_CBR_INIT, UBC_CBR0);
343 ctrl_outl(UBC_CRR_INIT, UBC_CRR0);
345 ctrl_outw(0, UBC_BBRA);
346 ctrl_outw(0, UBC_BBRB);
353 asmlinkage int sys_fork(unsigned long r4, unsigned long r5,
354 unsigned long r6, unsigned long r7,
355 struct pt_regs __regs)
358 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
359 return do_fork(SIGCHLD, regs->regs[15], regs, 0, NULL, NULL);
361 /* fork almost works, enough to trick you into looking elsewhere :-( */
366 asmlinkage int sys_clone(unsigned long clone_flags, unsigned long newsp,
367 unsigned long parent_tidptr,
368 unsigned long child_tidptr,
369 struct pt_regs __regs)
371 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
373 newsp = regs->regs[15];
374 return do_fork(clone_flags, newsp, regs, 0,
375 (int __user *)parent_tidptr,
376 (int __user *)child_tidptr);
380 * This is trivial, and on the face of it looks like it
381 * could equally well be done in user mode.
383 * Not so, for quite unobvious reasons - register pressure.
384 * In user mode vfork() cannot have a stack frame, and if
385 * done by calling the "clone()" system call directly, you
386 * do not have enough call-clobbered registers to hold all
387 * the information you need.
389 asmlinkage int sys_vfork(unsigned long r4, unsigned long r5,
390 unsigned long r6, unsigned long r7,
391 struct pt_regs __regs)
393 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
394 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->regs[15], regs,
399 * sys_execve() executes a new program.
401 asmlinkage int sys_execve(char __user *ufilename, char __user * __user *uargv,
402 char __user * __user *uenvp, unsigned long r7,
403 struct pt_regs __regs)
405 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
409 filename = getname(ufilename);
410 error = PTR_ERR(filename);
411 if (IS_ERR(filename))
414 error = do_execve(filename, uargv, uenvp, regs);
417 current->ptrace &= ~PT_DTRACE;
418 task_unlock(current);
425 unsigned long get_wchan(struct task_struct *p)
429 if (!p || p == current || p->state == TASK_RUNNING)
433 * The same comment as on the Alpha applies here, too ...
435 pc = thread_saved_pc(p);
437 #ifdef CONFIG_FRAME_POINTER
438 if (in_sched_functions(pc)) {
439 unsigned long schedule_frame = (unsigned long)p->thread.sp;
440 return ((unsigned long *)schedule_frame)[21];
447 asmlinkage void break_point_trap(void)
450 #if defined(CONFIG_CPU_SH4A)
451 ctrl_outl(UBC_CBR_INIT, UBC_CBR0);
452 ctrl_outl(UBC_CRR_INIT, UBC_CRR0);
454 ctrl_outw(0, UBC_BBRA);
455 ctrl_outw(0, UBC_BBRB);
457 current->thread.ubc_pc = 0;
460 force_sig(SIGTRAP, current);