Skip to contentSkip to conversation

MIPS: Get ready for non-executable stack.

PATCH Superseded David Daney 6 participants 14 messages Dec 3, 2014 – Dec 4, 2014 1 message not in the archive MIPS

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover MIPS: Get ready for non-executable stack.

From: David Daney <[email protected]>

Currently the MIPS FPU emulator uses eXecute Out of Line (XOL) on the stack to handle instructions in the delay slots of FPU branches. Because of this MIPS cannot have a non-executable stack.

A previous patch set from Leonid Yegoshin attempts to address the problem by moving the XOL location to a thread private mapping of a dedicated page.

I present here an alternative: Add an instruction set emulator and use it to execute the FPU delay slot instructions. The benefit of this approach is that we don't have to allocate a page per user-space thread for XOL, and we keep the TLB handling code slightly simpler as a result.

Currently this is a proof of concept, as it doesn't yet handle MIPS64 nor microMIPS instructions. But it is sufficient to run the entire Debian distribution on a FPU-less CPU.

Comments welcome.

David Daney (3):
MIPS: Add FPU emulator counter for non-FPU instructions emulated.
MIPS: Add full ISA emulator.
MIPS: Use full instruction emulation for FPU emulator delay slot
emulation.

 arch/mips/include/asm/fpu_emulator.h |   1 +
 arch/mips/kernel/Makefile            |   3 +-
 arch/mips/kernel/insn-emul.c         | 815 +++++++++++++++++++++++++++++++++++
 arch/mips/math-emu/cp1emu.c          |  13 +-
 arch/mips/math-emu/me-debugfs.c      |   1 +
 5 files changed, 830 insertions(+), 3 deletions(-)
 create mode 100644 arch/mips/kernel/insn-emul.c
Signature · 1 line
1.7.11.7
#1 lore raw

PATCH 1/3 MIPS: Add FPU emulator counter for non-FPU instructions emulated.

From: David Daney <[email protected]>

Used in follow-on patch, the counter is called "insn_emul".

---
 arch/mips/include/asm/fpu_emulator.h | 1 +
 arch/mips/math-emu/me-debugfs.c      | 1 +
 2 files changed, 2 insertions(+)
arch/mips/include/asm/fpu_emulator.h +1 -0
index 6370c82..bd5b63f 100644
@@ -45,6 +45,7 @@ struct mips_fpu_emulator_stats {
 	unsigned long ieee754_zerodiv;
 	unsigned long ieee754_invalidop;
 	unsigned long ds_emul;
+	unsigned long insn_emul;
 };
 
 DECLARE_PER_CPU(struct mips_fpu_emulator_stats, fpuemustats);
arch/mips/math-emu/me-debugfs.c +1 -0
index f308e0f..93fc155 100644
@@ -62,6 +62,7 @@ do {									\
 	FPU_STAT_CREATE(ieee754_zerodiv);
 	FPU_STAT_CREATE(ieee754_invalidop);
 	FPU_STAT_CREATE(ds_emul);
+	FPU_STAT_CREATE(insn_emul);
 
 	return 0;
 }
Signature · 1 line
1.7.11.7
#14 lore raw

PATCH 3/3 MIPS: Use full instruction emulation for FPU emulator delay slot emulation.

In reply to David Daney

From: David Daney <[email protected]>

Current delay slot handling does eXecute Out of Line (XOL) on the stack, which prevents a non-executable stack. Use the instruction emulator instead.

Tested by booting 32-bit Debian on OCTEON. More than 1700 instructions emulated to login to command line.

---
 arch/mips/math-emu/cp1emu.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
arch/mips/math-emu/cp1emu.c +11 -2
index cac529a..787de7a 100644
@@ -694,11 +694,12 @@ do {									\
  * Emulate the single floating point instruction pointed at by EPC.
  * Two instructions if the instruction is in a branch delay slot.
  */
-
+int mips_insn_emul(struct pt_regs *regs, mips_instruction ir, void *__user *fault_addr);
 static int cop1Emulate(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
 		struct mm_decoded_insn dec_insn, void *__user *fault_addr)
 {
 	unsigned long contpc = xcp->cp0_epc + dec_insn.pc_inc;
+	unsigned long origpc = xcp->cp0_epc;
 	unsigned int cond, cbit;
 	mips_instruction ir;
 	int likely, pc_inc;
@@ -1038,7 +1039,15 @@ emul:
 				 * Single step the non-cp1
 				 * instruction in the dslot
 				 */
-				return mips_dsemul(xcp, ir, contpc);
+				sig = mips_insn_emul(xcp, ir, fault_addr);
+				if (sig == 0) {
+					xcp->cp0_epc = contpc;
+					MIPS_FPU_EMU_INC_STATS(insn_emul);
+				} else {
+					xcp->cp0_epc = origpc;
+					pr_err("mips_insn_emul: %08x ->%d\n", (unsigned)ir, sig);
+				}
+				return sig;
 			} else if (likely) {	/* branch not taken */
 					/*
 					 * branch likely nullifies
Signature · 1 line
1.7.11.7
#2 lore raw

PATCH 2/3 MIPS: Add full ISA emulator.

In reply to David Daney

From: David Daney <[email protected]>

Used in follow-on patch to replace FPU delay-slot instruction execution on stack.

Only 32-bit instructions are emulated right now, still need to add 64-bit.

---
 arch/mips/kernel/Makefile    |   3 +-
 arch/mips/kernel/insn-emul.c | 815 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 817 insertions(+), 1 deletion(-)
 create mode 100644 arch/mips/kernel/insn-emul.c
arch/mips/kernel/Makefile +2 -1
index 008a2fe..b6e550d 100644
@@ -6,7 +6,8 @@ extra-y		:= head.o vmlinux.lds
 
 obj-y		+= cpu-probe.o branch.o entry.o genex.o idle.o irq.o process.o \
 		   prom.o ptrace.o reset.o setup.o signal.o syscall.o \
-		   time.o topology.o traps.o unaligned.o watch.o vdso.o
+		   time.o topology.o traps.o unaligned.o watch.o vdso.o \
+		   insn-emul.o
 
 ifdef CONFIG_FUNCTION_TRACER
 CFLAGS_REMOVE_ftrace.o = -pg
arch/mips/kernel/insn-emul.c new file +815 -0

815 lines not on this page · raw message

Signature · 1 line
1.7.11.7
#3 lore raw

Re: [PATCH 2/3] MIPS: Add full ISA emulator.

On 12/03/2014 03:44 PM, David Daney wrote:

(...)

Big work but it doesn't support customized instructions, multiple ASEs, MIPS R6 etc.

Well, it is still not a replacement of XOL emulation. Even close.

#4 lore raw

Re: [PATCH 2/3] MIPS: Add full ISA emulator.

Quoted 5 lines · Leonid Yegoshin

On 12/03/2014 03:44 PM, David Daney wrote:

(...)

Big work

Not really, although by number of lines of code, it is about 3x the size of your patch, it only touches the existing code in one place. It only took about 3 days to write, adding full MIPS64 and R6 support would probably be less than another week of work.

microMIPS I haven't looked at as we don't have anything to test it on.

Quoted 1 line

but it doesn't support customized instructions,

GCC will never put these in the delay slot of a FPU branch, so it is not needed.

Quoted 1 line

multiple ASEs,

Same as above. But any instructions that are deemed necessary can easily be added.

Quoted 1 line

MIPS R6

It is a proof of concept. R6 can easily be added if needed.

Your XOL emulation doesn't handle R6 either, so this is no worse than your patch in that respect.

Quoted 1 line

etc.

GCC will never put trapping instructions in the delay slot either.

All we have to support are non-trapping and non-branch/jump instructions from the ISA manuals that can be executed from userspace processes. That makes it slightly simpler than complete ISA emulation.

Quoted 2 lines

Well, it is still not a replacement of XOL emulation.

For use by the FPU emulator, it is probably good enough

Quoted 1 line

Even close.

I disagree, that is why I took the time to do it.

Quoted 2 lines
#5 lore raw

Re: [PATCH 2/3] MIPS: Add full ISA emulator.

Quoted 7 lines · David Daney
Quoted 5 lines · Leonid Yegoshin

On 12/03/2014 03:44 PM, David Daney wrote:

(...)

Big work

Quoted 7 lines

Not really, although by number of lines of code, it is about 3x the size of your patch, it only touches the existing code in one place. It only took about 3 days to write, adding full MIPS64 and R6 support would probably be less than another week of work.

microMIPS I haven't looked at as we don't have anything to test it on.

Quoted 1 line

but it doesn't support customized instructions,

GCC will never put these in the delay slot of a FPU branch, so it is not needed.

I doubt that it is correct in all situations and with any GCC parameter combination.

Never say never, if it is about toolchain. IMG Arch team was assured that branch likely are never used and removed it in MIPS R6, but BGEZL (or so) was a first which I hit then I ran GLIBC.

Besides GCC there are LLVM and another JITs.

Quoted 10 lines
Quoted 2 lines

multiple ASEs,

Same as above. But any instructions that are deemed necessary can easily be added.

Quoted 1 line

MIPS R6

It is a proof of concept. R6 can easily be added if needed.

Your XOL emulation doesn't handle R6 either, so this is no worse than your patch in that respect.

You probably didn't research it well. A lot of changes in arch/mips/kernel/branch.c and and arch/mips/math-emu/cp1emu.c, all of it related with R6.

Quoted 4 lines
Quoted 2 lines

etc.

GCC will never put trapping instructions in the delay slot either.

It seems like it is not correct and requires a more accurate statement. FPU instructions may trap, LWL and LWR traps on R6 with RI, etc. Yes, there are restrictions but basing a kernel on that assumptions is unsafe. The only safe is HW architecture document.

Finally, there is a manual encoding too.

Quoted 15 lines

All we have to support are non-trapping and non-branch/jump instructions from the ISA manuals that can be executed from userspace processes. That makes it slightly simpler than complete ISA emulation.

Quoted 2 lines

Well, it is still not a replacement of XOL emulation.

For use by the FPU emulator, it is probably good enough

Quoted 1 line

Even close.

I disagree, that is why I took the time to do it.

Quoted 2 lines
#6 lore raw

Re: [PATCH 2/3] MIPS: Add full ISA emulator.

Quoted 15 lines · Leonid Yegoshin
Quoted 14 lines · David Daney
Quoted 5 lines · Leonid Yegoshin

On 12/03/2014 03:44 PM, David Daney wrote:

(...)

Big work

Not really, although by number of lines of code, it is about 3x the size of your patch, it only touches the existing code in one place. It only took about 3 days to write, adding full MIPS64 and R6 support would probably be less than another week of work.

microMIPS I haven't looked at as we don't have anything to test it on.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

but it doesn't support customized instructions,

GCC will never put these in the delay slot of a FPU branch, so it is not needed.

I doubt that it is correct in all situations and with any GCC parameter combination.

My GCC experts assert that it is true.

Quoted 4 lines

Never say never, if it is about toolchain. IMG Arch team was assured that branch likely are never used and removed it in MIPS R6, but BGEZL (or so) was a first which I hit then I ran GLIBC.

The fact that the Arch Team designed R6 without bothering to determine what legacy code does is not relevant to this conversation.

Quoted 3 lines

Besides GCC there are LLVM and another JITs.

They don't do it either. But that is not really important. We can easily emulate faulting instructions if needed.

Quoted 8 lines
Quoted 8 lines
Quoted 2 lines

multiple ASEs,

Same as above. But any instructions that are deemed necessary can easily be added.

Quoted 1 line

MIPS R6

Quoted 9 lines
Quoted 4 lines

It is a proof of concept. R6 can easily be added if needed.

Your XOL emulation doesn't handle R6 either, so this is no worse than your patch in that respect.

You probably didn't research it well. A lot of changes in arch/mips/kernel/branch.c and and arch/mips/math-emu/cp1emu.c, all of it related with R6.

I looked at:
commit 3a18ca061311f2f1ee9c44012f89c7436d392117

And I saw no R6 support.

Is it there, or in some other branch that isn't merged?

Quoted 30 lines
Quoted 4 lines
Quoted 2 lines

etc.

GCC will never put trapping instructions in the delay slot either.

It seems like it is not correct and requires a more accurate statement. FPU instructions may trap, LWL and LWR traps on R6 with RI, etc. Yes, there are restrictions but basing a kernel on that assumptions is unsafe. The only safe is HW architecture document.

Finally, there is a manual encoding too.

Quoted 17 lines

All we have to support are non-trapping and non-branch/jump instructions from the ISA manuals that can be executed from userspace processes. That makes it slightly simpler than complete ISA emulation.

Quoted 2 lines

Well, it is still not a replacement of XOL emulation.

For use by the FPU emulator, it is probably good enough

Quoted 1 line

Even close.

I disagree, that is why I took the time to do it.

Quoted 2 lines
#8 lore raw

Re: [PATCH 2/3] MIPS: Add full ISA emulator.

In reply to a message not in the archive
Quoted 5 lines · Leonid Yegoshin

David,

I feel we can close a discission at that point - we disagree which approach is better, and there is no sense to continue dancing around.

That is something I do agree with.

Quoted 6 lines

I see only two technical issues here which differs:

1. You believe your GCC experts, I trust HW Architecture manual and don't trust toolchain people too much ==> we see a different value in fact that your approach has a subset of emulated ISAs (and it can't, of course, emulate anything because some custom opcodes are reused).

Yes, I agree that the emulation approach cannot handle some of the cases you mention (most would have to be the result of hand coded assembly specifically trying to break it).

Quoted 3 lines

2. My approach is ready to use and is used right now, you still have a framework which passed an initial boot.

Let's add some more, please correct me if I misstate the facts:

3) Your approach uses one additional page of memory per user space thread, even if emulation is never needed or there is a hardware FPU.

4) Your approach adds a Thread creation overhead of copy_page().

Quoted 33 lines · David Daney
Quoted 12 lines · Leonid Yegoshin
Quoted 10 lines · David Daney
Quoted 4 lines

It is a proof of concept. R6 can easily be added if needed.

Your XOL emulation doesn't handle R6 either, so this is no worse than your patch in that respect.

You probably didn't research it well. A lot of changes in arch/mips/kernel/branch.c and and arch/mips/math-emu/cp1emu.c, all of it related with R6.

Quoted 6 lines

I looked at:
commit 3a18ca061311f2f1ee9c44012f89c7436d392117

And I saw no R6 support.

Is it there, or in some other branch that isn't merged?

Sorry, I misunderstood your statement:

Yes, my "MIPS: Setup an instruction emulation in VDSO protected page instead of user stack <http://patchwork.linux-mips.org/patch/8631/>" has no any MIPS R6 specifics and actually has no any another MIPS Rx specific or FPU specific besides the fact that emulation can be done by multiple emulators and a small stack is supported in so-called "VDSO" page. I just remember that I pointed you to place where MIPS R6 is done and it has a lot of MIPS R6 instruction emulation and confused both events.

- Leonid.

#9 lore raw

Re: [PATCH 2/3] MIPS: Add full ISA emulator.

Nice work David, I like this approach. It's so much simpler than hacking atop the current dsemul code. I also imagine this could be reused for emulation of instructions removed in r6, when running pre-r6 userland binaries on r6 systems.

Quoted 11 lines · David Daney
Quoted 6 lines · Leonid Yegoshin

I see only two technical issues here which differs:

1. You believe your GCC experts, I trust HW Architecture manual and don't trust toolchain people too much ==> we see a different value in fact that your approach has a subset of emulated ISAs (and it can't, of course, emulate anything because some custom opcodes are reused).

Yes, I agree that the emulation approach cannot handle some of the cases you mention (most would have to be the result of hand coded assembly specifically trying to break it).

I'm not sure I'd agree even with that - ASEs & vendor-specific instructions could easily be added if necessary.

Quoted 2 lines · Leonid Yehoshin
Quoted 2 lines

2. My approach is ready to use and is used right now, you still have a framework which passed an initial boot.

Subjective.

Thanks,
Paul

#10 lore raw

Re: [PATCH 2/3] MIPS: Add full ISA emulator.

By all means I don't really understand the whole issues surrounding this but this approach looks better to me as well. It seems more generic and future proof and at least I can understand the patch series.

But did I say I don't understand all of this? Would be nice to hear from more people :)

Qais

Quoted 26 lines · Paul Burton

Nice work David, I like this approach. It's so much simpler than hacking atop the current dsemul code. I also imagine this could be reused for emulation of instructions removed in r6, when running pre-r6 userland binaries on r6 systems.

Quoted 10 lines · David Daney
Quoted 6 lines · Leonid Yegoshin

I see only two technical issues here which differs:

1. You believe your GCC experts, I trust HW Architecture manual and don't trust toolchain people too much ==> we see a different value in fact that your approach has a subset of emulated ISAs (and it can't, of course, emulate anything because some custom opcodes are reused).

Yes, I agree that the emulation approach cannot handle some of the cases you mention (most would have to be the result of hand coded assembly specifically trying to break it).

I'm not sure I'd agree even with that - ASEs & vendor-specific instructions could easily be added if necessary.

Quoted 2 lines · Leonid Yehoshin
Quoted 2 lines

2. My approach is ready to use and is used right now, you still have a framework which passed an initial boot.

Subjective.

Thanks,
Paul

#11 lore raw

Re: [PATCH 2/3] MIPS: Add full ISA emulator.

In reply to David Daney
Quoted 9 lines · David Daney
Quoted 1 line

but it doesn't support customized instructions,

GCC will never put these in the delay slot of a FPU branch, so it is not needed.

Quoted 1 line

multiple ASEs,

Same as above. But any instructions that are deemed necessary can easily be added.

GAS will happily schedule any instruction into a branch delay slot as
long as the instruction is not architecturally forbidden there (e.g. ERET), there is no data dependency with the branch that would affect the result produced and the instruction is not an explicit exception trap operation (BREAK, SYSCALL, TEQ, etc.). For some reason, unknown to me all MT ASE instructions are disallowed too. Anything else -- free to go in!

Of course instructions can be scheduled into branch delay slots manually
too, in handcoded assembly, and that has to continue working.

Maciej

#12 lore raw

Re: [PATCH 2/3] MIPS: Add full ISA emulator.

Quoted 19 lines · Maciej W. Rozycki
Quoted 9 lines · David Daney
Quoted 1 line

but it doesn't support customized instructions,

GCC will never put these in the delay slot of a FPU branch, so it is not needed.

Quoted 1 line

multiple ASEs,

Same as above. But any instructions that are deemed necessary can easily be added.

GAS will happily schedule any instruction into a branch delay slot as
long as the instruction is not architecturally forbidden there (e.g. ERET), there is no data dependency with the branch that would affect the result produced and the instruction is not an explicit exception trap operation (BREAK, SYSCALL, TEQ, etc.). For some reason, unknown to me all MT ASE instructions are disallowed too. Anything else -- free to go in!

Quoted 3 lines

Of course instructions can be scheduled into branch delay slots manually
too, in handcoded assembly, and that has to continue working.

It is not difficult to also emulate the trapping instructions. In order to move forward, I will implement the trapping instructions in my emulator for the next patch.

Thanks,
David Daney

#13 lore raw

Re: [PATCH 2/3] MIPS: Add full ISA emulator.

Quoted 7 lines · David Daney
Quoted 7 lines

GAS will happily schedule any instruction into a branch delay slot as
long as the instruction is not architecturally forbidden there (e.g. ERET), there is no data dependency with the branch that would affect the result produced and the instruction is not an explicit exception trap operation (BREAK, SYSCALL, TEQ, etc.). For some reason, unknown to me all MT ASE instructions are disallowed too. Anything else -- free to go in!

Quoted 7 lines
Quoted 3 lines

Of course instructions can be scheduled into branch delay slots manually
too, in handcoded assembly, and that has to continue working.

It is not difficult to also emulate the trapping instructions. In order to move forward, I will implement the trapping instructions in my emulator for the next patch.

I'd be more concerned about getting the more exotic instructions or cases
right (did you get MADDU right for SmartMIPS processors and set the ACX register on them?) -- how do you propose to validate and regression-test the emulator in a reproducible manner?

The combination of the rare case of an instruction being placed in an FP
branch delay slot and the rarity of some instructions themselves makes me scared of bugs lurking there forever and occasionally biting people -- who may not be aware that software emulation is involved let alone be capable to track them down -- in the most frustrating way. To say nothing of the infinite amount of effort to maintain the emulator associated with adding architectural and vendor-specific instructions. See how much effort has been put into QEMU and still it does not get all the MIPS instruction set bits right.

Maciej