[96205] | 1 | ; $Id: fesetenv.asm 98103 2023-01-17 14:15:46Z vboxsync $
|
---|
| 2 | ;; @file
|
---|
| 3 | ; IPRT - No-CRT fesetenv - AMD64 & X86.
|
---|
| 4 | ;
|
---|
| 5 |
|
---|
| 6 | ;
|
---|
[98103] | 7 | ; Copyright (C) 2022-2023 Oracle and/or its affiliates.
|
---|
[96205] | 8 | ;
|
---|
[96407] | 9 | ; This file is part of VirtualBox base platform packages, as
|
---|
| 10 | ; available from https://www.virtualbox.org.
|
---|
[96205] | 11 | ;
|
---|
[96407] | 12 | ; This program is free software; you can redistribute it and/or
|
---|
| 13 | ; modify it under the terms of the GNU General Public License
|
---|
| 14 | ; as published by the Free Software Foundation, in version 3 of the
|
---|
| 15 | ; License.
|
---|
| 16 | ;
|
---|
| 17 | ; This program is distributed in the hope that it will be useful, but
|
---|
| 18 | ; WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 19 | ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 20 | ; General Public License for more details.
|
---|
| 21 | ;
|
---|
| 22 | ; You should have received a copy of the GNU General Public License
|
---|
| 23 | ; along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
| 24 | ;
|
---|
[96205] | 25 | ; The contents of this file may alternatively be used under the terms
|
---|
| 26 | ; of the Common Development and Distribution License Version 1.0
|
---|
[96407] | 27 | ; (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
| 28 | ; in the VirtualBox distribution, in which case the provisions of the
|
---|
[96205] | 29 | ; CDDL are applicable instead of those of the GPL.
|
---|
| 30 | ;
|
---|
| 31 | ; You may elect to license modified versions of this file under the
|
---|
| 32 | ; terms and conditions of either the GPL or the CDDL or both.
|
---|
| 33 | ;
|
---|
[96407] | 34 | ; SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
| 35 | ;
|
---|
[96205] | 36 |
|
---|
| 37 |
|
---|
| 38 | %define RT_ASM_WITH_SEH64
|
---|
| 39 | %include "iprt/asmdefs.mac"
|
---|
| 40 | %include "iprt/x86.mac"
|
---|
| 41 |
|
---|
| 42 | %define RT_NOCRT_FE_DFL_ENV 1
|
---|
| 43 | %define RT_NOCRT_FE_NOMASK_ENV 2
|
---|
[96213] | 44 | %define RT_NOCRT_FE_PC53_ENV 3
|
---|
| 45 | %define RT_NOCRT_FE_PC64_ENV 4
|
---|
| 46 | %define RT_NOCRT_FE_LAST_ENV 4
|
---|
[96205] | 47 |
|
---|
| 48 |
|
---|
| 49 | BEGINCODE
|
---|
| 50 |
|
---|
| 51 | ;;
|
---|
| 52 | ; Sets the FPU+SSE environment.
|
---|
| 53 | ;
|
---|
| 54 | ; @returns eax = 0 on success, -1 on failure.
|
---|
| 55 | ; @param pEnv 32-bit: [xBP+8] msc64: rcx gcc64: rdi - Saved environment to restore.
|
---|
| 56 | ;
|
---|
| 57 | RT_NOCRT_BEGINPROC fesetenv
|
---|
| 58 | push xBP
|
---|
| 59 | SEH64_PUSH_xBP
|
---|
| 60 | mov xBP, xSP
|
---|
| 61 | SEH64_SET_FRAME_xBP 0
|
---|
| 62 | sub xBP, 20h
|
---|
| 63 | SEH64_ALLOCATE_STACK 20h
|
---|
| 64 | SEH64_END_PROLOGUE
|
---|
| 65 |
|
---|
| 66 | ;
|
---|
| 67 | ; Load the parameter into rcx.
|
---|
| 68 | ;
|
---|
| 69 | %ifdef ASM_CALL64_GCC
|
---|
| 70 | mov rcx, rdi
|
---|
| 71 | %elifdef RT_ARCH_X86
|
---|
| 72 | mov ecx, [xBP + xCB*2]
|
---|
| 73 | %endif
|
---|
| 74 |
|
---|
| 75 | ;
|
---|
| 76 | ; For the x87 state we only set FSW.XCPT, FCW.XCPT, FCW.RC and FCW.PC.
|
---|
| 77 | ; So we save the current environment, merge those fields and load it.
|
---|
| 78 | ;
|
---|
| 79 | fnstenv [xBP - 20h]
|
---|
| 80 |
|
---|
[96213] | 81 | ; Check for special "pointer" values:
|
---|
| 82 | cmp xCX, RT_NOCRT_FE_LAST_ENV
|
---|
| 83 | ja .x87_regular
|
---|
| 84 |
|
---|
| 85 | or eax, -1
|
---|
| 86 | test xCX, xCX
|
---|
| 87 | jnz .x87_special
|
---|
| 88 | %ifdef RT_STRICT
|
---|
| 89 | int3
|
---|
| 90 | %endif
|
---|
| 91 | jmp .return
|
---|
| 92 |
|
---|
| 93 | ;
|
---|
| 94 | ; Special x87 state. Clear all pending exceptions.
|
---|
| 95 | ;
|
---|
| 96 | ; We have 4 special environments with only some differences in FCW differs, so set
|
---|
| 97 | ; up FCW in AX, starting with a NOMASK environment as it has the fewest bits set.
|
---|
| 98 | ;
|
---|
| 99 | .x87_special:
|
---|
| 100 | and word [xBP - 20h + X86FSTENV32P.FSW], ~X86_FSW_XCPT_ES_MASK
|
---|
| 101 | mov ax, [xBP - 20h + X86FSTENV32P.FCW]
|
---|
| 102 | and ax, ~(X86_FCW_MASK_ALL | X86_FCW_PC_MASK | X86_FCW_RC_MASK | X86_FCW_IC_MASK)
|
---|
| 103 | %ifdef RT_OS_WINDOWS
|
---|
| 104 | or ax, X86_FCW_DM | X86_FCW_PC_53 | X86_FCW_RC_NEAREST | X86_FCW_IC_PROJECTIVE
|
---|
| 105 | %else
|
---|
| 106 | or ax, X86_FCW_DM | X86_FCW_PC_64 | X86_FCW_RC_NEAREST | X86_FCW_IC_PROJECTIVE
|
---|
| 107 | %endif
|
---|
[96205] | 108 | cmp xCX, RT_NOCRT_FE_NOMASK_ENV
|
---|
[96213] | 109 | je .x87_special_done
|
---|
| 110 | or ax, X86_FCW_MASK_ALL
|
---|
[96205] | 111 |
|
---|
[96213] | 112 | %ifdef RT_OS_WINDOWS
|
---|
| 113 | cmp xCX, RT_NOCRT_FE_PC64_ENV
|
---|
| 114 | jne .x87_special_done
|
---|
| 115 | or ax, X86_FCW_PC_64 ; X86_FCW_PC_64 is a super set of X86_FCW_PC_53, so no need to clear bits
|
---|
| 116 | %else
|
---|
| 117 | cmp xCX, RT_NOCRT_FE_PC53_ENV
|
---|
| 118 | jne .x87_special_done
|
---|
| 119 | and ax, X86_FCW_PC_64 & ~X86_FCW_PC_53 ; X86_FCW_PC_64 is a super set of X86_FCW_PC_53, so clear the bit that differs.
|
---|
| 120 | %endif
|
---|
[96205] | 121 |
|
---|
[96213] | 122 | .x87_special_done:
|
---|
| 123 | mov [xBP - 20h + X86FSTENV32P.FCW], ax
|
---|
[96205] | 124 | jmp .x87_common
|
---|
| 125 |
|
---|
[96213] | 126 | ;
|
---|
| 127 | ; Merge input and current.
|
---|
| 128 | ;
|
---|
[96205] | 129 | .x87_regular:
|
---|
| 130 | ; FCW:
|
---|
| 131 | mov ax, [xCX + X86FSTENV32P.FCW]
|
---|
| 132 | mov dx, [xBP - 20h + X86FSTENV32P.FCW]
|
---|
| 133 | and ax, X86_FCW_MASK_ALL | X86_FCW_RC_MASK | X86_FCW_PC_MASK
|
---|
| 134 | and dx, ~(X86_FCW_MASK_ALL | X86_FCW_RC_MASK | X86_FCW_PC_MASK)
|
---|
| 135 | or dx, ax
|
---|
| 136 | mov [xBP - 20h + X86FSTENV32P.FCW], dx
|
---|
| 137 | ; FSW
|
---|
| 138 | mov ax, [xCX + X86FSTENV32P.FSW]
|
---|
| 139 | mov dx, [xBP - 20h + X86FSTENV32P.FSW]
|
---|
| 140 | and ax, X86_FSW_XCPT_MASK
|
---|
| 141 | and dx, ~(X86_FSW_XCPT_MASK)
|
---|
| 142 | or dx, ax
|
---|
| 143 | mov [xBP - 20h + X86FSTENV32P.FSW], dx
|
---|
| 144 |
|
---|
| 145 | .x87_common:
|
---|
| 146 | ; Clear the exception info.
|
---|
| 147 | xor eax, eax
|
---|
| 148 | mov [xBP - 20h + X86FSTENV32P.FPUIP], eax
|
---|
| 149 | mov [xBP - 20h + X86FSTENV32P.FPUCS], eax ; covers FOP too.
|
---|
| 150 | mov [xBP - 20h + X86FSTENV32P.FPUDP], eax
|
---|
| 151 | mov [xBP - 20h + X86FSTENV32P.FPUDS], eax
|
---|
| 152 |
|
---|
| 153 | ; Load the merged and cleaned up environment.
|
---|
| 154 | fldenv [xBP - 20h]
|
---|
| 155 |
|
---|
[96213] | 156 |
|
---|
[96205] | 157 | ;
|
---|
| 158 | ; Now for SSE, if supported, where we'll restore everything as is.
|
---|
| 159 | ;
|
---|
| 160 | %ifdef RT_ARCH_X86
|
---|
| 161 | ; SSE supported (ecx preserved)?
|
---|
| 162 | extern NAME(rtNoCrtHasSse)
|
---|
| 163 | call NAME(rtNoCrtHasSse)
|
---|
| 164 | test al, al
|
---|
| 165 | jz .return_okay
|
---|
| 166 | %endif
|
---|
| 167 |
|
---|
[96213] | 168 | cmp xCX, RT_NOCRT_FE_LAST_ENV
|
---|
| 169 | jb .sse_special_env
|
---|
[96205] | 170 | ldmxcsr [xCX + 28]
|
---|
| 171 | jmp .return_okay
|
---|
| 172 |
|
---|
| 173 | .sse_special_env:
|
---|
| 174 | stmxcsr [xBP - 10h]
|
---|
| 175 | mov eax, [xBP - 10h]
|
---|
| 176 | and eax, ~(X86_MXCSR_XCPT_FLAGS | X86_MXCSR_XCPT_MASK | X86_MXCSR_RC_MASK | X86_MXCSR_DAZ | X86_MXCSR_FZ)
|
---|
| 177 | or eax, X86_MXCSR_RC_NEAREST | X86_MXCSR_DM
|
---|
[96213] | 178 | cmp xCX, RT_NOCRT_FE_NOMASK_ENV ; Only the NOMASK one differs here.
|
---|
[96205] | 179 | je .sse_special_load_eax
|
---|
[96213] | 180 | or eax, X86_MXCSR_RC_NEAREST | X86_MXCSR_XCPT_MASK ; default environment masks all exceptions
|
---|
[96205] | 181 | .sse_special_load_eax:
|
---|
| 182 | mov [xBP - 10h], eax
|
---|
| 183 | ldmxcsr [xBP - 10h]
|
---|
| 184 |
|
---|
| 185 | ;
|
---|
| 186 | ; Return success.
|
---|
| 187 | ;
|
---|
| 188 | .return_okay:
|
---|
| 189 | xor eax, eax
|
---|
[96213] | 190 | .return:
|
---|
[96205] | 191 | leave
|
---|
| 192 | ret
|
---|
| 193 | ENDPROC RT_NOCRT(fesetenv)
|
---|
| 194 |
|
---|