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 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).
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
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.
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.
49 #include "vc4_qpu_defines.h"
51 struct vc4_shader_validation_state
{
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_clamp_offsets
[32 + 32 + 4];
65 waddr_to_live_reg_index(uint32_t waddr
, bool is_b
)
72 } 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 add_a
= QPU_GET_FIELD(inst
, QPU_ADD_A
);
84 uint32_t raddr_a
= QPU_GET_FIELD(inst
, QPU_RADDR_A
);
85 uint32_t raddr_b
= QPU_GET_FIELD(inst
, QPU_RADDR_B
);
87 if (add_a
== QPU_MUX_A
) {
89 } else if (add_a
== QPU_MUX_B
) {
91 } else if (add_a
<= QPU_MUX_R4
) {
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_validated_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(uint64_t inst
,
142 struct vc4_validated_shader_info
*validated_shader
,
143 struct vc4_shader_validation_state
*validation_state
,
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 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_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_validated_texture_sample(validated_shader
,
229 validation_state
, tmu
)) {
233 validation_state
->tmu_write_count
[tmu
] = 0;
240 check_register_write(uint64_t inst
,
241 struct vc4_validated_shader_info
*validated_shader
,
242 struct vc4_shader_validation_state
*validation_state
,
245 uint32_t waddr
= (is_mul
?
246 QPU_GET_FIELD(inst
, QPU_WADDR_MUL
) :
247 QPU_GET_FIELD(inst
, QPU_WADDR_ADD
));
248 bool is_b
= is_mul
!= ((inst
& QPU_WS
) != 0);
249 uint32_t live_reg_index
;
252 case QPU_W_UNIFORMS_ADDRESS
:
253 /* XXX: We'll probably need to support this for reladdr, but
254 * it's definitely a security-related one.
256 DRM_ERROR("uniforms address load unsupported\n");
259 case QPU_W_TLB_COLOR_MS
:
260 case QPU_W_TLB_COLOR_ALL
:
262 /* These only interact with the tile buffer, not main memory,
275 return check_tmu_write(inst
, validated_shader
, validation_state
,
279 case QPU_W_TMU_NOSWAP
:
280 case QPU_W_TLB_ALPHA_MASK
:
281 case QPU_W_MUTEX_RELEASE
:
282 /* XXX: I haven't thought about these, so don't support them
285 DRM_ERROR("Unsupported waddr %d\n", waddr
);
289 DRM_ERROR("General VPM DMA unsupported\n");
293 case QPU_W_VPMVCD_SETUP
:
294 /* We allow VPM setup in general, even including VPM DMA
295 * configuration setup, because the (unsafe) DMA can only be
296 * triggered by QPU_W_VPM_ADDR writes.
300 case QPU_W_TLB_STENCIL_SETUP
:
304 /* Clear out the live offset clamp tracking for the written register.
305 * If this particular instruction is setting up an offset clamp, it'll
306 * get tracked immediately after we return.
308 live_reg_index
= waddr_to_live_reg_index(waddr
, is_b
);
309 if (live_reg_index
!= ~0)
310 validation_state
->live_clamp_offsets
[live_reg_index
] = ~0;
316 track_live_clamps(uint64_t inst
,
317 struct vc4_validated_shader_info
*validated_shader
,
318 struct vc4_shader_validation_state
*validation_state
)
320 uint32_t waddr_add
= QPU_GET_FIELD(inst
, QPU_WADDR_ADD
);
321 uint32_t add_b
= QPU_GET_FIELD(inst
, QPU_ADD_B
);
322 uint32_t raddr_a
= QPU_GET_FIELD(inst
, QPU_RADDR_A
);
323 uint32_t raddr_b
= QPU_GET_FIELD(inst
, QPU_RADDR_B
);
324 uint32_t sig
= QPU_GET_FIELD(inst
, QPU_SIG
);
325 bool is_b
= inst
& QPU_WS
;
326 uint32_t live_reg_index
;
328 if (QPU_GET_FIELD(inst
, QPU_OP_ADD
) != QPU_A_MIN
)
331 if (!(add_b
== QPU_MUX_A
&& raddr_a
== QPU_R_UNIF
) &&
332 !(add_b
== QPU_MUX_B
&& raddr_b
== QPU_R_UNIF
&&
333 sig
!= QPU_SIG_SMALL_IMM
)) {
337 live_reg_index
= waddr_to_live_reg_index(waddr_add
, is_b
);
338 if (live_reg_index
!= ~0) {
339 validation_state
->live_clamp_offsets
[live_reg_index
] =
340 validated_shader
->uniforms_size
;
345 check_instruction_writes(uint64_t inst
,
346 struct vc4_validated_shader_info
*validated_shader
,
347 struct vc4_shader_validation_state
*validation_state
)
349 uint32_t waddr_add
= QPU_GET_FIELD(inst
, QPU_WADDR_ADD
);
350 uint32_t waddr_mul
= QPU_GET_FIELD(inst
, QPU_WADDR_MUL
);
353 if (is_tmu_write(waddr_add
) && is_tmu_write(waddr_mul
)) {
354 DRM_ERROR("ADD and MUL both set up textures\n");
358 ok
= (check_register_write(inst
, validated_shader
, validation_state
, false) &&
359 check_register_write(inst
, validated_shader
, validation_state
, true));
361 track_live_clamps(inst
, validated_shader
, validation_state
);
367 check_instruction_reads(uint64_t inst
,
368 struct vc4_validated_shader_info
*validated_shader
)
370 uint32_t raddr_a
= QPU_GET_FIELD(inst
, QPU_RADDR_A
);
371 uint32_t raddr_b
= QPU_GET_FIELD(inst
, QPU_RADDR_B
);
372 uint32_t sig
= QPU_GET_FIELD(inst
, QPU_SIG
);
374 if (raddr_a
== QPU_R_UNIF
||
375 (raddr_b
== QPU_R_UNIF
&& sig
!= QPU_SIG_SMALL_IMM
)) {
376 /* This can't overflow the uint32_t, because we're reading 8
377 * bytes of instruction to increment by 4 here, so we'd
380 validated_shader
->uniforms_size
+= 4;
386 struct vc4_validated_shader_info
*
387 vc4_validate_shader(struct drm_gem_cma_object
*shader_obj
)
389 bool found_shader_end
= false;
390 int shader_end_ip
= 0;
393 struct vc4_validated_shader_info
*validated_shader
;
394 struct vc4_shader_validation_state validation_state
;
397 memset(&validation_state
, 0, sizeof(validation_state
));
399 for (i
= 0; i
< 8; i
++)
400 validation_state
.tmu_setup
[i
/ 4].p_offset
[i
% 4] = ~0;
401 for (i
= 0; i
< ARRAY_SIZE(validation_state
.live_clamp_offsets
); i
++)
402 validation_state
.live_clamp_offsets
[i
] = ~0;
404 shader
= shader_obj
->vaddr
;
405 max_ip
= shader_obj
->base
.size
/ sizeof(uint64_t);
407 validated_shader
= kcalloc(sizeof(*validated_shader
), 1, GFP_KERNEL
);
408 if (!validated_shader
)
411 for (ip
= 0; ip
< max_ip
; ip
++) {
412 uint64_t inst
= shader
[ip
];
413 uint32_t sig
= QPU_GET_FIELD(inst
, QPU_SIG
);
417 case QPU_SIG_WAIT_FOR_SCOREBOARD
:
418 case QPU_SIG_SCOREBOARD_UNLOCK
:
419 case QPU_SIG_COLOR_LOAD
:
420 case QPU_SIG_LOAD_TMU0
:
421 case QPU_SIG_LOAD_TMU1
:
422 case QPU_SIG_PROG_END
:
423 case QPU_SIG_SMALL_IMM
:
424 if (!check_instruction_writes(inst
, validated_shader
,
425 &validation_state
)) {
426 DRM_ERROR("Bad write at ip %d\n", ip
);
430 if (!check_instruction_reads(inst
, validated_shader
))
433 if (sig
== QPU_SIG_PROG_END
) {
434 found_shader_end
= true;
440 case QPU_SIG_LOAD_IMM
:
441 if (!check_instruction_writes(inst
, validated_shader
,
442 &validation_state
)) {
443 DRM_ERROR("Bad LOAD_IMM write at ip %d\n", ip
);
449 DRM_ERROR("Unsupported QPU signal %d at "
450 "instruction %d\n", sig
, ip
);
454 /* There are two delay slots after program end is signaled
455 * that are still executed, then we're finished.
457 if (found_shader_end
&& ip
== shader_end_ip
+ 2)
462 DRM_ERROR("shader failed to terminate before "
463 "shader BO end at %d\n",
464 shader_obj
->base
.size
);
468 /* Again, no chance of integer overflow here because the worst case
469 * scenario is 8 bytes of uniforms plus handles per 8-byte
472 validated_shader
->uniforms_src_size
=
473 (validated_shader
->uniforms_size
+
474 4 * validated_shader
->num_texture_samples
);
476 return validated_shader
;
479 kfree(validated_shader
);