nds32 FPU port
v4 cover nds32 FPU port
This patch set contains basic components for supporting the nds32 FPU,
such as exception handlers and context switch for FPU registers. By default, the lazy FPU scheme is supported and the user can configure it via CONFIG_LZAY_FPU. In addition, a floating point emulator is required to handle all arithmetic of denormalized number because it is not supported by the nds32 FPU.
As mentioned above, the nds32 FPU does not support denormalized number
This means the denormalized operands and results are not permitted. If an instruction contains denormalized operands, the nds32 FPU will raise an denormalized input exception to inform kernel to deal with this instruction. If the result of the instruction is a denormalized number, normally nds32 FPU will treat it as an underflow case and round the result to an appropriate value based on current rounding mode. Obviously, there is a precision gap for tininess number. To reduce this precision gap, kernel will enable the underflow trap by default to direct all underflow cases to the floating pointer emulator. By the floating pointer emulator, the correct denormalized number can be derived in kernel and return to the user program. The feature can be configured by CONFIG_SUPPORT_DENORMAL_ARITHMETIC, and if the precision requirement is not critical for tininess number, user may disables this feature to keep performance.
The implementation of floating point emulator is based on soft-fp
which is located in include/math-emu folder. However, soft-fp is too outdated to pass the current compiler check. The needed modifications for soft-fp are included in this patch set
Changes in v4:
- Fix compilation error
Changes in v3:
- Kernel with FPU support enabled still can run on a CPU without FPU
- Rename CONFIG_UNLAZY_FPU to CONFIG_LAYZ_FPU
- Rename _switch() to _switch_fpu()
- Store FPU context when kernel suspends
- Modify the comments in code and patch
Changes in v2:
- Remove the initilzation for floating pointer register before entering to signal handler.
Vincent Chen (5):
nds32: nds32 FPU port
nds32: Support FP emulation
nds32: support denormalized result through FP emulator math-emu/op-2.h: Use statement expressions to prevent negative
constant shift
math-emu/soft-fp.h: (_FP_ROUND_ZERO) cast 0 to void to fix warning
arch/nds32/Kconfig | 1 + arch/nds32/Kconfig.cpu | 34 +++ arch/nds32/Makefile | 5 + arch/nds32/include/asm/bitfield.h | 15 ++ arch/nds32/include/asm/elf.h | 11 + arch/nds32/include/asm/fpu.h | 126 +++++++++++ arch/nds32/include/asm/fpuemu.h | 32 +++ arch/nds32/include/asm/nds32_fpu_inst.h | 109 +++++++++ arch/nds32/include/asm/processor.h | 7 + arch/nds32/include/asm/sfp-machine.h | 158 +++++++++++++ arch/nds32/include/asm/syscalls.h | 1 + arch/nds32/include/uapi/asm/auxvec.h | 7 + arch/nds32/include/uapi/asm/sigcontext.h | 14 ++ arch/nds32/include/uapi/asm/udftrap.h | 13 + arch/nds32/include/uapi/asm/unistd.h | 2 + arch/nds32/kernel/Makefile | 4 + arch/nds32/kernel/ex-entry.S | 24 ++- arch/nds32/kernel/ex-exit.S | 13 +- arch/nds32/kernel/ex-scall.S | 8 +- arch/nds32/kernel/fpu.c | 269 ++++++++++++++++++++++ arch/nds32/kernel/process.c | 64 +++++- arch/nds32/kernel/setup.c | 12 +- arch/nds32/kernel/signal.c | 62 +++++- arch/nds32/kernel/sleep.S | 4 +- arch/nds32/kernel/sys_nds32.c | 32 +++ arch/nds32/kernel/traps.c | 16 ++ arch/nds32/math-emu/Makefile | 7 + arch/nds32/math-emu/faddd.c | 24 ++ arch/nds32/math-emu/fadds.c | 24 ++ arch/nds32/math-emu/fcmpd.c | 24 ++ arch/nds32/math-emu/fcmps.c | 24 ++ arch/nds32/math-emu/fd2s.c | 22 ++ arch/nds32/math-emu/fdivd.c | 27 +++ arch/nds32/math-emu/fdivs.c | 26 +++ arch/nds32/math-emu/fmuld.c | 23 ++ arch/nds32/math-emu/fmuls.c | 23 ++ arch/nds32/math-emu/fnegd.c | 21 ++ arch/nds32/math-emu/fnegs.c | 21 ++ arch/nds32/math-emu/fpuemu.c | 357 ++++++++++++++++++++++++++++++ arch/nds32/math-emu/fs2d.c | 23 ++ arch/nds32/math-emu/fsqrtd.c | 21 ++ arch/nds32/math-emu/fsqrts.c | 21 ++ arch/nds32/math-emu/fsubd.c | 27 +++ arch/nds32/math-emu/fsubs.c | 27 +++ include/math-emu/op-2.h | 97 ++++----- include/math-emu/soft-fp.h | 2 +- 46 files changed, 1816 insertions(+), 68 deletions(-) create mode 100644 arch/nds32/include/asm/fpu.h create mode 100644 arch/nds32/include/asm/fpuemu.h create mode 100644 arch/nds32/include/asm/nds32_fpu_inst.h create mode 100644 arch/nds32/include/asm/sfp-machine.h create mode 100644 arch/nds32/include/uapi/asm/udftrap.h create mode 100644 arch/nds32/kernel/fpu.c create mode 100644 arch/nds32/math-emu/Makefile create mode 100644 arch/nds32/math-emu/faddd.c create mode 100644 arch/nds32/math-emu/fadds.c create mode 100644 arch/nds32/math-emu/fcmpd.c create mode 100644 arch/nds32/math-emu/fcmps.c create mode 100644 arch/nds32/math-emu/fd2s.c create mode 100644 arch/nds32/math-emu/fdivd.c create mode 100644 arch/nds32/math-emu/fdivs.c create mode 100644 arch/nds32/math-emu/fmuld.c create mode 100644 arch/nds32/math-emu/fmuls.c create mode 100644 arch/nds32/math-emu/fnegd.c create mode 100644 arch/nds32/math-emu/fnegs.c create mode 100644 arch/nds32/math-emu/fpuemu.c create mode 100644 arch/nds32/math-emu/fs2d.c create mode 100644 arch/nds32/math-emu/fsqrtd.c create mode 100644 arch/nds32/math-emu/fsqrts.c create mode 100644 arch/nds32/math-emu/fsubd.c create mode 100644 arch/nds32/math-emu/fsubs.c
PATCH v4 1/5 nds32: nds32 FPU port
This patch set contains basic components for supporting the nds32 FPU, such as exception handlers and context switch for FPU registers. By default, the lazy FPU scheme is supported and the user can configure it via CONFIG_LZAY_FPU.
--- arch/nds32/Kconfig | 1 + arch/nds32/Kconfig.cpu | 21 +++ arch/nds32/Makefile | 4 + arch/nds32/include/asm/bitfield.h | 15 ++ arch/nds32/include/asm/fpu.h | 114 +++++++++++++++ arch/nds32/include/asm/processor.h | 7 + arch/nds32/include/uapi/asm/sigcontext.h | 5 + arch/nds32/kernel/Makefile | 4 + arch/nds32/kernel/ex-entry.S | 24 +++- arch/nds32/kernel/ex-exit.S | 13 ++- arch/nds32/kernel/ex-scall.S | 8 +- arch/nds32/kernel/fpu.c | 231 ++++++++++++++++++++++++++++++ arch/nds32/kernel/process.c | 64 ++++++++- arch/nds32/kernel/setup.c | 12 ++- arch/nds32/kernel/signal.c | 62 ++++++++- arch/nds32/kernel/sleep.S | 4 +- arch/nds32/kernel/traps.c | 16 ++ 17 files changed, 589 insertions(+), 16 deletions(-) create mode 100644 arch/nds32/include/asm/fpu.h create mode 100644 arch/nds32/kernel/fpu.c
arch/nds32/Kconfig +1 -0
@@ -29,6 +29,7 @@ config NDS32 select HANDLE_DOMAIN_IRQ select HAVE_ARCH_TRACEHOOK select HAVE_DEBUG_KMEMLEAK + select HAVE_EXIT_THREAD select HAVE_MEMBLOCK select HAVE_REGS_AND_STACK_ACCESS_API select HAVE_PERF_EVENTS
arch/nds32/Kconfig.cpu +21 -0
@@ -7,6 +7,27 @@ config CPU_LITTLE_ENDIAN bool "Little endian" default y +config FPU + bool "FPU support" + default n + help + If FPU ISA is used in user space, this configuration shall be Y to + enable required support in kerenl such as fpu context switch and + fpu exception handler. + + If no FPU ISA is used in user space, say N. + +config LAZY_FPU + bool "lazy FPU support" + depends on FPU + default y + help + Say Y here to enable the lazy FPU scheme. The lazy FPU scheme can + enhance system performance by reducing the context switch + frequency of the FPU register. + + For nomal case, say Y. + config HWZOL bool "hardware zero overhead loop support" depends on CPU_D10 || CPU_D15
arch/nds32/Makefile +4 -0
@@ -5,10 +5,14 @@ KBUILD_DEFCONFIG := defconfig comma = , + ifdef CONFIG_FUNCTION_TRACER arch-y += -malways-save-lp -mno-relax endif +# Avoid generating FPU instructions +arch-y += -mno-ext-fpu-sp -mno-ext-fpu-dp -mfloat-abi=soft + KBUILD_CFLAGS += $(call cc-option, -mno-sched-prolog-epilog) KBUILD_CFLAGS += -mcmodel=large
arch/nds32/include/asm/bitfield.h +15 -0
@@ -251,6 +251,11 @@#define ITYPE_mskSTYPE ( 0xF << ITYPE_offSTYPE )#define ITYPE_mskCPID ( 0x3 << ITYPE_offCPID ) +/* Additional definitions of ITYPE register for FPU */ +#define FPU_DISABLE_EXCEPTION (0x1 << ITYPE_offSTYPE) +#define FPU_EXCEPTION (0x2 << ITYPE_offSTYPE) +#define FPU_CPID 0/* FPU Co-Processor ID is 0 */ +#define NDS32_VECTOR_mskNONEXCEPTION 0x78#define NDS32_VECTOR_offEXCEPTION 8#define NDS32_VECTOR_offINTERRUPT 9
@@ -926,6 +931,7 @@#define FPCSR_mskDNIT ( 0x1 << FPCSR_offDNIT )#define FPCSR_mskRIT ( 0x1 << FPCSR_offRIT )#define FPCSR_mskALL (FPCSR_mskIVO | FPCSR_mskDBZ | FPCSR_mskOVF | FPCSR_mskUDF | FPCSR_mskIEX) +#define FPCSR_mskALLE_NO_UDFE (FPCSR_mskIVOE | FPCSR_mskDBZE | FPCSR_mskOVFE | FPCSR_mskIEXE)#define FPCSR_mskALLE (FPCSR_mskIVOE | FPCSR_mskDBZE | FPCSR_mskOVFE | FPCSR_mskUDFE | FPCSR_mskIEXE)#define FPCSR_mskALLT (FPCSR_mskIVOT | FPCSR_mskDBZT | FPCSR_mskOVFT | FPCSR_mskUDFT | FPCSR_mskIEXT |FPCSR_mskDNIT | FPCSR_mskRIT)
@@ -946,6 +952,15 @@#define FPCFG_mskIMVER ( 0x1F << FPCFG_offIMVER )#define FPCFG_mskAVER ( 0x1F << FPCFG_offAVER ) +/* 8 Single precision or 4 double precision registers are available */ +#define SP8_DP4_reg 0 +/* 16 Single precision or 8 double precision registers are available */ +#define SP16_DP8_reg 1 +/* 32 Single precision or 16 double precision registers are available */ +#define SP32_DP16_reg 2 +/* 32 Single precision or 32 double precision registers are available */ +#define SP32_DP32_reg 3 +/****************************************************************************** * fucpr: FUCOP_CTL (FPU and Coprocessor Enable Control Register) *****************************************************************************/
arch/nds32/include/asm/fpu.h new file +114 -0
114 lines not on this page · raw message
arch/nds32/include/asm/processor.h +7 -0
@@ -41,6 +41,8 @@ struct thread_struct {unsigned long address;unsigned long trap_no;unsigned long error_code; + +struct fpu_struct fpu; };#define INIT_THREAD { }
@@ -78,6 +80,11 @@ struct thread_struct {/* Free all resources held by a thread. */ #define release_thread (thread) do { } while(0) +#if IS_ENABLED (CONFIG_FPU ) +#if !IS_ENABLED (CONFIG_UNLAZU_FPU ) +extern struct task_struct *last_task_used_math; +#endif +#endif /* Prepare to copy thread state - unlazy all lazy status */ #define prepare_to_copy (tsk) do { } while (0)
arch/nds32/include/uapi/asm/sigcontext.h +5 -0
@@ -9,6 +9,10 @@* before the signal handler was invoked. Note: only add new entries * to the end of the structure. */ +struct fpu_struct { +unsigned long long fd_regs[32 ]; +unsigned long fpcsr; +};struct zol_struct {unsigned long nds32_lc;/* $LC */
@@ -54,6 +58,7 @@ struct sigcontext {unsigned long fault_address;unsigned long used_math_flag;/* FPU Registers */ +struct fpu_struct fpu;struct zol_struct zol; };
arch/nds32/kernel/Makefile +4 -0
@@ -13,12 +13,16 @@ obj-y := ex-entry.o ex-exit.o ex-scall.o irq.o \ obj-$(CONFIG_MODULES) += nds32_ksyms.o module.o obj-$(CONFIG_STACKTRACE) += stacktrace.o +obj-$(CONFIG_FPU) += fpu.o obj-$(CONFIG_OF) += devtree.o obj-$(CONFIG_CACHE_L2) += atl2c.o obj-$(CONFIG_PERF_EVENTS) += perf_event_cpu.o obj-$(CONFIG_PM) += pm.o sleep.o extra-y := head.o vmlinux.lds +CFLAGS_fpu.o += -mext-fpu-sp -mext-fpu-dp + + obj-y += vdso/ obj-$(CONFIG_FUNCTION_TRACER) += ftrace.o
arch/nds32/kernel/ex-entry.S +22 -2
@@ -7,6 +7,7 @@ #include <asm/errno.h> #include <asm/asm-offsets.h> #include <asm/page.h> +#include <asm/fpu.h> #ifdef CONFIG_HWZOL .macro push_zol
@@ -15,12 +16,31 @@ mfusr $r16, $LC .endm #endif + .macro skip_save_fucop_ctl +#if defined(CONFIG_FPU) +skip_fucop_ctl: + smw.adm $p0, [$sp], $p0, #0x1 + j fucop_ctl_done +#endif + .endm .macro save_user_regs - +#if defined(CONFIG_FPU) + sethi $p0, hi20(has_fpu) + lbsi $p0, [$p0+lo12(has_fpu)] + beqz $p0, skip_fucop_ctl + mfsr $p0, $FUCOP_CTL + smw.adm $p0, [$sp], $p0, #0x1 + bclr $p0, $p0, #FUCOP_CTL_offCP0EN + mtsr $p0, $FUCOP_CTL +fucop_ctl_done: + /* move $SP to the bottom of pt_regs */ + addi $sp, $sp, -FUCOP_CTL_OFFSET +#else smw.adm $sp, [$sp], $sp, #0x1 /* move $SP to the bottom of pt_regs */ addi $sp, $sp, -OSP_OFFSET +#endif /* push $r0 ~ $r25 */ smw.bim $r0, [$sp], $r25
@@ -79,6 +99,7 @@ exception_handlers: .long eh_syscall !Syscall .long asm_do_IRQ !IRQ + skip_save_fucop_ctl common_exception_handler: save_user_regs mfsr $p0, $ITYPE
@@ -103,7 +124,6 @@ common_exception_handler: mtsr $r21, $PSW dsb jr $p1 - /* syscall */ 1: addi $p1, $p0, #-NDS32_VECTOR_offEXCEPTION
arch/nds32/kernel/ex-exit.S +11 -2
@@ -8,6 +8,7 @@ #include <asm/asm-offsets.h> #include <asm/thread_info.h> #include <asm/current.h> +#include <asm/fpu.h>
@@ -22,10 +23,18 @@ .macro restore_user_regs_first setgie.d isb - +#if defined(CONFIG_FPU) + addi $sp, $sp, OSP_OFFSET + lmw.adm $r12, [$sp], $r25, #0x0 + sethi $p0, hi20(has_fpu) + lbsi $p0, [$p0+lo12(has_fpu)] + beqz $p0, 2f + mtsr $r25, $FUCOP_CTL +2: +#else addi $sp, $sp, FUCOP_CTL_OFFSET - lmw.adm $r12, [$sp], $r24, #0x0 +#endif mtsr $r12, $SP_USR mtsr $r13, $IPC #ifdef CONFIG_HWZOL
arch/nds32/kernel/ex-scall.S +5 -3
@@ -19,11 +19,13 @@ ENTRY(__switch_to) la $p0, __entry_task sw $r1, [$p0] - move $p1, $r0 - addi $p1, $p1, #THREAD_CPU_CONTEXT + addi $p1, $r0, #THREAD_CPU_CONTEXT smw.bi $r6, [$p1], $r14, #0xb ! push r6~r14, fp, lp, sp move $r25, $r1 - addi $r1, $r1, #THREAD_CPU_CONTEXT +#if defined(CONFIG_FPU) + call _switch_fpu +#endif + addi $r1, $r25, #THREAD_CPU_CONTEXT lmw.bi $r6, [$r1], $r14, #0xb ! pop r6~r14, fp, lp, sp ret
arch/nds32/kernel/fpu.c new file +231 -0
231 lines not on this page · raw message
arch/nds32/kernel/process.c +59 -5
107 lines not on this page · raw message
arch/nds32/kernel/setup.c +10 -2
@@ -16,6 +16,7 @@#include <asm/proc-fns.h> #include <asm/cache_info.h> #include <asm/elf.h> +#include <asm/fpu.h> #include <nds32_intrinsic.h> #define HWCAP_MFUSR_PC 0x000001
@@ -41,6 +42,7 @@#define HWCAP_DX_REGS 0x100000unsigned long cpu_id, cpu_rev, cpu_cfgid; +bool has_fpu = false;char cpu_series;char *endianness =NULL ;
@@ -137,6 +139,11 @@ static void __init dump_cpu_info(int cpu) (aliasing_num - 1) << PAGE_SHIFT; }#endif +#ifdef CONFIG_FPU +/* Disable fpu and enable when it is used. */ +if (has_fpu) +disable_fpu (); +#endif }static void __init setup_cpuinfo (void )
@@ -181,9 +188,10 @@ static void __init setup_cpuinfo(void)if (cpu_cfgid &0x0004 ) elf_hwcap |=HWCAP_EXT2 ; -if (cpu_cfgid &0x0008 ) +if (cpu_cfgid &0x0008 ) { elf_hwcap |=HWCAP_FPU ; - + has_fpu = true; + }if (cpu_cfgid &0x0010 ) elf_hwcap |=HWCAP_STRING ;
arch/nds32/kernel/signal.c +61 -1
86 lines not on this page · raw message
arch/nds32/kernel/sleep.S +3 -1
@@ -36,7 +36,9 @@ suspend2ram: mfsr $r17, $ir14 mfsr $r18, $ir15 pushm $r0, $r19 - +#if defined(CONFIG_FPU) + jal store_fpu_for_suspend +#endif tlbop FlushAll isb
arch/nds32/kernel/traps.c +16 -0
@@ -12,6 +12,7 @@#include <asm/proc-fns.h> #include <asm/unistd.h> +#include <asm/fpu.h> #include <linux/ptrace.h> #include <nds32_intrinsic.h>
@@ -359,6 +360,21 @@ void do_dispatch_general(unsigned long entry, unsigned long addr, }else if (type ==ETYPE_RESERVED_INSTRUCTION ) {/* Reserved instruction */ do_revinsn (regs); + }else if (type ==ETYPE_COPROCESSOR ) { +/* Coprocessor */ +#if IS_ENABLED (CONFIG_FPU ) +unsigned int fucop_exist =__nds32__mfsr (NDS32_SR_FUCOP_EXIST ); +unsigned int cpid = ((itype & ITYPE_mskCPID) >> ITYPE_offCPID); + +if ((cpid ==FPU_CPID ) && + (fucop_exist & FUCOP_EXIST_mskCP0ISFPU)) { +unsigned int subtype = (itype & ITYPE_mskSTYPE); + +if (true ==do_fpu_exception (subtype, regs)) +return ; + } +#endif +unhandled_exceptions (entry, addr, type, regs); }else if (type ==ETYPE_TRAP && swid ==SWID_RAISE_INTERRUPT_LEVEL ) {/* trap, used on v3 EDM target debugging workaround */ /*
Signature · 1 line
1.7.1
PATCH v4 2/5 nds32: Support FP emulation
In reply to Vincent ChenThe Andes FPU coprocessor does not support denormalized number handling. According to the specification, FPU generates a denorm input exception that requires the kernel to deal with this instrution operation when it encounters denormalized operands. Hence an nds32 FPU ISA emulator in the kernel is required to meet requirement.
--- arch/nds32/Makefile | 1 + arch/nds32/include/asm/fpu.h | 1 + arch/nds32/include/asm/fpuemu.h | 32 +++ arch/nds32/include/asm/nds32_fpu_inst.h | 109 ++++++++++ arch/nds32/include/asm/sfp-machine.h | 158 ++++++++++++++ arch/nds32/kernel/fpu.c | 31 +++- arch/nds32/math-emu/Makefile | 7 + arch/nds32/math-emu/faddd.c | 24 ++ arch/nds32/math-emu/fadds.c | 24 ++ arch/nds32/math-emu/fcmpd.c | 24 ++ arch/nds32/math-emu/fcmps.c | 24 ++ arch/nds32/math-emu/fd2s.c | 22 ++ arch/nds32/math-emu/fdivd.c | 27 +++ arch/nds32/math-emu/fdivs.c | 26 +++ arch/nds32/math-emu/fmuld.c | 23 ++ arch/nds32/math-emu/fmuls.c | 23 ++ arch/nds32/math-emu/fnegd.c | 21 ++ arch/nds32/math-emu/fnegs.c | 21 ++ arch/nds32/math-emu/fpuemu.c | 352 +++++++++++++++++++++++++++++++ arch/nds32/math-emu/fs2d.c | 23 ++ arch/nds32/math-emu/fsqrtd.c | 21 ++ arch/nds32/math-emu/fsqrts.c | 21 ++ arch/nds32/math-emu/fsubd.c | 27 +++ arch/nds32/math-emu/fsubs.c | 27 +++ 24 files changed, 1064 insertions(+), 5 deletions(-) create mode 100644 arch/nds32/include/asm/fpuemu.h create mode 100644 arch/nds32/include/asm/nds32_fpu_inst.h create mode 100644 arch/nds32/include/asm/sfp-machine.h create mode 100644 arch/nds32/math-emu/Makefile create mode 100644 arch/nds32/math-emu/faddd.c create mode 100644 arch/nds32/math-emu/fadds.c create mode 100644 arch/nds32/math-emu/fcmpd.c create mode 100644 arch/nds32/math-emu/fcmps.c create mode 100644 arch/nds32/math-emu/fd2s.c create mode 100644 arch/nds32/math-emu/fdivd.c create mode 100644 arch/nds32/math-emu/fdivs.c create mode 100644 arch/nds32/math-emu/fmuld.c create mode 100644 arch/nds32/math-emu/fmuls.c create mode 100644 arch/nds32/math-emu/fnegd.c create mode 100644 arch/nds32/math-emu/fnegs.c create mode 100644 arch/nds32/math-emu/fpuemu.c create mode 100644 arch/nds32/math-emu/fs2d.c create mode 100644 arch/nds32/math-emu/fsqrtd.c create mode 100644 arch/nds32/math-emu/fsqrts.c create mode 100644 arch/nds32/math-emu/fsubd.c create mode 100644 arch/nds32/math-emu/fsubs.c
arch/nds32/Makefile +1 -0
@@ -30,6 +30,7 @@ export TEXTADDR # If we have a machine-specific directory, then include it in the build. core-y += arch/nds32/kernel/ arch/nds32/mm/ +core-$(CONFIG_FPU) += arch/nds32/math-emu/ libs-y += arch/nds32/lib/ ifneq '$(CONFIG_NDS32_BUILTIN_DTB)' '""'
arch/nds32/include/asm/fpu.h +1 -0
@@ -15,6 +15,7 @@extern void save_fpu (struct task_struct *__tsk);extern void load_fpu (const struct fpu_struct *fpregs);extern bool do_fpu_exception (unsigned int subtype,struct pt_regs *regs); +extern int do_fpuemu (struct pt_regs *regs,struct fpu_struct *fpu);#define test_tsk_fpu (regs) (regs->fucop_ctl & FUCOP_CTL_mskCP0EN)
arch/nds32/include/asm/fpuemu.h new file +32 -0
@@ -0,0 +1,32 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (C) 2005-2018 Andes Technology Corporation */ + +#ifndef __ARCH_NDS32_FPUEMU_H +#define __ARCH_NDS32_FPUEMU_H + +/* +* single precision +*/ + +void fadds (void *ft,void *fa,void *fb); +void fsubs (void *ft,void *fa,void *fb); +void fmuls (void *ft,void *fa,void *fb); +void fdivs (void *ft,void *fa,void *fb); +void fs2d (void *ft,void *fa); +void fsqrts (void *ft,void *fa); +void fnegs (void *ft,void *fa); +int fcmps (void *ft,void *fa,void *fb,int cop); + +/* +* double precision +*/ +void faddd (void *ft,void *fa,void *fb); +void fsubd (void *ft,void *fa,void *fb); +void fmuld (void *ft,void *fa,void *fb); +void fdivd (void *ft,void *fa,void *fb); +void fsqrtd (void *ft,void *fa); +void fd2s (void *ft,void *fa); +void fnegd (void *ft,void *fa); +int fcmpd (void *ft,void *fa,void *fb,int cop); + +#endif /* __ARCH_NDS32_FPUEMU_H */
arch/nds32/include/asm/nds32_fpu_inst.h new file +109 -0
109 lines not on this page · raw message
arch/nds32/include/asm/sfp-machine.h new file +158 -0
158 lines not on this page · raw message
arch/nds32/kernel/fpu.c +26 -5
@@ -183,10 +183,10 @@ inline void fill_sigfpe_signo(unsigned int fpcsr, int *signo) {if (fpcsr & FPCSR_mskOVFT) *signo =FPE_FLTOVF ; -else if (fpcsr & FPCSR_mskUDFT) - *signo =FPE_FLTUND ;else if (fpcsr & FPCSR_mskIVOT) *signo =FPE_FLTINV ; +else if (fpcsr & FPCSR_mskUDFT) + *signo =FPE_FLTUND ;else if (fpcsr & FPCSR_mskDBZT) *signo =FPE_FLTDIV ;else if (fpcsr & FPCSR_mskIEXT)
@@ -201,16 +201,37 @@ inline void handle_fpu_exception(struct pt_regs *regs)lose_fpu (); fpcsr = current->thread.fpu.fpcsr; -if (fpcsr & FPCSR_mskRIT) { +if (fpcsr & FPCSR_mskDNIT) { + si_signo =do_fpuemu (regs, ¤t->thread.fpu); + fpcsr = current->thread.fpu.fpcsr; +if (!si_signo) + goto done; + }else if (fpcsr & FPCSR_mskRIT) {if (!user_mode (regs))do_exit (SIGILL ); si_signo =SIGILL ; + } + + +switch (si_signo) { +case SIGFPE : +fill_sigfpe_signo (fpcsr, &si_code); +break ; +case SIGILL :show_regs (regs); si_code =ILL_COPROC ; - }else -fill_sigfpe_signo (fpcsr, &si_code); +break ; +case SIGBUS : + si_code =BUS_ADRERR ; +break ; +default : +break ; + } +force_sig_fault (si_signo, si_code, (void __user *)instruction_pointer (regs), current); +done: +own_fpu (); }bool do_fpu_exception (unsigned int subtype,struct pt_regs *regs)
arch/nds32/math-emu/Makefile new file +7 -0
@@ -0,0 +1,7 @@ +# +# Makefile for the Linux/nds32 kernel FPU emulation. +# + +obj-y := fpuemu.o \ + fdivd.o fmuld.o fsubd.o faddd.o fs2d.o fsqrtd.o fcmpd.o fnegs.o \ + fdivs.o fmuls.o fsubs.o fadds.o fd2s.o fsqrts.o fcmps.o fnegd.o
arch/nds32/math-emu/faddd.c new file +24 -0
@@ -0,0 +1,24 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2005-2018 Andes Technology Corporation +#include <linux/uaccess.h> + +#include <asm/sfp-machine.h> +#include <math-emu/soft-fp.h> +#include <math-emu/double.h> +void faddd (void *ft,void *fa,void *fb) +{ +FP_DECL_D (A ); +FP_DECL_D (B ); +FP_DECL_D (R ); +FP_DECL_EX ; + +FP_UNPACK_DP (A , fa); +FP_UNPACK_DP (B , fb); + +FP_ADD_D (R ,A ,B ); + +FP_PACK_DP (ft,R ); + + __FPU_FPCSR |=FP_CUR_EXCEPTIONS ; + +}
arch/nds32/math-emu/fadds.c new file +24 -0
@@ -0,0 +1,24 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2005-2018 Andes Technology Corporation +#include <linux/uaccess.h> + +#include <asm/sfp-machine.h> +#include <math-emu/soft-fp.h> +#include <math-emu/single.h> +void fadds (void *ft,void *fa,void *fb) +{ +FP_DECL_S (A ); +FP_DECL_S (B ); +FP_DECL_S (R ); +FP_DECL_EX ; + +FP_UNPACK_SP (A , fa); +FP_UNPACK_SP (B , fb); + +FP_ADD_S (R ,A ,B ); + +FP_PACK_SP (ft,R ); + + __FPU_FPCSR |=FP_CUR_EXCEPTIONS ; + +}
arch/nds32/math-emu/fcmpd.c new file +24 -0
@@ -0,0 +1,24 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2005-2018 Andes Technology Corporation +#include <asm/sfp-machine.h> +#include <math-emu/soft-fp.h> +#include <math-emu/double.h> +int fcmpd (void *ft,void *fa,void *fb,int cmpop) +{ +FP_DECL_D (A ); +FP_DECL_D (B ); +FP_DECL_EX ; +long cmp; + +FP_UNPACK_DP (A , fa); +FP_UNPACK_DP (B , fb); + +FP_CMP_D (cmp,A ,B ,SF_CUN ); + cmp +=2 ; +if (cmp ==SF_CGT ) + *(long *)ft =0 ; +else + *(long *)ft = (cmp & cmpop) ?1 :0 ; + +return 0 ; +}
arch/nds32/math-emu/fcmps.c new file +24 -0
@@ -0,0 +1,24 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2005-2018 Andes Technology Corporation +#include <asm/sfp-machine.h> +#include <math-emu/soft-fp.h> +#include <math-emu/single.h> +int fcmps (void *ft,void *fa,void *fb,int cmpop) +{ +FP_DECL_S (A ); +FP_DECL_S (B ); +FP_DECL_EX ; +long cmp; + +FP_UNPACK_SP (A , fa); +FP_UNPACK_SP (B , fb); + +FP_CMP_S (cmp,A ,B ,SF_CUN ); + cmp +=2 ; +if (cmp ==SF_CGT ) + *(int *)ft =0x0 ; +else + *(int *)ft = (cmp & cmpop) ?0x1 :0x0 ; + +return 0 ; +}
arch/nds32/math-emu/fd2s.c new file +22 -0
@@ -0,0 +1,22 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2005-2018 Andes Technology Corporation +#include <linux/uaccess.h> + +#include <asm/sfp-machine.h> +#include <math-emu/double.h> +#include <math-emu/single.h> +#include <math-emu/soft-fp.h> +void fd2s (void *ft,void *fa) +{ +FP_DECL_D (A ); +FP_DECL_S (R ); +FP_DECL_EX ; + +FP_UNPACK_DP (A , fa); + +FP_CONV (S ,D ,1 ,2 ,R ,A ); + +FP_PACK_SP (ft,R ); + + __FPU_FPCSR |=FP_CUR_EXCEPTIONS ; +}
arch/nds32/math-emu/fdivd.c new file +27 -0
@@ -0,0 +1,27 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2005-2018 Andes Technology Corporation + +#include <linux/uaccess.h> +#include <asm/sfp-machine.h> +#include <math-emu/soft-fp.h> +#include <math-emu/double.h> + +void fdivd (void *ft,void *fa,void *fb) +{ +FP_DECL_D (A ); +FP_DECL_D (B ); +FP_DECL_D (R ); +FP_DECL_EX ; + +FP_UNPACK_DP (A , fa); +FP_UNPACK_DP (B , fb); + +if (B_c ==FP_CLS_ZERO && A_c !=FP_CLS_ZERO ) +FP_SET_EXCEPTION (FP_EX_DIVZERO ); + +FP_DIV_D (R ,A ,B ); + +FP_PACK_DP (ft,R ); + + __FPU_FPCSR |=FP_CUR_EXCEPTIONS ; +}
arch/nds32/math-emu/fdivs.c new file +26 -0
@@ -0,0 +1,26 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2005-2018 Andes Technology Corporation +#include <linux/uaccess.h> + +#include <asm/sfp-machine.h> +#include <math-emu/soft-fp.h> +#include <math-emu/single.h> +void fdivs (void *ft,void *fa,void *fb) +{ +FP_DECL_S (A ); +FP_DECL_S (B ); +FP_DECL_S (R ); +FP_DECL_EX ; + +FP_UNPACK_SP (A , fa); +FP_UNPACK_SP (B , fb); + +if (B_c ==FP_CLS_ZERO && A_c !=FP_CLS_ZERO ) +FP_SET_EXCEPTION (FP_EX_DIVZERO ); + +FP_DIV_S (R ,A ,B ); + +FP_PACK_SP (ft,R ); + + __FPU_FPCSR |=FP_CUR_EXCEPTIONS ; +}
arch/nds32/math-emu/fmuld.c new file +23 -0
@@ -0,0 +1,23 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2005-2018 Andes Technology Corporation +#include <linux/uaccess.h> + +#include <asm/sfp-machine.h> +#include <math-emu/soft-fp.h> +#include <math-emu/double.h> +void fmuld (void *ft,void *fa,void *fb) +{ +FP_DECL_D (A ); +FP_DECL_D (B ); +FP_DECL_D (R ); +FP_DECL_EX ; + +FP_UNPACK_DP (A , fa); +FP_UNPACK_DP (B , fb); + +FP_MUL_D (R ,A ,B ); + +FP_PACK_DP (ft,R ); + + __FPU_FPCSR |=FP_CUR_EXCEPTIONS ; +}
arch/nds32/math-emu/fmuls.c new file +23 -0
@@ -0,0 +1,23 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2005-2018 Andes Technology Corporation +#include <linux/uaccess.h> + +#include <asm/sfp-machine.h> +#include <math-emu/soft-fp.h> +#include <math-emu/single.h> +void fmuls (void *ft,void *fa,void *fb) +{ +FP_DECL_S (A ); +FP_DECL_S (B ); +FP_DECL_S (R ); +FP_DECL_EX ; + +FP_UNPACK_SP (A , fa); +FP_UNPACK_SP (B , fb); + +FP_MUL_S (R ,A ,B ); + +FP_PACK_SP (ft,R ); + + __FPU_FPCSR |=FP_CUR_EXCEPTIONS ; +}
arch/nds32/math-emu/fnegd.c new file +21 -0
@@ -0,0 +1,21 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2005-2018 Andes Technology Corporation +#include <linux/uaccess.h> + +#include <asm/sfp-machine.h> +#include <math-emu/soft-fp.h> +#include <math-emu/double.h> +void fnegd (void *ft,void *fa) +{ +FP_DECL_D (A ); +FP_DECL_D (R ); +FP_DECL_EX ; + +FP_UNPACK_DP (A , fa); + +FP_NEG_D (R ,A ); + +FP_PACK_DP (ft,R ); + + __FPU_FPCSR |=FP_CUR_EXCEPTIONS ; +}
arch/nds32/math-emu/fnegs.c new file +21 -0
@@ -0,0 +1,21 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2005-2018 Andes Technology Corporation +#include <linux/uaccess.h> + +#include <asm/sfp-machine.h> +#include <math-emu/soft-fp.h> +#include <math-emu/single.h> +void fnegs (void *ft,void *fa) +{ +FP_DECL_S (A ); +FP_DECL_S (R ); +FP_DECL_EX ; + +FP_UNPACK_SP (A , fa); + +FP_NEG_S (R ,A ); + +FP_PACK_SP (ft,R ); + + __FPU_FPCSR |=FP_CUR_EXCEPTIONS ; +}
arch/nds32/math-emu/fpuemu.c new file +352 -0
352 lines not on this page · raw message
arch/nds32/math-emu/fs2d.c new file +23 -0
@@ -0,0 +1,23 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2005-2018 Andes Technology Corporation + +#include <linux/uaccess.h> +#include <asm/sfp-machine.h> +#include <math-emu/double.h> +#include <math-emu/single.h> +#include <math-emu/soft-fp.h> + +void fs2d (void *ft,void *fa) +{ +FP_DECL_S (A ); +FP_DECL_D (R ); +FP_DECL_EX ; + +FP_UNPACK_SP (A , fa); + +FP_CONV (D ,S ,2 ,1 ,R ,A ); + +FP_PACK_DP (ft,R ); + + __FPU_FPCSR |=FP_CUR_EXCEPTIONS ; +}
arch/nds32/math-emu/fsqrtd.c new file +21 -0
@@ -0,0 +1,21 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2005-2018 Andes Technology Corporation + +#include <linux/uaccess.h> +#include <asm/sfp-machine.h> +#include <math-emu/soft-fp.h> +#include <math-emu/double.h> +void fsqrtd (void *ft,void *fa) +{ +FP_DECL_D (A ); +FP_DECL_D (R ); +FP_DECL_EX ; + +FP_UNPACK_DP (A , fa); + +FP_SQRT_D (R ,A ); + +FP_PACK_DP (ft,R ); + + __FPU_FPCSR |=FP_CUR_EXCEPTIONS ; +}
arch/nds32/math-emu/fsqrts.c new file +21 -0
@@ -0,0 +1,21 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2005-2018 Andes Technology Corporation + +#include <linux/uaccess.h> +#include <asm/sfp-machine.h> +#include <math-emu/soft-fp.h> +#include <math-emu/single.h> +void fsqrts (void *ft,void *fa) +{ +FP_DECL_S (A ); +FP_DECL_S (R ); +FP_DECL_EX ; + +FP_UNPACK_SP (A , fa); + +FP_SQRT_S (R ,A ); + +FP_PACK_SP (ft,R ); + + __FPU_FPCSR |=FP_CUR_EXCEPTIONS ; +}
arch/nds32/math-emu/fsubd.c new file +27 -0
@@ -0,0 +1,27 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2005-2018 Andes Technology Corporation +#include <linux/uaccess.h> + +#include <asm/sfp-machine.h> +#include <math-emu/soft-fp.h> +#include <math-emu/double.h> +void fsubd (void *ft,void *fa,void *fb) +{ + +FP_DECL_D (A ); +FP_DECL_D (B ); +FP_DECL_D (R ); +FP_DECL_EX ; + +FP_UNPACK_DP (A , fa); +FP_UNPACK_DP (B , fb); + +if (B_c !=FP_CLS_NAN ) + B_s ^=1 ; + +FP_ADD_D (R ,A ,B ); + +FP_PACK_DP (ft,R ); + + __FPU_FPCSR |=FP_CUR_EXCEPTIONS ; +}
arch/nds32/math-emu/fsubs.c new file +27 -0
@@ -0,0 +1,27 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2005-2018 Andes Technology Corporation +#include <linux/uaccess.h> + +#include <asm/sfp-machine.h> +#include <math-emu/soft-fp.h> +#include <math-emu/single.h> +void fsubs (void *ft,void *fa,void *fb) +{ + +FP_DECL_S (A ); +FP_DECL_S (B ); +FP_DECL_S (R ); +FP_DECL_EX ; + +FP_UNPACK_SP (A , fa); +FP_UNPACK_SP (B , fb); + +if (B_c !=FP_CLS_NAN ) + B_s ^=1 ; + +FP_ADD_S (R ,A ,B ); + +FP_PACK_SP (ft,R ); + + __FPU_FPCSR |=FP_CUR_EXCEPTIONS ; +}
Signature · 1 line
1.7.1
RE: [PATCH v4 2/5] nds32: Support FP emulation
From: Vincent Chen
Quoted 7 lines
Sent: 22 November 2018 03:15
The Andes FPU coprocessor does not support denormalized number handling. According to the specification, FPU generates a denorm input exception that requires the kernel to deal with this instrution operation when it encounters denormalized operands. Hence an nds32 FPU ISA emulator in the kernel is required to meet requirement.
What does the FPU generate for results near zero?
If it doesn't generate denormalised results (but does detect them) then I assume it never generates FP values with exponent zero. (So the gap around zero is even larger than it would be if exponent zero values had a hidden bit.)
So is it really worth doing anything other than treating denormalised values as zero?
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
Re: [PATCH v4 2/5] nds32: Support FP emulation
Quoted 10 lines · David Laight
From: Vincent Chen
Quoted 7 lines
Sent: 22 November 2018 03:15
The Andes FPU coprocessor does not support denormalized number handling. According to the specification, FPU generates a denorm input exception that requires the kernel to deal with this instrution operation when it encounters denormalized operands. Hence an nds32 FPU ISA emulator in the kernel is required to meet requirement.
What does the FPU generate for results near zero?
1. The calculation result is a denormalized number
Depending on the state of underflow trap, the FPU will raise an underflow exception or flash the result to zero.
2. One of the operands is a denormalized number
Depending on the state of the flash-to-zero mode, the FPU will raise a denormalized input exception, which is a specific exception of the nds FPU, or directly treats the operand as 0.
Quoted 9 lines
If it doesn't generate denormalised results (but does detect them) then I assume it never generates FP values with exponent zero. (So the gap around zero is even larger than it would be if exponent zero values had a hidden bit.)
So is it really worth doing anything other than treating denormalised values as zero?David
Sorry, I can't fully understand what you mean, but we really have encountered the above two cases in some testsuites such as glibc testsuite. Hence we implement a FPU emulator in this commit which will recalculate the instructions again by software to enhance the precision.
Quoted 4 lines
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
RE: [PATCH v4 2/5] nds32: Support FP emulation
From: Vincent Chen
Quoted 13 lines
Sent: 26 November 2018 01:23
Quoted 10 lines · David Laight
From: Vincent Chen
Quoted 7 lines
Sent: 22 November 2018 03:15
The Andes FPU coprocessor does not support denormalized number handling. According to the specification, FPU generates a denorm input exception that requires the kernel to deal with this instrution operation when it encounters denormalized operands. Hence an nds32 FPU ISA emulator in the kernel is required to meet requirement.
What does the FPU generate for results near zero?
Quoted 8 lines
1. The calculation result is a denormalized number
Depending on the state of underflow trap, the FPU will raise an underflow exception or flash the result to zero.2. One of the operands is a denormalized number
Depending on the state of the flash-to-zero mode, the FPU will raise a denormalized input exception, which is a specific exception of the nds FPU, or directly treats the operand as 0.
You didn't mention (1) in the commit message.
It ought to be possibly for the exception routine to rescale the values, re-issue the same FP instruction, then scale the result.
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
Re: [PATCH v4 2/5] nds32: Support FP emulation
Quoted 18 lines · David Laight
From: Vincent Chen
Quoted 17 lines
Sent: 26 November 2018 01:23
Quoted 10 lines · David Laight
From: Vincent Chen
Quoted 7 lines
Sent: 22 November 2018 03:15
The Andes FPU coprocessor does not support denormalized number handling. According to the specification, FPU generates a denorm input exception that requires the kernel to deal with this instrution operation when it encounters denormalized operands. Hence an nds32 FPU ISA emulator in the kernel is required to meet requirement.
What does the FPU generate for results near zero?
1. The calculation result is a denormalized number
Depending on the state of underflow trap, the FPU will raise an underflow exception or flash the result to zero.
Quoted 7 lines
Quoted 4 lines
2. One of the operands is a denormalized number
Depending on the state of the flash-to-zero mode, the FPU will raise a denormalized input exception, which is a specific exception of the nds FPU, or directly treats the operand as 0.You didn't mention (1) in the commit message.
I use two commits to complete the implementation of the FPU emulator in this patch set. The handling for (1) case is in another patch, "nds32:
support denormalized result through FP emulator". Sorry, the above description may be misleading.
Quoted 5 lines
It ought to be possibly for the exception routine to rescale the values, re-issue the same FP instruction, then scale the result.
David
I Agree. The scheme is common when dealing with denormalized numbers, including the softfp module of the kernel used in this patchset.
Quoted 4 lines
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
PATCH v4 3/5 nds32: support denormalized result through FP emulator
In reply to Vincent ChenCurrently, the nds32 FPU dose not support the arithmetic of denormalized number. When the nds32 FPU finds the result of the instruction is a denormlized number, the nds32 FPU considers it to be an underflow condition and rounds the result to an appropriate number. It may causes some loss of precision. This commit proposes a solution to re-execute the instruction by the FPU emulator to enhance the precision. To transfer calculations from user space to kernel space, this feature will enable the underflow exception trap by default. Enabling this feature may cause some side effects:
1. Performance loss due to extra FPU exception
2. Need another scheme to control real underflow trap
A new parameter, UDF_trap, which is belong to FPU context is used
to control underflow trap.
User can configure this feature via CONFIG_SUPPORT_DENORMAL_ARITHMETIC
--- arch/nds32/Kconfig.cpu | 13 ++++++++++++ arch/nds32/include/asm/elf.h | 11 ++++++++++ arch/nds32/include/asm/fpu.h | 11 ++++++++++ arch/nds32/include/asm/syscalls.h | 1 + arch/nds32/include/uapi/asm/auxvec.h | 7 ++++++ arch/nds32/include/uapi/asm/sigcontext.h | 9 ++++++++ arch/nds32/include/uapi/asm/udftrap.h | 13 ++++++++++++ arch/nds32/include/uapi/asm/unistd.h | 2 + arch/nds32/kernel/fpu.c | 25 +++++++++++++++++++--- arch/nds32/kernel/sys_nds32.c | 32 ++++++++++++++++++++++++++++++ arch/nds32/math-emu/fpuemu.c | 5 ++++ 11 files changed, 125 insertions(+), 4 deletions(-) create mode 100644 arch/nds32/include/uapi/asm/udftrap.h
arch/nds32/Kconfig.cpu +13 -0
@@ -28,6 +28,19 @@ config LAZY_FPU For nomal case, say Y. +config SUPPORT_DENORMAL_ARITHMETIC + bool "Denormal arithmetic support" + depends on FPU + default n + help + Say Y here to enable arithmetic of denormalized number. Enabling + this feature can enhance the precision for tininess number. + However, performance loss in float pointe calculations is + possibly significant due to additional FPU exception. + + If the calculated tolerance for tininess number is not critical, + say N to prevent performance loss. + config HWZOL bool "hardware zero overhead loop support" depends on CPU_D10 || CPU_D15
arch/nds32/include/asm/elf.h +11 -0
@@ -9,6 +9,7 @@*/ #include <asm/ptrace.h> +#include <asm/fpu.h> typedef unsigned long elf_greg_t ;typedef unsigned long elf_freg_t [3 ];
@@ -159,8 +160,18 @@ struct user_fp {#endif + +#if IS_ENABLED (CONFIG_FPU ) +#define FPU_AUX_ENT NEW_AUX_ENT(AT_FPUCW, FPCSR_INIT) +#else +#define FPU_AUX_ENT NEW_AUX_ENT(AT_IGNORE, 0) +#endif +#define ARCH_DLINFO \ do { \ +/* Optional FPU initialization */ \ +FPU_AUX_ENT ; \ + \NEW_AUX_ENT (AT_SYSINFO_EHDR , \ (elf_addr_t )current->mm->context.vdso); \ }while (0 )
arch/nds32/include/asm/fpu.h +11 -0
@@ -28,7 +28,18 @@#define sNAN64 0xFFFFFFFFFFFFFFFFULL#define sNAN32 0xFFFFFFFFUL +#if IS_ENABLED (CONFIG_SUPPORT_DENORMAL_ARITHMETIC ) +/* +* Denormalized number is unsupported by nds32 FPU. Hence the operation +* is treated as underflow cases when the final result is a denormalized +* number. To enhance precision, underflow exception trap should be +* enabled by default and kerenl will re-execute it by fpu emulator +* when getting underflow exception. +*/ +#define FPCSR_INIT FPCSR_mskUDFE +#else #define FPCSR_INIT 0x0UL +#endif extern const struct fpu_struct init_fpuregs;
arch/nds32/include/asm/syscalls.h +1 -0
@@ -7,6 +7,7 @@asmlinkage long sys_cacheflush (unsigned long addr,unsigned long len,unsigned int op);asmlinkage long sys_fadvise64_64_wrapper (int fd,int advice,loff_t offset,loff_t len);asmlinkage long sys_rt_sigreturn_wrapper (void ); +asmlinkage long sys_udftrap (int option);#include <asm-generic/syscalls.h>
arch/nds32/include/uapi/asm/auxvec.h +7 -0
@@ -4,6 +4,13 @@#ifndef __ASM_AUXVEC_H#define __ASM_AUXVEC_H +/* +* This entry gives some information about the FPU initialization +* performed by the kernel. +*/ +#define AT_FPUCW 18/* Used FPU control word. */ + +/* VDSO location */ #define AT_SYSINFO_EHDR 33
arch/nds32/include/uapi/asm/sigcontext.h +9 -0
@@ -12,6 +12,15 @@struct fpu_struct {unsigned long long fd_regs[32 ];unsigned long fpcsr; +/* +* UDF_trap is used to recognize whether underflow trap is enabled +* or not. When UDF_trap == 1, this process will be traped and then +* get a SIGFPE signal when encountering an underflow exception. +* UDF_trap is only modified through setfputrap syscall. Therefore, +* UDF_trap needn't be saved or loaded to context in each context +* switch. +*/ +unsigned long UDF_trap; };struct zol_struct {
arch/nds32/include/uapi/asm/udftrap.h new file +13 -0
@@ -0,0 +1,13 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (C) 2005-2018 Andes Technology Corporation */ +#ifndef _ASM_SETFPUTRAP +#define _ASM_SETFPUTRAP + +/* +* Options for setfputrap system call +*/ +#define DISABLE_UDFTRAP 0/* disable underflow exception trap */ +#define ENABLE_UDFTRAP 1/* enable undeflos exception trap */ +#define GET_UDFTRAP 2/* only get undeflos exception trap status */ + +#endif /* _ASM_CACHECTL */
arch/nds32/include/uapi/asm/unistd.h +2 -0
@@ -8,4 +8,6 @@/* Additional NDS32 specific syscalls. */ #define __NR_cacheflush (__NR_arch_specific_syscall) +#define __NR_udftrap (__NR_arch_specific_syscall + 1) __SYSCALL(__NR_cacheflush, sys_cacheflush) +__SYSCALL (__NR_udftrap, sys_udftrap)
arch/nds32/kernel/fpu.c +21 -4
@@ -12,7 +12,10 @@const struct fpu_struct init_fpuregs = { .fd_regs = {[0 ...31 ] = sNAN64}, - .fpcsr =FPCSR_INIT + .fpcsr =FPCSR_INIT , +#if IS_ENABLED(CONFIG_SUPPORT_DENORMAL_ARITHMETIC) + .UDF_trap =0 +#endif };void save_fpu (struct task_struct *tsk)
@@ -174,6 +177,9 @@ inline void do_fpu_context_switch(struct pt_regs *regs) }else {/* First time FPU user. */ load_fpu (&init_fpuregs); +#if IS_ENABLED (CONFIG_SUPPORT_DENORMAL_ARITHMETIC ) + current->thread.fpu.UDF_trap = init_fpuregs.UDF_trap; +#endif set_used_math (); }
@@ -183,10 +189,12 @@ inline void fill_sigfpe_signo(unsigned int fpcsr, int *signo) {if (fpcsr & FPCSR_mskOVFT) *signo =FPE_FLTOVF ; -else if (fpcsr & FPCSR_mskIVOT) - *signo =FPE_FLTINV ; +#ifndef CONFIG_SUPPORT_DENORMAL_ARITHMETIC else if (fpcsr & FPCSR_mskUDFT) *signo = FPE_FLTUND; +#endif + else if (fpcsr & FPCSR_mskIVOT) + *signo = FPE_FLTINV; else if (fpcsr & FPCSR_mskDBZT) *signo = FPE_FLTDIV; else if (fpcsr & FPCSR_mskIEXT)
@@ -197,11 +205,20 @@ inline void handle_fpu_exception(struct pt_regs *regs) {unsigned int fpcsr;int si_code =0 , si_signo =SIGFPE ; +#if IS_ENABLED (CONFIG_SUPPORT_DENORMAL_ARITHMETIC ) +unsigned long redo_except = FPCSR_mskDNIT|FPCSR_mskUDFT; +#else +unsigned long redo_except = FPCSR_mskDNIT; +#endif lose_fpu (); fpcsr = current->thread.fpu.fpcsr; -if (fpcsr & FPCSR_mskDNIT) { +if (fpcsr & redo_except) { +#if IS_ENABLED (CONFIG_SUPPORT_DENORMAL_ARITHMETIC ) +if (fpcsr & FPCSR_mskUDFT) + current->thread.fpu.fpcsr &= ~FPCSR_mskIEX; +#endif si_signo =do_fpuemu (regs, ¤t->thread.fpu); fpcsr = current->thread.fpu.fpcsr;if (!si_signo)
arch/nds32/kernel/sys_nds32.c +32 -0
@@ -6,6 +6,8 @@#include <asm/cachectl.h> #include <asm/proc-fns.h> +#include <asm/udftrap.h> +#include <asm/fpu.h> SYSCALL_DEFINE6(mmap2, unsigned long, addr, unsigned long, len, unsigned long, prot, unsigned long, flags,
@@ -48,3 +50,33 @@return 0 ; } + +SYSCALL_DEFINE1(udftrap, int, option) +{ +#if IS_ENABLED (CONFIG_SUPPORT_DENORMAL_ARITHMETIC ) +int old_udftrap; + +if (!used_math ()) { +load_fpu (&init_fpuregs); + current->thread.fpu.UDF_trap = init_fpuregs.UDF_trap; +set_used_math (); + } + + old_udftrap = current->thread.fpu.UDF_trap; +switch (option) { +case DISABLE_UDFTRAP : + current->thread.fpu.UDF_trap =0 ; +break ; +case ENABLE_UDFTRAP : + current->thread.fpu.UDF_trap = FPCSR_mskUDFE; +break ; +case GET_UDFTRAP : +break ; +default : +return -EINVAL ; + } +return old_udftrap; +#else +return -ENOTSUPP ; +#endif +}
arch/nds32/math-emu/fpuemu.c +5 -0
@@ -304,7 +304,12 @@ static int fpu_emu(struct fpu_struct *fpu_reg, unsigned long insn)/* * If an exception is required, generate a tidy SIGFPE exception. */ +#if IS_ENABLED (CONFIG_SUPPORT_DENORMAL_ARITHMETIC ) +if (((fpu_reg->fpcsr <<5 ) & fpu_reg->fpcsr & FPCSR_mskALLE_NO_UDFE) || + ((fpu_reg->fpcsr & FPCSR_mskUDF) && (fpu_reg->UDF_trap))) +#else if ((fpu_reg->fpcsr << 5) & fpu_reg->fpcsr & FPCSR_mskALLE) +#endif return SIGFPE ;return 0 ; }
Signature · 1 line
1.7.1
PATCH v4 4/5 math-emu/op-2.h: Use statement expressions to prevent negative constant shift
In reply to Vincent ChenThis modification is quoted from glibc 'commit < sysdeps/unix/sysv/linux/sparc/sparc64/dl-procinfo.c: Moved to> (fe0b1e854ad32a69b260)'
--- include/math-emu/op-2.h | 97 ++++++++++++++++++++++------------------------ 1 files changed, 46 insertions(+), 51 deletions(-)
include/math-emu/op-2.h +47 -52
@@ -31,61 +31,56 @@#define _FP_FRAC_HIGH_2 (X ) (X##_f1)#define _FP_FRAC_LOW_2 (X ) (X##_f0)#define _FP_FRAC_WORD_2 (X ,w) (X##_f##w) +#define _FP_FRAC_SLL_2 (X ,N ) ( \ + (void) (((N) < _FP_W_TYPE_SIZE) \ + ? ({ \ + if (__builtin_constant_p(N) && (N) == 1) { \ + X##_f1 = X##_f1 + X##_f1 + \ + (((_FP_WS_TYPE) (X##_f0)) < 0); \ + X##_f0 += X##_f0; \ + } else { \ + X##_f1 = X##_f1 << (N) | X##_f0 >> \ + (_FP_W_TYPE_SIZE - (N)); \ + X##_f0 <<= (N); \ + } \ + 0; \ + }) \ + : ({ \ + X##_f1 = X##_f0 << ((N) - _FP_W_TYPE_SIZE); \ + X##_f0 = 0; \ + }))) + + +#define _FP_FRAC_SRL_2 (X ,N ) ( \ + (void) (((N) < _FP_W_TYPE_SIZE) \ + ? ({ \ + X##_f0 = X##_f0 >> (N) | X##_f1 << (_FP_W_TYPE_SIZE - (N)); \ + X##_f1 >>= (N); \ + }) \ + : ({ \ + X##_f0 = X##_f1 >> ((N) - _FP_W_TYPE_SIZE); \ + X##_f1 = 0; \ + }))) -#define _FP_FRAC_SLL_2 (X ,N ) \ - do { \ - if ((N) < _FP_W_TYPE_SIZE) \ - { \ - if (__builtin_constant_p(N) && (N) == 1) \ - { \ - X##_f1 = X##_f1 + X##_f1 + (((_FP_WS_TYPE)(X##_f0)) < 0); \ - X##_f0 += X##_f0; \ - } \ - else \ - { \ - X##_f1 = X##_f1 << (N) | X##_f0 >> (_FP_W_TYPE_SIZE - (N)); \ - X##_f0 <<= (N); \ - } \ - } \ - else \ - { \ - X##_f1 = X##_f0 << ((N) - _FP_W_TYPE_SIZE); \ - X##_f0 = 0; \ - } \ - } while (0) - -#define _FP_FRAC_SRL_2 (X ,N ) \ - do { \ - if ((N) < _FP_W_TYPE_SIZE) \ - { \ - X##_f0 = X##_f0 >> (N) | X##_f1 << (_FP_W_TYPE_SIZE - (N)); \ - X##_f1 >>= (N); \ - } \ - else \ - { \ - X##_f0 = X##_f1 >> ((N) - _FP_W_TYPE_SIZE); \ - X##_f1 = 0; \ - } \ - } while (0)/* Right shift with sticky-lsb. */ -#define _FP_FRAC_SRS_2 (X ,N ,sz) \ - do { \ - if ((N) < _FP_W_TYPE_SIZE) \ - { \ - X##_f0 = (X##_f1 << (_FP_W_TYPE_SIZE - (N)) | X##_f0 >> (N) | \ - (__builtin_constant_p(N) && (N) == 1 \ - ? X##_f0 & 1 \ - : (X##_f0 << (_FP_W_TYPE_SIZE - (N))) != 0)); \ - X##_f1 >>= (N); \ - } \ - else \ - { \ - X##_f0 = (X##_f1 >> ((N) - _FP_W_TYPE_SIZE) | \ - (((X##_f1 << (2*_FP_W_TYPE_SIZE - (N))) | X##_f0) != 0)); \ - X##_f1 = 0; \ - } \ - } while (0) +#define _FP_FRAC_SRS_2 (X ,N , sz) ( \ + (void) (((N) < _FP_W_TYPE_SIZE) \ + ? ({ \ + X##_f0 = (X##_f1 << (_FP_W_TYPE_SIZE - (N)) | X##_f0 >> (N) \ + | (__builtin_constant_p(N) && (N) == 1 \ + ? X##_f0 & 1 \ + : (X##_f0 << (_FP_W_TYPE_SIZE - (N))) != 0)); \ + X##_f1 >>= (N); \ + }) \ + : ({ \ + X##_f0 = (X##_f1 >> ((N) - _FP_W_TYPE_SIZE) \ + | ((((N) == _FP_W_TYPE_SIZE \ + ? 0 \ + : (X##_f1 << (2*_FP_W_TYPE_SIZE - (N)))) \ + | X##_f0) != 0)); \ + X##_f1 = 0; \ + })))#define _FP_FRAC_ADDI_2 (X ,I ) \ __FP_FRAC_ADDI_2(X##_f1, X##_f0, I)
Signature · 1 line
1.7.1
PATCH v4 5/5 math-emu/soft-fp.h: (_FP_ROUND_ZERO) cast 0 to void to fix warning
In reply to Vincent Chen_FP_ROUND_ZERO is defined as 0 and used as a statemente in macro _FP_ROUND. This generates "error: statement with no effect [-Werror=unused-value]" from gcc. Defining _FP_ROUND_ZERO as (void)0 to fix it.
This modification is quoted from glibc 'commit <In libc/:> (8ed1e7d5894000c155acbd06f)'
--- include/math-emu/soft-fp.h | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
include/math-emu/soft-fp.h +1 -1
@@ -138,7 +138,7 @@ _FP_FRAC_ADDI_##wc(X, _FP_WORK_ROUND); \ } while (0) -#define _FP_ROUND_ZERO (wc,X ) 0 +#define _FP_ROUND_ZERO (wc,X ) (void)0#define _FP_ROUND_PINF (wc,X ) \ do { \
Signature · 1 line
1.7.1
Vincent Chen <[email protected]> 於 2018年11月22日 週四 上午11:14寫道:
Quoted 114 lines
This patch set contains basic components for supporting the nds32 FPU,
such as exception handlers and context switch for FPU registers. By default, the lazy FPU scheme is supported and the user can configure it viaCONFIG_LZAY_FPU. In addition, a floating point emulator is required to handle all arithmetic of denormalized number because it is not supported by the nds32 FPU.As mentioned above, the nds32 FPU does not support denormalized number
This means the denormalized operands and results are not permitted. If an instruction contains denormalized operands, the nds32 FPU will raise an denormalized input exception to inform kernel to deal with this instruction. If the result of the instruction is a denormalized number, normally nds32 FPU will treat it as an underflow case and round the result to an appropriate value based on current rounding mode. Obviously, there is a precision gap for tininess number. To reduce this precision gap, kernel will enable the underflow trap by default to direct all underflow cases to the floating pointer emulator. By the floating pointer emulator, the correct denormalized number can be derived in kernel and return to the user program. The feature can be configured byCONFIG_SUPPORT_DENORMAL_ARITHMETIC, and if the precision requirement is not critical for tininess number, user may disables this feature to keep performance.The implementation of floating point emulator is based on soft-fp
which is located ininclude/math-emufolder. However, soft-fp is too outdated to pass the current compiler check. The needed modifications for soft-fp are included in this patch setChanges in v4:
- Fix compilation errorChanges in v3:
- Kernel with FPU support enabled still can run on a CPU without FPU
- RenameCONFIG_UNLAZY_FPUtoCONFIG_LAYZ_FPU
- Rename_switch()to_switch_fpu()
- Store FPU context when kernel suspends
- Modify the comments in code and patchChanges in v2:
- Remove the initilzation for floating pointer register before entering to signal handler.Vincent Chen (5):
nds32: nds32 FPU port
nds32: Support FP emulation
nds32: support denormalized result through FP emulatormath-emu/op-2.h: Use statement expressions to prevent negative
constant shift
math-emu/soft-fp.h: (_FP_ROUND_ZERO) cast 0 to void to fix warningarch/nds32/Kconfig | 1 + arch/nds32/Kconfig.cpu | 34 +++ arch/nds32/Makefile | 5 + arch/nds32/include/asm/bitfield.h | 15 ++ arch/nds32/include/asm/elf.h | 11 + arch/nds32/include/asm/fpu.h | 126 +++++++++++ arch/nds32/include/asm/fpuemu.h | 32 +++ arch/nds32/include/asm/nds32_fpu_inst.h | 109 +++++++++ arch/nds32/include/asm/processor.h | 7 + arch/nds32/include/asm/sfp-machine.h | 158 +++++++++++++ arch/nds32/include/asm/syscalls.h | 1 + arch/nds32/include/uapi/asm/auxvec.h | 7 + arch/nds32/include/uapi/asm/sigcontext.h | 14 ++ arch/nds32/include/uapi/asm/udftrap.h | 13 + arch/nds32/include/uapi/asm/unistd.h | 2 + arch/nds32/kernel/Makefile | 4 + arch/nds32/kernel/ex-entry.S | 24 ++- arch/nds32/kernel/ex-exit.S | 13 +- arch/nds32/kernel/ex-scall.S | 8 +- arch/nds32/kernel/fpu.c | 269 ++++++++++++++++++++++ arch/nds32/kernel/process.c | 64 +++++- arch/nds32/kernel/setup.c | 12 +- arch/nds32/kernel/signal.c | 62 +++++- arch/nds32/kernel/sleep.S | 4 +- arch/nds32/kernel/sys_nds32.c | 32 +++ arch/nds32/kernel/traps.c | 16 ++ arch/nds32/math-emu/Makefile | 7 + arch/nds32/math-emu/faddd.c | 24 ++ arch/nds32/math-emu/fadds.c | 24 ++ arch/nds32/math-emu/fcmpd.c | 24 ++ arch/nds32/math-emu/fcmps.c | 24 ++ arch/nds32/math-emu/fd2s.c | 22 ++ arch/nds32/math-emu/fdivd.c | 27 +++ arch/nds32/math-emu/fdivs.c | 26 +++ arch/nds32/math-emu/fmuld.c | 23 ++ arch/nds32/math-emu/fmuls.c | 23 ++ arch/nds32/math-emu/fnegd.c | 21 ++ arch/nds32/math-emu/fnegs.c | 21 ++ arch/nds32/math-emu/fpuemu.c | 357 ++++++++++++++++++++++++++++++ arch/nds32/math-emu/fs2d.c | 23 ++ arch/nds32/math-emu/fsqrtd.c | 21 ++ arch/nds32/math-emu/fsqrts.c | 21 ++ arch/nds32/math-emu/fsubd.c | 27 +++ arch/nds32/math-emu/fsubs.c | 27 +++ include/math-emu/op-2.h | 97 ++++----- include/math-emu/soft-fp.h | 2 +- 46 files changed, 1816 insertions(+), 68 deletions(-) create mode 100644 arch/nds32/include/asm/fpu.h create mode 100644 arch/nds32/include/asm/fpuemu.h create mode 100644 arch/nds32/include/asm/nds32_fpu_inst.h create mode 100644 arch/nds32/include/asm/sfp-machine.h create mode 100644 arch/nds32/include/uapi/asm/udftrap.h create mode 100644 arch/nds32/kernel/fpu.c create mode 100644 arch/nds32/math-emu/Makefile create mode 100644 arch/nds32/math-emu/faddd.c create mode 100644 arch/nds32/math-emu/fadds.c create mode 100644 arch/nds32/math-emu/fcmpd.c create mode 100644 arch/nds32/math-emu/fcmps.c create mode 100644 arch/nds32/math-emu/fd2s.c create mode 100644 arch/nds32/math-emu/fdivd.c create mode 100644 arch/nds32/math-emu/fdivs.c create mode 100644 arch/nds32/math-emu/fmuld.c create mode 100644 arch/nds32/math-emu/fmuls.c
Quoted 8 lines
create mode 100644 arch/nds32/math-emu/fnegd.c create mode 100644 arch/nds32/math-emu/fnegs.c create mode 100644 arch/nds32/math-emu/fpuemu.c create mode 100644 arch/nds32/math-emu/fs2d.c create mode 100644 arch/nds32/math-emu/fsqrtd.c create mode 100644 arch/nds32/math-emu/fsqrts.c create mode 100644 arch/nds32/math-emu/fsubd.c create mode 100644 arch/nds32/math-emu/fsubs.c
Hi Vincent,
Thank you.