Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  linux/arch/arm/kernel/process.c
0004  *
0005  *  Copyright (C) 1996-2000 Russell King - Converted to ARM.
0006  *  Original Copyright (C) 1995  Linus Torvalds
0007  */
0008 #include <linux/export.h>
0009 #include <linux/sched.h>
0010 #include <linux/sched/debug.h>
0011 #include <linux/sched/task.h>
0012 #include <linux/sched/task_stack.h>
0013 #include <linux/kernel.h>
0014 #include <linux/mm.h>
0015 #include <linux/stddef.h>
0016 #include <linux/unistd.h>
0017 #include <linux/user.h>
0018 #include <linux/interrupt.h>
0019 #include <linux/init.h>
0020 #include <linux/elfcore.h>
0021 #include <linux/pm.h>
0022 #include <linux/tick.h>
0023 #include <linux/utsname.h>
0024 #include <linux/uaccess.h>
0025 #include <linux/random.h>
0026 #include <linux/hw_breakpoint.h>
0027 #include <linux/leds.h>
0028 
0029 #include <asm/processor.h>
0030 #include <asm/thread_notify.h>
0031 #include <asm/stacktrace.h>
0032 #include <asm/system_misc.h>
0033 #include <asm/mach/time.h>
0034 #include <asm/tls.h>
0035 #include <asm/vdso.h>
0036 
0037 #include "signal.h"
0038 
0039 #if defined(CONFIG_CURRENT_POINTER_IN_TPIDRURO) || defined(CONFIG_SMP)
0040 DEFINE_PER_CPU(struct task_struct *, __entry_task);
0041 #endif
0042 
0043 #if defined(CONFIG_STACKPROTECTOR) && !defined(CONFIG_STACKPROTECTOR_PER_TASK)
0044 #include <linux/stackprotector.h>
0045 unsigned long __stack_chk_guard __read_mostly;
0046 EXPORT_SYMBOL(__stack_chk_guard);
0047 #endif
0048 
0049 #ifndef CONFIG_CURRENT_POINTER_IN_TPIDRURO
0050 asmlinkage struct task_struct *__current;
0051 EXPORT_SYMBOL(__current);
0052 #endif
0053 
0054 static const char *processor_modes[] __maybe_unused = {
0055   "USER_26", "FIQ_26" , "IRQ_26" , "SVC_26" , "UK4_26" , "UK5_26" , "UK6_26" , "UK7_26" ,
0056   "UK8_26" , "UK9_26" , "UK10_26", "UK11_26", "UK12_26", "UK13_26", "UK14_26", "UK15_26",
0057   "USER_32", "FIQ_32" , "IRQ_32" , "SVC_32" , "UK4_32" , "UK5_32" , "MON_32" , "ABT_32" ,
0058   "UK8_32" , "UK9_32" , "HYP_32", "UND_32" , "UK12_32", "UK13_32", "UK14_32", "SYS_32"
0059 };
0060 
0061 static const char *isa_modes[] __maybe_unused = {
0062   "ARM" , "Thumb" , "Jazelle", "ThumbEE"
0063 };
0064 
0065 /*
0066  * This is our default idle handler.
0067  */
0068 
0069 void (*arm_pm_idle)(void);
0070 
0071 /*
0072  * Called from the core idle loop.
0073  */
0074 
0075 void arch_cpu_idle(void)
0076 {
0077     if (arm_pm_idle)
0078         arm_pm_idle();
0079     else
0080         cpu_do_idle();
0081     raw_local_irq_enable();
0082 }
0083 
0084 void arch_cpu_idle_prepare(void)
0085 {
0086     local_fiq_enable();
0087 }
0088 
0089 void arch_cpu_idle_enter(void)
0090 {
0091     ledtrig_cpu(CPU_LED_IDLE_START);
0092 #ifdef CONFIG_PL310_ERRATA_769419
0093     wmb();
0094 #endif
0095 }
0096 
0097 void arch_cpu_idle_exit(void)
0098 {
0099     ledtrig_cpu(CPU_LED_IDLE_END);
0100 }
0101 
0102 void __show_regs_alloc_free(struct pt_regs *regs)
0103 {
0104     int i;
0105 
0106     /* check for r0 - r12 only */
0107     for (i = 0; i < 13; i++) {
0108         pr_alert("Register r%d information:", i);
0109         mem_dump_obj((void *)regs->uregs[i]);
0110     }
0111 }
0112 
0113 void __show_regs(struct pt_regs *regs)
0114 {
0115     unsigned long flags;
0116     char buf[64];
0117 #ifndef CONFIG_CPU_V7M
0118     unsigned int domain;
0119 #ifdef CONFIG_CPU_SW_DOMAIN_PAN
0120     /*
0121      * Get the domain register for the parent context. In user
0122      * mode, we don't save the DACR, so lets use what it should
0123      * be. For other modes, we place it after the pt_regs struct.
0124      */
0125     if (user_mode(regs)) {
0126         domain = DACR_UACCESS_ENABLE;
0127     } else {
0128         domain = to_svc_pt_regs(regs)->dacr;
0129     }
0130 #else
0131     domain = get_domain();
0132 #endif
0133 #endif
0134 
0135     show_regs_print_info(KERN_DEFAULT);
0136 
0137     printk("PC is at %pS\n", (void *)instruction_pointer(regs));
0138     printk("LR is at %pS\n", (void *)regs->ARM_lr);
0139     printk("pc : [<%08lx>]    lr : [<%08lx>]    psr: %08lx\n",
0140            regs->ARM_pc, regs->ARM_lr, regs->ARM_cpsr);
0141     printk("sp : %08lx  ip : %08lx  fp : %08lx\n",
0142            regs->ARM_sp, regs->ARM_ip, regs->ARM_fp);
0143     printk("r10: %08lx  r9 : %08lx  r8 : %08lx\n",
0144         regs->ARM_r10, regs->ARM_r9,
0145         regs->ARM_r8);
0146     printk("r7 : %08lx  r6 : %08lx  r5 : %08lx  r4 : %08lx\n",
0147         regs->ARM_r7, regs->ARM_r6,
0148         regs->ARM_r5, regs->ARM_r4);
0149     printk("r3 : %08lx  r2 : %08lx  r1 : %08lx  r0 : %08lx\n",
0150         regs->ARM_r3, regs->ARM_r2,
0151         regs->ARM_r1, regs->ARM_r0);
0152 
0153     flags = regs->ARM_cpsr;
0154     buf[0] = flags & PSR_N_BIT ? 'N' : 'n';
0155     buf[1] = flags & PSR_Z_BIT ? 'Z' : 'z';
0156     buf[2] = flags & PSR_C_BIT ? 'C' : 'c';
0157     buf[3] = flags & PSR_V_BIT ? 'V' : 'v';
0158     buf[4] = '\0';
0159 
0160 #ifndef CONFIG_CPU_V7M
0161     {
0162         const char *segment;
0163 
0164         if ((domain & domain_mask(DOMAIN_USER)) ==
0165             domain_val(DOMAIN_USER, DOMAIN_NOACCESS))
0166             segment = "none";
0167         else
0168             segment = "user";
0169 
0170         printk("Flags: %s  IRQs o%s  FIQs o%s  Mode %s  ISA %s  Segment %s\n",
0171             buf, interrupts_enabled(regs) ? "n" : "ff",
0172             fast_interrupts_enabled(regs) ? "n" : "ff",
0173             processor_modes[processor_mode(regs)],
0174             isa_modes[isa_mode(regs)], segment);
0175     }
0176 #else
0177     printk("xPSR: %08lx\n", regs->ARM_cpsr);
0178 #endif
0179 
0180 #ifdef CONFIG_CPU_CP15
0181     {
0182         unsigned int ctrl;
0183 
0184         buf[0] = '\0';
0185 #ifdef CONFIG_CPU_CP15_MMU
0186         {
0187             unsigned int transbase;
0188             asm("mrc p15, 0, %0, c2, c0\n\t"
0189                 : "=r" (transbase));
0190             snprintf(buf, sizeof(buf), "  Table: %08x  DAC: %08x",
0191                 transbase, domain);
0192         }
0193 #endif
0194         asm("mrc p15, 0, %0, c1, c0\n" : "=r" (ctrl));
0195 
0196         printk("Control: %08x%s\n", ctrl, buf);
0197     }
0198 #endif
0199 }
0200 
0201 void show_regs(struct pt_regs * regs)
0202 {
0203     __show_regs(regs);
0204     dump_stack();
0205 }
0206 
0207 ATOMIC_NOTIFIER_HEAD(thread_notify_head);
0208 
0209 EXPORT_SYMBOL_GPL(thread_notify_head);
0210 
0211 /*
0212  * Free current thread data structures etc..
0213  */
0214 void exit_thread(struct task_struct *tsk)
0215 {
0216     thread_notify(THREAD_NOTIFY_EXIT, task_thread_info(tsk));
0217 }
0218 
0219 void flush_thread(void)
0220 {
0221     struct thread_info *thread = current_thread_info();
0222     struct task_struct *tsk = current;
0223 
0224     flush_ptrace_hw_breakpoint(tsk);
0225 
0226     memset(thread->used_cp, 0, sizeof(thread->used_cp));
0227     memset(&tsk->thread.debug, 0, sizeof(struct debug_info));
0228     memset(&thread->fpstate, 0, sizeof(union fp_state));
0229 
0230     flush_tls();
0231 
0232     thread_notify(THREAD_NOTIFY_FLUSH, thread);
0233 }
0234 
0235 void release_thread(struct task_struct *dead_task)
0236 {
0237 }
0238 
0239 asmlinkage void ret_from_fork(void) __asm__("ret_from_fork");
0240 
0241 int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
0242 {
0243     unsigned long clone_flags = args->flags;
0244     unsigned long stack_start = args->stack;
0245     unsigned long tls = args->tls;
0246     struct thread_info *thread = task_thread_info(p);
0247     struct pt_regs *childregs = task_pt_regs(p);
0248 
0249     memset(&thread->cpu_context, 0, sizeof(struct cpu_context_save));
0250 
0251 #ifdef CONFIG_CPU_USE_DOMAINS
0252     /*
0253      * Copy the initial value of the domain access control register
0254      * from the current thread: thread->addr_limit will have been
0255      * copied from the current thread via setup_thread_stack() in
0256      * kernel/fork.c
0257      */
0258     thread->cpu_domain = get_domain();
0259 #endif
0260 
0261     if (likely(!args->fn)) {
0262         *childregs = *current_pt_regs();
0263         childregs->ARM_r0 = 0;
0264         if (stack_start)
0265             childregs->ARM_sp = stack_start;
0266     } else {
0267         memset(childregs, 0, sizeof(struct pt_regs));
0268         thread->cpu_context.r4 = (unsigned long)args->fn_arg;
0269         thread->cpu_context.r5 = (unsigned long)args->fn;
0270         childregs->ARM_cpsr = SVC_MODE;
0271     }
0272     thread->cpu_context.pc = (unsigned long)ret_from_fork;
0273     thread->cpu_context.sp = (unsigned long)childregs;
0274 
0275     clear_ptrace_hw_breakpoint(p);
0276 
0277     if (clone_flags & CLONE_SETTLS)
0278         thread->tp_value[0] = tls;
0279     thread->tp_value[1] = get_tpuser();
0280 
0281     thread_notify(THREAD_NOTIFY_COPY, thread);
0282 
0283     return 0;
0284 }
0285 
0286 unsigned long __get_wchan(struct task_struct *p)
0287 {
0288     struct stackframe frame;
0289     unsigned long stack_page;
0290     int count = 0;
0291 
0292     frame.fp = thread_saved_fp(p);
0293     frame.sp = thread_saved_sp(p);
0294     frame.lr = 0;           /* recovered from the stack */
0295     frame.pc = thread_saved_pc(p);
0296     stack_page = (unsigned long)task_stack_page(p);
0297     do {
0298         if (frame.sp < stack_page ||
0299             frame.sp >= stack_page + THREAD_SIZE ||
0300             unwind_frame(&frame) < 0)
0301             return 0;
0302         if (!in_sched_functions(frame.pc))
0303             return frame.pc;
0304     } while (count ++ < 16);
0305     return 0;
0306 }
0307 
0308 #ifdef CONFIG_MMU
0309 #ifdef CONFIG_KUSER_HELPERS
0310 /*
0311  * The vectors page is always readable from user space for the
0312  * atomic helpers. Insert it into the gate_vma so that it is visible
0313  * through ptrace and /proc/<pid>/mem.
0314  */
0315 static struct vm_area_struct gate_vma;
0316 
0317 static int __init gate_vma_init(void)
0318 {
0319     vma_init(&gate_vma, NULL);
0320     gate_vma.vm_page_prot = PAGE_READONLY_EXEC;
0321     gate_vma.vm_start = 0xffff0000;
0322     gate_vma.vm_end = 0xffff0000 + PAGE_SIZE;
0323     gate_vma.vm_flags = VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYEXEC;
0324     return 0;
0325 }
0326 arch_initcall(gate_vma_init);
0327 
0328 struct vm_area_struct *get_gate_vma(struct mm_struct *mm)
0329 {
0330     return &gate_vma;
0331 }
0332 
0333 int in_gate_area(struct mm_struct *mm, unsigned long addr)
0334 {
0335     return (addr >= gate_vma.vm_start) && (addr < gate_vma.vm_end);
0336 }
0337 
0338 int in_gate_area_no_mm(unsigned long addr)
0339 {
0340     return in_gate_area(NULL, addr);
0341 }
0342 #define is_gate_vma(vma)    ((vma) == &gate_vma)
0343 #else
0344 #define is_gate_vma(vma)    0
0345 #endif
0346 
0347 const char *arch_vma_name(struct vm_area_struct *vma)
0348 {
0349     return is_gate_vma(vma) ? "[vectors]" : NULL;
0350 }
0351 
0352 /* If possible, provide a placement hint at a random offset from the
0353  * stack for the sigpage and vdso pages.
0354  */
0355 static unsigned long sigpage_addr(const struct mm_struct *mm,
0356                   unsigned int npages)
0357 {
0358     unsigned long offset;
0359     unsigned long first;
0360     unsigned long last;
0361     unsigned long addr;
0362     unsigned int slots;
0363 
0364     first = PAGE_ALIGN(mm->start_stack);
0365 
0366     last = TASK_SIZE - (npages << PAGE_SHIFT);
0367 
0368     /* No room after stack? */
0369     if (first > last)
0370         return 0;
0371 
0372     /* Just enough room? */
0373     if (first == last)
0374         return first;
0375 
0376     slots = ((last - first) >> PAGE_SHIFT) + 1;
0377 
0378     offset = get_random_int() % slots;
0379 
0380     addr = first + (offset << PAGE_SHIFT);
0381 
0382     return addr;
0383 }
0384 
0385 static struct page *signal_page;
0386 extern struct page *get_signal_page(void);
0387 
0388 static int sigpage_mremap(const struct vm_special_mapping *sm,
0389         struct vm_area_struct *new_vma)
0390 {
0391     current->mm->context.sigpage = new_vma->vm_start;
0392     return 0;
0393 }
0394 
0395 static const struct vm_special_mapping sigpage_mapping = {
0396     .name = "[sigpage]",
0397     .pages = &signal_page,
0398     .mremap = sigpage_mremap,
0399 };
0400 
0401 int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
0402 {
0403     struct mm_struct *mm = current->mm;
0404     struct vm_area_struct *vma;
0405     unsigned long npages;
0406     unsigned long addr;
0407     unsigned long hint;
0408     int ret = 0;
0409 
0410     if (!signal_page)
0411         signal_page = get_signal_page();
0412     if (!signal_page)
0413         return -ENOMEM;
0414 
0415     npages = 1; /* for sigpage */
0416     npages += vdso_total_pages;
0417 
0418     if (mmap_write_lock_killable(mm))
0419         return -EINTR;
0420     hint = sigpage_addr(mm, npages);
0421     addr = get_unmapped_area(NULL, hint, npages << PAGE_SHIFT, 0, 0);
0422     if (IS_ERR_VALUE(addr)) {
0423         ret = addr;
0424         goto up_fail;
0425     }
0426 
0427     vma = _install_special_mapping(mm, addr, PAGE_SIZE,
0428         VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC,
0429         &sigpage_mapping);
0430 
0431     if (IS_ERR(vma)) {
0432         ret = PTR_ERR(vma);
0433         goto up_fail;
0434     }
0435 
0436     mm->context.sigpage = addr;
0437 
0438     /* Unlike the sigpage, failure to install the vdso is unlikely
0439      * to be fatal to the process, so no error check needed
0440      * here.
0441      */
0442     arm_install_vdso(mm, addr + PAGE_SIZE);
0443 
0444  up_fail:
0445     mmap_write_unlock(mm);
0446     return ret;
0447 }
0448 #endif