entry_64.S 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  1. /*
  2. * PowerPC version
  3. * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
  4. * Rewritten by Cort Dougan (cort@cs.nmt.edu) for PReP
  5. * Copyright (C) 1996 Cort Dougan <cort@cs.nmt.edu>
  6. * Adapted for Power Macintosh by Paul Mackerras.
  7. * Low-level exception handlers and MMU support
  8. * rewritten by Paul Mackerras.
  9. * Copyright (C) 1996 Paul Mackerras.
  10. * MPC8xx modifications Copyright (C) 1997 Dan Malek (dmalek@jlc.net).
  11. *
  12. * This file contains the system call entry code, context switch
  13. * code, and exception/interrupt return code for PowerPC.
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License
  17. * as published by the Free Software Foundation; either version
  18. * 2 of the License, or (at your option) any later version.
  19. */
  20. #include <linux/errno.h>
  21. #include <asm/unistd.h>
  22. #include <asm/processor.h>
  23. #include <asm/page.h>
  24. #include <asm/mmu.h>
  25. #include <asm/thread_info.h>
  26. #include <asm/ppc_asm.h>
  27. #include <asm/asm-offsets.h>
  28. #include <asm/cputable.h>
  29. #include <asm/firmware.h>
  30. #include <asm/bug.h>
  31. #include <asm/ptrace.h>
  32. #include <asm/irqflags.h>
  33. #include <asm/ftrace.h>
  34. /*
  35. * System calls.
  36. */
  37. .section ".toc","aw"
  38. .SYS_CALL_TABLE:
  39. .tc .sys_call_table[TC],.sys_call_table
  40. /* This value is used to mark exception frames on the stack. */
  41. exception_marker:
  42. .tc ID_EXC_MARKER[TC],STACK_FRAME_REGS_MARKER
  43. .section ".text"
  44. .align 7
  45. #undef SHOW_SYSCALLS
  46. .globl system_call_common
  47. system_call_common:
  48. andi. r10,r12,MSR_PR
  49. mr r10,r1
  50. addi r1,r1,-INT_FRAME_SIZE
  51. beq- 1f
  52. ld r1,PACAKSAVE(r13)
  53. 1: std r10,0(r1)
  54. std r11,_NIP(r1)
  55. std r12,_MSR(r1)
  56. std r0,GPR0(r1)
  57. std r10,GPR1(r1)
  58. ACCOUNT_CPU_USER_ENTRY(r10, r11)
  59. /*
  60. * This "crclr so" clears CR0.SO, which is the error indication on
  61. * return from this system call. There must be no cmp instruction
  62. * between it and the "mfcr r9" below, otherwise if XER.SO is set,
  63. * CR0.SO will get set, causing all system calls to appear to fail.
  64. */
  65. crclr so
  66. std r2,GPR2(r1)
  67. std r3,GPR3(r1)
  68. std r4,GPR4(r1)
  69. std r5,GPR5(r1)
  70. std r6,GPR6(r1)
  71. std r7,GPR7(r1)
  72. std r8,GPR8(r1)
  73. li r11,0
  74. std r11,GPR9(r1)
  75. std r11,GPR10(r1)
  76. std r11,GPR11(r1)
  77. std r11,GPR12(r1)
  78. std r9,GPR13(r1)
  79. mfcr r9
  80. mflr r10
  81. li r11,0xc01
  82. std r9,_CCR(r1)
  83. std r10,_LINK(r1)
  84. std r11,_TRAP(r1)
  85. mfxer r9
  86. mfctr r10
  87. std r9,_XER(r1)
  88. std r10,_CTR(r1)
  89. std r3,ORIG_GPR3(r1)
  90. ld r2,PACATOC(r13)
  91. addi r9,r1,STACK_FRAME_OVERHEAD
  92. ld r11,exception_marker@toc(r2)
  93. std r11,-16(r9) /* "regshere" marker */
  94. #ifdef CONFIG_TRACE_IRQFLAGS
  95. bl .trace_hardirqs_on
  96. REST_GPR(0,r1)
  97. REST_4GPRS(3,r1)
  98. REST_2GPRS(7,r1)
  99. addi r9,r1,STACK_FRAME_OVERHEAD
  100. ld r12,_MSR(r1)
  101. #endif /* CONFIG_TRACE_IRQFLAGS */
  102. li r10,1
  103. stb r10,PACASOFTIRQEN(r13)
  104. stb r10,PACAHARDIRQEN(r13)
  105. std r10,SOFTE(r1)
  106. #ifdef CONFIG_PPC_ISERIES
  107. BEGIN_FW_FTR_SECTION
  108. /* Hack for handling interrupts when soft-enabling on iSeries */
  109. cmpdi cr1,r0,0x5555 /* syscall 0x5555 */
  110. andi. r10,r12,MSR_PR /* from kernel */
  111. crand 4*cr0+eq,4*cr1+eq,4*cr0+eq
  112. bne 2f
  113. b hardware_interrupt_entry
  114. 2:
  115. END_FW_FTR_SECTION_IFSET(FW_FEATURE_ISERIES)
  116. #endif /* CONFIG_PPC_ISERIES */
  117. /* Hard enable interrupts */
  118. #ifdef CONFIG_PPC_BOOK3E
  119. wrteei 1
  120. #else
  121. mfmsr r11
  122. ori r11,r11,MSR_EE
  123. mtmsrd r11,1
  124. #endif /* CONFIG_PPC_BOOK3E */
  125. #ifdef SHOW_SYSCALLS
  126. bl .do_show_syscall
  127. REST_GPR(0,r1)
  128. REST_4GPRS(3,r1)
  129. REST_2GPRS(7,r1)
  130. addi r9,r1,STACK_FRAME_OVERHEAD
  131. #endif
  132. clrrdi r11,r1,THREAD_SHIFT
  133. ld r10,TI_FLAGS(r11)
  134. andi. r11,r10,_TIF_SYSCALL_T_OR_A
  135. bne- syscall_dotrace
  136. syscall_dotrace_cont:
  137. cmpldi 0,r0,NR_syscalls
  138. bge- syscall_enosys
  139. system_call: /* label this so stack traces look sane */
  140. /*
  141. * Need to vector to 32 Bit or default sys_call_table here,
  142. * based on caller's run-mode / personality.
  143. */
  144. ld r11,.SYS_CALL_TABLE@toc(2)
  145. andi. r10,r10,_TIF_32BIT
  146. beq 15f
  147. addi r11,r11,8 /* use 32-bit syscall entries */
  148. clrldi r3,r3,32
  149. clrldi r4,r4,32
  150. clrldi r5,r5,32
  151. clrldi r6,r6,32
  152. clrldi r7,r7,32
  153. clrldi r8,r8,32
  154. 15:
  155. slwi r0,r0,4
  156. ldx r10,r11,r0 /* Fetch system call handler [ptr] */
  157. mtctr r10
  158. bctrl /* Call handler */
  159. syscall_exit:
  160. std r3,RESULT(r1)
  161. #ifdef SHOW_SYSCALLS
  162. bl .do_show_syscall_exit
  163. ld r3,RESULT(r1)
  164. #endif
  165. clrrdi r12,r1,THREAD_SHIFT
  166. ld r8,_MSR(r1)
  167. #ifdef CONFIG_PPC_BOOK3S
  168. /* No MSR:RI on BookE */
  169. andi. r10,r8,MSR_RI
  170. beq- unrecov_restore
  171. #endif
  172. /* Disable interrupts so current_thread_info()->flags can't change,
  173. * and so that we don't get interrupted after loading SRR0/1.
  174. */
  175. #ifdef CONFIG_PPC_BOOK3E
  176. wrteei 0
  177. #else
  178. mfmsr r10
  179. rldicl r10,r10,48,1
  180. rotldi r10,r10,16
  181. mtmsrd r10,1
  182. #endif /* CONFIG_PPC_BOOK3E */
  183. ld r9,TI_FLAGS(r12)
  184. li r11,-_LAST_ERRNO
  185. andi. r0,r9,(_TIF_SYSCALL_T_OR_A|_TIF_SINGLESTEP|_TIF_USER_WORK_MASK|_TIF_PERSYSCALL_MASK)
  186. bne- syscall_exit_work
  187. cmpld r3,r11
  188. ld r5,_CCR(r1)
  189. bge- syscall_error
  190. syscall_error_cont:
  191. ld r7,_NIP(r1)
  192. BEGIN_FTR_SECTION
  193. stdcx. r0,0,r1 /* to clear the reservation */
  194. END_FTR_SECTION_IFCLR(CPU_FTR_STCX_CHECKS_ADDRESS)
  195. andi. r6,r8,MSR_PR
  196. ld r4,_LINK(r1)
  197. /*
  198. * Clear RI before restoring r13. If we are returning to
  199. * userspace and we take an exception after restoring r13,
  200. * we end up corrupting the userspace r13 value.
  201. */
  202. #ifdef CONFIG_PPC_BOOK3S
  203. /* No MSR:RI on BookE */
  204. li r12,MSR_RI
  205. andc r11,r10,r12
  206. mtmsrd r11,1 /* clear MSR.RI */
  207. #endif /* CONFIG_PPC_BOOK3S */
  208. beq- 1f
  209. ACCOUNT_CPU_USER_EXIT(r11, r12)
  210. ld r13,GPR13(r1) /* only restore r13 if returning to usermode */
  211. 1: ld r2,GPR2(r1)
  212. ld r1,GPR1(r1)
  213. mtlr r4
  214. mtcr r5
  215. mtspr SPRN_SRR0,r7
  216. mtspr SPRN_SRR1,r8
  217. RFI
  218. b . /* prevent speculative execution */
  219. syscall_error:
  220. oris r5,r5,0x1000 /* Set SO bit in CR */
  221. neg r3,r3
  222. std r5,_CCR(r1)
  223. b syscall_error_cont
  224. /* Traced system call support */
  225. syscall_dotrace:
  226. bl .save_nvgprs
  227. addi r3,r1,STACK_FRAME_OVERHEAD
  228. bl .do_syscall_trace_enter
  229. /*
  230. * Restore argument registers possibly just changed.
  231. * We use the return value of do_syscall_trace_enter
  232. * for the call number to look up in the table (r0).
  233. */
  234. mr r0,r3
  235. ld r3,GPR3(r1)
  236. ld r4,GPR4(r1)
  237. ld r5,GPR5(r1)
  238. ld r6,GPR6(r1)
  239. ld r7,GPR7(r1)
  240. ld r8,GPR8(r1)
  241. addi r9,r1,STACK_FRAME_OVERHEAD
  242. clrrdi r10,r1,THREAD_SHIFT
  243. ld r10,TI_FLAGS(r10)
  244. b syscall_dotrace_cont
  245. syscall_enosys:
  246. li r3,-ENOSYS
  247. b syscall_exit
  248. syscall_exit_work:
  249. /* If TIF_RESTOREALL is set, don't scribble on either r3 or ccr.
  250. If TIF_NOERROR is set, just save r3 as it is. */
  251. andi. r0,r9,_TIF_RESTOREALL
  252. beq+ 0f
  253. REST_NVGPRS(r1)
  254. b 2f
  255. 0: cmpld r3,r11 /* r10 is -LAST_ERRNO */
  256. blt+ 1f
  257. andi. r0,r9,_TIF_NOERROR
  258. bne- 1f
  259. ld r5,_CCR(r1)
  260. neg r3,r3
  261. oris r5,r5,0x1000 /* Set SO bit in CR */
  262. std r5,_CCR(r1)
  263. 1: std r3,GPR3(r1)
  264. 2: andi. r0,r9,(_TIF_PERSYSCALL_MASK)
  265. beq 4f
  266. /* Clear per-syscall TIF flags if any are set. */
  267. li r11,_TIF_PERSYSCALL_MASK
  268. addi r12,r12,TI_FLAGS
  269. 3: ldarx r10,0,r12
  270. andc r10,r10,r11
  271. stdcx. r10,0,r12
  272. bne- 3b
  273. subi r12,r12,TI_FLAGS
  274. 4: /* Anything else left to do? */
  275. andi. r0,r9,(_TIF_SYSCALL_T_OR_A|_TIF_SINGLESTEP)
  276. beq .ret_from_except_lite
  277. /* Re-enable interrupts */
  278. #ifdef CONFIG_PPC_BOOK3E
  279. wrteei 1
  280. #else
  281. mfmsr r10
  282. ori r10,r10,MSR_EE
  283. mtmsrd r10,1
  284. #endif /* CONFIG_PPC_BOOK3E */
  285. bl .save_nvgprs
  286. addi r3,r1,STACK_FRAME_OVERHEAD
  287. bl .do_syscall_trace_leave
  288. b .ret_from_except
  289. /* Save non-volatile GPRs, if not already saved. */
  290. _GLOBAL(save_nvgprs)
  291. ld r11,_TRAP(r1)
  292. andi. r0,r11,1
  293. beqlr-
  294. SAVE_NVGPRS(r1)
  295. clrrdi r0,r11,1
  296. std r0,_TRAP(r1)
  297. blr
  298. /*
  299. * The sigsuspend and rt_sigsuspend system calls can call do_signal
  300. * and thus put the process into the stopped state where we might
  301. * want to examine its user state with ptrace. Therefore we need
  302. * to save all the nonvolatile registers (r14 - r31) before calling
  303. * the C code. Similarly, fork, vfork and clone need the full
  304. * register state on the stack so that it can be copied to the child.
  305. */
  306. _GLOBAL(ppc_fork)
  307. bl .save_nvgprs
  308. bl .sys_fork
  309. b syscall_exit
  310. _GLOBAL(ppc_vfork)
  311. bl .save_nvgprs
  312. bl .sys_vfork
  313. b syscall_exit
  314. _GLOBAL(ppc_clone)
  315. bl .save_nvgprs
  316. bl .sys_clone
  317. b syscall_exit
  318. _GLOBAL(ppc32_swapcontext)
  319. bl .save_nvgprs
  320. bl .compat_sys_swapcontext
  321. b syscall_exit
  322. _GLOBAL(ppc64_swapcontext)
  323. bl .save_nvgprs
  324. bl .sys_swapcontext
  325. b syscall_exit
  326. _GLOBAL(ret_from_fork)
  327. bl .schedule_tail
  328. REST_NVGPRS(r1)
  329. li r3,0
  330. b syscall_exit
  331. /*
  332. * This routine switches between two different tasks. The process
  333. * state of one is saved on its kernel stack. Then the state
  334. * of the other is restored from its kernel stack. The memory
  335. * management hardware is updated to the second process's state.
  336. * Finally, we can return to the second process, via ret_from_except.
  337. * On entry, r3 points to the THREAD for the current task, r4
  338. * points to the THREAD for the new task.
  339. *
  340. * Note: there are two ways to get to the "going out" portion
  341. * of this code; either by coming in via the entry (_switch)
  342. * or via "fork" which must set up an environment equivalent
  343. * to the "_switch" path. If you change this you'll have to change
  344. * the fork code also.
  345. *
  346. * The code which creates the new task context is in 'copy_thread'
  347. * in arch/powerpc/kernel/process.c
  348. */
  349. .align 7
  350. _GLOBAL(_switch)
  351. mflr r0
  352. std r0,16(r1)
  353. stdu r1,-SWITCH_FRAME_SIZE(r1)
  354. /* r3-r13 are caller saved -- Cort */
  355. SAVE_8GPRS(14, r1)
  356. SAVE_10GPRS(22, r1)
  357. mflr r20 /* Return to switch caller */
  358. mfmsr r22
  359. li r0, MSR_FP
  360. #ifdef CONFIG_VSX
  361. BEGIN_FTR_SECTION
  362. oris r0,r0,MSR_VSX@h /* Disable VSX */
  363. END_FTR_SECTION_IFSET(CPU_FTR_VSX)
  364. #endif /* CONFIG_VSX */
  365. #ifdef CONFIG_ALTIVEC
  366. BEGIN_FTR_SECTION
  367. oris r0,r0,MSR_VEC@h /* Disable altivec */
  368. mfspr r24,SPRN_VRSAVE /* save vrsave register value */
  369. std r24,THREAD_VRSAVE(r3)
  370. END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
  371. #endif /* CONFIG_ALTIVEC */
  372. and. r0,r0,r22
  373. beq+ 1f
  374. andc r22,r22,r0
  375. MTMSRD(r22)
  376. isync
  377. 1: std r20,_NIP(r1)
  378. mfcr r23
  379. std r23,_CCR(r1)
  380. std r1,KSP(r3) /* Set old stack pointer */
  381. #ifdef CONFIG_SMP
  382. /* We need a sync somewhere here to make sure that if the
  383. * previous task gets rescheduled on another CPU, it sees all
  384. * stores it has performed on this one.
  385. */
  386. sync
  387. #endif /* CONFIG_SMP */
  388. /*
  389. * If we optimise away the clear of the reservation in system
  390. * calls because we know the CPU tracks the address of the
  391. * reservation, then we need to clear it here to cover the
  392. * case that the kernel context switch path has no larx
  393. * instructions.
  394. */
  395. BEGIN_FTR_SECTION
  396. ldarx r6,0,r1
  397. END_FTR_SECTION_IFSET(CPU_FTR_STCX_CHECKS_ADDRESS)
  398. addi r6,r4,-THREAD /* Convert THREAD to 'current' */
  399. std r6,PACACURRENT(r13) /* Set new 'current' */
  400. ld r8,KSP(r4) /* new stack pointer */
  401. #ifdef CONFIG_PPC_BOOK3S
  402. BEGIN_FTR_SECTION
  403. BEGIN_FTR_SECTION_NESTED(95)
  404. clrrdi r6,r8,28 /* get its ESID */
  405. clrrdi r9,r1,28 /* get current sp ESID */
  406. FTR_SECTION_ELSE_NESTED(95)
  407. clrrdi r6,r8,40 /* get its 1T ESID */
  408. clrrdi r9,r1,40 /* get current sp 1T ESID */
  409. ALT_FTR_SECTION_END_NESTED_IFCLR(CPU_FTR_1T_SEGMENT, 95)
  410. FTR_SECTION_ELSE
  411. b 2f
  412. ALT_FTR_SECTION_END_IFSET(CPU_FTR_SLB)
  413. clrldi. r0,r6,2 /* is new ESID c00000000? */
  414. cmpd cr1,r6,r9 /* or is new ESID the same as current ESID? */
  415. cror eq,4*cr1+eq,eq
  416. beq 2f /* if yes, don't slbie it */
  417. /* Bolt in the new stack SLB entry */
  418. ld r7,KSP_VSID(r4) /* Get new stack's VSID */
  419. oris r0,r6,(SLB_ESID_V)@h
  420. ori r0,r0,(SLB_NUM_BOLTED-1)@l
  421. BEGIN_FTR_SECTION
  422. li r9,MMU_SEGSIZE_1T /* insert B field */
  423. oris r6,r6,(MMU_SEGSIZE_1T << SLBIE_SSIZE_SHIFT)@h
  424. rldimi r7,r9,SLB_VSID_SSIZE_SHIFT,0
  425. END_FTR_SECTION_IFSET(CPU_FTR_1T_SEGMENT)
  426. /* Update the last bolted SLB. No write barriers are needed
  427. * here, provided we only update the current CPU's SLB shadow
  428. * buffer.
  429. */
  430. ld r9,PACA_SLBSHADOWPTR(r13)
  431. li r12,0
  432. std r12,SLBSHADOW_STACKESID(r9) /* Clear ESID */
  433. std r7,SLBSHADOW_STACKVSID(r9) /* Save VSID */
  434. std r0,SLBSHADOW_STACKESID(r9) /* Save ESID */
  435. /* No need to check for CPU_FTR_NO_SLBIE_B here, since when
  436. * we have 1TB segments, the only CPUs known to have the errata
  437. * only support less than 1TB of system memory and we'll never
  438. * actually hit this code path.
  439. */
  440. slbie r6
  441. slbie r6 /* Workaround POWER5 < DD2.1 issue */
  442. slbmte r7,r0
  443. isync
  444. 2:
  445. #endif /* !CONFIG_PPC_BOOK3S */
  446. clrrdi r7,r8,THREAD_SHIFT /* base of new stack */
  447. /* Note: this uses SWITCH_FRAME_SIZE rather than INT_FRAME_SIZE
  448. because we don't need to leave the 288-byte ABI gap at the
  449. top of the kernel stack. */
  450. addi r7,r7,THREAD_SIZE-SWITCH_FRAME_SIZE
  451. mr r1,r8 /* start using new stack pointer */
  452. std r7,PACAKSAVE(r13)
  453. ld r6,_CCR(r1)
  454. mtcrf 0xFF,r6
  455. #ifdef CONFIG_ALTIVEC
  456. BEGIN_FTR_SECTION
  457. ld r0,THREAD_VRSAVE(r4)
  458. mtspr SPRN_VRSAVE,r0 /* if G4, restore VRSAVE reg */
  459. END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
  460. #endif /* CONFIG_ALTIVEC */
  461. /* r3-r13 are destroyed -- Cort */
  462. REST_8GPRS(14, r1)
  463. REST_10GPRS(22, r1)
  464. /* convert old thread to its task_struct for return value */
  465. addi r3,r3,-THREAD
  466. ld r7,_NIP(r1) /* Return to _switch caller in new task */
  467. mtlr r7
  468. addi r1,r1,SWITCH_FRAME_SIZE
  469. blr
  470. .align 7
  471. _GLOBAL(ret_from_except)
  472. ld r11,_TRAP(r1)
  473. andi. r0,r11,1
  474. bne .ret_from_except_lite
  475. REST_NVGPRS(r1)
  476. _GLOBAL(ret_from_except_lite)
  477. /*
  478. * Disable interrupts so that current_thread_info()->flags
  479. * can't change between when we test it and when we return
  480. * from the interrupt.
  481. */
  482. #ifdef CONFIG_PPC_BOOK3E
  483. wrteei 0
  484. #else
  485. mfmsr r10 /* Get current interrupt state */
  486. rldicl r9,r10,48,1 /* clear MSR_EE */
  487. rotldi r9,r9,16
  488. mtmsrd r9,1 /* Update machine state */
  489. #endif /* CONFIG_PPC_BOOK3E */
  490. #ifdef CONFIG_PREEMPT
  491. clrrdi r9,r1,THREAD_SHIFT /* current_thread_info() */
  492. li r0,_TIF_NEED_RESCHED /* bits to check */
  493. ld r3,_MSR(r1)
  494. ld r4,TI_FLAGS(r9)
  495. /* Move MSR_PR bit in r3 to _TIF_SIGPENDING position in r0 */
  496. rlwimi r0,r3,32+TIF_SIGPENDING-MSR_PR_LG,_TIF_SIGPENDING
  497. and. r0,r4,r0 /* check NEED_RESCHED and maybe SIGPENDING */
  498. bne do_work
  499. #else /* !CONFIG_PREEMPT */
  500. ld r3,_MSR(r1) /* Returning to user mode? */
  501. andi. r3,r3,MSR_PR
  502. beq restore /* if not, just restore regs and return */
  503. /* Check current_thread_info()->flags */
  504. clrrdi r9,r1,THREAD_SHIFT
  505. ld r4,TI_FLAGS(r9)
  506. andi. r0,r4,_TIF_USER_WORK_MASK
  507. bne do_work
  508. #endif
  509. restore:
  510. BEGIN_FW_FTR_SECTION
  511. ld r5,SOFTE(r1)
  512. FW_FTR_SECTION_ELSE
  513. b .Liseries_check_pending_irqs
  514. ALT_FW_FTR_SECTION_END_IFCLR(FW_FEATURE_ISERIES)
  515. 2:
  516. TRACE_AND_RESTORE_IRQ(r5);
  517. /* extract EE bit and use it to restore paca->hard_enabled */
  518. ld r3,_MSR(r1)
  519. rldicl r4,r3,49,63 /* r0 = (r3 >> 15) & 1 */
  520. stb r4,PACAHARDIRQEN(r13)
  521. #ifdef CONFIG_PPC_BOOK3E
  522. b .exception_return_book3e
  523. #else
  524. ld r4,_CTR(r1)
  525. ld r0,_LINK(r1)
  526. mtctr r4
  527. mtlr r0
  528. ld r4,_XER(r1)
  529. mtspr SPRN_XER,r4
  530. REST_8GPRS(5, r1)
  531. andi. r0,r3,MSR_RI
  532. beq- unrecov_restore
  533. /*
  534. * Clear the reservation. If we know the CPU tracks the address of
  535. * the reservation then we can potentially save some cycles and use
  536. * a larx. On POWER6 and POWER7 this is significantly faster.
  537. */
  538. BEGIN_FTR_SECTION
  539. stdcx. r0,0,r1 /* to clear the reservation */
  540. FTR_SECTION_ELSE
  541. ldarx r4,0,r1
  542. ALT_FTR_SECTION_END_IFCLR(CPU_FTR_STCX_CHECKS_ADDRESS)
  543. /*
  544. * Clear RI before restoring r13. If we are returning to
  545. * userspace and we take an exception after restoring r13,
  546. * we end up corrupting the userspace r13 value.
  547. */
  548. mfmsr r4
  549. andc r4,r4,r0 /* r0 contains MSR_RI here */
  550. mtmsrd r4,1
  551. /*
  552. * r13 is our per cpu area, only restore it if we are returning to
  553. * userspace
  554. */
  555. andi. r0,r3,MSR_PR
  556. beq 1f
  557. ACCOUNT_CPU_USER_EXIT(r2, r4)
  558. REST_GPR(13, r1)
  559. 1:
  560. mtspr SPRN_SRR1,r3
  561. ld r2,_CCR(r1)
  562. mtcrf 0xFF,r2
  563. ld r2,_NIP(r1)
  564. mtspr SPRN_SRR0,r2
  565. ld r0,GPR0(r1)
  566. ld r2,GPR2(r1)
  567. ld r3,GPR3(r1)
  568. ld r4,GPR4(r1)
  569. ld r1,GPR1(r1)
  570. rfid
  571. b . /* prevent speculative execution */
  572. #endif /* CONFIG_PPC_BOOK3E */
  573. .Liseries_check_pending_irqs:
  574. #ifdef CONFIG_PPC_ISERIES
  575. ld r5,SOFTE(r1)
  576. cmpdi 0,r5,0
  577. beq 2b
  578. /* Check for pending interrupts (iSeries) */
  579. ld r3,PACALPPACAPTR(r13)
  580. ld r3,LPPACAANYINT(r3)
  581. cmpdi r3,0
  582. beq+ 2b /* skip do_IRQ if no interrupts */
  583. li r3,0
  584. stb r3,PACASOFTIRQEN(r13) /* ensure we are soft-disabled */
  585. #ifdef CONFIG_TRACE_IRQFLAGS
  586. bl .trace_hardirqs_off
  587. mfmsr r10
  588. #endif
  589. ori r10,r10,MSR_EE
  590. mtmsrd r10 /* hard-enable again */
  591. addi r3,r1,STACK_FRAME_OVERHEAD
  592. bl .do_IRQ
  593. b .ret_from_except_lite /* loop back and handle more */
  594. #endif
  595. do_work:
  596. #ifdef CONFIG_PREEMPT
  597. andi. r0,r3,MSR_PR /* Returning to user mode? */
  598. bne user_work
  599. /* Check that preempt_count() == 0 and interrupts are enabled */
  600. lwz r8,TI_PREEMPT(r9)
  601. cmpwi cr1,r8,0
  602. ld r0,SOFTE(r1)
  603. cmpdi r0,0
  604. crandc eq,cr1*4+eq,eq
  605. bne restore
  606. /* Here we are preempting the current task.
  607. *
  608. * Ensure interrupts are soft-disabled. We also properly mark
  609. * the PACA to reflect the fact that they are hard-disabled
  610. * and trace the change
  611. */
  612. li r0,0
  613. stb r0,PACASOFTIRQEN(r13)
  614. stb r0,PACAHARDIRQEN(r13)
  615. TRACE_DISABLE_INTS
  616. /* Call the scheduler with soft IRQs off */
  617. 1: bl .preempt_schedule_irq
  618. /* Hard-disable interrupts again (and update PACA) */
  619. #ifdef CONFIG_PPC_BOOK3E
  620. wrteei 0
  621. #else
  622. mfmsr r10
  623. rldicl r10,r10,48,1
  624. rotldi r10,r10,16
  625. mtmsrd r10,1
  626. #endif /* CONFIG_PPC_BOOK3E */
  627. li r0,0
  628. stb r0,PACAHARDIRQEN(r13)
  629. /* Re-test flags and eventually loop */
  630. clrrdi r9,r1,THREAD_SHIFT
  631. ld r4,TI_FLAGS(r9)
  632. andi. r0,r4,_TIF_NEED_RESCHED
  633. bne 1b
  634. b restore
  635. user_work:
  636. #endif /* CONFIG_PREEMPT */
  637. /* Enable interrupts */
  638. #ifdef CONFIG_PPC_BOOK3E
  639. wrteei 1
  640. #else
  641. ori r10,r10,MSR_EE
  642. mtmsrd r10,1
  643. #endif /* CONFIG_PPC_BOOK3E */
  644. andi. r0,r4,_TIF_NEED_RESCHED
  645. beq 1f
  646. bl .schedule
  647. b .ret_from_except_lite
  648. 1: bl .save_nvgprs
  649. addi r3,r1,STACK_FRAME_OVERHEAD
  650. bl .do_signal
  651. b .ret_from_except
  652. unrecov_restore:
  653. addi r3,r1,STACK_FRAME_OVERHEAD
  654. bl .unrecoverable_exception
  655. b unrecov_restore
  656. #ifdef CONFIG_PPC_RTAS
  657. /*
  658. * On CHRP, the Run-Time Abstraction Services (RTAS) have to be
  659. * called with the MMU off.
  660. *
  661. * In addition, we need to be in 32b mode, at least for now.
  662. *
  663. * Note: r3 is an input parameter to rtas, so don't trash it...
  664. */
  665. _GLOBAL(enter_rtas)
  666. mflr r0
  667. std r0,16(r1)
  668. stdu r1,-RTAS_FRAME_SIZE(r1) /* Save SP and create stack space. */
  669. /* Because RTAS is running in 32b mode, it clobbers the high order half
  670. * of all registers that it saves. We therefore save those registers
  671. * RTAS might touch to the stack. (r0, r3-r13 are caller saved)
  672. */
  673. SAVE_GPR(2, r1) /* Save the TOC */
  674. SAVE_GPR(13, r1) /* Save paca */
  675. SAVE_8GPRS(14, r1) /* Save the non-volatiles */
  676. SAVE_10GPRS(22, r1) /* ditto */
  677. mfcr r4
  678. std r4,_CCR(r1)
  679. mfctr r5
  680. std r5,_CTR(r1)
  681. mfspr r6,SPRN_XER
  682. std r6,_XER(r1)
  683. mfdar r7
  684. std r7,_DAR(r1)
  685. mfdsisr r8
  686. std r8,_DSISR(r1)
  687. /* Temporary workaround to clear CR until RTAS can be modified to
  688. * ignore all bits.
  689. */
  690. li r0,0
  691. mtcr r0
  692. #ifdef CONFIG_BUG
  693. /* There is no way it is acceptable to get here with interrupts enabled,
  694. * check it with the asm equivalent of WARN_ON
  695. */
  696. lbz r0,PACASOFTIRQEN(r13)
  697. 1: tdnei r0,0
  698. EMIT_BUG_ENTRY 1b,__FILE__,__LINE__,BUGFLAG_WARNING
  699. #endif
  700. /* Hard-disable interrupts */
  701. mfmsr r6
  702. rldicl r7,r6,48,1
  703. rotldi r7,r7,16
  704. mtmsrd r7,1
  705. /* Unfortunately, the stack pointer and the MSR are also clobbered,
  706. * so they are saved in the PACA which allows us to restore
  707. * our original state after RTAS returns.
  708. */
  709. std r1,PACAR1(r13)
  710. std r6,PACASAVEDMSR(r13)
  711. /* Setup our real return addr */
  712. LOAD_REG_ADDR(r4,.rtas_return_loc)
  713. clrldi r4,r4,2 /* convert to realmode address */
  714. mtlr r4
  715. li r0,0
  716. ori r0,r0,MSR_EE|MSR_SE|MSR_BE|MSR_RI
  717. andc r0,r6,r0
  718. li r9,1
  719. rldicr r9,r9,MSR_SF_LG,(63-MSR_SF_LG)
  720. ori r9,r9,MSR_IR|MSR_DR|MSR_FE0|MSR_FE1|MSR_FP|MSR_RI
  721. andc r6,r0,r9
  722. sync /* disable interrupts so SRR0/1 */
  723. mtmsrd r0 /* don't get trashed */
  724. LOAD_REG_ADDR(r4, rtas)
  725. ld r5,RTASENTRY(r4) /* get the rtas->entry value */
  726. ld r4,RTASBASE(r4) /* get the rtas->base value */
  727. mtspr SPRN_SRR0,r5
  728. mtspr SPRN_SRR1,r6
  729. rfid
  730. b . /* prevent speculative execution */
  731. _STATIC(rtas_return_loc)
  732. /* relocation is off at this point */
  733. mfspr r4,SPRN_SPRG_PACA /* Get PACA */
  734. clrldi r4,r4,2 /* convert to realmode address */
  735. bcl 20,31,$+4
  736. 0: mflr r3
  737. ld r3,(1f-0b)(r3) /* get &.rtas_restore_regs */
  738. mfmsr r6
  739. li r0,MSR_RI
  740. andc r6,r6,r0
  741. sync
  742. mtmsrd r6
  743. ld r1,PACAR1(r4) /* Restore our SP */
  744. ld r4,PACASAVEDMSR(r4) /* Restore our MSR */
  745. mtspr SPRN_SRR0,r3
  746. mtspr SPRN_SRR1,r4
  747. rfid
  748. b . /* prevent speculative execution */
  749. .align 3
  750. 1: .llong .rtas_restore_regs
  751. _STATIC(rtas_restore_regs)
  752. /* relocation is on at this point */
  753. REST_GPR(2, r1) /* Restore the TOC */
  754. REST_GPR(13, r1) /* Restore paca */
  755. REST_8GPRS(14, r1) /* Restore the non-volatiles */
  756. REST_10GPRS(22, r1) /* ditto */
  757. mfspr r13,SPRN_SPRG_PACA
  758. ld r4,_CCR(r1)
  759. mtcr r4
  760. ld r5,_CTR(r1)
  761. mtctr r5
  762. ld r6,_XER(r1)
  763. mtspr SPRN_XER,r6
  764. ld r7,_DAR(r1)
  765. mtdar r7
  766. ld r8,_DSISR(r1)
  767. mtdsisr r8
  768. addi r1,r1,RTAS_FRAME_SIZE /* Unstack our frame */
  769. ld r0,16(r1) /* get return address */
  770. mtlr r0
  771. blr /* return to caller */
  772. #endif /* CONFIG_PPC_RTAS */
  773. _GLOBAL(enter_prom)
  774. mflr r0
  775. std r0,16(r1)
  776. stdu r1,-PROM_FRAME_SIZE(r1) /* Save SP and create stack space */
  777. /* Because PROM is running in 32b mode, it clobbers the high order half
  778. * of all registers that it saves. We therefore save those registers
  779. * PROM might touch to the stack. (r0, r3-r13 are caller saved)
  780. */
  781. SAVE_GPR(2, r1)
  782. SAVE_GPR(13, r1)
  783. SAVE_8GPRS(14, r1)
  784. SAVE_10GPRS(22, r1)
  785. mfcr r10
  786. mfmsr r11
  787. std r10,_CCR(r1)
  788. std r11,_MSR(r1)
  789. /* Get the PROM entrypoint */
  790. mtlr r4
  791. /* Switch MSR to 32 bits mode
  792. */
  793. #ifdef CONFIG_PPC_BOOK3E
  794. rlwinm r11,r11,0,1,31
  795. mtmsr r11
  796. #else /* CONFIG_PPC_BOOK3E */
  797. mfmsr r11
  798. li r12,1
  799. rldicr r12,r12,MSR_SF_LG,(63-MSR_SF_LG)
  800. andc r11,r11,r12
  801. li r12,1
  802. rldicr r12,r12,MSR_ISF_LG,(63-MSR_ISF_LG)
  803. andc r11,r11,r12
  804. mtmsrd r11
  805. #endif /* CONFIG_PPC_BOOK3E */
  806. isync
  807. /* Enter PROM here... */
  808. blrl
  809. /* Just make sure that r1 top 32 bits didn't get
  810. * corrupt by OF
  811. */
  812. rldicl r1,r1,0,32
  813. /* Restore the MSR (back to 64 bits) */
  814. ld r0,_MSR(r1)
  815. MTMSRD(r0)
  816. isync
  817. /* Restore other registers */
  818. REST_GPR(2, r1)
  819. REST_GPR(13, r1)
  820. REST_8GPRS(14, r1)
  821. REST_10GPRS(22, r1)
  822. ld r4,_CCR(r1)
  823. mtcr r4
  824. addi r1,r1,PROM_FRAME_SIZE
  825. ld r0,16(r1)
  826. mtlr r0
  827. blr
  828. #ifdef CONFIG_FUNCTION_TRACER
  829. #ifdef CONFIG_DYNAMIC_FTRACE
  830. _GLOBAL(mcount)
  831. _GLOBAL(_mcount)
  832. blr
  833. _GLOBAL(ftrace_caller)
  834. /* Taken from output of objdump from lib64/glibc */
  835. mflr r3
  836. ld r11, 0(r1)
  837. stdu r1, -112(r1)
  838. std r3, 128(r1)
  839. ld r4, 16(r11)
  840. subi r3, r3, MCOUNT_INSN_SIZE
  841. .globl ftrace_call
  842. ftrace_call:
  843. bl ftrace_stub
  844. nop
  845. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  846. .globl ftrace_graph_call
  847. ftrace_graph_call:
  848. b ftrace_graph_stub
  849. _GLOBAL(ftrace_graph_stub)
  850. #endif
  851. ld r0, 128(r1)
  852. mtlr r0
  853. addi r1, r1, 112
  854. _GLOBAL(ftrace_stub)
  855. blr
  856. #else
  857. _GLOBAL(mcount)
  858. blr
  859. _GLOBAL(_mcount)
  860. /* Taken from output of objdump from lib64/glibc */
  861. mflr r3
  862. ld r11, 0(r1)
  863. stdu r1, -112(r1)
  864. std r3, 128(r1)
  865. ld r4, 16(r11)
  866. subi r3, r3, MCOUNT_INSN_SIZE
  867. LOAD_REG_ADDR(r5,ftrace_trace_function)
  868. ld r5,0(r5)
  869. ld r5,0(r5)
  870. mtctr r5
  871. bctrl
  872. nop
  873. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  874. b ftrace_graph_caller
  875. #endif
  876. ld r0, 128(r1)
  877. mtlr r0
  878. addi r1, r1, 112
  879. _GLOBAL(ftrace_stub)
  880. blr
  881. #endif /* CONFIG_DYNAMIC_FTRACE */
  882. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  883. _GLOBAL(ftrace_graph_caller)
  884. /* load r4 with local address */
  885. ld r4, 128(r1)
  886. subi r4, r4, MCOUNT_INSN_SIZE
  887. /* get the parent address */
  888. ld r11, 112(r1)
  889. addi r3, r11, 16
  890. bl .prepare_ftrace_return
  891. nop
  892. ld r0, 128(r1)
  893. mtlr r0
  894. addi r1, r1, 112
  895. blr
  896. _GLOBAL(return_to_handler)
  897. /* need to save return values */
  898. std r4, -24(r1)
  899. std r3, -16(r1)
  900. std r31, -8(r1)
  901. mr r31, r1
  902. stdu r1, -112(r1)
  903. bl .ftrace_return_to_handler
  904. nop
  905. /* return value has real return address */
  906. mtlr r3
  907. ld r1, 0(r1)
  908. ld r4, -24(r1)
  909. ld r3, -16(r1)
  910. ld r31, -8(r1)
  911. /* Jump back to real return address */
  912. blr
  913. _GLOBAL(mod_return_to_handler)
  914. /* need to save return values */
  915. std r4, -32(r1)
  916. std r3, -24(r1)
  917. /* save TOC */
  918. std r2, -16(r1)
  919. std r31, -8(r1)
  920. mr r31, r1
  921. stdu r1, -112(r1)
  922. /*
  923. * We are in a module using the module's TOC.
  924. * Switch to our TOC to run inside the core kernel.
  925. */
  926. ld r2, PACATOC(r13)
  927. bl .ftrace_return_to_handler
  928. nop
  929. /* return value has real return address */
  930. mtlr r3
  931. ld r1, 0(r1)
  932. ld r4, -32(r1)
  933. ld r3, -24(r1)
  934. ld r2, -16(r1)
  935. ld r31, -8(r1)
  936. /* Jump back to real return address */
  937. blr
  938. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  939. #endif /* CONFIG_FUNCTION_TRACER */