init_32.c 30 KB

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