head.S 14 KB

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