adi.txt 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. Application Data Integrity (ADI)
  2. ================================
  3. SPARC M7 processor adds the Application Data Integrity (ADI) feature.
  4. ADI allows a task to set version tags on any subset of its address
  5. space. Once ADI is enabled and version tags are set for ranges of
  6. address space of a task, the processor will compare the tag in pointers
  7. to memory in these ranges to the version set by the application
  8. previously. Access to memory is granted only if the tag in given pointer
  9. matches the tag set by the application. In case of mismatch, processor
  10. raises an exception.
  11. Following steps must be taken by a task to enable ADI fully:
  12. 1. Set the user mode PSTATE.mcde bit. This acts as master switch for
  13. the task's entire address space to enable/disable ADI for the task.
  14. 2. Set TTE.mcd bit on any TLB entries that correspond to the range of
  15. addresses ADI is being enabled on. MMU checks the version tag only
  16. on the pages that have TTE.mcd bit set.
  17. 3. Set the version tag for virtual addresses using stxa instruction
  18. and one of the MCD specific ASIs. Each stxa instruction sets the
  19. given tag for one ADI block size number of bytes. This step must
  20. be repeated for entire page to set tags for entire page.
  21. ADI block size for the platform is provided by the hypervisor to kernel
  22. in machine description tables. Hypervisor also provides the number of
  23. top bits in the virtual address that specify the version tag. Once
  24. version tag has been set for a memory location, the tag is stored in the
  25. physical memory and the same tag must be present in the ADI version tag
  26. bits of the virtual address being presented to the MMU. For example on
  27. SPARC M7 processor, MMU uses bits 63-60 for version tags and ADI block
  28. size is same as cacheline size which is 64 bytes. A task that sets ADI
  29. version to, say 10, on a range of memory, must access that memory using
  30. virtual addresses that contain 0xa in bits 63-60.
  31. ADI is enabled on a set of pages using mprotect() with PROT_ADI flag.
  32. When ADI is enabled on a set of pages by a task for the first time,
  33. kernel sets the PSTATE.mcde bit fot the task. Version tags for memory
  34. addresses are set with an stxa instruction on the addresses using
  35. ASI_MCD_PRIMARY or ASI_MCD_ST_BLKINIT_PRIMARY. ADI block size is
  36. provided by the hypervisor to the kernel. Kernel returns the value of
  37. ADI block size to userspace using auxiliary vector along with other ADI
  38. info. Following auxiliary vectors are provided by the kernel:
  39. AT_ADI_BLKSZ ADI block size. This is the granularity and
  40. alignment, in bytes, of ADI versioning.
  41. AT_ADI_NBITS Number of ADI version bits in the VA
  42. IMPORTANT NOTES:
  43. - Version tag values of 0x0 and 0xf are reserved. These values match any
  44. tag in virtual address and never generate a mismatch exception.
  45. - Version tags are set on virtual addresses from userspace even though
  46. tags are stored in physical memory. Tags are set on a physical page
  47. after it has been allocated to a task and a pte has been created for
  48. it.
  49. - When a task frees a memory page it had set version tags on, the page
  50. goes back to free page pool. When this page is re-allocated to a task,
  51. kernel clears the page using block initialization ASI which clears the
  52. version tags as well for the page. If a page allocated to a task is
  53. freed and allocated back to the same task, old version tags set by the
  54. task on that page will no longer be present.
  55. - ADI tag mismatches are not detected for non-faulting loads.
  56. - Kernel does not set any tags for user pages and it is entirely a
  57. task's responsibility to set any version tags. Kernel does ensure the
  58. version tags are preserved if a page is swapped out to the disk and
  59. swapped back in. It also preserves that version tags if a page is
  60. migrated.
  61. - ADI works for any size pages. A userspace task need not be aware of
  62. page size when using ADI. It can simply select a virtual address
  63. range, enable ADI on the range using mprotect() and set version tags
  64. for the entire range. mprotect() ensures range is aligned to page size
  65. and is a multiple of page size.
  66. - ADI tags can only be set on writable memory. For example, ADI tags can
  67. not be set on read-only mappings.
  68. ADI related traps
  69. -----------------
  70. With ADI enabled, following new traps may occur:
  71. Disrupting memory corruption
  72. When a store accesses a memory localtion that has TTE.mcd=1,
  73. the task is running with ADI enabled (PSTATE.mcde=1), and the ADI
  74. tag in the address used (bits 63:60) does not match the tag set on
  75. the corresponding cacheline, a memory corruption trap occurs. By
  76. default, it is a disrupting trap and is sent to the hypervisor
  77. first. Hypervisor creates a sun4v error report and sends a
  78. resumable error (TT=0x7e) trap to the kernel. The kernel sends
  79. a SIGSEGV to the task that resulted in this trap with the following
  80. info:
  81. siginfo.si_signo = SIGSEGV;
  82. siginfo.errno = 0;
  83. siginfo.si_code = SEGV_ADIDERR;
  84. siginfo.si_addr = addr; /* PC where first mismatch occurred */
  85. siginfo.si_trapno = 0;
  86. Precise memory corruption
  87. When a store accesses a memory location that has TTE.mcd=1,
  88. the task is running with ADI enabled (PSTATE.mcde=1), and the ADI
  89. tag in the address used (bits 63:60) does not match the tag set on
  90. the corresponding cacheline, a memory corruption trap occurs. If
  91. MCD precise exception is enabled (MCDPERR=1), a precise
  92. exception is sent to the kernel with TT=0x1a. The kernel sends
  93. a SIGSEGV to the task that resulted in this trap with the following
  94. info:
  95. siginfo.si_signo = SIGSEGV;
  96. siginfo.errno = 0;
  97. siginfo.si_code = SEGV_ADIPERR;
  98. siginfo.si_addr = addr; /* address that caused trap */
  99. siginfo.si_trapno = 0;
  100. NOTE: ADI tag mismatch on a load always results in precise trap.
  101. MCD disabled
  102. When a task has not enabled ADI and attempts to set ADI version
  103. on a memory address, processor sends an MCD disabled trap. This
  104. trap is handled by hypervisor first and the hypervisor vectors this
  105. trap through to the kernel as Data Access Exception trap with
  106. fault type set to 0xa (invalid ASI). When this occurs, the kernel
  107. sends the task SIGSEGV signal with following info:
  108. siginfo.si_signo = SIGSEGV;
  109. siginfo.errno = 0;
  110. siginfo.si_code = SEGV_ACCADI;
  111. siginfo.si_addr = addr; /* address that caused trap */
  112. siginfo.si_trapno = 0;
  113. Sample program to use ADI
  114. -------------------------
  115. Following sample program is meant to illustrate how to use the ADI
  116. functionality.
  117. #include <unistd.h>
  118. #include <stdio.h>
  119. #include <stdlib.h>
  120. #include <elf.h>
  121. #include <sys/ipc.h>
  122. #include <sys/shm.h>
  123. #include <sys/mman.h>
  124. #include <asm/asi.h>
  125. #ifndef AT_ADI_BLKSZ
  126. #define AT_ADI_BLKSZ 48
  127. #endif
  128. #ifndef AT_ADI_NBITS
  129. #define AT_ADI_NBITS 49
  130. #endif
  131. #ifndef PROT_ADI
  132. #define PROT_ADI 0x10
  133. #endif
  134. #define BUFFER_SIZE 32*1024*1024UL
  135. main(int argc, char* argv[], char* envp[])
  136. {
  137. unsigned long i, mcde, adi_blksz, adi_nbits;
  138. char *shmaddr, *tmp_addr, *end, *veraddr, *clraddr;
  139. int shmid, version;
  140. Elf64_auxv_t *auxv;
  141. adi_blksz = 0;
  142. while(*envp++ != NULL);
  143. for (auxv = (Elf64_auxv_t *)envp; auxv->a_type != AT_NULL; auxv++) {
  144. switch (auxv->a_type) {
  145. case AT_ADI_BLKSZ:
  146. adi_blksz = auxv->a_un.a_val;
  147. break;
  148. case AT_ADI_NBITS:
  149. adi_nbits = auxv->a_un.a_val;
  150. break;
  151. }
  152. }
  153. if (adi_blksz == 0) {
  154. fprintf(stderr, "Oops! ADI is not supported\n");
  155. exit(1);
  156. }
  157. printf("ADI capabilities:\n");
  158. printf("\tBlock size = %ld\n", adi_blksz);
  159. printf("\tNumber of bits = %ld\n", adi_nbits);
  160. if ((shmid = shmget(2, BUFFER_SIZE,
  161. IPC_CREAT | SHM_R | SHM_W)) < 0) {
  162. perror("shmget failed");
  163. exit(1);
  164. }
  165. shmaddr = shmat(shmid, NULL, 0);
  166. if (shmaddr == (char *)-1) {
  167. perror("shm attach failed");
  168. shmctl(shmid, IPC_RMID, NULL);
  169. exit(1);
  170. }
  171. if (mprotect(shmaddr, BUFFER_SIZE, PROT_READ|PROT_WRITE|PROT_ADI)) {
  172. perror("mprotect failed");
  173. goto err_out;
  174. }
  175. /* Set the ADI version tag on the shm segment
  176. */
  177. version = 10;
  178. tmp_addr = shmaddr;
  179. end = shmaddr + BUFFER_SIZE;
  180. while (tmp_addr < end) {
  181. asm volatile(
  182. "stxa %1, [%0]0x90\n\t"
  183. :
  184. : "r" (tmp_addr), "r" (version));
  185. tmp_addr += adi_blksz;
  186. }
  187. asm volatile("membar #Sync\n\t");
  188. /* Create a versioned address from the normal address by placing
  189. * version tag in the upper adi_nbits bits
  190. */
  191. tmp_addr = (void *) ((unsigned long)shmaddr << adi_nbits);
  192. tmp_addr = (void *) ((unsigned long)tmp_addr >> adi_nbits);
  193. veraddr = (void *) (((unsigned long)version << (64-adi_nbits))
  194. | (unsigned long)tmp_addr);
  195. printf("Starting the writes:\n");
  196. for (i = 0; i < BUFFER_SIZE; i++) {
  197. veraddr[i] = (char)(i);
  198. if (!(i % (1024 * 1024)))
  199. printf(".");
  200. }
  201. printf("\n");
  202. printf("Verifying data...");
  203. fflush(stdout);
  204. for (i = 0; i < BUFFER_SIZE; i++)
  205. if (veraddr[i] != (char)i)
  206. printf("\nIndex %lu mismatched\n", i);
  207. printf("Done.\n");
  208. /* Disable ADI and clean up
  209. */
  210. if (mprotect(shmaddr, BUFFER_SIZE, PROT_READ|PROT_WRITE)) {
  211. perror("mprotect failed");
  212. goto err_out;
  213. }
  214. if (shmdt((const void *)shmaddr) != 0)
  215. perror("Detach failure");
  216. shmctl(shmid, IPC_RMID, NULL);
  217. exit(0);
  218. err_out:
  219. if (shmdt((const void *)shmaddr) != 0)
  220. perror("Detach failure");
  221. shmctl(shmid, IPC_RMID, NULL);
  222. exit(1);
  223. }