init_32.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  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/efi.h>
  30. #include <linux/memory_hotplug.h>
  31. #include <linux/initrd.h>
  32. #include <linux/cpumask.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/tlb.h>
  42. #include <asm/tlbflush.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(__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; pmd_idx < PTRS_PER_PMD && pfn < max_low_pfn; pmd++, pmd_idx++) {
  148. unsigned int address = pfn * PAGE_SIZE + PAGE_OFFSET;
  149. /* Map with big pages if possible, otherwise create normal page tables. */
  150. if (cpu_has_pse) {
  151. unsigned int address2 = (pfn + PTRS_PER_PTE - 1) * PAGE_SIZE + PAGE_OFFSET + PAGE_SIZE-1;
  152. if (is_kernel_text(address) || is_kernel_text(address2))
  153. set_pmd(pmd, pfn_pmd(pfn, PAGE_KERNEL_LARGE_EXEC));
  154. else
  155. set_pmd(pmd, pfn_pmd(pfn, PAGE_KERNEL_LARGE));
  156. pfn += PTRS_PER_PTE;
  157. } else {
  158. pte = one_page_table_init(pmd);
  159. for (pte_ofs = 0;
  160. pte_ofs < PTRS_PER_PTE && pfn < max_low_pfn;
  161. pte++, pfn++, pte_ofs++, address += PAGE_SIZE) {
  162. if (is_kernel_text(address))
  163. set_pte(pte, pfn_pte(pfn, PAGE_KERNEL_EXEC));
  164. else
  165. set_pte(pte, pfn_pte(pfn, PAGE_KERNEL));
  166. }
  167. }
  168. }
  169. }
  170. }
  171. static inline int page_kills_ppro(unsigned long pagenr)
  172. {
  173. if (pagenr >= 0x70000 && pagenr <= 0x7003F)
  174. return 1;
  175. return 0;
  176. }
  177. int page_is_ram(unsigned long pagenr)
  178. {
  179. int i;
  180. unsigned long addr, end;
  181. if (efi_enabled) {
  182. efi_memory_desc_t *md;
  183. void *p;
  184. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  185. md = p;
  186. if (!is_available_memory(md))
  187. continue;
  188. addr = (md->phys_addr+PAGE_SIZE-1) >> PAGE_SHIFT;
  189. end = (md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT)) >> PAGE_SHIFT;
  190. if ((pagenr >= addr) && (pagenr < end))
  191. return 1;
  192. }
  193. return 0;
  194. }
  195. for (i = 0; i < e820.nr_map; i++) {
  196. if (e820.map[i].type != E820_RAM) /* not usable memory */
  197. continue;
  198. /*
  199. * !!!FIXME!!! Some BIOSen report areas as RAM that
  200. * are not. Notably the 640->1Mb area. We need a sanity
  201. * check here.
  202. */
  203. addr = (e820.map[i].addr+PAGE_SIZE-1) >> PAGE_SHIFT;
  204. end = (e820.map[i].addr+e820.map[i].size) >> PAGE_SHIFT;
  205. if ((pagenr >= addr) && (pagenr < end))
  206. return 1;
  207. }
  208. return 0;
  209. }
  210. #ifdef CONFIG_HIGHMEM
  211. pte_t *kmap_pte;
  212. pgprot_t kmap_prot;
  213. #define kmap_get_fixmap_pte(vaddr) \
  214. pte_offset_kernel(pmd_offset(pud_offset(pgd_offset_k(vaddr), vaddr), (vaddr)), (vaddr))
  215. static void __init kmap_init(void)
  216. {
  217. unsigned long kmap_vstart;
  218. /* cache the first kmap pte */
  219. kmap_vstart = __fix_to_virt(FIX_KMAP_BEGIN);
  220. kmap_pte = kmap_get_fixmap_pte(kmap_vstart);
  221. kmap_prot = PAGE_KERNEL;
  222. }
  223. static void __init permanent_kmaps_init(pgd_t *pgd_base)
  224. {
  225. pgd_t *pgd;
  226. pud_t *pud;
  227. pmd_t *pmd;
  228. pte_t *pte;
  229. unsigned long vaddr;
  230. vaddr = PKMAP_BASE;
  231. page_table_range_init(vaddr, vaddr + PAGE_SIZE*LAST_PKMAP, pgd_base);
  232. pgd = swapper_pg_dir + pgd_index(vaddr);
  233. pud = pud_offset(pgd, vaddr);
  234. pmd = pmd_offset(pud, vaddr);
  235. pte = pte_offset_kernel(pmd, vaddr);
  236. pkmap_page_table = pte;
  237. }
  238. static void __meminit free_new_highpage(struct page *page)
  239. {
  240. init_page_count(page);
  241. __free_page(page);
  242. totalhigh_pages++;
  243. }
  244. void __init add_one_highpage_init(struct page *page, int pfn, int bad_ppro)
  245. {
  246. if (page_is_ram(pfn) && !(bad_ppro && page_kills_ppro(pfn))) {
  247. ClearPageReserved(page);
  248. free_new_highpage(page);
  249. } else
  250. SetPageReserved(page);
  251. }
  252. static int __meminit add_one_highpage_hotplug(struct page *page, unsigned long pfn)
  253. {
  254. free_new_highpage(page);
  255. totalram_pages++;
  256. #ifdef CONFIG_FLATMEM
  257. max_mapnr = max(pfn, max_mapnr);
  258. #endif
  259. num_physpages++;
  260. return 0;
  261. }
  262. /*
  263. * Not currently handling the NUMA case.
  264. * Assuming single node and all memory that
  265. * has been added dynamically that would be
  266. * onlined here is in HIGHMEM
  267. */
  268. void __meminit online_page(struct page *page)
  269. {
  270. ClearPageReserved(page);
  271. add_one_highpage_hotplug(page, page_to_pfn(page));
  272. }
  273. #ifdef CONFIG_NUMA
  274. extern void set_highmem_pages_init(int);
  275. #else
  276. static void __init set_highmem_pages_init(int bad_ppro)
  277. {
  278. int pfn;
  279. for (pfn = highstart_pfn; pfn < highend_pfn; pfn++) {
  280. /*
  281. * Holes under sparsemem might not have no mem_map[]:
  282. */
  283. if (pfn_valid(pfn))
  284. add_one_highpage_init(pfn_to_page(pfn), pfn, bad_ppro);
  285. }
  286. totalram_pages += totalhigh_pages;
  287. }
  288. #endif /* CONFIG_FLATMEM */
  289. #else
  290. #define kmap_init() do { } while (0)
  291. #define permanent_kmaps_init(pgd_base) do { } while (0)
  292. #define set_highmem_pages_init(bad_ppro) do { } while (0)
  293. #endif /* CONFIG_HIGHMEM */
  294. unsigned long long __PAGE_KERNEL = _PAGE_KERNEL;
  295. EXPORT_SYMBOL(__PAGE_KERNEL);
  296. unsigned long long __PAGE_KERNEL_EXEC = _PAGE_KERNEL_EXEC;
  297. #ifdef CONFIG_NUMA
  298. extern void __init remap_numa_kva(void);
  299. #else
  300. #define remap_numa_kva() do {} while (0)
  301. #endif
  302. void __init native_pagetable_setup_start(pgd_t *base)
  303. {
  304. #ifdef CONFIG_X86_PAE
  305. int i;
  306. /*
  307. * Init entries of the first-level page table to the
  308. * zero page, if they haven't already been set up.
  309. *
  310. * In a normal native boot, we'll be running on a
  311. * pagetable rooted in swapper_pg_dir, but not in PAE
  312. * mode, so this will end up clobbering the mappings
  313. * for the lower 24Mbytes of the address space,
  314. * without affecting the kernel address space.
  315. */
  316. for (i = 0; i < USER_PTRS_PER_PGD; i++)
  317. set_pgd(&base[i],
  318. __pgd(__pa(empty_zero_page) | _PAGE_PRESENT));
  319. /* Make sure kernel address space is empty so that a pagetable
  320. will be allocated for it. */
  321. memset(&base[USER_PTRS_PER_PGD], 0,
  322. KERNEL_PGD_PTRS * sizeof(pgd_t));
  323. #else
  324. paravirt_alloc_pd(__pa(swapper_pg_dir) >> PAGE_SHIFT);
  325. #endif
  326. }
  327. void __init native_pagetable_setup_done(pgd_t *base)
  328. {
  329. #ifdef CONFIG_X86_PAE
  330. /*
  331. * Add low memory identity-mappings - SMP needs it when
  332. * starting up on an AP from real-mode. In the non-PAE
  333. * case we already have these mappings through head.S.
  334. * All user-space mappings are explicitly cleared after
  335. * SMP startup.
  336. */
  337. set_pgd(&base[0], base[USER_PTRS_PER_PGD]);
  338. #endif
  339. }
  340. /*
  341. * Build a proper pagetable for the kernel mappings. Up until this
  342. * point, we've been running on some set of pagetables constructed by
  343. * the boot process.
  344. *
  345. * If we're booting on native hardware, this will be a pagetable
  346. * constructed in arch/i386/kernel/head.S, and not running in PAE mode
  347. * (even if we'll end up running in PAE). The root of the pagetable
  348. * will be swapper_pg_dir.
  349. *
  350. * If we're booting paravirtualized under a hypervisor, then there are
  351. * more options: we may already be running PAE, and the pagetable may
  352. * or may not be based in swapper_pg_dir. In any case,
  353. * paravirt_pagetable_setup_start() will set up swapper_pg_dir
  354. * appropriately for the rest of the initialization to work.
  355. *
  356. * In general, pagetable_init() assumes that the pagetable may already
  357. * be partially populated, and so it avoids stomping on any existing
  358. * mappings.
  359. */
  360. static void __init pagetable_init (void)
  361. {
  362. unsigned long vaddr, end;
  363. pgd_t *pgd_base = swapper_pg_dir;
  364. paravirt_pagetable_setup_start(pgd_base);
  365. /* Enable PSE if available */
  366. if (cpu_has_pse)
  367. set_in_cr4(X86_CR4_PSE);
  368. /* Enable PGE if available */
  369. if (cpu_has_pge) {
  370. set_in_cr4(X86_CR4_PGE);
  371. __PAGE_KERNEL |= _PAGE_GLOBAL;
  372. __PAGE_KERNEL_EXEC |= _PAGE_GLOBAL;
  373. }
  374. kernel_physical_mapping_init(pgd_base);
  375. remap_numa_kva();
  376. /*
  377. * Fixed mappings, only the page table structure has to be
  378. * created - mappings will be set by set_fixmap():
  379. */
  380. vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK;
  381. end = (FIXADDR_TOP + PMD_SIZE - 1) & PMD_MASK;
  382. page_table_range_init(vaddr, end, pgd_base);
  383. permanent_kmaps_init(pgd_base);
  384. paravirt_pagetable_setup_done(pgd_base);
  385. }
  386. #if defined(CONFIG_HIBERNATION) || defined(CONFIG_ACPI)
  387. /*
  388. * Swap suspend & friends need this for resume because things like the intel-agp
  389. * driver might have split up a kernel 4MB mapping.
  390. */
  391. char __nosavedata swsusp_pg_dir[PAGE_SIZE]
  392. __attribute__ ((aligned (PAGE_SIZE)));
  393. static inline void save_pg_dir(void)
  394. {
  395. memcpy(swsusp_pg_dir, swapper_pg_dir, PAGE_SIZE);
  396. }
  397. #else
  398. static inline void save_pg_dir(void)
  399. {
  400. }
  401. #endif
  402. void zap_low_mappings (void)
  403. {
  404. int i;
  405. save_pg_dir();
  406. /*
  407. * Zap initial low-memory mappings.
  408. *
  409. * Note that "pgd_clear()" doesn't do it for
  410. * us, because pgd_clear() is a no-op on i386.
  411. */
  412. for (i = 0; i < USER_PTRS_PER_PGD; i++)
  413. #ifdef CONFIG_X86_PAE
  414. set_pgd(swapper_pg_dir+i, __pgd(1 + __pa(empty_zero_page)));
  415. #else
  416. set_pgd(swapper_pg_dir+i, __pgd(0));
  417. #endif
  418. flush_tlb_all();
  419. }
  420. int nx_enabled = 0;
  421. #ifdef CONFIG_X86_PAE
  422. static int disable_nx __initdata = 0;
  423. u64 __supported_pte_mask __read_mostly = ~_PAGE_NX;
  424. EXPORT_SYMBOL_GPL(__supported_pte_mask);
  425. /*
  426. * noexec = on|off
  427. *
  428. * Control non executable mappings.
  429. *
  430. * on Enable
  431. * off Disable
  432. */
  433. static int __init noexec_setup(char *str)
  434. {
  435. if (!str || !strcmp(str, "on")) {
  436. if (cpu_has_nx) {
  437. __supported_pte_mask |= _PAGE_NX;
  438. disable_nx = 0;
  439. }
  440. } else if (!strcmp(str,"off")) {
  441. disable_nx = 1;
  442. __supported_pte_mask &= ~_PAGE_NX;
  443. } else
  444. return -EINVAL;
  445. return 0;
  446. }
  447. early_param("noexec", noexec_setup);
  448. static void __init set_nx(void)
  449. {
  450. unsigned int v[4], l, h;
  451. if (cpu_has_pae && (cpuid_eax(0x80000000) > 0x80000001)) {
  452. cpuid(0x80000001, &v[0], &v[1], &v[2], &v[3]);
  453. if ((v[3] & (1 << 20)) && !disable_nx) {
  454. rdmsr(MSR_EFER, l, h);
  455. l |= EFER_NX;
  456. wrmsr(MSR_EFER, l, h);
  457. nx_enabled = 1;
  458. __supported_pte_mask |= _PAGE_NX;
  459. }
  460. }
  461. }
  462. /*
  463. * Enables/disables executability of a given kernel page and
  464. * returns the previous setting.
  465. */
  466. int __init set_kernel_exec(unsigned long vaddr, int enable)
  467. {
  468. pte_t *pte;
  469. int ret = 1;
  470. if (!nx_enabled)
  471. goto out;
  472. pte = lookup_address(vaddr);
  473. BUG_ON(!pte);
  474. if (!pte_exec_kernel(*pte))
  475. ret = 0;
  476. if (enable)
  477. pte->pte_high &= ~(1 << (_PAGE_BIT_NX - 32));
  478. else
  479. pte->pte_high |= 1 << (_PAGE_BIT_NX - 32);
  480. pte_update_defer(&init_mm, vaddr, pte);
  481. __flush_tlb_all();
  482. out:
  483. return ret;
  484. }
  485. #endif
  486. /*
  487. * paging_init() sets up the page tables - note that the first 8MB are
  488. * already mapped by head.S.
  489. *
  490. * This routines also unmaps the page at virtual kernel address 0, so
  491. * that we can trap those pesky NULL-reference errors in the kernel.
  492. */
  493. void __init paging_init(void)
  494. {
  495. #ifdef CONFIG_X86_PAE
  496. set_nx();
  497. if (nx_enabled)
  498. printk("NX (Execute Disable) protection: active\n");
  499. #endif
  500. pagetable_init();
  501. load_cr3(swapper_pg_dir);
  502. #ifdef CONFIG_X86_PAE
  503. /*
  504. * We will bail out later - printk doesn't work right now so
  505. * the user would just see a hanging kernel.
  506. */
  507. if (cpu_has_pae)
  508. set_in_cr4(X86_CR4_PAE);
  509. #endif
  510. __flush_tlb_all();
  511. kmap_init();
  512. }
  513. /*
  514. * Test if the WP bit works in supervisor mode. It isn't supported on 386's
  515. * and also on some strange 486's (NexGen etc.). All 586+'s are OK. This
  516. * used to involve black magic jumps to work around some nasty CPU bugs,
  517. * but fortunately the switch to using exceptions got rid of all that.
  518. */
  519. static void __init test_wp_bit(void)
  520. {
  521. printk("Checking if this processor honours the WP bit even in supervisor mode... ");
  522. /* Any page-aligned address will do, the test is non-destructive */
  523. __set_fixmap(FIX_WP_TEST, __pa(&swapper_pg_dir), PAGE_READONLY);
  524. boot_cpu_data.wp_works_ok = do_test_wp_bit();
  525. clear_fixmap(FIX_WP_TEST);
  526. if (!boot_cpu_data.wp_works_ok) {
  527. printk("No.\n");
  528. #ifdef CONFIG_X86_WP_WORKS_OK
  529. panic("This kernel doesn't support CPU's with broken WP. Recompile it for a 386!");
  530. #endif
  531. } else {
  532. printk("Ok.\n");
  533. }
  534. }
  535. static struct kcore_list kcore_mem, kcore_vmalloc;
  536. void __init mem_init(void)
  537. {
  538. extern int ppro_with_ram_bug(void);
  539. int codesize, reservedpages, datasize, initsize;
  540. int tmp;
  541. int bad_ppro;
  542. #ifdef CONFIG_FLATMEM
  543. BUG_ON(!mem_map);
  544. #endif
  545. bad_ppro = ppro_with_ram_bug();
  546. #ifdef CONFIG_HIGHMEM
  547. /* check that fixmap and pkmap do not overlap */
  548. if (PKMAP_BASE+LAST_PKMAP*PAGE_SIZE >= FIXADDR_START) {
  549. printk(KERN_ERR "fixmap and kmap areas overlap - this will crash\n");
  550. printk(KERN_ERR "pkstart: %lxh pkend: %lxh fixstart %lxh\n",
  551. PKMAP_BASE, PKMAP_BASE+LAST_PKMAP*PAGE_SIZE, FIXADDR_START);
  552. BUG();
  553. }
  554. #endif
  555. /* this will put all low memory onto the freelists */
  556. totalram_pages += free_all_bootmem();
  557. reservedpages = 0;
  558. for (tmp = 0; tmp < max_low_pfn; tmp++)
  559. /*
  560. * Only count reserved RAM pages
  561. */
  562. if (page_is_ram(tmp) && PageReserved(pfn_to_page(tmp)))
  563. reservedpages++;
  564. set_highmem_pages_init(bad_ppro);
  565. codesize = (unsigned long) &_etext - (unsigned long) &_text;
  566. datasize = (unsigned long) &_edata - (unsigned long) &_etext;
  567. initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
  568. kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT);
  569. kclist_add(&kcore_vmalloc, (void *)VMALLOC_START,
  570. VMALLOC_END-VMALLOC_START);
  571. printk(KERN_INFO "Memory: %luk/%luk available (%dk kernel code, %dk reserved, %dk data, %dk init, %ldk highmem)\n",
  572. (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
  573. num_physpages << (PAGE_SHIFT-10),
  574. codesize >> 10,
  575. reservedpages << (PAGE_SHIFT-10),
  576. datasize >> 10,
  577. initsize >> 10,
  578. (unsigned long) (totalhigh_pages << (PAGE_SHIFT-10))
  579. );
  580. #if 1 /* double-sanity-check paranoia */
  581. printk("virtual kernel memory layout:\n"
  582. " fixmap : 0x%08lx - 0x%08lx (%4ld kB)\n"
  583. #ifdef CONFIG_HIGHMEM
  584. " pkmap : 0x%08lx - 0x%08lx (%4ld kB)\n"
  585. #endif
  586. " vmalloc : 0x%08lx - 0x%08lx (%4ld MB)\n"
  587. " lowmem : 0x%08lx - 0x%08lx (%4ld MB)\n"
  588. " .init : 0x%08lx - 0x%08lx (%4ld kB)\n"
  589. " .data : 0x%08lx - 0x%08lx (%4ld kB)\n"
  590. " .text : 0x%08lx - 0x%08lx (%4ld kB)\n",
  591. FIXADDR_START, FIXADDR_TOP,
  592. (FIXADDR_TOP - FIXADDR_START) >> 10,
  593. #ifdef CONFIG_HIGHMEM
  594. PKMAP_BASE, PKMAP_BASE+LAST_PKMAP*PAGE_SIZE,
  595. (LAST_PKMAP*PAGE_SIZE) >> 10,
  596. #endif
  597. VMALLOC_START, VMALLOC_END,
  598. (VMALLOC_END - VMALLOC_START) >> 20,
  599. (unsigned long)__va(0), (unsigned long)high_memory,
  600. ((unsigned long)high_memory - (unsigned long)__va(0)) >> 20,
  601. (unsigned long)&__init_begin, (unsigned long)&__init_end,
  602. ((unsigned long)&__init_end - (unsigned long)&__init_begin) >> 10,
  603. (unsigned long)&_etext, (unsigned long)&_edata,
  604. ((unsigned long)&_edata - (unsigned long)&_etext) >> 10,
  605. (unsigned long)&_text, (unsigned long)&_etext,
  606. ((unsigned long)&_etext - (unsigned long)&_text) >> 10);
  607. #ifdef CONFIG_HIGHMEM
  608. BUG_ON(PKMAP_BASE+LAST_PKMAP*PAGE_SIZE > FIXADDR_START);
  609. BUG_ON(VMALLOC_END > PKMAP_BASE);
  610. #endif
  611. BUG_ON(VMALLOC_START > VMALLOC_END);
  612. BUG_ON((unsigned long)high_memory > VMALLOC_START);
  613. #endif /* double-sanity-check paranoia */
  614. #ifdef CONFIG_X86_PAE
  615. if (!cpu_has_pae)
  616. panic("cannot execute a PAE-enabled kernel on a PAE-less CPU!");
  617. #endif
  618. if (boot_cpu_data.wp_works_ok < 0)
  619. test_wp_bit();
  620. /*
  621. * Subtle. SMP is doing it's boot stuff late (because it has to
  622. * fork idle threads) - but it also needs low mappings for the
  623. * protected-mode entry to work. We zap these entries only after
  624. * the WP-bit has been tested.
  625. */
  626. #ifndef CONFIG_SMP
  627. zap_low_mappings();
  628. #endif
  629. }
  630. #ifdef CONFIG_MEMORY_HOTPLUG
  631. int arch_add_memory(int nid, u64 start, u64 size)
  632. {
  633. struct pglist_data *pgdata = NODE_DATA(nid);
  634. struct zone *zone = pgdata->node_zones + ZONE_HIGHMEM;
  635. unsigned long start_pfn = start >> PAGE_SHIFT;
  636. unsigned long nr_pages = size >> PAGE_SHIFT;
  637. return __add_pages(zone, start_pfn, nr_pages);
  638. }
  639. #endif
  640. struct kmem_cache *pmd_cache;
  641. void __init pgtable_cache_init(void)
  642. {
  643. if (PTRS_PER_PMD > 1)
  644. pmd_cache = kmem_cache_create("pmd",
  645. PTRS_PER_PMD*sizeof(pmd_t),
  646. PTRS_PER_PMD*sizeof(pmd_t),
  647. SLAB_PANIC,
  648. pmd_ctor);
  649. }
  650. /*
  651. * This function cannot be __init, since exceptions don't work in that
  652. * section. Put this after the callers, so that it cannot be inlined.
  653. */
  654. static int noinline do_test_wp_bit(void)
  655. {
  656. char tmp_reg;
  657. int flag;
  658. __asm__ __volatile__(
  659. " movb %0,%1 \n"
  660. "1: movb %1,%0 \n"
  661. " xorl %2,%2 \n"
  662. "2: \n"
  663. ".section __ex_table,\"a\"\n"
  664. " .align 4 \n"
  665. " .long 1b,2b \n"
  666. ".previous \n"
  667. :"=m" (*(char *)fix_to_virt(FIX_WP_TEST)),
  668. "=q" (tmp_reg),
  669. "=r" (flag)
  670. :"2" (1)
  671. :"memory");
  672. return flag;
  673. }
  674. #ifdef CONFIG_DEBUG_RODATA
  675. void mark_rodata_ro(void)
  676. {
  677. unsigned long start = PFN_ALIGN(_text);
  678. unsigned long size = PFN_ALIGN(_etext) - start;
  679. #ifndef CONFIG_KPROBES
  680. #ifdef CONFIG_HOTPLUG_CPU
  681. /* It must still be possible to apply SMP alternatives. */
  682. if (num_possible_cpus() <= 1)
  683. #endif
  684. {
  685. change_page_attr(virt_to_page(start),
  686. size >> PAGE_SHIFT, PAGE_KERNEL_RX);
  687. printk("Write protecting the kernel text: %luk\n", size >> 10);
  688. }
  689. #endif
  690. start += size;
  691. size = (unsigned long)__end_rodata - start;
  692. change_page_attr(virt_to_page(start),
  693. size >> PAGE_SHIFT, PAGE_KERNEL_RO);
  694. printk("Write protecting the kernel read-only data: %luk\n",
  695. size >> 10);
  696. /*
  697. * change_page_attr() requires a global_flush_tlb() call after it.
  698. * We do this after the printk so that if something went wrong in the
  699. * change, the printk gets out at least to give a better debug hint
  700. * of who is the culprit.
  701. */
  702. global_flush_tlb();
  703. }
  704. #endif
  705. void free_init_pages(char *what, unsigned long begin, unsigned long end)
  706. {
  707. unsigned long addr;
  708. for (addr = begin; addr < end; addr += PAGE_SIZE) {
  709. ClearPageReserved(virt_to_page(addr));
  710. init_page_count(virt_to_page(addr));
  711. memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE);
  712. free_page(addr);
  713. totalram_pages++;
  714. }
  715. printk(KERN_INFO "Freeing %s: %luk freed\n", what, (end - begin) >> 10);
  716. }
  717. void free_initmem(void)
  718. {
  719. free_init_pages("unused kernel memory",
  720. (unsigned long)(&__init_begin),
  721. (unsigned long)(&__init_end));
  722. }
  723. #ifdef CONFIG_BLK_DEV_INITRD
  724. void free_initrd_mem(unsigned long start, unsigned long end)
  725. {
  726. free_init_pages("initrd memory", start, end);
  727. }
  728. #endif