xref: /linux-6.15/arch/um/kernel/process.c (revision 8891b176)
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 ([email protected])
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>
189ffc6724STiwei Bie #include <linux/cpu.h>
195a0e3ad6STejun Heo #include <linux/slab.h>
20c5d4bb17SJeff Dike #include <linux/sched.h>
21b17b0153SIngo Molnar #include <linux/sched/debug.h>
2229930025SIngo Molnar #include <linux/sched/task.h>
2368db0cf1SIngo Molnar #include <linux/sched/task_stack.h>
246613c5e8SAlexey Dobriyan #include <linux/seq_file.h>
25c5d4bb17SJeff Dike #include <linux/tick.h>
26c5d4bb17SJeff Dike #include <linux/threads.h>
2703248addSEric W. Biederman #include <linux/resume_user_mode.h>
28c5d4bb17SJeff Dike #include <asm/current.h>
29445c5786SAl Viro #include <asm/mmu_context.h>
30179d83d8STiwei Bie #include <asm/switch_to.h>
319ffc6724STiwei Bie #include <asm/exec.h>
327c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
3337185b33SAl Viro #include <as-layout.h>
3437185b33SAl Viro #include <kern_util.h>
3537185b33SAl Viro #include <os.h>
3637185b33SAl Viro #include <skas.h>
37dbba7f70SAl Viro #include <registers.h>
38f185063bSJohannes Berg #include <linux/time-internal.h>
39bdbadfccSAl Viro #include <linux/elfcore.h>
40995473aeSJeff Dike 
41ba180fd4SJeff Dike /*
42ba180fd4SJeff Dike  * This is a per-cpu array.  A processor only modifies its entry and it only
43995473aeSJeff Dike  * cares about its entry, so it's OK if another processor is modifying its
44995473aeSJeff Dike  * entry.
45995473aeSJeff Dike  */
462f681ba4SBenjamin Berg struct task_struct *cpu_tasks[NR_CPUS];
472f681ba4SBenjamin Berg EXPORT_SYMBOL(cpu_tasks);
48995473aeSJeff Dike 
free_stack(unsigned long stack,int order)49995473aeSJeff Dike void free_stack(unsigned long stack, int order)
50995473aeSJeff Dike {
51995473aeSJeff Dike 	free_pages(stack, order);
52995473aeSJeff Dike }
53995473aeSJeff Dike 
alloc_stack(int order,int atomic)542fcb4090SJohannes Berg unsigned long alloc_stack(int order, int atomic)
55995473aeSJeff Dike {
562fcb4090SJohannes Berg 	unsigned long page;
57995473aeSJeff Dike 	gfp_t flags = GFP_KERNEL;
58995473aeSJeff Dike 
59995473aeSJeff Dike 	if (atomic)
60995473aeSJeff Dike 		flags = GFP_ATOMIC;
612fcb4090SJohannes Berg 	page = __get_free_pages(flags, order);
625c8aaceaSJeff Dike 
632fcb4090SJohannes Berg 	return page;
64995473aeSJeff Dike }
65995473aeSJeff Dike 
set_current(struct task_struct * task)666e21aec3SJeff Dike static inline void set_current(struct task_struct *task)
67995473aeSJeff Dike {
682f681ba4SBenjamin Berg 	cpu_tasks[task_thread_info(task)->cpu] = task;
69995473aeSJeff Dike }
70995473aeSJeff Dike 
__switch_to(struct task_struct * from,struct task_struct * to)71179d83d8STiwei Bie struct task_struct *__switch_to(struct task_struct *from, struct task_struct *to)
72995473aeSJeff Dike {
73995473aeSJeff Dike 	to->thread.prev_sched = from;
74995473aeSJeff Dike 	set_current(to);
75995473aeSJeff Dike 
76a1850e9cSRichard Weinberger 	switch_threads(&from->thread.switch_buf, &to->thread.switch_buf);
77291248fdSKarol Swietlicki 	arch_switch_to(current);
7877bf4400SJeff Dike 
796e21aec3SJeff Dike 	return current->thread.prev_sched;
80995473aeSJeff Dike }
81995473aeSJeff Dike 
interrupt_end(void)82995473aeSJeff Dike void interrupt_end(void)
83995473aeSJeff Dike {
84ccaee5f8SIngo Molnar 	struct pt_regs *regs = &current->thread.regs;
85ccaee5f8SIngo Molnar 
866e21aec3SJeff Dike 	if (need_resched())
876e21aec3SJeff Dike 		schedule();
8809041c92SJens Axboe 	if (test_thread_flag(TIF_SIGPENDING) ||
8909041c92SJens Axboe 	    test_thread_flag(TIF_NOTIFY_SIGNAL))
90ccaee5f8SIngo Molnar 		do_signal(regs);
913c532798SJens Axboe 	if (test_thread_flag(TIF_NOTIFY_RESUME))
9203248addSEric W. Biederman 		resume_user_mode_work(regs);
93995473aeSJeff Dike }
94995473aeSJeff Dike 
get_current_pid(void)95c2220b2aSAl Viro int get_current_pid(void)
96995473aeSJeff Dike {
97c2220b2aSAl Viro 	return task_pid_nr(current);
98995473aeSJeff Dike }
99995473aeSJeff Dike 
100ba180fd4SJeff Dike /*
101ba180fd4SJeff Dike  * This is called magically, by its address being stuffed in a jmp_buf
10277bf4400SJeff Dike  * and being longjmp-d to.
10377bf4400SJeff Dike  */
new_thread_handler(void)10477bf4400SJeff Dike void new_thread_handler(void)
10577bf4400SJeff Dike {
106dac847aeSJohannes Berg 	int (*fn)(void *);
10777bf4400SJeff Dike 	void *arg;
10877bf4400SJeff Dike 
10977bf4400SJeff Dike 	if (current->thread.prev_sched != NULL)
11077bf4400SJeff Dike 		schedule_tail(current->thread.prev_sched);
11177bf4400SJeff Dike 	current->thread.prev_sched = NULL;
11277bf4400SJeff Dike 
11394090f41STiwei Bie 	fn = current->thread.request.thread.proc;
11494090f41STiwei Bie 	arg = current->thread.request.thread.arg;
11577bf4400SJeff Dike 
116ba180fd4SJeff Dike 	/*
11722e2430dSAl Viro 	 * callback returns only if the kernel thread execs a process
11877bf4400SJeff Dike 	 */
119dac847aeSJohannes Berg 	fn(arg);
120c6ce7200SBenjamin Berg 	userspace(&current->thread.regs.regs);
12177bf4400SJeff Dike }
12277bf4400SJeff Dike 
12377bf4400SJeff Dike /* Called magically, see new_thread_handler above */
fork_handler(void)12453471c57STiwei Bie static void fork_handler(void)
12577bf4400SJeff Dike {
12677bf4400SJeff Dike 	schedule_tail(current->thread.prev_sched);
12777bf4400SJeff Dike 
128ba180fd4SJeff Dike 	/*
129ba180fd4SJeff Dike 	 * XXX: if interrupt_end() calls schedule, this call to
13077bf4400SJeff Dike 	 * arch_switch_to isn't needed. We could want to apply this to
131ba180fd4SJeff Dike 	 * improve performance. -bb
132ba180fd4SJeff Dike 	 */
133291248fdSKarol Swietlicki 	arch_switch_to(current);
13477bf4400SJeff Dike 
13577bf4400SJeff Dike 	current->thread.prev_sched = NULL;
13677bf4400SJeff Dike 
137c6ce7200SBenjamin Berg 	userspace(&current->thread.regs.regs);
13877bf4400SJeff Dike }
13977bf4400SJeff Dike 
copy_thread(struct task_struct * p,const struct kernel_clone_args * args)140c5febea0SEric W. Biederman int copy_thread(struct task_struct * p, const struct kernel_clone_args *args)
141995473aeSJeff Dike {
142c5febea0SEric W. Biederman 	unsigned long clone_flags = args->flags;
143c5febea0SEric W. Biederman 	unsigned long sp = args->stack;
144c5febea0SEric W. Biederman 	unsigned long tls = args->tls;
14577bf4400SJeff Dike 	void (*handler)(void);
14677bf4400SJeff Dike 	int ret = 0;
147995473aeSJeff Dike 
148995473aeSJeff Dike 	p->thread = (struct thread_struct) INIT_THREAD;
149995473aeSJeff Dike 
1505bd2e97cSEric W. Biederman 	if (!args->fn) {
1512b067fc9SAl Viro 	  	memcpy(&p->thread.regs.regs, current_pt_regs(),
15277bf4400SJeff Dike 		       sizeof(p->thread.regs.regs));
153a3170d2eSAl Viro 		PT_REGS_SET_SYSCALL_RETURN(&p->thread.regs, 0);
15477bf4400SJeff Dike 		if (sp != 0)
15518badddaSJeff Dike 			REGS_SP(p->thread.regs.regs.gp) = sp;
156995473aeSJeff Dike 
15777bf4400SJeff Dike 		handler = fork_handler;
15877bf4400SJeff Dike 
15977bf4400SJeff Dike 		arch_copy_thread(&current->thread.arch, &p->thread.arch);
160d2ce4e92SAl Viro 	} else {
161fbfe9c84SIngo van Lil 		get_safe_registers(p->thread.regs.regs.gp, p->thread.regs.regs.fp);
16294090f41STiwei Bie 		p->thread.request.thread.proc = args->fn;
16394090f41STiwei Bie 		p->thread.request.thread.arg = args->fn_arg;
16477bf4400SJeff Dike 		handler = new_thread_handler;
16577bf4400SJeff Dike 	}
16677bf4400SJeff Dike 
16777bf4400SJeff Dike 	new_thread(task_stack_page(p), &p->thread.switch_buf, handler);
16877bf4400SJeff Dike 
1695bd2e97cSEric W. Biederman 	if (!args->fn) {
170995473aeSJeff Dike 		clear_flushed_tls(p);
171995473aeSJeff Dike 
172995473aeSJeff Dike 		/*
173995473aeSJeff Dike 		 * Set a new TLS for the child thread?
174995473aeSJeff Dike 		 */
175995473aeSJeff Dike 		if (clone_flags & CLONE_SETTLS)
176457677c7SAmanieu d'Antras 			ret = arch_set_tls(p, tls);
17777bf4400SJeff Dike 	}
178995473aeSJeff Dike 
179995473aeSJeff Dike 	return ret;
180995473aeSJeff Dike }
181995473aeSJeff Dike 
initial_thread_cb(void (* proc)(void *),void * arg)182995473aeSJeff Dike void initial_thread_cb(void (*proc)(void *), void *arg)
183995473aeSJeff Dike {
184995473aeSJeff Dike 	int save_kmalloc_ok = kmalloc_ok;
185995473aeSJeff Dike 
186995473aeSJeff Dike 	kmalloc_ok = 0;
1876aa802ceSJeff Dike 	initial_thread_cb_skas(proc, arg);
188995473aeSJeff Dike 	kmalloc_ok = save_kmalloc_ok;
189995473aeSJeff Dike }
190995473aeSJeff Dike 
arch_dup_task_struct(struct task_struct * dst,struct task_struct * src)1913f17fed2SBenjamin Berg int arch_dup_task_struct(struct task_struct *dst,
1923f17fed2SBenjamin Berg 			 struct task_struct *src)
1933f17fed2SBenjamin Berg {
194*8891b176SBenjamin Berg 	/* init_task is not dynamically sized (missing FPU state) */
195*8891b176SBenjamin Berg 	if (unlikely(src == &init_task)) {
196*8891b176SBenjamin Berg 		memcpy(dst, src, sizeof(init_task));
197*8891b176SBenjamin Berg 		memset((void *)dst + sizeof(init_task), 0,
198*8891b176SBenjamin Berg 		       arch_task_struct_size - sizeof(init_task));
199*8891b176SBenjamin Berg 	} else {
2003f17fed2SBenjamin Berg 		memcpy(dst, src, arch_task_struct_size);
201*8891b176SBenjamin Berg 	}
202*8891b176SBenjamin Berg 
2033f17fed2SBenjamin Berg 	return 0;
2043f17fed2SBenjamin Berg }
2053f17fed2SBenjamin Berg 
um_idle_sleep(void)206a374b7cbSJohannes Berg void um_idle_sleep(void)
20706503870SJohannes Berg {
20849da38a3SJohannes Berg 	if (time_travel_mode != TT_MODE_OFF)
20949da38a3SJohannes Berg 		time_travel_sleep();
21049da38a3SJohannes Berg 	else
21149da38a3SJohannes Berg 		os_idle_sleep();
21206503870SJohannes Berg }
21306503870SJohannes Berg 
arch_cpu_idle(void)2148198c169SRichard Weinberger void arch_cpu_idle(void)
215995473aeSJeff Dike {
21606503870SJohannes Berg 	um_idle_sleep();
217995473aeSJeff Dike }
218995473aeSJeff Dike 
__uml_cant_sleep(void)219541d4e4dSAnton Ivanov int __uml_cant_sleep(void) {
220995473aeSJeff Dike 	return in_atomic() || irqs_disabled() || in_interrupt();
221995473aeSJeff Dike 	/* Is in_interrupt() really needed? */
222995473aeSJeff Dike }
223995473aeSJeff Dike 
224995473aeSJeff Dike extern exitcall_t __uml_exitcall_begin, __uml_exitcall_end;
225995473aeSJeff Dike 
do_uml_exitcalls(void)226995473aeSJeff Dike void do_uml_exitcalls(void)
227995473aeSJeff Dike {
228995473aeSJeff Dike 	exitcall_t *call;
229995473aeSJeff Dike 
230995473aeSJeff Dike 	call = &__uml_exitcall_end;
231995473aeSJeff Dike 	while (--call >= &__uml_exitcall_begin)
232995473aeSJeff Dike 		(*call)();
233995473aeSJeff Dike }
234995473aeSJeff Dike 
uml_strdup(const char * string)235c0a9290eSWANG Cong char *uml_strdup(const char *string)
236995473aeSJeff Dike {
237995473aeSJeff Dike 	return kstrdup(string, GFP_KERNEL);
238995473aeSJeff Dike }
23973395a00SAl Viro EXPORT_SYMBOL(uml_strdup);
240995473aeSJeff Dike 
copy_from_user_proc(void * to,void __user * from,int size)241995473aeSJeff Dike int copy_from_user_proc(void *to, void __user *from, int size)
242995473aeSJeff Dike {
2436e21aec3SJeff Dike 	return copy_from_user(to, from, size);
244995473aeSJeff Dike }
245995473aeSJeff Dike 
singlestepping(void)246a5571984SBenjamin Berg int singlestepping(void)
247995473aeSJeff Dike {
248a5571984SBenjamin Berg 	return test_thread_flag(TIF_SINGLESTEP);
249995473aeSJeff Dike }
250995473aeSJeff Dike 
251995473aeSJeff Dike /*
252995473aeSJeff Dike  * Only x86 and x86_64 have an arch_align_stack().
253995473aeSJeff Dike  * All other arches have "#define arch_align_stack(x) (x)"
254cf7bc58fSDavid Howells  * in their asm/exec.h
255995473aeSJeff Dike  * As this is included in UML from asm-um/system-generic.h,
256995473aeSJeff Dike  * we can use it to behave as the subarch does.
257995473aeSJeff Dike  */
258995473aeSJeff Dike #ifndef arch_align_stack
arch_align_stack(unsigned long sp)259995473aeSJeff Dike unsigned long arch_align_stack(unsigned long sp)
260995473aeSJeff Dike {
261995473aeSJeff Dike 	if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
2628032bf12SJason A. Donenfeld 		sp -= get_random_u32_below(8192);
263995473aeSJeff Dike 	return sp & ~0xf;
264995473aeSJeff Dike }
265995473aeSJeff Dike #endif
266c1127465SJeff Dike 
__get_wchan(struct task_struct * p)26742a20f86SKees Cook unsigned long __get_wchan(struct task_struct *p)
268c1127465SJeff Dike {
269c1127465SJeff Dike 	unsigned long stack_page, sp, ip;
270c1127465SJeff Dike 	bool seen_sched = 0;
271c1127465SJeff Dike 
272c1127465SJeff Dike 	stack_page = (unsigned long) task_stack_page(p);
273c1127465SJeff Dike 	/* Bail if the process has no kernel stack for some reason */
274c1127465SJeff Dike 	if (stack_page == 0)
275c1127465SJeff Dike 		return 0;
276c1127465SJeff Dike 
277c1127465SJeff Dike 	sp = p->thread.switch_buf->JB_SP;
278c1127465SJeff Dike 	/*
279c1127465SJeff Dike 	 * Bail if the stack pointer is below the bottom of the kernel
280c1127465SJeff Dike 	 * stack for some reason
281c1127465SJeff Dike 	 */
282c1127465SJeff Dike 	if (sp < stack_page)
283c1127465SJeff Dike 		return 0;
284c1127465SJeff Dike 
285c1127465SJeff Dike 	while (sp < stack_page + THREAD_SIZE) {
286c1127465SJeff Dike 		ip = *((unsigned long *) sp);
287c1127465SJeff Dike 		if (in_sched_functions(ip))
288c1127465SJeff Dike 			/* Ignore everything until we're above the scheduler */
289c1127465SJeff Dike 			seen_sched = 1;
290c1127465SJeff Dike 		else if (kernel_text_address(ip) && seen_sched)
291c1127465SJeff Dike 			return ip;
292c1127465SJeff Dike 
293c1127465SJeff Dike 		sp += sizeof(unsigned long);
294c1127465SJeff Dike 	}
295c1127465SJeff Dike 
296c1127465SJeff Dike 	return 0;
297c1127465SJeff Dike }
298