2019-08-25 10:49:17 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2006-09-27 01:50:40 -07:00
|
|
|
/*
|
2015-11-02 16:16:37 +00:00
|
|
|
* Copyright (C) 2015 Anton Ivanov (aivanov@{brocade.com,kot-begemot.co.uk})
|
|
|
|
|
* Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de)
|
2007-10-16 01:27:00 -07:00
|
|
|
* Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
|
2005-04-16 15:20:36 -07:00
|
|
|
* Copyright 2003 PathScale, Inc.
|
|
|
|
|
*/
|
|
|
|
|
|
2008-02-04 22:31:14 -08:00
|
|
|
#include <linux/stddef.h>
|
|
|
|
|
#include <linux/err.h>
|
|
|
|
|
#include <linux/hardirq.h>
|
|
|
|
|
#include <linux/mm.h>
|
2009-12-14 18:00:11 -08:00
|
|
|
#include <linux/module.h>
|
2008-02-04 22:31:14 -08:00
|
|
|
#include <linux/personality.h>
|
|
|
|
|
#include <linux/proc_fs.h>
|
|
|
|
|
#include <linux/ptrace.h>
|
|
|
|
|
#include <linux/random.h>
|
2024-03-06 18:19:21 +08:00
|
|
|
#include <linux/cpu.h>
|
2010-03-24 17:04:11 +09:00
|
|
|
#include <linux/slab.h>
|
2008-02-04 22:31:14 -08:00
|
|
|
#include <linux/sched.h>
|
2017-02-08 18:51:35 +01:00
|
|
|
#include <linux/sched/debug.h>
|
2017-02-08 18:51:36 +01:00
|
|
|
#include <linux/sched/task.h>
|
2017-02-08 18:51:37 +01:00
|
|
|
#include <linux/sched/task_stack.h>
|
2009-12-14 18:00:11 -08:00
|
|
|
#include <linux/seq_file.h>
|
2008-02-04 22:31:14 -08:00
|
|
|
#include <linux/tick.h>
|
|
|
|
|
#include <linux/threads.h>
|
2022-02-09 12:20:45 -06:00
|
|
|
#include <linux/resume_user_mode.h>
|
2008-02-04 22:31:14 -08:00
|
|
|
#include <asm/current.h>
|
2011-08-18 20:07:59 +01:00
|
|
|
#include <asm/mmu_context.h>
|
2024-03-06 18:19:20 +08:00
|
|
|
#include <asm/switch_to.h>
|
2024-03-06 18:19:21 +08:00
|
|
|
#include <asm/exec.h>
|
2016-12-24 11:46:01 -08:00
|
|
|
#include <linux/uaccess.h>
|
2012-10-08 03:27:32 +01:00
|
|
|
#include <as-layout.h>
|
|
|
|
|
#include <kern_util.h>
|
|
|
|
|
#include <os.h>
|
|
|
|
|
#include <skas.h>
|
2021-09-20 21:32:50 +00:00
|
|
|
#include <registers.h>
|
2020-02-13 14:26:44 +01:00
|
|
|
#include <linux/time-internal.h>
|
2022-09-03 20:23:56 -04:00
|
|
|
#include <linux/elfcore.h>
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2007-10-16 01:27:00 -07:00
|
|
|
/*
|
|
|
|
|
* This is a per-cpu array. A processor only modifies its entry and it only
|
2005-04-16 15:20:36 -07:00
|
|
|
* cares about its entry, so it's OK if another processor is modifying its
|
|
|
|
|
* entry.
|
|
|
|
|
*/
|
2025-09-24 11:32:13 +02:00
|
|
|
struct task_struct *cpu_tasks[NR_CPUS] = {
|
|
|
|
|
[0 ... NR_CPUS - 1] = &init_task,
|
|
|
|
|
};
|
2024-11-11 11:29:10 +01:00
|
|
|
EXPORT_SYMBOL(cpu_tasks);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
|
|
|
|
void free_stack(unsigned long stack, int order)
|
|
|
|
|
{
|
|
|
|
|
free_pages(stack, order);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-10 19:05:08 +01:00
|
|
|
unsigned long alloc_stack(int order, int atomic)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
2021-01-10 19:05:08 +01:00
|
|
|
unsigned long page;
|
2005-10-21 03:22:24 -04:00
|
|
|
gfp_t flags = GFP_KERNEL;
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2005-09-22 21:44:20 -07:00
|
|
|
if (atomic)
|
|
|
|
|
flags = GFP_ATOMIC;
|
2021-01-10 19:05:08 +01:00
|
|
|
page = __get_free_pages(flags, order);
|
2007-10-16 01:26:46 -07:00
|
|
|
|
2021-01-10 19:05:08 +01:00
|
|
|
return page;
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
|
|
|
|
|
2007-05-06 14:51:21 -07:00
|
|
|
static inline void set_current(struct task_struct *task)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
2024-11-11 11:29:10 +01:00
|
|
|
cpu_tasks[task_thread_info(task)->cpu] = task;
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
|
|
|
|
|
2024-03-06 18:19:20 +08:00
|
|
|
struct task_struct *__switch_to(struct task_struct *from, struct task_struct *to)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
2006-09-27 01:50:40 -07:00
|
|
|
to->thread.prev_sched = from;
|
|
|
|
|
set_current(to);
|
2005-09-16 19:27:43 -07:00
|
|
|
|
2013-09-23 17:38:03 +02:00
|
|
|
switch_threads(&from->thread.switch_buf, &to->thread.switch_buf);
|
|
|
|
|
arch_switch_to(current);
|
2005-09-16 19:27:43 -07:00
|
|
|
|
2007-05-06 14:51:21 -07:00
|
|
|
return current->thread.prev_sched;
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void interrupt_end(void)
|
|
|
|
|
{
|
2015-07-03 12:44:20 -07:00
|
|
|
struct pt_regs *regs = ¤t->thread.regs;
|
2025-07-04 14:34:47 +02:00
|
|
|
unsigned long thread_flags;
|
2015-07-03 12:44:20 -07:00
|
|
|
|
2025-07-04 14:34:47 +02:00
|
|
|
thread_flags = read_thread_flags();
|
|
|
|
|
while (thread_flags & _TIF_WORK_MASK) {
|
|
|
|
|
if (thread_flags & _TIF_NEED_RESCHED)
|
|
|
|
|
schedule();
|
|
|
|
|
if (thread_flags & (_TIF_SIGPENDING | _TIF_NOTIFY_SIGNAL))
|
|
|
|
|
do_signal(regs);
|
|
|
|
|
if (thread_flags & _TIF_NOTIFY_RESUME)
|
|
|
|
|
resume_user_mode_work(regs);
|
|
|
|
|
thread_flags = read_thread_flags();
|
|
|
|
|
}
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
|
|
|
|
|
2012-01-30 16:30:48 -05:00
|
|
|
int get_current_pid(void)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
2012-01-30 16:30:48 -05:00
|
|
|
return task_pid_nr(current);
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
|
|
|
|
|
2007-10-16 01:27:00 -07:00
|
|
|
/*
|
|
|
|
|
* This is called magically, by its address being stuffed in a jmp_buf
|
2007-10-16 01:26:58 -07:00
|
|
|
* and being longjmp-d to.
|
|
|
|
|
*/
|
|
|
|
|
void new_thread_handler(void)
|
|
|
|
|
{
|
2024-03-28 10:06:38 +01:00
|
|
|
int (*fn)(void *);
|
2007-10-16 01:26:58 -07:00
|
|
|
void *arg;
|
|
|
|
|
|
2007-10-16 01:27:00 -07:00
|
|
|
if (current->thread.prev_sched != NULL)
|
2007-10-16 01:26:58 -07:00
|
|
|
schedule_tail(current->thread.prev_sched);
|
|
|
|
|
current->thread.prev_sched = NULL;
|
|
|
|
|
|
2024-08-26 18:08:11 +08:00
|
|
|
fn = current->thread.request.thread.proc;
|
|
|
|
|
arg = current->thread.request.thread.arg;
|
2007-10-16 01:26:58 -07:00
|
|
|
|
2007-10-16 01:27:00 -07:00
|
|
|
/*
|
2012-10-10 21:35:42 -04:00
|
|
|
* callback returns only if the kernel thread execs a process
|
2007-10-16 01:26:58 -07:00
|
|
|
*/
|
2024-03-28 10:06:38 +01:00
|
|
|
fn(arg);
|
2024-10-05 01:38:21 +02:00
|
|
|
userspace(¤t->thread.regs.regs);
|
2007-10-16 01:26:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Called magically, see new_thread_handler above */
|
2024-03-06 18:19:17 +08:00
|
|
|
static void fork_handler(void)
|
2007-10-16 01:26:58 -07:00
|
|
|
{
|
|
|
|
|
schedule_tail(current->thread.prev_sched);
|
|
|
|
|
|
2007-10-16 01:27:00 -07:00
|
|
|
/*
|
|
|
|
|
* XXX: if interrupt_end() calls schedule, this call to
|
2007-10-16 01:26:58 -07:00
|
|
|
* arch_switch_to isn't needed. We could want to apply this to
|
2007-10-16 01:27:00 -07:00
|
|
|
* improve performance. -bb
|
|
|
|
|
*/
|
2008-02-04 22:30:49 -08:00
|
|
|
arch_switch_to(current);
|
2007-10-16 01:26:58 -07:00
|
|
|
|
|
|
|
|
current->thread.prev_sched = NULL;
|
|
|
|
|
|
2024-10-05 01:38:21 +02:00
|
|
|
userspace(¤t->thread.regs.regs);
|
2007-10-16 01:26:58 -07:00
|
|
|
}
|
|
|
|
|
|
2022-04-08 18:07:50 -05:00
|
|
|
int copy_thread(struct task_struct * p, const struct kernel_clone_args *args)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
2025-09-01 15:09:52 +02:00
|
|
|
u64 clone_flags = args->flags;
|
2022-04-08 18:07:50 -05:00
|
|
|
unsigned long sp = args->stack;
|
|
|
|
|
unsigned long tls = args->tls;
|
2007-10-16 01:26:58 -07:00
|
|
|
void (*handler)(void);
|
|
|
|
|
int ret = 0;
|
2006-03-31 02:30:22 -08:00
|
|
|
|
2005-04-16 15:20:36 -07:00
|
|
|
p->thread = (struct thread_struct) INIT_THREAD;
|
2006-03-31 02:30:22 -08:00
|
|
|
|
2022-04-12 10:18:48 -05:00
|
|
|
if (!args->fn) {
|
2012-10-29 21:36:45 -04:00
|
|
|
memcpy(&p->thread.regs.regs, current_pt_regs(),
|
2007-10-16 01:26:58 -07:00
|
|
|
sizeof(p->thread.regs.regs));
|
2012-05-22 21:16:35 -04:00
|
|
|
PT_REGS_SET_SYSCALL_RETURN(&p->thread.regs, 0);
|
2007-10-16 01:27:00 -07:00
|
|
|
if (sp != 0)
|
2007-10-16 01:27:07 -07:00
|
|
|
REGS_SP(p->thread.regs.regs.gp) = sp;
|
2006-03-31 02:30:22 -08:00
|
|
|
|
2007-10-16 01:26:58 -07:00
|
|
|
handler = fork_handler;
|
2006-03-31 02:30:22 -08:00
|
|
|
|
2007-10-16 01:26:58 -07:00
|
|
|
arch_copy_thread(¤t->thread.arch, &p->thread.arch);
|
2012-09-20 09:28:25 -04:00
|
|
|
} else {
|
2011-09-14 16:21:23 -07:00
|
|
|
get_safe_registers(p->thread.regs.regs.gp, p->thread.regs.regs.fp);
|
2024-08-26 18:08:11 +08:00
|
|
|
p->thread.request.thread.proc = args->fn;
|
|
|
|
|
p->thread.request.thread.arg = args->fn_arg;
|
2007-10-16 01:26:58 -07:00
|
|
|
handler = new_thread_handler;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
new_thread(task_stack_page(p), &p->thread.switch_buf, handler);
|
|
|
|
|
|
2022-04-12 10:18:48 -05:00
|
|
|
if (!args->fn) {
|
2007-10-16 01:26:58 -07:00
|
|
|
clear_flushed_tls(p);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Set a new TLS for the child thread?
|
|
|
|
|
*/
|
|
|
|
|
if (clone_flags & CLONE_SETTLS)
|
2020-01-04 13:39:30 +01:00
|
|
|
ret = arch_set_tls(p, tls);
|
2007-10-16 01:26:58 -07:00
|
|
|
}
|
2006-03-31 02:30:22 -08:00
|
|
|
|
|
|
|
|
return ret;
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void initial_thread_cb(void (*proc)(void *), void *arg)
|
|
|
|
|
{
|
2007-10-16 01:26:56 -07:00
|
|
|
initial_thread_cb_skas(proc, arg);
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
2006-09-27 01:50:40 -07:00
|
|
|
|
2024-10-23 11:41:20 +02:00
|
|
|
int arch_dup_task_struct(struct task_struct *dst,
|
|
|
|
|
struct task_struct *src)
|
|
|
|
|
{
|
2024-12-17 21:27:44 +01:00
|
|
|
/* init_task is not dynamically sized (missing FPU state) */
|
|
|
|
|
if (unlikely(src == &init_task)) {
|
|
|
|
|
memcpy(dst, src, sizeof(init_task));
|
|
|
|
|
memset((void *)dst + sizeof(init_task), 0,
|
|
|
|
|
arch_task_struct_size - sizeof(init_task));
|
|
|
|
|
} else {
|
|
|
|
|
memcpy(dst, src, arch_task_struct_size);
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-23 11:41:20 +02:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-02 20:58:07 +01:00
|
|
|
void um_idle_sleep(void)
|
2019-05-27 10:34:27 +02:00
|
|
|
{
|
2020-12-02 20:58:04 +01:00
|
|
|
if (time_travel_mode != TT_MODE_OFF)
|
|
|
|
|
time_travel_sleep();
|
|
|
|
|
else
|
|
|
|
|
os_idle_sleep();
|
2019-05-27 10:34:27 +02:00
|
|
|
}
|
|
|
|
|
|
2013-04-16 23:53:29 +02:00
|
|
|
void arch_cpu_idle(void)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
2019-05-27 10:34:27 +02:00
|
|
|
um_idle_sleep();
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
|
|
|
|
|
2025-10-27 08:18:12 +08:00
|
|
|
void arch_cpu_idle_prepare(void)
|
|
|
|
|
{
|
|
|
|
|
os_idle_prepare();
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-21 15:34:44 +01:00
|
|
|
int __uml_cant_sleep(void) {
|
2006-01-18 17:42:58 -08:00
|
|
|
return in_atomic() || irqs_disabled() || in_interrupt();
|
|
|
|
|
/* Is in_interrupt() really needed? */
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
|
|
|
|
|
2025-10-27 08:18:10 +08:00
|
|
|
int uml_need_resched(void)
|
|
|
|
|
{
|
|
|
|
|
return need_resched();
|
|
|
|
|
}
|
|
|
|
|
|
2005-04-16 15:20:36 -07:00
|
|
|
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)();
|
|
|
|
|
}
|
|
|
|
|
|
2008-02-04 22:30:41 -08:00
|
|
|
char *uml_strdup(const char *string)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
2005-06-23 00:09:04 -07:00
|
|
|
return kstrdup(string, GFP_KERNEL);
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
2011-08-18 20:14:10 +01:00
|
|
|
EXPORT_SYMBOL(uml_strdup);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
|
|
|
|
int copy_from_user_proc(void *to, void __user *from, int size)
|
|
|
|
|
{
|
2007-05-06 14:51:21 -07:00
|
|
|
return copy_from_user(to, from, size);
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
|
|
|
|
|
2023-11-10 12:03:38 +01:00
|
|
|
int singlestepping(void)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
2023-11-10 12:03:38 +01:00
|
|
|
return test_thread_flag(TIF_SINGLESTEP);
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
|
|
|
|
|
2005-05-06 21:30:53 -07:00
|
|
|
/*
|
|
|
|
|
* Only x86 and x86_64 have an arch_align_stack().
|
|
|
|
|
* All other arches have "#define arch_align_stack(x) (x)"
|
2014-04-07 15:39:22 -07:00
|
|
|
* in their asm/exec.h
|
2005-05-06 21:30:53 -07:00
|
|
|
* 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
|
2005-04-16 15:20:36 -07:00
|
|
|
unsigned long arch_align_stack(unsigned long sp)
|
|
|
|
|
{
|
2006-09-25 23:33:01 -07:00
|
|
|
if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
|
2022-10-09 20:44:02 -06:00
|
|
|
sp -= get_random_u32_below(8192);
|
2005-04-16 15:20:36 -07:00
|
|
|
return sp & ~0xf;
|
|
|
|
|
}
|
2005-05-06 21:30:53 -07:00
|
|
|
#endif
|
2008-02-04 22:30:36 -08:00
|
|
|
|
2021-09-29 15:02:14 -07:00
|
|
|
unsigned long __get_wchan(struct task_struct *p)
|
2008-02-04 22:30:36 -08:00
|
|
|
{
|
|
|
|
|
unsigned long stack_page, sp, ip;
|
|
|
|
|
bool seen_sched = 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;
|
|
|
|
|
}
|