xref: /linux/arch/um/kernel/process.c (revision 49ff7d871242d7fd8adb8a2d8347c5d94dda808b)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2015 Anton Ivanov (aivanov@{brocade.com,kot-begemot.co.uk})
4  * Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de)
5  * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
6  * Copyright 2003 PathScale, Inc.
7  */
8 
9 #include <linux/stddef.h>
10 #include <linux/err.h>
11 #include <linux/hardirq.h>
12 #include <linux/mm.h>
13 #include <linux/module.h>
14 #include <linux/personality.h>
15 #include <linux/proc_fs.h>
16 #include <linux/ptrace.h>
17 #include <linux/random.h>
18 #include <linux/cpu.h>
19 #include <linux/slab.h>
20 #include <linux/sched.h>
21 #include <linux/sched/debug.h>
22 #include <linux/sched/task.h>
23 #include <linux/sched/task_stack.h>
24 #include <linux/seq_file.h>
25 #include <linux/tick.h>
26 #include <linux/threads.h>
27 #include <linux/resume_user_mode.h>
28 #include <asm/current.h>
29 #include <asm/mmu_context.h>
30 #include <asm/switch_to.h>
31 #include <asm/exec.h>
32 #include <linux/uaccess.h>
33 #include <as-layout.h>
34 #include <kern_util.h>
35 #include <os.h>
36 #include <skas.h>
37 #include <registers.h>
38 #include <linux/time-internal.h>
39 #include <linux/elfcore.h>
40 
41 /*
42  * This is a per-cpu array.  A processor only modifies its entry and it only
43  * cares about its entry, so it's OK if another processor is modifying its
44  * entry.
45  */
46 struct cpu_task cpu_tasks[NR_CPUS] = { [0 ... NR_CPUS - 1] = { -1, NULL } };
47 
48 static inline int external_pid(void)
49 {
50 	/* FIXME: Need to look up userspace_pid by cpu */
51 	return userspace_pid[0];
52 }
53 
54 void free_stack(unsigned long stack, int order)
55 {
56 	free_pages(stack, order);
57 }
58 
59 unsigned long alloc_stack(int order, int atomic)
60 {
61 	unsigned long page;
62 	gfp_t flags = GFP_KERNEL;
63 
64 	if (atomic)
65 		flags = GFP_ATOMIC;
66 	page = __get_free_pages(flags, order);
67 
68 	return page;
69 }
70 
71 static inline void set_current(struct task_struct *task)
72 {
73 	cpu_tasks[task_thread_info(task)->cpu] = ((struct cpu_task)
74 		{ external_pid(), task });
75 }
76 
77 struct task_struct *__switch_to(struct task_struct *from, struct task_struct *to)
78 {
79 	to->thread.prev_sched = from;
80 	set_current(to);
81 
82 	switch_threads(&from->thread.switch_buf, &to->thread.switch_buf);
83 	arch_switch_to(current);
84 
85 	return current->thread.prev_sched;
86 }
87 
88 void interrupt_end(void)
89 {
90 	struct pt_regs *regs = &current->thread.regs;
91 
92 	if (need_resched())
93 		schedule();
94 	if (test_thread_flag(TIF_SIGPENDING) ||
95 	    test_thread_flag(TIF_NOTIFY_SIGNAL))
96 		do_signal(regs);
97 	if (test_thread_flag(TIF_NOTIFY_RESUME))
98 		resume_user_mode_work(regs);
99 }
100 
101 int get_current_pid(void)
102 {
103 	return task_pid_nr(current);
104 }
105 
106 /*
107  * This is called magically, by its address being stuffed in a jmp_buf
108  * and being longjmp-d to.
109  */
110 void new_thread_handler(void)
111 {
112 	int (*fn)(void *), n;
113 	void *arg;
114 
115 	if (current->thread.prev_sched != NULL)
116 		schedule_tail(current->thread.prev_sched);
117 	current->thread.prev_sched = NULL;
118 
119 	fn = current->thread.request.u.thread.proc;
120 	arg = current->thread.request.u.thread.arg;
121 
122 	/*
123 	 * callback returns only if the kernel thread execs a process
124 	 */
125 	n = fn(arg);
126 	userspace(&current->thread.regs.regs, current_thread_info()->aux_fp_regs);
127 }
128 
129 /* Called magically, see new_thread_handler above */
130 static void fork_handler(void)
131 {
132 	force_flush_all();
133 
134 	schedule_tail(current->thread.prev_sched);
135 
136 	/*
137 	 * XXX: if interrupt_end() calls schedule, this call to
138 	 * arch_switch_to isn't needed. We could want to apply this to
139 	 * improve performance. -bb
140 	 */
141 	arch_switch_to(current);
142 
143 	current->thread.prev_sched = NULL;
144 
145 	userspace(&current->thread.regs.regs, current_thread_info()->aux_fp_regs);
146 }
147 
148 int copy_thread(struct task_struct * p, const struct kernel_clone_args *args)
149 {
150 	unsigned long clone_flags = args->flags;
151 	unsigned long sp = args->stack;
152 	unsigned long tls = args->tls;
153 	void (*handler)(void);
154 	int ret = 0;
155 
156 	p->thread = (struct thread_struct) INIT_THREAD;
157 
158 	if (!args->fn) {
159 	  	memcpy(&p->thread.regs.regs, current_pt_regs(),
160 		       sizeof(p->thread.regs.regs));
161 		PT_REGS_SET_SYSCALL_RETURN(&p->thread.regs, 0);
162 		if (sp != 0)
163 			REGS_SP(p->thread.regs.regs.gp) = sp;
164 
165 		handler = fork_handler;
166 
167 		arch_copy_thread(&current->thread.arch, &p->thread.arch);
168 	} else {
169 		get_safe_registers(p->thread.regs.regs.gp, p->thread.regs.regs.fp);
170 		p->thread.request.u.thread.proc = args->fn;
171 		p->thread.request.u.thread.arg = args->fn_arg;
172 		handler = new_thread_handler;
173 	}
174 
175 	new_thread(task_stack_page(p), &p->thread.switch_buf, handler);
176 
177 	if (!args->fn) {
178 		clear_flushed_tls(p);
179 
180 		/*
181 		 * Set a new TLS for the child thread?
182 		 */
183 		if (clone_flags & CLONE_SETTLS)
184 			ret = arch_set_tls(p, tls);
185 	}
186 
187 	return ret;
188 }
189 
190 void initial_thread_cb(void (*proc)(void *), void *arg)
191 {
192 	int save_kmalloc_ok = kmalloc_ok;
193 
194 	kmalloc_ok = 0;
195 	initial_thread_cb_skas(proc, arg);
196 	kmalloc_ok = save_kmalloc_ok;
197 }
198 
199 void um_idle_sleep(void)
200 {
201 	if (time_travel_mode != TT_MODE_OFF)
202 		time_travel_sleep();
203 	else
204 		os_idle_sleep();
205 }
206 
207 void arch_cpu_idle(void)
208 {
209 	cpu_tasks[current_thread_info()->cpu].pid = os_getpid();
210 	um_idle_sleep();
211 }
212 
213 int __uml_cant_sleep(void) {
214 	return in_atomic() || irqs_disabled() || in_interrupt();
215 	/* Is in_interrupt() really needed? */
216 }
217 
218 int user_context(unsigned long sp)
219 {
220 	unsigned long stack;
221 
222 	stack = sp & (PAGE_MASK << CONFIG_KERNEL_STACK_ORDER);
223 	return stack != (unsigned long) current_thread_info();
224 }
225 
226 extern exitcall_t __uml_exitcall_begin, __uml_exitcall_end;
227 
228 void do_uml_exitcalls(void)
229 {
230 	exitcall_t *call;
231 
232 	call = &__uml_exitcall_end;
233 	while (--call >= &__uml_exitcall_begin)
234 		(*call)();
235 }
236 
237 char *uml_strdup(const char *string)
238 {
239 	return kstrdup(string, GFP_KERNEL);
240 }
241 EXPORT_SYMBOL(uml_strdup);
242 
243 int copy_from_user_proc(void *to, void __user *from, int size)
244 {
245 	return copy_from_user(to, from, size);
246 }
247 
248 static atomic_t using_sysemu = ATOMIC_INIT(0);
249 int sysemu_supported;
250 
251 static void set_using_sysemu(int value)
252 {
253 	if (value > sysemu_supported)
254 		return;
255 	atomic_set(&using_sysemu, value);
256 }
257 
258 static int get_using_sysemu(void)
259 {
260 	return atomic_read(&using_sysemu);
261 }
262 
263 static int sysemu_proc_show(struct seq_file *m, void *v)
264 {
265 	seq_printf(m, "%d\n", get_using_sysemu());
266 	return 0;
267 }
268 
269 static int sysemu_proc_open(struct inode *inode, struct file *file)
270 {
271 	return single_open(file, sysemu_proc_show, NULL);
272 }
273 
274 static ssize_t sysemu_proc_write(struct file *file, const char __user *buf,
275 				 size_t count, loff_t *pos)
276 {
277 	char tmp[2];
278 
279 	if (copy_from_user(tmp, buf, 1))
280 		return -EFAULT;
281 
282 	if (tmp[0] >= '0' && tmp[0] <= '2')
283 		set_using_sysemu(tmp[0] - '0');
284 	/* We use the first char, but pretend to write everything */
285 	return count;
286 }
287 
288 static const struct proc_ops sysemu_proc_ops = {
289 	.proc_open	= sysemu_proc_open,
290 	.proc_read	= seq_read,
291 	.proc_lseek	= seq_lseek,
292 	.proc_release	= single_release,
293 	.proc_write	= sysemu_proc_write,
294 };
295 
296 static int __init make_proc_sysemu(void)
297 {
298 	struct proc_dir_entry *ent;
299 	if (!sysemu_supported)
300 		return 0;
301 
302 	ent = proc_create("sysemu", 0600, NULL, &sysemu_proc_ops);
303 
304 	if (ent == NULL)
305 	{
306 		printk(KERN_WARNING "Failed to register /proc/sysemu\n");
307 		return 0;
308 	}
309 
310 	return 0;
311 }
312 
313 late_initcall(make_proc_sysemu);
314 
315 int singlestepping(void)
316 {
317 	return test_thread_flag(TIF_SINGLESTEP);
318 }
319 
320 /*
321  * Only x86 and x86_64 have an arch_align_stack().
322  * All other arches have "#define arch_align_stack(x) (x)"
323  * in their asm/exec.h
324  * As this is included in UML from asm-um/system-generic.h,
325  * we can use it to behave as the subarch does.
326  */
327 #ifndef arch_align_stack
328 unsigned long arch_align_stack(unsigned long sp)
329 {
330 	if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
331 		sp -= get_random_u32_below(8192);
332 	return sp & ~0xf;
333 }
334 #endif
335 
336 unsigned long __get_wchan(struct task_struct *p)
337 {
338 	unsigned long stack_page, sp, ip;
339 	bool seen_sched = 0;
340 
341 	stack_page = (unsigned long) task_stack_page(p);
342 	/* Bail if the process has no kernel stack for some reason */
343 	if (stack_page == 0)
344 		return 0;
345 
346 	sp = p->thread.switch_buf->JB_SP;
347 	/*
348 	 * Bail if the stack pointer is below the bottom of the kernel
349 	 * stack for some reason
350 	 */
351 	if (sp < stack_page)
352 		return 0;
353 
354 	while (sp < stack_page + THREAD_SIZE) {
355 		ip = *((unsigned long *) sp);
356 		if (in_sched_functions(ip))
357 			/* Ignore everything until we're above the scheduler */
358 			seen_sched = 1;
359 		else if (kernel_text_address(ip) && seen_sched)
360 			return ip;
361 
362 		sp += sizeof(unsigned long);
363 	}
364 
365 	return 0;
366 }
367 
368 int elf_core_copy_task_fpregs(struct task_struct *t, elf_fpregset_t *fpu)
369 {
370 	int cpu = current_thread_info()->cpu;
371 
372 	return save_i387_registers(userspace_pid[cpu], (unsigned long *) fpu);
373 }
374 
375