Commit 65ea47dc authored by Julien Thierry's avatar Julien Thierry Committed by Ingo Molnar

objtool: Support multiple stack_op per instruction

Instruction sets can include more or less complex operations which might
not fit the currently defined set of stack_ops.

Combining more than one stack_op provides more flexibility to describe
the behaviour of an instruction. This also reduces the need to define
new stack_ops specific to a single instruction set.

Allow instruction decoders to generate multiple stack_op per
instruction.
Signed-off-by: default avatarJulien Thierry <jthierry@redhat.com>
Signed-off-by: default avatarPeter Zijlstra (Intel) <peterz@infradead.org>
Reviewed-by: default avatarMiroslav Benes <mbenes@suse.cz>
Reviewed-by: default avatarAlexandre Chartre <alexandre.chartre@oracle.com>
Acked-by: default avatarJosh Poimboeuf <jpoimboe@redhat.com>
Link: https://lkml.kernel.org/r/20200327152847.15294-11-jthierry@redhat.comSigned-off-by: default avatarIngo Molnar <mingo@kernel.org>
parent 1ee44470
...@@ -64,6 +64,7 @@ struct op_src { ...@@ -64,6 +64,7 @@ struct op_src {
struct stack_op { struct stack_op {
struct op_dest dest; struct op_dest dest;
struct op_src src; struct op_src src;
struct list_head list;
}; };
struct instruction; struct instruction;
...@@ -73,7 +74,8 @@ void arch_initial_func_cfi_state(struct cfi_state *state); ...@@ -73,7 +74,8 @@ void arch_initial_func_cfi_state(struct cfi_state *state);
int arch_decode_instruction(struct elf *elf, struct section *sec, int arch_decode_instruction(struct elf *elf, struct section *sec,
unsigned long offset, unsigned int maxlen, unsigned long offset, unsigned int maxlen,
unsigned int *len, enum insn_type *type, unsigned int *len, enum insn_type *type,
unsigned long *immediate, struct stack_op *op); unsigned long *immediate,
struct list_head *ops_list);
bool arch_callee_saved_reg(unsigned char reg); bool arch_callee_saved_reg(unsigned char reg);
......
...@@ -80,13 +80,15 @@ unsigned long arch_jump_destination(struct instruction *insn) ...@@ -80,13 +80,15 @@ unsigned long arch_jump_destination(struct instruction *insn)
int arch_decode_instruction(struct elf *elf, struct section *sec, int arch_decode_instruction(struct elf *elf, struct section *sec,
unsigned long offset, unsigned int maxlen, unsigned long offset, unsigned int maxlen,
unsigned int *len, enum insn_type *type, unsigned int *len, enum insn_type *type,
unsigned long *immediate, struct stack_op *op) unsigned long *immediate,
struct list_head *ops_list)
{ {
struct insn insn; struct insn insn;
int x86_64, sign; int x86_64, sign;
unsigned char op1, op2, rex = 0, rex_b = 0, rex_r = 0, rex_w = 0, unsigned char op1, op2, rex = 0, rex_b = 0, rex_r = 0, rex_w = 0,
rex_x = 0, modrm = 0, modrm_mod = 0, modrm_rm = 0, rex_x = 0, modrm = 0, modrm_mod = 0, modrm_rm = 0,
modrm_reg = 0, sib = 0; modrm_reg = 0, sib = 0;
struct stack_op *op;
x86_64 = is_x86_64(elf); x86_64 = is_x86_64(elf);
if (x86_64 == -1) if (x86_64 == -1)
...@@ -127,6 +129,10 @@ int arch_decode_instruction(struct elf *elf, struct section *sec, ...@@ -127,6 +129,10 @@ int arch_decode_instruction(struct elf *elf, struct section *sec,
if (insn.sib.nbytes) if (insn.sib.nbytes)
sib = insn.sib.bytes[0]; sib = insn.sib.bytes[0];
op = calloc(1, sizeof(*op));
if (!op)
return -1;
switch (op1) { switch (op1) {
case 0x1: case 0x1:
...@@ -488,6 +494,11 @@ int arch_decode_instruction(struct elf *elf, struct section *sec, ...@@ -488,6 +494,11 @@ int arch_decode_instruction(struct elf *elf, struct section *sec,
*immediate = insn.immediate.nbytes ? insn.immediate.value : 0; *immediate = insn.immediate.nbytes ? insn.immediate.value : 0;
if (*type == INSN_STACK)
list_add_tail(&op->list, ops_list);
else
free(op);
return 0; return 0;
} }
......
...@@ -260,6 +260,7 @@ static int decode_instructions(struct objtool_file *file) ...@@ -260,6 +260,7 @@ static int decode_instructions(struct objtool_file *file)
} }
memset(insn, 0, sizeof(*insn)); memset(insn, 0, sizeof(*insn));
INIT_LIST_HEAD(&insn->alts); INIT_LIST_HEAD(&insn->alts);
INIT_LIST_HEAD(&insn->stack_ops);
clear_insn_state(&insn->state); clear_insn_state(&insn->state);
insn->sec = sec; insn->sec = sec;
...@@ -269,7 +270,7 @@ static int decode_instructions(struct objtool_file *file) ...@@ -269,7 +270,7 @@ static int decode_instructions(struct objtool_file *file)
sec->len - offset, sec->len - offset,
&insn->len, &insn->type, &insn->len, &insn->type,
&insn->immediate, &insn->immediate,
&insn->stack_op); &insn->stack_ops);
if (ret) if (ret)
goto err; goto err;
...@@ -754,6 +755,7 @@ static int handle_group_alt(struct objtool_file *file, ...@@ -754,6 +755,7 @@ static int handle_group_alt(struct objtool_file *file,
} }
memset(fake_jump, 0, sizeof(*fake_jump)); memset(fake_jump, 0, sizeof(*fake_jump));
INIT_LIST_HEAD(&fake_jump->alts); INIT_LIST_HEAD(&fake_jump->alts);
INIT_LIST_HEAD(&fake_jump->stack_ops);
clear_insn_state(&fake_jump->state); clear_insn_state(&fake_jump->state);
fake_jump->sec = special_alt->new_sec; fake_jump->sec = special_alt->new_sec;
...@@ -1452,10 +1454,11 @@ static bool has_valid_stack_frame(struct insn_state *state) ...@@ -1452,10 +1454,11 @@ static bool has_valid_stack_frame(struct insn_state *state)
return false; return false;
} }
static int update_insn_state_regs(struct instruction *insn, struct insn_state *state) static int update_insn_state_regs(struct instruction *insn,
struct insn_state *state,
struct stack_op *op)
{ {
struct cfi_reg *cfa = &state->cfa; struct cfi_reg *cfa = &state->cfa;
struct stack_op *op = &insn->stack_op;
if (cfa->base != CFI_SP) if (cfa->base != CFI_SP)
return 0; return 0;
...@@ -1545,9 +1548,9 @@ static void restore_reg(struct insn_state *state, unsigned char reg) ...@@ -1545,9 +1548,9 @@ static void restore_reg(struct insn_state *state, unsigned char reg)
* 41 5d pop %r13 * 41 5d pop %r13
* c3 retq * c3 retq
*/ */
static int update_insn_state(struct instruction *insn, struct insn_state *state) static int update_insn_state(struct instruction *insn, struct insn_state *state,
struct stack_op *op)
{ {
struct stack_op *op = &insn->stack_op;
struct cfi_reg *cfa = &state->cfa; struct cfi_reg *cfa = &state->cfa;
struct cfi_reg *regs = state->regs; struct cfi_reg *regs = state->regs;
...@@ -1561,7 +1564,7 @@ static int update_insn_state(struct instruction *insn, struct insn_state *state) ...@@ -1561,7 +1564,7 @@ static int update_insn_state(struct instruction *insn, struct insn_state *state)
} }
if (state->type == ORC_TYPE_REGS || state->type == ORC_TYPE_REGS_IRET) if (state->type == ORC_TYPE_REGS || state->type == ORC_TYPE_REGS_IRET)
return update_insn_state_regs(insn, state); return update_insn_state_regs(insn, state, op);
switch (op->dest.type) { switch (op->dest.type) {
...@@ -1898,6 +1901,42 @@ static int update_insn_state(struct instruction *insn, struct insn_state *state) ...@@ -1898,6 +1901,42 @@ static int update_insn_state(struct instruction *insn, struct insn_state *state)
return 0; return 0;
} }
static int handle_insn_ops(struct instruction *insn, struct insn_state *state)
{
struct stack_op *op;
list_for_each_entry(op, &insn->stack_ops, list) {
int res;
res = update_insn_state(insn, state, op);
if (res)
return res;
if (op->dest.type == OP_DEST_PUSHF) {
if (!state->uaccess_stack) {
state->uaccess_stack = 1;
} else if (state->uaccess_stack >> 31) {
WARN_FUNC("PUSHF stack exhausted",
insn->sec, insn->offset);
return 1;
}
state->uaccess_stack <<= 1;
state->uaccess_stack |= state->uaccess;
}
if (op->src.type == OP_SRC_POPF) {
if (state->uaccess_stack) {
state->uaccess = state->uaccess_stack & 1;
state->uaccess_stack >>= 1;
if (state->uaccess_stack == 1)
state->uaccess_stack = 0;
}
}
}
return 0;
}
static bool insn_state_match(struct instruction *insn, struct insn_state *state) static bool insn_state_match(struct instruction *insn, struct insn_state *state)
{ {
struct insn_state *state1 = &insn->state, *state2 = state; struct insn_state *state1 = &insn->state, *state2 = state;
...@@ -2198,29 +2237,8 @@ static int validate_branch(struct objtool_file *file, struct symbol *func, ...@@ -2198,29 +2237,8 @@ static int validate_branch(struct objtool_file *file, struct symbol *func,
return 0; return 0;
case INSN_STACK: case INSN_STACK:
if (update_insn_state(insn, &state)) if (handle_insn_ops(insn, &state))
return 1;
if (insn->stack_op.dest.type == OP_DEST_PUSHF) {
if (!state.uaccess_stack) {
state.uaccess_stack = 1;
} else if (state.uaccess_stack >> 31) {
WARN_FUNC("PUSHF stack exhausted", sec, insn->offset);
return 1; return 1;
}
state.uaccess_stack <<= 1;
state.uaccess_stack |= state.uaccess;
}
if (insn->stack_op.src.type == OP_SRC_POPF) {
if (state.uaccess_stack) {
state.uaccess = state.uaccess_stack & 1;
state.uaccess_stack >>= 1;
if (state.uaccess_stack == 1)
state.uaccess_stack = 0;
}
}
break; break;
case INSN_STAC: case INSN_STAC:
......
...@@ -42,7 +42,7 @@ struct instruction { ...@@ -42,7 +42,7 @@ struct instruction {
struct rela *jump_table; struct rela *jump_table;
struct list_head alts; struct list_head alts;
struct symbol *func; struct symbol *func;
struct stack_op stack_op; struct list_head stack_ops;
struct insn_state state; struct insn_state state;
struct orc_entry orc; struct orc_entry orc;
}; };
......
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