1/* i386 support code for fibers and multithreading.
2   Copyright (C) 2019-2020 Free Software Foundation, Inc.
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 3, or (at your option) any later
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14for more details.
15
16Under Section 7 of GPL version 3, you are granted additional
17permissions described in the GCC Runtime Library Exception, version
183.1, as published by the Free Software Foundation.
19
20You should have received a copy of the GNU General Public License and
21a copy of the GCC Runtime Library Exception along with this program;
22see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23<http://www.gnu.org/licenses/>.  */
24
25#include "../common/threadasm.S"
26
27#if defined(__i386__)
28
29    .text
30    .globl CSYM(fiber_switchContext)
31    .type CSYM(fiber_switchContext), @function
32    .align 16
33CSYM(fiber_switchContext):
34    .cfi_startproc
35    // save current stack state
36    push %ebp
37    mov  %esp, %ebp
38    push %edi
39    push %esi
40    push %ebx
41    push %eax
42
43    // store oldp again with more accurate address
44    mov 8(%ebp), %eax
45    mov %esp, (%eax)
46    // load newp to begin context switch
47    mov 12(%ebp), %esp
48
49    // load saved state from new stack
50    pop %eax
51    pop %ebx
52    pop %esi
53    pop %edi
54    pop %ebp
55
56    // 'return' to complete switch
57    ret
58    .cfi_endproc
59    .size CSYM(fiber_switchContext),.-CSYM(fiber_switchContext)
60
61#elif defined(__x86_64__) && !defined(__ILP32__)
62
63    .text
64    .globl CSYM(fiber_switchContext)
65    .type CSYM(fiber_switchContext), @function
66    .align 16
67CSYM(fiber_switchContext):
68    .cfi_startproc
69    // Save current stack state.save current stack state
70    push %rbp
71    mov  %rsp, %rbp
72    push %rbx
73    push %r12
74    push %r13
75    push %r14
76    push %r15
77
78    // store oldp again with more accurate address
79    mov %rsp, (%rdi)
80    // load newp to begin context switch
81    mov %rsi, %rsp
82
83    // load saved state from new stack
84    pop %r15
85    pop %r14
86    pop %r13
87    pop %r12
88    pop %rbx
89    pop %rbp
90
91    // 'return' to complete switch
92    ret
93    .cfi_endproc
94   .size CSYM(fiber_switchContext),.-CSYM(fiber_switchContext)
95
96#endif
97