David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 1 | /* MN10300 Process handling code |
| 2 | * |
| 3 | * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved. |
| 4 | * Written by David Howells (dhowells@redhat.com) |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public Licence |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the Licence, or (at your option) any later version. |
| 10 | */ |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/errno.h> |
| 13 | #include <linux/sched.h> |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/mm.h> |
| 16 | #include <linux/smp.h> |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 17 | #include <linux/stddef.h> |
| 18 | #include <linux/unistd.h> |
| 19 | #include <linux/ptrace.h> |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 20 | #include <linux/user.h> |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 21 | #include <linux/interrupt.h> |
| 22 | #include <linux/delay.h> |
| 23 | #include <linux/reboot.h> |
| 24 | #include <linux/percpu.h> |
| 25 | #include <linux/err.h> |
| 26 | #include <linux/fs.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 27 | #include <linux/slab.h> |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 28 | #include <asm/uaccess.h> |
| 29 | #include <asm/pgtable.h> |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 30 | #include <asm/io.h> |
| 31 | #include <asm/processor.h> |
| 32 | #include <asm/mmu_context.h> |
| 33 | #include <asm/fpu.h> |
| 34 | #include <asm/reset-regs.h> |
| 35 | #include <asm/gdb-stub.h> |
| 36 | #include "internal.h" |
| 37 | |
| 38 | /* |
| 39 | * power management idle function, if any.. |
| 40 | */ |
| 41 | void (*pm_idle)(void); |
| 42 | EXPORT_SYMBOL(pm_idle); |
| 43 | |
| 44 | /* |
| 45 | * return saved PC of a blocked thread. |
| 46 | */ |
| 47 | unsigned long thread_saved_pc(struct task_struct *tsk) |
| 48 | { |
| 49 | return ((unsigned long *) tsk->thread.sp)[3]; |
| 50 | } |
| 51 | |
| 52 | /* |
| 53 | * power off function, if any |
| 54 | */ |
| 55 | void (*pm_power_off)(void); |
| 56 | EXPORT_SYMBOL(pm_power_off); |
| 57 | |
Akira Takeuchi | 368dd5a | 2010-10-27 17:28:55 +0100 | [diff] [blame] | 58 | #if !defined(CONFIG_SMP) || defined(CONFIG_HOTPLUG_CPU) |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 59 | /* |
| 60 | * we use this if we don't have any better idle routine |
| 61 | */ |
| 62 | static void default_idle(void) |
| 63 | { |
| 64 | local_irq_disable(); |
| 65 | if (!need_resched()) |
| 66 | safe_halt(); |
| 67 | else |
| 68 | local_irq_enable(); |
| 69 | } |
| 70 | |
Akira Takeuchi | 368dd5a | 2010-10-27 17:28:55 +0100 | [diff] [blame] | 71 | #else /* !CONFIG_SMP || CONFIG_HOTPLUG_CPU */ |
| 72 | /* |
| 73 | * On SMP it's slightly faster (but much more power-consuming!) |
| 74 | * to poll the ->work.need_resched flag instead of waiting for the |
| 75 | * cross-CPU IPI to arrive. Use this option with caution. |
| 76 | */ |
| 77 | static inline void poll_idle(void) |
| 78 | { |
| 79 | int oldval; |
| 80 | |
| 81 | local_irq_enable(); |
| 82 | |
| 83 | /* |
| 84 | * Deal with another CPU just having chosen a thread to |
| 85 | * run here: |
| 86 | */ |
| 87 | oldval = test_and_clear_thread_flag(TIF_NEED_RESCHED); |
| 88 | |
| 89 | if (!oldval) { |
| 90 | set_thread_flag(TIF_POLLING_NRFLAG); |
| 91 | while (!need_resched()) |
| 92 | cpu_relax(); |
| 93 | clear_thread_flag(TIF_POLLING_NRFLAG); |
| 94 | } else { |
| 95 | set_need_resched(); |
| 96 | } |
| 97 | } |
| 98 | #endif /* !CONFIG_SMP || CONFIG_HOTPLUG_CPU */ |
| 99 | |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 100 | /* |
| 101 | * the idle thread |
| 102 | * - there's no useful work to be done, so just try to conserve power and have |
| 103 | * a low exit latency (ie sit in a loop waiting for somebody to say that |
| 104 | * they'd like to reschedule) |
| 105 | */ |
| 106 | void cpu_idle(void) |
| 107 | { |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 108 | /* endless idle loop with no priority at all */ |
| 109 | for (;;) { |
| 110 | while (!need_resched()) { |
| 111 | void (*idle)(void); |
| 112 | |
| 113 | smp_rmb(); |
| 114 | idle = pm_idle; |
Akira Takeuchi | 368dd5a | 2010-10-27 17:28:55 +0100 | [diff] [blame] | 115 | if (!idle) { |
| 116 | #if defined(CONFIG_SMP) && !defined(CONFIG_HOTPLUG_CPU) |
| 117 | idle = poll_idle; |
| 118 | #else /* CONFIG_SMP && !CONFIG_HOTPLUG_CPU */ |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 119 | idle = default_idle; |
Akira Takeuchi | 368dd5a | 2010-10-27 17:28:55 +0100 | [diff] [blame] | 120 | #endif /* CONFIG_SMP && !CONFIG_HOTPLUG_CPU */ |
| 121 | } |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 122 | idle(); |
| 123 | } |
| 124 | |
Thomas Gleixner | bd2f553 | 2011-03-21 12:33:18 +0100 | [diff] [blame] | 125 | schedule_preempt_disabled(); |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
| 129 | void release_segments(struct mm_struct *mm) |
| 130 | { |
| 131 | } |
| 132 | |
| 133 | void machine_restart(char *cmd) |
| 134 | { |
David Howells | 044264b | 2011-03-18 16:54:31 +0000 | [diff] [blame] | 135 | #ifdef CONFIG_KERNEL_DEBUGGER |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 136 | gdbstub_exit(0); |
| 137 | #endif |
| 138 | |
| 139 | #ifdef mn10300_unit_hard_reset |
| 140 | mn10300_unit_hard_reset(); |
| 141 | #else |
| 142 | mn10300_proc_hard_reset(); |
| 143 | #endif |
| 144 | } |
| 145 | |
| 146 | void machine_halt(void) |
| 147 | { |
David Howells | 044264b | 2011-03-18 16:54:31 +0000 | [diff] [blame] | 148 | #ifdef CONFIG_KERNEL_DEBUGGER |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 149 | gdbstub_exit(0); |
| 150 | #endif |
| 151 | } |
| 152 | |
| 153 | void machine_power_off(void) |
| 154 | { |
David Howells | 044264b | 2011-03-18 16:54:31 +0000 | [diff] [blame] | 155 | #ifdef CONFIG_KERNEL_DEBUGGER |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 156 | gdbstub_exit(0); |
| 157 | #endif |
| 158 | } |
| 159 | |
| 160 | void show_regs(struct pt_regs *regs) |
| 161 | { |
| 162 | } |
| 163 | |
| 164 | /* |
| 165 | * create a kernel thread |
| 166 | */ |
| 167 | int kernel_thread(int (*fn)(void *), void *arg, unsigned long flags) |
| 168 | { |
| 169 | struct pt_regs regs; |
| 170 | |
| 171 | memset(®s, 0, sizeof(regs)); |
| 172 | |
| 173 | regs.a2 = (unsigned long) fn; |
| 174 | regs.d2 = (unsigned long) arg; |
| 175 | regs.pc = (unsigned long) kernel_thread_helper; |
| 176 | local_save_flags(regs.epsw); |
| 177 | regs.epsw |= EPSW_IE | EPSW_IM_7; |
| 178 | |
| 179 | /* Ok, create the new process.. */ |
| 180 | return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, ®s, 0, |
| 181 | NULL, NULL); |
| 182 | } |
David Howells | 7fc7228 | 2008-07-04 09:59:46 -0700 | [diff] [blame] | 183 | EXPORT_SYMBOL(kernel_thread); |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 184 | |
| 185 | /* |
| 186 | * free current thread data structures etc.. |
| 187 | */ |
| 188 | void exit_thread(void) |
| 189 | { |
| 190 | exit_fpu(); |
| 191 | } |
| 192 | |
| 193 | void flush_thread(void) |
| 194 | { |
| 195 | flush_fpu(); |
| 196 | } |
| 197 | |
| 198 | void release_thread(struct task_struct *dead_task) |
| 199 | { |
| 200 | } |
| 201 | |
| 202 | /* |
| 203 | * we do not have to muck with descriptors here, that is |
| 204 | * done in switch_mm() as needed. |
| 205 | */ |
| 206 | void copy_segments(struct task_struct *p, struct mm_struct *new_mm) |
| 207 | { |
| 208 | } |
| 209 | |
| 210 | /* |
| 211 | * this gets called before we allocate a new thread and copy the current task |
| 212 | * into it so that we can store lazy state into memory |
| 213 | */ |
| 214 | void prepare_to_copy(struct task_struct *tsk) |
| 215 | { |
| 216 | unlazy_fpu(tsk); |
| 217 | } |
| 218 | |
| 219 | /* |
| 220 | * set up the kernel stack for a new thread and copy arch-specific thread |
| 221 | * control information |
| 222 | */ |
Alexey Dobriyan | 6f2c55b | 2009-04-02 16:56:59 -0700 | [diff] [blame] | 223 | int copy_thread(unsigned long clone_flags, |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 224 | unsigned long c_usp, unsigned long ustk_size, |
| 225 | struct task_struct *p, struct pt_regs *kregs) |
| 226 | { |
David Howells | 7c7fcf7 | 2010-10-27 17:29:01 +0100 | [diff] [blame] | 227 | struct thread_info *ti = task_thread_info(p); |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 228 | struct pt_regs *c_uregs, *c_kregs, *uregs; |
| 229 | unsigned long c_ksp; |
| 230 | |
| 231 | uregs = current->thread.uregs; |
| 232 | |
| 233 | c_ksp = (unsigned long) task_stack_page(p) + THREAD_SIZE; |
| 234 | |
| 235 | /* allocate the userspace exception frame and set it up */ |
| 236 | c_ksp -= sizeof(struct pt_regs); |
| 237 | c_uregs = (struct pt_regs *) c_ksp; |
| 238 | |
| 239 | p->thread.uregs = c_uregs; |
| 240 | *c_uregs = *uregs; |
| 241 | c_uregs->sp = c_usp; |
| 242 | c_uregs->epsw &= ~EPSW_FE; /* my FPU */ |
| 243 | |
| 244 | c_ksp -= 12; /* allocate function call ABI slack */ |
| 245 | |
| 246 | /* the new TLS pointer is passed in as arg #5 to sys_clone() */ |
| 247 | if (clone_flags & CLONE_SETTLS) |
David Howells | 7c7fcf7 | 2010-10-27 17:29:01 +0100 | [diff] [blame] | 248 | c_uregs->e2 = current_frame()->d3; |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 249 | |
| 250 | /* set up the return kernel frame if called from kernel_thread() */ |
| 251 | c_kregs = c_uregs; |
| 252 | if (kregs != uregs) { |
| 253 | c_ksp -= sizeof(struct pt_regs); |
| 254 | c_kregs = (struct pt_regs *) c_ksp; |
| 255 | *c_kregs = *kregs; |
| 256 | c_kregs->sp = c_usp; |
| 257 | c_kregs->next = c_uregs; |
| 258 | #ifdef CONFIG_MN10300_CURRENT_IN_E2 |
| 259 | c_kregs->e2 = (unsigned long) p; /* current */ |
| 260 | #endif |
| 261 | |
| 262 | c_ksp -= 12; /* allocate function call ABI slack */ |
| 263 | } |
| 264 | |
| 265 | /* set up things up so the scheduler can start the new task */ |
David Howells | 7c7fcf7 | 2010-10-27 17:29:01 +0100 | [diff] [blame] | 266 | ti->frame = c_kregs; |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 267 | p->thread.a3 = (unsigned long) c_kregs; |
| 268 | p->thread.sp = c_ksp; |
| 269 | p->thread.pc = (unsigned long) ret_from_fork; |
| 270 | p->thread.wchan = (unsigned long) ret_from_fork; |
| 271 | p->thread.usp = c_usp; |
| 272 | |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | /* |
| 277 | * clone a process |
David Howells | 7c7fcf7 | 2010-10-27 17:29:01 +0100 | [diff] [blame] | 278 | * - tlsptr is retrieved by copy_thread() from current_frame()->d3 |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 279 | */ |
| 280 | asmlinkage long sys_clone(unsigned long clone_flags, unsigned long newsp, |
| 281 | int __user *parent_tidptr, int __user *child_tidptr, |
| 282 | int __user *tlsptr) |
| 283 | { |
David Howells | 7c7fcf7 | 2010-10-27 17:29:01 +0100 | [diff] [blame] | 284 | return do_fork(clone_flags, newsp ?: current_frame()->sp, |
| 285 | current_frame(), 0, parent_tidptr, child_tidptr); |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | asmlinkage long sys_fork(void) |
| 289 | { |
David Howells | 7c7fcf7 | 2010-10-27 17:29:01 +0100 | [diff] [blame] | 290 | return do_fork(SIGCHLD, current_frame()->sp, |
| 291 | current_frame(), 0, NULL, NULL); |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | asmlinkage long sys_vfork(void) |
| 295 | { |
David Howells | 7c7fcf7 | 2010-10-27 17:29:01 +0100 | [diff] [blame] | 296 | return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, current_frame()->sp, |
| 297 | current_frame(), 0, NULL, NULL); |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 298 | } |
| 299 | |
David Howells | c788732 | 2010-08-11 11:26:22 +0100 | [diff] [blame] | 300 | asmlinkage long sys_execve(const char __user *name, |
David Howells | d762746 | 2010-08-17 23:52:56 +0100 | [diff] [blame] | 301 | const char __user *const __user *argv, |
| 302 | const char __user *const __user *envp) |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 303 | { |
| 304 | char *filename; |
| 305 | int error; |
| 306 | |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 307 | filename = getname(name); |
| 308 | error = PTR_ERR(filename); |
John Kacur | 8c0daee | 2009-10-12 23:41:55 +0200 | [diff] [blame] | 309 | if (IS_ERR(filename)) |
| 310 | return error; |
David Howells | 7c7fcf7 | 2010-10-27 17:29:01 +0100 | [diff] [blame] | 311 | error = do_execve(filename, argv, envp, current_frame()); |
John Kacur | 8c0daee | 2009-10-12 23:41:55 +0200 | [diff] [blame] | 312 | putname(filename); |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 313 | return error; |
| 314 | } |
| 315 | |
| 316 | unsigned long get_wchan(struct task_struct *p) |
| 317 | { |
| 318 | return p->thread.wchan; |
| 319 | } |