init_64.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  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/initrd.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/pfn.h>
  26. #include <linux/poison.h>
  27. #include <linux/dma-mapping.h>
  28. #include <linux/module.h>
  29. #include <linux/memory_hotplug.h>
  30. #include <linux/nmi.h>
  31. #include <asm/processor.h>
  32. #include <asm/system.h>
  33. #include <asm/uaccess.h>
  34. #include <asm/pgtable.h>
  35. #include <asm/pgalloc.h>
  36. #include <asm/dma.h>
  37. #include <asm/fixmap.h>
  38. #include <asm/e820.h>
  39. #include <asm/apic.h>
  40. #include <asm/tlb.h>
  41. #include <asm/mmu_context.h>
  42. #include <asm/proto.h>
  43. #include <asm/smp.h>
  44. #include <asm/sections.h>
  45. #include <asm/kdebug.h>
  46. #include <asm/numa.h>
  47. #include <asm/cacheflush.h>
  48. /*
  49. * end_pfn only includes RAM, while max_pfn_mapped includes all e820 entries.
  50. * The direct mapping extends to max_pfn_mapped, so that we can directly access
  51. * apertures, ACPI and other tables without having to play with fixmaps.
  52. */
  53. unsigned long max_low_pfn_mapped;
  54. unsigned long max_pfn_mapped;
  55. static unsigned long dma_reserve __initdata;
  56. DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
  57. int direct_gbpages
  58. #ifdef CONFIG_DIRECT_GBPAGES
  59. = 1
  60. #endif
  61. ;
  62. static int __init parse_direct_gbpages_off(char *arg)
  63. {
  64. direct_gbpages = 0;
  65. return 0;
  66. }
  67. early_param("nogbpages", parse_direct_gbpages_off);
  68. static int __init parse_direct_gbpages_on(char *arg)
  69. {
  70. direct_gbpages = 1;
  71. return 0;
  72. }
  73. early_param("gbpages", parse_direct_gbpages_on);
  74. /*
  75. * NOTE: pagetable_init alloc all the fixmap pagetables contiguous on the
  76. * physical space so we can cache the place of the first one and move
  77. * around without checking the pgd every time.
  78. */
  79. int after_bootmem;
  80. unsigned long __supported_pte_mask __read_mostly = ~0UL;
  81. EXPORT_SYMBOL_GPL(__supported_pte_mask);
  82. static int do_not_nx __cpuinitdata;
  83. /*
  84. * noexec=on|off
  85. * Control non-executable mappings for 64-bit processes.
  86. *
  87. * on Enable (default)
  88. * off Disable
  89. */
  90. static int __init nonx_setup(char *str)
  91. {
  92. if (!str)
  93. return -EINVAL;
  94. if (!strncmp(str, "on", 2)) {
  95. __supported_pte_mask |= _PAGE_NX;
  96. do_not_nx = 0;
  97. } else if (!strncmp(str, "off", 3)) {
  98. do_not_nx = 1;
  99. __supported_pte_mask &= ~_PAGE_NX;
  100. }
  101. return 0;
  102. }
  103. early_param("noexec", nonx_setup);
  104. void __cpuinit check_efer(void)
  105. {
  106. unsigned long efer;
  107. rdmsrl(MSR_EFER, efer);
  108. if (!(efer & EFER_NX) || do_not_nx)
  109. __supported_pte_mask &= ~_PAGE_NX;
  110. }
  111. int force_personality32;
  112. /*
  113. * noexec32=on|off
  114. * Control non executable heap for 32bit processes.
  115. * To control the stack too use noexec=off
  116. *
  117. * on PROT_READ does not imply PROT_EXEC for 32-bit processes (default)
  118. * off PROT_READ implies PROT_EXEC
  119. */
  120. static int __init nonx32_setup(char *str)
  121. {
  122. if (!strcmp(str, "on"))
  123. force_personality32 &= ~READ_IMPLIES_EXEC;
  124. else if (!strcmp(str, "off"))
  125. force_personality32 |= READ_IMPLIES_EXEC;
  126. return 1;
  127. }
  128. __setup("noexec32=", nonx32_setup);
  129. /*
  130. * NOTE: This function is marked __ref because it calls __init function
  131. * (alloc_bootmem_pages). It's safe to do it ONLY when after_bootmem == 0.
  132. */
  133. static __ref void *spp_getpage(void)
  134. {
  135. void *ptr;
  136. if (after_bootmem)
  137. ptr = (void *) get_zeroed_page(GFP_ATOMIC);
  138. else
  139. ptr = alloc_bootmem_pages(PAGE_SIZE);
  140. if (!ptr || ((unsigned long)ptr & ~PAGE_MASK)) {
  141. panic("set_pte_phys: cannot allocate page data %s\n",
  142. after_bootmem ? "after bootmem" : "");
  143. }
  144. pr_debug("spp_getpage %p\n", ptr);
  145. return ptr;
  146. }
  147. void
  148. set_pte_vaddr_pud(pud_t *pud_page, unsigned long vaddr, pte_t new_pte)
  149. {
  150. pud_t *pud;
  151. pmd_t *pmd;
  152. pte_t *pte;
  153. pud = pud_page + pud_index(vaddr);
  154. if (pud_none(*pud)) {
  155. pmd = (pmd_t *) spp_getpage();
  156. pud_populate(&init_mm, pud, pmd);
  157. if (pmd != pmd_offset(pud, 0)) {
  158. printk(KERN_ERR "PAGETABLE BUG #01! %p <-> %p\n",
  159. pmd, pmd_offset(pud, 0));
  160. return;
  161. }
  162. }
  163. pmd = pmd_offset(pud, vaddr);
  164. if (pmd_none(*pmd)) {
  165. pte = (pte_t *) spp_getpage();
  166. pmd_populate_kernel(&init_mm, pmd, pte);
  167. if (pte != pte_offset_kernel(pmd, 0)) {
  168. printk(KERN_ERR "PAGETABLE BUG #02!\n");
  169. return;
  170. }
  171. }
  172. pte = pte_offset_kernel(pmd, vaddr);
  173. if (!pte_none(*pte) && pte_val(new_pte) &&
  174. pte_val(*pte) != (pte_val(new_pte) & __supported_pte_mask))
  175. pte_ERROR(*pte);
  176. set_pte(pte, new_pte);
  177. /*
  178. * It's enough to flush this one mapping.
  179. * (PGE mappings get flushed as well)
  180. */
  181. __flush_tlb_one(vaddr);
  182. }
  183. void
  184. set_pte_vaddr(unsigned long vaddr, pte_t pteval)
  185. {
  186. pgd_t *pgd;
  187. pud_t *pud_page;
  188. pr_debug("set_pte_vaddr %lx to %lx\n", vaddr, native_pte_val(pteval));
  189. pgd = pgd_offset_k(vaddr);
  190. if (pgd_none(*pgd)) {
  191. printk(KERN_ERR
  192. "PGD FIXMAP MISSING, it should be setup in head.S!\n");
  193. return;
  194. }
  195. pud_page = (pud_t*)pgd_page_vaddr(*pgd);
  196. set_pte_vaddr_pud(pud_page, vaddr, pteval);
  197. }
  198. /*
  199. * Create large page table mappings for a range of physical addresses.
  200. */
  201. static void __init __init_extra_mapping(unsigned long phys, unsigned long size,
  202. pgprot_t prot)
  203. {
  204. pgd_t *pgd;
  205. pud_t *pud;
  206. pmd_t *pmd;
  207. BUG_ON((phys & ~PMD_MASK) || (size & ~PMD_MASK));
  208. for (; size; phys += PMD_SIZE, size -= PMD_SIZE) {
  209. pgd = pgd_offset_k((unsigned long)__va(phys));
  210. if (pgd_none(*pgd)) {
  211. pud = (pud_t *) spp_getpage();
  212. set_pgd(pgd, __pgd(__pa(pud) | _KERNPG_TABLE |
  213. _PAGE_USER));
  214. }
  215. pud = pud_offset(pgd, (unsigned long)__va(phys));
  216. if (pud_none(*pud)) {
  217. pmd = (pmd_t *) spp_getpage();
  218. set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE |
  219. _PAGE_USER));
  220. }
  221. pmd = pmd_offset(pud, phys);
  222. BUG_ON(!pmd_none(*pmd));
  223. set_pmd(pmd, __pmd(phys | pgprot_val(prot)));
  224. }
  225. }
  226. void __init init_extra_mapping_wb(unsigned long phys, unsigned long size)
  227. {
  228. __init_extra_mapping(phys, size, PAGE_KERNEL_LARGE);
  229. }
  230. void __init init_extra_mapping_uc(unsigned long phys, unsigned long size)
  231. {
  232. __init_extra_mapping(phys, size, PAGE_KERNEL_LARGE_NOCACHE);
  233. }
  234. /*
  235. * The head.S code sets up the kernel high mapping:
  236. *
  237. * from __START_KERNEL_map to __START_KERNEL_map + size (== _end-_text)
  238. *
  239. * phys_addr holds the negative offset to the kernel, which is added
  240. * to the compile time generated pmds. This results in invalid pmds up
  241. * to the point where we hit the physaddr 0 mapping.
  242. *
  243. * We limit the mappings to the region from _text to _end. _end is
  244. * rounded up to the 2MB boundary. This catches the invalid pmds as
  245. * well, as they are located before _text:
  246. */
  247. void __init cleanup_highmap(void)
  248. {
  249. unsigned long vaddr = __START_KERNEL_map;
  250. unsigned long end = roundup((unsigned long)_end, PMD_SIZE) - 1;
  251. pmd_t *pmd = level2_kernel_pgt;
  252. pmd_t *last_pmd = pmd + PTRS_PER_PMD;
  253. for (; pmd < last_pmd; pmd++, vaddr += PMD_SIZE) {
  254. if (pmd_none(*pmd))
  255. continue;
  256. if (vaddr < (unsigned long) _text || vaddr > end)
  257. set_pmd(pmd, __pmd(0));
  258. }
  259. }
  260. static unsigned long __initdata table_start;
  261. static unsigned long __meminitdata table_end;
  262. static unsigned long __meminitdata table_top;
  263. static __ref void *alloc_low_page(unsigned long *phys)
  264. {
  265. unsigned long pfn = table_end++;
  266. void *adr;
  267. if (after_bootmem) {
  268. adr = (void *)get_zeroed_page(GFP_ATOMIC);
  269. *phys = __pa(adr);
  270. return adr;
  271. }
  272. if (pfn >= table_top)
  273. panic("alloc_low_page: ran out of memory");
  274. adr = early_ioremap(pfn * PAGE_SIZE, PAGE_SIZE);
  275. memset(adr, 0, PAGE_SIZE);
  276. *phys = pfn * PAGE_SIZE;
  277. return adr;
  278. }
  279. static __ref void unmap_low_page(void *adr)
  280. {
  281. if (after_bootmem)
  282. return;
  283. early_iounmap(adr, PAGE_SIZE);
  284. }
  285. static unsigned long __meminit
  286. phys_pte_init(pte_t *pte_page, unsigned long addr, unsigned long end,
  287. pgprot_t prot)
  288. {
  289. unsigned pages = 0;
  290. unsigned long last_map_addr = end;
  291. int i;
  292. pte_t *pte = pte_page + pte_index(addr);
  293. for(i = pte_index(addr); i < PTRS_PER_PTE; i++, addr += PAGE_SIZE, pte++) {
  294. if (addr >= end) {
  295. if (!after_bootmem) {
  296. for(; i < PTRS_PER_PTE; i++, pte++)
  297. set_pte(pte, __pte(0));
  298. }
  299. break;
  300. }
  301. /*
  302. * We will re-use the existing mapping.
  303. * Xen for example has some special requirements, like mapping
  304. * pagetable pages as RO. So assume someone who pre-setup
  305. * these mappings are more intelligent.
  306. */
  307. if (pte_val(*pte))
  308. continue;
  309. if (0)
  310. printk(" pte=%p addr=%lx pte=%016lx\n",
  311. pte, addr, pfn_pte(addr >> PAGE_SHIFT, PAGE_KERNEL).pte);
  312. pages++;
  313. set_pte(pte, pfn_pte(addr >> PAGE_SHIFT, prot));
  314. last_map_addr = (addr & PAGE_MASK) + PAGE_SIZE;
  315. }
  316. update_page_count(PG_LEVEL_4K, pages);
  317. return last_map_addr;
  318. }
  319. static unsigned long __meminit
  320. phys_pte_update(pmd_t *pmd, unsigned long address, unsigned long end,
  321. pgprot_t prot)
  322. {
  323. pte_t *pte = (pte_t *)pmd_page_vaddr(*pmd);
  324. return phys_pte_init(pte, address, end, prot);
  325. }
  326. static unsigned long __meminit
  327. phys_pmd_init(pmd_t *pmd_page, unsigned long address, unsigned long end,
  328. unsigned long page_size_mask, pgprot_t prot)
  329. {
  330. unsigned long pages = 0;
  331. unsigned long last_map_addr = end;
  332. int i = pmd_index(address);
  333. for (; i < PTRS_PER_PMD; i++, address += PMD_SIZE) {
  334. unsigned long pte_phys;
  335. pmd_t *pmd = pmd_page + pmd_index(address);
  336. pte_t *pte;
  337. pgprot_t new_prot = prot;
  338. if (address >= end) {
  339. if (!after_bootmem) {
  340. for (; i < PTRS_PER_PMD; i++, pmd++)
  341. set_pmd(pmd, __pmd(0));
  342. }
  343. break;
  344. }
  345. if (pmd_val(*pmd)) {
  346. if (!pmd_large(*pmd)) {
  347. spin_lock(&init_mm.page_table_lock);
  348. last_map_addr = phys_pte_update(pmd, address,
  349. end, prot);
  350. spin_unlock(&init_mm.page_table_lock);
  351. continue;
  352. }
  353. /*
  354. * If we are ok with PG_LEVEL_2M mapping, then we will
  355. * use the existing mapping,
  356. *
  357. * Otherwise, we will split the large page mapping but
  358. * use the same existing protection bits except for
  359. * large page, so that we don't violate Intel's TLB
  360. * Application note (317080) which says, while changing
  361. * the page sizes, new and old translations should
  362. * not differ with respect to page frame and
  363. * attributes.
  364. */
  365. if (page_size_mask & (1 << PG_LEVEL_2M))
  366. continue;
  367. new_prot = pte_pgprot(pte_clrhuge(*(pte_t *)pmd));
  368. }
  369. if (page_size_mask & (1<<PG_LEVEL_2M)) {
  370. pages++;
  371. spin_lock(&init_mm.page_table_lock);
  372. set_pte((pte_t *)pmd,
  373. pfn_pte(address >> PAGE_SHIFT,
  374. __pgprot(pgprot_val(prot) | _PAGE_PSE)));
  375. spin_unlock(&init_mm.page_table_lock);
  376. last_map_addr = (address & PMD_MASK) + PMD_SIZE;
  377. continue;
  378. }
  379. pte = alloc_low_page(&pte_phys);
  380. last_map_addr = phys_pte_init(pte, address, end, new_prot);
  381. unmap_low_page(pte);
  382. spin_lock(&init_mm.page_table_lock);
  383. pmd_populate_kernel(&init_mm, pmd, __va(pte_phys));
  384. spin_unlock(&init_mm.page_table_lock);
  385. }
  386. update_page_count(PG_LEVEL_2M, pages);
  387. return last_map_addr;
  388. }
  389. static unsigned long __meminit
  390. phys_pmd_update(pud_t *pud, unsigned long address, unsigned long end,
  391. unsigned long page_size_mask, pgprot_t prot)
  392. {
  393. pmd_t *pmd = pmd_offset(pud, 0);
  394. unsigned long last_map_addr;
  395. last_map_addr = phys_pmd_init(pmd, address, end, page_size_mask, prot);
  396. __flush_tlb_all();
  397. return last_map_addr;
  398. }
  399. static unsigned long __meminit
  400. phys_pud_init(pud_t *pud_page, unsigned long addr, unsigned long end,
  401. unsigned long page_size_mask)
  402. {
  403. unsigned long pages = 0;
  404. unsigned long last_map_addr = end;
  405. int i = pud_index(addr);
  406. for (; i < PTRS_PER_PUD; i++, addr = (addr & PUD_MASK) + PUD_SIZE) {
  407. unsigned long pmd_phys;
  408. pud_t *pud = pud_page + pud_index(addr);
  409. pmd_t *pmd;
  410. pgprot_t prot = PAGE_KERNEL;
  411. if (addr >= end)
  412. break;
  413. if (!after_bootmem &&
  414. !e820_any_mapped(addr, addr+PUD_SIZE, 0)) {
  415. set_pud(pud, __pud(0));
  416. continue;
  417. }
  418. if (pud_val(*pud)) {
  419. if (!pud_large(*pud)) {
  420. last_map_addr = phys_pmd_update(pud, addr, end,
  421. page_size_mask, prot);
  422. continue;
  423. }
  424. /*
  425. * If we are ok with PG_LEVEL_1G mapping, then we will
  426. * use the existing mapping.
  427. *
  428. * Otherwise, we will split the gbpage mapping but use
  429. * the same existing protection bits except for large
  430. * page, so that we don't violate Intel's TLB
  431. * Application note (317080) which says, while changing
  432. * the page sizes, new and old translations should
  433. * not differ with respect to page frame and
  434. * attributes.
  435. */
  436. if (page_size_mask & (1 << PG_LEVEL_1G))
  437. continue;
  438. prot = pte_pgprot(pte_clrhuge(*(pte_t *)pud));
  439. }
  440. if (page_size_mask & (1<<PG_LEVEL_1G)) {
  441. pages++;
  442. spin_lock(&init_mm.page_table_lock);
  443. set_pte((pte_t *)pud,
  444. pfn_pte(addr >> PAGE_SHIFT, PAGE_KERNEL_LARGE));
  445. spin_unlock(&init_mm.page_table_lock);
  446. last_map_addr = (addr & PUD_MASK) + PUD_SIZE;
  447. continue;
  448. }
  449. pmd = alloc_low_page(&pmd_phys);
  450. last_map_addr = phys_pmd_init(pmd, addr, end, page_size_mask,
  451. prot);
  452. unmap_low_page(pmd);
  453. spin_lock(&init_mm.page_table_lock);
  454. pud_populate(&init_mm, pud, __va(pmd_phys));
  455. spin_unlock(&init_mm.page_table_lock);
  456. }
  457. __flush_tlb_all();
  458. update_page_count(PG_LEVEL_1G, pages);
  459. return last_map_addr;
  460. }
  461. static unsigned long __meminit
  462. phys_pud_update(pgd_t *pgd, unsigned long addr, unsigned long end,
  463. unsigned long page_size_mask)
  464. {
  465. pud_t *pud;
  466. pud = (pud_t *)pgd_page_vaddr(*pgd);
  467. return phys_pud_init(pud, addr, end, page_size_mask);
  468. }
  469. static void __init find_early_table_space(unsigned long end, int use_pse,
  470. int use_gbpages)
  471. {
  472. unsigned long puds, pmds, ptes, tables, start;
  473. puds = (end + PUD_SIZE - 1) >> PUD_SHIFT;
  474. tables = roundup(puds * sizeof(pud_t), PAGE_SIZE);
  475. if (use_gbpages) {
  476. unsigned long extra;
  477. extra = end - ((end>>PUD_SHIFT) << PUD_SHIFT);
  478. pmds = (extra + PMD_SIZE - 1) >> PMD_SHIFT;
  479. } else
  480. pmds = (end + PMD_SIZE - 1) >> PMD_SHIFT;
  481. tables += roundup(pmds * sizeof(pmd_t), PAGE_SIZE);
  482. if (use_pse) {
  483. unsigned long extra;
  484. extra = end - ((end>>PMD_SHIFT) << PMD_SHIFT);
  485. ptes = (extra + PAGE_SIZE - 1) >> PAGE_SHIFT;
  486. } else
  487. ptes = (end + PAGE_SIZE - 1) >> PAGE_SHIFT;
  488. tables += roundup(ptes * sizeof(pte_t), PAGE_SIZE);
  489. /*
  490. * RED-PEN putting page tables only on node 0 could
  491. * cause a hotspot and fill up ZONE_DMA. The page tables
  492. * need roughly 0.5KB per GB.
  493. */
  494. start = 0x8000;
  495. table_start = find_e820_area(start, end, tables, PAGE_SIZE);
  496. if (table_start == -1UL)
  497. panic("Cannot find space for the kernel page tables");
  498. table_start >>= PAGE_SHIFT;
  499. table_end = table_start;
  500. table_top = table_start + (tables >> PAGE_SHIFT);
  501. printk(KERN_DEBUG "kernel direct mapping tables up to %lx @ %lx-%lx\n",
  502. end, table_start << PAGE_SHIFT, table_top << PAGE_SHIFT);
  503. }
  504. static void __init init_gbpages(void)
  505. {
  506. if (direct_gbpages && cpu_has_gbpages)
  507. printk(KERN_INFO "Using GB pages for direct mapping\n");
  508. else
  509. direct_gbpages = 0;
  510. }
  511. static unsigned long __init kernel_physical_mapping_init(unsigned long start,
  512. unsigned long end,
  513. unsigned long page_size_mask)
  514. {
  515. unsigned long next, last_map_addr = end;
  516. start = (unsigned long)__va(start);
  517. end = (unsigned long)__va(end);
  518. for (; start < end; start = next) {
  519. pgd_t *pgd = pgd_offset_k(start);
  520. unsigned long pud_phys;
  521. pud_t *pud;
  522. next = (start + PGDIR_SIZE) & PGDIR_MASK;
  523. if (next > end)
  524. next = end;
  525. if (pgd_val(*pgd)) {
  526. last_map_addr = phys_pud_update(pgd, __pa(start),
  527. __pa(end), page_size_mask);
  528. continue;
  529. }
  530. pud = alloc_low_page(&pud_phys);
  531. last_map_addr = phys_pud_init(pud, __pa(start), __pa(next),
  532. page_size_mask);
  533. unmap_low_page(pud);
  534. spin_lock(&init_mm.page_table_lock);
  535. pgd_populate(&init_mm, pgd, __va(pud_phys));
  536. spin_unlock(&init_mm.page_table_lock);
  537. }
  538. __flush_tlb_all();
  539. return last_map_addr;
  540. }
  541. struct map_range {
  542. unsigned long start;
  543. unsigned long end;
  544. unsigned page_size_mask;
  545. };
  546. #define NR_RANGE_MR 5
  547. static int save_mr(struct map_range *mr, int nr_range,
  548. unsigned long start_pfn, unsigned long end_pfn,
  549. unsigned long page_size_mask)
  550. {
  551. if (start_pfn < end_pfn) {
  552. if (nr_range >= NR_RANGE_MR)
  553. panic("run out of range for init_memory_mapping\n");
  554. mr[nr_range].start = start_pfn<<PAGE_SHIFT;
  555. mr[nr_range].end = end_pfn<<PAGE_SHIFT;
  556. mr[nr_range].page_size_mask = page_size_mask;
  557. nr_range++;
  558. }
  559. return nr_range;
  560. }
  561. /*
  562. * Setup the direct mapping of the physical memory at PAGE_OFFSET.
  563. * This runs before bootmem is initialized and gets pages directly from
  564. * the physical memory. To access them they are temporarily mapped.
  565. */
  566. unsigned long __init_refok init_memory_mapping(unsigned long start,
  567. unsigned long end)
  568. {
  569. unsigned long last_map_addr = 0;
  570. unsigned long page_size_mask = 0;
  571. unsigned long start_pfn, end_pfn;
  572. struct map_range mr[NR_RANGE_MR];
  573. int nr_range, i;
  574. int use_pse, use_gbpages;
  575. printk(KERN_INFO "init_memory_mapping\n");
  576. /*
  577. * Find space for the kernel direct mapping tables.
  578. *
  579. * Later we should allocate these tables in the local node of the
  580. * memory mapped. Unfortunately this is done currently before the
  581. * nodes are discovered.
  582. */
  583. if (!after_bootmem)
  584. init_gbpages();
  585. #ifdef CONFIG_DEBUG_PAGEALLOC
  586. /*
  587. * For CONFIG_DEBUG_PAGEALLOC, identity mapping will use small pages.
  588. * This will simplify cpa(), which otherwise needs to support splitting
  589. * large pages into small in interrupt context, etc.
  590. */
  591. use_pse = use_gbpages = 0;
  592. #else
  593. use_pse = cpu_has_pse;
  594. use_gbpages = direct_gbpages;
  595. #endif
  596. if (use_gbpages)
  597. page_size_mask |= 1 << PG_LEVEL_1G;
  598. if (use_pse)
  599. page_size_mask |= 1 << PG_LEVEL_2M;
  600. memset(mr, 0, sizeof(mr));
  601. nr_range = 0;
  602. /* head if not big page alignment ?*/
  603. start_pfn = start >> PAGE_SHIFT;
  604. end_pfn = ((start + (PMD_SIZE - 1)) >> PMD_SHIFT)
  605. << (PMD_SHIFT - PAGE_SHIFT);
  606. nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, 0);
  607. /* big page (2M) range*/
  608. start_pfn = ((start + (PMD_SIZE - 1))>>PMD_SHIFT)
  609. << (PMD_SHIFT - PAGE_SHIFT);
  610. end_pfn = ((start + (PUD_SIZE - 1))>>PUD_SHIFT)
  611. << (PUD_SHIFT - PAGE_SHIFT);
  612. if (end_pfn > ((end>>PUD_SHIFT)<<(PUD_SHIFT - PAGE_SHIFT)))
  613. end_pfn = ((end>>PUD_SHIFT)<<(PUD_SHIFT - PAGE_SHIFT));
  614. nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
  615. page_size_mask & (1<<PG_LEVEL_2M));
  616. /* big page (1G) range */
  617. start_pfn = end_pfn;
  618. end_pfn = (end>>PUD_SHIFT) << (PUD_SHIFT - PAGE_SHIFT);
  619. nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
  620. page_size_mask &
  621. ((1<<PG_LEVEL_2M)|(1<<PG_LEVEL_1G)));
  622. /* tail is not big page (1G) alignment */
  623. start_pfn = end_pfn;
  624. end_pfn = (end>>PMD_SHIFT) << (PMD_SHIFT - PAGE_SHIFT);
  625. nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
  626. page_size_mask & (1<<PG_LEVEL_2M));
  627. /* tail is not big page (2M) alignment */
  628. start_pfn = end_pfn;
  629. end_pfn = end>>PAGE_SHIFT;
  630. nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, 0);
  631. /* try to merge same page size and continuous */
  632. for (i = 0; nr_range > 1 && i < nr_range - 1; i++) {
  633. unsigned long old_start;
  634. if (mr[i].end != mr[i+1].start ||
  635. mr[i].page_size_mask != mr[i+1].page_size_mask)
  636. continue;
  637. /* move it */
  638. old_start = mr[i].start;
  639. memmove(&mr[i], &mr[i+1],
  640. (nr_range - 1 - i) * sizeof (struct map_range));
  641. mr[i].start = old_start;
  642. nr_range--;
  643. }
  644. for (i = 0; i < nr_range; i++)
  645. printk(KERN_DEBUG " %010lx - %010lx page %s\n",
  646. mr[i].start, mr[i].end,
  647. (mr[i].page_size_mask & (1<<PG_LEVEL_1G))?"1G":(
  648. (mr[i].page_size_mask & (1<<PG_LEVEL_2M))?"2M":"4k"));
  649. if (!after_bootmem)
  650. find_early_table_space(end, use_pse, use_gbpages);
  651. for (i = 0; i < nr_range; i++)
  652. last_map_addr = kernel_physical_mapping_init(
  653. mr[i].start, mr[i].end,
  654. mr[i].page_size_mask);
  655. if (!after_bootmem)
  656. mmu_cr4_features = read_cr4();
  657. __flush_tlb_all();
  658. if (!after_bootmem && table_end > table_start)
  659. reserve_early(table_start << PAGE_SHIFT,
  660. table_end << PAGE_SHIFT, "PGTABLE");
  661. printk(KERN_INFO "last_map_addr: %lx end: %lx\n",
  662. last_map_addr, end);
  663. if (!after_bootmem)
  664. early_memtest(start, end);
  665. return last_map_addr >> PAGE_SHIFT;
  666. }
  667. #ifndef CONFIG_NUMA
  668. void __init initmem_init(unsigned long start_pfn, unsigned long end_pfn)
  669. {
  670. unsigned long bootmap_size, bootmap;
  671. bootmap_size = bootmem_bootmap_pages(end_pfn)<<PAGE_SHIFT;
  672. bootmap = find_e820_area(0, end_pfn<<PAGE_SHIFT, bootmap_size,
  673. PAGE_SIZE);
  674. if (bootmap == -1L)
  675. panic("Cannot find bootmem map of size %ld\n", bootmap_size);
  676. /* don't touch min_low_pfn */
  677. bootmap_size = init_bootmem_node(NODE_DATA(0), bootmap >> PAGE_SHIFT,
  678. 0, end_pfn);
  679. e820_register_active_regions(0, start_pfn, end_pfn);
  680. free_bootmem_with_active_regions(0, end_pfn);
  681. early_res_to_bootmem(0, end_pfn<<PAGE_SHIFT);
  682. reserve_bootmem(bootmap, bootmap_size, BOOTMEM_DEFAULT);
  683. }
  684. void __init paging_init(void)
  685. {
  686. unsigned long max_zone_pfns[MAX_NR_ZONES];
  687. memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
  688. max_zone_pfns[ZONE_DMA] = MAX_DMA_PFN;
  689. max_zone_pfns[ZONE_DMA32] = MAX_DMA32_PFN;
  690. max_zone_pfns[ZONE_NORMAL] = max_pfn;
  691. memory_present(0, 0, max_pfn);
  692. sparse_init();
  693. free_area_init_nodes(max_zone_pfns);
  694. }
  695. #endif
  696. /*
  697. * Memory hotplug specific functions
  698. */
  699. #ifdef CONFIG_MEMORY_HOTPLUG
  700. /*
  701. * Memory is added always to NORMAL zone. This means you will never get
  702. * additional DMA/DMA32 memory.
  703. */
  704. int arch_add_memory(int nid, u64 start, u64 size)
  705. {
  706. struct pglist_data *pgdat = NODE_DATA(nid);
  707. struct zone *zone = pgdat->node_zones + ZONE_NORMAL;
  708. unsigned long last_mapped_pfn, start_pfn = start >> PAGE_SHIFT;
  709. unsigned long nr_pages = size >> PAGE_SHIFT;
  710. int ret;
  711. last_mapped_pfn = init_memory_mapping(start, start + size-1);
  712. if (last_mapped_pfn > max_pfn_mapped)
  713. max_pfn_mapped = last_mapped_pfn;
  714. ret = __add_pages(zone, start_pfn, nr_pages);
  715. WARN_ON(1);
  716. return ret;
  717. }
  718. EXPORT_SYMBOL_GPL(arch_add_memory);
  719. #if !defined(CONFIG_ACPI_NUMA) && defined(CONFIG_NUMA)
  720. int memory_add_physaddr_to_nid(u64 start)
  721. {
  722. return 0;
  723. }
  724. EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid);
  725. #endif
  726. #endif /* CONFIG_MEMORY_HOTPLUG */
  727. /*
  728. * devmem_is_allowed() checks to see if /dev/mem access to a certain address
  729. * is valid. The argument is a physical page number.
  730. *
  731. *
  732. * On x86, access has to be given to the first megabyte of ram because that area
  733. * contains bios code and data regions used by X and dosemu and similar apps.
  734. * Access has to be given to non-kernel-ram areas as well, these contain the PCI
  735. * mmio resources as well as potential bios/acpi data regions.
  736. */
  737. int devmem_is_allowed(unsigned long pagenr)
  738. {
  739. if (pagenr <= 256)
  740. return 1;
  741. if (!page_is_ram(pagenr))
  742. return 1;
  743. return 0;
  744. }
  745. static struct kcore_list kcore_mem, kcore_vmalloc, kcore_kernel,
  746. kcore_modules, kcore_vsyscall;
  747. void __init mem_init(void)
  748. {
  749. long codesize, reservedpages, datasize, initsize;
  750. pci_iommu_alloc();
  751. /* clear_bss() already clear the empty_zero_page */
  752. reservedpages = 0;
  753. /* this will put all low memory onto the freelists */
  754. #ifdef CONFIG_NUMA
  755. totalram_pages = numa_free_all_bootmem();
  756. #else
  757. totalram_pages = free_all_bootmem();
  758. #endif
  759. reservedpages = max_pfn - totalram_pages -
  760. absent_pages_in_range(0, max_pfn);
  761. after_bootmem = 1;
  762. codesize = (unsigned long) &_etext - (unsigned long) &_text;
  763. datasize = (unsigned long) &_edata - (unsigned long) &_etext;
  764. initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
  765. /* Register memory areas for /proc/kcore */
  766. kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT);
  767. kclist_add(&kcore_vmalloc, (void *)VMALLOC_START,
  768. VMALLOC_END-VMALLOC_START);
  769. kclist_add(&kcore_kernel, &_stext, _end - _stext);
  770. kclist_add(&kcore_modules, (void *)MODULES_VADDR, MODULES_LEN);
  771. kclist_add(&kcore_vsyscall, (void *)VSYSCALL_START,
  772. VSYSCALL_END - VSYSCALL_START);
  773. printk(KERN_INFO "Memory: %luk/%luk available (%ldk kernel code, "
  774. "%ldk reserved, %ldk data, %ldk init)\n",
  775. (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
  776. max_pfn << (PAGE_SHIFT-10),
  777. codesize >> 10,
  778. reservedpages << (PAGE_SHIFT-10),
  779. datasize >> 10,
  780. initsize >> 10);
  781. }
  782. void free_init_pages(char *what, unsigned long begin, unsigned long end)
  783. {
  784. unsigned long addr = begin;
  785. if (addr >= end)
  786. return;
  787. /*
  788. * If debugging page accesses then do not free this memory but
  789. * mark them not present - any buggy init-section access will
  790. * create a kernel page fault:
  791. */
  792. #ifdef CONFIG_DEBUG_PAGEALLOC
  793. printk(KERN_INFO "debug: unmapping init memory %08lx..%08lx\n",
  794. begin, PAGE_ALIGN(end));
  795. set_memory_np(begin, (end - begin) >> PAGE_SHIFT);
  796. #else
  797. printk(KERN_INFO "Freeing %s: %luk freed\n", what, (end - begin) >> 10);
  798. for (; addr < end; addr += PAGE_SIZE) {
  799. ClearPageReserved(virt_to_page(addr));
  800. init_page_count(virt_to_page(addr));
  801. memset((void *)(addr & ~(PAGE_SIZE-1)),
  802. POISON_FREE_INITMEM, PAGE_SIZE);
  803. free_page(addr);
  804. totalram_pages++;
  805. }
  806. #endif
  807. }
  808. void free_initmem(void)
  809. {
  810. free_init_pages("unused kernel memory",
  811. (unsigned long)(&__init_begin),
  812. (unsigned long)(&__init_end));
  813. }
  814. #ifdef CONFIG_DEBUG_RODATA
  815. const int rodata_test_data = 0xC3;
  816. EXPORT_SYMBOL_GPL(rodata_test_data);
  817. void mark_rodata_ro(void)
  818. {
  819. unsigned long start = PFN_ALIGN(_stext), end = PFN_ALIGN(__end_rodata);
  820. unsigned long rodata_start =
  821. ((unsigned long)__start_rodata + PAGE_SIZE - 1) & PAGE_MASK;
  822. #ifdef CONFIG_DYNAMIC_FTRACE
  823. /* Dynamic tracing modifies the kernel text section */
  824. start = rodata_start;
  825. #endif
  826. printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n",
  827. (end - start) >> 10);
  828. set_memory_ro(start, (end - start) >> PAGE_SHIFT);
  829. /*
  830. * The rodata section (but not the kernel text!) should also be
  831. * not-executable.
  832. */
  833. set_memory_nx(rodata_start, (end - rodata_start) >> PAGE_SHIFT);
  834. rodata_test();
  835. #ifdef CONFIG_CPA_DEBUG
  836. printk(KERN_INFO "Testing CPA: undo %lx-%lx\n", start, end);
  837. set_memory_rw(start, (end-start) >> PAGE_SHIFT);
  838. printk(KERN_INFO "Testing CPA: again\n");
  839. set_memory_ro(start, (end-start) >> PAGE_SHIFT);
  840. #endif
  841. }
  842. #endif
  843. #ifdef CONFIG_BLK_DEV_INITRD
  844. void free_initrd_mem(unsigned long start, unsigned long end)
  845. {
  846. free_init_pages("initrd memory", start, end);
  847. }
  848. #endif
  849. int __init reserve_bootmem_generic(unsigned long phys, unsigned long len,
  850. int flags)
  851. {
  852. #ifdef CONFIG_NUMA
  853. int nid, next_nid;
  854. int ret;
  855. #endif
  856. unsigned long pfn = phys >> PAGE_SHIFT;
  857. if (pfn >= max_pfn) {
  858. /*
  859. * This can happen with kdump kernels when accessing
  860. * firmware tables:
  861. */
  862. if (pfn < max_pfn_mapped)
  863. return -EFAULT;
  864. printk(KERN_ERR "reserve_bootmem: illegal reserve %lx %lu\n",
  865. phys, len);
  866. return -EFAULT;
  867. }
  868. /* Should check here against the e820 map to avoid double free */
  869. #ifdef CONFIG_NUMA
  870. nid = phys_to_nid(phys);
  871. next_nid = phys_to_nid(phys + len - 1);
  872. if (nid == next_nid)
  873. ret = reserve_bootmem_node(NODE_DATA(nid), phys, len, flags);
  874. else
  875. ret = reserve_bootmem(phys, len, flags);
  876. if (ret != 0)
  877. return ret;
  878. #else
  879. reserve_bootmem(phys, len, BOOTMEM_DEFAULT);
  880. #endif
  881. if (phys+len <= MAX_DMA_PFN*PAGE_SIZE) {
  882. dma_reserve += len / PAGE_SIZE;
  883. set_dma_reserve(dma_reserve);
  884. }
  885. return 0;
  886. }
  887. int kern_addr_valid(unsigned long addr)
  888. {
  889. unsigned long above = ((long)addr) >> __VIRTUAL_MASK_SHIFT;
  890. pgd_t *pgd;
  891. pud_t *pud;
  892. pmd_t *pmd;
  893. pte_t *pte;
  894. if (above != 0 && above != -1UL)
  895. return 0;
  896. pgd = pgd_offset_k(addr);
  897. if (pgd_none(*pgd))
  898. return 0;
  899. pud = pud_offset(pgd, addr);
  900. if (pud_none(*pud))
  901. return 0;
  902. pmd = pmd_offset(pud, addr);
  903. if (pmd_none(*pmd))
  904. return 0;
  905. if (pmd_large(*pmd))
  906. return pfn_valid(pmd_pfn(*pmd));
  907. pte = pte_offset_kernel(pmd, addr);
  908. if (pte_none(*pte))
  909. return 0;
  910. return pfn_valid(pte_pfn(*pte));
  911. }
  912. /*
  913. * A pseudo VMA to allow ptrace access for the vsyscall page. This only
  914. * covers the 64bit vsyscall page now. 32bit has a real VMA now and does
  915. * not need special handling anymore:
  916. */
  917. static struct vm_area_struct gate_vma = {
  918. .vm_start = VSYSCALL_START,
  919. .vm_end = VSYSCALL_START + (VSYSCALL_MAPPED_PAGES * PAGE_SIZE),
  920. .vm_page_prot = PAGE_READONLY_EXEC,
  921. .vm_flags = VM_READ | VM_EXEC
  922. };
  923. struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
  924. {
  925. #ifdef CONFIG_IA32_EMULATION
  926. if (test_tsk_thread_flag(tsk, TIF_IA32))
  927. return NULL;
  928. #endif
  929. return &gate_vma;
  930. }
  931. int in_gate_area(struct task_struct *task, unsigned long addr)
  932. {
  933. struct vm_area_struct *vma = get_gate_vma(task);
  934. if (!vma)
  935. return 0;
  936. return (addr >= vma->vm_start) && (addr < vma->vm_end);
  937. }
  938. /*
  939. * Use this when you have no reliable task/vma, typically from interrupt
  940. * context. It is less reliable than using the task's vma and may give
  941. * false positives:
  942. */
  943. int in_gate_area_no_task(unsigned long addr)
  944. {
  945. return (addr >= VSYSCALL_START) && (addr < VSYSCALL_END);
  946. }
  947. const char *arch_vma_name(struct vm_area_struct *vma)
  948. {
  949. if (vma->vm_mm && vma->vm_start == (long)vma->vm_mm->context.vdso)
  950. return "[vdso]";
  951. if (vma == &gate_vma)
  952. return "[vsyscall]";
  953. return NULL;
  954. }
  955. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  956. /*
  957. * Initialise the sparsemem vmemmap using huge-pages at the PMD level.
  958. */
  959. static long __meminitdata addr_start, addr_end;
  960. static void __meminitdata *p_start, *p_end;
  961. static int __meminitdata node_start;
  962. int __meminit
  963. vmemmap_populate(struct page *start_page, unsigned long size, int node)
  964. {
  965. unsigned long addr = (unsigned long)start_page;
  966. unsigned long end = (unsigned long)(start_page + size);
  967. unsigned long next;
  968. pgd_t *pgd;
  969. pud_t *pud;
  970. pmd_t *pmd;
  971. for (; addr < end; addr = next) {
  972. void *p = NULL;
  973. pgd = vmemmap_pgd_populate(addr, node);
  974. if (!pgd)
  975. return -ENOMEM;
  976. pud = vmemmap_pud_populate(pgd, addr, node);
  977. if (!pud)
  978. return -ENOMEM;
  979. if (!cpu_has_pse) {
  980. next = (addr + PAGE_SIZE) & PAGE_MASK;
  981. pmd = vmemmap_pmd_populate(pud, addr, node);
  982. if (!pmd)
  983. return -ENOMEM;
  984. p = vmemmap_pte_populate(pmd, addr, node);
  985. if (!p)
  986. return -ENOMEM;
  987. addr_end = addr + PAGE_SIZE;
  988. p_end = p + PAGE_SIZE;
  989. } else {
  990. next = pmd_addr_end(addr, end);
  991. pmd = pmd_offset(pud, addr);
  992. if (pmd_none(*pmd)) {
  993. pte_t entry;
  994. p = vmemmap_alloc_block(PMD_SIZE, node);
  995. if (!p)
  996. return -ENOMEM;
  997. entry = pfn_pte(__pa(p) >> PAGE_SHIFT,
  998. PAGE_KERNEL_LARGE);
  999. set_pmd(pmd, __pmd(pte_val(entry)));
  1000. /* check to see if we have contiguous blocks */
  1001. if (p_end != p || node_start != node) {
  1002. if (p_start)
  1003. printk(KERN_DEBUG " [%lx-%lx] PMD -> [%p-%p] on node %d\n",
  1004. addr_start, addr_end-1, p_start, p_end-1, node_start);
  1005. addr_start = addr;
  1006. node_start = node;
  1007. p_start = p;
  1008. }
  1009. addr_end = addr + PMD_SIZE;
  1010. p_end = p + PMD_SIZE;
  1011. } else
  1012. vmemmap_verify((pte_t *)pmd, node, addr, next);
  1013. }
  1014. }
  1015. return 0;
  1016. }
  1017. void __meminit vmemmap_populate_print_last(void)
  1018. {
  1019. if (p_start) {
  1020. printk(KERN_DEBUG " [%lx-%lx] PMD -> [%p-%p] on node %d\n",
  1021. addr_start, addr_end-1, p_start, p_end-1, node_start);
  1022. p_start = NULL;
  1023. p_end = NULL;
  1024. node_start = 0;
  1025. }
  1026. }
  1027. #endif