init_32.c 20 KB

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