Commit 1de10cfc authored by Jon Bloomfield's avatar Jon Bloomfield Committed by Stefan Bader

UBUNTU: SAUCE: i915_bpo: drm/i915: Add gen9 BCS cmdparsing

For gen9 we enable cmdparsing on the BCS ring, specifically
to catch inadvertent accesses to sensitive registers

Unlike gen7/hsw, we use the parser only to block certain
registers. We can rely on h/w to block restricted commands,
so the command tables only provide enough info to allow the
parser to delineate each command, and identify commands that
access registers.

Note: This patch deliberately ignores checkpatch issues in
favour of matching the style of the surrounding code. We'll
correct the entire file in one go in a later patch.
Signed-off-by: default avatarJon Bloomfield <jon.bloomfield@intel.com>
Cc: Joonas Lahtinen <joonas.lahtinen@intel.com>
Cc: Rodrigo Vivi <rodrigo.vivi@intel.com>

CVE-2019-0155

[tjaalton: backport to i915_bpo
 - intel_engine_cs struct members, variables got renamed s/ring/engine/,
   follow the same renaming here.
 - i915_cmd_parser_init_ring has changed since 4.4, so add gen9_blt_reg_tables
   and use it as on the patch for 4.15.
 - Use type safe register definitions.]
Signed-off-by: default avatarTimo Aaltonen <timo.aaltonen@canonical.com>
Signed-off-by: default avatarStefan Bader <stefan.bader@canonical.com>
parent 6e38d571
...@@ -346,6 +346,47 @@ static const struct drm_i915_cmd_descriptor hsw_blt_cmds[] = { ...@@ -346,6 +346,47 @@ static const struct drm_i915_cmd_descriptor hsw_blt_cmds[] = {
CMD( MI_LOAD_SCAN_LINES_EXCL, SMI, !F, 0x3F, R ), CMD( MI_LOAD_SCAN_LINES_EXCL, SMI, !F, 0x3F, R ),
}; };
/*
* For Gen9 we can still rely on the h/w to enforce cmd security, and only
* need to re-enforce the register access checks. We therefore only need to
* teach the cmdparser how to find the end of each command, and identify
* register accesses. The table doesn't need to reject any commands, and so
* the only commands listed here are:
* 1) Those that touch registers
* 2) Those that do not have the default 8-bit length
*
* Note that the default MI length mask chosen for this table is 0xFF, not
* the 0x3F used on older devices. This is because the vast majority of MI
* cmds on Gen9 use a standard 8-bit Length field.
* All the Gen9 blitter instructions are standard 0xFF length mask, and
* none allow access to non-general registers, so in fact no BLT cmds are
* included in the table at all.
*
*/
static const struct drm_i915_cmd_descriptor gen9_blt_cmds[] = {
CMD( MI_NOOP, SMI, F, 1, S ),
CMD( MI_USER_INTERRUPT, SMI, F, 1, S ),
CMD( MI_WAIT_FOR_EVENT, SMI, F, 1, S ),
CMD( MI_FLUSH, SMI, F, 1, S ),
CMD( MI_ARB_CHECK, SMI, F, 1, S ),
CMD( MI_REPORT_HEAD, SMI, F, 1, S ),
CMD( MI_ARB_ON_OFF, SMI, F, 1, S ),
CMD( MI_SUSPEND_FLUSH, SMI, F, 1, S ),
CMD( MI_LOAD_SCAN_LINES_INCL, SMI, !F, 0x3F, S ),
CMD( MI_LOAD_SCAN_LINES_EXCL, SMI, !F, 0x3F, S ),
CMD( MI_STORE_DWORD_IMM, SMI, !F, 0x3FF, S ),
CMD( MI_LOAD_REGISTER_IMM(1), SMI, !F, 0xFF, W,
.reg = { .offset = 1, .mask = 0x007FFFFC, .step = 2 } ),
CMD( MI_UPDATE_GTT, SMI, !F, 0x3FF, S ),
CMD( MI_STORE_REGISTER_MEM_GEN8, SMI, F, 4, W,
.reg = { .offset = 1, .mask = 0x007FFFFC } ),
CMD( MI_FLUSH_DW, SMI, !F, 0x3F, S ),
CMD( MI_LOAD_REGISTER_MEM_GEN8, SMI, F, 4, W,
.reg = { .offset = 1, .mask = 0x007FFFFC } ),
CMD( MI_LOAD_REGISTER_REG, SMI, !F, 0xFF, W,
.reg = { .offset = 1, .mask = 0x007FFFFC, .step = 1 } ),
};
#undef CMD #undef CMD
#undef SMI #undef SMI
#undef S3D #undef S3D
...@@ -389,6 +430,11 @@ static const struct drm_i915_cmd_table hsw_blt_ring_cmd_table[] = { ...@@ -389,6 +430,11 @@ static const struct drm_i915_cmd_table hsw_blt_ring_cmd_table[] = {
{ hsw_blt_cmds, ARRAY_SIZE(hsw_blt_cmds) }, { hsw_blt_cmds, ARRAY_SIZE(hsw_blt_cmds) },
}; };
static const struct drm_i915_cmd_table gen9_blt_cmd_table[] = {
{ gen9_blt_cmds, ARRAY_SIZE(gen9_blt_cmds) },
};
/* /*
* Register whitelists, sorted by increasing register offset. * Register whitelists, sorted by increasing register offset.
*/ */
...@@ -427,6 +473,10 @@ struct drm_i915_reg_descriptor { ...@@ -427,6 +473,10 @@ struct drm_i915_reg_descriptor {
{ .addr = _reg(idx) }, \ { .addr = _reg(idx) }, \
{ .addr = _reg ## _UDW(idx) } { .addr = _reg ## _UDW(idx) }
#define REG64_IDX(_reg, idx) \
{ .addr = _reg(idx) }, \
{ .addr = _reg ## _UDW(idx) }
static const struct drm_i915_reg_descriptor gen7_render_regs[] = { static const struct drm_i915_reg_descriptor gen7_render_regs[] = {
REG64(GPGPU_THREADS_DISPATCHED), REG64(GPGPU_THREADS_DISPATCHED),
REG64(HS_INVOCATION_COUNT), REG64(HS_INVOCATION_COUNT),
...@@ -500,6 +550,29 @@ static const struct drm_i915_reg_descriptor gen7_blt_regs[] = { ...@@ -500,6 +550,29 @@ static const struct drm_i915_reg_descriptor gen7_blt_regs[] = {
REG32(BCS_SWCTRL), REG32(BCS_SWCTRL),
}; };
static const struct drm_i915_reg_descriptor gen9_blt_regs[] = {
REG64_IDX(RING_TIMESTAMP, RENDER_RING_BASE),
REG64_IDX(RING_TIMESTAMP, BSD_RING_BASE),
REG32(BCS_SWCTRL),
REG64_IDX(RING_TIMESTAMP, BLT_RING_BASE),
REG64_IDX(BCS_GPR, 0),
REG64_IDX(BCS_GPR, 1),
REG64_IDX(BCS_GPR, 2),
REG64_IDX(BCS_GPR, 3),
REG64_IDX(BCS_GPR, 4),
REG64_IDX(BCS_GPR, 5),
REG64_IDX(BCS_GPR, 6),
REG64_IDX(BCS_GPR, 7),
REG64_IDX(BCS_GPR, 8),
REG64_IDX(BCS_GPR, 9),
REG64_IDX(BCS_GPR, 10),
REG64_IDX(BCS_GPR, 11),
REG64_IDX(BCS_GPR, 12),
REG64_IDX(BCS_GPR, 13),
REG64_IDX(BCS_GPR, 14),
REG64_IDX(BCS_GPR, 15),
};
#undef REG64 #undef REG64
#undef REG32 #undef REG32
...@@ -525,6 +598,10 @@ static const struct drm_i915_reg_table hsw_blt_reg_tables[] = { ...@@ -525,6 +598,10 @@ static const struct drm_i915_reg_table hsw_blt_reg_tables[] = {
{ gen7_blt_regs, ARRAY_SIZE(gen7_blt_regs) }, { gen7_blt_regs, ARRAY_SIZE(gen7_blt_regs) },
}; };
static const struct drm_i915_reg_table gen9_blt_reg_tables[] = {
{ gen9_blt_regs, ARRAY_SIZE(gen9_blt_regs) },
};
static u32 gen7_render_get_cmd_length_mask(u32 cmd_header) static u32 gen7_render_get_cmd_length_mask(u32 cmd_header)
{ {
u32 client = (cmd_header & INSTR_CLIENT_MASK) >> INSTR_CLIENT_SHIFT; u32 client = (cmd_header & INSTR_CLIENT_MASK) >> INSTR_CLIENT_SHIFT;
...@@ -580,6 +657,17 @@ static u32 gen7_blt_get_cmd_length_mask(u32 cmd_header) ...@@ -580,6 +657,17 @@ static u32 gen7_blt_get_cmd_length_mask(u32 cmd_header)
return 0; return 0;
} }
static u32 gen9_blt_get_cmd_length_mask(u32 cmd_header)
{
u32 client = (cmd_header & INSTR_CLIENT_MASK) >> INSTR_CLIENT_SHIFT;
if (client == INSTR_MI_CLIENT || client == INSTR_BC_CLIENT)
return 0xFF;
DRM_DEBUG_DRIVER("CMD: Abnormal blt cmd length! 0x%08X\n", cmd_header);
return 0;
}
static bool validate_cmds_sorted(struct intel_engine_cs *engine, static bool validate_cmds_sorted(struct intel_engine_cs *engine,
const struct drm_i915_cmd_table *cmd_tables, const struct drm_i915_cmd_table *cmd_tables,
int cmd_table_count) int cmd_table_count)
...@@ -728,7 +816,7 @@ int i915_cmd_parser_init_ring(struct intel_engine_cs *engine) ...@@ -728,7 +816,7 @@ int i915_cmd_parser_init_ring(struct intel_engine_cs *engine)
int cmd_table_count; int cmd_table_count;
int ret; int ret;
if (!IS_GEN7(engine->dev)) if (!IS_GEN7(engine->dev) && !(IS_GEN9(engine->dev) && engine->id == BCS))
return 0; return 0;
switch (engine->id) { switch (engine->id) {
...@@ -758,7 +846,17 @@ int i915_cmd_parser_init_ring(struct intel_engine_cs *engine) ...@@ -758,7 +846,17 @@ int i915_cmd_parser_init_ring(struct intel_engine_cs *engine)
engine->get_cmd_length_mask = gen7_bsd_get_cmd_length_mask; engine->get_cmd_length_mask = gen7_bsd_get_cmd_length_mask;
break; break;
case BCS: case BCS:
if (IS_HASWELL(engine->dev)) { engine->get_cmd_length_mask = gen7_blt_get_cmd_length_mask;
if (IS_GEN9(engine->dev)) {
cmd_tables = gen9_blt_cmd_table;
cmd_table_count = ARRAY_SIZE(gen9_blt_cmd_table);
engine->get_cmd_length_mask =
gen9_blt_get_cmd_length_mask;
/* BCS Engine unsafe without parser */
engine->requires_cmd_parser = 1;
}
else if (IS_HASWELL(engine->dev)) {
cmd_tables = hsw_blt_ring_cmd_table; cmd_tables = hsw_blt_ring_cmd_table;
cmd_table_count = ARRAY_SIZE(hsw_blt_ring_cmd_table); cmd_table_count = ARRAY_SIZE(hsw_blt_ring_cmd_table);
} else { } else {
...@@ -766,7 +864,11 @@ int i915_cmd_parser_init_ring(struct intel_engine_cs *engine) ...@@ -766,7 +864,11 @@ int i915_cmd_parser_init_ring(struct intel_engine_cs *engine)
cmd_table_count = ARRAY_SIZE(gen7_blt_cmd_table); cmd_table_count = ARRAY_SIZE(gen7_blt_cmd_table);
} }
if (IS_HASWELL(engine->dev)) { if (IS_GEN9(engine->dev)) {
engine->reg_tables = gen9_blt_reg_tables;
engine->reg_table_count =
ARRAY_SIZE(gen9_blt_reg_tables);
} else if (IS_HASWELL(engine->dev)) {
engine->reg_tables = hsw_blt_reg_tables; engine->reg_tables = hsw_blt_reg_tables;
engine->reg_table_count = ARRAY_SIZE(hsw_blt_reg_tables); engine->reg_table_count = ARRAY_SIZE(hsw_blt_reg_tables);
} else { } else {
...@@ -774,7 +876,6 @@ int i915_cmd_parser_init_ring(struct intel_engine_cs *engine) ...@@ -774,7 +876,6 @@ int i915_cmd_parser_init_ring(struct intel_engine_cs *engine)
engine->reg_table_count = ARRAY_SIZE(ivb_blt_reg_tables); engine->reg_table_count = ARRAY_SIZE(ivb_blt_reg_tables);
} }
engine->get_cmd_length_mask = gen7_blt_get_cmd_length_mask;
break; break;
case VECS: case VECS:
cmd_tables = hsw_vebox_cmd_table; cmd_tables = hsw_vebox_cmd_table;
...@@ -1161,9 +1262,9 @@ int i915_parse_cmds(struct intel_engine_cs *engine, ...@@ -1161,9 +1262,9 @@ int i915_parse_cmds(struct intel_engine_cs *engine,
} }
/* /*
* If the batch buffer contains a chained batch, return an * We don't try to handle BATCH_BUFFER_START because it adds
* error that tells the caller to abort and dispatch the * non-trivial complexity. Instead we abort the scan and return
* workload as a non-secure batch. * and error to indicate that the batch is unsafe.
*/ */
if (desc->cmd.value == MI_BATCH_BUFFER_START) { if (desc->cmd.value == MI_BATCH_BUFFER_START) {
ret = -EACCES; ret = -EACCES;
...@@ -1185,7 +1286,7 @@ int i915_parse_cmds(struct intel_engine_cs *engine, ...@@ -1185,7 +1286,7 @@ int i915_parse_cmds(struct intel_engine_cs *engine,
} }
if (!check_cmd(engine, desc, cmd, length, &oacontrol_set)) { if (!check_cmd(engine, desc, cmd, length, &oacontrol_set)) {
ret = -EINVAL; ret = CMDPARSER_USES_GGTT(engine->dev) ? -EINVAL : -EACCES;
break; break;
} }
...@@ -1215,7 +1316,7 @@ int i915_parse_cmds(struct intel_engine_cs *engine, ...@@ -1215,7 +1316,7 @@ int i915_parse_cmds(struct intel_engine_cs *engine,
* *
* Return: the current version number of the cmd parser * Return: the current version number of the cmd parser
*/ */
int i915_cmd_parser_get_version(void) int i915_cmd_parser_get_version(struct drm_i915_private *dev_priv)
{ {
/* /*
* Command parser version history * Command parser version history
...@@ -1228,6 +1329,7 @@ int i915_cmd_parser_get_version(void) ...@@ -1228,6 +1329,7 @@ int i915_cmd_parser_get_version(void)
* 4. L3 atomic chicken bits of HSW_SCRATCH1 and HSW_ROW_CHICKEN3. * 4. L3 atomic chicken bits of HSW_SCRATCH1 and HSW_ROW_CHICKEN3.
* 5. GPGPU dispatch compute indirect registers. * 5. GPGPU dispatch compute indirect registers.
* 6. TIMESTAMP register and Haswell CS GPR registers * 6. TIMESTAMP register and Haswell CS GPR registers
* 10. Gen9 only - Supports the new ppgtt based BLIT parser
*/ */
return 6; return CMDPARSER_USES_GGTT(dev_priv) ? 6 : 10;;
} }
...@@ -204,7 +204,7 @@ static int i915_getparam(struct drm_device *dev, void *data, ...@@ -204,7 +204,7 @@ static int i915_getparam(struct drm_device *dev, void *data,
value = 1; value = 1;
break; break;
case I915_PARAM_CMD_PARSER_VERSION: case I915_PARAM_CMD_PARSER_VERSION:
value = i915_cmd_parser_get_version(); value = i915_cmd_parser_get_version(dev_priv);
break; break;
case I915_PARAM_HAS_COHERENT_PHYS_GTT: case I915_PARAM_HAS_COHERENT_PHYS_GTT:
value = 1; value = 1;
......
...@@ -3464,7 +3464,7 @@ void i915_get_extra_instdone(struct drm_device *dev, uint32_t *instdone); ...@@ -3464,7 +3464,7 @@ void i915_get_extra_instdone(struct drm_device *dev, uint32_t *instdone);
const char *i915_cache_level_str(struct drm_i915_private *i915, int type); const char *i915_cache_level_str(struct drm_i915_private *i915, int type);
/* i915_cmd_parser.c */ /* i915_cmd_parser.c */
int i915_cmd_parser_get_version(void); int i915_cmd_parser_get_version(struct drm_i915_private *dev_priv);
int i915_cmd_parser_init_ring(struct intel_engine_cs *engine); int i915_cmd_parser_init_ring(struct intel_engine_cs *engine);
void i915_cmd_parser_fini_ring(struct intel_engine_cs *engine); void i915_cmd_parser_fini_ring(struct intel_engine_cs *engine);
bool i915_needs_cmd_parser(struct intel_engine_cs *engine); bool i915_needs_cmd_parser(struct intel_engine_cs *engine);
......
...@@ -124,7 +124,8 @@ static int sanitize_enable_ppgtt(struct drm_device *dev, int enable_ppgtt) ...@@ -124,7 +124,8 @@ static int sanitize_enable_ppgtt(struct drm_device *dev, int enable_ppgtt)
(enable_ppgtt == 0 || !has_aliasing_ppgtt)) (enable_ppgtt == 0 || !has_aliasing_ppgtt))
return 0; return 0;
if (enable_ppgtt == 1) /* Full PPGTT is required by the Gen9 cmdparser */
if (enable_ppgtt == 1 && INTEL_INFO(dev)->gen != 9)
return 1; return 1;
if (enable_ppgtt == 2 && has_full_ppgtt) if (enable_ppgtt == 2 && has_full_ppgtt)
......
...@@ -567,6 +567,10 @@ static inline bool i915_mmio_reg_valid(i915_reg_t reg) ...@@ -567,6 +567,10 @@ static inline bool i915_mmio_reg_valid(i915_reg_t reg)
*/ */
#define BCS_SWCTRL _MMIO(0x22200) #define BCS_SWCTRL _MMIO(0x22200)
/* There are 16 GPR registers */
#define BCS_GPR(n) _MMIO(0x22600 + (n) * 8)
#define BCS_GPR_UDW(n) _MMIO(0x22600 + (n) * 8 + 4)
#define GPGPU_THREADS_DISPATCHED _MMIO(0x2290) #define GPGPU_THREADS_DISPATCHED _MMIO(0x2290)
#define GPGPU_THREADS_DISPATCHED_UDW _MMIO(0x2290 + 4) #define GPGPU_THREADS_DISPATCHED_UDW _MMIO(0x2290 + 4)
#define HS_INVOCATION_COUNT _MMIO(0x2300) #define HS_INVOCATION_COUNT _MMIO(0x2300)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment