^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * This file handles the architecture dependent parts of process handling.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright IBM Corp. 1999, 2009
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Hartmut Penner <hp@de.ibm.com>,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Denis Joseph Barrow,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/elf-randomize.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/compiler.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/cpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/sched/debug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/sched/task.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/sched/task_stack.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/elfcore.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/smp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/tick.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/personality.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/syscalls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/compat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/kprobes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <linux/init_task.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <asm/cpu_mf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include <asm/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include <asm/processor.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #include <asm/vtimer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #include <asm/exec.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #include <asm/irq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #include <asm/nmi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #include <asm/smp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #include <asm/stacktrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #include <asm/switch_to.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #include <asm/runtime_instr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #include <asm/unwind.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #include "entry.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) asmlinkage void ret_from_fork(void) asm ("ret_from_fork");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) extern void kernel_thread_starter(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) void flush_thread(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) void arch_setup_new_exec(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) if (S390_lowcore.current_pid != current->pid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) S390_lowcore.current_pid = current->pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (test_facility(40))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) lpp(&S390_lowcore.lpp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) void arch_release_task_struct(struct task_struct *tsk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) runtime_instr_release(tsk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) guarded_storage_release(tsk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * Save the floating-point or vector register state of the current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * task and set the CIF_FPU flag to lazy restore the FPU register
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) * state when returning to user space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) save_fpu_regs();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) memcpy(dst, src, arch_task_struct_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) dst->thread.fpu.regs = dst->thread.fpu.fprs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) int copy_thread(unsigned long clone_flags, unsigned long new_stackp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) unsigned long arg, struct task_struct *p, unsigned long tls)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) struct fake_frame
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) struct stack_frame sf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) struct pt_regs childregs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) } *frame;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) frame = container_of(task_pt_regs(p), struct fake_frame, childregs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) p->thread.ksp = (unsigned long) frame;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) /* Save access registers to new thread structure. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) save_access_regs(&p->thread.acrs[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) /* start new process with ar4 pointing to the correct address space */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) p->thread.mm_segment = get_fs();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) /* Don't copy debug registers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) memset(&p->thread.per_user, 0, sizeof(p->thread.per_user));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) memset(&p->thread.per_event, 0, sizeof(p->thread.per_event));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) clear_tsk_thread_flag(p, TIF_SINGLE_STEP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) p->thread.per_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) /* Initialize per thread user and system timer values */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) p->thread.user_timer = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) p->thread.guest_timer = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) p->thread.system_timer = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) p->thread.hardirq_timer = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) p->thread.softirq_timer = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) p->thread.last_break = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) frame->sf.back_chain = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) /* new return point is ret_from_fork */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) frame->sf.gprs[8] = (unsigned long) ret_from_fork;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) /* fake return stack for resume(), don't go back to schedule */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) frame->sf.gprs[9] = (unsigned long) frame;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) /* Store access registers to kernel stack of new process. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) if (unlikely(p->flags & PF_KTHREAD)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) /* kernel thread */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) memset(&frame->childregs, 0, sizeof(struct pt_regs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) frame->childregs.psw.mask = PSW_KERNEL_BITS | PSW_MASK_DAT |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) frame->childregs.psw.addr =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) (unsigned long) kernel_thread_starter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) frame->childregs.gprs[9] = new_stackp; /* function */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) frame->childregs.gprs[10] = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) frame->childregs.gprs[11] = (unsigned long) do_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) frame->childregs.orig_gpr2 = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) frame->childregs = *current_pt_regs();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) frame->childregs.gprs[2] = 0; /* child returns 0 on fork. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) frame->childregs.flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) if (new_stackp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) frame->childregs.gprs[15] = new_stackp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) /* Don't copy runtime instrumentation info */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) p->thread.ri_cb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) frame->childregs.psw.mask &= ~PSW_MASK_RI;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) /* Don't copy guarded storage control block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) p->thread.gs_cb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) p->thread.gs_bc_cb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) /* Set a new TLS ? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if (clone_flags & CLONE_SETTLS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) if (is_compat_task()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) p->thread.acrs[0] = (unsigned int)tls;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) p->thread.acrs[0] = (unsigned int)(tls >> 32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) p->thread.acrs[1] = (unsigned int)tls;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) asmlinkage void execve_tail(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) current->thread.fpu.fpc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) asm volatile("sfpc %0" : : "d" (0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) unsigned long get_wchan(struct task_struct *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) struct unwind_state state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) unsigned long ip = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) if (!p || p == current || p->state == TASK_RUNNING || !task_stack_page(p))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if (!try_get_task_stack(p))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) unwind_for_each_frame(&state, p, NULL, 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) if (state.stack_info.type != STACK_TYPE_TASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) ip = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) ip = unwind_get_return_address(&state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) if (!ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) if (!in_sched_functions(ip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) put_task_stack(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) return ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) unsigned long arch_align_stack(unsigned long sp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) sp -= get_random_int() & ~PAGE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) return sp & ~0xf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) static inline unsigned long brk_rnd(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) return (get_random_int() & BRK_RND_MASK) << PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) unsigned long arch_randomize_brk(struct mm_struct *mm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) unsigned long ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) ret = PAGE_ALIGN(mm->brk + brk_rnd());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) return (ret > mm->brk) ? ret : mm->brk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) void set_fs_fixup(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) struct pt_regs *regs = current_pt_regs();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) static bool warned;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) set_fs(USER_DS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) if (warned)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) WARN(1, "Unbalanced set_fs - int code: 0x%x\n", regs->int_code);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) show_registers(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) warned = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) }