init_32.c 20 KB

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