init.c 12 KB

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