process.c 9.6 KB
Newer Older
J
Jeff Dike 已提交
1
/*
J
Jeff Dike 已提交
2
 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
L
Linus Torvalds 已提交
3 4 5 6
 * Copyright 2003 PathScale, Inc.
 * Licensed under the GPL
 */

J
Jeff Dike 已提交
7 8 9 10
#include <linux/stddef.h>
#include <linux/err.h>
#include <linux/hardirq.h>
#include <linux/mm.h>
11
#include <linux/module.h>
J
Jeff Dike 已提交
12 13 14 15
#include <linux/personality.h>
#include <linux/proc_fs.h>
#include <linux/ptrace.h>
#include <linux/random.h>
16
#include <linux/slab.h>
J
Jeff Dike 已提交
17
#include <linux/sched.h>
18
#include <linux/seq_file.h>
J
Jeff Dike 已提交
19 20
#include <linux/tick.h>
#include <linux/threads.h>
A
Al Viro 已提交
21
#include <linux/tracehook.h>
J
Jeff Dike 已提交
22 23
#include <asm/current.h>
#include <asm/pgtable.h>
A
Al Viro 已提交
24
#include <asm/mmu_context.h>
J
Jeff Dike 已提交
25
#include <asm/uaccess.h>
J
Jeff Dike 已提交
26
#include "as-layout.h"
J
Jeff Dike 已提交
27
#include "kern_util.h"
L
Linus Torvalds 已提交
28
#include "os.h"
29
#include "skas.h"
L
Linus Torvalds 已提交
30

J
Jeff Dike 已提交
31 32
/*
 * This is a per-cpu array.  A processor only modifies its entry and it only
L
Linus Torvalds 已提交
33 34 35 36 37
 * cares about its entry, so it's OK if another processor is modifying its
 * entry.
 */
struct cpu_task cpu_tasks[NR_CPUS] = { [0 ... NR_CPUS - 1] = { -1, NULL } };

38
static inline int external_pid(void)
L
Linus Torvalds 已提交
39
{
40
	/* FIXME: Need to look up userspace_pid by cpu */
J
Jeff Dike 已提交
41
	return userspace_pid[0];
L
Linus Torvalds 已提交
42 43 44 45 46 47
}

int pid_to_processor_id(int pid)
{
	int i;

J
Jeff Dike 已提交
48
	for (i = 0; i < ncpus; i++) {
J
Jeff Dike 已提交
49
		if (cpu_tasks[i].pid == pid)
J
Jeff Dike 已提交
50
			return i;
L
Linus Torvalds 已提交
51
	}
J
Jeff Dike 已提交
52
	return -1;
L
Linus Torvalds 已提交
53 54 55 56 57 58 59 60 61 62
}

void free_stack(unsigned long stack, int order)
{
	free_pages(stack, order);
}

unsigned long alloc_stack(int order, int atomic)
{
	unsigned long page;
A
Al Viro 已提交
63
	gfp_t flags = GFP_KERNEL;
L
Linus Torvalds 已提交
64

65 66
	if (atomic)
		flags = GFP_ATOMIC;
L
Linus Torvalds 已提交
67
	page = __get_free_pages(flags, order);
68

J
Jeff Dike 已提交
69
	return page;
L
Linus Torvalds 已提交
70 71 72 73 74 75 76 77
}

int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
{
	int pid;

	current->thread.request.u.thread.proc = fn;
	current->thread.request.u.thread.arg = arg;
J
Jeff Dike 已提交
78 79
	pid = do_fork(CLONE_VM | CLONE_UNTRACED | flags, 0,
		      &current->thread.regs, 0, NULL, NULL);
J
Jeff Dike 已提交
80
	return pid;
L
Linus Torvalds 已提交
81
}
82
EXPORT_SYMBOL(kernel_thread);
L
Linus Torvalds 已提交
83

J
Jeff Dike 已提交
84
static inline void set_current(struct task_struct *task)
L
Linus Torvalds 已提交
85
{
A
Al Viro 已提交
86
	cpu_tasks[task_thread_info(task)->cpu] = ((struct cpu_task)
87
		{ external_pid(), task });
L
Linus Torvalds 已提交
88 89
}

90
extern void arch_switch_to(struct task_struct *to);
91

92
void *__switch_to(struct task_struct *from, struct task_struct *to)
L
Linus Torvalds 已提交
93
{
J
Jeff Dike 已提交
94 95
	to->thread.prev_sched = from;
	set_current(to);
96

97
	do {
J
Jeff Dike 已提交
98
		current->thread.saved_task = NULL;
99

J
Jeff Dike 已提交
100 101
		switch_threads(&from->thread.switch_buf,
			       &to->thread.switch_buf);
102

103
		arch_switch_to(current);
104

J
Jeff Dike 已提交
105
		if (current->thread.saved_task)
106
			show_regs(&(current->thread.regs));
J
Jeff Dike 已提交
107 108
		to = current->thread.saved_task;
		from = current;
109
	} while (current->thread.saved_task);
110

J
Jeff Dike 已提交
111
	return current->thread.prev_sched;
L
Linus Torvalds 已提交
112 113 114 115
}

void interrupt_end(void)
{
J
Jeff Dike 已提交
116
	if (need_resched())
J
Jeff Dike 已提交
117
		schedule();
A
Al Viro 已提交
118
	if (test_thread_flag(TIF_SIGPENDING))
J
Jeff Dike 已提交
119
		do_signal();
120
	if (test_and_clear_thread_flag(TIF_NOTIFY_RESUME))
A
Al Viro 已提交
121
		tracehook_notify_resume(&current->thread.regs);
L
Linus Torvalds 已提交
122 123 124 125 126
}

void exit_thread(void)
{
}
J
Jeff Dike 已提交
127

A
Al Viro 已提交
128
int get_current_pid(void)
L
Linus Torvalds 已提交
129
{
A
Al Viro 已提交
130
	return task_pid_nr(current);
L
Linus Torvalds 已提交
131 132
}

J
Jeff Dike 已提交
133 134
/*
 * This is called magically, by its address being stuffed in a jmp_buf
135 136 137 138 139 140 141
 * and being longjmp-d to.
 */
void new_thread_handler(void)
{
	int (*fn)(void *), n;
	void *arg;

J
Jeff Dike 已提交
142
	if (current->thread.prev_sched != NULL)
143 144 145 146 147 148
		schedule_tail(current->thread.prev_sched);
	current->thread.prev_sched = NULL;

	fn = current->thread.request.u.thread.proc;
	arg = current->thread.request.u.thread.arg;

J
Jeff Dike 已提交
149 150
	/*
	 * The return value is 1 if the kernel thread execs a process,
151 152 153
	 * 0 if it just exits
	 */
	n = run_kernel_thread(fn, arg, &current->thread.exec_buf);
J
Jeff Dike 已提交
154
	if (n == 1) {
155 156 157 158 159 160 161 162 163 164 165 166 167 168
		/* Handle any immediate reschedules or signals */
		interrupt_end();
		userspace(&current->thread.regs.regs);
	}
	else do_exit(0);
}

/* Called magically, see new_thread_handler above */
void fork_handler(void)
{
	force_flush_all();

	schedule_tail(current->thread.prev_sched);

J
Jeff Dike 已提交
169 170
	/*
	 * XXX: if interrupt_end() calls schedule, this call to
171
	 * arch_switch_to isn't needed. We could want to apply this to
J
Jeff Dike 已提交
172 173
	 * improve performance. -bb
	 */
174
	arch_switch_to(current);
175 176 177 178 179 180 181 182 183

	current->thread.prev_sched = NULL;

	/* Handle any immediate reschedules or signals */
	interrupt_end();

	userspace(&current->thread.regs.regs);
}

A
Alexey Dobriyan 已提交
184
int copy_thread(unsigned long clone_flags, unsigned long sp,
J
Jeff Dike 已提交
185
		unsigned long stack_top, struct task_struct * p,
L
Linus Torvalds 已提交
186 187
		struct pt_regs *regs)
{
188 189
	void (*handler)(void);
	int ret = 0;
190

L
Linus Torvalds 已提交
191
	p->thread = (struct thread_struct) INIT_THREAD;
192

J
Jeff Dike 已提交
193
	if (current->thread.forking) {
194 195
	  	memcpy(&p->thread.regs.regs, &regs->regs,
		       sizeof(p->thread.regs.regs));
196
		UPT_SET_SYSCALL_RETURN(&p->thread.regs.regs, 0);
J
Jeff Dike 已提交
197
		if (sp != 0)
198
			REGS_SP(p->thread.regs.regs.gp) = sp;
199

200
		handler = fork_handler;
201

202 203 204
		arch_copy_thread(&current->thread.arch, &p->thread.arch);
	}
	else {
205
		get_safe_registers(p->thread.regs.regs.gp, p->thread.regs.regs.fp);
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
		p->thread.request.u.thread = current->thread.request.u.thread;
		handler = new_thread_handler;
	}

	new_thread(task_stack_page(p), &p->thread.switch_buf, handler);

	if (current->thread.forking) {
		clear_flushed_tls(p);

		/*
		 * Set a new TLS for the child thread?
		 */
		if (clone_flags & CLONE_SETTLS)
			ret = arch_copy_tls(p);
	}
221 222

	return ret;
L
Linus Torvalds 已提交
223 224 225 226 227 228 229
}

void initial_thread_cb(void (*proc)(void *), void *arg)
{
	int save_kmalloc_ok = kmalloc_ok;

	kmalloc_ok = 0;
J
Jeff Dike 已提交
230
	initial_thread_cb_skas(proc, arg);
L
Linus Torvalds 已提交
231 232
	kmalloc_ok = save_kmalloc_ok;
}
J
Jeff Dike 已提交
233

L
Linus Torvalds 已提交
234 235
void default_idle(void)
{
236 237
	unsigned long long nsecs;

J
Jeff Dike 已提交
238
	while (1) {
L
Linus Torvalds 已提交
239 240 241 242 243 244
		/* endless idle loop with no priority at all */

		/*
		 * although we are an idle CPU, we do not want to
		 * get into the scheduler unnecessarily.
		 */
J
Jeff Dike 已提交
245
		if (need_resched())
L
Linus Torvalds 已提交
246
			schedule();
J
Jeff Dike 已提交
247

248 249
		tick_nohz_idle_enter();
		rcu_idle_enter();
250 251
		nsecs = disable_timer();
		idle_sleep(nsecs);
252 253
		rcu_idle_exit();
		tick_nohz_idle_exit();
L
Linus Torvalds 已提交
254 255 256 257 258
	}
}

void cpu_idle(void)
{
J
Jeff Dike 已提交
259
	cpu_tasks[current_thread_info()->cpu].pid = os_getpid();
260
	default_idle();
L
Linus Torvalds 已提交
261 262
}

263 264 265
int __cant_sleep(void) {
	return in_atomic() || irqs_disabled() || in_interrupt();
	/* Is in_interrupt() really needed? */
L
Linus Torvalds 已提交
266 267 268 269 270 271 272
}

int user_context(unsigned long sp)
{
	unsigned long stack;

	stack = sp & (PAGE_MASK << CONFIG_KERNEL_STACK_ORDER);
J
Jeff Dike 已提交
273
	return stack != (unsigned long) current_thread_info();
L
Linus Torvalds 已提交
274 275 276 277 278 279 280 281 282 283 284 285 286
}

extern exitcall_t __uml_exitcall_begin, __uml_exitcall_end;

void do_uml_exitcalls(void)
{
	exitcall_t *call;

	call = &__uml_exitcall_end;
	while (--call >= &__uml_exitcall_begin)
		(*call)();
}

W
WANG Cong 已提交
287
char *uml_strdup(const char *string)
L
Linus Torvalds 已提交
288
{
289
	return kstrdup(string, GFP_KERNEL);
L
Linus Torvalds 已提交
290
}
291
EXPORT_SYMBOL(uml_strdup);
L
Linus Torvalds 已提交
292 293 294

int copy_to_user_proc(void __user *to, void *from, int size)
{
J
Jeff Dike 已提交
295
	return copy_to_user(to, from, size);
L
Linus Torvalds 已提交
296 297 298 299
}

int copy_from_user_proc(void *to, void __user *from, int size)
{
J
Jeff Dike 已提交
300
	return copy_from_user(to, from, size);
L
Linus Torvalds 已提交
301 302 303 304
}

int clear_user_proc(void __user *buf, int size)
{
J
Jeff Dike 已提交
305
	return clear_user(buf, size);
L
Linus Torvalds 已提交
306 307 308 309
}

int strlen_user_proc(char __user *str)
{
J
Jeff Dike 已提交
310
	return strlen_user(str);
L
Linus Torvalds 已提交
311 312 313 314 315
}

int smp_sigio_handler(void)
{
#ifdef CONFIG_SMP
J
Jeff Dike 已提交
316
	int cpu = current_thread_info()->cpu;
L
Linus Torvalds 已提交
317
	IPI_handler(cpu);
J
Jeff Dike 已提交
318
	if (cpu != 0)
J
Jeff Dike 已提交
319
		return 1;
L
Linus Torvalds 已提交
320
#endif
J
Jeff Dike 已提交
321
	return 0;
L
Linus Torvalds 已提交
322 323 324 325
}

int cpu(void)
{
J
Jeff Dike 已提交
326
	return current_thread_info()->cpu;
L
Linus Torvalds 已提交
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
}

static atomic_t using_sysemu = ATOMIC_INIT(0);
int sysemu_supported;

void set_using_sysemu(int value)
{
	if (value > sysemu_supported)
		return;
	atomic_set(&using_sysemu, value);
}

int get_using_sysemu(void)
{
	return atomic_read(&using_sysemu);
}

344
static int sysemu_proc_show(struct seq_file *m, void *v)
L
Linus Torvalds 已提交
345
{
346 347 348
	seq_printf(m, "%d\n", get_using_sysemu());
	return 0;
}
L
Linus Torvalds 已提交
349

350 351 352
static int sysemu_proc_open(struct inode *inode, struct file *file)
{
	return single_open(file, sysemu_proc_show, NULL);
L
Linus Torvalds 已提交
353 354
}

355 356
static ssize_t sysemu_proc_write(struct file *file, const char __user *buf,
				 size_t count, loff_t *pos)
L
Linus Torvalds 已提交
357 358 359 360 361 362 363 364
{
	char tmp[2];

	if (copy_from_user(tmp, buf, 1))
		return -EFAULT;

	if (tmp[0] >= '0' && tmp[0] <= '2')
		set_using_sysemu(tmp[0] - '0');
J
Jeff Dike 已提交
365 366
	/* We use the first char, but pretend to write everything */
	return count;
L
Linus Torvalds 已提交
367 368
}

369 370 371 372 373 374 375 376 377
static const struct file_operations sysemu_proc_fops = {
	.owner		= THIS_MODULE,
	.open		= sysemu_proc_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
	.write		= sysemu_proc_write,
};

L
Linus Torvalds 已提交
378 379 380 381 382 383
int __init make_proc_sysemu(void)
{
	struct proc_dir_entry *ent;
	if (!sysemu_supported)
		return 0;

384
	ent = proc_create("sysemu", 0600, NULL, &sysemu_proc_fops);
L
Linus Torvalds 已提交
385 386 387

	if (ent == NULL)
	{
388
		printk(KERN_WARNING "Failed to register /proc/sysemu\n");
J
Jeff Dike 已提交
389
		return 0;
L
Linus Torvalds 已提交
390 391 392 393 394 395 396 397 398 399 400
	}

	return 0;
}

late_initcall(make_proc_sysemu);

int singlestepping(void * t)
{
	struct task_struct *task = t ? t : current;

J
Jeff Dike 已提交
401
	if (!(task->ptrace & PT_DTRACE))
J
Jeff Dike 已提交
402
		return 0;
L
Linus Torvalds 已提交
403 404

	if (task->thread.singlestep_syscall)
J
Jeff Dike 已提交
405
		return 1;
L
Linus Torvalds 已提交
406 407 408 409

	return 2;
}

410 411 412 413 414 415 416 417
/*
 * Only x86 and x86_64 have an arch_align_stack().
 * All other arches have "#define arch_align_stack(x) (x)"
 * in their asm/system.h
 * As this is included in UML from asm-um/system-generic.h,
 * we can use it to behave as the subarch does.
 */
#ifndef arch_align_stack
L
Linus Torvalds 已提交
418 419
unsigned long arch_align_stack(unsigned long sp)
{
J
Jeff Dike 已提交
420
	if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
L
Linus Torvalds 已提交
421 422 423
		sp -= get_random_int() % 8192;
	return sp & ~0xf;
}
424
#endif
J
Jeff Dike 已提交
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459

unsigned long get_wchan(struct task_struct *p)
{
	unsigned long stack_page, sp, ip;
	bool seen_sched = 0;

	if ((p == NULL) || (p == current) || (p->state == TASK_RUNNING))
		return 0;

	stack_page = (unsigned long) task_stack_page(p);
	/* Bail if the process has no kernel stack for some reason */
	if (stack_page == 0)
		return 0;

	sp = p->thread.switch_buf->JB_SP;
	/*
	 * Bail if the stack pointer is below the bottom of the kernel
	 * stack for some reason
	 */
	if (sp < stack_page)
		return 0;

	while (sp < stack_page + THREAD_SIZE) {
		ip = *((unsigned long *) sp);
		if (in_sched_functions(ip))
			/* Ignore everything until we're above the scheduler */
			seen_sched = 1;
		else if (kernel_text_address(ip) && seen_sched)
			return ip;

		sp += sizeof(unsigned long);
	}

	return 0;
}
J
Jeff Dike 已提交
460 461 462 463 464 465 466 467

int elf_core_copy_fpregs(struct task_struct *t, elf_fpregset_t *fpu)
{
	int cpu = current_thread_info()->cpu;

	return save_fp_registers(userspace_pid[cpu], (unsigned long *) fpu);
}