xref: /openbmc/linux/arch/um/kernel/process.c (revision f185063b)
10d1fb0a4SAlex Dewar // SPDX-License-Identifier: GPL-2.0
2995473aeSJeff Dike /*
32eb5f31bSAnton Ivanov  * Copyright (C) 2015 Anton Ivanov (aivanov@{brocade.com,kot-begemot.co.uk})
42eb5f31bSAnton Ivanov  * Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de)
5ba180fd4SJeff Dike  * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
6995473aeSJeff Dike  * Copyright 2003 PathScale, Inc.
7995473aeSJeff Dike  */
8995473aeSJeff Dike 
9c5d4bb17SJeff Dike #include <linux/stddef.h>
10c5d4bb17SJeff Dike #include <linux/err.h>
11c5d4bb17SJeff Dike #include <linux/hardirq.h>
12c5d4bb17SJeff Dike #include <linux/mm.h>
136613c5e8SAlexey Dobriyan #include <linux/module.h>
14c5d4bb17SJeff Dike #include <linux/personality.h>
15c5d4bb17SJeff Dike #include <linux/proc_fs.h>
16c5d4bb17SJeff Dike #include <linux/ptrace.h>
17c5d4bb17SJeff Dike #include <linux/random.h>
185a0e3ad6STejun Heo #include <linux/slab.h>
19c5d4bb17SJeff Dike #include <linux/sched.h>
20b17b0153SIngo Molnar #include <linux/sched/debug.h>
2129930025SIngo Molnar #include <linux/sched/task.h>
2268db0cf1SIngo Molnar #include <linux/sched/task_stack.h>
236613c5e8SAlexey Dobriyan #include <linux/seq_file.h>
24c5d4bb17SJeff Dike #include <linux/tick.h>
25c5d4bb17SJeff Dike #include <linux/threads.h>
26d50349b0SAl Viro #include <linux/tracehook.h>
27c5d4bb17SJeff Dike #include <asm/current.h>
28c5d4bb17SJeff Dike #include <asm/pgtable.h>
29445c5786SAl Viro #include <asm/mmu_context.h>
307c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
3137185b33SAl Viro #include <as-layout.h>
3237185b33SAl Viro #include <kern_util.h>
3337185b33SAl Viro #include <os.h>
3437185b33SAl Viro #include <skas.h>
35f185063bSJohannes Berg #include <linux/time-internal.h>
36995473aeSJeff Dike 
37ba180fd4SJeff Dike /*
38ba180fd4SJeff Dike  * This is a per-cpu array.  A processor only modifies its entry and it only
39995473aeSJeff Dike  * cares about its entry, so it's OK if another processor is modifying its
40995473aeSJeff Dike  * entry.
41995473aeSJeff Dike  */
42995473aeSJeff Dike struct cpu_task cpu_tasks[NR_CPUS] = { [0 ... NR_CPUS - 1] = { -1, NULL } };
43995473aeSJeff Dike 
442dc5802aSKarol Swietlicki static inline int external_pid(void)
45995473aeSJeff Dike {
4677bf4400SJeff Dike 	/* FIXME: Need to look up userspace_pid by cpu */
47ba180fd4SJeff Dike 	return userspace_pid[0];
48995473aeSJeff Dike }
49995473aeSJeff Dike 
50995473aeSJeff Dike int pid_to_processor_id(int pid)
51995473aeSJeff Dike {
52995473aeSJeff Dike 	int i;
53995473aeSJeff Dike 
54995473aeSJeff Dike 	for (i = 0; i < ncpus; i++) {
556e21aec3SJeff Dike 		if (cpu_tasks[i].pid == pid)
566e21aec3SJeff Dike 			return i;
57995473aeSJeff Dike 	}
586e21aec3SJeff Dike 	return -1;
59995473aeSJeff Dike }
60995473aeSJeff Dike 
61995473aeSJeff Dike void free_stack(unsigned long stack, int order)
62995473aeSJeff Dike {
63995473aeSJeff Dike 	free_pages(stack, order);
64995473aeSJeff Dike }
65995473aeSJeff Dike 
66995473aeSJeff Dike unsigned long alloc_stack(int order, int atomic)
67995473aeSJeff Dike {
68995473aeSJeff Dike 	unsigned long page;
69995473aeSJeff Dike 	gfp_t flags = GFP_KERNEL;
70995473aeSJeff Dike 
71995473aeSJeff Dike 	if (atomic)
72995473aeSJeff Dike 		flags = GFP_ATOMIC;
73995473aeSJeff Dike 	page = __get_free_pages(flags, order);
745c8aaceaSJeff Dike 
756e21aec3SJeff Dike 	return page;
76995473aeSJeff Dike }
77995473aeSJeff Dike 
786e21aec3SJeff Dike static inline void set_current(struct task_struct *task)
79995473aeSJeff Dike {
80995473aeSJeff Dike 	cpu_tasks[task_thread_info(task)->cpu] = ((struct cpu_task)
812dc5802aSKarol Swietlicki 		{ external_pid(), task });
82995473aeSJeff Dike }
83995473aeSJeff Dike 
84291248fdSKarol Swietlicki extern void arch_switch_to(struct task_struct *to);
8577bf4400SJeff Dike 
8676b278edSRichard Weinberger void *__switch_to(struct task_struct *from, struct task_struct *to)
87995473aeSJeff Dike {
88995473aeSJeff Dike 	to->thread.prev_sched = from;
89995473aeSJeff Dike 	set_current(to);
90995473aeSJeff Dike 
91a1850e9cSRichard Weinberger 	switch_threads(&from->thread.switch_buf, &to->thread.switch_buf);
92291248fdSKarol Swietlicki 	arch_switch_to(current);
9377bf4400SJeff Dike 
946e21aec3SJeff Dike 	return current->thread.prev_sched;
95995473aeSJeff Dike }
96995473aeSJeff Dike 
97995473aeSJeff Dike void interrupt_end(void)
98995473aeSJeff Dike {
99ccaee5f8SIngo Molnar 	struct pt_regs *regs = &current->thread.regs;
100ccaee5f8SIngo Molnar 
1016e21aec3SJeff Dike 	if (need_resched())
1026e21aec3SJeff Dike 		schedule();
103d50349b0SAl Viro 	if (test_thread_flag(TIF_SIGPENDING))
104ccaee5f8SIngo Molnar 		do_signal(regs);
105a42c6dedSAl Viro 	if (test_and_clear_thread_flag(TIF_NOTIFY_RESUME))
106ccaee5f8SIngo Molnar 		tracehook_notify_resume(regs);
107995473aeSJeff Dike }
108995473aeSJeff Dike 
109c2220b2aSAl Viro int get_current_pid(void)
110995473aeSJeff Dike {
111c2220b2aSAl Viro 	return task_pid_nr(current);
112995473aeSJeff Dike }
113995473aeSJeff Dike 
114ba180fd4SJeff Dike /*
115ba180fd4SJeff Dike  * This is called magically, by its address being stuffed in a jmp_buf
11677bf4400SJeff Dike  * and being longjmp-d to.
11777bf4400SJeff Dike  */
11877bf4400SJeff Dike void new_thread_handler(void)
11977bf4400SJeff Dike {
12077bf4400SJeff Dike 	int (*fn)(void *), n;
12177bf4400SJeff Dike 	void *arg;
12277bf4400SJeff Dike 
12377bf4400SJeff Dike 	if (current->thread.prev_sched != NULL)
12477bf4400SJeff Dike 		schedule_tail(current->thread.prev_sched);
12577bf4400SJeff Dike 	current->thread.prev_sched = NULL;
12677bf4400SJeff Dike 
12777bf4400SJeff Dike 	fn = current->thread.request.u.thread.proc;
12877bf4400SJeff Dike 	arg = current->thread.request.u.thread.arg;
12977bf4400SJeff Dike 
130ba180fd4SJeff Dike 	/*
13122e2430dSAl Viro 	 * callback returns only if the kernel thread execs a process
13277bf4400SJeff Dike 	 */
13322e2430dSAl Viro 	n = fn(arg);
1346f602afdSThomas Meyer 	userspace(&current->thread.regs.regs, current_thread_info()->aux_fp_regs);
13577bf4400SJeff Dike }
13677bf4400SJeff Dike 
13777bf4400SJeff Dike /* Called magically, see new_thread_handler above */
13877bf4400SJeff Dike void fork_handler(void)
13977bf4400SJeff Dike {
14077bf4400SJeff Dike 	force_flush_all();
14177bf4400SJeff Dike 
14277bf4400SJeff Dike 	schedule_tail(current->thread.prev_sched);
14377bf4400SJeff Dike 
144ba180fd4SJeff Dike 	/*
145ba180fd4SJeff Dike 	 * XXX: if interrupt_end() calls schedule, this call to
14677bf4400SJeff Dike 	 * arch_switch_to isn't needed. We could want to apply this to
147ba180fd4SJeff Dike 	 * improve performance. -bb
148ba180fd4SJeff Dike 	 */
149291248fdSKarol Swietlicki 	arch_switch_to(current);
15077bf4400SJeff Dike 
15177bf4400SJeff Dike 	current->thread.prev_sched = NULL;
15277bf4400SJeff Dike 
1536f602afdSThomas Meyer 	userspace(&current->thread.regs.regs, current_thread_info()->aux_fp_regs);
15477bf4400SJeff Dike }
15577bf4400SJeff Dike 
156457677c7SAmanieu d'Antras int copy_thread_tls(unsigned long clone_flags, unsigned long sp,
157457677c7SAmanieu d'Antras 		unsigned long arg, struct task_struct * p, unsigned long tls)
158995473aeSJeff Dike {
15977bf4400SJeff Dike 	void (*handler)(void);
160d2ce4e92SAl Viro 	int kthread = current->flags & PF_KTHREAD;
16177bf4400SJeff Dike 	int ret = 0;
162995473aeSJeff Dike 
163995473aeSJeff Dike 	p->thread = (struct thread_struct) INIT_THREAD;
164995473aeSJeff Dike 
165d2ce4e92SAl Viro 	if (!kthread) {
1662b067fc9SAl Viro 	  	memcpy(&p->thread.regs.regs, current_pt_regs(),
16777bf4400SJeff Dike 		       sizeof(p->thread.regs.regs));
168a3170d2eSAl Viro 		PT_REGS_SET_SYSCALL_RETURN(&p->thread.regs, 0);
16977bf4400SJeff Dike 		if (sp != 0)
17018badddaSJeff Dike 			REGS_SP(p->thread.regs.regs.gp) = sp;
171995473aeSJeff Dike 
17277bf4400SJeff Dike 		handler = fork_handler;
17377bf4400SJeff Dike 
17477bf4400SJeff Dike 		arch_copy_thread(&current->thread.arch, &p->thread.arch);
175d2ce4e92SAl Viro 	} else {
176fbfe9c84SIngo van Lil 		get_safe_registers(p->thread.regs.regs.gp, p->thread.regs.regs.fp);
1771f02ab4aSAl Viro 		p->thread.request.u.thread.proc = (int (*)(void *))sp;
1781f02ab4aSAl Viro 		p->thread.request.u.thread.arg = (void *)arg;
17977bf4400SJeff Dike 		handler = new_thread_handler;
18077bf4400SJeff Dike 	}
18177bf4400SJeff Dike 
18277bf4400SJeff Dike 	new_thread(task_stack_page(p), &p->thread.switch_buf, handler);
18377bf4400SJeff Dike 
184d2ce4e92SAl Viro 	if (!kthread) {
185995473aeSJeff Dike 		clear_flushed_tls(p);
186995473aeSJeff Dike 
187995473aeSJeff Dike 		/*
188995473aeSJeff Dike 		 * Set a new TLS for the child thread?
189995473aeSJeff Dike 		 */
190995473aeSJeff Dike 		if (clone_flags & CLONE_SETTLS)
191457677c7SAmanieu d'Antras 			ret = arch_set_tls(p, tls);
19277bf4400SJeff Dike 	}
193995473aeSJeff Dike 
194995473aeSJeff Dike 	return ret;
195995473aeSJeff Dike }
196995473aeSJeff Dike 
197995473aeSJeff Dike void initial_thread_cb(void (*proc)(void *), void *arg)
198995473aeSJeff Dike {
199995473aeSJeff Dike 	int save_kmalloc_ok = kmalloc_ok;
200995473aeSJeff Dike 
201995473aeSJeff Dike 	kmalloc_ok = 0;
2026aa802ceSJeff Dike 	initial_thread_cb_skas(proc, arg);
203995473aeSJeff Dike 	kmalloc_ok = save_kmalloc_ok;
204995473aeSJeff Dike }
205995473aeSJeff Dike 
20606503870SJohannes Berg static void time_travel_sleep(unsigned long long duration)
20706503870SJohannes Berg {
20806503870SJohannes Berg 	unsigned long long next = time_travel_time + duration;
20906503870SJohannes Berg 
21006503870SJohannes Berg 	if (time_travel_mode != TT_MODE_INFCPU)
21106503870SJohannes Berg 		os_timer_disable();
21206503870SJohannes Berg 
213eec94b8aSJohannes Berg 	while (time_travel_timer_mode == TT_TMR_PERIODIC &&
214eec94b8aSJohannes Berg 	       time_travel_timer_expiry < time_travel_time)
215eec94b8aSJohannes Berg 		time_travel_set_timer_expiry(time_travel_timer_expiry +
216eec94b8aSJohannes Berg 					     time_travel_timer_interval);
217eec94b8aSJohannes Berg 
218eec94b8aSJohannes Berg 	if (time_travel_timer_mode != TT_TMR_DISABLED &&
21906503870SJohannes Berg 	    time_travel_timer_expiry < next) {
22006503870SJohannes Berg 		if (time_travel_timer_mode == TT_TMR_ONESHOT)
221e0917f87SJohannes Berg 			time_travel_set_timer_mode(TT_TMR_DISABLED);
22206503870SJohannes Berg 		/*
223278911eeSJohannes Berg 		 * In basic mode, time_travel_time will be adjusted in
224278911eeSJohannes Berg 		 * the timer IRQ handler so it works even when the signal
225278911eeSJohannes Berg 		 * comes from the OS timer, see there.
22606503870SJohannes Berg 		 */
227278911eeSJohannes Berg 		if (time_travel_mode != TT_MODE_BASIC)
228278911eeSJohannes Berg 			time_travel_set_time(time_travel_timer_expiry);
229278911eeSJohannes Berg 
23006503870SJohannes Berg 		deliver_alarm();
23106503870SJohannes Berg 	} else {
23206503870SJohannes Berg 		time_travel_set_time(next);
23306503870SJohannes Berg 	}
23406503870SJohannes Berg 
23506503870SJohannes Berg 	if (time_travel_mode != TT_MODE_INFCPU) {
23606503870SJohannes Berg 		if (time_travel_timer_mode == TT_TMR_PERIODIC)
23706503870SJohannes Berg 			os_timer_set_interval(time_travel_timer_interval);
23806503870SJohannes Berg 		else if (time_travel_timer_mode == TT_TMR_ONESHOT)
23906503870SJohannes Berg 			os_timer_one_shot(time_travel_timer_expiry - next);
24006503870SJohannes Berg 	}
24106503870SJohannes Berg }
24206503870SJohannes Berg 
24306503870SJohannes Berg static void um_idle_sleep(void)
24406503870SJohannes Berg {
24506503870SJohannes Berg 	unsigned long long duration = UM_NSEC_PER_SEC;
24606503870SJohannes Berg 
24706503870SJohannes Berg 	if (time_travel_mode != TT_MODE_OFF) {
24806503870SJohannes Berg 		time_travel_sleep(duration);
24906503870SJohannes Berg 	} else {
25006503870SJohannes Berg 		os_idle_sleep(duration);
25106503870SJohannes Berg 	}
25206503870SJohannes Berg }
25306503870SJohannes Berg 
2548198c169SRichard Weinberger void arch_cpu_idle(void)
255995473aeSJeff Dike {
2568198c169SRichard Weinberger 	cpu_tasks[current_thread_info()->cpu].pid = os_getpid();
25706503870SJohannes Berg 	um_idle_sleep();
2588198c169SRichard Weinberger 	local_irq_enable();
259995473aeSJeff Dike }
260995473aeSJeff Dike 
261995473aeSJeff Dike int __cant_sleep(void) {
262995473aeSJeff Dike 	return in_atomic() || irqs_disabled() || in_interrupt();
263995473aeSJeff Dike 	/* Is in_interrupt() really needed? */
264995473aeSJeff Dike }
265995473aeSJeff Dike 
266995473aeSJeff Dike int user_context(unsigned long sp)
267995473aeSJeff Dike {
268995473aeSJeff Dike 	unsigned long stack;
269995473aeSJeff Dike 
270995473aeSJeff Dike 	stack = sp & (PAGE_MASK << CONFIG_KERNEL_STACK_ORDER);
271a5a678c8SJeff Dike 	return stack != (unsigned long) current_thread_info();
272995473aeSJeff Dike }
273995473aeSJeff Dike 
274995473aeSJeff Dike extern exitcall_t __uml_exitcall_begin, __uml_exitcall_end;
275995473aeSJeff Dike 
276995473aeSJeff Dike void do_uml_exitcalls(void)
277995473aeSJeff Dike {
278995473aeSJeff Dike 	exitcall_t *call;
279995473aeSJeff Dike 
280995473aeSJeff Dike 	call = &__uml_exitcall_end;
281995473aeSJeff Dike 	while (--call >= &__uml_exitcall_begin)
282995473aeSJeff Dike 		(*call)();
283995473aeSJeff Dike }
284995473aeSJeff Dike 
285c0a9290eSWANG Cong char *uml_strdup(const char *string)
286995473aeSJeff Dike {
287995473aeSJeff Dike 	return kstrdup(string, GFP_KERNEL);
288995473aeSJeff Dike }
28973395a00SAl Viro EXPORT_SYMBOL(uml_strdup);
290995473aeSJeff Dike 
291995473aeSJeff Dike int copy_to_user_proc(void __user *to, void *from, int size)
292995473aeSJeff Dike {
2936e21aec3SJeff Dike 	return copy_to_user(to, from, size);
294995473aeSJeff Dike }
295995473aeSJeff Dike 
296995473aeSJeff Dike int copy_from_user_proc(void *to, void __user *from, int size)
297995473aeSJeff Dike {
2986e21aec3SJeff Dike 	return copy_from_user(to, from, size);
299995473aeSJeff Dike }
300995473aeSJeff Dike 
301995473aeSJeff Dike int clear_user_proc(void __user *buf, int size)
302995473aeSJeff Dike {
3036e21aec3SJeff Dike 	return clear_user(buf, size);
304995473aeSJeff Dike }
305995473aeSJeff Dike 
306995473aeSJeff Dike int cpu(void)
307995473aeSJeff Dike {
308a5a678c8SJeff Dike 	return current_thread_info()->cpu;
309995473aeSJeff Dike }
310995473aeSJeff Dike 
311995473aeSJeff Dike static atomic_t using_sysemu = ATOMIC_INIT(0);
312995473aeSJeff Dike int sysemu_supported;
313995473aeSJeff Dike 
314995473aeSJeff Dike void set_using_sysemu(int value)
315995473aeSJeff Dike {
316995473aeSJeff Dike 	if (value > sysemu_supported)
317995473aeSJeff Dike 		return;
318995473aeSJeff Dike 	atomic_set(&using_sysemu, value);
319995473aeSJeff Dike }
320995473aeSJeff Dike 
321995473aeSJeff Dike int get_using_sysemu(void)
322995473aeSJeff Dike {
323995473aeSJeff Dike 	return atomic_read(&using_sysemu);
324995473aeSJeff Dike }
325995473aeSJeff Dike 
3266613c5e8SAlexey Dobriyan static int sysemu_proc_show(struct seq_file *m, void *v)
327995473aeSJeff Dike {
3286613c5e8SAlexey Dobriyan 	seq_printf(m, "%d\n", get_using_sysemu());
3296613c5e8SAlexey Dobriyan 	return 0;
330995473aeSJeff Dike }
331995473aeSJeff Dike 
3326613c5e8SAlexey Dobriyan static int sysemu_proc_open(struct inode *inode, struct file *file)
3336613c5e8SAlexey Dobriyan {
3346613c5e8SAlexey Dobriyan 	return single_open(file, sysemu_proc_show, NULL);
3356613c5e8SAlexey Dobriyan }
3366613c5e8SAlexey Dobriyan 
3376613c5e8SAlexey Dobriyan static ssize_t sysemu_proc_write(struct file *file, const char __user *buf,
3386613c5e8SAlexey Dobriyan 				 size_t count, loff_t *pos)
339995473aeSJeff Dike {
340995473aeSJeff Dike 	char tmp[2];
341995473aeSJeff Dike 
342995473aeSJeff Dike 	if (copy_from_user(tmp, buf, 1))
343995473aeSJeff Dike 		return -EFAULT;
344995473aeSJeff Dike 
345995473aeSJeff Dike 	if (tmp[0] >= '0' && tmp[0] <= '2')
346995473aeSJeff Dike 		set_using_sysemu(tmp[0] - '0');
347ba180fd4SJeff Dike 	/* We use the first char, but pretend to write everything */
348ba180fd4SJeff Dike 	return count;
349995473aeSJeff Dike }
350995473aeSJeff Dike 
35197a32539SAlexey Dobriyan static const struct proc_ops sysemu_proc_ops = {
35297a32539SAlexey Dobriyan 	.proc_open	= sysemu_proc_open,
35397a32539SAlexey Dobriyan 	.proc_read	= seq_read,
35497a32539SAlexey Dobriyan 	.proc_lseek	= seq_lseek,
35597a32539SAlexey Dobriyan 	.proc_release	= single_release,
35697a32539SAlexey Dobriyan 	.proc_write	= sysemu_proc_write,
3576613c5e8SAlexey Dobriyan };
3586613c5e8SAlexey Dobriyan 
359995473aeSJeff Dike int __init make_proc_sysemu(void)
360995473aeSJeff Dike {
361995473aeSJeff Dike 	struct proc_dir_entry *ent;
362995473aeSJeff Dike 	if (!sysemu_supported)
363995473aeSJeff Dike 		return 0;
364995473aeSJeff Dike 
36597a32539SAlexey Dobriyan 	ent = proc_create("sysemu", 0600, NULL, &sysemu_proc_ops);
366995473aeSJeff Dike 
367995473aeSJeff Dike 	if (ent == NULL)
368995473aeSJeff Dike 	{
369995473aeSJeff Dike 		printk(KERN_WARNING "Failed to register /proc/sysemu\n");
3706e21aec3SJeff Dike 		return 0;
371995473aeSJeff Dike 	}
372995473aeSJeff Dike 
373995473aeSJeff Dike 	return 0;
374995473aeSJeff Dike }
375995473aeSJeff Dike 
376995473aeSJeff Dike late_initcall(make_proc_sysemu);
377995473aeSJeff Dike 
378995473aeSJeff Dike int singlestepping(void * t)
379995473aeSJeff Dike {
380995473aeSJeff Dike 	struct task_struct *task = t ? t : current;
381995473aeSJeff Dike 
382995473aeSJeff Dike 	if (!(task->ptrace & PT_DTRACE))
383ba180fd4SJeff Dike 		return 0;
384995473aeSJeff Dike 
385995473aeSJeff Dike 	if (task->thread.singlestep_syscall)
386ba180fd4SJeff Dike 		return 1;
387995473aeSJeff Dike 
388995473aeSJeff Dike 	return 2;
389995473aeSJeff Dike }
390995473aeSJeff Dike 
391995473aeSJeff Dike /*
392995473aeSJeff Dike  * Only x86 and x86_64 have an arch_align_stack().
393995473aeSJeff Dike  * All other arches have "#define arch_align_stack(x) (x)"
394cf7bc58fSDavid Howells  * in their asm/exec.h
395995473aeSJeff Dike  * As this is included in UML from asm-um/system-generic.h,
396995473aeSJeff Dike  * we can use it to behave as the subarch does.
397995473aeSJeff Dike  */
398995473aeSJeff Dike #ifndef arch_align_stack
399995473aeSJeff Dike unsigned long arch_align_stack(unsigned long sp)
400995473aeSJeff Dike {
401995473aeSJeff Dike 	if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
402995473aeSJeff Dike 		sp -= get_random_int() % 8192;
403995473aeSJeff Dike 	return sp & ~0xf;
404995473aeSJeff Dike }
405995473aeSJeff Dike #endif
406c1127465SJeff Dike 
407c1127465SJeff Dike unsigned long get_wchan(struct task_struct *p)
408c1127465SJeff Dike {
409c1127465SJeff Dike 	unsigned long stack_page, sp, ip;
410c1127465SJeff Dike 	bool seen_sched = 0;
411c1127465SJeff Dike 
412c1127465SJeff Dike 	if ((p == NULL) || (p == current) || (p->state == TASK_RUNNING))
413c1127465SJeff Dike 		return 0;
414c1127465SJeff Dike 
415c1127465SJeff Dike 	stack_page = (unsigned long) task_stack_page(p);
416c1127465SJeff Dike 	/* Bail if the process has no kernel stack for some reason */
417c1127465SJeff Dike 	if (stack_page == 0)
418c1127465SJeff Dike 		return 0;
419c1127465SJeff Dike 
420c1127465SJeff Dike 	sp = p->thread.switch_buf->JB_SP;
421c1127465SJeff Dike 	/*
422c1127465SJeff Dike 	 * Bail if the stack pointer is below the bottom of the kernel
423c1127465SJeff Dike 	 * stack for some reason
424c1127465SJeff Dike 	 */
425c1127465SJeff Dike 	if (sp < stack_page)
426c1127465SJeff Dike 		return 0;
427c1127465SJeff Dike 
428c1127465SJeff Dike 	while (sp < stack_page + THREAD_SIZE) {
429c1127465SJeff Dike 		ip = *((unsigned long *) sp);
430c1127465SJeff Dike 		if (in_sched_functions(ip))
431c1127465SJeff Dike 			/* Ignore everything until we're above the scheduler */
432c1127465SJeff Dike 			seen_sched = 1;
433c1127465SJeff Dike 		else if (kernel_text_address(ip) && seen_sched)
434c1127465SJeff Dike 			return ip;
435c1127465SJeff Dike 
436c1127465SJeff Dike 		sp += sizeof(unsigned long);
437c1127465SJeff Dike 	}
438c1127465SJeff Dike 
439c1127465SJeff Dike 	return 0;
440c1127465SJeff Dike }
4418192ab42SJeff Dike 
4428192ab42SJeff Dike int elf_core_copy_fpregs(struct task_struct *t, elf_fpregset_t *fpu)
4438192ab42SJeff Dike {
4448192ab42SJeff Dike 	int cpu = current_thread_info()->cpu;
4458192ab42SJeff Dike 
446a78ff111SEli Cooper 	return save_i387_registers(userspace_pid[cpu], (unsigned long *) fpu);
4478192ab42SJeff Dike }
4488192ab42SJeff Dike 
449