vc4_validate_shaders.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  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. * DOC: Shader validator for VC4.
  25. *
  26. * The VC4 has no IOMMU between it and system memory, so a user with
  27. * access to execute shaders could escalate privilege by overwriting
  28. * system memory (using the VPM write address register in the
  29. * general-purpose DMA mode) or reading system memory it shouldn't
  30. * (reading it as a texture, or uniform data, or vertex data).
  31. *
  32. * This walks over a shader BO, ensuring that its accesses are
  33. * appropriately bounded, and recording how many texture accesses are
  34. * made and where so that we can do relocations for them in the
  35. * uniform stream.
  36. */
  37. #include "vc4_drv.h"
  38. #include "vc4_qpu_defines.h"
  39. #define LIVE_REG_COUNT (32 + 32 + 4)
  40. struct vc4_shader_validation_state {
  41. /* Current IP being validated. */
  42. uint32_t ip;
  43. /* IP at the end of the BO, do not read shader[max_ip] */
  44. uint32_t max_ip;
  45. uint64_t *shader;
  46. struct vc4_texture_sample_info tmu_setup[2];
  47. int tmu_write_count[2];
  48. /* For registers that were last written to by a MIN instruction with
  49. * one argument being a uniform, the address of the uniform.
  50. * Otherwise, ~0.
  51. *
  52. * This is used for the validation of direct address memory reads.
  53. */
  54. uint32_t live_min_clamp_offsets[LIVE_REG_COUNT];
  55. bool live_max_clamp_regs[LIVE_REG_COUNT];
  56. uint32_t live_immediates[LIVE_REG_COUNT];
  57. /* Bitfield of which IPs are used as branch targets.
  58. *
  59. * Used for validation that the uniform stream is updated at the right
  60. * points and clearing the texturing/clamping state.
  61. */
  62. unsigned long *branch_targets;
  63. /* Set when entering a basic block, and cleared when the uniform
  64. * address update is found. This is used to make sure that we don't
  65. * read uniforms when the address is undefined.
  66. */
  67. bool needs_uniform_address_update;
  68. /* Set when we find a backwards branch. If the branch is backwards,
  69. * the taraget is probably doing an address reset to read uniforms,
  70. * and so we need to be sure that a uniforms address is present in the
  71. * stream, even if the shader didn't need to read uniforms in later
  72. * basic blocks.
  73. */
  74. bool needs_uniform_address_for_loop;
  75. };
  76. static uint32_t
  77. waddr_to_live_reg_index(uint32_t waddr, bool is_b)
  78. {
  79. if (waddr < 32) {
  80. if (is_b)
  81. return 32 + waddr;
  82. else
  83. return waddr;
  84. } else if (waddr <= QPU_W_ACC3) {
  85. return 64 + waddr - QPU_W_ACC0;
  86. } else {
  87. return ~0;
  88. }
  89. }
  90. static uint32_t
  91. raddr_add_a_to_live_reg_index(uint64_t inst)
  92. {
  93. uint32_t sig = QPU_GET_FIELD(inst, QPU_SIG);
  94. uint32_t add_a = QPU_GET_FIELD(inst, QPU_ADD_A);
  95. uint32_t raddr_a = QPU_GET_FIELD(inst, QPU_RADDR_A);
  96. uint32_t raddr_b = QPU_GET_FIELD(inst, QPU_RADDR_B);
  97. if (add_a == QPU_MUX_A)
  98. return raddr_a;
  99. else if (add_a == QPU_MUX_B && sig != QPU_SIG_SMALL_IMM)
  100. return 32 + raddr_b;
  101. else if (add_a <= QPU_MUX_R3)
  102. return 64 + add_a;
  103. else
  104. return ~0;
  105. }
  106. static bool
  107. is_tmu_submit(uint32_t waddr)
  108. {
  109. return (waddr == QPU_W_TMU0_S ||
  110. waddr == QPU_W_TMU1_S);
  111. }
  112. static bool
  113. is_tmu_write(uint32_t waddr)
  114. {
  115. return (waddr >= QPU_W_TMU0_S &&
  116. waddr <= QPU_W_TMU1_B);
  117. }
  118. static bool
  119. record_texture_sample(struct vc4_validated_shader_info *validated_shader,
  120. struct vc4_shader_validation_state *validation_state,
  121. int tmu)
  122. {
  123. uint32_t s = validated_shader->num_texture_samples;
  124. int i;
  125. struct vc4_texture_sample_info *temp_samples;
  126. temp_samples = krealloc(validated_shader->texture_samples,
  127. (s + 1) * sizeof(*temp_samples),
  128. GFP_KERNEL);
  129. if (!temp_samples)
  130. return false;
  131. memcpy(&temp_samples[s],
  132. &validation_state->tmu_setup[tmu],
  133. sizeof(*temp_samples));
  134. validated_shader->num_texture_samples = s + 1;
  135. validated_shader->texture_samples = temp_samples;
  136. for (i = 0; i < 4; i++)
  137. validation_state->tmu_setup[tmu].p_offset[i] = ~0;
  138. return true;
  139. }
  140. static bool
  141. check_tmu_write(struct vc4_validated_shader_info *validated_shader,
  142. struct vc4_shader_validation_state *validation_state,
  143. bool is_mul)
  144. {
  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);
  155. if (is_direct) {
  156. uint32_t add_b = QPU_GET_FIELD(inst, QPU_ADD_B);
  157. uint32_t clamp_reg, clamp_offset;
  158. if (sig == QPU_SIG_SMALL_IMM) {
  159. DRM_ERROR("direct TMU read used small immediate\n");
  160. return false;
  161. }
  162. /* Make sure that this texture load is an add of the base
  163. * address of the UBO to a clamped offset within the UBO.
  164. */
  165. if (is_mul ||
  166. QPU_GET_FIELD(inst, QPU_OP_ADD) != QPU_A_ADD) {
  167. DRM_ERROR("direct TMU load wasn't an add\n");
  168. return false;
  169. }
  170. /* We assert that the clamped address is the first
  171. * argument, and the UBO base address is the second argument.
  172. * This is arbitrary, but simpler than supporting flipping the
  173. * two either way.
  174. */
  175. clamp_reg = raddr_add_a_to_live_reg_index(inst);
  176. if (clamp_reg == ~0) {
  177. DRM_ERROR("direct TMU load wasn't clamped\n");
  178. return false;
  179. }
  180. clamp_offset = validation_state->live_min_clamp_offsets[clamp_reg];
  181. if (clamp_offset == ~0) {
  182. DRM_ERROR("direct TMU load wasn't clamped\n");
  183. return false;
  184. }
  185. /* Store the clamp value's offset in p1 (see reloc_tex() in
  186. * vc4_validate.c).
  187. */
  188. validation_state->tmu_setup[tmu].p_offset[1] =
  189. clamp_offset;
  190. if (!(add_b == QPU_MUX_A && raddr_a == QPU_R_UNIF) &&
  191. !(add_b == QPU_MUX_B && raddr_b == QPU_R_UNIF)) {
  192. DRM_ERROR("direct TMU load didn't add to a uniform\n");
  193. return false;
  194. }
  195. validation_state->tmu_setup[tmu].is_direct = true;
  196. } else {
  197. if (raddr_a == QPU_R_UNIF || (sig != QPU_SIG_SMALL_IMM &&
  198. raddr_b == QPU_R_UNIF)) {
  199. DRM_ERROR("uniform read in the same instruction as "
  200. "texture setup.\n");
  201. return false;
  202. }
  203. }
  204. if (validation_state->tmu_write_count[tmu] >= 4) {
  205. DRM_ERROR("TMU%d got too many parameters before dispatch\n",
  206. tmu);
  207. return false;
  208. }
  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()
  214. */
  215. if (!is_direct) {
  216. if (validation_state->needs_uniform_address_update) {
  217. DRM_ERROR("Texturing with undefined uniform address\n");
  218. return false;
  219. }
  220. validated_shader->uniforms_size += 4;
  221. }
  222. if (submit) {
  223. if (!record_texture_sample(validated_shader,
  224. validation_state, tmu)) {
  225. return false;
  226. }
  227. validation_state->tmu_write_count[tmu] = 0;
  228. }
  229. return true;
  230. }
  231. static bool require_uniform_address_uniform(struct vc4_validated_shader_info *validated_shader)
  232. {
  233. uint32_t o = validated_shader->num_uniform_addr_offsets;
  234. uint32_t num_uniforms = validated_shader->uniforms_size / 4;
  235. validated_shader->uniform_addr_offsets =
  236. krealloc(validated_shader->uniform_addr_offsets,
  237. (o + 1) *
  238. sizeof(*validated_shader->uniform_addr_offsets),
  239. GFP_KERNEL);
  240. if (!validated_shader->uniform_addr_offsets)
  241. return false;
  242. validated_shader->uniform_addr_offsets[o] = num_uniforms;
  243. validated_shader->num_uniform_addr_offsets++;
  244. return true;
  245. }
  246. static bool
  247. validate_uniform_address_write(struct vc4_validated_shader_info *validated_shader,
  248. struct vc4_shader_validation_state *validation_state,
  249. bool is_mul)
  250. {
  251. uint64_t inst = validation_state->shader[validation_state->ip];
  252. u32 add_b = QPU_GET_FIELD(inst, QPU_ADD_B);
  253. u32 raddr_a = QPU_GET_FIELD(inst, QPU_RADDR_A);
  254. u32 raddr_b = QPU_GET_FIELD(inst, QPU_RADDR_B);
  255. u32 add_lri = raddr_add_a_to_live_reg_index(inst);
  256. /* We want our reset to be pointing at whatever uniform follows the
  257. * uniforms base address.
  258. */
  259. u32 expected_offset = validated_shader->uniforms_size + 4;
  260. /* We only support absolute uniform address changes, and we
  261. * require that they be in the current basic block before any
  262. * of its uniform reads.
  263. *
  264. * One could potentially emit more efficient QPU code, by
  265. * noticing that (say) an if statement does uniform control
  266. * flow for all threads and that the if reads the same number
  267. * of uniforms on each side. However, this scheme is easy to
  268. * validate so it's all we allow for now.
  269. */
  270. if (QPU_GET_FIELD(inst, QPU_SIG) != QPU_SIG_NONE) {
  271. DRM_ERROR("uniforms address change must be "
  272. "normal math\n");
  273. return false;
  274. }
  275. if (is_mul || QPU_GET_FIELD(inst, QPU_OP_ADD) != QPU_A_ADD) {
  276. DRM_ERROR("Uniform address reset must be an ADD.\n");
  277. return false;
  278. }
  279. if (QPU_GET_FIELD(inst, QPU_COND_ADD) != QPU_COND_ALWAYS) {
  280. DRM_ERROR("Uniform address reset must be unconditional.\n");
  281. return false;
  282. }
  283. if (QPU_GET_FIELD(inst, QPU_PACK) != QPU_PACK_A_NOP &&
  284. !(inst & QPU_PM)) {
  285. DRM_ERROR("No packing allowed on uniforms reset\n");
  286. return false;
  287. }
  288. if (add_lri == -1) {
  289. DRM_ERROR("First argument of uniform address write must be "
  290. "an immediate value.\n");
  291. return false;
  292. }
  293. if (validation_state->live_immediates[add_lri] != expected_offset) {
  294. DRM_ERROR("Resetting uniforms with offset %db instead of %db\n",
  295. validation_state->live_immediates[add_lri],
  296. expected_offset);
  297. return false;
  298. }
  299. if (!(add_b == QPU_MUX_A && raddr_a == QPU_R_UNIF) &&
  300. !(add_b == QPU_MUX_B && raddr_b == QPU_R_UNIF)) {
  301. DRM_ERROR("Second argument of uniform address write must be "
  302. "a uniform.\n");
  303. return false;
  304. }
  305. validation_state->needs_uniform_address_update = false;
  306. validation_state->needs_uniform_address_for_loop = false;
  307. return require_uniform_address_uniform(validated_shader);
  308. }
  309. static bool
  310. check_reg_write(struct vc4_validated_shader_info *validated_shader,
  311. struct vc4_shader_validation_state *validation_state,
  312. bool is_mul)
  313. {
  314. uint64_t inst = validation_state->shader[validation_state->ip];
  315. uint32_t waddr = (is_mul ?
  316. QPU_GET_FIELD(inst, QPU_WADDR_MUL) :
  317. QPU_GET_FIELD(inst, QPU_WADDR_ADD));
  318. uint32_t sig = QPU_GET_FIELD(inst, QPU_SIG);
  319. bool ws = inst & QPU_WS;
  320. bool is_b = is_mul ^ ws;
  321. u32 lri = waddr_to_live_reg_index(waddr, is_b);
  322. if (lri != -1) {
  323. uint32_t cond_add = QPU_GET_FIELD(inst, QPU_COND_ADD);
  324. uint32_t cond_mul = QPU_GET_FIELD(inst, QPU_COND_MUL);
  325. if (sig == QPU_SIG_LOAD_IMM &&
  326. QPU_GET_FIELD(inst, QPU_PACK) == QPU_PACK_A_NOP &&
  327. ((is_mul && cond_mul == QPU_COND_ALWAYS) ||
  328. (!is_mul && cond_add == QPU_COND_ALWAYS))) {
  329. validation_state->live_immediates[lri] =
  330. QPU_GET_FIELD(inst, QPU_LOAD_IMM);
  331. } else {
  332. validation_state->live_immediates[lri] = ~0;
  333. }
  334. }
  335. switch (waddr) {
  336. case QPU_W_UNIFORMS_ADDRESS:
  337. if (is_b) {
  338. DRM_ERROR("relative uniforms address change "
  339. "unsupported\n");
  340. return false;
  341. }
  342. return validate_uniform_address_write(validated_shader,
  343. validation_state,
  344. is_mul);
  345. case QPU_W_TLB_COLOR_MS:
  346. case QPU_W_TLB_COLOR_ALL:
  347. case QPU_W_TLB_Z:
  348. /* These only interact with the tile buffer, not main memory,
  349. * so they're safe.
  350. */
  351. return true;
  352. case QPU_W_TMU0_S:
  353. case QPU_W_TMU0_T:
  354. case QPU_W_TMU0_R:
  355. case QPU_W_TMU0_B:
  356. case QPU_W_TMU1_S:
  357. case QPU_W_TMU1_T:
  358. case QPU_W_TMU1_R:
  359. case QPU_W_TMU1_B:
  360. return check_tmu_write(validated_shader, validation_state,
  361. is_mul);
  362. case QPU_W_HOST_INT:
  363. case QPU_W_TMU_NOSWAP:
  364. case QPU_W_TLB_ALPHA_MASK:
  365. case QPU_W_MUTEX_RELEASE:
  366. /* XXX: I haven't thought about these, so don't support them
  367. * for now.
  368. */
  369. DRM_ERROR("Unsupported waddr %d\n", waddr);
  370. return false;
  371. case QPU_W_VPM_ADDR:
  372. DRM_ERROR("General VPM DMA unsupported\n");
  373. return false;
  374. case QPU_W_VPM:
  375. case QPU_W_VPMVCD_SETUP:
  376. /* We allow VPM setup in general, even including VPM DMA
  377. * configuration setup, because the (unsafe) DMA can only be
  378. * triggered by QPU_W_VPM_ADDR writes.
  379. */
  380. return true;
  381. case QPU_W_TLB_STENCIL_SETUP:
  382. return true;
  383. }
  384. return true;
  385. }
  386. static void
  387. track_live_clamps(struct vc4_validated_shader_info *validated_shader,
  388. struct vc4_shader_validation_state *validation_state)
  389. {
  390. uint64_t inst = validation_state->shader[validation_state->ip];
  391. uint32_t op_add = QPU_GET_FIELD(inst, QPU_OP_ADD);
  392. uint32_t waddr_add = QPU_GET_FIELD(inst, QPU_WADDR_ADD);
  393. uint32_t waddr_mul = QPU_GET_FIELD(inst, QPU_WADDR_MUL);
  394. uint32_t cond_add = QPU_GET_FIELD(inst, QPU_COND_ADD);
  395. uint32_t add_a = QPU_GET_FIELD(inst, QPU_ADD_A);
  396. uint32_t add_b = QPU_GET_FIELD(inst, QPU_ADD_B);
  397. uint32_t raddr_a = QPU_GET_FIELD(inst, QPU_RADDR_A);
  398. uint32_t raddr_b = QPU_GET_FIELD(inst, QPU_RADDR_B);
  399. uint32_t sig = QPU_GET_FIELD(inst, QPU_SIG);
  400. bool ws = inst & QPU_WS;
  401. uint32_t lri_add_a, lri_add, lri_mul;
  402. bool add_a_is_min_0;
  403. /* Check whether OP_ADD's A argumennt comes from a live MAX(x, 0),
  404. * before we clear previous live state.
  405. */
  406. lri_add_a = raddr_add_a_to_live_reg_index(inst);
  407. add_a_is_min_0 = (lri_add_a != ~0 &&
  408. validation_state->live_max_clamp_regs[lri_add_a]);
  409. /* Clear live state for registers written by our instruction. */
  410. lri_add = waddr_to_live_reg_index(waddr_add, ws);
  411. lri_mul = waddr_to_live_reg_index(waddr_mul, !ws);
  412. if (lri_mul != ~0) {
  413. validation_state->live_max_clamp_regs[lri_mul] = false;
  414. validation_state->live_min_clamp_offsets[lri_mul] = ~0;
  415. }
  416. if (lri_add != ~0) {
  417. validation_state->live_max_clamp_regs[lri_add] = false;
  418. validation_state->live_min_clamp_offsets[lri_add] = ~0;
  419. } else {
  420. /* Nothing further to do for live tracking, since only ADDs
  421. * generate new live clamp registers.
  422. */
  423. return;
  424. }
  425. /* Now, handle remaining live clamp tracking for the ADD operation. */
  426. if (cond_add != QPU_COND_ALWAYS)
  427. return;
  428. if (op_add == QPU_A_MAX) {
  429. /* Track live clamps of a value to a minimum of 0 (in either
  430. * arg).
  431. */
  432. if (sig != QPU_SIG_SMALL_IMM || raddr_b != 0 ||
  433. (add_a != QPU_MUX_B && add_b != QPU_MUX_B)) {
  434. return;
  435. }
  436. validation_state->live_max_clamp_regs[lri_add] = true;
  437. } else if (op_add == QPU_A_MIN) {
  438. /* Track live clamps of a value clamped to a minimum of 0 and
  439. * a maximum of some uniform's offset.
  440. */
  441. if (!add_a_is_min_0)
  442. return;
  443. if (!(add_b == QPU_MUX_A && raddr_a == QPU_R_UNIF) &&
  444. !(add_b == QPU_MUX_B && raddr_b == QPU_R_UNIF &&
  445. sig != QPU_SIG_SMALL_IMM)) {
  446. return;
  447. }
  448. validation_state->live_min_clamp_offsets[lri_add] =
  449. validated_shader->uniforms_size;
  450. }
  451. }
  452. static bool
  453. check_instruction_writes(struct vc4_validated_shader_info *validated_shader,
  454. struct vc4_shader_validation_state *validation_state)
  455. {
  456. uint64_t inst = validation_state->shader[validation_state->ip];
  457. uint32_t waddr_add = QPU_GET_FIELD(inst, QPU_WADDR_ADD);
  458. uint32_t waddr_mul = QPU_GET_FIELD(inst, QPU_WADDR_MUL);
  459. bool ok;
  460. if (is_tmu_write(waddr_add) && is_tmu_write(waddr_mul)) {
  461. DRM_ERROR("ADD and MUL both set up textures\n");
  462. return false;
  463. }
  464. ok = (check_reg_write(validated_shader, validation_state, false) &&
  465. check_reg_write(validated_shader, validation_state, true));
  466. track_live_clamps(validated_shader, validation_state);
  467. return ok;
  468. }
  469. static bool
  470. check_branch(uint64_t inst,
  471. struct vc4_validated_shader_info *validated_shader,
  472. struct vc4_shader_validation_state *validation_state,
  473. int ip)
  474. {
  475. int32_t branch_imm = QPU_GET_FIELD(inst, QPU_BRANCH_TARGET);
  476. uint32_t waddr_add = QPU_GET_FIELD(inst, QPU_WADDR_ADD);
  477. uint32_t waddr_mul = QPU_GET_FIELD(inst, QPU_WADDR_MUL);
  478. if ((int)branch_imm < 0)
  479. validation_state->needs_uniform_address_for_loop = true;
  480. /* We don't want to have to worry about validation of this, and
  481. * there's no need for it.
  482. */
  483. if (waddr_add != QPU_W_NOP || waddr_mul != QPU_W_NOP) {
  484. DRM_ERROR("branch instruction at %d wrote a register.\n",
  485. validation_state->ip);
  486. return false;
  487. }
  488. return true;
  489. }
  490. static bool
  491. check_instruction_reads(struct vc4_validated_shader_info *validated_shader,
  492. struct vc4_shader_validation_state *validation_state)
  493. {
  494. uint64_t inst = validation_state->shader[validation_state->ip];
  495. uint32_t raddr_a = QPU_GET_FIELD(inst, QPU_RADDR_A);
  496. uint32_t raddr_b = QPU_GET_FIELD(inst, QPU_RADDR_B);
  497. uint32_t sig = QPU_GET_FIELD(inst, QPU_SIG);
  498. if (raddr_a == QPU_R_UNIF ||
  499. (raddr_b == QPU_R_UNIF && sig != QPU_SIG_SMALL_IMM)) {
  500. /* This can't overflow the uint32_t, because we're reading 8
  501. * bytes of instruction to increment by 4 here, so we'd
  502. * already be OOM.
  503. */
  504. validated_shader->uniforms_size += 4;
  505. if (validation_state->needs_uniform_address_update) {
  506. DRM_ERROR("Uniform read with undefined uniform "
  507. "address\n");
  508. return false;
  509. }
  510. }
  511. return true;
  512. }
  513. /* Make sure that all branches are absolute and point within the shader, and
  514. * note their targets for later.
  515. */
  516. static bool
  517. vc4_validate_branches(struct vc4_shader_validation_state *validation_state)
  518. {
  519. uint32_t max_branch_target = 0;
  520. bool found_shader_end = false;
  521. int ip;
  522. int shader_end_ip = 0;
  523. int last_branch = -2;
  524. for (ip = 0; ip < validation_state->max_ip; ip++) {
  525. uint64_t inst = validation_state->shader[ip];
  526. int32_t branch_imm = QPU_GET_FIELD(inst, QPU_BRANCH_TARGET);
  527. uint32_t sig = QPU_GET_FIELD(inst, QPU_SIG);
  528. uint32_t after_delay_ip = ip + 4;
  529. uint32_t branch_target_ip;
  530. if (sig == QPU_SIG_PROG_END) {
  531. shader_end_ip = ip;
  532. found_shader_end = true;
  533. continue;
  534. }
  535. if (sig != QPU_SIG_BRANCH)
  536. continue;
  537. if (ip - last_branch < 4) {
  538. DRM_ERROR("Branch at %d during delay slots\n", ip);
  539. return false;
  540. }
  541. last_branch = ip;
  542. if (inst & QPU_BRANCH_REG) {
  543. DRM_ERROR("branching from register relative "
  544. "not supported\n");
  545. return false;
  546. }
  547. if (!(inst & QPU_BRANCH_REL)) {
  548. DRM_ERROR("relative branching required\n");
  549. return false;
  550. }
  551. /* The actual branch target is the instruction after the delay
  552. * slots, plus whatever byte offset is in the low 32 bits of
  553. * the instruction. Make sure we're not branching beyond the
  554. * end of the shader object.
  555. */
  556. if (branch_imm % sizeof(inst) != 0) {
  557. DRM_ERROR("branch target not aligned\n");
  558. return false;
  559. }
  560. branch_target_ip = after_delay_ip + (branch_imm >> 3);
  561. if (branch_target_ip >= validation_state->max_ip) {
  562. DRM_ERROR("Branch at %d outside of shader (ip %d/%d)\n",
  563. ip, branch_target_ip,
  564. validation_state->max_ip);
  565. return false;
  566. }
  567. set_bit(branch_target_ip, validation_state->branch_targets);
  568. /* Make sure that the non-branching path is also not outside
  569. * the shader.
  570. */
  571. if (after_delay_ip >= validation_state->max_ip) {
  572. DRM_ERROR("Branch at %d continues past shader end "
  573. "(%d/%d)\n",
  574. ip, after_delay_ip, validation_state->max_ip);
  575. return false;
  576. }
  577. set_bit(after_delay_ip, validation_state->branch_targets);
  578. max_branch_target = max(max_branch_target, after_delay_ip);
  579. /* There are two delay slots after program end is signaled
  580. * that are still executed, then we're finished.
  581. */
  582. if (found_shader_end && ip == shader_end_ip + 2)
  583. break;
  584. }
  585. if (max_branch_target > shader_end_ip) {
  586. DRM_ERROR("Branch landed after QPU_SIG_PROG_END");
  587. return false;
  588. }
  589. return true;
  590. }
  591. /* Resets any known state for the shader, used when we may be branched to from
  592. * multiple locations in the program (or at shader start).
  593. */
  594. static void
  595. reset_validation_state(struct vc4_shader_validation_state *validation_state)
  596. {
  597. int i;
  598. for (i = 0; i < 8; i++)
  599. validation_state->tmu_setup[i / 4].p_offset[i % 4] = ~0;
  600. for (i = 0; i < LIVE_REG_COUNT; i++) {
  601. validation_state->live_min_clamp_offsets[i] = ~0;
  602. validation_state->live_max_clamp_regs[i] = false;
  603. validation_state->live_immediates[i] = ~0;
  604. }
  605. }
  606. static bool
  607. texturing_in_progress(struct vc4_shader_validation_state *validation_state)
  608. {
  609. return (validation_state->tmu_write_count[0] != 0 ||
  610. validation_state->tmu_write_count[1] != 0);
  611. }
  612. static bool
  613. vc4_handle_branch_target(struct vc4_shader_validation_state *validation_state)
  614. {
  615. uint32_t ip = validation_state->ip;
  616. if (!test_bit(ip, validation_state->branch_targets))
  617. return true;
  618. if (texturing_in_progress(validation_state)) {
  619. DRM_ERROR("Branch target landed during TMU setup\n");
  620. return false;
  621. }
  622. /* Reset our live values tracking, since this instruction may have
  623. * multiple predecessors.
  624. *
  625. * One could potentially do analysis to determine that, for
  626. * example, all predecessors have a live max clamp in the same
  627. * register, but we don't bother with that.
  628. */
  629. reset_validation_state(validation_state);
  630. /* Since we've entered a basic block from potentially multiple
  631. * predecessors, we need the uniforms address to be updated before any
  632. * unforms are read. We require that after any branch point, the next
  633. * uniform to be loaded is a uniform address offset. That uniform's
  634. * offset will be marked by the uniform address register write
  635. * validation, or a one-off the end-of-program check.
  636. */
  637. validation_state->needs_uniform_address_update = true;
  638. return true;
  639. }
  640. struct vc4_validated_shader_info *
  641. vc4_validate_shader(struct drm_gem_cma_object *shader_obj)
  642. {
  643. bool found_shader_end = false;
  644. int shader_end_ip = 0;
  645. uint32_t ip;
  646. struct vc4_validated_shader_info *validated_shader = NULL;
  647. struct vc4_shader_validation_state validation_state;
  648. memset(&validation_state, 0, sizeof(validation_state));
  649. validation_state.shader = shader_obj->vaddr;
  650. validation_state.max_ip = shader_obj->base.size / sizeof(uint64_t);
  651. reset_validation_state(&validation_state);
  652. validation_state.branch_targets =
  653. kcalloc(BITS_TO_LONGS(validation_state.max_ip),
  654. sizeof(unsigned long), GFP_KERNEL);
  655. if (!validation_state.branch_targets)
  656. goto fail;
  657. validated_shader = kcalloc(1, sizeof(*validated_shader), GFP_KERNEL);
  658. if (!validated_shader)
  659. goto fail;
  660. if (!vc4_validate_branches(&validation_state))
  661. goto fail;
  662. for (ip = 0; ip < validation_state.max_ip; ip++) {
  663. uint64_t inst = validation_state.shader[ip];
  664. uint32_t sig = QPU_GET_FIELD(inst, QPU_SIG);
  665. validation_state.ip = ip;
  666. if (!vc4_handle_branch_target(&validation_state))
  667. goto fail;
  668. switch (sig) {
  669. case QPU_SIG_NONE:
  670. case QPU_SIG_WAIT_FOR_SCOREBOARD:
  671. case QPU_SIG_SCOREBOARD_UNLOCK:
  672. case QPU_SIG_COLOR_LOAD:
  673. case QPU_SIG_LOAD_TMU0:
  674. case QPU_SIG_LOAD_TMU1:
  675. case QPU_SIG_PROG_END:
  676. case QPU_SIG_SMALL_IMM:
  677. if (!check_instruction_writes(validated_shader,
  678. &validation_state)) {
  679. DRM_ERROR("Bad write at ip %d\n", ip);
  680. goto fail;
  681. }
  682. if (!check_instruction_reads(validated_shader,
  683. &validation_state))
  684. goto fail;
  685. if (sig == QPU_SIG_PROG_END) {
  686. found_shader_end = true;
  687. shader_end_ip = ip;
  688. }
  689. break;
  690. case QPU_SIG_LOAD_IMM:
  691. if (!check_instruction_writes(validated_shader,
  692. &validation_state)) {
  693. DRM_ERROR("Bad LOAD_IMM write at ip %d\n", ip);
  694. goto fail;
  695. }
  696. break;
  697. case QPU_SIG_BRANCH:
  698. if (!check_branch(inst, validated_shader,
  699. &validation_state, ip))
  700. goto fail;
  701. break;
  702. default:
  703. DRM_ERROR("Unsupported QPU signal %d at "
  704. "instruction %d\n", sig, ip);
  705. goto fail;
  706. }
  707. /* There are two delay slots after program end is signaled
  708. * that are still executed, then we're finished.
  709. */
  710. if (found_shader_end && ip == shader_end_ip + 2)
  711. break;
  712. }
  713. if (ip == validation_state.max_ip) {
  714. DRM_ERROR("shader failed to terminate before "
  715. "shader BO end at %zd\n",
  716. shader_obj->base.size);
  717. goto fail;
  718. }
  719. /* If we did a backwards branch and we haven't emitted a uniforms
  720. * reset since then, we still need the uniforms stream to have the
  721. * uniforms address available so that the backwards branch can do its
  722. * uniforms reset.
  723. *
  724. * We could potentially prove that the backwards branch doesn't
  725. * contain any uses of uniforms until program exit, but that doesn't
  726. * seem to be worth the trouble.
  727. */
  728. if (validation_state.needs_uniform_address_for_loop) {
  729. if (!require_uniform_address_uniform(validated_shader))
  730. goto fail;
  731. validated_shader->uniforms_size += 4;
  732. }
  733. /* Again, no chance of integer overflow here because the worst case
  734. * scenario is 8 bytes of uniforms plus handles per 8-byte
  735. * instruction.
  736. */
  737. validated_shader->uniforms_src_size =
  738. (validated_shader->uniforms_size +
  739. 4 * validated_shader->num_texture_samples);
  740. kfree(validation_state.branch_targets);
  741. return validated_shader;
  742. fail:
  743. kfree(validation_state.branch_targets);
  744. if (validated_shader) {
  745. kfree(validated_shader->texture_samples);
  746. kfree(validated_shader);
  747. }
  748. return NULL;
  749. }