blob: c7ea7f2a894516bc787286fe6adc8bdca981b8a6 [file] [log] [blame]
Jeff Dike995473a2006-09-27 01:50:40 -07001/*
Jeff Dikeba180fd2007-10-16 01:27:00 -07002 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Copyright 2003 PathScale, Inc.
4 * Licensed under the GPL
5 */
6
Jeff Dikeba180fd2007-10-16 01:27:00 -07007#include "linux/stddef.h"
8#include "linux/err.h"
9#include "linux/hardirq.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include "linux/mm.h"
Jeff Dikeba180fd2007-10-16 01:27:00 -070011#include "linux/personality.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include "linux/proc_fs.h"
13#include "linux/ptrace.h"
14#include "linux/random.h"
Jeff Dikeba180fd2007-10-16 01:27:00 -070015#include "linux/sched.h"
Jeff Diked2753a62007-10-16 01:27:25 -070016#include "linux/tick.h"
Jeff Dikeba180fd2007-10-16 01:27:00 -070017#include "linux/threads.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "asm/pgtable.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "asm/uaccess.h"
Jeff Dike4ff83ce2007-05-06 14:51:08 -070020#include "as-layout.h"
Jeff Dikeba180fd2007-10-16 01:27:00 -070021#include "kern_util.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "os.h"
Jeff Dike77bf4402007-10-16 01:26:58 -070023#include "skas.h"
Jeff Dikeba180fd2007-10-16 01:27:00 -070024#include "tlb.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Jeff Dikeba180fd2007-10-16 01:27:00 -070026/*
27 * This is a per-cpu array. A processor only modifies its entry and it only
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 * cares about its entry, so it's OK if another processor is modifying its
29 * entry.
30 */
31struct cpu_task cpu_tasks[NR_CPUS] = { [0 ... NR_CPUS - 1] = { -1, NULL } };
32
Jeff Dike6e21aec2007-05-06 14:51:21 -070033static inline int external_pid(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
Jeff Dike77bf4402007-10-16 01:26:58 -070035 /* FIXME: Need to look up userspace_pid by cpu */
Jeff Dikeba180fd2007-10-16 01:27:00 -070036 return userspace_pid[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -070037}
38
39int pid_to_processor_id(int pid)
40{
41 int i;
42
Jeff Dikeba180fd2007-10-16 01:27:00 -070043 for(i = 0; i < ncpus; i++) {
44 if (cpu_tasks[i].pid == pid)
Jeff Dike6e21aec2007-05-06 14:51:21 -070045 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 }
Jeff Dike6e21aec2007-05-06 14:51:21 -070047 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048}
49
50void free_stack(unsigned long stack, int order)
51{
52 free_pages(stack, order);
53}
54
55unsigned long alloc_stack(int order, int atomic)
56{
57 unsigned long page;
Al Viro53f9fc92005-10-21 03:22:24 -040058 gfp_t flags = GFP_KERNEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Paolo 'Blaisorblade' Giarrusso46db4a42005-09-22 21:44:20 -070060 if (atomic)
61 flags = GFP_ATOMIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 page = __get_free_pages(flags, order);
Jeff Dike5c8aace2007-10-16 01:26:46 -070063 if (page == 0)
Jeff Dike6e21aec2007-05-06 14:51:21 -070064 return 0;
Jeff Dike5c8aace2007-10-16 01:26:46 -070065
Jeff Dike6e21aec2007-05-06 14:51:21 -070066 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067}
68
69int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
70{
71 int pid;
72
73 current->thread.request.u.thread.proc = fn;
74 current->thread.request.u.thread.arg = arg;
Jeff Dikee0877f02005-06-25 14:55:21 -070075 pid = do_fork(CLONE_VM | CLONE_UNTRACED | flags, 0,
76 &current->thread.regs, 0, NULL, NULL);
Jeff Dike6e21aec2007-05-06 14:51:21 -070077 return pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79
Jeff Dike6e21aec2007-05-06 14:51:21 -070080static inline void set_current(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
Al Viroca9bc0b2006-01-12 01:05:48 -080082 cpu_tasks[task_thread_info(task)->cpu] = ((struct cpu_task)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 { external_pid(task), task });
84}
85
Jeff Dike77bf4402007-10-16 01:26:58 -070086extern void arch_switch_to(struct task_struct *from, struct task_struct *to);
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088void *_switch_to(void *prev, void *next, void *last)
89{
Jeff Dike995473a2006-09-27 01:50:40 -070090 struct task_struct *from = prev;
91 struct task_struct *to= next;
Jeff Dikef6e34c62005-09-16 19:27:43 -070092
Jeff Dike995473a2006-09-27 01:50:40 -070093 to->thread.prev_sched = from;
94 set_current(to);
Jeff Dikef6e34c62005-09-16 19:27:43 -070095
Jeff Dike3eddddc2005-09-16 19:27:46 -070096 do {
Jeff Dike6aa802c2007-10-16 01:26:56 -070097 current->thread.saved_task = NULL;
Jeff Dike77bf4402007-10-16 01:26:58 -070098
Jeff Dike77bf4402007-10-16 01:26:58 -070099 switch_threads(&from->thread.switch_buf,
100 &to->thread.switch_buf);
101
102 arch_switch_to(current->thread.prev_sched, current);
103
Jeff Dikeba180fd2007-10-16 01:27:00 -0700104 if (current->thread.saved_task)
Jeff Dike3eddddc2005-09-16 19:27:46 -0700105 show_regs(&(current->thread.regs));
106 next= current->thread.saved_task;
107 prev= current;
108 } while(current->thread.saved_task);
Jeff Dikef6e34c62005-09-16 19:27:43 -0700109
Jeff Dike6e21aec2007-05-06 14:51:21 -0700110 return current->thread.prev_sched;
Jeff Dikef6e34c62005-09-16 19:27:43 -0700111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
114void interrupt_end(void)
115{
Jeff Dikeba180fd2007-10-16 01:27:00 -0700116 if (need_resched())
Jeff Dike6e21aec2007-05-06 14:51:21 -0700117 schedule();
Jeff Dikeba180fd2007-10-16 01:27:00 -0700118 if (test_tsk_thread_flag(current, TIF_SIGPENDING))
Jeff Dike6e21aec2007-05-06 14:51:21 -0700119 do_signal();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122void exit_thread(void)
123{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
Jeff Dike995473a2006-09-27 01:50:40 -0700125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126void *get_current(void)
127{
Jeff Dike6e21aec2007-05-06 14:51:21 -0700128 return current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
Jeff Dike77bf4402007-10-16 01:26:58 -0700131extern void schedule_tail(struct task_struct *prev);
132
Jeff Dikeba180fd2007-10-16 01:27:00 -0700133/*
134 * This is called magically, by its address being stuffed in a jmp_buf
Jeff Dike77bf4402007-10-16 01:26:58 -0700135 * and being longjmp-d to.
136 */
137void new_thread_handler(void)
138{
139 int (*fn)(void *), n;
140 void *arg;
141
Jeff Dikeba180fd2007-10-16 01:27:00 -0700142 if (current->thread.prev_sched != NULL)
Jeff Dike77bf4402007-10-16 01:26:58 -0700143 schedule_tail(current->thread.prev_sched);
144 current->thread.prev_sched = NULL;
145
146 fn = current->thread.request.u.thread.proc;
147 arg = current->thread.request.u.thread.arg;
148
Jeff Dikeba180fd2007-10-16 01:27:00 -0700149 /*
150 * The return value is 1 if the kernel thread execs a process,
Jeff Dike77bf4402007-10-16 01:26:58 -0700151 * 0 if it just exits
152 */
153 n = run_kernel_thread(fn, arg, &current->thread.exec_buf);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700154 if (n == 1) {
Jeff Dike77bf4402007-10-16 01:26:58 -0700155 /* Handle any immediate reschedules or signals */
156 interrupt_end();
157 userspace(&current->thread.regs.regs);
158 }
159 else do_exit(0);
160}
161
162/* Called magically, see new_thread_handler above */
163void fork_handler(void)
164{
165 force_flush_all();
Jeff Dikeba180fd2007-10-16 01:27:00 -0700166 if (current->thread.prev_sched == NULL)
Jeff Dike77bf4402007-10-16 01:26:58 -0700167 panic("blech");
168
169 schedule_tail(current->thread.prev_sched);
170
Jeff Dikeba180fd2007-10-16 01:27:00 -0700171 /*
172 * XXX: if interrupt_end() calls schedule, this call to
Jeff Dike77bf4402007-10-16 01:26:58 -0700173 * arch_switch_to isn't needed. We could want to apply this to
Jeff Dikeba180fd2007-10-16 01:27:00 -0700174 * improve performance. -bb
175 */
Jeff Dike77bf4402007-10-16 01:26:58 -0700176 arch_switch_to(current->thread.prev_sched, current);
177
178 current->thread.prev_sched = NULL;
179
180 /* Handle any immediate reschedules or signals */
181 interrupt_end();
182
183 userspace(&current->thread.regs.regs);
184}
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186int copy_thread(int nr, unsigned long clone_flags, unsigned long sp,
Jeff Dike995473a2006-09-27 01:50:40 -0700187 unsigned long stack_top, struct task_struct * p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 struct pt_regs *regs)
189{
Jeff Dike77bf4402007-10-16 01:26:58 -0700190 void (*handler)(void);
191 int ret = 0;
Paolo 'Blaisorblade' Giarrussoaa6758d2006-03-31 02:30:22 -0800192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 p->thread = (struct thread_struct) INIT_THREAD;
Paolo 'Blaisorblade' Giarrussoaa6758d2006-03-31 02:30:22 -0800194
Jeff Dikeba180fd2007-10-16 01:27:00 -0700195 if (current->thread.forking) {
Jeff Dike77bf4402007-10-16 01:26:58 -0700196 memcpy(&p->thread.regs.regs, &regs->regs,
197 sizeof(p->thread.regs.regs));
Jeff Dike18baddd2007-10-16 01:27:07 -0700198 REGS_SET_SYSCALL_RETURN(p->thread.regs.regs.gp, 0);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700199 if (sp != 0)
Jeff Dike18baddd2007-10-16 01:27:07 -0700200 REGS_SP(p->thread.regs.regs.gp) = sp;
Paolo 'Blaisorblade' Giarrussoaa6758d2006-03-31 02:30:22 -0800201
Jeff Dike77bf4402007-10-16 01:26:58 -0700202 handler = fork_handler;
Paolo 'Blaisorblade' Giarrussoaa6758d2006-03-31 02:30:22 -0800203
Jeff Dike77bf4402007-10-16 01:26:58 -0700204 arch_copy_thread(&current->thread.arch, &p->thread.arch);
205 }
206 else {
207 init_thread_registers(&p->thread.regs.regs);
208 p->thread.request.u.thread = current->thread.request.u.thread;
209 handler = new_thread_handler;
210 }
Paolo 'Blaisorblade' Giarrussoaa6758d2006-03-31 02:30:22 -0800211
Jeff Dike77bf4402007-10-16 01:26:58 -0700212 new_thread(task_stack_page(p), &p->thread.switch_buf, handler);
213
214 if (current->thread.forking) {
215 clear_flushed_tls(p);
216
217 /*
218 * Set a new TLS for the child thread?
219 */
220 if (clone_flags & CLONE_SETTLS)
221 ret = arch_copy_tls(p);
222 }
223
Paolo 'Blaisorblade' Giarrussoaa6758d2006-03-31 02:30:22 -0800224 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
227void initial_thread_cb(void (*proc)(void *), void *arg)
228{
229 int save_kmalloc_ok = kmalloc_ok;
230
231 kmalloc_ok = 0;
Jeff Dike6aa802c2007-10-16 01:26:56 -0700232 initial_thread_cb_skas(proc, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 kmalloc_ok = save_kmalloc_ok;
234}
Jeff Dike995473a2006-09-27 01:50:40 -0700235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236void default_idle(void)
237{
Jeff Dikeb160fb632007-10-16 01:27:26 -0700238 unsigned long long nsecs;
239
Jeff Dikeba180fd2007-10-16 01:27:00 -0700240 while(1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 /* endless idle loop with no priority at all */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 /*
244 * although we are an idle CPU, we do not want to
245 * get into the scheduler unnecessarily.
246 */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700247 if (need_resched())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 schedule();
Jeff Dike995473a2006-09-27 01:50:40 -0700249
Jeff Diked2753a62007-10-16 01:27:25 -0700250 tick_nohz_stop_sched_tick();
Jeff Dikeb160fb632007-10-16 01:27:26 -0700251 nsecs = disable_timer();
252 idle_sleep(nsecs);
Jeff Diked2753a62007-10-16 01:27:25 -0700253 tick_nohz_restart_sched_tick();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 }
255}
256
257void cpu_idle(void)
258{
Jeff Dike77bf4402007-10-16 01:26:58 -0700259 cpu_tasks[current_thread->cpu].pid = os_getpid();
260 default_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
Jeff Dike995473a2006-09-27 01:50:40 -0700263void *um_virt_to_phys(struct task_struct *task, unsigned long addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 pte_t *pte_out)
265{
266 pgd_t *pgd;
267 pud_t *pud;
268 pmd_t *pmd;
269 pte_t *pte;
Hugh Dickins8f5cd762005-10-29 18:16:38 -0700270 pte_t ptent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Jeff Dikeba180fd2007-10-16 01:27:00 -0700272 if (task->mm == NULL)
Jeff Dike6e21aec2007-05-06 14:51:21 -0700273 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 pgd = pgd_offset(task->mm, addr);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700275 if (!pgd_present(*pgd))
Jeff Dike6e21aec2007-05-06 14:51:21 -0700276 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 pud = pud_offset(pgd, addr);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700279 if (!pud_present(*pud))
Jeff Dike6e21aec2007-05-06 14:51:21 -0700280 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 pmd = pmd_offset(pud, addr);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700283 if (!pmd_present(*pmd))
Jeff Dike6e21aec2007-05-06 14:51:21 -0700284 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 pte = pte_offset_kernel(pmd, addr);
Hugh Dickins8f5cd762005-10-29 18:16:38 -0700287 ptent = *pte;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700288 if (!pte_present(ptent))
Jeff Dike6e21aec2007-05-06 14:51:21 -0700289 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Jeff Dikeba180fd2007-10-16 01:27:00 -0700291 if (pte_out != NULL)
Hugh Dickins8f5cd762005-10-29 18:16:38 -0700292 *pte_out = ptent;
Jeff Dike6e21aec2007-05-06 14:51:21 -0700293 return (void *) (pte_val(ptent) & PAGE_MASK) + (addr & ~PAGE_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
296char *current_cmd(void)
297{
298#if defined(CONFIG_SMP) || defined(CONFIG_HIGHMEM)
Jeff Dike6e21aec2007-05-06 14:51:21 -0700299 return "(Unknown)";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300#else
301 void *addr = um_virt_to_phys(current, current->mm->arg_start, NULL);
302 return IS_ERR(addr) ? "(Unknown)": __va((unsigned long) addr);
303#endif
304}
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306void dump_thread(struct pt_regs *regs, struct user *u)
307{
308}
309
Paolo 'Blaisorblade' Giarrussob6316292006-01-18 17:42:58 -0800310int __cant_sleep(void) {
311 return in_atomic() || irqs_disabled() || in_interrupt();
312 /* Is in_interrupt() really needed? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315int user_context(unsigned long sp)
316{
317 unsigned long stack;
318
319 stack = sp & (PAGE_MASK << CONFIG_KERNEL_STACK_ORDER);
Jeff Dike6e21aec2007-05-06 14:51:21 -0700320 return stack != (unsigned long) current_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321}
322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323extern exitcall_t __uml_exitcall_begin, __uml_exitcall_end;
324
325void do_uml_exitcalls(void)
326{
327 exitcall_t *call;
328
329 call = &__uml_exitcall_end;
330 while (--call >= &__uml_exitcall_begin)
331 (*call)();
332}
333
334char *uml_strdup(char *string)
335{
Robert Lovedfe52242005-06-23 00:09:04 -0700336 return kstrdup(string, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339int copy_to_user_proc(void __user *to, void *from, int size)
340{
Jeff Dike6e21aec2007-05-06 14:51:21 -0700341 return copy_to_user(to, from, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
343
344int copy_from_user_proc(void *to, void __user *from, int size)
345{
Jeff Dike6e21aec2007-05-06 14:51:21 -0700346 return copy_from_user(to, from, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
348
349int clear_user_proc(void __user *buf, int size)
350{
Jeff Dike6e21aec2007-05-06 14:51:21 -0700351 return clear_user(buf, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352}
353
354int strlen_user_proc(char __user *str)
355{
Jeff Dike6e21aec2007-05-06 14:51:21 -0700356 return strlen_user(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
358
359int smp_sigio_handler(void)
360{
361#ifdef CONFIG_SMP
362 int cpu = current_thread->cpu;
363 IPI_handler(cpu);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700364 if (cpu != 0)
Jeff Dike6e21aec2007-05-06 14:51:21 -0700365 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366#endif
Jeff Dike6e21aec2007-05-06 14:51:21 -0700367 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368}
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370int cpu(void)
371{
Jeff Dike6e21aec2007-05-06 14:51:21 -0700372 return current_thread->cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374
375static atomic_t using_sysemu = ATOMIC_INIT(0);
376int sysemu_supported;
377
378void set_using_sysemu(int value)
379{
380 if (value > sysemu_supported)
381 return;
382 atomic_set(&using_sysemu, value);
383}
384
385int get_using_sysemu(void)
386{
387 return atomic_read(&using_sysemu);
388}
389
390static int proc_read_sysemu(char *buf, char **start, off_t offset, int size,int *eof, void *data)
391{
Jeff Dikeba180fd2007-10-16 01:27:00 -0700392 if (snprintf(buf, size, "%d\n", get_using_sysemu()) < size)
393 /* No overflow */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 *eof = 1;
395
396 return strlen(buf);
397}
398
Al Viro4d338e12006-03-31 02:30:15 -0800399static int proc_write_sysemu(struct file *file,const char __user *buf, unsigned long count,void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
401 char tmp[2];
402
403 if (copy_from_user(tmp, buf, 1))
404 return -EFAULT;
405
406 if (tmp[0] >= '0' && tmp[0] <= '2')
407 set_using_sysemu(tmp[0] - '0');
Jeff Dikeba180fd2007-10-16 01:27:00 -0700408 /* We use the first char, but pretend to write everything */
409 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410}
411
412int __init make_proc_sysemu(void)
413{
414 struct proc_dir_entry *ent;
415 if (!sysemu_supported)
416 return 0;
417
418 ent = create_proc_entry("sysemu", 0600, &proc_root);
419
420 if (ent == NULL)
421 {
Christophe Lucas30f417c2005-07-28 21:16:12 -0700422 printk(KERN_WARNING "Failed to register /proc/sysemu\n");
Jeff Dike6e21aec2007-05-06 14:51:21 -0700423 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
425
426 ent->read_proc = proc_read_sysemu;
427 ent->write_proc = proc_write_sysemu;
428
429 return 0;
430}
431
432late_initcall(make_proc_sysemu);
433
434int singlestepping(void * t)
435{
436 struct task_struct *task = t ? t : current;
437
438 if ( ! (task->ptrace & PT_DTRACE) )
Jeff Dikeba180fd2007-10-16 01:27:00 -0700439 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 if (task->thread.singlestep_syscall)
Jeff Dikeba180fd2007-10-16 01:27:00 -0700442 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 return 2;
445}
446
Bodo Stroesserb8bd0222005-05-06 21:30:53 -0700447/*
448 * Only x86 and x86_64 have an arch_align_stack().
449 * All other arches have "#define arch_align_stack(x) (x)"
450 * in their asm/system.h
451 * As this is included in UML from asm-um/system-generic.h,
452 * we can use it to behave as the subarch does.
453 */
454#ifndef arch_align_stack
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455unsigned long arch_align_stack(unsigned long sp)
456{
Jeff Dike8f80e942006-09-25 23:33:01 -0700457 if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 sp -= get_random_int() % 8192;
459 return sp & ~0xf;
460}
Bodo Stroesserb8bd0222005-05-06 21:30:53 -0700461#endif
Jeff Dikec1127462008-02-04 22:30:36 -0800462
463unsigned long get_wchan(struct task_struct *p)
464{
465 unsigned long stack_page, sp, ip;
466 bool seen_sched = 0;
467
468 if ((p == NULL) || (p == current) || (p->state == TASK_RUNNING))
469 return 0;
470
471 stack_page = (unsigned long) task_stack_page(p);
472 /* Bail if the process has no kernel stack for some reason */
473 if (stack_page == 0)
474 return 0;
475
476 sp = p->thread.switch_buf->JB_SP;
477 /*
478 * Bail if the stack pointer is below the bottom of the kernel
479 * stack for some reason
480 */
481 if (sp < stack_page)
482 return 0;
483
484 while (sp < stack_page + THREAD_SIZE) {
485 ip = *((unsigned long *) sp);
486 if (in_sched_functions(ip))
487 /* Ignore everything until we're above the scheduler */
488 seen_sched = 1;
489 else if (kernel_text_address(ip) && seen_sched)
490 return ip;
491
492 sp += sizeof(unsigned long);
493 }
494
495 return 0;
496}