init_32.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270
  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/pci.h>
  24. #include <linux/pfn.h>
  25. #include <linux/poison.h>
  26. #include <linux/bootmem.h>
  27. #include <linux/slab.h>
  28. #include <linux/proc_fs.h>
  29. #include <linux/memory_hotplug.h>
  30. #include <linux/initrd.h>
  31. #include <linux/cpumask.h>
  32. #include <asm/asm.h>
  33. #include <asm/bios_ebda.h>
  34. #include <asm/processor.h>
  35. #include <asm/system.h>
  36. #include <asm/uaccess.h>
  37. #include <asm/pgtable.h>
  38. #include <asm/dma.h>
  39. #include <asm/fixmap.h>
  40. #include <asm/e820.h>
  41. #include <asm/apic.h>
  42. #include <asm/bugs.h>
  43. #include <asm/tlb.h>
  44. #include <asm/tlbflush.h>
  45. #include <asm/pgalloc.h>
  46. #include <asm/sections.h>
  47. #include <asm/paravirt.h>
  48. #include <asm/setup.h>
  49. #include <asm/cacheflush.h>
  50. unsigned long max_low_pfn_mapped;
  51. unsigned long max_pfn_mapped;
  52. DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
  53. unsigned long highstart_pfn, highend_pfn;
  54. static noinline int do_test_wp_bit(void);
  55. static unsigned long __initdata table_start;
  56. static unsigned long __meminitdata table_end;
  57. static unsigned long __meminitdata table_top;
  58. static int __initdata after_init_bootmem;
  59. static __init void *alloc_low_page(void)
  60. {
  61. unsigned long pfn = table_end++;
  62. void *adr;
  63. if (pfn >= table_top)
  64. panic("alloc_low_page: ran out of memory");
  65. adr = __va(pfn * PAGE_SIZE);
  66. memset(adr, 0, PAGE_SIZE);
  67. return adr;
  68. }
  69. /*
  70. * Creates a middle page table and puts a pointer to it in the
  71. * given global directory entry. This only returns the gd entry
  72. * in non-PAE compilation mode, since the middle layer is folded.
  73. */
  74. static pmd_t * __init one_md_table_init(pgd_t *pgd)
  75. {
  76. pud_t *pud;
  77. pmd_t *pmd_table;
  78. #ifdef CONFIG_X86_PAE
  79. if (!(pgd_val(*pgd) & _PAGE_PRESENT)) {
  80. if (after_init_bootmem)
  81. pmd_table = (pmd_t *)alloc_bootmem_low_pages(PAGE_SIZE);
  82. else
  83. pmd_table = (pmd_t *)alloc_low_page();
  84. paravirt_alloc_pmd(&init_mm, __pa(pmd_table) >> PAGE_SHIFT);
  85. set_pgd(pgd, __pgd(__pa(pmd_table) | _PAGE_PRESENT));
  86. pud = pud_offset(pgd, 0);
  87. BUG_ON(pmd_table != pmd_offset(pud, 0));
  88. return pmd_table;
  89. }
  90. #endif
  91. pud = pud_offset(pgd, 0);
  92. pmd_table = pmd_offset(pud, 0);
  93. return pmd_table;
  94. }
  95. /*
  96. * Create a page table and place a pointer to it in a middle page
  97. * directory entry:
  98. */
  99. static pte_t * __init one_page_table_init(pmd_t *pmd)
  100. {
  101. if (!(pmd_val(*pmd) & _PAGE_PRESENT)) {
  102. pte_t *page_table = NULL;
  103. if (after_init_bootmem) {
  104. #ifdef CONFIG_DEBUG_PAGEALLOC
  105. page_table = (pte_t *) alloc_bootmem_pages(PAGE_SIZE);
  106. #endif
  107. if (!page_table)
  108. page_table =
  109. (pte_t *)alloc_bootmem_low_pages(PAGE_SIZE);
  110. } else
  111. page_table = (pte_t *)alloc_low_page();
  112. paravirt_alloc_pte(&init_mm, __pa(page_table) >> PAGE_SHIFT);
  113. set_pmd(pmd, __pmd(__pa(page_table) | _PAGE_TABLE));
  114. BUG_ON(page_table != pte_offset_kernel(pmd, 0));
  115. }
  116. return pte_offset_kernel(pmd, 0);
  117. }
  118. static pte_t *__init page_table_kmap_check(pte_t *pte, pmd_t *pmd,
  119. unsigned long vaddr, pte_t *lastpte)
  120. {
  121. #ifdef CONFIG_HIGHMEM
  122. /*
  123. * Something (early fixmap) may already have put a pte
  124. * page here, which causes the page table allocation
  125. * to become nonlinear. Attempt to fix it, and if it
  126. * is still nonlinear then we have to bug.
  127. */
  128. int pmd_idx_kmap_begin = fix_to_virt(FIX_KMAP_END) >> PMD_SHIFT;
  129. int pmd_idx_kmap_end = fix_to_virt(FIX_KMAP_BEGIN) >> PMD_SHIFT;
  130. if (pmd_idx_kmap_begin != pmd_idx_kmap_end
  131. && (vaddr >> PMD_SHIFT) >= pmd_idx_kmap_begin
  132. && (vaddr >> PMD_SHIFT) <= pmd_idx_kmap_end
  133. && ((__pa(pte) >> PAGE_SHIFT) < table_start
  134. || (__pa(pte) >> PAGE_SHIFT) >= table_end)) {
  135. pte_t *newpte;
  136. int i;
  137. BUG_ON(after_init_bootmem);
  138. newpte = alloc_low_page();
  139. for (i = 0; i < PTRS_PER_PTE; i++)
  140. set_pte(newpte + i, pte[i]);
  141. paravirt_alloc_pte(&init_mm, __pa(newpte) >> PAGE_SHIFT);
  142. set_pmd(pmd, __pmd(__pa(newpte)|_PAGE_TABLE));
  143. BUG_ON(newpte != pte_offset_kernel(pmd, 0));
  144. __flush_tlb_all();
  145. paravirt_release_pte(__pa(pte) >> PAGE_SHIFT);
  146. pte = newpte;
  147. }
  148. BUG_ON(vaddr < fix_to_virt(FIX_KMAP_BEGIN - 1)
  149. && vaddr > fix_to_virt(FIX_KMAP_END)
  150. && lastpte && lastpte + PTRS_PER_PTE != pte);
  151. #endif
  152. return pte;
  153. }
  154. /*
  155. * This function initializes a certain range of kernel virtual memory
  156. * with new bootmem page tables, everywhere page tables are missing in
  157. * the given range.
  158. *
  159. * NOTE: The pagetables are allocated contiguous on the physical space
  160. * so we can cache the place of the first one and move around without
  161. * checking the pgd every time.
  162. */
  163. static void __init
  164. page_table_range_init(unsigned long start, unsigned long end, pgd_t *pgd_base)
  165. {
  166. int pgd_idx, pmd_idx;
  167. unsigned long vaddr;
  168. pgd_t *pgd;
  169. pmd_t *pmd;
  170. pte_t *pte = NULL;
  171. vaddr = start;
  172. pgd_idx = pgd_index(vaddr);
  173. pmd_idx = pmd_index(vaddr);
  174. pgd = pgd_base + pgd_idx;
  175. for ( ; (pgd_idx < PTRS_PER_PGD) && (vaddr != end); pgd++, pgd_idx++) {
  176. pmd = one_md_table_init(pgd);
  177. pmd = pmd + pmd_index(vaddr);
  178. for (; (pmd_idx < PTRS_PER_PMD) && (vaddr != end);
  179. pmd++, pmd_idx++) {
  180. pte = page_table_kmap_check(one_page_table_init(pmd),
  181. pmd, vaddr, pte);
  182. vaddr += PMD_SIZE;
  183. }
  184. pmd_idx = 0;
  185. }
  186. }
  187. static inline int is_kernel_text(unsigned long addr)
  188. {
  189. if (addr >= PAGE_OFFSET && addr <= (unsigned long)__init_end)
  190. return 1;
  191. return 0;
  192. }
  193. /*
  194. * This maps the physical memory to kernel virtual address space, a total
  195. * of max_low_pfn pages, by creating page tables starting from address
  196. * PAGE_OFFSET:
  197. */
  198. static void __init kernel_physical_mapping_init(pgd_t *pgd_base,
  199. unsigned long start_pfn,
  200. unsigned long end_pfn,
  201. int use_pse)
  202. {
  203. int pgd_idx, pmd_idx, pte_ofs;
  204. unsigned long pfn;
  205. pgd_t *pgd;
  206. pmd_t *pmd;
  207. pte_t *pte;
  208. unsigned pages_2m, pages_4k;
  209. int mapping_iter;
  210. /*
  211. * First iteration will setup identity mapping using large/small pages
  212. * based on use_pse, with other attributes same as set by
  213. * the early code in head_32.S
  214. *
  215. * Second iteration will setup the appropriate attributes (NX, GLOBAL..)
  216. * as desired for the kernel identity mapping.
  217. *
  218. * This two pass mechanism conforms to the TLB app note which says:
  219. *
  220. * "Software should not write to a paging-structure entry in a way
  221. * that would change, for any linear address, both the page size
  222. * and either the page frame or attributes."
  223. */
  224. mapping_iter = 1;
  225. if (!cpu_has_pse)
  226. use_pse = 0;
  227. repeat:
  228. pages_2m = pages_4k = 0;
  229. pfn = start_pfn;
  230. pgd_idx = pgd_index((pfn<<PAGE_SHIFT) + PAGE_OFFSET);
  231. pgd = pgd_base + pgd_idx;
  232. for (; pgd_idx < PTRS_PER_PGD; pgd++, pgd_idx++) {
  233. pmd = one_md_table_init(pgd);
  234. if (pfn >= end_pfn)
  235. continue;
  236. #ifdef CONFIG_X86_PAE
  237. pmd_idx = pmd_index((pfn<<PAGE_SHIFT) + PAGE_OFFSET);
  238. pmd += pmd_idx;
  239. #else
  240. pmd_idx = 0;
  241. #endif
  242. for (; pmd_idx < PTRS_PER_PMD && pfn < end_pfn;
  243. pmd++, pmd_idx++) {
  244. unsigned int addr = pfn * PAGE_SIZE + PAGE_OFFSET;
  245. /*
  246. * Map with big pages if possible, otherwise
  247. * create normal page tables:
  248. */
  249. if (use_pse) {
  250. unsigned int addr2;
  251. pgprot_t prot = PAGE_KERNEL_LARGE;
  252. /*
  253. * first pass will use the same initial
  254. * identity mapping attribute + _PAGE_PSE.
  255. */
  256. pgprot_t init_prot =
  257. __pgprot(PTE_IDENT_ATTR |
  258. _PAGE_PSE);
  259. addr2 = (pfn + PTRS_PER_PTE-1) * PAGE_SIZE +
  260. PAGE_OFFSET + PAGE_SIZE-1;
  261. if (is_kernel_text(addr) ||
  262. is_kernel_text(addr2))
  263. prot = PAGE_KERNEL_LARGE_EXEC;
  264. pages_2m++;
  265. if (mapping_iter == 1)
  266. set_pmd(pmd, pfn_pmd(pfn, init_prot));
  267. else
  268. set_pmd(pmd, pfn_pmd(pfn, prot));
  269. pfn += PTRS_PER_PTE;
  270. continue;
  271. }
  272. pte = one_page_table_init(pmd);
  273. pte_ofs = pte_index((pfn<<PAGE_SHIFT) + PAGE_OFFSET);
  274. pte += pte_ofs;
  275. for (; pte_ofs < PTRS_PER_PTE && pfn < end_pfn;
  276. pte++, pfn++, pte_ofs++, addr += PAGE_SIZE) {
  277. pgprot_t prot = PAGE_KERNEL;
  278. /*
  279. * first pass will use the same initial
  280. * identity mapping attribute.
  281. */
  282. pgprot_t init_prot = __pgprot(PTE_IDENT_ATTR);
  283. if (is_kernel_text(addr))
  284. prot = PAGE_KERNEL_EXEC;
  285. pages_4k++;
  286. if (mapping_iter == 1)
  287. set_pte(pte, pfn_pte(pfn, init_prot));
  288. else
  289. set_pte(pte, pfn_pte(pfn, prot));
  290. }
  291. }
  292. }
  293. if (mapping_iter == 1) {
  294. /*
  295. * update direct mapping page count only in the first
  296. * iteration.
  297. */
  298. update_page_count(PG_LEVEL_2M, pages_2m);
  299. update_page_count(PG_LEVEL_4K, pages_4k);
  300. /*
  301. * local global flush tlb, which will flush the previous
  302. * mappings present in both small and large page TLB's.
  303. */
  304. __flush_tlb_all();
  305. /*
  306. * Second iteration will set the actual desired PTE attributes.
  307. */
  308. mapping_iter = 2;
  309. goto repeat;
  310. }
  311. }
  312. /*
  313. * devmem_is_allowed() checks to see if /dev/mem access to a certain address
  314. * is valid. The argument is a physical page number.
  315. *
  316. *
  317. * On x86, access has to be given to the first megabyte of ram because that area
  318. * contains bios code and data regions used by X and dosemu and similar apps.
  319. * Access has to be given to non-kernel-ram areas as well, these contain the PCI
  320. * mmio resources as well as potential bios/acpi data regions.
  321. */
  322. int devmem_is_allowed(unsigned long pagenr)
  323. {
  324. if (pagenr <= 256)
  325. return 1;
  326. if (iomem_is_exclusive(pagenr << PAGE_SHIFT))
  327. return 0;
  328. if (!page_is_ram(pagenr))
  329. return 1;
  330. return 0;
  331. }
  332. pte_t *kmap_pte;
  333. pgprot_t kmap_prot;
  334. static inline pte_t *kmap_get_fixmap_pte(unsigned long vaddr)
  335. {
  336. return pte_offset_kernel(pmd_offset(pud_offset(pgd_offset_k(vaddr),
  337. vaddr), vaddr), vaddr);
  338. }
  339. static void __init kmap_init(void)
  340. {
  341. unsigned long kmap_vstart;
  342. /*
  343. * Cache the first kmap pte:
  344. */
  345. kmap_vstart = __fix_to_virt(FIX_KMAP_BEGIN);
  346. kmap_pte = kmap_get_fixmap_pte(kmap_vstart);
  347. kmap_prot = PAGE_KERNEL;
  348. }
  349. #ifdef CONFIG_HIGHMEM
  350. static void __init permanent_kmaps_init(pgd_t *pgd_base)
  351. {
  352. unsigned long vaddr;
  353. pgd_t *pgd;
  354. pud_t *pud;
  355. pmd_t *pmd;
  356. pte_t *pte;
  357. vaddr = PKMAP_BASE;
  358. page_table_range_init(vaddr, vaddr + PAGE_SIZE*LAST_PKMAP, pgd_base);
  359. pgd = swapper_pg_dir + pgd_index(vaddr);
  360. pud = pud_offset(pgd, vaddr);
  361. pmd = pmd_offset(pud, vaddr);
  362. pte = pte_offset_kernel(pmd, vaddr);
  363. pkmap_page_table = pte;
  364. }
  365. static void __init add_one_highpage_init(struct page *page, int pfn)
  366. {
  367. ClearPageReserved(page);
  368. init_page_count(page);
  369. __free_page(page);
  370. totalhigh_pages++;
  371. }
  372. struct add_highpages_data {
  373. unsigned long start_pfn;
  374. unsigned long end_pfn;
  375. };
  376. static int __init add_highpages_work_fn(unsigned long start_pfn,
  377. unsigned long end_pfn, void *datax)
  378. {
  379. int node_pfn;
  380. struct page *page;
  381. unsigned long final_start_pfn, final_end_pfn;
  382. struct add_highpages_data *data;
  383. data = (struct add_highpages_data *)datax;
  384. final_start_pfn = max(start_pfn, data->start_pfn);
  385. final_end_pfn = min(end_pfn, data->end_pfn);
  386. if (final_start_pfn >= final_end_pfn)
  387. return 0;
  388. for (node_pfn = final_start_pfn; node_pfn < final_end_pfn;
  389. node_pfn++) {
  390. if (!pfn_valid(node_pfn))
  391. continue;
  392. page = pfn_to_page(node_pfn);
  393. add_one_highpage_init(page, node_pfn);
  394. }
  395. return 0;
  396. }
  397. void __init add_highpages_with_active_regions(int nid, unsigned long start_pfn,
  398. unsigned long end_pfn)
  399. {
  400. struct add_highpages_data data;
  401. data.start_pfn = start_pfn;
  402. data.end_pfn = end_pfn;
  403. work_with_active_regions(nid, add_highpages_work_fn, &data);
  404. }
  405. #ifndef CONFIG_NUMA
  406. static void __init set_highmem_pages_init(void)
  407. {
  408. add_highpages_with_active_regions(0, highstart_pfn, highend_pfn);
  409. totalram_pages += totalhigh_pages;
  410. }
  411. #endif /* !CONFIG_NUMA */
  412. #else
  413. static inline void permanent_kmaps_init(pgd_t *pgd_base)
  414. {
  415. }
  416. static inline void set_highmem_pages_init(void)
  417. {
  418. }
  419. #endif /* CONFIG_HIGHMEM */
  420. void __init native_pagetable_setup_start(pgd_t *base)
  421. {
  422. unsigned long pfn, va;
  423. pgd_t *pgd;
  424. pud_t *pud;
  425. pmd_t *pmd;
  426. pte_t *pte;
  427. /*
  428. * Remove any mappings which extend past the end of physical
  429. * memory from the boot time page table:
  430. */
  431. for (pfn = max_low_pfn + 1; pfn < 1<<(32-PAGE_SHIFT); pfn++) {
  432. va = PAGE_OFFSET + (pfn<<PAGE_SHIFT);
  433. pgd = base + pgd_index(va);
  434. if (!pgd_present(*pgd))
  435. break;
  436. pud = pud_offset(pgd, va);
  437. pmd = pmd_offset(pud, va);
  438. if (!pmd_present(*pmd))
  439. break;
  440. pte = pte_offset_kernel(pmd, va);
  441. if (!pte_present(*pte))
  442. break;
  443. pte_clear(NULL, va, pte);
  444. }
  445. paravirt_alloc_pmd(&init_mm, __pa(base) >> PAGE_SHIFT);
  446. }
  447. void __init native_pagetable_setup_done(pgd_t *base)
  448. {
  449. }
  450. /*
  451. * Build a proper pagetable for the kernel mappings. Up until this
  452. * point, we've been running on some set of pagetables constructed by
  453. * the boot process.
  454. *
  455. * If we're booting on native hardware, this will be a pagetable
  456. * constructed in arch/x86/kernel/head_32.S. The root of the
  457. * pagetable will be swapper_pg_dir.
  458. *
  459. * If we're booting paravirtualized under a hypervisor, then there are
  460. * more options: we may already be running PAE, and the pagetable may
  461. * or may not be based in swapper_pg_dir. In any case,
  462. * paravirt_pagetable_setup_start() will set up swapper_pg_dir
  463. * appropriately for the rest of the initialization to work.
  464. *
  465. * In general, pagetable_init() assumes that the pagetable may already
  466. * be partially populated, and so it avoids stomping on any existing
  467. * mappings.
  468. */
  469. static void __init early_ioremap_page_table_range_init(pgd_t *pgd_base)
  470. {
  471. unsigned long vaddr, end;
  472. /*
  473. * Fixed mappings, only the page table structure has to be
  474. * created - mappings will be set by set_fixmap():
  475. */
  476. vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK;
  477. end = (FIXADDR_TOP + PMD_SIZE - 1) & PMD_MASK;
  478. page_table_range_init(vaddr, end, pgd_base);
  479. early_ioremap_reset();
  480. }
  481. static void __init pagetable_init(void)
  482. {
  483. pgd_t *pgd_base = swapper_pg_dir;
  484. permanent_kmaps_init(pgd_base);
  485. }
  486. #ifdef CONFIG_ACPI_SLEEP
  487. /*
  488. * ACPI suspend needs this for resume, because things like the intel-agp
  489. * driver might have split up a kernel 4MB mapping.
  490. */
  491. char swsusp_pg_dir[PAGE_SIZE]
  492. __attribute__ ((aligned(PAGE_SIZE)));
  493. static inline void save_pg_dir(void)
  494. {
  495. memcpy(swsusp_pg_dir, swapper_pg_dir, PAGE_SIZE);
  496. }
  497. #else /* !CONFIG_ACPI_SLEEP */
  498. static inline void save_pg_dir(void)
  499. {
  500. }
  501. #endif /* !CONFIG_ACPI_SLEEP */
  502. void zap_low_mappings(void)
  503. {
  504. int i;
  505. /*
  506. * Zap initial low-memory mappings.
  507. *
  508. * Note that "pgd_clear()" doesn't do it for
  509. * us, because pgd_clear() is a no-op on i386.
  510. */
  511. for (i = 0; i < KERNEL_PGD_BOUNDARY; i++) {
  512. #ifdef CONFIG_X86_PAE
  513. set_pgd(swapper_pg_dir+i, __pgd(1 + __pa(empty_zero_page)));
  514. #else
  515. set_pgd(swapper_pg_dir+i, __pgd(0));
  516. #endif
  517. }
  518. flush_tlb_all();
  519. }
  520. int nx_enabled;
  521. pteval_t __supported_pte_mask __read_mostly = ~(_PAGE_NX | _PAGE_GLOBAL | _PAGE_IOMAP);
  522. EXPORT_SYMBOL_GPL(__supported_pte_mask);
  523. #ifdef CONFIG_X86_PAE
  524. static int disable_nx __initdata;
  525. /*
  526. * noexec = on|off
  527. *
  528. * Control non executable mappings.
  529. *
  530. * on Enable
  531. * off Disable
  532. */
  533. static int __init noexec_setup(char *str)
  534. {
  535. if (!str || !strcmp(str, "on")) {
  536. if (cpu_has_nx) {
  537. __supported_pte_mask |= _PAGE_NX;
  538. disable_nx = 0;
  539. }
  540. } else {
  541. if (!strcmp(str, "off")) {
  542. disable_nx = 1;
  543. __supported_pte_mask &= ~_PAGE_NX;
  544. } else {
  545. return -EINVAL;
  546. }
  547. }
  548. return 0;
  549. }
  550. early_param("noexec", noexec_setup);
  551. static void __init set_nx(void)
  552. {
  553. unsigned int v[4], l, h;
  554. if (cpu_has_pae && (cpuid_eax(0x80000000) > 0x80000001)) {
  555. cpuid(0x80000001, &v[0], &v[1], &v[2], &v[3]);
  556. if ((v[3] & (1 << 20)) && !disable_nx) {
  557. rdmsr(MSR_EFER, l, h);
  558. l |= EFER_NX;
  559. wrmsr(MSR_EFER, l, h);
  560. nx_enabled = 1;
  561. __supported_pte_mask |= _PAGE_NX;
  562. }
  563. }
  564. }
  565. #endif
  566. /* user-defined highmem size */
  567. static unsigned int highmem_pages = -1;
  568. /*
  569. * highmem=size forces highmem to be exactly 'size' bytes.
  570. * This works even on boxes that have no highmem otherwise.
  571. * This also works to reduce highmem size on bigger boxes.
  572. */
  573. static int __init parse_highmem(char *arg)
  574. {
  575. if (!arg)
  576. return -EINVAL;
  577. highmem_pages = memparse(arg, &arg) >> PAGE_SHIFT;
  578. return 0;
  579. }
  580. early_param("highmem", parse_highmem);
  581. #define MSG_HIGHMEM_TOO_BIG \
  582. "highmem size (%luMB) is bigger than pages available (%luMB)!\n"
  583. #define MSG_LOWMEM_TOO_SMALL \
  584. "highmem size (%luMB) results in <64MB lowmem, ignoring it!\n"
  585. /*
  586. * All of RAM fits into lowmem - but if user wants highmem
  587. * artificially via the highmem=x boot parameter then create
  588. * it:
  589. */
  590. void __init lowmem_pfn_init(void)
  591. {
  592. /* max_low_pfn is 0, we already have early_res support */
  593. max_low_pfn = max_pfn;
  594. if (highmem_pages == -1)
  595. highmem_pages = 0;
  596. #ifdef CONFIG_HIGHMEM
  597. if (highmem_pages >= max_pfn) {
  598. printk(KERN_ERR MSG_HIGHMEM_TOO_BIG,
  599. pages_to_mb(highmem_pages), pages_to_mb(max_pfn));
  600. highmem_pages = 0;
  601. }
  602. if (highmem_pages) {
  603. if (max_low_pfn - highmem_pages < 64*1024*1024/PAGE_SIZE) {
  604. printk(KERN_ERR MSG_LOWMEM_TOO_SMALL,
  605. pages_to_mb(highmem_pages));
  606. highmem_pages = 0;
  607. }
  608. max_low_pfn -= highmem_pages;
  609. }
  610. #else
  611. if (highmem_pages)
  612. printk(KERN_ERR "ignoring highmem size on non-highmem kernel!\n");
  613. #endif
  614. }
  615. #define MSG_HIGHMEM_TOO_SMALL \
  616. "only %luMB highmem pages available, ignoring highmem size of %luMB!\n"
  617. #define MSG_HIGHMEM_TRIMMED \
  618. "Warning: only 4GB will be used. Use a HIGHMEM64G enabled kernel!\n"
  619. /*
  620. * We have more RAM than fits into lowmem - we try to put it into
  621. * highmem, also taking the highmem=x boot parameter into account:
  622. */
  623. void __init highmem_pfn_init(void)
  624. {
  625. max_low_pfn = MAXMEM_PFN;
  626. if (highmem_pages == -1)
  627. highmem_pages = max_pfn - MAXMEM_PFN;
  628. if (highmem_pages + MAXMEM_PFN < max_pfn)
  629. max_pfn = MAXMEM_PFN + highmem_pages;
  630. if (highmem_pages + MAXMEM_PFN > max_pfn) {
  631. printk(KERN_WARNING MSG_HIGHMEM_TOO_SMALL,
  632. pages_to_mb(max_pfn - MAXMEM_PFN),
  633. pages_to_mb(highmem_pages));
  634. highmem_pages = 0;
  635. }
  636. #ifndef CONFIG_HIGHMEM
  637. /* Maximum memory usable is what is directly addressable */
  638. printk(KERN_WARNING "Warning only %ldMB will be used.\n", MAXMEM>>20);
  639. if (max_pfn > MAX_NONPAE_PFN)
  640. printk(KERN_WARNING "Use a HIGHMEM64G enabled kernel.\n");
  641. else
  642. printk(KERN_WARNING "Use a HIGHMEM enabled kernel.\n");
  643. max_pfn = MAXMEM_PFN;
  644. #else /* !CONFIG_HIGHMEM */
  645. #ifndef CONFIG_HIGHMEM64G
  646. if (max_pfn > MAX_NONPAE_PFN) {
  647. max_pfn = MAX_NONPAE_PFN;
  648. printk(KERN_WARNING MSG_HIGHMEM_TRIMMED);
  649. }
  650. #endif /* !CONFIG_HIGHMEM64G */
  651. #endif /* !CONFIG_HIGHMEM */
  652. }
  653. /*
  654. * Determine low and high memory ranges:
  655. */
  656. void __init find_low_pfn_range(void)
  657. {
  658. /* it could update max_pfn */
  659. if (max_pfn <= MAXMEM_PFN)
  660. lowmem_pfn_init();
  661. else
  662. highmem_pfn_init();
  663. }
  664. #ifndef CONFIG_NEED_MULTIPLE_NODES
  665. void __init initmem_init(unsigned long start_pfn,
  666. unsigned long end_pfn)
  667. {
  668. #ifdef CONFIG_HIGHMEM
  669. highstart_pfn = highend_pfn = max_pfn;
  670. if (max_pfn > max_low_pfn)
  671. highstart_pfn = max_low_pfn;
  672. memory_present(0, 0, highend_pfn);
  673. e820_register_active_regions(0, 0, highend_pfn);
  674. printk(KERN_NOTICE "%ldMB HIGHMEM available.\n",
  675. pages_to_mb(highend_pfn - highstart_pfn));
  676. num_physpages = highend_pfn;
  677. high_memory = (void *) __va(highstart_pfn * PAGE_SIZE - 1) + 1;
  678. #else
  679. memory_present(0, 0, max_low_pfn);
  680. e820_register_active_regions(0, 0, max_low_pfn);
  681. num_physpages = max_low_pfn;
  682. high_memory = (void *) __va(max_low_pfn * PAGE_SIZE - 1) + 1;
  683. #endif
  684. #ifdef CONFIG_FLATMEM
  685. max_mapnr = num_physpages;
  686. #endif
  687. printk(KERN_NOTICE "%ldMB LOWMEM available.\n",
  688. pages_to_mb(max_low_pfn));
  689. setup_bootmem_allocator();
  690. }
  691. #endif /* !CONFIG_NEED_MULTIPLE_NODES */
  692. static void __init zone_sizes_init(void)
  693. {
  694. unsigned long max_zone_pfns[MAX_NR_ZONES];
  695. memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
  696. max_zone_pfns[ZONE_DMA] =
  697. virt_to_phys((char *)MAX_DMA_ADDRESS) >> PAGE_SHIFT;
  698. max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
  699. #ifdef CONFIG_HIGHMEM
  700. max_zone_pfns[ZONE_HIGHMEM] = highend_pfn;
  701. #endif
  702. free_area_init_nodes(max_zone_pfns);
  703. }
  704. void __init setup_bootmem_allocator(void)
  705. {
  706. int i;
  707. unsigned long bootmap_size, bootmap;
  708. /*
  709. * Initialize the boot-time allocator (with low memory only):
  710. */
  711. bootmap_size = bootmem_bootmap_pages(max_low_pfn)<<PAGE_SHIFT;
  712. bootmap = find_e820_area(min_low_pfn<<PAGE_SHIFT,
  713. max_pfn_mapped<<PAGE_SHIFT, bootmap_size,
  714. PAGE_SIZE);
  715. if (bootmap == -1L)
  716. panic("Cannot find bootmem map of size %ld\n", bootmap_size);
  717. reserve_early(bootmap, bootmap + bootmap_size, "BOOTMAP");
  718. /* don't touch min_low_pfn */
  719. bootmap_size = init_bootmem_node(NODE_DATA(0), bootmap >> PAGE_SHIFT,
  720. min_low_pfn, max_low_pfn);
  721. printk(KERN_INFO " mapped low ram: 0 - %08lx\n",
  722. max_pfn_mapped<<PAGE_SHIFT);
  723. printk(KERN_INFO " low ram: %08lx - %08lx\n",
  724. min_low_pfn<<PAGE_SHIFT, max_low_pfn<<PAGE_SHIFT);
  725. printk(KERN_INFO " bootmap %08lx - %08lx\n",
  726. bootmap, bootmap + bootmap_size);
  727. for_each_online_node(i)
  728. free_bootmem_with_active_regions(i, max_low_pfn);
  729. early_res_to_bootmem(0, max_low_pfn<<PAGE_SHIFT);
  730. after_init_bootmem = 1;
  731. }
  732. static void __init find_early_table_space(unsigned long end, int use_pse)
  733. {
  734. unsigned long puds, pmds, ptes, tables, start;
  735. puds = (end + PUD_SIZE - 1) >> PUD_SHIFT;
  736. tables = roundup(puds * sizeof(pud_t), PAGE_SIZE);
  737. pmds = (end + PMD_SIZE - 1) >> PMD_SHIFT;
  738. tables += roundup(pmds * sizeof(pmd_t), PAGE_SIZE);
  739. if (use_pse) {
  740. unsigned long extra;
  741. extra = end - ((end>>PMD_SHIFT) << PMD_SHIFT);
  742. extra += PMD_SIZE;
  743. ptes = (extra + PAGE_SIZE - 1) >> PAGE_SHIFT;
  744. } else
  745. ptes = (end + PAGE_SIZE - 1) >> PAGE_SHIFT;
  746. tables += roundup(ptes * sizeof(pte_t), PAGE_SIZE);
  747. /* for fixmap */
  748. tables += roundup(__end_of_fixed_addresses * sizeof(pte_t), PAGE_SIZE);
  749. /*
  750. * RED-PEN putting page tables only on node 0 could
  751. * cause a hotspot and fill up ZONE_DMA. The page tables
  752. * need roughly 0.5KB per GB.
  753. */
  754. start = 0x7000;
  755. table_start = find_e820_area(start, max_pfn_mapped<<PAGE_SHIFT,
  756. tables, PAGE_SIZE);
  757. if (table_start == -1UL)
  758. panic("Cannot find space for the kernel page tables");
  759. table_start >>= PAGE_SHIFT;
  760. table_end = table_start;
  761. table_top = table_start + (tables>>PAGE_SHIFT);
  762. printk(KERN_DEBUG "kernel direct mapping tables up to %lx @ %lx-%lx\n",
  763. end, table_start << PAGE_SHIFT,
  764. (table_start << PAGE_SHIFT) + tables);
  765. }
  766. unsigned long __init_refok init_memory_mapping(unsigned long start,
  767. unsigned long end)
  768. {
  769. pgd_t *pgd_base = swapper_pg_dir;
  770. unsigned long start_pfn, end_pfn;
  771. unsigned long big_page_start;
  772. #ifdef CONFIG_DEBUG_PAGEALLOC
  773. /*
  774. * For CONFIG_DEBUG_PAGEALLOC, identity mapping will use small pages.
  775. * This will simplify cpa(), which otherwise needs to support splitting
  776. * large pages into small in interrupt context, etc.
  777. */
  778. int use_pse = 0;
  779. #else
  780. int use_pse = cpu_has_pse;
  781. #endif
  782. /*
  783. * Find space for the kernel direct mapping tables.
  784. */
  785. if (!after_init_bootmem)
  786. find_early_table_space(end, use_pse);
  787. #ifdef CONFIG_X86_PAE
  788. set_nx();
  789. if (nx_enabled)
  790. printk(KERN_INFO "NX (Execute Disable) protection: active\n");
  791. #endif
  792. /* Enable PSE if available */
  793. if (cpu_has_pse)
  794. set_in_cr4(X86_CR4_PSE);
  795. /* Enable PGE if available */
  796. if (cpu_has_pge) {
  797. set_in_cr4(X86_CR4_PGE);
  798. __supported_pte_mask |= _PAGE_GLOBAL;
  799. }
  800. /*
  801. * Don't use a large page for the first 2/4MB of memory
  802. * because there are often fixed size MTRRs in there
  803. * and overlapping MTRRs into large pages can cause
  804. * slowdowns.
  805. */
  806. big_page_start = PMD_SIZE;
  807. if (start < big_page_start) {
  808. start_pfn = start >> PAGE_SHIFT;
  809. end_pfn = min(big_page_start>>PAGE_SHIFT, end>>PAGE_SHIFT);
  810. } else {
  811. /* head is not big page alignment ? */
  812. start_pfn = start >> PAGE_SHIFT;
  813. end_pfn = ((start + (PMD_SIZE - 1))>>PMD_SHIFT)
  814. << (PMD_SHIFT - PAGE_SHIFT);
  815. }
  816. if (start_pfn < end_pfn)
  817. kernel_physical_mapping_init(pgd_base, start_pfn, end_pfn, 0);
  818. /* big page range */
  819. start_pfn = ((start + (PMD_SIZE - 1))>>PMD_SHIFT)
  820. << (PMD_SHIFT - PAGE_SHIFT);
  821. if (start_pfn < (big_page_start >> PAGE_SHIFT))
  822. start_pfn = big_page_start >> PAGE_SHIFT;
  823. end_pfn = (end>>PMD_SHIFT) << (PMD_SHIFT - PAGE_SHIFT);
  824. if (start_pfn < end_pfn)
  825. kernel_physical_mapping_init(pgd_base, start_pfn, end_pfn,
  826. use_pse);
  827. /* tail is not big page alignment ? */
  828. start_pfn = end_pfn;
  829. if (start_pfn > (big_page_start>>PAGE_SHIFT)) {
  830. end_pfn = end >> PAGE_SHIFT;
  831. if (start_pfn < end_pfn)
  832. kernel_physical_mapping_init(pgd_base, start_pfn,
  833. end_pfn, 0);
  834. }
  835. early_ioremap_page_table_range_init(pgd_base);
  836. load_cr3(swapper_pg_dir);
  837. __flush_tlb_all();
  838. if (!after_init_bootmem)
  839. reserve_early(table_start << PAGE_SHIFT,
  840. table_end << PAGE_SHIFT, "PGTABLE");
  841. if (!after_init_bootmem)
  842. early_memtest(start, end);
  843. return end >> PAGE_SHIFT;
  844. }
  845. /*
  846. * paging_init() sets up the page tables - note that the first 8MB are
  847. * already mapped by head.S.
  848. *
  849. * This routines also unmaps the page at virtual kernel address 0, so
  850. * that we can trap those pesky NULL-reference errors in the kernel.
  851. */
  852. void __init paging_init(void)
  853. {
  854. pagetable_init();
  855. __flush_tlb_all();
  856. kmap_init();
  857. /*
  858. * NOTE: at this point the bootmem allocator is fully available.
  859. */
  860. sparse_init();
  861. zone_sizes_init();
  862. }
  863. /*
  864. * Test if the WP bit works in supervisor mode. It isn't supported on 386's
  865. * and also on some strange 486's. All 586+'s are OK. This used to involve
  866. * black magic jumps to work around some nasty CPU bugs, but fortunately the
  867. * switch to using exceptions got rid of all that.
  868. */
  869. static void __init test_wp_bit(void)
  870. {
  871. printk(KERN_INFO
  872. "Checking if this processor honours the WP bit even in supervisor mode...");
  873. /* Any page-aligned address will do, the test is non-destructive */
  874. __set_fixmap(FIX_WP_TEST, __pa(&swapper_pg_dir), PAGE_READONLY);
  875. boot_cpu_data.wp_works_ok = do_test_wp_bit();
  876. clear_fixmap(FIX_WP_TEST);
  877. if (!boot_cpu_data.wp_works_ok) {
  878. printk(KERN_CONT "No.\n");
  879. #ifdef CONFIG_X86_WP_WORKS_OK
  880. panic(
  881. "This kernel doesn't support CPU's with broken WP. Recompile it for a 386!");
  882. #endif
  883. } else {
  884. printk(KERN_CONT "Ok.\n");
  885. }
  886. }
  887. static struct kcore_list kcore_mem, kcore_vmalloc;
  888. void __init mem_init(void)
  889. {
  890. int codesize, reservedpages, datasize, initsize;
  891. int tmp;
  892. pci_iommu_alloc();
  893. #ifdef CONFIG_FLATMEM
  894. BUG_ON(!mem_map);
  895. #endif
  896. /* this will put all low memory onto the freelists */
  897. totalram_pages += free_all_bootmem();
  898. reservedpages = 0;
  899. for (tmp = 0; tmp < max_low_pfn; tmp++)
  900. /*
  901. * Only count reserved RAM pages:
  902. */
  903. if (page_is_ram(tmp) && PageReserved(pfn_to_page(tmp)))
  904. reservedpages++;
  905. set_highmem_pages_init();
  906. codesize = (unsigned long) &_etext - (unsigned long) &_text;
  907. datasize = (unsigned long) &_edata - (unsigned long) &_etext;
  908. initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
  909. kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT);
  910. kclist_add(&kcore_vmalloc, (void *)VMALLOC_START,
  911. VMALLOC_END-VMALLOC_START);
  912. printk(KERN_INFO "Memory: %luk/%luk available (%dk kernel code, "
  913. "%dk reserved, %dk data, %dk init, %ldk highmem)\n",
  914. (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
  915. num_physpages << (PAGE_SHIFT-10),
  916. codesize >> 10,
  917. reservedpages << (PAGE_SHIFT-10),
  918. datasize >> 10,
  919. initsize >> 10,
  920. (unsigned long) (totalhigh_pages << (PAGE_SHIFT-10))
  921. );
  922. printk(KERN_INFO "virtual kernel memory layout:\n"
  923. " fixmap : 0x%08lx - 0x%08lx (%4ld kB)\n"
  924. #ifdef CONFIG_HIGHMEM
  925. " pkmap : 0x%08lx - 0x%08lx (%4ld kB)\n"
  926. #endif
  927. " vmalloc : 0x%08lx - 0x%08lx (%4ld MB)\n"
  928. " lowmem : 0x%08lx - 0x%08lx (%4ld MB)\n"
  929. " .init : 0x%08lx - 0x%08lx (%4ld kB)\n"
  930. " .data : 0x%08lx - 0x%08lx (%4ld kB)\n"
  931. " .text : 0x%08lx - 0x%08lx (%4ld kB)\n",
  932. FIXADDR_START, FIXADDR_TOP,
  933. (FIXADDR_TOP - FIXADDR_START) >> 10,
  934. #ifdef CONFIG_HIGHMEM
  935. PKMAP_BASE, PKMAP_BASE+LAST_PKMAP*PAGE_SIZE,
  936. (LAST_PKMAP*PAGE_SIZE) >> 10,
  937. #endif
  938. VMALLOC_START, VMALLOC_END,
  939. (VMALLOC_END - VMALLOC_START) >> 20,
  940. (unsigned long)__va(0), (unsigned long)high_memory,
  941. ((unsigned long)high_memory - (unsigned long)__va(0)) >> 20,
  942. (unsigned long)&__init_begin, (unsigned long)&__init_end,
  943. ((unsigned long)&__init_end -
  944. (unsigned long)&__init_begin) >> 10,
  945. (unsigned long)&_etext, (unsigned long)&_edata,
  946. ((unsigned long)&_edata - (unsigned long)&_etext) >> 10,
  947. (unsigned long)&_text, (unsigned long)&_etext,
  948. ((unsigned long)&_etext - (unsigned long)&_text) >> 10);
  949. /*
  950. * Check boundaries twice: Some fundamental inconsistencies can
  951. * be detected at build time already.
  952. */
  953. #define __FIXADDR_TOP (-PAGE_SIZE)
  954. #ifdef CONFIG_HIGHMEM
  955. BUILD_BUG_ON(PKMAP_BASE + LAST_PKMAP*PAGE_SIZE > FIXADDR_START);
  956. BUILD_BUG_ON(VMALLOC_END > PKMAP_BASE);
  957. #endif
  958. #define high_memory (-128UL << 20)
  959. BUILD_BUG_ON(VMALLOC_START >= VMALLOC_END);
  960. #undef high_memory
  961. #undef __FIXADDR_TOP
  962. #ifdef CONFIG_HIGHMEM
  963. BUG_ON(PKMAP_BASE + LAST_PKMAP*PAGE_SIZE > FIXADDR_START);
  964. BUG_ON(VMALLOC_END > PKMAP_BASE);
  965. #endif
  966. BUG_ON(VMALLOC_START >= VMALLOC_END);
  967. BUG_ON((unsigned long)high_memory > VMALLOC_START);
  968. if (boot_cpu_data.wp_works_ok < 0)
  969. test_wp_bit();
  970. save_pg_dir();
  971. zap_low_mappings();
  972. }
  973. #ifdef CONFIG_MEMORY_HOTPLUG
  974. int arch_add_memory(int nid, u64 start, u64 size)
  975. {
  976. struct pglist_data *pgdata = NODE_DATA(nid);
  977. struct zone *zone = pgdata->node_zones + ZONE_HIGHMEM;
  978. unsigned long start_pfn = start >> PAGE_SHIFT;
  979. unsigned long nr_pages = size >> PAGE_SHIFT;
  980. return __add_pages(nid, zone, start_pfn, nr_pages);
  981. }
  982. #endif
  983. /*
  984. * This function cannot be __init, since exceptions don't work in that
  985. * section. Put this after the callers, so that it cannot be inlined.
  986. */
  987. static noinline int do_test_wp_bit(void)
  988. {
  989. char tmp_reg;
  990. int flag;
  991. __asm__ __volatile__(
  992. " movb %0, %1 \n"
  993. "1: movb %1, %0 \n"
  994. " xorl %2, %2 \n"
  995. "2: \n"
  996. _ASM_EXTABLE(1b,2b)
  997. :"=m" (*(char *)fix_to_virt(FIX_WP_TEST)),
  998. "=q" (tmp_reg),
  999. "=r" (flag)
  1000. :"2" (1)
  1001. :"memory");
  1002. return flag;
  1003. }
  1004. #ifdef CONFIG_DEBUG_RODATA
  1005. const int rodata_test_data = 0xC3;
  1006. EXPORT_SYMBOL_GPL(rodata_test_data);
  1007. void mark_rodata_ro(void)
  1008. {
  1009. unsigned long start = PFN_ALIGN(_text);
  1010. unsigned long size = PFN_ALIGN(_etext) - start;
  1011. #ifndef CONFIG_DYNAMIC_FTRACE
  1012. /* Dynamic tracing modifies the kernel text section */
  1013. set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
  1014. printk(KERN_INFO "Write protecting the kernel text: %luk\n",
  1015. size >> 10);
  1016. #ifdef CONFIG_CPA_DEBUG
  1017. printk(KERN_INFO "Testing CPA: Reverting %lx-%lx\n",
  1018. start, start+size);
  1019. set_pages_rw(virt_to_page(start), size>>PAGE_SHIFT);
  1020. printk(KERN_INFO "Testing CPA: write protecting again\n");
  1021. set_pages_ro(virt_to_page(start), size>>PAGE_SHIFT);
  1022. #endif
  1023. #endif /* CONFIG_DYNAMIC_FTRACE */
  1024. start += size;
  1025. size = (unsigned long)__end_rodata - start;
  1026. set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
  1027. printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n",
  1028. size >> 10);
  1029. rodata_test();
  1030. #ifdef CONFIG_CPA_DEBUG
  1031. printk(KERN_INFO "Testing CPA: undo %lx-%lx\n", start, start + size);
  1032. set_pages_rw(virt_to_page(start), size >> PAGE_SHIFT);
  1033. printk(KERN_INFO "Testing CPA: write protecting again\n");
  1034. set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
  1035. #endif
  1036. }
  1037. #endif
  1038. void free_init_pages(char *what, unsigned long begin, unsigned long end)
  1039. {
  1040. unsigned long addr = begin;
  1041. if (addr >= end)
  1042. return;
  1043. /*
  1044. * If debugging page accesses then do not free this memory but
  1045. * mark them not present - any buggy init-section access will
  1046. * create a kernel page fault:
  1047. */
  1048. #ifdef CONFIG_DEBUG_PAGEALLOC
  1049. printk(KERN_INFO "debug: unmapping init memory %08lx..%08lx\n",
  1050. begin, PAGE_ALIGN(end));
  1051. set_memory_np(begin, (end - begin) >> PAGE_SHIFT);
  1052. #else
  1053. /*
  1054. * We just marked the kernel text read only above, now that
  1055. * we are going to free part of that, we need to make that
  1056. * writeable first.
  1057. */
  1058. set_memory_rw(begin, (end - begin) >> PAGE_SHIFT);
  1059. printk(KERN_INFO "Freeing %s: %luk freed\n", what, (end - begin) >> 10);
  1060. for (; addr < end; addr += PAGE_SIZE) {
  1061. ClearPageReserved(virt_to_page(addr));
  1062. init_page_count(virt_to_page(addr));
  1063. memset((void *)(addr & ~(PAGE_SIZE-1)),
  1064. POISON_FREE_INITMEM, PAGE_SIZE);
  1065. free_page(addr);
  1066. totalram_pages++;
  1067. }
  1068. #endif
  1069. }
  1070. void free_initmem(void)
  1071. {
  1072. free_init_pages("unused kernel memory",
  1073. (unsigned long)(&__init_begin),
  1074. (unsigned long)(&__init_end));
  1075. }
  1076. #ifdef CONFIG_BLK_DEV_INITRD
  1077. void free_initrd_mem(unsigned long start, unsigned long end)
  1078. {
  1079. free_init_pages("initrd memory", start, end);
  1080. }
  1081. #endif
  1082. int __init reserve_bootmem_generic(unsigned long phys, unsigned long len,
  1083. int flags)
  1084. {
  1085. return reserve_bootmem(phys, len, flags);
  1086. }