123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782 |
- #include <linux/smp.h>
- #include <linux/ptrace.h>
- #include <linux/slab.h>
- #include <linux/thread_info.h>
- #include <linux/uaccess.h>
- #include <linux/mman.h>
- #include <linux/types.h>
- #include <linux/err.h>
- #include <linux/prctl.h>
- #include <asm/cacheflush.h>
- #include <asm/traps.h>
- #include <asm/uaccess.h>
- #include <asm/unaligned.h>
- #include <arch/abi.h>
- #include <arch/spr_def.h>
- #include <arch/opcode.h>
- #ifndef __tilegx__
- #define signExtend17(val) sign_extend((val), 17)
- #define TILE_X1_MASK (0xffffffffULL << 31)
- enum mem_op {
- MEMOP_NONE,
- MEMOP_LOAD,
- MEMOP_STORE,
- MEMOP_LOAD_POSTINCR,
- MEMOP_STORE_POSTINCR
- };
- static inline tilepro_bundle_bits set_BrOff_X1(tilepro_bundle_bits n,
- s32 offset)
- {
- tilepro_bundle_bits result;
-
- tilepro_bundle_bits mask = create_BrOff_X1(-1);
- result = n & (~mask);
-
- result |= create_BrOff_X1(offset);
- return result;
- }
- static inline tilepro_bundle_bits move_X1(tilepro_bundle_bits n, int dest,
- int src)
- {
- tilepro_bundle_bits result;
- tilepro_bundle_bits op;
- result = n & (~TILE_X1_MASK);
- op = create_Opcode_X1(SPECIAL_0_OPCODE_X1) |
- create_RRROpcodeExtension_X1(OR_SPECIAL_0_OPCODE_X1) |
- create_Dest_X1(dest) |
- create_SrcB_X1(TREG_ZERO) |
- create_SrcA_X1(src) ;
- result |= op;
- return result;
- }
- static inline tilepro_bundle_bits nop_X1(tilepro_bundle_bits n)
- {
- return move_X1(n, TREG_ZERO, TREG_ZERO);
- }
- static inline tilepro_bundle_bits addi_X1(
- tilepro_bundle_bits n, int dest, int src, int imm)
- {
- n &= ~TILE_X1_MASK;
- n |= (create_SrcA_X1(src) |
- create_Dest_X1(dest) |
- create_Imm8_X1(imm) |
- create_S_X1(0) |
- create_Opcode_X1(IMM_0_OPCODE_X1) |
- create_ImmOpcodeExtension_X1(ADDI_IMM_0_OPCODE_X1));
- return n;
- }
- static tilepro_bundle_bits rewrite_load_store_unaligned(
- struct single_step_state *state,
- tilepro_bundle_bits bundle,
- struct pt_regs *regs,
- enum mem_op mem_op,
- int size, int sign_ext)
- {
- unsigned char __user *addr;
- int val_reg, addr_reg, err, val;
- int align_ctl;
- align_ctl = unaligned_fixup;
- switch (task_thread_info(current)->align_ctl) {
- case PR_UNALIGN_NOPRINT:
- align_ctl = 1;
- break;
- case PR_UNALIGN_SIGBUS:
- align_ctl = 0;
- break;
- }
-
- if (bundle & TILEPRO_BUNDLE_Y_ENCODING_MASK) {
- addr_reg = get_SrcA_Y2(bundle);
- val_reg = get_SrcBDest_Y2(bundle);
- } else if (mem_op == MEMOP_LOAD || mem_op == MEMOP_LOAD_POSTINCR) {
- addr_reg = get_SrcA_X1(bundle);
- val_reg = get_Dest_X1(bundle);
- } else {
- addr_reg = get_SrcA_X1(bundle);
- val_reg = get_SrcB_X1(bundle);
- }
-
- if ((val_reg >= PTREGS_NR_GPRS &&
- (val_reg != TREG_ZERO ||
- mem_op == MEMOP_LOAD ||
- mem_op == MEMOP_LOAD_POSTINCR)) ||
- addr_reg >= PTREGS_NR_GPRS)
- return bundle;
-
- addr = (void __user *)regs->regs[addr_reg];
- if (((unsigned long)addr % size) == 0)
- return bundle;
-
- if (align_ctl == 0) {
- siginfo_t info = {
- .si_signo = SIGBUS,
- .si_code = BUS_ADRALN,
- .si_addr = addr
- };
- trace_unhandled_signal("unaligned trap", regs,
- (unsigned long)addr, SIGBUS);
- force_sig_info(info.si_signo, &info, current);
- return (tilepro_bundle_bits) 0;
- }
-
- if (mem_op == MEMOP_LOAD || mem_op == MEMOP_LOAD_POSTINCR) {
- unsigned short val_16;
- switch (size) {
- case 2:
- err = copy_from_user(&val_16, addr, sizeof(val_16));
- val = sign_ext ? ((short)val_16) : val_16;
- break;
- case 4:
- err = copy_from_user(&val, addr, sizeof(val));
- break;
- default:
- BUG();
- }
- if (err == 0) {
- state->update_reg = val_reg;
- state->update_value = val;
- state->update = 1;
- }
- } else {
- unsigned short val_16;
- val = (val_reg == TREG_ZERO) ? 0 : regs->regs[val_reg];
- switch (size) {
- case 2:
- val_16 = val;
- err = copy_to_user(addr, &val_16, sizeof(val_16));
- break;
- case 4:
- err = copy_to_user(addr, &val, sizeof(val));
- break;
- default:
- BUG();
- }
- }
- if (err) {
- siginfo_t info = {
- .si_signo = SIGBUS,
- .si_code = BUS_ADRALN,
- .si_addr = addr
- };
- trace_unhandled_signal("bad address for unaligned fixup", regs,
- (unsigned long)addr, SIGBUS);
- force_sig_info(info.si_signo, &info, current);
- return (tilepro_bundle_bits) 0;
- }
- if (unaligned_printk || unaligned_fixup_count == 0) {
- pr_info("Process %d/%s: PC %#lx: Fixup of unaligned %s at %#lx\n",
- current->pid, current->comm, regs->pc,
- mem_op == MEMOP_LOAD || mem_op == MEMOP_LOAD_POSTINCR ?
- "load" : "store",
- (unsigned long)addr);
- if (!unaligned_printk) {
- #define P pr_info
- P("\n");
- P("Unaligned fixups in the kernel will slow your application considerably.\n");
- P("To find them, write a \"1\" to /proc/sys/tile/unaligned_fixup/printk,\n");
- P("which requests the kernel show all unaligned fixups, or write a \"0\"\n");
- P("to /proc/sys/tile/unaligned_fixup/enabled, in which case each unaligned\n");
- P("access will become a SIGBUS you can debug. No further warnings will be\n");
- P("shown so as to avoid additional slowdown, but you can track the number\n");
- P("of fixups performed via /proc/sys/tile/unaligned_fixup/count.\n");
- P("Use the tile-addr2line command (see \"info addr2line\") to decode PCs.\n");
- P("\n");
- #undef P
- }
- }
- ++unaligned_fixup_count;
- if (bundle & TILEPRO_BUNDLE_Y_ENCODING_MASK) {
-
- bundle &= ~(create_SrcBDest_Y2(-1) |
- create_Opcode_Y2(-1));
- bundle |= (create_SrcBDest_Y2(TREG_ZERO) |
- create_Opcode_Y2(LW_OPCODE_Y2));
-
- } else if (mem_op == MEMOP_LOAD_POSTINCR) {
- bundle = addi_X1(bundle, addr_reg, addr_reg,
- get_Imm8_X1(bundle));
-
- } else if (mem_op == MEMOP_STORE_POSTINCR) {
- bundle = addi_X1(bundle, addr_reg, addr_reg,
- get_Dest_Imm8_X1(bundle));
- } else {
-
- bundle &= ~(create_Opcode_X1(-1) |
- create_UnShOpcodeExtension_X1(-1) |
- create_UnOpcodeExtension_X1(-1));
- bundle |= (create_Opcode_X1(SHUN_0_OPCODE_X1) |
- create_UnShOpcodeExtension_X1(
- UN_0_SHUN_0_OPCODE_X1) |
- create_UnOpcodeExtension_X1(
- NOP_UN_0_SHUN_0_OPCODE_X1));
- }
- return bundle;
- }
- void single_step_execve(void)
- {
- struct thread_info *ti = current_thread_info();
- kfree(ti->step_state);
- ti->step_state = NULL;
- }
- void single_step_once(struct pt_regs *regs)
- {
- extern tilepro_bundle_bits __single_step_ill_insn;
- extern tilepro_bundle_bits __single_step_j_insn;
- extern tilepro_bundle_bits __single_step_addli_insn;
- extern tilepro_bundle_bits __single_step_auli_insn;
- struct thread_info *info = (void *)current_thread_info();
- struct single_step_state *state = info->step_state;
- int is_single_step = test_ti_thread_flag(info, TIF_SINGLESTEP);
- tilepro_bundle_bits __user *buffer, *pc;
- tilepro_bundle_bits bundle;
- int temp_reg;
- int target_reg = TREG_LR;
- int err;
- enum mem_op mem_op = MEMOP_NONE;
- int size = 0, sign_ext = 0;
- int align_ctl;
- align_ctl = unaligned_fixup;
- switch (task_thread_info(current)->align_ctl) {
- case PR_UNALIGN_NOPRINT:
- align_ctl = 1;
- break;
- case PR_UNALIGN_SIGBUS:
- align_ctl = 0;
- break;
- }
- asm(
- " .pushsection .rodata.single_step\n"
- " .align 8\n"
- " .globl __single_step_ill_insn\n"
- "__single_step_ill_insn:\n"
- " ill\n"
- " .globl __single_step_addli_insn\n"
- "__single_step_addli_insn:\n"
- " { nop; addli r0, zero, 0 }\n"
- " .globl __single_step_auli_insn\n"
- "__single_step_auli_insn:\n"
- " { nop; auli r0, r0, 0 }\n"
- " .globl __single_step_j_insn\n"
- "__single_step_j_insn:\n"
- " j .\n"
- " .popsection\n"
- );
-
- local_irq_enable();
- if (state == NULL) {
-
- state = kmalloc(sizeof(struct single_step_state), GFP_KERNEL);
- if (state == NULL) {
- pr_err("Out of kernel memory trying to single-step\n");
- return;
- }
-
- buffer = (void __user *) vm_mmap(NULL, 0, 64,
- PROT_EXEC | PROT_READ | PROT_WRITE,
- MAP_PRIVATE | MAP_ANONYMOUS,
- 0);
- if (IS_ERR((void __force *)buffer)) {
- kfree(state);
- pr_err("Out of kernel pages trying to single-step\n");
- return;
- }
- state->buffer = buffer;
- state->is_enabled = 0;
- info->step_state = state;
-
- BUG_ON(get_Opcode_X1(__single_step_addli_insn) !=
- ADDLI_OPCODE_X1);
- BUG_ON(get_Opcode_X1(__single_step_auli_insn) !=
- AULI_OPCODE_X1);
- BUG_ON(get_SrcA_X1(__single_step_addli_insn) != TREG_ZERO);
- BUG_ON(get_Dest_X1(__single_step_addli_insn) != 0);
- BUG_ON(get_JOffLong_X1(__single_step_j_insn) != 0);
- }
-
- if (regs->faultnum == INT_SWINT_1)
- regs->pc -= 8;
- pc = (tilepro_bundle_bits __user *)(regs->pc);
- if (get_user(bundle, pc) != 0) {
- pr_err("Couldn't read instruction at %p trying to step\n", pc);
- return;
- }
-
- state->orig_pc = (unsigned long)pc;
- state->next_pc = (unsigned long)(pc + 1);
- state->branch_next_pc = 0;
- state->update = 0;
- if (!(bundle & TILEPRO_BUNDLE_Y_ENCODING_MASK)) {
-
- int opcode = get_Opcode_X1(bundle);
- switch (opcode) {
-
- case BRANCH_OPCODE_X1:
- {
- s32 offset = signExtend17(get_BrOff_X1(bundle));
-
- state->branch_next_pc = (unsigned long)(pc + offset);
-
- bundle = set_BrOff_X1(bundle, 2);
- }
- break;
-
- case JALB_OPCODE_X1:
- case JALF_OPCODE_X1:
- state->update = 1;
- state->next_pc =
- (unsigned long) (pc + get_JOffLong_X1(bundle));
- break;
- case JB_OPCODE_X1:
- case JF_OPCODE_X1:
- state->next_pc =
- (unsigned long) (pc + get_JOffLong_X1(bundle));
- bundle = nop_X1(bundle);
- break;
- case SPECIAL_0_OPCODE_X1:
- switch (get_RRROpcodeExtension_X1(bundle)) {
-
- case JALRP_SPECIAL_0_OPCODE_X1:
- case JALR_SPECIAL_0_OPCODE_X1:
- state->update = 1;
- state->next_pc =
- regs->regs[get_SrcA_X1(bundle)];
- break;
- case JRP_SPECIAL_0_OPCODE_X1:
- case JR_SPECIAL_0_OPCODE_X1:
- state->next_pc =
- regs->regs[get_SrcA_X1(bundle)];
- bundle = nop_X1(bundle);
- break;
- case LNK_SPECIAL_0_OPCODE_X1:
- state->update = 1;
- target_reg = get_Dest_X1(bundle);
- break;
-
- case SH_SPECIAL_0_OPCODE_X1:
- mem_op = MEMOP_STORE;
- size = 2;
- break;
- case SW_SPECIAL_0_OPCODE_X1:
- mem_op = MEMOP_STORE;
- size = 4;
- break;
- }
- break;
-
- case SHUN_0_OPCODE_X1:
- if (get_UnShOpcodeExtension_X1(bundle) ==
- UN_0_SHUN_0_OPCODE_X1) {
- switch (get_UnOpcodeExtension_X1(bundle)) {
- case LH_UN_0_SHUN_0_OPCODE_X1:
- mem_op = MEMOP_LOAD;
- size = 2;
- sign_ext = 1;
- break;
- case LH_U_UN_0_SHUN_0_OPCODE_X1:
- mem_op = MEMOP_LOAD;
- size = 2;
- sign_ext = 0;
- break;
- case LW_UN_0_SHUN_0_OPCODE_X1:
- mem_op = MEMOP_LOAD;
- size = 4;
- break;
- case IRET_UN_0_SHUN_0_OPCODE_X1:
- {
- unsigned long ex0_0 = __insn_mfspr(
- SPR_EX_CONTEXT_0_0);
- unsigned long ex0_1 = __insn_mfspr(
- SPR_EX_CONTEXT_0_1);
-
- if (EX1_PL(ex0_1) == USER_PL) {
- state->next_pc = ex0_0;
- regs->ex1 = ex0_1;
- bundle = nop_X1(bundle);
- }
- }
- }
- }
- break;
-
- case IMM_0_OPCODE_X1:
- switch (get_ImmOpcodeExtension_X1(bundle)) {
- case LWADD_IMM_0_OPCODE_X1:
- mem_op = MEMOP_LOAD_POSTINCR;
- size = 4;
- break;
- case LHADD_IMM_0_OPCODE_X1:
- mem_op = MEMOP_LOAD_POSTINCR;
- size = 2;
- sign_ext = 1;
- break;
- case LHADD_U_IMM_0_OPCODE_X1:
- mem_op = MEMOP_LOAD_POSTINCR;
- size = 2;
- sign_ext = 0;
- break;
- case SWADD_IMM_0_OPCODE_X1:
- mem_op = MEMOP_STORE_POSTINCR;
- size = 4;
- break;
- case SHADD_IMM_0_OPCODE_X1:
- mem_op = MEMOP_STORE_POSTINCR;
- size = 2;
- break;
- default:
- break;
- }
- break;
- }
- if (state->update) {
-
- u32 mask = (u32) ~((1ULL << get_Dest_X0(bundle)) |
- (1ULL << get_SrcA_X0(bundle)) |
- (1ULL << get_SrcB_X0(bundle)) |
- (1ULL << target_reg));
- temp_reg = __builtin_ctz(mask);
- state->update_reg = temp_reg;
- state->update_value = regs->regs[temp_reg];
- regs->regs[temp_reg] = (unsigned long) (pc+1);
- regs->flags |= PT_FLAGS_RESTORE_REGS;
- bundle = move_X1(bundle, target_reg, temp_reg);
- }
- } else {
- int opcode = get_Opcode_Y2(bundle);
- switch (opcode) {
-
- case LH_OPCODE_Y2:
- mem_op = MEMOP_LOAD;
- size = 2;
- sign_ext = 1;
- break;
- case LH_U_OPCODE_Y2:
- mem_op = MEMOP_LOAD;
- size = 2;
- sign_ext = 0;
- break;
- case LW_OPCODE_Y2:
- mem_op = MEMOP_LOAD;
- size = 4;
- break;
-
- case SH_OPCODE_Y2:
- mem_op = MEMOP_STORE;
- size = 2;
- break;
- case SW_OPCODE_Y2:
- mem_op = MEMOP_STORE;
- size = 4;
- break;
- }
- }
-
- if (mem_op != MEMOP_NONE && align_ctl >= 0) {
- bundle = rewrite_load_store_unaligned(state, bundle, regs,
- mem_op, size, sign_ext);
- if (bundle == 0)
- return;
- }
-
- buffer = state->buffer;
- err = __put_user(bundle, buffer++);
-
- if (is_single_step) {
- err |= __put_user(__single_step_ill_insn, buffer++);
- err |= __put_user(__single_step_ill_insn, buffer++);
- } else {
- long delta;
- if (state->update) {
-
- int ha16;
- bundle = __single_step_addli_insn;
- bundle |= create_Dest_X1(state->update_reg);
- bundle |= create_Imm16_X1(state->update_value);
- err |= __put_user(bundle, buffer++);
- bundle = __single_step_auli_insn;
- bundle |= create_Dest_X1(state->update_reg);
- bundle |= create_SrcA_X1(state->update_reg);
- ha16 = (state->update_value + 0x8000) >> 16;
- bundle |= create_Imm16_X1(ha16);
- err |= __put_user(bundle, buffer++);
- state->update = 0;
- }
-
- delta = ((regs->pc + TILEPRO_BUNDLE_SIZE_IN_BYTES) -
- (unsigned long)buffer) >>
- TILEPRO_LOG2_BUNDLE_ALIGNMENT_IN_BYTES;
- bundle = __single_step_j_insn;
- bundle |= create_JOffLong_X1(delta);
- err |= __put_user(bundle, buffer++);
- }
- if (err) {
- pr_err("Fault when writing to single-step buffer\n");
- return;
- }
-
- __flush_icache_range((unsigned long)state->buffer,
- (unsigned long)buffer);
-
- state->is_enabled = is_single_step;
- regs->pc = (unsigned long)state->buffer;
-
- if (regs->faultnum == INT_SWINT_1)
- regs->pc += 8;
- }
- #else
- static DEFINE_PER_CPU(unsigned long, ss_saved_pc);
- void gx_singlestep_handle(struct pt_regs *regs, int fault_num)
- {
- unsigned long *ss_pc = this_cpu_ptr(&ss_saved_pc);
- struct thread_info *info = (void *)current_thread_info();
- int is_single_step = test_ti_thread_flag(info, TIF_SINGLESTEP);
- unsigned long control = __insn_mfspr(SPR_SINGLE_STEP_CONTROL_K);
- if (is_single_step == 0) {
- __insn_mtspr(SPR_SINGLE_STEP_EN_K_K, 0);
- } else if ((*ss_pc != regs->pc) ||
- (!(control & SPR_SINGLE_STEP_CONTROL_1__CANCELED_MASK))) {
- control |= SPR_SINGLE_STEP_CONTROL_1__CANCELED_MASK;
- control |= SPR_SINGLE_STEP_CONTROL_1__INHIBIT_MASK;
- __insn_mtspr(SPR_SINGLE_STEP_CONTROL_K, control);
- send_sigtrap(current, regs);
- }
- }
- void single_step_once(struct pt_regs *regs)
- {
- unsigned long *ss_pc = this_cpu_ptr(&ss_saved_pc);
- unsigned long control = __insn_mfspr(SPR_SINGLE_STEP_CONTROL_K);
- *ss_pc = regs->pc;
- control |= SPR_SINGLE_STEP_CONTROL_1__CANCELED_MASK;
- control |= SPR_SINGLE_STEP_CONTROL_1__INHIBIT_MASK;
- __insn_mtspr(SPR_SINGLE_STEP_CONTROL_K, control);
- __insn_mtspr(SPR_SINGLE_STEP_EN_K_K, 1 << USER_PL);
- }
- void single_step_execve(void)
- {
-
- }
- #endif
|