process.c 8.7 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
J
Jeff Dike 已提交
2
/*
A
Anton Ivanov 已提交
3 4
 * Copyright (C) 2015 Anton Ivanov (aivanov@{brocade.com,kot-begemot.co.uk})
 * Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de)
J
Jeff Dike 已提交
5
 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
L
Linus Torvalds 已提交
6 7 8
 * Copyright 2003 PathScale, Inc.
 */

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

J
Jeff Dike 已提交
36 37
/*
 * This is a per-cpu array.  A processor only modifies its entry and it only
L
Linus Torvalds 已提交
38 39 40 41 42
 * 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 } };

43
static inline int external_pid(void)
L
Linus Torvalds 已提交
44
{
45
	/* FIXME: Need to look up userspace_pid by cpu */
J
Jeff Dike 已提交
46
	return userspace_pid[0];
L
Linus Torvalds 已提交
47 48 49 50 51 52
}

int pid_to_processor_id(int pid)
{
	int i;

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

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 已提交
68
	gfp_t flags = GFP_KERNEL;
L
Linus Torvalds 已提交
69

70 71
	if (atomic)
		flags = GFP_ATOMIC;
L
Linus Torvalds 已提交
72
	page = __get_free_pages(flags, order);
73

J
Jeff Dike 已提交
74
	return page;
L
Linus Torvalds 已提交
75 76
}

J
Jeff Dike 已提交
77
static inline void set_current(struct task_struct *task)
L
Linus Torvalds 已提交
78
{
A
Al Viro 已提交
79
	cpu_tasks[task_thread_info(task)->cpu] = ((struct cpu_task)
80
		{ external_pid(), task });
L
Linus Torvalds 已提交
81 82
}

83
extern void arch_switch_to(struct task_struct *to);
84

85
void *__switch_to(struct task_struct *from, struct task_struct *to)
L
Linus Torvalds 已提交
86
{
J
Jeff Dike 已提交
87 88
	to->thread.prev_sched = from;
	set_current(to);
89

90 91
	switch_threads(&from->thread.switch_buf, &to->thread.switch_buf);
	arch_switch_to(current);
92

J
Jeff Dike 已提交
93
	return current->thread.prev_sched;
L
Linus Torvalds 已提交
94 95 96 97
}

void interrupt_end(void)
{
I
Ingo Molnar 已提交
98 99
	struct pt_regs *regs = &current->thread.regs;

J
Jeff Dike 已提交
100
	if (need_resched())
J
Jeff Dike 已提交
101
		schedule();
A
Al Viro 已提交
102
	if (test_thread_flag(TIF_SIGPENDING))
I
Ingo Molnar 已提交
103
		do_signal(regs);
104
	if (test_thread_flag(TIF_NOTIFY_RESUME))
I
Ingo Molnar 已提交
105
		tracehook_notify_resume(regs);
L
Linus Torvalds 已提交
106 107
}

A
Al Viro 已提交
108
int get_current_pid(void)
L
Linus Torvalds 已提交
109
{
A
Al Viro 已提交
110
	return task_pid_nr(current);
L
Linus Torvalds 已提交
111 112
}

J
Jeff Dike 已提交
113 114
/*
 * This is called magically, by its address being stuffed in a jmp_buf
115 116 117 118 119 120 121
 * and being longjmp-d to.
 */
void new_thread_handler(void)
{
	int (*fn)(void *), n;
	void *arg;

J
Jeff Dike 已提交
122
	if (current->thread.prev_sched != NULL)
123 124 125 126 127 128
		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 已提交
129
	/*
130
	 * callback returns only if the kernel thread execs a process
131
	 */
132
	n = fn(arg);
133
	userspace(&current->thread.regs.regs, current_thread_info()->aux_fp_regs);
134 135 136 137 138 139 140 141 142
}

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

	schedule_tail(current->thread.prev_sched);

J
Jeff Dike 已提交
143 144
	/*
	 * XXX: if interrupt_end() calls schedule, this call to
145
	 * arch_switch_to isn't needed. We could want to apply this to
J
Jeff Dike 已提交
146 147
	 * improve performance. -bb
	 */
148
	arch_switch_to(current);
149 150 151

	current->thread.prev_sched = NULL;

152
	userspace(&current->thread.regs.regs, current_thread_info()->aux_fp_regs);
153 154
}

155
int copy_thread(unsigned long clone_flags, unsigned long sp,
A
Amanieu d'Antras 已提交
156
		unsigned long arg, struct task_struct * p, unsigned long tls)
L
Linus Torvalds 已提交
157
{
158
	void (*handler)(void);
A
Al Viro 已提交
159
	int kthread = current->flags & PF_KTHREAD;
160
	int ret = 0;
161

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

A
Al Viro 已提交
164
	if (!kthread) {
165
	  	memcpy(&p->thread.regs.regs, current_pt_regs(),
166
		       sizeof(p->thread.regs.regs));
167
		PT_REGS_SET_SYSCALL_RETURN(&p->thread.regs, 0);
J
Jeff Dike 已提交
168
		if (sp != 0)
169
			REGS_SP(p->thread.regs.regs.gp) = sp;
170

171
		handler = fork_handler;
172

173
		arch_copy_thread(&current->thread.arch, &p->thread.arch);
A
Al Viro 已提交
174
	} else {
175
		get_safe_registers(p->thread.regs.regs.gp, p->thread.regs.regs.fp);
A
Al Viro 已提交
176 177
		p->thread.request.u.thread.proc = (int (*)(void *))sp;
		p->thread.request.u.thread.arg = (void *)arg;
178 179 180 181 182
		handler = new_thread_handler;
	}

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

A
Al Viro 已提交
183
	if (!kthread) {
184 185 186 187 188 189
		clear_flushed_tls(p);

		/*
		 * Set a new TLS for the child thread?
		 */
		if (clone_flags & CLONE_SETTLS)
A
Amanieu d'Antras 已提交
190
			ret = arch_set_tls(p, tls);
191
	}
192 193

	return ret;
L
Linus Torvalds 已提交
194 195 196 197 198 199 200
}

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

	kmalloc_ok = 0;
J
Jeff Dike 已提交
201
	initial_thread_cb_skas(proc, arg);
L
Linus Torvalds 已提交
202 203
	kmalloc_ok = save_kmalloc_ok;
}
J
Jeff Dike 已提交
204

J
Johannes Berg 已提交
205 206 207 208 209 210 211 212 213 214 215
static void um_idle_sleep(void)
{
	unsigned long long duration = UM_NSEC_PER_SEC;

	if (time_travel_mode != TT_MODE_OFF) {
		time_travel_sleep(duration);
	} else {
		os_idle_sleep(duration);
	}
}

R
Richard Weinberger 已提交
216
void arch_cpu_idle(void)
L
Linus Torvalds 已提交
217
{
J
Jeff Dike 已提交
218
	cpu_tasks[current_thread_info()->cpu].pid = os_getpid();
J
Johannes Berg 已提交
219
	um_idle_sleep();
R
Richard Weinberger 已提交
220
	local_irq_enable();
L
Linus Torvalds 已提交
221 222
}

223 224 225
int __cant_sleep(void) {
	return in_atomic() || irqs_disabled() || in_interrupt();
	/* Is in_interrupt() really needed? */
L
Linus Torvalds 已提交
226 227 228 229 230 231 232
}

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

	stack = sp & (PAGE_MASK << CONFIG_KERNEL_STACK_ORDER);
J
Jeff Dike 已提交
233
	return stack != (unsigned long) current_thread_info();
L
Linus Torvalds 已提交
234 235 236 237 238 239 240 241 242 243 244 245 246
}

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 已提交
247
char *uml_strdup(const char *string)
L
Linus Torvalds 已提交
248
{
249
	return kstrdup(string, GFP_KERNEL);
L
Linus Torvalds 已提交
250
}
251
EXPORT_SYMBOL(uml_strdup);
L
Linus Torvalds 已提交
252 253 254

int copy_to_user_proc(void __user *to, void *from, int size)
{
J
Jeff Dike 已提交
255
	return copy_to_user(to, from, size);
L
Linus Torvalds 已提交
256 257 258 259
}

int copy_from_user_proc(void *to, void __user *from, int size)
{
J
Jeff Dike 已提交
260
	return copy_from_user(to, from, size);
L
Linus Torvalds 已提交
261 262 263 264
}

int clear_user_proc(void __user *buf, int size)
{
J
Jeff Dike 已提交
265
	return clear_user(buf, size);
L
Linus Torvalds 已提交
266 267 268 269
}

int cpu(void)
{
J
Jeff Dike 已提交
270
	return current_thread_info()->cpu;
L
Linus Torvalds 已提交
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
}

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);
}

288
static int sysemu_proc_show(struct seq_file *m, void *v)
L
Linus Torvalds 已提交
289
{
290 291 292
	seq_printf(m, "%d\n", get_using_sysemu());
	return 0;
}
L
Linus Torvalds 已提交
293

294 295 296
static int sysemu_proc_open(struct inode *inode, struct file *file)
{
	return single_open(file, sysemu_proc_show, NULL);
L
Linus Torvalds 已提交
297 298
}

299 300
static ssize_t sysemu_proc_write(struct file *file, const char __user *buf,
				 size_t count, loff_t *pos)
L
Linus Torvalds 已提交
301 302 303 304 305 306 307 308
{
	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 已提交
309 310
	/* We use the first char, but pretend to write everything */
	return count;
L
Linus Torvalds 已提交
311 312
}

313 314 315 316 317 318
static const struct proc_ops sysemu_proc_ops = {
	.proc_open	= sysemu_proc_open,
	.proc_read	= seq_read,
	.proc_lseek	= seq_lseek,
	.proc_release	= single_release,
	.proc_write	= sysemu_proc_write,
319 320
};

L
Linus Torvalds 已提交
321 322 323 324 325 326
int __init make_proc_sysemu(void)
{
	struct proc_dir_entry *ent;
	if (!sysemu_supported)
		return 0;

327
	ent = proc_create("sysemu", 0600, NULL, &sysemu_proc_ops);
L
Linus Torvalds 已提交
328 329 330

	if (ent == NULL)
	{
331
		printk(KERN_WARNING "Failed to register /proc/sysemu\n");
J
Jeff Dike 已提交
332
		return 0;
L
Linus Torvalds 已提交
333 334 335 336 337 338 339 340 341 342 343
	}

	return 0;
}

late_initcall(make_proc_sysemu);

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

J
Jeff Dike 已提交
344
	if (!(task->ptrace & PT_DTRACE))
J
Jeff Dike 已提交
345
		return 0;
L
Linus Torvalds 已提交
346 347

	if (task->thread.singlestep_syscall)
J
Jeff Dike 已提交
348
		return 1;
L
Linus Torvalds 已提交
349 350 351 352

	return 2;
}

353 354 355
/*
 * Only x86 and x86_64 have an arch_align_stack().
 * All other arches have "#define arch_align_stack(x) (x)"
356
 * in their asm/exec.h
357 358 359 360
 * 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 已提交
361 362
unsigned long arch_align_stack(unsigned long sp)
{
J
Jeff Dike 已提交
363
	if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
L
Linus Torvalds 已提交
364 365 366
		sp -= get_random_int() % 8192;
	return sp & ~0xf;
}
367
#endif
J
Jeff Dike 已提交
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402

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 已提交
403 404 405 406 407

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

408
	return save_i387_registers(userspace_pid[cpu], (unsigned long *) fpu);
J
Jeff Dike 已提交
409 410
}