vc4: Fix decision for whether the MIN operation writes to the B regfile.
[mesa.git] / src / gallium / drivers / vc4 / kernel / vc4_validate_shaders.c
1 /*
2 * Copyright © 2014 Broadcom
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 /**
25 * DOC: Shader validator for VC4.
26 *
27 * The VC4 has no IOMMU between it and system memory. So, a user with access
28 * to execute shaders could escalate privilege by overwriting system memory
29 * (using the VPM write address register in the general-purpose DMA mode) or
30 * reading system memory it shouldn't (reading it as a texture, or uniform
31 * data, or vertex data).
32 *
33 * This walks over a shader starting from some offset within a BO, ensuring
34 * that its accesses are appropriately bounded, and recording how many texture
35 * accesses are made and where so that we can do relocations for them in the
36 * uniform stream.
37 *
38 * The kernel API has shaders stored in user-mapped BOs. The BOs will be
39 * forcibly unmapped from the process before validation, and any cache of
40 * validated state will be flushed if the mapping is faulted back in.
41 *
42 * Storing the shaders in BOs means that the validation process will be slow
43 * due to uncached reads, but since shaders are long-lived and shader BOs are
44 * never actually modified, this shouldn't be a problem.
45 */
46
47 #include "vc4_drv.h"
48 #include "vc4_qpu.h"
49 #include "vc4_qpu_defines.h"
50
51 struct vc4_shader_validation_state {
52 struct vc4_texture_sample_info tmu_setup[2];
53 int tmu_write_count[2];
54
55 /* For registers that were last written to by a MIN instruction with
56 * one argument being a uniform, the address of the uniform.
57 * Otherwise, ~0.
58 *
59 * This is used for the validation of direct address memory reads.
60 */
61 uint32_t live_clamp_offsets[32 + 32 + 4];
62 };
63
64 static uint32_t
65 waddr_to_live_reg_index(uint32_t waddr, bool is_b)
66 {
67 if (waddr < 32) {
68 if (is_b)
69 return 32 + waddr;
70 else
71 return waddr;
72 } else if (waddr <= QPU_W_ACC3) {
73
74 return 64 + waddr - QPU_W_ACC0;
75 } else {
76 return ~0;
77 }
78 }
79
80 static bool
81 is_tmu_submit(uint32_t waddr)
82 {
83 return (waddr == QPU_W_TMU0_S ||
84 waddr == QPU_W_TMU1_S);
85 }
86
87 static bool
88 is_tmu_write(uint32_t waddr)
89 {
90 return (waddr >= QPU_W_TMU0_S &&
91 waddr <= QPU_W_TMU1_B);
92 }
93
94 static bool
95 record_validated_texture_sample(struct vc4_validated_shader_info *validated_shader,
96 struct vc4_shader_validation_state *validation_state,
97 int tmu)
98 {
99 uint32_t s = validated_shader->num_texture_samples;
100 int i;
101 struct vc4_texture_sample_info *temp_samples;
102
103 temp_samples = krealloc(validated_shader->texture_samples,
104 (s + 1) * sizeof(*temp_samples),
105 GFP_KERNEL);
106 if (!temp_samples)
107 return false;
108
109 memcpy(&temp_samples[s],
110 &validation_state->tmu_setup[tmu],
111 sizeof(*temp_samples));
112
113 validated_shader->num_texture_samples = s + 1;
114 validated_shader->texture_samples = temp_samples;
115
116 for (i = 0; i < 4; i++)
117 validation_state->tmu_setup[tmu].p_offset[i] = ~0;
118
119 return true;
120 }
121
122 static bool
123 check_tmu_write(uint64_t inst,
124 struct vc4_validated_shader_info *validated_shader,
125 struct vc4_shader_validation_state *validation_state,
126 bool is_mul)
127 {
128 uint32_t waddr = (is_mul ?
129 QPU_GET_FIELD(inst, QPU_WADDR_MUL) :
130 QPU_GET_FIELD(inst, QPU_WADDR_ADD));
131 uint32_t raddr_a = QPU_GET_FIELD(inst, QPU_RADDR_A);
132 uint32_t raddr_b = QPU_GET_FIELD(inst, QPU_RADDR_B);
133 int tmu = waddr > QPU_W_TMU0_B;
134 bool submit = is_tmu_submit(waddr);
135 bool is_direct = submit && validation_state->tmu_write_count[tmu] == 0;
136
137 if (is_direct) {
138 uint32_t add_a = QPU_GET_FIELD(inst, QPU_ADD_A);
139 uint32_t add_b = QPU_GET_FIELD(inst, QPU_ADD_B);
140 uint32_t clamp_offset = ~0;
141
142 /* Make sure that this texture load is an add of the base
143 * address of the UBO to a clamped offset within the UBO.
144 */
145 if (is_mul ||
146 QPU_GET_FIELD(inst, QPU_OP_ADD) != QPU_A_ADD) {
147 DRM_ERROR("direct TMU load wasn't an add\n");
148 return false;
149 }
150
151 /* We assert that the the clamped address is the first
152 * argument, and the UBO base address is the second argument.
153 * This is arbitrary, but simpler than supporting flipping the
154 * two either way.
155 */
156 if (add_a == QPU_MUX_A) {
157 clamp_offset = validation_state->live_clamp_offsets[raddr_a];
158 } else if (add_a == QPU_MUX_B) {
159 clamp_offset = validation_state->live_clamp_offsets[32 + raddr_b];
160 } else if (add_a <= QPU_MUX_R4) {
161 clamp_offset = validation_state->live_clamp_offsets[64 + add_a];
162 }
163
164 if (clamp_offset == ~0) {
165 DRM_ERROR("direct TMU load wasn't clamped\n");
166 return false;
167 }
168
169 /* Store the clamp value's offset in p1 (see reloc_tex() in
170 * vc4_validate.c).
171 */
172 validation_state->tmu_setup[tmu].p_offset[1] =
173 clamp_offset;
174
175 if (!(add_b == QPU_MUX_A && raddr_a == QPU_R_UNIF) &&
176 !(add_b == QPU_MUX_B && raddr_b == QPU_R_UNIF)) {
177 DRM_ERROR("direct TMU load didn't add to a uniform\n");
178 return false;
179 }
180
181 validation_state->tmu_setup[tmu].is_direct = true;
182 } else {
183 if (raddr_a == QPU_R_UNIF || raddr_b == QPU_R_UNIF) {
184 DRM_ERROR("uniform read in the same instruction as "
185 "texture setup.\n");
186 return false;
187 }
188 }
189
190 if (validation_state->tmu_write_count[tmu] >= 4) {
191 DRM_ERROR("TMU%d got too many parameters before dispatch\n",
192 tmu);
193 return false;
194 }
195 validation_state->tmu_setup[tmu].p_offset[validation_state->tmu_write_count[tmu]] =
196 validated_shader->uniforms_size;
197 validation_state->tmu_write_count[tmu]++;
198 /* Since direct uses a RADDR uniform reference, it will get counted in
199 * check_instruction_reads()
200 */
201 if (!is_direct)
202 validated_shader->uniforms_size += 4;
203
204 if (submit) {
205 if (!record_validated_texture_sample(validated_shader,
206 validation_state, tmu)) {
207 return false;
208 }
209
210 validation_state->tmu_write_count[tmu] = 0;
211 }
212
213 return true;
214 }
215
216 static bool
217 check_register_write(uint64_t inst,
218 struct vc4_validated_shader_info *validated_shader,
219 struct vc4_shader_validation_state *validation_state,
220 bool is_mul)
221 {
222 uint32_t waddr = (is_mul ?
223 QPU_GET_FIELD(inst, QPU_WADDR_MUL) :
224 QPU_GET_FIELD(inst, QPU_WADDR_ADD));
225 bool is_b = is_mul != ((inst & QPU_WS) != 0);
226 uint32_t live_reg_index;
227
228 switch (waddr) {
229 case QPU_W_UNIFORMS_ADDRESS:
230 /* XXX: We'll probably need to support this for reladdr, but
231 * it's definitely a security-related one.
232 */
233 DRM_ERROR("uniforms address load unsupported\n");
234 return false;
235
236 case QPU_W_TLB_COLOR_MS:
237 case QPU_W_TLB_COLOR_ALL:
238 case QPU_W_TLB_Z:
239 /* These only interact with the tile buffer, not main memory,
240 * so they're safe.
241 */
242 return true;
243
244 case QPU_W_TMU0_S:
245 case QPU_W_TMU0_T:
246 case QPU_W_TMU0_R:
247 case QPU_W_TMU0_B:
248 case QPU_W_TMU1_S:
249 case QPU_W_TMU1_T:
250 case QPU_W_TMU1_R:
251 case QPU_W_TMU1_B:
252 return check_tmu_write(inst, validated_shader, validation_state,
253 is_mul);
254
255 case QPU_W_HOST_INT:
256 case QPU_W_TMU_NOSWAP:
257 case QPU_W_TLB_ALPHA_MASK:
258 case QPU_W_MUTEX_RELEASE:
259 /* XXX: I haven't thought about these, so don't support them
260 * for now.
261 */
262 DRM_ERROR("Unsupported waddr %d\n", waddr);
263 return false;
264
265 case QPU_W_VPM_ADDR:
266 DRM_ERROR("General VPM DMA unsupported\n");
267 return false;
268
269 case QPU_W_VPM:
270 case QPU_W_VPMVCD_SETUP:
271 /* We allow VPM setup in general, even including VPM DMA
272 * configuration setup, because the (unsafe) DMA can only be
273 * triggered by QPU_W_VPM_ADDR writes.
274 */
275 return true;
276
277 case QPU_W_TLB_STENCIL_SETUP:
278 return true;
279 }
280
281 /* Clear out the live offset clamp tracking for the written register.
282 * If this particular instruction is setting up an offset clamp, it'll
283 * get tracked immediately after we return.
284 */
285 live_reg_index = waddr_to_live_reg_index(waddr, is_b);
286 if (live_reg_index != ~0)
287 validation_state->live_clamp_offsets[live_reg_index] = ~0;
288
289 return true;
290 }
291
292 static void
293 track_live_clamps(uint64_t inst,
294 struct vc4_validated_shader_info *validated_shader,
295 struct vc4_shader_validation_state *validation_state)
296 {
297 uint32_t waddr_add = QPU_GET_FIELD(inst, QPU_WADDR_ADD);
298 uint32_t add_b = QPU_GET_FIELD(inst, QPU_ADD_B);
299 uint32_t raddr_a = QPU_GET_FIELD(inst, QPU_RADDR_A);
300 uint32_t raddr_b = QPU_GET_FIELD(inst, QPU_RADDR_B);
301 bool is_b = inst & QPU_WS;
302 uint32_t live_reg_index;
303
304 if (QPU_GET_FIELD(inst, QPU_OP_ADD) != QPU_A_MIN)
305 return;
306
307 if (!(add_b == QPU_MUX_A && raddr_a == QPU_R_UNIF) &&
308 !(add_b == QPU_MUX_B && raddr_b == QPU_R_UNIF)) {
309 return;
310 }
311
312 live_reg_index = waddr_to_live_reg_index(waddr_add, is_b);
313 if (live_reg_index != ~0) {
314 validation_state->live_clamp_offsets[live_reg_index] =
315 validated_shader->uniforms_size;
316 }
317 }
318
319 static bool
320 check_instruction_writes(uint64_t inst,
321 struct vc4_validated_shader_info *validated_shader,
322 struct vc4_shader_validation_state *validation_state)
323 {
324 uint32_t waddr_add = QPU_GET_FIELD(inst, QPU_WADDR_ADD);
325 uint32_t waddr_mul = QPU_GET_FIELD(inst, QPU_WADDR_MUL);
326 bool ok;
327
328 if (is_tmu_write(waddr_add) && is_tmu_write(waddr_mul)) {
329 DRM_ERROR("ADD and MUL both set up textures\n");
330 return false;
331 }
332
333 ok = (check_register_write(inst, validated_shader, validation_state, false) &&
334 check_register_write(inst, validated_shader, validation_state, true));
335
336 track_live_clamps(inst, validated_shader, validation_state);
337
338 return ok;
339 }
340
341 static bool
342 check_instruction_reads(uint64_t inst,
343 struct vc4_validated_shader_info *validated_shader)
344 {
345 uint32_t raddr_a = QPU_GET_FIELD(inst, QPU_RADDR_A);
346 uint32_t raddr_b = QPU_GET_FIELD(inst, QPU_RADDR_B);
347
348 if (raddr_a == QPU_R_UNIF ||
349 raddr_b == QPU_R_UNIF) {
350 /* This can't overflow the uint32_t, because we're reading 8
351 * bytes of instruction to increment by 4 here, so we'd
352 * already be OOM.
353 */
354 validated_shader->uniforms_size += 4;
355 }
356
357 return true;
358 }
359
360 struct vc4_validated_shader_info *
361 vc4_validate_shader(struct drm_gem_cma_object *shader_obj,
362 uint32_t start_offset)
363 {
364 bool found_shader_end = false;
365 int shader_end_ip = 0;
366 uint32_t ip, max_ip;
367 uint64_t *shader;
368 struct vc4_validated_shader_info *validated_shader;
369 struct vc4_shader_validation_state validation_state;
370 int i;
371
372 memset(&validation_state, 0, sizeof(validation_state));
373
374 for (i = 0; i < 8; i++)
375 validation_state.tmu_setup[i / 4].p_offset[i % 4] = ~0;
376 for (i = 0; i < ARRAY_SIZE(validation_state.live_clamp_offsets); i++)
377 validation_state.live_clamp_offsets[i] = ~0;
378
379 if (start_offset + sizeof(uint64_t) > shader_obj->base.size) {
380 DRM_ERROR("shader starting at %d outside of BO sized %d\n",
381 start_offset,
382 shader_obj->base.size);
383 return NULL;
384 }
385 shader = shader_obj->vaddr + start_offset;
386 max_ip = (shader_obj->base.size - start_offset) / sizeof(uint64_t);
387
388 validated_shader = kcalloc(sizeof(*validated_shader), 1, GFP_KERNEL);
389 if (!validated_shader)
390 return NULL;
391
392 for (ip = 0; ip < max_ip; ip++) {
393 uint64_t inst = shader[ip];
394 uint32_t sig = QPU_GET_FIELD(inst, QPU_SIG);
395
396 switch (sig) {
397 case QPU_SIG_NONE:
398 case QPU_SIG_WAIT_FOR_SCOREBOARD:
399 case QPU_SIG_SCOREBOARD_UNLOCK:
400 case QPU_SIG_COLOR_LOAD:
401 case QPU_SIG_LOAD_TMU0:
402 case QPU_SIG_LOAD_TMU1:
403 case QPU_SIG_PROG_END:
404 if (!check_instruction_writes(inst, validated_shader,
405 &validation_state)) {
406 DRM_ERROR("Bad write at ip %d\n", ip);
407 goto fail;
408 }
409
410 if (!check_instruction_reads(inst, validated_shader))
411 goto fail;
412
413 if (sig == QPU_SIG_PROG_END) {
414 found_shader_end = true;
415 shader_end_ip = ip;
416 }
417
418 break;
419
420 case QPU_SIG_LOAD_IMM:
421 if (!check_instruction_writes(inst, validated_shader,
422 &validation_state)) {
423 DRM_ERROR("Bad LOAD_IMM write at ip %d\n", ip);
424 goto fail;
425 }
426 break;
427
428 default:
429 DRM_ERROR("Unsupported QPU signal %d at "
430 "instruction %d\n", sig, ip);
431 goto fail;
432 }
433
434 /* There are two delay slots after program end is signaled
435 * that are still executed, then we're finished.
436 */
437 if (found_shader_end && ip == shader_end_ip + 2)
438 break;
439 }
440
441 if (ip == max_ip) {
442 DRM_ERROR("shader starting at %d failed to terminate before "
443 "shader BO end at %d\n",
444 start_offset,
445 shader_obj->base.size);
446 goto fail;
447 }
448
449 /* Again, no chance of integer overflow here because the worst case
450 * scenario is 8 bytes of uniforms plus handles per 8-byte
451 * instruction.
452 */
453 validated_shader->uniforms_src_size =
454 (validated_shader->uniforms_size +
455 4 * validated_shader->num_texture_samples);
456
457 return validated_shader;
458
459 fail:
460 kfree(validated_shader);
461 return NULL;
462 }