blob: 91bd68eaba2000ed8a1ef4553d0a2a5a8a09382f [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
Jeff Dike6e21aec2007-05-06 14:51:21 -070064 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065}
66
67int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
68{
69 int pid;
70
71 current->thread.request.u.thread.proc = fn;
72 current->thread.request.u.thread.arg = arg;
Jeff Dikee0877f02005-06-25 14:55:21 -070073 pid = do_fork(CLONE_VM | CLONE_UNTRACED | flags, 0,
74 &current->thread.regs, 0, NULL, NULL);
Jeff Dike6e21aec2007-05-06 14:51:21 -070075 return pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076}
77
Jeff Dike6e21aec2007-05-06 14:51:21 -070078static inline void set_current(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Al Viroca9bc0b2006-01-12 01:05:48 -080080 cpu_tasks[task_thread_info(task)->cpu] = ((struct cpu_task)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 { external_pid(task), task });
82}
83
Jeff Dike77bf4402007-10-16 01:26:58 -070084extern void arch_switch_to(struct task_struct *from, struct task_struct *to);
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086void *_switch_to(void *prev, void *next, void *last)
87{
Jeff Dike995473a2006-09-27 01:50:40 -070088 struct task_struct *from = prev;
89 struct task_struct *to= next;
Jeff Dikef6e34c62005-09-16 19:27:43 -070090
Jeff Dike995473a2006-09-27 01:50:40 -070091 to->thread.prev_sched = from;
92 set_current(to);
Jeff Dikef6e34c62005-09-16 19:27:43 -070093
Jeff Dike3eddddc2005-09-16 19:27:46 -070094 do {
Jeff Dike6aa802c2007-10-16 01:26:56 -070095 current->thread.saved_task = NULL;
Jeff Dike77bf4402007-10-16 01:26:58 -070096
Jeff Dike77bf4402007-10-16 01:26:58 -070097 switch_threads(&from->thread.switch_buf,
98 &to->thread.switch_buf);
99
100 arch_switch_to(current->thread.prev_sched, current);
101
Jeff Dikeba180fd2007-10-16 01:27:00 -0700102 if (current->thread.saved_task)
Jeff Dike3eddddc2005-09-16 19:27:46 -0700103 show_regs(&(current->thread.regs));
104 next= current->thread.saved_task;
105 prev= current;
106 } while(current->thread.saved_task);
Jeff Dikef6e34c62005-09-16 19:27:43 -0700107
Jeff Dike6e21aec2007-05-06 14:51:21 -0700108 return current->thread.prev_sched;
Jeff Dikef6e34c62005-09-16 19:27:43 -0700109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111
112void interrupt_end(void)
113{
Jeff Dikeba180fd2007-10-16 01:27:00 -0700114 if (need_resched())
Jeff Dike6e21aec2007-05-06 14:51:21 -0700115 schedule();
Jeff Dikeba180fd2007-10-16 01:27:00 -0700116 if (test_tsk_thread_flag(current, TIF_SIGPENDING))
Jeff Dike6e21aec2007-05-06 14:51:21 -0700117 do_signal();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120void exit_thread(void)
121{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
Jeff Dike995473a2006-09-27 01:50:40 -0700123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124void *get_current(void)
125{
Jeff Dike6e21aec2007-05-06 14:51:21 -0700126 return current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128
Jeff Dike77bf4402007-10-16 01:26:58 -0700129extern void schedule_tail(struct task_struct *prev);
130
Jeff Dikeba180fd2007-10-16 01:27:00 -0700131/*
132 * This is called magically, by its address being stuffed in a jmp_buf
Jeff Dike77bf4402007-10-16 01:26:58 -0700133 * and being longjmp-d to.
134 */
135void new_thread_handler(void)
136{
137 int (*fn)(void *), n;
138 void *arg;
139
Jeff Dikeba180fd2007-10-16 01:27:00 -0700140 if (current->thread.prev_sched != NULL)
Jeff Dike77bf4402007-10-16 01:26:58 -0700141 schedule_tail(current->thread.prev_sched);
142 current->thread.prev_sched = NULL;
143
144 fn = current->thread.request.u.thread.proc;
145 arg = current->thread.request.u.thread.arg;
146
Jeff Dikeba180fd2007-10-16 01:27:00 -0700147 /*
148 * The return value is 1 if the kernel thread execs a process,
Jeff Dike77bf4402007-10-16 01:26:58 -0700149 * 0 if it just exits
150 */
151 n = run_kernel_thread(fn, arg, &current->thread.exec_buf);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700152 if (n == 1) {
Jeff Dike77bf4402007-10-16 01:26:58 -0700153 /* Handle any immediate reschedules or signals */
154 interrupt_end();
155 userspace(&current->thread.regs.regs);
156 }
157 else do_exit(0);
158}
159
160/* Called magically, see new_thread_handler above */
161void fork_handler(void)
162{
163 force_flush_all();
Jeff Dikeba180fd2007-10-16 01:27:00 -0700164 if (current->thread.prev_sched == NULL)
Jeff Dike77bf4402007-10-16 01:26:58 -0700165 panic("blech");
166
167 schedule_tail(current->thread.prev_sched);
168
Jeff Dikeba180fd2007-10-16 01:27:00 -0700169 /*
170 * XXX: if interrupt_end() calls schedule, this call to
Jeff Dike77bf4402007-10-16 01:26:58 -0700171 * arch_switch_to isn't needed. We could want to apply this to
Jeff Dikeba180fd2007-10-16 01:27:00 -0700172 * improve performance. -bb
173 */
Jeff Dike77bf4402007-10-16 01:26:58 -0700174 arch_switch_to(current->thread.prev_sched, current);
175
176 current->thread.prev_sched = NULL;
177
178 /* Handle any immediate reschedules or signals */
179 interrupt_end();
180
181 userspace(&current->thread.regs.regs);
182}
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184int copy_thread(int nr, unsigned long clone_flags, unsigned long sp,
Jeff Dike995473a2006-09-27 01:50:40 -0700185 unsigned long stack_top, struct task_struct * p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 struct pt_regs *regs)
187{
Jeff Dike77bf4402007-10-16 01:26:58 -0700188 void (*handler)(void);
189 int ret = 0;
Paolo 'Blaisorblade' Giarrussoaa6758d2006-03-31 02:30:22 -0800190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 p->thread = (struct thread_struct) INIT_THREAD;
Paolo 'Blaisorblade' Giarrussoaa6758d2006-03-31 02:30:22 -0800192
Jeff Dikeba180fd2007-10-16 01:27:00 -0700193 if (current->thread.forking) {
Jeff Dike77bf4402007-10-16 01:26:58 -0700194 memcpy(&p->thread.regs.regs, &regs->regs,
195 sizeof(p->thread.regs.regs));
Jeff Dike18baddd2007-10-16 01:27:07 -0700196 REGS_SET_SYSCALL_RETURN(p->thread.regs.regs.gp, 0);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700197 if (sp != 0)
Jeff Dike18baddd2007-10-16 01:27:07 -0700198 REGS_SP(p->thread.regs.regs.gp) = sp;
Paolo 'Blaisorblade' Giarrussoaa6758d2006-03-31 02:30:22 -0800199
Jeff Dike77bf4402007-10-16 01:26:58 -0700200 handler = fork_handler;
Paolo 'Blaisorblade' Giarrussoaa6758d2006-03-31 02:30:22 -0800201
Jeff Dike77bf4402007-10-16 01:26:58 -0700202 arch_copy_thread(&current->thread.arch, &p->thread.arch);
203 }
204 else {
205 init_thread_registers(&p->thread.regs.regs);
206 p->thread.request.u.thread = current->thread.request.u.thread;
207 handler = new_thread_handler;
208 }
Paolo 'Blaisorblade' Giarrussoaa6758d2006-03-31 02:30:22 -0800209
Jeff Dike77bf4402007-10-16 01:26:58 -0700210 new_thread(task_stack_page(p), &p->thread.switch_buf, handler);
211
212 if (current->thread.forking) {
213 clear_flushed_tls(p);
214
215 /*
216 * Set a new TLS for the child thread?
217 */
218 if (clone_flags & CLONE_SETTLS)
219 ret = arch_copy_tls(p);
220 }
221
Paolo 'Blaisorblade' Giarrussoaa6758d2006-03-31 02:30:22 -0800222 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
225void initial_thread_cb(void (*proc)(void *), void *arg)
226{
227 int save_kmalloc_ok = kmalloc_ok;
228
229 kmalloc_ok = 0;
Jeff Dike6aa802c2007-10-16 01:26:56 -0700230 initial_thread_cb_skas(proc, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 kmalloc_ok = save_kmalloc_ok;
232}
Jeff Dike995473a2006-09-27 01:50:40 -0700233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234void default_idle(void)
235{
Jeff Dikeb160fb632007-10-16 01:27:26 -0700236 unsigned long long nsecs;
237
Jeff Dikeba180fd2007-10-16 01:27:00 -0700238 while(1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 /* endless idle loop with no priority at all */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 /*
242 * although we are an idle CPU, we do not want to
243 * get into the scheduler unnecessarily.
244 */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700245 if (need_resched())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 schedule();
Jeff Dike995473a2006-09-27 01:50:40 -0700247
Jeff Diked2753a62007-10-16 01:27:25 -0700248 tick_nohz_stop_sched_tick();
Jeff Dikeb160fb632007-10-16 01:27:26 -0700249 nsecs = disable_timer();
250 idle_sleep(nsecs);
Jeff Diked2753a62007-10-16 01:27:25 -0700251 tick_nohz_restart_sched_tick();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
253}
254
255void cpu_idle(void)
256{
Jeff Dike77bf4402007-10-16 01:26:58 -0700257 cpu_tasks[current_thread->cpu].pid = os_getpid();
258 default_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
Jeff Dike995473a2006-09-27 01:50:40 -0700261void *um_virt_to_phys(struct task_struct *task, unsigned long addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 pte_t *pte_out)
263{
264 pgd_t *pgd;
265 pud_t *pud;
266 pmd_t *pmd;
267 pte_t *pte;
Hugh Dickins8f5cd762005-10-29 18:16:38 -0700268 pte_t ptent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Jeff Dikeba180fd2007-10-16 01:27:00 -0700270 if (task->mm == NULL)
Jeff Dike6e21aec2007-05-06 14:51:21 -0700271 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 pgd = pgd_offset(task->mm, addr);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700273 if (!pgd_present(*pgd))
Jeff Dike6e21aec2007-05-06 14:51:21 -0700274 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 pud = pud_offset(pgd, addr);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700277 if (!pud_present(*pud))
Jeff Dike6e21aec2007-05-06 14:51:21 -0700278 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 pmd = pmd_offset(pud, addr);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700281 if (!pmd_present(*pmd))
Jeff Dike6e21aec2007-05-06 14:51:21 -0700282 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 pte = pte_offset_kernel(pmd, addr);
Hugh Dickins8f5cd762005-10-29 18:16:38 -0700285 ptent = *pte;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700286 if (!pte_present(ptent))
Jeff Dike6e21aec2007-05-06 14:51:21 -0700287 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Jeff Dikeba180fd2007-10-16 01:27:00 -0700289 if (pte_out != NULL)
Hugh Dickins8f5cd762005-10-29 18:16:38 -0700290 *pte_out = ptent;
Jeff Dike6e21aec2007-05-06 14:51:21 -0700291 return (void *) (pte_val(ptent) & PAGE_MASK) + (addr & ~PAGE_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293
294char *current_cmd(void)
295{
296#if defined(CONFIG_SMP) || defined(CONFIG_HIGHMEM)
Jeff Dike6e21aec2007-05-06 14:51:21 -0700297 return "(Unknown)";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298#else
299 void *addr = um_virt_to_phys(current, current->mm->arg_start, NULL);
300 return IS_ERR(addr) ? "(Unknown)": __va((unsigned long) addr);
301#endif
302}
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304void dump_thread(struct pt_regs *regs, struct user *u)
305{
306}
307
Paolo 'Blaisorblade' Giarrussob6316292006-01-18 17:42:58 -0800308int __cant_sleep(void) {
309 return in_atomic() || irqs_disabled() || in_interrupt();
310 /* Is in_interrupt() really needed? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313int user_context(unsigned long sp)
314{
315 unsigned long stack;
316
317 stack = sp & (PAGE_MASK << CONFIG_KERNEL_STACK_ORDER);
Jeff Dike6e21aec2007-05-06 14:51:21 -0700318 return stack != (unsigned long) current_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321extern exitcall_t __uml_exitcall_begin, __uml_exitcall_end;
322
323void do_uml_exitcalls(void)
324{
325 exitcall_t *call;
326
327 call = &__uml_exitcall_end;
328 while (--call >= &__uml_exitcall_begin)
329 (*call)();
330}
331
WANG Congc0a92902008-02-04 22:30:41 -0800332char *uml_strdup(const char *string)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
Robert Lovedfe52242005-06-23 00:09:04 -0700334 return kstrdup(string, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337int copy_to_user_proc(void __user *to, void *from, int size)
338{
Jeff Dike6e21aec2007-05-06 14:51:21 -0700339 return copy_to_user(to, from, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341
342int copy_from_user_proc(void *to, void __user *from, int size)
343{
Jeff Dike6e21aec2007-05-06 14:51:21 -0700344 return copy_from_user(to, from, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
347int clear_user_proc(void __user *buf, int size)
348{
Jeff Dike6e21aec2007-05-06 14:51:21 -0700349 return clear_user(buf, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350}
351
352int strlen_user_proc(char __user *str)
353{
Jeff Dike6e21aec2007-05-06 14:51:21 -0700354 return strlen_user(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
357int smp_sigio_handler(void)
358{
359#ifdef CONFIG_SMP
360 int cpu = current_thread->cpu;
361 IPI_handler(cpu);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700362 if (cpu != 0)
Jeff Dike6e21aec2007-05-06 14:51:21 -0700363 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364#endif
Jeff Dike6e21aec2007-05-06 14:51:21 -0700365 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366}
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368int cpu(void)
369{
Jeff Dike6e21aec2007-05-06 14:51:21 -0700370 return current_thread->cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372
373static atomic_t using_sysemu = ATOMIC_INIT(0);
374int sysemu_supported;
375
376void set_using_sysemu(int value)
377{
378 if (value > sysemu_supported)
379 return;
380 atomic_set(&using_sysemu, value);
381}
382
383int get_using_sysemu(void)
384{
385 return atomic_read(&using_sysemu);
386}
387
388static int proc_read_sysemu(char *buf, char **start, off_t offset, int size,int *eof, void *data)
389{
Jeff Dikeba180fd2007-10-16 01:27:00 -0700390 if (snprintf(buf, size, "%d\n", get_using_sysemu()) < size)
391 /* No overflow */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 *eof = 1;
393
394 return strlen(buf);
395}
396
Al Viro4d338e12006-03-31 02:30:15 -0800397static int proc_write_sysemu(struct file *file,const char __user *buf, unsigned long count,void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
399 char tmp[2];
400
401 if (copy_from_user(tmp, buf, 1))
402 return -EFAULT;
403
404 if (tmp[0] >= '0' && tmp[0] <= '2')
405 set_using_sysemu(tmp[0] - '0');
Jeff Dikeba180fd2007-10-16 01:27:00 -0700406 /* We use the first char, but pretend to write everything */
407 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408}
409
410int __init make_proc_sysemu(void)
411{
412 struct proc_dir_entry *ent;
413 if (!sysemu_supported)
414 return 0;
415
416 ent = create_proc_entry("sysemu", 0600, &proc_root);
417
418 if (ent == NULL)
419 {
Christophe Lucas30f417c2005-07-28 21:16:12 -0700420 printk(KERN_WARNING "Failed to register /proc/sysemu\n");
Jeff Dike6e21aec2007-05-06 14:51:21 -0700421 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
423
424 ent->read_proc = proc_read_sysemu;
425 ent->write_proc = proc_write_sysemu;
426
427 return 0;
428}
429
430late_initcall(make_proc_sysemu);
431
432int singlestepping(void * t)
433{
434 struct task_struct *task = t ? t : current;
435
436 if ( ! (task->ptrace & PT_DTRACE) )
Jeff Dikeba180fd2007-10-16 01:27:00 -0700437 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 if (task->thread.singlestep_syscall)
Jeff Dikeba180fd2007-10-16 01:27:00 -0700440 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 return 2;
443}
444
Bodo Stroesserb8bd0222005-05-06 21:30:53 -0700445/*
446 * Only x86 and x86_64 have an arch_align_stack().
447 * All other arches have "#define arch_align_stack(x) (x)"
448 * in their asm/system.h
449 * As this is included in UML from asm-um/system-generic.h,
450 * we can use it to behave as the subarch does.
451 */
452#ifndef arch_align_stack
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453unsigned long arch_align_stack(unsigned long sp)
454{
Jeff Dike8f80e942006-09-25 23:33:01 -0700455 if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 sp -= get_random_int() % 8192;
457 return sp & ~0xf;
458}
Bodo Stroesserb8bd0222005-05-06 21:30:53 -0700459#endif
Jeff Dikec1127462008-02-04 22:30:36 -0800460
461unsigned long get_wchan(struct task_struct *p)
462{
463 unsigned long stack_page, sp, ip;
464 bool seen_sched = 0;
465
466 if ((p == NULL) || (p == current) || (p->state == TASK_RUNNING))
467 return 0;
468
469 stack_page = (unsigned long) task_stack_page(p);
470 /* Bail if the process has no kernel stack for some reason */
471 if (stack_page == 0)
472 return 0;
473
474 sp = p->thread.switch_buf->JB_SP;
475 /*
476 * Bail if the stack pointer is below the bottom of the kernel
477 * stack for some reason
478 */
479 if (sp < stack_page)
480 return 0;
481
482 while (sp < stack_page + THREAD_SIZE) {
483 ip = *((unsigned long *) sp);
484 if (in_sched_functions(ip))
485 /* Ignore everything until we're above the scheduler */
486 seen_sched = 1;
487 else if (kernel_text_address(ip) && seen_sched)
488 return ip;
489
490 sp += sizeof(unsigned long);
491 }
492
493 return 0;
494}