1;;
2;; Copyright (c) 2012-2020, Intel Corporation
3;;
4;; Redistribution and use in source and binary forms, with or without
5;; modification, are permitted provided that the following conditions are met:
6;;
7;;     * Redistributions of source code must retain the above copyright notice,
8;;       this list of conditions and the following disclaimer.
9;;     * Redistributions in binary form must reproduce the above copyright
10;;       notice, this list of conditions and the following disclaimer in the
11;;       documentation and/or other materials provided with the distribution.
12;;     * Neither the name of Intel Corporation nor the names of its contributors
13;;       may be used to endorse or promote products derived from this software
14;;       without specific prior written permission.
15;;
16;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17;; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19;; DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20;; FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22;; SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23;; CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24;; OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25;; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26;;
27
28; Macros for "printing" for debug purposes from within asm code
29;
30; The basic macros are:
31;   DBGPRINT16, DBGPRINT32, DBGPRINT64, DBGPRINT_XMM, DBGPRINT_YMM, DBGPRINT_ZMM
32; These are called with 1 or more arguments, all of which are of the
33; size/type as specified in the name. E.g.
34;   DBGPRINT64 reg1, reg2, reg3, ...
35;
36; There is also a macro DEBUGPRINTL that takes one argument, a string. E.g.
37;   DBGPRINTL "hit this point in the code"
38;
39; There are also variations on these with the "DBGPRINT" suffixed with "L", e.g.
40; DBGPRINTL64. These take two or more arguments, where the first is a string,
41; and the rest are of the specified type, e.g.
42;   DBGPRINTL64 "Rindex", Rindex
43; Essentially, this is the same as a DBGPRINTL followed by DBGPRINT64.
44;
45; If DO_DBGPRINT is defined, then the macros write the debug information into
46; a buffer. If DO_DBGPRINT is *not* defined, then the macros expand to nothing.
47;
48; CAVEAT: The macros need a GPR. Currently, it uses R15. If the first register
49; argument is R15, then it will use R14. This means that if you try
50;   DBGPRINTL64 "text", rax, r15
51; you will not get the proper value of r15.
52; One way to avoid this issue is to not use multiple registers on the same line
53; if the register types are GPR (i.e. this is not an issue for printing XMM
54; registers). E.g the above could be done with:
55;   DBGPRINTL64 "test", rax
56;   DBGPRINT64 r15
57;
58; Note also that the macros only check for r15. Thus is you tried something
59; like (after token expansion):
60;   DBGPRINT32 r15d
61; you won't get the right results. If you want to display r15d, you should
62; print it as the 64-bit r15.
63;
64; To actually print the data, from your C code include the file
65; "dbgprint.h". The default buffer size is 16kB. If you want to change
66; that, #define DBG_BUFFER_SIZE before including "dbgprint.h".
67;
68; Then, (after your asm routine(s) have returned, call
69;   print_debug()    or    print_debug(file pointer)
70; If you do not specify a file pointer, it defaults to stdout.
71;
72; Printing the debug data also resets the write pointer to the beginning,
73; effectively "deleting" the previous messages.
74;
75%ifndef DBGPRINT_ASM_INCLUDED
76%define DBGPRINT_ASM_INCLUDED
77
78;%define DO_DBGPRINT
79%ifdef DO_DBGPRINT
80extern pDebugBuffer
81;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
82;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
83; DBGPRINT_INT size, param, ...
84%macro DBGPRINT_INT 2-*
85%ifidni %2,r15
86%xdefine %%reg r14
87%else
88%xdefine %%reg r15
89%endif
90%xdefine %%size %1
91%rotate 1
92	push	%%reg
93	mov	%%reg, [pDebugBuffer]
94%rep %0 - 1
95	mov	byte [%%reg], %%size
96	%if (%%size == 2)
97	mov	word [%%reg+1], %1
98	%elif (%%size == 4)
99	mov	dword [%%reg+1], %1
100	%elif (%%size == 8)
101	mov	qword [%%reg+1], %1
102	%elif (%%size == 16)
103	movdqu	oword [%%reg+1], %1
104	%elif (%%size == 32)
105	vmovdqu	[%%reg+1], %1
106	%elif (%%size == 64)
107	vmovdqu32 [%%reg+1], %1
108	%else
109	%error invalid size %%size
110	%endif
111	add	%%reg, %%size+1
112%rotate 1
113%endrep
114	mov	[pDebugBuffer], %%reg
115	pop	%%reg
116%endmacro
117;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
118;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
119; DBGPRINTL_INT size, label, param, ...
120%macro DBGPRINTL_INT 3-*
121%ifidni %3,r15
122%xdefine %%reg r14
123%else
124%xdefine %%reg r15
125%endif
126%xdefine %%size %1
127%rotate 1
128	push	%%reg
129	mov	%%reg, [pDebugBuffer]
130
131	mov	byte [%%reg], 0x57
132section .data
133%%lab: db %1, 0
134section .text
135	mov	qword [%%reg+1], %%lab
136	add	%%reg, 8+1
137%rotate 1
138
139%rep %0 - 2
140	mov	byte [%%reg], %%size
141%if (%%size == 2)
142	mov	word [%%reg+1], %1
143%elif (%%size == 4)
144	mov	dword [%%reg+1], %1
145%elif (%%size == 8)
146	mov	qword [%%reg+1], %1
147%elif (%%size == 16)
148	movdqu	oword [%%reg+1], %1
149%elif (%%size == 32)
150	vmovdqu	[%%reg+1], %1
151%elif (%%size == 64)
152	vmovdqu32 [%%reg+1], %1
153%else
154%error invalid size %%size
155%endif
156	add	%%reg, %%size+1
157%rotate 1
158%endrep
159	mov	[pDebugBuffer], %%reg
160	pop	%%reg
161%endmacro
162;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
163;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
164; DBGPRINTL* data, ...
165%macro DBGPRINT16 1+
166	DBGPRINT_INT 2, %1
167%endmacro
168%macro DBGPRINT32 1+
169	DBGPRINT_INT 4, %1
170%endmacro
171%macro DBGPRINT64 1+
172	DBGPRINT_INT 8, %1
173%endmacro
174%macro DBGPRINT_XMM 1+
175	DBGPRINT_INT 16, %1
176%endmacro
177%macro DBGPRINT_YMM 1+
178	DBGPRINT_INT 32, %1
179%endmacro
180%macro DBGPRINT_ZMM 1+
181	DBGPRINT_INT 64, %1
182%endmacro
183;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
184; DBGPRINTL* label, data, ...
185%macro DBGPRINTL16 2+
186	DBGPRINTL_INT 2, %1, %2
187%endmacro
188%macro DBGPRINTL32 2+
189	DBGPRINTL_INT 4, %1, %2
190%endmacro
191%macro DBGPRINTL64 2+
192	DBGPRINTL_INT 8, %1, %2
193%endmacro
194%macro DBGPRINTL_XMM 2+
195	DBGPRINTL_INT 16, %1, %2
196%endmacro
197%macro DBGPRINTL_YMM 2+
198	DBGPRINTL_INT 32, %1, %2
199%endmacro
200%macro DBGPRINTL_ZMM 2+
201	DBGPRINTL_INT 64, %1, %2
202%endmacro
203;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
204%macro DBGPRINTL 1
205	push	r15
206	mov	r15, [pDebugBuffer]
207
208	mov	byte [r15], 0x57
209section .data
210%%lab: db %1, 0
211section .text
212	mov	qword [r15+1], %%lab
213	add	r15, 8+1
214
215	mov	[pDebugBuffer], r15
216	pop	r15
217%endmacro
218;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
219;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
220%else
221%macro DBGPRINT16 1+
222%endmacro
223%macro DBGPRINT32 1+
224%endmacro
225%macro DBGPRINT64 1+
226%endmacro
227%macro DBGPRINT_XMM 1+
228%endmacro
229%macro DBGPRINT_YMM 1+
230%endmacro
231%macro DBGPRINT_ZMM 1+
232%endmacro
233;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
234%macro DBGPRINTL16 2+
235%endmacro
236%macro DBGPRINTL32 2+
237%endmacro
238%macro DBGPRINTL64 2+
239%endmacro
240%macro DBGPRINTL_XMM 2+
241%endmacro
242%macro DBGPRINTL_YMM 2+
243%endmacro
244%macro DBGPRINTL_ZMM 2+
245%endmacro
246;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
247%macro DBGPRINTL 1
248%endmacro
249%endif
250
251
252
253%if 0 ; OLD
254%macro DBGPRINTL_ZMM 2-*
255	push	rax
256	mov	rax, [pDebugBuffer]
257
258	mov	byte [rax], 0x57
259section .data
260%%lab: db %1, 0
261section .text
262	mov	qword [rax+1], %%lab
263	add	rax, 8+1
264%rotate 1
265
266%rep %0 - 1
267	mov	byte [rax], 64
268	vmovdqu32 [rax+1], %1
269%rotate 1
270	add	rax, 64+1
271%endrep
272	mov	[pDebugBuffer], rax
273	pop	rax
274%endmacro
275;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
276%macro DBGPRINT_ZMM 1-*
277	push	rax
278	mov	rax, [pDebugBuffer]
279%rep %0
280	mov	byte [rax], 64
281	vmovdqu32 [rax+1], %1
282%rotate 1
283	add	rax, 64+1
284%endrep
285	mov	[pDebugBuffer], rax
286	pop	rax
287%endmacro
288;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
289%macro DBGPRINT_YMM 1-*
290	push	rax
291	mov	rax, [pDebugBuffer]
292%rep %0
293	mov	byte [rax], 32
294	vmovdqu	[rax+1], %1
295%rotate 1
296	add	rax, 32+1
297%endrep
298	mov	[pDebugBuffer], rax
299	pop	rax
300%endmacro
301;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
302%macro DBGPRINT_XMM 1-*
303	push	rax
304	mov	rax, [pDebugBuffer]
305%rep %0
306	mov	byte [rax], 16
307	vmovdqu	oword [rax+1], %1
308%rotate 1
309	add	rax, 16+1
310%endrep
311	mov	[pDebugBuffer], rax
312	pop	rax
313%endmacro
314;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
315%macro DBGPRINTL64 2-*
316	push	rax
317	mov	rax, [pDebugBuffer]
318
319	mov	byte [rax], 0x57
320section .data
321%%lab: db %1, 0
322section .text
323	mov	qword [rax+1], %%lab
324	add	rax, 8+1
325%rotate 1
326
327%rep %0 - 1
328	mov	byte [rax], 8
329	mov	qword [rax+1], %1
330%rotate 1
331	add	rax, 8+1
332%endrep
333	mov	[pDebugBuffer], rax
334	pop	rax
335%endmacro
336;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
337%macro DBGPRINT64 1-*
338	push	rax
339	mov	rax, [pDebugBuffer]
340%rep %0
341	mov	byte [rax], 8
342	mov	qword [rax+1], %1
343%rotate 1
344	add	rax, 8+1
345%endrep
346	mov	[pDebugBuffer], rax
347	pop	rax
348%endmacro
349;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
350%macro DBGPRINT32 1-*
351	push	rax
352	mov	rax, [pDebugBuffer]
353%rep %0
354	mov	byte [rax], 4
355	mov	dword [rax+1], %1
356%rotate 1
357	add	rax, 4+1
358%endrep
359	mov	[pDebugBuffer], rax
360	pop	rax
361%endmacro
362;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
363%macro DBGPRINT16 1-*
364	push	rax
365	mov	rax, [pDebugBuffer]
366%rep %0
367	mov	byte [rax], 2
368	mov	word [rax+1], %1
369%rotate 1
370	add	rax, 2+1
371%endrep
372	mov	[pDebugBuffer], rax
373	pop	rax
374%endmacro
375;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
376%macro DBGPRINT_LAB 1
377	push	rax
378	mov	rax, [pDebugBuffer]
379
380	mov	byte [rax], 0x57
381section .data
382%%lab: db %1, 0
383section .text
384	mov	qword [rax+1], %%lab
385	add	rax, 8+1
386
387	mov	[pDebugBuffer], rax
388	pop	rax
389%endmacro
390;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
391;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
392%macro DBGHIST 2
393	inc	dword [%1 + 4 * %2]
394%endmacro
395%macro DBGPRINT_ZMM 1-*
396%endmacro
397%macro DBGPRINT_YMM 1-*
398%endmacro
399%macro DBGPRINT_XMM 1-*
400%endmacro
401%macro DBGPRINT64 1-*
402%endmacro
403%macro DBGPRINT32 1-*
404%endmacro
405%macro DBGPRINT16 1-*
406%endmacro
407%macro DBGHIST 2
408%endmacro
409;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
410;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
411%endif ; ifdef 0 ; OLD
412
413%endif ; DBGPRINT_ASM_INCLUDED
414