init.c 19 KB

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