init_64.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. /*
  2. * linux/arch/x86_64/mm/init.c
  3. *
  4. * Copyright (C) 1995 Linus Torvalds
  5. * Copyright (C) 2000 Pavel Machek <pavel@suse.cz>
  6. * Copyright (C) 2002,2003 Andi Kleen <ak@suse.de>
  7. */
  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/swap.h>
  18. #include <linux/smp.h>
  19. #include <linux/init.h>
  20. #include <linux/pagemap.h>
  21. #include <linux/bootmem.h>
  22. #include <linux/proc_fs.h>
  23. #include <linux/pci.h>
  24. #include <linux/pfn.h>
  25. #include <linux/poison.h>
  26. #include <linux/dma-mapping.h>
  27. #include <linux/module.h>
  28. #include <linux/memory_hotplug.h>
  29. #include <linux/nmi.h>
  30. #include <asm/processor.h>
  31. #include <asm/system.h>
  32. #include <asm/uaccess.h>
  33. #include <asm/pgtable.h>
  34. #include <asm/pgalloc.h>
  35. #include <asm/dma.h>
  36. #include <asm/fixmap.h>
  37. #include <asm/e820.h>
  38. #include <asm/apic.h>
  39. #include <asm/tlb.h>
  40. #include <asm/mmu_context.h>
  41. #include <asm/proto.h>
  42. #include <asm/smp.h>
  43. #include <asm/sections.h>
  44. #include <asm/kdebug.h>
  45. #include <asm/numa.h>
  46. #ifndef Dprintk
  47. #define Dprintk(x...)
  48. #endif
  49. const struct dma_mapping_ops* dma_ops;
  50. EXPORT_SYMBOL(dma_ops);
  51. static unsigned long dma_reserve __initdata;
  52. DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
  53. /*
  54. * NOTE: pagetable_init alloc all the fixmap pagetables contiguous on the
  55. * physical space so we can cache the place of the first one and move
  56. * around without checking the pgd every time.
  57. */
  58. void show_mem(void)
  59. {
  60. long i, total = 0, reserved = 0;
  61. long shared = 0, cached = 0;
  62. pg_data_t *pgdat;
  63. struct page *page;
  64. printk(KERN_INFO "Mem-info:\n");
  65. show_free_areas();
  66. printk(KERN_INFO "Free swap: %6ldkB\n", nr_swap_pages<<(PAGE_SHIFT-10));
  67. for_each_online_pgdat(pgdat) {
  68. for (i = 0; i < pgdat->node_spanned_pages; ++i) {
  69. /* this loop can take a while with 256 GB and 4k pages
  70. so update the NMI watchdog */
  71. if (unlikely(i % MAX_ORDER_NR_PAGES == 0)) {
  72. touch_nmi_watchdog();
  73. }
  74. if (!pfn_valid(pgdat->node_start_pfn + i))
  75. continue;
  76. page = pfn_to_page(pgdat->node_start_pfn + i);
  77. total++;
  78. if (PageReserved(page))
  79. reserved++;
  80. else if (PageSwapCache(page))
  81. cached++;
  82. else if (page_count(page))
  83. shared += page_count(page) - 1;
  84. }
  85. }
  86. printk(KERN_INFO "%lu pages of RAM\n", total);
  87. printk(KERN_INFO "%lu reserved pages\n",reserved);
  88. printk(KERN_INFO "%lu pages shared\n",shared);
  89. printk(KERN_INFO "%lu pages swap cached\n",cached);
  90. }
  91. int after_bootmem;
  92. static __init void *spp_getpage(void)
  93. {
  94. void *ptr;
  95. if (after_bootmem)
  96. ptr = (void *) get_zeroed_page(GFP_ATOMIC);
  97. else
  98. ptr = alloc_bootmem_pages(PAGE_SIZE);
  99. if (!ptr || ((unsigned long)ptr & ~PAGE_MASK))
  100. panic("set_pte_phys: cannot allocate page data %s\n", after_bootmem?"after bootmem":"");
  101. Dprintk("spp_getpage %p\n", ptr);
  102. return ptr;
  103. }
  104. static __init void set_pte_phys(unsigned long vaddr,
  105. unsigned long phys, pgprot_t prot)
  106. {
  107. pgd_t *pgd;
  108. pud_t *pud;
  109. pmd_t *pmd;
  110. pte_t *pte, new_pte;
  111. Dprintk("set_pte_phys %lx to %lx\n", vaddr, phys);
  112. pgd = pgd_offset_k(vaddr);
  113. if (pgd_none(*pgd)) {
  114. printk("PGD FIXMAP MISSING, it should be setup in head.S!\n");
  115. return;
  116. }
  117. pud = pud_offset(pgd, vaddr);
  118. if (pud_none(*pud)) {
  119. pmd = (pmd_t *) spp_getpage();
  120. set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE | _PAGE_USER));
  121. if (pmd != pmd_offset(pud, 0)) {
  122. printk("PAGETABLE BUG #01! %p <-> %p\n", pmd, pmd_offset(pud,0));
  123. return;
  124. }
  125. }
  126. pmd = pmd_offset(pud, vaddr);
  127. if (pmd_none(*pmd)) {
  128. pte = (pte_t *) spp_getpage();
  129. set_pmd(pmd, __pmd(__pa(pte) | _KERNPG_TABLE | _PAGE_USER));
  130. if (pte != pte_offset_kernel(pmd, 0)) {
  131. printk("PAGETABLE BUG #02!\n");
  132. return;
  133. }
  134. }
  135. new_pte = pfn_pte(phys >> PAGE_SHIFT, prot);
  136. pte = pte_offset_kernel(pmd, vaddr);
  137. if (!pte_none(*pte) &&
  138. pte_val(*pte) != (pte_val(new_pte) & __supported_pte_mask))
  139. pte_ERROR(*pte);
  140. set_pte(pte, new_pte);
  141. /*
  142. * It's enough to flush this one mapping.
  143. * (PGE mappings get flushed as well)
  144. */
  145. __flush_tlb_one(vaddr);
  146. }
  147. /* NOTE: this is meant to be run only at boot */
  148. void __init
  149. __set_fixmap (enum fixed_addresses idx, unsigned long phys, pgprot_t prot)
  150. {
  151. unsigned long address = __fix_to_virt(idx);
  152. if (idx >= __end_of_fixed_addresses) {
  153. printk("Invalid __set_fixmap\n");
  154. return;
  155. }
  156. set_pte_phys(address, phys, prot);
  157. }
  158. static unsigned long __initdata table_start;
  159. static unsigned long __meminitdata table_end;
  160. static __meminit void *alloc_low_page(unsigned long *phys)
  161. {
  162. unsigned long pfn = table_end++;
  163. void *adr;
  164. if (after_bootmem) {
  165. adr = (void *)get_zeroed_page(GFP_ATOMIC);
  166. *phys = __pa(adr);
  167. return adr;
  168. }
  169. if (pfn >= end_pfn)
  170. panic("alloc_low_page: ran out of memory");
  171. adr = early_ioremap(pfn * PAGE_SIZE, PAGE_SIZE);
  172. memset(adr, 0, PAGE_SIZE);
  173. *phys = pfn * PAGE_SIZE;
  174. return adr;
  175. }
  176. static __meminit void unmap_low_page(void *adr)
  177. {
  178. if (after_bootmem)
  179. return;
  180. early_iounmap(adr, PAGE_SIZE);
  181. }
  182. /* Must run before zap_low_mappings */
  183. __meminit void *early_ioremap(unsigned long addr, unsigned long size)
  184. {
  185. unsigned long vaddr;
  186. pmd_t *pmd, *last_pmd;
  187. int i, pmds;
  188. pmds = ((addr & ~PMD_MASK) + size + ~PMD_MASK) / PMD_SIZE;
  189. vaddr = __START_KERNEL_map;
  190. pmd = level2_kernel_pgt;
  191. last_pmd = level2_kernel_pgt + PTRS_PER_PMD - 1;
  192. for (; pmd <= last_pmd; pmd++, vaddr += PMD_SIZE) {
  193. for (i = 0; i < pmds; i++) {
  194. if (pmd_present(pmd[i]))
  195. goto next;
  196. }
  197. vaddr += addr & ~PMD_MASK;
  198. addr &= PMD_MASK;
  199. for (i = 0; i < pmds; i++, addr += PMD_SIZE)
  200. set_pmd(pmd+i, __pmd(addr | __PAGE_KERNEL_LARGE_EXEC));
  201. __flush_tlb_all();
  202. return (void *)vaddr;
  203. next:
  204. ;
  205. }
  206. printk("early_ioremap(0x%lx, %lu) failed\n", addr, size);
  207. return NULL;
  208. }
  209. /* To avoid virtual aliases later */
  210. __meminit void early_iounmap(void *addr, unsigned long size)
  211. {
  212. unsigned long vaddr;
  213. pmd_t *pmd;
  214. int i, pmds;
  215. vaddr = (unsigned long)addr;
  216. pmds = ((vaddr & ~PMD_MASK) + size + ~PMD_MASK) / PMD_SIZE;
  217. pmd = level2_kernel_pgt + pmd_index(vaddr);
  218. for (i = 0; i < pmds; i++)
  219. pmd_clear(pmd + i);
  220. __flush_tlb_all();
  221. }
  222. static void __meminit
  223. phys_pmd_init(pmd_t *pmd_page, unsigned long address, unsigned long end)
  224. {
  225. int i = pmd_index(address);
  226. for (; i < PTRS_PER_PMD; i++, address += PMD_SIZE) {
  227. unsigned long entry;
  228. pmd_t *pmd = pmd_page + pmd_index(address);
  229. if (address >= end) {
  230. if (!after_bootmem)
  231. for (; i < PTRS_PER_PMD; i++, pmd++)
  232. set_pmd(pmd, __pmd(0));
  233. break;
  234. }
  235. if (pmd_val(*pmd))
  236. continue;
  237. entry = __PAGE_KERNEL_LARGE|_PAGE_GLOBAL|address;
  238. entry &= __supported_pte_mask;
  239. set_pmd(pmd, __pmd(entry));
  240. }
  241. }
  242. static void __meminit
  243. phys_pmd_update(pud_t *pud, unsigned long address, unsigned long end)
  244. {
  245. pmd_t *pmd = pmd_offset(pud,0);
  246. spin_lock(&init_mm.page_table_lock);
  247. phys_pmd_init(pmd, address, end);
  248. spin_unlock(&init_mm.page_table_lock);
  249. __flush_tlb_all();
  250. }
  251. static void __meminit phys_pud_init(pud_t *pud_page, unsigned long addr, unsigned long end)
  252. {
  253. int i = pud_index(addr);
  254. for (; i < PTRS_PER_PUD; i++, addr = (addr & PUD_MASK) + PUD_SIZE ) {
  255. unsigned long pmd_phys;
  256. pud_t *pud = pud_page + pud_index(addr);
  257. pmd_t *pmd;
  258. if (addr >= end)
  259. break;
  260. if (!after_bootmem && !e820_any_mapped(addr,addr+PUD_SIZE,0)) {
  261. set_pud(pud, __pud(0));
  262. continue;
  263. }
  264. if (pud_val(*pud)) {
  265. phys_pmd_update(pud, addr, end);
  266. continue;
  267. }
  268. pmd = alloc_low_page(&pmd_phys);
  269. spin_lock(&init_mm.page_table_lock);
  270. set_pud(pud, __pud(pmd_phys | _KERNPG_TABLE));
  271. phys_pmd_init(pmd, addr, end);
  272. spin_unlock(&init_mm.page_table_lock);
  273. unmap_low_page(pmd);
  274. }
  275. __flush_tlb_all();
  276. }
  277. static void __init find_early_table_space(unsigned long end)
  278. {
  279. unsigned long puds, pmds, tables, start;
  280. puds = (end + PUD_SIZE - 1) >> PUD_SHIFT;
  281. pmds = (end + PMD_SIZE - 1) >> PMD_SHIFT;
  282. tables = round_up(puds * sizeof(pud_t), PAGE_SIZE) +
  283. round_up(pmds * sizeof(pmd_t), PAGE_SIZE);
  284. /* RED-PEN putting page tables only on node 0 could
  285. cause a hotspot and fill up ZONE_DMA. The page tables
  286. need roughly 0.5KB per GB. */
  287. start = 0x8000;
  288. table_start = find_e820_area(start, end, tables);
  289. if (table_start == -1UL)
  290. panic("Cannot find space for the kernel page tables");
  291. table_start >>= PAGE_SHIFT;
  292. table_end = table_start;
  293. early_printk("kernel direct mapping tables up to %lx @ %lx-%lx\n",
  294. end, table_start << PAGE_SHIFT,
  295. (table_start << PAGE_SHIFT) + tables);
  296. }
  297. /* Setup the direct mapping of the physical memory at PAGE_OFFSET.
  298. This runs before bootmem is initialized and gets pages directly from the
  299. physical memory. To access them they are temporarily mapped. */
  300. void __init_refok init_memory_mapping(unsigned long start, unsigned long end)
  301. {
  302. unsigned long next;
  303. Dprintk("init_memory_mapping\n");
  304. /*
  305. * Find space for the kernel direct mapping tables.
  306. * Later we should allocate these tables in the local node of the memory
  307. * mapped. Unfortunately this is done currently before the nodes are
  308. * discovered.
  309. */
  310. if (!after_bootmem)
  311. find_early_table_space(end);
  312. start = (unsigned long)__va(start);
  313. end = (unsigned long)__va(end);
  314. for (; start < end; start = next) {
  315. unsigned long pud_phys;
  316. pgd_t *pgd = pgd_offset_k(start);
  317. pud_t *pud;
  318. if (after_bootmem)
  319. pud = pud_offset(pgd, start & PGDIR_MASK);
  320. else
  321. pud = alloc_low_page(&pud_phys);
  322. next = start + PGDIR_SIZE;
  323. if (next > end)
  324. next = end;
  325. phys_pud_init(pud, __pa(start), __pa(next));
  326. if (!after_bootmem)
  327. set_pgd(pgd_offset_k(start), mk_kernel_pgd(pud_phys));
  328. unmap_low_page(pud);
  329. }
  330. if (!after_bootmem)
  331. mmu_cr4_features = read_cr4();
  332. __flush_tlb_all();
  333. reserve_early(table_start << PAGE_SHIFT, table_end << PAGE_SHIFT);
  334. }
  335. #ifndef CONFIG_NUMA
  336. void __init paging_init(void)
  337. {
  338. unsigned long max_zone_pfns[MAX_NR_ZONES];
  339. memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
  340. max_zone_pfns[ZONE_DMA] = MAX_DMA_PFN;
  341. max_zone_pfns[ZONE_DMA32] = MAX_DMA32_PFN;
  342. max_zone_pfns[ZONE_NORMAL] = end_pfn;
  343. memory_present(0, 0, end_pfn);
  344. sparse_init();
  345. free_area_init_nodes(max_zone_pfns);
  346. }
  347. #endif
  348. /* Unmap a kernel mapping if it exists. This is useful to avoid prefetches
  349. from the CPU leading to inconsistent cache lines. address and size
  350. must be aligned to 2MB boundaries.
  351. Does nothing when the mapping doesn't exist. */
  352. void __init clear_kernel_mapping(unsigned long address, unsigned long size)
  353. {
  354. unsigned long end = address + size;
  355. BUG_ON(address & ~LARGE_PAGE_MASK);
  356. BUG_ON(size & ~LARGE_PAGE_MASK);
  357. for (; address < end; address += LARGE_PAGE_SIZE) {
  358. pgd_t *pgd = pgd_offset_k(address);
  359. pud_t *pud;
  360. pmd_t *pmd;
  361. if (pgd_none(*pgd))
  362. continue;
  363. pud = pud_offset(pgd, address);
  364. if (pud_none(*pud))
  365. continue;
  366. pmd = pmd_offset(pud, address);
  367. if (!pmd || pmd_none(*pmd))
  368. continue;
  369. if (0 == (pmd_val(*pmd) & _PAGE_PSE)) {
  370. /* Could handle this, but it should not happen currently. */
  371. printk(KERN_ERR
  372. "clear_kernel_mapping: mapping has been split. will leak memory\n");
  373. pmd_ERROR(*pmd);
  374. }
  375. set_pmd(pmd, __pmd(0));
  376. }
  377. __flush_tlb_all();
  378. }
  379. /*
  380. * Memory hotplug specific functions
  381. */
  382. void online_page(struct page *page)
  383. {
  384. ClearPageReserved(page);
  385. init_page_count(page);
  386. __free_page(page);
  387. totalram_pages++;
  388. num_physpages++;
  389. }
  390. #ifdef CONFIG_MEMORY_HOTPLUG
  391. /*
  392. * Memory is added always to NORMAL zone. This means you will never get
  393. * additional DMA/DMA32 memory.
  394. */
  395. int arch_add_memory(int nid, u64 start, u64 size)
  396. {
  397. struct pglist_data *pgdat = NODE_DATA(nid);
  398. struct zone *zone = pgdat->node_zones + ZONE_NORMAL;
  399. unsigned long start_pfn = start >> PAGE_SHIFT;
  400. unsigned long nr_pages = size >> PAGE_SHIFT;
  401. int ret;
  402. init_memory_mapping(start, (start + size -1));
  403. ret = __add_pages(zone, start_pfn, nr_pages);
  404. if (ret)
  405. goto error;
  406. return ret;
  407. error:
  408. printk("%s: Problem encountered in __add_pages!\n", __func__);
  409. return ret;
  410. }
  411. EXPORT_SYMBOL_GPL(arch_add_memory);
  412. #if !defined(CONFIG_ACPI_NUMA) && defined(CONFIG_NUMA)
  413. int memory_add_physaddr_to_nid(u64 start)
  414. {
  415. return 0;
  416. }
  417. EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid);
  418. #endif
  419. #endif /* CONFIG_MEMORY_HOTPLUG */
  420. static struct kcore_list kcore_mem, kcore_vmalloc, kcore_kernel, kcore_modules,
  421. kcore_vsyscall;
  422. void __init mem_init(void)
  423. {
  424. long codesize, reservedpages, datasize, initsize;
  425. pci_iommu_alloc();
  426. /* clear_bss() already clear the empty_zero_page */
  427. /* temporary debugging - double check it's true: */
  428. {
  429. int i;
  430. for (i = 0; i < 1024; i++)
  431. WARN_ON_ONCE(empty_zero_page[i]);
  432. }
  433. reservedpages = 0;
  434. /* this will put all low memory onto the freelists */
  435. #ifdef CONFIG_NUMA
  436. totalram_pages = numa_free_all_bootmem();
  437. #else
  438. totalram_pages = free_all_bootmem();
  439. #endif
  440. reservedpages = end_pfn - totalram_pages -
  441. absent_pages_in_range(0, end_pfn);
  442. after_bootmem = 1;
  443. codesize = (unsigned long) &_etext - (unsigned long) &_text;
  444. datasize = (unsigned long) &_edata - (unsigned long) &_etext;
  445. initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
  446. /* Register memory areas for /proc/kcore */
  447. kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT);
  448. kclist_add(&kcore_vmalloc, (void *)VMALLOC_START,
  449. VMALLOC_END-VMALLOC_START);
  450. kclist_add(&kcore_kernel, &_stext, _end - _stext);
  451. kclist_add(&kcore_modules, (void *)MODULES_VADDR, MODULES_LEN);
  452. kclist_add(&kcore_vsyscall, (void *)VSYSCALL_START,
  453. VSYSCALL_END - VSYSCALL_START);
  454. printk("Memory: %luk/%luk available (%ldk kernel code, %ldk reserved, %ldk data, %ldk init)\n",
  455. (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
  456. end_pfn << (PAGE_SHIFT-10),
  457. codesize >> 10,
  458. reservedpages << (PAGE_SHIFT-10),
  459. datasize >> 10,
  460. initsize >> 10);
  461. }
  462. void free_init_pages(char *what, unsigned long begin, unsigned long end)
  463. {
  464. unsigned long addr;
  465. if (begin >= end)
  466. return;
  467. printk(KERN_INFO "Freeing %s: %luk freed\n", what, (end - begin) >> 10);
  468. for (addr = begin; addr < end; addr += PAGE_SIZE) {
  469. ClearPageReserved(virt_to_page(addr));
  470. init_page_count(virt_to_page(addr));
  471. memset((void *)(addr & ~(PAGE_SIZE-1)),
  472. POISON_FREE_INITMEM, PAGE_SIZE);
  473. free_page(addr);
  474. totalram_pages++;
  475. }
  476. #ifdef CONFIG_DEBUG_RODATA
  477. /*
  478. * This will make the __init pages not present and
  479. * not executable, so that any attempt to use a
  480. * __init function from now on will fault immediately
  481. * rather than supriously later when memory gets reused.
  482. *
  483. * We only do this for DEBUG_RODATA to not break up the
  484. * 2Mb kernel mapping just for this debug feature.
  485. */
  486. if (begin >= __START_KERNEL_map) {
  487. set_memory_rw(begin, (end - begin)/PAGE_SIZE);
  488. set_memory_np(begin, (end - begin)/PAGE_SIZE);
  489. set_memory_nx(begin, (end - begin)/PAGE_SIZE);
  490. }
  491. #endif
  492. }
  493. void free_initmem(void)
  494. {
  495. free_init_pages("unused kernel memory",
  496. (unsigned long)(&__init_begin),
  497. (unsigned long)(&__init_end));
  498. }
  499. #ifdef CONFIG_DEBUG_RODATA
  500. const int rodata_test_data = 0xC3;
  501. EXPORT_SYMBOL_GPL(rodata_test_data);
  502. void mark_rodata_ro(void)
  503. {
  504. unsigned long start = (unsigned long)_stext, end;
  505. #ifdef CONFIG_HOTPLUG_CPU
  506. /* It must still be possible to apply SMP alternatives. */
  507. if (num_possible_cpus() > 1)
  508. start = (unsigned long)_etext;
  509. #endif
  510. #ifdef CONFIG_KPROBES
  511. start = (unsigned long)__start_rodata;
  512. #endif
  513. end = (unsigned long)__end_rodata;
  514. start = (start + PAGE_SIZE - 1) & PAGE_MASK;
  515. end &= PAGE_MASK;
  516. if (end <= start)
  517. return;
  518. set_memory_ro(start, (end - start) >> PAGE_SHIFT);
  519. printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n",
  520. (end - start) >> 10);
  521. rodata_test();
  522. #ifdef CONFIG_CPA_DEBUG
  523. printk("Testing CPA: undo %lx-%lx\n", start, end);
  524. set_memory_rw(start, (end-start) >> PAGE_SHIFT);
  525. printk("Testing CPA: again\n");
  526. set_memory_ro(start, (end-start) >> PAGE_SHIFT);
  527. #endif
  528. }
  529. #endif
  530. #ifdef CONFIG_BLK_DEV_INITRD
  531. void free_initrd_mem(unsigned long start, unsigned long end)
  532. {
  533. free_init_pages("initrd memory", start, end);
  534. }
  535. #endif
  536. void __init reserve_bootmem_generic(unsigned long phys, unsigned len)
  537. {
  538. #ifdef CONFIG_NUMA
  539. int nid = phys_to_nid(phys);
  540. #endif
  541. unsigned long pfn = phys >> PAGE_SHIFT;
  542. if (pfn >= end_pfn) {
  543. /* This can happen with kdump kernels when accessing firmware
  544. tables. */
  545. if (pfn < end_pfn_map)
  546. return;
  547. printk(KERN_ERR "reserve_bootmem: illegal reserve %lx %u\n",
  548. phys, len);
  549. return;
  550. }
  551. /* Should check here against the e820 map to avoid double free */
  552. #ifdef CONFIG_NUMA
  553. reserve_bootmem_node(NODE_DATA(nid), phys, len);
  554. #else
  555. reserve_bootmem(phys, len);
  556. #endif
  557. if (phys+len <= MAX_DMA_PFN*PAGE_SIZE) {
  558. dma_reserve += len / PAGE_SIZE;
  559. set_dma_reserve(dma_reserve);
  560. }
  561. }
  562. int kern_addr_valid(unsigned long addr)
  563. {
  564. unsigned long above = ((long)addr) >> __VIRTUAL_MASK_SHIFT;
  565. pgd_t *pgd;
  566. pud_t *pud;
  567. pmd_t *pmd;
  568. pte_t *pte;
  569. if (above != 0 && above != -1UL)
  570. return 0;
  571. pgd = pgd_offset_k(addr);
  572. if (pgd_none(*pgd))
  573. return 0;
  574. pud = pud_offset(pgd, addr);
  575. if (pud_none(*pud))
  576. return 0;
  577. pmd = pmd_offset(pud, addr);
  578. if (pmd_none(*pmd))
  579. return 0;
  580. if (pmd_large(*pmd))
  581. return pfn_valid(pmd_pfn(*pmd));
  582. pte = pte_offset_kernel(pmd, addr);
  583. if (pte_none(*pte))
  584. return 0;
  585. return pfn_valid(pte_pfn(*pte));
  586. }
  587. /* A pseudo VMA to allow ptrace access for the vsyscall page. This only
  588. covers the 64bit vsyscall page now. 32bit has a real VMA now and does
  589. not need special handling anymore. */
  590. static struct vm_area_struct gate_vma = {
  591. .vm_start = VSYSCALL_START,
  592. .vm_end = VSYSCALL_START + (VSYSCALL_MAPPED_PAGES << PAGE_SHIFT),
  593. .vm_page_prot = PAGE_READONLY_EXEC,
  594. .vm_flags = VM_READ | VM_EXEC
  595. };
  596. struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
  597. {
  598. #ifdef CONFIG_IA32_EMULATION
  599. if (test_tsk_thread_flag(tsk, TIF_IA32))
  600. return NULL;
  601. #endif
  602. return &gate_vma;
  603. }
  604. int in_gate_area(struct task_struct *task, unsigned long addr)
  605. {
  606. struct vm_area_struct *vma = get_gate_vma(task);
  607. if (!vma)
  608. return 0;
  609. return (addr >= vma->vm_start) && (addr < vma->vm_end);
  610. }
  611. /* Use this when you have no reliable task/vma, typically from interrupt
  612. * context. It is less reliable than using the task's vma and may give
  613. * false positives.
  614. */
  615. int in_gate_area_no_task(unsigned long addr)
  616. {
  617. return (addr >= VSYSCALL_START) && (addr < VSYSCALL_END);
  618. }
  619. const char *arch_vma_name(struct vm_area_struct *vma)
  620. {
  621. if (vma->vm_mm && vma->vm_start == (long)vma->vm_mm->context.vdso)
  622. return "[vdso]";
  623. if (vma == &gate_vma)
  624. return "[vsyscall]";
  625. return NULL;
  626. }
  627. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  628. /*
  629. * Initialise the sparsemem vmemmap using huge-pages at the PMD level.
  630. */
  631. int __meminit vmemmap_populate(struct page *start_page,
  632. unsigned long size, int node)
  633. {
  634. unsigned long addr = (unsigned long)start_page;
  635. unsigned long end = (unsigned long)(start_page + size);
  636. unsigned long next;
  637. pgd_t *pgd;
  638. pud_t *pud;
  639. pmd_t *pmd;
  640. for (; addr < end; addr = next) {
  641. next = pmd_addr_end(addr, end);
  642. pgd = vmemmap_pgd_populate(addr, node);
  643. if (!pgd)
  644. return -ENOMEM;
  645. pud = vmemmap_pud_populate(pgd, addr, node);
  646. if (!pud)
  647. return -ENOMEM;
  648. pmd = pmd_offset(pud, addr);
  649. if (pmd_none(*pmd)) {
  650. pte_t entry;
  651. void *p = vmemmap_alloc_block(PMD_SIZE, node);
  652. if (!p)
  653. return -ENOMEM;
  654. entry = pfn_pte(__pa(p) >> PAGE_SHIFT, PAGE_KERNEL_LARGE);
  655. set_pmd(pmd, __pmd(pte_val(entry)));
  656. printk(KERN_DEBUG " [%lx-%lx] PMD ->%p on node %d\n",
  657. addr, addr + PMD_SIZE - 1, p, node);
  658. } else
  659. vmemmap_verify((pte_t *)pmd, node, addr, next);
  660. }
  661. return 0;
  662. }
  663. #endif