adi.rst 9.9 KB

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