head.S 13 KB

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