Commit 97724ab7 authored by Dominik Charousset's avatar Dominik Charousset

boost.context

parent 68494302
...@@ -8,7 +8,6 @@ ...@@ -8,7 +8,6 @@
*.lo *.lo
*.o *.o
*.png *.png
*.s
*.tmp *.tmp
.DS_Store .DS_Store
.deps/ .deps/
......
/*
Copyright Oliver Kowalke 2009.
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or copy at
http://www.boost.org/LICENSE_1_0.txt)
*/
/*******************************************************************
* *
* ------------------------------------------------------------- *
* | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | *
* ------------------------------------------------------------- *
* | 0x0 | 0x4 | 0x8 | 0xc | 0x10| 0x14| 0x18| 0x1c| 0x20| 0x24| *
* ------------------------------------------------------------- *
* | v1 | v2 | v3 | v4 | v5 | v6 | v7 | v8 | sp | lr | *
* ------------------------------------------------------------- *
* ------------------------------------------------------------- *
* | 10 | | *
* ------------------------------------------------------------- *
* | 0x28| | *
* ------------------------------------------------------------- *
* | pc | | *
* ------------------------------------------------------------- *
* ------------------------------------------------------------- *
* | 11 | 12 | | *
* ------------------------------------------------------------- *
* | 0x2c| 0x30| | *
* ------------------------------------------------------------- *
* |sbase|slimit| | *
* ------------------------------------------------------------- *
* ------------------------------------------------------------- *
* | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | *
* ------------------------------------------------------------- *
* | 0x34| 0x38|0x3c| 0x40| 0x44| 0x48| 0x4c| 0x50| 0x54| 0x58 | *
* ------------------------------------------------------------- *
* | s16 | s17 | s18 | s19 | s20 | s21 | s22 | s23 | s24 | s25 | *
* ------------------------------------------------------------- *
* ------------------------------------------------------------- *
* | 23 | 24 | 25 | 26 | 27 | 28 | | *
* ------------------------------------------------------------- *
* | 0x5c| 0x60| 0x64| 0x68| 0x6c| 0x70| | *
* ------------------------------------------------------------- *
* | s26 | s27 | s28 | s29 | s30 | s31 | | *
* ------------------------------------------------------------- *
* *
* *****************************************************************/
.text
.globl jump_fcontext
.align 2
.type jump_fcontext,%function
jump_fcontext:
stmia a1, {v1-v8,sp-lr} @ save V1-V8,SP-LR
str lr, [a1,#40] @ save LR as PC
#if (defined(__VFP_FP__) && !defined(__SOFTFP__))
cmp a4, #0 @ test if fpu env should be preserved
be 1f
ldr a4, [a1,#52]
stmia a4, {s16-s31} @ save S16-S31
ldr a4, [a2,#52]
ldmia a4, {s16-s31} @ restore S16-S31
1:
#endif
mov a1, a3 @ use third arg as return value after jump
@ and as first arg in context function
ldmia a2, {v1-v8,sp-pc} @ restore v1-V8,SP-PC
.size jump_fcontext,.-jump_fcontext
.text
.globl make_fcontext
.align 2
.type make_fcontext,%function
make_fcontext:
str a1, [a1,#0] @ save the address of passed context
str a2, [a1,#40] @ save address of the context function
ldr a2, [a1,#44] @ load the stack base
push {a1,lr} @ save pointer to fcontext_t
mov a1, a2 @ stack pointer as arg for align_stack
bl align_stack@PLT @ align stack
mov a2, a1 @ begin of aligned stack
pop {a1,lr} @ restore pointer to fcontext_t
str a2, [a1,#32] @ save the aligned stack base
adr a2, finish @ address of finish; called after context function returns
str a2, [a1,#36]
mov a1, #0
bx lr
finish:
mov a1, #0 @ exit code is zero
bl _exit@PLT @ exit application
.size make_fcontext,.-make_fcontext
/*
Copyright Oliver Kowalke 2009.
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or copy at
http://www.boost.org/LICENSE_1_0.txt)
*/
/********************************************************************
* *
* -------------------------------------------------------------- *
* | 0 | 1 | 2 | 3 | 4 | 5 | *
* -------------------------------------------------------------- *
* | 0x0 | 0x4 | 0x8 | 0xc | 0x10 | 0x14 | *
* -------------------------------------------------------------- *
* | EDI | ESI | EBX | EBP | ESP | EIP | *
* -------------------------------------------------------------- *
* -------------------------------------------------------------- *
* | 6 | 7 | | *
* -------------------------------------------------------------- *
* | 0x18 | 0x1c | | *
* -------------------------------------------------------------- *
* | sbase | slimit | | *
* -------------------------------------------------------------- *
* -------------------------------------------------------------- *
* | 8 | 9 | | *
* -------------------------------------------------------------- *
* | 0x20 | 0x24 | | *
* -------------------------------------------------------------- *
* | fc_mxcsr|fc_x87_cw| | *
* -------------------------------------------------------------- *
* *
* *****************************************************************/
.text
.globl jump_fcontext
.align 2
.type jump_fcontext,@function
jump_fcontext:
movl 0x4(%esp), %ecx /* load address of the first fcontext_t arg */
movl %edi, (%ecx) /* save EDI */
movl %esi, 0x4(%ecx) /* save ESI */
movl %ebx, 0x8(%ecx) /* save EBX */
movl %ebp, 0xc(%ecx) /* save EBP */
leal 0x4(%esp), %eax /* exclude the return address */
movl %eax, 0x10(%ecx) /* save as stack pointer */
movl (%esp), %eax /* load return address */
movl %eax, 0x14(%ecx) /* save return address */
movl 0x8(%esp), %edx /* load address of the second fcontext_t arg */
movl (%edx), %edi /* restore EDI */
movl 0x4(%edx), %esi /* restore ESI */
movl 0x8(%edx), %ebx /* restore EBX */
movl 0xc(%edx), %ebp /* restore EBP */
movl 0x10(%esp), %eax /* check if fpu enve preserving was requested */
test %eax, %eax
je 1f
stmxcsr 0x20(%ecx) /* save MMX control and status word */
fnstcw 0x24(%ecx) /* save x87 control word */
ldmxcsr 0x20(%edx) /* restore MMX control and status word */
fldcw 0x24(%edx) /* restore x87 control word */
1:
movl 0xc(%esp), %eax /* use third arg as return value after jump */
movl 0x10(%edx), %esp /* restore ESP */
movl %eax, 0x4(%esp) /* use third arg as first arg in context function */
movl 0x14(%edx), %edx /* fetch the address to return to */
jmp *%edx /* indirect jump to context */
.size jump_fcontext,.-jump_fcontext
.text
.globl make_fcontext
.align 2
.type make_fcontext,@function
make_fcontext:
movl 0x4(%esp), %eax /* load address of the fcontext_t */
movl %eax, (%eax) /* save the address of current context */
movl 0x8(%esp), %ecx /* load the address of the context function */
movl %ecx, 0x14(%eax) /* save the address of the context function */
movl 0x18(%eax), %edx /* load the stack base */
pushl %eax /* save pointer to fcontext_t */
pushl %ebx /* save EBX */
pushl %edx /* stack pointer as arg for align_stack */
call 1f
1: popl %ebx /* address of label 1 */
addl $_GLOBAL_OFFSET_TABLE_+[.-1b], %ebx /* compute address of GOT and store it in EBX */
call align_stack@PLT /* align stack */
movl %eax, %edx /* begin of aligned stack */
popl %eax /* remove arg for align_stack */
popl %ebx /* restore EBX */
popl %eax /* restore pointer to fcontext_t */
leal -0x14(%edx), %edx /* reserve space for the last frame on stack, (ESP + 4) % 16 == 0 */
movl %edx, 0x10(%eax) /* save the aligned stack base */
stmxcsr 0x20(%eax) /* save MMX control and status word */
fnstcw 0x24(%eax) /* save x87 control word */
call 2f
2: popl %ecx /* address of label 2 */
addl $finish-2b, %ecx /* helper code executed after context function returns */
movl %ecx, (%edx)
xorl %eax, %eax
ret
finish:
leal -0xc(%esp), %esp
call 3f
3: popl %ebx /* address of label 3 */
addl $_GLOBAL_OFFSET_TABLE_+[.-3b], %ebx /* compute address of GOT and store it in EBX */
xorl %eax, %eax
pushl %eax /* exit code is zero */
call _exit@PLT /* exit application */
hlt
.size make_fcontext,.-make_fcontext
/*
Copyright Oliver Kowalke 2009.
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or copy at
http://www.boost.org/LICENSE_1_0.txt)
*/
/********************************************************************
* *
* -------------------------------------------------------------- *
* | 0 | 1 | 2 | 3 | 4 | 5 | *
* -------------------------------------------------------------- *
* | 0x0 | 0x4 | 0x8 | 0xc | 0x10 | 0x14 | *
* -------------------------------------------------------------- *
* | EDI | ESI | EBX | EBP | ESP | EIP | *
* -------------------------------------------------------------- *
* -------------------------------------------------------------- *
* | 6 | 7 | | *
* -------------------------------------------------------------- *
* | 0x18 | 0x1c | | *
* -------------------------------------------------------------- *
* | sbase | slimit | | *
* -------------------------------------------------------------- *
* -------------------------------------------------------------- *
* | 8 | 9 | | *
* -------------------------------------------------------------- *
* | 0x20 | 0x24 | | *
* -------------------------------------------------------------- *
* | fc_mxcsr|fc_x87_cw| | *
* -------------------------------------------------------------- *
* *
* *****************************************************************/
.text
.globl _jump_fcontext
.align 2
_jump_fcontext:
movl 0x4(%esp), %ecx /* load address of the first fcontext_t arg */
movl %edi, (%ecx) /* save EDI */
movl %esi, 0x4(%ecx) /* save ESI */
movl %ebx, 0x8(%ecx) /* save EBX */
movl %ebp, 0xc(%ecx) /* save EBP */
leal 0x4(%esp), %eax /* exclude the return address */
movl %eax, 0x10(%ecx) /* save as stack pointer */
movl (%esp), %eax /* load return address */
movl %eax, 0x14(%ecx) /* save return address */
movl 0x8(%esp), %edx /* load address of the second fcontext_t arg */
movl (%edx), %edi /* restore EDI */
movl 0x4(%edx), %esi /* restore ESI */
movl 0x8(%edx), %ebx /* restore EBX */
movl 0xc(%edx), %ebp /* restore EBP */
movl 0x10(%esp), %eax /* check if fpu enve preserving was requested */
test %eax, %eax
je 1f
stmxcsr 0x20(%ecx) /* save MMX control and status word */
fnstcw 0x24(%ecx) /* save x87 control word */
ldmxcsr 0x20(%edx) /* restore MMX control and status word */
fldcw 0x24(%edx) /* restore x87 control word */
1:
movl 0xc(%esp), %eax /* use third arg as return value after jump */
movl 0x10(%edx), %esp /* restore ESP */
movl %eax, 0x4(%esp) /* use third arg as first arg in context function */
movl 0x14(%edx), %edx /* fetch the address to return to */
jmp *%edx /* indirect jump to context */
.text
.globl _make_fcontext
.align 2
_make_fcontext:
movl 0x4(%esp), %eax /* load address of the fcontext_t */
movl %eax, (%eax) /* save the address of current context */
movl 0x8(%esp), %ecx /* load the address of the context function */
movl %ecx, 0x14(%eax) /* save the address of the context function */
movl 0x18(%eax), %edx /* load the stack base */
pushl %eax /* save pointer to fcontext_t */
pushl %ebx /* save EBX */
pushl %edx /* stack pointer as arg for align_stack */
call 1f
1: popl %ebx /* address of label 1 */
addl $_GLOBAL_OFFSET_TABLE_+[.-1b], %ebx /* compute address of GOT and store it in EBX */
call align_stack@PLT /* align stack */
movl %eax, %edx /* begin of aligned stack */
popl %eax /* remove arg for align_stack */
popl %ebx /* restore EBX */
popl %eax /* restore pointer to fcontext_t */
leal -0x14(%edx), %edx /* reserve space for the last frame on stack, (ESP + 4) % 16 == 0 */
movl %edx, 0x10(%eax) /* save the aligned stack base */
stmxcsr 0x20(%eax) /* save MMX control and status word */
fnstcw 0x24(%eax) /* save x87 control word */
call 2f
2: popl %ecx /* address of label 2 */
addl $finish-2b, %ecx /* helper code executed after context function returns */
movl %ecx, (%edx)
xorl %eax, %eax
ret
finish:
leal -0xc(%esp), %esp
call 3f
3: popl %ebx /* address of label 3 */
addl $_GLOBAL_OFFSET_TABLE_+[.-3b], %ebx /* compute address of GOT and store it in EBX */
xorl %eax, %eax
pushl %eax /* exit code is zero */
call _exit@PLT /* exit application */
hlt
/*
Copyright Oliver Kowalke 2009.
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or copy at
http://www.boost.org/LICENSE_1_0.txt)
*/
/*******************************************************************
* *
* ------------------------------------------------------------- *
* | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | *
* ------------------------------------------------------------- *
* | 0 | 8 | 16 | 24 | 32 | 40 | 48 | 56 | 64 | 72 | *
* ------------------------------------------------------------- *
* | S0 | S1 | S2 | S3 | S4 | S5 | S6 | S7 | GP | SP | *
* ------------------------------------------------------------- *
* ------------------------------------------------------------- *
* | 10 | 11 | 12 | | *
* ------------------------------------------------------------- *
* | 80 | 88 | 96 | | *
* ------------------------------------------------------------- *
* | S8 | RA | PC | | *
* ------------------------------------------------------------- *
* ------------------------------------------------------------- *
* | 13 | 14 | | *
* ------------------------------------------------------------- *
* | 104 | 112 | | *
* ------------------------------------------------------------- *
* |sbase|slimt| | *
* ------------------------------------------------------------- *
* ------------------------------------------------------------- *
* | 15 | 16 | 17 | 18 | 19 | 20 | | *
* ------------------------------------------------------------- *
* | 120 | 128 | 136 | 144 | 152 | 160 | | *
* ------------------------------------------------------------- *
* | F20 | F22 | F24 | F26 | F28 | F30 | | *
* ------------------------------------------------------------- *
* *
* *****************************************************************/
.text
.globl jump_fcontext
.align 2
.type jump_fcontext,@function
.ent jump_fcontext
jump_fcontext:
sw $s0, ($a0) # save S0
sw $s1, 8($a0) # save S1
sw $s2, 16($a0) # save S2
sw $s3, 24($a0) # save S3
sw $s4, 32($a0) # save S4
sw $s5, 40($a0) # save S5
sw $s6, 48($a0) # save S6
sw $s7, 56($a0) # save S7
sw $gp, 64($a0) # save GP
sw $sp, 72($a0) # save SP
sw $s8, 80($a0) # save S8
sw $ra, 88($a0) # save RA
sw $ra, 96($a0) # save RA as PC
#if defined(__mips_hard_float)
beqz $a3, 1f # test if fpu env should be preserved
s.d $f20, 120($a0) # save F20
s.d $f22, 128($a0) # save F22
s.d $f24, 136($a0) # save F24
s.d $f26, 144($a0) # save F26
s.d $f28, 152($a0) # save F28
s.d $f30, 160($a0) # save F30
l.d $f20, 120($a1) # restore F20
l.d $f22, 128($a1) # restore F22
l.d $f24, 136($a1) # restore F24
l.d $f26, 144($a1) # restore F26
l.d $f28, 152($a1) # restore F28
l.d $f30, 160($a1) # restore F30
1:
#endif
lw $s0, ($a1) # restore S0
lw $s1, 8($a1) # restore S1
lw $s2, 16($a1) # restore S2
lw $s3, 24($a1) # restore S3
lw $s4, 32($a1) # restore S4
lw $s5, 40($a1) # restore S5
lw $s6, 48($a1) # restore S6
lw $s7, 56($a1) # restore S7
lw $gp, 64($a1) # restore GP
lw $sp, 72($a1) # restore SP
lw $s8, 80($a1) # restore S8
lw $ra, 88($a1) # restore RA
move $a0, $s2 # restore void pointer as argument
move $v0, $a2 # use third arg as return value after jump
move $a0, $a2 # use third arg as first arg in context function
lw $t9, 96($a1) # load PC
jr $t9 # jump to context
.end jump_fcontext
.size jump_fcontext, .-jump_fcontext
.text
.globl make_fcontext
.align 2
.type make_fcontext,@function
.ent make_fcontext
make_fcontext:
#ifdef __PIC__
.set noreorder
.cpload $t9
.set reorder
#endif
sw $a0, ($a0) # save the current context
sw $gp, 24($a0) # save global pointer
sw $a1, 96($a0) # save the address of the context function
lw $t0, 104($a0) # load the stack base
sub $sp, $sp, 28
sw $ra, 24($sp)
sw $a0, 20($sp)
move $a0, $t0 # stack pointer as arg for align_stack
lw $t9, %call16(align_stack)($gp) # align stack
jalr $t9
nop
move $t0, $v0 # begin of aligned stack
lw $ra, 24($sp)
lw $a0, 20($sp)
addi $sp, $sp, 28
sub $t0, $t0, 16 # reserve 16 byte of argument space
sw $t0, 72($a0) # save the algned stack base
la $t9, finish # helper code executed after context function returns
sw $t9, 88($a0)
move $v0, $zero
jr $ra
finish:
move $gp, $s3 # restore GP (global pointer)
move $a0, $zero # exit code is zero
lw $t9, %call16(_exit)($gp) # exit application
jalr $t9
.end make_fcontext
.size make_fcontext, .-make_fcontext
/*
Copyright Oliver Kowalke 2009.
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or copy at
http://www.boost.org/LICENSE_1_0.txt)
*/
/****************************************************************************************
* *
* ---------------------------------------------------------------------------------- *
* | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | *
* ---------------------------------------------------------------------------------- *
* | 0x0 | 0x4 | 0x8 | 0xc | 0x10 | 0x14 | 0x18 | 0x1c | *
* ---------------------------------------------------------------------------------- *
* | RBX | R12 | R13 | R14 | *
* ---------------------------------------------------------------------------------- *
* ---------------------------------------------------------------------------------- *
* | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | *
* ---------------------------------------------------------------------------------- *
* | 0x20 | 0x24 | 0x28 | 0x2c | 0x30 | 0x34 | 0x38 | 0x3c | *
* ---------------------------------------------------------------------------------- *
* | R15 | RBP | RSP | RIP | *
* ---------------------------------------------------------------------------------- *
* ---------------------------------------------------------------------------------- *
* | 16 | 17 | 18 | 19 | | *
* ---------------------------------------------------------------------------------- *
* | 0x40 | 0x44 | 0x48 | 0x4c | | *
* ---------------------------------------------------------------------------------- *
* | sbase | slimit | | *
* ---------------------------------------------------------------------------------- *
* ---------------------------------------------------------------------------------- *
* | 20 | 21 | | *
* ---------------------------------------------------------------------------------- *
* | 0x50 | 0x54 | | *
* ---------------------------------------------------------------------------------- *
* | fc_mxcsr|fc_x87_cw| | *
* ---------------------------------------------------------------------------------- *
* *
* **************************************************************************************/
.text
.globl jump_fcontext
.type jump_fcontext,@function
.align 16
jump_fcontext:
movq %rbx, (%rdi) /* save RBX */
movq %r12, 0x8(%rdi) /* save R12 */
movq %r13, 0x10(%rdi) /* save R13 */
movq %r14, 0x18(%rdi) /* save R14 */
movq %r15, 0x20(%rdi) /* save R15 */
movq %rbp, 0x28(%rdi) /* save RBP */
cmp $0, %rcx
je 1f
stmxcsr 0x50(%rdi) /* save MMX control and status word */
fnstcw 0x54(%rdi) /* save x87 control word */
ldmxcsr 0x50(%rsi) /* restore MMX control and status word */
fldcw 0x54(%rsi) /* restore x87 control word */
1:
leaq 0x8(%rsp), %rax /* exclude the return address and save as stack pointer */
movq %rax, 0x30(%rdi) /* save as stack pointer */
movq (%rsp), %rax /* save return address */
movq %rax, 0x38(%rdi) /* save return address as RIP */
movq (%rsi), %rbx /* restore RBX */
movq 0x8(%rsi), %r12 /* restore R12 */
movq 0x10(%rsi), %r13 /* restore R13 */
movq 0x18(%rsi), %r14 /* restore R14 */
movq 0x20(%rsi), %r15 /* restore R15 */
movq 0x28(%rsi), %rbp /* restore RBP */
movq 0x30(%rsi), %rsp /* restore RSP */
movq 0x38(%rsi), %rcx /* fetch the address to return to */
movq %rdx, %rax /* use third arg as return value after jump */
movq %rdx, %rdi /* use third arg as first arg in context function */
jmp *%rcx /* indirect jump to context */
.size jump_fcontext,.-jump_fcontext
.text
.globl make_fcontext
.type make_fcontext,@function
.align 16
make_fcontext:
movq %rdi, (%rdi) /* save the address of passed context */
movq %rsi, 0x38(%rdi) /* save the address of the context function */
movq 0x40(%rdi), %rdx /* load the stack base */
pushq %rdi /* save pointer to fcontext_t */
movq %rdx, %rdi /* stack pointer as arg for align_stack */
call align_stack@PLT /* align stack */
movq %rax, %rdx /* begin of aligned stack */
popq %rdi /* restore pointer to fcontext_t */
leaq -0x8(%rdx), %rdx /* reserve space for the last frame on stack, (RSP + 8) & 15 == 0 */
movq %rdx, 0x30(%rdi) /* save the algined stack base */
stmxcsr 0x50(%rdi) /* save MMX control and status word */
fnstcw 0x54(%rdi) /* save x87 control word */
leaq finish(%rip), %rcx /* address of finish; called after context function returns */
movq %rcx, (%rdx)
xorq %rax, %rax
ret
finish:
xorq %rdi, %rdi /* exit code is zero */
call _exit@PLT /* exit application */
hlt
.size make_fcontext,.-make_fcontext
/*
Copyright Oliver Kowalke 2009.
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or copy at
http://www.boost.org/LICENSE_1_0.txt)
*/
/****************************************************************************************
* *
* ---------------------------------------------------------------------------------- *
* | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | *
* ---------------------------------------------------------------------------------- *
* | 0x0 | 0x4 | 0x8 | 0xc | 0x10 | 0x14 | 0x18 | 0x1c | *
* ---------------------------------------------------------------------------------- *
* | RBX | R12 | R13 | R14 | *
* ---------------------------------------------------------------------------------- *
* ---------------------------------------------------------------------------------- *
* | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | *
* ---------------------------------------------------------------------------------- *
* | 0x20 | 0x24 | 0x28 | 0x2c | 0x30 | 0x34 | 0x38 | 0x3c | *
* ---------------------------------------------------------------------------------- *
* | R15 | RBP | RSP | RIP | *
* ---------------------------------------------------------------------------------- *
* ---------------------------------------------------------------------------------- *
* | 16 | 17 | 18 | 19 | | *
* ---------------------------------------------------------------------------------- *
* | 0x40 | 0x44 | 0x48 | 0x4c | | *
* ---------------------------------------------------------------------------------- *
* | sbase | slimit | | *
* ---------------------------------------------------------------------------------- *
* ---------------------------------------------------------------------------------- *
* | 20 | 21 | | *
* ---------------------------------------------------------------------------------- *
* | 0x50 | 0x54 | | *
* ---------------------------------------------------------------------------------- *
* | fc_mxcsr|fc_x87_cw| | *
* ---------------------------------------------------------------------------------- *
* *
* **************************************************************************************/
.text
.globl _jump_fcontext
.align 8
_jump_fcontext:
movq %rbx, (%rdi) /* save RBX */
movq %r12, 0x8(%rdi) /* save R12 */
movq %r13, 0x10(%rdi) /* save R13 */
movq %r14, 0x18(%rdi) /* save R14 */
movq %r15, 0x20(%rdi) /* save R15 */
movq %rbp, 0x28(%rdi) /* save RBP */
cmp $0, %rcx
je 1f
stmxcsr 0x50(%rdi) /* save MMX control and status word */
fnstcw 0x54(%rdi) /* save x87 control word */
ldmxcsr 0x50(%rsi) /* restore MMX control and status word */
fldcw 0x54(%rsi) /* restore x87 control word */
1:
leaq 0x8(%rsp), %rax /* exclude the return address and save as stack pointer */
movq %rax, 0x30(%rdi) /* save as stack pointer */
movq (%rsp), %rax /* save return address */
movq %rax, 0x38(%rdi) /* save return address as RIP */
movq (%rsi), %rbx /* restore RBX */
movq 0x8(%rsi), %r12 /* restore R12 */
movq 0x10(%rsi), %r13 /* restore R13 */
movq 0x18(%rsi), %r14 /* restore R14 */
movq 0x20(%rsi), %r15 /* restore R15 */
movq 0x28(%rsi), %rbp /* restore RBP */
movq 0x30(%rsi), %rsp /* restore RSP */
movq 0x38(%rsi), %rcx /* fetch the address to return to */
movq %rdx, %rax /* use third arg as return value after jump */
movq %rdx, %rdi /* use third arg as first arg in context function */
jmp *%rcx /* indirect jump to context */
.text
.globl _make_fcontext
.align 8
_make_fcontext:
movq %rdi, (%rdi) /* save the address of current context */
movq %rsi, 0x38(%rdi) /* save the address of the function supposed to run */
movq 0x40(%rdi), %rdx /* load the stack base */
pushq %rdi /* save pointer to fcontext_t */
movq %rdx, %rdi /* stack pointer as arg for align_stack */
call _align_stack /* align stack */
//call align_stack@PLT /* align stack */
movq %rax, %rdx /* begin of aligned stack */
popq %rdi /* restore pointer to fcontext_t */
leaq -0x8(%rdx), %rdx /* reserve space for the last frame on stack, (RSP + 8) % 16 == 0 */
movq %rdx, 0x30(%rdi) /* save the address */
stmxcsr 0x50(%rdi) /* save MMX control and status word */
fnstcw 0x54(%rdi) /* save x87 control word */
leaq finish(%rip), %rcx /* helper code executed after context function returns */
movq %rcx, (%rdx)
xorq %rax, %rax /* set RAX to zero */
ret
finish:
xorq %rdi, %rdi /* exit code is zero */
//call _exit@PLT /* exit application */
call _exit /* exit application */
hlt
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment