init.c 23 KB

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