/* * handle an instruction that does an unaligned memory access by emulating the * desired behaviour * - note that PC _may not_ point to the faulting instruction * (if that instruction is in a branch delay slot) * - return 0 if emulation okay, -EFAULT on existential error
*/ staticint handle_unaligned_ins(insn_size_t instruction, struct pt_regs *regs, struct mem_access *ma)
{ int ret, index, count; unsignedlong *rm, *rn; unsignedchar *src, *dst; unsignedchar __user *srcu, *dstu;
index = (instruction>>8)&15; /* 0x0F00 */
rn = ®s->regs[index];
index = (instruction>>4)&15; /* 0x00F0 */
rm = ®s->regs[index];
count = 1<<(instruction&3);
switch (count) { case 1: inc_unaligned_byte_access(); break; case 2: inc_unaligned_word_access(); break; case 4: inc_unaligned_dword_access(); break; case 8: inc_unaligned_multi_access(); break;
}
ret = -EFAULT; switch (instruction>>12) { case 0: /* mov.[bwl] to/from memory via r0+rn */ if (instruction & 8) { /* from memory */
srcu = (unsignedchar __user *)*rm;
srcu += regs->regs[0];
dst = (unsignedchar *)rn;
*(unsignedlong *)dst = 0;
if (ma->from(dst, srcu, 4)) goto fetch_fault;
ret = 0; break;
} return ret;
fetch_fault: /* Argh. Address not only misaligned but also non-existent. * Raise an EFAULT and see if it's trapped
*/
die_if_no_fixup("Fault in unaligned fixup", regs, 0); return -EFAULT;
}
/* * emulate the instruction in the delay slot * - fetches the instruction from PC+2
*/ staticinlineint handle_delayslot(struct pt_regs *regs,
insn_size_t old_instruction, struct mem_access *ma)
{
insn_size_t instruction; void __user *addr = (void __user *)(regs->pc +
instruction_size(old_instruction));
if (copy_from_user(&instruction, addr, sizeof(instruction))) { /* the instruction-fetch faulted */ if (user_mode(regs)) return -EFAULT;
/* kernel */
die("delay-slot-insn faulting in handle_unaligned_delayslot",
regs, 0);
}
/* * handle an instruction that does an unaligned memory access * - have to be careful of branch delay-slot instructions that fault * SH3: * - if the branch would be taken PC points to the branch * - if the branch would not be taken, PC points to delay-slot * SH4: * - PC always points to delayed branch * - return 0 if handled, -EFAULT if failed (may not return if in kernel)
*/
/* Macros to determine offset from current PC for branch instructions */ /* Explicit type coercion is used to force sign extension where needed */ #define SH_PC_8BIT_OFFSET(instr) ((((signedchar)(instr))*2) + 4) #define SH_PC_12BIT_OFFSET(instr) ((((signedshort)(instr<<4))>>3) + 4)
int handle_unaligned_access(insn_size_t instruction, struct pt_regs *regs, struct mem_access *ma, int expected, unsignedlong address)
{
u_int rm; int ret, index;
/* * XXX: We can't handle mixed 16/32-bit instructions yet
*/ if (instruction_size(instruction) != 2) return -EINVAL;
index = (instruction>>8)&15; /* 0x0F00 */
rm = regs->regs[index];
/* * Log the unexpected fixups, and then pass them on to perf. * * We intentionally don't report the expected cases to perf as * otherwise the trapped I/O case will skew the results too much * to be useful.
*/ if (!expected) {
unaligned_fixups_notify(current, instruction, regs);
perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS, 1,
regs, address);
}
ret = -EFAULT; switch (instruction&0xF000) { case 0x0000: if (instruction==0x000B) { /* rts */
ret = handle_delayslot(regs, instruction, ma); if (ret==0)
regs->pc = regs->pr;
} elseif ((instruction&0x00FF)==0x0023) { /* braf @Rm */
ret = handle_delayslot(regs, instruction, ma); if (ret==0)
regs->pc += rm + 4;
} elseif ((instruction&0x00FF)==0x0003) { /* bsrf @Rm */
ret = handle_delayslot(regs, instruction, ma); if (ret==0) {
regs->pr = regs->pc + 4;
regs->pc += rm + 4;
}
} else { /* mov.[bwl] to/from memory via r0+rn */ goto simple;
} break;
case 0x1000: /* mov.l Rm,@(disp,Rn) */ goto simple;
case 0x2000: /* mov.[bwl] to memory, possibly with pre-decrement */ goto simple;
case 0x4000: if ((instruction&0x00FF)==0x002B) { /* jmp @Rm */
ret = handle_delayslot(regs, instruction, ma); if (ret==0)
regs->pc = rm;
} elseif ((instruction&0x00FF)==0x000B) { /* jsr @Rm */
ret = handle_delayslot(regs, instruction, ma); if (ret==0) {
regs->pr = regs->pc + 4;
regs->pc = rm;
}
} else { /* mov.[bwl] to/from memory via r0+rn */ goto simple;
} break;
case 0x5000: /* mov.l @(disp,Rm),Rn */ goto simple;
case 0x6000: /* mov.[bwl] from memory, possibly with post-increment */ goto simple;
case 0x8000: /* bf lab, bf/s lab, bt lab, bt/s lab */ switch (instruction&0x0F00) { case 0x0100: /* mov.w R0,@(disp,Rm) */ goto simple; case 0x0500: /* mov.w @(disp,Rm),R0 */ goto simple; case 0x0B00: /* bf lab - no delayslot*/
ret = 0; break; case 0x0F00: /* bf/s lab */
ret = handle_delayslot(regs, instruction, ma); if (ret==0) { #ifdefined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB) if ((regs->sr & 0x00000001) != 0)
regs->pc += 4; /* next after slot */ else #endif
regs->pc += SH_PC_8BIT_OFFSET(instruction);
} break; case 0x0900: /* bt lab - no delayslot */
ret = 0; break; case 0x0D00: /* bt/s lab */
ret = handle_delayslot(regs, instruction, ma); if (ret==0) { #ifdefined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB) if ((regs->sr & 0x00000001) == 0)
regs->pc += 4; /* next after slot */ else #endif
regs->pc += SH_PC_8BIT_OFFSET(instruction);
} break;
} break;
case 0x9000: /* mov.w @(disp,Rm),Rn */ goto simple;
case 0xA000: /* bra label */
ret = handle_delayslot(regs, instruction, ma); if (ret==0)
regs->pc += SH_PC_12BIT_OFFSET(instruction); break;
case 0xB000: /* bsr label */
ret = handle_delayslot(regs, instruction, ma); if (ret==0) {
regs->pr = regs->pc + 4;
regs->pc += SH_PC_12BIT_OFFSET(instruction);
} break;
/* handle non-delay-slot instruction */
simple:
ret = handle_unaligned_ins(instruction, regs, ma); if (ret==0)
regs->pc += instruction_size(instruction); return ret;
}
/* * Handle various address error exceptions: * - instruction address error: * misaligned PC * PC >= 0x80000000 in user mode * - data address error (read and write) * misaligned data access * access to >= 0x80000000 is user mode * Unfortuntaly we can't distinguish between instruction address error * and data address errors caused by read accesses.
*/
asmlinkage void do_address_error(struct pt_regs *regs, unsignedlong writeaccess, unsignedlong address)
{ unsignedlong error_code = 0;
insn_size_t instruction; int tmp;
if (regs->pc & 1)
die("unaligned program counter", regs, error_code);
if (copy_from_kernel_nofault(&instruction, (void *)(regs->pc), sizeof(instruction))) { /* Argh. Fault on the instruction itself. This should never happen non-SMP
*/
die("insn faulting in do_address_error", regs, 0);
}
#ifdef CONFIG_CPU_SH2A
asmlinkage void do_divide_error(unsignedlong r4)
{ int code;
switch (r4) { case TRAP_DIVZERO_ERROR:
code = FPE_INTDIV; break; case TRAP_DIVOVF_ERROR:
code = FPE_INTOVF; break; default: /* Let gcc know unhandled cases don't make it past here */ return;
}
force_sig_fault(SIGFPE, code, NULL);
} #endif
#ifdefined(CONFIG_CPU_SH4) && !defined(CONFIG_SH_FPU) || \ defined(CONFIG_SH_FPU_EMU) /* * For SH-4 lacking an FPU, treat floating point instructions as * reserved. They'll be handled in the math-emu case, or faulted on * otherwise.
*/
set_exception_table_evt(0x800, do_reserved_inst);
set_exception_table_evt(0x820, do_illegal_slot_inst); #elifdefined(CONFIG_SH_FPU)
set_exception_table_evt(0x800, fpu_state_restore_trap_handler);
set_exception_table_evt(0x820, fpu_state_restore_trap_handler); #endif
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.