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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
David Howells | 84e8cd6 | 2006-07-10 04:44:55 -0700 | [diff] [blame] | 29 | #include <asm/asm-offsets.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <asm/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <asm/setup.h> |
| 32 | #include <asm/pgtable.h> |
Christoph Lameter | 8defab3 | 2007-05-09 02:32:48 -0700 | [diff] [blame] | 33 | #include <asm/tlb.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include <asm/gdb-stub.h> |
| 35 | #include <asm/mb-regs.h> |
| 36 | |
| 37 | #include "local.h" |
| 38 | |
| 39 | asmlinkage void ret_from_fork(void); |
Al Viro | 02ce496 | 2012-09-18 22:18:51 -0400 | [diff] [blame^] | 40 | asmlinkage void ret_from_kernel_thread(void); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
| 42 | #include <asm/pgalloc.h> |
| 43 | |
David Howells | 9dec17e | 2006-07-10 04:44:51 -0700 | [diff] [blame] | 44 | void (*pm_power_off)(void); |
| 45 | EXPORT_SYMBOL(pm_power_off); |
| 46 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | static void core_sleep_idle(void) |
| 48 | { |
| 49 | #ifdef LED_DEBUG_SLEEP |
| 50 | /* Show that we're sleeping... */ |
| 51 | __set_LEDS(0x55aa); |
| 52 | #endif |
| 53 | frv_cpu_core_sleep(); |
| 54 | #ifdef LED_DEBUG_SLEEP |
| 55 | /* ... and that we woke up */ |
| 56 | __set_LEDS(0); |
| 57 | #endif |
| 58 | mb(); |
| 59 | } |
| 60 | |
| 61 | void (*idle)(void) = core_sleep_idle; |
| 62 | |
| 63 | /* |
| 64 | * The idle thread. There's no useful work to be |
| 65 | * done, so just try to conserve power and have a |
| 66 | * low exit latency (ie sit in a loop waiting for |
| 67 | * somebody to say that they'd like to reschedule) |
| 68 | */ |
| 69 | void cpu_idle(void) |
| 70 | { |
| 71 | /* endless idle loop with no priority at all */ |
| 72 | while (1) { |
| 73 | while (!need_resched()) { |
Christoph Lameter | 8defab3 | 2007-05-09 02:32:48 -0700 | [diff] [blame] | 74 | check_pgt_cache(); |
| 75 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | if (!frv_dma_inprogress && idle) |
| 77 | idle(); |
| 78 | } |
| 79 | |
Thomas Gleixner | bd2f553 | 2011-03-21 12:33:18 +0100 | [diff] [blame] | 80 | schedule_preempt_disabled(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
| 84 | void machine_restart(char * __unused) |
| 85 | { |
| 86 | unsigned long reset_addr; |
| 87 | #ifdef CONFIG_GDBSTUB |
| 88 | gdbstub_exit(0); |
| 89 | #endif |
| 90 | |
| 91 | if (PSR_IMPLE(__get_PSR()) == PSR_IMPLE_FR551) |
| 92 | reset_addr = 0xfefff500; |
| 93 | else |
| 94 | reset_addr = 0xfeff0500; |
| 95 | |
| 96 | /* Software reset. */ |
| 97 | asm volatile(" dcef @(gr0,gr0),1 ! membar !" |
| 98 | " sti %1,@(%0,0) !" |
| 99 | " nop ! nop ! nop ! nop ! nop ! " |
| 100 | " nop ! nop ! nop ! nop ! nop ! " |
| 101 | " nop ! nop ! nop ! nop ! nop ! " |
| 102 | " nop ! nop ! nop ! nop ! nop ! " |
| 103 | : : "r" (reset_addr), "r" (1) ); |
| 104 | |
| 105 | for (;;) |
| 106 | ; |
| 107 | } |
| 108 | |
| 109 | void machine_halt(void) |
| 110 | { |
| 111 | #ifdef CONFIG_GDBSTUB |
| 112 | gdbstub_exit(0); |
| 113 | #endif |
| 114 | |
| 115 | for (;;); |
| 116 | } |
| 117 | |
| 118 | void machine_power_off(void) |
| 119 | { |
| 120 | #ifdef CONFIG_GDBSTUB |
| 121 | gdbstub_exit(0); |
| 122 | #endif |
| 123 | |
| 124 | for (;;); |
| 125 | } |
| 126 | |
| 127 | void flush_thread(void) |
| 128 | { |
Mathias Krause | adc400f | 2011-07-26 16:08:15 -0700 | [diff] [blame] | 129 | /* nothing */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | inline unsigned long user_stack(const struct pt_regs *regs) |
| 133 | { |
| 134 | while (regs->next_frame) |
| 135 | regs = regs->next_frame; |
| 136 | return user_mode(regs) ? regs->sp : 0; |
| 137 | } |
| 138 | |
| 139 | asmlinkage int sys_fork(void) |
| 140 | { |
| 141 | #ifndef CONFIG_MMU |
| 142 | /* fork almost works, enough to trick you into looking elsewhere:-( */ |
| 143 | return -EINVAL; |
| 144 | #else |
| 145 | return do_fork(SIGCHLD, user_stack(__frame), __frame, 0, NULL, NULL); |
| 146 | #endif |
| 147 | } |
| 148 | |
| 149 | asmlinkage int sys_vfork(void) |
| 150 | { |
| 151 | return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, user_stack(__frame), __frame, 0, |
| 152 | NULL, NULL); |
| 153 | } |
| 154 | |
| 155 | /*****************************************************************************/ |
| 156 | /* |
| 157 | * clone a process |
| 158 | * - tlsptr is retrieved by copy_thread() |
| 159 | */ |
| 160 | asmlinkage int sys_clone(unsigned long clone_flags, unsigned long newsp, |
| 161 | int __user *parent_tidptr, int __user *child_tidptr, |
| 162 | int __user *tlsptr) |
| 163 | { |
| 164 | if (!newsp) |
| 165 | newsp = user_stack(__frame); |
| 166 | return do_fork(clone_flags, newsp, __frame, 0, parent_tidptr, child_tidptr); |
| 167 | } /* end sys_clone() */ |
| 168 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | /* |
| 170 | * set up the kernel stack and exception frames for a new process |
| 171 | */ |
Alexey Dobriyan | 6f2c55b | 2009-04-02 16:56:59 -0700 | [diff] [blame] | 172 | int copy_thread(unsigned long clone_flags, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | unsigned long usp, unsigned long topstk, |
| 174 | struct task_struct *p, struct pt_regs *regs) |
| 175 | { |
Al Viro | 02ce496 | 2012-09-18 22:18:51 -0400 | [diff] [blame^] | 176 | struct pt_regs *childregs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | |
Al Viro | 02ce496 | 2012-09-18 22:18:51 -0400 | [diff] [blame^] | 178 | childregs = (struct pt_regs *) |
David Howells | 84e8cd6 | 2006-07-10 04:44:55 -0700 | [diff] [blame] | 179 | (task_stack_page(p) + THREAD_SIZE - FRV_FRAME0_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | |
| 181 | /* set up the userspace frame (the only place that the USP is stored) */ |
Al Viro | 02ce496 | 2012-09-18 22:18:51 -0400 | [diff] [blame^] | 182 | *childregs = *regs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | |
Al Viro | 02ce496 | 2012-09-18 22:18:51 -0400 | [diff] [blame^] | 184 | childregs->sp = usp; |
| 185 | childregs->next_frame = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | |
Al Viro | 02ce496 | 2012-09-18 22:18:51 -0400 | [diff] [blame^] | 187 | if (unlikely(!user_mode(regs))) |
| 188 | p->thread.pc = (unsigned long) ret_from_kernel_thread; |
| 189 | else |
| 190 | p->thread.pc = (unsigned long) ret_from_fork; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | |
| 192 | p->set_child_tid = p->clear_child_tid = NULL; |
| 193 | |
| 194 | p->thread.frame = childregs; |
| 195 | p->thread.curr = p; |
| 196 | p->thread.sp = (unsigned long) childregs; |
| 197 | p->thread.fp = 0; |
| 198 | p->thread.lr = 0; |
Al Viro | 02ce496 | 2012-09-18 22:18:51 -0400 | [diff] [blame^] | 199 | p->thread.frame0 = childregs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | |
| 201 | /* the new TLS pointer is passed in as arg #5 to sys_clone() */ |
| 202 | if (clone_flags & CLONE_SETTLS) |
| 203 | childregs->gr29 = childregs->gr12; |
| 204 | |
| 205 | save_user_regs(p->thread.user); |
| 206 | |
| 207 | return 0; |
| 208 | } /* end copy_thread() */ |
| 209 | |
| 210 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | * sys_execve() executes a new program. |
| 212 | */ |
David Howells | d762746 | 2010-08-17 23:52:56 +0100 | [diff] [blame] | 213 | asmlinkage int sys_execve(const char __user *name, |
| 214 | const char __user *const __user *argv, |
| 215 | const char __user *const __user *envp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | { |
| 217 | int error; |
| 218 | char * filename; |
| 219 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | filename = getname(name); |
| 221 | error = PTR_ERR(filename); |
| 222 | if (IS_ERR(filename)) |
John Kacur | b69975a | 2009-10-12 22:53:25 +0200 | [diff] [blame] | 223 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | error = do_execve(filename, argv, envp, __frame); |
| 225 | putname(filename); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | return error; |
| 227 | } |
| 228 | |
| 229 | unsigned long get_wchan(struct task_struct *p) |
| 230 | { |
| 231 | struct pt_regs *regs0; |
| 232 | unsigned long fp, pc; |
| 233 | unsigned long stack_limit; |
| 234 | int count = 0; |
| 235 | if (!p || p == current || p->state == TASK_RUNNING) |
| 236 | return 0; |
| 237 | |
| 238 | stack_limit = (unsigned long) (p + 1); |
| 239 | fp = p->thread.fp; |
| 240 | regs0 = p->thread.frame0; |
| 241 | |
| 242 | do { |
| 243 | if (fp < stack_limit || fp >= (unsigned long) regs0 || fp & 3) |
| 244 | return 0; |
| 245 | |
| 246 | pc = ((unsigned long *) fp)[2]; |
| 247 | |
| 248 | /* FIXME: This depends on the order of these functions. */ |
| 249 | if (!in_sched_functions(pc)) |
| 250 | return pc; |
| 251 | |
| 252 | fp = *(unsigned long *) fp; |
| 253 | } while (count++ < 16); |
| 254 | |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | unsigned long thread_saved_pc(struct task_struct *tsk) |
| 259 | { |
| 260 | /* Check whether the thread is blocked in resume() */ |
| 261 | if (in_sched_functions(tsk->thread.pc)) |
| 262 | return ((unsigned long *)tsk->thread.fp)[2]; |
| 263 | else |
| 264 | return tsk->thread.pc; |
| 265 | } |
| 266 | |
| 267 | int elf_check_arch(const struct elf32_hdr *hdr) |
| 268 | { |
| 269 | unsigned long hsr0 = __get_HSR(0); |
| 270 | unsigned long psr = __get_PSR(); |
| 271 | |
| 272 | if (hdr->e_machine != EM_FRV) |
| 273 | return 0; |
| 274 | |
| 275 | switch (hdr->e_flags & EF_FRV_GPR_MASK) { |
| 276 | case EF_FRV_GPR64: |
| 277 | if ((hsr0 & HSR0_GRN) == HSR0_GRN_32) |
| 278 | return 0; |
| 279 | case EF_FRV_GPR32: |
| 280 | case 0: |
| 281 | break; |
| 282 | default: |
| 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | switch (hdr->e_flags & EF_FRV_FPR_MASK) { |
| 287 | case EF_FRV_FPR64: |
| 288 | if ((hsr0 & HSR0_FRN) == HSR0_FRN_32) |
| 289 | return 0; |
| 290 | case EF_FRV_FPR32: |
| 291 | case EF_FRV_FPR_NONE: |
| 292 | case 0: |
| 293 | break; |
| 294 | default: |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | if ((hdr->e_flags & EF_FRV_MULADD) == EF_FRV_MULADD) |
| 299 | if (PSR_IMPLE(psr) != PSR_IMPLE_FR405 && |
| 300 | PSR_IMPLE(psr) != PSR_IMPLE_FR451) |
| 301 | return 0; |
| 302 | |
| 303 | switch (hdr->e_flags & EF_FRV_CPU_MASK) { |
| 304 | case EF_FRV_CPU_GENERIC: |
| 305 | break; |
| 306 | case EF_FRV_CPU_FR300: |
| 307 | case EF_FRV_CPU_SIMPLE: |
| 308 | case EF_FRV_CPU_TOMCAT: |
| 309 | default: |
| 310 | return 0; |
| 311 | case EF_FRV_CPU_FR400: |
| 312 | if (PSR_IMPLE(psr) != PSR_IMPLE_FR401 && |
| 313 | PSR_IMPLE(psr) != PSR_IMPLE_FR405 && |
| 314 | PSR_IMPLE(psr) != PSR_IMPLE_FR451 && |
| 315 | PSR_IMPLE(psr) != PSR_IMPLE_FR551) |
| 316 | return 0; |
| 317 | break; |
| 318 | case EF_FRV_CPU_FR450: |
| 319 | if (PSR_IMPLE(psr) != PSR_IMPLE_FR451) |
| 320 | return 0; |
| 321 | break; |
| 322 | case EF_FRV_CPU_FR500: |
| 323 | if (PSR_IMPLE(psr) != PSR_IMPLE_FR501) |
| 324 | return 0; |
| 325 | break; |
| 326 | case EF_FRV_CPU_FR550: |
| 327 | if (PSR_IMPLE(psr) != PSR_IMPLE_FR551) |
| 328 | return 0; |
| 329 | break; |
| 330 | } |
| 331 | |
| 332 | return 1; |
| 333 | } |
David Howells | 6d8c4e3 | 2006-07-10 04:44:55 -0700 | [diff] [blame] | 334 | |
| 335 | int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpregs) |
| 336 | { |
| 337 | memcpy(fpregs, |
| 338 | ¤t->thread.user->f, |
| 339 | sizeof(current->thread.user->f)); |
| 340 | return 1; |
| 341 | } |
Al Viro | 02ce496 | 2012-09-18 22:18:51 -0400 | [diff] [blame^] | 342 | |
| 343 | int kernel_thread(int (*fn)(void *), void *arg, unsigned long flags) |
| 344 | { |
| 345 | struct pt_regs regs = { |
| 346 | .gr8 = (unsigned long)arg; |
| 347 | .gr9 = (unsigned long)fn; |
| 348 | .psr = PSR_S; |
| 349 | }; |
| 350 | return do_fork(flags|CLONE_VM|CLONE_UNTRACED, 0, ®s, 0, NULL, NULL); |
| 351 | } |