init.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. #include <linux/gfp.h>
  2. #include <linux/initrd.h>
  3. #include <linux/ioport.h>
  4. #include <linux/swap.h>
  5. #include <linux/memblock.h>
  6. #include <linux/bootmem.h> /* for max_low_pfn */
  7. #include <asm/cacheflush.h>
  8. #include <asm/e820.h>
  9. #include <asm/init.h>
  10. #include <asm/page.h>
  11. #include <asm/page_types.h>
  12. #include <asm/sections.h>
  13. #include <asm/setup.h>
  14. #include <asm/system.h>
  15. #include <asm/tlbflush.h>
  16. #include <asm/tlb.h>
  17. #include <asm/proto.h>
  18. #include <asm/dma.h> /* for MAX_DMA_PFN */
  19. unsigned long __initdata pgt_buf_start;
  20. unsigned long __meminitdata pgt_buf_end;
  21. unsigned long __meminitdata pgt_buf_top;
  22. int after_bootmem;
  23. int direct_gbpages
  24. #ifdef CONFIG_DIRECT_GBPAGES
  25. = 1
  26. #endif
  27. ;
  28. static void __init find_early_table_space(unsigned long end, int use_pse,
  29. int use_gbpages)
  30. {
  31. unsigned long puds, pmds, ptes, tables, start = 0, good_end = end;
  32. phys_addr_t base;
  33. puds = (end + PUD_SIZE - 1) >> PUD_SHIFT;
  34. tables = roundup(puds * sizeof(pud_t), PAGE_SIZE);
  35. if (use_gbpages) {
  36. unsigned long extra;
  37. extra = end - ((end>>PUD_SHIFT) << PUD_SHIFT);
  38. pmds = (extra + PMD_SIZE - 1) >> PMD_SHIFT;
  39. } else
  40. pmds = (end + PMD_SIZE - 1) >> PMD_SHIFT;
  41. tables += roundup(pmds * sizeof(pmd_t), PAGE_SIZE);
  42. if (use_pse) {
  43. unsigned long extra;
  44. extra = end - ((end>>PMD_SHIFT) << PMD_SHIFT);
  45. #ifdef CONFIG_X86_32
  46. extra += PMD_SIZE;
  47. #endif
  48. ptes = (extra + PAGE_SIZE - 1) >> PAGE_SHIFT;
  49. } else
  50. ptes = (end + PAGE_SIZE - 1) >> PAGE_SHIFT;
  51. tables += roundup(ptes * sizeof(pte_t), PAGE_SIZE);
  52. #ifdef CONFIG_X86_32
  53. /* for fixmap */
  54. tables += roundup(__end_of_fixed_addresses * sizeof(pte_t), PAGE_SIZE);
  55. #endif
  56. good_end = max_pfn_mapped << PAGE_SHIFT;
  57. base = memblock_find_in_range(start, good_end, tables, PAGE_SIZE);
  58. if (!base)
  59. panic("Cannot find space for the kernel page tables");
  60. pgt_buf_start = base >> PAGE_SHIFT;
  61. pgt_buf_end = pgt_buf_start;
  62. pgt_buf_top = pgt_buf_start + (tables >> PAGE_SHIFT);
  63. printk(KERN_DEBUG "kernel direct mapping tables up to %lx @ %lx-%lx\n",
  64. end, pgt_buf_start << PAGE_SHIFT, pgt_buf_top << PAGE_SHIFT);
  65. }
  66. void __init native_pagetable_reserve(u64 start, u64 end)
  67. {
  68. memblock_reserve(start, end - start);
  69. }
  70. struct map_range {
  71. unsigned long start;
  72. unsigned long end;
  73. unsigned page_size_mask;
  74. };
  75. #ifdef CONFIG_X86_32
  76. #define NR_RANGE_MR 3
  77. #else /* CONFIG_X86_64 */
  78. #define NR_RANGE_MR 5
  79. #endif
  80. static int __meminit save_mr(struct map_range *mr, int nr_range,
  81. unsigned long start_pfn, unsigned long end_pfn,
  82. unsigned long page_size_mask)
  83. {
  84. if (start_pfn < end_pfn) {
  85. if (nr_range >= NR_RANGE_MR)
  86. panic("run out of range for init_memory_mapping\n");
  87. mr[nr_range].start = start_pfn<<PAGE_SHIFT;
  88. mr[nr_range].end = end_pfn<<PAGE_SHIFT;
  89. mr[nr_range].page_size_mask = page_size_mask;
  90. nr_range++;
  91. }
  92. return nr_range;
  93. }
  94. /*
  95. * Setup the direct mapping of the physical memory at PAGE_OFFSET.
  96. * This runs before bootmem is initialized and gets pages directly from
  97. * the physical memory. To access them they are temporarily mapped.
  98. */
  99. unsigned long __init_refok init_memory_mapping(unsigned long start,
  100. unsigned long end)
  101. {
  102. unsigned long page_size_mask = 0;
  103. unsigned long start_pfn, end_pfn;
  104. unsigned long ret = 0;
  105. unsigned long pos;
  106. struct map_range mr[NR_RANGE_MR];
  107. int nr_range, i;
  108. int use_pse, use_gbpages;
  109. printk(KERN_INFO "init_memory_mapping: %016lx-%016lx\n", start, end);
  110. #if defined(CONFIG_DEBUG_PAGEALLOC) || defined(CONFIG_KMEMCHECK)
  111. /*
  112. * For CONFIG_DEBUG_PAGEALLOC, identity mapping will use small pages.
  113. * This will simplify cpa(), which otherwise needs to support splitting
  114. * large pages into small in interrupt context, etc.
  115. */
  116. use_pse = use_gbpages = 0;
  117. #else
  118. use_pse = cpu_has_pse;
  119. use_gbpages = direct_gbpages;
  120. #endif
  121. /* Enable PSE if available */
  122. if (cpu_has_pse)
  123. set_in_cr4(X86_CR4_PSE);
  124. /* Enable PGE if available */
  125. if (cpu_has_pge) {
  126. set_in_cr4(X86_CR4_PGE);
  127. __supported_pte_mask |= _PAGE_GLOBAL;
  128. }
  129. if (use_gbpages)
  130. page_size_mask |= 1 << PG_LEVEL_1G;
  131. if (use_pse)
  132. page_size_mask |= 1 << PG_LEVEL_2M;
  133. memset(mr, 0, sizeof(mr));
  134. nr_range = 0;
  135. /* head if not big page alignment ? */
  136. start_pfn = start >> PAGE_SHIFT;
  137. pos = start_pfn << PAGE_SHIFT;
  138. #ifdef CONFIG_X86_32
  139. /*
  140. * Don't use a large page for the first 2/4MB of memory
  141. * because there are often fixed size MTRRs in there
  142. * and overlapping MTRRs into large pages can cause
  143. * slowdowns.
  144. */
  145. if (pos == 0)
  146. end_pfn = 1<<(PMD_SHIFT - PAGE_SHIFT);
  147. else
  148. end_pfn = ((pos + (PMD_SIZE - 1))>>PMD_SHIFT)
  149. << (PMD_SHIFT - PAGE_SHIFT);
  150. #else /* CONFIG_X86_64 */
  151. end_pfn = ((pos + (PMD_SIZE - 1)) >> PMD_SHIFT)
  152. << (PMD_SHIFT - PAGE_SHIFT);
  153. #endif
  154. if (end_pfn > (end >> PAGE_SHIFT))
  155. end_pfn = end >> PAGE_SHIFT;
  156. if (start_pfn < end_pfn) {
  157. nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, 0);
  158. pos = end_pfn << PAGE_SHIFT;
  159. }
  160. /* big page (2M) range */
  161. start_pfn = ((pos + (PMD_SIZE - 1))>>PMD_SHIFT)
  162. << (PMD_SHIFT - PAGE_SHIFT);
  163. #ifdef CONFIG_X86_32
  164. end_pfn = (end>>PMD_SHIFT) << (PMD_SHIFT - PAGE_SHIFT);
  165. #else /* CONFIG_X86_64 */
  166. end_pfn = ((pos + (PUD_SIZE - 1))>>PUD_SHIFT)
  167. << (PUD_SHIFT - PAGE_SHIFT);
  168. if (end_pfn > ((end>>PMD_SHIFT)<<(PMD_SHIFT - PAGE_SHIFT)))
  169. end_pfn = ((end>>PMD_SHIFT)<<(PMD_SHIFT - PAGE_SHIFT));
  170. #endif
  171. if (start_pfn < end_pfn) {
  172. nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
  173. page_size_mask & (1<<PG_LEVEL_2M));
  174. pos = end_pfn << PAGE_SHIFT;
  175. }
  176. #ifdef CONFIG_X86_64
  177. /* big page (1G) range */
  178. start_pfn = ((pos + (PUD_SIZE - 1))>>PUD_SHIFT)
  179. << (PUD_SHIFT - PAGE_SHIFT);
  180. end_pfn = (end >> PUD_SHIFT) << (PUD_SHIFT - PAGE_SHIFT);
  181. if (start_pfn < end_pfn) {
  182. nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
  183. page_size_mask &
  184. ((1<<PG_LEVEL_2M)|(1<<PG_LEVEL_1G)));
  185. pos = end_pfn << PAGE_SHIFT;
  186. }
  187. /* tail is not big page (1G) alignment */
  188. start_pfn = ((pos + (PMD_SIZE - 1))>>PMD_SHIFT)
  189. << (PMD_SHIFT - PAGE_SHIFT);
  190. end_pfn = (end >> PMD_SHIFT) << (PMD_SHIFT - PAGE_SHIFT);
  191. if (start_pfn < end_pfn) {
  192. nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
  193. page_size_mask & (1<<PG_LEVEL_2M));
  194. pos = end_pfn << PAGE_SHIFT;
  195. }
  196. #endif
  197. /* tail is not big page (2M) alignment */
  198. start_pfn = pos>>PAGE_SHIFT;
  199. end_pfn = end>>PAGE_SHIFT;
  200. nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, 0);
  201. /* try to merge same page size and continuous */
  202. for (i = 0; nr_range > 1 && i < nr_range - 1; i++) {
  203. unsigned long old_start;
  204. if (mr[i].end != mr[i+1].start ||
  205. mr[i].page_size_mask != mr[i+1].page_size_mask)
  206. continue;
  207. /* move it */
  208. old_start = mr[i].start;
  209. memmove(&mr[i], &mr[i+1],
  210. (nr_range - 1 - i) * sizeof(struct map_range));
  211. mr[i--].start = old_start;
  212. nr_range--;
  213. }
  214. for (i = 0; i < nr_range; i++)
  215. printk(KERN_DEBUG " %010lx - %010lx page %s\n",
  216. mr[i].start, mr[i].end,
  217. (mr[i].page_size_mask & (1<<PG_LEVEL_1G))?"1G":(
  218. (mr[i].page_size_mask & (1<<PG_LEVEL_2M))?"2M":"4k"));
  219. /*
  220. * Find space for the kernel direct mapping tables.
  221. *
  222. * Later we should allocate these tables in the local node of the
  223. * memory mapped. Unfortunately this is done currently before the
  224. * nodes are discovered.
  225. */
  226. if (!after_bootmem)
  227. find_early_table_space(end, use_pse, use_gbpages);
  228. for (i = 0; i < nr_range; i++)
  229. ret = kernel_physical_mapping_init(mr[i].start, mr[i].end,
  230. mr[i].page_size_mask);
  231. #ifdef CONFIG_X86_32
  232. early_ioremap_page_table_range_init();
  233. load_cr3(swapper_pg_dir);
  234. #endif
  235. __flush_tlb_all();
  236. /*
  237. * Reserve the kernel pagetable pages we used (pgt_buf_start -
  238. * pgt_buf_end) and free the other ones (pgt_buf_end - pgt_buf_top)
  239. * so that they can be reused for other purposes.
  240. *
  241. * On native it just means calling memblock_reserve, on Xen it also
  242. * means marking RW the pagetable pages that we allocated before
  243. * but that haven't been used.
  244. *
  245. * In fact on xen we mark RO the whole range pgt_buf_start -
  246. * pgt_buf_top, because we have to make sure that when
  247. * init_memory_mapping reaches the pagetable pages area, it maps
  248. * RO all the pagetable pages, including the ones that are beyond
  249. * pgt_buf_end at that time.
  250. */
  251. if (!after_bootmem && pgt_buf_end > pgt_buf_start)
  252. x86_init.mapping.pagetable_reserve(PFN_PHYS(pgt_buf_start),
  253. PFN_PHYS(pgt_buf_end));
  254. if (!after_bootmem)
  255. early_memtest(start, end);
  256. return ret >> PAGE_SHIFT;
  257. }
  258. /*
  259. * devmem_is_allowed() checks to see if /dev/mem access to a certain address
  260. * is valid. The argument is a physical page number.
  261. *
  262. *
  263. * On x86, access has to be given to the first megabyte of ram because that area
  264. * contains bios code and data regions used by X and dosemu and similar apps.
  265. * Access has to be given to non-kernel-ram areas as well, these contain the PCI
  266. * mmio resources as well as potential bios/acpi data regions.
  267. */
  268. int devmem_is_allowed(unsigned long pagenr)
  269. {
  270. if (pagenr <= 256)
  271. return 1;
  272. if (iomem_is_exclusive(pagenr << PAGE_SHIFT))
  273. return 0;
  274. if (!page_is_ram(pagenr))
  275. return 1;
  276. return 0;
  277. }
  278. void free_init_pages(char *what, unsigned long begin, unsigned long end)
  279. {
  280. unsigned long addr;
  281. unsigned long begin_aligned, end_aligned;
  282. /* Make sure boundaries are page aligned */
  283. begin_aligned = PAGE_ALIGN(begin);
  284. end_aligned = end & PAGE_MASK;
  285. if (WARN_ON(begin_aligned != begin || end_aligned != end)) {
  286. begin = begin_aligned;
  287. end = end_aligned;
  288. }
  289. if (begin >= end)
  290. return;
  291. addr = begin;
  292. /*
  293. * If debugging page accesses then do not free this memory but
  294. * mark them not present - any buggy init-section access will
  295. * create a kernel page fault:
  296. */
  297. #ifdef CONFIG_DEBUG_PAGEALLOC
  298. printk(KERN_INFO "debug: unmapping init memory %08lx..%08lx\n",
  299. begin, end);
  300. set_memory_np(begin, (end - begin) >> PAGE_SHIFT);
  301. #else
  302. /*
  303. * We just marked the kernel text read only above, now that
  304. * we are going to free part of that, we need to make that
  305. * writeable and non-executable first.
  306. */
  307. set_memory_nx(begin, (end - begin) >> PAGE_SHIFT);
  308. set_memory_rw(begin, (end - begin) >> PAGE_SHIFT);
  309. printk(KERN_INFO "Freeing %s: %luk freed\n", what, (end - begin) >> 10);
  310. for (; addr < end; addr += PAGE_SIZE) {
  311. ClearPageReserved(virt_to_page(addr));
  312. init_page_count(virt_to_page(addr));
  313. memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE);
  314. free_page(addr);
  315. totalram_pages++;
  316. }
  317. #endif
  318. }
  319. void free_initmem(void)
  320. {
  321. free_init_pages("unused kernel memory",
  322. (unsigned long)(&__init_begin),
  323. (unsigned long)(&__init_end));
  324. }
  325. #ifdef CONFIG_BLK_DEV_INITRD
  326. void free_initrd_mem(unsigned long start, unsigned long end)
  327. {
  328. /*
  329. * end could be not aligned, and We can not align that,
  330. * decompresser could be confused by aligned initrd_end
  331. * We already reserve the end partial page before in
  332. * - i386_start_kernel()
  333. * - x86_64_start_kernel()
  334. * - relocate_initrd()
  335. * So here We can do PAGE_ALIGN() safely to get partial page to be freed
  336. */
  337. free_init_pages("initrd memory", start, PAGE_ALIGN(end));
  338. }
  339. #endif
  340. void __init zone_sizes_init(void)
  341. {
  342. unsigned long max_zone_pfns[MAX_NR_ZONES];
  343. memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
  344. #ifdef CONFIG_ZONE_DMA
  345. max_zone_pfns[ZONE_DMA] = MAX_DMA_PFN;
  346. #endif
  347. #ifdef CONFIG_ZONE_DMA32
  348. max_zone_pfns[ZONE_DMA32] = MAX_DMA32_PFN;
  349. #endif
  350. max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
  351. #ifdef CONFIG_HIGHMEM
  352. max_zone_pfns[ZONE_HIGHMEM] = max_pfn;
  353. #endif
  354. free_area_init_nodes(max_zone_pfns);
  355. }