vc4_validate_shaders.c 27 KB

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