entry.S 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  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/config.h>
  22. #include <linux/errno.h>
  23. #include <linux/sys.h>
  24. #include <linux/threads.h>
  25. #include <asm/processor.h>
  26. #include <asm/page.h>
  27. #include <asm/mmu.h>
  28. #include <asm/cputable.h>
  29. #include <asm/thread_info.h>
  30. #include <asm/ppc_asm.h>
  31. #include <asm/asm-offsets.h>
  32. #include <asm/unistd.h>
  33. #undef SHOW_SYSCALLS
  34. #undef SHOW_SYSCALLS_TASK
  35. /*
  36. * MSR_KERNEL is > 0x10000 on 4xx/Book-E since it include MSR_CE.
  37. */
  38. #if MSR_KERNEL >= 0x10000
  39. #define LOAD_MSR_KERNEL(r, x) lis r,(x)@h; ori r,r,(x)@l
  40. #else
  41. #define LOAD_MSR_KERNEL(r, x) li r,(x)
  42. #endif
  43. #ifdef CONFIG_BOOKE
  44. #include "head_booke.h"
  45. #define TRANSFER_TO_HANDLER_EXC_LEVEL(exc_level) \
  46. mtspr exc_level##_SPRG,r8; \
  47. BOOKE_LOAD_EXC_LEVEL_STACK(exc_level); \
  48. lwz r0,GPR10-INT_FRAME_SIZE(r8); \
  49. stw r0,GPR10(r11); \
  50. lwz r0,GPR11-INT_FRAME_SIZE(r8); \
  51. stw r0,GPR11(r11); \
  52. mfspr r8,exc_level##_SPRG
  53. .globl mcheck_transfer_to_handler
  54. mcheck_transfer_to_handler:
  55. TRANSFER_TO_HANDLER_EXC_LEVEL(MCHECK)
  56. b transfer_to_handler_full
  57. .globl debug_transfer_to_handler
  58. debug_transfer_to_handler:
  59. TRANSFER_TO_HANDLER_EXC_LEVEL(DEBUG)
  60. b transfer_to_handler_full
  61. .globl crit_transfer_to_handler
  62. crit_transfer_to_handler:
  63. TRANSFER_TO_HANDLER_EXC_LEVEL(CRIT)
  64. /* fall through */
  65. #endif
  66. #ifdef CONFIG_40x
  67. .globl crit_transfer_to_handler
  68. crit_transfer_to_handler:
  69. lwz r0,crit_r10@l(0)
  70. stw r0,GPR10(r11)
  71. lwz r0,crit_r11@l(0)
  72. stw r0,GPR11(r11)
  73. /* fall through */
  74. #endif
  75. /*
  76. * This code finishes saving the registers to the exception frame
  77. * and jumps to the appropriate handler for the exception, turning
  78. * on address translation.
  79. * Note that we rely on the caller having set cr0.eq iff the exception
  80. * occurred in kernel mode (i.e. MSR:PR = 0).
  81. */
  82. .globl transfer_to_handler_full
  83. transfer_to_handler_full:
  84. SAVE_NVGPRS(r11)
  85. /* fall through */
  86. .globl transfer_to_handler
  87. transfer_to_handler:
  88. stw r2,GPR2(r11)
  89. stw r12,_NIP(r11)
  90. stw r9,_MSR(r11)
  91. andi. r2,r9,MSR_PR
  92. mfctr r12
  93. mfspr r2,SPRN_XER
  94. stw r12,_CTR(r11)
  95. stw r2,_XER(r11)
  96. mfspr r12,SPRN_SPRG3
  97. addi r2,r12,-THREAD
  98. tovirt(r2,r2) /* set r2 to current */
  99. beq 2f /* if from user, fix up THREAD.regs */
  100. addi r11,r1,STACK_FRAME_OVERHEAD
  101. stw r11,PT_REGS(r12)
  102. #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
  103. /* Check to see if the dbcr0 register is set up to debug. Use the
  104. single-step bit to do this. */
  105. lwz r12,THREAD_DBCR0(r12)
  106. andis. r12,r12,DBCR0_IC@h
  107. beq+ 3f
  108. /* From user and task is ptraced - load up global dbcr0 */
  109. li r12,-1 /* clear all pending debug events */
  110. mtspr SPRN_DBSR,r12
  111. lis r11,global_dbcr0@ha
  112. tophys(r11,r11)
  113. addi r11,r11,global_dbcr0@l
  114. lwz r12,0(r11)
  115. mtspr SPRN_DBCR0,r12
  116. lwz r12,4(r11)
  117. addi r12,r12,-1
  118. stw r12,4(r11)
  119. #endif
  120. b 3f
  121. 2: /* if from kernel, check interrupted DOZE/NAP mode and
  122. * check for stack overflow
  123. */
  124. #ifdef CONFIG_6xx
  125. mfspr r11,SPRN_HID0
  126. mtcr r11
  127. BEGIN_FTR_SECTION
  128. bt- 8,4f /* Check DOZE */
  129. END_FTR_SECTION_IFSET(CPU_FTR_CAN_DOZE)
  130. BEGIN_FTR_SECTION
  131. bt- 9,4f /* Check NAP */
  132. END_FTR_SECTION_IFSET(CPU_FTR_CAN_NAP)
  133. #endif /* CONFIG_6xx */
  134. .globl transfer_to_handler_cont
  135. transfer_to_handler_cont:
  136. lwz r11,THREAD_INFO-THREAD(r12)
  137. cmplw r1,r11 /* if r1 <= current->thread_info */
  138. ble- stack_ovf /* then the kernel stack overflowed */
  139. 3:
  140. mflr r9
  141. lwz r11,0(r9) /* virtual address of handler */
  142. lwz r9,4(r9) /* where to go when done */
  143. FIX_SRR1(r10,r12)
  144. mtspr SPRN_SRR0,r11
  145. mtspr SPRN_SRR1,r10
  146. mtlr r9
  147. SYNC
  148. RFI /* jump to handler, enable MMU */
  149. #ifdef CONFIG_6xx
  150. 4: b power_save_6xx_restore
  151. #endif
  152. /*
  153. * On kernel stack overflow, load up an initial stack pointer
  154. * and call StackOverflow(regs), which should not return.
  155. */
  156. stack_ovf:
  157. /* sometimes we use a statically-allocated stack, which is OK. */
  158. lis r11,_end@h
  159. ori r11,r11,_end@l
  160. cmplw r1,r11
  161. ble 3b /* r1 <= &_end is OK */
  162. SAVE_NVGPRS(r11)
  163. addi r3,r1,STACK_FRAME_OVERHEAD
  164. lis r1,init_thread_union@ha
  165. addi r1,r1,init_thread_union@l
  166. addi r1,r1,THREAD_SIZE-STACK_FRAME_OVERHEAD
  167. lis r9,StackOverflow@ha
  168. addi r9,r9,StackOverflow@l
  169. LOAD_MSR_KERNEL(r10,MSR_KERNEL)
  170. FIX_SRR1(r10,r12)
  171. mtspr SPRN_SRR0,r9
  172. mtspr SPRN_SRR1,r10
  173. SYNC
  174. RFI
  175. /*
  176. * Handle a system call.
  177. */
  178. .stabs "arch/ppc/kernel/",N_SO,0,0,0f
  179. .stabs "entry.S",N_SO,0,0,0f
  180. 0:
  181. _GLOBAL(DoSyscall)
  182. stw r0,THREAD+LAST_SYSCALL(r2)
  183. stw r3,ORIG_GPR3(r1)
  184. li r12,0
  185. stw r12,RESULT(r1)
  186. lwz r11,_CCR(r1) /* Clear SO bit in CR */
  187. rlwinm r11,r11,0,4,2
  188. stw r11,_CCR(r1)
  189. #ifdef SHOW_SYSCALLS
  190. bl do_show_syscall
  191. #endif /* SHOW_SYSCALLS */
  192. rlwinm r10,r1,0,0,18 /* current_thread_info() */
  193. lwz r11,TI_FLAGS(r10)
  194. andi. r11,r11,_TIF_SYSCALL_T_OR_A
  195. bne- syscall_dotrace
  196. syscall_dotrace_cont:
  197. cmplwi 0,r0,NR_syscalls
  198. lis r10,sys_call_table@h
  199. ori r10,r10,sys_call_table@l
  200. slwi r0,r0,2
  201. bge- 66f
  202. lwzx r10,r10,r0 /* Fetch system call handler [ptr] */
  203. mtlr r10
  204. addi r9,r1,STACK_FRAME_OVERHEAD
  205. PPC440EP_ERR42
  206. blrl /* Call handler */
  207. .globl ret_from_syscall
  208. ret_from_syscall:
  209. #ifdef SHOW_SYSCALLS
  210. bl do_show_syscall_exit
  211. #endif
  212. mr r6,r3
  213. rlwinm r12,r1,0,0,18 /* current_thread_info() */
  214. /* disable interrupts so current_thread_info()->flags can't change */
  215. LOAD_MSR_KERNEL(r10,MSR_KERNEL) /* doesn't include MSR_EE */
  216. SYNC
  217. MTMSRD(r10)
  218. lwz r9,TI_FLAGS(r12)
  219. li r8,-_LAST_ERRNO
  220. andi. r0,r9,(_TIF_SYSCALL_T_OR_A|_TIF_SINGLESTEP|_TIF_USER_WORK_MASK|_TIF_PERSYSCALL_MASK)
  221. bne- syscall_exit_work
  222. cmplw 0,r3,r8
  223. blt+ syscall_exit_cont
  224. lwz r11,_CCR(r1) /* Load CR */
  225. neg r3,r3
  226. oris r11,r11,0x1000 /* Set SO bit in CR */
  227. stw r11,_CCR(r1)
  228. syscall_exit_cont:
  229. #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
  230. /* If the process has its own DBCR0 value, load it up. The single
  231. step bit tells us that dbcr0 should be loaded. */
  232. lwz r0,THREAD+THREAD_DBCR0(r2)
  233. andis. r10,r0,DBCR0_IC@h
  234. bnel- load_dbcr0
  235. #endif
  236. stwcx. r0,0,r1 /* to clear the reservation */
  237. lwz r4,_LINK(r1)
  238. lwz r5,_CCR(r1)
  239. mtlr r4
  240. mtcr r5
  241. lwz r7,_NIP(r1)
  242. lwz r8,_MSR(r1)
  243. FIX_SRR1(r8, r0)
  244. lwz r2,GPR2(r1)
  245. lwz r1,GPR1(r1)
  246. mtspr SPRN_SRR0,r7
  247. mtspr SPRN_SRR1,r8
  248. SYNC
  249. RFI
  250. 66: li r3,-ENOSYS
  251. b ret_from_syscall
  252. .globl ret_from_fork
  253. ret_from_fork:
  254. REST_NVGPRS(r1)
  255. bl schedule_tail
  256. li r3,0
  257. b ret_from_syscall
  258. /* Traced system call support */
  259. syscall_dotrace:
  260. SAVE_NVGPRS(r1)
  261. li r0,0xc00
  262. stw r0,TRAP(r1)
  263. addi r3,r1,STACK_FRAME_OVERHEAD
  264. bl do_syscall_trace_enter
  265. lwz r0,GPR0(r1) /* Restore original registers */
  266. lwz r3,GPR3(r1)
  267. lwz r4,GPR4(r1)
  268. lwz r5,GPR5(r1)
  269. lwz r6,GPR6(r1)
  270. lwz r7,GPR7(r1)
  271. lwz r8,GPR8(r1)
  272. REST_NVGPRS(r1)
  273. b syscall_dotrace_cont
  274. syscall_exit_work:
  275. andi. r0,r9,_TIF_RESTOREALL
  276. beq+ 0f
  277. REST_NVGPRS(r1)
  278. b 2f
  279. 0: cmplw 0,r3,r8
  280. blt+ 1f
  281. andi. r0,r9,_TIF_NOERROR
  282. bne- 1f
  283. lwz r11,_CCR(r1) /* Load CR */
  284. neg r3,r3
  285. oris r11,r11,0x1000 /* Set SO bit in CR */
  286. stw r11,_CCR(r1)
  287. 1: stw r6,RESULT(r1) /* Save result */
  288. stw r3,GPR3(r1) /* Update return value */
  289. 2: andi. r0,r9,(_TIF_PERSYSCALL_MASK)
  290. beq 4f
  291. /* Clear per-syscall TIF flags if any are set. */
  292. li r11,_TIF_PERSYSCALL_MASK
  293. addi r12,r12,TI_FLAGS
  294. 3: lwarx r8,0,r12
  295. andc r8,r8,r11
  296. #ifdef CONFIG_IBM405_ERR77
  297. dcbt 0,r12
  298. #endif
  299. stwcx. r8,0,r12
  300. bne- 3b
  301. subi r12,r12,TI_FLAGS
  302. 4: /* Anything which requires enabling interrupts? */
  303. andi. r0,r9,(_TIF_SYSCALL_T_OR_A|_TIF_SINGLESTEP)
  304. beq ret_from_except
  305. /* Re-enable interrupts */
  306. ori r10,r10,MSR_EE
  307. SYNC
  308. MTMSRD(r10)
  309. /* Save NVGPRS if they're not saved already */
  310. lwz r4,TRAP(r1)
  311. andi. r4,r4,1
  312. beq 5f
  313. SAVE_NVGPRS(r1)
  314. li r4,0xc00
  315. stw r4,TRAP(r1)
  316. 5:
  317. addi r3,r1,STACK_FRAME_OVERHEAD
  318. bl do_syscall_trace_leave
  319. b ret_from_except_full
  320. #ifdef SHOW_SYSCALLS
  321. do_show_syscall:
  322. #ifdef SHOW_SYSCALLS_TASK
  323. lis r11,show_syscalls_task@ha
  324. lwz r11,show_syscalls_task@l(r11)
  325. cmp 0,r2,r11
  326. bnelr
  327. #endif
  328. stw r31,GPR31(r1)
  329. mflr r31
  330. lis r3,7f@ha
  331. addi r3,r3,7f@l
  332. lwz r4,GPR0(r1)
  333. lwz r5,GPR3(r1)
  334. lwz r6,GPR4(r1)
  335. lwz r7,GPR5(r1)
  336. lwz r8,GPR6(r1)
  337. lwz r9,GPR7(r1)
  338. bl printk
  339. lis r3,77f@ha
  340. addi r3,r3,77f@l
  341. lwz r4,GPR8(r1)
  342. mr r5,r2
  343. bl printk
  344. lwz r0,GPR0(r1)
  345. lwz r3,GPR3(r1)
  346. lwz r4,GPR4(r1)
  347. lwz r5,GPR5(r1)
  348. lwz r6,GPR6(r1)
  349. lwz r7,GPR7(r1)
  350. lwz r8,GPR8(r1)
  351. mtlr r31
  352. lwz r31,GPR31(r1)
  353. blr
  354. do_show_syscall_exit:
  355. #ifdef SHOW_SYSCALLS_TASK
  356. lis r11,show_syscalls_task@ha
  357. lwz r11,show_syscalls_task@l(r11)
  358. cmp 0,r2,r11
  359. bnelr
  360. #endif
  361. stw r31,GPR31(r1)
  362. mflr r31
  363. stw r3,RESULT(r1) /* Save result */
  364. mr r4,r3
  365. lis r3,79f@ha
  366. addi r3,r3,79f@l
  367. bl printk
  368. lwz r3,RESULT(r1)
  369. mtlr r31
  370. lwz r31,GPR31(r1)
  371. blr
  372. 7: .string "syscall %d(%x, %x, %x, %x, %x, "
  373. 77: .string "%x), current=%p\n"
  374. 79: .string " -> %x\n"
  375. .align 2,0
  376. #ifdef SHOW_SYSCALLS_TASK
  377. .data
  378. .globl show_syscalls_task
  379. show_syscalls_task:
  380. .long -1
  381. .text
  382. #endif
  383. #endif /* SHOW_SYSCALLS */
  384. /*
  385. * The fork/clone functions need to copy the full register set into
  386. * the child process. Therefore we need to save all the nonvolatile
  387. * registers (r13 - r31) before calling the C code.
  388. */
  389. .globl ppc_fork
  390. ppc_fork:
  391. SAVE_NVGPRS(r1)
  392. lwz r0,TRAP(r1)
  393. rlwinm r0,r0,0,0,30 /* clear LSB to indicate full */
  394. stw r0,TRAP(r1) /* register set saved */
  395. b sys_fork
  396. .globl ppc_vfork
  397. ppc_vfork:
  398. SAVE_NVGPRS(r1)
  399. lwz r0,TRAP(r1)
  400. rlwinm r0,r0,0,0,30 /* clear LSB to indicate full */
  401. stw r0,TRAP(r1) /* register set saved */
  402. b sys_vfork
  403. .globl ppc_clone
  404. ppc_clone:
  405. SAVE_NVGPRS(r1)
  406. lwz r0,TRAP(r1)
  407. rlwinm r0,r0,0,0,30 /* clear LSB to indicate full */
  408. stw r0,TRAP(r1) /* register set saved */
  409. b sys_clone
  410. .globl ppc_swapcontext
  411. ppc_swapcontext:
  412. SAVE_NVGPRS(r1)
  413. lwz r0,TRAP(r1)
  414. rlwinm r0,r0,0,0,30 /* clear LSB to indicate full */
  415. stw r0,TRAP(r1) /* register set saved */
  416. b sys_swapcontext
  417. /*
  418. * Top-level page fault handling.
  419. * This is in assembler because if do_page_fault tells us that
  420. * it is a bad kernel page fault, we want to save the non-volatile
  421. * registers before calling bad_page_fault.
  422. */
  423. .globl handle_page_fault
  424. handle_page_fault:
  425. stw r4,_DAR(r1)
  426. addi r3,r1,STACK_FRAME_OVERHEAD
  427. bl do_page_fault
  428. cmpwi r3,0
  429. beq+ ret_from_except
  430. SAVE_NVGPRS(r1)
  431. lwz r0,TRAP(r1)
  432. clrrwi r0,r0,1
  433. stw r0,TRAP(r1)
  434. mr r5,r3
  435. addi r3,r1,STACK_FRAME_OVERHEAD
  436. lwz r4,_DAR(r1)
  437. bl bad_page_fault
  438. b ret_from_except_full
  439. /*
  440. * This routine switches between two different tasks. The process
  441. * state of one is saved on its kernel stack. Then the state
  442. * of the other is restored from its kernel stack. The memory
  443. * management hardware is updated to the second process's state.
  444. * Finally, we can return to the second process.
  445. * On entry, r3 points to the THREAD for the current task, r4
  446. * points to the THREAD for the new task.
  447. *
  448. * This routine is always called with interrupts disabled.
  449. *
  450. * Note: there are two ways to get to the "going out" portion
  451. * of this code; either by coming in via the entry (_switch)
  452. * or via "fork" which must set up an environment equivalent
  453. * to the "_switch" path. If you change this , you'll have to
  454. * change the fork code also.
  455. *
  456. * The code which creates the new task context is in 'copy_thread'
  457. * in arch/ppc/kernel/process.c
  458. */
  459. _GLOBAL(_switch)
  460. stwu r1,-INT_FRAME_SIZE(r1)
  461. mflr r0
  462. stw r0,INT_FRAME_SIZE+4(r1)
  463. /* r3-r12 are caller saved -- Cort */
  464. SAVE_NVGPRS(r1)
  465. stw r0,_NIP(r1) /* Return to switch caller */
  466. mfmsr r11
  467. li r0,MSR_FP /* Disable floating-point */
  468. #ifdef CONFIG_ALTIVEC
  469. BEGIN_FTR_SECTION
  470. oris r0,r0,MSR_VEC@h /* Disable altivec */
  471. mfspr r12,SPRN_VRSAVE /* save vrsave register value */
  472. stw r12,THREAD+THREAD_VRSAVE(r2)
  473. END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
  474. #endif /* CONFIG_ALTIVEC */
  475. #ifdef CONFIG_SPE
  476. oris r0,r0,MSR_SPE@h /* Disable SPE */
  477. mfspr r12,SPRN_SPEFSCR /* save spefscr register value */
  478. stw r12,THREAD+THREAD_SPEFSCR(r2)
  479. #endif /* CONFIG_SPE */
  480. and. r0,r0,r11 /* FP or altivec or SPE enabled? */
  481. beq+ 1f
  482. andc r11,r11,r0
  483. MTMSRD(r11)
  484. isync
  485. 1: stw r11,_MSR(r1)
  486. mfcr r10
  487. stw r10,_CCR(r1)
  488. stw r1,KSP(r3) /* Set old stack pointer */
  489. #ifdef CONFIG_SMP
  490. /* We need a sync somewhere here to make sure that if the
  491. * previous task gets rescheduled on another CPU, it sees all
  492. * stores it has performed on this one.
  493. */
  494. sync
  495. #endif /* CONFIG_SMP */
  496. tophys(r0,r4)
  497. CLR_TOP32(r0)
  498. mtspr SPRN_SPRG3,r0 /* Update current THREAD phys addr */
  499. lwz r1,KSP(r4) /* Load new stack pointer */
  500. /* save the old current 'last' for return value */
  501. mr r3,r2
  502. addi r2,r4,-THREAD /* Update current */
  503. #ifdef CONFIG_ALTIVEC
  504. BEGIN_FTR_SECTION
  505. lwz r0,THREAD+THREAD_VRSAVE(r2)
  506. mtspr SPRN_VRSAVE,r0 /* if G4, restore VRSAVE reg */
  507. END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
  508. #endif /* CONFIG_ALTIVEC */
  509. #ifdef CONFIG_SPE
  510. lwz r0,THREAD+THREAD_SPEFSCR(r2)
  511. mtspr SPRN_SPEFSCR,r0 /* restore SPEFSCR reg */
  512. #endif /* CONFIG_SPE */
  513. lwz r0,_CCR(r1)
  514. mtcrf 0xFF,r0
  515. /* r3-r12 are destroyed -- Cort */
  516. REST_NVGPRS(r1)
  517. lwz r4,_NIP(r1) /* Return to _switch caller in new task */
  518. mtlr r4
  519. addi r1,r1,INT_FRAME_SIZE
  520. blr
  521. .globl fast_exception_return
  522. fast_exception_return:
  523. #if !(defined(CONFIG_4xx) || defined(CONFIG_BOOKE))
  524. andi. r10,r9,MSR_RI /* check for recoverable interrupt */
  525. beq 1f /* if not, we've got problems */
  526. #endif
  527. 2: REST_4GPRS(3, r11)
  528. lwz r10,_CCR(r11)
  529. REST_GPR(1, r11)
  530. mtcr r10
  531. lwz r10,_LINK(r11)
  532. mtlr r10
  533. REST_GPR(10, r11)
  534. mtspr SPRN_SRR1,r9
  535. mtspr SPRN_SRR0,r12
  536. REST_GPR(9, r11)
  537. REST_GPR(12, r11)
  538. lwz r11,GPR11(r11)
  539. SYNC
  540. RFI
  541. #if !(defined(CONFIG_4xx) || defined(CONFIG_BOOKE))
  542. /* check if the exception happened in a restartable section */
  543. 1: lis r3,exc_exit_restart_end@ha
  544. addi r3,r3,exc_exit_restart_end@l
  545. cmplw r12,r3
  546. bge 3f
  547. lis r4,exc_exit_restart@ha
  548. addi r4,r4,exc_exit_restart@l
  549. cmplw r12,r4
  550. blt 3f
  551. lis r3,fee_restarts@ha
  552. tophys(r3,r3)
  553. lwz r5,fee_restarts@l(r3)
  554. addi r5,r5,1
  555. stw r5,fee_restarts@l(r3)
  556. mr r12,r4 /* restart at exc_exit_restart */
  557. b 2b
  558. .comm fee_restarts,4
  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. .comm global_dbcr0,8
  792. #endif /* !(CONFIG_4xx || CONFIG_BOOKE) */
  793. do_work: /* r10 contains MSR_KERNEL here */
  794. andi. r0,r9,_TIF_NEED_RESCHED
  795. beq do_user_signal
  796. do_resched: /* r10 contains MSR_KERNEL here */
  797. ori r10,r10,MSR_EE
  798. SYNC
  799. MTMSRD(r10) /* hard-enable interrupts */
  800. bl schedule
  801. recheck:
  802. LOAD_MSR_KERNEL(r10,MSR_KERNEL)
  803. SYNC
  804. MTMSRD(r10) /* disable interrupts */
  805. rlwinm r9,r1,0,0,18
  806. lwz r9,TI_FLAGS(r9)
  807. andi. r0,r9,_TIF_NEED_RESCHED
  808. bne- do_resched
  809. andi. r0,r9,_TIF_SIGPENDING
  810. beq restore_user
  811. do_user_signal: /* r10 contains MSR_KERNEL here */
  812. ori r10,r10,MSR_EE
  813. SYNC
  814. MTMSRD(r10) /* hard-enable interrupts */
  815. /* save r13-r31 in the exception frame, if not already done */
  816. lwz r3,TRAP(r1)
  817. andi. r0,r3,1
  818. beq 2f
  819. SAVE_NVGPRS(r1)
  820. rlwinm r3,r3,0,0,30
  821. stw r3,TRAP(r1)
  822. 2: li r3,0
  823. addi r4,r1,STACK_FRAME_OVERHEAD
  824. bl do_signal
  825. REST_NVGPRS(r1)
  826. b recheck
  827. /*
  828. * We come here when we are at the end of handling an exception
  829. * that occurred at a place where taking an exception will lose
  830. * state information, such as the contents of SRR0 and SRR1.
  831. */
  832. nonrecoverable:
  833. lis r10,exc_exit_restart_end@ha
  834. addi r10,r10,exc_exit_restart_end@l
  835. cmplw r12,r10
  836. bge 3f
  837. lis r11,exc_exit_restart@ha
  838. addi r11,r11,exc_exit_restart@l
  839. cmplw r12,r11
  840. blt 3f
  841. lis r10,ee_restarts@ha
  842. lwz r12,ee_restarts@l(r10)
  843. addi r12,r12,1
  844. stw r12,ee_restarts@l(r10)
  845. mr r12,r11 /* restart at exc_exit_restart */
  846. blr
  847. 3: /* OK, we can't recover, kill this process */
  848. /* but the 601 doesn't implement the RI bit, so assume it's OK */
  849. BEGIN_FTR_SECTION
  850. blr
  851. END_FTR_SECTION_IFSET(CPU_FTR_601)
  852. lwz r3,TRAP(r1)
  853. andi. r0,r3,1
  854. beq 4f
  855. SAVE_NVGPRS(r1)
  856. rlwinm r3,r3,0,0,30
  857. stw r3,TRAP(r1)
  858. 4: addi r3,r1,STACK_FRAME_OVERHEAD
  859. bl nonrecoverable_exception
  860. /* shouldn't return */
  861. b 4b
  862. .comm ee_restarts,4