arm.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*++
  2. Copyright (c) 2012 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. arm.inc
  5. Abstract:
  6. This module contains common definitions for the ARM architecture.
  7. Author:
  8. Evan Green 11-Aug-2012
  9. Environment:
  10. Kernel mode
  11. --*/
  12. ##
  13. ## --------------------------------------------------------------- Definitions
  14. ##
  15. ##
  16. ## Set this to 1 to enable Thumb-2 instructions.
  17. ##
  18. #ifdef __thumb__
  19. #define THUMB 1
  20. #else
  21. #define THUMB 0
  22. #endif
  23. #define NULL 0x0
  24. ##
  25. ## Processor flags
  26. ##
  27. ##
  28. ## Program Status Register flags.
  29. ##
  30. #define PSR_FLAG_NEGATIVE 0x80000000
  31. #define PSR_FLAG_ZERO 0x40000000
  32. #define PSR_FLAG_CARRY 0x20000000
  33. #define PSR_FLAG_OVERFLOW 0x10000000
  34. #define PSR_FLAG_SATURATION 0x08000000
  35. #define PSR_FLAG_JAZELLE 0x01000000
  36. #define PSR_FLAG_THUMB 0x00000020
  37. #define PSR_FLAG_IRQ 0x00000080
  38. #define PSR_FLAG_FIQ 0x00000040
  39. ##
  40. ## Processor modes
  41. ##
  42. #define ARM_MODE_USER 0x00000010
  43. #define ARM_MODE_FIQ 0x00000011
  44. #define ARM_MODE_IRQ 0x00000012
  45. #define ARM_MODE_SVC 0x00000013
  46. #define ARM_MODE_MON 0x00000016
  47. #define ARM_MODE_ABORT 0x00000017
  48. #define ARM_MODE_HYP 0x0000001A
  49. #define ARM_MODE_UNDEF 0x0000001B
  50. #define ARM_MODE_SYSTEM 0x0000001F
  51. #define ARM_MODE_MASK 0x0000001F
  52. ##
  53. ## Basic constants.
  54. ##
  55. #define FALSE 0
  56. #define TRUE 1
  57. ##
  58. ## Kernel constants.
  59. ##
  60. #define EXCEPTION_BREAK 0x03
  61. #define EXCEPTION_SINGLE_STEP 0x04
  62. #define EXCEPTION_ACCESS_VIOLATION 0x05
  63. #define EXCEPTION_ASSERTION_FAILURE 0x07
  64. ##
  65. ## Translation table base register address mask.
  66. ##
  67. ## Bit definitions are tricky for this register because they change based on
  68. ## whether or not the Multiprocessing Extensions are supported on the CPU.
  69. ##
  70. #define TTBR_ADDRESS_MASK 0x00003FFF
  71. ##
  72. ## MMU Control bits (SCTLR, CP15, register 1).
  73. ##
  74. #define MMU_ENABLED 0x00000001
  75. #define MMU_ALIGNMENT_FAULT_ENABLED 0x00000002
  76. #define MMU_DCACHE_ENABLED 0x00000004
  77. #define MMU_WRITE_BUFFER_ENABLED 0x00000008
  78. #define MMU_ENDIANNESS 0x00000080
  79. #define MMU_SYSTEM_PROTECTION 0x00000100
  80. #define MMU_ROM_PROTECTION 0x00000200
  81. #define MMU_BRANCH_PREDICTION_ENABLED 0x00000800
  82. #define MMU_ICACHE_ENABLED 0x00001000
  83. #define MMU_HIGH_EXCEPTION_VECTORS 0x00002000
  84. #define MMU_PREDICTABLE_REPLACEMENT 0x00004000
  85. #define MMU_DISABLE_THUMB_DEPRECATED 0x00008000
  86. #define MMU_FAST_INTERRUPTS 0x00200000
  87. #define MMU_UNALIGNED_ACCESS_ENABLED 0x00400000
  88. #define MMU_VMSA6_ENABLED 0x00800000
  89. #define MMU_VECTORED_INTERRUPTS_ENABLED 0x01000000
  90. #define MMU_EXCEPTION_ENDIAN 0x02000000
  91. #define MMU_THUMB_EXCEPTIONS 0x40000000
  92. #define MMU_CONTROL_DEFAULT_VALUE 0x00C0180D
  93. ##
  94. ## ARMv6 auxiliary control register bits (ACTLR).
  95. ##
  96. #define ARMV6_AUX_16K_CACHE_SIZE 0x00000040
  97. ##
  98. ## Cortex A17 auxiliary control register bits (ACTLR).
  99. ##
  100. #define CORTEX_A17_AUX_SMP_ENABLE 0x00000040
  101. ##
  102. ## Nonsecure Access Control Register bits (CP15, register 1, opcode2 2).
  103. ##
  104. #define MMU_TLB_LOCKING_ENABLE 0x00020000
  105. ##
  106. ## Secure Configuration Register bits (SCR).
  107. ##
  108. #define SCR_NON_SECURE 0x00000001
  109. #define SCR_MONITOR_MODE_IRQ 0x00000002
  110. #define SCR_MONITOR_MODE_FIQ 0x00000004
  111. #define SCR_MONITOR_MODE_EXTERNAL_ABORT 0x00000008
  112. #define SCR_CPSR_FIQ_WRITABLE 0x00000010
  113. #define SCR_CPSR_ASYNC_ABORT_WRITABLE 0x00000020
  114. #define SCR_EARLY_TERMINATION_DISABLED 0x00000040
  115. #define SCR_NON_SECURE_SMC_DISABLED 0x00000080
  116. #define SCR_NON_SECURE_HVC_ENABLED 0x00000100
  117. #define SCR_NON_SECURE_INSTRUCTION_FETCH_DISABLED 0x00000200
  118. ##
  119. ## Definition for the structure on the exception stacks.
  120. ##
  121. #define EXCEPTION_STACK_R0 0
  122. #define EXCEPTION_STACK_CPSR 4
  123. ##
  124. ## Define the offsets for members of the TRAP_FRAME structure.
  125. ##
  126. #define TRAP_SVCSP 0
  127. #define TRAP_USERSP 4
  128. #define TRAP_USERLR 8
  129. #define TRAP_R0 12
  130. #define TRAP_EXCEPTION_CPSR 16
  131. #define TRAP_R1 20
  132. #define TRAP_R2 24
  133. #define TRAP_R3 28
  134. #define TRAP_R4 32
  135. #define TRAP_R5 36
  136. #define TRAP_R6 40
  137. #define TRAP_R7 44
  138. #define TRAP_R8 48
  139. #define TRAP_R9 52
  140. #define TRAP_R10 56
  141. #define TRAP_R11 60
  142. #define TRAP_R12 64
  143. #define TRAP_SVCLR 68
  144. #define TRAP_PC 72
  145. #define TRAP_CPSR 76
  146. #define TRAP_FRAME_SIZE 80
  147. ##
  148. ## Define the size of the common SIGNAL_CONTEXT structure.
  149. ##
  150. #define SIGNAL_CONTEXT_SIZE 32
  151. ##
  152. ## Define the size of the PROCESSOR_CONTEXT structure.
  153. ##
  154. #define PROCESSOR_CONTEXT_SIZE 192
  155. ##
  156. ## Define the instructions for DSB/ISB, which are different in ARMv6 vs ARMv7.
  157. ##
  158. #if __ARM_ARCH == 6
  159. #define DMB mcr p15, 0, %r0, %cr7, %cr10, 5
  160. #define DSB mcr p15, 0, %r0, %cr7, %cr10, 4
  161. #define ISB mcr p15, 0, %r0, %cr7, %cr5, 4
  162. #define BPIALL
  163. #elif __ARM_ARCH == 7
  164. #define DMB dmb
  165. #define DSB dsb
  166. #define ISB isb
  167. #define BPIALL mcr p15, 0, %r0, %cr7, %cr5, 6
  168. #else
  169. #error Unsupported ARM architecture version
  170. #endif
  171. ##
  172. ## Define the system call number for resuming after a signal.
  173. ##
  174. #define SystemCallRestoreContext 1
  175. #define SIGNAL_PARAMETERS_SIZE 24
  176. ##
  177. ## -------------------------------------------------------------------- Macros
  178. ##
  179. ##
  180. ## This macro emits a thumb if-then instruction, if compiling in Thumb mode.
  181. ##
  182. #define IT(_Condition) \
  183. .if THUMB ; \
  184. it _Condition ; \
  185. .endif
  186. #define ITE(_Condition) \
  187. .if THUMB ; \
  188. ite _Condition ; \
  189. .endif
  190. #define ITTE(_Condition) \
  191. .if THUMB ; \
  192. itte _Condition ; \
  193. .endif
  194. #define ITTTE(_Condition) \
  195. .if THUMB ; \
  196. ittte _Condition ; \
  197. .endif
  198. ##
  199. ## This macro goes at the top of an assembly file.
  200. ## .text specifies that this code belongs in the executable section.
  201. ## .thumb specifies this is Thumb code (not ARM code).
  202. ## .syntax unified turns on Unified Assembly Language which is required to
  203. ## enable Thumb-2 features.
  204. ## .align 4 ensures functions are emitted on proper boundaries.
  205. ##
  206. .macro ASSEMBLY_FILE_HEADER
  207. .text
  208. .syntax unified
  209. .cfi_sections .debug_frame
  210. .if THUMB
  211. .thumb
  212. .else
  213. .arm
  214. .endif
  215. #if __ARM_ARCH == 6
  216. .cpu arm1176jz-s
  217. #elif __ARM_ARCH == 7
  218. .arch armv7-a
  219. .arch_extension sec
  220. #else
  221. #error Unsupported ARM architecture version
  222. #endif
  223. .endm
  224. ##
  225. ## This macro defines a function, callable from C code within the current
  226. ## module.
  227. ##
  228. .macro FUNCTION FunctionName
  229. .hidden \FunctionName
  230. EXPORTED_FUNCTION \FunctionName
  231. .endm
  232. ##
  233. ## This macro defines a function, callable from C code in any module, but
  234. ## always called locally in the current module.
  235. ##
  236. .macro PROTECTED_FUNCTION FunctionName
  237. .protected \FunctionName
  238. EXPORTED_FUNCTION \FunctionName
  239. .endm
  240. ##
  241. ## This macro defines a function, callable from C code in any module, and
  242. ## capable of being overridden by other modules.
  243. ##
  244. .macro EXPORTED_FUNCTION FunctionName
  245. .func \FunctionName
  246. .type \FunctionName, %function
  247. .if THUMB
  248. .thumb_func
  249. .endif
  250. .globl \FunctionName
  251. .cfi_startproc
  252. .cfi_same_value r14
  253. .if THUMB
  254. .cfi_def_cfa_register r7
  255. .else
  256. .cfi_def_cfa_register r11
  257. .endif
  258. \FunctionName:
  259. .endm
  260. ##
  261. ## This macro defines the end of a function.
  262. ##
  263. .macro END_FUNCTION FunctionName
  264. .ltorg
  265. .size \FunctionName, .-\FunctionName
  266. .cfi_endproc
  267. .endfunc
  268. .endm
  269. ##
  270. ## Define a macro used to generate the debugger break instruction.
  271. ##
  272. .macro DEBUGGER_BREAK
  273. .if THUMB
  274. .hword 0xDE20
  275. .else
  276. .word 0xE7F000F3
  277. .endif
  278. .endm
  279. ##
  280. ## Define a macro used to generate the debug service instruction.
  281. ##
  282. .macro DEBUG_SERVICE
  283. .if THUMB
  284. .hword 0xDE24
  285. .else
  286. .word 0xE7F000F4
  287. .endif
  288. .endm
  289. ##
  290. ## This macro can be run immediately after an exception or interrupt. It
  291. ## switches back to SVC mode and creates a trap frame. This uses several
  292. ## instructions from ARMv6T2, including srs, cpsid #mode, and clrex.
  293. ##
  294. .macro ARM_ENTER_INTERRUPT
  295. srsdb %sp!, #ARM_MODE_SVC @ Push lr and spsr to SVC.
  296. mrs %lr, cpsr @ Get exception mode CPSR.
  297. stmdb %sp, {%r0, %lr} @ Push R0 and exception CPSR.
  298. sub %r0, %sp, #8 @ Remember exception stack.
  299. cpsid i, #ARM_MODE_SVC @ Switch to SVC mode.
  300. stmdb %sp!, {%r1-%r12, %lr} @ Push general registers.
  301. ldmia %r0, {%r1, %r2} @ Get R0 and exception CPSR.
  302. stmdb %sp!, {%r1, %r2} @ Save onto trap frame.
  303. mov %r0, %sp @ Get SVC stack.
  304. sub %sp, %sp, #12 @ Allocate space.
  305. cpsid i, #ARM_MODE_SYSTEM @ Move to system mode.
  306. str %lr, [%r0, #-4] @ Save usermode SP.
  307. str %sp, [%r0, #-8] @ Save usermode LR.
  308. cpsid i, #ARM_MODE_SVC @ Switch back to SVC mode.
  309. str %sp, [%sp] @ Save SVC stack pointer.
  310. clrex @ Clear exclusive monitors.
  311. .endm
  312. ##
  313. ## This macro can be used for an interrupt or exception that was entered with
  314. ## ARM_INTERRUPT_ENTER. It restores the state in the trap frame and returns
  315. ## from the exception.
  316. ##
  317. .macro ARM_EXIT_INTERRUPT
  318. ##
  319. ## Restore the stack pointer. The rest of the context better be at the new
  320. ## stack location. Then restore the user mode sp and lr.
  321. ##
  322. ldr %sp, [%sp] @ Restore stack pointer.
  323. add %sp, %sp, #4 @ Pop stack pointer.
  324. ##
  325. ## Restore the user mode stack and link registers.
  326. ##
  327. mov %r0, %sp @ Get SVC stack pointer.
  328. cpsid i, #ARM_MODE_SYSTEM @ Switch to system mode.
  329. ldr %sp, [%r0] @ Restore usermode SP.
  330. ldr %lr, [%r0, #4] @ Resotre usermode LR.
  331. cpsid i, #ARM_MODE_SVC @ Switch back to svc mode.
  332. ldr %r0, [%sp, #8] @ Restore R0.
  333. add %sp, %sp, #16 @ Pop up to R1.
  334. ldmia %sp!, {%r1-%r12, %lr} @ Restore general registers.
  335. rfeia %sp! @ Restore PC and CPSR.
  336. .endm