Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * This file handles the architecture dependent parts of process handling.
0004  *
0005  *    Copyright IBM Corp. 1999, 2009
0006  *    Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>,
0007  *       Hartmut Penner <hp@de.ibm.com>,
0008  *       Denis Joseph Barrow,
0009  */
0010 
0011 #include <linux/elf-randomize.h>
0012 #include <linux/compiler.h>
0013 #include <linux/cpu.h>
0014 #include <linux/sched.h>
0015 #include <linux/sched/debug.h>
0016 #include <linux/sched/task.h>
0017 #include <linux/sched/task_stack.h>
0018 #include <linux/kernel.h>
0019 #include <linux/mm.h>
0020 #include <linux/elfcore.h>
0021 #include <linux/smp.h>
0022 #include <linux/slab.h>
0023 #include <linux/interrupt.h>
0024 #include <linux/tick.h>
0025 #include <linux/personality.h>
0026 #include <linux/syscalls.h>
0027 #include <linux/compat.h>
0028 #include <linux/kprobes.h>
0029 #include <linux/random.h>
0030 #include <linux/export.h>
0031 #include <linux/init_task.h>
0032 #include <linux/entry-common.h>
0033 #include <asm/cpu_mf.h>
0034 #include <asm/io.h>
0035 #include <asm/processor.h>
0036 #include <asm/vtimer.h>
0037 #include <asm/exec.h>
0038 #include <asm/irq.h>
0039 #include <asm/nmi.h>
0040 #include <asm/smp.h>
0041 #include <asm/stacktrace.h>
0042 #include <asm/switch_to.h>
0043 #include <asm/runtime_instr.h>
0044 #include <asm/unwind.h>
0045 #include "entry.h"
0046 
0047 void ret_from_fork(void) asm("ret_from_fork");
0048 
0049 void __ret_from_fork(struct task_struct *prev, struct pt_regs *regs)
0050 {
0051     void (*func)(void *arg);
0052 
0053     schedule_tail(prev);
0054 
0055     if (!user_mode(regs)) {
0056         /* Kernel thread */
0057         func = (void *)regs->gprs[9];
0058         func((void *)regs->gprs[10]);
0059     }
0060     clear_pt_regs_flag(regs, PIF_SYSCALL);
0061     syscall_exit_to_user_mode(regs);
0062 }
0063 
0064 void flush_thread(void)
0065 {
0066 }
0067 
0068 void arch_setup_new_exec(void)
0069 {
0070     if (S390_lowcore.current_pid != current->pid) {
0071         S390_lowcore.current_pid = current->pid;
0072         if (test_facility(40))
0073             lpp(&S390_lowcore.lpp);
0074     }
0075 }
0076 
0077 void arch_release_task_struct(struct task_struct *tsk)
0078 {
0079     runtime_instr_release(tsk);
0080     guarded_storage_release(tsk);
0081 }
0082 
0083 int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
0084 {
0085     /*
0086      * Save the floating-point or vector register state of the current
0087      * task and set the CIF_FPU flag to lazy restore the FPU register
0088      * state when returning to user space.
0089      */
0090     save_fpu_regs();
0091 
0092     memcpy(dst, src, arch_task_struct_size);
0093     dst->thread.fpu.regs = dst->thread.fpu.fprs;
0094 
0095     /*
0096      * Don't transfer over the runtime instrumentation or the guarded
0097      * storage control block pointers. These fields are cleared here instead
0098      * of in copy_thread() to avoid premature freeing of associated memory
0099      * on fork() failure. Wait to clear the RI flag because ->stack still
0100      * refers to the source thread.
0101      */
0102     dst->thread.ri_cb = NULL;
0103     dst->thread.gs_cb = NULL;
0104     dst->thread.gs_bc_cb = NULL;
0105 
0106     return 0;
0107 }
0108 
0109 int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
0110 {
0111     unsigned long clone_flags = args->flags;
0112     unsigned long new_stackp = args->stack;
0113     unsigned long tls = args->tls;
0114     struct fake_frame
0115     {
0116         struct stack_frame sf;
0117         struct pt_regs childregs;
0118     } *frame;
0119 
0120     frame = container_of(task_pt_regs(p), struct fake_frame, childregs);
0121     p->thread.ksp = (unsigned long) frame;
0122     /* Save access registers to new thread structure. */
0123     save_access_regs(&p->thread.acrs[0]);
0124     /* start new process with ar4 pointing to the correct address space */
0125     /* Don't copy debug registers */
0126     memset(&p->thread.per_user, 0, sizeof(p->thread.per_user));
0127     memset(&p->thread.per_event, 0, sizeof(p->thread.per_event));
0128     clear_tsk_thread_flag(p, TIF_SINGLE_STEP);
0129     p->thread.per_flags = 0;
0130     /* Initialize per thread user and system timer values */
0131     p->thread.user_timer = 0;
0132     p->thread.guest_timer = 0;
0133     p->thread.system_timer = 0;
0134     p->thread.hardirq_timer = 0;
0135     p->thread.softirq_timer = 0;
0136     p->thread.last_break = 1;
0137 
0138     frame->sf.back_chain = 0;
0139     frame->sf.gprs[5] = (unsigned long)frame + sizeof(struct stack_frame);
0140     frame->sf.gprs[6] = (unsigned long)p;
0141     /* new return point is ret_from_fork */
0142     frame->sf.gprs[8] = (unsigned long)ret_from_fork;
0143     /* fake return stack for resume(), don't go back to schedule */
0144     frame->sf.gprs[9] = (unsigned long)frame;
0145 
0146     /* Store access registers to kernel stack of new process. */
0147     if (unlikely(args->fn)) {
0148         /* kernel thread */
0149         memset(&frame->childregs, 0, sizeof(struct pt_regs));
0150         frame->childregs.psw.mask = PSW_KERNEL_BITS | PSW_MASK_DAT |
0151                 PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK;
0152         frame->childregs.psw.addr =
0153                 (unsigned long)__ret_from_fork;
0154         frame->childregs.gprs[9] = (unsigned long)args->fn;
0155         frame->childregs.gprs[10] = (unsigned long)args->fn_arg;
0156         frame->childregs.orig_gpr2 = -1;
0157         frame->childregs.last_break = 1;
0158         return 0;
0159     }
0160     frame->childregs = *current_pt_regs();
0161     frame->childregs.gprs[2] = 0;   /* child returns 0 on fork. */
0162     frame->childregs.flags = 0;
0163     if (new_stackp)
0164         frame->childregs.gprs[15] = new_stackp;
0165     /*
0166      * Clear the runtime instrumentation flag after the above childregs
0167      * copy. The CB pointer was already cleared in arch_dup_task_struct().
0168      */
0169     frame->childregs.psw.mask &= ~PSW_MASK_RI;
0170 
0171     /* Set a new TLS ?  */
0172     if (clone_flags & CLONE_SETTLS) {
0173         if (is_compat_task()) {
0174             p->thread.acrs[0] = (unsigned int)tls;
0175         } else {
0176             p->thread.acrs[0] = (unsigned int)(tls >> 32);
0177             p->thread.acrs[1] = (unsigned int)tls;
0178         }
0179     }
0180     /*
0181      * s390 stores the svc return address in arch_data when calling
0182      * sigreturn()/restart_syscall() via vdso. 1 means no valid address
0183      * stored.
0184      */
0185     p->restart_block.arch_data = 1;
0186     return 0;
0187 }
0188 
0189 void execve_tail(void)
0190 {
0191     current->thread.fpu.fpc = 0;
0192     asm volatile("sfpc %0" : : "d" (0));
0193 }
0194 
0195 unsigned long __get_wchan(struct task_struct *p)
0196 {
0197     struct unwind_state state;
0198     unsigned long ip = 0;
0199 
0200     if (!task_stack_page(p))
0201         return 0;
0202 
0203     if (!try_get_task_stack(p))
0204         return 0;
0205 
0206     unwind_for_each_frame(&state, p, NULL, 0) {
0207         if (state.stack_info.type != STACK_TYPE_TASK) {
0208             ip = 0;
0209             break;
0210         }
0211 
0212         ip = unwind_get_return_address(&state);
0213         if (!ip)
0214             break;
0215 
0216         if (!in_sched_functions(ip))
0217             break;
0218     }
0219 
0220     put_task_stack(p);
0221     return ip;
0222 }
0223 
0224 unsigned long arch_align_stack(unsigned long sp)
0225 {
0226     if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
0227         sp -= get_random_int() & ~PAGE_MASK;
0228     return sp & ~0xf;
0229 }
0230 
0231 static inline unsigned long brk_rnd(void)
0232 {
0233     return (get_random_int() & BRK_RND_MASK) << PAGE_SHIFT;
0234 }
0235 
0236 unsigned long arch_randomize_brk(struct mm_struct *mm)
0237 {
0238     unsigned long ret;
0239 
0240     ret = PAGE_ALIGN(mm->brk + brk_rnd());
0241     return (ret > mm->brk) ? ret : mm->brk;
0242 }