Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* process.c: FRV specific parts of process handling |
| 2 | * |
| 3 | * Copyright (C) 2003-5 Red Hat, Inc. All Rights Reserved. |
| 4 | * Written by David Howells (dhowells@redhat.com) |
| 5 | * - Derived from arch/m68k/kernel/process.c |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version |
| 10 | * 2 of the License, or (at your option) any later version. |
| 11 | */ |
| 12 | |
David Howells | 9dec17e | 2006-07-10 04:44:51 -0700 | [diff] [blame] | 13 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/errno.h> |
| 15 | #include <linux/sched.h> |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/mm.h> |
| 18 | #include <linux/smp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/stddef.h> |
| 20 | #include <linux/unistd.h> |
| 21 | #include <linux/ptrace.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/user.h> |
| 24 | #include <linux/elf.h> |
| 25 | #include <linux/reboot.h> |
| 26 | #include <linux/interrupt.h> |
Christoph Lameter | 8defab3 | 2007-05-09 02:32:48 -0700 | [diff] [blame] | 27 | #include <linux/pagemap.h> |
Frederic Weisbecker | 41d8fe5 | 2012-08-22 17:27:34 +0200 | [diff] [blame] | 28 | #include <linux/rcupdate.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
David Howells | 84e8cd6 | 2006-07-10 04:44:55 -0700 | [diff] [blame] | 30 | #include <asm/asm-offsets.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <asm/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <asm/setup.h> |
| 33 | #include <asm/pgtable.h> |
Christoph Lameter | 8defab3 | 2007-05-09 02:32:48 -0700 | [diff] [blame] | 34 | #include <asm/tlb.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <asm/gdb-stub.h> |
| 36 | #include <asm/mb-regs.h> |
| 37 | |
| 38 | #include "local.h" |
| 39 | |
| 40 | asmlinkage void ret_from_fork(void); |
Al Viro | 02ce496 | 2012-09-18 22:18:51 -0400 | [diff] [blame] | 41 | asmlinkage void ret_from_kernel_thread(void); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
| 43 | #include <asm/pgalloc.h> |
| 44 | |
David Howells | 9dec17e | 2006-07-10 04:44:51 -0700 | [diff] [blame] | 45 | void (*pm_power_off)(void); |
| 46 | EXPORT_SYMBOL(pm_power_off); |
| 47 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | static void core_sleep_idle(void) |
| 49 | { |
| 50 | #ifdef LED_DEBUG_SLEEP |
| 51 | /* Show that we're sleeping... */ |
| 52 | __set_LEDS(0x55aa); |
| 53 | #endif |
| 54 | frv_cpu_core_sleep(); |
| 55 | #ifdef LED_DEBUG_SLEEP |
| 56 | /* ... and that we woke up */ |
| 57 | __set_LEDS(0); |
| 58 | #endif |
| 59 | mb(); |
| 60 | } |
| 61 | |
Thomas Gleixner | ccf8e78 | 2013-03-21 22:49:44 +0100 | [diff] [blame] | 62 | void arch_cpu_idle(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | { |
Thomas Gleixner | ccf8e78 | 2013-03-21 22:49:44 +0100 | [diff] [blame] | 64 | if (!frv_dma_inprogress) |
| 65 | core_sleep_idle(); |
| 66 | else |
| 67 | local_irq_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void machine_restart(char * __unused) |
| 71 | { |
| 72 | unsigned long reset_addr; |
| 73 | #ifdef CONFIG_GDBSTUB |
| 74 | gdbstub_exit(0); |
| 75 | #endif |
| 76 | |
| 77 | if (PSR_IMPLE(__get_PSR()) == PSR_IMPLE_FR551) |
| 78 | reset_addr = 0xfefff500; |
| 79 | else |
| 80 | reset_addr = 0xfeff0500; |
| 81 | |
| 82 | /* Software reset. */ |
| 83 | asm volatile(" dcef @(gr0,gr0),1 ! membar !" |
| 84 | " sti %1,@(%0,0) !" |
| 85 | " nop ! nop ! nop ! nop ! nop ! " |
| 86 | " nop ! nop ! nop ! nop ! nop ! " |
| 87 | " nop ! nop ! nop ! nop ! nop ! " |
| 88 | " nop ! nop ! nop ! nop ! nop ! " |
| 89 | : : "r" (reset_addr), "r" (1) ); |
| 90 | |
| 91 | for (;;) |
| 92 | ; |
| 93 | } |
| 94 | |
| 95 | void machine_halt(void) |
| 96 | { |
| 97 | #ifdef CONFIG_GDBSTUB |
| 98 | gdbstub_exit(0); |
| 99 | #endif |
| 100 | |
| 101 | for (;;); |
| 102 | } |
| 103 | |
| 104 | void machine_power_off(void) |
| 105 | { |
| 106 | #ifdef CONFIG_GDBSTUB |
| 107 | gdbstub_exit(0); |
| 108 | #endif |
| 109 | |
| 110 | for (;;); |
| 111 | } |
| 112 | |
| 113 | void flush_thread(void) |
| 114 | { |
Mathias Krause | adc400f | 2011-07-26 16:08:15 -0700 | [diff] [blame] | 115 | /* nothing */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | inline unsigned long user_stack(const struct pt_regs *regs) |
| 119 | { |
| 120 | while (regs->next_frame) |
| 121 | regs = regs->next_frame; |
| 122 | return user_mode(regs) ? regs->sp : 0; |
| 123 | } |
| 124 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | /* |
| 126 | * set up the kernel stack and exception frames for a new process |
| 127 | */ |
Alexey Dobriyan | 6f2c55b | 2009-04-02 16:56:59 -0700 | [diff] [blame] | 128 | int copy_thread(unsigned long clone_flags, |
Al Viro | 49ed339 | 2012-09-22 18:10:15 -0400 | [diff] [blame] | 129 | unsigned long usp, unsigned long arg, |
Al Viro | afa86fc | 2012-10-22 22:51:14 -0400 | [diff] [blame] | 130 | struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | { |
Al Viro | 02ce496 | 2012-09-18 22:18:51 -0400 | [diff] [blame] | 132 | struct pt_regs *childregs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | |
Al Viro | 02ce496 | 2012-09-18 22:18:51 -0400 | [diff] [blame] | 134 | childregs = (struct pt_regs *) |
David Howells | 84e8cd6 | 2006-07-10 04:44:55 -0700 | [diff] [blame] | 135 | (task_stack_page(p) + THREAD_SIZE - FRV_FRAME0_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
David Howells | e7aa51b | 2012-11-02 13:20:43 +0000 | [diff] [blame] | 137 | /* set up the userspace frame (the only place that the USP is stored) */ |
Al Viro | b9763ce | 2012-10-21 16:40:35 -0400 | [diff] [blame] | 138 | *childregs = *current_pt_regs(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
| 140 | p->thread.frame = childregs; |
| 141 | p->thread.curr = p; |
| 142 | p->thread.sp = (unsigned long) childregs; |
| 143 | p->thread.fp = 0; |
| 144 | p->thread.lr = 0; |
Al Viro | 02ce496 | 2012-09-18 22:18:51 -0400 | [diff] [blame] | 145 | p->thread.frame0 = childregs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
Al Viro | b9763ce | 2012-10-21 16:40:35 -0400 | [diff] [blame] | 147 | if (unlikely(p->flags & PF_KTHREAD)) { |
Al Viro | 49ed339 | 2012-09-22 18:10:15 -0400 | [diff] [blame] | 148 | childregs->gr9 = usp; /* function */ |
| 149 | childregs->gr8 = arg; |
Al Viro | 49ed339 | 2012-09-22 18:10:15 -0400 | [diff] [blame] | 150 | p->thread.pc = (unsigned long) ret_from_kernel_thread; |
| 151 | save_user_regs(p->thread.user); |
| 152 | return 0; |
| 153 | } |
Al Viro | b9763ce | 2012-10-21 16:40:35 -0400 | [diff] [blame] | 154 | if (usp) |
| 155 | childregs->sp = usp; |
Al Viro | 49ed339 | 2012-09-22 18:10:15 -0400 | [diff] [blame] | 156 | childregs->next_frame = NULL; |
| 157 | |
| 158 | p->thread.pc = (unsigned long) ret_from_fork; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | |
| 160 | /* the new TLS pointer is passed in as arg #5 to sys_clone() */ |
| 161 | if (clone_flags & CLONE_SETTLS) |
| 162 | childregs->gr29 = childregs->gr12; |
| 163 | |
| 164 | save_user_regs(p->thread.user); |
| 165 | |
| 166 | return 0; |
| 167 | } /* end copy_thread() */ |
| 168 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | unsigned long get_wchan(struct task_struct *p) |
| 170 | { |
| 171 | struct pt_regs *regs0; |
| 172 | unsigned long fp, pc; |
| 173 | unsigned long stack_limit; |
| 174 | int count = 0; |
| 175 | if (!p || p == current || p->state == TASK_RUNNING) |
| 176 | return 0; |
| 177 | |
| 178 | stack_limit = (unsigned long) (p + 1); |
| 179 | fp = p->thread.fp; |
| 180 | regs0 = p->thread.frame0; |
| 181 | |
| 182 | do { |
| 183 | if (fp < stack_limit || fp >= (unsigned long) regs0 || fp & 3) |
| 184 | return 0; |
| 185 | |
| 186 | pc = ((unsigned long *) fp)[2]; |
| 187 | |
| 188 | /* FIXME: This depends on the order of these functions. */ |
| 189 | if (!in_sched_functions(pc)) |
| 190 | return pc; |
| 191 | |
| 192 | fp = *(unsigned long *) fp; |
| 193 | } while (count++ < 16); |
| 194 | |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | unsigned long thread_saved_pc(struct task_struct *tsk) |
| 199 | { |
| 200 | /* Check whether the thread is blocked in resume() */ |
| 201 | if (in_sched_functions(tsk->thread.pc)) |
| 202 | return ((unsigned long *)tsk->thread.fp)[2]; |
| 203 | else |
| 204 | return tsk->thread.pc; |
| 205 | } |
| 206 | |
| 207 | int elf_check_arch(const struct elf32_hdr *hdr) |
| 208 | { |
| 209 | unsigned long hsr0 = __get_HSR(0); |
| 210 | unsigned long psr = __get_PSR(); |
| 211 | |
| 212 | if (hdr->e_machine != EM_FRV) |
| 213 | return 0; |
| 214 | |
| 215 | switch (hdr->e_flags & EF_FRV_GPR_MASK) { |
| 216 | case EF_FRV_GPR64: |
| 217 | if ((hsr0 & HSR0_GRN) == HSR0_GRN_32) |
| 218 | return 0; |
| 219 | case EF_FRV_GPR32: |
| 220 | case 0: |
| 221 | break; |
| 222 | default: |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | switch (hdr->e_flags & EF_FRV_FPR_MASK) { |
| 227 | case EF_FRV_FPR64: |
| 228 | if ((hsr0 & HSR0_FRN) == HSR0_FRN_32) |
| 229 | return 0; |
| 230 | case EF_FRV_FPR32: |
| 231 | case EF_FRV_FPR_NONE: |
| 232 | case 0: |
| 233 | break; |
| 234 | default: |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | if ((hdr->e_flags & EF_FRV_MULADD) == EF_FRV_MULADD) |
| 239 | if (PSR_IMPLE(psr) != PSR_IMPLE_FR405 && |
| 240 | PSR_IMPLE(psr) != PSR_IMPLE_FR451) |
| 241 | return 0; |
| 242 | |
| 243 | switch (hdr->e_flags & EF_FRV_CPU_MASK) { |
| 244 | case EF_FRV_CPU_GENERIC: |
| 245 | break; |
| 246 | case EF_FRV_CPU_FR300: |
| 247 | case EF_FRV_CPU_SIMPLE: |
| 248 | case EF_FRV_CPU_TOMCAT: |
| 249 | default: |
| 250 | return 0; |
| 251 | case EF_FRV_CPU_FR400: |
| 252 | if (PSR_IMPLE(psr) != PSR_IMPLE_FR401 && |
| 253 | PSR_IMPLE(psr) != PSR_IMPLE_FR405 && |
| 254 | PSR_IMPLE(psr) != PSR_IMPLE_FR451 && |
| 255 | PSR_IMPLE(psr) != PSR_IMPLE_FR551) |
| 256 | return 0; |
| 257 | break; |
| 258 | case EF_FRV_CPU_FR450: |
| 259 | if (PSR_IMPLE(psr) != PSR_IMPLE_FR451) |
| 260 | return 0; |
| 261 | break; |
| 262 | case EF_FRV_CPU_FR500: |
| 263 | if (PSR_IMPLE(psr) != PSR_IMPLE_FR501) |
| 264 | return 0; |
| 265 | break; |
| 266 | case EF_FRV_CPU_FR550: |
| 267 | if (PSR_IMPLE(psr) != PSR_IMPLE_FR551) |
| 268 | return 0; |
| 269 | break; |
| 270 | } |
| 271 | |
| 272 | return 1; |
| 273 | } |
David Howells | 6d8c4e3 | 2006-07-10 04:44:55 -0700 | [diff] [blame] | 274 | |
| 275 | int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpregs) |
| 276 | { |
| 277 | memcpy(fpregs, |
| 278 | ¤t->thread.user->f, |
| 279 | sizeof(current->thread.user->f)); |
| 280 | return 1; |
| 281 | } |