init_32.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203
  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/module.h>
  8. #include <linux/signal.h>
  9. #include <linux/sched.h>
  10. #include <linux/kernel.h>
  11. #include <linux/errno.h>
  12. #include <linux/string.h>
  13. #include <linux/types.h>
  14. #include <linux/ptrace.h>
  15. #include <linux/mman.h>
  16. #include <linux/mm.h>
  17. #include <linux/hugetlb.h>
  18. #include <linux/swap.h>
  19. #include <linux/smp.h>
  20. #include <linux/init.h>
  21. #include <linux/highmem.h>
  22. #include <linux/pagemap.h>
  23. #include <linux/pci.h>
  24. #include <linux/pfn.h>
  25. #include <linux/poison.h>
  26. #include <linux/bootmem.h>
  27. #include <linux/slab.h>
  28. #include <linux/proc_fs.h>
  29. #include <linux/memory_hotplug.h>
  30. #include <linux/initrd.h>
  31. #include <linux/cpumask.h>
  32. #include <asm/asm.h>
  33. #include <asm/bios_ebda.h>
  34. #include <asm/processor.h>
  35. #include <asm/system.h>
  36. #include <asm/uaccess.h>
  37. #include <asm/pgtable.h>
  38. #include <asm/dma.h>
  39. #include <asm/fixmap.h>
  40. #include <asm/e820.h>
  41. #include <asm/apic.h>
  42. #include <asm/bugs.h>
  43. #include <asm/tlb.h>
  44. #include <asm/tlbflush.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. unsigned int __VMALLOC_RESERVE = 128 << 20;
  51. unsigned long max_low_pfn_mapped;
  52. unsigned long max_pfn_mapped;
  53. DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
  54. unsigned long highstart_pfn, highend_pfn;
  55. static noinline int do_test_wp_bit(void);
  56. static unsigned long __initdata table_start;
  57. static unsigned long __meminitdata table_end;
  58. static unsigned long __meminitdata table_top;
  59. static int __initdata after_init_bootmem;
  60. static __init void *alloc_low_page(void)
  61. {
  62. unsigned long pfn = table_end++;
  63. void *adr;
  64. if (pfn >= table_top)
  65. panic("alloc_low_page: ran out of memory");
  66. adr = __va(pfn * PAGE_SIZE);
  67. memset(adr, 0, PAGE_SIZE);
  68. return adr;
  69. }
  70. /*
  71. * Creates a middle page table and puts a pointer to it in the
  72. * given global directory entry. This only returns the gd entry
  73. * in non-PAE compilation mode, since the middle layer is folded.
  74. */
  75. static pmd_t * __init one_md_table_init(pgd_t *pgd)
  76. {
  77. pud_t *pud;
  78. pmd_t *pmd_table;
  79. #ifdef CONFIG_X86_PAE
  80. if (!(pgd_val(*pgd) & _PAGE_PRESENT)) {
  81. if (after_init_bootmem)
  82. pmd_table = (pmd_t *)alloc_bootmem_low_pages(PAGE_SIZE);
  83. else
  84. pmd_table = (pmd_t *)alloc_low_page();
  85. paravirt_alloc_pmd(&init_mm, __pa(pmd_table) >> PAGE_SHIFT);
  86. set_pgd(pgd, __pgd(__pa(pmd_table) | _PAGE_PRESENT));
  87. pud = pud_offset(pgd, 0);
  88. BUG_ON(pmd_table != pmd_offset(pud, 0));
  89. return pmd_table;
  90. }
  91. #endif
  92. pud = pud_offset(pgd, 0);
  93. pmd_table = pmd_offset(pud, 0);
  94. return pmd_table;
  95. }
  96. /*
  97. * Create a page table and place a pointer to it in a middle page
  98. * directory entry:
  99. */
  100. static pte_t * __init one_page_table_init(pmd_t *pmd)
  101. {
  102. if (!(pmd_val(*pmd) & _PAGE_PRESENT)) {
  103. pte_t *page_table = NULL;
  104. if (after_init_bootmem) {
  105. #ifdef CONFIG_DEBUG_PAGEALLOC
  106. page_table = (pte_t *) alloc_bootmem_pages(PAGE_SIZE);
  107. #endif
  108. if (!page_table)
  109. page_table =
  110. (pte_t *)alloc_bootmem_low_pages(PAGE_SIZE);
  111. } else
  112. page_table = (pte_t *)alloc_low_page();
  113. paravirt_alloc_pte(&init_mm, __pa(page_table) >> PAGE_SHIFT);
  114. set_pmd(pmd, __pmd(__pa(page_table) | _PAGE_TABLE));
  115. BUG_ON(page_table != pte_offset_kernel(pmd, 0));
  116. }
  117. return pte_offset_kernel(pmd, 0);
  118. }
  119. /*
  120. * This function initializes a certain range of kernel virtual memory
  121. * with new bootmem page tables, everywhere page tables are missing in
  122. * the given range.
  123. *
  124. * NOTE: The pagetables are allocated contiguous on the physical space
  125. * so we can cache the place of the first one and move around without
  126. * checking the pgd every time.
  127. */
  128. static void __init
  129. page_table_range_init(unsigned long start, unsigned long end, pgd_t *pgd_base)
  130. {
  131. int pgd_idx, pmd_idx;
  132. unsigned long vaddr;
  133. pgd_t *pgd;
  134. pmd_t *pmd;
  135. vaddr = start;
  136. pgd_idx = pgd_index(vaddr);
  137. pmd_idx = pmd_index(vaddr);
  138. pgd = pgd_base + pgd_idx;
  139. for ( ; (pgd_idx < PTRS_PER_PGD) && (vaddr != end); pgd++, pgd_idx++) {
  140. pmd = one_md_table_init(pgd);
  141. pmd = pmd + pmd_index(vaddr);
  142. for (; (pmd_idx < PTRS_PER_PMD) && (vaddr != end);
  143. pmd++, pmd_idx++) {
  144. one_page_table_init(pmd);
  145. vaddr += PMD_SIZE;
  146. }
  147. pmd_idx = 0;
  148. }
  149. }
  150. static inline int is_kernel_text(unsigned long addr)
  151. {
  152. if (addr >= PAGE_OFFSET && addr <= (unsigned long)__init_end)
  153. return 1;
  154. return 0;
  155. }
  156. /*
  157. * This maps the physical memory to kernel virtual address space, a total
  158. * of max_low_pfn pages, by creating page tables starting from address
  159. * PAGE_OFFSET:
  160. */
  161. static void __init kernel_physical_mapping_init(pgd_t *pgd_base,
  162. unsigned long start_pfn,
  163. unsigned long end_pfn,
  164. int use_pse)
  165. {
  166. int pgd_idx, pmd_idx, pte_ofs;
  167. unsigned long pfn;
  168. pgd_t *pgd;
  169. pmd_t *pmd;
  170. pte_t *pte;
  171. unsigned pages_2m, pages_4k;
  172. int mapping_iter;
  173. /*
  174. * First iteration will setup identity mapping using large/small pages
  175. * based on use_pse, with other attributes same as set by
  176. * the early code in head_32.S
  177. *
  178. * Second iteration will setup the appropriate attributes (NX, GLOBAL..)
  179. * as desired for the kernel identity mapping.
  180. *
  181. * This two pass mechanism conforms to the TLB app note which says:
  182. *
  183. * "Software should not write to a paging-structure entry in a way
  184. * that would change, for any linear address, both the page size
  185. * and either the page frame or attributes."
  186. */
  187. mapping_iter = 1;
  188. if (!cpu_has_pse)
  189. use_pse = 0;
  190. repeat:
  191. pages_2m = pages_4k = 0;
  192. pfn = start_pfn;
  193. pgd_idx = pgd_index((pfn<<PAGE_SHIFT) + PAGE_OFFSET);
  194. pgd = pgd_base + pgd_idx;
  195. for (; pgd_idx < PTRS_PER_PGD; pgd++, pgd_idx++) {
  196. pmd = one_md_table_init(pgd);
  197. if (pfn >= end_pfn)
  198. continue;
  199. #ifdef CONFIG_X86_PAE
  200. pmd_idx = pmd_index((pfn<<PAGE_SHIFT) + PAGE_OFFSET);
  201. pmd += pmd_idx;
  202. #else
  203. pmd_idx = 0;
  204. #endif
  205. for (; pmd_idx < PTRS_PER_PMD && pfn < end_pfn;
  206. pmd++, pmd_idx++) {
  207. unsigned int addr = pfn * PAGE_SIZE + PAGE_OFFSET;
  208. /*
  209. * Map with big pages if possible, otherwise
  210. * create normal page tables:
  211. */
  212. if (use_pse) {
  213. unsigned int addr2;
  214. pgprot_t prot = PAGE_KERNEL_LARGE;
  215. /*
  216. * first pass will use the same initial
  217. * identity mapping attribute + _PAGE_PSE.
  218. */
  219. pgprot_t init_prot =
  220. __pgprot(PTE_IDENT_ATTR |
  221. _PAGE_PSE);
  222. addr2 = (pfn + PTRS_PER_PTE-1) * PAGE_SIZE +
  223. PAGE_OFFSET + PAGE_SIZE-1;
  224. if (is_kernel_text(addr) ||
  225. is_kernel_text(addr2))
  226. prot = PAGE_KERNEL_LARGE_EXEC;
  227. pages_2m++;
  228. if (mapping_iter == 1)
  229. set_pmd(pmd, pfn_pmd(pfn, init_prot));
  230. else
  231. set_pmd(pmd, pfn_pmd(pfn, prot));
  232. pfn += PTRS_PER_PTE;
  233. continue;
  234. }
  235. pte = one_page_table_init(pmd);
  236. pte_ofs = pte_index((pfn<<PAGE_SHIFT) + PAGE_OFFSET);
  237. pte += pte_ofs;
  238. for (; pte_ofs < PTRS_PER_PTE && pfn < end_pfn;
  239. pte++, pfn++, pte_ofs++, addr += PAGE_SIZE) {
  240. pgprot_t prot = PAGE_KERNEL;
  241. /*
  242. * first pass will use the same initial
  243. * identity mapping attribute.
  244. */
  245. pgprot_t init_prot = __pgprot(PTE_IDENT_ATTR);
  246. if (is_kernel_text(addr))
  247. prot = PAGE_KERNEL_EXEC;
  248. pages_4k++;
  249. if (mapping_iter == 1)
  250. set_pte(pte, pfn_pte(pfn, init_prot));
  251. else
  252. set_pte(pte, pfn_pte(pfn, prot));
  253. }
  254. }
  255. }
  256. if (mapping_iter == 1) {
  257. /*
  258. * update direct mapping page count only in the first
  259. * iteration.
  260. */
  261. update_page_count(PG_LEVEL_2M, pages_2m);
  262. update_page_count(PG_LEVEL_4K, pages_4k);
  263. /*
  264. * local global flush tlb, which will flush the previous
  265. * mappings present in both small and large page TLB's.
  266. */
  267. __flush_tlb_all();
  268. /*
  269. * Second iteration will set the actual desired PTE attributes.
  270. */
  271. mapping_iter = 2;
  272. goto repeat;
  273. }
  274. }
  275. /*
  276. * devmem_is_allowed() checks to see if /dev/mem access to a certain address
  277. * is valid. The argument is a physical page number.
  278. *
  279. *
  280. * On x86, access has to be given to the first megabyte of ram because that area
  281. * contains bios code and data regions used by X and dosemu and similar apps.
  282. * Access has to be given to non-kernel-ram areas as well, these contain the PCI
  283. * mmio resources as well as potential bios/acpi data regions.
  284. */
  285. int devmem_is_allowed(unsigned long pagenr)
  286. {
  287. if (pagenr <= 256)
  288. return 1;
  289. if (iomem_is_exclusive(pagenr << PAGE_SHIFT))
  290. return 0;
  291. if (!page_is_ram(pagenr))
  292. return 1;
  293. return 0;
  294. }
  295. pte_t *kmap_pte;
  296. pgprot_t kmap_prot;
  297. static inline pte_t *kmap_get_fixmap_pte(unsigned long vaddr)
  298. {
  299. return pte_offset_kernel(pmd_offset(pud_offset(pgd_offset_k(vaddr),
  300. vaddr), vaddr), vaddr);
  301. }
  302. static void __init kmap_init(void)
  303. {
  304. unsigned long kmap_vstart;
  305. /*
  306. * Cache the first kmap pte:
  307. */
  308. kmap_vstart = __fix_to_virt(FIX_KMAP_BEGIN);
  309. kmap_pte = kmap_get_fixmap_pte(kmap_vstart);
  310. kmap_prot = PAGE_KERNEL;
  311. }
  312. #ifdef CONFIG_HIGHMEM
  313. static void __init permanent_kmaps_init(pgd_t *pgd_base)
  314. {
  315. unsigned long vaddr;
  316. pgd_t *pgd;
  317. pud_t *pud;
  318. pmd_t *pmd;
  319. pte_t *pte;
  320. vaddr = PKMAP_BASE;
  321. page_table_range_init(vaddr, vaddr + PAGE_SIZE*LAST_PKMAP, pgd_base);
  322. pgd = swapper_pg_dir + pgd_index(vaddr);
  323. pud = pud_offset(pgd, vaddr);
  324. pmd = pmd_offset(pud, vaddr);
  325. pte = pte_offset_kernel(pmd, vaddr);
  326. pkmap_page_table = pte;
  327. }
  328. static void __init add_one_highpage_init(struct page *page, int pfn)
  329. {
  330. ClearPageReserved(page);
  331. init_page_count(page);
  332. __free_page(page);
  333. totalhigh_pages++;
  334. }
  335. struct add_highpages_data {
  336. unsigned long start_pfn;
  337. unsigned long end_pfn;
  338. };
  339. static int __init add_highpages_work_fn(unsigned long start_pfn,
  340. unsigned long end_pfn, void *datax)
  341. {
  342. int node_pfn;
  343. struct page *page;
  344. unsigned long final_start_pfn, final_end_pfn;
  345. struct add_highpages_data *data;
  346. data = (struct add_highpages_data *)datax;
  347. final_start_pfn = max(start_pfn, data->start_pfn);
  348. final_end_pfn = min(end_pfn, data->end_pfn);
  349. if (final_start_pfn >= final_end_pfn)
  350. return 0;
  351. for (node_pfn = final_start_pfn; node_pfn < final_end_pfn;
  352. node_pfn++) {
  353. if (!pfn_valid(node_pfn))
  354. continue;
  355. page = pfn_to_page(node_pfn);
  356. add_one_highpage_init(page, node_pfn);
  357. }
  358. return 0;
  359. }
  360. void __init add_highpages_with_active_regions(int nid, unsigned long start_pfn,
  361. unsigned long end_pfn)
  362. {
  363. struct add_highpages_data data;
  364. data.start_pfn = start_pfn;
  365. data.end_pfn = end_pfn;
  366. work_with_active_regions(nid, add_highpages_work_fn, &data);
  367. }
  368. #ifndef CONFIG_NUMA
  369. static void __init set_highmem_pages_init(void)
  370. {
  371. add_highpages_with_active_regions(0, highstart_pfn, highend_pfn);
  372. totalram_pages += totalhigh_pages;
  373. }
  374. #endif /* !CONFIG_NUMA */
  375. #else
  376. static inline void permanent_kmaps_init(pgd_t *pgd_base)
  377. {
  378. }
  379. static inline void set_highmem_pages_init(void)
  380. {
  381. }
  382. #endif /* CONFIG_HIGHMEM */
  383. void __init native_pagetable_setup_start(pgd_t *base)
  384. {
  385. unsigned long pfn, va;
  386. pgd_t *pgd;
  387. pud_t *pud;
  388. pmd_t *pmd;
  389. pte_t *pte;
  390. /*
  391. * Remove any mappings which extend past the end of physical
  392. * memory from the boot time page table:
  393. */
  394. for (pfn = max_low_pfn + 1; pfn < 1<<(32-PAGE_SHIFT); pfn++) {
  395. va = PAGE_OFFSET + (pfn<<PAGE_SHIFT);
  396. pgd = base + pgd_index(va);
  397. if (!pgd_present(*pgd))
  398. break;
  399. pud = pud_offset(pgd, va);
  400. pmd = pmd_offset(pud, va);
  401. if (!pmd_present(*pmd))
  402. break;
  403. pte = pte_offset_kernel(pmd, va);
  404. if (!pte_present(*pte))
  405. break;
  406. pte_clear(NULL, va, pte);
  407. }
  408. paravirt_alloc_pmd(&init_mm, __pa(base) >> PAGE_SHIFT);
  409. }
  410. void __init native_pagetable_setup_done(pgd_t *base)
  411. {
  412. }
  413. /*
  414. * Build a proper pagetable for the kernel mappings. Up until this
  415. * point, we've been running on some set of pagetables constructed by
  416. * the boot process.
  417. *
  418. * If we're booting on native hardware, this will be a pagetable
  419. * constructed in arch/x86/kernel/head_32.S. The root of the
  420. * pagetable will be swapper_pg_dir.
  421. *
  422. * If we're booting paravirtualized under a hypervisor, then there are
  423. * more options: we may already be running PAE, and the pagetable may
  424. * or may not be based in swapper_pg_dir. In any case,
  425. * paravirt_pagetable_setup_start() will set up swapper_pg_dir
  426. * appropriately for the rest of the initialization to work.
  427. *
  428. * In general, pagetable_init() assumes that the pagetable may already
  429. * be partially populated, and so it avoids stomping on any existing
  430. * mappings.
  431. */
  432. static void __init early_ioremap_page_table_range_init(pgd_t *pgd_base)
  433. {
  434. unsigned long vaddr, end;
  435. /*
  436. * Fixed mappings, only the page table structure has to be
  437. * created - mappings will be set by set_fixmap():
  438. */
  439. early_ioremap_clear();
  440. vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK;
  441. end = (FIXADDR_TOP + PMD_SIZE - 1) & PMD_MASK;
  442. page_table_range_init(vaddr, end, pgd_base);
  443. early_ioremap_reset();
  444. }
  445. static void __init pagetable_init(void)
  446. {
  447. pgd_t *pgd_base = swapper_pg_dir;
  448. permanent_kmaps_init(pgd_base);
  449. }
  450. #ifdef CONFIG_ACPI_SLEEP
  451. /*
  452. * ACPI suspend needs this for resume, because things like the intel-agp
  453. * driver might have split up a kernel 4MB mapping.
  454. */
  455. char swsusp_pg_dir[PAGE_SIZE]
  456. __attribute__ ((aligned(PAGE_SIZE)));
  457. static inline void save_pg_dir(void)
  458. {
  459. memcpy(swsusp_pg_dir, swapper_pg_dir, PAGE_SIZE);
  460. }
  461. #else /* !CONFIG_ACPI_SLEEP */
  462. static inline void save_pg_dir(void)
  463. {
  464. }
  465. #endif /* !CONFIG_ACPI_SLEEP */
  466. void zap_low_mappings(void)
  467. {
  468. int i;
  469. /*
  470. * Zap initial low-memory mappings.
  471. *
  472. * Note that "pgd_clear()" doesn't do it for
  473. * us, because pgd_clear() is a no-op on i386.
  474. */
  475. for (i = 0; i < KERNEL_PGD_BOUNDARY; i++) {
  476. #ifdef CONFIG_X86_PAE
  477. set_pgd(swapper_pg_dir+i, __pgd(1 + __pa(empty_zero_page)));
  478. #else
  479. set_pgd(swapper_pg_dir+i, __pgd(0));
  480. #endif
  481. }
  482. flush_tlb_all();
  483. }
  484. int nx_enabled;
  485. pteval_t __supported_pte_mask __read_mostly = ~(_PAGE_NX | _PAGE_GLOBAL | _PAGE_IOMAP);
  486. EXPORT_SYMBOL_GPL(__supported_pte_mask);
  487. #ifdef CONFIG_X86_PAE
  488. static int disable_nx __initdata;
  489. /*
  490. * noexec = on|off
  491. *
  492. * Control non executable mappings.
  493. *
  494. * on Enable
  495. * off Disable
  496. */
  497. static int __init noexec_setup(char *str)
  498. {
  499. if (!str || !strcmp(str, "on")) {
  500. if (cpu_has_nx) {
  501. __supported_pte_mask |= _PAGE_NX;
  502. disable_nx = 0;
  503. }
  504. } else {
  505. if (!strcmp(str, "off")) {
  506. disable_nx = 1;
  507. __supported_pte_mask &= ~_PAGE_NX;
  508. } else {
  509. return -EINVAL;
  510. }
  511. }
  512. return 0;
  513. }
  514. early_param("noexec", noexec_setup);
  515. static void __init set_nx(void)
  516. {
  517. unsigned int v[4], l, h;
  518. if (cpu_has_pae && (cpuid_eax(0x80000000) > 0x80000001)) {
  519. cpuid(0x80000001, &v[0], &v[1], &v[2], &v[3]);
  520. if ((v[3] & (1 << 20)) && !disable_nx) {
  521. rdmsr(MSR_EFER, l, h);
  522. l |= EFER_NX;
  523. wrmsr(MSR_EFER, l, h);
  524. nx_enabled = 1;
  525. __supported_pte_mask |= _PAGE_NX;
  526. }
  527. }
  528. }
  529. #endif
  530. /* user-defined highmem size */
  531. static unsigned int highmem_pages = -1;
  532. /*
  533. * highmem=size forces highmem to be exactly 'size' bytes.
  534. * This works even on boxes that have no highmem otherwise.
  535. * This also works to reduce highmem size on bigger boxes.
  536. */
  537. static int __init parse_highmem(char *arg)
  538. {
  539. if (!arg)
  540. return -EINVAL;
  541. highmem_pages = memparse(arg, &arg) >> PAGE_SHIFT;
  542. return 0;
  543. }
  544. early_param("highmem", parse_highmem);
  545. /*
  546. * Determine low and high memory ranges:
  547. */
  548. void __init find_low_pfn_range(void)
  549. {
  550. /* it could update max_pfn */
  551. /* max_low_pfn is 0, we already have early_res support */
  552. max_low_pfn = max_pfn;
  553. if (max_low_pfn > MAXMEM_PFN) {
  554. if (highmem_pages == -1)
  555. highmem_pages = max_pfn - MAXMEM_PFN;
  556. if (highmem_pages + MAXMEM_PFN < max_pfn)
  557. max_pfn = MAXMEM_PFN + highmem_pages;
  558. if (highmem_pages + MAXMEM_PFN > max_pfn) {
  559. printk(KERN_WARNING "only %luMB highmem pages "
  560. "available, ignoring highmem size of %uMB.\n",
  561. pages_to_mb(max_pfn - MAXMEM_PFN),
  562. pages_to_mb(highmem_pages));
  563. highmem_pages = 0;
  564. }
  565. max_low_pfn = MAXMEM_PFN;
  566. #ifndef CONFIG_HIGHMEM
  567. /* Maximum memory usable is what is directly addressable */
  568. printk(KERN_WARNING "Warning only %ldMB will be used.\n",
  569. MAXMEM>>20);
  570. if (max_pfn > MAX_NONPAE_PFN)
  571. printk(KERN_WARNING
  572. "Use a HIGHMEM64G enabled kernel.\n");
  573. else
  574. printk(KERN_WARNING "Use a HIGHMEM enabled kernel.\n");
  575. max_pfn = MAXMEM_PFN;
  576. #else /* !CONFIG_HIGHMEM */
  577. #ifndef CONFIG_HIGHMEM64G
  578. if (max_pfn > MAX_NONPAE_PFN) {
  579. max_pfn = MAX_NONPAE_PFN;
  580. printk(KERN_WARNING "Warning only 4GB will be used."
  581. "Use a HIGHMEM64G enabled kernel.\n");
  582. }
  583. #endif /* !CONFIG_HIGHMEM64G */
  584. #endif /* !CONFIG_HIGHMEM */
  585. } else {
  586. if (highmem_pages == -1)
  587. highmem_pages = 0;
  588. #ifdef CONFIG_HIGHMEM
  589. if (highmem_pages >= max_pfn) {
  590. printk(KERN_ERR "highmem size specified (%uMB) is "
  591. "bigger than pages available (%luMB)!.\n",
  592. pages_to_mb(highmem_pages),
  593. pages_to_mb(max_pfn));
  594. highmem_pages = 0;
  595. }
  596. if (highmem_pages) {
  597. if (max_low_pfn - highmem_pages <
  598. 64*1024*1024/PAGE_SIZE){
  599. printk(KERN_ERR "highmem size %uMB results in "
  600. "smaller than 64MB lowmem, ignoring it.\n"
  601. , pages_to_mb(highmem_pages));
  602. highmem_pages = 0;
  603. }
  604. max_low_pfn -= highmem_pages;
  605. }
  606. #else
  607. if (highmem_pages)
  608. printk(KERN_ERR "ignoring highmem size on non-highmem"
  609. " kernel!\n");
  610. #endif
  611. }
  612. }
  613. #ifndef CONFIG_NEED_MULTIPLE_NODES
  614. void __init initmem_init(unsigned long start_pfn,
  615. unsigned long end_pfn)
  616. {
  617. #ifdef CONFIG_HIGHMEM
  618. highstart_pfn = highend_pfn = max_pfn;
  619. if (max_pfn > max_low_pfn)
  620. highstart_pfn = max_low_pfn;
  621. memory_present(0, 0, highend_pfn);
  622. e820_register_active_regions(0, 0, highend_pfn);
  623. printk(KERN_NOTICE "%ldMB HIGHMEM available.\n",
  624. pages_to_mb(highend_pfn - highstart_pfn));
  625. num_physpages = highend_pfn;
  626. high_memory = (void *) __va(highstart_pfn * PAGE_SIZE - 1) + 1;
  627. #else
  628. memory_present(0, 0, max_low_pfn);
  629. e820_register_active_regions(0, 0, max_low_pfn);
  630. num_physpages = max_low_pfn;
  631. high_memory = (void *) __va(max_low_pfn * PAGE_SIZE - 1) + 1;
  632. #endif
  633. #ifdef CONFIG_FLATMEM
  634. max_mapnr = num_physpages;
  635. #endif
  636. printk(KERN_NOTICE "%ldMB LOWMEM available.\n",
  637. pages_to_mb(max_low_pfn));
  638. setup_bootmem_allocator();
  639. }
  640. #endif /* !CONFIG_NEED_MULTIPLE_NODES */
  641. static void __init zone_sizes_init(void)
  642. {
  643. unsigned long max_zone_pfns[MAX_NR_ZONES];
  644. memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
  645. max_zone_pfns[ZONE_DMA] =
  646. virt_to_phys((char *)MAX_DMA_ADDRESS) >> PAGE_SHIFT;
  647. max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
  648. #ifdef CONFIG_HIGHMEM
  649. max_zone_pfns[ZONE_HIGHMEM] = highend_pfn;
  650. #endif
  651. free_area_init_nodes(max_zone_pfns);
  652. }
  653. void __init setup_bootmem_allocator(void)
  654. {
  655. int i;
  656. unsigned long bootmap_size, bootmap;
  657. /*
  658. * Initialize the boot-time allocator (with low memory only):
  659. */
  660. bootmap_size = bootmem_bootmap_pages(max_low_pfn)<<PAGE_SHIFT;
  661. bootmap = find_e820_area(min_low_pfn<<PAGE_SHIFT,
  662. max_pfn_mapped<<PAGE_SHIFT, bootmap_size,
  663. PAGE_SIZE);
  664. if (bootmap == -1L)
  665. panic("Cannot find bootmem map of size %ld\n", bootmap_size);
  666. reserve_early(bootmap, bootmap + bootmap_size, "BOOTMAP");
  667. /* don't touch min_low_pfn */
  668. bootmap_size = init_bootmem_node(NODE_DATA(0), bootmap >> PAGE_SHIFT,
  669. min_low_pfn, max_low_pfn);
  670. printk(KERN_INFO " mapped low ram: 0 - %08lx\n",
  671. max_pfn_mapped<<PAGE_SHIFT);
  672. printk(KERN_INFO " low ram: %08lx - %08lx\n",
  673. min_low_pfn<<PAGE_SHIFT, max_low_pfn<<PAGE_SHIFT);
  674. printk(KERN_INFO " bootmap %08lx - %08lx\n",
  675. bootmap, bootmap + bootmap_size);
  676. for_each_online_node(i)
  677. free_bootmem_with_active_regions(i, max_low_pfn);
  678. early_res_to_bootmem(0, max_low_pfn<<PAGE_SHIFT);
  679. after_init_bootmem = 1;
  680. }
  681. static void __init find_early_table_space(unsigned long end, int use_pse)
  682. {
  683. unsigned long puds, pmds, ptes, tables, start;
  684. puds = (end + PUD_SIZE - 1) >> PUD_SHIFT;
  685. tables = PAGE_ALIGN(puds * sizeof(pud_t));
  686. pmds = (end + PMD_SIZE - 1) >> PMD_SHIFT;
  687. tables += PAGE_ALIGN(pmds * sizeof(pmd_t));
  688. if (use_pse) {
  689. unsigned long extra;
  690. extra = end - ((end>>PMD_SHIFT) << PMD_SHIFT);
  691. extra += PMD_SIZE;
  692. ptes = (extra + PAGE_SIZE - 1) >> PAGE_SHIFT;
  693. } else
  694. ptes = (end + PAGE_SIZE - 1) >> PAGE_SHIFT;
  695. tables += PAGE_ALIGN(ptes * sizeof(pte_t));
  696. /* for fixmap */
  697. tables += PAGE_SIZE * 2;
  698. /*
  699. * RED-PEN putting page tables only on node 0 could
  700. * cause a hotspot and fill up ZONE_DMA. The page tables
  701. * need roughly 0.5KB per GB.
  702. */
  703. start = 0x7000;
  704. table_start = find_e820_area(start, max_pfn_mapped<<PAGE_SHIFT,
  705. tables, PAGE_SIZE);
  706. if (table_start == -1UL)
  707. panic("Cannot find space for the kernel page tables");
  708. table_start >>= PAGE_SHIFT;
  709. table_end = table_start;
  710. table_top = table_start + (tables>>PAGE_SHIFT);
  711. printk(KERN_DEBUG "kernel direct mapping tables up to %lx @ %lx-%lx\n",
  712. end, table_start << PAGE_SHIFT,
  713. (table_start << PAGE_SHIFT) + tables);
  714. }
  715. unsigned long __init_refok init_memory_mapping(unsigned long start,
  716. unsigned long end)
  717. {
  718. pgd_t *pgd_base = swapper_pg_dir;
  719. unsigned long start_pfn, end_pfn;
  720. unsigned long big_page_start;
  721. #ifdef CONFIG_DEBUG_PAGEALLOC
  722. /*
  723. * For CONFIG_DEBUG_PAGEALLOC, identity mapping will use small pages.
  724. * This will simplify cpa(), which otherwise needs to support splitting
  725. * large pages into small in interrupt context, etc.
  726. */
  727. int use_pse = 0;
  728. #else
  729. int use_pse = cpu_has_pse;
  730. #endif
  731. /*
  732. * Find space for the kernel direct mapping tables.
  733. */
  734. if (!after_init_bootmem)
  735. find_early_table_space(end, use_pse);
  736. #ifdef CONFIG_X86_PAE
  737. set_nx();
  738. if (nx_enabled)
  739. printk(KERN_INFO "NX (Execute Disable) protection: active\n");
  740. #endif
  741. /* Enable PSE if available */
  742. if (cpu_has_pse)
  743. set_in_cr4(X86_CR4_PSE);
  744. /* Enable PGE if available */
  745. if (cpu_has_pge) {
  746. set_in_cr4(X86_CR4_PGE);
  747. __supported_pte_mask |= _PAGE_GLOBAL;
  748. }
  749. /*
  750. * Don't use a large page for the first 2/4MB of memory
  751. * because there are often fixed size MTRRs in there
  752. * and overlapping MTRRs into large pages can cause
  753. * slowdowns.
  754. */
  755. big_page_start = PMD_SIZE;
  756. if (start < big_page_start) {
  757. start_pfn = start >> PAGE_SHIFT;
  758. end_pfn = min(big_page_start>>PAGE_SHIFT, end>>PAGE_SHIFT);
  759. } else {
  760. /* head is not big page alignment ? */
  761. start_pfn = start >> PAGE_SHIFT;
  762. end_pfn = ((start + (PMD_SIZE - 1))>>PMD_SHIFT)
  763. << (PMD_SHIFT - PAGE_SHIFT);
  764. }
  765. if (start_pfn < end_pfn)
  766. kernel_physical_mapping_init(pgd_base, start_pfn, end_pfn, 0);
  767. /* big page range */
  768. start_pfn = ((start + (PMD_SIZE - 1))>>PMD_SHIFT)
  769. << (PMD_SHIFT - PAGE_SHIFT);
  770. if (start_pfn < (big_page_start >> PAGE_SHIFT))
  771. start_pfn = big_page_start >> PAGE_SHIFT;
  772. end_pfn = (end>>PMD_SHIFT) << (PMD_SHIFT - PAGE_SHIFT);
  773. if (start_pfn < end_pfn)
  774. kernel_physical_mapping_init(pgd_base, start_pfn, end_pfn,
  775. use_pse);
  776. /* tail is not big page alignment ? */
  777. start_pfn = end_pfn;
  778. if (start_pfn > (big_page_start>>PAGE_SHIFT)) {
  779. end_pfn = end >> PAGE_SHIFT;
  780. if (start_pfn < end_pfn)
  781. kernel_physical_mapping_init(pgd_base, start_pfn,
  782. end_pfn, 0);
  783. }
  784. early_ioremap_page_table_range_init(pgd_base);
  785. load_cr3(swapper_pg_dir);
  786. __flush_tlb_all();
  787. if (!after_init_bootmem)
  788. reserve_early(table_start << PAGE_SHIFT,
  789. table_end << PAGE_SHIFT, "PGTABLE");
  790. if (!after_init_bootmem)
  791. early_memtest(start, end);
  792. return end >> PAGE_SHIFT;
  793. }
  794. /*
  795. * paging_init() sets up the page tables - note that the first 8MB are
  796. * already mapped by head.S.
  797. *
  798. * This routines also unmaps the page at virtual kernel address 0, so
  799. * that we can trap those pesky NULL-reference errors in the kernel.
  800. */
  801. void __init paging_init(void)
  802. {
  803. pagetable_init();
  804. __flush_tlb_all();
  805. kmap_init();
  806. /*
  807. * NOTE: at this point the bootmem allocator is fully available.
  808. */
  809. sparse_init();
  810. zone_sizes_init();
  811. }
  812. /*
  813. * Test if the WP bit works in supervisor mode. It isn't supported on 386's
  814. * and also on some strange 486's. All 586+'s are OK. This used to involve
  815. * black magic jumps to work around some nasty CPU bugs, but fortunately the
  816. * switch to using exceptions got rid of all that.
  817. */
  818. static void __init test_wp_bit(void)
  819. {
  820. printk(KERN_INFO
  821. "Checking if this processor honours the WP bit even in supervisor mode...");
  822. /* Any page-aligned address will do, the test is non-destructive */
  823. __set_fixmap(FIX_WP_TEST, __pa(&swapper_pg_dir), PAGE_READONLY);
  824. boot_cpu_data.wp_works_ok = do_test_wp_bit();
  825. clear_fixmap(FIX_WP_TEST);
  826. if (!boot_cpu_data.wp_works_ok) {
  827. printk(KERN_CONT "No.\n");
  828. #ifdef CONFIG_X86_WP_WORKS_OK
  829. panic(
  830. "This kernel doesn't support CPU's with broken WP. Recompile it for a 386!");
  831. #endif
  832. } else {
  833. printk(KERN_CONT "Ok.\n");
  834. }
  835. }
  836. static struct kcore_list kcore_mem, kcore_vmalloc;
  837. void __init mem_init(void)
  838. {
  839. int codesize, reservedpages, datasize, initsize;
  840. int tmp;
  841. pci_iommu_alloc();
  842. #ifdef CONFIG_FLATMEM
  843. BUG_ON(!mem_map);
  844. #endif
  845. /* this will put all low memory onto the freelists */
  846. totalram_pages += free_all_bootmem();
  847. reservedpages = 0;
  848. for (tmp = 0; tmp < max_low_pfn; tmp++)
  849. /*
  850. * Only count reserved RAM pages:
  851. */
  852. if (page_is_ram(tmp) && PageReserved(pfn_to_page(tmp)))
  853. reservedpages++;
  854. set_highmem_pages_init();
  855. codesize = (unsigned long) &_etext - (unsigned long) &_text;
  856. datasize = (unsigned long) &_edata - (unsigned long) &_etext;
  857. initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
  858. kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT);
  859. kclist_add(&kcore_vmalloc, (void *)VMALLOC_START,
  860. VMALLOC_END-VMALLOC_START);
  861. printk(KERN_INFO "Memory: %luk/%luk available (%dk kernel code, "
  862. "%dk reserved, %dk data, %dk init, %ldk highmem)\n",
  863. (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
  864. num_physpages << (PAGE_SHIFT-10),
  865. codesize >> 10,
  866. reservedpages << (PAGE_SHIFT-10),
  867. datasize >> 10,
  868. initsize >> 10,
  869. (unsigned long) (totalhigh_pages << (PAGE_SHIFT-10))
  870. );
  871. printk(KERN_INFO "virtual kernel memory layout:\n"
  872. " fixmap : 0x%08lx - 0x%08lx (%4ld kB)\n"
  873. #ifdef CONFIG_HIGHMEM
  874. " pkmap : 0x%08lx - 0x%08lx (%4ld kB)\n"
  875. #endif
  876. " vmalloc : 0x%08lx - 0x%08lx (%4ld MB)\n"
  877. " lowmem : 0x%08lx - 0x%08lx (%4ld MB)\n"
  878. " .init : 0x%08lx - 0x%08lx (%4ld kB)\n"
  879. " .data : 0x%08lx - 0x%08lx (%4ld kB)\n"
  880. " .text : 0x%08lx - 0x%08lx (%4ld kB)\n",
  881. FIXADDR_START, FIXADDR_TOP,
  882. (FIXADDR_TOP - FIXADDR_START) >> 10,
  883. #ifdef CONFIG_HIGHMEM
  884. PKMAP_BASE, PKMAP_BASE+LAST_PKMAP*PAGE_SIZE,
  885. (LAST_PKMAP*PAGE_SIZE) >> 10,
  886. #endif
  887. VMALLOC_START, VMALLOC_END,
  888. (VMALLOC_END - VMALLOC_START) >> 20,
  889. (unsigned long)__va(0), (unsigned long)high_memory,
  890. ((unsigned long)high_memory - (unsigned long)__va(0)) >> 20,
  891. (unsigned long)&__init_begin, (unsigned long)&__init_end,
  892. ((unsigned long)&__init_end -
  893. (unsigned long)&__init_begin) >> 10,
  894. (unsigned long)&_etext, (unsigned long)&_edata,
  895. ((unsigned long)&_edata - (unsigned long)&_etext) >> 10,
  896. (unsigned long)&_text, (unsigned long)&_etext,
  897. ((unsigned long)&_etext - (unsigned long)&_text) >> 10);
  898. /*
  899. * Check boundaries twice: Some fundamental inconsistencies can
  900. * be detected at build time already.
  901. */
  902. #define __FIXADDR_TOP (-PAGE_SIZE)
  903. #ifdef CONFIG_HIGHMEM
  904. BUILD_BUG_ON(PKMAP_BASE + LAST_PKMAP*PAGE_SIZE > FIXADDR_START);
  905. BUILD_BUG_ON(VMALLOC_END > PKMAP_BASE);
  906. #endif
  907. #define high_memory (-128UL << 20)
  908. BUILD_BUG_ON(VMALLOC_START >= VMALLOC_END);
  909. #undef high_memory
  910. #undef __FIXADDR_TOP
  911. #ifdef CONFIG_HIGHMEM
  912. BUG_ON(PKMAP_BASE + LAST_PKMAP*PAGE_SIZE > FIXADDR_START);
  913. BUG_ON(VMALLOC_END > PKMAP_BASE);
  914. #endif
  915. BUG_ON(VMALLOC_START >= VMALLOC_END);
  916. BUG_ON((unsigned long)high_memory > VMALLOC_START);
  917. if (boot_cpu_data.wp_works_ok < 0)
  918. test_wp_bit();
  919. save_pg_dir();
  920. zap_low_mappings();
  921. }
  922. #ifdef CONFIG_MEMORY_HOTPLUG
  923. int arch_add_memory(int nid, u64 start, u64 size)
  924. {
  925. struct pglist_data *pgdata = NODE_DATA(nid);
  926. struct zone *zone = pgdata->node_zones + ZONE_HIGHMEM;
  927. unsigned long start_pfn = start >> PAGE_SHIFT;
  928. unsigned long nr_pages = size >> PAGE_SHIFT;
  929. return __add_pages(nid, zone, start_pfn, nr_pages);
  930. }
  931. #endif
  932. /*
  933. * This function cannot be __init, since exceptions don't work in that
  934. * section. Put this after the callers, so that it cannot be inlined.
  935. */
  936. static noinline int do_test_wp_bit(void)
  937. {
  938. char tmp_reg;
  939. int flag;
  940. __asm__ __volatile__(
  941. " movb %0, %1 \n"
  942. "1: movb %1, %0 \n"
  943. " xorl %2, %2 \n"
  944. "2: \n"
  945. _ASM_EXTABLE(1b,2b)
  946. :"=m" (*(char *)fix_to_virt(FIX_WP_TEST)),
  947. "=q" (tmp_reg),
  948. "=r" (flag)
  949. :"2" (1)
  950. :"memory");
  951. return flag;
  952. }
  953. #ifdef CONFIG_DEBUG_RODATA
  954. const int rodata_test_data = 0xC3;
  955. EXPORT_SYMBOL_GPL(rodata_test_data);
  956. void mark_rodata_ro(void)
  957. {
  958. unsigned long start = PFN_ALIGN(_text);
  959. unsigned long size = PFN_ALIGN(_etext) - start;
  960. #ifndef CONFIG_DYNAMIC_FTRACE
  961. /* Dynamic tracing modifies the kernel text section */
  962. set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
  963. printk(KERN_INFO "Write protecting the kernel text: %luk\n",
  964. size >> 10);
  965. #ifdef CONFIG_CPA_DEBUG
  966. printk(KERN_INFO "Testing CPA: Reverting %lx-%lx\n",
  967. start, start+size);
  968. set_pages_rw(virt_to_page(start), size>>PAGE_SHIFT);
  969. printk(KERN_INFO "Testing CPA: write protecting again\n");
  970. set_pages_ro(virt_to_page(start), size>>PAGE_SHIFT);
  971. #endif
  972. #endif /* CONFIG_DYNAMIC_FTRACE */
  973. start += size;
  974. size = (unsigned long)__end_rodata - start;
  975. set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
  976. printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n",
  977. size >> 10);
  978. rodata_test();
  979. #ifdef CONFIG_CPA_DEBUG
  980. printk(KERN_INFO "Testing CPA: undo %lx-%lx\n", start, start + size);
  981. set_pages_rw(virt_to_page(start), size >> PAGE_SHIFT);
  982. printk(KERN_INFO "Testing CPA: write protecting again\n");
  983. set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
  984. #endif
  985. }
  986. #endif
  987. void free_init_pages(char *what, unsigned long begin, unsigned long end)
  988. {
  989. #ifdef CONFIG_DEBUG_PAGEALLOC
  990. /*
  991. * If debugging page accesses then do not free this memory but
  992. * mark them not present - any buggy init-section access will
  993. * create a kernel page fault:
  994. */
  995. printk(KERN_INFO "debug: unmapping init memory %08lx..%08lx\n",
  996. begin, PAGE_ALIGN(end));
  997. set_memory_np(begin, (end - begin) >> PAGE_SHIFT);
  998. #else
  999. unsigned long addr;
  1000. /*
  1001. * We just marked the kernel text read only above, now that
  1002. * we are going to free part of that, we need to make that
  1003. * writeable first.
  1004. */
  1005. set_memory_rw(begin, (end - begin) >> PAGE_SHIFT);
  1006. for (addr = begin; addr < end; addr += PAGE_SIZE) {
  1007. ClearPageReserved(virt_to_page(addr));
  1008. init_page_count(virt_to_page(addr));
  1009. memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE);
  1010. free_page(addr);
  1011. totalram_pages++;
  1012. }
  1013. printk(KERN_INFO "Freeing %s: %luk freed\n", what, (end - begin) >> 10);
  1014. #endif
  1015. }
  1016. void free_initmem(void)
  1017. {
  1018. free_init_pages("unused kernel memory",
  1019. (unsigned long)(&__init_begin),
  1020. (unsigned long)(&__init_end));
  1021. }
  1022. #ifdef CONFIG_BLK_DEV_INITRD
  1023. void free_initrd_mem(unsigned long start, unsigned long end)
  1024. {
  1025. free_init_pages("initrd memory", start, end);
  1026. }
  1027. #endif
  1028. int __init reserve_bootmem_generic(unsigned long phys, unsigned long len,
  1029. int flags)
  1030. {
  1031. return reserve_bootmem(phys, len, flags);
  1032. }