Path: blob/21.2-virgl/src/gallium/drivers/vc4/kernel/vc4_validate_shaders.c
4574 views
/*1* Copyright © 2014 Broadcom2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223/**24* DOC: Shader validator for VC4.25*26* The VC4 has no IOMMU between it and system memory, so a user with27* access to execute shaders could escalate privilege by overwriting28* system memory (using the VPM write address register in the29* general-purpose DMA mode) or reading system memory it shouldn't30* (reading it as a texture, or uniform data, or vertex data).31*32* This walks over a shader BO, ensuring that its accesses are33* appropriately bounded, and recording how many texture accesses are34* made and where so that we can do relocations for them in the35* uniform stream.36*/3738#include "vc4_drv.h"39#include "vc4_qpu.h"40#include "vc4_qpu_defines.h"4142#define LIVE_REG_COUNT (32 + 32 + 4)4344struct vc4_shader_validation_state {45/* Current IP being validated. */46uint32_t ip;4748/* IP at the end of the BO, do not read shader[max_ip] */49uint32_t max_ip;5051uint64_t *shader;5253struct vc4_texture_sample_info tmu_setup[2];54int tmu_write_count[2];5556/* For registers that were last written to by a MIN instruction with57* one argument being a uniform, the address of the uniform.58* Otherwise, ~0.59*60* This is used for the validation of direct address memory reads.61*/62uint32_t live_min_clamp_offsets[LIVE_REG_COUNT];63bool live_max_clamp_regs[LIVE_REG_COUNT];64uint32_t live_immediates[LIVE_REG_COUNT];6566/* Bitfield of which IPs are used as branch targets.67*68* Used for validation that the uniform stream is updated at the right69* points and clearing the texturing/clamping state.70*/71unsigned long *branch_targets;7273/* Set when entering a basic block, and cleared when the uniform74* address update is found. This is used to make sure that we don't75* read uniforms when the address is undefined.76*/77bool needs_uniform_address_update;7879/* Set when we find a backwards branch. If the branch is backwards,80* the taraget is probably doing an address reset to read uniforms,81* and so we need to be sure that a uniforms address is present in the82* stream, even if the shader didn't need to read uniforms in later83* basic blocks.84*/85bool needs_uniform_address_for_loop;8687/* Set when we find an instruction which violates the criterion for a88* threaded shader. These are:89* - only write the lower half of the register space90* - last thread switch signaled at the end91* So track the usage of the thread switches and the register usage.92*/93bool all_registers_used;94};9596static uint32_t97waddr_to_live_reg_index(uint32_t waddr, bool is_b)98{99if (waddr < 32) {100if (is_b)101return 32 + waddr;102else103return waddr;104} else if (waddr <= QPU_W_ACC3) {105return 64 + waddr - QPU_W_ACC0;106} else {107return ~0;108}109}110111static uint32_t112raddr_add_a_to_live_reg_index(uint64_t inst)113{114uint32_t sig = QPU_GET_FIELD(inst, QPU_SIG);115uint32_t add_a = QPU_GET_FIELD(inst, QPU_ADD_A);116uint32_t raddr_a = QPU_GET_FIELD(inst, QPU_RADDR_A);117uint32_t raddr_b = QPU_GET_FIELD(inst, QPU_RADDR_B);118119if (add_a == QPU_MUX_A)120return raddr_a;121else if (add_a == QPU_MUX_B && sig != QPU_SIG_SMALL_IMM)122return 32 + raddr_b;123else if (add_a <= QPU_MUX_R3)124return 64 + add_a;125else126return ~0;127}128129static bool live_reg_is_upper_half(uint32_t lri)130{131return (lri >=16 && lri < 32) ||132(lri >=32 + 16 && lri < 32 + 32);133}134135static bool136is_tmu_submit(uint32_t waddr)137{138return (waddr == QPU_W_TMU0_S ||139waddr == QPU_W_TMU1_S);140}141142static bool143is_tmu_write(uint32_t waddr)144{145return (waddr >= QPU_W_TMU0_S &&146waddr <= QPU_W_TMU1_B);147}148149static bool150record_texture_sample(struct vc4_validated_shader_info *validated_shader,151struct vc4_shader_validation_state *validation_state,152int tmu)153{154uint32_t s = validated_shader->num_texture_samples;155int i;156struct vc4_texture_sample_info *temp_samples;157158temp_samples = krealloc(validated_shader->texture_samples,159(s + 1) * sizeof(*temp_samples),160GFP_KERNEL);161if (!temp_samples)162return false;163164memcpy(&temp_samples[s],165&validation_state->tmu_setup[tmu],166sizeof(*temp_samples));167168validated_shader->num_texture_samples = s + 1;169validated_shader->texture_samples = temp_samples;170171for (i = 0; i < 4; i++)172validation_state->tmu_setup[tmu].p_offset[i] = ~0;173174return true;175}176177static bool178check_tmu_write(struct vc4_validated_shader_info *validated_shader,179struct vc4_shader_validation_state *validation_state,180bool is_mul)181{182uint64_t inst = validation_state->shader[validation_state->ip];183uint32_t waddr = (is_mul ?184QPU_GET_FIELD(inst, QPU_WADDR_MUL) :185QPU_GET_FIELD(inst, QPU_WADDR_ADD));186uint32_t raddr_a = QPU_GET_FIELD(inst, QPU_RADDR_A);187uint32_t raddr_b = QPU_GET_FIELD(inst, QPU_RADDR_B);188int tmu = waddr > QPU_W_TMU0_B;189bool submit = is_tmu_submit(waddr);190bool is_direct = submit && validation_state->tmu_write_count[tmu] == 0;191uint32_t sig = QPU_GET_FIELD(inst, QPU_SIG);192193if (is_direct) {194uint32_t add_b = QPU_GET_FIELD(inst, QPU_ADD_B);195uint32_t clamp_reg, clamp_offset;196197if (sig == QPU_SIG_SMALL_IMM) {198DRM_ERROR("direct TMU read used small immediate\n");199return false;200}201202/* Make sure that this texture load is an add of the base203* address of the UBO to a clamped offset within the UBO.204*/205if (is_mul ||206QPU_GET_FIELD(inst, QPU_OP_ADD) != QPU_A_ADD) {207DRM_ERROR("direct TMU load wasn't an add\n");208return false;209}210211/* We assert that the clamped address is the first212* argument, and the UBO base address is the second argument.213* This is arbitrary, but simpler than supporting flipping the214* two either way.215*/216clamp_reg = raddr_add_a_to_live_reg_index(inst);217if (clamp_reg == ~0) {218DRM_ERROR("direct TMU load wasn't clamped\n");219return false;220}221222clamp_offset = validation_state->live_min_clamp_offsets[clamp_reg];223if (clamp_offset == ~0) {224DRM_ERROR("direct TMU load wasn't clamped\n");225return false;226}227228/* Store the clamp value's offset in p1 (see reloc_tex() in229* vc4_validate.c).230*/231validation_state->tmu_setup[tmu].p_offset[1] =232clamp_offset;233234if (!(add_b == QPU_MUX_A && raddr_a == QPU_R_UNIF) &&235!(add_b == QPU_MUX_B && raddr_b == QPU_R_UNIF)) {236DRM_ERROR("direct TMU load didn't add to a uniform\n");237return false;238}239240validation_state->tmu_setup[tmu].is_direct = true;241} else {242if (raddr_a == QPU_R_UNIF || (sig != QPU_SIG_SMALL_IMM &&243raddr_b == QPU_R_UNIF)) {244DRM_ERROR("uniform read in the same instruction as "245"texture setup.\n");246return false;247}248}249250if (validation_state->tmu_write_count[tmu] >= 4) {251DRM_ERROR("TMU%d got too many parameters before dispatch\n",252tmu);253return false;254}255validation_state->tmu_setup[tmu].p_offset[validation_state->tmu_write_count[tmu]] =256validated_shader->uniforms_size;257validation_state->tmu_write_count[tmu]++;258/* Since direct uses a RADDR uniform reference, it will get counted in259* check_instruction_reads()260*/261if (!is_direct) {262if (validation_state->needs_uniform_address_update) {263DRM_ERROR("Texturing with undefined uniform address\n");264return false;265}266267validated_shader->uniforms_size += 4;268}269270if (submit) {271if (!record_texture_sample(validated_shader,272validation_state, tmu)) {273return false;274}275276validation_state->tmu_write_count[tmu] = 0;277}278279return true;280}281282static bool require_uniform_address_uniform(struct vc4_validated_shader_info *validated_shader)283{284uint32_t o = validated_shader->num_uniform_addr_offsets;285uint32_t num_uniforms = validated_shader->uniforms_size / 4;286287validated_shader->uniform_addr_offsets =288krealloc(validated_shader->uniform_addr_offsets,289(o + 1) *290sizeof(*validated_shader->uniform_addr_offsets),291GFP_KERNEL);292if (!validated_shader->uniform_addr_offsets)293return false;294295validated_shader->uniform_addr_offsets[o] = num_uniforms;296validated_shader->num_uniform_addr_offsets++;297298return true;299}300301static bool302validate_uniform_address_write(struct vc4_validated_shader_info *validated_shader,303struct vc4_shader_validation_state *validation_state,304bool is_mul)305{306uint64_t inst = validation_state->shader[validation_state->ip];307u32 add_b = QPU_GET_FIELD(inst, QPU_ADD_B);308u32 raddr_a = QPU_GET_FIELD(inst, QPU_RADDR_A);309u32 raddr_b = QPU_GET_FIELD(inst, QPU_RADDR_B);310u32 add_lri = raddr_add_a_to_live_reg_index(inst);311/* We want our reset to be pointing at whatever uniform follows the312* uniforms base address.313*/314u32 expected_offset = validated_shader->uniforms_size + 4;315316/* We only support absolute uniform address changes, and we317* require that they be in the current basic block before any318* of its uniform reads.319*320* One could potentially emit more efficient QPU code, by321* noticing that (say) an if statement does uniform control322* flow for all threads and that the if reads the same number323* of uniforms on each side. However, this scheme is easy to324* validate so it's all we allow for now.325*/326327if (QPU_GET_FIELD(inst, QPU_SIG) != QPU_SIG_NONE) {328DRM_ERROR("uniforms address change must be "329"normal math\n");330return false;331}332333if (is_mul || QPU_GET_FIELD(inst, QPU_OP_ADD) != QPU_A_ADD) {334DRM_ERROR("Uniform address reset must be an ADD.\n");335return false;336}337338if (QPU_GET_FIELD(inst, QPU_COND_ADD) != QPU_COND_ALWAYS) {339DRM_ERROR("Uniform address reset must be unconditional.\n");340return false;341}342343if (QPU_GET_FIELD(inst, QPU_PACK) != QPU_PACK_A_NOP &&344!(inst & QPU_PM)) {345DRM_ERROR("No packing allowed on uniforms reset\n");346return false;347}348349if (add_lri == -1) {350DRM_ERROR("First argument of uniform address write must be "351"an immediate value.\n");352return false;353}354355if (validation_state->live_immediates[add_lri] != expected_offset) {356DRM_ERROR("Resetting uniforms with offset %db instead of %db\n",357validation_state->live_immediates[add_lri],358expected_offset);359return false;360}361362if (!(add_b == QPU_MUX_A && raddr_a == QPU_R_UNIF) &&363!(add_b == QPU_MUX_B && raddr_b == QPU_R_UNIF)) {364DRM_ERROR("Second argument of uniform address write must be "365"a uniform.\n");366return false;367}368369validation_state->needs_uniform_address_update = false;370validation_state->needs_uniform_address_for_loop = false;371return require_uniform_address_uniform(validated_shader);372}373374static bool375check_reg_write(struct vc4_validated_shader_info *validated_shader,376struct vc4_shader_validation_state *validation_state,377bool is_mul)378{379uint64_t inst = validation_state->shader[validation_state->ip];380uint32_t waddr = (is_mul ?381QPU_GET_FIELD(inst, QPU_WADDR_MUL) :382QPU_GET_FIELD(inst, QPU_WADDR_ADD));383uint32_t sig = QPU_GET_FIELD(inst, QPU_SIG);384bool ws = inst & QPU_WS;385bool is_b = is_mul ^ ws;386u32 lri = waddr_to_live_reg_index(waddr, is_b);387388if (lri != -1) {389uint32_t cond_add = QPU_GET_FIELD(inst, QPU_COND_ADD);390uint32_t cond_mul = QPU_GET_FIELD(inst, QPU_COND_MUL);391392if (sig == QPU_SIG_LOAD_IMM &&393QPU_GET_FIELD(inst, QPU_PACK) == QPU_PACK_A_NOP &&394((is_mul && cond_mul == QPU_COND_ALWAYS) ||395(!is_mul && cond_add == QPU_COND_ALWAYS))) {396validation_state->live_immediates[lri] =397QPU_GET_FIELD(inst, QPU_LOAD_IMM);398} else {399validation_state->live_immediates[lri] = ~0;400}401402if (live_reg_is_upper_half(lri))403validation_state->all_registers_used = true;404}405406switch (waddr) {407case QPU_W_UNIFORMS_ADDRESS:408if (is_b) {409DRM_ERROR("relative uniforms address change "410"unsupported\n");411return false;412}413414return validate_uniform_address_write(validated_shader,415validation_state,416is_mul);417418case QPU_W_TLB_COLOR_MS:419case QPU_W_TLB_COLOR_ALL:420case QPU_W_TLB_Z:421/* These only interact with the tile buffer, not main memory,422* so they're safe.423*/424return true;425426case QPU_W_TMU0_S:427case QPU_W_TMU0_T:428case QPU_W_TMU0_R:429case QPU_W_TMU0_B:430case QPU_W_TMU1_S:431case QPU_W_TMU1_T:432case QPU_W_TMU1_R:433case QPU_W_TMU1_B:434return check_tmu_write(validated_shader, validation_state,435is_mul);436437case QPU_W_HOST_INT:438case QPU_W_TMU_NOSWAP:439case QPU_W_TLB_ALPHA_MASK:440case QPU_W_MUTEX_RELEASE:441/* XXX: I haven't thought about these, so don't support them442* for now.443*/444DRM_ERROR("Unsupported waddr %d\n", waddr);445return false;446447case QPU_W_VPM_ADDR:448DRM_ERROR("General VPM DMA unsupported\n");449return false;450451case QPU_W_VPM:452case QPU_W_VPMVCD_SETUP:453/* We allow VPM setup in general, even including VPM DMA454* configuration setup, because the (unsafe) DMA can only be455* triggered by QPU_W_VPM_ADDR writes.456*/457return true;458459case QPU_W_TLB_STENCIL_SETUP:460return true;461}462463return true;464}465466static void467track_live_clamps(struct vc4_validated_shader_info *validated_shader,468struct vc4_shader_validation_state *validation_state)469{470uint64_t inst = validation_state->shader[validation_state->ip];471uint32_t op_add = QPU_GET_FIELD(inst, QPU_OP_ADD);472uint32_t waddr_add = QPU_GET_FIELD(inst, QPU_WADDR_ADD);473uint32_t waddr_mul = QPU_GET_FIELD(inst, QPU_WADDR_MUL);474uint32_t cond_add = QPU_GET_FIELD(inst, QPU_COND_ADD);475uint32_t add_a = QPU_GET_FIELD(inst, QPU_ADD_A);476uint32_t add_b = QPU_GET_FIELD(inst, QPU_ADD_B);477uint32_t raddr_a = QPU_GET_FIELD(inst, QPU_RADDR_A);478uint32_t raddr_b = QPU_GET_FIELD(inst, QPU_RADDR_B);479uint32_t sig = QPU_GET_FIELD(inst, QPU_SIG);480bool ws = inst & QPU_WS;481uint32_t lri_add_a, lri_add, lri_mul;482bool add_a_is_min_0;483484/* Check whether OP_ADD's A argumennt comes from a live MAX(x, 0),485* before we clear previous live state.486*/487lri_add_a = raddr_add_a_to_live_reg_index(inst);488add_a_is_min_0 = (lri_add_a != ~0 &&489validation_state->live_max_clamp_regs[lri_add_a]);490491/* Clear live state for registers written by our instruction. */492lri_add = waddr_to_live_reg_index(waddr_add, ws);493lri_mul = waddr_to_live_reg_index(waddr_mul, !ws);494if (lri_mul != ~0) {495validation_state->live_max_clamp_regs[lri_mul] = false;496validation_state->live_min_clamp_offsets[lri_mul] = ~0;497}498if (lri_add != ~0) {499validation_state->live_max_clamp_regs[lri_add] = false;500validation_state->live_min_clamp_offsets[lri_add] = ~0;501} else {502/* Nothing further to do for live tracking, since only ADDs503* generate new live clamp registers.504*/505return;506}507508/* Now, handle remaining live clamp tracking for the ADD operation. */509510if (cond_add != QPU_COND_ALWAYS)511return;512513if (op_add == QPU_A_MAX) {514/* Track live clamps of a value to a minimum of 0 (in either515* arg).516*/517if (sig != QPU_SIG_SMALL_IMM || raddr_b != 0 ||518(add_a != QPU_MUX_B && add_b != QPU_MUX_B)) {519return;520}521522validation_state->live_max_clamp_regs[lri_add] = true;523} else if (op_add == QPU_A_MIN) {524/* Track live clamps of a value clamped to a minimum of 0 and525* a maximum of some uniform's offset.526*/527if (!add_a_is_min_0)528return;529530if (!(add_b == QPU_MUX_A && raddr_a == QPU_R_UNIF) &&531!(add_b == QPU_MUX_B && raddr_b == QPU_R_UNIF &&532sig != QPU_SIG_SMALL_IMM)) {533return;534}535536validation_state->live_min_clamp_offsets[lri_add] =537validated_shader->uniforms_size;538}539}540541static bool542check_instruction_writes(struct vc4_validated_shader_info *validated_shader,543struct vc4_shader_validation_state *validation_state)544{545uint64_t inst = validation_state->shader[validation_state->ip];546uint32_t waddr_add = QPU_GET_FIELD(inst, QPU_WADDR_ADD);547uint32_t waddr_mul = QPU_GET_FIELD(inst, QPU_WADDR_MUL);548bool ok;549550if (is_tmu_write(waddr_add) && is_tmu_write(waddr_mul)) {551DRM_ERROR("ADD and MUL both set up textures\n");552return false;553}554555ok = (check_reg_write(validated_shader, validation_state, false) &&556check_reg_write(validated_shader, validation_state, true));557558track_live_clamps(validated_shader, validation_state);559560return ok;561}562563static bool564check_branch(uint64_t inst,565struct vc4_validated_shader_info *validated_shader,566struct vc4_shader_validation_state *validation_state,567int ip)568{569int32_t branch_imm = QPU_GET_FIELD(inst, QPU_BRANCH_TARGET);570uint32_t waddr_add = QPU_GET_FIELD(inst, QPU_WADDR_ADD);571uint32_t waddr_mul = QPU_GET_FIELD(inst, QPU_WADDR_MUL);572573if ((int)branch_imm < 0)574validation_state->needs_uniform_address_for_loop = true;575576/* We don't want to have to worry about validation of this, and577* there's no need for it.578*/579if (waddr_add != QPU_W_NOP || waddr_mul != QPU_W_NOP) {580DRM_ERROR("branch instruction at %d wrote a register.\n",581validation_state->ip);582return false;583}584585return true;586}587588static bool589check_instruction_reads(struct vc4_validated_shader_info *validated_shader,590struct vc4_shader_validation_state *validation_state)591{592uint64_t inst = validation_state->shader[validation_state->ip];593uint32_t raddr_a = QPU_GET_FIELD(inst, QPU_RADDR_A);594uint32_t raddr_b = QPU_GET_FIELD(inst, QPU_RADDR_B);595uint32_t sig = QPU_GET_FIELD(inst, QPU_SIG);596597if (raddr_a == QPU_R_UNIF ||598(raddr_b == QPU_R_UNIF && sig != QPU_SIG_SMALL_IMM)) {599/* This can't overflow the uint32_t, because we're reading 8600* bytes of instruction to increment by 4 here, so we'd601* already be OOM.602*/603validated_shader->uniforms_size += 4;604605if (validation_state->needs_uniform_address_update) {606DRM_ERROR("Uniform read with undefined uniform "607"address\n");608return false;609}610}611612if ((raddr_a >= 16 && raddr_a < 32) ||613(raddr_b >= 16 && raddr_b < 32 && sig != QPU_SIG_SMALL_IMM)) {614validation_state->all_registers_used = true;615}616617return true;618}619620/* Make sure that all branches are absolute and point within the shader, and621* note their targets for later.622*/623static bool624vc4_validate_branches(struct vc4_shader_validation_state *validation_state)625{626uint32_t max_branch_target = 0;627int ip;628int last_branch = -2;629630for (ip = 0; ip < validation_state->max_ip; ip++) {631uint64_t inst = validation_state->shader[ip];632int32_t branch_imm = QPU_GET_FIELD(inst, QPU_BRANCH_TARGET);633uint32_t sig = QPU_GET_FIELD(inst, QPU_SIG);634uint32_t after_delay_ip = ip + 4;635uint32_t branch_target_ip;636637if (sig == QPU_SIG_PROG_END) {638/* There are two delay slots after program end is639* signaled that are still executed, then we're640* finished. validation_state->max_ip is the641* instruction after the last valid instruction in the642* program.643*/644validation_state->max_ip = ip + 3;645continue;646}647648if (sig != QPU_SIG_BRANCH)649continue;650651if (ip - last_branch < 4) {652DRM_ERROR("Branch at %d during delay slots\n", ip);653return false;654}655last_branch = ip;656657if (inst & QPU_BRANCH_REG) {658DRM_ERROR("branching from register relative "659"not supported\n");660return false;661}662663if (!(inst & QPU_BRANCH_REL)) {664DRM_ERROR("relative branching required\n");665return false;666}667668/* The actual branch target is the instruction after the delay669* slots, plus whatever byte offset is in the low 32 bits of670* the instruction. Make sure we're not branching beyond the671* end of the shader object.672*/673if (branch_imm % sizeof(inst) != 0) {674DRM_ERROR("branch target not aligned\n");675return false;676};677678branch_target_ip = after_delay_ip + (branch_imm >> 3);679if (branch_target_ip >= validation_state->max_ip) {680DRM_ERROR("Branch at %d outside of shader (ip %d/%d)\n",681ip, branch_target_ip,682validation_state->max_ip);683return false;684}685set_bit(branch_target_ip, validation_state->branch_targets);686687/* Make sure that the non-branching path is also not outside688* the shader.689*/690if (after_delay_ip >= validation_state->max_ip) {691DRM_ERROR("Branch at %d continues past shader end "692"(%d/%d)\n",693ip, after_delay_ip, validation_state->max_ip);694return false;695}696set_bit(after_delay_ip, validation_state->branch_targets);697max_branch_target = max(max_branch_target, after_delay_ip);698}699700if (max_branch_target > validation_state->max_ip - 3) {701DRM_ERROR("Branch landed after QPU_SIG_PROG_END");702return false;703}704705return true;706}707708/* Resets any known state for the shader, used when we may be branched to from709* multiple locations in the program (or at shader start).710*/711static void712reset_validation_state(struct vc4_shader_validation_state *validation_state)713{714int i;715716for (i = 0; i < 8; i++)717validation_state->tmu_setup[i / 4].p_offset[i % 4] = ~0;718719for (i = 0; i < LIVE_REG_COUNT; i++) {720validation_state->live_min_clamp_offsets[i] = ~0;721validation_state->live_max_clamp_regs[i] = false;722validation_state->live_immediates[i] = ~0;723}724}725726static bool727texturing_in_progress(struct vc4_shader_validation_state *validation_state)728{729return (validation_state->tmu_write_count[0] != 0 ||730validation_state->tmu_write_count[1] != 0);731}732733static bool734vc4_handle_branch_target(struct vc4_shader_validation_state *validation_state)735{736uint32_t ip = validation_state->ip;737738if (!test_bit(ip, validation_state->branch_targets))739return true;740741if (texturing_in_progress(validation_state)) {742DRM_ERROR("Branch target landed during TMU setup\n");743return false;744}745746/* Reset our live values tracking, since this instruction may have747* multiple predecessors.748*749* One could potentially do analysis to determine that, for750* example, all predecessors have a live max clamp in the same751* register, but we don't bother with that.752*/753reset_validation_state(validation_state);754755/* Since we've entered a basic block from potentially multiple756* predecessors, we need the uniforms address to be updated before any757* unforms are read. We require that after any branch point, the next758* uniform to be loaded is a uniform address offset. That uniform's759* offset will be marked by the uniform address register write760* validation, or a one-off the end-of-program check.761*/762validation_state->needs_uniform_address_update = true;763764return true;765}766767struct vc4_validated_shader_info *768vc4_validate_shader(struct drm_gem_cma_object *shader_obj)769{770bool found_shader_end = false;771int shader_end_ip = 0;772uint32_t last_thread_switch_ip = -3;773uint32_t ip;774struct vc4_validated_shader_info *validated_shader = NULL;775struct vc4_shader_validation_state validation_state;776777memset(&validation_state, 0, sizeof(validation_state));778validation_state.shader = shader_obj->vaddr;779validation_state.max_ip = shader_obj->base.size / sizeof(uint64_t);780781reset_validation_state(&validation_state);782783validation_state.branch_targets =784kcalloc(BITS_TO_LONGS(validation_state.max_ip),785sizeof(unsigned long), GFP_KERNEL);786if (!validation_state.branch_targets)787goto fail;788789validated_shader = kcalloc(1, sizeof(*validated_shader), GFP_KERNEL);790if (!validated_shader)791goto fail;792793if (!vc4_validate_branches(&validation_state))794goto fail;795796for (ip = 0; ip < validation_state.max_ip; ip++) {797uint64_t inst = validation_state.shader[ip];798uint32_t sig = QPU_GET_FIELD(inst, QPU_SIG);799800validation_state.ip = ip;801802if (!vc4_handle_branch_target(&validation_state))803goto fail;804805if (ip == last_thread_switch_ip + 3) {806/* Reset r0-r3 live clamp data */807int i;808for (i = 64; i < LIVE_REG_COUNT; i++) {809validation_state.live_min_clamp_offsets[i] = ~0;810validation_state.live_max_clamp_regs[i] = false;811validation_state.live_immediates[i] = ~0;812}813}814815switch (sig) {816case QPU_SIG_NONE:817case QPU_SIG_WAIT_FOR_SCOREBOARD:818case QPU_SIG_SCOREBOARD_UNLOCK:819case QPU_SIG_COLOR_LOAD:820case QPU_SIG_LOAD_TMU0:821case QPU_SIG_LOAD_TMU1:822case QPU_SIG_PROG_END:823case QPU_SIG_SMALL_IMM:824case QPU_SIG_THREAD_SWITCH:825case QPU_SIG_LAST_THREAD_SWITCH:826if (!check_instruction_writes(validated_shader,827&validation_state)) {828DRM_ERROR("Bad write at ip %d\n", ip);829goto fail;830}831832if (!check_instruction_reads(validated_shader,833&validation_state))834goto fail;835836if (sig == QPU_SIG_PROG_END) {837found_shader_end = true;838shader_end_ip = ip;839}840841if (sig == QPU_SIG_THREAD_SWITCH ||842sig == QPU_SIG_LAST_THREAD_SWITCH) {843validated_shader->is_threaded = true;844845if (ip < last_thread_switch_ip + 3) {846DRM_ERROR("Thread switch too soon after "847"last switch at ip %d\n", ip);848goto fail;849}850last_thread_switch_ip = ip;851}852853break;854855case QPU_SIG_LOAD_IMM:856if (!check_instruction_writes(validated_shader,857&validation_state)) {858DRM_ERROR("Bad LOAD_IMM write at ip %d\n", ip);859goto fail;860}861break;862863case QPU_SIG_BRANCH:864if (!check_branch(inst, validated_shader,865&validation_state, ip))866goto fail;867868if (ip < last_thread_switch_ip + 3) {869DRM_ERROR("Branch in thread switch at ip %d",870ip);871goto fail;872}873874break;875default:876DRM_ERROR("Unsupported QPU signal %d at "877"instruction %d\n", sig, ip);878goto fail;879}880881/* There are two delay slots after program end is signaled882* that are still executed, then we're finished.883*/884if (found_shader_end && ip == shader_end_ip + 2)885break;886}887888if (ip == validation_state.max_ip) {889DRM_ERROR("shader failed to terminate before "890"shader BO end at %zd\n",891shader_obj->base.size);892goto fail;893}894895/* Might corrupt other thread */896if (validated_shader->is_threaded &&897validation_state.all_registers_used) {898DRM_ERROR("Shader uses threading, but uses the upper "899"half of the registers, too\n");900goto fail;901}902903/* If we did a backwards branch and we haven't emitted a uniforms904* reset since then, we still need the uniforms stream to have the905* uniforms address available so that the backwards branch can do its906* uniforms reset.907*908* We could potentially prove that the backwards branch doesn't909* contain any uses of uniforms until program exit, but that doesn't910* seem to be worth the trouble.911*/912if (validation_state.needs_uniform_address_for_loop) {913if (!require_uniform_address_uniform(validated_shader))914goto fail;915validated_shader->uniforms_size += 4;916}917918/* Again, no chance of integer overflow here because the worst case919* scenario is 8 bytes of uniforms plus handles per 8-byte920* instruction.921*/922validated_shader->uniforms_src_size =923(validated_shader->uniforms_size +9244 * validated_shader->num_texture_samples);925926kfree(validation_state.branch_targets);927928return validated_shader;929930fail:931kfree(validation_state.branch_targets);932if (validated_shader) {933kfree(validated_shader->texture_samples);934kfree(validated_shader);935}936return NULL;937}938939940