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 | |
| 13 | #include <linux/config.h> |
| 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> |
| 19 | #include <linux/smp_lock.h> |
| 20 | #include <linux/stddef.h> |
| 21 | #include <linux/unistd.h> |
| 22 | #include <linux/ptrace.h> |
| 23 | #include <linux/slab.h> |
| 24 | #include <linux/user.h> |
| 25 | #include <linux/elf.h> |
| 26 | #include <linux/reboot.h> |
| 27 | #include <linux/interrupt.h> |
| 28 | |
| 29 | #include <asm/uaccess.h> |
| 30 | #include <asm/system.h> |
| 31 | #include <asm/setup.h> |
| 32 | #include <asm/pgtable.h> |
| 33 | #include <asm/gdb-stub.h> |
| 34 | #include <asm/mb-regs.h> |
| 35 | |
| 36 | #include "local.h" |
| 37 | |
| 38 | asmlinkage void ret_from_fork(void); |
| 39 | |
| 40 | #include <asm/pgalloc.h> |
| 41 | |
| 42 | struct task_struct *alloc_task_struct(void) |
| 43 | { |
| 44 | struct task_struct *p = kmalloc(THREAD_SIZE, GFP_KERNEL); |
| 45 | if (p) |
| 46 | atomic_set((atomic_t *)(p+1), 1); |
| 47 | return p; |
| 48 | } |
| 49 | |
| 50 | void free_task_struct(struct task_struct *p) |
| 51 | { |
| 52 | if (atomic_dec_and_test((atomic_t *)(p+1))) |
| 53 | kfree(p); |
| 54 | } |
| 55 | |
| 56 | static void core_sleep_idle(void) |
| 57 | { |
| 58 | #ifdef LED_DEBUG_SLEEP |
| 59 | /* Show that we're sleeping... */ |
| 60 | __set_LEDS(0x55aa); |
| 61 | #endif |
| 62 | frv_cpu_core_sleep(); |
| 63 | #ifdef LED_DEBUG_SLEEP |
| 64 | /* ... and that we woke up */ |
| 65 | __set_LEDS(0); |
| 66 | #endif |
| 67 | mb(); |
| 68 | } |
| 69 | |
| 70 | void (*idle)(void) = core_sleep_idle; |
| 71 | |
| 72 | /* |
| 73 | * The idle thread. There's no useful work to be |
| 74 | * done, so just try to conserve power and have a |
| 75 | * low exit latency (ie sit in a loop waiting for |
| 76 | * somebody to say that they'd like to reschedule) |
| 77 | */ |
| 78 | void cpu_idle(void) |
| 79 | { |
| 80 | /* endless idle loop with no priority at all */ |
| 81 | while (1) { |
| 82 | while (!need_resched()) { |
| 83 | irq_stat[smp_processor_id()].idle_timestamp = jiffies; |
| 84 | |
| 85 | if (!frv_dma_inprogress && idle) |
| 86 | idle(); |
| 87 | } |
| 88 | |
| 89 | schedule(); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | void machine_restart(char * __unused) |
| 94 | { |
| 95 | unsigned long reset_addr; |
| 96 | #ifdef CONFIG_GDBSTUB |
| 97 | gdbstub_exit(0); |
| 98 | #endif |
| 99 | |
| 100 | if (PSR_IMPLE(__get_PSR()) == PSR_IMPLE_FR551) |
| 101 | reset_addr = 0xfefff500; |
| 102 | else |
| 103 | reset_addr = 0xfeff0500; |
| 104 | |
| 105 | /* Software reset. */ |
| 106 | asm volatile(" dcef @(gr0,gr0),1 ! membar !" |
| 107 | " sti %1,@(%0,0) !" |
| 108 | " nop ! nop ! nop ! nop ! nop ! " |
| 109 | " nop ! nop ! nop ! nop ! nop ! " |
| 110 | " nop ! nop ! nop ! nop ! nop ! " |
| 111 | " nop ! nop ! nop ! nop ! nop ! " |
| 112 | : : "r" (reset_addr), "r" (1) ); |
| 113 | |
| 114 | for (;;) |
| 115 | ; |
| 116 | } |
| 117 | |
| 118 | void machine_halt(void) |
| 119 | { |
| 120 | #ifdef CONFIG_GDBSTUB |
| 121 | gdbstub_exit(0); |
| 122 | #endif |
| 123 | |
| 124 | for (;;); |
| 125 | } |
| 126 | |
| 127 | void machine_power_off(void) |
| 128 | { |
| 129 | #ifdef CONFIG_GDBSTUB |
| 130 | gdbstub_exit(0); |
| 131 | #endif |
| 132 | |
| 133 | for (;;); |
| 134 | } |
| 135 | |
| 136 | void flush_thread(void) |
| 137 | { |
| 138 | #if 0 //ndef NO_FPU |
| 139 | unsigned long zero = 0; |
| 140 | #endif |
| 141 | set_fs(USER_DS); |
| 142 | } |
| 143 | |
| 144 | inline unsigned long user_stack(const struct pt_regs *regs) |
| 145 | { |
| 146 | while (regs->next_frame) |
| 147 | regs = regs->next_frame; |
| 148 | return user_mode(regs) ? regs->sp : 0; |
| 149 | } |
| 150 | |
| 151 | asmlinkage int sys_fork(void) |
| 152 | { |
| 153 | #ifndef CONFIG_MMU |
| 154 | /* fork almost works, enough to trick you into looking elsewhere:-( */ |
| 155 | return -EINVAL; |
| 156 | #else |
| 157 | return do_fork(SIGCHLD, user_stack(__frame), __frame, 0, NULL, NULL); |
| 158 | #endif |
| 159 | } |
| 160 | |
| 161 | asmlinkage int sys_vfork(void) |
| 162 | { |
| 163 | return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, user_stack(__frame), __frame, 0, |
| 164 | NULL, NULL); |
| 165 | } |
| 166 | |
| 167 | /*****************************************************************************/ |
| 168 | /* |
| 169 | * clone a process |
| 170 | * - tlsptr is retrieved by copy_thread() |
| 171 | */ |
| 172 | asmlinkage int sys_clone(unsigned long clone_flags, unsigned long newsp, |
| 173 | int __user *parent_tidptr, int __user *child_tidptr, |
| 174 | int __user *tlsptr) |
| 175 | { |
| 176 | if (!newsp) |
| 177 | newsp = user_stack(__frame); |
| 178 | return do_fork(clone_flags, newsp, __frame, 0, parent_tidptr, child_tidptr); |
| 179 | } /* end sys_clone() */ |
| 180 | |
| 181 | /*****************************************************************************/ |
| 182 | /* |
| 183 | * This gets called before we allocate a new thread and copy |
| 184 | * the current task into it. |
| 185 | */ |
| 186 | void prepare_to_copy(struct task_struct *tsk) |
| 187 | { |
| 188 | //unlazy_fpu(tsk); |
| 189 | } /* end prepare_to_copy() */ |
| 190 | |
| 191 | /*****************************************************************************/ |
| 192 | /* |
| 193 | * set up the kernel stack and exception frames for a new process |
| 194 | */ |
| 195 | int copy_thread(int nr, unsigned long clone_flags, |
| 196 | unsigned long usp, unsigned long topstk, |
| 197 | struct task_struct *p, struct pt_regs *regs) |
| 198 | { |
| 199 | struct pt_regs *childregs0, *childregs, *regs0; |
| 200 | |
| 201 | regs0 = __kernel_frame0_ptr; |
| 202 | childregs0 = (struct pt_regs *) |
| 203 | ((unsigned long) p->thread_info + THREAD_SIZE - USER_CONTEXT_SIZE); |
| 204 | childregs = childregs0; |
| 205 | |
| 206 | /* set up the userspace frame (the only place that the USP is stored) */ |
| 207 | *childregs0 = *regs0; |
| 208 | |
| 209 | childregs0->gr8 = 0; |
| 210 | childregs0->sp = usp; |
| 211 | childregs0->next_frame = NULL; |
| 212 | |
| 213 | /* set up the return kernel frame if called from kernel_thread() */ |
| 214 | if (regs != regs0) { |
| 215 | childregs--; |
| 216 | *childregs = *regs; |
| 217 | childregs->sp = (unsigned long) childregs0; |
| 218 | childregs->next_frame = childregs0; |
| 219 | childregs->gr15 = (unsigned long) p->thread_info; |
| 220 | childregs->gr29 = (unsigned long) p; |
| 221 | } |
| 222 | |
| 223 | p->set_child_tid = p->clear_child_tid = NULL; |
| 224 | |
| 225 | p->thread.frame = childregs; |
| 226 | p->thread.curr = p; |
| 227 | p->thread.sp = (unsigned long) childregs; |
| 228 | p->thread.fp = 0; |
| 229 | p->thread.lr = 0; |
| 230 | p->thread.pc = (unsigned long) ret_from_fork; |
| 231 | p->thread.frame0 = childregs0; |
| 232 | |
| 233 | /* the new TLS pointer is passed in as arg #5 to sys_clone() */ |
| 234 | if (clone_flags & CLONE_SETTLS) |
| 235 | childregs->gr29 = childregs->gr12; |
| 236 | |
| 237 | save_user_regs(p->thread.user); |
| 238 | |
| 239 | return 0; |
| 240 | } /* end copy_thread() */ |
| 241 | |
| 242 | /* |
| 243 | * fill in the user structure for a core dump.. |
| 244 | */ |
| 245 | void dump_thread(struct pt_regs *regs, struct user *dump) |
| 246 | { |
| 247 | #if 0 |
| 248 | /* changed the size calculations - should hopefully work better. lbt */ |
| 249 | dump->magic = CMAGIC; |
| 250 | dump->start_code = 0; |
| 251 | dump->start_stack = user_stack(regs) & ~(PAGE_SIZE - 1); |
| 252 | dump->u_tsize = ((unsigned long) current->mm->end_code) >> PAGE_SHIFT; |
| 253 | dump->u_dsize = ((unsigned long) (current->mm->brk + (PAGE_SIZE-1))) >> PAGE_SHIFT; |
| 254 | dump->u_dsize -= dump->u_tsize; |
| 255 | dump->u_ssize = 0; |
| 256 | |
| 257 | if (dump->start_stack < TASK_SIZE) |
| 258 | dump->u_ssize = ((unsigned long) (TASK_SIZE - dump->start_stack)) >> PAGE_SHIFT; |
| 259 | |
| 260 | dump->regs = *(struct user_context *) regs; |
| 261 | #endif |
| 262 | } |
| 263 | |
| 264 | /* |
| 265 | * sys_execve() executes a new program. |
| 266 | */ |
| 267 | asmlinkage int sys_execve(char *name, char **argv, char **envp) |
| 268 | { |
| 269 | int error; |
| 270 | char * filename; |
| 271 | |
| 272 | lock_kernel(); |
| 273 | filename = getname(name); |
| 274 | error = PTR_ERR(filename); |
| 275 | if (IS_ERR(filename)) |
| 276 | goto out; |
| 277 | error = do_execve(filename, argv, envp, __frame); |
| 278 | putname(filename); |
| 279 | out: |
| 280 | unlock_kernel(); |
| 281 | return error; |
| 282 | } |
| 283 | |
| 284 | unsigned long get_wchan(struct task_struct *p) |
| 285 | { |
| 286 | struct pt_regs *regs0; |
| 287 | unsigned long fp, pc; |
| 288 | unsigned long stack_limit; |
| 289 | int count = 0; |
| 290 | if (!p || p == current || p->state == TASK_RUNNING) |
| 291 | return 0; |
| 292 | |
| 293 | stack_limit = (unsigned long) (p + 1); |
| 294 | fp = p->thread.fp; |
| 295 | regs0 = p->thread.frame0; |
| 296 | |
| 297 | do { |
| 298 | if (fp < stack_limit || fp >= (unsigned long) regs0 || fp & 3) |
| 299 | return 0; |
| 300 | |
| 301 | pc = ((unsigned long *) fp)[2]; |
| 302 | |
| 303 | /* FIXME: This depends on the order of these functions. */ |
| 304 | if (!in_sched_functions(pc)) |
| 305 | return pc; |
| 306 | |
| 307 | fp = *(unsigned long *) fp; |
| 308 | } while (count++ < 16); |
| 309 | |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | unsigned long thread_saved_pc(struct task_struct *tsk) |
| 314 | { |
| 315 | /* Check whether the thread is blocked in resume() */ |
| 316 | if (in_sched_functions(tsk->thread.pc)) |
| 317 | return ((unsigned long *)tsk->thread.fp)[2]; |
| 318 | else |
| 319 | return tsk->thread.pc; |
| 320 | } |
| 321 | |
| 322 | int elf_check_arch(const struct elf32_hdr *hdr) |
| 323 | { |
| 324 | unsigned long hsr0 = __get_HSR(0); |
| 325 | unsigned long psr = __get_PSR(); |
| 326 | |
| 327 | if (hdr->e_machine != EM_FRV) |
| 328 | return 0; |
| 329 | |
| 330 | switch (hdr->e_flags & EF_FRV_GPR_MASK) { |
| 331 | case EF_FRV_GPR64: |
| 332 | if ((hsr0 & HSR0_GRN) == HSR0_GRN_32) |
| 333 | return 0; |
| 334 | case EF_FRV_GPR32: |
| 335 | case 0: |
| 336 | break; |
| 337 | default: |
| 338 | return 0; |
| 339 | } |
| 340 | |
| 341 | switch (hdr->e_flags & EF_FRV_FPR_MASK) { |
| 342 | case EF_FRV_FPR64: |
| 343 | if ((hsr0 & HSR0_FRN) == HSR0_FRN_32) |
| 344 | return 0; |
| 345 | case EF_FRV_FPR32: |
| 346 | case EF_FRV_FPR_NONE: |
| 347 | case 0: |
| 348 | break; |
| 349 | default: |
| 350 | return 0; |
| 351 | } |
| 352 | |
| 353 | if ((hdr->e_flags & EF_FRV_MULADD) == EF_FRV_MULADD) |
| 354 | if (PSR_IMPLE(psr) != PSR_IMPLE_FR405 && |
| 355 | PSR_IMPLE(psr) != PSR_IMPLE_FR451) |
| 356 | return 0; |
| 357 | |
| 358 | switch (hdr->e_flags & EF_FRV_CPU_MASK) { |
| 359 | case EF_FRV_CPU_GENERIC: |
| 360 | break; |
| 361 | case EF_FRV_CPU_FR300: |
| 362 | case EF_FRV_CPU_SIMPLE: |
| 363 | case EF_FRV_CPU_TOMCAT: |
| 364 | default: |
| 365 | return 0; |
| 366 | case EF_FRV_CPU_FR400: |
| 367 | if (PSR_IMPLE(psr) != PSR_IMPLE_FR401 && |
| 368 | PSR_IMPLE(psr) != PSR_IMPLE_FR405 && |
| 369 | PSR_IMPLE(psr) != PSR_IMPLE_FR451 && |
| 370 | PSR_IMPLE(psr) != PSR_IMPLE_FR551) |
| 371 | return 0; |
| 372 | break; |
| 373 | case EF_FRV_CPU_FR450: |
| 374 | if (PSR_IMPLE(psr) != PSR_IMPLE_FR451) |
| 375 | return 0; |
| 376 | break; |
| 377 | case EF_FRV_CPU_FR500: |
| 378 | if (PSR_IMPLE(psr) != PSR_IMPLE_FR501) |
| 379 | return 0; |
| 380 | break; |
| 381 | case EF_FRV_CPU_FR550: |
| 382 | if (PSR_IMPLE(psr) != PSR_IMPLE_FR551) |
| 383 | return 0; |
| 384 | break; |
| 385 | } |
| 386 | |
| 387 | return 1; |
| 388 | } |