revert between 56095 -> 55830 in arch
[AROS.git] / arch / x86_64-all / exec / stackswap.S
blobc3d9828b208d5c42185feecc28861d3129efebce
1 #include <aros/x86_64/asm.h>
2 #include <aros/config.h>
4                 .text
5                 .align  16
6                 .globl  AROS_SLIB_ENTRY(StackSwap, Exec, 122)
7                 .type   AROS_SLIB_ENTRY(StackSwap, Exec, 122), @function
8                 
9 AROS_SLIB_ENTRY(StackSwap, Exec, 122):
10                 /* struct StackSwap is %rdi */
11                 /* SysBase is %rsi */
12                 
13                 pushq   %rbx
14                 
15                 /* StackSwap -> %rbx */
16                 movq    %rdi, %rbx
17                 
18                 /* Disable interrupts */
19                 pushq   %rsi
20                 movq    %rsi, %rdi      /* Put SysBase into rdi (first argument) */
21                 call    *Disable(%rsi)
22                 popq    %rsi
24                 /* movq    ThisTask(%rsi), %rax */
25                 xorq    %rdi, %rdi
26                 pushq   %rsi
27                 call    *FindTask(%rsi)
28                 popq    %rsi
30                 /* Exchange tc_SPLower */
31                 movq    tc_SPLower(%rax), %rcx
32                 xchgq   %rcx, stk_Lower(%rbx)
33                 movq    %rcx, tc_SPLower(%rax)
35                 /* Exchange tc_SPUpper */
36                 movq    tc_SPUpper(%rax), %rcx
37                 xchgq   %rcx, stk_Upper(%rbx)
38                 movq    %rcx, tc_SPUpper(%rax)
40                 movb    tc_Flags(%rax), %cl
41                 test    $TF_STACKCHK, %cl
42                 je      noStackSnoop
44                 /* Fill [stk_Lower .. stk_Pointer - 16] with 0xE1 */
46                 /* Destination register = %rdi = NEW SPLower.
47                    Which was already swapped above, so to be found
48                    in task->tc_SPLower */
49                 movq    tc_SPLower(%rax), %rdi
51                 /* %rcx = count register = NEW SP register - NEW SP_Lower - 16 */
52                 movq    stk_Pointer(%rbx), %rcx
53                 subq    $16, %rcx
54                 subq    %rdi, %rcx
56                 /* byte value to store */
57                 movb    $0xE1, %al
59                 /* direction to store: forward */
60                 cld
62                 /* do the store operation: put %rcx times %al into memory starting at %rdi. */
63                 rep
64                 stosb
66 noStackSnoop:
67                 /* StackSwap -> %rdi */
68                 movq    %rbx, %rdi
69                 popq    %rbx
70                 
71                 /* Get the return address */
72                 popq    %rax
73                 
74                 /* Restore *NEW* stack pointer */
75                 xchgq   %rsp, stk_Pointer(%rdi)
76                 
77                 pushq   %rdi
78                 pushq   %rax
79                 movq    %rsi, %rdi
80                 call    *Enable(%rsi)
81                 popq    %rax
82                 popq    %rdi
83                 
84                 jmpq    *%rax