entry.S 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  1. /*
  2. * PowerPC version
  3. * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
  4. * Rewritten by Cort Dougan (cort@fsmlabs.com) for PReP
  5. * Copyright (C) 1996 Cort Dougan <cort@fsmlabs.com>
  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. */
  21. #include <linux/errno.h>
  22. #include <linux/sys.h>
  23. #include <linux/threads.h>
  24. #include <asm/processor.h>
  25. #include <asm/page.h>
  26. #include <asm/mmu.h>
  27. #include <asm/cputable.h>
  28. #include <asm/thread_info.h>
  29. #include <asm/ppc_asm.h>
  30. #include <asm/asm-offsets.h>
  31. #include <asm/unistd.h>
  32. #undef SHOW_SYSCALLS
  33. #undef SHOW_SYSCALLS_TASK
  34. /*
  35. * MSR_KERNEL is > 0x10000 on 4xx/Book-E since it include MSR_CE.
  36. */
  37. #if MSR_KERNEL >= 0x10000
  38. #define LOAD_MSR_KERNEL(r, x) lis r,(x)@h; ori r,r,(x)@l
  39. #else
  40. #define LOAD_MSR_KERNEL(r, x) li r,(x)
  41. #endif
  42. #ifdef CONFIG_BOOKE
  43. #include "head_booke.h"
  44. #define TRANSFER_TO_HANDLER_EXC_LEVEL(exc_level) \
  45. mtspr exc_level##_SPRG,r8; \
  46. BOOKE_LOAD_EXC_LEVEL_STACK(exc_level); \
  47. lwz r0,GPR10-INT_FRAME_SIZE(r8); \
  48. stw r0,GPR10(r11); \
  49. lwz r0,GPR11-INT_FRAME_SIZE(r8); \
  50. stw r0,GPR11(r11); \
  51. mfspr r8,exc_level##_SPRG
  52. .globl mcheck_transfer_to_handler
  53. mcheck_transfer_to_handler:
  54. TRANSFER_TO_HANDLER_EXC_LEVEL(MCHECK)
  55. b transfer_to_handler_full
  56. .globl debug_transfer_to_handler
  57. debug_transfer_to_handler:
  58. TRANSFER_TO_HANDLER_EXC_LEVEL(DEBUG)
  59. b transfer_to_handler_full
  60. .globl crit_transfer_to_handler
  61. crit_transfer_to_handler:
  62. TRANSFER_TO_HANDLER_EXC_LEVEL(CRIT)
  63. /* fall through */
  64. #endif
  65. #ifdef CONFIG_40x
  66. .globl crit_transfer_to_handler
  67. crit_transfer_to_handler:
  68. lwz r0,crit_r10@l(0)
  69. stw r0,GPR10(r11)
  70. lwz r0,crit_r11@l(0)
  71. stw r0,GPR11(r11)
  72. /* fall through */
  73. #endif
  74. /*
  75. * This code finishes saving the registers to the exception frame
  76. * and jumps to the appropriate handler for the exception, turning
  77. * on address translation.
  78. * Note that we rely on the caller having set cr0.eq iff the exception
  79. * occurred in kernel mode (i.e. MSR:PR = 0).
  80. */
  81. .globl transfer_to_handler_full
  82. transfer_to_handler_full:
  83. SAVE_NVGPRS(r11)
  84. /* fall through */
  85. .globl transfer_to_handler
  86. transfer_to_handler:
  87. stw r2,GPR2(r11)
  88. stw r12,_NIP(r11)
  89. stw r9,_MSR(r11)
  90. andi. r2,r9,MSR_PR
  91. mfctr r12
  92. mfspr r2,SPRN_XER
  93. stw r12,_CTR(r11)
  94. stw r2,_XER(r11)
  95. mfspr r12,SPRN_SPRG3
  96. addi r2,r12,-THREAD
  97. tovirt(r2,r2) /* set r2 to current */
  98. beq 2f /* if from user, fix up THREAD.regs */
  99. addi r11,r1,STACK_FRAME_OVERHEAD
  100. stw r11,PT_REGS(r12)
  101. #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
  102. /* Check to see if the dbcr0 register is set up to debug. Use the
  103. single-step bit to do this. */
  104. lwz r12,THREAD_DBCR0(r12)
  105. andis. r12,r12,DBCR0_IC@h
  106. beq+ 3f
  107. /* From user and task is ptraced - load up global dbcr0 */
  108. li r12,-1 /* clear all pending debug events */
  109. mtspr SPRN_DBSR,r12
  110. lis r11,global_dbcr0@ha
  111. tophys(r11,r11)
  112. addi r11,r11,global_dbcr0@l
  113. lwz r12,0(r11)
  114. mtspr SPRN_DBCR0,r12
  115. lwz r12,4(r11)
  116. addi r12,r12,-1
  117. stw r12,4(r11)
  118. #endif
  119. b 3f
  120. 2: /* if from kernel, check interrupted DOZE/NAP mode and
  121. * check for stack overflow
  122. */
  123. lwz r9,THREAD_INFO-THREAD(r12)
  124. cmplw r1,r9 /* if r1 <= current->thread_info */
  125. ble- stack_ovf /* then the kernel stack overflowed */
  126. 5:
  127. #ifdef CONFIG_6xx
  128. tophys(r9,r9) /* check local flags */
  129. lwz r12,TI_LOCAL_FLAGS(r9)
  130. mtcrf 0x01,r12
  131. bt- 31-TLF_NAPPING,4f
  132. #endif /* CONFIG_6xx */
  133. .globl transfer_to_handler_cont
  134. transfer_to_handler_cont:
  135. 3:
  136. mflr r9
  137. lwz r11,0(r9) /* virtual address of handler */
  138. lwz r9,4(r9) /* where to go when done */
  139. mtspr SPRN_SRR0,r11
  140. mtspr SPRN_SRR1,r10
  141. mtlr r9
  142. SYNC
  143. RFI /* jump to handler, enable MMU */
  144. #ifdef CONFIG_6xx
  145. 4: rlwinm r12,r12,0,~_TLF_NAPPING
  146. stw r12,TI_LOCAL_FLAGS(r9)
  147. b power_save_6xx_restore
  148. #endif
  149. /*
  150. * On kernel stack overflow, load up an initial stack pointer
  151. * and call StackOverflow(regs), which should not return.
  152. */
  153. stack_ovf:
  154. /* sometimes we use a statically-allocated stack, which is OK. */
  155. lis r12,_end@h
  156. ori r12,r12,_end@l
  157. cmplw r1,r12
  158. ble 5b /* r1 <= &_end is OK */
  159. SAVE_NVGPRS(r11)
  160. addi r3,r1,STACK_FRAME_OVERHEAD
  161. lis r1,init_thread_union@ha
  162. addi r1,r1,init_thread_union@l
  163. addi r1,r1,THREAD_SIZE-STACK_FRAME_OVERHEAD
  164. lis r9,StackOverflow@ha
  165. addi r9,r9,StackOverflow@l
  166. LOAD_MSR_KERNEL(r10,MSR_KERNEL)
  167. FIX_SRR1(r10,r12)
  168. mtspr SPRN_SRR0,r9
  169. mtspr SPRN_SRR1,r10
  170. SYNC
  171. RFI
  172. /*
  173. * Handle a system call.
  174. */
  175. .stabs "arch/ppc/kernel/",N_SO,0,0,0f
  176. .stabs "entry.S",N_SO,0,0,0f
  177. 0:
  178. _GLOBAL(DoSyscall)
  179. stw r3,ORIG_GPR3(r1)
  180. li r12,0
  181. stw r12,RESULT(r1)
  182. lwz r11,_CCR(r1) /* Clear SO bit in CR */
  183. rlwinm r11,r11,0,4,2
  184. stw r11,_CCR(r1)
  185. #ifdef SHOW_SYSCALLS
  186. bl do_show_syscall
  187. #endif /* SHOW_SYSCALLS */
  188. rlwinm r10,r1,0,0,18 /* current_thread_info() */
  189. lwz r11,TI_FLAGS(r10)
  190. andi. r11,r11,_TIF_SYSCALL_T_OR_A
  191. bne- syscall_dotrace
  192. syscall_dotrace_cont:
  193. cmplwi 0,r0,NR_syscalls
  194. lis r10,sys_call_table@h
  195. ori r10,r10,sys_call_table@l
  196. slwi r0,r0,2
  197. bge- 66f
  198. lwzx r10,r10,r0 /* Fetch system call handler [ptr] */
  199. mtlr r10
  200. addi r9,r1,STACK_FRAME_OVERHEAD
  201. PPC440EP_ERR42
  202. blrl /* Call handler */
  203. .globl ret_from_syscall
  204. ret_from_syscall:
  205. #ifdef SHOW_SYSCALLS
  206. bl do_show_syscall_exit
  207. #endif
  208. mr r6,r3
  209. rlwinm r12,r1,0,0,18 /* current_thread_info() */
  210. /* disable interrupts so current_thread_info()->flags can't change */
  211. LOAD_MSR_KERNEL(r10,MSR_KERNEL) /* doesn't include MSR_EE */
  212. SYNC
  213. MTMSRD(r10)
  214. lwz r9,TI_FLAGS(r12)
  215. li r8,-_LAST_ERRNO
  216. andi. r0,r9,(_TIF_SYSCALL_T_OR_A|_TIF_SINGLESTEP|_TIF_USER_WORK_MASK|_TIF_PERSYSCALL_MASK)
  217. bne- syscall_exit_work
  218. cmplw 0,r3,r8
  219. blt+ syscall_exit_cont
  220. lwz r11,_CCR(r1) /* Load CR */
  221. neg r3,r3
  222. oris r11,r11,0x1000 /* Set SO bit in CR */
  223. stw r11,_CCR(r1)
  224. syscall_exit_cont:
  225. #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
  226. /* If the process has its own DBCR0 value, load it up. The single
  227. step bit tells us that dbcr0 should be loaded. */
  228. lwz r0,THREAD+THREAD_DBCR0(r2)
  229. andis. r10,r0,DBCR0_IC@h
  230. bnel- load_dbcr0
  231. #endif
  232. stwcx. r0,0,r1 /* to clear the reservation */
  233. lwz r4,_LINK(r1)
  234. lwz r5,_CCR(r1)
  235. mtlr r4
  236. mtcr r5
  237. lwz r7,_NIP(r1)
  238. lwz r8,_MSR(r1)
  239. FIX_SRR1(r8, r0)
  240. lwz r2,GPR2(r1)
  241. lwz r1,GPR1(r1)
  242. mtspr SPRN_SRR0,r7
  243. mtspr SPRN_SRR1,r8
  244. SYNC
  245. RFI
  246. 66: li r3,-ENOSYS
  247. b ret_from_syscall
  248. .globl ret_from_fork
  249. ret_from_fork:
  250. REST_NVGPRS(r1)
  251. bl schedule_tail
  252. li r3,0
  253. b ret_from_syscall
  254. /* Traced system call support */
  255. syscall_dotrace:
  256. SAVE_NVGPRS(r1)
  257. li r0,0xc00
  258. stw r0,TRAP(r1)
  259. addi r3,r1,STACK_FRAME_OVERHEAD
  260. bl do_syscall_trace_enter
  261. lwz r0,GPR0(r1) /* Restore original registers */
  262. lwz r3,GPR3(r1)
  263. lwz r4,GPR4(r1)
  264. lwz r5,GPR5(r1)
  265. lwz r6,GPR6(r1)
  266. lwz r7,GPR7(r1)
  267. lwz r8,GPR8(r1)
  268. REST_NVGPRS(r1)
  269. b syscall_dotrace_cont
  270. syscall_exit_work:
  271. andi. r0,r9,_TIF_RESTOREALL
  272. beq+ 0f
  273. REST_NVGPRS(r1)
  274. b 2f
  275. 0: cmplw 0,r3,r8
  276. blt+ 1f
  277. andi. r0,r9,_TIF_NOERROR
  278. bne- 1f
  279. lwz r11,_CCR(r1) /* Load CR */
  280. neg r3,r3
  281. oris r11,r11,0x1000 /* Set SO bit in CR */
  282. stw r11,_CCR(r1)
  283. 1: stw r6,RESULT(r1) /* Save result */
  284. stw r3,GPR3(r1) /* Update return value */
  285. 2: andi. r0,r9,(_TIF_PERSYSCALL_MASK)
  286. beq 4f
  287. /* Clear per-syscall TIF flags if any are set. */
  288. li r11,_TIF_PERSYSCALL_MASK
  289. addi r12,r12,TI_FLAGS
  290. 3: lwarx r8,0,r12
  291. andc r8,r8,r11
  292. #ifdef CONFIG_IBM405_ERR77
  293. dcbt 0,r12
  294. #endif
  295. stwcx. r8,0,r12
  296. bne- 3b
  297. subi r12,r12,TI_FLAGS
  298. 4: /* Anything which requires enabling interrupts? */
  299. andi. r0,r9,(_TIF_SYSCALL_T_OR_A|_TIF_SINGLESTEP)
  300. beq ret_from_except
  301. /* Re-enable interrupts */
  302. ori r10,r10,MSR_EE
  303. SYNC
  304. MTMSRD(r10)
  305. /* Save NVGPRS if they're not saved already */
  306. lwz r4,TRAP(r1)
  307. andi. r4,r4,1
  308. beq 5f
  309. SAVE_NVGPRS(r1)
  310. li r4,0xc00
  311. stw r4,TRAP(r1)
  312. 5:
  313. addi r3,r1,STACK_FRAME_OVERHEAD
  314. bl do_syscall_trace_leave
  315. b ret_from_except_full
  316. #ifdef SHOW_SYSCALLS
  317. do_show_syscall:
  318. #ifdef SHOW_SYSCALLS_TASK
  319. lis r11,show_syscalls_task@ha
  320. lwz r11,show_syscalls_task@l(r11)
  321. cmp 0,r2,r11
  322. bnelr
  323. #endif
  324. stw r31,GPR31(r1)
  325. mflr r31
  326. lis r3,7f@ha
  327. addi r3,r3,7f@l
  328. lwz r4,GPR0(r1)
  329. lwz r5,GPR3(r1)
  330. lwz r6,GPR4(r1)
  331. lwz r7,GPR5(r1)
  332. lwz r8,GPR6(r1)
  333. lwz r9,GPR7(r1)
  334. bl printk
  335. lis r3,77f@ha
  336. addi r3,r3,77f@l
  337. lwz r4,GPR8(r1)
  338. mr r5,r2
  339. bl printk
  340. lwz r0,GPR0(r1)
  341. lwz r3,GPR3(r1)
  342. lwz r4,GPR4(r1)
  343. lwz r5,GPR5(r1)
  344. lwz r6,GPR6(r1)
  345. lwz r7,GPR7(r1)
  346. lwz r8,GPR8(r1)
  347. mtlr r31
  348. lwz r31,GPR31(r1)
  349. blr
  350. do_show_syscall_exit:
  351. #ifdef SHOW_SYSCALLS_TASK
  352. lis r11,show_syscalls_task@ha
  353. lwz r11,show_syscalls_task@l(r11)
  354. cmp 0,r2,r11
  355. bnelr
  356. #endif
  357. stw r31,GPR31(r1)
  358. mflr r31
  359. stw r3,RESULT(r1) /* Save result */
  360. mr r4,r3
  361. lis r3,79f@ha
  362. addi r3,r3,79f@l
  363. bl printk
  364. lwz r3,RESULT(r1)
  365. mtlr r31
  366. lwz r31,GPR31(r1)
  367. blr
  368. 7: .string "syscall %d(%x, %x, %x, %x, %x, "
  369. 77: .string "%x), current=%p\n"
  370. 79: .string " -> %x\n"
  371. .align 2,0
  372. #ifdef SHOW_SYSCALLS_TASK
  373. .data
  374. .globl show_syscalls_task
  375. show_syscalls_task:
  376. .long -1
  377. .text
  378. #endif
  379. #endif /* SHOW_SYSCALLS */
  380. /*
  381. * The fork/clone functions need to copy the full register set into
  382. * the child process. Therefore we need to save all the nonvolatile
  383. * registers (r13 - r31) before calling the C code.
  384. */
  385. .globl ppc_fork
  386. ppc_fork:
  387. SAVE_NVGPRS(r1)
  388. lwz r0,TRAP(r1)
  389. rlwinm r0,r0,0,0,30 /* clear LSB to indicate full */
  390. stw r0,TRAP(r1) /* register set saved */
  391. b sys_fork
  392. .globl ppc_vfork
  393. ppc_vfork:
  394. SAVE_NVGPRS(r1)
  395. lwz r0,TRAP(r1)
  396. rlwinm r0,r0,0,0,30 /* clear LSB to indicate full */
  397. stw r0,TRAP(r1) /* register set saved */
  398. b sys_vfork
  399. .globl ppc_clone
  400. ppc_clone:
  401. SAVE_NVGPRS(r1)
  402. lwz r0,TRAP(r1)
  403. rlwinm r0,r0,0,0,30 /* clear LSB to indicate full */
  404. stw r0,TRAP(r1) /* register set saved */
  405. b sys_clone
  406. .globl ppc_swapcontext
  407. ppc_swapcontext:
  408. SAVE_NVGPRS(r1)
  409. lwz r0,TRAP(r1)
  410. rlwinm r0,r0,0,0,30 /* clear LSB to indicate full */
  411. stw r0,TRAP(r1) /* register set saved */
  412. b sys_swapcontext
  413. /*
  414. * Top-level page fault handling.
  415. * This is in assembler because if do_page_fault tells us that
  416. * it is a bad kernel page fault, we want to save the non-volatile
  417. * registers before calling bad_page_fault.
  418. */
  419. .globl handle_page_fault
  420. handle_page_fault:
  421. stw r4,_DAR(r1)
  422. addi r3,r1,STACK_FRAME_OVERHEAD
  423. bl do_page_fault
  424. cmpwi r3,0
  425. beq+ ret_from_except
  426. SAVE_NVGPRS(r1)
  427. lwz r0,TRAP(r1)
  428. clrrwi r0,r0,1
  429. stw r0,TRAP(r1)
  430. mr r5,r3
  431. addi r3,r1,STACK_FRAME_OVERHEAD
  432. lwz r4,_DAR(r1)
  433. bl bad_page_fault
  434. b ret_from_except_full
  435. /*
  436. * This routine switches between two different tasks. The process
  437. * state of one is saved on its kernel stack. Then the state
  438. * of the other is restored from its kernel stack. The memory
  439. * management hardware is updated to the second process's state.
  440. * Finally, we can return to the second process.
  441. * On entry, r3 points to the THREAD for the current task, r4
  442. * points to the THREAD for the new task.
  443. *
  444. * This routine is always called with interrupts disabled.
  445. *
  446. * Note: there are two ways to get to the "going out" portion
  447. * of this code; either by coming in via the entry (_switch)
  448. * or via "fork" which must set up an environment equivalent
  449. * to the "_switch" path. If you change this , you'll have to
  450. * change the fork code also.
  451. *
  452. * The code which creates the new task context is in 'copy_thread'
  453. * in arch/ppc/kernel/process.c
  454. */
  455. _GLOBAL(_switch)
  456. stwu r1,-INT_FRAME_SIZE(r1)
  457. mflr r0
  458. stw r0,INT_FRAME_SIZE+4(r1)
  459. /* r3-r12 are caller saved -- Cort */
  460. SAVE_NVGPRS(r1)
  461. stw r0,_NIP(r1) /* Return to switch caller */
  462. mfmsr r11
  463. li r0,MSR_FP /* Disable floating-point */
  464. #ifdef CONFIG_ALTIVEC
  465. BEGIN_FTR_SECTION
  466. oris r0,r0,MSR_VEC@h /* Disable altivec */
  467. mfspr r12,SPRN_VRSAVE /* save vrsave register value */
  468. stw r12,THREAD+THREAD_VRSAVE(r2)
  469. END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
  470. #endif /* CONFIG_ALTIVEC */
  471. #ifdef CONFIG_SPE
  472. oris r0,r0,MSR_SPE@h /* Disable SPE */
  473. mfspr r12,SPRN_SPEFSCR /* save spefscr register value */
  474. stw r12,THREAD+THREAD_SPEFSCR(r2)
  475. #endif /* CONFIG_SPE */
  476. and. r0,r0,r11 /* FP or altivec or SPE enabled? */
  477. beq+ 1f
  478. andc r11,r11,r0
  479. MTMSRD(r11)
  480. isync
  481. 1: stw r11,_MSR(r1)
  482. mfcr r10
  483. stw r10,_CCR(r1)
  484. stw r1,KSP(r3) /* Set old stack pointer */
  485. #ifdef CONFIG_SMP
  486. /* We need a sync somewhere here to make sure that if the
  487. * previous task gets rescheduled on another CPU, it sees all
  488. * stores it has performed on this one.
  489. */
  490. sync
  491. #endif /* CONFIG_SMP */
  492. tophys(r0,r4)
  493. CLR_TOP32(r0)
  494. mtspr SPRN_SPRG3,r0 /* Update current THREAD phys addr */
  495. lwz r1,KSP(r4) /* Load new stack pointer */
  496. /* save the old current 'last' for return value */
  497. mr r3,r2
  498. addi r2,r4,-THREAD /* Update current */
  499. #ifdef CONFIG_ALTIVEC
  500. BEGIN_FTR_SECTION
  501. lwz r0,THREAD+THREAD_VRSAVE(r2)
  502. mtspr SPRN_VRSAVE,r0 /* if G4, restore VRSAVE reg */
  503. END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
  504. #endif /* CONFIG_ALTIVEC */
  505. #ifdef CONFIG_SPE
  506. lwz r0,THREAD+THREAD_SPEFSCR(r2)
  507. mtspr SPRN_SPEFSCR,r0 /* restore SPEFSCR reg */
  508. #endif /* CONFIG_SPE */
  509. lwz r0,_CCR(r1)
  510. mtcrf 0xFF,r0
  511. /* r3-r12 are destroyed -- Cort */
  512. REST_NVGPRS(r1)
  513. lwz r4,_NIP(r1) /* Return to _switch caller in new task */
  514. mtlr r4
  515. addi r1,r1,INT_FRAME_SIZE
  516. blr
  517. .globl fast_exception_return
  518. fast_exception_return:
  519. #if !(defined(CONFIG_4xx) || defined(CONFIG_BOOKE))
  520. andi. r10,r9,MSR_RI /* check for recoverable interrupt */
  521. beq 1f /* if not, we've got problems */
  522. #endif
  523. 2: REST_4GPRS(3, r11)
  524. lwz r10,_CCR(r11)
  525. REST_GPR(1, r11)
  526. mtcr r10
  527. lwz r10,_LINK(r11)
  528. mtlr r10
  529. REST_GPR(10, r11)
  530. mtspr SPRN_SRR1,r9
  531. mtspr SPRN_SRR0,r12
  532. REST_GPR(9, r11)
  533. REST_GPR(12, r11)
  534. lwz r11,GPR11(r11)
  535. SYNC
  536. RFI
  537. #if !(defined(CONFIG_4xx) || defined(CONFIG_BOOKE))
  538. /* check if the exception happened in a restartable section */
  539. 1: lis r3,exc_exit_restart_end@ha
  540. addi r3,r3,exc_exit_restart_end@l
  541. cmplw r12,r3
  542. bge 3f
  543. lis r4,exc_exit_restart@ha
  544. addi r4,r4,exc_exit_restart@l
  545. cmplw r12,r4
  546. blt 3f
  547. lis r3,fee_restarts@ha
  548. tophys(r3,r3)
  549. lwz r5,fee_restarts@l(r3)
  550. addi r5,r5,1
  551. stw r5,fee_restarts@l(r3)
  552. mr r12,r4 /* restart at exc_exit_restart */
  553. b 2b
  554. .section .bss
  555. .align 2
  556. fee_restarts:
  557. .space 4
  558. .previous
  559. /* aargh, a nonrecoverable interrupt, panic */
  560. /* aargh, we don't know which trap this is */
  561. /* but the 601 doesn't implement the RI bit, so assume it's OK */
  562. 3:
  563. BEGIN_FTR_SECTION
  564. b 2b
  565. END_FTR_SECTION_IFSET(CPU_FTR_601)
  566. li r10,-1
  567. stw r10,TRAP(r11)
  568. addi r3,r1,STACK_FRAME_OVERHEAD
  569. lis r10,MSR_KERNEL@h
  570. ori r10,r10,MSR_KERNEL@l
  571. bl transfer_to_handler_full
  572. .long nonrecoverable_exception
  573. .long ret_from_except
  574. #endif
  575. .globl ret_from_except_full
  576. ret_from_except_full:
  577. REST_NVGPRS(r1)
  578. /* fall through */
  579. .globl ret_from_except
  580. ret_from_except:
  581. /* Hard-disable interrupts so that current_thread_info()->flags
  582. * can't change between when we test it and when we return
  583. * from the interrupt. */
  584. LOAD_MSR_KERNEL(r10,MSR_KERNEL)
  585. SYNC /* Some chip revs have problems here... */
  586. MTMSRD(r10) /* disable interrupts */
  587. lwz r3,_MSR(r1) /* Returning to user mode? */
  588. andi. r0,r3,MSR_PR
  589. beq resume_kernel
  590. user_exc_return: /* r10 contains MSR_KERNEL here */
  591. /* Check current_thread_info()->flags */
  592. rlwinm r9,r1,0,0,18
  593. lwz r9,TI_FLAGS(r9)
  594. andi. r0,r9,(_TIF_SIGPENDING|_TIF_RESTORE_SIGMASK|_TIF_NEED_RESCHED)
  595. bne do_work
  596. restore_user:
  597. #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
  598. /* Check whether this process has its own DBCR0 value. The single
  599. step bit tells us that dbcr0 should be loaded. */
  600. lwz r0,THREAD+THREAD_DBCR0(r2)
  601. andis. r10,r0,DBCR0_IC@h
  602. bnel- load_dbcr0
  603. #endif
  604. #ifdef CONFIG_PREEMPT
  605. b restore
  606. /* N.B. the only way to get here is from the beq following ret_from_except. */
  607. resume_kernel:
  608. /* check current_thread_info->preempt_count */
  609. rlwinm r9,r1,0,0,18
  610. lwz r0,TI_PREEMPT(r9)
  611. cmpwi 0,r0,0 /* if non-zero, just restore regs and return */
  612. bne restore
  613. lwz r0,TI_FLAGS(r9)
  614. andi. r0,r0,_TIF_NEED_RESCHED
  615. beq+ restore
  616. andi. r0,r3,MSR_EE /* interrupts off? */
  617. beq restore /* don't schedule if so */
  618. 1: bl preempt_schedule_irq
  619. rlwinm r9,r1,0,0,18
  620. lwz r3,TI_FLAGS(r9)
  621. andi. r0,r3,_TIF_NEED_RESCHED
  622. bne- 1b
  623. #else
  624. resume_kernel:
  625. #endif /* CONFIG_PREEMPT */
  626. /* interrupts are hard-disabled at this point */
  627. restore:
  628. lwz r0,GPR0(r1)
  629. lwz r2,GPR2(r1)
  630. REST_4GPRS(3, r1)
  631. REST_2GPRS(7, r1)
  632. lwz r10,_XER(r1)
  633. lwz r11,_CTR(r1)
  634. mtspr SPRN_XER,r10
  635. mtctr r11
  636. PPC405_ERR77(0,r1)
  637. stwcx. r0,0,r1 /* to clear the reservation */
  638. #if !(defined(CONFIG_4xx) || defined(CONFIG_BOOKE))
  639. lwz r9,_MSR(r1)
  640. andi. r10,r9,MSR_RI /* check if this exception occurred */
  641. beql nonrecoverable /* at a bad place (MSR:RI = 0) */
  642. lwz r10,_CCR(r1)
  643. lwz r11,_LINK(r1)
  644. mtcrf 0xFF,r10
  645. mtlr r11
  646. /*
  647. * Once we put values in SRR0 and SRR1, we are in a state
  648. * where exceptions are not recoverable, since taking an
  649. * exception will trash SRR0 and SRR1. Therefore we clear the
  650. * MSR:RI bit to indicate this. If we do take an exception,
  651. * we can't return to the point of the exception but we
  652. * can restart the exception exit path at the label
  653. * exc_exit_restart below. -- paulus
  654. */
  655. LOAD_MSR_KERNEL(r10,MSR_KERNEL & ~MSR_RI)
  656. SYNC
  657. MTMSRD(r10) /* clear the RI bit */
  658. .globl exc_exit_restart
  659. exc_exit_restart:
  660. lwz r9,_MSR(r1)
  661. lwz r12,_NIP(r1)
  662. FIX_SRR1(r9,r10)
  663. mtspr SPRN_SRR0,r12
  664. mtspr SPRN_SRR1,r9
  665. REST_4GPRS(9, r1)
  666. lwz r1,GPR1(r1)
  667. .globl exc_exit_restart_end
  668. exc_exit_restart_end:
  669. SYNC
  670. RFI
  671. #else /* !(CONFIG_4xx || CONFIG_BOOKE) */
  672. /*
  673. * This is a bit different on 4xx/Book-E because it doesn't have
  674. * the RI bit in the MSR.
  675. * The TLB miss handler checks if we have interrupted
  676. * the exception exit path and restarts it if so
  677. * (well maybe one day it will... :).
  678. */
  679. lwz r11,_LINK(r1)
  680. mtlr r11
  681. lwz r10,_CCR(r1)
  682. mtcrf 0xff,r10
  683. REST_2GPRS(9, r1)
  684. .globl exc_exit_restart
  685. exc_exit_restart:
  686. lwz r11,_NIP(r1)
  687. lwz r12,_MSR(r1)
  688. exc_exit_start:
  689. mtspr SPRN_SRR0,r11
  690. mtspr SPRN_SRR1,r12
  691. REST_2GPRS(11, r1)
  692. lwz r1,GPR1(r1)
  693. .globl exc_exit_restart_end
  694. exc_exit_restart_end:
  695. PPC405_ERR77_SYNC
  696. rfi
  697. b . /* prevent prefetch past rfi */
  698. /*
  699. * Returning from a critical interrupt in user mode doesn't need
  700. * to be any different from a normal exception. For a critical
  701. * interrupt in the kernel, we just return (without checking for
  702. * preemption) since the interrupt may have happened at some crucial
  703. * place (e.g. inside the TLB miss handler), and because we will be
  704. * running with r1 pointing into critical_stack, not the current
  705. * process's kernel stack (and therefore current_thread_info() will
  706. * give the wrong answer).
  707. * We have to restore various SPRs that may have been in use at the
  708. * time of the critical interrupt.
  709. *
  710. */
  711. #ifdef CONFIG_40x
  712. #define PPC_40x_TURN_OFF_MSR_DR \
  713. /* avoid any possible TLB misses here by turning off MSR.DR, we \
  714. * assume the instructions here are mapped by a pinned TLB entry */ \
  715. li r10,MSR_IR; \
  716. mtmsr r10; \
  717. isync; \
  718. tophys(r1, r1);
  719. #else
  720. #define PPC_40x_TURN_OFF_MSR_DR
  721. #endif
  722. #define RET_FROM_EXC_LEVEL(exc_lvl_srr0, exc_lvl_srr1, exc_lvl_rfi) \
  723. REST_NVGPRS(r1); \
  724. lwz r3,_MSR(r1); \
  725. andi. r3,r3,MSR_PR; \
  726. LOAD_MSR_KERNEL(r10,MSR_KERNEL); \
  727. bne user_exc_return; \
  728. lwz r0,GPR0(r1); \
  729. lwz r2,GPR2(r1); \
  730. REST_4GPRS(3, r1); \
  731. REST_2GPRS(7, r1); \
  732. lwz r10,_XER(r1); \
  733. lwz r11,_CTR(r1); \
  734. mtspr SPRN_XER,r10; \
  735. mtctr r11; \
  736. PPC405_ERR77(0,r1); \
  737. stwcx. r0,0,r1; /* to clear the reservation */ \
  738. lwz r11,_LINK(r1); \
  739. mtlr r11; \
  740. lwz r10,_CCR(r1); \
  741. mtcrf 0xff,r10; \
  742. PPC_40x_TURN_OFF_MSR_DR; \
  743. lwz r9,_DEAR(r1); \
  744. lwz r10,_ESR(r1); \
  745. mtspr SPRN_DEAR,r9; \
  746. mtspr SPRN_ESR,r10; \
  747. lwz r11,_NIP(r1); \
  748. lwz r12,_MSR(r1); \
  749. mtspr exc_lvl_srr0,r11; \
  750. mtspr exc_lvl_srr1,r12; \
  751. lwz r9,GPR9(r1); \
  752. lwz r12,GPR12(r1); \
  753. lwz r10,GPR10(r1); \
  754. lwz r11,GPR11(r1); \
  755. lwz r1,GPR1(r1); \
  756. PPC405_ERR77_SYNC; \
  757. exc_lvl_rfi; \
  758. b .; /* prevent prefetch past exc_lvl_rfi */
  759. .globl ret_from_crit_exc
  760. ret_from_crit_exc:
  761. RET_FROM_EXC_LEVEL(SPRN_CSRR0, SPRN_CSRR1, RFCI)
  762. #ifdef CONFIG_BOOKE
  763. .globl ret_from_debug_exc
  764. ret_from_debug_exc:
  765. RET_FROM_EXC_LEVEL(SPRN_DSRR0, SPRN_DSRR1, RFDI)
  766. .globl ret_from_mcheck_exc
  767. ret_from_mcheck_exc:
  768. RET_FROM_EXC_LEVEL(SPRN_MCSRR0, SPRN_MCSRR1, RFMCI)
  769. #endif /* CONFIG_BOOKE */
  770. /*
  771. * Load the DBCR0 value for a task that is being ptraced,
  772. * having first saved away the global DBCR0. Note that r0
  773. * has the dbcr0 value to set upon entry to this.
  774. */
  775. load_dbcr0:
  776. mfmsr r10 /* first disable debug exceptions */
  777. rlwinm r10,r10,0,~MSR_DE
  778. mtmsr r10
  779. isync
  780. mfspr r10,SPRN_DBCR0
  781. lis r11,global_dbcr0@ha
  782. addi r11,r11,global_dbcr0@l
  783. stw r10,0(r11)
  784. mtspr SPRN_DBCR0,r0
  785. lwz r10,4(r11)
  786. addi r10,r10,1
  787. stw r10,4(r11)
  788. li r11,-1
  789. mtspr SPRN_DBSR,r11 /* clear all pending debug events */
  790. blr
  791. .section .bss
  792. .align 4
  793. global_dbcr0:
  794. .space 8
  795. .previous
  796. #endif /* !(CONFIG_4xx || CONFIG_BOOKE) */
  797. do_work: /* r10 contains MSR_KERNEL here */
  798. andi. r0,r9,_TIF_NEED_RESCHED
  799. beq do_user_signal
  800. do_resched: /* r10 contains MSR_KERNEL here */
  801. ori r10,r10,MSR_EE
  802. SYNC
  803. MTMSRD(r10) /* hard-enable interrupts */
  804. bl schedule
  805. recheck:
  806. LOAD_MSR_KERNEL(r10,MSR_KERNEL)
  807. SYNC
  808. MTMSRD(r10) /* disable interrupts */
  809. rlwinm r9,r1,0,0,18
  810. lwz r9,TI_FLAGS(r9)
  811. andi. r0,r9,_TIF_NEED_RESCHED
  812. bne- do_resched
  813. andi. r0,r9,_TIF_SIGPENDING
  814. beq restore_user
  815. do_user_signal: /* r10 contains MSR_KERNEL here */
  816. ori r10,r10,MSR_EE
  817. SYNC
  818. MTMSRD(r10) /* hard-enable interrupts */
  819. /* save r13-r31 in the exception frame, if not already done */
  820. lwz r3,TRAP(r1)
  821. andi. r0,r3,1
  822. beq 2f
  823. SAVE_NVGPRS(r1)
  824. rlwinm r3,r3,0,0,30
  825. stw r3,TRAP(r1)
  826. 2: li r3,0
  827. addi r4,r1,STACK_FRAME_OVERHEAD
  828. bl do_signal
  829. REST_NVGPRS(r1)
  830. b recheck
  831. /*
  832. * We come here when we are at the end of handling an exception
  833. * that occurred at a place where taking an exception will lose
  834. * state information, such as the contents of SRR0 and SRR1.
  835. */
  836. nonrecoverable:
  837. lis r10,exc_exit_restart_end@ha
  838. addi r10,r10,exc_exit_restart_end@l
  839. cmplw r12,r10
  840. bge 3f
  841. lis r11,exc_exit_restart@ha
  842. addi r11,r11,exc_exit_restart@l
  843. cmplw r12,r11
  844. blt 3f
  845. lis r10,ee_restarts@ha
  846. lwz r12,ee_restarts@l(r10)
  847. addi r12,r12,1
  848. stw r12,ee_restarts@l(r10)
  849. mr r12,r11 /* restart at exc_exit_restart */
  850. blr
  851. 3: /* OK, we can't recover, kill this process */
  852. /* but the 601 doesn't implement the RI bit, so assume it's OK */
  853. BEGIN_FTR_SECTION
  854. blr
  855. END_FTR_SECTION_IFSET(CPU_FTR_601)
  856. lwz r3,TRAP(r1)
  857. andi. r0,r3,1
  858. beq 4f
  859. SAVE_NVGPRS(r1)
  860. rlwinm r3,r3,0,0,30
  861. stw r3,TRAP(r1)
  862. 4: addi r3,r1,STACK_FRAME_OVERHEAD
  863. bl nonrecoverable_exception
  864. /* shouldn't return */
  865. b 4b
  866. .section .bss
  867. .align 2
  868. ee_restarts:
  869. .space 4
  870. .previous