blob: 39c20afad7ed9ed3b4b967a54d3a435e07eccf9e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 NetWinder Floating Point Emulator
3 (c) Rebel.COM, 1998
4 (c) 1998, 1999 Philip Blundell
5
6 Direct questions, comments to Scott Bambrough <scottb@netwinder.org>
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21*/
Russell King6ebbf2c2014-06-30 16:29:12 +010022#include <asm/assembler.h>
Leif Lindholme7f626d2011-12-12 19:44:49 +010023#include <asm/opcodes.h>
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025/* This is the kernel's entry point into the floating point emulator.
26It is called from the kernel with code similar to this:
27
28 sub r4, r5, #4
29 ldrt r0, [r4] @ r0 = instruction
30 adrsvc al, r9, ret_from_exception @ r9 = normal FP return
31 adrsvc al, lr, fpundefinstr @ lr = undefined instr return
32
33 get_current_task r10
34 mov r8, #1
35 strb r8, [r10, #TSK_USED_MATH] @ set current->used_math
36 add r10, r10, #TSS_FPESAVE @ r10 = workspace
37 ldr r4, .LC2
38 ldr pc, [r4] @ Call FP emulator entry point
39
40The kernel expects the emulator to return via one of two possible
41points of return it passes to the emulator. The emulator, if
42successful in its emulation, jumps to ret_from_exception (passed in
43r9) and the kernel takes care of returning control from the trap to
44the user code. If the emulator is unable to emulate the instruction,
45it returns via _fpundefinstr (passed via lr) and the kernel halts the
46user program with a core dump.
47
48On entry to the emulator r10 points to an area of private FP workspace
49reserved in the thread structure for this process. This is where the
50emulator saves its registers across calls. The first word of this area
51is used as a flag to detect the first time a process uses floating point,
52so that the emulator startup cost can be avoided for tasks that don't
53want it.
54
55This routine does three things:
56
571) The kernel has created a struct pt_regs on the stack and saved the
58user registers into it. See /usr/include/asm/proc/ptrace.h for details.
59
602) It calls EmulateAll to emulate a floating point instruction.
61EmulateAll returns 1 if the emulation was successful, or 0 if not.
62
633) If an instruction has been emulated successfully, it looks ahead at
64the next instruction. If it is a floating point instruction, it
65executes the instruction, without returning to user space. In this
66way it repeatedly looks ahead and executes floating point instructions
67until it encounters a non floating point instruction, at which time it
68returns via _fpreturn.
69
70This is done to reduce the effect of the trap overhead on each
71floating point instructions. GCC attempts to group floating point
72instructions to allow the emulator to spread the cost of the trap over
73several floating point instructions. */
74
Catalin Marinasc1f438f2007-09-25 15:21:00 +010075#include <asm/asm-offsets.h>
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 .globl nwfpe_enter
78nwfpe_enter:
79 mov r4, lr @ save the failure-return addresses
80 mov sl, sp @ we access the registers via 'sl'
81
Catalin Marinasc1f438f2007-09-25 15:21:00 +010082 ldr r5, [sp, #S_PC] @ get contents of PC;
83 mov r6, r0 @ save the opcode
Linus Torvalds1da177e2005-04-16 15:20:36 -070084emulate:
Catalin Marinasc1f438f2007-09-25 15:21:00 +010085 ldr r1, [sp, #S_PSR] @ fetch the PSR
Leif Lindholme7f626d2011-12-12 19:44:49 +010086 bl arm_check_condition @ check the condition
87 cmp r0, #ARM_OPCODE_CONDTEST_PASS @ condition passed?
Catalin Marinasc1f438f2007-09-25 15:21:00 +010088
89 @ if condition code failed to match, next insn
Leif Lindholme7f626d2011-12-12 19:44:49 +010090 bne next @ get the next instruction;
Catalin Marinasc1f438f2007-09-25 15:21:00 +010091
92 mov r0, r6 @ prepare for EmulateAll()
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 bl EmulateAll @ emulate the instruction
94 cmp r0, #0 @ was emulation successful
Russell King6ebbf2c2014-06-30 16:29:12 +010095 reteq r4 @ no, return failure
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97next:
Russell King39dc53d2015-09-07 00:29:15 +010098 uaccess_enable r3
Linus Torvalds1da177e2005-04-16 15:20:36 -070099.Lx1: ldrt r6, [r5], #4 @ get the next instruction and
100 @ increment PC
Russell King39dc53d2015-09-07 00:29:15 +0100101 uaccess_disable r3
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 and r2, r6, #0x0F000000 @ test for FP insns
103 teq r2, #0x0C000000
104 teqne r2, #0x0D000000
105 teqne r2, #0x0E000000
Russell King6ebbf2c2014-06-30 16:29:12 +0100106 retne r9 @ return ok if not a fp insn
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Catalin Marinasc1f438f2007-09-25 15:21:00 +0100108 str r5, [sp, #S_PC] @ update PC copy in regs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110 mov r0, r6 @ save a copy
Catalin Marinasc1f438f2007-09-25 15:21:00 +0100111 b emulate @ check condition and emulate
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113 @ We need to be prepared for the instructions at .Lx1 and .Lx2
114 @ to fault. Emit the appropriate exception gunk to fix things up.
115 @ ??? For some reason, faults can happen at .Lx2 even with a
116 @ plain LDR instruction. Weird, but it seems harmless.
Ard Biesheuvelc4a84ae2015-03-24 10:41:09 +0100117 .pushsection .text.fixup,"ax"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 .align 2
Russell King6ebbf2c2014-06-30 16:29:12 +0100119.Lfix: ret r9 @ let the user eat segfaults
Russell King42604152010-04-19 10:15:03 +0100120 .popsection
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Russell King42604152010-04-19 10:15:03 +0100122 .pushsection __ex_table,"a"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 .align 3
124 .long .Lx1, .Lfix
Russell King42604152010-04-19 10:15:03 +0100125 .popsection