xref: /f-stack/app/micro_thread/arch_ctx.S (revision a9643ea8)
1
2/**
3 * Tencent is pleased to support the open source community by making MSEC available.
4 *
5 * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
6 *
7 * Licensed under the GNU General Public License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License. You may
9 * obtain a copy of the License at
10 *
11 *     https://opensource.org/licenses/GPL-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software distributed under the
14 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
15 * either express or implied. See the License for the specific language governing permissions
16 * and limitations under the License.
17 */
18
19
20#
21#  context  x86 or x86_64 save and restore
22#
23#  x86_64    x86
24# 0	%rbx    %ebx
25# 1	%rsp    %esp
26# 2	%rbp    %ebp
27# 3	%r12    %esi
28# 4	%r13    %edi
29# 5	%r14    %eip
30# 6	%r15
31# 7	%rip
32
33
34
35#if  defined(__amd64__) || defined(__x86_64__)
36
37##
38#  @brief save_context
39##
40	.text
41	.align 4
42	.globl save_context
43	.type save_context, @function
44save_context:
45	pop  %rsi
46	xorl %eax,%eax
47	movq %rbx,(%rdi)
48	movq %rsp,8(%rdi)
49	push %rsi
50	movq %rbp,16(%rdi)
51	movq %r12,24(%rdi)
52	movq %r13,32(%rdi)
53	movq %r14,40(%rdi)
54	movq %r15,48(%rdi)
55	movq %rsi,56(%rdi)
56	ret
57
58	.size save_context,.-save_context
59
60##
61#  @brief restore_context
62##
63	.text
64	.align 4
65	.globl restore_context
66	.type restore_context, @function
67restore_context:
68	movl %esi,%eax
69	movq (%rdi),%rbx
70	movq 8(%rdi),%rsp
71	movq 16(%rdi),%rbp
72	movq 24(%rdi),%r12
73	movq 32(%rdi),%r13
74	movq 40(%rdi),%r14
75	movq 48(%rdi),%r15
76	jmp *56(%rdi)
77
78	.size restore_context,.-restore_context
79
80##
81#  @brief replace_esp
82##
83	.text
84	.align 4
85	.globl replace_esp
86	.type replace_esp, @function
87replace_esp:
88	movq %rsi,8(%rdi)
89	ret
90
91	.size replace_esp,.-replace_esp
92
93
94#elif defined(__i386__)
95
96##
97#  @brief save_context
98##
99	.text
100	.align 4
101	.globl save_context
102	.type save_context, @function
103save_context:
104	movl 4(%esp),%edx
105	popl %ecx
106	xorl %eax,%eax
107	movl %ebx,(%edx)
108	movl %esp,4(%edx)
109	pushl %ecx
110	movl %ebp,8(%edx)
111	movl %esi,12(%edx)
112	movl %edi,16(%edx)
113	movl %ecx,20(%edx)
114	ret
115
116	.size save_context,.-save_context
117
118
119##
120#  @brief restore_context
121##
122	.text
123	.align 4
124	.globl restore_context
125	.type restore_context, @function
126restore_context:
127	movl 4(%esp),%edx
128	movl 8(%esp),%eax
129	movl (%edx),%ebx
130	movl 4(%edx),%esp
131	movl 8(%edx),%ebp
132	movl 12(%edx),%esi
133	movl 16(%edx),%edi
134	jmp *20(%edx)
135
136	.size restore_context,.-restore_context
137
138##
139#  @brief replace_esp
140##
141    .text
142    .align 4
143    .globl replace_esp
144    .type replace_esp, @function
145replace_esp:
146	movl 4(%esp),%edx
147	movl 8(%esp),%eax
148    movl %eax,4(%edx)
149    ret
150
151    .size replace_esp,.-replace_esp
152
153
154#else
155#error "Linux cpu arch not supported"
156#endif
157
158