entry.S 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  1. /*
  2. * OpenRISC entry.S
  3. *
  4. * Linux architectural port borrowing liberally from similar works of
  5. * others. All original copyrights apply as per the original source
  6. * declaration.
  7. *
  8. * Modifications for the OpenRISC architecture:
  9. * Copyright (C) 2003 Matjaz Breskvar <phoenix@bsemi.com>
  10. * Copyright (C) 2005 Gyorgy Jeney <nog@bsemi.com>
  11. * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se>
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License
  15. * as published by the Free Software Foundation; either version
  16. * 2 of the License, or (at your option) any later version.
  17. */
  18. #include <linux/linkage.h>
  19. #include <asm/processor.h>
  20. #include <asm/unistd.h>
  21. #include <asm/thread_info.h>
  22. #include <asm/errno.h>
  23. #include <asm/spr_defs.h>
  24. #include <asm/page.h>
  25. #include <asm/mmu.h>
  26. #include <asm/pgtable.h>
  27. #include <asm/asm-offsets.h>
  28. #define DISABLE_INTERRUPTS(t1,t2) \
  29. l.mfspr t2,r0,SPR_SR ;\
  30. l.movhi t1,hi(~(SPR_SR_IEE|SPR_SR_TEE)) ;\
  31. l.ori t1,t1,lo(~(SPR_SR_IEE|SPR_SR_TEE)) ;\
  32. l.and t2,t2,t1 ;\
  33. l.mtspr r0,t2,SPR_SR
  34. #define ENABLE_INTERRUPTS(t1) \
  35. l.mfspr t1,r0,SPR_SR ;\
  36. l.ori t1,t1,lo(SPR_SR_IEE|SPR_SR_TEE) ;\
  37. l.mtspr r0,t1,SPR_SR
  38. /* =========================================================[ macros ]=== */
  39. /*
  40. * We need to disable interrupts at beginning of RESTORE_ALL
  41. * since interrupt might come in after we've loaded EPC return address
  42. * and overwrite EPC with address somewhere in RESTORE_ALL
  43. * which is of course wrong!
  44. */
  45. #define RESTORE_ALL \
  46. DISABLE_INTERRUPTS(r3,r4) ;\
  47. l.lwz r3,PT_PC(r1) ;\
  48. l.mtspr r0,r3,SPR_EPCR_BASE ;\
  49. l.lwz r3,PT_SR(r1) ;\
  50. l.mtspr r0,r3,SPR_ESR_BASE ;\
  51. l.lwz r2,PT_GPR2(r1) ;\
  52. l.lwz r3,PT_GPR3(r1) ;\
  53. l.lwz r4,PT_GPR4(r1) ;\
  54. l.lwz r5,PT_GPR5(r1) ;\
  55. l.lwz r6,PT_GPR6(r1) ;\
  56. l.lwz r7,PT_GPR7(r1) ;\
  57. l.lwz r8,PT_GPR8(r1) ;\
  58. l.lwz r9,PT_GPR9(r1) ;\
  59. l.lwz r10,PT_GPR10(r1) ;\
  60. l.lwz r11,PT_GPR11(r1) ;\
  61. l.lwz r12,PT_GPR12(r1) ;\
  62. l.lwz r13,PT_GPR13(r1) ;\
  63. l.lwz r14,PT_GPR14(r1) ;\
  64. l.lwz r15,PT_GPR15(r1) ;\
  65. l.lwz r16,PT_GPR16(r1) ;\
  66. l.lwz r17,PT_GPR17(r1) ;\
  67. l.lwz r18,PT_GPR18(r1) ;\
  68. l.lwz r19,PT_GPR19(r1) ;\
  69. l.lwz r20,PT_GPR20(r1) ;\
  70. l.lwz r21,PT_GPR21(r1) ;\
  71. l.lwz r22,PT_GPR22(r1) ;\
  72. l.lwz r23,PT_GPR23(r1) ;\
  73. l.lwz r24,PT_GPR24(r1) ;\
  74. l.lwz r25,PT_GPR25(r1) ;\
  75. l.lwz r26,PT_GPR26(r1) ;\
  76. l.lwz r27,PT_GPR27(r1) ;\
  77. l.lwz r28,PT_GPR28(r1) ;\
  78. l.lwz r29,PT_GPR29(r1) ;\
  79. l.lwz r30,PT_GPR30(r1) ;\
  80. l.lwz r31,PT_GPR31(r1) ;\
  81. l.lwz r1,PT_SP(r1) ;\
  82. l.rfe
  83. #define EXCEPTION_ENTRY(handler) \
  84. .global handler ;\
  85. handler: ;\
  86. /* r1, EPCR, ESR a already saved */ ;\
  87. l.sw PT_GPR2(r1),r2 ;\
  88. l.sw PT_GPR3(r1),r3 ;\
  89. /* r4 already save */ ;\
  90. l.sw PT_GPR5(r1),r5 ;\
  91. l.sw PT_GPR6(r1),r6 ;\
  92. l.sw PT_GPR7(r1),r7 ;\
  93. l.sw PT_GPR8(r1),r8 ;\
  94. l.sw PT_GPR9(r1),r9 ;\
  95. /* r10 already saved */ ;\
  96. l.sw PT_GPR11(r1),r11 ;\
  97. /* r12 already saved */ ;\
  98. l.sw PT_GPR13(r1),r13 ;\
  99. l.sw PT_GPR14(r1),r14 ;\
  100. l.sw PT_GPR15(r1),r15 ;\
  101. l.sw PT_GPR16(r1),r16 ;\
  102. l.sw PT_GPR17(r1),r17 ;\
  103. l.sw PT_GPR18(r1),r18 ;\
  104. l.sw PT_GPR19(r1),r19 ;\
  105. l.sw PT_GPR20(r1),r20 ;\
  106. l.sw PT_GPR21(r1),r21 ;\
  107. l.sw PT_GPR22(r1),r22 ;\
  108. l.sw PT_GPR23(r1),r23 ;\
  109. l.sw PT_GPR24(r1),r24 ;\
  110. l.sw PT_GPR25(r1),r25 ;\
  111. l.sw PT_GPR26(r1),r26 ;\
  112. l.sw PT_GPR27(r1),r27 ;\
  113. l.sw PT_GPR28(r1),r28 ;\
  114. l.sw PT_GPR29(r1),r29 ;\
  115. /* r30 already save */ ;\
  116. /* l.sw PT_GPR30(r1),r30*/ ;\
  117. l.sw PT_GPR31(r1),r31 ;\
  118. /* Store -1 in orig_gpr11 for non-syscall exceptions */ ;\
  119. l.addi r30,r0,-1 ;\
  120. l.sw PT_ORIG_GPR11(r1),r30
  121. #define UNHANDLED_EXCEPTION(handler,vector) \
  122. .global handler ;\
  123. handler: ;\
  124. /* r1, EPCR, ESR already saved */ ;\
  125. l.sw PT_GPR2(r1),r2 ;\
  126. l.sw PT_GPR3(r1),r3 ;\
  127. l.sw PT_GPR5(r1),r5 ;\
  128. l.sw PT_GPR6(r1),r6 ;\
  129. l.sw PT_GPR7(r1),r7 ;\
  130. l.sw PT_GPR8(r1),r8 ;\
  131. l.sw PT_GPR9(r1),r9 ;\
  132. /* r10 already saved */ ;\
  133. l.sw PT_GPR11(r1),r11 ;\
  134. /* r12 already saved */ ;\
  135. l.sw PT_GPR13(r1),r13 ;\
  136. l.sw PT_GPR14(r1),r14 ;\
  137. l.sw PT_GPR15(r1),r15 ;\
  138. l.sw PT_GPR16(r1),r16 ;\
  139. l.sw PT_GPR17(r1),r17 ;\
  140. l.sw PT_GPR18(r1),r18 ;\
  141. l.sw PT_GPR19(r1),r19 ;\
  142. l.sw PT_GPR20(r1),r20 ;\
  143. l.sw PT_GPR21(r1),r21 ;\
  144. l.sw PT_GPR22(r1),r22 ;\
  145. l.sw PT_GPR23(r1),r23 ;\
  146. l.sw PT_GPR24(r1),r24 ;\
  147. l.sw PT_GPR25(r1),r25 ;\
  148. l.sw PT_GPR26(r1),r26 ;\
  149. l.sw PT_GPR27(r1),r27 ;\
  150. l.sw PT_GPR28(r1),r28 ;\
  151. l.sw PT_GPR29(r1),r29 ;\
  152. /* r31 already saved */ ;\
  153. l.sw PT_GPR30(r1),r30 ;\
  154. /* l.sw PT_GPR31(r1),r31 */ ;\
  155. /* Store -1 in orig_gpr11 for non-syscall exceptions */ ;\
  156. l.addi r30,r0,-1 ;\
  157. l.sw PT_ORIG_GPR11(r1),r30 ;\
  158. l.addi r3,r1,0 ;\
  159. /* r4 is exception EA */ ;\
  160. l.addi r5,r0,vector ;\
  161. l.jal unhandled_exception ;\
  162. l.nop ;\
  163. l.j _ret_from_exception ;\
  164. l.nop
  165. /*
  166. * NOTE: one should never assume that SPR_EPC, SPR_ESR, SPR_EEAR
  167. * contain the same values as when exception we're handling
  168. * occured. in fact they never do. if you need them use
  169. * values saved on stack (for SPR_EPC, SPR_ESR) or content
  170. * of r4 (for SPR_EEAR). for details look at EXCEPTION_HANDLE()
  171. * in 'arch/or32/kernel/head.S'
  172. */
  173. /* =====================================================[ exceptions] === */
  174. /* ---[ 0x100: RESET exception ]----------------------------------------- */
  175. EXCEPTION_ENTRY(_tng_kernel_start)
  176. l.jal _start
  177. l.andi r0,r0,0
  178. /* ---[ 0x200: BUS exception ]------------------------------------------- */
  179. EXCEPTION_ENTRY(_bus_fault_handler)
  180. /* r4: EA of fault (set by EXCEPTION_HANDLE) */
  181. l.jal do_bus_fault
  182. l.addi r3,r1,0 /* pt_regs */
  183. l.j _ret_from_exception
  184. l.nop
  185. /* ---[ 0x300: Data Page Fault exception ]------------------------------- */
  186. EXCEPTION_ENTRY(_dtlb_miss_page_fault_handler)
  187. l.and r5,r5,r0
  188. l.j 1f
  189. l.nop
  190. EXCEPTION_ENTRY(_data_page_fault_handler)
  191. /* set up parameters for do_page_fault */
  192. l.ori r5,r0,0x300 // exception vector
  193. 1:
  194. l.addi r3,r1,0 // pt_regs
  195. /* r4 set be EXCEPTION_HANDLE */ // effective address of fault
  196. /*
  197. * __PHX__: TODO
  198. *
  199. * all this can be written much simpler. look at
  200. * DTLB miss handler in the CONFIG_GUARD_PROTECTED_CORE part
  201. */
  202. #ifdef CONFIG_OPENRISC_NO_SPR_SR_DSX
  203. l.lwz r6,PT_PC(r3) // address of an offending insn
  204. l.lwz r6,0(r6) // instruction that caused pf
  205. l.srli r6,r6,26 // check opcode for jump insn
  206. l.sfeqi r6,0 // l.j
  207. l.bf 8f
  208. l.sfeqi r6,1 // l.jal
  209. l.bf 8f
  210. l.sfeqi r6,3 // l.bnf
  211. l.bf 8f
  212. l.sfeqi r6,4 // l.bf
  213. l.bf 8f
  214. l.sfeqi r6,0x11 // l.jr
  215. l.bf 8f
  216. l.sfeqi r6,0x12 // l.jalr
  217. l.bf 8f
  218. l.nop
  219. l.j 9f
  220. l.nop
  221. 8:
  222. l.lwz r6,PT_PC(r3) // address of an offending insn
  223. l.addi r6,r6,4
  224. l.lwz r6,0(r6) // instruction that caused pf
  225. l.srli r6,r6,26 // get opcode
  226. 9:
  227. #else
  228. l.mfspr r6,r0,SPR_SR // SR
  229. // l.lwz r6,PT_SR(r3) // ESR
  230. l.andi r6,r6,SPR_SR_DSX // check for delay slot exception
  231. l.sfeqi r6,0x1 // exception happened in delay slot
  232. l.bnf 7f
  233. l.lwz r6,PT_PC(r3) // address of an offending insn
  234. l.addi r6,r6,4 // offending insn is in delay slot
  235. 7:
  236. l.lwz r6,0(r6) // instruction that caused pf
  237. l.srli r6,r6,26 // check opcode for write access
  238. #endif
  239. l.sfgeui r6,0x34 // check opcode for write access
  240. l.bnf 1f
  241. l.sfleui r6,0x37
  242. l.bnf 1f
  243. l.ori r6,r0,0x1 // write access
  244. l.j 2f
  245. l.nop
  246. 1: l.ori r6,r0,0x0 // !write access
  247. 2:
  248. /* call fault.c handler in or32/mm/fault.c */
  249. l.jal do_page_fault
  250. l.nop
  251. l.j _ret_from_exception
  252. l.nop
  253. /* ---[ 0x400: Insn Page Fault exception ]------------------------------- */
  254. EXCEPTION_ENTRY(_itlb_miss_page_fault_handler)
  255. l.and r5,r5,r0
  256. l.j 1f
  257. l.nop
  258. EXCEPTION_ENTRY(_insn_page_fault_handler)
  259. /* set up parameters for do_page_fault */
  260. l.ori r5,r0,0x400 // exception vector
  261. 1:
  262. l.addi r3,r1,0 // pt_regs
  263. /* r4 set be EXCEPTION_HANDLE */ // effective address of fault
  264. l.ori r6,r0,0x0 // !write access
  265. /* call fault.c handler in or32/mm/fault.c */
  266. l.jal do_page_fault
  267. l.nop
  268. l.j _ret_from_exception
  269. l.nop
  270. /* ---[ 0x500: Timer exception ]----------------------------------------- */
  271. EXCEPTION_ENTRY(_timer_handler)
  272. l.jal timer_interrupt
  273. l.addi r3,r1,0 /* pt_regs */
  274. l.j _ret_from_intr
  275. l.nop
  276. /* ---[ 0x600: Aligment exception ]-------------------------------------- */
  277. EXCEPTION_ENTRY(_alignment_handler)
  278. /* r4: EA of fault (set by EXCEPTION_HANDLE) */
  279. l.jal do_unaligned_access
  280. l.addi r3,r1,0 /* pt_regs */
  281. l.j _ret_from_exception
  282. l.nop
  283. #if 0
  284. EXCEPTION_ENTRY(_aligment_handler)
  285. // l.mfspr r2,r0,SPR_EEAR_BASE /* Load the efective addres */
  286. l.addi r2,r4,0
  287. // l.mfspr r5,r0,SPR_EPCR_BASE /* Load the insn address */
  288. l.lwz r5,PT_PC(r1)
  289. l.lwz r3,0(r5) /* Load insn */
  290. l.srli r4,r3,26 /* Shift left to get the insn opcode */
  291. l.sfeqi r4,0x00 /* Check if the load/store insn is in delay slot */
  292. l.bf jmp
  293. l.sfeqi r4,0x01
  294. l.bf jmp
  295. l.sfeqi r4,0x03
  296. l.bf jmp
  297. l.sfeqi r4,0x04
  298. l.bf jmp
  299. l.sfeqi r4,0x11
  300. l.bf jr
  301. l.sfeqi r4,0x12
  302. l.bf jr
  303. l.nop
  304. l.j 1f
  305. l.addi r5,r5,4 /* Increment PC to get return insn address */
  306. jmp:
  307. l.slli r4,r3,6 /* Get the signed extended jump length */
  308. l.srai r4,r4,4
  309. l.lwz r3,4(r5) /* Load the real load/store insn */
  310. l.add r5,r5,r4 /* Calculate jump target address */
  311. l.j 1f
  312. l.srli r4,r3,26 /* Shift left to get the insn opcode */
  313. jr:
  314. l.slli r4,r3,9 /* Shift to get the reg nb */
  315. l.andi r4,r4,0x7c
  316. l.lwz r3,4(r5) /* Load the real load/store insn */
  317. l.add r4,r4,r1 /* Load the jump register value from the stack */
  318. l.lwz r5,0(r4)
  319. l.srli r4,r3,26 /* Shift left to get the insn opcode */
  320. 1:
  321. // l.mtspr r0,r5,SPR_EPCR_BASE
  322. l.sw PT_PC(r1),r5
  323. l.sfeqi r4,0x26
  324. l.bf lhs
  325. l.sfeqi r4,0x25
  326. l.bf lhz
  327. l.sfeqi r4,0x22
  328. l.bf lws
  329. l.sfeqi r4,0x21
  330. l.bf lwz
  331. l.sfeqi r4,0x37
  332. l.bf sh
  333. l.sfeqi r4,0x35
  334. l.bf sw
  335. l.nop
  336. 1: l.j 1b /* I don't know what to do */
  337. l.nop
  338. lhs: l.lbs r5,0(r2)
  339. l.slli r5,r5,8
  340. l.lbz r6,1(r2)
  341. l.or r5,r5,r6
  342. l.srli r4,r3,19
  343. l.andi r4,r4,0x7c
  344. l.add r4,r4,r1
  345. l.j align_end
  346. l.sw 0(r4),r5
  347. lhz: l.lbz r5,0(r2)
  348. l.slli r5,r5,8
  349. l.lbz r6,1(r2)
  350. l.or r5,r5,r6
  351. l.srli r4,r3,19
  352. l.andi r4,r4,0x7c
  353. l.add r4,r4,r1
  354. l.j align_end
  355. l.sw 0(r4),r5
  356. lws: l.lbs r5,0(r2)
  357. l.slli r5,r5,24
  358. l.lbz r6,1(r2)
  359. l.slli r6,r6,16
  360. l.or r5,r5,r6
  361. l.lbz r6,2(r2)
  362. l.slli r6,r6,8
  363. l.or r5,r5,r6
  364. l.lbz r6,3(r2)
  365. l.or r5,r5,r6
  366. l.srli r4,r3,19
  367. l.andi r4,r4,0x7c
  368. l.add r4,r4,r1
  369. l.j align_end
  370. l.sw 0(r4),r5
  371. lwz: l.lbz r5,0(r2)
  372. l.slli r5,r5,24
  373. l.lbz r6,1(r2)
  374. l.slli r6,r6,16
  375. l.or r5,r5,r6
  376. l.lbz r6,2(r2)
  377. l.slli r6,r6,8
  378. l.or r5,r5,r6
  379. l.lbz r6,3(r2)
  380. l.or r5,r5,r6
  381. l.srli r4,r3,19
  382. l.andi r4,r4,0x7c
  383. l.add r4,r4,r1
  384. l.j align_end
  385. l.sw 0(r4),r5
  386. sh:
  387. l.srli r4,r3,9
  388. l.andi r4,r4,0x7c
  389. l.add r4,r4,r1
  390. l.lwz r5,0(r4)
  391. l.sb 1(r2),r5
  392. l.srli r5,r5,8
  393. l.j align_end
  394. l.sb 0(r2),r5
  395. sw:
  396. l.srli r4,r3,9
  397. l.andi r4,r4,0x7c
  398. l.add r4,r4,r1
  399. l.lwz r5,0(r4)
  400. l.sb 3(r2),r5
  401. l.srli r5,r5,8
  402. l.sb 2(r2),r5
  403. l.srli r5,r5,8
  404. l.sb 1(r2),r5
  405. l.srli r5,r5,8
  406. l.j align_end
  407. l.sb 0(r2),r5
  408. align_end:
  409. l.j _ret_from_intr
  410. l.nop
  411. #endif
  412. /* ---[ 0x700: Illegal insn exception ]---------------------------------- */
  413. EXCEPTION_ENTRY(_illegal_instruction_handler)
  414. /* r4: EA of fault (set by EXCEPTION_HANDLE) */
  415. l.jal do_illegal_instruction
  416. l.addi r3,r1,0 /* pt_regs */
  417. l.j _ret_from_exception
  418. l.nop
  419. /* ---[ 0x800: External interrupt exception ]---------------------------- */
  420. EXCEPTION_ENTRY(_external_irq_handler)
  421. #ifdef CONFIG_OPENRISC_ESR_EXCEPTION_BUG_CHECK
  422. l.lwz r4,PT_SR(r1) // were interrupts enabled ?
  423. l.andi r4,r4,SPR_SR_IEE
  424. l.sfeqi r4,0
  425. l.bnf 1f // ext irq enabled, all ok.
  426. l.nop
  427. l.addi r1,r1,-0x8
  428. l.movhi r3,hi(42f)
  429. l.ori r3,r3,lo(42f)
  430. l.sw 0x0(r1),r3
  431. l.jal printk
  432. l.sw 0x4(r1),r4
  433. l.addi r1,r1,0x8
  434. .section .rodata, "a"
  435. 42:
  436. .string "\n\rESR interrupt bug: in _external_irq_handler (ESR %x)\n\r"
  437. .align 4
  438. .previous
  439. l.ori r4,r4,SPR_SR_IEE // fix the bug
  440. // l.sw PT_SR(r1),r4
  441. 1:
  442. #endif
  443. l.addi r3,r1,0
  444. l.movhi r8,hi(do_IRQ)
  445. l.ori r8,r8,lo(do_IRQ)
  446. l.jalr r8
  447. l.nop
  448. l.j _ret_from_intr
  449. l.nop
  450. /* ---[ 0x900: DTLB miss exception ]------------------------------------- */
  451. /* ---[ 0xa00: ITLB miss exception ]------------------------------------- */
  452. /* ---[ 0xb00: Range exception ]----------------------------------------- */
  453. UNHANDLED_EXCEPTION(_vector_0xb00,0xb00)
  454. /* ---[ 0xc00: Syscall exception ]--------------------------------------- */
  455. /*
  456. * Syscalls are a special type of exception in that they are
  457. * _explicitly_ invoked by userspace and can therefore be
  458. * held to conform to the same ABI as normal functions with
  459. * respect to whether registers are preserved across the call
  460. * or not.
  461. */
  462. /* Upon syscall entry we just save the callee-saved registers
  463. * and not the call-clobbered ones.
  464. */
  465. _string_syscall_return:
  466. .string "syscall return %ld \n\r\0"
  467. .align 4
  468. ENTRY(_sys_call_handler)
  469. /* syscalls run with interrupts enabled */
  470. ENABLE_INTERRUPTS(r29) // enable interrupts, r29 is temp
  471. /* r1, EPCR, ESR a already saved */
  472. l.sw PT_GPR2(r1),r2
  473. /* r3-r8 must be saved because syscall restart relies
  474. * on us being able to restart the syscall args... technically
  475. * they should be clobbered, otherwise
  476. */
  477. l.sw PT_GPR3(r1),r3
  478. /* r4 already saved */
  479. /* r4 holds the EEAR address of the fault, load the original r4 */
  480. l.lwz r4,PT_GPR4(r1)
  481. l.sw PT_GPR5(r1),r5
  482. l.sw PT_GPR6(r1),r6
  483. l.sw PT_GPR7(r1),r7
  484. l.sw PT_GPR8(r1),r8
  485. l.sw PT_GPR9(r1),r9
  486. /* r10 already saved */
  487. l.sw PT_GPR11(r1),r11
  488. /* orig_gpr11 must be set for syscalls */
  489. l.sw PT_ORIG_GPR11(r1),r11
  490. /* r12,r13 already saved */
  491. /* r14-r28 (even) aren't touched by the syscall fast path below
  492. * so we don't need to save them. However, the functions that return
  493. * to userspace via a call to switch() DO need to save these because
  494. * switch() effectively clobbers them... saving these registers for
  495. * such functions is handled in their syscall wrappers (see fork, vfork,
  496. * and clone, below).
  497. /* r30 is the only register we clobber in the fast path */
  498. /* r30 already saved */
  499. /* l.sw PT_GPR30(r1),r30 */
  500. _syscall_check_trace_enter:
  501. /* If TIF_SYSCALL_TRACE is set, then we want to do syscall tracing */
  502. l.lwz r30,TI_FLAGS(r10)
  503. l.andi r30,r30,_TIF_SYSCALL_TRACE
  504. l.sfne r30,r0
  505. l.bf _syscall_trace_enter
  506. l.nop
  507. _syscall_check:
  508. /* Ensure that the syscall number is reasonable */
  509. l.sfgeui r11,__NR_syscalls
  510. l.bf _syscall_badsys
  511. l.nop
  512. _syscall_call:
  513. l.movhi r29,hi(sys_call_table)
  514. l.ori r29,r29,lo(sys_call_table)
  515. l.slli r11,r11,2
  516. l.add r29,r29,r11
  517. l.lwz r29,0(r29)
  518. l.jalr r29
  519. l.nop
  520. _syscall_return:
  521. /* All syscalls return here... just pay attention to ret_from_fork
  522. * which does it in a round-about way.
  523. */
  524. l.sw PT_GPR11(r1),r11 // save return value
  525. #if 0
  526. _syscall_debug:
  527. l.movhi r3,hi(_string_syscall_return)
  528. l.ori r3,r3,lo(_string_syscall_return)
  529. l.ori r27,r0,1
  530. l.sw -4(r1),r27
  531. l.sw -8(r1),r11
  532. l.addi r1,r1,-8
  533. l.movhi r27,hi(printk)
  534. l.ori r27,r27,lo(printk)
  535. l.jalr r27
  536. l.nop
  537. l.addi r1,r1,8
  538. #endif
  539. _syscall_check_trace_leave:
  540. /* r30 is a callee-saved register so this should still hold the
  541. * _TIF_SYSCALL_TRACE flag from _syscall_check_trace_enter above...
  542. * _syscall_trace_leave expects syscall result to be in pt_regs->r11.
  543. */
  544. l.sfne r30,r0
  545. l.bf _syscall_trace_leave
  546. l.nop
  547. /* This is where the exception-return code begins... interrupts need to be
  548. * disabled the rest of the way here because we can't afford to miss any
  549. * interrupts that set NEED_RESCHED or SIGNALPENDING... really true? */
  550. _syscall_check_work:
  551. /* Here we need to disable interrupts */
  552. DISABLE_INTERRUPTS(r27,r29)
  553. l.lwz r30,TI_FLAGS(r10)
  554. l.andi r30,r30,_TIF_WORK_MASK
  555. l.sfne r30,r0
  556. l.bnf _syscall_resume_userspace
  557. l.nop
  558. /* Work pending follows a different return path, so we need to
  559. * make sure that all the call-saved registers get into pt_regs
  560. * before branching...
  561. */
  562. l.sw PT_GPR14(r1),r14
  563. l.sw PT_GPR16(r1),r16
  564. l.sw PT_GPR18(r1),r18
  565. l.sw PT_GPR20(r1),r20
  566. l.sw PT_GPR22(r1),r22
  567. l.sw PT_GPR24(r1),r24
  568. l.sw PT_GPR26(r1),r26
  569. l.sw PT_GPR28(r1),r28
  570. /* _work_pending needs to be called with interrupts disabled */
  571. l.j _work_pending
  572. l.nop
  573. _syscall_resume_userspace:
  574. // ENABLE_INTERRUPTS(r29)
  575. /* This is the hot path for returning to userspace from a syscall. If there's
  576. * work to be done and the branch to _work_pending was taken above, then the
  577. * return to userspace will be done via the normal exception return path...
  578. * that path restores _all_ registers and will overwrite the "clobbered"
  579. * registers with whatever garbage is in pt_regs -- that's OK because those
  580. * registers are clobbered anyway and because the extra work is insignificant
  581. * in the context of the extra work that _work_pending is doing.
  582. /* Once again, syscalls are special and only guarantee to preserve the
  583. * same registers as a normal function call */
  584. /* The assumption here is that the registers r14-r28 (even) are untouched and
  585. * don't need to be restored... be sure that that's really the case!
  586. */
  587. /* This is still too much... we should only be restoring what we actually
  588. * clobbered... we should even be using 'scratch' (odd) regs above so that
  589. * we don't need to restore anything, hardly...
  590. */
  591. l.lwz r2,PT_GPR2(r1)
  592. /* Restore args */
  593. /* r3-r8 are technically clobbered, but syscall restart needs these
  594. * to be restored...
  595. */
  596. l.lwz r3,PT_GPR3(r1)
  597. l.lwz r4,PT_GPR4(r1)
  598. l.lwz r5,PT_GPR5(r1)
  599. l.lwz r6,PT_GPR6(r1)
  600. l.lwz r7,PT_GPR7(r1)
  601. l.lwz r8,PT_GPR8(r1)
  602. l.lwz r9,PT_GPR9(r1)
  603. l.lwz r10,PT_GPR10(r1)
  604. l.lwz r11,PT_GPR11(r1)
  605. /* r30 is the only register we clobber in the fast path */
  606. l.lwz r30,PT_GPR30(r1)
  607. /* Here we use r13-r19 (odd) as scratch regs */
  608. l.lwz r13,PT_PC(r1)
  609. l.lwz r15,PT_SR(r1)
  610. l.lwz r1,PT_SP(r1)
  611. /* Interrupts need to be disabled for setting EPCR and ESR
  612. * so that another interrupt doesn't come in here and clobber
  613. * them before we can use them for our l.rfe */
  614. DISABLE_INTERRUPTS(r17,r19)
  615. l.mtspr r0,r13,SPR_EPCR_BASE
  616. l.mtspr r0,r15,SPR_ESR_BASE
  617. l.rfe
  618. /* End of hot path!
  619. * Keep the below tracing and error handling out of the hot path...
  620. */
  621. _syscall_trace_enter:
  622. /* Here we pass pt_regs to do_syscall_trace_enter. Make sure
  623. * that function is really getting all the info it needs as
  624. * pt_regs isn't a complete set of userspace regs, just the
  625. * ones relevant to the syscall...
  626. *
  627. * Note use of delay slot for setting argument.
  628. */
  629. l.jal do_syscall_trace_enter
  630. l.addi r3,r1,0
  631. /* Restore arguments (not preserved across do_syscall_trace_enter)
  632. * so that we can do the syscall for real and return to the syscall
  633. * hot path.
  634. */
  635. l.lwz r11,PT_GPR11(r1)
  636. l.lwz r3,PT_GPR3(r1)
  637. l.lwz r4,PT_GPR4(r1)
  638. l.lwz r5,PT_GPR5(r1)
  639. l.lwz r6,PT_GPR6(r1)
  640. l.lwz r7,PT_GPR7(r1)
  641. l.j _syscall_check
  642. l.lwz r8,PT_GPR8(r1)
  643. _syscall_trace_leave:
  644. l.jal do_syscall_trace_leave
  645. l.addi r3,r1,0
  646. l.j _syscall_check_work
  647. l.nop
  648. _syscall_badsys:
  649. /* Here we effectively pretend to have executed an imaginary
  650. * syscall that returns -ENOSYS and then return to the regular
  651. * syscall hot path.
  652. * Note that "return value" is set in the delay slot...
  653. */
  654. l.j _syscall_return
  655. l.addi r11,r0,-ENOSYS
  656. /******* END SYSCALL HANDLING *******/
  657. /* ---[ 0xd00: Trap exception ]------------------------------------------ */
  658. UNHANDLED_EXCEPTION(_vector_0xd00,0xd00)
  659. /* ---[ 0xe00: Trap exception ]------------------------------------------ */
  660. EXCEPTION_ENTRY(_trap_handler)
  661. /* r4: EA of fault (set by EXCEPTION_HANDLE) */
  662. l.jal do_trap
  663. l.addi r3,r1,0 /* pt_regs */
  664. l.j _ret_from_exception
  665. l.nop
  666. /* ---[ 0xf00: Reserved exception ]-------------------------------------- */
  667. UNHANDLED_EXCEPTION(_vector_0xf00,0xf00)
  668. /* ---[ 0x1000: Reserved exception ]------------------------------------- */
  669. UNHANDLED_EXCEPTION(_vector_0x1000,0x1000)
  670. /* ---[ 0x1100: Reserved exception ]------------------------------------- */
  671. UNHANDLED_EXCEPTION(_vector_0x1100,0x1100)
  672. /* ---[ 0x1200: Reserved exception ]------------------------------------- */
  673. UNHANDLED_EXCEPTION(_vector_0x1200,0x1200)
  674. /* ---[ 0x1300: Reserved exception ]------------------------------------- */
  675. UNHANDLED_EXCEPTION(_vector_0x1300,0x1300)
  676. /* ---[ 0x1400: Reserved exception ]------------------------------------- */
  677. UNHANDLED_EXCEPTION(_vector_0x1400,0x1400)
  678. /* ---[ 0x1500: Reserved exception ]------------------------------------- */
  679. UNHANDLED_EXCEPTION(_vector_0x1500,0x1500)
  680. /* ---[ 0x1600: Reserved exception ]------------------------------------- */
  681. UNHANDLED_EXCEPTION(_vector_0x1600,0x1600)
  682. /* ---[ 0x1700: Reserved exception ]------------------------------------- */
  683. UNHANDLED_EXCEPTION(_vector_0x1700,0x1700)
  684. /* ---[ 0x1800: Reserved exception ]------------------------------------- */
  685. UNHANDLED_EXCEPTION(_vector_0x1800,0x1800)
  686. /* ---[ 0x1900: Reserved exception ]------------------------------------- */
  687. UNHANDLED_EXCEPTION(_vector_0x1900,0x1900)
  688. /* ---[ 0x1a00: Reserved exception ]------------------------------------- */
  689. UNHANDLED_EXCEPTION(_vector_0x1a00,0x1a00)
  690. /* ---[ 0x1b00: Reserved exception ]------------------------------------- */
  691. UNHANDLED_EXCEPTION(_vector_0x1b00,0x1b00)
  692. /* ---[ 0x1c00: Reserved exception ]------------------------------------- */
  693. UNHANDLED_EXCEPTION(_vector_0x1c00,0x1c00)
  694. /* ---[ 0x1d00: Reserved exception ]------------------------------------- */
  695. UNHANDLED_EXCEPTION(_vector_0x1d00,0x1d00)
  696. /* ---[ 0x1e00: Reserved exception ]------------------------------------- */
  697. UNHANDLED_EXCEPTION(_vector_0x1e00,0x1e00)
  698. /* ---[ 0x1f00: Reserved exception ]------------------------------------- */
  699. UNHANDLED_EXCEPTION(_vector_0x1f00,0x1f00)
  700. /* ========================================================[ return ] === */
  701. _resume_userspace:
  702. DISABLE_INTERRUPTS(r3,r4)
  703. l.lwz r4,TI_FLAGS(r10)
  704. l.andi r13,r4,_TIF_WORK_MASK
  705. l.sfeqi r13,0
  706. l.bf _restore_all
  707. l.nop
  708. _work_pending:
  709. l.lwz r5,PT_ORIG_GPR11(r1)
  710. l.sfltsi r5,0
  711. l.bnf 1f
  712. l.nop
  713. l.andi r5,r5,0
  714. 1:
  715. l.jal do_work_pending
  716. l.ori r3,r1,0 /* pt_regs */
  717. l.sfeqi r11,0
  718. l.bf _restore_all
  719. l.nop
  720. l.sfltsi r11,0
  721. l.bnf 1f
  722. l.nop
  723. l.and r11,r11,r0
  724. l.ori r11,r11,__NR_restart_syscall
  725. l.j _syscall_check_trace_enter
  726. l.nop
  727. 1:
  728. l.lwz r11,PT_ORIG_GPR11(r1)
  729. /* Restore arg registers */
  730. l.lwz r3,PT_GPR3(r1)
  731. l.lwz r4,PT_GPR4(r1)
  732. l.lwz r5,PT_GPR5(r1)
  733. l.lwz r6,PT_GPR6(r1)
  734. l.lwz r7,PT_GPR7(r1)
  735. l.j _syscall_check_trace_enter
  736. l.lwz r8,PT_GPR8(r1)
  737. _restore_all:
  738. RESTORE_ALL
  739. /* This returns to userspace code */
  740. ENTRY(_ret_from_intr)
  741. ENTRY(_ret_from_exception)
  742. l.lwz r4,PT_SR(r1)
  743. l.andi r3,r4,SPR_SR_SM
  744. l.sfeqi r3,0
  745. l.bnf _restore_all
  746. l.nop
  747. l.j _resume_userspace
  748. l.nop
  749. ENTRY(ret_from_fork)
  750. l.jal schedule_tail
  751. l.nop
  752. /* Check if we are a kernel thread */
  753. l.sfeqi r20,0
  754. l.bf 1f
  755. l.nop
  756. /* ...we are a kernel thread so invoke the requested callback */
  757. l.jalr r20
  758. l.or r3,r22,r0
  759. 1:
  760. /* _syscall_returns expect r11 to contain return value */
  761. l.lwz r11,PT_GPR11(r1)
  762. /* The syscall fast path return expects call-saved registers
  763. * r12-r28 to be untouched, so we restore them here as they
  764. * will have been effectively clobbered when arriving here
  765. * via the call to switch()
  766. */
  767. l.lwz r12,PT_GPR12(r1)
  768. l.lwz r14,PT_GPR14(r1)
  769. l.lwz r16,PT_GPR16(r1)
  770. l.lwz r18,PT_GPR18(r1)
  771. l.lwz r20,PT_GPR20(r1)
  772. l.lwz r22,PT_GPR22(r1)
  773. l.lwz r24,PT_GPR24(r1)
  774. l.lwz r26,PT_GPR26(r1)
  775. l.lwz r28,PT_GPR28(r1)
  776. l.j _syscall_return
  777. l.nop
  778. /* ========================================================[ switch ] === */
  779. /*
  780. * This routine switches between two different tasks. The process
  781. * state of one is saved on its kernel stack. Then the state
  782. * of the other is restored from its kernel stack. The memory
  783. * management hardware is updated to the second process's state.
  784. * Finally, we can return to the second process, via the 'return'.
  785. *
  786. * Note: there are two ways to get to the "going out" portion
  787. * of this code; either by coming in via the entry (_switch)
  788. * or via "fork" which must set up an environment equivalent
  789. * to the "_switch" path. If you change this (or in particular, the
  790. * SAVE_REGS macro), you'll have to change the fork code also.
  791. */
  792. /* _switch MUST never lay on page boundry, cause it runs from
  793. * effective addresses and beeing interrupted by iTLB miss would kill it.
  794. * dTLB miss seams to never accour in the bad place since data accesses
  795. * are from task structures which are always page aligned.
  796. *
  797. * The problem happens in RESTORE_ALL_NO_R11 where we first set the EPCR
  798. * register, then load the previous register values and only at the end call
  799. * the l.rfe instruction. If get TLB miss in beetwen the EPCR register gets
  800. * garbled and we end up calling l.rfe with the wrong EPCR. (same probably
  801. * holds for ESR)
  802. *
  803. * To avoid this problems it is sufficient to align _switch to
  804. * some nice round number smaller than it's size...
  805. */
  806. /* ABI rules apply here... we either enter _switch via schedule() or via
  807. * an imaginary call to which we shall return at return_from_fork. Either
  808. * way, we are a function call and only need to preserve the callee-saved
  809. * registers when we return. As such, we don't need to save the registers
  810. * on the stack that we won't be returning as they were...
  811. */
  812. .align 0x400
  813. ENTRY(_switch)
  814. /* We don't store SR as _switch only gets called in a context where
  815. * the SR will be the same going in and coming out... */
  816. /* Set up new pt_regs struct for saving task state */
  817. l.addi r1,r1,-(INT_FRAME_SIZE)
  818. /* No need to store r1/PT_SP as it goes into KSP below */
  819. l.sw PT_GPR2(r1),r2
  820. l.sw PT_GPR9(r1),r9
  821. /* This is wrong, r12 shouldn't be here... but GCC is broken for the time being
  822. * and expects r12 to be callee-saved... */
  823. l.sw PT_GPR12(r1),r12
  824. l.sw PT_GPR14(r1),r14
  825. l.sw PT_GPR16(r1),r16
  826. l.sw PT_GPR18(r1),r18
  827. l.sw PT_GPR20(r1),r20
  828. l.sw PT_GPR22(r1),r22
  829. l.sw PT_GPR24(r1),r24
  830. l.sw PT_GPR26(r1),r26
  831. l.sw PT_GPR28(r1),r28
  832. l.sw PT_GPR30(r1),r30
  833. l.addi r11,r10,0 /* Save old 'current' to 'last' return value*/
  834. /* We use thread_info->ksp for storing the address of the above
  835. * structure so that we can get back to it later... we don't want
  836. * to lose the value of thread_info->ksp, though, so store it as
  837. * pt_regs->sp so that we can easily restore it when we are made
  838. * live again...
  839. */
  840. /* Save the old value of thread_info->ksp as pt_regs->sp */
  841. l.lwz r29,TI_KSP(r10)
  842. l.sw PT_SP(r1),r29
  843. /* Swap kernel stack pointers */
  844. l.sw TI_KSP(r10),r1 /* Save old stack pointer */
  845. l.or r10,r4,r0 /* Set up new current_thread_info */
  846. l.lwz r1,TI_KSP(r10) /* Load new stack pointer */
  847. /* Restore the old value of thread_info->ksp */
  848. l.lwz r29,PT_SP(r1)
  849. l.sw TI_KSP(r10),r29
  850. /* ...and restore the registers, except r11 because the return value
  851. * has already been set above.
  852. */
  853. l.lwz r2,PT_GPR2(r1)
  854. l.lwz r9,PT_GPR9(r1)
  855. /* No need to restore r10 */
  856. /* ...and do not restore r11 */
  857. /* This is wrong, r12 shouldn't be here... but GCC is broken for the time being
  858. * and expects r12 to be callee-saved... */
  859. l.lwz r12,PT_GPR12(r1)
  860. l.lwz r14,PT_GPR14(r1)
  861. l.lwz r16,PT_GPR16(r1)
  862. l.lwz r18,PT_GPR18(r1)
  863. l.lwz r20,PT_GPR20(r1)
  864. l.lwz r22,PT_GPR22(r1)
  865. l.lwz r24,PT_GPR24(r1)
  866. l.lwz r26,PT_GPR26(r1)
  867. l.lwz r28,PT_GPR28(r1)
  868. l.lwz r30,PT_GPR30(r1)
  869. /* Unwind stack to pre-switch state */
  870. l.addi r1,r1,(INT_FRAME_SIZE)
  871. /* Return via the link-register back to where we 'came from', where
  872. * that may be either schedule(), ret_from_fork(), or
  873. * ret_from_kernel_thread(). If we are returning to a new thread,
  874. * we are expected to have set up the arg to schedule_tail already,
  875. * hence we do so here unconditionally:
  876. */
  877. l.lwz r3,TI_TASK(r3) /* Load 'prev' as schedule_tail arg */
  878. l.jr r9
  879. l.nop
  880. /* ==================================================================== */
  881. /* These all use the delay slot for setting the argument register, so the
  882. * jump is always happening after the l.addi instruction.
  883. *
  884. * These are all just wrappers that don't touch the link-register r9, so the
  885. * return from the "real" syscall function will return back to the syscall
  886. * code that did the l.jal that brought us here.
  887. */
  888. /* fork requires that we save all the callee-saved registers because they
  889. * are all effectively clobbered by the call to _switch. Here we store
  890. * all the registers that aren't touched by the syscall fast path and thus
  891. * weren't saved there.
  892. */
  893. _fork_save_extra_regs_and_call:
  894. l.sw PT_GPR14(r1),r14
  895. l.sw PT_GPR16(r1),r16
  896. l.sw PT_GPR18(r1),r18
  897. l.sw PT_GPR20(r1),r20
  898. l.sw PT_GPR22(r1),r22
  899. l.sw PT_GPR24(r1),r24
  900. l.sw PT_GPR26(r1),r26
  901. l.jr r29
  902. l.sw PT_GPR28(r1),r28
  903. ENTRY(__sys_clone)
  904. l.movhi r29,hi(sys_clone)
  905. l.ori r29,r29,lo(sys_clone)
  906. l.j _fork_save_extra_regs_and_call
  907. l.addi r7,r1,0
  908. ENTRY(__sys_fork)
  909. l.movhi r29,hi(sys_fork)
  910. l.ori r29,r29,lo(sys_fork)
  911. l.j _fork_save_extra_regs_and_call
  912. l.addi r3,r1,0
  913. ENTRY(sys_rt_sigreturn)
  914. l.j _sys_rt_sigreturn
  915. l.addi r3,r1,0
  916. /* This is a catch-all syscall for atomic instructions for the OpenRISC 1000.
  917. * The functions takes a variable number of parameters depending on which
  918. * particular flavour of atomic you want... parameter 1 is a flag identifying
  919. * the atomic in question. Currently, this function implements the
  920. * following variants:
  921. *
  922. * XCHG:
  923. * @flag: 1
  924. * @ptr1:
  925. * @ptr2:
  926. * Atomically exchange the values in pointers 1 and 2.
  927. *
  928. */
  929. ENTRY(sys_or1k_atomic)
  930. /* FIXME: This ignores r3 and always does an XCHG */
  931. DISABLE_INTERRUPTS(r17,r19)
  932. l.lwz r29,0(r4)
  933. l.lwz r27,0(r5)
  934. l.sw 0(r4),r27
  935. l.sw 0(r5),r29
  936. ENABLE_INTERRUPTS(r17)
  937. l.jr r9
  938. l.or r11,r0,r0
  939. /* ============================================================[ EOF ]=== */