init.c 19 KB

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