Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Heiko Carstens | cbdc229 | 2009-03-26 15:23:52 +0100 | [diff] [blame] | 3 | * This file handles the architecture dependent parts of process handling. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Heiko Carstens | a53c8fa | 2012-07-20 11:15:04 +0200 | [diff] [blame] | 5 | * Copyright IBM Corp. 1999, 2009 |
Heiko Carstens | cbdc229 | 2009-03-26 15:23:52 +0100 | [diff] [blame] | 6 | * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>, |
| 7 | * Hartmut Penner <hp@de.ibm.com>, |
| 8 | * Denis Joseph Barrow, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
Heiko Carstens | ca21872 | 2016-05-07 12:15:34 +0200 | [diff] [blame] | 11 | #include <linux/elf-randomize.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/compiler.h> |
| 13 | #include <linux/cpu.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/sched.h> |
Ingo Molnar | b17b015 | 2017-02-08 18:51:35 +0100 | [diff] [blame] | 15 | #include <linux/sched/debug.h> |
Ingo Molnar | 2993002 | 2017-02-08 18:51:36 +0100 | [diff] [blame] | 16 | #include <linux/sched/task.h> |
Ingo Molnar | 68db0cf | 2017-02-08 18:51:37 +0100 | [diff] [blame] | 17 | #include <linux/sched/task_stack.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/kernel.h> |
| 19 | #include <linux/mm.h> |
Martin Schwidefsky | 638ad34 | 2011-10-30 15:17:13 +0100 | [diff] [blame] | 20 | #include <linux/elfcore.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/smp.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 22 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/interrupt.h> |
Heiko Carstens | 5a62b19 | 2008-04-17 07:46:25 +0200 | [diff] [blame] | 24 | #include <linux/tick.h> |
Heiko Carstens | 9887a1f | 2011-01-12 09:55:28 +0100 | [diff] [blame] | 25 | #include <linux/personality.h> |
Heiko Carstens | 2668945 | 2009-01-14 14:14:36 +0100 | [diff] [blame] | 26 | #include <linux/syscalls.h> |
Heiko Carstens | 3e86a8c | 2009-09-22 22:58:42 +0200 | [diff] [blame] | 27 | #include <linux/compat.h> |
Martin Schwidefsky | 860dba4 | 2011-01-05 12:47:25 +0100 | [diff] [blame] | 28 | #include <linux/kprobes.h> |
Heiko Carstens | 9887a1f | 2011-01-12 09:55:28 +0100 | [diff] [blame] | 29 | #include <linux/random.h> |
Paul Gortmaker | 3994a52 | 2017-02-09 15:20:23 -0500 | [diff] [blame] | 30 | #include <linux/export.h> |
Hendrik Brueckner | 0ac2777 | 2015-09-29 17:53:22 +0200 | [diff] [blame] | 31 | #include <linux/init_task.h> |
Martin Schwidefsky | 2317b07 | 2018-04-20 10:21:21 +0200 | [diff] [blame] | 32 | #include <asm/cpu_mf.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <asm/io.h> |
| 34 | #include <asm/processor.h> |
Martin Schwidefsky | 27f6b41 | 2012-07-20 11:15:08 +0200 | [diff] [blame] | 35 | #include <asm/vtimer.h> |
Heiko Carstens | f6e3869 | 2012-09-14 13:45:24 +0200 | [diff] [blame] | 36 | #include <asm/exec.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <asm/irq.h> |
Heiko Carstens | f5daba1 | 2009-03-26 15:24:01 +0100 | [diff] [blame] | 38 | #include <asm/nmi.h> |
Heiko Carstens | da7f51c | 2011-01-05 12:48:09 +0100 | [diff] [blame] | 39 | #include <asm/smp.h> |
David Howells | a0616cd | 2012-03-28 18:30:02 +0100 | [diff] [blame] | 40 | #include <asm/switch_to.h> |
Jan Glauber | e4b8b3f | 2012-07-31 10:52:05 +0200 | [diff] [blame] | 41 | #include <asm/runtime_instr.h> |
Heiko Carstens | a806170 | 2008-04-17 07:46:26 +0200 | [diff] [blame] | 42 | #include "entry.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
Martin Schwidefsky | 94c12cc | 2006-09-28 16:56:43 +0200 | [diff] [blame] | 44 | asmlinkage void ret_from_fork(void) asm ("ret_from_fork"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
Heiko Carstens | 7a5388d | 2014-10-22 12:42:38 +0200 | [diff] [blame] | 46 | extern void kernel_thread_starter(void); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | void flush_thread(void) |
| 49 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Martin Schwidefsky | 2317b07 | 2018-04-20 10:21:21 +0200 | [diff] [blame] | 52 | void arch_setup_new_exec(void) |
| 53 | { |
| 54 | if (S390_lowcore.current_pid != current->pid) { |
| 55 | S390_lowcore.current_pid = current->pid; |
| 56 | if (test_facility(40)) |
| 57 | lpp(&S390_lowcore.lpp); |
| 58 | } |
| 59 | } |
| 60 | |
Hendrik Brueckner | 6a039ea | 2015-02-09 16:47:00 +0100 | [diff] [blame] | 61 | void arch_release_task_struct(struct task_struct *tsk) |
| 62 | { |
Heiko Carstens | 8d9047f | 2017-09-11 11:24:22 +0200 | [diff] [blame] | 63 | runtime_instr_release(tsk); |
Heiko Carstens | 7b83c62 | 2017-09-11 11:24:23 +0200 | [diff] [blame] | 64 | guarded_storage_release(tsk); |
Hendrik Brueckner | 155e839 | 2015-06-11 16:57:20 +0200 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src) |
| 68 | { |
Hendrik Brueckner | 9977e88 | 2015-06-10 12:53:42 +0200 | [diff] [blame] | 69 | /* |
| 70 | * Save the floating-point or vector register state of the current |
Hendrik Brueckner | b5510d9 | 2015-09-29 10:04:41 +0200 | [diff] [blame] | 71 | * task and set the CIF_FPU flag to lazy restore the FPU register |
| 72 | * state when returning to user space. |
Hendrik Brueckner | 9977e88 | 2015-06-10 12:53:42 +0200 | [diff] [blame] | 73 | */ |
Hendrik Brueckner | d0164ee | 2015-06-29 16:43:06 +0200 | [diff] [blame] | 74 | save_fpu_regs(); |
Hendrik Brueckner | b5510d9 | 2015-09-29 10:04:41 +0200 | [diff] [blame] | 75 | |
Martin Schwidefsky | 3f6813b | 2016-04-01 15:42:15 +0200 | [diff] [blame] | 76 | memcpy(dst, src, arch_task_struct_size); |
| 77 | dst->thread.fpu.regs = dst->thread.fpu.fprs; |
Hendrik Brueckner | 155e839 | 2015-06-11 16:57:20 +0200 | [diff] [blame] | 78 | return 0; |
Hendrik Brueckner | 6a039ea | 2015-02-09 16:47:00 +0100 | [diff] [blame] | 79 | } |
Hendrik Brueckner | 6a039ea | 2015-02-09 16:47:00 +0100 | [diff] [blame] | 80 | |
Heiko Carstens | f50c0e6 | 2017-02-20 09:38:42 +0100 | [diff] [blame] | 81 | int copy_thread_tls(unsigned long clone_flags, unsigned long new_stackp, |
| 82 | unsigned long arg, struct task_struct *p, unsigned long tls) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | { |
Heiko Carstens | cbdc229 | 2009-03-26 15:23:52 +0100 | [diff] [blame] | 84 | struct fake_frame |
| 85 | { |
| 86 | struct stack_frame sf; |
| 87 | struct pt_regs childregs; |
| 88 | } *frame; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | |
Heiko Carstens | cbdc229 | 2009-03-26 15:23:52 +0100 | [diff] [blame] | 90 | frame = container_of(task_pt_regs(p), struct fake_frame, childregs); |
| 91 | p->thread.ksp = (unsigned long) frame; |
Al Viro | f9a7e02 | 2012-09-21 20:48:32 -0400 | [diff] [blame] | 92 | /* Save access registers to new thread structure. */ |
| 93 | save_access_regs(&p->thread.acrs[0]); |
| 94 | /* start new process with ar4 pointing to the correct address space */ |
| 95 | p->thread.mm_segment = get_fs(); |
| 96 | /* Don't copy debug registers */ |
| 97 | memset(&p->thread.per_user, 0, sizeof(p->thread.per_user)); |
| 98 | memset(&p->thread.per_event, 0, sizeof(p->thread.per_event)); |
| 99 | clear_tsk_thread_flag(p, TIF_SINGLE_STEP); |
Heiko Carstens | a1c5bef | 2017-11-09 12:29:34 +0100 | [diff] [blame] | 100 | p->thread.per_flags = 0; |
Al Viro | f9a7e02 | 2012-09-21 20:48:32 -0400 | [diff] [blame] | 101 | /* Initialize per thread user and system timer values */ |
Martin Schwidefsky | 90c53e6 | 2016-11-08 12:15:59 +0100 | [diff] [blame] | 102 | p->thread.user_timer = 0; |
Martin Schwidefsky | 3c915bd | 2017-03-01 09:18:34 +0100 | [diff] [blame] | 103 | p->thread.guest_timer = 0; |
Martin Schwidefsky | 90c53e6 | 2016-11-08 12:15:59 +0100 | [diff] [blame] | 104 | p->thread.system_timer = 0; |
Martin Schwidefsky | 3c915bd | 2017-03-01 09:18:34 +0100 | [diff] [blame] | 105 | p->thread.hardirq_timer = 0; |
| 106 | p->thread.softirq_timer = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
Al Viro | f9a7e02 | 2012-09-21 20:48:32 -0400 | [diff] [blame] | 108 | frame->sf.back_chain = 0; |
Heiko Carstens | cbdc229 | 2009-03-26 15:23:52 +0100 | [diff] [blame] | 109 | /* new return point is ret_from_fork */ |
| 110 | frame->sf.gprs[8] = (unsigned long) ret_from_fork; |
Heiko Carstens | cbdc229 | 2009-03-26 15:23:52 +0100 | [diff] [blame] | 111 | /* fake return stack for resume(), don't go back to schedule */ |
| 112 | frame->sf.gprs[9] = (unsigned long) frame; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
Al Viro | f9a7e02 | 2012-09-21 20:48:32 -0400 | [diff] [blame] | 114 | /* Store access registers to kernel stack of new process. */ |
Al Viro | 87f1ca8 | 2012-10-21 16:44:27 -0400 | [diff] [blame] | 115 | if (unlikely(p->flags & PF_KTHREAD)) { |
Al Viro | f9a7e02 | 2012-09-21 20:48:32 -0400 | [diff] [blame] | 116 | /* kernel thread */ |
| 117 | memset(&frame->childregs, 0, sizeof(struct pt_regs)); |
Martin Schwidefsky | e258d71 | 2013-09-24 09:14:56 +0200 | [diff] [blame] | 118 | frame->childregs.psw.mask = PSW_KERNEL_BITS | PSW_MASK_DAT | |
Al Viro | f9a7e02 | 2012-09-21 20:48:32 -0400 | [diff] [blame] | 119 | PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK; |
Heiko Carstens | fecc868a | 2016-01-18 12:49:44 +0100 | [diff] [blame] | 120 | frame->childregs.psw.addr = |
Al Viro | f9a7e02 | 2012-09-21 20:48:32 -0400 | [diff] [blame] | 121 | (unsigned long) kernel_thread_starter; |
| 122 | frame->childregs.gprs[9] = new_stackp; /* function */ |
| 123 | frame->childregs.gprs[10] = arg; |
| 124 | frame->childregs.gprs[11] = (unsigned long) do_exit; |
| 125 | frame->childregs.orig_gpr2 = -1; |
| 126 | |
| 127 | return 0; |
| 128 | } |
Al Viro | 87f1ca8 | 2012-10-21 16:44:27 -0400 | [diff] [blame] | 129 | frame->childregs = *current_pt_regs(); |
Al Viro | f9a7e02 | 2012-09-21 20:48:32 -0400 | [diff] [blame] | 130 | frame->childregs.gprs[2] = 0; /* child returns 0 on fork. */ |
Martin Schwidefsky | d3a73ac | 2014-04-15 12:55:07 +0200 | [diff] [blame] | 131 | frame->childregs.flags = 0; |
Al Viro | 87f1ca8 | 2012-10-21 16:44:27 -0400 | [diff] [blame] | 132 | if (new_stackp) |
| 133 | frame->childregs.gprs[15] = new_stackp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | |
Jan Glauber | e4b8b3f | 2012-07-31 10:52:05 +0200 | [diff] [blame] | 135 | /* Don't copy runtime instrumentation info */ |
| 136 | p->thread.ri_cb = NULL; |
Jan Glauber | e4b8b3f | 2012-07-31 10:52:05 +0200 | [diff] [blame] | 137 | frame->childregs.psw.mask &= ~PSW_MASK_RI; |
Martin Schwidefsky | 916cda1 | 2016-01-26 14:10:34 +0100 | [diff] [blame] | 138 | /* Don't copy guarded storage control block */ |
| 139 | p->thread.gs_cb = NULL; |
| 140 | p->thread.gs_bc_cb = NULL; |
Jan Glauber | e4b8b3f | 2012-07-31 10:52:05 +0200 | [diff] [blame] | 141 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | /* Set a new TLS ? */ |
| 143 | if (clone_flags & CLONE_SETTLS) { |
Heiko Carstens | 7757591 | 2009-06-12 10:26:25 +0200 | [diff] [blame] | 144 | if (is_compat_task()) { |
Al Viro | 87f1ca8 | 2012-10-21 16:44:27 -0400 | [diff] [blame] | 145 | p->thread.acrs[0] = (unsigned int)tls; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | } else { |
Al Viro | 87f1ca8 | 2012-10-21 16:44:27 -0400 | [diff] [blame] | 147 | p->thread.acrs[0] = (unsigned int)(tls >> 32); |
| 148 | p->thread.acrs[1] = (unsigned int)tls; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | } |
| 150 | } |
Heiko Carstens | cbdc229 | 2009-03-26 15:23:52 +0100 | [diff] [blame] | 151 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Martin Schwidefsky | 03ff9a2 | 2007-04-27 16:01:40 +0200 | [diff] [blame] | 154 | asmlinkage void execve_tail(void) |
| 155 | { |
Hendrik Brueckner | 904818e | 2015-06-11 15:33:54 +0200 | [diff] [blame] | 156 | current->thread.fpu.fpc = 0; |
Heiko Carstens | e47994d | 2015-07-06 15:02:37 +0200 | [diff] [blame] | 157 | asm volatile("sfpc %0" : : "d" (0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | * fill in the FPU structure for a core dump. |
| 162 | */ |
| 163 | int dump_fpu (struct pt_regs * regs, s390_fp_regs *fpregs) |
| 164 | { |
Hendrik Brueckner | d0164ee | 2015-06-29 16:43:06 +0200 | [diff] [blame] | 165 | save_fpu_regs(); |
Hendrik Brueckner | 9977e88 | 2015-06-10 12:53:42 +0200 | [diff] [blame] | 166 | fpregs->fpc = current->thread.fpu.fpc; |
| 167 | fpregs->pad = 0; |
Hendrik Brueckner | b5510d9 | 2015-09-29 10:04:41 +0200 | [diff] [blame] | 168 | if (MACHINE_HAS_VX) |
Hendrik Brueckner | 9977e88 | 2015-06-10 12:53:42 +0200 | [diff] [blame] | 169 | convert_vx_to_fp((freg_t *)&fpregs->fprs, |
| 170 | current->thread.fpu.vxrs); |
| 171 | else |
| 172 | memcpy(&fpregs->fprs, current->thread.fpu.fprs, |
| 173 | sizeof(fpregs->fprs)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | return 1; |
| 175 | } |
Heiko Carstens | 1485c5c | 2009-03-26 15:24:04 +0100 | [diff] [blame] | 176 | EXPORT_SYMBOL(dump_fpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | unsigned long get_wchan(struct task_struct *p) |
| 179 | { |
| 180 | struct stack_frame *sf, *low, *high; |
| 181 | unsigned long return_address; |
| 182 | int count; |
| 183 | |
Al Viro | 30af712 | 2006-01-12 01:05:50 -0800 | [diff] [blame] | 184 | if (!p || p == current || p->state == TASK_RUNNING || !task_stack_page(p)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | return 0; |
Al Viro | 30af712 | 2006-01-12 01:05:50 -0800 | [diff] [blame] | 186 | low = task_stack_page(p); |
| 187 | high = (struct stack_frame *) task_pt_regs(p); |
Heiko Carstens | 9cb1cce | 2016-01-18 13:12:19 +0100 | [diff] [blame] | 188 | sf = (struct stack_frame *) p->thread.ksp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | if (sf <= low || sf > high) |
| 190 | return 0; |
| 191 | for (count = 0; count < 16; count++) { |
Heiko Carstens | 9cb1cce | 2016-01-18 13:12:19 +0100 | [diff] [blame] | 192 | sf = (struct stack_frame *) sf->back_chain; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | if (sf <= low || sf > high) |
| 194 | return 0; |
Heiko Carstens | 9cb1cce | 2016-01-18 13:12:19 +0100 | [diff] [blame] | 195 | return_address = sf->gprs[8]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | if (!in_sched_functions(return_address)) |
| 197 | return return_address; |
| 198 | } |
| 199 | return 0; |
| 200 | } |
Heiko Carstens | 9887a1f | 2011-01-12 09:55:28 +0100 | [diff] [blame] | 201 | |
| 202 | unsigned long arch_align_stack(unsigned long sp) |
| 203 | { |
| 204 | if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space) |
| 205 | sp -= get_random_int() & ~PAGE_MASK; |
| 206 | return sp & ~0xf; |
| 207 | } |
Heiko Carstens | 3351918 | 2011-01-12 09:55:30 +0100 | [diff] [blame] | 208 | |
| 209 | static inline unsigned long brk_rnd(void) |
| 210 | { |
Martin Schwidefsky | c7e8b2c | 2015-11-10 12:30:28 +0100 | [diff] [blame] | 211 | return (get_random_int() & BRK_RND_MASK) << PAGE_SHIFT; |
Heiko Carstens | 3351918 | 2011-01-12 09:55:30 +0100 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | unsigned long arch_randomize_brk(struct mm_struct *mm) |
| 215 | { |
Martin Schwidefsky | 9efe4f2 | 2013-12-17 13:41:31 +0100 | [diff] [blame] | 216 | unsigned long ret; |
Heiko Carstens | 3351918 | 2011-01-12 09:55:30 +0100 | [diff] [blame] | 217 | |
Martin Schwidefsky | 9efe4f2 | 2013-12-17 13:41:31 +0100 | [diff] [blame] | 218 | ret = PAGE_ALIGN(mm->brk + brk_rnd()); |
| 219 | return (ret > mm->brk) ? ret : mm->brk; |
Heiko Carstens | 3351918 | 2011-01-12 09:55:30 +0100 | [diff] [blame] | 220 | } |
Heiko Carstens | b5a882f | 2017-02-17 08:13:28 +0100 | [diff] [blame] | 221 | |
| 222 | void set_fs_fixup(void) |
| 223 | { |
| 224 | struct pt_regs *regs = current_pt_regs(); |
| 225 | static bool warned; |
| 226 | |
| 227 | set_fs(USER_DS); |
| 228 | if (warned) |
| 229 | return; |
| 230 | WARN(1, "Unbalanced set_fs - int code: 0x%x\n", regs->int_code); |
| 231 | show_registers(regs); |
| 232 | warned = true; |
| 233 | } |