blob: 35415d0be39e4ef553ac7f29dd3c21edfdd4c512 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* $Id: process.c,v 1.28 2004/05/05 16:54:23 lethal Exp $
2 *
3 * linux/arch/sh/kernel/process.c
4 *
5 * Copyright (C) 1995 Linus Torvalds
6 *
7 * SuperH version: Copyright (C) 1999, 2000 Niibe Yutaka & Kaz Kojima
8 */
9
10/*
11 * This file handles the architecture-dependent parts of process handling..
12 */
13
14#include <linux/module.h>
15#include <linux/unistd.h>
16#include <linux/mm.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>
23
24#include <asm/io.h>
25#include <asm/uaccess.h>
26#include <asm/mmu_context.h>
27#include <asm/elf.h>
28#if defined(CONFIG_SH_HS7751RVOIP)
29#include <asm/hs7751rvoip/hs7751rvoip.h>
30#elif defined(CONFIG_SH_RTS7751R2D)
31#include <asm/rts7751r2d/rts7751r2d.h>
32#endif
33
34static int hlt_counter=0;
35
36int ubc_usercnt = 0;
37
38#define HARD_IDLE_TIMEOUT (HZ / 3)
39
40void disable_hlt(void)
41{
42 hlt_counter++;
43}
44
45EXPORT_SYMBOL(disable_hlt);
46
47void enable_hlt(void)
48{
49 hlt_counter--;
50}
51
52EXPORT_SYMBOL(enable_hlt);
53
Nick Piggin64c7c8f2005-11-08 21:39:04 -080054void cpu_idle(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
56 /* endless idle loop with no priority at all */
57 while (1) {
58 if (hlt_counter) {
Nick Piggin64c7c8f2005-11-08 21:39:04 -080059 while (!need_resched())
60 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 } else {
62 while (!need_resched())
63 cpu_sleep();
64 }
65
Nick Piggin5bfb5d62005-11-08 21:39:01 -080066 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 schedule();
Nick Piggin5bfb5d62005-11-08 21:39:01 -080068 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 }
70}
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072void machine_restart(char * __unused)
73{
74 /* SR.BL=1 and invoke address error to let CPU reset (manual reset) */
75 asm volatile("ldc %0, sr\n\t"
76 "mov.l @%1, %0" : : "r" (0x10000000), "r" (0x80000001));
77}
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079void machine_halt(void)
80{
81#if defined(CONFIG_SH_HS7751RVOIP)
82 unsigned short value;
83
84 value = ctrl_inw(PA_OUTPORTR);
85 ctrl_outw((value & 0xffdf), PA_OUTPORTR);
86#elif defined(CONFIG_SH_RTS7751R2D)
87 ctrl_outw(0x0001, PA_POWOFF);
88#endif
89 while (1)
90 cpu_sleep();
91}
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093void machine_power_off(void)
94{
95#if defined(CONFIG_SH_HS7751RVOIP)
96 unsigned short value;
97
98 value = ctrl_inw(PA_OUTPORTR);
99 ctrl_outw((value & 0xffdf), PA_OUTPORTR);
100#elif defined(CONFIG_SH_RTS7751R2D)
101 ctrl_outw(0x0001, PA_POWOFF);
102#endif
103}
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105void show_regs(struct pt_regs * regs)
106{
107 printk("\n");
108 printk("Pid : %d, Comm: %20s\n", current->pid, current->comm);
109 print_symbol("PC is at %s\n", regs->pc);
110 printk("PC : %08lx SP : %08lx SR : %08lx ",
111 regs->pc, regs->regs[15], regs->sr);
112#ifdef CONFIG_MMU
113 printk("TEA : %08x ", ctrl_inl(MMU_TEA));
114#else
115 printk(" ");
116#endif
117 printk("%s\n", print_tainted());
118
119 printk("R0 : %08lx R1 : %08lx R2 : %08lx R3 : %08lx\n",
120 regs->regs[0],regs->regs[1],
121 regs->regs[2],regs->regs[3]);
122 printk("R4 : %08lx R5 : %08lx R6 : %08lx R7 : %08lx\n",
123 regs->regs[4],regs->regs[5],
124 regs->regs[6],regs->regs[7]);
125 printk("R8 : %08lx R9 : %08lx R10 : %08lx R11 : %08lx\n",
126 regs->regs[8],regs->regs[9],
127 regs->regs[10],regs->regs[11]);
128 printk("R12 : %08lx R13 : %08lx R14 : %08lx\n",
129 regs->regs[12],regs->regs[13],
130 regs->regs[14]);
131 printk("MACH: %08lx MACL: %08lx GBR : %08lx PR : %08lx\n",
132 regs->mach, regs->macl, regs->gbr, regs->pr);
133
134 /*
135 * If we're in kernel mode, dump the stack too..
136 */
137 if (!user_mode(regs)) {
138 extern void show_task(unsigned long *sp);
139 unsigned long sp = regs->regs[15];
140
141 show_task((unsigned long *)sp);
142 }
143}
144
145/*
146 * Create a kernel thread
147 */
148
149/*
150 * This is the mechanism for creating a new kernel thread.
151 *
152 */
153extern void kernel_thread_helper(void);
154__asm__(".align 5\n"
155 "kernel_thread_helper:\n\t"
156 "jsr @r5\n\t"
157 " nop\n\t"
158 "mov.l 1f, r1\n\t"
159 "jsr @r1\n\t"
160 " mov r0, r4\n\t"
161 ".align 2\n\t"
162 "1:.long do_exit");
163
164int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
165{ /* Don't use this in BL=1(cli). Or else, CPU resets! */
166 struct pt_regs regs;
167
168 memset(&regs, 0, sizeof(regs));
169 regs.regs[4] = (unsigned long) arg;
170 regs.regs[5] = (unsigned long) fn;
171
172 regs.pc = (unsigned long) kernel_thread_helper;
173 regs.sr = (1 << 30);
174
175 /* Ok, create the new process.. */
176 return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, &regs, 0, NULL, NULL);
177}
178
179/*
180 * Free current thread data structures etc..
181 */
182void exit_thread(void)
183{
184 if (current->thread.ubc_pc) {
185 current->thread.ubc_pc = 0;
186 ubc_usercnt -= 1;
187 }
188}
189
190void flush_thread(void)
191{
192#if defined(CONFIG_SH_FPU)
193 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 /* Forget lazy FPU state */
Al Viro3cf0f4e2006-01-12 01:05:44 -0800195 clear_fpu(tsk, task_pt_regs(tsk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 clear_used_math();
197#endif
198}
199
200void release_thread(struct task_struct *dead_task)
201{
202 /* do nothing */
203}
204
205/* Fill in the fpu structure for a core dump.. */
206int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu)
207{
208 int fpvalid = 0;
209
210#if defined(CONFIG_SH_FPU)
211 struct task_struct *tsk = current;
212
213 fpvalid = !!tsk_used_math(tsk);
214 if (fpvalid) {
215 unlazy_fpu(tsk, regs);
216 memcpy(fpu, &tsk->thread.fpu.hard, sizeof(*fpu));
217 }
218#endif
219
220 return fpvalid;
221}
222
223/*
224 * Capture the user space registers if the task is not running (in user space)
225 */
226int dump_task_regs(struct task_struct *tsk, elf_gregset_t *regs)
227{
228 struct pt_regs ptregs;
229
Al Viro3cf0f4e2006-01-12 01:05:44 -0800230 ptregs = *task_pt_regs(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 elf_core_copy_regs(regs, &ptregs);
232
233 return 1;
234}
235
236int
237dump_task_fpu (struct task_struct *tsk, elf_fpregset_t *fpu)
238{
239 int fpvalid = 0;
240
241#if defined(CONFIG_SH_FPU)
242 fpvalid = !!tsk_used_math(tsk);
243 if (fpvalid) {
Al Viro3cf0f4e2006-01-12 01:05:44 -0800244 unlazy_fpu(tsk, task_pt_regs(tsk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 memcpy(fpu, &tsk->thread.fpu.hard, sizeof(*fpu));
246 }
247#endif
248
249 return fpvalid;
250}
251
252asmlinkage void ret_from_fork(void);
253
254int copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
255 unsigned long unused,
256 struct task_struct *p, struct pt_regs *regs)
257{
258 struct pt_regs *childregs;
259#if defined(CONFIG_SH_FPU)
260 struct task_struct *tsk = current;
261
262 unlazy_fpu(tsk, regs);
263 p->thread.fpu = tsk->thread.fpu;
264 copy_to_stopped_child_used_math(p);
265#endif
266
Al Viro3cf0f4e2006-01-12 01:05:44 -0800267 childregs = task_pt_regs(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 *childregs = *regs;
269
270 if (user_mode(regs)) {
271 childregs->regs[15] = usp;
272 } else {
273 childregs->regs[15] = (unsigned long)p->thread_info + THREAD_SIZE;
274 }
275 if (clone_flags & CLONE_SETTLS) {
276 childregs->gbr = childregs->regs[0];
277 }
278 childregs->regs[0] = 0; /* Set return value for child */
279
280 p->thread.sp = (unsigned long) childregs;
281 p->thread.pc = (unsigned long) ret_from_fork;
282
283 p->thread.ubc_pc = 0;
284
285 return 0;
286}
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288/* Tracing by user break controller. */
289static void
290ubc_set_tracing(int asid, unsigned long pc)
291{
292 ctrl_outl(pc, UBC_BARA);
293
294 /* We don't have any ASID settings for the SH-2! */
295 if (cpu_data->type != CPU_SH7604)
296 ctrl_outb(asid, UBC_BASRA);
297
298 ctrl_outl(0, UBC_BAMRA);
299
300 if (cpu_data->type == CPU_SH7729) {
301 ctrl_outw(BBR_INST | BBR_READ | BBR_CPU, UBC_BBRA);
302 ctrl_outl(BRCR_PCBA | BRCR_PCTE, UBC_BRCR);
303 } else {
304 ctrl_outw(BBR_INST | BBR_READ, UBC_BBRA);
305 ctrl_outw(BRCR_PCBA, UBC_BRCR);
306 }
307}
308
309/*
310 * switch_to(x,y) should switch tasks from x to y.
311 *
312 */
313struct task_struct *__switch_to(struct task_struct *prev, struct task_struct *next)
314{
315#if defined(CONFIG_SH_FPU)
Al Viro3cf0f4e2006-01-12 01:05:44 -0800316 unlazy_fpu(prev, task_pt_regs(prev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317#endif
318
319#ifdef CONFIG_PREEMPT
320 {
321 unsigned long flags;
322 struct pt_regs *regs;
323
324 local_irq_save(flags);
Al Viro3cf0f4e2006-01-12 01:05:44 -0800325 regs = task_pt_regs(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (user_mode(regs) && regs->regs[15] >= 0xc0000000) {
327 int offset = (int)regs->regs[15];
328
329 /* Reset stack pointer: clear critical region mark */
330 regs->regs[15] = regs->regs[1];
331 if (regs->pc < regs->regs[0])
332 /* Go to rewind point */
333 regs->pc = regs->regs[0] + offset;
334 }
335 local_irq_restore(flags);
336 }
337#endif
338
339 /*
340 * Restore the kernel mode register
341 * k7 (r7_bank1)
342 */
343 asm volatile("ldc %0, r7_bank"
344 : /* no output */
Al Virocafcfca2006-01-12 01:05:45 -0800345 : "r" (task_thread_info(next)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347#ifdef CONFIG_MMU
348 /* If no tasks are using the UBC, we're done */
349 if (ubc_usercnt == 0)
350 /* If no tasks are using the UBC, we're done */;
351 else if (next->thread.ubc_pc && next->mm) {
352 ubc_set_tracing(next->mm->context & MMU_CONTEXT_ASID_MASK,
353 next->thread.ubc_pc);
354 } else {
355 ctrl_outw(0, UBC_BBRA);
356 ctrl_outw(0, UBC_BBRB);
357 }
358#endif
359
360 return prev;
361}
362
363asmlinkage int sys_fork(unsigned long r4, unsigned long r5,
364 unsigned long r6, unsigned long r7,
365 struct pt_regs regs)
366{
367#ifdef CONFIG_MMU
368 return do_fork(SIGCHLD, regs.regs[15], &regs, 0, NULL, NULL);
369#else
370 /* fork almost works, enough to trick you into looking elsewhere :-( */
371 return -EINVAL;
372#endif
373}
374
375asmlinkage int sys_clone(unsigned long clone_flags, unsigned long newsp,
376 unsigned long parent_tidptr,
377 unsigned long child_tidptr,
378 struct pt_regs regs)
379{
380 if (!newsp)
381 newsp = regs.regs[15];
382 return do_fork(clone_flags, newsp, &regs, 0,
383 (int __user *)parent_tidptr, (int __user *)child_tidptr);
384}
385
386/*
387 * This is trivial, and on the face of it looks like it
388 * could equally well be done in user mode.
389 *
390 * Not so, for quite unobvious reasons - register pressure.
391 * In user mode vfork() cannot have a stack frame, and if
392 * done by calling the "clone()" system call directly, you
393 * do not have enough call-clobbered registers to hold all
394 * the information you need.
395 */
396asmlinkage int sys_vfork(unsigned long r4, unsigned long r5,
397 unsigned long r6, unsigned long r7,
398 struct pt_regs regs)
399{
400 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs.regs[15], &regs,
401 0, NULL, NULL);
402}
403
404/*
405 * sys_execve() executes a new program.
406 */
407asmlinkage int sys_execve(char *ufilename, char **uargv,
408 char **uenvp, unsigned long r7,
409 struct pt_regs regs)
410{
411 int error;
412 char *filename;
413
414 filename = getname((char __user *)ufilename);
415 error = PTR_ERR(filename);
416 if (IS_ERR(filename))
417 goto out;
418
419 error = do_execve(filename,
420 (char __user * __user *)uargv,
421 (char __user * __user *)uenvp,
422 &regs);
423 if (error == 0) {
424 task_lock(current);
425 current->ptrace &= ~PT_DTRACE;
426 task_unlock(current);
427 }
428 putname(filename);
429out:
430 return error;
431}
432
433unsigned long get_wchan(struct task_struct *p)
434{
435 unsigned long schedule_frame;
436 unsigned long pc;
437
438 if (!p || p == current || p->state == TASK_RUNNING)
439 return 0;
440
441 /*
442 * The same comment as on the Alpha applies here, too ...
443 */
444 pc = thread_saved_pc(p);
445 if (in_sched_functions(pc)) {
446 schedule_frame = ((unsigned long *)(long)p->thread.sp)[1];
447 return (unsigned long)((unsigned long *)schedule_frame)[1];
448 }
449 return pc;
450}
451
452asmlinkage void break_point_trap(unsigned long r4, unsigned long r5,
453 unsigned long r6, unsigned long r7,
454 struct pt_regs regs)
455{
456 /* Clear tracing. */
457 ctrl_outw(0, UBC_BBRA);
458 ctrl_outw(0, UBC_BBRB);
459 current->thread.ubc_pc = 0;
460 ubc_usercnt -= 1;
461
462 force_sig(SIGTRAP, current);
463}
464
465asmlinkage void break_point_trap_software(unsigned long r4, unsigned long r5,
466 unsigned long r6, unsigned long r7,
467 struct pt_regs regs)
468{
469 regs.pc -= 2;
470 force_sig(SIGTRAP, current);
471}