blob: 6e758bb6cd29b70821bec49fd69b6d8d76111d2e [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Heiko Carstenscbdc2292009-03-26 15:23:52 +01003 * This file handles the architecture dependent parts of process handling.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02005 * Copyright IBM Corp. 1999, 2009
Heiko Carstenscbdc2292009-03-26 15:23:52 +01006 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>,
7 * Hartmut Penner <hp@de.ibm.com>,
8 * Denis Joseph Barrow,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
Heiko Carstensca218722016-05-07 12:15:34 +020011#include <linux/elf-randomize.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/compiler.h>
13#include <linux/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/sched.h>
Ingo Molnarb17b0152017-02-08 18:51:35 +010015#include <linux/sched/debug.h>
Ingo Molnar29930022017-02-08 18:51:36 +010016#include <linux/sched/task.h>
Ingo Molnar68db0cf2017-02-08 18:51:37 +010017#include <linux/sched/task_stack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/kernel.h>
19#include <linux/mm.h>
Martin Schwidefsky638ad342011-10-30 15:17:13 +010020#include <linux/elfcore.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/smp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/interrupt.h>
Heiko Carstens5a62b192008-04-17 07:46:25 +020024#include <linux/tick.h>
Heiko Carstens9887a1f2011-01-12 09:55:28 +010025#include <linux/personality.h>
Heiko Carstens26689452009-01-14 14:14:36 +010026#include <linux/syscalls.h>
Heiko Carstens3e86a8c2009-09-22 22:58:42 +020027#include <linux/compat.h>
Martin Schwidefsky860dba42011-01-05 12:47:25 +010028#include <linux/kprobes.h>
Heiko Carstens9887a1f2011-01-12 09:55:28 +010029#include <linux/random.h>
Paul Gortmaker3994a522017-02-09 15:20:23 -050030#include <linux/export.h>
Hendrik Brueckner0ac27772015-09-29 17:53:22 +020031#include <linux/init_task.h>
Martin Schwidefsky2317b072018-04-20 10:21:21 +020032#include <asm/cpu_mf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <asm/io.h>
34#include <asm/processor.h>
Martin Schwidefsky27f6b412012-07-20 11:15:08 +020035#include <asm/vtimer.h>
Heiko Carstensf6e38692012-09-14 13:45:24 +020036#include <asm/exec.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <asm/irq.h>
Heiko Carstensf5daba12009-03-26 15:24:01 +010038#include <asm/nmi.h>
Heiko Carstensda7f51c2011-01-05 12:48:09 +010039#include <asm/smp.h>
David Howellsa0616cd2012-03-28 18:30:02 +010040#include <asm/switch_to.h>
Jan Glaubere4b8b3f2012-07-31 10:52:05 +020041#include <asm/runtime_instr.h>
Heiko Carstensa8061702008-04-17 07:46:26 +020042#include "entry.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020044asmlinkage void ret_from_fork(void) asm ("ret_from_fork");
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Heiko Carstens7a5388d2014-10-22 12:42:38 +020046extern void kernel_thread_starter(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Linus Torvalds1da177e2005-04-16 15:20:36 -070048void flush_thread(void)
49{
Linus Torvalds1da177e2005-04-16 15:20:36 -070050}
51
Martin Schwidefsky2317b072018-04-20 10:21:21 +020052void 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 Brueckner6a039ea2015-02-09 16:47:00 +010061void arch_release_task_struct(struct task_struct *tsk)
62{
Heiko Carstens8d9047f2017-09-11 11:24:22 +020063 runtime_instr_release(tsk);
Heiko Carstens7b83c622017-09-11 11:24:23 +020064 guarded_storage_release(tsk);
Hendrik Brueckner155e8392015-06-11 16:57:20 +020065}
66
67int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
68{
Hendrik Brueckner9977e882015-06-10 12:53:42 +020069 /*
70 * Save the floating-point or vector register state of the current
Hendrik Bruecknerb5510d92015-09-29 10:04:41 +020071 * task and set the CIF_FPU flag to lazy restore the FPU register
72 * state when returning to user space.
Hendrik Brueckner9977e882015-06-10 12:53:42 +020073 */
Hendrik Bruecknerd0164ee2015-06-29 16:43:06 +020074 save_fpu_regs();
Hendrik Bruecknerb5510d92015-09-29 10:04:41 +020075
Martin Schwidefsky3f6813b2016-04-01 15:42:15 +020076 memcpy(dst, src, arch_task_struct_size);
77 dst->thread.fpu.regs = dst->thread.fpu.fprs;
Hendrik Brueckner155e8392015-06-11 16:57:20 +020078 return 0;
Hendrik Brueckner6a039ea2015-02-09 16:47:00 +010079}
Hendrik Brueckner6a039ea2015-02-09 16:47:00 +010080
Heiko Carstensf50c0e62017-02-20 09:38:42 +010081int copy_thread_tls(unsigned long clone_flags, unsigned long new_stackp,
82 unsigned long arg, struct task_struct *p, unsigned long tls)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
Heiko Carstenscbdc2292009-03-26 15:23:52 +010084 struct fake_frame
85 {
86 struct stack_frame sf;
87 struct pt_regs childregs;
88 } *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Heiko Carstenscbdc2292009-03-26 15:23:52 +010090 frame = container_of(task_pt_regs(p), struct fake_frame, childregs);
91 p->thread.ksp = (unsigned long) frame;
Al Virof9a7e022012-09-21 20:48:32 -040092 /* 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 Carstensa1c5bef2017-11-09 12:29:34 +0100100 p->thread.per_flags = 0;
Al Virof9a7e022012-09-21 20:48:32 -0400101 /* Initialize per thread user and system timer values */
Martin Schwidefsky90c53e62016-11-08 12:15:59 +0100102 p->thread.user_timer = 0;
Martin Schwidefsky3c915bd2017-03-01 09:18:34 +0100103 p->thread.guest_timer = 0;
Martin Schwidefsky90c53e62016-11-08 12:15:59 +0100104 p->thread.system_timer = 0;
Martin Schwidefsky3c915bd2017-03-01 09:18:34 +0100105 p->thread.hardirq_timer = 0;
106 p->thread.softirq_timer = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Al Virof9a7e022012-09-21 20:48:32 -0400108 frame->sf.back_chain = 0;
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100109 /* new return point is ret_from_fork */
110 frame->sf.gprs[8] = (unsigned long) ret_from_fork;
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100111 /* fake return stack for resume(), don't go back to schedule */
112 frame->sf.gprs[9] = (unsigned long) frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Al Virof9a7e022012-09-21 20:48:32 -0400114 /* Store access registers to kernel stack of new process. */
Al Viro87f1ca82012-10-21 16:44:27 -0400115 if (unlikely(p->flags & PF_KTHREAD)) {
Al Virof9a7e022012-09-21 20:48:32 -0400116 /* kernel thread */
117 memset(&frame->childregs, 0, sizeof(struct pt_regs));
Martin Schwidefskye258d712013-09-24 09:14:56 +0200118 frame->childregs.psw.mask = PSW_KERNEL_BITS | PSW_MASK_DAT |
Al Virof9a7e022012-09-21 20:48:32 -0400119 PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK;
Heiko Carstensfecc868a2016-01-18 12:49:44 +0100120 frame->childregs.psw.addr =
Al Virof9a7e022012-09-21 20:48:32 -0400121 (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 Viro87f1ca82012-10-21 16:44:27 -0400129 frame->childregs = *current_pt_regs();
Al Virof9a7e022012-09-21 20:48:32 -0400130 frame->childregs.gprs[2] = 0; /* child returns 0 on fork. */
Martin Schwidefskyd3a73ac2014-04-15 12:55:07 +0200131 frame->childregs.flags = 0;
Al Viro87f1ca82012-10-21 16:44:27 -0400132 if (new_stackp)
133 frame->childregs.gprs[15] = new_stackp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Jan Glaubere4b8b3f2012-07-31 10:52:05 +0200135 /* Don't copy runtime instrumentation info */
136 p->thread.ri_cb = NULL;
Jan Glaubere4b8b3f2012-07-31 10:52:05 +0200137 frame->childregs.psw.mask &= ~PSW_MASK_RI;
Martin Schwidefsky916cda12016-01-26 14:10:34 +0100138 /* Don't copy guarded storage control block */
139 p->thread.gs_cb = NULL;
140 p->thread.gs_bc_cb = NULL;
Jan Glaubere4b8b3f2012-07-31 10:52:05 +0200141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 /* Set a new TLS ? */
143 if (clone_flags & CLONE_SETTLS) {
Heiko Carstens77575912009-06-12 10:26:25 +0200144 if (is_compat_task()) {
Al Viro87f1ca82012-10-21 16:44:27 -0400145 p->thread.acrs[0] = (unsigned int)tls;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 } else {
Al Viro87f1ca82012-10-21 16:44:27 -0400147 p->thread.acrs[0] = (unsigned int)(tls >> 32);
148 p->thread.acrs[1] = (unsigned int)tls;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 }
150 }
Heiko Carstenscbdc2292009-03-26 15:23:52 +0100151 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152}
153
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200154asmlinkage void execve_tail(void)
155{
Hendrik Brueckner904818e2015-06-11 15:33:54 +0200156 current->thread.fpu.fpc = 0;
Heiko Carstense47994d2015-07-06 15:02:37 +0200157 asm volatile("sfpc %0" : : "d" (0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158}
159
160/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 * fill in the FPU structure for a core dump.
162 */
163int dump_fpu (struct pt_regs * regs, s390_fp_regs *fpregs)
164{
Hendrik Bruecknerd0164ee2015-06-29 16:43:06 +0200165 save_fpu_regs();
Hendrik Brueckner9977e882015-06-10 12:53:42 +0200166 fpregs->fpc = current->thread.fpu.fpc;
167 fpregs->pad = 0;
Hendrik Bruecknerb5510d92015-09-29 10:04:41 +0200168 if (MACHINE_HAS_VX)
Hendrik Brueckner9977e882015-06-10 12:53:42 +0200169 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 Torvalds1da177e2005-04-16 15:20:36 -0700174 return 1;
175}
Heiko Carstens1485c5c2009-03-26 15:24:04 +0100176EXPORT_SYMBOL(dump_fpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178unsigned 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 Viro30af7122006-01-12 01:05:50 -0800184 if (!p || p == current || p->state == TASK_RUNNING || !task_stack_page(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 return 0;
Al Viro30af7122006-01-12 01:05:50 -0800186 low = task_stack_page(p);
187 high = (struct stack_frame *) task_pt_regs(p);
Heiko Carstens9cb1cce2016-01-18 13:12:19 +0100188 sf = (struct stack_frame *) p->thread.ksp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 if (sf <= low || sf > high)
190 return 0;
191 for (count = 0; count < 16; count++) {
Heiko Carstens9cb1cce2016-01-18 13:12:19 +0100192 sf = (struct stack_frame *) sf->back_chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 if (sf <= low || sf > high)
194 return 0;
Heiko Carstens9cb1cce2016-01-18 13:12:19 +0100195 return_address = sf->gprs[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 if (!in_sched_functions(return_address))
197 return return_address;
198 }
199 return 0;
200}
Heiko Carstens9887a1f2011-01-12 09:55:28 +0100201
202unsigned 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 Carstens33519182011-01-12 09:55:30 +0100208
209static inline unsigned long brk_rnd(void)
210{
Martin Schwidefskyc7e8b2c2015-11-10 12:30:28 +0100211 return (get_random_int() & BRK_RND_MASK) << PAGE_SHIFT;
Heiko Carstens33519182011-01-12 09:55:30 +0100212}
213
214unsigned long arch_randomize_brk(struct mm_struct *mm)
215{
Martin Schwidefsky9efe4f22013-12-17 13:41:31 +0100216 unsigned long ret;
Heiko Carstens33519182011-01-12 09:55:30 +0100217
Martin Schwidefsky9efe4f22013-12-17 13:41:31 +0100218 ret = PAGE_ALIGN(mm->brk + brk_rnd());
219 return (ret > mm->brk) ? ret : mm->brk;
Heiko Carstens33519182011-01-12 09:55:30 +0100220}
Heiko Carstensb5a882f2017-02-17 08:13:28 +0100221
222void 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}