vc4_validate_shaders.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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. struct vc4_shader_validation_state {
  40. struct vc4_texture_sample_info tmu_setup[2];
  41. int tmu_write_count[2];
  42. /* For registers that were last written to by a MIN instruction with
  43. * one argument being a uniform, the address of the uniform.
  44. * Otherwise, ~0.
  45. *
  46. * This is used for the validation of direct address memory reads.
  47. */
  48. uint32_t live_min_clamp_offsets[32 + 32 + 4];
  49. bool live_max_clamp_regs[32 + 32 + 4];
  50. };
  51. static uint32_t
  52. waddr_to_live_reg_index(uint32_t waddr, bool is_b)
  53. {
  54. if (waddr < 32) {
  55. if (is_b)
  56. return 32 + waddr;
  57. else
  58. return waddr;
  59. } else if (waddr <= QPU_W_ACC3) {
  60. return 64 + waddr - QPU_W_ACC0;
  61. } else {
  62. return ~0;
  63. }
  64. }
  65. static uint32_t
  66. raddr_add_a_to_live_reg_index(uint64_t inst)
  67. {
  68. uint32_t sig = QPU_GET_FIELD(inst, QPU_SIG);
  69. uint32_t add_a = QPU_GET_FIELD(inst, QPU_ADD_A);
  70. uint32_t raddr_a = QPU_GET_FIELD(inst, QPU_RADDR_A);
  71. uint32_t raddr_b = QPU_GET_FIELD(inst, QPU_RADDR_B);
  72. if (add_a == QPU_MUX_A)
  73. return raddr_a;
  74. else if (add_a == QPU_MUX_B && sig != QPU_SIG_SMALL_IMM)
  75. return 32 + raddr_b;
  76. else if (add_a <= QPU_MUX_R3)
  77. return 64 + add_a;
  78. else
  79. return ~0;
  80. }
  81. static bool
  82. is_tmu_submit(uint32_t waddr)
  83. {
  84. return (waddr == QPU_W_TMU0_S ||
  85. waddr == QPU_W_TMU1_S);
  86. }
  87. static bool
  88. is_tmu_write(uint32_t waddr)
  89. {
  90. return (waddr >= QPU_W_TMU0_S &&
  91. waddr <= QPU_W_TMU1_B);
  92. }
  93. static bool
  94. record_texture_sample(struct vc4_validated_shader_info *validated_shader,
  95. struct vc4_shader_validation_state *validation_state,
  96. int tmu)
  97. {
  98. uint32_t s = validated_shader->num_texture_samples;
  99. int i;
  100. struct vc4_texture_sample_info *temp_samples;
  101. temp_samples = krealloc(validated_shader->texture_samples,
  102. (s + 1) * sizeof(*temp_samples),
  103. GFP_KERNEL);
  104. if (!temp_samples)
  105. return false;
  106. memcpy(&temp_samples[s],
  107. &validation_state->tmu_setup[tmu],
  108. sizeof(*temp_samples));
  109. validated_shader->num_texture_samples = s + 1;
  110. validated_shader->texture_samples = temp_samples;
  111. for (i = 0; i < 4; i++)
  112. validation_state->tmu_setup[tmu].p_offset[i] = ~0;
  113. return true;
  114. }
  115. static bool
  116. check_tmu_write(uint64_t inst,
  117. struct vc4_validated_shader_info *validated_shader,
  118. struct vc4_shader_validation_state *validation_state,
  119. bool is_mul)
  120. {
  121. uint32_t waddr = (is_mul ?
  122. QPU_GET_FIELD(inst, QPU_WADDR_MUL) :
  123. QPU_GET_FIELD(inst, QPU_WADDR_ADD));
  124. uint32_t raddr_a = QPU_GET_FIELD(inst, QPU_RADDR_A);
  125. uint32_t raddr_b = QPU_GET_FIELD(inst, QPU_RADDR_B);
  126. int tmu = waddr > QPU_W_TMU0_B;
  127. bool submit = is_tmu_submit(waddr);
  128. bool is_direct = submit && validation_state->tmu_write_count[tmu] == 0;
  129. uint32_t sig = QPU_GET_FIELD(inst, QPU_SIG);
  130. if (is_direct) {
  131. uint32_t add_b = QPU_GET_FIELD(inst, QPU_ADD_B);
  132. uint32_t clamp_reg, clamp_offset;
  133. if (sig == QPU_SIG_SMALL_IMM) {
  134. DRM_ERROR("direct TMU read used small immediate\n");
  135. return false;
  136. }
  137. /* Make sure that this texture load is an add of the base
  138. * address of the UBO to a clamped offset within the UBO.
  139. */
  140. if (is_mul ||
  141. QPU_GET_FIELD(inst, QPU_OP_ADD) != QPU_A_ADD) {
  142. DRM_ERROR("direct TMU load wasn't an add\n");
  143. return false;
  144. }
  145. /* We assert that the the clamped address is the first
  146. * argument, and the UBO base address is the second argument.
  147. * This is arbitrary, but simpler than supporting flipping the
  148. * two either way.
  149. */
  150. clamp_reg = raddr_add_a_to_live_reg_index(inst);
  151. if (clamp_reg == ~0) {
  152. DRM_ERROR("direct TMU load wasn't clamped\n");
  153. return false;
  154. }
  155. clamp_offset = validation_state->live_min_clamp_offsets[clamp_reg];
  156. if (clamp_offset == ~0) {
  157. DRM_ERROR("direct TMU load wasn't clamped\n");
  158. return false;
  159. }
  160. /* Store the clamp value's offset in p1 (see reloc_tex() in
  161. * vc4_validate.c).
  162. */
  163. validation_state->tmu_setup[tmu].p_offset[1] =
  164. clamp_offset;
  165. if (!(add_b == QPU_MUX_A && raddr_a == QPU_R_UNIF) &&
  166. !(add_b == QPU_MUX_B && raddr_b == QPU_R_UNIF)) {
  167. DRM_ERROR("direct TMU load didn't add to a uniform\n");
  168. return false;
  169. }
  170. validation_state->tmu_setup[tmu].is_direct = true;
  171. } else {
  172. if (raddr_a == QPU_R_UNIF || (sig != QPU_SIG_SMALL_IMM &&
  173. raddr_b == QPU_R_UNIF)) {
  174. DRM_ERROR("uniform read in the same instruction as "
  175. "texture setup.\n");
  176. return false;
  177. }
  178. }
  179. if (validation_state->tmu_write_count[tmu] >= 4) {
  180. DRM_ERROR("TMU%d got too many parameters before dispatch\n",
  181. tmu);
  182. return false;
  183. }
  184. validation_state->tmu_setup[tmu].p_offset[validation_state->tmu_write_count[tmu]] =
  185. validated_shader->uniforms_size;
  186. validation_state->tmu_write_count[tmu]++;
  187. /* Since direct uses a RADDR uniform reference, it will get counted in
  188. * check_instruction_reads()
  189. */
  190. if (!is_direct)
  191. validated_shader->uniforms_size += 4;
  192. if (submit) {
  193. if (!record_texture_sample(validated_shader,
  194. validation_state, tmu)) {
  195. return false;
  196. }
  197. validation_state->tmu_write_count[tmu] = 0;
  198. }
  199. return true;
  200. }
  201. static bool
  202. check_reg_write(uint64_t inst,
  203. struct vc4_validated_shader_info *validated_shader,
  204. struct vc4_shader_validation_state *validation_state,
  205. bool is_mul)
  206. {
  207. uint32_t waddr = (is_mul ?
  208. QPU_GET_FIELD(inst, QPU_WADDR_MUL) :
  209. QPU_GET_FIELD(inst, QPU_WADDR_ADD));
  210. switch (waddr) {
  211. case QPU_W_UNIFORMS_ADDRESS:
  212. /* XXX: We'll probably need to support this for reladdr, but
  213. * it's definitely a security-related one.
  214. */
  215. DRM_ERROR("uniforms address load unsupported\n");
  216. return false;
  217. case QPU_W_TLB_COLOR_MS:
  218. case QPU_W_TLB_COLOR_ALL:
  219. case QPU_W_TLB_Z:
  220. /* These only interact with the tile buffer, not main memory,
  221. * so they're safe.
  222. */
  223. return true;
  224. case QPU_W_TMU0_S:
  225. case QPU_W_TMU0_T:
  226. case QPU_W_TMU0_R:
  227. case QPU_W_TMU0_B:
  228. case QPU_W_TMU1_S:
  229. case QPU_W_TMU1_T:
  230. case QPU_W_TMU1_R:
  231. case QPU_W_TMU1_B:
  232. return check_tmu_write(inst, validated_shader, validation_state,
  233. is_mul);
  234. case QPU_W_HOST_INT:
  235. case QPU_W_TMU_NOSWAP:
  236. case QPU_W_TLB_ALPHA_MASK:
  237. case QPU_W_MUTEX_RELEASE:
  238. /* XXX: I haven't thought about these, so don't support them
  239. * for now.
  240. */
  241. DRM_ERROR("Unsupported waddr %d\n", waddr);
  242. return false;
  243. case QPU_W_VPM_ADDR:
  244. DRM_ERROR("General VPM DMA unsupported\n");
  245. return false;
  246. case QPU_W_VPM:
  247. case QPU_W_VPMVCD_SETUP:
  248. /* We allow VPM setup in general, even including VPM DMA
  249. * configuration setup, because the (unsafe) DMA can only be
  250. * triggered by QPU_W_VPM_ADDR writes.
  251. */
  252. return true;
  253. case QPU_W_TLB_STENCIL_SETUP:
  254. return true;
  255. }
  256. return true;
  257. }
  258. static void
  259. track_live_clamps(uint64_t inst,
  260. struct vc4_validated_shader_info *validated_shader,
  261. struct vc4_shader_validation_state *validation_state)
  262. {
  263. uint32_t op_add = QPU_GET_FIELD(inst, QPU_OP_ADD);
  264. uint32_t waddr_add = QPU_GET_FIELD(inst, QPU_WADDR_ADD);
  265. uint32_t waddr_mul = QPU_GET_FIELD(inst, QPU_WADDR_MUL);
  266. uint32_t cond_add = QPU_GET_FIELD(inst, QPU_COND_ADD);
  267. uint32_t add_a = QPU_GET_FIELD(inst, QPU_ADD_A);
  268. uint32_t add_b = QPU_GET_FIELD(inst, QPU_ADD_B);
  269. uint32_t raddr_a = QPU_GET_FIELD(inst, QPU_RADDR_A);
  270. uint32_t raddr_b = QPU_GET_FIELD(inst, QPU_RADDR_B);
  271. uint32_t sig = QPU_GET_FIELD(inst, QPU_SIG);
  272. bool ws = inst & QPU_WS;
  273. uint32_t lri_add_a, lri_add, lri_mul;
  274. bool add_a_is_min_0;
  275. /* Check whether OP_ADD's A argumennt comes from a live MAX(x, 0),
  276. * before we clear previous live state.
  277. */
  278. lri_add_a = raddr_add_a_to_live_reg_index(inst);
  279. add_a_is_min_0 = (lri_add_a != ~0 &&
  280. validation_state->live_max_clamp_regs[lri_add_a]);
  281. /* Clear live state for registers written by our instruction. */
  282. lri_add = waddr_to_live_reg_index(waddr_add, ws);
  283. lri_mul = waddr_to_live_reg_index(waddr_mul, !ws);
  284. if (lri_mul != ~0) {
  285. validation_state->live_max_clamp_regs[lri_mul] = false;
  286. validation_state->live_min_clamp_offsets[lri_mul] = ~0;
  287. }
  288. if (lri_add != ~0) {
  289. validation_state->live_max_clamp_regs[lri_add] = false;
  290. validation_state->live_min_clamp_offsets[lri_add] = ~0;
  291. } else {
  292. /* Nothing further to do for live tracking, since only ADDs
  293. * generate new live clamp registers.
  294. */
  295. return;
  296. }
  297. /* Now, handle remaining live clamp tracking for the ADD operation. */
  298. if (cond_add != QPU_COND_ALWAYS)
  299. return;
  300. if (op_add == QPU_A_MAX) {
  301. /* Track live clamps of a value to a minimum of 0 (in either
  302. * arg).
  303. */
  304. if (sig != QPU_SIG_SMALL_IMM || raddr_b != 0 ||
  305. (add_a != QPU_MUX_B && add_b != QPU_MUX_B)) {
  306. return;
  307. }
  308. validation_state->live_max_clamp_regs[lri_add] = true;
  309. } else if (op_add == QPU_A_MIN) {
  310. /* Track live clamps of a value clamped to a minimum of 0 and
  311. * a maximum of some uniform's offset.
  312. */
  313. if (!add_a_is_min_0)
  314. return;
  315. if (!(add_b == QPU_MUX_A && raddr_a == QPU_R_UNIF) &&
  316. !(add_b == QPU_MUX_B && raddr_b == QPU_R_UNIF &&
  317. sig != QPU_SIG_SMALL_IMM)) {
  318. return;
  319. }
  320. validation_state->live_min_clamp_offsets[lri_add] =
  321. validated_shader->uniforms_size;
  322. }
  323. }
  324. static bool
  325. check_instruction_writes(uint64_t inst,
  326. struct vc4_validated_shader_info *validated_shader,
  327. struct vc4_shader_validation_state *validation_state)
  328. {
  329. uint32_t waddr_add = QPU_GET_FIELD(inst, QPU_WADDR_ADD);
  330. uint32_t waddr_mul = QPU_GET_FIELD(inst, QPU_WADDR_MUL);
  331. bool ok;
  332. if (is_tmu_write(waddr_add) && is_tmu_write(waddr_mul)) {
  333. DRM_ERROR("ADD and MUL both set up textures\n");
  334. return false;
  335. }
  336. ok = (check_reg_write(inst, validated_shader, validation_state,
  337. false) &&
  338. check_reg_write(inst, validated_shader, validation_state,
  339. true));
  340. track_live_clamps(inst, validated_shader, validation_state);
  341. return ok;
  342. }
  343. static bool
  344. check_instruction_reads(uint64_t inst,
  345. struct vc4_validated_shader_info *validated_shader)
  346. {
  347. uint32_t raddr_a = QPU_GET_FIELD(inst, QPU_RADDR_A);
  348. uint32_t raddr_b = QPU_GET_FIELD(inst, QPU_RADDR_B);
  349. uint32_t sig = QPU_GET_FIELD(inst, QPU_SIG);
  350. if (raddr_a == QPU_R_UNIF ||
  351. (raddr_b == QPU_R_UNIF && sig != QPU_SIG_SMALL_IMM)) {
  352. /* This can't overflow the uint32_t, because we're reading 8
  353. * bytes of instruction to increment by 4 here, so we'd
  354. * already be OOM.
  355. */
  356. validated_shader->uniforms_size += 4;
  357. }
  358. return true;
  359. }
  360. struct vc4_validated_shader_info *
  361. vc4_validate_shader(struct drm_gem_cma_object *shader_obj)
  362. {
  363. bool found_shader_end = false;
  364. int shader_end_ip = 0;
  365. uint32_t ip, max_ip;
  366. uint64_t *shader;
  367. struct vc4_validated_shader_info *validated_shader;
  368. struct vc4_shader_validation_state validation_state;
  369. int i;
  370. memset(&validation_state, 0, sizeof(validation_state));
  371. for (i = 0; i < 8; i++)
  372. validation_state.tmu_setup[i / 4].p_offset[i % 4] = ~0;
  373. for (i = 0; i < ARRAY_SIZE(validation_state.live_min_clamp_offsets); i++)
  374. validation_state.live_min_clamp_offsets[i] = ~0;
  375. shader = shader_obj->vaddr;
  376. max_ip = shader_obj->base.size / sizeof(uint64_t);
  377. validated_shader = kcalloc(1, sizeof(*validated_shader), GFP_KERNEL);
  378. if (!validated_shader)
  379. return NULL;
  380. for (ip = 0; ip < max_ip; ip++) {
  381. uint64_t inst = shader[ip];
  382. uint32_t sig = QPU_GET_FIELD(inst, QPU_SIG);
  383. switch (sig) {
  384. case QPU_SIG_NONE:
  385. case QPU_SIG_WAIT_FOR_SCOREBOARD:
  386. case QPU_SIG_SCOREBOARD_UNLOCK:
  387. case QPU_SIG_COLOR_LOAD:
  388. case QPU_SIG_LOAD_TMU0:
  389. case QPU_SIG_LOAD_TMU1:
  390. case QPU_SIG_PROG_END:
  391. case QPU_SIG_SMALL_IMM:
  392. if (!check_instruction_writes(inst, validated_shader,
  393. &validation_state)) {
  394. DRM_ERROR("Bad write at ip %d\n", ip);
  395. goto fail;
  396. }
  397. if (!check_instruction_reads(inst, validated_shader))
  398. goto fail;
  399. if (sig == QPU_SIG_PROG_END) {
  400. found_shader_end = true;
  401. shader_end_ip = ip;
  402. }
  403. break;
  404. case QPU_SIG_LOAD_IMM:
  405. if (!check_instruction_writes(inst, validated_shader,
  406. &validation_state)) {
  407. DRM_ERROR("Bad LOAD_IMM write at ip %d\n", ip);
  408. goto fail;
  409. }
  410. break;
  411. default:
  412. DRM_ERROR("Unsupported QPU signal %d at "
  413. "instruction %d\n", sig, ip);
  414. goto fail;
  415. }
  416. /* There are two delay slots after program end is signaled
  417. * that are still executed, then we're finished.
  418. */
  419. if (found_shader_end && ip == shader_end_ip + 2)
  420. break;
  421. }
  422. if (ip == max_ip) {
  423. DRM_ERROR("shader failed to terminate before "
  424. "shader BO end at %zd\n",
  425. shader_obj->base.size);
  426. goto fail;
  427. }
  428. /* Again, no chance of integer overflow here because the worst case
  429. * scenario is 8 bytes of uniforms plus handles per 8-byte
  430. * instruction.
  431. */
  432. validated_shader->uniforms_src_size =
  433. (validated_shader->uniforms_size +
  434. 4 * validated_shader->num_texture_samples);
  435. return validated_shader;
  436. fail:
  437. if (validated_shader) {
  438. kfree(validated_shader->texture_samples);
  439. kfree(validated_shader);
  440. }
  441. return NULL;
  442. }