Skip to content

Commit 44e92e0

Browse files
Vincent ChenGreentime Hu
authored andcommitted
nds32: support denormalized result through FP emulator
Currently, 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 Signed-off-by: Vincent Chen <vincentc@andestech.com> Acked-by: Greentime Hu <greentime@andestech.com> Signed-off-by: Greentime Hu <greentime@andestech.com>
1 parent 1ac8325 commit 44e92e0

11 files changed

Lines changed: 125 additions & 4 deletions

File tree

‎arch/nds32/Kconfig.cpu‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@ config LAZY_FPU
2828

2929
For nomal case, say Y.
3030

31+
config SUPPORT_DENORMAL_ARITHMETIC
32+
bool "Denormal arithmetic support"
33+
depends on FPU
34+
default n
35+
help
36+
Say Y here to enable arithmetic of denormalized number. Enabling
37+
this feature can enhance the precision for tininess number.
38+
However, performance loss in float pointe calculations is
39+
possibly significant due to additional FPU exception.
40+
41+
If the calculated tolerance for tininess number is not critical,
42+
say N to prevent performance loss.
43+
3144
config HWZOL
3245
bool "hardware zero overhead loop support"
3346
depends on CPU_D10 || CPU_D15

‎arch/nds32/include/asm/elf.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010

1111
#include <asm/ptrace.h>
12+
#include <asm/fpu.h>
1213

1314
typedef unsigned long elf_greg_t;
1415
typedef unsigned long elf_freg_t[3];
@@ -159,8 +160,18 @@ struct elf32_hdr;
159160

160161
#endif
161162

163+
164+
#if IS_ENABLED(CONFIG_FPU)
165+
#define FPU_AUX_ENT NEW_AUX_ENT(AT_FPUCW, FPCSR_INIT)
166+
#else
167+
#define FPU_AUX_ENT NEW_AUX_ENT(AT_IGNORE, 0)
168+
#endif
169+
162170
#define ARCH_DLINFO \
163171
do { \
172+
/* Optional FPU initialization */ \
173+
FPU_AUX_ENT; \
174+
\
164175
NEW_AUX_ENT(AT_SYSINFO_EHDR, \
165176
(elf_addr_t)current->mm->context.vdso); \
166177
} while (0)

‎arch/nds32/include/asm/fpu.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,18 @@ extern int do_fpuemu(struct pt_regs *regs, struct fpu_struct *fpu);
2828
#define sNAN64 0xFFFFFFFFFFFFFFFFULL
2929
#define sNAN32 0xFFFFFFFFUL
3030

31+
#if IS_ENABLED(CONFIG_SUPPORT_DENORMAL_ARITHMETIC)
32+
/*
33+
* Denormalized number is unsupported by nds32 FPU. Hence the operation
34+
* is treated as underflow cases when the final result is a denormalized
35+
* number. To enhance precision, underflow exception trap should be
36+
* enabled by default and kerenl will re-execute it by fpu emulator
37+
* when getting underflow exception.
38+
*/
39+
#define FPCSR_INIT FPCSR_mskUDFE
40+
#else
3141
#define FPCSR_INIT 0x0UL
42+
#endif
3243

3344
extern const struct fpu_struct init_fpuregs;
3445

‎arch/nds32/include/asm/syscalls.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
asmlinkage long sys_cacheflush(unsigned long addr, unsigned long len, unsigned int op);
88
asmlinkage long sys_fadvise64_64_wrapper(int fd, int advice, loff_t offset, loff_t len);
99
asmlinkage long sys_rt_sigreturn_wrapper(void);
10+
asmlinkage long sys_udftrap(int option);
1011

1112
#include <asm-generic/syscalls.h>
1213

‎arch/nds32/include/uapi/asm/auxvec.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
#ifndef __ASM_AUXVEC_H
55
#define __ASM_AUXVEC_H
66

7+
/*
8+
* This entry gives some information about the FPU initialization
9+
* performed by the kernel.
10+
*/
11+
#define AT_FPUCW 18 /* Used FPU control word. */
12+
13+
714
/* VDSO location */
815
#define AT_SYSINFO_EHDR 33
916

‎arch/nds32/include/uapi/asm/sigcontext.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@
1212
struct fpu_struct {
1313
unsigned long long fd_regs[32];
1414
unsigned long fpcsr;
15+
/*
16+
* UDF_trap is used to recognize whether underflow trap is enabled
17+
* or not. When UDF_trap == 1, this process will be traped and then
18+
* get a SIGFPE signal when encountering an underflow exception.
19+
* UDF_trap is only modified through setfputrap syscall. Therefore,
20+
* UDF_trap needn't be saved or loaded to context in each context
21+
* switch.
22+
*/
23+
unsigned long UDF_trap;
1524
};
1625

1726
struct zol_struct {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/* Copyright (C) 2005-2018 Andes Technology Corporation */
3+
#ifndef _ASM_SETFPUTRAP
4+
#define _ASM_SETFPUTRAP
5+
6+
/*
7+
* Options for setfputrap system call
8+
*/
9+
#define DISABLE_UDFTRAP 0 /* disable underflow exception trap */
10+
#define ENABLE_UDFTRAP 1 /* enable undeflos exception trap */
11+
#define GET_UDFTRAP 2 /* only get undeflos exception trap status */
12+
13+
#endif /* _ASM_CACHECTL */

‎arch/nds32/include/uapi/asm/unistd.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@
99

1010
/* Additional NDS32 specific syscalls. */
1111
#define __NR_cacheflush (__NR_arch_specific_syscall)
12+
#define __NR_udftrap (__NR_arch_specific_syscall + 1)
1213
__SYSCALL(__NR_cacheflush, sys_cacheflush)
14+
__SYSCALL(__NR_udftrap, sys_udftrap)

‎arch/nds32/kernel/fpu.c‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212

1313
const struct fpu_struct init_fpuregs = {
1414
.fd_regs = {[0 ... 31] = sNAN64},
15-
.fpcsr = FPCSR_INIT
15+
.fpcsr = FPCSR_INIT,
16+
#if IS_ENABLED(CONFIG_SUPPORT_DENORMAL_ARITHMETIC)
17+
.UDF_trap = 0
18+
#endif
1619
};
1720

1821
void save_fpu(struct task_struct *tsk)
@@ -174,6 +177,9 @@ inline void do_fpu_context_switch(struct pt_regs *regs)
174177
} else {
175178
/* First time FPU user. */
176179
load_fpu(&init_fpuregs);
180+
#if IS_ENABLED(CONFIG_SUPPORT_DENORMAL_ARITHMETIC)
181+
current->thread.fpu.UDF_trap = init_fpuregs.UDF_trap;
182+
#endif
177183
set_used_math();
178184
}
179185

@@ -183,10 +189,12 @@ inline void fill_sigfpe_signo(unsigned int fpcsr, int *signo)
183189
{
184190
if (fpcsr & FPCSR_mskOVFT)
185191
*signo = FPE_FLTOVF;
186-
else if (fpcsr & FPCSR_mskIVOT)
187-
*signo = FPE_FLTINV;
192+
#ifndef CONFIG_SUPPORT_DENORMAL_ARITHMETIC
188193
else if (fpcsr & FPCSR_mskUDFT)
189194
*signo = FPE_FLTUND;
195+
#endif
196+
else if (fpcsr & FPCSR_mskIVOT)
197+
*signo = FPE_FLTINV;
190198
else if (fpcsr & FPCSR_mskDBZT)
191199
*signo = FPE_FLTDIV;
192200
else if (fpcsr & FPCSR_mskIEXT)
@@ -197,11 +205,20 @@ inline void handle_fpu_exception(struct pt_regs *regs)
197205
{
198206
unsigned int fpcsr;
199207
int si_code = 0, si_signo = SIGFPE;
208+
#if IS_ENABLED(CONFIG_SUPPORT_DENORMAL_ARITHMETIC)
209+
unsigned long redo_except = FPCSR_mskDNIT|FPCSR_mskUDFT;
210+
#else
211+
unsigned long redo_except = FPCSR_mskDNIT;
212+
#endif
200213

201214
lose_fpu();
202215
fpcsr = current->thread.fpu.fpcsr;
203216

204-
if (fpcsr & FPCSR_mskDNIT) {
217+
if (fpcsr & redo_except) {
218+
#if IS_ENABLED(CONFIG_SUPPORT_DENORMAL_ARITHMETIC)
219+
if (fpcsr & FPCSR_mskUDFT)
220+
current->thread.fpu.fpcsr &= ~FPCSR_mskIEX;
221+
#endif
205222
si_signo = do_fpuemu(regs, &current->thread.fpu);
206223
fpcsr = current->thread.fpu.fpcsr;
207224
if (!si_signo)

‎arch/nds32/kernel/sys_nds32.c‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
#include <asm/cachectl.h>
88
#include <asm/proc-fns.h>
9+
#include <asm/udftrap.h>
10+
#include <asm/fpu.h>
911

1012
SYSCALL_DEFINE6(mmap2, unsigned long, addr, unsigned long, len,
1113
unsigned long, prot, unsigned long, flags,
@@ -48,3 +50,33 @@ SYSCALL_DEFINE3(cacheflush, unsigned int, start, unsigned int, end, int, cache)
4850

4951
return 0;
5052
}
53+
54+
SYSCALL_DEFINE1(udftrap, int, option)
55+
{
56+
#if IS_ENABLED(CONFIG_SUPPORT_DENORMAL_ARITHMETIC)
57+
int old_udftrap;
58+
59+
if (!used_math()) {
60+
load_fpu(&init_fpuregs);
61+
current->thread.fpu.UDF_trap = init_fpuregs.UDF_trap;
62+
set_used_math();
63+
}
64+
65+
old_udftrap = current->thread.fpu.UDF_trap;
66+
switch (option) {
67+
case DISABLE_UDFTRAP:
68+
current->thread.fpu.UDF_trap = 0;
69+
break;
70+
case ENABLE_UDFTRAP:
71+
current->thread.fpu.UDF_trap = FPCSR_mskUDFE;
72+
break;
73+
case GET_UDFTRAP:
74+
break;
75+
default:
76+
return -EINVAL;
77+
}
78+
return old_udftrap;
79+
#else
80+
return -ENOTSUPP;
81+
#endif
82+
}

0 commit comments

Comments
 (0)