aboutsummaryrefslogtreecommitdiffstats
path: root/arch/mips/include
diff options
context:
space:
mode:
authorPaul Burton <paul.burton@imgtec.com>2016-07-08 06:06:19 -0400
committerRalf Baechle <ralf@linux-mips.org>2016-08-02 03:28:53 -0400
commit432c6bacbd0c16ec210c43da411ccc3855c4c010 (patch)
treed71032862a6ee0c04c021fb57e1a127b8d3a0466 /arch/mips/include
parent33799a6d1aeb892862d5f69ee87195becabf8d0c (diff)
MIPS: Use per-mm page to execute branch delay slot instructions
In some cases the kernel needs to execute an instruction from the delay slot of an emulated branch instruction. These cases include: - Emulated floating point branch instructions (bc1[ft]l?) for systems which don't include an FPU, or upon which the kernel is run with the "nofpu" parameter. - MIPSr6 systems running binaries targeting older revisions of the architecture, which may include branch instructions whose encodings are no longer valid in MIPSr6. Executing instructions from such delay slots is done by writing the instruction to memory followed by a trap, as part of an "emuframe", and executing it. This avoids the requirement of an emulator for the entire MIPS instruction set. Prior to this patch such emuframes are written to the user stack and executed from there. This patch moves FP branch delay emuframes off of the user stack and into a per-mm page. Allocating a page per-mm leaves userland with access to only what it had access to previously, and compared to other solutions is relatively simple. When a thread requires a delay slot emulation, it is allocated a frame. A thread may only have one frame allocated at any one time, since it may only ever be executing one instruction at any one time. In order to ensure that we can free up allocated frame later, its index is recorded in struct thread_struct. In the typical case, after executing the delay slot instruction we'll execute a break instruction with the BRK_MEMU code. This traps back to the kernel & leads to a call to do_dsemulret which frees the allocated frame & moves the user PC back to the instruction that would have executed following the emulated branch. In some cases the delay slot instruction may be invalid, such as a branch, or may trigger an exception. In these cases the BRK_MEMU break instruction will not be hit. In order to ensure that frames are freed this patch introduces dsemul_thread_cleanup() and calls it to free any allocated frame upon thread exit. If the instruction generated an exception & leads to a signal being delivered to the thread, or indeed if a signal simply happens to be delivered to the thread whilst it is executing from the struct emuframe, then we need to take care to exit the frame appropriately. This is done by either rolling back the user PC to the branch or advancing it to the continuation PC prior to signal delivery, using dsemul_thread_rollback(). If this were not done then a sigreturn would return to the struct emuframe, and if that frame had meanwhile been used in response to an emulated branch instruction within the signal handler then we would execute the wrong user code. Whilst a user could theoretically place something like a compact branch to self in a delay slot and cause their thread to become stuck in an infinite loop with the frame never being deallocated, this would: - Only affect the users single process. - Be architecturally invalid since there would be a branch in the delay slot, which is forbidden. - Be extremely unlikely to happen by mistake, and provide a program with no more ability to harm the system than a simple infinite loop would. If a thread requires a delay slot emulation & no frame is available to it (ie. the process has enough other threads that all frames are currently in use) then the thread joins a waitqueue. It will sleep until a frame is freed by another thread in the process. Since we now know whether a thread has an allocated frame due to our tracking of its index, the cookie field of struct emuframe is removed as we can be more certain whether we have a valid frame. Since a thread may only ever have a single frame at any given time, the epc field of struct emuframe is also removed & the PC to continue from is instead stored in struct thread_struct. Together these changes simplify & shrink struct emuframe somewhat, allowing twice as many frames to fit into the page allocated for them. The primary benefit of this patch is that we are now free to mark the user stack non-executable where that is possible. Signed-off-by: Paul Burton <paul.burton@imgtec.com> Cc: Leonid Yegoshin <leonid.yegoshin@imgtec.com> Cc: Maciej Rozycki <maciej.rozycki@imgtec.com> Cc: Faraz Shahbazker <faraz.shahbazker@imgtec.com> Cc: Raghu Gandham <raghu.gandham@imgtec.com> Cc: Matthew Fortune <matthew.fortune@imgtec.com> Cc: linux-mips@linux-mips.org Patchwork: https://patchwork.linux-mips.org/patch/13764/ Signed-off-by: Ralf Baechle <ralf@linux-mips.org>
Diffstat (limited to 'arch/mips/include')
-rw-r--r--arch/mips/include/asm/dsemul.h92
-rw-r--r--arch/mips/include/asm/fpu_emulator.h17
-rw-r--r--arch/mips/include/asm/mmu.h9
-rw-r--r--arch/mips/include/asm/mmu_context.h6
-rw-r--r--arch/mips/include/asm/processor.h18
5 files changed, 127 insertions, 15 deletions
diff --git a/arch/mips/include/asm/dsemul.h b/arch/mips/include/asm/dsemul.h
new file mode 100644
index 000000000000..a6e067801f23
--- /dev/null
+++ b/arch/mips/include/asm/dsemul.h
@@ -0,0 +1,92 @@
1/*
2 * Copyright (C) 2016 Imagination Technologies
3 * Author: Paul Burton <paul.burton@imgtec.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
9 */
10
11#ifndef __MIPS_ASM_DSEMUL_H__
12#define __MIPS_ASM_DSEMUL_H__
13
14#include <asm/break.h>
15#include <asm/inst.h>
16
17/* Break instruction with special math emu break code set */
18#define BREAK_MATH(micromips) (((micromips) ? 0x7 : 0xd) | (BRK_MEMU << 16))
19
20/* When used as a frame index, indicates the lack of a frame */
21#define BD_EMUFRAME_NONE ((int)BIT(31))
22
23struct mm_struct;
24struct pt_regs;
25struct task_struct;
26
27/**
28 * mips_dsemul() - 'Emulate' an instruction from a branch delay slot
29 * @regs: User thread register context.
30 * @ir: The instruction to be 'emulated'.
31 * @branch_pc: The PC of the branch instruction.
32 * @cont_pc: The PC to continue at following 'emulation'.
33 *
34 * Emulate or execute an arbitrary MIPS instruction within the context of
35 * the current user thread. This is used primarily to handle instructions
36 * in the delay slots of emulated branch instructions, for example FP
37 * branch instructions on systems without an FPU.
38 *
39 * Return: Zero on success, negative if ir is a NOP, signal number on failure.
40 */
41extern int mips_dsemul(struct pt_regs *regs, mips_instruction ir,
42 unsigned long branch_pc, unsigned long cont_pc);
43
44/**
45 * do_dsemulret() - Return from a delay slot 'emulation' frame
46 * @xcp: User thread register context.
47 *
48 * Call in response to the BRK_MEMU break instruction used to return to
49 * the kernel from branch delay slot 'emulation' frames following a call
50 * to mips_dsemul(). Restores the user thread PC to the value that was
51 * passed as the cpc parameter to mips_dsemul().
52 *
53 * Return: True if an emulation frame was returned from, else false.
54 */
55extern bool do_dsemulret(struct pt_regs *xcp);
56
57/**
58 * dsemul_thread_cleanup() - Cleanup thread 'emulation' frame
59 * @tsk: The task structure associated with the thread
60 *
61 * If the thread @tsk has a branch delay slot 'emulation' frame
62 * allocated to it then free that frame.
63 *
64 * Return: True if a frame was freed, else false.
65 */
66extern bool dsemul_thread_cleanup(struct task_struct *tsk);
67
68/**
69 * dsemul_thread_rollback() - Rollback from an 'emulation' frame
70 * @regs: User thread register context.
71 *
72 * If the current thread, whose register context is represented by @regs,
73 * is executing within a delay slot 'emulation' frame then exit that
74 * frame. The PC will be rolled back to the branch if the instruction
75 * that was being 'emulated' has not yet executed, or advanced to the
76 * continuation PC if it has.
77 *
78 * Return: True if a frame was exited, else false.
79 */
80extern bool dsemul_thread_rollback(struct pt_regs *regs);
81
82/**
83 * dsemul_mm_cleanup() - Cleanup per-mm delay slot 'emulation' state
84 * @mm: The struct mm_struct to cleanup state for.
85 *
86 * Cleanup state for the given @mm, ensuring that any memory allocated
87 * for delay slot 'emulation' book-keeping is freed. This is to be called
88 * before @mm is freed in order to avoid memory leaks.
89 */
90extern void dsemul_mm_cleanup(struct mm_struct *mm);
91
92#endif /* __MIPS_ASM_DSEMUL_H__ */
diff --git a/arch/mips/include/asm/fpu_emulator.h b/arch/mips/include/asm/fpu_emulator.h
index 3225c3c0724b..355dc25172e7 100644
--- a/arch/mips/include/asm/fpu_emulator.h
+++ b/arch/mips/include/asm/fpu_emulator.h
@@ -24,7 +24,7 @@
24#define _ASM_FPU_EMULATOR_H 24#define _ASM_FPU_EMULATOR_H
25 25
26#include <linux/sched.h> 26#include <linux/sched.h>
27#include <asm/break.h> 27#include <asm/dsemul.h>
28#include <asm/thread_info.h> 28#include <asm/thread_info.h>
29#include <asm/inst.h> 29#include <asm/inst.h>
30#include <asm/local.h> 30#include <asm/local.h>
@@ -60,27 +60,16 @@ do { \
60#define MIPS_FPU_EMU_INC_STATS(M) do { } while (0) 60#define MIPS_FPU_EMU_INC_STATS(M) do { } while (0)
61#endif /* CONFIG_DEBUG_FS */ 61#endif /* CONFIG_DEBUG_FS */
62 62
63extern int mips_dsemul(struct pt_regs *regs, mips_instruction ir,
64 unsigned long cpc);
65extern int do_dsemulret(struct pt_regs *xcp);
66extern int fpu_emulator_cop1Handler(struct pt_regs *xcp, 63extern int fpu_emulator_cop1Handler(struct pt_regs *xcp,
67 struct mips_fpu_struct *ctx, int has_fpu, 64 struct mips_fpu_struct *ctx, int has_fpu,
68 void *__user *fault_addr); 65 void *__user *fault_addr);
69int process_fpemu_return(int sig, void __user *fault_addr, 66int process_fpemu_return(int sig, void __user *fault_addr,
70 unsigned long fcr31); 67 unsigned long fcr31);
68int isBranchInstr(struct pt_regs *regs, struct mm_decoded_insn dec_insn,
69 unsigned long *contpc);
71int mm_isBranchInstr(struct pt_regs *regs, struct mm_decoded_insn dec_insn, 70int mm_isBranchInstr(struct pt_regs *regs, struct mm_decoded_insn dec_insn,
72 unsigned long *contpc); 71 unsigned long *contpc);
73 72
74/*
75 * Instruction inserted following the badinst to further tag the sequence
76 */
77#define BD_COOKIE 0x0000bd36 /* tne $0, $0 with baggage */
78
79/*
80 * Break instruction with special math emu break code set
81 */
82#define BREAK_MATH(micromips) (((micromips) ? 0x7 : 0xd) | (BRK_MEMU << 16))
83
84#define SIGNALLING_NAN 0x7ff800007ff80000LL 73#define SIGNALLING_NAN 0x7ff800007ff80000LL
85 74
86static inline void fpu_emulator_init_fpu(void) 75static inline void fpu_emulator_init_fpu(void)
diff --git a/arch/mips/include/asm/mmu.h b/arch/mips/include/asm/mmu.h
index 1afa1f986df8..f6ba08d77931 100644
--- a/arch/mips/include/asm/mmu.h
+++ b/arch/mips/include/asm/mmu.h
@@ -2,11 +2,20 @@
2#define __ASM_MMU_H 2#define __ASM_MMU_H
3 3
4#include <linux/atomic.h> 4#include <linux/atomic.h>
5#include <linux/spinlock.h>
6#include <linux/wait.h>
5 7
6typedef struct { 8typedef struct {
7 unsigned long asid[NR_CPUS]; 9 unsigned long asid[NR_CPUS];
8 void *vdso; 10 void *vdso;
9 atomic_t fp_mode_switching; 11 atomic_t fp_mode_switching;
12
13 /* lock to be held whilst modifying fp_bd_emupage_allocmap */
14 spinlock_t bd_emupage_lock;
15 /* bitmap tracking allocation of fp_bd_emupage */
16 unsigned long *bd_emupage_allocmap;
17 /* wait queue for threads requiring an emuframe */
18 wait_queue_head_t bd_emupage_queue;
10} mm_context_t; 19} mm_context_t;
11 20
12#endif /* __ASM_MMU_H */ 21#endif /* __ASM_MMU_H */
diff --git a/arch/mips/include/asm/mmu_context.h b/arch/mips/include/asm/mmu_context.h
index fc57e135cb0a..ddd57ade1aa8 100644
--- a/arch/mips/include/asm/mmu_context.h
+++ b/arch/mips/include/asm/mmu_context.h
@@ -16,6 +16,7 @@
16#include <linux/smp.h> 16#include <linux/smp.h>
17#include <linux/slab.h> 17#include <linux/slab.h>
18#include <asm/cacheflush.h> 18#include <asm/cacheflush.h>
19#include <asm/dsemul.h>
19#include <asm/hazards.h> 20#include <asm/hazards.h>
20#include <asm/tlbflush.h> 21#include <asm/tlbflush.h>
21#include <asm-generic/mm_hooks.h> 22#include <asm-generic/mm_hooks.h>
@@ -128,6 +129,10 @@ init_new_context(struct task_struct *tsk, struct mm_struct *mm)
128 129
129 atomic_set(&mm->context.fp_mode_switching, 0); 130 atomic_set(&mm->context.fp_mode_switching, 0);
130 131
132 mm->context.bd_emupage_allocmap = NULL;
133 spin_lock_init(&mm->context.bd_emupage_lock);
134 init_waitqueue_head(&mm->context.bd_emupage_queue);
135
131 return 0; 136 return 0;
132} 137}
133 138
@@ -162,6 +167,7 @@ static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
162 */ 167 */
163static inline void destroy_context(struct mm_struct *mm) 168static inline void destroy_context(struct mm_struct *mm)
164{ 169{
170 dsemul_mm_cleanup(mm);
165} 171}
166 172
167#define deactivate_mm(tsk, mm) do { } while (0) 173#define deactivate_mm(tsk, mm) do { } while (0)
diff --git a/arch/mips/include/asm/processor.h b/arch/mips/include/asm/processor.h
index 7e78b6208d7d..0d36c87acbe2 100644
--- a/arch/mips/include/asm/processor.h
+++ b/arch/mips/include/asm/processor.h
@@ -11,12 +11,14 @@
11#ifndef _ASM_PROCESSOR_H 11#ifndef _ASM_PROCESSOR_H
12#define _ASM_PROCESSOR_H 12#define _ASM_PROCESSOR_H
13 13
14#include <linux/atomic.h>
14#include <linux/cpumask.h> 15#include <linux/cpumask.h>
15#include <linux/threads.h> 16#include <linux/threads.h>
16 17
17#include <asm/cachectl.h> 18#include <asm/cachectl.h>
18#include <asm/cpu.h> 19#include <asm/cpu.h>
19#include <asm/cpu-info.h> 20#include <asm/cpu-info.h>
21#include <asm/dsemul.h>
20#include <asm/mipsregs.h> 22#include <asm/mipsregs.h>
21#include <asm/prefetch.h> 23#include <asm/prefetch.h>
22 24
@@ -78,7 +80,11 @@ extern unsigned int vced_count, vcei_count;
78 80
79#endif 81#endif
80 82
81#define STACK_TOP (TASK_SIZE & PAGE_MASK) 83/*
84 * One page above the stack is used for branch delay slot "emulation".
85 * See dsemul.c for details.
86 */
87#define STACK_TOP ((TASK_SIZE & PAGE_MASK) - PAGE_SIZE)
82 88
83/* 89/*
84 * This decides where the kernel will search for a free chunk of vm 90 * This decides where the kernel will search for a free chunk of vm
@@ -256,6 +262,12 @@ struct thread_struct {
256 262
257 /* Saved fpu/fpu emulator stuff. */ 263 /* Saved fpu/fpu emulator stuff. */
258 struct mips_fpu_struct fpu FPU_ALIGN; 264 struct mips_fpu_struct fpu FPU_ALIGN;
265 /* Assigned branch delay slot 'emulation' frame */
266 atomic_t bd_emu_frame;
267 /* PC of the branch from a branch delay slot 'emulation' */
268 unsigned long bd_emu_branch_pc;
269 /* PC to continue from following a branch delay slot 'emulation' */
270 unsigned long bd_emu_cont_pc;
259#ifdef CONFIG_MIPS_MT_FPAFF 271#ifdef CONFIG_MIPS_MT_FPAFF
260 /* Emulated instruction count */ 272 /* Emulated instruction count */
261 unsigned long emulated_fp; 273 unsigned long emulated_fp;
@@ -323,6 +335,10 @@ struct thread_struct {
323 * FPU affinity state (null if not FPAFF) \ 335 * FPU affinity state (null if not FPAFF) \
324 */ \ 336 */ \
325 FPAFF_INIT \ 337 FPAFF_INIT \
338 /* Delay slot emulation */ \
339 .bd_emu_frame = ATOMIC_INIT(BD_EMUFRAME_NONE), \
340 .bd_emu_branch_pc = 0, \
341 .bd_emu_cont_pc = 0, \
326 /* \ 342 /* \
327 * Saved DSP stuff \ 343 * Saved DSP stuff \
328 */ \ 344 */ \