108dbd0f8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
24b30f965SRichard Kuo /*
34b30f965SRichard Kuo * Process creation support for Hexagon
44b30f965SRichard Kuo *
5e1858b2aSRichard Kuo * Copyright (c) 2010-2012, The Linux Foundation. All rights reserved.
64b30f965SRichard Kuo */
74b30f965SRichard Kuo
8b0f73122SNathan Chancellor #include <linux/cpu.h>
94b30f965SRichard Kuo #include <linux/sched.h>
10b17b0153SIngo Molnar #include <linux/sched/debug.h>
1129930025SIngo Molnar #include <linux/sched/task.h>
1268db0cf1SIngo Molnar #include <linux/sched/task_stack.h>
134b30f965SRichard Kuo #include <linux/types.h>
144b30f965SRichard Kuo #include <linux/module.h>
154b30f965SRichard Kuo #include <linux/tick.h>
164b30f965SRichard Kuo #include <linux/uaccess.h>
174b30f965SRichard Kuo #include <linux/slab.h>
1803248addSEric W. Biederman #include <linux/resume_user_mode.h>
194b30f965SRichard Kuo
204b30f965SRichard Kuo /*
214b30f965SRichard Kuo * Program thread launch. Often defined as a macro in processor.h,
224b30f965SRichard Kuo * but we're shooting for a small footprint and it's not an inner-loop
234b30f965SRichard Kuo * performance-critical operation.
244b30f965SRichard Kuo *
254b30f965SRichard Kuo * The Hexagon ABI specifies that R28 is zero'ed before program launch,
264b30f965SRichard Kuo * so that gets automatically done here. If we ever stop doing that here,
274b30f965SRichard Kuo * we'll probably want to define the ELF_PLAT_INIT macro.
284b30f965SRichard Kuo */
start_thread(struct pt_regs * regs,unsigned long pc,unsigned long sp)294b30f965SRichard Kuo void start_thread(struct pt_regs *regs, unsigned long pc, unsigned long sp)
304b30f965SRichard Kuo {
314b30f965SRichard Kuo /* We want to zero all data-containing registers. Is this overkill? */
324b30f965SRichard Kuo memset(regs, 0, sizeof(*regs));
334b30f965SRichard Kuo /* We might want to also zero all Processor registers here */
344b30f965SRichard Kuo pt_set_usermode(regs);
354b30f965SRichard Kuo pt_set_elr(regs, pc);
364b30f965SRichard Kuo pt_set_rte_sp(regs, sp);
374b30f965SRichard Kuo }
384b30f965SRichard Kuo
394b30f965SRichard Kuo /*
404b30f965SRichard Kuo * Spin, or better still, do a hardware or VM wait instruction
414b30f965SRichard Kuo * If hardware or VM offer wait termination even though interrupts
424b30f965SRichard Kuo * are disabled.
434b30f965SRichard Kuo */
arch_cpu_idle(void)444e0fcc56SThomas Gleixner void arch_cpu_idle(void)
454b30f965SRichard Kuo {
464b30f965SRichard Kuo __vmwait();
474e0fcc56SThomas Gleixner /* interrupts wake us up, but irqs are still disabled */
484b30f965SRichard Kuo }
494b30f965SRichard Kuo
504b30f965SRichard Kuo /*
514b30f965SRichard Kuo * Copy architecture-specific thread state
524b30f965SRichard Kuo */
copy_thread(struct task_struct * p,const struct kernel_clone_args * args)53c5febea0SEric W. Biederman int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
544b30f965SRichard Kuo {
55c5febea0SEric W. Biederman unsigned long clone_flags = args->flags;
56c5febea0SEric W. Biederman unsigned long usp = args->stack;
57c5febea0SEric W. Biederman unsigned long tls = args->tls;
584b30f965SRichard Kuo struct thread_info *ti = task_thread_info(p);
594b30f965SRichard Kuo struct hexagon_switch_stack *ss;
604b30f965SRichard Kuo struct pt_regs *childregs;
614b30f965SRichard Kuo asmlinkage void ret_from_fork(void);
624b30f965SRichard Kuo
634b30f965SRichard Kuo childregs = (struct pt_regs *) (((unsigned long) ti + THREAD_SIZE) -
644b30f965SRichard Kuo sizeof(*childregs));
654b30f965SRichard Kuo
664b30f965SRichard Kuo ti->regs = childregs;
674b30f965SRichard Kuo
684b30f965SRichard Kuo /*
694b30f965SRichard Kuo * Establish kernel stack pointer and initial PC for new thread
7099521855SAl Viro * Note that unlike the usual situation, we do not copy the
7199521855SAl Viro * parent's callee-saved here; those are in pt_regs and whatever
7299521855SAl Viro * we leave here will be overridden on return to userland.
734b30f965SRichard Kuo */
744b30f965SRichard Kuo ss = (struct hexagon_switch_stack *) ((unsigned long) childregs -
754b30f965SRichard Kuo sizeof(*ss));
764b30f965SRichard Kuo ss->lr = (unsigned long)ret_from_fork;
774b30f965SRichard Kuo p->thread.switch_sp = ss;
785bd2e97cSEric W. Biederman if (unlikely(args->fn)) {
7999521855SAl Viro memset(childregs, 0, sizeof(struct pt_regs));
8099521855SAl Viro /* r24 <- fn, r25 <- arg */
815bd2e97cSEric W. Biederman ss->r24 = (unsigned long)args->fn;
825bd2e97cSEric W. Biederman ss->r25 = (unsigned long)args->fn_arg;
8399521855SAl Viro pt_set_kmode(childregs);
8499521855SAl Viro return 0;
8599521855SAl Viro }
86f01aceacSAl Viro memcpy(childregs, current_pt_regs(), sizeof(*childregs));
8799521855SAl Viro ss->r2524 = 0;
884b30f965SRichard Kuo
89f01aceacSAl Viro if (usp)
904b30f965SRichard Kuo pt_set_rte_sp(childregs, usp);
914b30f965SRichard Kuo
924b30f965SRichard Kuo /* Child sees zero return value */
934b30f965SRichard Kuo childregs->r00 = 0;
944b30f965SRichard Kuo
954b30f965SRichard Kuo /*
964b30f965SRichard Kuo * The clone syscall has the C signature:
974b30f965SRichard Kuo * int [r0] clone(int flags [r0],
984b30f965SRichard Kuo * void *child_frame [r1],
994b30f965SRichard Kuo * void *parent_tid [r2],
1004b30f965SRichard Kuo * void *child_tid [r3],
1014b30f965SRichard Kuo * void *thread_control_block [r4]);
1024b30f965SRichard Kuo * ugp is used to provide TLS support.
1034b30f965SRichard Kuo */
1044b30f965SRichard Kuo if (clone_flags & CLONE_SETTLS)
105643d48b8SChristian Brauner childregs->ugp = tls;
1064b30f965SRichard Kuo
1074b30f965SRichard Kuo /*
1084b30f965SRichard Kuo * Parent sees new pid -- not necessary, not even possible at
1094b30f965SRichard Kuo * this point in the fork process
1104b30f965SRichard Kuo */
1114b30f965SRichard Kuo
1124b30f965SRichard Kuo return 0;
1134b30f965SRichard Kuo }
1144b30f965SRichard Kuo
1154b30f965SRichard Kuo /*
1164b30f965SRichard Kuo * Some archs flush debug and FPU info here
1174b30f965SRichard Kuo */
flush_thread(void)1184b30f965SRichard Kuo void flush_thread(void)
1194b30f965SRichard Kuo {
1204b30f965SRichard Kuo }
1214b30f965SRichard Kuo
1224b30f965SRichard Kuo /*
1234b30f965SRichard Kuo * The "wait channel" terminology is archaic, but what we want
1244b30f965SRichard Kuo * is an identification of the point at which the scheduler
1254b30f965SRichard Kuo * was invoked by a blocked thread.
1264b30f965SRichard Kuo */
__get_wchan(struct task_struct * p)12742a20f86SKees Cook unsigned long __get_wchan(struct task_struct *p)
1284b30f965SRichard Kuo {
1294b30f965SRichard Kuo unsigned long fp, pc;
1304b30f965SRichard Kuo unsigned long stack_page;
1314b30f965SRichard Kuo int count = 0;
1324b30f965SRichard Kuo
1334b30f965SRichard Kuo stack_page = (unsigned long)task_stack_page(p);
1344b30f965SRichard Kuo fp = ((struct hexagon_switch_stack *)p->thread.switch_sp)->fp;
1354b30f965SRichard Kuo do {
1364b30f965SRichard Kuo if (fp < (stack_page + sizeof(struct thread_info)) ||
1374b30f965SRichard Kuo fp >= (THREAD_SIZE - 8 + stack_page))
1384b30f965SRichard Kuo return 0;
1394b30f965SRichard Kuo pc = ((unsigned long *)fp)[1];
1404b30f965SRichard Kuo if (!in_sched_functions(pc))
1414b30f965SRichard Kuo return pc;
1424b30f965SRichard Kuo fp = *(unsigned long *) fp;
1434b30f965SRichard Kuo } while (count++ < 16);
1444b30f965SRichard Kuo
1454b30f965SRichard Kuo return 0;
1464b30f965SRichard Kuo }
1474b30f965SRichard Kuo
1484b30f965SRichard Kuo /*
149a11e67c2SRichard Kuo * Called on the exit path of event entry; see vm_entry.S
150a11e67c2SRichard Kuo *
151a11e67c2SRichard Kuo * Interrupts will already be disabled.
152a11e67c2SRichard Kuo *
153a11e67c2SRichard Kuo * Returns 0 if there's no need to re-check for more work.
154a11e67c2SRichard Kuo */
155a11e67c2SRichard Kuo
156*54ba0eabSNathan Chancellor int do_work_pending(struct pt_regs *regs, u32 thread_info_flags);
do_work_pending(struct pt_regs * regs,u32 thread_info_flags)157a11e67c2SRichard Kuo int do_work_pending(struct pt_regs *regs, u32 thread_info_flags)
158a11e67c2SRichard Kuo {
159f8722a4dSRichard Kuo if (!(thread_info_flags & _TIF_WORK_MASK)) {
160a11e67c2SRichard Kuo return 0;
161a11e67c2SRichard Kuo } /* shortcut -- no work to be done */
162a11e67c2SRichard Kuo
163a11e67c2SRichard Kuo local_irq_enable();
164a11e67c2SRichard Kuo
165a11e67c2SRichard Kuo if (thread_info_flags & _TIF_NEED_RESCHED) {
166a11e67c2SRichard Kuo schedule();
167a11e67c2SRichard Kuo return 1;
168a11e67c2SRichard Kuo }
169a11e67c2SRichard Kuo
170aeec8193SJens Axboe if (thread_info_flags & (_TIF_SIGPENDING | _TIF_NOTIFY_SIGNAL)) {
171a11e67c2SRichard Kuo do_signal(regs);
172a11e67c2SRichard Kuo return 1;
173a11e67c2SRichard Kuo }
174a11e67c2SRichard Kuo
175a11e67c2SRichard Kuo if (thread_info_flags & _TIF_NOTIFY_RESUME) {
17603248addSEric W. Biederman resume_user_mode_work(regs);
177c710f590SRichard Kuo return 1;
178a11e67c2SRichard Kuo }
179a11e67c2SRichard Kuo
180a11e67c2SRichard Kuo /* Should not even reach here */
181a11e67c2SRichard Kuo panic("%s: bad thread_info flags 0x%08x\n", __func__,
182a11e67c2SRichard Kuo thread_info_flags);
183a11e67c2SRichard Kuo }
184