// SPDX-License-Identifier: GPL-2.0-only1/*2* Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)3*4* Amit Bhor, Kanika Nema: Codito Technologies 20045*/67#include <linux/errno.h>8#include <linux/module.h>9#include <linux/sched.h>10#include <linux/sched/task.h>11#include <linux/sched/task_stack.h>1213#include <linux/mm.h>14#include <linux/fs.h>15#include <linux/unistd.h>16#include <linux/ptrace.h>17#include <linux/slab.h>18#include <linux/syscalls.h>19#include <linux/elf.h>20#include <linux/tick.h>2122#include <asm/fpu.h>2324SYSCALL_DEFINE1(arc_settls, void *, user_tls_data_ptr)25{26task_thread_info(current)->thr_ptr = (unsigned int)user_tls_data_ptr;27return 0;28}2930/*31* We return the user space TLS data ptr as sys-call return code32* Ideally it should be copy to user.33* However we can cheat by the fact that some sys-calls do return34* absurdly high values35* Since the tls dat aptr is not going to be in range of 0xFFFF_xxxx36* it won't be considered a sys-call error37* and it will be loads better than copy-to-user, which is a definite38* D-TLB Miss39*/40SYSCALL_DEFINE0(arc_gettls)41{42return task_thread_info(current)->thr_ptr;43}4445SYSCALL_DEFINE3(arc_usr_cmpxchg, int __user *, uaddr, int, expected, int, new)46{47struct pt_regs *regs = current_pt_regs();48u32 uval;49int ret;5051/*52* This is only for old cores lacking LLOCK/SCOND, which by definition53* can't possibly be SMP. Thus doesn't need to be SMP safe.54* And this also helps reduce the overhead for serializing in55* the UP case56*/57WARN_ON_ONCE(IS_ENABLED(CONFIG_SMP));5859/* Z indicates to userspace if operation succeeded */60regs->status32 &= ~STATUS_Z_MASK;6162ret = access_ok(uaddr, sizeof(*uaddr));63if (!ret)64goto fail;6566again:67preempt_disable();6869ret = __get_user(uval, uaddr);70if (ret)71goto fault;7273if (uval != expected)74goto out;7576ret = __put_user(new, uaddr);77if (ret)78goto fault;7980regs->status32 |= STATUS_Z_MASK;8182out:83preempt_enable();84return uval;8586fault:87preempt_enable();8889if (unlikely(ret != -EFAULT))90goto fail;9192mmap_read_lock(current->mm);93ret = fixup_user_fault(current->mm, (unsigned long) uaddr,94FAULT_FLAG_WRITE, NULL);95mmap_read_unlock(current->mm);9697if (likely(!ret))98goto again;99100fail:101force_sig(SIGSEGV);102return ret;103}104105#ifdef CONFIG_ISA_ARCV2106107void arch_cpu_idle(void)108{109/* Re-enable interrupts <= default irq priority before committing SLEEP */110const unsigned int arg = 0x10 | ARCV2_IRQ_DEF_PRIO;111112__asm__ __volatile__(113"sleep %0 \n"114:115:"I"(arg)); /* can't be "r" has to be embedded const */116117raw_local_irq_disable();118}119120#else /* ARC700 */121122void arch_cpu_idle(void)123{124/* sleep, but enable both set E1/E2 (levels of interrupts) before committing */125__asm__ __volatile__("sleep 0x3 \n");126raw_local_irq_disable();127}128129#endif130131asmlinkage void ret_from_fork(void);132133/*134* Copy architecture-specific thread state135*136* Layout of Child kernel mode stack as setup at the end of this function is137*138* | ... |139* | ... |140* | unused |141* | |142* ------------------143* | r25 | <==== top of Stack (thread_info.ksp)144* ~ ~145* | --to-- | (CALLEE Regs of kernel mode)146* | r13 |147* ------------------148* | fp |149* | blink | @ret_from_fork150* ------------------151* | |152* ~ ~153* ~ ~154* | |155* ------------------156* | r12 |157* ~ ~158* | --to-- | (scratch Regs of user mode)159* | r0 |160* ------------------161* | SP |162* | orig_r0 |163* | event/ECR |164* ------------------ <===== END of PAGE165*/166int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)167{168unsigned long clone_flags = args->flags;169unsigned long usp = args->stack;170unsigned long tls = args->tls;171struct pt_regs *c_regs; /* child's pt_regs */172unsigned long *childksp; /* to unwind out of __switch_to() */173struct callee_regs *c_callee; /* child's callee regs */174struct callee_regs *parent_callee; /* paren't callee */175struct pt_regs *regs = current_pt_regs();176177/* Mark the specific anchors to begin with (see pic above) */178c_regs = task_pt_regs(p);179childksp = (unsigned long *)c_regs - 2; /* 2 words for FP/BLINK */180c_callee = ((struct callee_regs *)childksp) - 1;181182/*183* __switch_to() uses thread_info.ksp to start unwinding stack184* For kernel threads we don't need to create callee regs, the185* stack layout nevertheless needs to remain the same.186* Also, since __switch_to anyways unwinds callee regs, we use187* this to populate kernel thread entry-pt/args into callee regs,188* so that ret_from_kernel_thread() becomes simpler.189*/190task_thread_info(p)->ksp = (unsigned long)c_callee; /* THREAD_INFO_KSP */191192/* __switch_to expects FP(0), BLINK(return addr) at top */193childksp[0] = 0; /* fp */194childksp[1] = (unsigned long)ret_from_fork; /* blink */195196if (unlikely(args->fn)) {197memset(c_regs, 0, sizeof(struct pt_regs));198199c_callee->r13 = (unsigned long)args->fn_arg;200c_callee->r14 = (unsigned long)args->fn;201202return 0;203}204205/*--------- User Task Only --------------*/206207/* __switch_to expects FP(0), BLINK(return addr) at top of stack */208childksp[0] = 0; /* for POP fp */209childksp[1] = (unsigned long)ret_from_fork; /* for POP blink */210211/* Copy parents pt regs on child's kernel mode stack */212*c_regs = *regs;213214if (usp)215c_regs->sp = usp;216217c_regs->r0 = 0; /* fork returns 0 in child */218219parent_callee = ((struct callee_regs *)regs) - 1;220*c_callee = *parent_callee;221222if (unlikely(clone_flags & CLONE_SETTLS)) {223/*224* set task's userland tls data ptr from 4th arg225* clone C-lib call is difft from clone sys-call226*/227task_thread_info(p)->thr_ptr = tls;228} else {229/* Normal fork case: set parent's TLS ptr in child */230task_thread_info(p)->thr_ptr =231task_thread_info(current)->thr_ptr;232}233234235/*236* setup usermode thread pointer #1:237* when child is picked by scheduler, __switch_to() uses @c_callee to238* populate usermode callee regs: this works (despite being in a kernel239* function) since special return path for child @ret_from_fork()240* ensures those regs are not clobbered all the way to RTIE to usermode241*/242c_callee->r25 = task_thread_info(p)->thr_ptr;243244return 0;245}246247/*248* Do necessary setup to start up a new user task249*/250void start_thread(struct pt_regs *regs, unsigned long pc, unsigned long usp)251{252regs->sp = usp;253regs->ret = pc;254255/*256* [U]ser Mode bit set257* [L] ZOL loop inhibited to begin with - cleared by a LP insn258* Interrupts enabled259*/260regs->status32 = STATUS_U_MASK | STATUS_L_MASK | ISA_INIT_STATUS_BITS;261262fpu_init_task(regs);263264/* bogus seed values for debugging */265regs->lp_start = 0x10;266regs->lp_end = 0x80;267}268269/*270* Some archs flush debug and FPU info here271*/272void flush_thread(void)273{274}275276int elf_check_arch(const struct elf32_hdr *x)277{278unsigned int eflags;279280if (x->e_machine != EM_ARC_INUSE) {281pr_err("ELF not built for %s ISA\n",282is_isa_arcompact() ? "ARCompact":"ARCv2");283return 0;284}285286eflags = x->e_flags;287if ((eflags & EF_ARC_OSABI_MSK) != EF_ARC_OSABI_CURRENT) {288pr_err("ABI mismatch - you need newer toolchain\n");289force_fatal_sig(SIGSEGV);290return 0;291}292293return 1;294}295EXPORT_SYMBOL(elf_check_arch);296297298