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
41 #include "vc4_qpu_defines.h"
43 struct vc4_shader_validation_state
{
44 /* Current IP being validated. */
47 /* IP at the end of the BO, do not read shader[max_ip] */
52 struct vc4_texture_sample_info tmu_setup
[2];
53 int tmu_write_count
[2];
55 /* For registers that were last written to by a MIN instruction with
56 * one argument being a uniform, the address of the uniform.
59 * This is used for the validation of direct address memory reads.
61 uint32_t live_min_clamp_offsets
[32 + 32 + 4];
62 bool live_max_clamp_regs
[32 + 32 + 4];
66 waddr_to_live_reg_index(uint32_t waddr
, bool is_b
)
73 } else if (waddr
<= QPU_W_ACC3
) {
74 return 64 + waddr
- QPU_W_ACC0
;
81 raddr_add_a_to_live_reg_index(uint64_t inst
)
83 uint32_t sig
= QPU_GET_FIELD(inst
, QPU_SIG
);
84 uint32_t add_a
= QPU_GET_FIELD(inst
, QPU_ADD_A
);
85 uint32_t raddr_a
= QPU_GET_FIELD(inst
, QPU_RADDR_A
);
86 uint32_t raddr_b
= QPU_GET_FIELD(inst
, QPU_RADDR_B
);
88 if (add_a
== QPU_MUX_A
)
90 else if (add_a
== QPU_MUX_B
&& sig
!= QPU_SIG_SMALL_IMM
)
92 else if (add_a
<= QPU_MUX_R3
)
99 is_tmu_submit(uint32_t waddr
)
101 return (waddr
== QPU_W_TMU0_S
||
102 waddr
== QPU_W_TMU1_S
);
106 is_tmu_write(uint32_t waddr
)
108 return (waddr
>= QPU_W_TMU0_S
&&
109 waddr
<= QPU_W_TMU1_B
);
113 record_texture_sample(struct vc4_validated_shader_info
*validated_shader
,
114 struct vc4_shader_validation_state
*validation_state
,
117 uint32_t s
= validated_shader
->num_texture_samples
;
119 struct vc4_texture_sample_info
*temp_samples
;
121 temp_samples
= krealloc(validated_shader
->texture_samples
,
122 (s
+ 1) * sizeof(*temp_samples
),
127 memcpy(&temp_samples
[s
],
128 &validation_state
->tmu_setup
[tmu
],
129 sizeof(*temp_samples
));
131 validated_shader
->num_texture_samples
= s
+ 1;
132 validated_shader
->texture_samples
= temp_samples
;
134 for (i
= 0; i
< 4; i
++)
135 validation_state
->tmu_setup
[tmu
].p_offset
[i
] = ~0;
141 check_tmu_write(struct vc4_validated_shader_info
*validated_shader
,
142 struct vc4_shader_validation_state
*validation_state
,
145 uint64_t inst
= validation_state
->shader
[validation_state
->ip
];
146 uint32_t waddr
= (is_mul
?
147 QPU_GET_FIELD(inst
, QPU_WADDR_MUL
) :
148 QPU_GET_FIELD(inst
, QPU_WADDR_ADD
));
149 uint32_t raddr_a
= QPU_GET_FIELD(inst
, QPU_RADDR_A
);
150 uint32_t raddr_b
= QPU_GET_FIELD(inst
, QPU_RADDR_B
);
151 int tmu
= waddr
> QPU_W_TMU0_B
;
152 bool submit
= is_tmu_submit(waddr
);
153 bool is_direct
= submit
&& validation_state
->tmu_write_count
[tmu
] == 0;
154 uint32_t sig
= QPU_GET_FIELD(inst
, QPU_SIG
);
157 uint32_t add_b
= QPU_GET_FIELD(inst
, QPU_ADD_B
);
158 uint32_t clamp_reg
, clamp_offset
;
160 if (sig
== QPU_SIG_SMALL_IMM
) {
161 DRM_ERROR("direct TMU read used small immediate\n");
165 /* Make sure that this texture load is an add of the base
166 * address of the UBO to a clamped offset within the UBO.
169 QPU_GET_FIELD(inst
, QPU_OP_ADD
) != QPU_A_ADD
) {
170 DRM_ERROR("direct TMU load wasn't an add\n");
174 /* We assert that the clamped address is the first
175 * argument, and the UBO base address is the second argument.
176 * This is arbitrary, but simpler than supporting flipping the
179 clamp_reg
= raddr_add_a_to_live_reg_index(inst
);
180 if (clamp_reg
== ~0) {
181 DRM_ERROR("direct TMU load wasn't clamped\n");
185 clamp_offset
= validation_state
->live_min_clamp_offsets
[clamp_reg
];
186 if (clamp_offset
== ~0) {
187 DRM_ERROR("direct TMU load wasn't clamped\n");
191 /* Store the clamp value's offset in p1 (see reloc_tex() in
194 validation_state
->tmu_setup
[tmu
].p_offset
[1] =
197 if (!(add_b
== QPU_MUX_A
&& raddr_a
== QPU_R_UNIF
) &&
198 !(add_b
== QPU_MUX_B
&& raddr_b
== QPU_R_UNIF
)) {
199 DRM_ERROR("direct TMU load didn't add to a uniform\n");
203 validation_state
->tmu_setup
[tmu
].is_direct
= true;
205 if (raddr_a
== QPU_R_UNIF
|| (sig
!= QPU_SIG_SMALL_IMM
&&
206 raddr_b
== QPU_R_UNIF
)) {
207 DRM_ERROR("uniform read in the same instruction as "
213 if (validation_state
->tmu_write_count
[tmu
] >= 4) {
214 DRM_ERROR("TMU%d got too many parameters before dispatch\n",
218 validation_state
->tmu_setup
[tmu
].p_offset
[validation_state
->tmu_write_count
[tmu
]] =
219 validated_shader
->uniforms_size
;
220 validation_state
->tmu_write_count
[tmu
]++;
221 /* Since direct uses a RADDR uniform reference, it will get counted in
222 * check_instruction_reads()
225 validated_shader
->uniforms_size
+= 4;
228 if (!record_texture_sample(validated_shader
,
229 validation_state
, tmu
)) {
233 validation_state
->tmu_write_count
[tmu
] = 0;
240 check_reg_write(struct vc4_validated_shader_info
*validated_shader
,
241 struct vc4_shader_validation_state
*validation_state
,
244 uint64_t inst
= validation_state
->shader
[validation_state
->ip
];
245 uint32_t waddr
= (is_mul
?
246 QPU_GET_FIELD(inst
, QPU_WADDR_MUL
) :
247 QPU_GET_FIELD(inst
, QPU_WADDR_ADD
));
250 case QPU_W_UNIFORMS_ADDRESS
:
251 /* XXX: We'll probably need to support this for reladdr, but
252 * it's definitely a security-related one.
254 DRM_ERROR("uniforms address load unsupported\n");
257 case QPU_W_TLB_COLOR_MS
:
258 case QPU_W_TLB_COLOR_ALL
:
260 /* These only interact with the tile buffer, not main memory,
273 return check_tmu_write(validated_shader
, validation_state
,
277 case QPU_W_TMU_NOSWAP
:
278 case QPU_W_TLB_ALPHA_MASK
:
279 case QPU_W_MUTEX_RELEASE
:
280 /* XXX: I haven't thought about these, so don't support them
283 DRM_ERROR("Unsupported waddr %d\n", waddr
);
287 DRM_ERROR("General VPM DMA unsupported\n");
291 case QPU_W_VPMVCD_SETUP
:
292 /* We allow VPM setup in general, even including VPM DMA
293 * configuration setup, because the (unsafe) DMA can only be
294 * triggered by QPU_W_VPM_ADDR writes.
298 case QPU_W_TLB_STENCIL_SETUP
:
306 track_live_clamps(struct vc4_validated_shader_info
*validated_shader
,
307 struct vc4_shader_validation_state
*validation_state
)
309 uint64_t inst
= validation_state
->shader
[validation_state
->ip
];
310 uint32_t op_add
= QPU_GET_FIELD(inst
, QPU_OP_ADD
);
311 uint32_t waddr_add
= QPU_GET_FIELD(inst
, QPU_WADDR_ADD
);
312 uint32_t waddr_mul
= QPU_GET_FIELD(inst
, QPU_WADDR_MUL
);
313 uint32_t cond_add
= QPU_GET_FIELD(inst
, QPU_COND_ADD
);
314 uint32_t add_a
= QPU_GET_FIELD(inst
, QPU_ADD_A
);
315 uint32_t add_b
= QPU_GET_FIELD(inst
, QPU_ADD_B
);
316 uint32_t raddr_a
= QPU_GET_FIELD(inst
, QPU_RADDR_A
);
317 uint32_t raddr_b
= QPU_GET_FIELD(inst
, QPU_RADDR_B
);
318 uint32_t sig
= QPU_GET_FIELD(inst
, QPU_SIG
);
319 bool ws
= inst
& QPU_WS
;
320 uint32_t lri_add_a
, lri_add
, lri_mul
;
323 /* Check whether OP_ADD's A argumennt comes from a live MAX(x, 0),
324 * before we clear previous live state.
326 lri_add_a
= raddr_add_a_to_live_reg_index(inst
);
327 add_a_is_min_0
= (lri_add_a
!= ~0 &&
328 validation_state
->live_max_clamp_regs
[lri_add_a
]);
330 /* Clear live state for registers written by our instruction. */
331 lri_add
= waddr_to_live_reg_index(waddr_add
, ws
);
332 lri_mul
= waddr_to_live_reg_index(waddr_mul
, !ws
);
334 validation_state
->live_max_clamp_regs
[lri_mul
] = false;
335 validation_state
->live_min_clamp_offsets
[lri_mul
] = ~0;
338 validation_state
->live_max_clamp_regs
[lri_add
] = false;
339 validation_state
->live_min_clamp_offsets
[lri_add
] = ~0;
341 /* Nothing further to do for live tracking, since only ADDs
342 * generate new live clamp registers.
347 /* Now, handle remaining live clamp tracking for the ADD operation. */
349 if (cond_add
!= QPU_COND_ALWAYS
)
352 if (op_add
== QPU_A_MAX
) {
353 /* Track live clamps of a value to a minimum of 0 (in either
356 if (sig
!= QPU_SIG_SMALL_IMM
|| raddr_b
!= 0 ||
357 (add_a
!= QPU_MUX_B
&& add_b
!= QPU_MUX_B
)) {
361 validation_state
->live_max_clamp_regs
[lri_add
] = true;
362 } else if (op_add
== QPU_A_MIN
) {
363 /* Track live clamps of a value clamped to a minimum of 0 and
364 * a maximum of some uniform's offset.
369 if (!(add_b
== QPU_MUX_A
&& raddr_a
== QPU_R_UNIF
) &&
370 !(add_b
== QPU_MUX_B
&& raddr_b
== QPU_R_UNIF
&&
371 sig
!= QPU_SIG_SMALL_IMM
)) {
375 validation_state
->live_min_clamp_offsets
[lri_add
] =
376 validated_shader
->uniforms_size
;
381 check_instruction_writes(struct vc4_validated_shader_info
*validated_shader
,
382 struct vc4_shader_validation_state
*validation_state
)
384 uint64_t inst
= validation_state
->shader
[validation_state
->ip
];
385 uint32_t waddr_add
= QPU_GET_FIELD(inst
, QPU_WADDR_ADD
);
386 uint32_t waddr_mul
= QPU_GET_FIELD(inst
, QPU_WADDR_MUL
);
389 if (is_tmu_write(waddr_add
) && is_tmu_write(waddr_mul
)) {
390 DRM_ERROR("ADD and MUL both set up textures\n");
394 ok
= (check_reg_write(validated_shader
, validation_state
, false) &&
395 check_reg_write(validated_shader
, validation_state
, true));
397 track_live_clamps(validated_shader
, validation_state
);
403 check_instruction_reads(uint64_t inst
,
404 struct vc4_validated_shader_info
*validated_shader
)
406 uint32_t raddr_a
= QPU_GET_FIELD(inst
, QPU_RADDR_A
);
407 uint32_t raddr_b
= QPU_GET_FIELD(inst
, QPU_RADDR_B
);
408 uint32_t sig
= QPU_GET_FIELD(inst
, QPU_SIG
);
410 if (raddr_a
== QPU_R_UNIF
||
411 (raddr_b
== QPU_R_UNIF
&& sig
!= QPU_SIG_SMALL_IMM
)) {
412 /* This can't overflow the uint32_t, because we're reading 8
413 * bytes of instruction to increment by 4 here, so we'd
416 validated_shader
->uniforms_size
+= 4;
422 struct vc4_validated_shader_info
*
423 vc4_validate_shader(struct drm_gem_cma_object
*shader_obj
)
425 bool found_shader_end
= false;
426 int shader_end_ip
= 0;
428 struct vc4_validated_shader_info
*validated_shader
;
429 struct vc4_shader_validation_state validation_state
;
432 memset(&validation_state
, 0, sizeof(validation_state
));
433 validation_state
.shader
= shader_obj
->vaddr
;
434 validation_state
.max_ip
= shader_obj
->base
.size
/ sizeof(uint64_t);
436 for (i
= 0; i
< 8; i
++)
437 validation_state
.tmu_setup
[i
/ 4].p_offset
[i
% 4] = ~0;
438 for (i
= 0; i
< ARRAY_SIZE(validation_state
.live_min_clamp_offsets
); i
++)
439 validation_state
.live_min_clamp_offsets
[i
] = ~0;
441 validated_shader
= kcalloc(1, sizeof(*validated_shader
), GFP_KERNEL
);
442 if (!validated_shader
)
445 for (ip
= 0; ip
< validation_state
.max_ip
; ip
++) {
446 uint64_t inst
= validation_state
.shader
[ip
];
447 uint32_t sig
= QPU_GET_FIELD(inst
, QPU_SIG
);
449 validation_state
.ip
= ip
;
453 case QPU_SIG_WAIT_FOR_SCOREBOARD
:
454 case QPU_SIG_SCOREBOARD_UNLOCK
:
455 case QPU_SIG_COLOR_LOAD
:
456 case QPU_SIG_LOAD_TMU0
:
457 case QPU_SIG_LOAD_TMU1
:
458 case QPU_SIG_PROG_END
:
459 case QPU_SIG_SMALL_IMM
:
460 if (!check_instruction_writes(validated_shader
,
461 &validation_state
)) {
462 DRM_ERROR("Bad write at ip %d\n", ip
);
466 if (!check_instruction_reads(inst
, validated_shader
))
469 if (sig
== QPU_SIG_PROG_END
) {
470 found_shader_end
= true;
476 case QPU_SIG_LOAD_IMM
:
477 if (!check_instruction_writes(validated_shader
,
478 &validation_state
)) {
479 DRM_ERROR("Bad LOAD_IMM write at ip %d\n", ip
);
485 DRM_ERROR("Unsupported QPU signal %d at "
486 "instruction %d\n", sig
, ip
);
490 /* There are two delay slots after program end is signaled
491 * that are still executed, then we're finished.
493 if (found_shader_end
&& ip
== shader_end_ip
+ 2)
497 if (ip
== validation_state
.max_ip
) {
498 DRM_ERROR("shader failed to terminate before "
499 "shader BO end at %zd\n",
500 shader_obj
->base
.size
);
504 /* Again, no chance of integer overflow here because the worst case
505 * scenario is 8 bytes of uniforms plus handles per 8-byte
508 validated_shader
->uniforms_src_size
=
509 (validated_shader
->uniforms_size
+
510 4 * validated_shader
->num_texture_samples
);
512 return validated_shader
;
515 if (validated_shader
) {
516 kfree(validated_shader
->texture_samples
);
517 kfree(validated_shader
);