Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1495924
  • 博文数量: 267
  • 博客积分: 3010
  • 博客等级: 少校
  • 技术积分: 3089
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-05 17:09
个人简介

尊天命,尽人事

文章分类

全部博文(267)

文章存档

2017年(6)

2015年(4)

2014年(27)

2013年(52)

2012年(59)

2011年(120)

分类: 嵌入式

2011-10-19 21:45:30

  1. /*
  2. * linux/arch/arm/boot/compressed/head.S
  3. *
  4. * Copyright (C) 1996-2002 Russell King
  5. * Copyright (C) 2004 Hyok S. Choi (MPU support)
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include
  12. /*
  13. * Debugging stuff 定义了一些marco
  14. *
  15. * Note that these macros must not contain any code which is not
  16. * 100% relocatable. Any attempt to do so will result in a crash.
  17. * Please select one of the following when turning on debugging.
  18. */
  19. #ifdef DEBUG

  20. #if defined(CONFIG_DEBUG_ICEDCC)

  21. #ifdef CONFIG_CPU_V6
  22. .macro loadsp, rb
  23. .endm
  24. .macro writeb, ch, rb
  25. mcr p14, 0, /ch, c0, c5, 0
  26. .endm

  27. #else

  28. .macro loadsp, rb
  29. .endm
  30. .macro writeb, ch, rb
  31. mcr p14, 0, /ch, c1, c0, 0
  32. .endm

  33. #endif

  34. #else

  35. #include

  36. .macro writeb, ch, rb
  37. senduart /ch, /rb
  38. .endm

  39. #if defined(CONFIG_ARCH_SA1100)

  40. .macro loadsp, rb
  41. mov /rb, #0x80000000 @ physical base address

  42. #ifdef CONFIG_DEBUG_LL_SER3
  43. add /rb, /rb, #0x00050000 @ Ser3
  44. #else
  45. add /rb, /rb, #0x00010000 @ Ser1
  46. #endif
  47. .endm
  48. #elif defined(CONFIG_ARCH_S3C2410)
  49. .macro loadsp, rb
  50. mov /rb, #0x50000000
  51. add /rb, /rb, #0x4000 * CONFIG_S3C_LOWLEVEL_UART_PORT
  52. .endm
  53. #else
  54. .macro loadsp, rb
  55. addruart /rb
  56. .endm
  57. #endif
  58. #endif
  59. #endif
  60. .macro kputc,val
  61. mov r0, /val
  62. bl putc
  63. .endm
  64. .macro kphex,val,len
  65. mov r0, /val
  66. mov r1, #/len
  67. bl phex
  68. .endm
  69. .macro debug_reloc_start
  70. #ifdef DEBUG
  71. kputc #'/n'
  72. kphex r6, 8 /* processor id */
  73. kputc #':'
  74. kphex r7, 8 /* architecture id */
  75. #ifdef CONFIG_CPU_CP15
  76. kputc #':'
  77. mrc p15, 0, r0, c1, c0
  78. kphex r0, 8 /* control reg */
  79. #endif
  80. kputc #'/n'
  81. kphex r5, 8 /* decompressed kernel start */
  82. kputc #'-'
  83. kphex r9, 8 /* decompressed kernel end */
  84. kputc #'>'
  85. kphex r4, 8 /* kernel execution address */
  86. kputc #'/n'
  87. #endif
  88. .endm
  89. .macro debug_reloc_end
  90. #ifdef DEBUG
  91. kphex r5, 8 /* end of kernel */
  92. kputc #'/n'
  93. mov r0, r4
  94. bl memdump /* dump 256 bytes at start of kernel */
  95. #endif
  96. .endm
  97. .section ".start", #alloc, #execinstr
  98. /*
  99. * sort out different calling conventions 真正代码的开始,之前都是一些宏定义
  100. */
  101. .align
  102. start:
  103. .type start,#function
  104. .rept 8
  105. mov r0, r0
  106. .endr
  107. b 1f
  108. .word 0x016f2818 @ Magic numbers to help the loader 这个应该是uboot判断是不是zImage的magic number
  109. .word start @ absolute load/run zImage address
  110. .word _edata @ zImage end address
  111. 1: mov r7, r1 @ save architecture ID
  112. mov r8, r2 @ save atags pointer
  113. #ifndef __ARM_ARCH_2__
  114. /*
  115. * Booting from Angel - need to enter SVC mode and disable
  116. * FIQs/IRQs (numeric definitions from angel arm.h source).
  117. * We only do this if we were in user mode on entry.
  118. */
  119. mrs r2, cpsr @ get current mode 将cpsr保存到r2
  120. tst r2, #3 @ not user? r2 & 3,cpsr的最后两位为0表示在user mode
  121. bne not_angel @ 不是0,即不是user mode, 则跳到 not_angel
  122. mov r0, #0x17 @ angel_SWIreason_EnterSVC 这个r0?
  123. swi 0x123456 @ angel_SWI_ARM 这个主要是设置CPSR,进入Supervisor Mode
  124. not_angel:
  125. mrs r2, cpsr @ turn off interrupts to 再取一次CPSR
  126. orr r2, r2, #0xc0 @ prevent angel from running 设置I F bit,Disable IRQ/FIQ interrupt
  127. msr cpsr_c, r2 @ 写回CPSR
  128. #else
  129. not go here
  130. teqp pc, #0x0c000003 @ turn off interrupts
  131. #endif
  132. /*
  133. * Note that some cache flushing and other stuff may
  134. * be needed here - is there an Angel SWI call for this?
  135. */
  136. /*
  137. * some architecture specific code can be inserted
  138. * by the linker here, but it should preserve r7, r8, and r9.
  139. */
  140. .text
  141. adr r0, LC0 @将LC0的地址存到r0
  142. ldmia r0, {r1, r2, r3, r4, r5, r6, ip, sp} @ 将r0所指的地址的值依次存入寄存器列表中 (升序)
  143. subs r0, r0, r1 @ calculate the delta offset r0 = r0 - r1 , 这是什么的delta?
  144. @ if delta is zero, we are
  145. beq not_relocated @ running at the address we
  146. @ were linked at.
  147. /*
  148. * We're running at a different address. We need to fix
  149. * up various pointers:
  150. * r5 - zImage base address
  151. * r6 - GOT start
  152. * ip - GOT end
  153. */
  154. add r5, r5, r0
  155. add r6, r6, r0
  156. add ip, ip, r0
  157. #ifndef CONFIG_ZBOOT_ROM
  158. /*
  159. * If we're running fully PIC === CONFIG_ZBOOT_ROM = n,
  160. * we need to fix up pointers into the BSS region.
  161. * r2 - BSS start
  162. * r3 - BSS end
  163. * sp - stack pointer
  164. */
  165. add r2, r2, r0
  166. add r3, r3, r0
  167. add sp, sp, r0
  168. /*
  169. * Relocate all entries in the GOT table.
  170. */
  171. 1: ldr r1, [r6, #0] @ relocate entries in the GOT
  172. add r1, r1, r0 @ table. This fixes up the
  173. str r1, [r6], #4 @ C references.
  174. cmp r6, ip
  175. blo 1b
  176. #else
  177. not go here
  178. /*
  179. * Relocate entries in the GOT table. We only relocate
  180. * the entries that are outside the (relocated) BSS region.
  181. */
  182. 1: ldr r1, [r6, #0] @ relocate entries in the GOT
  183. cmp r1, r2 @ entry < bss_start ||
  184. cmphs r3, r1 @ _end < entry
  185. addlo r1, r1, r0 @ table. This fixes up the
  186. str r1, [r6], #4 @ C references.
  187. cmp r6, ip
  188. blo 1b
  189. #endif
  190. not_relocated: mov r0, #0 @不用relocate
  191. 1: str r0, [r2], #4 @ clear bss 写0到r2指向的地方。 r2指向了__bss_start ,
  192. str r0, [r2], #4 @ 做了四次 ?
  193. str r0, [r2], #4
  194. str r0, [r2], #4
  195. cmp r2, r3
  196. blo 1b
  197. /*
  198. * The C runtime environment should now be setup
  199. * sufficiently. Turn the cache on, set up some
  200. * pointers, and start decompressing.
  201. */
  202. bl cache_on
  203. mov r1, sp @ malloc space above stack r1 = sp;
  204. add r2, sp, #0x10000 @ 64k max r2 = sp + 64k ;
  205. /*
  206. * Check to see if we will overwrite ourselves.
  207. * r4 = final kernel address
  208. * r5 = start of this image
  209. * r2 = end of malloc space (and therefore this image)
  210. * We basically want:
  211. * r4 >= r2 -> OK
  212. * r4 + image length <= r5 -> OK
  213. */
  214. cmp r4, r2 @ r4 - r2
  215. bhs wont_overwrite @ r4 >= r2 空间足够
  216. sub r3, sp, r5 @ > compressed kernel size
  217. add r0, r4, r3, lsl #2 @ allow for 4x expansion
  218. cmp r0, r5
  219. bls wont_overwrite
  220. mov r5, r2 @ decompress after malloc space
  221. mov r0, r5
  222. mov r3, r7
  223. bl decompress_kernel
  224. add r0, r0, #127 + 128 @ alignment + stack
  225. bic r0, r0, #127 @ align the kernel length
  226. /*
  227. * r0 = decompressed kernel length
  228. * r1-r3 = unused
  229. * r4 = kernel execution address
  230. * r5 = decompressed kernel start
  231. * r6 = processor ID
  232. * r7 = architecture ID
  233. * r8 = atags pointer
  234. * r9-r14 = corrupted
  235. */
  236. add r1, r5, r0 @ end of decompressed kernel
  237. adr r2, reloc_start
  238. ldr r3, LC1
  239. add r3, r2, r3
  240. 1: ldmia r2!, {r9 - r14} @ copy relocation code
  241. stmia r1!, {r9 - r14}
  242. ldmia r2!, {r9 - r14}
  243. stmia r1!, {r9 - r14}
  244. cmp r2, r3
  245. blo 1b
  246. add sp, r1, #128 @ relocate the stack
  247. bl cache_clean_flush
  248. add pc, r5, r0 @ call relocation code
  249. /*
  250. * We're not in danger of overwriting ourselves. Do this the simple way.
  251. *
  252. * r4 = kernel execution address
  253. * r7 = architecture ID
  254. */
  255. wont_overwrite: mov r0, r4 @ r0 = r4 = kernel execution address
  256. mov r3, r7 @ r3 = r7 = architecture ID
  257. bl decompress_kernel @传入参数为,r0是要解压到的地址,r1是当前的sp,r2是sp+64k,所以r1-r2就是分配的空闲空间
  258. b call_kernel @进入kernel
  259. .type LC0, #object
  260. LC0: .word LC0 @ r1
  261. .word __bss_start @ r2
  262. .word _end @ r3
  263. .word zreladdr @ r4
  264. .word _start @ r5
  265. .word _got_start @ r6
  266. .word _got_end @ ip
  267. .word user_stack+4096 @ sp
  268. LC1: .word reloc_end - reloc_start
  269. .size LC0, . - LC0
  270. #ifdef CONFIG_ARCH_RPC
  271. not go here
  272. .globl params
  273. params: ldr r0, =params_phys
  274. mov pc, lr
  275. .ltorg
  276. .align
  277. #endif
  278. /*
  279. * Turn on the cache. We need to setup some page tables so that we
  280. * can have both the I and D caches on.
  281. *
  282. * We place the page tables 16k down from the kernel execution address,
  283. * and we hope that nothing else is using it. If we're using it, we
  284. * will go pop!
  285. *
  286. * On entry,
  287. * r4 = kernel execution address
  288. * r6 = processor ID
  289. * r7 = architecture number
  290. * r8 = atags pointer
  291. * r9 = run-time address of "start" (???)
  292. * On exit,
  293. * r1, r2, r3, r9, r10, r12 corrupted
  294. * This routine must preserve:
  295. * r4, r5, r6, r7, r8
  296. */
  297. .align 5
  298. cache_on: mov r3, #8 @ cache_on function
  299. b call_cache_fn
  300. /*
  301. * Initialize the highest priority protection region, PR7
  302. * to cover all 32bit address and cacheable and bufferable.
  303. */
  304. __armv4_mpu_cache_on:
  305. mov r0, #0x3f @ 4G, the whole
  306. mcr p15, 0, r0, c6, c7, 0 @ PR7 Area Setting
  307. mcr p15, 0, r0, c6, c7, 1
  308. mov r0, #0x80 @ PR7
  309. mcr p15, 0, r0, c2, c0, 0 @ D-cache on
  310. mcr p15, 0, r0, c2, c0, 1 @ I-cache on
  311. mcr p15, 0, r0, c3, c0, 0 @ write-buffer on
  312. mov r0, #0xc000
  313. mcr p15, 0, r0, c5, c0, 1 @ I-access permission
  314. mcr p15, 0, r0, c5, c0, 0 @ D-access permission
  315. mov r0, #0
  316. mcr p15, 0, r0, c7, c10, 4 @ drain write buffer
  317. mcr p15, 0, r0, c7, c5, 0 @ flush(inval) I-Cache
  318. mcr p15, 0, r0, c7, c6, 0 @ flush(inval) D-Cache
  319. mrc p15, 0, r0, c1, c0, 0 @ read control reg
  320. @ ...I .... ..D. WC.M
  321. orr r0, r0, #0x002d @ .... .... ..1. 11.1
  322. orr r0, r0, #0x1000 @ ...1 .... .... ....
  323. mcr p15, 0, r0, c1, c0, 0 @ write control reg
  324. mov r0, #0
  325. mcr p15, 0, r0, c7, c5, 0 @ flush(inval) I-Cache
  326. mcr p15, 0, r0, c7, c6, 0 @ flush(inval) D-Cache
  327. mov pc, lr
  328. __armv3_mpu_cache_on:
  329. mov r0, #0x3f @ 4G, the whole
  330. mcr p15, 0, r0, c6, c7, 0 @ PR7 Area Setting
  331. mov r0, #0x80 @ PR7
  332. mcr p15, 0, r0, c2, c0, 0 @ cache on
  333. mcr p15, 0, r0, c3, c0, 0 @ write-buffer on
  334. mov r0, #0xc000
  335. mcr p15, 0, r0, c5, c0, 0 @ access permission
  336. mov r0, #0
  337. mcr p15, 0, r0, c7, c0, 0 @ invalidate whole cache v3
  338. mrc p15, 0, r0, c1, c0, 0 @ read control reg
  339. @ .... .... .... WC.M
  340. orr r0, r0, #0x000d @ .... .... .... 11.1
  341. mov r0, #0
  342. mcr p15, 0, r0, c1, c0, 0 @ write control reg
  343. mcr p15, 0, r0, c7, c0, 0 @ invalidate whole cache v3
  344. mov pc, lr
  345. __setup_mmu: sub r3, r4, #16384 @ Page directory size
  346. bic r3, r3, #0xff @ Align the pointer
  347. bic r3, r3, #0x3f00
  348. /*
  349. * Initialise the page tables, turning on the cacheable and bufferable
  350. * bits for the RAM area only.
  351. */
  352. mov r0, r3
  353. mov r9, r0, lsr #18
  354. mov r9, r9, lsl #18 @ start of RAM
  355. add r10, r9, #0x10000000 @ a reasonable RAM size
  356. mov r1, #0x12
  357. orr r1, r1, #3 << 10
  358. add r2, r3, #16384
  359. 1: cmp r1, r9 @ if virt > start of RAM
  360. orrhs r1, r1, #0x0c @ set cacheable, bufferable
  361. cmp r1, r10 @ if virt > end of RAM
  362. bichs r1, r1, #0x0c @ clear cacheable, bufferable
  363. str r1, [r0], #4 @ 1:1 mapping
  364. add r1, r1, #1048576
  365. teq r0, r2
  366. bne 1b
  367. /*
  368. * If ever we are running from Flash, then we surely want the cache
  369. * to be enabled also for our execution instance... We map 2MB of it
  370. * so there is no map overlap problem for up to 1 MB compressed kernel.
  371. * If the execution is in RAM then we would only be duplicating the above.
  372. */
  373. mov r1, #0x1e
  374. orr r1, r1, #3 << 10
  375. mov r2, pc, lsr #20
  376. orr r1, r1, r2, lsl #20
  377. add r0, r3, r2, lsl #2
  378. str r1, [r0], #4
  379. add r1, r1, #1048576
  380. str r1, [r0]
  381. mov pc, lr
  382. ENDPROC(__setup_mmu)
  383. __armv4_mmu_cache_on:
  384. mov r12, lr
  385. bl __setup_mmu
  386. mov r0, #0
  387. mcr p15, 0, r0, c7, c10, 4 @ drain write buffer
  388. mcr p15, 0, r0, c8, c7, 0 @ flush I,D TLBs
  389. mrc p15, 0, r0, c1, c0, 0 @ read control reg
  390. orr r0, r0, #0x5000 @ I-cache enable, RR cache replacement
  391. orr r0, r0, #0x0030
  392. bl __common_mmu_cache_on
  393. mov r0, #0
  394. mcr p15, 0, r0, c8, c7, 0 @ flush I,D TLBs
  395. mov pc, r12
  396. __armv7_mmu_cache_on:
  397. mov r12, lr
  398. mrc p15, 0, r11, c0, c1, 4 @ read ID_MMFR0
  399. tst r11, #0xf @ VMSA
  400. blne __setup_mmu
  401. mov r0, #0
  402. mcr p15, 0, r0, c7, c10, 4 @ drain write buffer
  403. tst r11, #0xf @ VMSA
  404. mcrne p15, 0, r0, c8, c7, 0 @ flush I,D TLBs
  405. mrc p15, 0, r0, c1, c0, 0 @ read control reg
  406. orr r0, r0, #0x5000 @ I-cache enable, RR cache replacement
  407. orr r0, r0, #0x003c @ write buffer
  408. orrne r0, r0, #1 @ MMU enabled
  409. movne r1, #-1
  410. mcrne p15, 0, r3, c2, c0, 0 @ load page table pointer
  411. mcrne p15, 0, r1, c3, c0, 0 @ load domain access control
  412. mcr p15, 0, r0, c1, c0, 0 @ load control register
  413. mrc p15, 0, r0, c1, c0, 0 @ and read it back
  414. mov r0, #0
  415. mcr p15, 0, r0, c7, c5, 4 @ ISB
  416. mov pc, r12
  417. __arm6_mmu_cache_on:
  418. mov r12, lr
  419. bl __setup_mmu
  420. mov r0, #0
  421. mcr p15, 0, r0, c7, c0, 0 @ invalidate whole cache v3
  422. mcr p15, 0, r0, c5, c0, 0 @ invalidate whole TLB v3
  423. mov r0, #0x30
  424. bl __common_mmu_cache_on
  425. mov r0, #0
  426. mcr p15, 0, r0, c5, c0, 0 @ invalidate whole TLB v3
  427. mov pc, r12
  428. __common_mmu_cache_on:
  429. #ifndef DEBUG
  430. orr r0, r0, #0x000d @ Write buffer, mmu
  431. #endif
  432. mov r1, #-1
  433. mcr p15, 0, r3, c2, c0, 0 @ load page table pointer
  434. mcr p15, 0, r1, c3, c0, 0 @ load domain access control
  435. b 1f
  436. .align 5 @ cache line aligned
  437. 1: mcr p15, 0, r0, c1, c0, 0 @ load control register
  438. mrc p15, 0, r0, c1, c0, 0 @ and read it back to
  439. sub pc, lr, r0, lsr #32 @ properly flush pipeline
  440. /*
  441. * All code following this line is relocatable. It is relocated by
  442. * the above code to the end of the decompressed kernel image and
  443. * executed there. During this time, we have no stacks.
  444. *
  445. * r0 = decompressed kernel length
  446. * r1-r3 = unused
  447. * r4 = kernel execution address
  448. * r5 = decompressed kernel start
  449. * r6 = processor ID
  450. * r7 = architecture ID
  451. * r8 = atags pointer
  452. * r9-r14 = corrupted
  453. */
  454. .align 5
  455. reloc_start: add r9, r5, r0
  456. sub r9, r9, #128 @ do not copy the stack
  457. debug_reloc_start
  458. mov r1, r4
  459. 1:
  460. .rept 4
  461. ldmia r5!, {r0, r2, r3, r10 - r14} @ relocate kernel
  462. stmia r1!, {r0, r2, r3, r10 - r14}
  463. .endr
  464. cmp r5, r9
  465. blo 1b
  466. add sp, r1, #128 @ relocate the stack
  467. debug_reloc_end
  468. call_kernel: bl cache_clean_flush @ clean?
  469. bl cache_off @ off
  470. mov r0, #0 @ must be zero 准备传入参数
  471. mov r1, r7 @ restore architecture number
  472. mov r2, r8 @ restore atags pointer
  473. mov pc, r4 @ call kernel r4是kernel解压缩到的地方 ,说是跳转到了arch/arm/kernel/head.S
  474. /*
  475. * Here follow the relocatable cache support functions for the
  476. * various processors. This is a generic hook for locating an
  477. * entry and jumping to an instruction at the specified offset
  478. * from the start of the block. Please note this is all position
  479. * independent code.
  480. *
  481. * r1 = corrupted
  482. * r2 = corrupted
  483. * r3 = block offset
  484. * r6 = corrupted
  485. * r12 = corrupted
  486. */
  487. call_cache_fn: adr r12, proc_types
  488. #ifdef CONFIG_CPU_CP15
  489. mrc p15, 0, r6, c0, c0 @ get processor ID
  490. #else
  491. not go here
  492. ldr r6, =CONFIG_PROCESSOR_ID
  493. #endif
  494. 1: ldr r1, [r12, #0] @ get value
  495. ldr r2, [r12, #4] @ get mask
  496. eor r1, r1, r6 @ (real ^ match)
  497. tst r1, r2 @ & mask
  498. addeq pc, r12, r3 @ call cache function
  499. add r12, r12, #4*5
  500. b 1b
  501. /*
  502. * Table for cache operations. This is basically:
  503. * - CPU ID match
  504. * - CPU ID mask
  505. * - 'cache on' method instruction
  506. * - 'cache off' method instruction
  507. * - 'cache flush' method instruction
  508. *
  509. * We match an entry using: ((real_id ^ match) & mask) == 0
  510. *
  511. * Writethrough caches generally only need 'on' and 'off'
  512. * methods. Writeback caches _must_ have the flush method
  513. * defined.
  514. */
  515. .type proc_types,#object
  516. proc_types:
  517. .word 0x41560600 @ ARM6/610
  518. .word 0xffffffe0
  519. b __arm6_mmu_cache_off @ works, but slow
  520. b __arm6_mmu_cache_off
  521. mov pc, lr
  522. @ b __arm6_mmu_cache_on @ untested
  523. @ b __arm6_mmu_cache_off
  524. @ b __armv3_mmu_cache_flush
  525. .word 0x00000000 @ old ARM ID
  526. .word 0x0000f000
  527. mov pc, lr
  528. mov pc, lr
  529. mov pc, lr
  530. .word 0x41007000 @ ARM7/710
  531. .word 0xfff8fe00
  532. b __arm7_mmu_cache_off
  533. b __arm7_mmu_cache_off
  534. mov pc, lr
  535. .word 0x41807200 @ ARM720T (writethrough)
  536. .word 0xffffff00
  537. b __armv4_mmu_cache_on
  538. b __armv4_mmu_cache_off
  539. mov pc, lr
  540. .word 0x41007400 @ ARM74x
  541. .word 0xff00ff00
  542. b __armv3_mpu_cache_on
  543. b __armv3_mpu_cache_off
  544. b __armv3_mpu_cache_flush
  545. .word 0x41009400 @ ARM94x
  546. .word 0xff00ff00
  547. b __armv4_mpu_cache_on
  548. b __armv4_mpu_cache_off
  549. b __armv4_mpu_cache_flush
  550. .word 0x00007000 @ ARM7 IDs
  551. .word 0x0000f000
  552. mov pc, lr
  553. mov pc, lr
  554. mov pc, lr
  555. @ Everything from here on will be the new ID system.
  556. .word 0x4401a100 @ sa110 / sa1100
  557. .word 0xffffffe0
  558. b __armv4_mmu_cache_on
  559. b __armv4_mmu_cache_off
  560. b __armv4_mmu_cache_flush
  561. .word 0x6901b110 @ sa1110
  562. .word 0xfffffff0
  563. b __armv4_mmu_cache_on
  564. b __armv4_mmu_cache_off
  565. b __armv4_mmu_cache_flush
  566. .word 0x56050000 @ Feroceon
  567. .word 0xff0f0000
  568. b __armv4_mmu_cache_on
  569. b __armv4_mmu_cache_off
  570. b __armv5tej_mmu_cache_flush
  571. @ These match on the architecture ID
  572. .word 0x00020000 @ ARMv4T
  573. .word 0x000f0000
  574. b __armv4_mmu_cache_on
  575. b __armv4_mmu_cache_off
  576. b __armv4_mmu_cache_flush
  577. .word 0x00050000 @ ARMv5TE
  578. .word 0x000f0000
  579. b __armv4_mmu_cache_on
  580. b __armv4_mmu_cache_off
  581. b __armv4_mmu_cache_flush
  582. .word 0x00060000 @ ARMv5TEJ
  583. .word 0x000f0000
  584. b __armv4_mmu_cache_on
  585. b __armv4_mmu_cache_off
  586. b __armv5tej_mmu_cache_flush
  587. .word 0x0007b000 @ ARMv6
  588. .word 0x000ff000
  589. b __armv4_mmu_cache_on
  590. b __armv4_mmu_cache_off
  591. b __armv6_mmu_cache_flush
  592. .word 0x000f0000 @ new CPU Id
  593. .word 0x000f0000
  594. b __armv7_mmu_cache_on
  595. b __armv7_mmu_cache_off
  596. b __armv7_mmu_cache_flush
  597. .word 0 @ unrecognised type
  598. .word 0
  599. mov pc, lr
  600. mov pc, lr
  601. mov pc, lr
  602. .size proc_types, . - proc_types
  603. /*
  604. * Turn off the Cache and MMU. ARMv3 does not support
  605. * reading the control register, but ARMv4 does.
  606. *
  607. * On entry, r6 = processor ID
  608. * On exit, r0, r1, r2, r3, r12 corrupted
  609. * This routine must preserve: r4, r6, r7
  610. */
  611. .align 5
  612. cache_off: mov r3, #12 @ cache_off function
  613. b call_cache_fn
  614. __armv4_mpu_cache_off:
  615. mrc p15, 0, r0, c1, c0
  616. bic r0, r0, #0x000d
  617. mcr p15, 0, r0, c1, c0 @ turn MPU and cache off
  618. mov r0, #0
  619. mcr p15, 0, r0, c7, c10, 4 @ drain write buffer
  620. mcr p15, 0, r0, c7, c6, 0 @ flush D-Cache
  621. mcr p15, 0, r0, c7, c5, 0 @ flush I-Cache
  622. mov pc, lr
  623. __armv3_mpu_cache_off:
  624. mrc p15, 0, r0, c1, c0
  625. bic r0, r0, #0x000d
  626. mcr p15, 0, r0, c1, c0, 0 @ turn MPU and cache off
  627. mov r0, #0
  628. mcr p15, 0, r0, c7, c0, 0 @ invalidate whole cache v3
  629. mov pc, lr
  630. __armv4_mmu_cache_off:
  631. mrc p15, 0, r0, c1, c0
  632. bic r0, r0, #0x000d
  633. mcr p15, 0, r0, c1, c0 @ turn MMU and cache off
  634. mov r0, #0
  635. mcr p15, 0, r0, c7, c7 @ invalidate whole cache v4
  636. mcr p15, 0, r0, c8, c7 @ invalidate whole TLB v4
  637. mov pc, lr
  638. __armv7_mmu_cache_off:
  639. mrc p15, 0, r0, c1, c0
  640. bic r0, r0, #0x000d
  641. mcr p15, 0, r0, c1, c0 @ turn MMU and cache off
  642. mov r12, lr
  643. bl __armv7_mmu_cache_flush
  644. mov r0, #0
  645. mcr p15, 0, r0, c8, c7, 0 @ invalidate whole TLB
  646. mov pc, r12
  647. __arm6_mmu_cache_off:
  648. mov r0, #0x00000030 @ ARM6 control reg.
  649. b __armv3_mmu_cache_off
  650. __arm7_mmu_cache_off:
  651. mov r0, #0x00000070 @ ARM7 control reg.
  652. b __armv3_mmu_cache_off
  653. __armv3_mmu_cache_off:
  654. mcr p15, 0, r0, c1, c0, 0 @ turn MMU and cache off
  655. mov r0, #0
  656. mcr p15, 0, r0, c7, c0, 0 @ invalidate whole cache v3
  657. mcr p15, 0, r0, c5, c0, 0 @ invalidate whole TLB v3
  658. mov pc, lr
  659. /*
  660. * Clean and flush the cache to maintain consistency.
  661. *
  662. * On entry,
  663. * r6 = processor ID
  664. * On exit,
  665. * r1, r2, r3, r11, r12 corrupted
  666. * This routine must preserve:
  667. * r0, r4, r5, r6, r7
  668. */
  669. .align 5
  670. cache_clean_flush:
  671. mov r3, #16
  672. b call_cache_fn
  673. __armv4_mpu_cache_flush:
  674. mov r2, #1
  675. mov r3, #0
  676. mcr p15, 0, ip, c7, c6, 0 @ invalidate D cache
  677. mov r1, #7 << 5 @ 8 segments
  678. 1: orr r3, r1, #63 << 26 @ 64 entries
  679. 2: mcr p15, 0, r3, c7, c14, 2 @ clean & invalidate D index
  680. subs r3, r3, #1 << 26
  681. bcs 2b @ entries 63 to 0
  682. subs r1, r1, #1 << 5
  683. bcs 1b @ segments 7 to 0
  684. teq r2, #0
  685. mcrne p15, 0, ip, c7, c5, 0 @ invalidate I cache
  686. mcr p15, 0, ip, c7, c10, 4 @ drain WB
  687. mov pc, lr
  688. __armv6_mmu_cache_flush:
  689. mov r1, #0
  690. mcr p15, 0, r1, c7, c14, 0 @ clean+invalidate D
  691. mcr p15, 0, r1, c7, c5, 0 @ invalidate I+BTB
  692. mcr p15, 0, r1, c7, c15, 0 @ clean+invalidate unified
  693. mcr p15, 0, r1, c7, c10, 4 @ drain WB
  694. mov pc, lr
  695. __armv7_mmu_cache_flush:
  696. mrc p15, 0, r10, c0, c1, 5 @ read ID_MMFR1
  697. tst r10, #0xf << 16 @ hierarchical cache (ARMv7)
  698. beq hierarchical
  699. mov r10, #0
  700. mcr p15, 0, r10, c7, c14, 0 @ clean+invalidate D
  701. b iflush
  702. hierarchical:
  703. stmfd sp!, {r0-r5, r7, r9-r11}
  704. mrc p15, 1, r0, c0, c0, 1 @ read clidr
  705. ands r3, r0, #0x7000000 @ extract loc from clidr
  706. mov r3, r3, lsr #23 @ left align loc bit field
  707. beq finished @ if loc is 0, then no need to clean
  708. mov r10, #0 @ start clean at cache level 0
  709. loop1:
  710. add r2, r10, r10, lsr #1 @ work out 3x current cache level
  711. mov r1, r0, lsr r2 @ extract cache type bits from clidr
  712. and r1, r1, #7 @ mask of the bits for current cache only
  713. cmp r1, #2 @ see what cache we have at this level
  714. blt skip @ skip if no cache, or just i-cache
  715. mcr p15, 2, r10, c0, c0, 0 @ select current cache level in cssr
  716. mcr p15, 0, r10, c7, c5, 4 @ isb to sych the new cssr&csidr
  717. mrc p15, 1, r1, c0, c0, 0 @ read the new csidr
  718. and r2, r1, #7 @ extract the length of the cache lines
  719. add r2, r2, #4 @ add 4 (line length offset)
  720. ldr r4, =0x3ff
  721. ands r4, r4, r1, lsr #3 @ find maximum number on the way size
  722. clz r5, r4 @ find bit position of way size increment
  723. ldr r7, =0x7fff
  724. ands r7, r7, r1, lsr #13 @ extract max number of the index size
  725. loop2:
  726. mov r9, r4 @ create working copy of max way size
  727. loop3:
  728. orr r11, r10, r9, lsl r5 @ factor way and cache number into r11
  729. orr r11, r11, r7, lsl r2 @ factor index number into r11
  730. mcr p15, 0, r11, c7, c14, 2 @ clean & invalidate by set/way
  731. subs r9, r9, #1 @ decrement the way
  732. bge loop3
  733. subs r7, r7, #1 @ decrement the index
  734. bge loop2
  735. skip:
  736. add r10, r10, #2 @ increment cache number
  737. cmp r3, r10
  738. bgt loop1
  739. finished:
  740. mov r10, #0 @ swith back to cache level 0
  741. mcr p15, 2, r10, c0, c0, 0 @ select current cache level in cssr
  742. ldmfd sp!, {r0-r5, r7, r9-r11}
  743. iflush:
  744. mcr p15, 0, r10, c7, c5, 0 @ invalidate I+BTB
  745. mcr p15, 0, r10, c7, c10, 4 @ drain WB
  746. mov pc, lr
  747. __armv5tej_mmu_cache_flush:
  748. 1: mrc p15, 0, r15, c7, c14, 3 @ test,clean,invalidate D cache
  749. bne 1b
  750. mcr p15, 0, r0, c7, c5, 0 @ flush I cache
  751. mcr p15, 0, r0, c7, c10, 4 @ drain WB
  752. mov pc, lr
  753. __armv4_mmu_cache_flush:
  754. mov r2, #64*1024 @ default: 32K dcache size (*2)
  755. mov r11, #32 @ default: 32 byte line size
  756. mrc p15, 0, r3, c0, c0, 1 @ read cache type
  757. teq r3, r6 @ cache ID register present?
  758. beq no_cache_id
  759. mov r1, r3, lsr #18
  760. and r1, r1, #7
  761. mov r2, #1024
  762. mov r2, r2, lsl r1 @ base dcache size *2
  763. tst r3, #1 << 14 @ test M bit
  764. addne r2, r2, r2, lsr #1 @ +1/2 size if M == 1
  765. mov r3, r3, lsr #12
  766. and r3, r3, #3
  767. mov r11, #8
  768. mov r11, r11, lsl r3 @ cache line size in bytes
  769. no_cache_id:
  770. bic r1, pc, #63 @ align to longest cache line
  771. add r2, r1, r2
  772. 1: ldr r3, [r1], r11 @ s/w flush D cache
  773. teq r1, r2
  774. bne 1b
  775. mcr p15, 0, r1, c7, c5, 0 @ flush I cache
  776. mcr p15, 0, r1, c7, c6, 0 @ flush D cache
  777. mcr p15, 0, r1, c7, c10, 4 @ drain WB
  778. mov pc, lr
  779. __armv3_mmu_cache_flush:
  780. __armv3_mpu_cache_flush:
  781. mov r1, #0
  782. mcr p15, 0, r0, c7, c0, 0 @ invalidate whole cache v3
  783. mov pc, lr
  784. /*
  785. * Various debugging routines for printing hex characters and
  786. * memory, which again must be relocatable.
  787. */
  788. #ifdef DEBUG
  789. .type phexbuf,#object
  790. phexbuf: .space 12
  791. .size phexbuf, . - phexbuf
  792. phex: adr r3, phexbuf
  793. mov r2, #0
  794. strb r2, [r3, r1]
  795. 1: subs r1, r1, #1
  796. movmi r0, r3
  797. bmi puts
  798. and r2, r0, #15
  799. mov r0, r0, lsr #4
  800. cmp r2, #10
  801. addge r2, r2, #7
  802. add r2, r2, #'0'
  803. strb r2, [r3, r1]
  804. b 1b
  805. puts: loadsp r3
  806. 1: ldrb r2, [r0], #1
  807. teq r2, #0
  808. moveq pc, lr
  809. 2: writeb r2, r3
  810. mov r1, #0x00020000
  811. 3: subs r1, r1, #1
  812. bne 3b
  813. teq r2, #'/n'
  814. moveq r2, #'/r'
  815. beq 2b
  816. teq r0, #0
  817. bne 1b
  818. mov pc, lr
  819. putc:
  820. mov r2, r0
  821. mov r0, #0
  822. loadsp r3
  823. b 2b
  824. memdump: mov r12, r0
  825. mov r10, lr
  826. mov r11, #0
  827. 2: mov r0, r11, lsl #2
  828. add r0, r0, r12
  829. mov r1, #8
  830. bl phex
  831. mov r0, #':'
  832. bl putc
  833. 1: mov r0, #' '
  834. bl putc
  835. ldr r0, [r12, r11, lsl #2]
  836. mov r1, #8
  837. bl phex
  838. and r0, r11, #7
  839. teq r0, #3
  840. moveq r0, #' '
  841. bleq putc
  842. and r0, r11, #7
  843. add r11, r11, #1
  844. teq r0, #7
  845. bne 1b
  846. mov r0, #'/n'
  847. bl putc
  848. cmp r11, #64
  849. blt 2b
  850. mov pc, r10
  851. #endif
  852. .ltorg
  853. reloc_end:
  854. .align
  855. .section ".stack", "w"
  856. user_stack: .space 4096

阅读(1111) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~