2022-09-06 01:22:32 +00:00
/* SPDX-License-Identifier: MIT */
/* Copyright (c) 2021-2022 The SRS Authors */
2021-10-02 00:54:53 +00:00
/* If user disable the ASM, such as avoiding bugs in ASM, donot compile it. */
# if ! d e f i n e d ( M D _ S T _ N O _ A S M )
# if d e f i n e d ( _ _ a m d64 _ _ ) | | d e f i n e d ( _ _ x86 _ 6 4 _ _ )
/****************************************************************/
/ *
* Internal _ _ j m p _ b u f l a y o u t
* /
# define J B _ R B X 0
# define J B _ R B P 1
# define J B _ R 1 2 2 / * R 1 2 : R 1 5 N o n v o l a t i l e M u s t b e p r e s e r v e d b y c a l l e e * /
# define J B _ R 1 3 3 / * @see https://docs.microsoft.com/en-us/cpp/build/x64-software-conventions?view=msvc-160#register-usage */
# define J B _ R 1 4 4 / * R B X , R B P , R D I , R S I , R 1 2 , R 1 4 , R 1 4 , a n d R 1 5 m u s t b e s a v e d i n a n y f u n c t i o n u s i n g t h e m . * /
# define J B _ R 1 5 5 / * @see https://software.intel.com/content/www/us/en/develop/articles/introduction-to-x64-assembly.html */
# define J B _ R S P 6
# define J B _ P C 7
.file " md_ c y g w i n 6 4 . S "
.text
/* _st_md_cxt_save(__jmp_buf env) */ /* The env is rcx, https://docs.microsoft.com/en-us/cpp/build/x64-calling-convention?view=msvc-160 */
.globl _st_md_cxt_save
.align 16
_st_md_cxt_save :
/ *
* Save r e g i s t e r s .
* /
movq % r b x , ( J B _ R B X * 8 ) ( % r c x ) / * S a v e r b x t o e n v [ 0 ] , * ( i n t 6 4 _ t * ) ( r c x + 0 ) =rbx * /
movq % r b p , ( J B _ R B P * 8 ) ( % r c x ) / * S a v e r b p t o e n v [ 1 ] , * ( i n t 6 4 _ t * ) ( r c x + 1 ) =rbp * /
movq % r12 , ( J B _ R 1 2 * 8 ) ( % r c x ) / * S a v e r12 t o e n v [ 2 ] , * ( i n t 6 4 _ t * ) ( r c x + 2 ) =r12 * /
movq % r13 , ( J B _ R 1 3 * 8 ) ( % r c x ) / * S a v e r13 t o e n v [ 3 ] , * ( i n t 6 4 _ t * ) ( r c x + 3 ) =r13 * /
movq % r14 , ( J B _ R 1 4 * 8 ) ( % r c x ) / * S a v e r14 t o e n v [ 4 ] , * ( i n t 6 4 _ t * ) ( r c x + 4 ) =r14 * /
movq % r15 , ( J B _ R 1 5 * 8 ) ( % r c x ) / * S a v e r15 t o e n v [ 5 ] , * ( i n t 6 4 _ t * ) ( r c x + 5 ) =r15 * /
/* Save SP */
leaq 8 ( % r s p ) , % r8 / * S a v e * ( i n t 6 4 _ t * ) ( r s p + 8 ) t o r8 , h t t p s : / / g i t h u b . c o m / o s s r s / s t a t e - t h r e a d s / i s s u e s / 2 0 #i s s u e c o m m e n t - 887569093 * /
movq % r8 , ( J B _ R S P * 8 ) ( % r c x ) / * S a v e r8 ( r s p ) t o e n v [ 6 ] , * ( i n t 6 4 _ t * ) ( r c x + 6 ) =r8 * /
/* Save PC we are returning to */
movq ( % r s p ) , % r9 / * S a v e P C ( p a r e n t f u n c t i o n a d d r e s s ) % ( r s p ) t o r9 , h t t p s : / / g i t h u b . c o m / o s s r s / s t a t e - t h r e a d s / i s s u e s / 2 0 #i s s u e c o m m e n t - 887569093 * /
movq % r9 , ( J B _ P C * 8 ) ( % r c x ) / * S a v e r9 ( P C ) t o e n v [ 7 ] , * ( i n t 6 4 _ t * ) ( r c x + 7 ) =r9 * /
xorq % r a x , % r a x / * R e s e t r a x ( r e t u r n v a l u e ) t o 0 * /
ret
/****************************************************************/
/* _st_md_cxt_restore(__jmp_buf env, int val) */ /* The env is rcx, val is edx/rdx, https://docs.microsoft.com/en-us/cpp/build/x64-calling-convention?view=msvc-160 */
.globl _st_md_cxt_restore
.align 16
_st_md_cxt_restore :
/ *
* Restore r e g i s t e r s .
* /
movq ( J B _ R B X * 8 ) ( % r c x ) , % r b x / * L o a d r b x f r o m e n v [ 0 ] * /
movq ( J B _ R B P * 8 ) ( % r c x ) , % r b p / * L o a d r b p f r o m e n v [ 1 ] * /
movq ( J B _ R 1 2 * 8 ) ( % r c x ) , % r12 / * L o a d r12 f r o m e n v [ 2 ] * /
movq ( J B _ R 1 3 * 8 ) ( % r c x ) , % r13 / * L o a d r13 f r o m e n v [ 3 ] * /
movq ( J B _ R 1 4 * 8 ) ( % r c x ) , % r14 / * L o a d r14 f r o m e n v [ 4 ] * /
movq ( J B _ R 1 5 * 8 ) ( % r c x ) , % r15 / * L o a d r15 f r o m e n v [ 5 ] * /
/* Set return value */ /* The edx is param1 val, the eax is return value */
test % e d x , % e d x / * i f ( ! v a l ) { * /
mov $ 0 1 , % e a x / * v a l =1 ; */
cmove % e a x , % e d x / * } * /
mov % e d x , % e a x / * r e t u r n v a l ; */
/* Restore PC and RSP */
movq ( J B _ P C * 8 ) ( % r c x ) , % r8 / * L o a d r8 ( P C ) f r o m e n v [ 7 ] , h t t p s : / / g i t h u b . c o m / o s s r s / s t a t e - t h r e a d s / i s s u e s / 2 0 #i s s u e c o m m e n t - 887569093 * /
movq ( J B _ R S P * 8 ) ( % r c x ) , % r s p / * L o a d r s p f r o m e n v [ 6 ] * /
/* Jump to saved PC */
jmpq * % r8 / * J u m p t o r8 ( P C ) * /
/****************************************************************/
# endif
# endif