// SPDX-License-Identifier: GPL-2.0-only1/*2* Process creation support for Hexagon3*4* Copyright (c) 2010-2012, The Linux Foundation. All rights reserved.5*/67#include <linux/cpu.h>8#include <linux/sched.h>9#include <linux/sched/debug.h>10#include <linux/sched/task.h>11#include <linux/sched/task_stack.h>12#include <linux/types.h>13#include <linux/module.h>14#include <linux/tick.h>15#include <linux/uaccess.h>16#include <linux/slab.h>17#include <linux/resume_user_mode.h>1819/*20* Program thread launch. Often defined as a macro in processor.h,21* but we're shooting for a small footprint and it's not an inner-loop22* performance-critical operation.23*24* The Hexagon ABI specifies that R28 is zero'ed before program launch,25* so that gets automatically done here. If we ever stop doing that here,26* we'll probably want to define the ELF_PLAT_INIT macro.27*/28void start_thread(struct pt_regs *regs, unsigned long pc, unsigned long sp)29{30/* We want to zero all data-containing registers. Is this overkill? */31memset(regs, 0, sizeof(*regs));32/* We might want to also zero all Processor registers here */33pt_set_usermode(regs);34pt_set_elr(regs, pc);35pt_set_rte_sp(regs, sp);36}3738/*39* Spin, or better still, do a hardware or VM wait instruction40* If hardware or VM offer wait termination even though interrupts41* are disabled.42*/43void arch_cpu_idle(void)44{45__vmwait();46/* interrupts wake us up, but irqs are still disabled */47}4849/*50* Copy architecture-specific thread state51*/52int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)53{54unsigned long clone_flags = args->flags;55unsigned long usp = args->stack;56unsigned long tls = args->tls;57struct thread_info *ti = task_thread_info(p);58struct hexagon_switch_stack *ss;59struct pt_regs *childregs;60asmlinkage void ret_from_fork(void);6162childregs = (struct pt_regs *) (((unsigned long) ti + THREAD_SIZE) -63sizeof(*childregs));6465ti->regs = childregs;6667/*68* Establish kernel stack pointer and initial PC for new thread69* Note that unlike the usual situation, we do not copy the70* parent's callee-saved here; those are in pt_regs and whatever71* we leave here will be overridden on return to userland.72*/73ss = (struct hexagon_switch_stack *) ((unsigned long) childregs -74sizeof(*ss));75ss->lr = (unsigned long)ret_from_fork;76p->thread.switch_sp = ss;77if (unlikely(args->fn)) {78memset(childregs, 0, sizeof(struct pt_regs));79/* r24 <- fn, r25 <- arg */80ss->r24 = (unsigned long)args->fn;81ss->r25 = (unsigned long)args->fn_arg;82pt_set_kmode(childregs);83return 0;84}85memcpy(childregs, current_pt_regs(), sizeof(*childregs));86ss->r2524 = 0;8788if (usp)89pt_set_rte_sp(childregs, usp);9091/* Child sees zero return value */92childregs->r00 = 0;9394/*95* The clone syscall has the C signature:96* int [r0] clone(int flags [r0],97* void *child_frame [r1],98* void *parent_tid [r2],99* void *child_tid [r3],100* void *thread_control_block [r4]);101* ugp is used to provide TLS support.102*/103if (clone_flags & CLONE_SETTLS)104childregs->ugp = tls;105106/*107* Parent sees new pid -- not necessary, not even possible at108* this point in the fork process109*/110111return 0;112}113114/*115* Some archs flush debug and FPU info here116*/117void flush_thread(void)118{119}120121/*122* The "wait channel" terminology is archaic, but what we want123* is an identification of the point at which the scheduler124* was invoked by a blocked thread.125*/126unsigned long __get_wchan(struct task_struct *p)127{128unsigned long fp, pc;129unsigned long stack_page;130int count = 0;131132stack_page = (unsigned long)task_stack_page(p);133fp = ((struct hexagon_switch_stack *)p->thread.switch_sp)->fp;134do {135if (fp < (stack_page + sizeof(struct thread_info)) ||136fp >= (THREAD_SIZE - 8 + stack_page))137return 0;138pc = ((unsigned long *)fp)[1];139if (!in_sched_functions(pc))140return pc;141fp = *(unsigned long *) fp;142} while (count++ < 16);143144return 0;145}146147/*148* Called on the exit path of event entry; see vm_entry.S149*150* Interrupts will already be disabled.151*152* Returns 0 if there's no need to re-check for more work.153*/154155int do_work_pending(struct pt_regs *regs, u32 thread_info_flags);156int do_work_pending(struct pt_regs *regs, u32 thread_info_flags)157{158if (!(thread_info_flags & _TIF_WORK_MASK)) {159return 0;160} /* shortcut -- no work to be done */161162local_irq_enable();163164if (thread_info_flags & _TIF_NEED_RESCHED) {165schedule();166return 1;167}168169if (thread_info_flags & (_TIF_SIGPENDING | _TIF_NOTIFY_SIGNAL)) {170do_signal(regs);171return 1;172}173174if (thread_info_flags & _TIF_NOTIFY_RESUME) {175resume_user_mode_work(regs);176return 1;177}178179/* Should not even reach here */180panic("%s: bad thread_info flags 0x%08x\n", __func__,181thread_info_flags);182}183184185