init_64.c 29 KB

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