head.S 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. /*
  2. * linux/arch/arm/kernel/head.S
  3. *
  4. * Copyright (C) 1994-2002 Russell King
  5. * Copyright (c) 2003 ARM Limited
  6. * All Rights Reserved
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * Kernel startup code for all 32-bit CPUs
  13. */
  14. #include <linux/config.h>
  15. #include <linux/linkage.h>
  16. #include <linux/init.h>
  17. #include <asm/assembler.h>
  18. #include <asm/domain.h>
  19. #include <asm/procinfo.h>
  20. #include <asm/ptrace.h>
  21. #include <asm/asm-offsets.h>
  22. #include <asm/memory.h>
  23. #include <asm/thread_info.h>
  24. #include <asm/system.h>
  25. #define PROCINFO_MMUFLAGS 8
  26. #define PROCINFO_INITFUNC 12
  27. #define MACHINFO_TYPE 0
  28. #define MACHINFO_PHYSRAM 4
  29. #define MACHINFO_PHYSIO 8
  30. #define MACHINFO_PGOFFIO 12
  31. #define MACHINFO_NAME 16
  32. #define KERNEL_RAM_ADDR (PAGE_OFFSET + TEXT_OFFSET)
  33. /*
  34. * swapper_pg_dir is the virtual address of the initial page table.
  35. * We place the page tables 16K below KERNEL_RAM_ADDR. Therefore, we must
  36. * make sure that KERNEL_RAM_ADDR is correctly set. Currently, we expect
  37. * the least significant 16 bits to be 0x8000, but we could probably
  38. * relax this restriction to KERNEL_RAM_ADDR >= PAGE_OFFSET + 0x4000.
  39. */
  40. #if (KERNEL_RAM_ADDR & 0xffff) != 0x8000
  41. #error KERNEL_RAM_ADDR must start at 0xXXXX8000
  42. #endif
  43. .globl swapper_pg_dir
  44. .equ swapper_pg_dir, KERNEL_RAM_ADDR - 0x4000
  45. .macro pgtbl, rd
  46. ldr \rd, =(__virt_to_phys(KERNEL_RAM_ADDR - 0x4000))
  47. .endm
  48. #ifdef CONFIG_XIP_KERNEL
  49. #define TEXTADDR XIP_VIRT_ADDR(CONFIG_XIP_PHYS_ADDR)
  50. #else
  51. #define TEXTADDR KERNEL_RAM_ADDR
  52. #endif
  53. /*
  54. * Kernel startup entry point.
  55. * ---------------------------
  56. *
  57. * This is normally called from the decompressor code. The requirements
  58. * are: MMU = off, D-cache = off, I-cache = dont care, r0 = 0,
  59. * r1 = machine nr.
  60. *
  61. * This code is mostly position independent, so if you link the kernel at
  62. * 0xc0008000, you call this at __pa(0xc0008000).
  63. *
  64. * See linux/arch/arm/tools/mach-types for the complete list of machine
  65. * numbers for r1.
  66. *
  67. * We're trying to keep crap to a minimum; DO NOT add any machine specific
  68. * crap here - that's what the boot loader (or in extreme, well justified
  69. * circumstances, zImage) is for.
  70. */
  71. __INIT
  72. .type stext, %function
  73. ENTRY(stext)
  74. msr cpsr_c, #PSR_F_BIT | PSR_I_BIT | MODE_SVC @ ensure svc mode
  75. @ and irqs disabled
  76. bl __lookup_processor_type @ r5=procinfo r9=cpuid
  77. movs r10, r5 @ invalid processor (r5=0)?
  78. beq __error_p @ yes, error 'p'
  79. bl __lookup_machine_type @ r5=machinfo
  80. movs r8, r5 @ invalid machine (r5=0)?
  81. beq __error_a @ yes, error 'a'
  82. bl __create_page_tables
  83. /*
  84. * The following calls CPU specific code in a position independent
  85. * manner. See arch/arm/mm/proc-*.S for details. r10 = base of
  86. * xxx_proc_info structure selected by __lookup_machine_type
  87. * above. On return, the CPU will be ready for the MMU to be
  88. * turned on, and r0 will hold the CPU control register value.
  89. */
  90. ldr r13, __switch_data @ address to jump to after
  91. @ mmu has been enabled
  92. adr lr, __enable_mmu @ return (PIC) address
  93. add pc, r10, #PROCINFO_INITFUNC
  94. .type __switch_data, %object
  95. __switch_data:
  96. .long __mmap_switched
  97. .long __data_loc @ r4
  98. .long __data_start @ r5
  99. .long __bss_start @ r6
  100. .long _end @ r7
  101. .long processor_id @ r4
  102. .long __machine_arch_type @ r5
  103. .long cr_alignment @ r6
  104. .long init_thread_union + THREAD_START_SP @ sp
  105. /*
  106. * The following fragment of code is executed with the MMU on, and uses
  107. * absolute addresses; this is not position independent.
  108. *
  109. * r0 = cp#15 control register
  110. * r1 = machine ID
  111. * r9 = processor ID
  112. */
  113. .type __mmap_switched, %function
  114. __mmap_switched:
  115. adr r3, __switch_data + 4
  116. ldmia r3!, {r4, r5, r6, r7}
  117. cmp r4, r5 @ Copy data segment if needed
  118. 1: cmpne r5, r6
  119. ldrne fp, [r4], #4
  120. strne fp, [r5], #4
  121. bne 1b
  122. mov fp, #0 @ Clear BSS (and zero fp)
  123. 1: cmp r6, r7
  124. strcc fp, [r6],#4
  125. bcc 1b
  126. ldmia r3, {r4, r5, r6, sp}
  127. str r9, [r4] @ Save processor ID
  128. str r1, [r5] @ Save machine type
  129. bic r4, r0, #CR_A @ Clear 'A' bit
  130. stmia r6, {r0, r4} @ Save control register values
  131. b start_kernel
  132. #if defined(CONFIG_SMP)
  133. .type secondary_startup, #function
  134. ENTRY(secondary_startup)
  135. /*
  136. * Common entry point for secondary CPUs.
  137. *
  138. * Ensure that we're in SVC mode, and IRQs are disabled. Lookup
  139. * the processor type - there is no need to check the machine type
  140. * as it has already been validated by the primary processor.
  141. */
  142. msr cpsr_c, #PSR_F_BIT | PSR_I_BIT | MODE_SVC
  143. bl __lookup_processor_type
  144. movs r10, r5 @ invalid processor?
  145. moveq r0, #'p' @ yes, error 'p'
  146. beq __error
  147. /*
  148. * Use the page tables supplied from __cpu_up.
  149. */
  150. adr r4, __secondary_data
  151. ldmia r4, {r5, r6, r13} @ address to jump to after
  152. sub r4, r4, r5 @ mmu has been enabled
  153. ldr r4, [r6, r4] @ get secondary_data.pgdir
  154. adr lr, __enable_mmu @ return address
  155. add pc, r10, #12 @ initialise processor
  156. @ (return control reg)
  157. /*
  158. * r6 = &secondary_data
  159. */
  160. ENTRY(__secondary_switched)
  161. ldr sp, [r6, #4] @ get secondary_data.stack
  162. mov fp, #0
  163. b secondary_start_kernel
  164. .type __secondary_data, %object
  165. __secondary_data:
  166. .long .
  167. .long secondary_data
  168. .long __secondary_switched
  169. #endif /* defined(CONFIG_SMP) */
  170. /*
  171. * Setup common bits before finally enabling the MMU. Essentially
  172. * this is just loading the page table pointer and domain access
  173. * registers.
  174. */
  175. .type __enable_mmu, %function
  176. __enable_mmu:
  177. #ifdef CONFIG_ALIGNMENT_TRAP
  178. orr r0, r0, #CR_A
  179. #else
  180. bic r0, r0, #CR_A
  181. #endif
  182. #ifdef CONFIG_CPU_DCACHE_DISABLE
  183. bic r0, r0, #CR_C
  184. #endif
  185. #ifdef CONFIG_CPU_BPREDICT_DISABLE
  186. bic r0, r0, #CR_Z
  187. #endif
  188. #ifdef CONFIG_CPU_ICACHE_DISABLE
  189. bic r0, r0, #CR_I
  190. #endif
  191. mov r5, #(domain_val(DOMAIN_USER, DOMAIN_MANAGER) | \
  192. domain_val(DOMAIN_KERNEL, DOMAIN_MANAGER) | \
  193. domain_val(DOMAIN_TABLE, DOMAIN_MANAGER) | \
  194. domain_val(DOMAIN_IO, DOMAIN_CLIENT))
  195. mcr p15, 0, r5, c3, c0, 0 @ load domain access register
  196. mcr p15, 0, r4, c2, c0, 0 @ load page table pointer
  197. b __turn_mmu_on
  198. /*
  199. * Enable the MMU. This completely changes the structure of the visible
  200. * memory space. You will not be able to trace execution through this.
  201. * If you have an enquiry about this, *please* check the linux-arm-kernel
  202. * mailing list archives BEFORE sending another post to the list.
  203. *
  204. * r0 = cp#15 control register
  205. * r13 = *virtual* address to jump to upon completion
  206. *
  207. * other registers depend on the function called upon completion
  208. */
  209. .align 5
  210. .type __turn_mmu_on, %function
  211. __turn_mmu_on:
  212. mov r0, r0
  213. mcr p15, 0, r0, c1, c0, 0 @ write control reg
  214. mrc p15, 0, r3, c0, c0, 0 @ read id reg
  215. mov r3, r3
  216. mov r3, r3
  217. mov pc, r13
  218. /*
  219. * Setup the initial page tables. We only setup the barest
  220. * amount which are required to get the kernel running, which
  221. * generally means mapping in the kernel code.
  222. *
  223. * r8 = machinfo
  224. * r9 = cpuid
  225. * r10 = procinfo
  226. *
  227. * Returns:
  228. * r0, r3, r6, r7 corrupted
  229. * r4 = physical page table address
  230. */
  231. .type __create_page_tables, %function
  232. __create_page_tables:
  233. pgtbl r4 @ page table address
  234. /*
  235. * Clear the 16K level 1 swapper page table
  236. */
  237. mov r0, r4
  238. mov r3, #0
  239. add r6, r0, #0x4000
  240. 1: str r3, [r0], #4
  241. str r3, [r0], #4
  242. str r3, [r0], #4
  243. str r3, [r0], #4
  244. teq r0, r6
  245. bne 1b
  246. ldr r7, [r10, #PROCINFO_MMUFLAGS] @ mmuflags
  247. /*
  248. * Create identity mapping for first MB of kernel to
  249. * cater for the MMU enable. This identity mapping
  250. * will be removed by paging_init(). We use our current program
  251. * counter to determine corresponding section base address.
  252. */
  253. mov r6, pc, lsr #20 @ start of kernel section
  254. orr r3, r7, r6, lsl #20 @ flags + kernel base
  255. str r3, [r4, r6, lsl #2] @ identity mapping
  256. /*
  257. * Now setup the pagetables for our kernel direct
  258. * mapped region. We round TEXTADDR down to the
  259. * nearest megabyte boundary. It is assumed that
  260. * the kernel fits within 4 contigous 1MB sections.
  261. */
  262. add r0, r4, #(TEXTADDR & 0xff000000) >> 18 @ start of kernel
  263. str r3, [r0, #(TEXTADDR & 0x00f00000) >> 18]!
  264. add r3, r3, #1 << 20
  265. str r3, [r0, #4]! @ KERNEL + 1MB
  266. add r3, r3, #1 << 20
  267. str r3, [r0, #4]! @ KERNEL + 2MB
  268. add r3, r3, #1 << 20
  269. str r3, [r0, #4] @ KERNEL + 3MB
  270. /*
  271. * Then map first 1MB of ram in case it contains our boot params.
  272. */
  273. add r0, r4, #PAGE_OFFSET >> 18
  274. orr r6, r7, #PHYS_OFFSET
  275. str r6, [r0]
  276. #ifdef CONFIG_XIP_KERNEL
  277. /*
  278. * Map some ram to cover our .data and .bss areas.
  279. * Mapping 3MB should be plenty.
  280. */
  281. sub r3, r4, #PHYS_OFFSET
  282. mov r3, r3, lsr #20
  283. add r0, r0, r3, lsl #2
  284. add r6, r6, r3, lsl #20
  285. str r6, [r0], #4
  286. add r6, r6, #(1 << 20)
  287. str r6, [r0], #4
  288. add r6, r6, #(1 << 20)
  289. str r6, [r0]
  290. #endif
  291. #ifdef CONFIG_DEBUG_LL
  292. bic r7, r7, #0x0c @ turn off cacheable
  293. @ and bufferable bits
  294. /*
  295. * Map in IO space for serial debugging.
  296. * This allows debug messages to be output
  297. * via a serial console before paging_init.
  298. */
  299. ldr r3, [r8, #MACHINFO_PGOFFIO]
  300. add r0, r4, r3
  301. rsb r3, r3, #0x4000 @ PTRS_PER_PGD*sizeof(long)
  302. cmp r3, #0x0800 @ limit to 512MB
  303. movhi r3, #0x0800
  304. add r6, r0, r3
  305. ldr r3, [r8, #MACHINFO_PHYSIO]
  306. orr r3, r3, r7
  307. 1: str r3, [r0], #4
  308. add r3, r3, #1 << 20
  309. teq r0, r6
  310. bne 1b
  311. #if defined(CONFIG_ARCH_NETWINDER) || defined(CONFIG_ARCH_CATS)
  312. /*
  313. * If we're using the NetWinder or CATS, we also need to map
  314. * in the 16550-type serial port for the debug messages
  315. */
  316. add r0, r4, #0xff000000 >> 18
  317. orr r3, r7, #0x7c000000
  318. str r3, [r0]
  319. #endif
  320. #ifdef CONFIG_ARCH_RPC
  321. /*
  322. * Map in screen at 0x02000000 & SCREEN2_BASE
  323. * Similar reasons here - for debug. This is
  324. * only for Acorn RiscPC architectures.
  325. */
  326. add r0, r4, #0x02000000 >> 18
  327. orr r3, r7, #0x02000000
  328. str r3, [r0]
  329. add r0, r4, #0xd8000000 >> 18
  330. str r3, [r0]
  331. #endif
  332. #endif
  333. mov pc, lr
  334. .ltorg
  335. /*
  336. * Exception handling. Something went wrong and we can't proceed. We
  337. * ought to tell the user, but since we don't have any guarantee that
  338. * we're even running on the right architecture, we do virtually nothing.
  339. *
  340. * If CONFIG_DEBUG_LL is set we try to print out something about the error
  341. * and hope for the best (useful if bootloader fails to pass a proper
  342. * machine ID for example).
  343. */
  344. .type __error_p, %function
  345. __error_p:
  346. #ifdef CONFIG_DEBUG_LL
  347. adr r0, str_p1
  348. bl printascii
  349. b __error
  350. str_p1: .asciz "\nError: unrecognized/unsupported processor variant.\n"
  351. .align
  352. #endif
  353. .type __error_a, %function
  354. __error_a:
  355. #ifdef CONFIG_DEBUG_LL
  356. mov r4, r1 @ preserve machine ID
  357. adr r0, str_a1
  358. bl printascii
  359. mov r0, r4
  360. bl printhex8
  361. adr r0, str_a2
  362. bl printascii
  363. adr r3, 3f
  364. ldmia r3, {r4, r5, r6} @ get machine desc list
  365. sub r4, r3, r4 @ get offset between virt&phys
  366. add r5, r5, r4 @ convert virt addresses to
  367. add r6, r6, r4 @ physical address space
  368. 1: ldr r0, [r5, #MACHINFO_TYPE] @ get machine type
  369. bl printhex8
  370. mov r0, #'\t'
  371. bl printch
  372. ldr r0, [r5, #MACHINFO_NAME] @ get machine name
  373. add r0, r0, r4
  374. bl printascii
  375. mov r0, #'\n'
  376. bl printch
  377. add r5, r5, #SIZEOF_MACHINE_DESC @ next machine_desc
  378. cmp r5, r6
  379. blo 1b
  380. adr r0, str_a3
  381. bl printascii
  382. b __error
  383. str_a1: .asciz "\nError: unrecognized/unsupported machine ID (r1 = 0x"
  384. str_a2: .asciz ").\n\nAvailable machine support:\n\nID (hex)\tNAME\n"
  385. str_a3: .asciz "\nPlease check your kernel config and/or bootloader.\n"
  386. .align
  387. #endif
  388. .type __error, %function
  389. __error:
  390. #ifdef CONFIG_ARCH_RPC
  391. /*
  392. * Turn the screen red on a error - RiscPC only.
  393. */
  394. mov r0, #0x02000000
  395. mov r3, #0x11
  396. orr r3, r3, r3, lsl #8
  397. orr r3, r3, r3, lsl #16
  398. str r3, [r0], #4
  399. str r3, [r0], #4
  400. str r3, [r0], #4
  401. str r3, [r0], #4
  402. #endif
  403. 1: mov r0, r0
  404. b 1b
  405. /*
  406. * Read processor ID register (CP#15, CR0), and look up in the linker-built
  407. * supported processor list. Note that we can't use the absolute addresses
  408. * for the __proc_info lists since we aren't running with the MMU on
  409. * (and therefore, we are not in the correct address space). We have to
  410. * calculate the offset.
  411. *
  412. * Returns:
  413. * r3, r4, r6 corrupted
  414. * r5 = proc_info pointer in physical address space
  415. * r9 = cpuid
  416. */
  417. .type __lookup_processor_type, %function
  418. __lookup_processor_type:
  419. adr r3, 3f
  420. ldmda r3, {r5, r6, r9}
  421. sub r3, r3, r9 @ get offset between virt&phys
  422. add r5, r5, r3 @ convert virt addresses to
  423. add r6, r6, r3 @ physical address space
  424. mrc p15, 0, r9, c0, c0 @ get processor id
  425. 1: ldmia r5, {r3, r4} @ value, mask
  426. and r4, r4, r9 @ mask wanted bits
  427. teq r3, r4
  428. beq 2f
  429. add r5, r5, #PROC_INFO_SZ @ sizeof(proc_info_list)
  430. cmp r5, r6
  431. blo 1b
  432. mov r5, #0 @ unknown processor
  433. 2: mov pc, lr
  434. /*
  435. * This provides a C-API version of the above function.
  436. */
  437. ENTRY(lookup_processor_type)
  438. stmfd sp!, {r4 - r6, r9, lr}
  439. bl __lookup_processor_type
  440. mov r0, r5
  441. ldmfd sp!, {r4 - r6, r9, pc}
  442. /*
  443. * Look in include/asm-arm/procinfo.h and arch/arm/kernel/arch.[ch] for
  444. * more information about the __proc_info and __arch_info structures.
  445. */
  446. .long __proc_info_begin
  447. .long __proc_info_end
  448. 3: .long .
  449. .long __arch_info_begin
  450. .long __arch_info_end
  451. /*
  452. * Lookup machine architecture in the linker-build list of architectures.
  453. * Note that we can't use the absolute addresses for the __arch_info
  454. * lists since we aren't running with the MMU on (and therefore, we are
  455. * not in the correct address space). We have to calculate the offset.
  456. *
  457. * r1 = machine architecture number
  458. * Returns:
  459. * r3, r4, r6 corrupted
  460. * r5 = mach_info pointer in physical address space
  461. */
  462. .type __lookup_machine_type, %function
  463. __lookup_machine_type:
  464. adr r3, 3b
  465. ldmia r3, {r4, r5, r6}
  466. sub r3, r3, r4 @ get offset between virt&phys
  467. add r5, r5, r3 @ convert virt addresses to
  468. add r6, r6, r3 @ physical address space
  469. 1: ldr r3, [r5, #MACHINFO_TYPE] @ get machine type
  470. teq r3, r1 @ matches loader number?
  471. beq 2f @ found
  472. add r5, r5, #SIZEOF_MACHINE_DESC @ next machine_desc
  473. cmp r5, r6
  474. blo 1b
  475. mov r5, #0 @ unknown machine
  476. 2: mov pc, lr
  477. /*
  478. * This provides a C-API version of the above function.
  479. */
  480. ENTRY(lookup_machine_type)
  481. stmfd sp!, {r4 - r6, lr}
  482. mov r1, r0
  483. bl __lookup_machine_type
  484. mov r0, r5
  485. ldmfd sp!, {r4 - r6, pc}