init.c 11 KB

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