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