init.c 11 KB

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