Skip to content

Instantly share code, notes, and snippets.

@jrziviani
Created February 2, 2018 20:37
Show Gist options
  • Save jrziviani/a65e71c5d661bffa8afcd6710fedd520 to your computer and use it in GitHub Desktop.
Save jrziviani/a65e71c5d661bffa8afcd6710fedd520 to your computer and use it in GitHub Desktop.
#include <sys/mman.h>
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <fcntl.h>
#include <altivec.h>
#include <stdlib.h>
#define DEVICE "/dev/mem"
#define ulong unsigned long
typedef __int128_t __attribute__((__vector_size__(16))) vector_128_t;
typedef union {
vector_128_t v;
unsigned char uc[sizeof(__int128_t)];
} v128_t;
void print_bytes(unsigned int n)
{
printf("%x", n);
}
void printer(vector_128_t *v)
{
size_t i = 15;
v128_t vec = { .v = *v };
printf("0x");
do {
print_bytes(vec.uc[i]);
} while (--i);
print_bytes(vec.uc[i]);
printf("\n");
}
__int128_t load(unsigned long long hi, unsigned long long lo)
{
__int128_t x = ((__int128_t)hi << 64) | (lo & 0xffffffffffffffff);
return x;
}
int main()
{
//ulong io_base = 0x210000040000;
ulong io_base = 0x210000030000;
//ulong io_base = 0x210000040000;
void *io_mem;
volatile uint64_t *io_address;
uint64_t *address = (uint64_t*)malloc(sizeof(vector_128_t));
vector_128_t vrt = (vector_128_t){ 0ULL };
vector_128_t vrs = (vector_128_t){ load(0x1234567855554444, 0xAAAABBBB87654321) };
int iofd;
// map the memory
iofd = open(DEVICE, O_RDWR);
if (iofd == -1) {
fprintf(stderr, "can't open /dev/mem for rw\n");
return 1;
}
io_mem = mmap(NULL,
getpagesize(),
PROT_READ | PROT_WRITE,
MAP_SHARED,
iofd,
io_base);
if (io_mem == MAP_FAILED) {
fprintf(stderr, "can't map mmio\n");
return 2;
}
close(iofd);
io_address = io_mem;
asm volatile ("stvx %0, 0, %1\n\t"
:
: "v"(vrs), "r"(address));
asm volatile ("stvx %0, 0, %1\n\t"
:
: "v"(vrs), "r"(io_address));
/*
asm volatile ("stxvd2x %0, 0, %1\n\t"
:
: "v"(vrs), "r"(io_address));
*/
asm volatile ("vxor 0, 0, 0\n\t");
asm volatile ("lvx %0, 0, %1\n\t"
: "=&v"(vrt)
: "r"(address));
printf("address: %p\n", address);
printer(&vrt);
asm volatile ("vxor 0, 0, 0\n\t");
asm volatile ("lvx %0, 0, %1\n\t"
: "=&v"(vrt)
: "r"(io_address));
printf("io_address: %p\n", io_address);
printer(&vrt);
if (munmap(io_mem, getpagesize())) {
fprintf(stderr, ":-(\n");
return 3;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment