#ifndef _SPARC64_ELF_SUPPORT_H
#define _SPARC64_ELF_SUPPORT_H
#ifdef __arch64__
static inline void
sparc_write_branch(void *where_, void *target)
{
const unsigned int BAA = 0x30800000U;
const unsigned int SETHI = 0x03000000U;
const unsigned int JMP = 0x81c06000U;
const unsigned int OR = 0x82106000U;
const unsigned int XOR = 0x82186000U;
const unsigned int MOV71 = 0x8213e000U;
const unsigned int MOV17 = 0x9e106000U;
const unsigned int CALL = 0x40000000U;
const unsigned int SLLX = 0x83287000U;
const unsigned int NEG = 0x82200001U;
const unsigned int SETHIG5 = 0x0b000000U;
const unsigned int ORG5 = 0x82104005U;
unsigned int *where = (unsigned int *)where_;
unsigned long value = (unsigned long)target;
unsigned long offset = value - (unsigned long)where;
#define HIVAL(v, s) (((v) >> (s)) & 0x003fffffU)
#define LOVAL(v, s) (((v) >> (s)) & 0x000003ffU)
if (offset + 0x800000 <= 0x7ffffc) {
where[0] = BAA | ((offset >> 2) & 0x3fffff);
__asm volatile("iflush %0+0" : : "r" (where));
return;
}
if (value <= 0xffffffffUL) {
where[1] = JMP | LOVAL(value, 0);
__asm volatile("iflush %0+4" : : "r" (where));
where[0] = SETHI | HIVAL(value, 10);
__asm volatile("iflush %0+0" : : "r" (where));
return;
}
if (value >= 0xffffffff00000000UL) {
where[2] = JMP;
where[1] = XOR | (value & 0x00003ff) | 0x1c00;
__asm volatile("iflush %0+4" : : "r" (where));
__asm volatile("iflush %0+8" : : "r" (where));
where[0] = SETHI | HIVAL(~value, 10);
__asm volatile("iflush %0+0" : : "r" (where));
return;
}
if ((offset + 4) + 0x80000000UL <= 0x100000000UL) {
where[1] = CALL | ((-(offset + 4)>> 2) & 0x3fffffffU);
where[2] = MOV17;
__asm volatile("iflush %0+4" : : "r" (where));
__asm volatile("iflush %0+8" : : "r" (where));
where[0] = MOV71;
__asm volatile("iflush %0+0" : : "r" (where));
return;
}
if (value < 0x100000000000UL) {
where[1] = OR | (((value) >> 12) & 0x00001fff);
where[2] = SLLX | 12;
where[3] = JMP | LOVAL(value, 0);
__asm volatile("iflush %0+4" : : "r" (where));
__asm volatile("iflush %0+8" : : "r" (where));
__asm volatile("iflush %0+12" : : "r" (where));
where[0] = SETHI | HIVAL(value, 22);
__asm volatile("iflush %0+0" : : "r" (where));
return;
}
if (value > 0xfffff00000000000UL) {
unsigned long neg = (-value)>>12;
where[1] = OR | (LOVAL(neg, 0)+1);
where[2] = NEG;
where[3] = SLLX | 12;
where[4] = JMP | (value & 0x0fff);
__asm volatile("iflush %0+4" : : "r" (where));
__asm volatile("iflush %0+8" : : "r" (where));
__asm volatile("iflush %0+12" : : "r" (where));
__asm volatile("iflush %0+16" : : "r" (where));
where[0] = SETHI | HIVAL(neg, 10);
__asm volatile("iflush %0+0" : : "r" (where));
return;
}
where[1] = SETHIG5 | HIVAL(value, 10);
where[2] = OR | LOVAL(value, 32);
where[3] = SLLX | 32;
where[4] = ORG5;
where[5] = JMP | LOVAL(value, 0);
__asm volatile("iflush %0+4" : : "r" (where));
__asm volatile("iflush %0+8" : : "r" (where));
__asm volatile("iflush %0+12" : : "r" (where));
__asm volatile("iflush %0+16" : : "r" (where));
__asm volatile("iflush %0+20" : : "r" (where));
where[0] = SETHI | HIVAL(value, 42);
__asm volatile("iflush %0+0" : : "r" (where));
#undef HIVAL
#undef LOVAL
}
#else
#include <sparc/elf_support.h>
#endif
#endif