init.c 18 KB

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