2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * arch/sh64/kernel/process.c
8 * Copyright (C) 2000, 2001 Paolo Alberelli
9 * Copyright (C) 2003 Paul Mundt
10 * Copyright (C) 2003, 2004 Richard Curnow
12 * Started from SH3/4 version:
13 * Copyright (C) 1999, 2000 Niibe Yutaka & Kaz Kojima
15 * In turn started from i386 version:
16 * Copyright (C) 1995 Linus Torvalds
21 * This file handles the architecture-dependent parts of process handling..
25 #include <linux/ptrace.h>
26 #include <linux/reboot.h>
27 #include <linux/init.h>
28 #include <linux/module.h>
29 #include <linux/proc_fs.h>
30 #include <asm/uaccess.h>
31 #include <asm/pgtable.h>
33 struct task_struct *last_task_used_math = NULL;
35 static int hlt_counter = 1;
37 #define HARD_IDLE_TIMEOUT (HZ / 3)
39 void disable_hlt(void)
49 static int __init nohlt_setup(char *__unused)
55 static int __init hlt_setup(char *__unused)
61 __setup("nohlt", nohlt_setup);
62 __setup("hlt", hlt_setup);
64 static inline void hlt(void)
66 __asm__ __volatile__ ("sleep" : : : "memory");
70 * The idle loop on a uniprocessor SH..
74 /* endless idle loop with no priority at all */
77 while (!need_resched())
81 while (!need_resched()) {
88 preempt_enable_no_resched();
95 void machine_restart(char * __unused)
97 extern void phys_stext(void);
102 void machine_halt(void)
107 void machine_power_off(void)
109 extern void enter_deep_standby(void);
111 enter_deep_standby();
114 void (*pm_power_off)(void) = machine_power_off;
115 EXPORT_SYMBOL(pm_power_off);
117 void show_regs(struct pt_regs * regs)
119 unsigned long long ah, al, bh, bl, ch, cl;
123 ah = (regs->pc) >> 32;
124 al = (regs->pc) & 0xffffffff;
125 bh = (regs->regs[18]) >> 32;
126 bl = (regs->regs[18]) & 0xffffffff;
127 ch = (regs->regs[15]) >> 32;
128 cl = (regs->regs[15]) & 0xffffffff;
129 printk("PC : %08Lx%08Lx LINK: %08Lx%08Lx SP : %08Lx%08Lx\n",
130 ah, al, bh, bl, ch, cl);
132 ah = (regs->sr) >> 32;
133 al = (regs->sr) & 0xffffffff;
134 asm volatile ("getcon " __TEA ", %0" : "=r" (bh));
135 asm volatile ("getcon " __TEA ", %0" : "=r" (bl));
137 bl = (bl) & 0xffffffff;
138 asm volatile ("getcon " __KCR0 ", %0" : "=r" (ch));
139 asm volatile ("getcon " __KCR0 ", %0" : "=r" (cl));
141 cl = (cl) & 0xffffffff;
142 printk("SR : %08Lx%08Lx TEA : %08Lx%08Lx KCR0: %08Lx%08Lx\n",
143 ah, al, bh, bl, ch, cl);
145 ah = (regs->regs[0]) >> 32;
146 al = (regs->regs[0]) & 0xffffffff;
147 bh = (regs->regs[1]) >> 32;
148 bl = (regs->regs[1]) & 0xffffffff;
149 ch = (regs->regs[2]) >> 32;
150 cl = (regs->regs[2]) & 0xffffffff;
151 printk("R0 : %08Lx%08Lx R1 : %08Lx%08Lx R2 : %08Lx%08Lx\n",
152 ah, al, bh, bl, ch, cl);
154 ah = (regs->regs[3]) >> 32;
155 al = (regs->regs[3]) & 0xffffffff;
156 bh = (regs->regs[4]) >> 32;
157 bl = (regs->regs[4]) & 0xffffffff;
158 ch = (regs->regs[5]) >> 32;
159 cl = (regs->regs[5]) & 0xffffffff;
160 printk("R3 : %08Lx%08Lx R4 : %08Lx%08Lx R5 : %08Lx%08Lx\n",
161 ah, al, bh, bl, ch, cl);
163 ah = (regs->regs[6]) >> 32;
164 al = (regs->regs[6]) & 0xffffffff;
165 bh = (regs->regs[7]) >> 32;
166 bl = (regs->regs[7]) & 0xffffffff;
167 ch = (regs->regs[8]) >> 32;
168 cl = (regs->regs[8]) & 0xffffffff;
169 printk("R6 : %08Lx%08Lx R7 : %08Lx%08Lx R8 : %08Lx%08Lx\n",
170 ah, al, bh, bl, ch, cl);
172 ah = (regs->regs[9]) >> 32;
173 al = (regs->regs[9]) & 0xffffffff;
174 bh = (regs->regs[10]) >> 32;
175 bl = (regs->regs[10]) & 0xffffffff;
176 ch = (regs->regs[11]) >> 32;
177 cl = (regs->regs[11]) & 0xffffffff;
178 printk("R9 : %08Lx%08Lx R10 : %08Lx%08Lx R11 : %08Lx%08Lx\n",
179 ah, al, bh, bl, ch, cl);
181 ah = (regs->regs[12]) >> 32;
182 al = (regs->regs[12]) & 0xffffffff;
183 bh = (regs->regs[13]) >> 32;
184 bl = (regs->regs[13]) & 0xffffffff;
185 ch = (regs->regs[14]) >> 32;
186 cl = (regs->regs[14]) & 0xffffffff;
187 printk("R12 : %08Lx%08Lx R13 : %08Lx%08Lx R14 : %08Lx%08Lx\n",
188 ah, al, bh, bl, ch, cl);
190 ah = (regs->regs[16]) >> 32;
191 al = (regs->regs[16]) & 0xffffffff;
192 bh = (regs->regs[17]) >> 32;
193 bl = (regs->regs[17]) & 0xffffffff;
194 ch = (regs->regs[19]) >> 32;
195 cl = (regs->regs[19]) & 0xffffffff;
196 printk("R16 : %08Lx%08Lx R17 : %08Lx%08Lx R19 : %08Lx%08Lx\n",
197 ah, al, bh, bl, ch, cl);
199 ah = (regs->regs[20]) >> 32;
200 al = (regs->regs[20]) & 0xffffffff;
201 bh = (regs->regs[21]) >> 32;
202 bl = (regs->regs[21]) & 0xffffffff;
203 ch = (regs->regs[22]) >> 32;
204 cl = (regs->regs[22]) & 0xffffffff;
205 printk("R20 : %08Lx%08Lx R21 : %08Lx%08Lx R22 : %08Lx%08Lx\n",
206 ah, al, bh, bl, ch, cl);
208 ah = (regs->regs[23]) >> 32;
209 al = (regs->regs[23]) & 0xffffffff;
210 bh = (regs->regs[24]) >> 32;
211 bl = (regs->regs[24]) & 0xffffffff;
212 ch = (regs->regs[25]) >> 32;
213 cl = (regs->regs[25]) & 0xffffffff;
214 printk("R23 : %08Lx%08Lx R24 : %08Lx%08Lx R25 : %08Lx%08Lx\n",
215 ah, al, bh, bl, ch, cl);
217 ah = (regs->regs[26]) >> 32;
218 al = (regs->regs[26]) & 0xffffffff;
219 bh = (regs->regs[27]) >> 32;
220 bl = (regs->regs[27]) & 0xffffffff;
221 ch = (regs->regs[28]) >> 32;
222 cl = (regs->regs[28]) & 0xffffffff;
223 printk("R26 : %08Lx%08Lx R27 : %08Lx%08Lx R28 : %08Lx%08Lx\n",
224 ah, al, bh, bl, ch, cl);
226 ah = (regs->regs[29]) >> 32;
227 al = (regs->regs[29]) & 0xffffffff;
228 bh = (regs->regs[30]) >> 32;
229 bl = (regs->regs[30]) & 0xffffffff;
230 ch = (regs->regs[31]) >> 32;
231 cl = (regs->regs[31]) & 0xffffffff;
232 printk("R29 : %08Lx%08Lx R30 : %08Lx%08Lx R31 : %08Lx%08Lx\n",
233 ah, al, bh, bl, ch, cl);
235 ah = (regs->regs[32]) >> 32;
236 al = (regs->regs[32]) & 0xffffffff;
237 bh = (regs->regs[33]) >> 32;
238 bl = (regs->regs[33]) & 0xffffffff;
239 ch = (regs->regs[34]) >> 32;
240 cl = (regs->regs[34]) & 0xffffffff;
241 printk("R32 : %08Lx%08Lx R33 : %08Lx%08Lx R34 : %08Lx%08Lx\n",
242 ah, al, bh, bl, ch, cl);
244 ah = (regs->regs[35]) >> 32;
245 al = (regs->regs[35]) & 0xffffffff;
246 bh = (regs->regs[36]) >> 32;
247 bl = (regs->regs[36]) & 0xffffffff;
248 ch = (regs->regs[37]) >> 32;
249 cl = (regs->regs[37]) & 0xffffffff;
250 printk("R35 : %08Lx%08Lx R36 : %08Lx%08Lx R37 : %08Lx%08Lx\n",
251 ah, al, bh, bl, ch, cl);
253 ah = (regs->regs[38]) >> 32;
254 al = (regs->regs[38]) & 0xffffffff;
255 bh = (regs->regs[39]) >> 32;
256 bl = (regs->regs[39]) & 0xffffffff;
257 ch = (regs->regs[40]) >> 32;
258 cl = (regs->regs[40]) & 0xffffffff;
259 printk("R38 : %08Lx%08Lx R39 : %08Lx%08Lx R40 : %08Lx%08Lx\n",
260 ah, al, bh, bl, ch, cl);
262 ah = (regs->regs[41]) >> 32;
263 al = (regs->regs[41]) & 0xffffffff;
264 bh = (regs->regs[42]) >> 32;
265 bl = (regs->regs[42]) & 0xffffffff;
266 ch = (regs->regs[43]) >> 32;
267 cl = (regs->regs[43]) & 0xffffffff;
268 printk("R41 : %08Lx%08Lx R42 : %08Lx%08Lx R43 : %08Lx%08Lx\n",
269 ah, al, bh, bl, ch, cl);
271 ah = (regs->regs[44]) >> 32;
272 al = (regs->regs[44]) & 0xffffffff;
273 bh = (regs->regs[45]) >> 32;
274 bl = (regs->regs[45]) & 0xffffffff;
275 ch = (regs->regs[46]) >> 32;
276 cl = (regs->regs[46]) & 0xffffffff;
277 printk("R44 : %08Lx%08Lx R45 : %08Lx%08Lx R46 : %08Lx%08Lx\n",
278 ah, al, bh, bl, ch, cl);
280 ah = (regs->regs[47]) >> 32;
281 al = (regs->regs[47]) & 0xffffffff;
282 bh = (regs->regs[48]) >> 32;
283 bl = (regs->regs[48]) & 0xffffffff;
284 ch = (regs->regs[49]) >> 32;
285 cl = (regs->regs[49]) & 0xffffffff;
286 printk("R47 : %08Lx%08Lx R48 : %08Lx%08Lx R49 : %08Lx%08Lx\n",
287 ah, al, bh, bl, ch, cl);
289 ah = (regs->regs[50]) >> 32;
290 al = (regs->regs[50]) & 0xffffffff;
291 bh = (regs->regs[51]) >> 32;
292 bl = (regs->regs[51]) & 0xffffffff;
293 ch = (regs->regs[52]) >> 32;
294 cl = (regs->regs[52]) & 0xffffffff;
295 printk("R50 : %08Lx%08Lx R51 : %08Lx%08Lx R52 : %08Lx%08Lx\n",
296 ah, al, bh, bl, ch, cl);
298 ah = (regs->regs[53]) >> 32;
299 al = (regs->regs[53]) & 0xffffffff;
300 bh = (regs->regs[54]) >> 32;
301 bl = (regs->regs[54]) & 0xffffffff;
302 ch = (regs->regs[55]) >> 32;
303 cl = (regs->regs[55]) & 0xffffffff;
304 printk("R53 : %08Lx%08Lx R54 : %08Lx%08Lx R55 : %08Lx%08Lx\n",
305 ah, al, bh, bl, ch, cl);
307 ah = (regs->regs[56]) >> 32;
308 al = (regs->regs[56]) & 0xffffffff;
309 bh = (regs->regs[57]) >> 32;
310 bl = (regs->regs[57]) & 0xffffffff;
311 ch = (regs->regs[58]) >> 32;
312 cl = (regs->regs[58]) & 0xffffffff;
313 printk("R56 : %08Lx%08Lx R57 : %08Lx%08Lx R58 : %08Lx%08Lx\n",
314 ah, al, bh, bl, ch, cl);
316 ah = (regs->regs[59]) >> 32;
317 al = (regs->regs[59]) & 0xffffffff;
318 bh = (regs->regs[60]) >> 32;
319 bl = (regs->regs[60]) & 0xffffffff;
320 ch = (regs->regs[61]) >> 32;
321 cl = (regs->regs[61]) & 0xffffffff;
322 printk("R59 : %08Lx%08Lx R60 : %08Lx%08Lx R61 : %08Lx%08Lx\n",
323 ah, al, bh, bl, ch, cl);
325 ah = (regs->regs[62]) >> 32;
326 al = (regs->regs[62]) & 0xffffffff;
327 bh = (regs->tregs[0]) >> 32;
328 bl = (regs->tregs[0]) & 0xffffffff;
329 ch = (regs->tregs[1]) >> 32;
330 cl = (regs->tregs[1]) & 0xffffffff;
331 printk("R62 : %08Lx%08Lx T0 : %08Lx%08Lx T1 : %08Lx%08Lx\n",
332 ah, al, bh, bl, ch, cl);
334 ah = (regs->tregs[2]) >> 32;
335 al = (regs->tregs[2]) & 0xffffffff;
336 bh = (regs->tregs[3]) >> 32;
337 bl = (regs->tregs[3]) & 0xffffffff;
338 ch = (regs->tregs[4]) >> 32;
339 cl = (regs->tregs[4]) & 0xffffffff;
340 printk("T2 : %08Lx%08Lx T3 : %08Lx%08Lx T4 : %08Lx%08Lx\n",
341 ah, al, bh, bl, ch, cl);
343 ah = (regs->tregs[5]) >> 32;
344 al = (regs->tregs[5]) & 0xffffffff;
345 bh = (regs->tregs[6]) >> 32;
346 bl = (regs->tregs[6]) & 0xffffffff;
347 ch = (regs->tregs[7]) >> 32;
348 cl = (regs->tregs[7]) & 0xffffffff;
349 printk("T5 : %08Lx%08Lx T6 : %08Lx%08Lx T7 : %08Lx%08Lx\n",
350 ah, al, bh, bl, ch, cl);
353 * If we're in kernel mode, dump the stack too..
355 if (!user_mode(regs)) {
356 void show_stack(struct task_struct *tsk, unsigned long *sp);
357 unsigned long sp = regs->regs[15] & 0xffffffff;
358 struct task_struct *tsk = get_current();
360 tsk->thread.kregs = regs;
362 show_stack(tsk, (unsigned long *)sp);
366 struct task_struct * alloc_task_struct(void)
368 /* Get task descriptor pages */
369 return (struct task_struct *)
370 __get_free_pages(GFP_KERNEL, get_order(THREAD_SIZE));
373 void free_task_struct(struct task_struct *p)
375 free_pages((unsigned long) p, get_order(THREAD_SIZE));
379 * Create a kernel thread
381 ATTRIB_NORET void kernel_thread_helper(void *arg, int (*fn)(void *))
387 * This is the mechanism for creating a new kernel thread.
389 * NOTE! Only a kernel-only process(ie the swapper or direct descendants
390 * who haven't done an "execve()") should use this: it will work within
391 * a system call from a "real" process, but the process memory space will
392 * not be freed until both the parent and the child have exited.
394 int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
398 memset(®s, 0, sizeof(regs));
399 regs.regs[2] = (unsigned long)arg;
400 regs.regs[3] = (unsigned long)fn;
402 regs.pc = (unsigned long)kernel_thread_helper;
405 return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0,
406 ®s, 0, NULL, NULL);
410 * Free current thread data structures etc..
412 void exit_thread(void)
414 /* See arch/sparc/kernel/process.c for the precedent for doing this -- RPC.
416 The SH-5 FPU save/restore approach relies on last_task_used_math
417 pointing to a live task_struct. When another task tries to use the
418 FPU for the 1st time, the FPUDIS trap handling (see
419 arch/sh64/kernel/fpu.c) will save the existing FPU state to the
420 FP regs field within last_task_used_math before re-loading the new
421 task's FPU state (or initialising it if the FPU has been used
422 before). So if last_task_used_math is stale, and its page has already been
423 re-allocated for another use, the consequences are rather grim. Unless we
424 null it here, there is no other path through which it would get safely
428 if (last_task_used_math == current) {
429 last_task_used_math = NULL;
434 void flush_thread(void)
437 /* Called by fs/exec.c (flush_old_exec) to remove traces of a
438 * previously running executable. */
440 if (last_task_used_math == current) {
441 last_task_used_math = NULL;
443 /* Force FPU state to be reinitialised after exec */
447 /* if we are a kernel thread, about to change to user thread,
450 if(current->thread.kregs==&fake_swapper_regs) {
451 current->thread.kregs =
452 ((struct pt_regs *)(THREAD_SIZE + (unsigned long) current) - 1);
453 current->thread.uregs = current->thread.kregs;
457 void release_thread(struct task_struct *dead_task)
462 /* Fill in the fpu structure for a core dump.. */
463 int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu)
467 struct task_struct *tsk = current;
469 fpvalid = !!tsk_used_math(tsk);
471 if (current == last_task_used_math) {
473 fpsave(&tsk->thread.fpu.hard);
475 last_task_used_math = 0;
479 memcpy(fpu, &tsk->thread.fpu.hard, sizeof(*fpu));
484 return 0; /* Task didn't use the fpu at all. */
488 asmlinkage void ret_from_fork(void);
490 int copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
491 unsigned long unused,
492 struct task_struct *p, struct pt_regs *regs)
494 struct pt_regs *childregs;
495 unsigned long long se; /* Sign extension */
498 if(last_task_used_math == current) {
500 fpsave(¤t->thread.fpu.hard);
502 last_task_used_math = NULL;
506 /* Copy from sh version */
507 childregs = (struct pt_regs *)(THREAD_SIZE + task_stack_page(p)) - 1;
511 if (user_mode(regs)) {
512 childregs->regs[15] = usp;
513 p->thread.uregs = childregs;
515 childregs->regs[15] = (unsigned long)task_stack_page(p) + THREAD_SIZE;
518 childregs->regs[9] = 0; /* Set return value for child */
519 childregs->sr |= SR_FD; /* Invalidate FPU flag */
521 p->thread.sp = (unsigned long) childregs;
522 p->thread.pc = (unsigned long) ret_from_fork;
525 * Sign extend the edited stack.
526 * Note that thread.pc and thread.pc will stay
527 * 32-bit wide and context switch must take care
528 * of NEFF sign extension.
531 se = childregs->regs[15];
532 se = (se & NEFF_SIGN) ? (se | NEFF_MASK) : se;
533 childregs->regs[15] = se;
538 asmlinkage int sys_fork(unsigned long r2, unsigned long r3,
539 unsigned long r4, unsigned long r5,
540 unsigned long r6, unsigned long r7,
541 struct pt_regs *pregs)
543 return do_fork(SIGCHLD, pregs->regs[15], pregs, 0, 0, 0);
546 asmlinkage int sys_clone(unsigned long clone_flags, unsigned long newsp,
547 unsigned long r4, unsigned long r5,
548 unsigned long r6, unsigned long r7,
549 struct pt_regs *pregs)
552 newsp = pregs->regs[15];
553 return do_fork(clone_flags, newsp, pregs, 0, 0, 0);
557 * This is trivial, and on the face of it looks like it
558 * could equally well be done in user mode.
560 * Not so, for quite unobvious reasons - register pressure.
561 * In user mode vfork() cannot have a stack frame, and if
562 * done by calling the "clone()" system call directly, you
563 * do not have enough call-clobbered registers to hold all
564 * the information you need.
566 asmlinkage int sys_vfork(unsigned long r2, unsigned long r3,
567 unsigned long r4, unsigned long r5,
568 unsigned long r6, unsigned long r7,
569 struct pt_regs *pregs)
571 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, pregs->regs[15], pregs, 0, 0, 0);
575 * sys_execve() executes a new program.
577 asmlinkage int sys_execve(char *ufilename, char **uargv,
578 char **uenvp, unsigned long r5,
579 unsigned long r6, unsigned long r7,
580 struct pt_regs *pregs)
586 filename = getname((char __user *)ufilename);
587 error = PTR_ERR(filename);
588 if (IS_ERR(filename))
591 error = do_execve(filename,
592 (char __user * __user *)uargv,
593 (char __user * __user *)uenvp,
597 current->ptrace &= ~PT_DTRACE;
598 task_unlock(current);
607 * These bracket the sleeping functions..
609 extern void interruptible_sleep_on(wait_queue_head_t *q);
611 #define mid_sched ((unsigned long) interruptible_sleep_on)
613 static int in_sh64_switch_to(unsigned long pc)
615 extern char __sh64_switch_to_end;
616 /* For a sleeping task, the PC is somewhere in the middle of the function,
617 so we don't have to worry about masking the LSB off */
618 return (pc >= (unsigned long) sh64_switch_to) &&
619 (pc < (unsigned long) &__sh64_switch_to_end);
622 unsigned long get_wchan(struct task_struct *p)
624 unsigned long schedule_fp;
625 unsigned long sh64_switch_to_fp;
626 unsigned long schedule_caller_pc;
629 if (!p || p == current || p->state == TASK_RUNNING)
633 * The same comment as on the Alpha applies here, too ...
635 pc = thread_saved_pc(p);
637 #ifdef CONFIG_FRAME_POINTER
638 if (in_sh64_switch_to(pc)) {
639 sh64_switch_to_fp = (long) p->thread.sp;
640 /* r14 is saved at offset 4 in the sh64_switch_to frame */
641 schedule_fp = *(unsigned long *) (long)(sh64_switch_to_fp + 4);
643 /* and the caller of 'schedule' is (currently!) saved at offset 24
644 in the frame of schedule (from disasm) */
645 schedule_caller_pc = *(unsigned long *) (long)(schedule_fp + 24);
646 return schedule_caller_pc;
652 /* Provide a /proc/asids file that lists out the
653 ASIDs currently associated with the processes. (If the DM.PC register is
654 examined through the debug link, this shows ASID + PC. To make use of this,
655 the PID->ASID relationship needs to be known. This is primarily for
659 #if defined(CONFIG_SH64_PROC_ASIDS)
661 asids_proc_info(char *buf, char **start, off_t fpos, int length, int *eof, void *data)
664 struct task_struct *p;
665 read_lock(&tasklist_lock);
666 for_each_process(p) {
668 struct mm_struct *mm;
672 unsigned long asid, context;
673 context = mm->context;
674 asid = (context & 0xff);
675 len += sprintf(buf+len, "%5d : %02lx\n", pid, asid);
677 len += sprintf(buf+len, "%5d : (none)\n", pid);
680 read_unlock(&tasklist_lock);
685 static int __init register_proc_asids(void)
687 create_proc_read_entry("asids", 0, NULL, asids_proc_info, NULL);
690 __initcall(register_proc_asids);