vm_entry.S 11 KB

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