entry_32.S 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  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/reg.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/powerpc/kernel/",N_SO,0,0,0f
  176. .stabs "entry_32.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,(31-THREAD_SHIFT) /* 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,(31-THREAD_SHIFT) /* 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. BEGIN_FTR_SECTION
  473. oris r0,r0,MSR_SPE@h /* Disable SPE */
  474. mfspr r12,SPRN_SPEFSCR /* save spefscr register value */
  475. stw r12,THREAD+THREAD_SPEFSCR(r2)
  476. END_FTR_SECTION_IFSET(CPU_FTR_SPE)
  477. #endif /* CONFIG_SPE */
  478. and. r0,r0,r11 /* FP or altivec or SPE enabled? */
  479. beq+ 1f
  480. andc r11,r11,r0
  481. MTMSRD(r11)
  482. isync
  483. 1: stw r11,_MSR(r1)
  484. mfcr r10
  485. stw r10,_CCR(r1)
  486. stw r1,KSP(r3) /* Set old stack pointer */
  487. #ifdef CONFIG_SMP
  488. /* We need a sync somewhere here to make sure that if the
  489. * previous task gets rescheduled on another CPU, it sees all
  490. * stores it has performed on this one.
  491. */
  492. sync
  493. #endif /* CONFIG_SMP */
  494. tophys(r0,r4)
  495. CLR_TOP32(r0)
  496. mtspr SPRN_SPRG3,r0 /* Update current THREAD phys addr */
  497. lwz r1,KSP(r4) /* Load new stack pointer */
  498. /* save the old current 'last' for return value */
  499. mr r3,r2
  500. addi r2,r4,-THREAD /* Update current */
  501. #ifdef CONFIG_ALTIVEC
  502. BEGIN_FTR_SECTION
  503. lwz r0,THREAD+THREAD_VRSAVE(r2)
  504. mtspr SPRN_VRSAVE,r0 /* if G4, restore VRSAVE reg */
  505. END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
  506. #endif /* CONFIG_ALTIVEC */
  507. #ifdef CONFIG_SPE
  508. BEGIN_FTR_SECTION
  509. lwz r0,THREAD+THREAD_SPEFSCR(r2)
  510. mtspr SPRN_SPEFSCR,r0 /* restore SPEFSCR reg */
  511. END_FTR_SECTION_IFSET(CPU_FTR_SPE)
  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. .section .bss
  559. .align 2
  560. fee_restarts:
  561. .space 4
  562. .previous
  563. /* aargh, a nonrecoverable interrupt, panic */
  564. /* aargh, we don't know which trap this is */
  565. /* but the 601 doesn't implement the RI bit, so assume it's OK */
  566. 3:
  567. BEGIN_FTR_SECTION
  568. b 2b
  569. END_FTR_SECTION_IFSET(CPU_FTR_601)
  570. li r10,-1
  571. stw r10,_TRAP(r11)
  572. addi r3,r1,STACK_FRAME_OVERHEAD
  573. lis r10,MSR_KERNEL@h
  574. ori r10,r10,MSR_KERNEL@l
  575. bl transfer_to_handler_full
  576. .long nonrecoverable_exception
  577. .long ret_from_except
  578. #endif
  579. .globl ret_from_except_full
  580. ret_from_except_full:
  581. REST_NVGPRS(r1)
  582. /* fall through */
  583. .globl ret_from_except
  584. ret_from_except:
  585. /* Hard-disable interrupts so that current_thread_info()->flags
  586. * can't change between when we test it and when we return
  587. * from the interrupt. */
  588. LOAD_MSR_KERNEL(r10,MSR_KERNEL)
  589. SYNC /* Some chip revs have problems here... */
  590. MTMSRD(r10) /* disable interrupts */
  591. lwz r3,_MSR(r1) /* Returning to user mode? */
  592. andi. r0,r3,MSR_PR
  593. beq resume_kernel
  594. user_exc_return: /* r10 contains MSR_KERNEL here */
  595. /* Check current_thread_info()->flags */
  596. rlwinm r9,r1,0,0,(31-THREAD_SHIFT)
  597. lwz r9,TI_FLAGS(r9)
  598. andi. r0,r9,(_TIF_SIGPENDING|_TIF_RESTORE_SIGMASK|_TIF_NEED_RESCHED)
  599. bne do_work
  600. restore_user:
  601. #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
  602. /* Check whether this process has its own DBCR0 value. The single
  603. step bit tells us that dbcr0 should be loaded. */
  604. lwz r0,THREAD+THREAD_DBCR0(r2)
  605. andis. r10,r0,DBCR0_IC@h
  606. bnel- load_dbcr0
  607. #endif
  608. #ifdef CONFIG_PREEMPT
  609. b restore
  610. /* N.B. the only way to get here is from the beq following ret_from_except. */
  611. resume_kernel:
  612. /* check current_thread_info->preempt_count */
  613. rlwinm r9,r1,0,0,(31-THREAD_SHIFT)
  614. lwz r0,TI_PREEMPT(r9)
  615. cmpwi 0,r0,0 /* if non-zero, just restore regs and return */
  616. bne restore
  617. lwz r0,TI_FLAGS(r9)
  618. andi. r0,r0,_TIF_NEED_RESCHED
  619. beq+ restore
  620. andi. r0,r3,MSR_EE /* interrupts off? */
  621. beq restore /* don't schedule if so */
  622. 1: bl preempt_schedule_irq
  623. rlwinm r9,r1,0,0,(31-THREAD_SHIFT)
  624. lwz r3,TI_FLAGS(r9)
  625. andi. r0,r3,_TIF_NEED_RESCHED
  626. bne- 1b
  627. #else
  628. resume_kernel:
  629. #endif /* CONFIG_PREEMPT */
  630. /* interrupts are hard-disabled at this point */
  631. restore:
  632. lwz r0,GPR0(r1)
  633. lwz r2,GPR2(r1)
  634. REST_4GPRS(3, r1)
  635. REST_2GPRS(7, r1)
  636. lwz r10,_XER(r1)
  637. lwz r11,_CTR(r1)
  638. mtspr SPRN_XER,r10
  639. mtctr r11
  640. PPC405_ERR77(0,r1)
  641. stwcx. r0,0,r1 /* to clear the reservation */
  642. #if !(defined(CONFIG_4xx) || defined(CONFIG_BOOKE))
  643. lwz r9,_MSR(r1)
  644. andi. r10,r9,MSR_RI /* check if this exception occurred */
  645. beql nonrecoverable /* at a bad place (MSR:RI = 0) */
  646. lwz r10,_CCR(r1)
  647. lwz r11,_LINK(r1)
  648. mtcrf 0xFF,r10
  649. mtlr r11
  650. /*
  651. * Once we put values in SRR0 and SRR1, we are in a state
  652. * where exceptions are not recoverable, since taking an
  653. * exception will trash SRR0 and SRR1. Therefore we clear the
  654. * MSR:RI bit to indicate this. If we do take an exception,
  655. * we can't return to the point of the exception but we
  656. * can restart the exception exit path at the label
  657. * exc_exit_restart below. -- paulus
  658. */
  659. LOAD_MSR_KERNEL(r10,MSR_KERNEL & ~MSR_RI)
  660. SYNC
  661. MTMSRD(r10) /* clear the RI bit */
  662. .globl exc_exit_restart
  663. exc_exit_restart:
  664. lwz r9,_MSR(r1)
  665. lwz r12,_NIP(r1)
  666. FIX_SRR1(r9,r10)
  667. mtspr SPRN_SRR0,r12
  668. mtspr SPRN_SRR1,r9
  669. REST_4GPRS(9, r1)
  670. lwz r1,GPR1(r1)
  671. .globl exc_exit_restart_end
  672. exc_exit_restart_end:
  673. SYNC
  674. RFI
  675. #else /* !(CONFIG_4xx || CONFIG_BOOKE) */
  676. /*
  677. * This is a bit different on 4xx/Book-E because it doesn't have
  678. * the RI bit in the MSR.
  679. * The TLB miss handler checks if we have interrupted
  680. * the exception exit path and restarts it if so
  681. * (well maybe one day it will... :).
  682. */
  683. lwz r11,_LINK(r1)
  684. mtlr r11
  685. lwz r10,_CCR(r1)
  686. mtcrf 0xff,r10
  687. REST_2GPRS(9, r1)
  688. .globl exc_exit_restart
  689. exc_exit_restart:
  690. lwz r11,_NIP(r1)
  691. lwz r12,_MSR(r1)
  692. exc_exit_start:
  693. mtspr SPRN_SRR0,r11
  694. mtspr SPRN_SRR1,r12
  695. REST_2GPRS(11, r1)
  696. lwz r1,GPR1(r1)
  697. .globl exc_exit_restart_end
  698. exc_exit_restart_end:
  699. PPC405_ERR77_SYNC
  700. rfi
  701. b . /* prevent prefetch past rfi */
  702. /*
  703. * Returning from a critical interrupt in user mode doesn't need
  704. * to be any different from a normal exception. For a critical
  705. * interrupt in the kernel, we just return (without checking for
  706. * preemption) since the interrupt may have happened at some crucial
  707. * place (e.g. inside the TLB miss handler), and because we will be
  708. * running with r1 pointing into critical_stack, not the current
  709. * process's kernel stack (and therefore current_thread_info() will
  710. * give the wrong answer).
  711. * We have to restore various SPRs that may have been in use at the
  712. * time of the critical interrupt.
  713. *
  714. */
  715. #ifdef CONFIG_40x
  716. #define PPC_40x_TURN_OFF_MSR_DR \
  717. /* avoid any possible TLB misses here by turning off MSR.DR, we \
  718. * assume the instructions here are mapped by a pinned TLB entry */ \
  719. li r10,MSR_IR; \
  720. mtmsr r10; \
  721. isync; \
  722. tophys(r1, r1);
  723. #else
  724. #define PPC_40x_TURN_OFF_MSR_DR
  725. #endif
  726. #define RET_FROM_EXC_LEVEL(exc_lvl_srr0, exc_lvl_srr1, exc_lvl_rfi) \
  727. REST_NVGPRS(r1); \
  728. lwz r3,_MSR(r1); \
  729. andi. r3,r3,MSR_PR; \
  730. LOAD_MSR_KERNEL(r10,MSR_KERNEL); \
  731. bne user_exc_return; \
  732. lwz r0,GPR0(r1); \
  733. lwz r2,GPR2(r1); \
  734. REST_4GPRS(3, r1); \
  735. REST_2GPRS(7, r1); \
  736. lwz r10,_XER(r1); \
  737. lwz r11,_CTR(r1); \
  738. mtspr SPRN_XER,r10; \
  739. mtctr r11; \
  740. PPC405_ERR77(0,r1); \
  741. stwcx. r0,0,r1; /* to clear the reservation */ \
  742. lwz r11,_LINK(r1); \
  743. mtlr r11; \
  744. lwz r10,_CCR(r1); \
  745. mtcrf 0xff,r10; \
  746. PPC_40x_TURN_OFF_MSR_DR; \
  747. lwz r9,_DEAR(r1); \
  748. lwz r10,_ESR(r1); \
  749. mtspr SPRN_DEAR,r9; \
  750. mtspr SPRN_ESR,r10; \
  751. lwz r11,_NIP(r1); \
  752. lwz r12,_MSR(r1); \
  753. mtspr exc_lvl_srr0,r11; \
  754. mtspr exc_lvl_srr1,r12; \
  755. lwz r9,GPR9(r1); \
  756. lwz r12,GPR12(r1); \
  757. lwz r10,GPR10(r1); \
  758. lwz r11,GPR11(r1); \
  759. lwz r1,GPR1(r1); \
  760. PPC405_ERR77_SYNC; \
  761. exc_lvl_rfi; \
  762. b .; /* prevent prefetch past exc_lvl_rfi */
  763. .globl ret_from_crit_exc
  764. ret_from_crit_exc:
  765. RET_FROM_EXC_LEVEL(SPRN_CSRR0, SPRN_CSRR1, RFCI)
  766. #ifdef CONFIG_BOOKE
  767. .globl ret_from_debug_exc
  768. ret_from_debug_exc:
  769. RET_FROM_EXC_LEVEL(SPRN_DSRR0, SPRN_DSRR1, RFDI)
  770. .globl ret_from_mcheck_exc
  771. ret_from_mcheck_exc:
  772. RET_FROM_EXC_LEVEL(SPRN_MCSRR0, SPRN_MCSRR1, RFMCI)
  773. #endif /* CONFIG_BOOKE */
  774. /*
  775. * Load the DBCR0 value for a task that is being ptraced,
  776. * having first saved away the global DBCR0. Note that r0
  777. * has the dbcr0 value to set upon entry to this.
  778. */
  779. load_dbcr0:
  780. mfmsr r10 /* first disable debug exceptions */
  781. rlwinm r10,r10,0,~MSR_DE
  782. mtmsr r10
  783. isync
  784. mfspr r10,SPRN_DBCR0
  785. lis r11,global_dbcr0@ha
  786. addi r11,r11,global_dbcr0@l
  787. stw r10,0(r11)
  788. mtspr SPRN_DBCR0,r0
  789. lwz r10,4(r11)
  790. addi r10,r10,1
  791. stw r10,4(r11)
  792. li r11,-1
  793. mtspr SPRN_DBSR,r11 /* clear all pending debug events */
  794. blr
  795. .section .bss
  796. .align 4
  797. global_dbcr0:
  798. .space 8
  799. .previous
  800. #endif /* !(CONFIG_4xx || CONFIG_BOOKE) */
  801. do_work: /* r10 contains MSR_KERNEL here */
  802. andi. r0,r9,_TIF_NEED_RESCHED
  803. beq do_user_signal
  804. do_resched: /* r10 contains MSR_KERNEL here */
  805. ori r10,r10,MSR_EE
  806. SYNC
  807. MTMSRD(r10) /* hard-enable interrupts */
  808. bl schedule
  809. recheck:
  810. LOAD_MSR_KERNEL(r10,MSR_KERNEL)
  811. SYNC
  812. MTMSRD(r10) /* disable interrupts */
  813. rlwinm r9,r1,0,0,(31-THREAD_SHIFT)
  814. lwz r9,TI_FLAGS(r9)
  815. andi. r0,r9,_TIF_NEED_RESCHED
  816. bne- do_resched
  817. andi. r0,r9,_TIF_SIGPENDING|_TIF_RESTORE_SIGMASK
  818. beq restore_user
  819. do_user_signal: /* r10 contains MSR_KERNEL here */
  820. ori r10,r10,MSR_EE
  821. SYNC
  822. MTMSRD(r10) /* hard-enable interrupts */
  823. /* save r13-r31 in the exception frame, if not already done */
  824. lwz r3,_TRAP(r1)
  825. andi. r0,r3,1
  826. beq 2f
  827. SAVE_NVGPRS(r1)
  828. rlwinm r3,r3,0,0,30
  829. stw r3,_TRAP(r1)
  830. 2: li r3,0
  831. addi r4,r1,STACK_FRAME_OVERHEAD
  832. bl do_signal
  833. REST_NVGPRS(r1)
  834. b recheck
  835. /*
  836. * We come here when we are at the end of handling an exception
  837. * that occurred at a place where taking an exception will lose
  838. * state information, such as the contents of SRR0 and SRR1.
  839. */
  840. nonrecoverable:
  841. lis r10,exc_exit_restart_end@ha
  842. addi r10,r10,exc_exit_restart_end@l
  843. cmplw r12,r10
  844. bge 3f
  845. lis r11,exc_exit_restart@ha
  846. addi r11,r11,exc_exit_restart@l
  847. cmplw r12,r11
  848. blt 3f
  849. lis r10,ee_restarts@ha
  850. lwz r12,ee_restarts@l(r10)
  851. addi r12,r12,1
  852. stw r12,ee_restarts@l(r10)
  853. mr r12,r11 /* restart at exc_exit_restart */
  854. blr
  855. 3: /* OK, we can't recover, kill this process */
  856. /* but the 601 doesn't implement the RI bit, so assume it's OK */
  857. BEGIN_FTR_SECTION
  858. blr
  859. END_FTR_SECTION_IFSET(CPU_FTR_601)
  860. lwz r3,_TRAP(r1)
  861. andi. r0,r3,1
  862. beq 4f
  863. SAVE_NVGPRS(r1)
  864. rlwinm r3,r3,0,0,30
  865. stw r3,_TRAP(r1)
  866. 4: addi r3,r1,STACK_FRAME_OVERHEAD
  867. bl nonrecoverable_exception
  868. /* shouldn't return */
  869. b 4b
  870. .section .bss
  871. .align 2
  872. ee_restarts:
  873. .space 4
  874. .previous
  875. /*
  876. * PROM code for specific machines follows. Put it
  877. * here so it's easy to add arch-specific sections later.
  878. * -- Cort
  879. */
  880. #ifdef CONFIG_PPC_RTAS
  881. /*
  882. * On CHRP, the Run-Time Abstraction Services (RTAS) have to be
  883. * called with the MMU off.
  884. */
  885. _GLOBAL(enter_rtas)
  886. stwu r1,-INT_FRAME_SIZE(r1)
  887. mflr r0
  888. stw r0,INT_FRAME_SIZE+4(r1)
  889. LOAD_REG_ADDR(r4, rtas)
  890. lis r6,1f@ha /* physical return address for rtas */
  891. addi r6,r6,1f@l
  892. tophys(r6,r6)
  893. tophys(r7,r1)
  894. lwz r8,RTASENTRY(r4)
  895. lwz r4,RTASBASE(r4)
  896. mfmsr r9
  897. stw r9,8(r1)
  898. LOAD_MSR_KERNEL(r0,MSR_KERNEL)
  899. SYNC /* disable interrupts so SRR0/1 */
  900. MTMSRD(r0) /* don't get trashed */
  901. li r9,MSR_KERNEL & ~(MSR_IR|MSR_DR)
  902. mtlr r6
  903. mtspr SPRN_SPRG2,r7
  904. mtspr SPRN_SRR0,r8
  905. mtspr SPRN_SRR1,r9
  906. RFI
  907. 1: tophys(r9,r1)
  908. lwz r8,INT_FRAME_SIZE+4(r9) /* get return address */
  909. lwz r9,8(r9) /* original msr value */
  910. FIX_SRR1(r9,r0)
  911. addi r1,r1,INT_FRAME_SIZE
  912. li r0,0
  913. mtspr SPRN_SPRG2,r0
  914. mtspr SPRN_SRR0,r8
  915. mtspr SPRN_SRR1,r9
  916. RFI /* return to caller */
  917. .globl machine_check_in_rtas
  918. machine_check_in_rtas:
  919. twi 31,0,0
  920. /* XXX load up BATs and panic */
  921. #endif /* CONFIG_PPC_RTAS */