Files

310 lines
6.8 KiB
C
Raw Permalink Normal View History

// 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>
#include <linux/slab.h>
2008-02-04 22:31:14 -08:00
#include <linux/sched.h>
#include <linux/sched/debug.h>
#include <linux/sched/task.h>
#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>
#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>
#include <linux/uaccess.h>
#include <as-layout.h>
#include <kern_util.h>
#include <os.h>
#include <skas.h>
#include <registers.h>
2020-02-13 14:26:44 +01:00
#include <linux/time-internal.h>
#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);
}
unsigned long alloc_stack(int order, int atomic)
2005-04-16 15:20:36 -07: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;
page = __get_free_pages(flags, order);
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 = &current->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
* and being longjmp-d to.
*/
void new_thread_handler(void)
{
2024-03-28 10:06:38 +01:00
int (*fn)(void *);
void *arg;
2007-10-16 01:27:00 -07:00
if (current->thread.prev_sched != NULL)
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:27:00 -07:00
/*
* callback returns only if the kernel thread execs a process
*/
2024-03-28 10:06:38 +01:00
fn(arg);
2024-10-05 01:38:21 +02:00
userspace(&current->thread.regs.regs);
}
/* Called magically, see new_thread_handler above */
static void fork_handler(void)
{
schedule_tail(current->thread.prev_sched);
2007-10-16 01:27:00 -07:00
/*
* XXX: if interrupt_end() calls schedule, this call to
* arch_switch_to isn't needed. We could want to apply this to
2007-10-16 01:27:00 -07:00
* improve performance. -bb
*/
arch_switch_to(current);
current->thread.prev_sched = NULL;
2024-10-05 01:38:21 +02:00
userspace(&current->thread.regs.regs);
}
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;
unsigned long sp = args->stack;
unsigned long tls = args->tls;
void (*handler)(void);
int ret = 0;
2005-04-16 15:20:36 -07:00
p->thread = (struct thread_struct) INIT_THREAD;
2022-04-12 10:18:48 -05:00
if (!args->fn) {
memcpy(&p->thread.regs.regs, current_pt_regs(),
sizeof(p->thread.regs.regs));
PT_REGS_SET_SYSCALL_RETURN(&p->thread.regs, 0);
2007-10-16 01:27:00 -07:00
if (sp != 0)
REGS_SP(p->thread.regs.regs.gp) = sp;
handler = fork_handler;
arch_copy_thread(&current->thread.arch, &p->thread.arch);
2012-09-20 09:28:25 -04:00
} else {
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;
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) {
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);
}
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
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);
}
return 0;
}
2020-12-02 20:58:07 +01:00
void um_idle_sleep(void)
2019-05-27 10:34:27 +02: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();
}
int __uml_cant_sleep(void) {
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
{
return kstrdup(string, GFP_KERNEL);
2005-04-16 15:20:36 -07: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
}
int singlestepping(void)
2005-04-16 15:20:36 -07:00
{
return test_thread_flag(TIF_SINGLESTEP);
2005-04-16 15:20:36 -07:00
}
/*
* Only x86 and x86_64 have an arch_align_stack().
* All other arches have "#define arch_align_stack(x) (x)"
* in their asm/exec.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
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)
sp -= get_random_u32_below(8192);
2005-04-16 15:20:36 -07:00
return sp & ~0xf;
}
#endif
2008-02-04 22:30:36 -08: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;
}