init_32.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  1. /*
  2. *
  3. * Copyright (C) 1995 Linus Torvalds
  4. *
  5. * Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999
  6. */
  7. #include <linux/signal.h>
  8. #include <linux/sched.h>
  9. #include <linux/kernel.h>
  10. #include <linux/errno.h>
  11. #include <linux/string.h>
  12. #include <linux/types.h>
  13. #include <linux/ptrace.h>
  14. #include <linux/mman.h>
  15. #include <linux/mm.h>
  16. #include <linux/hugetlb.h>
  17. #include <linux/swap.h>
  18. #include <linux/smp.h>
  19. #include <linux/init.h>
  20. #include <linux/highmem.h>
  21. #include <linux/pagemap.h>
  22. #include <linux/pci.h>
  23. #include <linux/pfn.h>
  24. #include <linux/poison.h>
  25. #include <linux/bootmem.h>
  26. #include <linux/memblock.h>
  27. #include <linux/proc_fs.h>
  28. #include <linux/memory_hotplug.h>
  29. #include <linux/initrd.h>
  30. #include <linux/cpumask.h>
  31. #include <linux/gfp.h>
  32. #include <asm/asm.h>
  33. #include <asm/bios_ebda.h>
  34. #include <asm/processor.h>
  35. #include <asm/uaccess.h>
  36. #include <asm/pgtable.h>
  37. #include <asm/dma.h>
  38. #include <asm/fixmap.h>
  39. #include <asm/e820.h>
  40. #include <asm/apic.h>
  41. #include <asm/bugs.h>
  42. #include <asm/tlb.h>
  43. #include <asm/tlbflush.h>
  44. #include <asm/olpc_ofw.h>
  45. #include <asm/pgalloc.h>
  46. #include <asm/sections.h>
  47. #include <asm/paravirt.h>
  48. #include <asm/setup.h>
  49. #include <asm/cacheflush.h>
  50. #include <asm/page_types.h>
  51. #include <asm/init.h>
  52. #include "mm_internal.h"
  53. unsigned long highstart_pfn, highend_pfn;
  54. static noinline int do_test_wp_bit(void);
  55. bool __read_mostly __vmalloc_start_set = false;
  56. /*
  57. * Creates a middle page table and puts a pointer to it in the
  58. * given global directory entry. This only returns the gd entry
  59. * in non-PAE compilation mode, since the middle layer is folded.
  60. */
  61. static pmd_t * __init one_md_table_init(pgd_t *pgd)
  62. {
  63. pud_t *pud;
  64. pmd_t *pmd_table;
  65. #ifdef CONFIG_X86_PAE
  66. if (!(pgd_val(*pgd) & _PAGE_PRESENT)) {
  67. pmd_table = (pmd_t *)alloc_low_page();
  68. paravirt_alloc_pmd(&init_mm, __pa(pmd_table) >> PAGE_SHIFT);
  69. set_pgd(pgd, __pgd(__pa(pmd_table) | _PAGE_PRESENT));
  70. pud = pud_offset(pgd, 0);
  71. BUG_ON(pmd_table != pmd_offset(pud, 0));
  72. return pmd_table;
  73. }
  74. #endif
  75. pud = pud_offset(pgd, 0);
  76. pmd_table = pmd_offset(pud, 0);
  77. return pmd_table;
  78. }
  79. /*
  80. * Create a page table and place a pointer to it in a middle page
  81. * directory entry:
  82. */
  83. static pte_t * __init one_page_table_init(pmd_t *pmd)
  84. {
  85. if (!(pmd_val(*pmd) & _PAGE_PRESENT)) {
  86. pte_t *page_table = (pte_t *)alloc_low_page();
  87. paravirt_alloc_pte(&init_mm, __pa(page_table) >> PAGE_SHIFT);
  88. set_pmd(pmd, __pmd(__pa(page_table) | _PAGE_TABLE));
  89. BUG_ON(page_table != pte_offset_kernel(pmd, 0));
  90. }
  91. return pte_offset_kernel(pmd, 0);
  92. }
  93. pmd_t * __init populate_extra_pmd(unsigned long vaddr)
  94. {
  95. int pgd_idx = pgd_index(vaddr);
  96. int pmd_idx = pmd_index(vaddr);
  97. return one_md_table_init(swapper_pg_dir + pgd_idx) + pmd_idx;
  98. }
  99. pte_t * __init populate_extra_pte(unsigned long vaddr)
  100. {
  101. int pte_idx = pte_index(vaddr);
  102. pmd_t *pmd;
  103. pmd = populate_extra_pmd(vaddr);
  104. return one_page_table_init(pmd) + pte_idx;
  105. }
  106. static unsigned long __init
  107. page_table_range_init_count(unsigned long start, unsigned long end)
  108. {
  109. unsigned long count = 0;
  110. #ifdef CONFIG_HIGHMEM
  111. int pmd_idx_kmap_begin = fix_to_virt(FIX_KMAP_END) >> PMD_SHIFT;
  112. int pmd_idx_kmap_end = fix_to_virt(FIX_KMAP_BEGIN) >> PMD_SHIFT;
  113. int pgd_idx, pmd_idx;
  114. unsigned long vaddr;
  115. if (pmd_idx_kmap_begin == pmd_idx_kmap_end)
  116. return 0;
  117. vaddr = start;
  118. pgd_idx = pgd_index(vaddr);
  119. pmd_idx = pmd_index(vaddr);
  120. for ( ; (pgd_idx < PTRS_PER_PGD) && (vaddr != end); pgd_idx++) {
  121. for (; (pmd_idx < PTRS_PER_PMD) && (vaddr != end);
  122. pmd_idx++) {
  123. if ((vaddr >> PMD_SHIFT) >= pmd_idx_kmap_begin &&
  124. (vaddr >> PMD_SHIFT) <= pmd_idx_kmap_end)
  125. count++;
  126. vaddr += PMD_SIZE;
  127. }
  128. pmd_idx = 0;
  129. }
  130. #endif
  131. return count;
  132. }
  133. static pte_t *__init page_table_kmap_check(pte_t *pte, pmd_t *pmd,
  134. unsigned long vaddr, pte_t *lastpte,
  135. void **adr)
  136. {
  137. #ifdef CONFIG_HIGHMEM
  138. /*
  139. * Something (early fixmap) may already have put a pte
  140. * page here, which causes the page table allocation
  141. * to become nonlinear. Attempt to fix it, and if it
  142. * is still nonlinear then we have to bug.
  143. */
  144. int pmd_idx_kmap_begin = fix_to_virt(FIX_KMAP_END) >> PMD_SHIFT;
  145. int pmd_idx_kmap_end = fix_to_virt(FIX_KMAP_BEGIN) >> PMD_SHIFT;
  146. if (pmd_idx_kmap_begin != pmd_idx_kmap_end
  147. && (vaddr >> PMD_SHIFT) >= pmd_idx_kmap_begin
  148. && (vaddr >> PMD_SHIFT) <= pmd_idx_kmap_end) {
  149. pte_t *newpte;
  150. int i;
  151. BUG_ON(after_bootmem);
  152. newpte = *adr;
  153. for (i = 0; i < PTRS_PER_PTE; i++)
  154. set_pte(newpte + i, pte[i]);
  155. *adr = (void *)(((unsigned long)(*adr)) + PAGE_SIZE);
  156. paravirt_alloc_pte(&init_mm, __pa(newpte) >> PAGE_SHIFT);
  157. set_pmd(pmd, __pmd(__pa(newpte)|_PAGE_TABLE));
  158. BUG_ON(newpte != pte_offset_kernel(pmd, 0));
  159. __flush_tlb_all();
  160. paravirt_release_pte(__pa(pte) >> PAGE_SHIFT);
  161. pte = newpte;
  162. }
  163. BUG_ON(vaddr < fix_to_virt(FIX_KMAP_BEGIN - 1)
  164. && vaddr > fix_to_virt(FIX_KMAP_END)
  165. && lastpte && lastpte + PTRS_PER_PTE != pte);
  166. #endif
  167. return pte;
  168. }
  169. /*
  170. * This function initializes a certain range of kernel virtual memory
  171. * with new bootmem page tables, everywhere page tables are missing in
  172. * the given range.
  173. *
  174. * NOTE: The pagetables are allocated contiguous on the physical space
  175. * so we can cache the place of the first one and move around without
  176. * checking the pgd every time.
  177. */
  178. static void __init
  179. page_table_range_init(unsigned long start, unsigned long end, pgd_t *pgd_base)
  180. {
  181. int pgd_idx, pmd_idx;
  182. unsigned long vaddr;
  183. pgd_t *pgd;
  184. pmd_t *pmd;
  185. pte_t *pte = NULL;
  186. unsigned long count = page_table_range_init_count(start, end);
  187. void *adr = NULL;
  188. if (count)
  189. adr = alloc_low_pages(count);
  190. vaddr = start;
  191. pgd_idx = pgd_index(vaddr);
  192. pmd_idx = pmd_index(vaddr);
  193. pgd = pgd_base + pgd_idx;
  194. for ( ; (pgd_idx < PTRS_PER_PGD) && (vaddr != end); pgd++, pgd_idx++) {
  195. pmd = one_md_table_init(pgd);
  196. pmd = pmd + pmd_index(vaddr);
  197. for (; (pmd_idx < PTRS_PER_PMD) && (vaddr != end);
  198. pmd++, pmd_idx++) {
  199. pte = page_table_kmap_check(one_page_table_init(pmd),
  200. pmd, vaddr, pte, &adr);
  201. vaddr += PMD_SIZE;
  202. }
  203. pmd_idx = 0;
  204. }
  205. }
  206. static inline int is_kernel_text(unsigned long addr)
  207. {
  208. if (addr >= (unsigned long)_text && addr <= (unsigned long)__init_end)
  209. return 1;
  210. return 0;
  211. }
  212. /*
  213. * This maps the physical memory to kernel virtual address space, a total
  214. * of max_low_pfn pages, by creating page tables starting from address
  215. * PAGE_OFFSET:
  216. */
  217. unsigned long __init
  218. kernel_physical_mapping_init(unsigned long start,
  219. unsigned long end,
  220. unsigned long page_size_mask)
  221. {
  222. int use_pse = page_size_mask == (1<<PG_LEVEL_2M);
  223. unsigned long last_map_addr = end;
  224. unsigned long start_pfn, end_pfn;
  225. pgd_t *pgd_base = swapper_pg_dir;
  226. int pgd_idx, pmd_idx, pte_ofs;
  227. unsigned long pfn;
  228. pgd_t *pgd;
  229. pmd_t *pmd;
  230. pte_t *pte;
  231. unsigned pages_2m, pages_4k;
  232. int mapping_iter;
  233. start_pfn = start >> PAGE_SHIFT;
  234. end_pfn = end >> PAGE_SHIFT;
  235. /*
  236. * First iteration will setup identity mapping using large/small pages
  237. * based on use_pse, with other attributes same as set by
  238. * the early code in head_32.S
  239. *
  240. * Second iteration will setup the appropriate attributes (NX, GLOBAL..)
  241. * as desired for the kernel identity mapping.
  242. *
  243. * This two pass mechanism conforms to the TLB app note which says:
  244. *
  245. * "Software should not write to a paging-structure entry in a way
  246. * that would change, for any linear address, both the page size
  247. * and either the page frame or attributes."
  248. */
  249. mapping_iter = 1;
  250. if (!boot_cpu_has(X86_FEATURE_PSE))
  251. use_pse = 0;
  252. repeat:
  253. pages_2m = pages_4k = 0;
  254. pfn = start_pfn;
  255. pgd_idx = pgd_index((pfn<<PAGE_SHIFT) + PAGE_OFFSET);
  256. pgd = pgd_base + pgd_idx;
  257. for (; pgd_idx < PTRS_PER_PGD; pgd++, pgd_idx++) {
  258. pmd = one_md_table_init(pgd);
  259. if (pfn >= end_pfn)
  260. continue;
  261. #ifdef CONFIG_X86_PAE
  262. pmd_idx = pmd_index((pfn<<PAGE_SHIFT) + PAGE_OFFSET);
  263. pmd += pmd_idx;
  264. #else
  265. pmd_idx = 0;
  266. #endif
  267. for (; pmd_idx < PTRS_PER_PMD && pfn < end_pfn;
  268. pmd++, pmd_idx++) {
  269. unsigned int addr = pfn * PAGE_SIZE + PAGE_OFFSET;
  270. /*
  271. * Map with big pages if possible, otherwise
  272. * create normal page tables:
  273. */
  274. if (use_pse) {
  275. unsigned int addr2;
  276. pgprot_t prot = PAGE_KERNEL_LARGE;
  277. /*
  278. * first pass will use the same initial
  279. * identity mapping attribute + _PAGE_PSE.
  280. */
  281. pgprot_t init_prot =
  282. __pgprot(PTE_IDENT_ATTR |
  283. _PAGE_PSE);
  284. pfn &= PMD_MASK >> PAGE_SHIFT;
  285. addr2 = (pfn + PTRS_PER_PTE-1) * PAGE_SIZE +
  286. PAGE_OFFSET + PAGE_SIZE-1;
  287. if (is_kernel_text(addr) ||
  288. is_kernel_text(addr2))
  289. prot = PAGE_KERNEL_LARGE_EXEC;
  290. pages_2m++;
  291. if (mapping_iter == 1)
  292. set_pmd(pmd, pfn_pmd(pfn, init_prot));
  293. else
  294. set_pmd(pmd, pfn_pmd(pfn, prot));
  295. pfn += PTRS_PER_PTE;
  296. continue;
  297. }
  298. pte = one_page_table_init(pmd);
  299. pte_ofs = pte_index((pfn<<PAGE_SHIFT) + PAGE_OFFSET);
  300. pte += pte_ofs;
  301. for (; pte_ofs < PTRS_PER_PTE && pfn < end_pfn;
  302. pte++, pfn++, pte_ofs++, addr += PAGE_SIZE) {
  303. pgprot_t prot = PAGE_KERNEL;
  304. /*
  305. * first pass will use the same initial
  306. * identity mapping attribute.
  307. */
  308. pgprot_t init_prot = __pgprot(PTE_IDENT_ATTR);
  309. if (is_kernel_text(addr))
  310. prot = PAGE_KERNEL_EXEC;
  311. pages_4k++;
  312. if (mapping_iter == 1) {
  313. set_pte(pte, pfn_pte(pfn, init_prot));
  314. last_map_addr = (pfn << PAGE_SHIFT) + PAGE_SIZE;
  315. } else
  316. set_pte(pte, pfn_pte(pfn, prot));
  317. }
  318. }
  319. }
  320. if (mapping_iter == 1) {
  321. /*
  322. * update direct mapping page count only in the first
  323. * iteration.
  324. */
  325. update_page_count(PG_LEVEL_2M, pages_2m);
  326. update_page_count(PG_LEVEL_4K, pages_4k);
  327. /*
  328. * local global flush tlb, which will flush the previous
  329. * mappings present in both small and large page TLB's.
  330. */
  331. __flush_tlb_all();
  332. /*
  333. * Second iteration will set the actual desired PTE attributes.
  334. */
  335. mapping_iter = 2;
  336. goto repeat;
  337. }
  338. return last_map_addr;
  339. }
  340. pte_t *kmap_pte;
  341. static inline pte_t *kmap_get_fixmap_pte(unsigned long vaddr)
  342. {
  343. return pte_offset_kernel(pmd_offset(pud_offset(pgd_offset_k(vaddr),
  344. vaddr), vaddr), vaddr);
  345. }
  346. static void __init kmap_init(void)
  347. {
  348. unsigned long kmap_vstart;
  349. /*
  350. * Cache the first kmap pte:
  351. */
  352. kmap_vstart = __fix_to_virt(FIX_KMAP_BEGIN);
  353. kmap_pte = kmap_get_fixmap_pte(kmap_vstart);
  354. }
  355. #ifdef CONFIG_HIGHMEM
  356. static void __init permanent_kmaps_init(pgd_t *pgd_base)
  357. {
  358. unsigned long vaddr;
  359. pgd_t *pgd;
  360. pud_t *pud;
  361. pmd_t *pmd;
  362. pte_t *pte;
  363. vaddr = PKMAP_BASE;
  364. page_table_range_init(vaddr, vaddr + PAGE_SIZE*LAST_PKMAP, pgd_base);
  365. pgd = swapper_pg_dir + pgd_index(vaddr);
  366. pud = pud_offset(pgd, vaddr);
  367. pmd = pmd_offset(pud, vaddr);
  368. pte = pte_offset_kernel(pmd, vaddr);
  369. pkmap_page_table = pte;
  370. }
  371. void __init add_highpages_with_active_regions(int nid,
  372. unsigned long start_pfn, unsigned long end_pfn)
  373. {
  374. phys_addr_t start, end;
  375. u64 i;
  376. for_each_free_mem_range(i, nid, MEMBLOCK_NONE, &start, &end, NULL) {
  377. unsigned long pfn = clamp_t(unsigned long, PFN_UP(start),
  378. start_pfn, end_pfn);
  379. unsigned long e_pfn = clamp_t(unsigned long, PFN_DOWN(end),
  380. start_pfn, end_pfn);
  381. for ( ; pfn < e_pfn; pfn++)
  382. if (pfn_valid(pfn))
  383. free_highmem_page(pfn_to_page(pfn));
  384. }
  385. }
  386. #else
  387. static inline void permanent_kmaps_init(pgd_t *pgd_base)
  388. {
  389. }
  390. #endif /* CONFIG_HIGHMEM */
  391. void __init native_pagetable_init(void)
  392. {
  393. unsigned long pfn, va;
  394. pgd_t *pgd, *base = swapper_pg_dir;
  395. pud_t *pud;
  396. pmd_t *pmd;
  397. pte_t *pte;
  398. /*
  399. * Remove any mappings which extend past the end of physical
  400. * memory from the boot time page table.
  401. * In virtual address space, we should have at least two pages
  402. * from VMALLOC_END to pkmap or fixmap according to VMALLOC_END
  403. * definition. And max_low_pfn is set to VMALLOC_END physical
  404. * address. If initial memory mapping is doing right job, we
  405. * should have pte used near max_low_pfn or one pmd is not present.
  406. */
  407. for (pfn = max_low_pfn; pfn < 1<<(32-PAGE_SHIFT); pfn++) {
  408. va = PAGE_OFFSET + (pfn<<PAGE_SHIFT);
  409. pgd = base + pgd_index(va);
  410. if (!pgd_present(*pgd))
  411. break;
  412. pud = pud_offset(pgd, va);
  413. pmd = pmd_offset(pud, va);
  414. if (!pmd_present(*pmd))
  415. break;
  416. /* should not be large page here */
  417. if (pmd_large(*pmd)) {
  418. pr_warn("try to clear pte for ram above max_low_pfn: pfn: %lx pmd: %p pmd phys: %lx, but pmd is big page and is not using pte !\n",
  419. pfn, pmd, __pa(pmd));
  420. BUG_ON(1);
  421. }
  422. pte = pte_offset_kernel(pmd, va);
  423. if (!pte_present(*pte))
  424. break;
  425. printk(KERN_DEBUG "clearing pte for ram above max_low_pfn: pfn: %lx pmd: %p pmd phys: %lx pte: %p pte phys: %lx\n",
  426. pfn, pmd, __pa(pmd), pte, __pa(pte));
  427. pte_clear(NULL, va, pte);
  428. }
  429. paravirt_alloc_pmd(&init_mm, __pa(base) >> PAGE_SHIFT);
  430. paging_init();
  431. }
  432. /*
  433. * Build a proper pagetable for the kernel mappings. Up until this
  434. * point, we've been running on some set of pagetables constructed by
  435. * the boot process.
  436. *
  437. * If we're booting on native hardware, this will be a pagetable
  438. * constructed in arch/x86/kernel/head_32.S. The root of the
  439. * pagetable will be swapper_pg_dir.
  440. *
  441. * If we're booting paravirtualized under a hypervisor, then there are
  442. * more options: we may already be running PAE, and the pagetable may
  443. * or may not be based in swapper_pg_dir. In any case,
  444. * paravirt_pagetable_init() will set up swapper_pg_dir
  445. * appropriately for the rest of the initialization to work.
  446. *
  447. * In general, pagetable_init() assumes that the pagetable may already
  448. * be partially populated, and so it avoids stomping on any existing
  449. * mappings.
  450. */
  451. void __init early_ioremap_page_table_range_init(void)
  452. {
  453. pgd_t *pgd_base = swapper_pg_dir;
  454. unsigned long vaddr, end;
  455. /*
  456. * Fixed mappings, only the page table structure has to be
  457. * created - mappings will be set by set_fixmap():
  458. */
  459. vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK;
  460. end = (FIXADDR_TOP + PMD_SIZE - 1) & PMD_MASK;
  461. page_table_range_init(vaddr, end, pgd_base);
  462. early_ioremap_reset();
  463. }
  464. static void __init pagetable_init(void)
  465. {
  466. pgd_t *pgd_base = swapper_pg_dir;
  467. permanent_kmaps_init(pgd_base);
  468. }
  469. pteval_t __supported_pte_mask __read_mostly = ~(_PAGE_NX | _PAGE_GLOBAL);
  470. EXPORT_SYMBOL_GPL(__supported_pte_mask);
  471. /* user-defined highmem size */
  472. static unsigned int highmem_pages = -1;
  473. /*
  474. * highmem=size forces highmem to be exactly 'size' bytes.
  475. * This works even on boxes that have no highmem otherwise.
  476. * This also works to reduce highmem size on bigger boxes.
  477. */
  478. static int __init parse_highmem(char *arg)
  479. {
  480. if (!arg)
  481. return -EINVAL;
  482. highmem_pages = memparse(arg, &arg) >> PAGE_SHIFT;
  483. return 0;
  484. }
  485. early_param("highmem", parse_highmem);
  486. #define MSG_HIGHMEM_TOO_BIG \
  487. "highmem size (%luMB) is bigger than pages available (%luMB)!\n"
  488. #define MSG_LOWMEM_TOO_SMALL \
  489. "highmem size (%luMB) results in <64MB lowmem, ignoring it!\n"
  490. /*
  491. * All of RAM fits into lowmem - but if user wants highmem
  492. * artificially via the highmem=x boot parameter then create
  493. * it:
  494. */
  495. static void __init lowmem_pfn_init(void)
  496. {
  497. /* max_low_pfn is 0, we already have early_res support */
  498. max_low_pfn = max_pfn;
  499. if (highmem_pages == -1)
  500. highmem_pages = 0;
  501. #ifdef CONFIG_HIGHMEM
  502. if (highmem_pages >= max_pfn) {
  503. printk(KERN_ERR MSG_HIGHMEM_TOO_BIG,
  504. pages_to_mb(highmem_pages), pages_to_mb(max_pfn));
  505. highmem_pages = 0;
  506. }
  507. if (highmem_pages) {
  508. if (max_low_pfn - highmem_pages < 64*1024*1024/PAGE_SIZE) {
  509. printk(KERN_ERR MSG_LOWMEM_TOO_SMALL,
  510. pages_to_mb(highmem_pages));
  511. highmem_pages = 0;
  512. }
  513. max_low_pfn -= highmem_pages;
  514. }
  515. #else
  516. if (highmem_pages)
  517. printk(KERN_ERR "ignoring highmem size on non-highmem kernel!\n");
  518. #endif
  519. }
  520. #define MSG_HIGHMEM_TOO_SMALL \
  521. "only %luMB highmem pages available, ignoring highmem size of %luMB!\n"
  522. #define MSG_HIGHMEM_TRIMMED \
  523. "Warning: only 4GB will be used. Use a HIGHMEM64G enabled kernel!\n"
  524. /*
  525. * We have more RAM than fits into lowmem - we try to put it into
  526. * highmem, also taking the highmem=x boot parameter into account:
  527. */
  528. static void __init highmem_pfn_init(void)
  529. {
  530. max_low_pfn = MAXMEM_PFN;
  531. if (highmem_pages == -1)
  532. highmem_pages = max_pfn - MAXMEM_PFN;
  533. if (highmem_pages + MAXMEM_PFN < max_pfn)
  534. max_pfn = MAXMEM_PFN + highmem_pages;
  535. if (highmem_pages + MAXMEM_PFN > max_pfn) {
  536. printk(KERN_WARNING MSG_HIGHMEM_TOO_SMALL,
  537. pages_to_mb(max_pfn - MAXMEM_PFN),
  538. pages_to_mb(highmem_pages));
  539. highmem_pages = 0;
  540. }
  541. #ifndef CONFIG_HIGHMEM
  542. /* Maximum memory usable is what is directly addressable */
  543. printk(KERN_WARNING "Warning only %ldMB will be used.\n", MAXMEM>>20);
  544. if (max_pfn > MAX_NONPAE_PFN)
  545. printk(KERN_WARNING "Use a HIGHMEM64G enabled kernel.\n");
  546. else
  547. printk(KERN_WARNING "Use a HIGHMEM enabled kernel.\n");
  548. max_pfn = MAXMEM_PFN;
  549. #else /* !CONFIG_HIGHMEM */
  550. #ifndef CONFIG_HIGHMEM64G
  551. if (max_pfn > MAX_NONPAE_PFN) {
  552. max_pfn = MAX_NONPAE_PFN;
  553. printk(KERN_WARNING MSG_HIGHMEM_TRIMMED);
  554. }
  555. #endif /* !CONFIG_HIGHMEM64G */
  556. #endif /* !CONFIG_HIGHMEM */
  557. }
  558. /*
  559. * Determine low and high memory ranges:
  560. */
  561. void __init find_low_pfn_range(void)
  562. {
  563. /* it could update max_pfn */
  564. if (max_pfn <= MAXMEM_PFN)
  565. lowmem_pfn_init();
  566. else
  567. highmem_pfn_init();
  568. }
  569. #ifndef CONFIG_NEED_MULTIPLE_NODES
  570. void __init initmem_init(void)
  571. {
  572. #ifdef CONFIG_HIGHMEM
  573. highstart_pfn = highend_pfn = max_pfn;
  574. if (max_pfn > max_low_pfn)
  575. highstart_pfn = max_low_pfn;
  576. printk(KERN_NOTICE "%ldMB HIGHMEM available.\n",
  577. pages_to_mb(highend_pfn - highstart_pfn));
  578. high_memory = (void *) __va(highstart_pfn * PAGE_SIZE - 1) + 1;
  579. #else
  580. high_memory = (void *) __va(max_low_pfn * PAGE_SIZE - 1) + 1;
  581. #endif
  582. memblock_set_node(0, (phys_addr_t)ULLONG_MAX, &memblock.memory, 0);
  583. sparse_memory_present_with_active_regions(0);
  584. #ifdef CONFIG_FLATMEM
  585. max_mapnr = IS_ENABLED(CONFIG_HIGHMEM) ? highend_pfn : max_low_pfn;
  586. #endif
  587. __vmalloc_start_set = true;
  588. printk(KERN_NOTICE "%ldMB LOWMEM available.\n",
  589. pages_to_mb(max_low_pfn));
  590. setup_bootmem_allocator();
  591. }
  592. #endif /* !CONFIG_NEED_MULTIPLE_NODES */
  593. void __init setup_bootmem_allocator(void)
  594. {
  595. printk(KERN_INFO " mapped low ram: 0 - %08lx\n",
  596. max_pfn_mapped<<PAGE_SHIFT);
  597. printk(KERN_INFO " low ram: 0 - %08lx\n", max_low_pfn<<PAGE_SHIFT);
  598. }
  599. /*
  600. * paging_init() sets up the page tables - note that the first 8MB are
  601. * already mapped by head.S.
  602. *
  603. * This routines also unmaps the page at virtual kernel address 0, so
  604. * that we can trap those pesky NULL-reference errors in the kernel.
  605. */
  606. void __init paging_init(void)
  607. {
  608. pagetable_init();
  609. __flush_tlb_all();
  610. kmap_init();
  611. /*
  612. * NOTE: at this point the bootmem allocator is fully available.
  613. */
  614. olpc_dt_build_devicetree();
  615. sparse_memory_present_with_active_regions(MAX_NUMNODES);
  616. sparse_init();
  617. zone_sizes_init();
  618. }
  619. /*
  620. * Test if the WP bit works in supervisor mode. It isn't supported on 386's
  621. * and also on some strange 486's. All 586+'s are OK. This used to involve
  622. * black magic jumps to work around some nasty CPU bugs, but fortunately the
  623. * switch to using exceptions got rid of all that.
  624. */
  625. static void __init test_wp_bit(void)
  626. {
  627. printk(KERN_INFO
  628. "Checking if this processor honours the WP bit even in supervisor mode...");
  629. /* Any page-aligned address will do, the test is non-destructive */
  630. __set_fixmap(FIX_WP_TEST, __pa(&swapper_pg_dir), PAGE_KERNEL_RO);
  631. boot_cpu_data.wp_works_ok = do_test_wp_bit();
  632. clear_fixmap(FIX_WP_TEST);
  633. if (!boot_cpu_data.wp_works_ok) {
  634. printk(KERN_CONT "No.\n");
  635. panic("Linux doesn't support CPUs with broken WP.");
  636. } else {
  637. printk(KERN_CONT "Ok.\n");
  638. }
  639. }
  640. void __init mem_init(void)
  641. {
  642. pci_iommu_alloc();
  643. #ifdef CONFIG_FLATMEM
  644. BUG_ON(!mem_map);
  645. #endif
  646. /*
  647. * With CONFIG_DEBUG_PAGEALLOC initialization of highmem pages has to
  648. * be done before free_all_bootmem(). Memblock use free low memory for
  649. * temporary data (see find_range_array()) and for this purpose can use
  650. * pages that was already passed to the buddy allocator, hence marked as
  651. * not accessible in the page tables when compiled with
  652. * CONFIG_DEBUG_PAGEALLOC. Otherwise order of initialization is not
  653. * important here.
  654. */
  655. set_highmem_pages_init();
  656. /* this will put all low memory onto the freelists */
  657. free_all_bootmem();
  658. after_bootmem = 1;
  659. mem_init_print_info(NULL);
  660. printk(KERN_INFO "virtual kernel memory layout:\n"
  661. " fixmap : 0x%08lx - 0x%08lx (%4ld kB)\n"
  662. #ifdef CONFIG_HIGHMEM
  663. " pkmap : 0x%08lx - 0x%08lx (%4ld kB)\n"
  664. #endif
  665. " vmalloc : 0x%08lx - 0x%08lx (%4ld MB)\n"
  666. " lowmem : 0x%08lx - 0x%08lx (%4ld MB)\n"
  667. " .init : 0x%08lx - 0x%08lx (%4ld kB)\n"
  668. " .data : 0x%08lx - 0x%08lx (%4ld kB)\n"
  669. " .text : 0x%08lx - 0x%08lx (%4ld kB)\n",
  670. FIXADDR_START, FIXADDR_TOP,
  671. (FIXADDR_TOP - FIXADDR_START) >> 10,
  672. #ifdef CONFIG_HIGHMEM
  673. PKMAP_BASE, PKMAP_BASE+LAST_PKMAP*PAGE_SIZE,
  674. (LAST_PKMAP*PAGE_SIZE) >> 10,
  675. #endif
  676. VMALLOC_START, VMALLOC_END,
  677. (VMALLOC_END - VMALLOC_START) >> 20,
  678. (unsigned long)__va(0), (unsigned long)high_memory,
  679. ((unsigned long)high_memory - (unsigned long)__va(0)) >> 20,
  680. (unsigned long)&__init_begin, (unsigned long)&__init_end,
  681. ((unsigned long)&__init_end -
  682. (unsigned long)&__init_begin) >> 10,
  683. (unsigned long)&_etext, (unsigned long)&_edata,
  684. ((unsigned long)&_edata - (unsigned long)&_etext) >> 10,
  685. (unsigned long)&_text, (unsigned long)&_etext,
  686. ((unsigned long)&_etext - (unsigned long)&_text) >> 10);
  687. /*
  688. * Check boundaries twice: Some fundamental inconsistencies can
  689. * be detected at build time already.
  690. */
  691. #define __FIXADDR_TOP (-PAGE_SIZE)
  692. #ifdef CONFIG_HIGHMEM
  693. BUILD_BUG_ON(PKMAP_BASE + LAST_PKMAP*PAGE_SIZE > FIXADDR_START);
  694. BUILD_BUG_ON(VMALLOC_END > PKMAP_BASE);
  695. #endif
  696. #define high_memory (-128UL << 20)
  697. BUILD_BUG_ON(VMALLOC_START >= VMALLOC_END);
  698. #undef high_memory
  699. #undef __FIXADDR_TOP
  700. #ifdef CONFIG_HIGHMEM
  701. BUG_ON(PKMAP_BASE + LAST_PKMAP*PAGE_SIZE > FIXADDR_START);
  702. BUG_ON(VMALLOC_END > PKMAP_BASE);
  703. #endif
  704. BUG_ON(VMALLOC_START >= VMALLOC_END);
  705. BUG_ON((unsigned long)high_memory > VMALLOC_START);
  706. if (boot_cpu_data.wp_works_ok < 0)
  707. test_wp_bit();
  708. }
  709. #ifdef CONFIG_MEMORY_HOTPLUG
  710. int arch_add_memory(int nid, u64 start, u64 size, bool for_device)
  711. {
  712. struct pglist_data *pgdata = NODE_DATA(nid);
  713. struct zone *zone = pgdata->node_zones +
  714. zone_for_memory(nid, start, size, ZONE_HIGHMEM, for_device);
  715. unsigned long start_pfn = start >> PAGE_SHIFT;
  716. unsigned long nr_pages = size >> PAGE_SHIFT;
  717. return __add_pages(nid, zone, start_pfn, nr_pages);
  718. }
  719. #ifdef CONFIG_MEMORY_HOTREMOVE
  720. int arch_remove_memory(u64 start, u64 size)
  721. {
  722. unsigned long start_pfn = start >> PAGE_SHIFT;
  723. unsigned long nr_pages = size >> PAGE_SHIFT;
  724. struct zone *zone;
  725. zone = page_zone(pfn_to_page(start_pfn));
  726. return __remove_pages(zone, start_pfn, nr_pages);
  727. }
  728. #endif
  729. #endif
  730. /*
  731. * This function cannot be __init, since exceptions don't work in that
  732. * section. Put this after the callers, so that it cannot be inlined.
  733. */
  734. static noinline int do_test_wp_bit(void)
  735. {
  736. char tmp_reg;
  737. int flag;
  738. __asm__ __volatile__(
  739. " movb %0, %1 \n"
  740. "1: movb %1, %0 \n"
  741. " xorl %2, %2 \n"
  742. "2: \n"
  743. _ASM_EXTABLE(1b,2b)
  744. :"=m" (*(char *)fix_to_virt(FIX_WP_TEST)),
  745. "=q" (tmp_reg),
  746. "=r" (flag)
  747. :"2" (1)
  748. :"memory");
  749. return flag;
  750. }
  751. const int rodata_test_data = 0xC3;
  752. EXPORT_SYMBOL_GPL(rodata_test_data);
  753. int kernel_set_to_readonly __read_mostly;
  754. void set_kernel_text_rw(void)
  755. {
  756. unsigned long start = PFN_ALIGN(_text);
  757. unsigned long size = PFN_ALIGN(_etext) - start;
  758. if (!kernel_set_to_readonly)
  759. return;
  760. pr_debug("Set kernel text: %lx - %lx for read write\n",
  761. start, start+size);
  762. set_pages_rw(virt_to_page(start), size >> PAGE_SHIFT);
  763. }
  764. void set_kernel_text_ro(void)
  765. {
  766. unsigned long start = PFN_ALIGN(_text);
  767. unsigned long size = PFN_ALIGN(_etext) - start;
  768. if (!kernel_set_to_readonly)
  769. return;
  770. pr_debug("Set kernel text: %lx - %lx for read only\n",
  771. start, start+size);
  772. set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
  773. }
  774. static void mark_nxdata_nx(void)
  775. {
  776. /*
  777. * When this called, init has already been executed and released,
  778. * so everything past _etext should be NX.
  779. */
  780. unsigned long start = PFN_ALIGN(_etext);
  781. /*
  782. * This comes from is_kernel_text upper limit. Also HPAGE where used:
  783. */
  784. unsigned long size = (((unsigned long)__init_end + HPAGE_SIZE) & HPAGE_MASK) - start;
  785. if (__supported_pte_mask & _PAGE_NX)
  786. printk(KERN_INFO "NX-protecting the kernel data: %luk\n", size >> 10);
  787. set_pages_nx(virt_to_page(start), size >> PAGE_SHIFT);
  788. }
  789. void mark_rodata_ro(void)
  790. {
  791. unsigned long start = PFN_ALIGN(_text);
  792. unsigned long size = PFN_ALIGN(_etext) - start;
  793. set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
  794. printk(KERN_INFO "Write protecting the kernel text: %luk\n",
  795. size >> 10);
  796. kernel_set_to_readonly = 1;
  797. #ifdef CONFIG_CPA_DEBUG
  798. printk(KERN_INFO "Testing CPA: Reverting %lx-%lx\n",
  799. start, start+size);
  800. set_pages_rw(virt_to_page(start), size>>PAGE_SHIFT);
  801. printk(KERN_INFO "Testing CPA: write protecting again\n");
  802. set_pages_ro(virt_to_page(start), size>>PAGE_SHIFT);
  803. #endif
  804. start += size;
  805. size = (unsigned long)__end_rodata - start;
  806. set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
  807. printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n",
  808. size >> 10);
  809. rodata_test();
  810. #ifdef CONFIG_CPA_DEBUG
  811. printk(KERN_INFO "Testing CPA: undo %lx-%lx\n", start, start + size);
  812. set_pages_rw(virt_to_page(start), size >> PAGE_SHIFT);
  813. printk(KERN_INFO "Testing CPA: write protecting again\n");
  814. set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
  815. #endif
  816. mark_nxdata_nx();
  817. if (__supported_pte_mask & _PAGE_NX)
  818. debug_checkwx();
  819. }