| 23 | |
| 24 | |
| 25 | class QlInterruptContext(ContextDecorator): |
| 26 | def __init__(self, ql: Qiling): |
| 27 | self.ql = ql |
| 28 | self.reg_context = ['xpsr', 'pc', 'lr', 'r12', 'r3', 'r2', 'r1', 'r0'] |
| 29 | |
| 30 | def __enter__(self): |
| 31 | for reg in self.reg_context: |
| 32 | val = self.ql.arch.regs.read(reg) |
| 33 | self.ql.arch.stack_push(val) |
| 34 | |
| 35 | if self.ql.verbose >= QL_VERBOSE.DISASM: |
| 36 | self.ql.log.info(f'Enter into interrupt') |
| 37 | |
| 38 | def __exit__(self, *exc): |
| 39 | retval = self.ql.arch.effective_pc |
| 40 | if retval & EXC_RETURN.MASK != EXC_RETURN.MASK: |
| 41 | self.ql.log.warning('Interrupt Crash') |
| 42 | self.ql.stop() |
| 43 | |
| 44 | else: |
| 45 | # Exit handler mode |
| 46 | self.ql.arch.regs.write('ipsr', 0) |
| 47 | |
| 48 | # switch the stack accroding exc_return |
| 49 | old_ctrl = self.ql.arch.regs.read('control') |
| 50 | if retval & EXC_RETURN.RETURN_SP: |
| 51 | self.ql.arch.regs.write('control', old_ctrl | CONTROL.SPSEL) |
| 52 | else: |
| 53 | self.ql.arch.regs.write('control', old_ctrl & ~CONTROL.SPSEL) |
| 54 | |
| 55 | # Restore stack |
| 56 | for reg in reversed(self.reg_context): |
| 57 | val = self.ql.arch.stack_pop() |
| 58 | if reg == 'xpsr': |
| 59 | self.ql.arch.regs.write('XPSR_NZCVQG', val) |
| 60 | else: |
| 61 | self.ql.arch.regs.write(reg, val) |
| 62 | |
| 63 | if self.ql.verbose >= QL_VERBOSE.DISASM: |
| 64 | self.ql.log.info('Exit from interrupt') |
| 65 | |
| 66 | |
| 67 | class QlArchCORTEX_M(QlArchARM): |