entry_32.S 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  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. #ifdef CONFIG_44x
  233. lis r4,icache_44x_need_flush@ha
  234. lwz r5,icache_44x_need_flush@l(r4)
  235. cmplwi cr0,r5,0
  236. bne- 2f
  237. 1:
  238. #endif /* CONFIG_44x */
  239. BEGIN_FTR_SECTION
  240. lwarx r7,0,r1
  241. END_FTR_SECTION_IFSET(CPU_FTR_NEED_PAIRED_STWCX)
  242. stwcx. r0,0,r1 /* to clear the reservation */
  243. lwz r4,_LINK(r1)
  244. lwz r5,_CCR(r1)
  245. mtlr r4
  246. mtcr r5
  247. lwz r7,_NIP(r1)
  248. lwz r8,_MSR(r1)
  249. FIX_SRR1(r8, r0)
  250. lwz r2,GPR2(r1)
  251. lwz r1,GPR1(r1)
  252. mtspr SPRN_SRR0,r7
  253. mtspr SPRN_SRR1,r8
  254. SYNC
  255. RFI
  256. #ifdef CONFIG_44x
  257. 2: li r7,0
  258. iccci r0,r0
  259. stw r7,icache_44x_need_flush@l(r4)
  260. b 1b
  261. #endif /* CONFIG_44x */
  262. 66: li r3,-ENOSYS
  263. b ret_from_syscall
  264. .globl ret_from_fork
  265. ret_from_fork:
  266. REST_NVGPRS(r1)
  267. bl schedule_tail
  268. li r3,0
  269. b ret_from_syscall
  270. /* Traced system call support */
  271. syscall_dotrace:
  272. SAVE_NVGPRS(r1)
  273. li r0,0xc00
  274. stw r0,_TRAP(r1)
  275. addi r3,r1,STACK_FRAME_OVERHEAD
  276. bl do_syscall_trace_enter
  277. lwz r0,GPR0(r1) /* Restore original registers */
  278. lwz r3,GPR3(r1)
  279. lwz r4,GPR4(r1)
  280. lwz r5,GPR5(r1)
  281. lwz r6,GPR6(r1)
  282. lwz r7,GPR7(r1)
  283. lwz r8,GPR8(r1)
  284. REST_NVGPRS(r1)
  285. b syscall_dotrace_cont
  286. syscall_exit_work:
  287. andi. r0,r9,_TIF_RESTOREALL
  288. beq+ 0f
  289. REST_NVGPRS(r1)
  290. b 2f
  291. 0: cmplw 0,r3,r8
  292. blt+ 1f
  293. andi. r0,r9,_TIF_NOERROR
  294. bne- 1f
  295. lwz r11,_CCR(r1) /* Load CR */
  296. neg r3,r3
  297. oris r11,r11,0x1000 /* Set SO bit in CR */
  298. stw r11,_CCR(r1)
  299. 1: stw r6,RESULT(r1) /* Save result */
  300. stw r3,GPR3(r1) /* Update return value */
  301. 2: andi. r0,r9,(_TIF_PERSYSCALL_MASK)
  302. beq 4f
  303. /* Clear per-syscall TIF flags if any are set. */
  304. li r11,_TIF_PERSYSCALL_MASK
  305. addi r12,r12,TI_FLAGS
  306. 3: lwarx r8,0,r12
  307. andc r8,r8,r11
  308. #ifdef CONFIG_IBM405_ERR77
  309. dcbt 0,r12
  310. #endif
  311. stwcx. r8,0,r12
  312. bne- 3b
  313. subi r12,r12,TI_FLAGS
  314. 4: /* Anything which requires enabling interrupts? */
  315. andi. r0,r9,(_TIF_SYSCALL_T_OR_A|_TIF_SINGLESTEP)
  316. beq ret_from_except
  317. /* Re-enable interrupts */
  318. ori r10,r10,MSR_EE
  319. SYNC
  320. MTMSRD(r10)
  321. /* Save NVGPRS if they're not saved already */
  322. lwz r4,_TRAP(r1)
  323. andi. r4,r4,1
  324. beq 5f
  325. SAVE_NVGPRS(r1)
  326. li r4,0xc00
  327. stw r4,_TRAP(r1)
  328. 5:
  329. addi r3,r1,STACK_FRAME_OVERHEAD
  330. bl do_syscall_trace_leave
  331. b ret_from_except_full
  332. #ifdef SHOW_SYSCALLS
  333. do_show_syscall:
  334. #ifdef SHOW_SYSCALLS_TASK
  335. lis r11,show_syscalls_task@ha
  336. lwz r11,show_syscalls_task@l(r11)
  337. cmp 0,r2,r11
  338. bnelr
  339. #endif
  340. stw r31,GPR31(r1)
  341. mflr r31
  342. lis r3,7f@ha
  343. addi r3,r3,7f@l
  344. lwz r4,GPR0(r1)
  345. lwz r5,GPR3(r1)
  346. lwz r6,GPR4(r1)
  347. lwz r7,GPR5(r1)
  348. lwz r8,GPR6(r1)
  349. lwz r9,GPR7(r1)
  350. bl printk
  351. lis r3,77f@ha
  352. addi r3,r3,77f@l
  353. lwz r4,GPR8(r1)
  354. mr r5,r2
  355. bl printk
  356. lwz r0,GPR0(r1)
  357. lwz r3,GPR3(r1)
  358. lwz r4,GPR4(r1)
  359. lwz r5,GPR5(r1)
  360. lwz r6,GPR6(r1)
  361. lwz r7,GPR7(r1)
  362. lwz r8,GPR8(r1)
  363. mtlr r31
  364. lwz r31,GPR31(r1)
  365. blr
  366. do_show_syscall_exit:
  367. #ifdef SHOW_SYSCALLS_TASK
  368. lis r11,show_syscalls_task@ha
  369. lwz r11,show_syscalls_task@l(r11)
  370. cmp 0,r2,r11
  371. bnelr
  372. #endif
  373. stw r31,GPR31(r1)
  374. mflr r31
  375. stw r3,RESULT(r1) /* Save result */
  376. mr r4,r3
  377. lis r3,79f@ha
  378. addi r3,r3,79f@l
  379. bl printk
  380. lwz r3,RESULT(r1)
  381. mtlr r31
  382. lwz r31,GPR31(r1)
  383. blr
  384. 7: .string "syscall %d(%x, %x, %x, %x, %x, "
  385. 77: .string "%x), current=%p\n"
  386. 79: .string " -> %x\n"
  387. .align 2,0
  388. #ifdef SHOW_SYSCALLS_TASK
  389. .data
  390. .globl show_syscalls_task
  391. show_syscalls_task:
  392. .long -1
  393. .text
  394. #endif
  395. #endif /* SHOW_SYSCALLS */
  396. /*
  397. * The fork/clone functions need to copy the full register set into
  398. * the child process. Therefore we need to save all the nonvolatile
  399. * registers (r13 - r31) before calling the C code.
  400. */
  401. .globl ppc_fork
  402. ppc_fork:
  403. SAVE_NVGPRS(r1)
  404. lwz r0,_TRAP(r1)
  405. rlwinm r0,r0,0,0,30 /* clear LSB to indicate full */
  406. stw r0,_TRAP(r1) /* register set saved */
  407. b sys_fork
  408. .globl ppc_vfork
  409. ppc_vfork:
  410. SAVE_NVGPRS(r1)
  411. lwz r0,_TRAP(r1)
  412. rlwinm r0,r0,0,0,30 /* clear LSB to indicate full */
  413. stw r0,_TRAP(r1) /* register set saved */
  414. b sys_vfork
  415. .globl ppc_clone
  416. ppc_clone:
  417. SAVE_NVGPRS(r1)
  418. lwz r0,_TRAP(r1)
  419. rlwinm r0,r0,0,0,30 /* clear LSB to indicate full */
  420. stw r0,_TRAP(r1) /* register set saved */
  421. b sys_clone
  422. .globl ppc_swapcontext
  423. ppc_swapcontext:
  424. SAVE_NVGPRS(r1)
  425. lwz r0,_TRAP(r1)
  426. rlwinm r0,r0,0,0,30 /* clear LSB to indicate full */
  427. stw r0,_TRAP(r1) /* register set saved */
  428. b sys_swapcontext
  429. /*
  430. * Top-level page fault handling.
  431. * This is in assembler because if do_page_fault tells us that
  432. * it is a bad kernel page fault, we want to save the non-volatile
  433. * registers before calling bad_page_fault.
  434. */
  435. .globl handle_page_fault
  436. handle_page_fault:
  437. stw r4,_DAR(r1)
  438. addi r3,r1,STACK_FRAME_OVERHEAD
  439. bl do_page_fault
  440. cmpwi r3,0
  441. beq+ ret_from_except
  442. SAVE_NVGPRS(r1)
  443. lwz r0,_TRAP(r1)
  444. clrrwi r0,r0,1
  445. stw r0,_TRAP(r1)
  446. mr r5,r3
  447. addi r3,r1,STACK_FRAME_OVERHEAD
  448. lwz r4,_DAR(r1)
  449. bl bad_page_fault
  450. b ret_from_except_full
  451. /*
  452. * This routine switches between two different tasks. The process
  453. * state of one is saved on its kernel stack. Then the state
  454. * of the other is restored from its kernel stack. The memory
  455. * management hardware is updated to the second process's state.
  456. * Finally, we can return to the second process.
  457. * On entry, r3 points to the THREAD for the current task, r4
  458. * points to the THREAD for the new task.
  459. *
  460. * This routine is always called with interrupts disabled.
  461. *
  462. * Note: there are two ways to get to the "going out" portion
  463. * of this code; either by coming in via the entry (_switch)
  464. * or via "fork" which must set up an environment equivalent
  465. * to the "_switch" path. If you change this , you'll have to
  466. * change the fork code also.
  467. *
  468. * The code which creates the new task context is in 'copy_thread'
  469. * in arch/ppc/kernel/process.c
  470. */
  471. _GLOBAL(_switch)
  472. stwu r1,-INT_FRAME_SIZE(r1)
  473. mflr r0
  474. stw r0,INT_FRAME_SIZE+4(r1)
  475. /* r3-r12 are caller saved -- Cort */
  476. SAVE_NVGPRS(r1)
  477. stw r0,_NIP(r1) /* Return to switch caller */
  478. mfmsr r11
  479. li r0,MSR_FP /* Disable floating-point */
  480. #ifdef CONFIG_ALTIVEC
  481. BEGIN_FTR_SECTION
  482. oris r0,r0,MSR_VEC@h /* Disable altivec */
  483. mfspr r12,SPRN_VRSAVE /* save vrsave register value */
  484. stw r12,THREAD+THREAD_VRSAVE(r2)
  485. END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
  486. #endif /* CONFIG_ALTIVEC */
  487. #ifdef CONFIG_SPE
  488. BEGIN_FTR_SECTION
  489. oris r0,r0,MSR_SPE@h /* Disable SPE */
  490. mfspr r12,SPRN_SPEFSCR /* save spefscr register value */
  491. stw r12,THREAD+THREAD_SPEFSCR(r2)
  492. END_FTR_SECTION_IFSET(CPU_FTR_SPE)
  493. #endif /* CONFIG_SPE */
  494. and. r0,r0,r11 /* FP or altivec or SPE enabled? */
  495. beq+ 1f
  496. andc r11,r11,r0
  497. MTMSRD(r11)
  498. isync
  499. 1: stw r11,_MSR(r1)
  500. mfcr r10
  501. stw r10,_CCR(r1)
  502. stw r1,KSP(r3) /* Set old stack pointer */
  503. #ifdef CONFIG_SMP
  504. /* We need a sync somewhere here to make sure that if the
  505. * previous task gets rescheduled on another CPU, it sees all
  506. * stores it has performed on this one.
  507. */
  508. sync
  509. #endif /* CONFIG_SMP */
  510. tophys(r0,r4)
  511. CLR_TOP32(r0)
  512. mtspr SPRN_SPRG3,r0 /* Update current THREAD phys addr */
  513. lwz r1,KSP(r4) /* Load new stack pointer */
  514. /* save the old current 'last' for return value */
  515. mr r3,r2
  516. addi r2,r4,-THREAD /* Update current */
  517. #ifdef CONFIG_ALTIVEC
  518. BEGIN_FTR_SECTION
  519. lwz r0,THREAD+THREAD_VRSAVE(r2)
  520. mtspr SPRN_VRSAVE,r0 /* if G4, restore VRSAVE reg */
  521. END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
  522. #endif /* CONFIG_ALTIVEC */
  523. #ifdef CONFIG_SPE
  524. BEGIN_FTR_SECTION
  525. lwz r0,THREAD+THREAD_SPEFSCR(r2)
  526. mtspr SPRN_SPEFSCR,r0 /* restore SPEFSCR reg */
  527. END_FTR_SECTION_IFSET(CPU_FTR_SPE)
  528. #endif /* CONFIG_SPE */
  529. lwz r0,_CCR(r1)
  530. mtcrf 0xFF,r0
  531. /* r3-r12 are destroyed -- Cort */
  532. REST_NVGPRS(r1)
  533. lwz r4,_NIP(r1) /* Return to _switch caller in new task */
  534. mtlr r4
  535. addi r1,r1,INT_FRAME_SIZE
  536. blr
  537. .globl fast_exception_return
  538. fast_exception_return:
  539. #if !(defined(CONFIG_4xx) || defined(CONFIG_BOOKE))
  540. andi. r10,r9,MSR_RI /* check for recoverable interrupt */
  541. beq 1f /* if not, we've got problems */
  542. #endif
  543. 2: REST_4GPRS(3, r11)
  544. lwz r10,_CCR(r11)
  545. REST_GPR(1, r11)
  546. mtcr r10
  547. lwz r10,_LINK(r11)
  548. mtlr r10
  549. REST_GPR(10, r11)
  550. mtspr SPRN_SRR1,r9
  551. mtspr SPRN_SRR0,r12
  552. REST_GPR(9, r11)
  553. REST_GPR(12, r11)
  554. lwz r11,GPR11(r11)
  555. SYNC
  556. RFI
  557. #if !(defined(CONFIG_4xx) || defined(CONFIG_BOOKE))
  558. /* check if the exception happened in a restartable section */
  559. 1: lis r3,exc_exit_restart_end@ha
  560. addi r3,r3,exc_exit_restart_end@l
  561. cmplw r12,r3
  562. bge 3f
  563. lis r4,exc_exit_restart@ha
  564. addi r4,r4,exc_exit_restart@l
  565. cmplw r12,r4
  566. blt 3f
  567. lis r3,fee_restarts@ha
  568. tophys(r3,r3)
  569. lwz r5,fee_restarts@l(r3)
  570. addi r5,r5,1
  571. stw r5,fee_restarts@l(r3)
  572. mr r12,r4 /* restart at exc_exit_restart */
  573. b 2b
  574. .section .bss
  575. .align 2
  576. fee_restarts:
  577. .space 4
  578. .previous
  579. /* aargh, a nonrecoverable interrupt, panic */
  580. /* aargh, we don't know which trap this is */
  581. /* but the 601 doesn't implement the RI bit, so assume it's OK */
  582. 3:
  583. BEGIN_FTR_SECTION
  584. b 2b
  585. END_FTR_SECTION_IFSET(CPU_FTR_601)
  586. li r10,-1
  587. stw r10,_TRAP(r11)
  588. addi r3,r1,STACK_FRAME_OVERHEAD
  589. lis r10,MSR_KERNEL@h
  590. ori r10,r10,MSR_KERNEL@l
  591. bl transfer_to_handler_full
  592. .long nonrecoverable_exception
  593. .long ret_from_except
  594. #endif
  595. .globl ret_from_except_full
  596. ret_from_except_full:
  597. REST_NVGPRS(r1)
  598. /* fall through */
  599. .globl ret_from_except
  600. ret_from_except:
  601. /* Hard-disable interrupts so that current_thread_info()->flags
  602. * can't change between when we test it and when we return
  603. * from the interrupt. */
  604. LOAD_MSR_KERNEL(r10,MSR_KERNEL)
  605. SYNC /* Some chip revs have problems here... */
  606. MTMSRD(r10) /* disable interrupts */
  607. lwz r3,_MSR(r1) /* Returning to user mode? */
  608. andi. r0,r3,MSR_PR
  609. beq resume_kernel
  610. user_exc_return: /* r10 contains MSR_KERNEL here */
  611. /* Check current_thread_info()->flags */
  612. rlwinm r9,r1,0,0,(31-THREAD_SHIFT)
  613. lwz r9,TI_FLAGS(r9)
  614. andi. r0,r9,(_TIF_SIGPENDING|_TIF_RESTORE_SIGMASK|_TIF_NEED_RESCHED)
  615. bne do_work
  616. restore_user:
  617. #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
  618. /* Check whether this process has its own DBCR0 value. The single
  619. step bit tells us that dbcr0 should be loaded. */
  620. lwz r0,THREAD+THREAD_DBCR0(r2)
  621. andis. r10,r0,DBCR0_IC@h
  622. bnel- load_dbcr0
  623. #endif
  624. #ifdef CONFIG_PREEMPT
  625. b restore
  626. /* N.B. the only way to get here is from the beq following ret_from_except. */
  627. resume_kernel:
  628. /* check current_thread_info->preempt_count */
  629. rlwinm r9,r1,0,0,(31-THREAD_SHIFT)
  630. lwz r0,TI_PREEMPT(r9)
  631. cmpwi 0,r0,0 /* if non-zero, just restore regs and return */
  632. bne restore
  633. lwz r0,TI_FLAGS(r9)
  634. andi. r0,r0,_TIF_NEED_RESCHED
  635. beq+ restore
  636. andi. r0,r3,MSR_EE /* interrupts off? */
  637. beq restore /* don't schedule if so */
  638. 1: bl preempt_schedule_irq
  639. rlwinm r9,r1,0,0,(31-THREAD_SHIFT)
  640. lwz r3,TI_FLAGS(r9)
  641. andi. r0,r3,_TIF_NEED_RESCHED
  642. bne- 1b
  643. #else
  644. resume_kernel:
  645. #endif /* CONFIG_PREEMPT */
  646. /* interrupts are hard-disabled at this point */
  647. restore:
  648. #ifdef CONFIG_44x
  649. lis r4,icache_44x_need_flush@ha
  650. lwz r5,icache_44x_need_flush@l(r4)
  651. cmplwi cr0,r5,0
  652. beq+ 1f
  653. li r6,0
  654. iccci r0,r0
  655. stw r6,icache_44x_need_flush@l(r4)
  656. 1:
  657. #endif /* CONFIG_44x */
  658. lwz r0,GPR0(r1)
  659. lwz r2,GPR2(r1)
  660. REST_4GPRS(3, r1)
  661. REST_2GPRS(7, r1)
  662. lwz r10,_XER(r1)
  663. lwz r11,_CTR(r1)
  664. mtspr SPRN_XER,r10
  665. mtctr r11
  666. PPC405_ERR77(0,r1)
  667. BEGIN_FTR_SECTION
  668. lwarx r11,0,r1
  669. END_FTR_SECTION_IFSET(CPU_FTR_NEED_PAIRED_STWCX)
  670. stwcx. r0,0,r1 /* to clear the reservation */
  671. #if !(defined(CONFIG_4xx) || defined(CONFIG_BOOKE))
  672. lwz r9,_MSR(r1)
  673. andi. r10,r9,MSR_RI /* check if this exception occurred */
  674. beql nonrecoverable /* at a bad place (MSR:RI = 0) */
  675. lwz r10,_CCR(r1)
  676. lwz r11,_LINK(r1)
  677. mtcrf 0xFF,r10
  678. mtlr r11
  679. /*
  680. * Once we put values in SRR0 and SRR1, we are in a state
  681. * where exceptions are not recoverable, since taking an
  682. * exception will trash SRR0 and SRR1. Therefore we clear the
  683. * MSR:RI bit to indicate this. If we do take an exception,
  684. * we can't return to the point of the exception but we
  685. * can restart the exception exit path at the label
  686. * exc_exit_restart below. -- paulus
  687. */
  688. LOAD_MSR_KERNEL(r10,MSR_KERNEL & ~MSR_RI)
  689. SYNC
  690. MTMSRD(r10) /* clear the RI bit */
  691. .globl exc_exit_restart
  692. exc_exit_restart:
  693. lwz r9,_MSR(r1)
  694. lwz r12,_NIP(r1)
  695. FIX_SRR1(r9,r10)
  696. mtspr SPRN_SRR0,r12
  697. mtspr SPRN_SRR1,r9
  698. REST_4GPRS(9, r1)
  699. lwz r1,GPR1(r1)
  700. .globl exc_exit_restart_end
  701. exc_exit_restart_end:
  702. SYNC
  703. RFI
  704. #else /* !(CONFIG_4xx || CONFIG_BOOKE) */
  705. /*
  706. * This is a bit different on 4xx/Book-E because it doesn't have
  707. * the RI bit in the MSR.
  708. * The TLB miss handler checks if we have interrupted
  709. * the exception exit path and restarts it if so
  710. * (well maybe one day it will... :).
  711. */
  712. lwz r11,_LINK(r1)
  713. mtlr r11
  714. lwz r10,_CCR(r1)
  715. mtcrf 0xff,r10
  716. REST_2GPRS(9, r1)
  717. .globl exc_exit_restart
  718. exc_exit_restart:
  719. lwz r11,_NIP(r1)
  720. lwz r12,_MSR(r1)
  721. exc_exit_start:
  722. mtspr SPRN_SRR0,r11
  723. mtspr SPRN_SRR1,r12
  724. REST_2GPRS(11, r1)
  725. lwz r1,GPR1(r1)
  726. .globl exc_exit_restart_end
  727. exc_exit_restart_end:
  728. PPC405_ERR77_SYNC
  729. rfi
  730. b . /* prevent prefetch past rfi */
  731. /*
  732. * Returning from a critical interrupt in user mode doesn't need
  733. * to be any different from a normal exception. For a critical
  734. * interrupt in the kernel, we just return (without checking for
  735. * preemption) since the interrupt may have happened at some crucial
  736. * place (e.g. inside the TLB miss handler), and because we will be
  737. * running with r1 pointing into critical_stack, not the current
  738. * process's kernel stack (and therefore current_thread_info() will
  739. * give the wrong answer).
  740. * We have to restore various SPRs that may have been in use at the
  741. * time of the critical interrupt.
  742. *
  743. */
  744. #ifdef CONFIG_40x
  745. #define PPC_40x_TURN_OFF_MSR_DR \
  746. /* avoid any possible TLB misses here by turning off MSR.DR, we \
  747. * assume the instructions here are mapped by a pinned TLB entry */ \
  748. li r10,MSR_IR; \
  749. mtmsr r10; \
  750. isync; \
  751. tophys(r1, r1);
  752. #else
  753. #define PPC_40x_TURN_OFF_MSR_DR
  754. #endif
  755. #define RET_FROM_EXC_LEVEL(exc_lvl_srr0, exc_lvl_srr1, exc_lvl_rfi) \
  756. REST_NVGPRS(r1); \
  757. lwz r3,_MSR(r1); \
  758. andi. r3,r3,MSR_PR; \
  759. LOAD_MSR_KERNEL(r10,MSR_KERNEL); \
  760. bne user_exc_return; \
  761. lwz r0,GPR0(r1); \
  762. lwz r2,GPR2(r1); \
  763. REST_4GPRS(3, r1); \
  764. REST_2GPRS(7, r1); \
  765. lwz r10,_XER(r1); \
  766. lwz r11,_CTR(r1); \
  767. mtspr SPRN_XER,r10; \
  768. mtctr r11; \
  769. PPC405_ERR77(0,r1); \
  770. stwcx. r0,0,r1; /* to clear the reservation */ \
  771. lwz r11,_LINK(r1); \
  772. mtlr r11; \
  773. lwz r10,_CCR(r1); \
  774. mtcrf 0xff,r10; \
  775. PPC_40x_TURN_OFF_MSR_DR; \
  776. lwz r9,_DEAR(r1); \
  777. lwz r10,_ESR(r1); \
  778. mtspr SPRN_DEAR,r9; \
  779. mtspr SPRN_ESR,r10; \
  780. lwz r11,_NIP(r1); \
  781. lwz r12,_MSR(r1); \
  782. mtspr exc_lvl_srr0,r11; \
  783. mtspr exc_lvl_srr1,r12; \
  784. lwz r9,GPR9(r1); \
  785. lwz r12,GPR12(r1); \
  786. lwz r10,GPR10(r1); \
  787. lwz r11,GPR11(r1); \
  788. lwz r1,GPR1(r1); \
  789. PPC405_ERR77_SYNC; \
  790. exc_lvl_rfi; \
  791. b .; /* prevent prefetch past exc_lvl_rfi */
  792. .globl ret_from_crit_exc
  793. ret_from_crit_exc:
  794. RET_FROM_EXC_LEVEL(SPRN_CSRR0, SPRN_CSRR1, RFCI)
  795. #ifdef CONFIG_BOOKE
  796. .globl ret_from_debug_exc
  797. ret_from_debug_exc:
  798. RET_FROM_EXC_LEVEL(SPRN_DSRR0, SPRN_DSRR1, RFDI)
  799. .globl ret_from_mcheck_exc
  800. ret_from_mcheck_exc:
  801. RET_FROM_EXC_LEVEL(SPRN_MCSRR0, SPRN_MCSRR1, RFMCI)
  802. #endif /* CONFIG_BOOKE */
  803. /*
  804. * Load the DBCR0 value for a task that is being ptraced,
  805. * having first saved away the global DBCR0. Note that r0
  806. * has the dbcr0 value to set upon entry to this.
  807. */
  808. load_dbcr0:
  809. mfmsr r10 /* first disable debug exceptions */
  810. rlwinm r10,r10,0,~MSR_DE
  811. mtmsr r10
  812. isync
  813. mfspr r10,SPRN_DBCR0
  814. lis r11,global_dbcr0@ha
  815. addi r11,r11,global_dbcr0@l
  816. stw r10,0(r11)
  817. mtspr SPRN_DBCR0,r0
  818. lwz r10,4(r11)
  819. addi r10,r10,1
  820. stw r10,4(r11)
  821. li r11,-1
  822. mtspr SPRN_DBSR,r11 /* clear all pending debug events */
  823. blr
  824. .section .bss
  825. .align 4
  826. global_dbcr0:
  827. .space 8
  828. .previous
  829. #endif /* !(CONFIG_4xx || CONFIG_BOOKE) */
  830. do_work: /* r10 contains MSR_KERNEL here */
  831. andi. r0,r9,_TIF_NEED_RESCHED
  832. beq do_user_signal
  833. do_resched: /* r10 contains MSR_KERNEL here */
  834. ori r10,r10,MSR_EE
  835. SYNC
  836. MTMSRD(r10) /* hard-enable interrupts */
  837. bl schedule
  838. recheck:
  839. LOAD_MSR_KERNEL(r10,MSR_KERNEL)
  840. SYNC
  841. MTMSRD(r10) /* disable interrupts */
  842. rlwinm r9,r1,0,0,(31-THREAD_SHIFT)
  843. lwz r9,TI_FLAGS(r9)
  844. andi. r0,r9,_TIF_NEED_RESCHED
  845. bne- do_resched
  846. andi. r0,r9,_TIF_SIGPENDING|_TIF_RESTORE_SIGMASK
  847. beq restore_user
  848. do_user_signal: /* r10 contains MSR_KERNEL here */
  849. ori r10,r10,MSR_EE
  850. SYNC
  851. MTMSRD(r10) /* hard-enable interrupts */
  852. /* save r13-r31 in the exception frame, if not already done */
  853. lwz r3,_TRAP(r1)
  854. andi. r0,r3,1
  855. beq 2f
  856. SAVE_NVGPRS(r1)
  857. rlwinm r3,r3,0,0,30
  858. stw r3,_TRAP(r1)
  859. 2: li r3,0
  860. addi r4,r1,STACK_FRAME_OVERHEAD
  861. bl do_signal
  862. REST_NVGPRS(r1)
  863. b recheck
  864. /*
  865. * We come here when we are at the end of handling an exception
  866. * that occurred at a place where taking an exception will lose
  867. * state information, such as the contents of SRR0 and SRR1.
  868. */
  869. nonrecoverable:
  870. lis r10,exc_exit_restart_end@ha
  871. addi r10,r10,exc_exit_restart_end@l
  872. cmplw r12,r10
  873. bge 3f
  874. lis r11,exc_exit_restart@ha
  875. addi r11,r11,exc_exit_restart@l
  876. cmplw r12,r11
  877. blt 3f
  878. lis r10,ee_restarts@ha
  879. lwz r12,ee_restarts@l(r10)
  880. addi r12,r12,1
  881. stw r12,ee_restarts@l(r10)
  882. mr r12,r11 /* restart at exc_exit_restart */
  883. blr
  884. 3: /* OK, we can't recover, kill this process */
  885. /* but the 601 doesn't implement the RI bit, so assume it's OK */
  886. BEGIN_FTR_SECTION
  887. blr
  888. END_FTR_SECTION_IFSET(CPU_FTR_601)
  889. lwz r3,_TRAP(r1)
  890. andi. r0,r3,1
  891. beq 4f
  892. SAVE_NVGPRS(r1)
  893. rlwinm r3,r3,0,0,30
  894. stw r3,_TRAP(r1)
  895. 4: addi r3,r1,STACK_FRAME_OVERHEAD
  896. bl nonrecoverable_exception
  897. /* shouldn't return */
  898. b 4b
  899. .section .bss
  900. .align 2
  901. ee_restarts:
  902. .space 4
  903. .previous
  904. /*
  905. * PROM code for specific machines follows. Put it
  906. * here so it's easy to add arch-specific sections later.
  907. * -- Cort
  908. */
  909. #ifdef CONFIG_PPC_RTAS
  910. /*
  911. * On CHRP, the Run-Time Abstraction Services (RTAS) have to be
  912. * called with the MMU off.
  913. */
  914. _GLOBAL(enter_rtas)
  915. stwu r1,-INT_FRAME_SIZE(r1)
  916. mflr r0
  917. stw r0,INT_FRAME_SIZE+4(r1)
  918. LOAD_REG_ADDR(r4, rtas)
  919. lis r6,1f@ha /* physical return address for rtas */
  920. addi r6,r6,1f@l
  921. tophys(r6,r6)
  922. tophys(r7,r1)
  923. lwz r8,RTASENTRY(r4)
  924. lwz r4,RTASBASE(r4)
  925. mfmsr r9
  926. stw r9,8(r1)
  927. LOAD_MSR_KERNEL(r0,MSR_KERNEL)
  928. SYNC /* disable interrupts so SRR0/1 */
  929. MTMSRD(r0) /* don't get trashed */
  930. li r9,MSR_KERNEL & ~(MSR_IR|MSR_DR)
  931. mtlr r6
  932. mtspr SPRN_SPRG2,r7
  933. mtspr SPRN_SRR0,r8
  934. mtspr SPRN_SRR1,r9
  935. RFI
  936. 1: tophys(r9,r1)
  937. lwz r8,INT_FRAME_SIZE+4(r9) /* get return address */
  938. lwz r9,8(r9) /* original msr value */
  939. FIX_SRR1(r9,r0)
  940. addi r1,r1,INT_FRAME_SIZE
  941. li r0,0
  942. mtspr SPRN_SPRG2,r0
  943. mtspr SPRN_SRR0,r8
  944. mtspr SPRN_SRR1,r9
  945. RFI /* return to caller */
  946. .globl machine_check_in_rtas
  947. machine_check_in_rtas:
  948. twi 31,0,0
  949. /* XXX load up BATs and panic */
  950. #endif /* CONFIG_PPC_RTAS */