vm_entry.S 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Event entry/exit for Hexagon
  4. *
  5. * Copyright (c) 2010-2013, The Linux Foundation. All rights reserved.
  6. */
  7. #include <asm/asm-offsets.h> /* assembly-safer versions of C defines */
  8. #include <asm/mem-layout.h> /* sigh, except for page_offset */
  9. #include <asm/hexagon_vm.h>
  10. #include <asm/thread_info.h>
  11. /*
  12. * Entry into guest-mode Linux under Hexagon Virtual Machine.
  13. * Stack pointer points to event record - build pt_regs on top of it,
  14. * set up a plausible C stack frame, and dispatch to the C handler.
  15. * On return, do vmrte virtual instruction with SP where we started.
  16. *
  17. * VM Spec 0.5 uses a trap to fetch HVM record now.
  18. */
  19. /*
  20. * Save full register state, while setting up thread_info struct
  21. * pointer derived from kernel stack pointer in THREADINFO_REG
  22. * register, putting prior thread_info.regs pointer in a callee-save
  23. * register (R24, which had better not ever be assigned to THREADINFO_REG),
  24. * and updating thread_info.regs to point to current stack frame,
  25. * so as to support nested events in kernel mode.
  26. *
  27. * As this is common code, we set the pt_regs system call number
  28. * to -1 for all events. It will be replaced with the system call
  29. * number in the case where we decode a system call (trap0(#1)).
  30. */
  31. #if CONFIG_HEXAGON_ARCH_VERSION < 4
  32. #define save_pt_regs()\
  33. memd(R0 + #_PT_R3130) = R31:30; \
  34. { memw(R0 + #_PT_R2928) = R28; \
  35. R31 = memw(R0 + #_PT_ER_VMPSP); }\
  36. { memw(R0 + #(_PT_R2928 + 4)) = R31; \
  37. R31 = ugp; } \
  38. { memd(R0 + #_PT_R2726) = R27:26; \
  39. R30 = gp ; } \
  40. memd(R0 + #_PT_R2524) = R25:24; \
  41. memd(R0 + #_PT_R2322) = R23:22; \
  42. memd(R0 + #_PT_R2120) = R21:20; \
  43. memd(R0 + #_PT_R1918) = R19:18; \
  44. memd(R0 + #_PT_R1716) = R17:16; \
  45. memd(R0 + #_PT_R1514) = R15:14; \
  46. memd(R0 + #_PT_R1312) = R13:12; \
  47. { memd(R0 + #_PT_R1110) = R11:10; \
  48. R15 = lc0; } \
  49. { memd(R0 + #_PT_R0908) = R9:8; \
  50. R14 = sa0; } \
  51. { memd(R0 + #_PT_R0706) = R7:6; \
  52. R13 = lc1; } \
  53. { memd(R0 + #_PT_R0504) = R5:4; \
  54. R12 = sa1; } \
  55. { memd(R0 + #_PT_GPUGP) = R31:30; \
  56. R11 = m1; \
  57. R2.H = #HI(_THREAD_SIZE); } \
  58. { memd(R0 + #_PT_LC0SA0) = R15:14; \
  59. R10 = m0; \
  60. R2.L = #LO(_THREAD_SIZE); } \
  61. { memd(R0 + #_PT_LC1SA1) = R13:12; \
  62. R15 = p3:0; \
  63. R2 = neg(R2); } \
  64. { memd(R0 + #_PT_M1M0) = R11:10; \
  65. R14 = usr; \
  66. R2 = and(R0,R2); } \
  67. { memd(R0 + #_PT_PREDSUSR) = R15:14; \
  68. THREADINFO_REG = R2; } \
  69. { r24 = memw(THREADINFO_REG + #_THREAD_INFO_PT_REGS); \
  70. memw(THREADINFO_REG + #_THREAD_INFO_PT_REGS) = R0; \
  71. R2 = #-1; } \
  72. { memw(R0 + #_PT_SYSCALL_NR) = R2; \
  73. R30 = #0; }
  74. #else
  75. /* V4+ */
  76. /* the # ## # syntax inserts a literal ## */
  77. #define save_pt_regs()\
  78. { memd(R0 + #_PT_R3130) = R31:30; \
  79. R30 = memw(R0 + #_PT_ER_VMPSP); }\
  80. { memw(R0 + #_PT_R2928) = R28; \
  81. memw(R0 + #(_PT_R2928 + 4)) = R30; }\
  82. { R31:30 = C11:10; \
  83. memd(R0 + #_PT_R2726) = R27:26; \
  84. memd(R0 + #_PT_R2524) = R25:24; }\
  85. { memd(R0 + #_PT_R2322) = R23:22; \
  86. memd(R0 + #_PT_R2120) = R21:20; }\
  87. { memd(R0 + #_PT_R1918) = R19:18; \
  88. memd(R0 + #_PT_R1716) = R17:16; }\
  89. { memd(R0 + #_PT_R1514) = R15:14; \
  90. memd(R0 + #_PT_R1312) = R13:12; \
  91. R17:16 = C13:12; }\
  92. { memd(R0 + #_PT_R1110) = R11:10; \
  93. memd(R0 + #_PT_R0908) = R9:8; \
  94. R15:14 = C1:0; } \
  95. { memd(R0 + #_PT_R0706) = R7:6; \
  96. memd(R0 + #_PT_R0504) = R5:4; \
  97. R13:12 = C3:2; } \
  98. { memd(R0 + #_PT_GPUGP) = R31:30; \
  99. memd(R0 + #_PT_LC0SA0) = R15:14; \
  100. R11:10 = C7:6; }\
  101. { THREADINFO_REG = and(R0, # ## #-_THREAD_SIZE); \
  102. memd(R0 + #_PT_LC1SA1) = R13:12; \
  103. R15 = p3:0; }\
  104. { memd(R0 + #_PT_M1M0) = R11:10; \
  105. memw(R0 + #_PT_PREDSUSR + 4) = R15; }\
  106. { r24 = memw(THREADINFO_REG + #_THREAD_INFO_PT_REGS); \
  107. memw(THREADINFO_REG + #_THREAD_INFO_PT_REGS) = R0; \
  108. R2 = #-1; } \
  109. { memw(R0 + #_PT_SYSCALL_NR) = R2; \
  110. memd(R0 + #_PT_CS1CS0) = R17:16; \
  111. R30 = #0; }
  112. #endif
  113. /*
  114. * Restore registers and thread_info.regs state. THREADINFO_REG
  115. * is assumed to still be sane, and R24 to have been correctly
  116. * preserved. Don't restore R29 (SP) until later.
  117. */
  118. #if CONFIG_HEXAGON_ARCH_VERSION < 4
  119. #define restore_pt_regs() \
  120. { memw(THREADINFO_REG + #_THREAD_INFO_PT_REGS) = R24; \
  121. R15:14 = memd(R0 + #_PT_PREDSUSR); } \
  122. { R11:10 = memd(R0 + #_PT_M1M0); \
  123. p3:0 = R15; } \
  124. { R13:12 = memd(R0 + #_PT_LC1SA1); \
  125. usr = R14; } \
  126. { R15:14 = memd(R0 + #_PT_LC0SA0); \
  127. m1 = R11; } \
  128. { R3:2 = memd(R0 + #_PT_R0302); \
  129. m0 = R10; } \
  130. { R5:4 = memd(R0 + #_PT_R0504); \
  131. lc1 = R13; } \
  132. { R7:6 = memd(R0 + #_PT_R0706); \
  133. sa1 = R12; } \
  134. { R9:8 = memd(R0 + #_PT_R0908); \
  135. lc0 = R15; } \
  136. { R11:10 = memd(R0 + #_PT_R1110); \
  137. sa0 = R14; } \
  138. { R13:12 = memd(R0 + #_PT_R1312); \
  139. R15:14 = memd(R0 + #_PT_R1514); } \
  140. { R17:16 = memd(R0 + #_PT_R1716); \
  141. R19:18 = memd(R0 + #_PT_R1918); } \
  142. { R21:20 = memd(R0 + #_PT_R2120); \
  143. R23:22 = memd(R0 + #_PT_R2322); } \
  144. { R25:24 = memd(R0 + #_PT_R2524); \
  145. R27:26 = memd(R0 + #_PT_R2726); } \
  146. R31:30 = memd(R0 + #_PT_GPUGP); \
  147. { R28 = memw(R0 + #_PT_R2928); \
  148. ugp = R31; } \
  149. { R31:30 = memd(R0 + #_PT_R3130); \
  150. gp = R30; }
  151. #else
  152. /* V4+ */
  153. #define restore_pt_regs() \
  154. { memw(THREADINFO_REG + #_THREAD_INFO_PT_REGS) = R24; \
  155. R15:14 = memd(R0 + #_PT_PREDSUSR); } \
  156. { R11:10 = memd(R0 + #_PT_M1M0); \
  157. R13:12 = memd(R0 + #_PT_LC1SA1); \
  158. p3:0 = R15; } \
  159. { R15:14 = memd(R0 + #_PT_LC0SA0); \
  160. R3:2 = memd(R0 + #_PT_R0302); \
  161. usr = R14; } \
  162. { R5:4 = memd(R0 + #_PT_R0504); \
  163. R7:6 = memd(R0 + #_PT_R0706); \
  164. C7:6 = R11:10; }\
  165. { R9:8 = memd(R0 + #_PT_R0908); \
  166. R11:10 = memd(R0 + #_PT_R1110); \
  167. C3:2 = R13:12; }\
  168. { R13:12 = memd(R0 + #_PT_R1312); \
  169. R15:14 = memd(R0 + #_PT_R1514); \
  170. C1:0 = R15:14; }\
  171. { R17:16 = memd(R0 + #_PT_R1716); \
  172. R19:18 = memd(R0 + #_PT_R1918); } \
  173. { R21:20 = memd(R0 + #_PT_R2120); \
  174. R23:22 = memd(R0 + #_PT_R2322); } \
  175. { R25:24 = memd(R0 + #_PT_R2524); \
  176. R27:26 = memd(R0 + #_PT_R2726); } \
  177. R31:30 = memd(R0 + #_PT_CS1CS0); \
  178. { C13:12 = R31:30; \
  179. R31:30 = memd(R0 + #_PT_GPUGP) ; \
  180. R28 = memw(R0 + #_PT_R2928); }\
  181. { C11:10 = R31:30; \
  182. R31:30 = memd(R0 + #_PT_R3130); }
  183. #endif
  184. /*
  185. * Clears off enough space for the rest of pt_regs; evrec is a part
  186. * of pt_regs in HVM mode. Save R0/R1, set handler's address in R1.
  187. * R0 is the address of pt_regs and is the parameter to save_pt_regs.
  188. */
  189. /*
  190. * Since the HVM isn't automagically pushing the EVREC onto the stack anymore,
  191. * we'll subract the entire size out and then fill it in ourselves.
  192. * Need to save off R0, R1, R2, R3 immediately.
  193. */
  194. #if CONFIG_HEXAGON_ARCH_VERSION < 4
  195. #define vm_event_entry(CHandler) \
  196. { \
  197. R29 = add(R29, #-(_PT_REGS_SIZE)); \
  198. memd(R29 + #(_PT_R0100 + -_PT_REGS_SIZE)) = R1:0; \
  199. } \
  200. { \
  201. memd(R29 +#_PT_R0302) = R3:2; \
  202. } \
  203. trap1(#HVM_TRAP1_VMGETREGS); \
  204. { \
  205. memd(R29 + #_PT_ER_VMEL) = R1:0; \
  206. R0 = R29; \
  207. R1.L = #LO(CHandler); \
  208. } \
  209. { \
  210. memd(R29 + #_PT_ER_VMPSP) = R3:2; \
  211. R1.H = #HI(CHandler); \
  212. jump event_dispatch; \
  213. }
  214. #else
  215. /* V4+ */
  216. /* turn on I$ prefetch early */
  217. /* the # ## # syntax inserts a literal ## */
  218. #define vm_event_entry(CHandler) \
  219. { \
  220. R29 = add(R29, #-(_PT_REGS_SIZE)); \
  221. memd(R29 + #(_PT_R0100 + -_PT_REGS_SIZE)) = R1:0; \
  222. memd(R29 + #(_PT_R0302 + -_PT_REGS_SIZE)) = R3:2; \
  223. R0 = usr; \
  224. } \
  225. { \
  226. memw(R29 + #_PT_PREDSUSR) = R0; \
  227. R0 = setbit(R0, #16); \
  228. } \
  229. usr = R0; \
  230. R1:0 = G1:0; \
  231. { \
  232. memd(R29 + #_PT_ER_VMEL) = R1:0; \
  233. R1 = # ## #(CHandler); \
  234. R3:2 = G3:2; \
  235. } \
  236. { \
  237. R0 = R29; \
  238. memd(R29 + #_PT_ER_VMPSP) = R3:2; \
  239. jump event_dispatch; \
  240. }
  241. #endif
  242. .text
  243. /*
  244. * Do bulk save/restore in one place.
  245. * Adds a jump to dispatch latency, but
  246. * saves hundreds of bytes.
  247. */
  248. event_dispatch:
  249. save_pt_regs()
  250. callr r1
  251. /*
  252. * Coming back from the C-world, our thread info pointer
  253. * should be in the designated register (usually R19)
  254. *
  255. * If we were in kernel mode, we don't need to check scheduler
  256. * or signals if CONFIG_PREEMPTION is not set. If set, then it has
  257. * to jump to a need_resched kind of block.
  258. * BTW, CONFIG_PREEMPTION is not supported yet.
  259. */
  260. #ifdef CONFIG_PREEMPTION
  261. R0 = #VM_INT_DISABLE
  262. trap1(#HVM_TRAP1_VMSETIE)
  263. #endif
  264. /* "Nested control path" -- if the previous mode was kernel */
  265. {
  266. R0 = memw(R29 + #_PT_ER_VMEST);
  267. R26.L = #LO(do_work_pending);
  268. }
  269. {
  270. P0 = tstbit(R0, #HVM_VMEST_UM_SFT);
  271. if (!P0.new) jump:nt restore_all;
  272. R26.H = #HI(do_work_pending);
  273. R0 = #VM_INT_DISABLE;
  274. }
  275. /*
  276. * Check also the return from fork/system call, normally coming back from
  277. * user mode
  278. *
  279. * R26 needs to have do_work_pending, and R0 should have VM_INT_DISABLE
  280. */
  281. check_work_pending:
  282. /* Disable interrupts while checking TIF */
  283. trap1(#HVM_TRAP1_VMSETIE)
  284. {
  285. R0 = R29; /* regs should still be at top of stack */
  286. R1 = memw(THREADINFO_REG + #_THREAD_INFO_FLAGS);
  287. callr R26;
  288. }
  289. {
  290. P0 = cmp.eq(R0, #0); if (!P0.new) jump:nt check_work_pending;
  291. R0 = #VM_INT_DISABLE;
  292. }
  293. restore_all:
  294. /*
  295. * Disable interrupts, if they weren't already, before reg restore.
  296. * R0 gets preloaded with #VM_INT_DISABLE before we get here.
  297. */
  298. trap1(#HVM_TRAP1_VMSETIE)
  299. /* do the setregs here for VM 0.5 */
  300. /* R29 here should already be pointing at pt_regs */
  301. {
  302. R1:0 = memd(R29 + #_PT_ER_VMEL);
  303. R3:2 = memd(R29 + #_PT_ER_VMPSP);
  304. }
  305. #if CONFIG_HEXAGON_ARCH_VERSION < 4
  306. trap1(#HVM_TRAP1_VMSETREGS);
  307. #else
  308. G1:0 = R1:0;
  309. G3:2 = R3:2;
  310. #endif
  311. R0 = R29
  312. restore_pt_regs()
  313. {
  314. R1:0 = memd(R29 + #_PT_R0100);
  315. R29 = add(R29, #_PT_REGS_SIZE);
  316. }
  317. trap1(#HVM_TRAP1_VMRTE)
  318. /* Notreached */
  319. .globl _K_enter_genex
  320. _K_enter_genex:
  321. vm_event_entry(do_genex)
  322. .globl _K_enter_interrupt
  323. _K_enter_interrupt:
  324. vm_event_entry(arch_do_IRQ)
  325. .globl _K_enter_trap0
  326. _K_enter_trap0:
  327. vm_event_entry(do_trap0)
  328. .globl _K_enter_machcheck
  329. _K_enter_machcheck:
  330. vm_event_entry(do_machcheck)
  331. .globl _K_enter_debug
  332. _K_enter_debug:
  333. vm_event_entry(do_debug_exception)
  334. .globl ret_from_fork
  335. ret_from_fork:
  336. {
  337. call schedule_tail
  338. R26.H = #HI(do_work_pending);
  339. }
  340. {
  341. P0 = cmp.eq(R24, #0);
  342. R26.L = #LO(do_work_pending);
  343. R0 = #VM_INT_DISABLE;
  344. }
  345. if (P0) jump check_work_pending
  346. {
  347. R0 = R25;
  348. callr R24
  349. }
  350. {
  351. jump check_work_pending
  352. R0 = #VM_INT_DISABLE;
  353. }