head_40x.S 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  1. /*
  2. * Copyright (c) 1995-1996 Gary Thomas <gdt@linuxppc.org>
  3. * Initial PowerPC version.
  4. * Copyright (c) 1996 Cort Dougan <cort@cs.nmt.edu>
  5. * Rewritten for PReP
  6. * Copyright (c) 1996 Paul Mackerras <paulus@cs.anu.edu.au>
  7. * Low-level exception handers, MMU support, and rewrite.
  8. * Copyright (c) 1997 Dan Malek <dmalek@jlc.net>
  9. * PowerPC 8xx modifications.
  10. * Copyright (c) 1998-1999 TiVo, Inc.
  11. * PowerPC 403GCX modifications.
  12. * Copyright (c) 1999 Grant Erickson <grant@lcse.umn.edu>
  13. * PowerPC 403GCX/405GP modifications.
  14. * Copyright 2000 MontaVista Software Inc.
  15. * PPC405 modifications
  16. * PowerPC 403GCX/405GP modifications.
  17. * Author: MontaVista Software, Inc.
  18. * frank_rowand@mvista.com or source@mvista.com
  19. * debbie_chu@mvista.com
  20. *
  21. *
  22. * Module name: head_4xx.S
  23. *
  24. * Description:
  25. * Kernel execution entry point code.
  26. *
  27. * This program is free software; you can redistribute it and/or
  28. * modify it under the terms of the GNU General Public License
  29. * as published by the Free Software Foundation; either version
  30. * 2 of the License, or (at your option) any later version.
  31. *
  32. */
  33. #include <linux/init.h>
  34. #include <asm/processor.h>
  35. #include <asm/page.h>
  36. #include <asm/mmu.h>
  37. #include <asm/pgtable.h>
  38. #include <asm/cputable.h>
  39. #include <asm/thread_info.h>
  40. #include <asm/ppc_asm.h>
  41. #include <asm/asm-offsets.h>
  42. #include <asm/ptrace.h>
  43. /* As with the other PowerPC ports, it is expected that when code
  44. * execution begins here, the following registers contain valid, yet
  45. * optional, information:
  46. *
  47. * r3 - Board info structure pointer (DRAM, frequency, MAC address, etc.)
  48. * r4 - Starting address of the init RAM disk
  49. * r5 - Ending address of the init RAM disk
  50. * r6 - Start of kernel command line string (e.g. "mem=96m")
  51. * r7 - End of kernel command line string
  52. *
  53. * This is all going to change RSN when we add bi_recs....... -- Dan
  54. */
  55. __HEAD
  56. _ENTRY(_stext);
  57. _ENTRY(_start);
  58. /* Save parameters we are passed.
  59. */
  60. mr r31,r3
  61. mr r30,r4
  62. mr r29,r5
  63. mr r28,r6
  64. mr r27,r7
  65. /* We have to turn on the MMU right away so we get cache modes
  66. * set correctly.
  67. */
  68. bl initial_mmu
  69. /* We now have the lower 16 Meg mapped into TLB entries, and the caches
  70. * ready to work.
  71. */
  72. turn_on_mmu:
  73. lis r0,MSR_KERNEL@h
  74. ori r0,r0,MSR_KERNEL@l
  75. mtspr SPRN_SRR1,r0
  76. lis r0,start_here@h
  77. ori r0,r0,start_here@l
  78. mtspr SPRN_SRR0,r0
  79. SYNC
  80. rfi /* enables MMU */
  81. b . /* prevent prefetch past rfi */
  82. /*
  83. * This area is used for temporarily saving registers during the
  84. * critical exception prolog.
  85. */
  86. . = 0xc0
  87. crit_save:
  88. _ENTRY(crit_r10)
  89. .space 4
  90. _ENTRY(crit_r11)
  91. .space 4
  92. _ENTRY(crit_srr0)
  93. .space 4
  94. _ENTRY(crit_srr1)
  95. .space 4
  96. _ENTRY(saved_ksp_limit)
  97. .space 4
  98. /*
  99. * Exception vector entry code. This code runs with address translation
  100. * turned off (i.e. using physical addresses). We assume SPRG_THREAD has
  101. * the physical address of the current task thread_struct.
  102. * Note that we have to have decremented r1 before we write to any fields
  103. * of the exception frame, since a critical interrupt could occur at any
  104. * time, and it will write to the area immediately below the current r1.
  105. */
  106. #define NORMAL_EXCEPTION_PROLOG \
  107. mtspr SPRN_SPRG_SCRATCH0,r10; /* save two registers to work with */\
  108. mtspr SPRN_SPRG_SCRATCH1,r11; \
  109. mtspr SPRN_SPRG_SCRATCH2,r1; \
  110. mfcr r10; /* save CR in r10 for now */\
  111. mfspr r11,SPRN_SRR1; /* check whether user or kernel */\
  112. andi. r11,r11,MSR_PR; \
  113. beq 1f; \
  114. mfspr r1,SPRN_SPRG_THREAD; /* if from user, start at top of */\
  115. lwz r1,THREAD_INFO-THREAD(r1); /* this thread's kernel stack */\
  116. addi r1,r1,THREAD_SIZE; \
  117. 1: subi r1,r1,INT_FRAME_SIZE; /* Allocate an exception frame */\
  118. tophys(r11,r1); \
  119. stw r10,_CCR(r11); /* save various registers */\
  120. stw r12,GPR12(r11); \
  121. stw r9,GPR9(r11); \
  122. mfspr r10,SPRN_SPRG_SCRATCH0; \
  123. stw r10,GPR10(r11); \
  124. mfspr r12,SPRN_SPRG_SCRATCH1; \
  125. stw r12,GPR11(r11); \
  126. mflr r10; \
  127. stw r10,_LINK(r11); \
  128. mfspr r10,SPRN_SPRG_SCRATCH2; \
  129. mfspr r12,SPRN_SRR0; \
  130. stw r10,GPR1(r11); \
  131. mfspr r9,SPRN_SRR1; \
  132. stw r10,0(r11); \
  133. rlwinm r9,r9,0,14,12; /* clear MSR_WE (necessary?) */\
  134. stw r0,GPR0(r11); \
  135. SAVE_4GPRS(3, r11); \
  136. SAVE_2GPRS(7, r11)
  137. /*
  138. * Exception prolog for critical exceptions. This is a little different
  139. * from the normal exception prolog above since a critical exception
  140. * can potentially occur at any point during normal exception processing.
  141. * Thus we cannot use the same SPRG registers as the normal prolog above.
  142. * Instead we use a couple of words of memory at low physical addresses.
  143. * This is OK since we don't support SMP on these processors.
  144. */
  145. #define CRITICAL_EXCEPTION_PROLOG \
  146. stw r10,crit_r10@l(0); /* save two registers to work with */\
  147. stw r11,crit_r11@l(0); \
  148. mfcr r10; /* save CR in r10 for now */\
  149. mfspr r11,SPRN_SRR3; /* check whether user or kernel */\
  150. andi. r11,r11,MSR_PR; \
  151. lis r11,critirq_ctx@ha; \
  152. tophys(r11,r11); \
  153. lwz r11,critirq_ctx@l(r11); \
  154. beq 1f; \
  155. /* COMING FROM USER MODE */ \
  156. mfspr r11,SPRN_SPRG_THREAD; /* if from user, start at top of */\
  157. lwz r11,THREAD_INFO-THREAD(r11); /* this thread's kernel stack */\
  158. 1: addi r11,r11,THREAD_SIZE-INT_FRAME_SIZE; /* Alloc an excpt frm */\
  159. tophys(r11,r11); \
  160. stw r10,_CCR(r11); /* save various registers */\
  161. stw r12,GPR12(r11); \
  162. stw r9,GPR9(r11); \
  163. mflr r10; \
  164. stw r10,_LINK(r11); \
  165. mfspr r12,SPRN_DEAR; /* save DEAR and ESR in the frame */\
  166. stw r12,_DEAR(r11); /* since they may have had stuff */\
  167. mfspr r9,SPRN_ESR; /* in them at the point where the */\
  168. stw r9,_ESR(r11); /* exception was taken */\
  169. mfspr r12,SPRN_SRR2; \
  170. stw r1,GPR1(r11); \
  171. mfspr r9,SPRN_SRR3; \
  172. stw r1,0(r11); \
  173. tovirt(r1,r11); \
  174. rlwinm r9,r9,0,14,12; /* clear MSR_WE (necessary?) */\
  175. stw r0,GPR0(r11); \
  176. SAVE_4GPRS(3, r11); \
  177. SAVE_2GPRS(7, r11)
  178. /*
  179. * State at this point:
  180. * r9 saved in stack frame, now saved SRR3 & ~MSR_WE
  181. * r10 saved in crit_r10 and in stack frame, trashed
  182. * r11 saved in crit_r11 and in stack frame,
  183. * now phys stack/exception frame pointer
  184. * r12 saved in stack frame, now saved SRR2
  185. * CR saved in stack frame, CR0.EQ = !SRR3.PR
  186. * LR, DEAR, ESR in stack frame
  187. * r1 saved in stack frame, now virt stack/excframe pointer
  188. * r0, r3-r8 saved in stack frame
  189. */
  190. /*
  191. * Exception vectors.
  192. */
  193. #define START_EXCEPTION(n, label) \
  194. . = n; \
  195. label:
  196. #define EXCEPTION(n, label, hdlr, xfer) \
  197. START_EXCEPTION(n, label); \
  198. NORMAL_EXCEPTION_PROLOG; \
  199. addi r3,r1,STACK_FRAME_OVERHEAD; \
  200. xfer(n, hdlr)
  201. #define CRITICAL_EXCEPTION(n, label, hdlr) \
  202. START_EXCEPTION(n, label); \
  203. CRITICAL_EXCEPTION_PROLOG; \
  204. addi r3,r1,STACK_FRAME_OVERHEAD; \
  205. EXC_XFER_TEMPLATE(hdlr, n+2, (MSR_KERNEL & ~(MSR_ME|MSR_DE|MSR_CE)), \
  206. NOCOPY, crit_transfer_to_handler, \
  207. ret_from_crit_exc)
  208. #define EXC_XFER_TEMPLATE(hdlr, trap, msr, copyee, tfer, ret) \
  209. li r10,trap; \
  210. stw r10,_TRAP(r11); \
  211. lis r10,msr@h; \
  212. ori r10,r10,msr@l; \
  213. copyee(r10, r9); \
  214. bl tfer; \
  215. .long hdlr; \
  216. .long ret
  217. #define COPY_EE(d, s) rlwimi d,s,0,16,16
  218. #define NOCOPY(d, s)
  219. #define EXC_XFER_STD(n, hdlr) \
  220. EXC_XFER_TEMPLATE(hdlr, n, MSR_KERNEL, NOCOPY, transfer_to_handler_full, \
  221. ret_from_except_full)
  222. #define EXC_XFER_LITE(n, hdlr) \
  223. EXC_XFER_TEMPLATE(hdlr, n+1, MSR_KERNEL, NOCOPY, transfer_to_handler, \
  224. ret_from_except)
  225. #define EXC_XFER_EE(n, hdlr) \
  226. EXC_XFER_TEMPLATE(hdlr, n, MSR_KERNEL, COPY_EE, transfer_to_handler_full, \
  227. ret_from_except_full)
  228. #define EXC_XFER_EE_LITE(n, hdlr) \
  229. EXC_XFER_TEMPLATE(hdlr, n+1, MSR_KERNEL, COPY_EE, transfer_to_handler, \
  230. ret_from_except)
  231. /*
  232. * 0x0100 - Critical Interrupt Exception
  233. */
  234. CRITICAL_EXCEPTION(0x0100, CriticalInterrupt, unknown_exception)
  235. /*
  236. * 0x0200 - Machine Check Exception
  237. */
  238. CRITICAL_EXCEPTION(0x0200, MachineCheck, machine_check_exception)
  239. /*
  240. * 0x0300 - Data Storage Exception
  241. * This happens for just a few reasons. U0 set (but we don't do that),
  242. * or zone protection fault (user violation, write to protected page).
  243. * If this is just an update of modified status, we do that quickly
  244. * and exit. Otherwise, we call heavywight functions to do the work.
  245. */
  246. START_EXCEPTION(0x0300, DataStorage)
  247. mtspr SPRN_SPRG_SCRATCH0, r10 /* Save some working registers */
  248. mtspr SPRN_SPRG_SCRATCH1, r11
  249. #ifdef CONFIG_403GCX
  250. stw r12, 0(r0)
  251. stw r9, 4(r0)
  252. mfcr r11
  253. mfspr r12, SPRN_PID
  254. stw r11, 8(r0)
  255. stw r12, 12(r0)
  256. #else
  257. mtspr SPRN_SPRG_SCRATCH3, r12
  258. mtspr SPRN_SPRG_SCRATCH4, r9
  259. mfcr r11
  260. mfspr r12, SPRN_PID
  261. mtspr SPRN_SPRG_SCRATCH6, r11
  262. mtspr SPRN_SPRG_SCRATCH5, r12
  263. #endif
  264. /* First, check if it was a zone fault (which means a user
  265. * tried to access a kernel or read-protected page - always
  266. * a SEGV). All other faults here must be stores, so no
  267. * need to check ESR_DST as well. */
  268. mfspr r10, SPRN_ESR
  269. andis. r10, r10, ESR_DIZ@h
  270. bne 2f
  271. mfspr r10, SPRN_DEAR /* Get faulting address */
  272. /* If we are faulting a kernel address, we have to use the
  273. * kernel page tables.
  274. */
  275. lis r11, PAGE_OFFSET@h
  276. cmplw r10, r11
  277. blt+ 3f
  278. lis r11, swapper_pg_dir@h
  279. ori r11, r11, swapper_pg_dir@l
  280. li r9, 0
  281. mtspr SPRN_PID, r9 /* TLB will have 0 TID */
  282. b 4f
  283. /* Get the PGD for the current thread.
  284. */
  285. 3:
  286. mfspr r11,SPRN_SPRG_THREAD
  287. lwz r11,PGDIR(r11)
  288. 4:
  289. tophys(r11, r11)
  290. rlwimi r11, r10, 12, 20, 29 /* Create L1 (pgdir/pmd) address */
  291. lwz r11, 0(r11) /* Get L1 entry */
  292. rlwinm. r12, r11, 0, 0, 19 /* Extract L2 (pte) base address */
  293. beq 2f /* Bail if no table */
  294. rlwimi r12, r10, 22, 20, 29 /* Compute PTE address */
  295. lwz r11, 0(r12) /* Get Linux PTE */
  296. andi. r9, r11, _PAGE_RW /* Is it writeable? */
  297. beq 2f /* Bail if not */
  298. /* Update 'changed'.
  299. */
  300. ori r11, r11, _PAGE_DIRTY|_PAGE_ACCESSED|_PAGE_HWWRITE
  301. stw r11, 0(r12) /* Update Linux page table */
  302. /* Most of the Linux PTE is ready to load into the TLB LO.
  303. * We set ZSEL, where only the LS-bit determines user access.
  304. * We set execute, because we don't have the granularity to
  305. * properly set this at the page level (Linux problem).
  306. * If shared is set, we cause a zero PID->TID load.
  307. * Many of these bits are software only. Bits we don't set
  308. * here we (properly should) assume have the appropriate value.
  309. */
  310. li r12, 0x0ce2
  311. andc r11, r11, r12 /* Make sure 20, 21 are zero */
  312. /* find the TLB index that caused the fault. It has to be here.
  313. */
  314. tlbsx r9, 0, r10
  315. tlbwe r11, r9, TLB_DATA /* Load TLB LO */
  316. /* Done...restore registers and get out of here.
  317. */
  318. #ifdef CONFIG_403GCX
  319. lwz r12, 12(r0)
  320. lwz r11, 8(r0)
  321. mtspr SPRN_PID, r12
  322. mtcr r11
  323. lwz r9, 4(r0)
  324. lwz r12, 0(r0)
  325. #else
  326. mfspr r12, SPRN_SPRG_SCRATCH5
  327. mfspr r11, SPRN_SPRG_SCRATCH6
  328. mtspr SPRN_PID, r12
  329. mtcr r11
  330. mfspr r9, SPRN_SPRG_SCRATCH4
  331. mfspr r12, SPRN_SPRG_SCRATCH3
  332. #endif
  333. mfspr r11, SPRN_SPRG_SCRATCH1
  334. mfspr r10, SPRN_SPRG_SCRATCH0
  335. PPC405_ERR77_SYNC
  336. rfi /* Should sync shadow TLBs */
  337. b . /* prevent prefetch past rfi */
  338. 2:
  339. /* The bailout. Restore registers to pre-exception conditions
  340. * and call the heavyweights to help us out.
  341. */
  342. #ifdef CONFIG_403GCX
  343. lwz r12, 12(r0)
  344. lwz r11, 8(r0)
  345. mtspr SPRN_PID, r12
  346. mtcr r11
  347. lwz r9, 4(r0)
  348. lwz r12, 0(r0)
  349. #else
  350. mfspr r12, SPRN_SPRG_SCRATCH5
  351. mfspr r11, SPRN_SPRG_SCRATCH6
  352. mtspr SPRN_PID, r12
  353. mtcr r11
  354. mfspr r9, SPRN_SPRG_SCRATCH4
  355. mfspr r12, SPRN_SPRG_SCRATCH3
  356. #endif
  357. mfspr r11, SPRN_SPRG_SCRATCH1
  358. mfspr r10, SPRN_SPRG_SCRATCH0
  359. b DataAccess
  360. /*
  361. * 0x0400 - Instruction Storage Exception
  362. * This is caused by a fetch from non-execute or guarded pages.
  363. */
  364. START_EXCEPTION(0x0400, InstructionAccess)
  365. NORMAL_EXCEPTION_PROLOG
  366. mr r4,r12 /* Pass SRR0 as arg2 */
  367. li r5,0 /* Pass zero as arg3 */
  368. EXC_XFER_EE_LITE(0x400, handle_page_fault)
  369. /* 0x0500 - External Interrupt Exception */
  370. EXCEPTION(0x0500, HardwareInterrupt, do_IRQ, EXC_XFER_LITE)
  371. /* 0x0600 - Alignment Exception */
  372. START_EXCEPTION(0x0600, Alignment)
  373. NORMAL_EXCEPTION_PROLOG
  374. mfspr r4,SPRN_DEAR /* Grab the DEAR and save it */
  375. stw r4,_DEAR(r11)
  376. addi r3,r1,STACK_FRAME_OVERHEAD
  377. EXC_XFER_EE(0x600, alignment_exception)
  378. /* 0x0700 - Program Exception */
  379. START_EXCEPTION(0x0700, ProgramCheck)
  380. NORMAL_EXCEPTION_PROLOG
  381. mfspr r4,SPRN_ESR /* Grab the ESR and save it */
  382. stw r4,_ESR(r11)
  383. addi r3,r1,STACK_FRAME_OVERHEAD
  384. EXC_XFER_STD(0x700, program_check_exception)
  385. EXCEPTION(0x0800, Trap_08, unknown_exception, EXC_XFER_EE)
  386. EXCEPTION(0x0900, Trap_09, unknown_exception, EXC_XFER_EE)
  387. EXCEPTION(0x0A00, Trap_0A, unknown_exception, EXC_XFER_EE)
  388. EXCEPTION(0x0B00, Trap_0B, unknown_exception, EXC_XFER_EE)
  389. /* 0x0C00 - System Call Exception */
  390. START_EXCEPTION(0x0C00, SystemCall)
  391. NORMAL_EXCEPTION_PROLOG
  392. EXC_XFER_EE_LITE(0xc00, DoSyscall)
  393. EXCEPTION(0x0D00, Trap_0D, unknown_exception, EXC_XFER_EE)
  394. EXCEPTION(0x0E00, Trap_0E, unknown_exception, EXC_XFER_EE)
  395. EXCEPTION(0x0F00, Trap_0F, unknown_exception, EXC_XFER_EE)
  396. /* 0x1000 - Programmable Interval Timer (PIT) Exception */
  397. START_EXCEPTION(0x1000, Decrementer)
  398. NORMAL_EXCEPTION_PROLOG
  399. lis r0,TSR_PIS@h
  400. mtspr SPRN_TSR,r0 /* Clear the PIT exception */
  401. addi r3,r1,STACK_FRAME_OVERHEAD
  402. EXC_XFER_LITE(0x1000, timer_interrupt)
  403. #if 0
  404. /* NOTE:
  405. * FIT and WDT handlers are not implemented yet.
  406. */
  407. /* 0x1010 - Fixed Interval Timer (FIT) Exception
  408. */
  409. STND_EXCEPTION(0x1010, FITException, unknown_exception)
  410. /* 0x1020 - Watchdog Timer (WDT) Exception
  411. */
  412. #ifdef CONFIG_BOOKE_WDT
  413. CRITICAL_EXCEPTION(0x1020, WDTException, WatchdogException)
  414. #else
  415. CRITICAL_EXCEPTION(0x1020, WDTException, unknown_exception)
  416. #endif
  417. #endif
  418. /* 0x1100 - Data TLB Miss Exception
  419. * As the name implies, translation is not in the MMU, so search the
  420. * page tables and fix it. The only purpose of this function is to
  421. * load TLB entries from the page table if they exist.
  422. */
  423. START_EXCEPTION(0x1100, DTLBMiss)
  424. mtspr SPRN_SPRG_SCRATCH0, r10 /* Save some working registers */
  425. mtspr SPRN_SPRG_SCRATCH1, r11
  426. #ifdef CONFIG_403GCX
  427. stw r12, 0(r0)
  428. stw r9, 4(r0)
  429. mfcr r11
  430. mfspr r12, SPRN_PID
  431. stw r11, 8(r0)
  432. stw r12, 12(r0)
  433. #else
  434. mtspr SPRN_SPRG_SCRATCH3, r12
  435. mtspr SPRN_SPRG_SCRATCH4, r9
  436. mfcr r11
  437. mfspr r12, SPRN_PID
  438. mtspr SPRN_SPRG_SCRATCH6, r11
  439. mtspr SPRN_SPRG_SCRATCH5, r12
  440. #endif
  441. mfspr r10, SPRN_DEAR /* Get faulting address */
  442. /* If we are faulting a kernel address, we have to use the
  443. * kernel page tables.
  444. */
  445. lis r11, PAGE_OFFSET@h
  446. cmplw r10, r11
  447. blt+ 3f
  448. lis r11, swapper_pg_dir@h
  449. ori r11, r11, swapper_pg_dir@l
  450. li r9, 0
  451. mtspr SPRN_PID, r9 /* TLB will have 0 TID */
  452. b 4f
  453. /* Get the PGD for the current thread.
  454. */
  455. 3:
  456. mfspr r11,SPRN_SPRG_THREAD
  457. lwz r11,PGDIR(r11)
  458. 4:
  459. tophys(r11, r11)
  460. rlwimi r11, r10, 12, 20, 29 /* Create L1 (pgdir/pmd) address */
  461. lwz r12, 0(r11) /* Get L1 entry */
  462. andi. r9, r12, _PMD_PRESENT /* Check if it points to a PTE page */
  463. beq 2f /* Bail if no table */
  464. rlwimi r12, r10, 22, 20, 29 /* Compute PTE address */
  465. lwz r11, 0(r12) /* Get Linux PTE */
  466. andi. r9, r11, _PAGE_PRESENT
  467. beq 5f
  468. ori r11, r11, _PAGE_ACCESSED
  469. stw r11, 0(r12)
  470. /* Create TLB tag. This is the faulting address plus a static
  471. * set of bits. These are size, valid, E, U0.
  472. */
  473. li r12, 0x00c0
  474. rlwimi r10, r12, 0, 20, 31
  475. b finish_tlb_load
  476. 2: /* Check for possible large-page pmd entry */
  477. rlwinm. r9, r12, 2, 22, 24
  478. beq 5f
  479. /* Create TLB tag. This is the faulting address, plus a static
  480. * set of bits (valid, E, U0) plus the size from the PMD.
  481. */
  482. ori r9, r9, 0x40
  483. rlwimi r10, r9, 0, 20, 31
  484. mr r11, r12
  485. b finish_tlb_load
  486. 5:
  487. /* The bailout. Restore registers to pre-exception conditions
  488. * and call the heavyweights to help us out.
  489. */
  490. #ifdef CONFIG_403GCX
  491. lwz r12, 12(r0)
  492. lwz r11, 8(r0)
  493. mtspr SPRN_PID, r12
  494. mtcr r11
  495. lwz r9, 4(r0)
  496. lwz r12, 0(r0)
  497. #else
  498. mfspr r12, SPRN_SPRG_SCRATCH5
  499. mfspr r11, SPRN_SPRG_SCRATCH6
  500. mtspr SPRN_PID, r12
  501. mtcr r11
  502. mfspr r9, SPRN_SPRG_SCRATCH4
  503. mfspr r12, SPRN_SPRG_SCRATCH3
  504. #endif
  505. mfspr r11, SPRN_SPRG_SCRATCH1
  506. mfspr r10, SPRN_SPRG_SCRATCH0
  507. b DataAccess
  508. /* 0x1200 - Instruction TLB Miss Exception
  509. * Nearly the same as above, except we get our information from different
  510. * registers and bailout to a different point.
  511. */
  512. START_EXCEPTION(0x1200, ITLBMiss)
  513. mtspr SPRN_SPRG_SCRATCH0, r10 /* Save some working registers */
  514. mtspr SPRN_SPRG_SCRATCH1, r11
  515. #ifdef CONFIG_403GCX
  516. stw r12, 0(r0)
  517. stw r9, 4(r0)
  518. mfcr r11
  519. mfspr r12, SPRN_PID
  520. stw r11, 8(r0)
  521. stw r12, 12(r0)
  522. #else
  523. mtspr SPRN_SPRG_SCRATCH3, r12
  524. mtspr SPRN_SPRG_SCRATCH4, r9
  525. mfcr r11
  526. mfspr r12, SPRN_PID
  527. mtspr SPRN_SPRG_SCRATCH6, r11
  528. mtspr SPRN_SPRG_SCRATCH5, r12
  529. #endif
  530. mfspr r10, SPRN_SRR0 /* Get faulting address */
  531. /* If we are faulting a kernel address, we have to use the
  532. * kernel page tables.
  533. */
  534. lis r11, PAGE_OFFSET@h
  535. cmplw r10, r11
  536. blt+ 3f
  537. lis r11, swapper_pg_dir@h
  538. ori r11, r11, swapper_pg_dir@l
  539. li r9, 0
  540. mtspr SPRN_PID, r9 /* TLB will have 0 TID */
  541. b 4f
  542. /* Get the PGD for the current thread.
  543. */
  544. 3:
  545. mfspr r11,SPRN_SPRG_THREAD
  546. lwz r11,PGDIR(r11)
  547. 4:
  548. tophys(r11, r11)
  549. rlwimi r11, r10, 12, 20, 29 /* Create L1 (pgdir/pmd) address */
  550. lwz r12, 0(r11) /* Get L1 entry */
  551. andi. r9, r12, _PMD_PRESENT /* Check if it points to a PTE page */
  552. beq 2f /* Bail if no table */
  553. rlwimi r12, r10, 22, 20, 29 /* Compute PTE address */
  554. lwz r11, 0(r12) /* Get Linux PTE */
  555. andi. r9, r11, _PAGE_PRESENT
  556. beq 5f
  557. ori r11, r11, _PAGE_ACCESSED
  558. stw r11, 0(r12)
  559. /* Create TLB tag. This is the faulting address plus a static
  560. * set of bits. These are size, valid, E, U0.
  561. */
  562. li r12, 0x00c0
  563. rlwimi r10, r12, 0, 20, 31
  564. b finish_tlb_load
  565. 2: /* Check for possible large-page pmd entry */
  566. rlwinm. r9, r12, 2, 22, 24
  567. beq 5f
  568. /* Create TLB tag. This is the faulting address, plus a static
  569. * set of bits (valid, E, U0) plus the size from the PMD.
  570. */
  571. ori r9, r9, 0x40
  572. rlwimi r10, r9, 0, 20, 31
  573. mr r11, r12
  574. b finish_tlb_load
  575. 5:
  576. /* The bailout. Restore registers to pre-exception conditions
  577. * and call the heavyweights to help us out.
  578. */
  579. #ifdef CONFIG_403GCX
  580. lwz r12, 12(r0)
  581. lwz r11, 8(r0)
  582. mtspr SPRN_PID, r12
  583. mtcr r11
  584. lwz r9, 4(r0)
  585. lwz r12, 0(r0)
  586. #else
  587. mfspr r12, SPRN_SPRG_SCRATCH5
  588. mfspr r11, SPRN_SPRG_SCRATCH6
  589. mtspr SPRN_PID, r12
  590. mtcr r11
  591. mfspr r9, SPRN_SPRG_SCRATCH4
  592. mfspr r12, SPRN_SPRG_SCRATCH3
  593. #endif
  594. mfspr r11, SPRN_SPRG_SCRATCH1
  595. mfspr r10, SPRN_SPRG_SCRATCH0
  596. b InstructionAccess
  597. EXCEPTION(0x1300, Trap_13, unknown_exception, EXC_XFER_EE)
  598. EXCEPTION(0x1400, Trap_14, unknown_exception, EXC_XFER_EE)
  599. EXCEPTION(0x1500, Trap_15, unknown_exception, EXC_XFER_EE)
  600. EXCEPTION(0x1600, Trap_16, unknown_exception, EXC_XFER_EE)
  601. #ifdef CONFIG_IBM405_ERR51
  602. /* 405GP errata 51 */
  603. START_EXCEPTION(0x1700, Trap_17)
  604. b DTLBMiss
  605. #else
  606. EXCEPTION(0x1700, Trap_17, unknown_exception, EXC_XFER_EE)
  607. #endif
  608. EXCEPTION(0x1800, Trap_18, unknown_exception, EXC_XFER_EE)
  609. EXCEPTION(0x1900, Trap_19, unknown_exception, EXC_XFER_EE)
  610. EXCEPTION(0x1A00, Trap_1A, unknown_exception, EXC_XFER_EE)
  611. EXCEPTION(0x1B00, Trap_1B, unknown_exception, EXC_XFER_EE)
  612. EXCEPTION(0x1C00, Trap_1C, unknown_exception, EXC_XFER_EE)
  613. EXCEPTION(0x1D00, Trap_1D, unknown_exception, EXC_XFER_EE)
  614. EXCEPTION(0x1E00, Trap_1E, unknown_exception, EXC_XFER_EE)
  615. EXCEPTION(0x1F00, Trap_1F, unknown_exception, EXC_XFER_EE)
  616. /* Check for a single step debug exception while in an exception
  617. * handler before state has been saved. This is to catch the case
  618. * where an instruction that we are trying to single step causes
  619. * an exception (eg ITLB/DTLB miss) and thus the first instruction of
  620. * the exception handler generates a single step debug exception.
  621. *
  622. * If we get a debug trap on the first instruction of an exception handler,
  623. * we reset the MSR_DE in the _exception handler's_ MSR (the debug trap is
  624. * a critical exception, so we are using SPRN_CSRR1 to manipulate the MSR).
  625. * The exception handler was handling a non-critical interrupt, so it will
  626. * save (and later restore) the MSR via SPRN_SRR1, which will still have
  627. * the MSR_DE bit set.
  628. */
  629. /* 0x2000 - Debug Exception */
  630. START_EXCEPTION(0x2000, DebugTrap)
  631. CRITICAL_EXCEPTION_PROLOG
  632. /*
  633. * If this is a single step or branch-taken exception in an
  634. * exception entry sequence, it was probably meant to apply to
  635. * the code where the exception occurred (since exception entry
  636. * doesn't turn off DE automatically). We simulate the effect
  637. * of turning off DE on entry to an exception handler by turning
  638. * off DE in the SRR3 value and clearing the debug status.
  639. */
  640. mfspr r10,SPRN_DBSR /* check single-step/branch taken */
  641. andis. r10,r10,DBSR_IC@h
  642. beq+ 2f
  643. andi. r10,r9,MSR_IR|MSR_PR /* check supervisor + MMU off */
  644. beq 1f /* branch and fix it up */
  645. mfspr r10,SPRN_SRR2 /* Faulting instruction address */
  646. cmplwi r10,0x2100
  647. bgt+ 2f /* address above exception vectors */
  648. /* here it looks like we got an inappropriate debug exception. */
  649. 1: rlwinm r9,r9,0,~MSR_DE /* clear DE in the SRR3 value */
  650. lis r10,DBSR_IC@h /* clear the IC event */
  651. mtspr SPRN_DBSR,r10
  652. /* restore state and get out */
  653. lwz r10,_CCR(r11)
  654. lwz r0,GPR0(r11)
  655. lwz r1,GPR1(r11)
  656. mtcrf 0x80,r10
  657. mtspr SPRN_SRR2,r12
  658. mtspr SPRN_SRR3,r9
  659. lwz r9,GPR9(r11)
  660. lwz r12,GPR12(r11)
  661. lwz r10,crit_r10@l(0)
  662. lwz r11,crit_r11@l(0)
  663. PPC405_ERR77_SYNC
  664. rfci
  665. b .
  666. /* continue normal handling for a critical exception... */
  667. 2: mfspr r4,SPRN_DBSR
  668. addi r3,r1,STACK_FRAME_OVERHEAD
  669. EXC_XFER_TEMPLATE(DebugException, 0x2002, \
  670. (MSR_KERNEL & ~(MSR_ME|MSR_DE|MSR_CE)), \
  671. NOCOPY, crit_transfer_to_handler, ret_from_crit_exc)
  672. /*
  673. * The other Data TLB exceptions bail out to this point
  674. * if they can't resolve the lightweight TLB fault.
  675. */
  676. DataAccess:
  677. NORMAL_EXCEPTION_PROLOG
  678. mfspr r5,SPRN_ESR /* Grab the ESR, save it, pass arg3 */
  679. stw r5,_ESR(r11)
  680. mfspr r4,SPRN_DEAR /* Grab the DEAR, save it, pass arg2 */
  681. EXC_XFER_EE_LITE(0x300, handle_page_fault)
  682. /* Other PowerPC processors, namely those derived from the 6xx-series
  683. * have vectors from 0x2100 through 0x2F00 defined, but marked as reserved.
  684. * However, for the 4xx-series processors these are neither defined nor
  685. * reserved.
  686. */
  687. /* Damn, I came up one instruction too many to fit into the
  688. * exception space :-). Both the instruction and data TLB
  689. * miss get to this point to load the TLB.
  690. * r10 - TLB_TAG value
  691. * r11 - Linux PTE
  692. * r12, r9 - available to use
  693. * PID - loaded with proper value when we get here
  694. * Upon exit, we reload everything and RFI.
  695. * Actually, it will fit now, but oh well.....a common place
  696. * to load the TLB.
  697. */
  698. tlb_4xx_index:
  699. .long 0
  700. finish_tlb_load:
  701. /* load the next available TLB index.
  702. */
  703. lwz r9, tlb_4xx_index@l(0)
  704. addi r9, r9, 1
  705. andi. r9, r9, (PPC40X_TLB_SIZE-1)
  706. stw r9, tlb_4xx_index@l(0)
  707. 6:
  708. /*
  709. * Clear out the software-only bits in the PTE to generate the
  710. * TLB_DATA value. These are the bottom 2 bits of the RPM, the
  711. * top 3 bits of the zone field, and M.
  712. */
  713. li r12, 0x0ce2
  714. andc r11, r11, r12
  715. tlbwe r11, r9, TLB_DATA /* Load TLB LO */
  716. tlbwe r10, r9, TLB_TAG /* Load TLB HI */
  717. /* Done...restore registers and get out of here.
  718. */
  719. #ifdef CONFIG_403GCX
  720. lwz r12, 12(r0)
  721. lwz r11, 8(r0)
  722. mtspr SPRN_PID, r12
  723. mtcr r11
  724. lwz r9, 4(r0)
  725. lwz r12, 0(r0)
  726. #else
  727. mfspr r12, SPRN_SPRG_SCRATCH5
  728. mfspr r11, SPRN_SPRG_SCRATCH6
  729. mtspr SPRN_PID, r12
  730. mtcr r11
  731. mfspr r9, SPRN_SPRG_SCRATCH4
  732. mfspr r12, SPRN_SPRG_SCRATCH3
  733. #endif
  734. mfspr r11, SPRN_SPRG_SCRATCH1
  735. mfspr r10, SPRN_SPRG_SCRATCH0
  736. PPC405_ERR77_SYNC
  737. rfi /* Should sync shadow TLBs */
  738. b . /* prevent prefetch past rfi */
  739. /* extern void giveup_fpu(struct task_struct *prev)
  740. *
  741. * The PowerPC 4xx family of processors do not have an FPU, so this just
  742. * returns.
  743. */
  744. _ENTRY(giveup_fpu)
  745. blr
  746. /* This is where the main kernel code starts.
  747. */
  748. start_here:
  749. /* ptr to current */
  750. lis r2,init_task@h
  751. ori r2,r2,init_task@l
  752. /* ptr to phys current thread */
  753. tophys(r4,r2)
  754. addi r4,r4,THREAD /* init task's THREAD */
  755. mtspr SPRN_SPRG_THREAD,r4
  756. /* stack */
  757. lis r1,init_thread_union@ha
  758. addi r1,r1,init_thread_union@l
  759. li r0,0
  760. stwu r0,THREAD_SIZE-STACK_FRAME_OVERHEAD(r1)
  761. bl early_init /* We have to do this with MMU on */
  762. /*
  763. * Decide what sort of machine this is and initialize the MMU.
  764. */
  765. mr r3,r31
  766. mr r4,r30
  767. mr r5,r29
  768. mr r6,r28
  769. mr r7,r27
  770. bl machine_init
  771. bl MMU_init
  772. /* Go back to running unmapped so we can load up new values
  773. * and change to using our exception vectors.
  774. * On the 4xx, all we have to do is invalidate the TLB to clear
  775. * the old 16M byte TLB mappings.
  776. */
  777. lis r4,2f@h
  778. ori r4,r4,2f@l
  779. tophys(r4,r4)
  780. lis r3,(MSR_KERNEL & ~(MSR_IR|MSR_DR))@h
  781. ori r3,r3,(MSR_KERNEL & ~(MSR_IR|MSR_DR))@l
  782. mtspr SPRN_SRR0,r4
  783. mtspr SPRN_SRR1,r3
  784. rfi
  785. b . /* prevent prefetch past rfi */
  786. /* Load up the kernel context */
  787. 2:
  788. sync /* Flush to memory before changing TLB */
  789. tlbia
  790. isync /* Flush shadow TLBs */
  791. /* set up the PTE pointers for the Abatron bdiGDB.
  792. */
  793. lis r6, swapper_pg_dir@h
  794. ori r6, r6, swapper_pg_dir@l
  795. lis r5, abatron_pteptrs@h
  796. ori r5, r5, abatron_pteptrs@l
  797. stw r5, 0xf0(r0) /* Must match your Abatron config file */
  798. tophys(r5,r5)
  799. stw r6, 0(r5)
  800. /* Now turn on the MMU for real! */
  801. lis r4,MSR_KERNEL@h
  802. ori r4,r4,MSR_KERNEL@l
  803. lis r3,start_kernel@h
  804. ori r3,r3,start_kernel@l
  805. mtspr SPRN_SRR0,r3
  806. mtspr SPRN_SRR1,r4
  807. rfi /* enable MMU and jump to start_kernel */
  808. b . /* prevent prefetch past rfi */
  809. /* Set up the initial MMU state so we can do the first level of
  810. * kernel initialization. This maps the first 16 MBytes of memory 1:1
  811. * virtual to physical and more importantly sets the cache mode.
  812. */
  813. initial_mmu:
  814. tlbia /* Invalidate all TLB entries */
  815. isync
  816. /* We should still be executing code at physical address 0x0000xxxx
  817. * at this point. However, start_here is at virtual address
  818. * 0xC000xxxx. So, set up a TLB mapping to cover this once
  819. * translation is enabled.
  820. */
  821. lis r3,KERNELBASE@h /* Load the kernel virtual address */
  822. ori r3,r3,KERNELBASE@l
  823. tophys(r4,r3) /* Load the kernel physical address */
  824. iccci r0,r3 /* Invalidate the i-cache before use */
  825. /* Load the kernel PID.
  826. */
  827. li r0,0
  828. mtspr SPRN_PID,r0
  829. sync
  830. /* Configure and load one entry into TLB slots 63 */
  831. clrrwi r4,r4,10 /* Mask off the real page number */
  832. ori r4,r4,(TLB_WR | TLB_EX) /* Set the write and execute bits */
  833. clrrwi r3,r3,10 /* Mask off the effective page number */
  834. ori r3,r3,(TLB_VALID | TLB_PAGESZ(PAGESZ_16M))
  835. li r0,63 /* TLB slot 63 */
  836. tlbwe r4,r0,TLB_DATA /* Load the data portion of the entry */
  837. tlbwe r3,r0,TLB_TAG /* Load the tag portion of the entry */
  838. #if defined(CONFIG_SERIAL_TEXT_DEBUG) && defined(SERIAL_DEBUG_IO_BASE)
  839. /* Load a TLB entry for the UART, so that ppc4xx_progress() can use
  840. * the UARTs nice and early. We use a 4k real==virtual mapping. */
  841. lis r3,SERIAL_DEBUG_IO_BASE@h
  842. ori r3,r3,SERIAL_DEBUG_IO_BASE@l
  843. mr r4,r3
  844. clrrwi r4,r4,12
  845. ori r4,r4,(TLB_WR|TLB_I|TLB_M|TLB_G)
  846. clrrwi r3,r3,12
  847. ori r3,r3,(TLB_VALID | TLB_PAGESZ(PAGESZ_4K))
  848. li r0,0 /* TLB slot 0 */
  849. tlbwe r4,r0,TLB_DATA
  850. tlbwe r3,r0,TLB_TAG
  851. #endif /* CONFIG_SERIAL_DEBUG_TEXT && SERIAL_DEBUG_IO_BASE */
  852. isync
  853. /* Establish the exception vector base
  854. */
  855. lis r4,KERNELBASE@h /* EVPR only uses the high 16-bits */
  856. tophys(r0,r4) /* Use the physical address */
  857. mtspr SPRN_EVPR,r0
  858. blr
  859. _GLOBAL(abort)
  860. mfspr r13,SPRN_DBCR0
  861. oris r13,r13,DBCR0_RST_SYSTEM@h
  862. mtspr SPRN_DBCR0,r13
  863. _GLOBAL(set_context)
  864. #ifdef CONFIG_BDI_SWITCH
  865. /* Context switch the PTE pointer for the Abatron BDI2000.
  866. * The PGDIR is the second parameter.
  867. */
  868. lis r5, KERNELBASE@h
  869. lwz r5, 0xf0(r5)
  870. stw r4, 0x4(r5)
  871. #endif
  872. sync
  873. mtspr SPRN_PID,r3
  874. isync /* Need an isync to flush shadow */
  875. /* TLBs after changing PID */
  876. blr
  877. /* We put a few things here that have to be page-aligned. This stuff
  878. * goes at the beginning of the data segment, which is page-aligned.
  879. */
  880. .data
  881. .align 12
  882. .globl sdata
  883. sdata:
  884. .globl empty_zero_page
  885. empty_zero_page:
  886. .space 4096
  887. .globl swapper_pg_dir
  888. swapper_pg_dir:
  889. .space PGD_TABLE_SIZE
  890. /* Room for two PTE pointers, usually the kernel and current user pointers
  891. * to their respective root page table.
  892. */
  893. abatron_pteptrs:
  894. .space 8