Nikolay Igotti
SPARC ASIs
One, not so frequently used, yet useful feature of SPARC CPUs is ASI, or address space identifier, essentially just a 8-bit tag attached to memory address, modifying the way how memory access happens. They could be used for different purposes, two useful for non-kernel programming are non-faulting reads and hardware accelerated little-to-big endian conversion. In following example I'll use ASI to load value from variable in little endian byte order, and then will safely dereference NULL pointer.You may find more info on ASIs in SPARCv9 manual.
#include <stdio.h>
volatile int var = 0x12345678;
volatile int as_le(int* addr) {
int rv;
__asm__ (
"lda [%0] 0x88, %1\n\t"
: "=r" (rv)
: "r"(addr)
);
return rv;
}
int as_le_test(int* addr) {
int x = *addr;
return
(((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) |
(((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24);
}
int load_nf(int* addr) {
int rv;
__asm__ (
"lda [%0] 0x82, %1\n\t"
: "=r" (rv)
: "r"(addr)
);
return rv;
}
int main() {
int* p = (int*)&var;
printf("var: %x as LE: %x should be: %x\n",
*p, as_le(p), as_le_test(p));
printf("nf=%d var=%d\n", load_nf(0), load_nf(p));
return 0;
}
Posted at 09:33PM Jun 29, 2007 by nike in Sun | Comments[2]
Friday Jun 29, 2007
Posted by J McLaughlin on June 30, 2007 at 07:13 PM MSD #
Posted by nike on July 02, 2007 at 02:06 PM MSD #