head.S 14 KB

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