vc4_validate_shaders.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  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. switch (QPU_GET_FIELD(inst, QPU_SIG)) {
  271. case QPU_SIG_NONE:
  272. case QPU_SIG_SCOREBOARD_UNLOCK:
  273. case QPU_SIG_COLOR_LOAD:
  274. case QPU_SIG_LOAD_TMU0:
  275. case QPU_SIG_LOAD_TMU1:
  276. break;
  277. default:
  278. DRM_ERROR("uniforms address change must be "
  279. "normal math\n");
  280. return false;
  281. }
  282. if (is_mul || QPU_GET_FIELD(inst, QPU_OP_ADD) != QPU_A_ADD) {
  283. DRM_ERROR("Uniform address reset must be an ADD.\n");
  284. return false;
  285. }
  286. if (QPU_GET_FIELD(inst, QPU_COND_ADD) != QPU_COND_ALWAYS) {
  287. DRM_ERROR("Uniform address reset must be unconditional.\n");
  288. return false;
  289. }
  290. if (QPU_GET_FIELD(inst, QPU_PACK) != QPU_PACK_A_NOP &&
  291. !(inst & QPU_PM)) {
  292. DRM_ERROR("No packing allowed on uniforms reset\n");
  293. return false;
  294. }
  295. if (add_lri == -1) {
  296. DRM_ERROR("First argument of uniform address write must be "
  297. "an immediate value.\n");
  298. return false;
  299. }
  300. if (validation_state->live_immediates[add_lri] != expected_offset) {
  301. DRM_ERROR("Resetting uniforms with offset %db instead of %db\n",
  302. validation_state->live_immediates[add_lri],
  303. expected_offset);
  304. return false;
  305. }
  306. if (!(add_b == QPU_MUX_A && raddr_a == QPU_R_UNIF) &&
  307. !(add_b == QPU_MUX_B && raddr_b == QPU_R_UNIF)) {
  308. DRM_ERROR("Second argument of uniform address write must be "
  309. "a uniform.\n");
  310. return false;
  311. }
  312. validation_state->needs_uniform_address_update = false;
  313. validation_state->needs_uniform_address_for_loop = false;
  314. return require_uniform_address_uniform(validated_shader);
  315. }
  316. static bool
  317. check_reg_write(struct vc4_validated_shader_info *validated_shader,
  318. struct vc4_shader_validation_state *validation_state,
  319. bool is_mul)
  320. {
  321. uint64_t inst = validation_state->shader[validation_state->ip];
  322. uint32_t waddr = (is_mul ?
  323. QPU_GET_FIELD(inst, QPU_WADDR_MUL) :
  324. QPU_GET_FIELD(inst, QPU_WADDR_ADD));
  325. uint32_t sig = QPU_GET_FIELD(inst, QPU_SIG);
  326. bool ws = inst & QPU_WS;
  327. bool is_b = is_mul ^ ws;
  328. u32 lri = waddr_to_live_reg_index(waddr, is_b);
  329. if (lri != -1) {
  330. uint32_t cond_add = QPU_GET_FIELD(inst, QPU_COND_ADD);
  331. uint32_t cond_mul = QPU_GET_FIELD(inst, QPU_COND_MUL);
  332. if (sig == QPU_SIG_LOAD_IMM &&
  333. QPU_GET_FIELD(inst, QPU_PACK) == QPU_PACK_A_NOP &&
  334. ((is_mul && cond_mul == QPU_COND_ALWAYS) ||
  335. (!is_mul && cond_add == QPU_COND_ALWAYS))) {
  336. validation_state->live_immediates[lri] =
  337. QPU_GET_FIELD(inst, QPU_LOAD_IMM);
  338. } else {
  339. validation_state->live_immediates[lri] = ~0;
  340. }
  341. }
  342. switch (waddr) {
  343. case QPU_W_UNIFORMS_ADDRESS:
  344. if (is_b) {
  345. DRM_ERROR("relative uniforms address change "
  346. "unsupported\n");
  347. return false;
  348. }
  349. return validate_uniform_address_write(validated_shader,
  350. validation_state,
  351. is_mul);
  352. case QPU_W_TLB_COLOR_MS:
  353. case QPU_W_TLB_COLOR_ALL:
  354. case QPU_W_TLB_Z:
  355. /* These only interact with the tile buffer, not main memory,
  356. * so they're safe.
  357. */
  358. return true;
  359. case QPU_W_TMU0_S:
  360. case QPU_W_TMU0_T:
  361. case QPU_W_TMU0_R:
  362. case QPU_W_TMU0_B:
  363. case QPU_W_TMU1_S:
  364. case QPU_W_TMU1_T:
  365. case QPU_W_TMU1_R:
  366. case QPU_W_TMU1_B:
  367. return check_tmu_write(validated_shader, validation_state,
  368. is_mul);
  369. case QPU_W_HOST_INT:
  370. case QPU_W_TMU_NOSWAP:
  371. case QPU_W_TLB_ALPHA_MASK:
  372. case QPU_W_MUTEX_RELEASE:
  373. /* XXX: I haven't thought about these, so don't support them
  374. * for now.
  375. */
  376. DRM_ERROR("Unsupported waddr %d\n", waddr);
  377. return false;
  378. case QPU_W_VPM_ADDR:
  379. DRM_ERROR("General VPM DMA unsupported\n");
  380. return false;
  381. case QPU_W_VPM:
  382. case QPU_W_VPMVCD_SETUP:
  383. /* We allow VPM setup in general, even including VPM DMA
  384. * configuration setup, because the (unsafe) DMA can only be
  385. * triggered by QPU_W_VPM_ADDR writes.
  386. */
  387. return true;
  388. case QPU_W_TLB_STENCIL_SETUP:
  389. return true;
  390. }
  391. return true;
  392. }
  393. static void
  394. track_live_clamps(struct vc4_validated_shader_info *validated_shader,
  395. struct vc4_shader_validation_state *validation_state)
  396. {
  397. uint64_t inst = validation_state->shader[validation_state->ip];
  398. uint32_t op_add = QPU_GET_FIELD(inst, QPU_OP_ADD);
  399. uint32_t waddr_add = QPU_GET_FIELD(inst, QPU_WADDR_ADD);
  400. uint32_t waddr_mul = QPU_GET_FIELD(inst, QPU_WADDR_MUL);
  401. uint32_t cond_add = QPU_GET_FIELD(inst, QPU_COND_ADD);
  402. uint32_t add_a = QPU_GET_FIELD(inst, QPU_ADD_A);
  403. uint32_t add_b = QPU_GET_FIELD(inst, QPU_ADD_B);
  404. uint32_t raddr_a = QPU_GET_FIELD(inst, QPU_RADDR_A);
  405. uint32_t raddr_b = QPU_GET_FIELD(inst, QPU_RADDR_B);
  406. uint32_t sig = QPU_GET_FIELD(inst, QPU_SIG);
  407. bool ws = inst & QPU_WS;
  408. uint32_t lri_add_a, lri_add, lri_mul;
  409. bool add_a_is_min_0;
  410. /* Check whether OP_ADD's A argumennt comes from a live MAX(x, 0),
  411. * before we clear previous live state.
  412. */
  413. lri_add_a = raddr_add_a_to_live_reg_index(inst);
  414. add_a_is_min_0 = (lri_add_a != ~0 &&
  415. validation_state->live_max_clamp_regs[lri_add_a]);
  416. /* Clear live state for registers written by our instruction. */
  417. lri_add = waddr_to_live_reg_index(waddr_add, ws);
  418. lri_mul = waddr_to_live_reg_index(waddr_mul, !ws);
  419. if (lri_mul != ~0) {
  420. validation_state->live_max_clamp_regs[lri_mul] = false;
  421. validation_state->live_min_clamp_offsets[lri_mul] = ~0;
  422. }
  423. if (lri_add != ~0) {
  424. validation_state->live_max_clamp_regs[lri_add] = false;
  425. validation_state->live_min_clamp_offsets[lri_add] = ~0;
  426. } else {
  427. /* Nothing further to do for live tracking, since only ADDs
  428. * generate new live clamp registers.
  429. */
  430. return;
  431. }
  432. /* Now, handle remaining live clamp tracking for the ADD operation. */
  433. if (cond_add != QPU_COND_ALWAYS)
  434. return;
  435. if (op_add == QPU_A_MAX) {
  436. /* Track live clamps of a value to a minimum of 0 (in either
  437. * arg).
  438. */
  439. if (sig != QPU_SIG_SMALL_IMM || raddr_b != 0 ||
  440. (add_a != QPU_MUX_B && add_b != QPU_MUX_B)) {
  441. return;
  442. }
  443. validation_state->live_max_clamp_regs[lri_add] = true;
  444. } else if (op_add == QPU_A_MIN) {
  445. /* Track live clamps of a value clamped to a minimum of 0 and
  446. * a maximum of some uniform's offset.
  447. */
  448. if (!add_a_is_min_0)
  449. return;
  450. if (!(add_b == QPU_MUX_A && raddr_a == QPU_R_UNIF) &&
  451. !(add_b == QPU_MUX_B && raddr_b == QPU_R_UNIF &&
  452. sig != QPU_SIG_SMALL_IMM)) {
  453. return;
  454. }
  455. validation_state->live_min_clamp_offsets[lri_add] =
  456. validated_shader->uniforms_size;
  457. }
  458. }
  459. static bool
  460. check_instruction_writes(struct vc4_validated_shader_info *validated_shader,
  461. struct vc4_shader_validation_state *validation_state)
  462. {
  463. uint64_t inst = validation_state->shader[validation_state->ip];
  464. uint32_t waddr_add = QPU_GET_FIELD(inst, QPU_WADDR_ADD);
  465. uint32_t waddr_mul = QPU_GET_FIELD(inst, QPU_WADDR_MUL);
  466. bool ok;
  467. if (is_tmu_write(waddr_add) && is_tmu_write(waddr_mul)) {
  468. DRM_ERROR("ADD and MUL both set up textures\n");
  469. return false;
  470. }
  471. ok = (check_reg_write(validated_shader, validation_state, false) &&
  472. check_reg_write(validated_shader, validation_state, true));
  473. track_live_clamps(validated_shader, validation_state);
  474. return ok;
  475. }
  476. static bool
  477. check_branch(uint64_t inst,
  478. struct vc4_validated_shader_info *validated_shader,
  479. struct vc4_shader_validation_state *validation_state,
  480. int ip)
  481. {
  482. int32_t branch_imm = QPU_GET_FIELD(inst, QPU_BRANCH_TARGET);
  483. uint32_t waddr_add = QPU_GET_FIELD(inst, QPU_WADDR_ADD);
  484. uint32_t waddr_mul = QPU_GET_FIELD(inst, QPU_WADDR_MUL);
  485. if ((int)branch_imm < 0)
  486. validation_state->needs_uniform_address_for_loop = true;
  487. /* We don't want to have to worry about validation of this, and
  488. * there's no need for it.
  489. */
  490. if (waddr_add != QPU_W_NOP || waddr_mul != QPU_W_NOP) {
  491. DRM_ERROR("branch instruction at %d wrote a register.\n",
  492. validation_state->ip);
  493. return false;
  494. }
  495. return true;
  496. }
  497. static bool
  498. check_instruction_reads(struct vc4_validated_shader_info *validated_shader,
  499. struct vc4_shader_validation_state *validation_state)
  500. {
  501. uint64_t inst = validation_state->shader[validation_state->ip];
  502. uint32_t raddr_a = QPU_GET_FIELD(inst, QPU_RADDR_A);
  503. uint32_t raddr_b = QPU_GET_FIELD(inst, QPU_RADDR_B);
  504. uint32_t sig = QPU_GET_FIELD(inst, QPU_SIG);
  505. if (raddr_a == QPU_R_UNIF ||
  506. (raddr_b == QPU_R_UNIF && sig != QPU_SIG_SMALL_IMM)) {
  507. /* This can't overflow the uint32_t, because we're reading 8
  508. * bytes of instruction to increment by 4 here, so we'd
  509. * already be OOM.
  510. */
  511. validated_shader->uniforms_size += 4;
  512. if (validation_state->needs_uniform_address_update) {
  513. DRM_ERROR("Uniform read with undefined uniform "
  514. "address\n");
  515. return false;
  516. }
  517. }
  518. return true;
  519. }
  520. /* Make sure that all branches are absolute and point within the shader, and
  521. * note their targets for later.
  522. */
  523. static bool
  524. vc4_validate_branches(struct vc4_shader_validation_state *validation_state)
  525. {
  526. uint32_t max_branch_target = 0;
  527. bool found_shader_end = false;
  528. int ip;
  529. int shader_end_ip = 0;
  530. int last_branch = -2;
  531. for (ip = 0; ip < validation_state->max_ip; ip++) {
  532. uint64_t inst = validation_state->shader[ip];
  533. int32_t branch_imm = QPU_GET_FIELD(inst, QPU_BRANCH_TARGET);
  534. uint32_t sig = QPU_GET_FIELD(inst, QPU_SIG);
  535. uint32_t after_delay_ip = ip + 4;
  536. uint32_t branch_target_ip;
  537. if (sig == QPU_SIG_PROG_END) {
  538. shader_end_ip = ip;
  539. found_shader_end = true;
  540. continue;
  541. }
  542. if (sig != QPU_SIG_BRANCH)
  543. continue;
  544. if (ip - last_branch < 4) {
  545. DRM_ERROR("Branch at %d during delay slots\n", ip);
  546. return false;
  547. }
  548. last_branch = ip;
  549. if (inst & QPU_BRANCH_REG) {
  550. DRM_ERROR("branching from register relative "
  551. "not supported\n");
  552. return false;
  553. }
  554. if (!(inst & QPU_BRANCH_REL)) {
  555. DRM_ERROR("relative branching required\n");
  556. return false;
  557. }
  558. /* The actual branch target is the instruction after the delay
  559. * slots, plus whatever byte offset is in the low 32 bits of
  560. * the instruction. Make sure we're not branching beyond the
  561. * end of the shader object.
  562. */
  563. if (branch_imm % sizeof(inst) != 0) {
  564. DRM_ERROR("branch target not aligned\n");
  565. return false;
  566. }
  567. branch_target_ip = after_delay_ip + (branch_imm >> 3);
  568. if (branch_target_ip >= validation_state->max_ip) {
  569. DRM_ERROR("Branch at %d outside of shader (ip %d/%d)\n",
  570. ip, branch_target_ip,
  571. validation_state->max_ip);
  572. return false;
  573. }
  574. set_bit(branch_target_ip, validation_state->branch_targets);
  575. /* Make sure that the non-branching path is also not outside
  576. * the shader.
  577. */
  578. if (after_delay_ip >= validation_state->max_ip) {
  579. DRM_ERROR("Branch at %d continues past shader end "
  580. "(%d/%d)\n",
  581. ip, after_delay_ip, validation_state->max_ip);
  582. return false;
  583. }
  584. set_bit(after_delay_ip, validation_state->branch_targets);
  585. max_branch_target = max(max_branch_target, after_delay_ip);
  586. /* There are two delay slots after program end is signaled
  587. * that are still executed, then we're finished.
  588. */
  589. if (found_shader_end && ip == shader_end_ip + 2)
  590. break;
  591. }
  592. if (max_branch_target > shader_end_ip) {
  593. DRM_ERROR("Branch landed after QPU_SIG_PROG_END");
  594. return false;
  595. }
  596. return true;
  597. }
  598. /* Resets any known state for the shader, used when we may be branched to from
  599. * multiple locations in the program (or at shader start).
  600. */
  601. static void
  602. reset_validation_state(struct vc4_shader_validation_state *validation_state)
  603. {
  604. int i;
  605. for (i = 0; i < 8; i++)
  606. validation_state->tmu_setup[i / 4].p_offset[i % 4] = ~0;
  607. for (i = 0; i < LIVE_REG_COUNT; i++) {
  608. validation_state->live_min_clamp_offsets[i] = ~0;
  609. validation_state->live_max_clamp_regs[i] = false;
  610. validation_state->live_immediates[i] = ~0;
  611. }
  612. }
  613. static bool
  614. texturing_in_progress(struct vc4_shader_validation_state *validation_state)
  615. {
  616. return (validation_state->tmu_write_count[0] != 0 ||
  617. validation_state->tmu_write_count[1] != 0);
  618. }
  619. static bool
  620. vc4_handle_branch_target(struct vc4_shader_validation_state *validation_state)
  621. {
  622. uint32_t ip = validation_state->ip;
  623. if (!test_bit(ip, validation_state->branch_targets))
  624. return true;
  625. if (texturing_in_progress(validation_state)) {
  626. DRM_ERROR("Branch target landed during TMU setup\n");
  627. return false;
  628. }
  629. /* Reset our live values tracking, since this instruction may have
  630. * multiple predecessors.
  631. *
  632. * One could potentially do analysis to determine that, for
  633. * example, all predecessors have a live max clamp in the same
  634. * register, but we don't bother with that.
  635. */
  636. reset_validation_state(validation_state);
  637. /* Since we've entered a basic block from potentially multiple
  638. * predecessors, we need the uniforms address to be updated before any
  639. * unforms are read. We require that after any branch point, the next
  640. * uniform to be loaded is a uniform address offset. That uniform's
  641. * offset will be marked by the uniform address register write
  642. * validation, or a one-off the end-of-program check.
  643. */
  644. validation_state->needs_uniform_address_update = true;
  645. return true;
  646. }
  647. struct vc4_validated_shader_info *
  648. vc4_validate_shader(struct drm_gem_cma_object *shader_obj)
  649. {
  650. bool found_shader_end = false;
  651. int shader_end_ip = 0;
  652. uint32_t ip;
  653. struct vc4_validated_shader_info *validated_shader = NULL;
  654. struct vc4_shader_validation_state validation_state;
  655. memset(&validation_state, 0, sizeof(validation_state));
  656. validation_state.shader = shader_obj->vaddr;
  657. validation_state.max_ip = shader_obj->base.size / sizeof(uint64_t);
  658. reset_validation_state(&validation_state);
  659. validation_state.branch_targets =
  660. kcalloc(BITS_TO_LONGS(validation_state.max_ip),
  661. sizeof(unsigned long), GFP_KERNEL);
  662. if (!validation_state.branch_targets)
  663. goto fail;
  664. validated_shader = kcalloc(1, sizeof(*validated_shader), GFP_KERNEL);
  665. if (!validated_shader)
  666. goto fail;
  667. if (!vc4_validate_branches(&validation_state))
  668. goto fail;
  669. for (ip = 0; ip < validation_state.max_ip; ip++) {
  670. uint64_t inst = validation_state.shader[ip];
  671. uint32_t sig = QPU_GET_FIELD(inst, QPU_SIG);
  672. validation_state.ip = ip;
  673. if (!vc4_handle_branch_target(&validation_state))
  674. goto fail;
  675. switch (sig) {
  676. case QPU_SIG_NONE:
  677. case QPU_SIG_WAIT_FOR_SCOREBOARD:
  678. case QPU_SIG_SCOREBOARD_UNLOCK:
  679. case QPU_SIG_COLOR_LOAD:
  680. case QPU_SIG_LOAD_TMU0:
  681. case QPU_SIG_LOAD_TMU1:
  682. case QPU_SIG_PROG_END:
  683. case QPU_SIG_SMALL_IMM:
  684. if (!check_instruction_writes(validated_shader,
  685. &validation_state)) {
  686. DRM_ERROR("Bad write at ip %d\n", ip);
  687. goto fail;
  688. }
  689. if (!check_instruction_reads(validated_shader,
  690. &validation_state))
  691. goto fail;
  692. if (sig == QPU_SIG_PROG_END) {
  693. found_shader_end = true;
  694. shader_end_ip = ip;
  695. }
  696. break;
  697. case QPU_SIG_LOAD_IMM:
  698. if (!check_instruction_writes(validated_shader,
  699. &validation_state)) {
  700. DRM_ERROR("Bad LOAD_IMM write at ip %d\n", ip);
  701. goto fail;
  702. }
  703. break;
  704. case QPU_SIG_BRANCH:
  705. if (!check_branch(inst, validated_shader,
  706. &validation_state, ip))
  707. goto fail;
  708. break;
  709. default:
  710. DRM_ERROR("Unsupported QPU signal %d at "
  711. "instruction %d\n", sig, ip);
  712. goto fail;
  713. }
  714. /* There are two delay slots after program end is signaled
  715. * that are still executed, then we're finished.
  716. */
  717. if (found_shader_end && ip == shader_end_ip + 2)
  718. break;
  719. }
  720. if (ip == validation_state.max_ip) {
  721. DRM_ERROR("shader failed to terminate before "
  722. "shader BO end at %zd\n",
  723. shader_obj->base.size);
  724. goto fail;
  725. }
  726. /* If we did a backwards branch and we haven't emitted a uniforms
  727. * reset since then, we still need the uniforms stream to have the
  728. * uniforms address available so that the backwards branch can do its
  729. * uniforms reset.
  730. *
  731. * We could potentially prove that the backwards branch doesn't
  732. * contain any uses of uniforms until program exit, but that doesn't
  733. * seem to be worth the trouble.
  734. */
  735. if (validation_state.needs_uniform_address_for_loop) {
  736. if (!require_uniform_address_uniform(validated_shader))
  737. goto fail;
  738. validated_shader->uniforms_size += 4;
  739. }
  740. /* Again, no chance of integer overflow here because the worst case
  741. * scenario is 8 bytes of uniforms plus handles per 8-byte
  742. * instruction.
  743. */
  744. validated_shader->uniforms_src_size =
  745. (validated_shader->uniforms_size +
  746. 4 * validated_shader->num_texture_samples);
  747. kfree(validation_state.branch_targets);
  748. return validated_shader;
  749. fail:
  750. kfree(validation_state.branch_targets);
  751. if (validated_shader) {
  752. kfree(validated_shader->texture_samples);
  753. kfree(validated_shader);
  754. }
  755. return NULL;
  756. }