init_32.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. /*
  2. * linux/arch/i386/mm/init.c
  3. *
  4. * Copyright (C) 1995 Linus Torvalds
  5. *
  6. * Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999
  7. */
  8. #include <linux/module.h>
  9. #include <linux/signal.h>
  10. #include <linux/sched.h>
  11. #include <linux/kernel.h>
  12. #include <linux/errno.h>
  13. #include <linux/string.h>
  14. #include <linux/types.h>
  15. #include <linux/ptrace.h>
  16. #include <linux/mman.h>
  17. #include <linux/mm.h>
  18. #include <linux/hugetlb.h>
  19. #include <linux/swap.h>
  20. #include <linux/smp.h>
  21. #include <linux/init.h>
  22. #include <linux/highmem.h>
  23. #include <linux/pagemap.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/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. unsigned int __VMALLOC_RESERVE = 128 << 20;
  50. unsigned long max_pfn_mapped;
  51. DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
  52. unsigned long highstart_pfn, highend_pfn;
  53. static noinline int do_test_wp_bit(void);
  54. /*
  55. * Creates a middle page table and puts a pointer to it in the
  56. * given global directory entry. This only returns the gd entry
  57. * in non-PAE compilation mode, since the middle layer is folded.
  58. */
  59. static pmd_t * __init one_md_table_init(pgd_t *pgd)
  60. {
  61. pud_t *pud;
  62. pmd_t *pmd_table;
  63. #ifdef CONFIG_X86_PAE
  64. if (!(pgd_val(*pgd) & _PAGE_PRESENT)) {
  65. pmd_table = (pmd_t *) alloc_bootmem_low_pages(PAGE_SIZE);
  66. paravirt_alloc_pd(&init_mm, __pa(pmd_table) >> PAGE_SHIFT);
  67. set_pgd(pgd, __pgd(__pa(pmd_table) | _PAGE_PRESENT));
  68. pud = pud_offset(pgd, 0);
  69. BUG_ON(pmd_table != pmd_offset(pud, 0));
  70. }
  71. #endif
  72. pud = pud_offset(pgd, 0);
  73. pmd_table = pmd_offset(pud, 0);
  74. return pmd_table;
  75. }
  76. /*
  77. * Create a page table and place a pointer to it in a middle page
  78. * directory entry:
  79. */
  80. static pte_t * __init one_page_table_init(pmd_t *pmd)
  81. {
  82. if (!(pmd_val(*pmd) & _PAGE_PRESENT)) {
  83. pte_t *page_table = NULL;
  84. #ifdef CONFIG_DEBUG_PAGEALLOC
  85. page_table = (pte_t *) alloc_bootmem_pages(PAGE_SIZE);
  86. #endif
  87. if (!page_table) {
  88. page_table =
  89. (pte_t *)alloc_bootmem_low_pages(PAGE_SIZE);
  90. }
  91. paravirt_alloc_pt(&init_mm, __pa(page_table) >> PAGE_SHIFT);
  92. set_pmd(pmd, __pmd(__pa(page_table) | _PAGE_TABLE));
  93. BUG_ON(page_table != pte_offset_kernel(pmd, 0));
  94. }
  95. return pte_offset_kernel(pmd, 0);
  96. }
  97. /*
  98. * This function initializes a certain range of kernel virtual memory
  99. * with new bootmem page tables, everywhere page tables are missing in
  100. * the given range.
  101. *
  102. * NOTE: The pagetables are allocated contiguous on the physical space
  103. * so we can cache the place of the first one and move around without
  104. * checking the pgd every time.
  105. */
  106. static void __init
  107. page_table_range_init(unsigned long start, unsigned long end, pgd_t *pgd_base)
  108. {
  109. int pgd_idx, pmd_idx;
  110. unsigned long vaddr;
  111. pgd_t *pgd;
  112. pmd_t *pmd;
  113. vaddr = start;
  114. pgd_idx = pgd_index(vaddr);
  115. pmd_idx = pmd_index(vaddr);
  116. pgd = pgd_base + pgd_idx;
  117. for ( ; (pgd_idx < PTRS_PER_PGD) && (vaddr != end); pgd++, pgd_idx++) {
  118. pmd = one_md_table_init(pgd);
  119. pmd = pmd + pmd_index(vaddr);
  120. for (; (pmd_idx < PTRS_PER_PMD) && (vaddr != end);
  121. pmd++, pmd_idx++) {
  122. one_page_table_init(pmd);
  123. vaddr += PMD_SIZE;
  124. }
  125. pmd_idx = 0;
  126. }
  127. }
  128. static inline int is_kernel_text(unsigned long addr)
  129. {
  130. if (addr >= PAGE_OFFSET && addr <= (unsigned long)__init_end)
  131. return 1;
  132. return 0;
  133. }
  134. /*
  135. * This maps the physical memory to kernel virtual address space, a total
  136. * of max_low_pfn pages, by creating page tables starting from address
  137. * PAGE_OFFSET:
  138. */
  139. static void __init kernel_physical_mapping_init(pgd_t *pgd_base)
  140. {
  141. int pgd_idx, pmd_idx, pte_ofs;
  142. unsigned long pfn;
  143. pgd_t *pgd;
  144. pmd_t *pmd;
  145. pte_t *pte;
  146. pgd_idx = pgd_index(PAGE_OFFSET);
  147. pgd = pgd_base + pgd_idx;
  148. pfn = 0;
  149. for (; pgd_idx < PTRS_PER_PGD; pgd++, pgd_idx++) {
  150. pmd = one_md_table_init(pgd);
  151. if (pfn >= max_low_pfn)
  152. continue;
  153. for (pmd_idx = 0;
  154. pmd_idx < PTRS_PER_PMD && pfn < max_low_pfn;
  155. pmd++, pmd_idx++) {
  156. unsigned int addr = pfn * PAGE_SIZE + PAGE_OFFSET;
  157. /*
  158. * Map with big pages if possible, otherwise
  159. * create normal page tables:
  160. */
  161. if (cpu_has_pse) {
  162. unsigned int addr2;
  163. pgprot_t prot = PAGE_KERNEL_LARGE;
  164. addr2 = (pfn + PTRS_PER_PTE-1) * PAGE_SIZE +
  165. PAGE_OFFSET + PAGE_SIZE-1;
  166. if (is_kernel_text(addr) ||
  167. is_kernel_text(addr2))
  168. prot = PAGE_KERNEL_LARGE_EXEC;
  169. set_pmd(pmd, pfn_pmd(pfn, prot));
  170. pfn += PTRS_PER_PTE;
  171. max_pfn_mapped = pfn;
  172. continue;
  173. }
  174. pte = one_page_table_init(pmd);
  175. for (pte_ofs = 0;
  176. pte_ofs < PTRS_PER_PTE && pfn < max_low_pfn;
  177. pte++, pfn++, pte_ofs++, addr += PAGE_SIZE) {
  178. pgprot_t prot = PAGE_KERNEL;
  179. if (is_kernel_text(addr))
  180. prot = PAGE_KERNEL_EXEC;
  181. set_pte(pte, pfn_pte(pfn, prot));
  182. }
  183. max_pfn_mapped = pfn;
  184. }
  185. }
  186. }
  187. static inline int page_kills_ppro(unsigned long pagenr)
  188. {
  189. if (pagenr >= 0x70000 && pagenr <= 0x7003F)
  190. return 1;
  191. return 0;
  192. }
  193. #ifdef CONFIG_HIGHMEM
  194. pte_t *kmap_pte;
  195. pgprot_t kmap_prot;
  196. static inline pte_t *kmap_get_fixmap_pte(unsigned long vaddr)
  197. {
  198. return pte_offset_kernel(pmd_offset(pud_offset(pgd_offset_k(vaddr),
  199. vaddr), vaddr), vaddr);
  200. }
  201. static void __init kmap_init(void)
  202. {
  203. unsigned long kmap_vstart;
  204. /*
  205. * Cache the first kmap pte:
  206. */
  207. kmap_vstart = __fix_to_virt(FIX_KMAP_BEGIN);
  208. kmap_pte = kmap_get_fixmap_pte(kmap_vstart);
  209. kmap_prot = PAGE_KERNEL;
  210. }
  211. static void __init permanent_kmaps_init(pgd_t *pgd_base)
  212. {
  213. unsigned long vaddr;
  214. pgd_t *pgd;
  215. pud_t *pud;
  216. pmd_t *pmd;
  217. pte_t *pte;
  218. vaddr = PKMAP_BASE;
  219. page_table_range_init(vaddr, vaddr + PAGE_SIZE*LAST_PKMAP, pgd_base);
  220. pgd = swapper_pg_dir + pgd_index(vaddr);
  221. pud = pud_offset(pgd, vaddr);
  222. pmd = pmd_offset(pud, vaddr);
  223. pte = pte_offset_kernel(pmd, vaddr);
  224. pkmap_page_table = pte;
  225. }
  226. static void __meminit free_new_highpage(struct page *page)
  227. {
  228. init_page_count(page);
  229. __free_page(page);
  230. totalhigh_pages++;
  231. }
  232. void __init add_one_highpage_init(struct page *page, int pfn, int bad_ppro)
  233. {
  234. if (page_is_ram(pfn) && !(bad_ppro && page_kills_ppro(pfn))) {
  235. ClearPageReserved(page);
  236. free_new_highpage(page);
  237. } else
  238. SetPageReserved(page);
  239. }
  240. static int __meminit
  241. add_one_highpage_hotplug(struct page *page, unsigned long pfn)
  242. {
  243. free_new_highpage(page);
  244. totalram_pages++;
  245. #ifdef CONFIG_FLATMEM
  246. max_mapnr = max(pfn, max_mapnr);
  247. #endif
  248. num_physpages++;
  249. return 0;
  250. }
  251. /*
  252. * Not currently handling the NUMA case.
  253. * Assuming single node and all memory that
  254. * has been added dynamically that would be
  255. * onlined here is in HIGHMEM.
  256. */
  257. void __meminit online_page(struct page *page)
  258. {
  259. ClearPageReserved(page);
  260. add_one_highpage_hotplug(page, page_to_pfn(page));
  261. }
  262. #ifndef CONFIG_NUMA
  263. static void __init set_highmem_pages_init(int bad_ppro)
  264. {
  265. int pfn;
  266. for (pfn = highstart_pfn; pfn < highend_pfn; pfn++) {
  267. /*
  268. * Holes under sparsemem might not have no mem_map[]:
  269. */
  270. if (pfn_valid(pfn))
  271. add_one_highpage_init(pfn_to_page(pfn), pfn, bad_ppro);
  272. }
  273. totalram_pages += totalhigh_pages;
  274. }
  275. #endif /* !CONFIG_NUMA */
  276. #else
  277. # define kmap_init() do { } while (0)
  278. # define permanent_kmaps_init(pgd_base) do { } while (0)
  279. # define set_highmem_pages_init(bad_ppro) do { } while (0)
  280. #endif /* CONFIG_HIGHMEM */
  281. pteval_t __PAGE_KERNEL = _PAGE_KERNEL;
  282. EXPORT_SYMBOL(__PAGE_KERNEL);
  283. pteval_t __PAGE_KERNEL_EXEC = _PAGE_KERNEL_EXEC;
  284. void __init native_pagetable_setup_start(pgd_t *base)
  285. {
  286. unsigned long pfn, va;
  287. pgd_t *pgd;
  288. pud_t *pud;
  289. pmd_t *pmd;
  290. pte_t *pte;
  291. /*
  292. * Remove any mappings which extend past the end of physical
  293. * memory from the boot time page table:
  294. */
  295. for (pfn = max_low_pfn + 1; pfn < 1<<(32-PAGE_SHIFT); pfn++) {
  296. va = PAGE_OFFSET + (pfn<<PAGE_SHIFT);
  297. pgd = base + pgd_index(va);
  298. if (!pgd_present(*pgd))
  299. break;
  300. pud = pud_offset(pgd, va);
  301. pmd = pmd_offset(pud, va);
  302. if (!pmd_present(*pmd))
  303. break;
  304. pte = pte_offset_kernel(pmd, va);
  305. if (!pte_present(*pte))
  306. break;
  307. pte_clear(NULL, va, pte);
  308. }
  309. paravirt_alloc_pd(&init_mm, __pa(base) >> PAGE_SHIFT);
  310. }
  311. void __init native_pagetable_setup_done(pgd_t *base)
  312. {
  313. }
  314. /*
  315. * Build a proper pagetable for the kernel mappings. Up until this
  316. * point, we've been running on some set of pagetables constructed by
  317. * the boot process.
  318. *
  319. * If we're booting on native hardware, this will be a pagetable
  320. * constructed in arch/x86/kernel/head_32.S. The root of the
  321. * pagetable will be swapper_pg_dir.
  322. *
  323. * If we're booting paravirtualized under a hypervisor, then there are
  324. * more options: we may already be running PAE, and the pagetable may
  325. * or may not be based in swapper_pg_dir. In any case,
  326. * paravirt_pagetable_setup_start() will set up swapper_pg_dir
  327. * appropriately for the rest of the initialization to work.
  328. *
  329. * In general, pagetable_init() assumes that the pagetable may already
  330. * be partially populated, and so it avoids stomping on any existing
  331. * mappings.
  332. */
  333. static void __init pagetable_init(void)
  334. {
  335. pgd_t *pgd_base = swapper_pg_dir;
  336. unsigned long vaddr, end;
  337. paravirt_pagetable_setup_start(pgd_base);
  338. /* Enable PSE if available */
  339. if (cpu_has_pse)
  340. set_in_cr4(X86_CR4_PSE);
  341. /* Enable PGE if available */
  342. if (cpu_has_pge) {
  343. set_in_cr4(X86_CR4_PGE);
  344. __PAGE_KERNEL |= _PAGE_GLOBAL;
  345. __PAGE_KERNEL_EXEC |= _PAGE_GLOBAL;
  346. }
  347. kernel_physical_mapping_init(pgd_base);
  348. remap_numa_kva();
  349. /*
  350. * Fixed mappings, only the page table structure has to be
  351. * created - mappings will be set by set_fixmap():
  352. */
  353. early_ioremap_clear();
  354. vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK;
  355. end = (FIXADDR_TOP + PMD_SIZE - 1) & PMD_MASK;
  356. page_table_range_init(vaddr, end, pgd_base);
  357. early_ioremap_reset();
  358. permanent_kmaps_init(pgd_base);
  359. paravirt_pagetable_setup_done(pgd_base);
  360. }
  361. #ifdef CONFIG_ACPI_SLEEP
  362. /*
  363. * ACPI suspend needs this for resume, because things like the intel-agp
  364. * driver might have split up a kernel 4MB mapping.
  365. */
  366. char swsusp_pg_dir[PAGE_SIZE]
  367. __attribute__ ((aligned(PAGE_SIZE)));
  368. static inline void save_pg_dir(void)
  369. {
  370. memcpy(swsusp_pg_dir, swapper_pg_dir, PAGE_SIZE);
  371. }
  372. #else /* !CONFIG_ACPI_SLEEP */
  373. static inline void save_pg_dir(void)
  374. {
  375. }
  376. #endif /* !CONFIG_ACPI_SLEEP */
  377. void zap_low_mappings(void)
  378. {
  379. int i;
  380. save_pg_dir();
  381. /*
  382. * Zap initial low-memory mappings.
  383. *
  384. * Note that "pgd_clear()" doesn't do it for
  385. * us, because pgd_clear() is a no-op on i386.
  386. */
  387. for (i = 0; i < USER_PTRS_PER_PGD; i++) {
  388. #ifdef CONFIG_X86_PAE
  389. set_pgd(swapper_pg_dir+i, __pgd(1 + __pa(empty_zero_page)));
  390. #else
  391. set_pgd(swapper_pg_dir+i, __pgd(0));
  392. #endif
  393. }
  394. flush_tlb_all();
  395. }
  396. int nx_enabled;
  397. pteval_t __supported_pte_mask __read_mostly = ~_PAGE_NX;
  398. EXPORT_SYMBOL_GPL(__supported_pte_mask);
  399. #ifdef CONFIG_X86_PAE
  400. static int disable_nx __initdata;
  401. /*
  402. * noexec = on|off
  403. *
  404. * Control non executable mappings.
  405. *
  406. * on Enable
  407. * off Disable
  408. */
  409. static int __init noexec_setup(char *str)
  410. {
  411. if (!str || !strcmp(str, "on")) {
  412. if (cpu_has_nx) {
  413. __supported_pte_mask |= _PAGE_NX;
  414. disable_nx = 0;
  415. }
  416. } else {
  417. if (!strcmp(str, "off")) {
  418. disable_nx = 1;
  419. __supported_pte_mask &= ~_PAGE_NX;
  420. } else {
  421. return -EINVAL;
  422. }
  423. }
  424. return 0;
  425. }
  426. early_param("noexec", noexec_setup);
  427. static void __init set_nx(void)
  428. {
  429. unsigned int v[4], l, h;
  430. if (cpu_has_pae && (cpuid_eax(0x80000000) > 0x80000001)) {
  431. cpuid(0x80000001, &v[0], &v[1], &v[2], &v[3]);
  432. if ((v[3] & (1 << 20)) && !disable_nx) {
  433. rdmsr(MSR_EFER, l, h);
  434. l |= EFER_NX;
  435. wrmsr(MSR_EFER, l, h);
  436. nx_enabled = 1;
  437. __supported_pte_mask |= _PAGE_NX;
  438. }
  439. }
  440. }
  441. #endif
  442. /*
  443. * paging_init() sets up the page tables - note that the first 8MB are
  444. * already mapped by head.S.
  445. *
  446. * This routines also unmaps the page at virtual kernel address 0, so
  447. * that we can trap those pesky NULL-reference errors in the kernel.
  448. */
  449. void __init paging_init(void)
  450. {
  451. #ifdef CONFIG_X86_PAE
  452. set_nx();
  453. if (nx_enabled)
  454. printk(KERN_INFO "NX (Execute Disable) protection: active\n");
  455. #endif
  456. pagetable_init();
  457. load_cr3(swapper_pg_dir);
  458. __flush_tlb_all();
  459. kmap_init();
  460. }
  461. /*
  462. * Test if the WP bit works in supervisor mode. It isn't supported on 386's
  463. * and also on some strange 486's (NexGen etc.). All 586+'s are OK. This
  464. * used to involve black magic jumps to work around some nasty CPU bugs,
  465. * but fortunately the switch to using exceptions got rid of all that.
  466. */
  467. static void __init test_wp_bit(void)
  468. {
  469. printk(KERN_INFO
  470. "Checking if this processor honours the WP bit even in supervisor mode...");
  471. /* Any page-aligned address will do, the test is non-destructive */
  472. __set_fixmap(FIX_WP_TEST, __pa(&swapper_pg_dir), PAGE_READONLY);
  473. boot_cpu_data.wp_works_ok = do_test_wp_bit();
  474. clear_fixmap(FIX_WP_TEST);
  475. if (!boot_cpu_data.wp_works_ok) {
  476. printk(KERN_CONT "No.\n");
  477. #ifdef CONFIG_X86_WP_WORKS_OK
  478. panic(
  479. "This kernel doesn't support CPU's with broken WP. Recompile it for a 386!");
  480. #endif
  481. } else {
  482. printk(KERN_CONT "Ok.\n");
  483. }
  484. }
  485. static struct kcore_list kcore_mem, kcore_vmalloc;
  486. void __init mem_init(void)
  487. {
  488. int codesize, reservedpages, datasize, initsize;
  489. int tmp, bad_ppro;
  490. #ifdef CONFIG_FLATMEM
  491. BUG_ON(!mem_map);
  492. #endif
  493. bad_ppro = ppro_with_ram_bug();
  494. #ifdef CONFIG_HIGHMEM
  495. /* check that fixmap and pkmap do not overlap */
  496. if (PKMAP_BASE + LAST_PKMAP*PAGE_SIZE >= FIXADDR_START) {
  497. printk(KERN_ERR
  498. "fixmap and kmap areas overlap - this will crash\n");
  499. printk(KERN_ERR "pkstart: %lxh pkend: %lxh fixstart %lxh\n",
  500. PKMAP_BASE, PKMAP_BASE + LAST_PKMAP*PAGE_SIZE,
  501. FIXADDR_START);
  502. BUG();
  503. }
  504. #endif
  505. /* this will put all low memory onto the freelists */
  506. totalram_pages += free_all_bootmem();
  507. reservedpages = 0;
  508. for (tmp = 0; tmp < max_low_pfn; tmp++)
  509. /*
  510. * Only count reserved RAM pages:
  511. */
  512. if (page_is_ram(tmp) && PageReserved(pfn_to_page(tmp)))
  513. reservedpages++;
  514. set_highmem_pages_init(bad_ppro);
  515. codesize = (unsigned long) &_etext - (unsigned long) &_text;
  516. datasize = (unsigned long) &_edata - (unsigned long) &_etext;
  517. initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
  518. kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT);
  519. kclist_add(&kcore_vmalloc, (void *)VMALLOC_START,
  520. VMALLOC_END-VMALLOC_START);
  521. printk(KERN_INFO "Memory: %luk/%luk available (%dk kernel code, "
  522. "%dk reserved, %dk data, %dk init, %ldk highmem)\n",
  523. (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
  524. num_physpages << (PAGE_SHIFT-10),
  525. codesize >> 10,
  526. reservedpages << (PAGE_SHIFT-10),
  527. datasize >> 10,
  528. initsize >> 10,
  529. (unsigned long) (totalhigh_pages << (PAGE_SHIFT-10))
  530. );
  531. #if 1 /* double-sanity-check paranoia */
  532. printk(KERN_INFO "virtual kernel memory layout:\n"
  533. " fixmap : 0x%08lx - 0x%08lx (%4ld kB)\n"
  534. #ifdef CONFIG_HIGHMEM
  535. " pkmap : 0x%08lx - 0x%08lx (%4ld kB)\n"
  536. #endif
  537. " vmalloc : 0x%08lx - 0x%08lx (%4ld MB)\n"
  538. " lowmem : 0x%08lx - 0x%08lx (%4ld MB)\n"
  539. " .init : 0x%08lx - 0x%08lx (%4ld kB)\n"
  540. " .data : 0x%08lx - 0x%08lx (%4ld kB)\n"
  541. " .text : 0x%08lx - 0x%08lx (%4ld kB)\n",
  542. FIXADDR_START, FIXADDR_TOP,
  543. (FIXADDR_TOP - FIXADDR_START) >> 10,
  544. #ifdef CONFIG_HIGHMEM
  545. PKMAP_BASE, PKMAP_BASE+LAST_PKMAP*PAGE_SIZE,
  546. (LAST_PKMAP*PAGE_SIZE) >> 10,
  547. #endif
  548. VMALLOC_START, VMALLOC_END,
  549. (VMALLOC_END - VMALLOC_START) >> 20,
  550. (unsigned long)__va(0), (unsigned long)high_memory,
  551. ((unsigned long)high_memory - (unsigned long)__va(0)) >> 20,
  552. (unsigned long)&__init_begin, (unsigned long)&__init_end,
  553. ((unsigned long)&__init_end -
  554. (unsigned long)&__init_begin) >> 10,
  555. (unsigned long)&_etext, (unsigned long)&_edata,
  556. ((unsigned long)&_edata - (unsigned long)&_etext) >> 10,
  557. (unsigned long)&_text, (unsigned long)&_etext,
  558. ((unsigned long)&_etext - (unsigned long)&_text) >> 10);
  559. #ifdef CONFIG_HIGHMEM
  560. BUG_ON(PKMAP_BASE + LAST_PKMAP*PAGE_SIZE > FIXADDR_START);
  561. BUG_ON(VMALLOC_END > PKMAP_BASE);
  562. #endif
  563. BUG_ON(VMALLOC_START > VMALLOC_END);
  564. BUG_ON((unsigned long)high_memory > VMALLOC_START);
  565. #endif /* double-sanity-check paranoia */
  566. if (boot_cpu_data.wp_works_ok < 0)
  567. test_wp_bit();
  568. cpa_init();
  569. /*
  570. * Subtle. SMP is doing it's boot stuff late (because it has to
  571. * fork idle threads) - but it also needs low mappings for the
  572. * protected-mode entry to work. We zap these entries only after
  573. * the WP-bit has been tested.
  574. */
  575. #ifndef CONFIG_SMP
  576. zap_low_mappings();
  577. #endif
  578. }
  579. #ifdef CONFIG_MEMORY_HOTPLUG
  580. int arch_add_memory(int nid, u64 start, u64 size)
  581. {
  582. struct pglist_data *pgdata = NODE_DATA(nid);
  583. struct zone *zone = pgdata->node_zones + ZONE_HIGHMEM;
  584. unsigned long start_pfn = start >> PAGE_SHIFT;
  585. unsigned long nr_pages = size >> PAGE_SHIFT;
  586. return __add_pages(zone, start_pfn, nr_pages);
  587. }
  588. #endif
  589. /*
  590. * This function cannot be __init, since exceptions don't work in that
  591. * section. Put this after the callers, so that it cannot be inlined.
  592. */
  593. static noinline int do_test_wp_bit(void)
  594. {
  595. char tmp_reg;
  596. int flag;
  597. __asm__ __volatile__(
  598. " movb %0, %1 \n"
  599. "1: movb %1, %0 \n"
  600. " xorl %2, %2 \n"
  601. "2: \n"
  602. _ASM_EXTABLE(1b,2b)
  603. :"=m" (*(char *)fix_to_virt(FIX_WP_TEST)),
  604. "=q" (tmp_reg),
  605. "=r" (flag)
  606. :"2" (1)
  607. :"memory");
  608. return flag;
  609. }
  610. #ifdef CONFIG_DEBUG_RODATA
  611. const int rodata_test_data = 0xC3;
  612. EXPORT_SYMBOL_GPL(rodata_test_data);
  613. void mark_rodata_ro(void)
  614. {
  615. unsigned long start = PFN_ALIGN(_text);
  616. unsigned long size = PFN_ALIGN(_etext) - start;
  617. set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
  618. printk(KERN_INFO "Write protecting the kernel text: %luk\n",
  619. size >> 10);
  620. #ifdef CONFIG_CPA_DEBUG
  621. printk(KERN_INFO "Testing CPA: Reverting %lx-%lx\n",
  622. start, start+size);
  623. set_pages_rw(virt_to_page(start), size>>PAGE_SHIFT);
  624. printk(KERN_INFO "Testing CPA: write protecting again\n");
  625. set_pages_ro(virt_to_page(start), size>>PAGE_SHIFT);
  626. #endif
  627. start += size;
  628. size = (unsigned long)__end_rodata - start;
  629. set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
  630. printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n",
  631. size >> 10);
  632. rodata_test();
  633. #ifdef CONFIG_CPA_DEBUG
  634. printk(KERN_INFO "Testing CPA: undo %lx-%lx\n", start, start + size);
  635. set_pages_rw(virt_to_page(start), size >> PAGE_SHIFT);
  636. printk(KERN_INFO "Testing CPA: write protecting again\n");
  637. set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
  638. #endif
  639. }
  640. #endif
  641. void free_init_pages(char *what, unsigned long begin, unsigned long end)
  642. {
  643. #ifdef CONFIG_DEBUG_PAGEALLOC
  644. /*
  645. * If debugging page accesses then do not free this memory but
  646. * mark them not present - any buggy init-section access will
  647. * create a kernel page fault:
  648. */
  649. printk(KERN_INFO "debug: unmapping init memory %08lx..%08lx\n",
  650. begin, PAGE_ALIGN(end));
  651. set_memory_np(begin, (end - begin) >> PAGE_SHIFT);
  652. #else
  653. unsigned long addr;
  654. /*
  655. * We just marked the kernel text read only above, now that
  656. * we are going to free part of that, we need to make that
  657. * writeable first.
  658. */
  659. set_memory_rw(begin, (end - begin) >> PAGE_SHIFT);
  660. for (addr = begin; addr < end; addr += PAGE_SIZE) {
  661. ClearPageReserved(virt_to_page(addr));
  662. init_page_count(virt_to_page(addr));
  663. memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE);
  664. free_page(addr);
  665. totalram_pages++;
  666. }
  667. printk(KERN_INFO "Freeing %s: %luk freed\n", what, (end - begin) >> 10);
  668. #endif
  669. }
  670. void free_initmem(void)
  671. {
  672. free_init_pages("unused kernel memory",
  673. (unsigned long)(&__init_begin),
  674. (unsigned long)(&__init_end));
  675. }
  676. #ifdef CONFIG_BLK_DEV_INITRD
  677. void free_initrd_mem(unsigned long start, unsigned long end)
  678. {
  679. free_init_pages("initrd memory", start, end);
  680. }
  681. #endif