Path: blob/master/arch/microblaze/kernel/selfmod.c
10817 views
/*1* Copyright (C) 2007-2009 Michal Simek <[email protected]>2* Copyright (C) 2009 PetaLogix3*4* This file is subject to the terms and conditions of the GNU General Public5* License. See the file "COPYING" in the main directory of this archive6* for more details.7*/89#include <linux/interrupt.h>10#include <asm/selfmod.h>1112#undef DEBUG1314#if __GNUC__ > 315#error GCC 4 unsupported SELFMOD. Please disable SELFMOD from menuconfig.16#endif1718#define OPCODE_IMM 0xB000000019#define OPCODE_LWI 0xE800000020#define OPCODE_LWI_MASK 0xEC00000021#define OPCODE_RTSD 0xB60F0008 /* return from func: rtsd r15, 8 */22#define OPCODE_ADDIK 0x3000000023#define OPCODE_ADDIK_MASK 0xFC0000002425#define IMM_BASE (OPCODE_IMM | (BARRIER_BASE_ADDR >> 16))26#define LWI_BASE (OPCODE_LWI | (BARRIER_BASE_ADDR & 0x0000ff00))27#define LWI_BASE_MASK (OPCODE_LWI_MASK | (BARRIER_BASE_ADDR & 0x0000ff00))28#define ADDIK_BASE (OPCODE_ADDIK | (BARRIER_BASE_ADDR & 0x0000ff00))29#define ADDIK_BASE_MASK (OPCODE_ADDIK_MASK | (BARRIER_BASE_ADDR & 0x0000ff00))3031#define MODIFY_INSTR { \32pr_debug("%s: curr instr, (%d):0x%x, next(%d):0x%x\n", \33__func__, i, addr[i], i + 1, addr[i + 1]); \34addr[i] = OPCODE_IMM + (base >> 16); \35/* keep instruction opcode and add only last 16bits */ \36addr[i + 1] = (addr[i + 1] & 0xffff00ff) + (base & 0xffff); \37__invalidate_icache(addr[i]); \38__invalidate_icache(addr[i + 1]); \39pr_debug("%s: hack instr, (%d):0x%x, next(%d):0x%x\n", \40__func__, i, addr[i], i + 1, addr[i + 1]); }4142/* NOTE43* self-modified part of code for improvement of interrupt controller44* save instruction in interrupt rutine45*/46void selfmod_function(const int *arr_fce, const unsigned int base)47{48unsigned int flags, i, j, *addr = NULL;4950local_irq_save(flags);51__disable_icache();5253/* zero terminated array */54for (j = 0; arr_fce[j] != 0; j++) {55/* get start address of function */56addr = (unsigned int *) arr_fce[j];57pr_debug("%s: func(%d) at 0x%x\n",58__func__, j, (unsigned int) addr);59for (i = 0; ; i++) {60pr_debug("%s: instruction code at %d: 0x%x\n",61__func__, i, addr[i]);62if (addr[i] == IMM_BASE) {63/* detecting of lwi (0xE8) or swi (0xF8) instr64* I can detect both opcode with one mask */65if ((addr[i + 1] & LWI_BASE_MASK) == LWI_BASE) {66MODIFY_INSTR;67} else /* detection addik for ack */68if ((addr[i + 1] & ADDIK_BASE_MASK) ==69ADDIK_BASE) {70MODIFY_INSTR;71}72} else if (addr[i] == OPCODE_RTSD) {73/* return from function means end of function */74pr_debug("%s: end of array %d\n", __func__, i);75break;76}77}78}79local_irq_restore(flags);80} /* end of self-modified code */818283