Guest User

DRAM Disturbance Error userspace tester

a guest
Dec 25th, 2014
1,677
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <sys/types.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <fcntl.h>
  8.  
  9. /*
  10.  DRAM Disturbance Error userspace tester
  11.  https://www.ece.cmu.edu/~safari/pubs/kim-isca14.pdf
  12. */
  13.  
  14. unsigned int mycrc32(char *buffer, unsigned length)
  15. {
  16.         unsigned i;
  17.         unsigned int crc32 = ~0L;
  18.         for (i = 0; i < length; i++)
  19.             crc32 += (unsigned)((unsigned char) buffer[i]);
  20.         return crc32;
  21. }
  22.  
  23. int main(int argc, char *argv[])
  24. {
  25.     char *bigseg;
  26.     unsigned long ROW_SIZE = 1 << 13;    
  27.     unsigned long ADDR_OFFSET = 1 << 23;
  28.     char *addr_up;
  29.     char *addr_down;
  30.     size_t segsize = (1024 * 1204) * 64;
  31.     int rnd;
  32.  
  33.     bigseg = NULL;
  34.     char *start;
  35.     char *end;
  36.     int tryit;
  37.     unsigned int crc_orig =0;// mycrc32(start, segsize);
  38.     unsigned int crc= 0;
  39.     int cnt = 0;
  40.     int iter=0;
  41.  
  42.     while(1)
  43.     {
  44. //      if (bigseg) free(bigseg);
  45.         bigseg =  malloc(segsize);
  46.         rnd=open("/dev/urandom", O_RDONLY);
  47.         read(rnd, bigseg, segsize/5);
  48.         close(rnd);
  49.         start = bigseg;
  50.         end   = bigseg + segsize;
  51.         crc_orig = mycrc32(start, segsize);
  52.         printf("#%i ", iter);
  53.  
  54.         for (addr_up = start; ; addr_up += (ROW_SIZE*3))
  55.         {
  56.             addr_down = addr_up + ADDR_OFFSET;
  57.             if (addr_down >= end)
  58.                 break;
  59.             for (tryit = 0; tryit < 4000000; tryit++)
  60.             {
  61.                 asm volatile("movl (%0), %%eax" : : "r" (addr_up) : "eax");
  62.                 asm volatile("movl (%0), %%ebx" : : "r" (addr_down) : "ebx");
  63.                 asm volatile("clflush (%0)" : : "r" (addr_up) : "memory");
  64.                 asm volatile("clflush (%0)" : : "r" (addr_down) : "memory");
  65.                 asm volatile("mfence");
  66.             }
  67.             printf(".");
  68.             cnt++;
  69.             fflush(0);
  70.             if ((cnt%40) == 0)
  71.             {
  72.                 if (crc_orig != mycrc32(start, segsize))
  73.                 {
  74.                     printf(" DRAM CRC FAIL! ;-)\n");
  75.                     exit(-1);
  76.                 } else
  77.                 {
  78.                     iter++;
  79.                     printf(" OK\n");
  80.                     break;
  81.                 }
  82.             }
  83.             if (iter > 32)
  84.             {
  85.                 printf("DRAM seems works fine...\n");
  86.                 exit(0);
  87.             }
  88.         }
  89.     }
  90. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×