vc4_validate_shaders.c 14 KB

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