summaryrefslogtreecommitdiff
path: root/debugger/system_routine/sr.g4a
blob: 4d6961df0f12488df15a7a96c41b4485f27af893 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include "debug.h"
#include "evict.h"

#define CR0_0_ME_STATE_CTRL (1 << 31)
#define CR0_0_BP_SUPPRESS (1 << 15)
#define CR0_0_SPF_EN (1 << 2)
#define CR0_0_ACC_DIS (1 << 1)
#define CR0_1_BES_CTRL (1 << 31)
#define CR0_1_HALT_CTRL (1 << 30)
#define CR0_1_SOFT_EXCEPTION_CTRL (1 << 29)
#define CR0_1_ILLGL_OP_STS (1 << 28)
#define CR0_1_STACK_OVRFLW_STS (1 << 27)

#define CR0_0_ENTRY_UNMASK (CR0_0_SPF_EN | CR0_0_ACC_DIS)
// TODO: Need to fix this for non breakpoint case
#define CR0_1_ENTRY_UNMASK ~(CR0_1_BES_CTRL)
#define CR0_0_RETURN_MASK ~(CR0_0_ME_STATE_CTRL | CR0_0_SPF_EN | CR0_0_ACC_DIS)

// TODO: not sure how to make this not hardcoded
#define PER_THREAD_SCRATCH_SIZE (256 * 1024)
#define PER_THREAD_QWORDS (PER_THREAD_SCRATCH_SIZE >> 4)

/* Should get this from brw_defines.h */
#define BRW_DATAPORT_OWORD_BLOCK_2_OWORDS     		2
#define BRW_DATAPORT_OWORD_BLOCK_4_OWORDS     		3
#define BRW_DATAPORT_OWORD_BLOCK_8_OWORDS     		4
#define GEN6_DATAPORT_WRITE_MESSAGE_OWORD_BLOCK_WRITE	8
#define BRW_DATAPORT_READ_MESSAGE_OWORD_BLOCK_READ	0

/* desc field, ie. dword3 6.3.66.2 and 2.11.2.1.4 */
#define SEND_MLEN_5		(5<<25)
#define SEND_MLEN_3		(3<<25)
#define SEND_MLEN_2		(2<<25)
#define SEND_MLEN_1		(1<<25)
#define SEND_RLEN_1		(1<<20)
#define SEND_RLEN_0		(0<<20)
#define SEND_HEADER_PRESENT	(1<<19)
#define SEND_WRITE_COMMIT	(1<<17)
#define SEND_TYPE_WRITE	(GEN6_DATAPORT_WRITE_MESSAGE_OWORD_BLOCK_WRITE<<13)
#define SEND_TYPE_READ	(BRW_DATAPORT_READ_MESSAGE_OWORD_BLOCK_READ<<13)
#define SEND_BLOCK_SIZE1	(BRW_DATAPORT_OWORD_BLOCK_2_OWORDS<<8)
#define SEND_BLOCK_SIZE2	(BRW_DATAPORT_OWORD_BLOCK_4_OWORDS<<8)
#define SEND_BLOCK_SIZE4	(BRW_DATAPORT_OWORD_BLOCK_8_OWORDS<<8)
#define SEND_BINDING_TABLE	(255<<0)
// No write commit
#define WRITE_DESC1_XXX SEND_BINDING_TABLE | SEND_BLOCK_SIZE1 | SEND_TYPE_WRITE | SEND_HEADER_PRESENT | SEND_MLEN_2
#define WRITE_DESC1_WC SEND_BINDING_TABLE | SEND_BLOCK_SIZE1 | SEND_TYPE_WRITE | SEND_HEADER_PRESENT | SEND_MLEN_2 | SEND_WRITE_COMMIT
#define WRITE_DESC2 SEND_BINDING_TABLE | SEND_BLOCK_SIZE2 | SEND_TYPE_WRITE | SEND_HEADER_PRESENT | SEND_MLEN_3
#define WRITE_DESC4 SEND_BINDING_TABLE | SEND_BLOCK_SIZE4 | SEND_TYPE_WRITE | SEND_HEADER_PRESENT | SEND_MLEN_5
#define RECV_DESC1 SEND_BINDING_TABLE | SEND_BLOCK_SIZE1 | SEND_TYPE_READ | SEND_HEADER_PRESENT | SEND_MLEN_1 | SEND_RLEN_1
//#define SEND_DESC1 0x40902FF
#define SEND_DESC1_WC 0x40b02FF

/* ex_desc field 6.3.66.2 */
#define SEND_DP_RENDER_CACHE	(5<<0)
#define SEND_EOT		(1<<5)
#define SEND_EX_DESC SEND_DP_RENDER_CACHE

/**
 * WRITE_SCRATCH1 - Write 2 owords.
 * cdst.2 - offset
 * cdst.5 - per thread scratch base, relative to gsba??
 * cdst+1 - data to be written.
 */
#define WRITE_SCRATCH1(cdst) \
	send (16) null cdst SEND_EX_DESC WRITE_DESC1_XXX FLAGS
#define WRITE_SCRATCH1_WC(cdst) \
	send (16) g1 cdst SEND_EX_DESC WRITE_DESC1_WC FLAGS
#define WRITE_SCRATCH2(cdst) \
	send (16) null cdst SEND_EX_DESC WRITE_DESC2 FLAGS
#define WRITE_SCRATCH4(cdst) \
	send (16) null cdst SEND_EX_DESC WRITE_DESC4 FLAGS

/**
 * READ_SCRATCH1 - Read 2 owords.
 * cdst.2 - offset
 * cdst.5 - per thread scratch base, relative to gsba??
 * grf - register where read data is populated.
 */
#define READ_SCRATCH1(grf, cdst) \
	send (16) grf:ud cdst SEND_EX_DESC RECV_DESC1 FLAGS

/**
 * SET_OFFSET - setup mrf for the given offset prior to a send instruction.
 * mrf - message register to be used as the header.
 * offset - offset.
 *
 * If a WRITE_SCRATCH follows, mrf+1 -> mrf+1+n should contain the data to be
 * written.
 */
#define SET_OFFSET(mrf, offset) \
	mov (1) mrf.5:ud g0.5:ud FLAGS; \
	mov (1) mrf.2:ud offset:ud FLAGS

/**
 * SAVE_CRF - save the control register
 */
#define CR_OFFSET 0x40
#define SAVE_CRF \
	SET_OFFSET(m0, CR_OFFSET); \
	mov (8) m1:ud 0xdeadbeef:ud FLAGS; \
	mov (1) m1.0:ud cr0.0 FLAGS; \
	mov (1) m1.1:ud cr0.1 FLAGS; \
	mov (1) m1.2:ud cr0.2 FLAGS; \
	mov (1) m1.3:ud sr0:ud FLAGS; \
	WRITE_SCRATCH1(m0)

#define STORE_GRF(grf, offset) \
	SET_OFFSET(m0, offset); \
	mov (8) m1:ud grf:ud FLAGS; \
	WRITE_SCRATCH1(m0)

/*
 * clobbers: m0.2, m0.5
 */
#define LOAD_GRF(grf, offset) \
	SET_OFFSET(m0, offset); \
	READ_SCRATCH1(grf, m0)

/*
 * clobbers: mrf.2 mrf.5
 */
#define SAVE_MRF(mrf, offset) \
	SET_OFFSET(mrf, offset); \
	WRITE_SCRATCH1(mrf)

/*
 * non-quirky semantics, unlike SAVE_MRF
 * clobbers: g1
 */
#define RESTORE_MRF(mrf, offset) \
	LOAD_GRF(g1, offset); \
	mov (8) mrf:ud g1:ud FLAGS

#define SAVE_ALL_MRF \
	/* m1 is saved already */ \
	SAVE_MRF(m1, 0x2); \
	SAVE_MRF(m2, 0x4); \
	SAVE_MRF(m3, 0x6); \
	SAVE_MRF(m4, 0x8); \
	SAVE_MRF(m5, 0xa); \
	SAVE_MRF(m6, 0xc); \
	SAVE_MRF(m7, 0xe); \
	SAVE_MRF(m8, 0x10); \
	SAVE_MRF(m9, 0x12); \
	SAVE_MRF(m10, 0x14); \
	SAVE_MRF(m11, 0x16); \
	SAVE_MRF(m12, 0x18); \
	SAVE_MRF(m13, 0x1a); \
	SAVE_MRF(m14, 0x1c)

#define RESTORE_ALL_MRF \
	RESTORE_MRF(m15, 0x1c); \
	RESTORE_MRF(m14, 0x1a); \
	RESTORE_MRF(m13, 0x18); \
	RESTORE_MRF(m12, 0x16); \
	RESTORE_MRF(m11, 0x14); \
	RESTORE_MRF(m10, 0x12); \
	RESTORE_MRF(m9, 0x10); \
	RESTORE_MRF(m8, 0xe); \
	RESTORE_MRF(m7, 0xc); \
	RESTORE_MRF(m6, 0xa); \
	RESTORE_MRF(m5, 0x8); \
	RESTORE_MRF(m4, 0x6); \
	RESTORE_MRF(m3, 0x4); \
	RESTORE_MRF(m2, 0x2); \
	RESTORE_MRF(m1, 0x0)

#ifndef SANDYBRIDGE
	#error Only SandyBridge is supported
#endif

/* Default flags for an instruction */
#define FLAGS { ALIGN1, SWITCH, MASK_DISABLE, ACCWRCTRL}

/*
 * We can clobber m0, and g0.4, everything else must be saved.
 */
Enter:
	nop;

	or (1) cr0.0 cr0.0 CR0_0_ENTRY_UNMASK:ud FLAGS;

	/*
	 * g0.5 has the per thread scratch space when running in FS or VS.
	 * If we don't have a valid g0.5, we can calculate a per thread scratch offset
	 * using the system registers. The problem is we do not have a good way to know
	 * the offset from GSBA. The system routine will have to be hardcoded or
	 * dynamically patched with the correct offset.
	 * TID is in sr0.0[2:0]
	 * EUID is in sr0.0[11:8]
	 */

	mov (1) g0.4:ud 0:ud				FLAGS;

#ifdef GPGPU
#if 0
	/* This should work according to the docs, the add blows up */
	shr (1) g0.8:uw sr0.0:uw 5			FLAGS;
	add (1) g0.16:ub gr0.16:ub sr0.0:ub		FLAGS;
#else
	shr (1) g0.8:uw sr0.0:uw 5			FLAGS;
	mov (1) g0.9:uw sr0.0:uw			FLAGS;
	and (1) g0.9:uw g0.9:uw 0x7:uw			FLAGS;
	add (1) g0.8:uw g0.8:uw g0.9:uw			FLAGS;
	mov (1) g0.9:uw 0:uw				FLAGS;
	mul (1) g0.4:ud g0.4:ud PER_THREAD_QWORDS	FLAGS;
#endif
#endif


	/* Saves must occur in order so as not to clobber the next register */
	SAVE_MRF(m0, 0);
	STORE_GRF(g0, 0x20);
	STORE_GRF(g1, 0x22);
	SAVE_ALL_MRF;

	mov (8) g1:ud STATE_EU_MSG:ud FLAGS;
	STORE_GRF(g1, STATE_QWORD);

	mov (8) g1:ud DEBUG_PROTOCOL_VERSION:ud FLAGS;
	STORE_GRF(g1, COMMUNICATION_QWORD);

	SAVE_CRF;

	mov (8) g1:ud 0xb00bb00b:ud FLAGS;
	EVICT_CACHE;

	wait n1:ud;


	mov (8) g1:ud 0xb00bb00b:ud FLAGS;
	EVICT_CACHE;

	/* Using this to try to keep coherency */
	LOAD_GRF(g1, CR_OFFSET);
	LOAD_GRF(g1, COMMUNICATION_QWORD);
	LOAD_GRF(g1, STATE_QWORD);

	// read state offset to see what to do

	RESTORE_ALL_MRF;
	LOAD_GRF(g1, 0x22);
	LOAD_GRF(g0, 0x20);

	/* Clear breakpoint status */
	and (1) cr0.1 cr0.1 CR0_1_ENTRY_UNMASK:ud FLAGS;

	/* set breakpoint suppress this should be conditional on bes */
	or  (1) cr0.0 cr0.0 CR0_0_BP_SUPPRESS:ud FLAGS;

	and (1) cr0.0 cr0.0 CR0_0_RETURN_MASK:ud FLAGS;
	nop;