Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 |
| 4 | * for more details. |
| 5 | * |
| 6 | * Copyright (C) 1994 - 1999, 2000 by Ralf Baechle and others. |
| 7 | * Copyright (C) 1999, 2000 Silicon Graphics, Inc. |
| 8 | * Copyright (C) 2004 Thiemo Seufer |
| 9 | */ |
| 10 | #include <linux/config.h> |
| 11 | #include <linux/errno.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/sched.h> |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/mm.h> |
| 16 | #include <linux/stddef.h> |
| 17 | #include <linux/unistd.h> |
| 18 | #include <linux/ptrace.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/mman.h> |
| 21 | #include <linux/personality.h> |
| 22 | #include <linux/sys.h> |
| 23 | #include <linux/user.h> |
| 24 | #include <linux/a.out.h> |
| 25 | #include <linux/init.h> |
| 26 | #include <linux/completion.h> |
| 27 | |
| 28 | #include <asm/bootinfo.h> |
| 29 | #include <asm/cpu.h> |
| 30 | #include <asm/fpu.h> |
| 31 | #include <asm/pgtable.h> |
| 32 | #include <asm/system.h> |
| 33 | #include <asm/mipsregs.h> |
| 34 | #include <asm/processor.h> |
| 35 | #include <asm/uaccess.h> |
| 36 | #include <asm/io.h> |
| 37 | #include <asm/elf.h> |
| 38 | #include <asm/isadep.h> |
| 39 | #include <asm/inst.h> |
| 40 | |
| 41 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | * The idle thread. There's no useful work to be done, so just try to conserve |
| 43 | * power and have a low exit latency (ie sit in a loop waiting for somebody to |
| 44 | * say that they'd like to reschedule) |
| 45 | */ |
| 46 | ATTRIB_NORET void cpu_idle(void) |
| 47 | { |
| 48 | /* endless idle loop with no priority at all */ |
| 49 | while (1) { |
| 50 | while (!need_resched()) |
| 51 | if (cpu_wait) |
| 52 | (*cpu_wait)(); |
| 53 | schedule(); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | asmlinkage void ret_from_fork(void); |
| 58 | |
| 59 | void start_thread(struct pt_regs * regs, unsigned long pc, unsigned long sp) |
| 60 | { |
| 61 | unsigned long status; |
| 62 | |
| 63 | /* New thread loses kernel privileges. */ |
| 64 | status = regs->cp0_status & ~(ST0_CU0|ST0_CU1|KU_MASK); |
Ralf Baechle | 875d43e | 2005-09-03 15:56:16 -0700 | [diff] [blame] | 65 | #ifdef CONFIG_64BIT |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | status &= ~ST0_FR; |
| 67 | status |= (current->thread.mflags & MF_32BIT_REGS) ? 0 : ST0_FR; |
| 68 | #endif |
| 69 | status |= KU_USER; |
| 70 | regs->cp0_status = status; |
| 71 | clear_used_math(); |
| 72 | lose_fpu(); |
| 73 | regs->cp0_epc = pc; |
| 74 | regs->regs[29] = sp; |
| 75 | current_thread_info()->addr_limit = USER_DS; |
| 76 | } |
| 77 | |
| 78 | void exit_thread(void) |
| 79 | { |
| 80 | } |
| 81 | |
| 82 | void flush_thread(void) |
| 83 | { |
| 84 | } |
| 85 | |
| 86 | int copy_thread(int nr, unsigned long clone_flags, unsigned long usp, |
| 87 | unsigned long unused, struct task_struct *p, struct pt_regs *regs) |
| 88 | { |
| 89 | struct thread_info *ti = p->thread_info; |
| 90 | struct pt_regs *childregs; |
| 91 | long childksp; |
Ralf Baechle | 3c37026 | 2005-04-13 17:43:59 +0000 | [diff] [blame^] | 92 | p->set_child_tid = p->clear_child_tid = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
| 94 | childksp = (unsigned long)ti + THREAD_SIZE - 32; |
| 95 | |
| 96 | preempt_disable(); |
| 97 | |
| 98 | if (is_fpu_owner()) { |
| 99 | save_fp(p); |
| 100 | } |
| 101 | |
| 102 | preempt_enable(); |
| 103 | |
| 104 | /* set up new TSS. */ |
| 105 | childregs = (struct pt_regs *) childksp - 1; |
| 106 | *childregs = *regs; |
| 107 | childregs->regs[7] = 0; /* Clear error flag */ |
| 108 | |
| 109 | #if defined(CONFIG_BINFMT_IRIX) |
| 110 | if (current->personality != PER_LINUX) { |
| 111 | /* Under IRIX things are a little different. */ |
| 112 | childregs->regs[3] = 1; |
| 113 | regs->regs[3] = 0; |
| 114 | } |
| 115 | #endif |
| 116 | childregs->regs[2] = 0; /* Child gets zero as return value */ |
| 117 | regs->regs[2] = p->pid; |
| 118 | |
| 119 | if (childregs->cp0_status & ST0_CU0) { |
| 120 | childregs->regs[28] = (unsigned long) ti; |
| 121 | childregs->regs[29] = childksp; |
| 122 | ti->addr_limit = KERNEL_DS; |
| 123 | } else { |
| 124 | childregs->regs[29] = usp; |
| 125 | ti->addr_limit = USER_DS; |
| 126 | } |
| 127 | p->thread.reg29 = (unsigned long) childregs; |
| 128 | p->thread.reg31 = (unsigned long) ret_from_fork; |
| 129 | |
| 130 | /* |
| 131 | * New tasks lose permission to use the fpu. This accelerates context |
| 132 | * switching for most programs since they don't use the fpu. |
| 133 | */ |
| 134 | p->thread.cp0_status = read_c0_status() & ~(ST0_CU2|ST0_CU1); |
| 135 | childregs->cp0_status &= ~(ST0_CU2|ST0_CU1); |
| 136 | clear_tsk_thread_flag(p, TIF_USEDFPU); |
| 137 | |
Ralf Baechle | 3c37026 | 2005-04-13 17:43:59 +0000 | [diff] [blame^] | 138 | if (clone_flags & CLONE_SETTLS) |
| 139 | ti->tp_value = regs->regs[7]; |
| 140 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | /* Fill in the fpu structure for a core dump.. */ |
| 145 | int dump_fpu(struct pt_regs *regs, elf_fpregset_t *r) |
| 146 | { |
| 147 | memcpy(r, ¤t->thread.fpu, sizeof(current->thread.fpu)); |
| 148 | |
| 149 | return 1; |
| 150 | } |
| 151 | |
| 152 | void dump_regs(elf_greg_t *gp, struct pt_regs *regs) |
| 153 | { |
| 154 | int i; |
| 155 | |
| 156 | for (i = 0; i < EF_R0; i++) |
| 157 | gp[i] = 0; |
| 158 | gp[EF_R0] = 0; |
| 159 | for (i = 1; i <= 31; i++) |
| 160 | gp[EF_R0 + i] = regs->regs[i]; |
| 161 | gp[EF_R26] = 0; |
| 162 | gp[EF_R27] = 0; |
| 163 | gp[EF_LO] = regs->lo; |
| 164 | gp[EF_HI] = regs->hi; |
| 165 | gp[EF_CP0_EPC] = regs->cp0_epc; |
| 166 | gp[EF_CP0_BADVADDR] = regs->cp0_badvaddr; |
| 167 | gp[EF_CP0_STATUS] = regs->cp0_status; |
| 168 | gp[EF_CP0_CAUSE] = regs->cp0_cause; |
| 169 | #ifdef EF_UNUSED0 |
| 170 | gp[EF_UNUSED0] = 0; |
| 171 | #endif |
| 172 | } |
| 173 | |
Ralf Baechle | 71e0e55 | 2005-03-14 10:16:59 +0000 | [diff] [blame] | 174 | int dump_task_regs (struct task_struct *tsk, elf_gregset_t *regs) |
| 175 | { |
| 176 | struct thread_info *ti = tsk->thread_info; |
| 177 | long ksp = (unsigned long)ti + THREAD_SIZE - 32; |
| 178 | dump_regs(&(*regs)[0], (struct pt_regs *) ksp - 1); |
| 179 | return 1; |
| 180 | } |
| 181 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | int dump_task_fpu (struct task_struct *t, elf_fpregset_t *fpr) |
| 183 | { |
| 184 | memcpy(fpr, &t->thread.fpu, sizeof(current->thread.fpu)); |
| 185 | |
| 186 | return 1; |
| 187 | } |
| 188 | |
| 189 | /* |
| 190 | * Create a kernel thread |
| 191 | */ |
| 192 | ATTRIB_NORET void kernel_thread_helper(void *arg, int (*fn)(void *)) |
| 193 | { |
| 194 | do_exit(fn(arg)); |
| 195 | } |
| 196 | |
| 197 | long kernel_thread(int (*fn)(void *), void *arg, unsigned long flags) |
| 198 | { |
| 199 | struct pt_regs regs; |
| 200 | |
| 201 | memset(®s, 0, sizeof(regs)); |
| 202 | |
| 203 | regs.regs[4] = (unsigned long) arg; |
| 204 | regs.regs[5] = (unsigned long) fn; |
| 205 | regs.cp0_epc = (unsigned long) kernel_thread_helper; |
| 206 | regs.cp0_status = read_c0_status(); |
| 207 | #if defined(CONFIG_CPU_R3000) || defined(CONFIG_CPU_TX39XX) |
| 208 | regs.cp0_status &= ~(ST0_KUP | ST0_IEC); |
| 209 | regs.cp0_status |= ST0_IEP; |
| 210 | #else |
| 211 | regs.cp0_status |= ST0_EXL; |
| 212 | #endif |
| 213 | |
| 214 | /* Ok, create the new process.. */ |
| 215 | return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, ®s, 0, NULL, NULL); |
| 216 | } |
| 217 | |
Thiemo Seufer | dc953df | 2005-02-21 10:55:16 +0000 | [diff] [blame] | 218 | static struct mips_frame_info { |
| 219 | void *func; |
| 220 | int omit_fp; /* compiled without fno-omit-frame-pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | int frame_offset; |
| 222 | int pc_offset; |
Thiemo Seufer | dc953df | 2005-02-21 10:55:16 +0000 | [diff] [blame] | 223 | } schedule_frame, mfinfo[] = { |
| 224 | { schedule, 0 }, /* must be first */ |
| 225 | /* arch/mips/kernel/semaphore.c */ |
| 226 | { __down, 1 }, |
| 227 | { __down_interruptible, 1 }, |
| 228 | /* kernel/sched.c */ |
| 229 | #ifdef CONFIG_PREEMPT |
| 230 | { preempt_schedule, 0 }, |
| 231 | #endif |
| 232 | { wait_for_completion, 0 }, |
| 233 | { interruptible_sleep_on, 0 }, |
| 234 | { interruptible_sleep_on_timeout, 0 }, |
| 235 | { sleep_on, 0 }, |
| 236 | { sleep_on_timeout, 0 }, |
| 237 | { yield, 0 }, |
| 238 | { io_schedule, 0 }, |
| 239 | { io_schedule_timeout, 0 }, |
| 240 | #if defined(CONFIG_SMP) && defined(CONFIG_PREEMPT) |
| 241 | { __preempt_spin_lock, 0 }, |
| 242 | { __preempt_write_lock, 0 }, |
| 243 | #endif |
| 244 | /* kernel/timer.c */ |
| 245 | { schedule_timeout, 1 }, |
| 246 | /* { nanosleep_restart, 1 }, */ |
| 247 | /* lib/rwsem-spinlock.c */ |
| 248 | { __down_read, 1 }, |
| 249 | { __down_write, 1 }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | }; |
Thiemo Seufer | dc953df | 2005-02-21 10:55:16 +0000 | [diff] [blame] | 251 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | static int mips_frame_info_initialized; |
Thiemo Seufer | dc953df | 2005-02-21 10:55:16 +0000 | [diff] [blame] | 253 | static int __init get_frame_info(struct mips_frame_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | { |
| 255 | int i; |
Thiemo Seufer | dc953df | 2005-02-21 10:55:16 +0000 | [diff] [blame] | 256 | void *func = info->func; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | union mips_instruction *ip = (union mips_instruction *)func; |
| 258 | info->pc_offset = -1; |
Thiemo Seufer | dc953df | 2005-02-21 10:55:16 +0000 | [diff] [blame] | 259 | info->frame_offset = info->omit_fp ? 0 : -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | for (i = 0; i < 128; i++, ip++) { |
| 261 | /* if jal, jalr, jr, stop. */ |
| 262 | if (ip->j_format.opcode == jal_op || |
| 263 | (ip->r_format.opcode == spec_op && |
| 264 | (ip->r_format.func == jalr_op || |
| 265 | ip->r_format.func == jr_op))) |
| 266 | break; |
| 267 | |
| 268 | if ( |
Ralf Baechle | 875d43e | 2005-09-03 15:56:16 -0700 | [diff] [blame] | 269 | #ifdef CONFIG_32BIT |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | ip->i_format.opcode == sw_op && |
| 271 | #endif |
Ralf Baechle | 875d43e | 2005-09-03 15:56:16 -0700 | [diff] [blame] | 272 | #ifdef CONFIG_64BIT |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | ip->i_format.opcode == sd_op && |
| 274 | #endif |
| 275 | ip->i_format.rs == 29) |
| 276 | { |
| 277 | /* sw / sd $ra, offset($sp) */ |
| 278 | if (ip->i_format.rt == 31) { |
| 279 | if (info->pc_offset != -1) |
Thiemo Seufer | dc953df | 2005-02-21 10:55:16 +0000 | [diff] [blame] | 280 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | info->pc_offset = |
| 282 | ip->i_format.simmediate / sizeof(long); |
| 283 | } |
| 284 | /* sw / sd $s8, offset($sp) */ |
| 285 | if (ip->i_format.rt == 30) { |
Thiemo Seufer | dc953df | 2005-02-21 10:55:16 +0000 | [diff] [blame] | 286 | //#if 0 /* gcc 3.4 does aggressive optimization... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | if (info->frame_offset != -1) |
Thiemo Seufer | dc953df | 2005-02-21 10:55:16 +0000 | [diff] [blame] | 288 | continue; |
| 289 | //#endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | info->frame_offset = |
| 291 | ip->i_format.simmediate / sizeof(long); |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | if (info->pc_offset == -1 || info->frame_offset == -1) { |
| 296 | printk("Can't analyze prologue code at %p\n", func); |
| 297 | info->pc_offset = -1; |
| 298 | info->frame_offset = -1; |
| 299 | return -1; |
| 300 | } |
| 301 | |
| 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | static int __init frame_info_init(void) |
| 306 | { |
Thiemo Seufer | dc953df | 2005-02-21 10:55:16 +0000 | [diff] [blame] | 307 | int i, found; |
| 308 | for (i = 0; i < ARRAY_SIZE(mfinfo); i++) |
| 309 | if (get_frame_info(&mfinfo[i])) |
| 310 | return -1; |
| 311 | schedule_frame = mfinfo[0]; |
| 312 | /* bubble sort */ |
| 313 | do { |
| 314 | struct mips_frame_info tmp; |
| 315 | found = 0; |
| 316 | for (i = 1; i < ARRAY_SIZE(mfinfo); i++) { |
| 317 | if (mfinfo[i-1].func > mfinfo[i].func) { |
| 318 | tmp = mfinfo[i]; |
| 319 | mfinfo[i] = mfinfo[i-1]; |
| 320 | mfinfo[i-1] = tmp; |
| 321 | found = 1; |
| 322 | } |
| 323 | } |
| 324 | } while (found); |
| 325 | mips_frame_info_initialized = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | arch_initcall(frame_info_init); |
| 330 | |
| 331 | /* |
| 332 | * Return saved PC of a blocked thread. |
| 333 | */ |
| 334 | unsigned long thread_saved_pc(struct task_struct *tsk) |
| 335 | { |
| 336 | struct thread_struct *t = &tsk->thread; |
| 337 | |
| 338 | /* New born processes are a special case */ |
| 339 | if (t->reg31 == (unsigned long) ret_from_fork) |
| 340 | return t->reg31; |
| 341 | |
| 342 | if (schedule_frame.pc_offset < 0) |
| 343 | return 0; |
| 344 | return ((unsigned long *)t->reg29)[schedule_frame.pc_offset]; |
| 345 | } |
| 346 | |
| 347 | /* get_wchan - a maintenance nightmare^W^Wpain in the ass ... */ |
| 348 | unsigned long get_wchan(struct task_struct *p) |
| 349 | { |
Thiemo Seufer | dc953df | 2005-02-21 10:55:16 +0000 | [diff] [blame] | 350 | unsigned long stack_page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | unsigned long frame, pc; |
| 352 | |
| 353 | if (!p || p == current || p->state == TASK_RUNNING) |
| 354 | return 0; |
| 355 | |
Thiemo Seufer | dc953df | 2005-02-21 10:55:16 +0000 | [diff] [blame] | 356 | stack_page = (unsigned long)p->thread_info; |
| 357 | if (!stack_page || !mips_frame_info_initialized) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | return 0; |
Thiemo Seufer | dc953df | 2005-02-21 10:55:16 +0000 | [diff] [blame] | 359 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | pc = thread_saved_pc(p); |
| 361 | if (!in_sched_functions(pc)) |
Thiemo Seufer | dc953df | 2005-02-21 10:55:16 +0000 | [diff] [blame] | 362 | return pc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | frame = ((unsigned long *)p->thread.reg30)[schedule_frame.frame_offset]; |
Thiemo Seufer | dc953df | 2005-02-21 10:55:16 +0000 | [diff] [blame] | 365 | do { |
| 366 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | |
Thiemo Seufer | dc953df | 2005-02-21 10:55:16 +0000 | [diff] [blame] | 368 | if (frame < stack_page || frame > stack_page + THREAD_SIZE - 32) |
| 369 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | |
Thiemo Seufer | dc953df | 2005-02-21 10:55:16 +0000 | [diff] [blame] | 371 | for (i = ARRAY_SIZE(mfinfo) - 1; i >= 0; i--) { |
| 372 | if (pc >= (unsigned long) mfinfo[i].func) |
| 373 | break; |
| 374 | } |
| 375 | if (i < 0) |
| 376 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | |
Thiemo Seufer | dc953df | 2005-02-21 10:55:16 +0000 | [diff] [blame] | 378 | if (mfinfo[i].omit_fp) |
| 379 | break; |
| 380 | pc = ((unsigned long *)frame)[mfinfo[i].pc_offset]; |
| 381 | frame = ((unsigned long *)frame)[mfinfo[i].frame_offset]; |
| 382 | } while (in_sched_functions(pc)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | |
| 384 | return pc; |
| 385 | } |
| 386 | |
| 387 | EXPORT_SYMBOL(get_wchan); |