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; |
| 92 | |
| 93 | childksp = (unsigned long)ti + THREAD_SIZE - 32; |
| 94 | |
| 95 | preempt_disable(); |
| 96 | |
| 97 | if (is_fpu_owner()) { |
| 98 | save_fp(p); |
| 99 | } |
| 100 | |
| 101 | preempt_enable(); |
| 102 | |
| 103 | /* set up new TSS. */ |
| 104 | childregs = (struct pt_regs *) childksp - 1; |
| 105 | *childregs = *regs; |
| 106 | childregs->regs[7] = 0; /* Clear error flag */ |
| 107 | |
| 108 | #if defined(CONFIG_BINFMT_IRIX) |
| 109 | if (current->personality != PER_LINUX) { |
| 110 | /* Under IRIX things are a little different. */ |
| 111 | childregs->regs[3] = 1; |
| 112 | regs->regs[3] = 0; |
| 113 | } |
| 114 | #endif |
| 115 | childregs->regs[2] = 0; /* Child gets zero as return value */ |
| 116 | regs->regs[2] = p->pid; |
| 117 | |
| 118 | if (childregs->cp0_status & ST0_CU0) { |
| 119 | childregs->regs[28] = (unsigned long) ti; |
| 120 | childregs->regs[29] = childksp; |
| 121 | ti->addr_limit = KERNEL_DS; |
| 122 | } else { |
| 123 | childregs->regs[29] = usp; |
| 124 | ti->addr_limit = USER_DS; |
| 125 | } |
| 126 | p->thread.reg29 = (unsigned long) childregs; |
| 127 | p->thread.reg31 = (unsigned long) ret_from_fork; |
| 128 | |
| 129 | /* |
| 130 | * New tasks lose permission to use the fpu. This accelerates context |
| 131 | * switching for most programs since they don't use the fpu. |
| 132 | */ |
| 133 | p->thread.cp0_status = read_c0_status() & ~(ST0_CU2|ST0_CU1); |
| 134 | childregs->cp0_status &= ~(ST0_CU2|ST0_CU1); |
| 135 | clear_tsk_thread_flag(p, TIF_USEDFPU); |
| 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | /* Fill in the fpu structure for a core dump.. */ |
| 141 | int dump_fpu(struct pt_regs *regs, elf_fpregset_t *r) |
| 142 | { |
| 143 | memcpy(r, ¤t->thread.fpu, sizeof(current->thread.fpu)); |
| 144 | |
| 145 | return 1; |
| 146 | } |
| 147 | |
| 148 | void dump_regs(elf_greg_t *gp, struct pt_regs *regs) |
| 149 | { |
| 150 | int i; |
| 151 | |
| 152 | for (i = 0; i < EF_R0; i++) |
| 153 | gp[i] = 0; |
| 154 | gp[EF_R0] = 0; |
| 155 | for (i = 1; i <= 31; i++) |
| 156 | gp[EF_R0 + i] = regs->regs[i]; |
| 157 | gp[EF_R26] = 0; |
| 158 | gp[EF_R27] = 0; |
| 159 | gp[EF_LO] = regs->lo; |
| 160 | gp[EF_HI] = regs->hi; |
| 161 | gp[EF_CP0_EPC] = regs->cp0_epc; |
| 162 | gp[EF_CP0_BADVADDR] = regs->cp0_badvaddr; |
| 163 | gp[EF_CP0_STATUS] = regs->cp0_status; |
| 164 | gp[EF_CP0_CAUSE] = regs->cp0_cause; |
| 165 | #ifdef EF_UNUSED0 |
| 166 | gp[EF_UNUSED0] = 0; |
| 167 | #endif |
| 168 | } |
| 169 | |
Ralf Baechle | 71e0e55 | 2005-03-14 10:16:59 +0000 | [diff] [blame^] | 170 | int dump_task_regs (struct task_struct *tsk, elf_gregset_t *regs) |
| 171 | { |
| 172 | struct thread_info *ti = tsk->thread_info; |
| 173 | long ksp = (unsigned long)ti + THREAD_SIZE - 32; |
| 174 | dump_regs(&(*regs)[0], (struct pt_regs *) ksp - 1); |
| 175 | return 1; |
| 176 | } |
| 177 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | int dump_task_fpu (struct task_struct *t, elf_fpregset_t *fpr) |
| 179 | { |
| 180 | memcpy(fpr, &t->thread.fpu, sizeof(current->thread.fpu)); |
| 181 | |
| 182 | return 1; |
| 183 | } |
| 184 | |
| 185 | /* |
| 186 | * Create a kernel thread |
| 187 | */ |
| 188 | ATTRIB_NORET void kernel_thread_helper(void *arg, int (*fn)(void *)) |
| 189 | { |
| 190 | do_exit(fn(arg)); |
| 191 | } |
| 192 | |
| 193 | long kernel_thread(int (*fn)(void *), void *arg, unsigned long flags) |
| 194 | { |
| 195 | struct pt_regs regs; |
| 196 | |
| 197 | memset(®s, 0, sizeof(regs)); |
| 198 | |
| 199 | regs.regs[4] = (unsigned long) arg; |
| 200 | regs.regs[5] = (unsigned long) fn; |
| 201 | regs.cp0_epc = (unsigned long) kernel_thread_helper; |
| 202 | regs.cp0_status = read_c0_status(); |
| 203 | #if defined(CONFIG_CPU_R3000) || defined(CONFIG_CPU_TX39XX) |
| 204 | regs.cp0_status &= ~(ST0_KUP | ST0_IEC); |
| 205 | regs.cp0_status |= ST0_IEP; |
| 206 | #else |
| 207 | regs.cp0_status |= ST0_EXL; |
| 208 | #endif |
| 209 | |
| 210 | /* Ok, create the new process.. */ |
| 211 | return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, ®s, 0, NULL, NULL); |
| 212 | } |
| 213 | |
Thiemo Seufer | dc953df | 2005-02-21 10:55:16 +0000 | [diff] [blame] | 214 | static struct mips_frame_info { |
| 215 | void *func; |
| 216 | int omit_fp; /* compiled without fno-omit-frame-pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | int frame_offset; |
| 218 | int pc_offset; |
Thiemo Seufer | dc953df | 2005-02-21 10:55:16 +0000 | [diff] [blame] | 219 | } schedule_frame, mfinfo[] = { |
| 220 | { schedule, 0 }, /* must be first */ |
| 221 | /* arch/mips/kernel/semaphore.c */ |
| 222 | { __down, 1 }, |
| 223 | { __down_interruptible, 1 }, |
| 224 | /* kernel/sched.c */ |
| 225 | #ifdef CONFIG_PREEMPT |
| 226 | { preempt_schedule, 0 }, |
| 227 | #endif |
| 228 | { wait_for_completion, 0 }, |
| 229 | { interruptible_sleep_on, 0 }, |
| 230 | { interruptible_sleep_on_timeout, 0 }, |
| 231 | { sleep_on, 0 }, |
| 232 | { sleep_on_timeout, 0 }, |
| 233 | { yield, 0 }, |
| 234 | { io_schedule, 0 }, |
| 235 | { io_schedule_timeout, 0 }, |
| 236 | #if defined(CONFIG_SMP) && defined(CONFIG_PREEMPT) |
| 237 | { __preempt_spin_lock, 0 }, |
| 238 | { __preempt_write_lock, 0 }, |
| 239 | #endif |
| 240 | /* kernel/timer.c */ |
| 241 | { schedule_timeout, 1 }, |
| 242 | /* { nanosleep_restart, 1 }, */ |
| 243 | /* lib/rwsem-spinlock.c */ |
| 244 | { __down_read, 1 }, |
| 245 | { __down_write, 1 }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | }; |
Thiemo Seufer | dc953df | 2005-02-21 10:55:16 +0000 | [diff] [blame] | 247 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | static int mips_frame_info_initialized; |
Thiemo Seufer | dc953df | 2005-02-21 10:55:16 +0000 | [diff] [blame] | 249 | static int __init get_frame_info(struct mips_frame_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | { |
| 251 | int i; |
Thiemo Seufer | dc953df | 2005-02-21 10:55:16 +0000 | [diff] [blame] | 252 | void *func = info->func; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | union mips_instruction *ip = (union mips_instruction *)func; |
| 254 | info->pc_offset = -1; |
Thiemo Seufer | dc953df | 2005-02-21 10:55:16 +0000 | [diff] [blame] | 255 | info->frame_offset = info->omit_fp ? 0 : -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | for (i = 0; i < 128; i++, ip++) { |
| 257 | /* if jal, jalr, jr, stop. */ |
| 258 | if (ip->j_format.opcode == jal_op || |
| 259 | (ip->r_format.opcode == spec_op && |
| 260 | (ip->r_format.func == jalr_op || |
| 261 | ip->r_format.func == jr_op))) |
| 262 | break; |
| 263 | |
| 264 | if ( |
Ralf Baechle | 875d43e | 2005-09-03 15:56:16 -0700 | [diff] [blame] | 265 | #ifdef CONFIG_32BIT |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | ip->i_format.opcode == sw_op && |
| 267 | #endif |
Ralf Baechle | 875d43e | 2005-09-03 15:56:16 -0700 | [diff] [blame] | 268 | #ifdef CONFIG_64BIT |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | ip->i_format.opcode == sd_op && |
| 270 | #endif |
| 271 | ip->i_format.rs == 29) |
| 272 | { |
| 273 | /* sw / sd $ra, offset($sp) */ |
| 274 | if (ip->i_format.rt == 31) { |
| 275 | if (info->pc_offset != -1) |
Thiemo Seufer | dc953df | 2005-02-21 10:55:16 +0000 | [diff] [blame] | 276 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | info->pc_offset = |
| 278 | ip->i_format.simmediate / sizeof(long); |
| 279 | } |
| 280 | /* sw / sd $s8, offset($sp) */ |
| 281 | if (ip->i_format.rt == 30) { |
Thiemo Seufer | dc953df | 2005-02-21 10:55:16 +0000 | [diff] [blame] | 282 | //#if 0 /* gcc 3.4 does aggressive optimization... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | if (info->frame_offset != -1) |
Thiemo Seufer | dc953df | 2005-02-21 10:55:16 +0000 | [diff] [blame] | 284 | continue; |
| 285 | //#endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | info->frame_offset = |
| 287 | ip->i_format.simmediate / sizeof(long); |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | if (info->pc_offset == -1 || info->frame_offset == -1) { |
| 292 | printk("Can't analyze prologue code at %p\n", func); |
| 293 | info->pc_offset = -1; |
| 294 | info->frame_offset = -1; |
| 295 | return -1; |
| 296 | } |
| 297 | |
| 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | static int __init frame_info_init(void) |
| 302 | { |
Thiemo Seufer | dc953df | 2005-02-21 10:55:16 +0000 | [diff] [blame] | 303 | int i, found; |
| 304 | for (i = 0; i < ARRAY_SIZE(mfinfo); i++) |
| 305 | if (get_frame_info(&mfinfo[i])) |
| 306 | return -1; |
| 307 | schedule_frame = mfinfo[0]; |
| 308 | /* bubble sort */ |
| 309 | do { |
| 310 | struct mips_frame_info tmp; |
| 311 | found = 0; |
| 312 | for (i = 1; i < ARRAY_SIZE(mfinfo); i++) { |
| 313 | if (mfinfo[i-1].func > mfinfo[i].func) { |
| 314 | tmp = mfinfo[i]; |
| 315 | mfinfo[i] = mfinfo[i-1]; |
| 316 | mfinfo[i-1] = tmp; |
| 317 | found = 1; |
| 318 | } |
| 319 | } |
| 320 | } while (found); |
| 321 | mips_frame_info_initialized = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | return 0; |
| 323 | } |
| 324 | |
| 325 | arch_initcall(frame_info_init); |
| 326 | |
| 327 | /* |
| 328 | * Return saved PC of a blocked thread. |
| 329 | */ |
| 330 | unsigned long thread_saved_pc(struct task_struct *tsk) |
| 331 | { |
| 332 | struct thread_struct *t = &tsk->thread; |
| 333 | |
| 334 | /* New born processes are a special case */ |
| 335 | if (t->reg31 == (unsigned long) ret_from_fork) |
| 336 | return t->reg31; |
| 337 | |
| 338 | if (schedule_frame.pc_offset < 0) |
| 339 | return 0; |
| 340 | return ((unsigned long *)t->reg29)[schedule_frame.pc_offset]; |
| 341 | } |
| 342 | |
| 343 | /* get_wchan - a maintenance nightmare^W^Wpain in the ass ... */ |
| 344 | unsigned long get_wchan(struct task_struct *p) |
| 345 | { |
Thiemo Seufer | dc953df | 2005-02-21 10:55:16 +0000 | [diff] [blame] | 346 | unsigned long stack_page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | unsigned long frame, pc; |
| 348 | |
| 349 | if (!p || p == current || p->state == TASK_RUNNING) |
| 350 | return 0; |
| 351 | |
Thiemo Seufer | dc953df | 2005-02-21 10:55:16 +0000 | [diff] [blame] | 352 | stack_page = (unsigned long)p->thread_info; |
| 353 | if (!stack_page || !mips_frame_info_initialized) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | return 0; |
Thiemo Seufer | dc953df | 2005-02-21 10:55:16 +0000 | [diff] [blame] | 355 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | pc = thread_saved_pc(p); |
| 357 | if (!in_sched_functions(pc)) |
Thiemo Seufer | dc953df | 2005-02-21 10:55:16 +0000 | [diff] [blame] | 358 | return pc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | frame = ((unsigned long *)p->thread.reg30)[schedule_frame.frame_offset]; |
Thiemo Seufer | dc953df | 2005-02-21 10:55:16 +0000 | [diff] [blame] | 361 | do { |
| 362 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | |
Thiemo Seufer | dc953df | 2005-02-21 10:55:16 +0000 | [diff] [blame] | 364 | if (frame < stack_page || frame > stack_page + THREAD_SIZE - 32) |
| 365 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | |
Thiemo Seufer | dc953df | 2005-02-21 10:55:16 +0000 | [diff] [blame] | 367 | for (i = ARRAY_SIZE(mfinfo) - 1; i >= 0; i--) { |
| 368 | if (pc >= (unsigned long) mfinfo[i].func) |
| 369 | break; |
| 370 | } |
| 371 | if (i < 0) |
| 372 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | |
Thiemo Seufer | dc953df | 2005-02-21 10:55:16 +0000 | [diff] [blame] | 374 | if (mfinfo[i].omit_fp) |
| 375 | break; |
| 376 | pc = ((unsigned long *)frame)[mfinfo[i].pc_offset]; |
| 377 | frame = ((unsigned long *)frame)[mfinfo[i].frame_offset]; |
| 378 | } while (in_sched_functions(pc)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | |
| 380 | return pc; |
| 381 | } |
| 382 | |
| 383 | EXPORT_SYMBOL(get_wchan); |