2 * Copyright © 2014 Broadcom
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:
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
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
25 * DOC: Shader validator for VC4.
27 * The VC4 has no IOMMU between it and system memory, so a user with
28 * access to execute shaders could escalate privilege by overwriting
29 * system memory (using the VPM write address register in the
30 * general-purpose DMA mode) or reading system memory it shouldn't
31 * (reading it as a texture, or uniform data, or vertex data).
33 * This walks over a shader BO, ensuring that its accesses are
34 * appropriately bounded, and recording how many texture accesses are
35 * made and where so that we can do relocations for them in the
40 #include "vc4_qpu_defines.h"
42 struct vc4_shader_validation_state
{
43 struct vc4_texture_sample_info tmu_setup
[2];
44 int tmu_write_count
[2];
46 /* For registers that were last written to by a MIN instruction with
47 * one argument being a uniform, the address of the uniform.
50 * This is used for the validation of direct address memory reads.
52 uint32_t live_min_clamp_offsets
[32 + 32 + 4];
53 bool live_max_clamp_regs
[32 + 32 + 4];
57 waddr_to_live_reg_index(uint32_t waddr
, bool is_b
)
64 } else if (waddr
<= QPU_W_ACC3
) {
65 return 64 + waddr
- QPU_W_ACC0
;
72 raddr_add_a_to_live_reg_index(uint64_t inst
)
74 uint32_t sig
= QPU_GET_FIELD(inst
, QPU_SIG
);
75 uint32_t add_a
= QPU_GET_FIELD(inst
, QPU_ADD_A
);
76 uint32_t raddr_a
= QPU_GET_FIELD(inst
, QPU_RADDR_A
);
77 uint32_t raddr_b
= QPU_GET_FIELD(inst
, QPU_RADDR_B
);
79 if (add_a
== QPU_MUX_A
)
81 else if (add_a
== QPU_MUX_B
&& sig
!= QPU_SIG_SMALL_IMM
)
83 else if (add_a
<= QPU_MUX_R3
)
90 is_tmu_submit(uint32_t waddr
)
92 return (waddr
== QPU_W_TMU0_S
||
93 waddr
== QPU_W_TMU1_S
);
97 is_tmu_write(uint32_t waddr
)
99 return (waddr
>= QPU_W_TMU0_S
&&
100 waddr
<= QPU_W_TMU1_B
);
104 record_texture_sample(struct vc4_validated_shader_info
*validated_shader
,
105 struct vc4_shader_validation_state
*validation_state
,
108 uint32_t s
= validated_shader
->num_texture_samples
;
110 struct vc4_texture_sample_info
*temp_samples
;
112 temp_samples
= krealloc(validated_shader
->texture_samples
,
113 (s
+ 1) * sizeof(*temp_samples
),
118 memcpy(&temp_samples
[s
],
119 &validation_state
->tmu_setup
[tmu
],
120 sizeof(*temp_samples
));
122 validated_shader
->num_texture_samples
= s
+ 1;
123 validated_shader
->texture_samples
= temp_samples
;
125 for (i
= 0; i
< 4; i
++)
126 validation_state
->tmu_setup
[tmu
].p_offset
[i
] = ~0;
132 check_tmu_write(uint64_t inst
,
133 struct vc4_validated_shader_info
*validated_shader
,
134 struct vc4_shader_validation_state
*validation_state
,
137 uint32_t waddr
= (is_mul
?
138 QPU_GET_FIELD(inst
, QPU_WADDR_MUL
) :
139 QPU_GET_FIELD(inst
, QPU_WADDR_ADD
));
140 uint32_t raddr_a
= QPU_GET_FIELD(inst
, QPU_RADDR_A
);
141 uint32_t raddr_b
= QPU_GET_FIELD(inst
, QPU_RADDR_B
);
142 int tmu
= waddr
> QPU_W_TMU0_B
;
143 bool submit
= is_tmu_submit(waddr
);
144 bool is_direct
= submit
&& validation_state
->tmu_write_count
[tmu
] == 0;
145 uint32_t sig
= QPU_GET_FIELD(inst
, QPU_SIG
);
148 uint32_t add_b
= QPU_GET_FIELD(inst
, QPU_ADD_B
);
149 uint32_t clamp_reg
, clamp_offset
;
151 if (sig
== QPU_SIG_SMALL_IMM
) {
152 DRM_ERROR("direct TMU read used small immediate\n");
156 /* Make sure that this texture load is an add of the base
157 * address of the UBO to a clamped offset within the UBO.
160 QPU_GET_FIELD(inst
, QPU_OP_ADD
) != QPU_A_ADD
) {
161 DRM_ERROR("direct TMU load wasn't an add\n");
165 /* We assert that the the clamped address is the first
166 * argument, and the UBO base address is the second argument.
167 * This is arbitrary, but simpler than supporting flipping the
170 clamp_reg
= raddr_add_a_to_live_reg_index(inst
);
171 if (clamp_reg
== ~0) {
172 DRM_ERROR("direct TMU load wasn't clamped\n");
176 clamp_offset
= validation_state
->live_min_clamp_offsets
[clamp_reg
];
177 if (clamp_offset
== ~0) {
178 DRM_ERROR("direct TMU load wasn't clamped\n");
182 /* Store the clamp value's offset in p1 (see reloc_tex() in
185 validation_state
->tmu_setup
[tmu
].p_offset
[1] =
188 if (!(add_b
== QPU_MUX_A
&& raddr_a
== QPU_R_UNIF
) &&
189 !(add_b
== QPU_MUX_B
&& raddr_b
== QPU_R_UNIF
)) {
190 DRM_ERROR("direct TMU load didn't add to a uniform\n");
194 validation_state
->tmu_setup
[tmu
].is_direct
= true;
196 if (raddr_a
== QPU_R_UNIF
|| (sig
!= QPU_SIG_SMALL_IMM
&&
197 raddr_b
== QPU_R_UNIF
)) {
198 DRM_ERROR("uniform read in the same instruction as "
204 if (validation_state
->tmu_write_count
[tmu
] >= 4) {
205 DRM_ERROR("TMU%d got too many parameters before dispatch\n",
209 validation_state
->tmu_setup
[tmu
].p_offset
[validation_state
->tmu_write_count
[tmu
]] =
210 validated_shader
->uniforms_size
;
211 validation_state
->tmu_write_count
[tmu
]++;
212 /* Since direct uses a RADDR uniform reference, it will get counted in
213 * check_instruction_reads()
216 validated_shader
->uniforms_size
+= 4;
219 if (!record_texture_sample(validated_shader
,
220 validation_state
, tmu
)) {
224 validation_state
->tmu_write_count
[tmu
] = 0;
231 check_reg_write(uint64_t inst
,
232 struct vc4_validated_shader_info
*validated_shader
,
233 struct vc4_shader_validation_state
*validation_state
,
236 uint32_t waddr
= (is_mul
?
237 QPU_GET_FIELD(inst
, QPU_WADDR_MUL
) :
238 QPU_GET_FIELD(inst
, QPU_WADDR_ADD
));
241 case QPU_W_UNIFORMS_ADDRESS
:
242 /* XXX: We'll probably need to support this for reladdr, but
243 * it's definitely a security-related one.
245 DRM_ERROR("uniforms address load unsupported\n");
248 case QPU_W_TLB_COLOR_MS
:
249 case QPU_W_TLB_COLOR_ALL
:
251 /* These only interact with the tile buffer, not main memory,
264 return check_tmu_write(inst
, validated_shader
, validation_state
,
268 case QPU_W_TMU_NOSWAP
:
269 case QPU_W_TLB_ALPHA_MASK
:
270 case QPU_W_MUTEX_RELEASE
:
271 /* XXX: I haven't thought about these, so don't support them
274 DRM_ERROR("Unsupported waddr %d\n", waddr
);
278 DRM_ERROR("General VPM DMA unsupported\n");
282 case QPU_W_VPMVCD_SETUP
:
283 /* We allow VPM setup in general, even including VPM DMA
284 * configuration setup, because the (unsafe) DMA can only be
285 * triggered by QPU_W_VPM_ADDR writes.
289 case QPU_W_TLB_STENCIL_SETUP
:
297 track_live_clamps(uint64_t inst
,
298 struct vc4_validated_shader_info
*validated_shader
,
299 struct vc4_shader_validation_state
*validation_state
)
301 uint32_t op_add
= QPU_GET_FIELD(inst
, QPU_OP_ADD
);
302 uint32_t waddr_add
= QPU_GET_FIELD(inst
, QPU_WADDR_ADD
);
303 uint32_t waddr_mul
= QPU_GET_FIELD(inst
, QPU_WADDR_MUL
);
304 uint32_t cond_add
= QPU_GET_FIELD(inst
, QPU_COND_ADD
);
305 uint32_t add_a
= QPU_GET_FIELD(inst
, QPU_ADD_A
);
306 uint32_t add_b
= QPU_GET_FIELD(inst
, QPU_ADD_B
);
307 uint32_t raddr_a
= QPU_GET_FIELD(inst
, QPU_RADDR_A
);
308 uint32_t raddr_b
= QPU_GET_FIELD(inst
, QPU_RADDR_B
);
309 uint32_t sig
= QPU_GET_FIELD(inst
, QPU_SIG
);
310 bool ws
= inst
& QPU_WS
;
311 uint32_t lri_add_a
, lri_add
, lri_mul
;
314 /* Check whether OP_ADD's A argumennt comes from a live MAX(x, 0),
315 * before we clear previous live state.
317 lri_add_a
= raddr_add_a_to_live_reg_index(inst
);
318 add_a_is_min_0
= (lri_add_a
!= ~0 &&
319 validation_state
->live_max_clamp_regs
[lri_add_a
]);
321 /* Clear live state for registers written by our instruction. */
322 lri_add
= waddr_to_live_reg_index(waddr_add
, ws
);
323 lri_mul
= waddr_to_live_reg_index(waddr_mul
, !ws
);
325 validation_state
->live_max_clamp_regs
[lri_mul
] = false;
326 validation_state
->live_min_clamp_offsets
[lri_mul
] = ~0;
329 validation_state
->live_max_clamp_regs
[lri_add
] = false;
330 validation_state
->live_min_clamp_offsets
[lri_add
] = ~0;
332 /* Nothing further to do for live tracking, since only ADDs
333 * generate new live clamp registers.
338 /* Now, handle remaining live clamp tracking for the ADD operation. */
340 if (cond_add
!= QPU_COND_ALWAYS
)
343 if (op_add
== QPU_A_MAX
) {
344 /* Track live clamps of a value to a minimum of 0 (in either
347 if (sig
!= QPU_SIG_SMALL_IMM
|| raddr_b
!= 0 ||
348 (add_a
!= QPU_MUX_B
&& add_b
!= QPU_MUX_B
)) {
352 validation_state
->live_max_clamp_regs
[lri_add
] = true;
353 } else if (op_add
== QPU_A_MIN
) {
354 /* Track live clamps of a value clamped to a minimum of 0 and
355 * a maximum of some uniform's offset.
360 if (!(add_b
== QPU_MUX_A
&& raddr_a
== QPU_R_UNIF
) &&
361 !(add_b
== QPU_MUX_B
&& raddr_b
== QPU_R_UNIF
&&
362 sig
!= QPU_SIG_SMALL_IMM
)) {
366 validation_state
->live_min_clamp_offsets
[lri_add
] =
367 validated_shader
->uniforms_size
;
372 check_instruction_writes(uint64_t inst
,
373 struct vc4_validated_shader_info
*validated_shader
,
374 struct vc4_shader_validation_state
*validation_state
)
376 uint32_t waddr_add
= QPU_GET_FIELD(inst
, QPU_WADDR_ADD
);
377 uint32_t waddr_mul
= QPU_GET_FIELD(inst
, QPU_WADDR_MUL
);
380 if (is_tmu_write(waddr_add
) && is_tmu_write(waddr_mul
)) {
381 DRM_ERROR("ADD and MUL both set up textures\n");
385 ok
= (check_reg_write(inst
, validated_shader
, validation_state
,
387 check_reg_write(inst
, validated_shader
, validation_state
,
390 track_live_clamps(inst
, validated_shader
, validation_state
);
396 check_instruction_reads(uint64_t inst
,
397 struct vc4_validated_shader_info
*validated_shader
)
399 uint32_t raddr_a
= QPU_GET_FIELD(inst
, QPU_RADDR_A
);
400 uint32_t raddr_b
= QPU_GET_FIELD(inst
, QPU_RADDR_B
);
401 uint32_t sig
= QPU_GET_FIELD(inst
, QPU_SIG
);
403 if (raddr_a
== QPU_R_UNIF
||
404 (raddr_b
== QPU_R_UNIF
&& sig
!= QPU_SIG_SMALL_IMM
)) {
405 /* This can't overflow the uint32_t, because we're reading 8
406 * bytes of instruction to increment by 4 here, so we'd
409 validated_shader
->uniforms_size
+= 4;
415 struct vc4_validated_shader_info
*
416 vc4_validate_shader(struct drm_gem_cma_object
*shader_obj
)
418 bool found_shader_end
= false;
419 int shader_end_ip
= 0;
422 struct vc4_validated_shader_info
*validated_shader
;
423 struct vc4_shader_validation_state validation_state
;
426 memset(&validation_state
, 0, sizeof(validation_state
));
428 for (i
= 0; i
< 8; i
++)
429 validation_state
.tmu_setup
[i
/ 4].p_offset
[i
% 4] = ~0;
430 for (i
= 0; i
< ARRAY_SIZE(validation_state
.live_min_clamp_offsets
); i
++)
431 validation_state
.live_min_clamp_offsets
[i
] = ~0;
433 shader
= shader_obj
->vaddr
;
434 max_ip
= shader_obj
->base
.size
/ sizeof(uint64_t);
436 validated_shader
= kcalloc(1, sizeof(*validated_shader
), GFP_KERNEL
);
437 if (!validated_shader
)
440 for (ip
= 0; ip
< max_ip
; ip
++) {
441 uint64_t inst
= shader
[ip
];
442 uint32_t sig
= QPU_GET_FIELD(inst
, QPU_SIG
);
446 case QPU_SIG_WAIT_FOR_SCOREBOARD
:
447 case QPU_SIG_SCOREBOARD_UNLOCK
:
448 case QPU_SIG_COLOR_LOAD
:
449 case QPU_SIG_LOAD_TMU0
:
450 case QPU_SIG_LOAD_TMU1
:
451 case QPU_SIG_PROG_END
:
452 case QPU_SIG_SMALL_IMM
:
453 if (!check_instruction_writes(inst
, validated_shader
,
454 &validation_state
)) {
455 DRM_ERROR("Bad write at ip %d\n", ip
);
459 if (!check_instruction_reads(inst
, validated_shader
))
462 if (sig
== QPU_SIG_PROG_END
) {
463 found_shader_end
= true;
469 case QPU_SIG_LOAD_IMM
:
470 if (!check_instruction_writes(inst
, validated_shader
,
471 &validation_state
)) {
472 DRM_ERROR("Bad LOAD_IMM write at ip %d\n", ip
);
478 DRM_ERROR("Unsupported QPU signal %d at "
479 "instruction %d\n", sig
, ip
);
483 /* There are two delay slots after program end is signaled
484 * that are still executed, then we're finished.
486 if (found_shader_end
&& ip
== shader_end_ip
+ 2)
491 DRM_ERROR("shader failed to terminate before "
492 "shader BO end at %zd\n",
493 shader_obj
->base
.size
);
497 /* Again, no chance of integer overflow here because the worst case
498 * scenario is 8 bytes of uniforms plus handles per 8-byte
501 validated_shader
->uniforms_src_size
=
502 (validated_shader
->uniforms_size
+
503 4 * validated_shader
->num_texture_samples
);
505 return validated_shader
;
508 if (validated_shader
) {
509 kfree(validated_shader
->texture_samples
);
510 kfree(validated_shader
);