init_32.c 20 KB

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