head.S 14 KB

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