1 /// -*- mode: asm; asm-comment-char: ?/ -*-
3 /// Register dump and debugging for x86
5 /// (c) 2019 Straylight/Edgeware
8 ///----- Licensing notice ---------------------------------------------------
10 /// This file is part of Catacomb.
12 /// Catacomb is free software: you can redistribute it and/or modify it
13 /// under the terms of the GNU Library General Public License as published
14 /// by the Free Software Foundation; either version 2 of the License, or
15 /// (at your option) any later version.
17 /// Catacomb is distributed in the hope that it will be useful, but
18 /// WITHOUT ANY WARRANTY; without even the implied warranty of
19 /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 /// Library General Public License for more details.
22 /// You should have received a copy of the GNU Library General Public
23 /// License along with Catacomb. If not, write to the Free Software
24 /// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
27 ///--------------------------------------------------------------------------
31 #include "asm-common.h"
38 ///--------------------------------------------------------------------------
43 // On entry, r/esp should point to a return address and
44 // `REGDUMP_GPSIZE' bytes of word-aligned storage to be the
45 // general-purpose save area, with flags saved in the bottom word,
46 // r/eax saved in the fourth, and (on 32-bit x86) ebx in the fifth.
47 // On exit, the initial registers are saved in this space, and
48 // modified: r/ebp points to the general-purpose save area, ecx
49 // contains the number of bytes required in the extended save area,
50 // ebx is preserved on 32-bit x86, and other general-purpose
51 // registers are clobbered or used to communicate with
52 // `regdump_xtsave' below. Doing anything other than lowering the
53 // stack pointer and calling `regdump_xtsave' is not recommended.
55 // Other code will insist that df is clear.
58 // Save r/ebp and establish it pointing to the save area.
59 mov [SP + WORDSZ + REGIX_BP*WORDSZ], BP
62 // Save the other easy general-purpose registers.
64 mov [BP + REGIX_BX*WORDSZ], BX
66 mov [BP + REGIX_CX*WORDSZ], CX
67 mov [BP + REGIX_DX*WORDSZ], DX
68 mov [BP + REGIX_SI*WORDSZ], SI
69 mov [BP + REGIX_DI*WORDSZ], DI
71 mov [BP + REGIX_R8*WORDSZ], r8
72 mov [BP + REGIX_R9*WORDSZ], r9
73 mov [BP + REGIX_R10*WORDSZ], r10
74 mov [BP + REGIX_R11*WORDSZ], r11
75 mov [BP + REGIX_R12*WORDSZ], r12
76 mov [BP + REGIX_R13*WORDSZ], r13
77 mov [BP + REGIX_R14*WORDSZ], r14
78 mov [BP + REGIX_R15*WORDSZ], r15
81 // Determine the previous stack pointer and save it.
82 #if CPUFAM_AMD64 && ABI_SYSV
83 lea AX, [BP + 128 + REGDUMP_GPSIZE]
85 lea AX, [BP + REGDUMP_GPSIZE]
87 mov [BP + REGIX_SP*WORDSZ], AX
89 // Collect the return address and save it as r/eip.
91 mov [BP + REGIX_IP*WORDSZ], AX
93 // Save the segment registers.
94 lea AX, [BP + REGIX_GPLIM*WORDSZ]
95 mov [AX + 2*REGIX_CS], cs
96 mov [AX + 2*REGIX_DS], ds
97 mov [AX + 2*REGIX_SS], ss
98 mov [AX + 2*REGIX_ES], es
99 mov [AX + 2*REGIX_FS], fs
100 mov [AX + 2*REGIX_GS], gs
102 // Determine the extended save area size. Preserve ebx on 32-bit x86
103 // here, because the caller needs it for PLT-indirect calls.
115 add ecx, regmap_size + 64 // map + align
118 1: mov ecx, 512 + regmap_size + 16 // fxsave + map + align
131 // On entry, r/ebp points to a general-purpose save area, established
132 // by `regdump_gpsave'. On exit, the general-purpose registers
133 // (other than the stack pointer) are restored to their original
136 // We assume nobody actually fiddled with the segment registers. So
137 // just the actual integer registers to do.
138 mov AX, [BP + REGIX_AX*WORDSZ]
139 mov BX, [BP + REGIX_BX*WORDSZ]
140 mov CX, [BP + REGIX_CX*WORDSZ]
141 mov DX, [BP + REGIX_DX*WORDSZ]
142 mov SI, [BP + REGIX_SI*WORDSZ]
143 mov DI, [BP + REGIX_DI*WORDSZ]
145 mov r8, [BP + REGIX_R8*WORDSZ]
146 mov r9, [BP + REGIX_R9*WORDSZ]
147 mov r10, [BP + REGIX_R10*WORDSZ]
148 mov r11, [BP + REGIX_R11*WORDSZ]
149 mov r12, [BP + REGIX_R12*WORDSZ]
150 mov r13, [BP + REGIX_R13*WORDSZ]
151 mov r14, [BP + REGIX_R14*WORDSZ]
152 mov r15, [BP + REGIX_R15*WORDSZ]
154 mov BP, [BP + REGIX_BP*WORDSZ]
162 # define fxsave fxsave64
163 # define fxrstor fxrstor64
164 # define xsave xsave64
165 # define xrstor xrstor64
170 // On entry, r/esp points to a return address and extended save area,
171 // of size determined by `regdump_gpsave' above. On exit, the save
172 // area is filled in and a handy map placed at its base, the x87
173 // floating-point state is reset, r/ebp is left pointing to the
174 // register map, ebx is preserved on 32-bit x86, and the other
175 // general registers are clobbered.
177 // Start by filling in the easy parts of the map.
178 mov [SP + WORDSZ + regmap_gp], BP
179 lea BP, [SP + WORDSZ]
181 xor eax, eax // clears rax too on amd64
182 mov [BP + regmap_avx], AX
184 // Find out whether we use `xsave'. (Preserve ebx.)
193 // We have the `xsave' machinery. Select the base address.
194 lea SI, [SP + WORDSZ + regmap_size + 63]
196 mov [BP + regmap_fx], SI
198 // Clear out the header area.
204 // Save the registers.
209 // Establish the AVX pointer, if available.
210 test dword ptr [SI + 512], 4 // = xstate_bv
217 mov [BP + regmap_avx], BX
221 // We have only `fxsave'. Set the base address.
222 5: lea SI, [SP + WORDSZ + regmap_size + 15]
224 mov [BP + regmap_fx], SI
226 // Save the registers.
229 // Clear the x87 state; otherwise it can cause trouble later.
242 // On entry, r/ebp points to a register-save map. On exit, the
243 // extended registers are restored from the save area; r/ebp is left
244 // pointing to the general-purpose save area, ebx is preserved on
245 // 32-bit x86, and the other general registers are clobbered.
247 // Find the extended register dump.
248 mov SI, [BP + regmap_fx]
250 // Probe to find out whether we have `xsave'.
259 // We have the `xsave' machinery.
265 // We must fake it up.
269 8: mov BP, [BP + regmap_gp]
277 ///----- That's all, folks --------------------------------------------------