/* * The runtime support for deprecated instruction support can be in one of * following three states - * * 0 = undef * 1 = emulate (software emulation) * 2 = hw (supported in hardware)
*/ enum insn_emulation_mode {
INSN_UNDEF,
INSN_EMULATE,
INSN_HW,
};
if (likely(res != -EAGAIN) || signal_pending(current)) break;
cond_resched();
}
return res;
}
/* * swp_handler logs the id of calling process, dissects the instruction, sanity * checks the memory location, calls emulate_swpX for the actual operation and * deals with fixup/error handling before returning
*/ staticint swp_handler(struct pt_regs *regs, u32 instr)
{
u32 destreg, data, type, address = 0; constvoid __user *user_ptr; int rn, rt2, res = 0;
switch (aarch32_check_condition(instr, regs->pstate)) { case ARM_OPCODE_CONDTEST_PASS: break; case ARM_OPCODE_CONDTEST_FAIL: /* Condition failed - return to next instruction */ goto ret; case ARM_OPCODE_CONDTEST_UNCOND: /* If unconditional encoding - not a SWP, undef */ return -EFAULT; default: return -EINVAL;
}
address = (u32)regs->user_regs.regs[rn];
data = (u32)regs->user_regs.regs[rt2];
destreg = aarch32_insn_extract_reg_num(instr, A32_RT_OFFSET);
pr_debug("addr in r%d->0x%08x, dest is r%d, source in r%d->0x%08x)\n",
rn, address, destreg,
aarch32_insn_extract_reg_num(instr, A32_RT2_OFFSET), data);
/* Check access in reasonable access range for both SWP and SWPB */
user_ptr = (constvoid __user *)(unsignedlong)(address & ~3); if (!access_ok(user_ptr, 4)) {
pr_debug("SWP{B} emulation: access to 0x%08x not allowed!\n",
address); goto fault;
}
res = emulate_swpX(address, &data, type); if (res == -EFAULT) goto fault; elseif (res == 0)
regs->user_regs.regs[destreg] = data;
ret: if (type == TYPE_SWPB)
trace_instruction_emulation("swpb", regs->pc); else
trace_instruction_emulation("swp", regs->pc);
staticbool try_emulate_swp(struct pt_regs *regs, u32 insn)
{ /* SWP{B} only exists in ARM state and does not exist in Thumb */ if (!compat_user_mode(regs) || compat_thumb_mode(regs)) returnfalse;
if ((insn & 0x0fb00ff0) != 0x01000090) returnfalse;
switch (aarch32_check_condition(instr, regs->pstate)) { case ARM_OPCODE_CONDTEST_PASS: break; case ARM_OPCODE_CONDTEST_FAIL: /* Condition failed - return to next instruction */ goto ret; case ARM_OPCODE_CONDTEST_UNCOND: /* If unconditional encoding - not a barrier instruction */ return -EFAULT; default: return -EINVAL;
}
switch (aarch32_insn_mcr_extract_crm(instr)) { case 10: /* * dmb - mcr p15, 0, Rt, c7, c10, 5 * dsb - mcr p15, 0, Rt, c7, c10, 4
*/ if (aarch32_insn_mcr_extract_opc2(instr) == 5) {
dmb(sy);
trace_instruction_emulation( "mcr p15, 0, Rt, c7, c10, 5 ; dmb", regs->pc);
} else {
dsb(sy);
trace_instruction_emulation( "mcr p15, 0, Rt, c7, c10, 4 ; dsb", regs->pc);
} break; case 5: /* * isb - mcr p15, 0, Rt, c7, c5, 4 * * Taking an exception or returning from one acts as an * instruction barrier. So no explicit barrier needed here.
*/
trace_instruction_emulation( "mcr p15, 0, Rt, c7, c5, 4 ; isb", regs->pc); break;
}
/* Run set_hw_mode(mode) on all active CPUs */ staticint run_all_cpu_set_hw_mode(struct insn_emulation *insn, bool enable)
{ if (!insn->set_hw_mode) return -EINVAL; if (enable)
on_each_cpu(enable_insn_hw_mode, (void *)insn, true); else
on_each_cpu(disable_insn_hw_mode, (void *)insn, true); return 0;
}
/* * Run set_hw_mode for all insns on a starting CPU. * Returns: * 0 - If all the hooks ran successfully. * -EINVAL - At least one hook is not supported by the CPU.
*/ staticint run_all_insn_set_hw_mode(unsignedint cpu)
{ int rc = 0; unsignedlong flags;
/* * Disable IRQs to serialize against an IPI from * run_all_cpu_set_hw_mode(), ensuring the HW is programmed to the most * recent enablement state if the two race with one another.
*/
local_irq_save(flags); for (int i = 0; i < ARRAY_SIZE(insn_emulations); i++) { struct insn_emulation *insn = insn_emulations[i]; bool enable = READ_ONCE(insn->current_mode) == INSN_HW; if (insn->status == INSN_UNAVAILABLE) continue;
if (insn->set_hw_mode && insn->set_hw_mode(enable)) {
pr_warn("CPU[%u] cannot support the emulation of %s",
cpu, insn->name);
rc = -EINVAL;
}
}
local_irq_restore(flags);
return rc;
}
staticint update_insn_emulation_mode(struct insn_emulation *insn, enum insn_emulation_mode prev)
{ int ret = 0;
switch (prev) { case INSN_UNDEF: /* Nothing to be done */ break; case INSN_EMULATE: break; case INSN_HW: if (!run_all_cpu_set_hw_mode(insn, false))
pr_notice("Disabled %s support\n", insn->name); break;
}
switch (insn->current_mode) { case INSN_UNDEF: break; case INSN_EMULATE: break; case INSN_HW:
ret = run_all_cpu_set_hw_mode(insn, true); if (!ret)
pr_notice("Enabled %s support\n", insn->name); break;
}
return ret;
}
staticint emulation_proc_handler(conststruct ctl_table *table, int write, void *buffer, size_t *lenp,
loff_t *ppos)
{ int ret = 0; struct insn_emulation *insn = container_of(table->data, struct insn_emulation, current_mode); enum insn_emulation_mode prev_mode = insn->current_mode;
mutex_lock(&insn_emulation_mutex);
ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
if (ret || !write || prev_mode == insn->current_mode) goto ret;
ret = update_insn_emulation_mode(insn, prev_mode); if (ret) { /* Mode change failed, revert to previous mode. */
WRITE_ONCE(insn->current_mode, prev_mode);
update_insn_emulation_mode(insn, INSN_UNDEF);
}
ret:
mutex_unlock(&insn_emulation_mutex); return ret;
}
switch (insn->status) { case INSN_DEPRECATED:
insn->current_mode = INSN_EMULATE; /* Disable the HW mode if it was turned on at early boot time */
run_all_cpu_set_hw_mode(insn, false);
insn->max = INSN_HW; break; case INSN_OBSOLETE:
insn->current_mode = INSN_UNDEF;
insn->max = INSN_EMULATE; break; case INSN_UNAVAILABLE:
insn->current_mode = INSN_UNDEF;
insn->max = INSN_UNDEF; break;
}
/* Program the HW if required */
update_insn_emulation_mode(insn, INSN_UNDEF);
if (insn->status != INSN_UNAVAILABLE) {
sysctl = &insn->sysctl;
bool try_emulate_armv8_deprecated(struct pt_regs *regs, u32 insn)
{ for (int i = 0; i < ARRAY_SIZE(insn_emulations); i++) { struct insn_emulation *ie = insn_emulations[i];
if (ie->status == INSN_UNAVAILABLE) continue;
/* * A trap may race with the mode being changed * INSN_EMULATE<->INSN_HW. Try to emulate the instruction to * avoid a spurious UNDEF.
*/ if (READ_ONCE(ie->current_mode) == INSN_UNDEF) continue;
if (ie->try_emulate(regs, insn)) returntrue;
}
returnfalse;
}
/* * Invoked as core_initcall, which guarantees that the instruction * emulation is ready for userspace.
*/ staticint __init armv8_deprecated_init(void)
{ #ifdef CONFIG_SETEND_EMULATION if (!system_supports_mixed_endian_el0()) {
insn_setend.status = INSN_UNAVAILABLE;
pr_info("setend instruction emulation is not supported on this system\n");
}
#endif for (int i = 0; i < ARRAY_SIZE(insn_emulations); i++) { struct insn_emulation *ie = insn_emulations[i];
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.