init.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (C) 1995-2005 Russell King
  3. // Copyright (C) 2012 ARM Ltd.
  4. // Copyright (C) 2013-2017 Andes Technology Corporation
  5. #include <linux/kernel.h>
  6. #include <linux/errno.h>
  7. #include <linux/swap.h>
  8. #include <linux/init.h>
  9. #include <linux/bootmem.h>
  10. #include <linux/mman.h>
  11. #include <linux/nodemask.h>
  12. #include <linux/initrd.h>
  13. #include <linux/highmem.h>
  14. #include <linux/memblock.h>
  15. #include <asm/sections.h>
  16. #include <asm/setup.h>
  17. #include <asm/tlb.h>
  18. #include <asm/page.h>
  19. DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
  20. DEFINE_SPINLOCK(anon_alias_lock);
  21. extern pgd_t swapper_pg_dir[PTRS_PER_PGD];
  22. extern unsigned long phys_initrd_start;
  23. extern unsigned long phys_initrd_size;
  24. /*
  25. * empty_zero_page is a special page that is used for
  26. * zero-initialized data and COW.
  27. */
  28. struct page *empty_zero_page;
  29. EXPORT_SYMBOL(empty_zero_page);
  30. static void __init zone_sizes_init(void)
  31. {
  32. unsigned long zones_size[MAX_NR_ZONES];
  33. /* Clear the zone sizes */
  34. memset(zones_size, 0, sizeof(zones_size));
  35. zones_size[ZONE_NORMAL] = max_low_pfn;
  36. #ifdef CONFIG_HIGHMEM
  37. zones_size[ZONE_HIGHMEM] = max_pfn;
  38. #endif
  39. free_area_init(zones_size);
  40. }
  41. /*
  42. * Map all physical memory under high_memory into kernel's address space.
  43. *
  44. * This is explicitly coded for two-level page tables, so if you need
  45. * something else then this needs to change.
  46. */
  47. static void __init map_ram(void)
  48. {
  49. unsigned long v, p, e;
  50. pgd_t *pge;
  51. pud_t *pue;
  52. pmd_t *pme;
  53. pte_t *pte;
  54. /* These mark extents of read-only kernel pages...
  55. * ...from vmlinux.lds.S
  56. */
  57. p = (u32) memblock_start_of_DRAM() & PAGE_MASK;
  58. e = min((u32) memblock_end_of_DRAM(), (u32) __pa(high_memory));
  59. v = (u32) __va(p);
  60. pge = pgd_offset_k(v);
  61. while (p < e) {
  62. int j;
  63. pue = pud_offset(pge, v);
  64. pme = pmd_offset(pue, v);
  65. if ((u32) pue != (u32) pge || (u32) pme != (u32) pge) {
  66. panic("%s: Kernel hardcoded for "
  67. "two-level page tables", __func__);
  68. }
  69. /* Alloc one page for holding PTE's... */
  70. pte = (pte_t *) __va(memblock_alloc(PAGE_SIZE, PAGE_SIZE));
  71. memset(pte, 0, PAGE_SIZE);
  72. set_pmd(pme, __pmd(__pa(pte) + _PAGE_KERNEL_TABLE));
  73. /* Fill the newly allocated page with PTE'S */
  74. for (j = 0; p < e && j < PTRS_PER_PTE;
  75. v += PAGE_SIZE, p += PAGE_SIZE, j++, pte++) {
  76. /* Create mapping between p and v. */
  77. /* TODO: more fine grant for page access permission */
  78. set_pte(pte, __pte(p + pgprot_val(PAGE_KERNEL)));
  79. }
  80. pge++;
  81. }
  82. }
  83. static pmd_t *fixmap_pmd_p;
  84. static void __init fixedrange_init(void)
  85. {
  86. unsigned long vaddr;
  87. pgd_t *pgd;
  88. pud_t *pud;
  89. pmd_t *pmd;
  90. #ifdef CONFIG_HIGHMEM
  91. pte_t *pte;
  92. #endif /* CONFIG_HIGHMEM */
  93. /*
  94. * Fixed mappings:
  95. */
  96. vaddr = __fix_to_virt(__end_of_fixed_addresses - 1);
  97. pgd = swapper_pg_dir + pgd_index(vaddr);
  98. pud = pud_offset(pgd, vaddr);
  99. pmd = pmd_offset(pud, vaddr);
  100. fixmap_pmd_p = (pmd_t *) __va(memblock_alloc(PAGE_SIZE, PAGE_SIZE));
  101. memset(fixmap_pmd_p, 0, PAGE_SIZE);
  102. set_pmd(pmd, __pmd(__pa(fixmap_pmd_p) + _PAGE_KERNEL_TABLE));
  103. #ifdef CONFIG_HIGHMEM
  104. /*
  105. * Permanent kmaps:
  106. */
  107. vaddr = PKMAP_BASE;
  108. pgd = swapper_pg_dir + pgd_index(vaddr);
  109. pud = pud_offset(pgd, vaddr);
  110. pmd = pmd_offset(pud, vaddr);
  111. pte = (pte_t *) __va(memblock_alloc(PAGE_SIZE, PAGE_SIZE));
  112. memset(pte, 0, PAGE_SIZE);
  113. set_pmd(pmd, __pmd(__pa(pte) + _PAGE_KERNEL_TABLE));
  114. pkmap_page_table = pte;
  115. #endif /* CONFIG_HIGHMEM */
  116. }
  117. /*
  118. * paging_init() sets up the page tables, initialises the zone memory
  119. * maps, and sets up the zero page, bad page and bad page tables.
  120. */
  121. void __init paging_init(void)
  122. {
  123. int i;
  124. void *zero_page;
  125. pr_info("Setting up paging and PTEs.\n");
  126. /* clear out the init_mm.pgd that will contain the kernel's mappings */
  127. for (i = 0; i < PTRS_PER_PGD; i++)
  128. swapper_pg_dir[i] = __pgd(1);
  129. map_ram();
  130. fixedrange_init();
  131. /* allocate space for empty_zero_page */
  132. zero_page = __va(memblock_alloc(PAGE_SIZE, PAGE_SIZE));
  133. memset(zero_page, 0, PAGE_SIZE);
  134. zone_sizes_init();
  135. empty_zero_page = virt_to_page(zero_page);
  136. flush_dcache_page(empty_zero_page);
  137. }
  138. static inline void __init free_highmem(void)
  139. {
  140. #ifdef CONFIG_HIGHMEM
  141. unsigned long pfn;
  142. for (pfn = PFN_UP(__pa(high_memory)); pfn < max_pfn; pfn++) {
  143. phys_addr_t paddr = (phys_addr_t) pfn << PAGE_SHIFT;
  144. if (!memblock_is_reserved(paddr))
  145. free_highmem_page(pfn_to_page(pfn));
  146. }
  147. #endif
  148. }
  149. static void __init set_max_mapnr_init(void)
  150. {
  151. max_mapnr = max_pfn;
  152. }
  153. /*
  154. * mem_init() marks the free areas in the mem_map and tells us how much
  155. * memory is free. This is done after various parts of the system have
  156. * claimed their memory after the kernel image.
  157. */
  158. void __init mem_init(void)
  159. {
  160. phys_addr_t memory_start = memblock_start_of_DRAM();
  161. BUG_ON(!mem_map);
  162. set_max_mapnr_init();
  163. free_highmem();
  164. /* this will put all low memory onto the freelists */
  165. free_all_bootmem();
  166. mem_init_print_info(NULL);
  167. pr_info("virtual kernel memory layout:\n"
  168. " fixmap : 0x%08lx - 0x%08lx (%4ld kB)\n"
  169. #ifdef CONFIG_HIGHMEM
  170. " pkmap : 0x%08lx - 0x%08lx (%4ld kB)\n"
  171. #endif
  172. " consist : 0x%08lx - 0x%08lx (%4ld MB)\n"
  173. " vmalloc : 0x%08lx - 0x%08lx (%4ld MB)\n"
  174. " lowmem : 0x%08lx - 0x%08lx (%4ld MB)\n"
  175. " .init : 0x%08lx - 0x%08lx (%4ld kB)\n"
  176. " .data : 0x%08lx - 0x%08lx (%4ld kB)\n"
  177. " .text : 0x%08lx - 0x%08lx (%4ld kB)\n",
  178. FIXADDR_START, FIXADDR_TOP, (FIXADDR_TOP - FIXADDR_START) >> 10,
  179. #ifdef CONFIG_HIGHMEM
  180. PKMAP_BASE, PKMAP_BASE + LAST_PKMAP * PAGE_SIZE,
  181. (LAST_PKMAP * PAGE_SIZE) >> 10,
  182. #endif
  183. CONSISTENT_BASE, CONSISTENT_END,
  184. ((CONSISTENT_END) - (CONSISTENT_BASE)) >> 20, VMALLOC_START,
  185. (unsigned long)VMALLOC_END, (VMALLOC_END - VMALLOC_START) >> 20,
  186. (unsigned long)__va(memory_start), (unsigned long)high_memory,
  187. ((unsigned long)high_memory -
  188. (unsigned long)__va(memory_start)) >> 20,
  189. (unsigned long)&__init_begin, (unsigned long)&__init_end,
  190. ((unsigned long)&__init_end -
  191. (unsigned long)&__init_begin) >> 10, (unsigned long)&_etext,
  192. (unsigned long)&_edata,
  193. ((unsigned long)&_edata - (unsigned long)&_etext) >> 10,
  194. (unsigned long)&_text, (unsigned long)&_etext,
  195. ((unsigned long)&_etext - (unsigned long)&_text) >> 10);
  196. /*
  197. * Check boundaries twice: Some fundamental inconsistencies can
  198. * be detected at build time already.
  199. */
  200. #ifdef CONFIG_HIGHMEM
  201. BUILD_BUG_ON(PKMAP_BASE + LAST_PKMAP * PAGE_SIZE > FIXADDR_START);
  202. BUILD_BUG_ON((CONSISTENT_END) > PKMAP_BASE);
  203. #endif
  204. BUILD_BUG_ON(VMALLOC_END > CONSISTENT_BASE);
  205. BUILD_BUG_ON(VMALLOC_START >= VMALLOC_END);
  206. #ifdef CONFIG_HIGHMEM
  207. BUG_ON(PKMAP_BASE + LAST_PKMAP * PAGE_SIZE > FIXADDR_START);
  208. BUG_ON(CONSISTENT_END > PKMAP_BASE);
  209. #endif
  210. BUG_ON(VMALLOC_END > CONSISTENT_BASE);
  211. BUG_ON(VMALLOC_START >= VMALLOC_END);
  212. BUG_ON((unsigned long)high_memory > VMALLOC_START);
  213. return;
  214. }
  215. void free_initmem(void)
  216. {
  217. free_initmem_default(-1);
  218. }
  219. #ifdef CONFIG_BLK_DEV_INITRD
  220. void free_initrd_mem(unsigned long start, unsigned long end)
  221. {
  222. free_reserved_area((void *)start, (void *)end, -1, "initrd");
  223. }
  224. #endif
  225. void __set_fixmap(enum fixed_addresses idx,
  226. phys_addr_t phys, pgprot_t flags)
  227. {
  228. unsigned long addr = __fix_to_virt(idx);
  229. pte_t *pte;
  230. BUG_ON(idx <= FIX_HOLE || idx >= __end_of_fixed_addresses);
  231. pte = (pte_t *)&fixmap_pmd_p[pte_index(addr)];;
  232. if (pgprot_val(flags)) {
  233. set_pte(pte, pfn_pte(phys >> PAGE_SHIFT, flags));
  234. } else {
  235. pte_clear(&init_mm, addr, pte);
  236. flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
  237. }
  238. }