init_32.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  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/pfn.h>
  24. #include <linux/poison.h>
  25. #include <linux/bootmem.h>
  26. #include <linux/slab.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 <asm/asm.h>
  32. #include <asm/bios_ebda.h>
  33. #include <asm/processor.h>
  34. #include <asm/system.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/pgalloc.h>
  45. #include <asm/sections.h>
  46. #include <asm/paravirt.h>
  47. #include <asm/setup.h>
  48. #include <asm/cacheflush.h>
  49. #include <asm/smp.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 (!page_is_ram(pagenr))
  290. return 1;
  291. return 0;
  292. }
  293. pte_t *kmap_pte;
  294. pgprot_t kmap_prot;
  295. static inline pte_t *kmap_get_fixmap_pte(unsigned long vaddr)
  296. {
  297. return pte_offset_kernel(pmd_offset(pud_offset(pgd_offset_k(vaddr),
  298. vaddr), vaddr), vaddr);
  299. }
  300. static void __init kmap_init(void)
  301. {
  302. unsigned long kmap_vstart;
  303. /*
  304. * Cache the first kmap pte:
  305. */
  306. kmap_vstart = __fix_to_virt(FIX_KMAP_BEGIN);
  307. kmap_pte = kmap_get_fixmap_pte(kmap_vstart);
  308. kmap_prot = PAGE_KERNEL;
  309. }
  310. #ifdef CONFIG_HIGHMEM
  311. static void __init permanent_kmaps_init(pgd_t *pgd_base)
  312. {
  313. unsigned long vaddr;
  314. pgd_t *pgd;
  315. pud_t *pud;
  316. pmd_t *pmd;
  317. pte_t *pte;
  318. vaddr = PKMAP_BASE;
  319. page_table_range_init(vaddr, vaddr + PAGE_SIZE*LAST_PKMAP, pgd_base);
  320. pgd = swapper_pg_dir + pgd_index(vaddr);
  321. pud = pud_offset(pgd, vaddr);
  322. pmd = pmd_offset(pud, vaddr);
  323. pte = pte_offset_kernel(pmd, vaddr);
  324. pkmap_page_table = pte;
  325. }
  326. static void __init add_one_highpage_init(struct page *page, int pfn)
  327. {
  328. ClearPageReserved(page);
  329. init_page_count(page);
  330. __free_page(page);
  331. totalhigh_pages++;
  332. }
  333. struct add_highpages_data {
  334. unsigned long start_pfn;
  335. unsigned long end_pfn;
  336. };
  337. static int __init add_highpages_work_fn(unsigned long start_pfn,
  338. unsigned long end_pfn, void *datax)
  339. {
  340. int node_pfn;
  341. struct page *page;
  342. unsigned long final_start_pfn, final_end_pfn;
  343. struct add_highpages_data *data;
  344. data = (struct add_highpages_data *)datax;
  345. final_start_pfn = max(start_pfn, data->start_pfn);
  346. final_end_pfn = min(end_pfn, data->end_pfn);
  347. if (final_start_pfn >= final_end_pfn)
  348. return 0;
  349. for (node_pfn = final_start_pfn; node_pfn < final_end_pfn;
  350. node_pfn++) {
  351. if (!pfn_valid(node_pfn))
  352. continue;
  353. page = pfn_to_page(node_pfn);
  354. add_one_highpage_init(page, node_pfn);
  355. }
  356. return 0;
  357. }
  358. void __init add_highpages_with_active_regions(int nid, unsigned long start_pfn,
  359. unsigned long end_pfn)
  360. {
  361. struct add_highpages_data data;
  362. data.start_pfn = start_pfn;
  363. data.end_pfn = end_pfn;
  364. work_with_active_regions(nid, add_highpages_work_fn, &data);
  365. }
  366. #ifndef CONFIG_NUMA
  367. static void __init set_highmem_pages_init(void)
  368. {
  369. add_highpages_with_active_regions(0, highstart_pfn, highend_pfn);
  370. totalram_pages += totalhigh_pages;
  371. }
  372. #endif /* !CONFIG_NUMA */
  373. #else
  374. # define permanent_kmaps_init(pgd_base) do { } while (0)
  375. # define set_highmem_pages_init() do { } while (0)
  376. #endif /* CONFIG_HIGHMEM */
  377. void __init native_pagetable_setup_start(pgd_t *base)
  378. {
  379. unsigned long pfn, va;
  380. pgd_t *pgd;
  381. pud_t *pud;
  382. pmd_t *pmd;
  383. pte_t *pte;
  384. /*
  385. * Remove any mappings which extend past the end of physical
  386. * memory from the boot time page table:
  387. */
  388. for (pfn = max_low_pfn + 1; pfn < 1<<(32-PAGE_SHIFT); pfn++) {
  389. va = PAGE_OFFSET + (pfn<<PAGE_SHIFT);
  390. pgd = base + pgd_index(va);
  391. if (!pgd_present(*pgd))
  392. break;
  393. pud = pud_offset(pgd, va);
  394. pmd = pmd_offset(pud, va);
  395. if (!pmd_present(*pmd))
  396. break;
  397. pte = pte_offset_kernel(pmd, va);
  398. if (!pte_present(*pte))
  399. break;
  400. pte_clear(NULL, va, pte);
  401. }
  402. paravirt_alloc_pmd(&init_mm, __pa(base) >> PAGE_SHIFT);
  403. }
  404. void __init native_pagetable_setup_done(pgd_t *base)
  405. {
  406. }
  407. /*
  408. * Build a proper pagetable for the kernel mappings. Up until this
  409. * point, we've been running on some set of pagetables constructed by
  410. * the boot process.
  411. *
  412. * If we're booting on native hardware, this will be a pagetable
  413. * constructed in arch/x86/kernel/head_32.S. The root of the
  414. * pagetable will be swapper_pg_dir.
  415. *
  416. * If we're booting paravirtualized under a hypervisor, then there are
  417. * more options: we may already be running PAE, and the pagetable may
  418. * or may not be based in swapper_pg_dir. In any case,
  419. * paravirt_pagetable_setup_start() will set up swapper_pg_dir
  420. * appropriately for the rest of the initialization to work.
  421. *
  422. * In general, pagetable_init() assumes that the pagetable may already
  423. * be partially populated, and so it avoids stomping on any existing
  424. * mappings.
  425. */
  426. static void __init early_ioremap_page_table_range_init(pgd_t *pgd_base)
  427. {
  428. unsigned long vaddr, end;
  429. /*
  430. * Fixed mappings, only the page table structure has to be
  431. * created - mappings will be set by set_fixmap():
  432. */
  433. early_ioremap_clear();
  434. vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK;
  435. end = (FIXADDR_TOP + PMD_SIZE - 1) & PMD_MASK;
  436. page_table_range_init(vaddr, end, pgd_base);
  437. early_ioremap_reset();
  438. }
  439. static void __init pagetable_init(void)
  440. {
  441. pgd_t *pgd_base = swapper_pg_dir;
  442. permanent_kmaps_init(pgd_base);
  443. }
  444. #ifdef CONFIG_ACPI_SLEEP
  445. /*
  446. * ACPI suspend needs this for resume, because things like the intel-agp
  447. * driver might have split up a kernel 4MB mapping.
  448. */
  449. char swsusp_pg_dir[PAGE_SIZE]
  450. __attribute__ ((aligned(PAGE_SIZE)));
  451. static inline void save_pg_dir(void)
  452. {
  453. memcpy(swsusp_pg_dir, swapper_pg_dir, PAGE_SIZE);
  454. }
  455. #else /* !CONFIG_ACPI_SLEEP */
  456. static inline void save_pg_dir(void)
  457. {
  458. }
  459. #endif /* !CONFIG_ACPI_SLEEP */
  460. void zap_low_mappings(void)
  461. {
  462. int i;
  463. /*
  464. * Zap initial low-memory mappings.
  465. *
  466. * Note that "pgd_clear()" doesn't do it for
  467. * us, because pgd_clear() is a no-op on i386.
  468. */
  469. for (i = 0; i < KERNEL_PGD_BOUNDARY; i++) {
  470. #ifdef CONFIG_X86_PAE
  471. set_pgd(swapper_pg_dir+i, __pgd(1 + __pa(empty_zero_page)));
  472. #else
  473. set_pgd(swapper_pg_dir+i, __pgd(0));
  474. #endif
  475. }
  476. flush_tlb_all();
  477. }
  478. int nx_enabled;
  479. pteval_t __supported_pte_mask __read_mostly = ~(_PAGE_NX | _PAGE_GLOBAL | _PAGE_IOMAP);
  480. EXPORT_SYMBOL_GPL(__supported_pte_mask);
  481. #ifdef CONFIG_X86_PAE
  482. static int disable_nx __initdata;
  483. /*
  484. * noexec = on|off
  485. *
  486. * Control non executable mappings.
  487. *
  488. * on Enable
  489. * off Disable
  490. */
  491. static int __init noexec_setup(char *str)
  492. {
  493. if (!str || !strcmp(str, "on")) {
  494. if (cpu_has_nx) {
  495. __supported_pte_mask |= _PAGE_NX;
  496. disable_nx = 0;
  497. }
  498. } else {
  499. if (!strcmp(str, "off")) {
  500. disable_nx = 1;
  501. __supported_pte_mask &= ~_PAGE_NX;
  502. } else {
  503. return -EINVAL;
  504. }
  505. }
  506. return 0;
  507. }
  508. early_param("noexec", noexec_setup);
  509. static void __init set_nx(void)
  510. {
  511. unsigned int v[4], l, h;
  512. if (cpu_has_pae && (cpuid_eax(0x80000000) > 0x80000001)) {
  513. cpuid(0x80000001, &v[0], &v[1], &v[2], &v[3]);
  514. if ((v[3] & (1 << 20)) && !disable_nx) {
  515. rdmsr(MSR_EFER, l, h);
  516. l |= EFER_NX;
  517. wrmsr(MSR_EFER, l, h);
  518. nx_enabled = 1;
  519. __supported_pte_mask |= _PAGE_NX;
  520. }
  521. }
  522. }
  523. #endif
  524. /* user-defined highmem size */
  525. static unsigned int highmem_pages = -1;
  526. /*
  527. * highmem=size forces highmem to be exactly 'size' bytes.
  528. * This works even on boxes that have no highmem otherwise.
  529. * This also works to reduce highmem size on bigger boxes.
  530. */
  531. static int __init parse_highmem(char *arg)
  532. {
  533. if (!arg)
  534. return -EINVAL;
  535. highmem_pages = memparse(arg, &arg) >> PAGE_SHIFT;
  536. return 0;
  537. }
  538. early_param("highmem", parse_highmem);
  539. /*
  540. * Determine low and high memory ranges:
  541. */
  542. void __init find_low_pfn_range(void)
  543. {
  544. /* it could update max_pfn */
  545. /* max_low_pfn is 0, we already have early_res support */
  546. max_low_pfn = max_pfn;
  547. if (max_low_pfn > MAXMEM_PFN) {
  548. if (highmem_pages == -1)
  549. highmem_pages = max_pfn - MAXMEM_PFN;
  550. if (highmem_pages + MAXMEM_PFN < max_pfn)
  551. max_pfn = MAXMEM_PFN + highmem_pages;
  552. if (highmem_pages + MAXMEM_PFN > max_pfn) {
  553. printk(KERN_WARNING "only %luMB highmem pages "
  554. "available, ignoring highmem size of %uMB.\n",
  555. pages_to_mb(max_pfn - MAXMEM_PFN),
  556. pages_to_mb(highmem_pages));
  557. highmem_pages = 0;
  558. }
  559. max_low_pfn = MAXMEM_PFN;
  560. #ifndef CONFIG_HIGHMEM
  561. /* Maximum memory usable is what is directly addressable */
  562. printk(KERN_WARNING "Warning only %ldMB will be used.\n",
  563. MAXMEM>>20);
  564. if (max_pfn > MAX_NONPAE_PFN)
  565. printk(KERN_WARNING
  566. "Use a HIGHMEM64G enabled kernel.\n");
  567. else
  568. printk(KERN_WARNING "Use a HIGHMEM enabled kernel.\n");
  569. max_pfn = MAXMEM_PFN;
  570. #else /* !CONFIG_HIGHMEM */
  571. #ifndef CONFIG_HIGHMEM64G
  572. if (max_pfn > MAX_NONPAE_PFN) {
  573. max_pfn = MAX_NONPAE_PFN;
  574. printk(KERN_WARNING "Warning only 4GB will be used."
  575. "Use a HIGHMEM64G enabled kernel.\n");
  576. }
  577. #endif /* !CONFIG_HIGHMEM64G */
  578. #endif /* !CONFIG_HIGHMEM */
  579. } else {
  580. if (highmem_pages == -1)
  581. highmem_pages = 0;
  582. #ifdef CONFIG_HIGHMEM
  583. if (highmem_pages >= max_pfn) {
  584. printk(KERN_ERR "highmem size specified (%uMB) is "
  585. "bigger than pages available (%luMB)!.\n",
  586. pages_to_mb(highmem_pages),
  587. pages_to_mb(max_pfn));
  588. highmem_pages = 0;
  589. }
  590. if (highmem_pages) {
  591. if (max_low_pfn - highmem_pages <
  592. 64*1024*1024/PAGE_SIZE){
  593. printk(KERN_ERR "highmem size %uMB results in "
  594. "smaller than 64MB lowmem, ignoring it.\n"
  595. , pages_to_mb(highmem_pages));
  596. highmem_pages = 0;
  597. }
  598. max_low_pfn -= highmem_pages;
  599. }
  600. #else
  601. if (highmem_pages)
  602. printk(KERN_ERR "ignoring highmem size on non-highmem"
  603. " kernel!\n");
  604. #endif
  605. }
  606. }
  607. #ifndef CONFIG_NEED_MULTIPLE_NODES
  608. void __init initmem_init(unsigned long start_pfn,
  609. unsigned long end_pfn)
  610. {
  611. #ifdef CONFIG_HIGHMEM
  612. highstart_pfn = highend_pfn = max_pfn;
  613. if (max_pfn > max_low_pfn)
  614. highstart_pfn = max_low_pfn;
  615. memory_present(0, 0, highend_pfn);
  616. e820_register_active_regions(0, 0, highend_pfn);
  617. printk(KERN_NOTICE "%ldMB HIGHMEM available.\n",
  618. pages_to_mb(highend_pfn - highstart_pfn));
  619. num_physpages = highend_pfn;
  620. high_memory = (void *) __va(highstart_pfn * PAGE_SIZE - 1) + 1;
  621. #else
  622. memory_present(0, 0, max_low_pfn);
  623. e820_register_active_regions(0, 0, max_low_pfn);
  624. num_physpages = max_low_pfn;
  625. high_memory = (void *) __va(max_low_pfn * PAGE_SIZE - 1) + 1;
  626. #endif
  627. #ifdef CONFIG_FLATMEM
  628. max_mapnr = num_physpages;
  629. #endif
  630. printk(KERN_NOTICE "%ldMB LOWMEM available.\n",
  631. pages_to_mb(max_low_pfn));
  632. setup_bootmem_allocator();
  633. }
  634. #endif /* !CONFIG_NEED_MULTIPLE_NODES */
  635. static void __init zone_sizes_init(void)
  636. {
  637. unsigned long max_zone_pfns[MAX_NR_ZONES];
  638. memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
  639. max_zone_pfns[ZONE_DMA] =
  640. virt_to_phys((char *)MAX_DMA_ADDRESS) >> PAGE_SHIFT;
  641. max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
  642. #ifdef CONFIG_HIGHMEM
  643. max_zone_pfns[ZONE_HIGHMEM] = highend_pfn;
  644. #endif
  645. free_area_init_nodes(max_zone_pfns);
  646. }
  647. void __init setup_bootmem_allocator(void)
  648. {
  649. int i;
  650. unsigned long bootmap_size, bootmap;
  651. /*
  652. * Initialize the boot-time allocator (with low memory only):
  653. */
  654. bootmap_size = bootmem_bootmap_pages(max_low_pfn)<<PAGE_SHIFT;
  655. bootmap = find_e820_area(min_low_pfn<<PAGE_SHIFT,
  656. max_pfn_mapped<<PAGE_SHIFT, bootmap_size,
  657. PAGE_SIZE);
  658. if (bootmap == -1L)
  659. panic("Cannot find bootmem map of size %ld\n", bootmap_size);
  660. reserve_early(bootmap, bootmap + bootmap_size, "BOOTMAP");
  661. /* don't touch min_low_pfn */
  662. bootmap_size = init_bootmem_node(NODE_DATA(0), bootmap >> PAGE_SHIFT,
  663. min_low_pfn, max_low_pfn);
  664. printk(KERN_INFO " mapped low ram: 0 - %08lx\n",
  665. max_pfn_mapped<<PAGE_SHIFT);
  666. printk(KERN_INFO " low ram: %08lx - %08lx\n",
  667. min_low_pfn<<PAGE_SHIFT, max_low_pfn<<PAGE_SHIFT);
  668. printk(KERN_INFO " bootmap %08lx - %08lx\n",
  669. bootmap, bootmap + bootmap_size);
  670. for_each_online_node(i)
  671. free_bootmem_with_active_regions(i, max_low_pfn);
  672. early_res_to_bootmem(0, max_low_pfn<<PAGE_SHIFT);
  673. after_init_bootmem = 1;
  674. }
  675. static void __init find_early_table_space(unsigned long end, int use_pse)
  676. {
  677. unsigned long puds, pmds, ptes, tables, start;
  678. puds = (end + PUD_SIZE - 1) >> PUD_SHIFT;
  679. tables = PAGE_ALIGN(puds * sizeof(pud_t));
  680. pmds = (end + PMD_SIZE - 1) >> PMD_SHIFT;
  681. tables += PAGE_ALIGN(pmds * sizeof(pmd_t));
  682. if (use_pse) {
  683. unsigned long extra;
  684. extra = end - ((end>>PMD_SHIFT) << PMD_SHIFT);
  685. extra += PMD_SIZE;
  686. ptes = (extra + PAGE_SIZE - 1) >> PAGE_SHIFT;
  687. } else
  688. ptes = (end + PAGE_SIZE - 1) >> PAGE_SHIFT;
  689. tables += PAGE_ALIGN(ptes * sizeof(pte_t));
  690. /* for fixmap */
  691. tables += PAGE_SIZE * 2;
  692. /*
  693. * RED-PEN putting page tables only on node 0 could
  694. * cause a hotspot and fill up ZONE_DMA. The page tables
  695. * need roughly 0.5KB per GB.
  696. */
  697. start = 0x7000;
  698. table_start = find_e820_area(start, max_pfn_mapped<<PAGE_SHIFT,
  699. tables, PAGE_SIZE);
  700. if (table_start == -1UL)
  701. panic("Cannot find space for the kernel page tables");
  702. table_start >>= PAGE_SHIFT;
  703. table_end = table_start;
  704. table_top = table_start + (tables>>PAGE_SHIFT);
  705. printk(KERN_DEBUG "kernel direct mapping tables up to %lx @ %lx-%lx\n",
  706. end, table_start << PAGE_SHIFT,
  707. (table_start << PAGE_SHIFT) + tables);
  708. }
  709. unsigned long __init_refok init_memory_mapping(unsigned long start,
  710. unsigned long end)
  711. {
  712. pgd_t *pgd_base = swapper_pg_dir;
  713. unsigned long start_pfn, end_pfn;
  714. unsigned long big_page_start;
  715. #ifdef CONFIG_DEBUG_PAGEALLOC
  716. /*
  717. * For CONFIG_DEBUG_PAGEALLOC, identity mapping will use small pages.
  718. * This will simplify cpa(), which otherwise needs to support splitting
  719. * large pages into small in interrupt context, etc.
  720. */
  721. int use_pse = 0;
  722. #else
  723. int use_pse = cpu_has_pse;
  724. #endif
  725. /*
  726. * Find space for the kernel direct mapping tables.
  727. */
  728. if (!after_init_bootmem)
  729. find_early_table_space(end, use_pse);
  730. #ifdef CONFIG_X86_PAE
  731. set_nx();
  732. if (nx_enabled)
  733. printk(KERN_INFO "NX (Execute Disable) protection: active\n");
  734. #endif
  735. /* Enable PSE if available */
  736. if (cpu_has_pse)
  737. set_in_cr4(X86_CR4_PSE);
  738. /* Enable PGE if available */
  739. if (cpu_has_pge) {
  740. set_in_cr4(X86_CR4_PGE);
  741. __supported_pte_mask |= _PAGE_GLOBAL;
  742. }
  743. /*
  744. * Don't use a large page for the first 2/4MB of memory
  745. * because there are often fixed size MTRRs in there
  746. * and overlapping MTRRs into large pages can cause
  747. * slowdowns.
  748. */
  749. big_page_start = PMD_SIZE;
  750. if (start < big_page_start) {
  751. start_pfn = start >> PAGE_SHIFT;
  752. end_pfn = min(big_page_start>>PAGE_SHIFT, end>>PAGE_SHIFT);
  753. } else {
  754. /* head is not big page alignment ? */
  755. start_pfn = start >> PAGE_SHIFT;
  756. end_pfn = ((start + (PMD_SIZE - 1))>>PMD_SHIFT)
  757. << (PMD_SHIFT - PAGE_SHIFT);
  758. }
  759. if (start_pfn < end_pfn)
  760. kernel_physical_mapping_init(pgd_base, start_pfn, end_pfn, 0);
  761. /* big page range */
  762. start_pfn = ((start + (PMD_SIZE - 1))>>PMD_SHIFT)
  763. << (PMD_SHIFT - PAGE_SHIFT);
  764. if (start_pfn < (big_page_start >> PAGE_SHIFT))
  765. start_pfn = big_page_start >> PAGE_SHIFT;
  766. end_pfn = (end>>PMD_SHIFT) << (PMD_SHIFT - PAGE_SHIFT);
  767. if (start_pfn < end_pfn)
  768. kernel_physical_mapping_init(pgd_base, start_pfn, end_pfn,
  769. use_pse);
  770. /* tail is not big page alignment ? */
  771. start_pfn = end_pfn;
  772. if (start_pfn > (big_page_start>>PAGE_SHIFT)) {
  773. end_pfn = end >> PAGE_SHIFT;
  774. if (start_pfn < end_pfn)
  775. kernel_physical_mapping_init(pgd_base, start_pfn,
  776. end_pfn, 0);
  777. }
  778. early_ioremap_page_table_range_init(pgd_base);
  779. load_cr3(swapper_pg_dir);
  780. __flush_tlb_all();
  781. if (!after_init_bootmem)
  782. reserve_early(table_start << PAGE_SHIFT,
  783. table_end << PAGE_SHIFT, "PGTABLE");
  784. if (!after_init_bootmem)
  785. early_memtest(start, end);
  786. return end >> PAGE_SHIFT;
  787. }
  788. /*
  789. * paging_init() sets up the page tables - note that the first 8MB are
  790. * already mapped by head.S.
  791. *
  792. * This routines also unmaps the page at virtual kernel address 0, so
  793. * that we can trap those pesky NULL-reference errors in the kernel.
  794. */
  795. void __init paging_init(void)
  796. {
  797. pagetable_init();
  798. __flush_tlb_all();
  799. kmap_init();
  800. /*
  801. * NOTE: at this point the bootmem allocator is fully available.
  802. */
  803. sparse_init();
  804. zone_sizes_init();
  805. }
  806. /*
  807. * Test if the WP bit works in supervisor mode. It isn't supported on 386's
  808. * and also on some strange 486's. All 586+'s are OK. This used to involve
  809. * black magic jumps to work around some nasty CPU bugs, but fortunately the
  810. * switch to using exceptions got rid of all that.
  811. */
  812. static void __init test_wp_bit(void)
  813. {
  814. printk(KERN_INFO
  815. "Checking if this processor honours the WP bit even in supervisor mode...");
  816. /* Any page-aligned address will do, the test is non-destructive */
  817. __set_fixmap(FIX_WP_TEST, __pa(&swapper_pg_dir), PAGE_READONLY);
  818. boot_cpu_data.wp_works_ok = do_test_wp_bit();
  819. clear_fixmap(FIX_WP_TEST);
  820. if (!boot_cpu_data.wp_works_ok) {
  821. printk(KERN_CONT "No.\n");
  822. #ifdef CONFIG_X86_WP_WORKS_OK
  823. panic(
  824. "This kernel doesn't support CPU's with broken WP. Recompile it for a 386!");
  825. #endif
  826. } else {
  827. printk(KERN_CONT "Ok.\n");
  828. }
  829. }
  830. static struct kcore_list kcore_mem, kcore_vmalloc;
  831. void __init mem_init(void)
  832. {
  833. int codesize, reservedpages, datasize, initsize;
  834. int tmp;
  835. start_periodic_check_for_corruption();
  836. #ifdef CONFIG_FLATMEM
  837. BUG_ON(!mem_map);
  838. #endif
  839. /* this will put all low memory onto the freelists */
  840. totalram_pages += free_all_bootmem();
  841. reservedpages = 0;
  842. for (tmp = 0; tmp < max_low_pfn; tmp++)
  843. /*
  844. * Only count reserved RAM pages:
  845. */
  846. if (page_is_ram(tmp) && PageReserved(pfn_to_page(tmp)))
  847. reservedpages++;
  848. set_highmem_pages_init();
  849. codesize = (unsigned long) &_etext - (unsigned long) &_text;
  850. datasize = (unsigned long) &_edata - (unsigned long) &_etext;
  851. initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
  852. kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT);
  853. kclist_add(&kcore_vmalloc, (void *)VMALLOC_START,
  854. VMALLOC_END-VMALLOC_START);
  855. printk(KERN_INFO "Memory: %luk/%luk available (%dk kernel code, "
  856. "%dk reserved, %dk data, %dk init, %ldk highmem)\n",
  857. (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
  858. num_physpages << (PAGE_SHIFT-10),
  859. codesize >> 10,
  860. reservedpages << (PAGE_SHIFT-10),
  861. datasize >> 10,
  862. initsize >> 10,
  863. (unsigned long) (totalhigh_pages << (PAGE_SHIFT-10))
  864. );
  865. printk(KERN_INFO "virtual kernel memory layout:\n"
  866. " fixmap : 0x%08lx - 0x%08lx (%4ld kB)\n"
  867. #ifdef CONFIG_HIGHMEM
  868. " pkmap : 0x%08lx - 0x%08lx (%4ld kB)\n"
  869. #endif
  870. " vmalloc : 0x%08lx - 0x%08lx (%4ld MB)\n"
  871. " lowmem : 0x%08lx - 0x%08lx (%4ld MB)\n"
  872. " .init : 0x%08lx - 0x%08lx (%4ld kB)\n"
  873. " .data : 0x%08lx - 0x%08lx (%4ld kB)\n"
  874. " .text : 0x%08lx - 0x%08lx (%4ld kB)\n",
  875. FIXADDR_START, FIXADDR_TOP,
  876. (FIXADDR_TOP - FIXADDR_START) >> 10,
  877. #ifdef CONFIG_HIGHMEM
  878. PKMAP_BASE, PKMAP_BASE+LAST_PKMAP*PAGE_SIZE,
  879. (LAST_PKMAP*PAGE_SIZE) >> 10,
  880. #endif
  881. VMALLOC_START, VMALLOC_END,
  882. (VMALLOC_END - VMALLOC_START) >> 20,
  883. (unsigned long)__va(0), (unsigned long)high_memory,
  884. ((unsigned long)high_memory - (unsigned long)__va(0)) >> 20,
  885. (unsigned long)&__init_begin, (unsigned long)&__init_end,
  886. ((unsigned long)&__init_end -
  887. (unsigned long)&__init_begin) >> 10,
  888. (unsigned long)&_etext, (unsigned long)&_edata,
  889. ((unsigned long)&_edata - (unsigned long)&_etext) >> 10,
  890. (unsigned long)&_text, (unsigned long)&_etext,
  891. ((unsigned long)&_etext - (unsigned long)&_text) >> 10);
  892. #ifdef CONFIG_HIGHMEM
  893. BUG_ON(PKMAP_BASE + LAST_PKMAP*PAGE_SIZE > FIXADDR_START);
  894. BUG_ON(VMALLOC_END > PKMAP_BASE);
  895. #endif
  896. BUG_ON(VMALLOC_START > VMALLOC_END);
  897. BUG_ON((unsigned long)high_memory > VMALLOC_START);
  898. if (boot_cpu_data.wp_works_ok < 0)
  899. test_wp_bit();
  900. save_pg_dir();
  901. zap_low_mappings();
  902. }
  903. #ifdef CONFIG_MEMORY_HOTPLUG
  904. int arch_add_memory(int nid, u64 start, u64 size)
  905. {
  906. struct pglist_data *pgdata = NODE_DATA(nid);
  907. struct zone *zone = pgdata->node_zones + ZONE_HIGHMEM;
  908. unsigned long start_pfn = start >> PAGE_SHIFT;
  909. unsigned long nr_pages = size >> PAGE_SHIFT;
  910. return __add_pages(zone, start_pfn, nr_pages);
  911. }
  912. #endif
  913. /*
  914. * This function cannot be __init, since exceptions don't work in that
  915. * section. Put this after the callers, so that it cannot be inlined.
  916. */
  917. static noinline int do_test_wp_bit(void)
  918. {
  919. char tmp_reg;
  920. int flag;
  921. __asm__ __volatile__(
  922. " movb %0, %1 \n"
  923. "1: movb %1, %0 \n"
  924. " xorl %2, %2 \n"
  925. "2: \n"
  926. _ASM_EXTABLE(1b,2b)
  927. :"=m" (*(char *)fix_to_virt(FIX_WP_TEST)),
  928. "=q" (tmp_reg),
  929. "=r" (flag)
  930. :"2" (1)
  931. :"memory");
  932. return flag;
  933. }
  934. #ifdef CONFIG_DEBUG_RODATA
  935. const int rodata_test_data = 0xC3;
  936. EXPORT_SYMBOL_GPL(rodata_test_data);
  937. void mark_rodata_ro(void)
  938. {
  939. unsigned long start = PFN_ALIGN(_text);
  940. unsigned long size = PFN_ALIGN(_etext) - start;
  941. #ifndef CONFIG_DYNAMIC_FTRACE
  942. /* Dynamic tracing modifies the kernel text section */
  943. set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
  944. printk(KERN_INFO "Write protecting the kernel text: %luk\n",
  945. size >> 10);
  946. #ifdef CONFIG_CPA_DEBUG
  947. printk(KERN_INFO "Testing CPA: Reverting %lx-%lx\n",
  948. start, start+size);
  949. set_pages_rw(virt_to_page(start), size>>PAGE_SHIFT);
  950. printk(KERN_INFO "Testing CPA: write protecting again\n");
  951. set_pages_ro(virt_to_page(start), size>>PAGE_SHIFT);
  952. #endif
  953. #endif /* CONFIG_DYNAMIC_FTRACE */
  954. start += size;
  955. size = (unsigned long)__end_rodata - start;
  956. set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
  957. printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n",
  958. size >> 10);
  959. rodata_test();
  960. #ifdef CONFIG_CPA_DEBUG
  961. printk(KERN_INFO "Testing CPA: undo %lx-%lx\n", start, start + size);
  962. set_pages_rw(virt_to_page(start), size >> PAGE_SHIFT);
  963. printk(KERN_INFO "Testing CPA: write protecting again\n");
  964. set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
  965. #endif
  966. }
  967. #endif
  968. void free_init_pages(char *what, unsigned long begin, unsigned long end)
  969. {
  970. #ifdef CONFIG_DEBUG_PAGEALLOC
  971. /*
  972. * If debugging page accesses then do not free this memory but
  973. * mark them not present - any buggy init-section access will
  974. * create a kernel page fault:
  975. */
  976. printk(KERN_INFO "debug: unmapping init memory %08lx..%08lx\n",
  977. begin, PAGE_ALIGN(end));
  978. set_memory_np(begin, (end - begin) >> PAGE_SHIFT);
  979. #else
  980. unsigned long addr;
  981. /*
  982. * We just marked the kernel text read only above, now that
  983. * we are going to free part of that, we need to make that
  984. * writeable first.
  985. */
  986. set_memory_rw(begin, (end - begin) >> PAGE_SHIFT);
  987. for (addr = begin; addr < end; addr += PAGE_SIZE) {
  988. ClearPageReserved(virt_to_page(addr));
  989. init_page_count(virt_to_page(addr));
  990. memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE);
  991. free_page(addr);
  992. totalram_pages++;
  993. }
  994. printk(KERN_INFO "Freeing %s: %luk freed\n", what, (end - begin) >> 10);
  995. #endif
  996. }
  997. void free_initmem(void)
  998. {
  999. free_init_pages("unused kernel memory",
  1000. (unsigned long)(&__init_begin),
  1001. (unsigned long)(&__init_end));
  1002. }
  1003. #ifdef CONFIG_BLK_DEV_INITRD
  1004. void free_initrd_mem(unsigned long start, unsigned long end)
  1005. {
  1006. free_init_pages("initrd memory", start, end);
  1007. }
  1008. #endif
  1009. int __init reserve_bootmem_generic(unsigned long phys, unsigned long len,
  1010. int flags)
  1011. {
  1012. return reserve_bootmem(phys, len, flags);
  1013. }