init.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  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. #include <asm/microcode.h>
  19. #include <asm/kaslr.h>
  20. /*
  21. * We need to define the tracepoints somewhere, and tlb.c
  22. * is only compied when SMP=y.
  23. */
  24. #define CREATE_TRACE_POINTS
  25. #include <trace/events/tlb.h>
  26. #include "mm_internal.h"
  27. /*
  28. * Tables translating between page_cache_type_t and pte encoding.
  29. *
  30. * The default values are defined statically as minimal supported mode;
  31. * WC and WT fall back to UC-. pat_init() updates these values to support
  32. * more cache modes, WC and WT, when it is safe to do so. See pat_init()
  33. * for the details. Note, __early_ioremap() used during early boot-time
  34. * takes pgprot_t (pte encoding) and does not use these tables.
  35. *
  36. * Index into __cachemode2pte_tbl[] is the cachemode.
  37. *
  38. * Index into __pte2cachemode_tbl[] are the caching attribute bits of the pte
  39. * (_PAGE_PWT, _PAGE_PCD, _PAGE_PAT) at index bit positions 0, 1, 2.
  40. */
  41. uint16_t __cachemode2pte_tbl[_PAGE_CACHE_MODE_NUM] = {
  42. [_PAGE_CACHE_MODE_WB ] = 0 | 0 ,
  43. [_PAGE_CACHE_MODE_WC ] = 0 | _PAGE_PCD,
  44. [_PAGE_CACHE_MODE_UC_MINUS] = 0 | _PAGE_PCD,
  45. [_PAGE_CACHE_MODE_UC ] = _PAGE_PWT | _PAGE_PCD,
  46. [_PAGE_CACHE_MODE_WT ] = 0 | _PAGE_PCD,
  47. [_PAGE_CACHE_MODE_WP ] = 0 | _PAGE_PCD,
  48. };
  49. EXPORT_SYMBOL(__cachemode2pte_tbl);
  50. uint8_t __pte2cachemode_tbl[8] = {
  51. [__pte2cm_idx( 0 | 0 | 0 )] = _PAGE_CACHE_MODE_WB,
  52. [__pte2cm_idx(_PAGE_PWT | 0 | 0 )] = _PAGE_CACHE_MODE_UC_MINUS,
  53. [__pte2cm_idx( 0 | _PAGE_PCD | 0 )] = _PAGE_CACHE_MODE_UC_MINUS,
  54. [__pte2cm_idx(_PAGE_PWT | _PAGE_PCD | 0 )] = _PAGE_CACHE_MODE_UC,
  55. [__pte2cm_idx( 0 | 0 | _PAGE_PAT)] = _PAGE_CACHE_MODE_WB,
  56. [__pte2cm_idx(_PAGE_PWT | 0 | _PAGE_PAT)] = _PAGE_CACHE_MODE_UC_MINUS,
  57. [__pte2cm_idx(0 | _PAGE_PCD | _PAGE_PAT)] = _PAGE_CACHE_MODE_UC_MINUS,
  58. [__pte2cm_idx(_PAGE_PWT | _PAGE_PCD | _PAGE_PAT)] = _PAGE_CACHE_MODE_UC,
  59. };
  60. EXPORT_SYMBOL(__pte2cachemode_tbl);
  61. static unsigned long __initdata pgt_buf_start;
  62. static unsigned long __initdata pgt_buf_end;
  63. static unsigned long __initdata pgt_buf_top;
  64. static unsigned long min_pfn_mapped;
  65. static bool __initdata can_use_brk_pgt = true;
  66. /*
  67. * Pages returned are already directly mapped.
  68. *
  69. * Changing that is likely to break Xen, see commit:
  70. *
  71. * 279b706 x86,xen: introduce x86_init.mapping.pagetable_reserve
  72. *
  73. * for detailed information.
  74. */
  75. __ref void *alloc_low_pages(unsigned int num)
  76. {
  77. unsigned long pfn;
  78. int i;
  79. if (after_bootmem) {
  80. unsigned int order;
  81. order = get_order((unsigned long)num << PAGE_SHIFT);
  82. return (void *)__get_free_pages(GFP_ATOMIC | __GFP_NOTRACK |
  83. __GFP_ZERO, order);
  84. }
  85. if ((pgt_buf_end + num) > pgt_buf_top || !can_use_brk_pgt) {
  86. unsigned long ret;
  87. if (min_pfn_mapped >= max_pfn_mapped)
  88. panic("alloc_low_pages: ran out of memory");
  89. ret = memblock_find_in_range(min_pfn_mapped << PAGE_SHIFT,
  90. max_pfn_mapped << PAGE_SHIFT,
  91. PAGE_SIZE * num , PAGE_SIZE);
  92. if (!ret)
  93. panic("alloc_low_pages: can not alloc memory");
  94. memblock_reserve(ret, PAGE_SIZE * num);
  95. pfn = ret >> PAGE_SHIFT;
  96. } else {
  97. pfn = pgt_buf_end;
  98. pgt_buf_end += num;
  99. printk(KERN_DEBUG "BRK [%#010lx, %#010lx] PGTABLE\n",
  100. pfn << PAGE_SHIFT, (pgt_buf_end << PAGE_SHIFT) - 1);
  101. }
  102. for (i = 0; i < num; i++) {
  103. void *adr;
  104. adr = __va((pfn + i) << PAGE_SHIFT);
  105. clear_page(adr);
  106. }
  107. return __va(pfn << PAGE_SHIFT);
  108. }
  109. /*
  110. * By default need 3 4k for initial PMD_SIZE, 3 4k for 0-ISA_END_ADDRESS.
  111. * With KASLR memory randomization, depending on the machine e820 memory
  112. * and the PUD alignment. We may need twice more pages when KASLR memory
  113. * randomization is enabled.
  114. */
  115. #ifndef CONFIG_RANDOMIZE_MEMORY
  116. #define INIT_PGD_PAGE_COUNT 6
  117. #else
  118. #define INIT_PGD_PAGE_COUNT 12
  119. #endif
  120. #define INIT_PGT_BUF_SIZE (INIT_PGD_PAGE_COUNT * PAGE_SIZE)
  121. RESERVE_BRK(early_pgt_alloc, INIT_PGT_BUF_SIZE);
  122. void __init early_alloc_pgt_buf(void)
  123. {
  124. unsigned long tables = INIT_PGT_BUF_SIZE;
  125. phys_addr_t base;
  126. base = __pa(extend_brk(tables, PAGE_SIZE));
  127. pgt_buf_start = base >> PAGE_SHIFT;
  128. pgt_buf_end = pgt_buf_start;
  129. pgt_buf_top = pgt_buf_start + (tables >> PAGE_SHIFT);
  130. }
  131. int after_bootmem;
  132. early_param_on_off("gbpages", "nogbpages", direct_gbpages, CONFIG_X86_DIRECT_GBPAGES);
  133. struct map_range {
  134. unsigned long start;
  135. unsigned long end;
  136. unsigned page_size_mask;
  137. };
  138. static int page_size_mask;
  139. static void __init probe_page_size_mask(void)
  140. {
  141. #if !defined(CONFIG_KMEMCHECK)
  142. /*
  143. * For CONFIG_KMEMCHECK or pagealloc debugging, identity mapping will
  144. * use small pages.
  145. * This will simplify cpa(), which otherwise needs to support splitting
  146. * large pages into small in interrupt context, etc.
  147. */
  148. if (boot_cpu_has(X86_FEATURE_PSE) && !debug_pagealloc_enabled())
  149. page_size_mask |= 1 << PG_LEVEL_2M;
  150. #endif
  151. /* Enable PSE if available */
  152. if (boot_cpu_has(X86_FEATURE_PSE))
  153. cr4_set_bits_and_update_boot(X86_CR4_PSE);
  154. /* Enable PGE if available */
  155. if (boot_cpu_has(X86_FEATURE_PGE) && !kaiser_enabled) {
  156. cr4_set_bits_and_update_boot(X86_CR4_PGE);
  157. __supported_pte_mask |= _PAGE_GLOBAL;
  158. } else
  159. __supported_pte_mask &= ~_PAGE_GLOBAL;
  160. /* Enable 1 GB linear kernel mappings if available: */
  161. if (direct_gbpages && boot_cpu_has(X86_FEATURE_GBPAGES)) {
  162. printk(KERN_INFO "Using GB pages for direct mapping\n");
  163. page_size_mask |= 1 << PG_LEVEL_1G;
  164. } else {
  165. direct_gbpages = 0;
  166. }
  167. }
  168. #ifdef CONFIG_X86_32
  169. #define NR_RANGE_MR 3
  170. #else /* CONFIG_X86_64 */
  171. #define NR_RANGE_MR 5
  172. #endif
  173. static int __meminit save_mr(struct map_range *mr, int nr_range,
  174. unsigned long start_pfn, unsigned long end_pfn,
  175. unsigned long page_size_mask)
  176. {
  177. if (start_pfn < end_pfn) {
  178. if (nr_range >= NR_RANGE_MR)
  179. panic("run out of range for init_memory_mapping\n");
  180. mr[nr_range].start = start_pfn<<PAGE_SHIFT;
  181. mr[nr_range].end = end_pfn<<PAGE_SHIFT;
  182. mr[nr_range].page_size_mask = page_size_mask;
  183. nr_range++;
  184. }
  185. return nr_range;
  186. }
  187. /*
  188. * adjust the page_size_mask for small range to go with
  189. * big page size instead small one if nearby are ram too.
  190. */
  191. static void __ref adjust_range_page_size_mask(struct map_range *mr,
  192. int nr_range)
  193. {
  194. int i;
  195. for (i = 0; i < nr_range; i++) {
  196. if ((page_size_mask & (1<<PG_LEVEL_2M)) &&
  197. !(mr[i].page_size_mask & (1<<PG_LEVEL_2M))) {
  198. unsigned long start = round_down(mr[i].start, PMD_SIZE);
  199. unsigned long end = round_up(mr[i].end, PMD_SIZE);
  200. #ifdef CONFIG_X86_32
  201. if ((end >> PAGE_SHIFT) > max_low_pfn)
  202. continue;
  203. #endif
  204. if (memblock_is_region_memory(start, end - start))
  205. mr[i].page_size_mask |= 1<<PG_LEVEL_2M;
  206. }
  207. if ((page_size_mask & (1<<PG_LEVEL_1G)) &&
  208. !(mr[i].page_size_mask & (1<<PG_LEVEL_1G))) {
  209. unsigned long start = round_down(mr[i].start, PUD_SIZE);
  210. unsigned long end = round_up(mr[i].end, PUD_SIZE);
  211. if (memblock_is_region_memory(start, end - start))
  212. mr[i].page_size_mask |= 1<<PG_LEVEL_1G;
  213. }
  214. }
  215. }
  216. static const char *page_size_string(struct map_range *mr)
  217. {
  218. static const char str_1g[] = "1G";
  219. static const char str_2m[] = "2M";
  220. static const char str_4m[] = "4M";
  221. static const char str_4k[] = "4k";
  222. if (mr->page_size_mask & (1<<PG_LEVEL_1G))
  223. return str_1g;
  224. /*
  225. * 32-bit without PAE has a 4M large page size.
  226. * PG_LEVEL_2M is misnamed, but we can at least
  227. * print out the right size in the string.
  228. */
  229. if (IS_ENABLED(CONFIG_X86_32) &&
  230. !IS_ENABLED(CONFIG_X86_PAE) &&
  231. mr->page_size_mask & (1<<PG_LEVEL_2M))
  232. return str_4m;
  233. if (mr->page_size_mask & (1<<PG_LEVEL_2M))
  234. return str_2m;
  235. return str_4k;
  236. }
  237. static int __meminit split_mem_range(struct map_range *mr, int nr_range,
  238. unsigned long start,
  239. unsigned long end)
  240. {
  241. unsigned long start_pfn, end_pfn, limit_pfn;
  242. unsigned long pfn;
  243. int i;
  244. limit_pfn = PFN_DOWN(end);
  245. /* head if not big page alignment ? */
  246. pfn = start_pfn = PFN_DOWN(start);
  247. #ifdef CONFIG_X86_32
  248. /*
  249. * Don't use a large page for the first 2/4MB of memory
  250. * because there are often fixed size MTRRs in there
  251. * and overlapping MTRRs into large pages can cause
  252. * slowdowns.
  253. */
  254. if (pfn == 0)
  255. end_pfn = PFN_DOWN(PMD_SIZE);
  256. else
  257. end_pfn = round_up(pfn, PFN_DOWN(PMD_SIZE));
  258. #else /* CONFIG_X86_64 */
  259. end_pfn = round_up(pfn, PFN_DOWN(PMD_SIZE));
  260. #endif
  261. if (end_pfn > limit_pfn)
  262. end_pfn = limit_pfn;
  263. if (start_pfn < end_pfn) {
  264. nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, 0);
  265. pfn = end_pfn;
  266. }
  267. /* big page (2M) range */
  268. start_pfn = round_up(pfn, PFN_DOWN(PMD_SIZE));
  269. #ifdef CONFIG_X86_32
  270. end_pfn = round_down(limit_pfn, PFN_DOWN(PMD_SIZE));
  271. #else /* CONFIG_X86_64 */
  272. end_pfn = round_up(pfn, PFN_DOWN(PUD_SIZE));
  273. if (end_pfn > round_down(limit_pfn, PFN_DOWN(PMD_SIZE)))
  274. end_pfn = round_down(limit_pfn, PFN_DOWN(PMD_SIZE));
  275. #endif
  276. if (start_pfn < end_pfn) {
  277. nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
  278. page_size_mask & (1<<PG_LEVEL_2M));
  279. pfn = end_pfn;
  280. }
  281. #ifdef CONFIG_X86_64
  282. /* big page (1G) range */
  283. start_pfn = round_up(pfn, PFN_DOWN(PUD_SIZE));
  284. end_pfn = round_down(limit_pfn, PFN_DOWN(PUD_SIZE));
  285. if (start_pfn < end_pfn) {
  286. nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
  287. page_size_mask &
  288. ((1<<PG_LEVEL_2M)|(1<<PG_LEVEL_1G)));
  289. pfn = end_pfn;
  290. }
  291. /* tail is not big page (1G) alignment */
  292. start_pfn = round_up(pfn, PFN_DOWN(PMD_SIZE));
  293. end_pfn = round_down(limit_pfn, PFN_DOWN(PMD_SIZE));
  294. if (start_pfn < end_pfn) {
  295. nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
  296. page_size_mask & (1<<PG_LEVEL_2M));
  297. pfn = end_pfn;
  298. }
  299. #endif
  300. /* tail is not big page (2M) alignment */
  301. start_pfn = pfn;
  302. end_pfn = limit_pfn;
  303. nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, 0);
  304. if (!after_bootmem)
  305. adjust_range_page_size_mask(mr, nr_range);
  306. /* try to merge same page size and continuous */
  307. for (i = 0; nr_range > 1 && i < nr_range - 1; i++) {
  308. unsigned long old_start;
  309. if (mr[i].end != mr[i+1].start ||
  310. mr[i].page_size_mask != mr[i+1].page_size_mask)
  311. continue;
  312. /* move it */
  313. old_start = mr[i].start;
  314. memmove(&mr[i], &mr[i+1],
  315. (nr_range - 1 - i) * sizeof(struct map_range));
  316. mr[i--].start = old_start;
  317. nr_range--;
  318. }
  319. for (i = 0; i < nr_range; i++)
  320. pr_debug(" [mem %#010lx-%#010lx] page %s\n",
  321. mr[i].start, mr[i].end - 1,
  322. page_size_string(&mr[i]));
  323. return nr_range;
  324. }
  325. struct range pfn_mapped[E820_X_MAX];
  326. int nr_pfn_mapped;
  327. static void add_pfn_range_mapped(unsigned long start_pfn, unsigned long end_pfn)
  328. {
  329. nr_pfn_mapped = add_range_with_merge(pfn_mapped, E820_X_MAX,
  330. nr_pfn_mapped, start_pfn, end_pfn);
  331. nr_pfn_mapped = clean_sort_range(pfn_mapped, E820_X_MAX);
  332. max_pfn_mapped = max(max_pfn_mapped, end_pfn);
  333. if (start_pfn < (1UL<<(32-PAGE_SHIFT)))
  334. max_low_pfn_mapped = max(max_low_pfn_mapped,
  335. min(end_pfn, 1UL<<(32-PAGE_SHIFT)));
  336. }
  337. bool pfn_range_is_mapped(unsigned long start_pfn, unsigned long end_pfn)
  338. {
  339. int i;
  340. for (i = 0; i < nr_pfn_mapped; i++)
  341. if ((start_pfn >= pfn_mapped[i].start) &&
  342. (end_pfn <= pfn_mapped[i].end))
  343. return true;
  344. return false;
  345. }
  346. /*
  347. * Setup the direct mapping of the physical memory at PAGE_OFFSET.
  348. * This runs before bootmem is initialized and gets pages directly from
  349. * the physical memory. To access them they are temporarily mapped.
  350. */
  351. unsigned long __ref init_memory_mapping(unsigned long start,
  352. unsigned long end)
  353. {
  354. struct map_range mr[NR_RANGE_MR];
  355. unsigned long ret = 0;
  356. int nr_range, i;
  357. pr_debug("init_memory_mapping: [mem %#010lx-%#010lx]\n",
  358. start, end - 1);
  359. memset(mr, 0, sizeof(mr));
  360. nr_range = split_mem_range(mr, 0, start, end);
  361. for (i = 0; i < nr_range; i++)
  362. ret = kernel_physical_mapping_init(mr[i].start, mr[i].end,
  363. mr[i].page_size_mask);
  364. add_pfn_range_mapped(start >> PAGE_SHIFT, ret >> PAGE_SHIFT);
  365. return ret >> PAGE_SHIFT;
  366. }
  367. /*
  368. * We need to iterate through the E820 memory map and create direct mappings
  369. * for only E820_RAM and E820_KERN_RESERVED regions. We cannot simply
  370. * create direct mappings for all pfns from [0 to max_low_pfn) and
  371. * [4GB to max_pfn) because of possible memory holes in high addresses
  372. * that cannot be marked as UC by fixed/variable range MTRRs.
  373. * Depending on the alignment of E820 ranges, this may possibly result
  374. * in using smaller size (i.e. 4K instead of 2M or 1G) page tables.
  375. *
  376. * init_mem_mapping() calls init_range_memory_mapping() with big range.
  377. * That range would have hole in the middle or ends, and only ram parts
  378. * will be mapped in init_range_memory_mapping().
  379. */
  380. static unsigned long __init init_range_memory_mapping(
  381. unsigned long r_start,
  382. unsigned long r_end)
  383. {
  384. unsigned long start_pfn, end_pfn;
  385. unsigned long mapped_ram_size = 0;
  386. int i;
  387. for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, NULL) {
  388. u64 start = clamp_val(PFN_PHYS(start_pfn), r_start, r_end);
  389. u64 end = clamp_val(PFN_PHYS(end_pfn), r_start, r_end);
  390. if (start >= end)
  391. continue;
  392. /*
  393. * if it is overlapping with brk pgt, we need to
  394. * alloc pgt buf from memblock instead.
  395. */
  396. can_use_brk_pgt = max(start, (u64)pgt_buf_end<<PAGE_SHIFT) >=
  397. min(end, (u64)pgt_buf_top<<PAGE_SHIFT);
  398. init_memory_mapping(start, end);
  399. mapped_ram_size += end - start;
  400. can_use_brk_pgt = true;
  401. }
  402. return mapped_ram_size;
  403. }
  404. static unsigned long __init get_new_step_size(unsigned long step_size)
  405. {
  406. /*
  407. * Initial mapped size is PMD_SIZE (2M).
  408. * We can not set step_size to be PUD_SIZE (1G) yet.
  409. * In worse case, when we cross the 1G boundary, and
  410. * PG_LEVEL_2M is not set, we will need 1+1+512 pages (2M + 8k)
  411. * to map 1G range with PTE. Hence we use one less than the
  412. * difference of page table level shifts.
  413. *
  414. * Don't need to worry about overflow in the top-down case, on 32bit,
  415. * when step_size is 0, round_down() returns 0 for start, and that
  416. * turns it into 0x100000000ULL.
  417. * In the bottom-up case, round_up(x, 0) returns 0 though too, which
  418. * needs to be taken into consideration by the code below.
  419. */
  420. return step_size << (PMD_SHIFT - PAGE_SHIFT - 1);
  421. }
  422. /**
  423. * memory_map_top_down - Map [map_start, map_end) top down
  424. * @map_start: start address of the target memory range
  425. * @map_end: end address of the target memory range
  426. *
  427. * This function will setup direct mapping for memory range
  428. * [map_start, map_end) in top-down. That said, the page tables
  429. * will be allocated at the end of the memory, and we map the
  430. * memory in top-down.
  431. */
  432. static void __init memory_map_top_down(unsigned long map_start,
  433. unsigned long map_end)
  434. {
  435. unsigned long real_end, start, last_start;
  436. unsigned long step_size;
  437. unsigned long addr;
  438. unsigned long mapped_ram_size = 0;
  439. /* xen has big range in reserved near end of ram, skip it at first.*/
  440. addr = memblock_find_in_range(map_start, map_end, PMD_SIZE, PMD_SIZE);
  441. real_end = addr + PMD_SIZE;
  442. /* step_size need to be small so pgt_buf from BRK could cover it */
  443. step_size = PMD_SIZE;
  444. max_pfn_mapped = 0; /* will get exact value next */
  445. min_pfn_mapped = real_end >> PAGE_SHIFT;
  446. last_start = start = real_end;
  447. /*
  448. * We start from the top (end of memory) and go to the bottom.
  449. * The memblock_find_in_range() gets us a block of RAM from the
  450. * end of RAM in [min_pfn_mapped, max_pfn_mapped) used as new pages
  451. * for page table.
  452. */
  453. while (last_start > map_start) {
  454. if (last_start > step_size) {
  455. start = round_down(last_start - 1, step_size);
  456. if (start < map_start)
  457. start = map_start;
  458. } else
  459. start = map_start;
  460. mapped_ram_size += init_range_memory_mapping(start,
  461. last_start);
  462. last_start = start;
  463. min_pfn_mapped = last_start >> PAGE_SHIFT;
  464. if (mapped_ram_size >= step_size)
  465. step_size = get_new_step_size(step_size);
  466. }
  467. if (real_end < map_end)
  468. init_range_memory_mapping(real_end, map_end);
  469. }
  470. /**
  471. * memory_map_bottom_up - Map [map_start, map_end) bottom up
  472. * @map_start: start address of the target memory range
  473. * @map_end: end address of the target memory range
  474. *
  475. * This function will setup direct mapping for memory range
  476. * [map_start, map_end) in bottom-up. Since we have limited the
  477. * bottom-up allocation above the kernel, the page tables will
  478. * be allocated just above the kernel and we map the memory
  479. * in [map_start, map_end) in bottom-up.
  480. */
  481. static void __init memory_map_bottom_up(unsigned long map_start,
  482. unsigned long map_end)
  483. {
  484. unsigned long next, start;
  485. unsigned long mapped_ram_size = 0;
  486. /* step_size need to be small so pgt_buf from BRK could cover it */
  487. unsigned long step_size = PMD_SIZE;
  488. start = map_start;
  489. min_pfn_mapped = start >> PAGE_SHIFT;
  490. /*
  491. * We start from the bottom (@map_start) and go to the top (@map_end).
  492. * The memblock_find_in_range() gets us a block of RAM from the
  493. * end of RAM in [min_pfn_mapped, max_pfn_mapped) used as new pages
  494. * for page table.
  495. */
  496. while (start < map_end) {
  497. if (step_size && map_end - start > step_size) {
  498. next = round_up(start + 1, step_size);
  499. if (next > map_end)
  500. next = map_end;
  501. } else {
  502. next = map_end;
  503. }
  504. mapped_ram_size += init_range_memory_mapping(start, next);
  505. start = next;
  506. if (mapped_ram_size >= step_size)
  507. step_size = get_new_step_size(step_size);
  508. }
  509. }
  510. void __init init_mem_mapping(void)
  511. {
  512. unsigned long end;
  513. probe_page_size_mask();
  514. #ifdef CONFIG_X86_64
  515. end = max_pfn << PAGE_SHIFT;
  516. #else
  517. end = max_low_pfn << PAGE_SHIFT;
  518. #endif
  519. /* the ISA range is always mapped regardless of memory holes */
  520. init_memory_mapping(0, ISA_END_ADDRESS);
  521. /* Init the trampoline, possibly with KASLR memory offset */
  522. init_trampoline();
  523. /*
  524. * If the allocation is in bottom-up direction, we setup direct mapping
  525. * in bottom-up, otherwise we setup direct mapping in top-down.
  526. */
  527. if (memblock_bottom_up()) {
  528. unsigned long kernel_end = __pa_symbol(_end);
  529. /*
  530. * we need two separate calls here. This is because we want to
  531. * allocate page tables above the kernel. So we first map
  532. * [kernel_end, end) to make memory above the kernel be mapped
  533. * as soon as possible. And then use page tables allocated above
  534. * the kernel to map [ISA_END_ADDRESS, kernel_end).
  535. */
  536. memory_map_bottom_up(kernel_end, end);
  537. memory_map_bottom_up(ISA_END_ADDRESS, kernel_end);
  538. } else {
  539. memory_map_top_down(ISA_END_ADDRESS, end);
  540. }
  541. #ifdef CONFIG_X86_64
  542. if (max_pfn > max_low_pfn) {
  543. /* can we preseve max_low_pfn ?*/
  544. max_low_pfn = max_pfn;
  545. }
  546. #else
  547. early_ioremap_page_table_range_init();
  548. #endif
  549. load_cr3(swapper_pg_dir);
  550. __flush_tlb_all();
  551. early_memtest(0, max_pfn_mapped << PAGE_SHIFT);
  552. }
  553. /*
  554. * devmem_is_allowed() checks to see if /dev/mem access to a certain address
  555. * is valid. The argument is a physical page number.
  556. *
  557. * On x86, access has to be given to the first megabyte of RAM because that
  558. * area traditionally contains BIOS code and data regions used by X, dosemu,
  559. * and similar apps. Since they map the entire memory range, the whole range
  560. * must be allowed (for mapping), but any areas that would otherwise be
  561. * disallowed are flagged as being "zero filled" instead of rejected.
  562. * Access has to be given to non-kernel-ram areas as well, these contain the
  563. * PCI mmio resources as well as potential bios/acpi data regions.
  564. */
  565. int devmem_is_allowed(unsigned long pagenr)
  566. {
  567. if (region_intersects(PFN_PHYS(pagenr), PAGE_SIZE,
  568. IORESOURCE_SYSTEM_RAM, IORES_DESC_NONE)
  569. != REGION_DISJOINT) {
  570. /*
  571. * For disallowed memory regions in the low 1MB range,
  572. * request that the page be shown as all zeros.
  573. */
  574. if (pagenr < 256)
  575. return 2;
  576. return 0;
  577. }
  578. /*
  579. * This must follow RAM test, since System RAM is considered a
  580. * restricted resource under CONFIG_STRICT_IOMEM.
  581. */
  582. if (iomem_is_exclusive(pagenr << PAGE_SHIFT)) {
  583. /* Low 1MB bypasses iomem restrictions. */
  584. if (pagenr < 256)
  585. return 1;
  586. return 0;
  587. }
  588. return 1;
  589. }
  590. void free_init_pages(char *what, unsigned long begin, unsigned long end)
  591. {
  592. unsigned long begin_aligned, end_aligned;
  593. /* Make sure boundaries are page aligned */
  594. begin_aligned = PAGE_ALIGN(begin);
  595. end_aligned = end & PAGE_MASK;
  596. if (WARN_ON(begin_aligned != begin || end_aligned != end)) {
  597. begin = begin_aligned;
  598. end = end_aligned;
  599. }
  600. if (begin >= end)
  601. return;
  602. /*
  603. * If debugging page accesses then do not free this memory but
  604. * mark them not present - any buggy init-section access will
  605. * create a kernel page fault:
  606. */
  607. if (debug_pagealloc_enabled()) {
  608. pr_info("debug: unmapping init [mem %#010lx-%#010lx]\n",
  609. begin, end - 1);
  610. set_memory_np(begin, (end - begin) >> PAGE_SHIFT);
  611. } else {
  612. /*
  613. * We just marked the kernel text read only above, now that
  614. * we are going to free part of that, we need to make that
  615. * writeable and non-executable first.
  616. */
  617. set_memory_nx(begin, (end - begin) >> PAGE_SHIFT);
  618. set_memory_rw(begin, (end - begin) >> PAGE_SHIFT);
  619. free_reserved_area((void *)begin, (void *)end,
  620. POISON_FREE_INITMEM, what);
  621. }
  622. }
  623. void __ref free_initmem(void)
  624. {
  625. e820_reallocate_tables();
  626. free_init_pages("unused kernel",
  627. (unsigned long)(&__init_begin),
  628. (unsigned long)(&__init_end));
  629. }
  630. #ifdef CONFIG_BLK_DEV_INITRD
  631. void __init free_initrd_mem(unsigned long start, unsigned long end)
  632. {
  633. /*
  634. * end could be not aligned, and We can not align that,
  635. * decompresser could be confused by aligned initrd_end
  636. * We already reserve the end partial page before in
  637. * - i386_start_kernel()
  638. * - x86_64_start_kernel()
  639. * - relocate_initrd()
  640. * So here We can do PAGE_ALIGN() safely to get partial page to be freed
  641. */
  642. free_init_pages("initrd", start, PAGE_ALIGN(end));
  643. }
  644. #endif
  645. void __init zone_sizes_init(void)
  646. {
  647. unsigned long max_zone_pfns[MAX_NR_ZONES];
  648. memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
  649. #ifdef CONFIG_ZONE_DMA
  650. max_zone_pfns[ZONE_DMA] = min(MAX_DMA_PFN, max_low_pfn);
  651. #endif
  652. #ifdef CONFIG_ZONE_DMA32
  653. max_zone_pfns[ZONE_DMA32] = min(MAX_DMA32_PFN, max_low_pfn);
  654. #endif
  655. max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
  656. #ifdef CONFIG_HIGHMEM
  657. max_zone_pfns[ZONE_HIGHMEM] = max_pfn;
  658. #endif
  659. free_area_init_nodes(max_zone_pfns);
  660. }
  661. DEFINE_PER_CPU_SHARED_ALIGNED(struct tlb_state, cpu_tlbstate) = {
  662. .active_mm = &init_mm,
  663. .state = 0,
  664. .cr4 = ~0UL, /* fail hard if we screw up cr4 shadow initialization */
  665. };
  666. EXPORT_PER_CPU_SYMBOL(cpu_tlbstate);
  667. void update_cache_mode_entry(unsigned entry, enum page_cache_mode cache)
  668. {
  669. /* entry 0 MUST be WB (hardwired to speed up translations) */
  670. BUG_ON(!entry && cache != _PAGE_CACHE_MODE_WB);
  671. __cachemode2pte_tbl[cache] = __cm_idx2pte(entry);
  672. __pte2cachemode_tbl[entry] = cache;
  673. }