init_64.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  1. /*
  2. * linux/arch/x86_64/mm/init.c
  3. *
  4. * Copyright (C) 1995 Linus Torvalds
  5. * Copyright (C) 2000 Pavel Machek <pavel@ucw.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/memblock.h>
  24. #include <linux/proc_fs.h>
  25. #include <linux/pci.h>
  26. #include <linux/pfn.h>
  27. #include <linux/poison.h>
  28. #include <linux/dma-mapping.h>
  29. #include <linux/module.h>
  30. #include <linux/memory_hotplug.h>
  31. #include <linux/nmi.h>
  32. #include <linux/gfp.h>
  33. #include <asm/processor.h>
  34. #include <asm/bios_ebda.h>
  35. #include <asm/system.h>
  36. #include <asm/uaccess.h>
  37. #include <asm/pgtable.h>
  38. #include <asm/pgalloc.h>
  39. #include <asm/dma.h>
  40. #include <asm/fixmap.h>
  41. #include <asm/e820.h>
  42. #include <asm/apic.h>
  43. #include <asm/tlb.h>
  44. #include <asm/mmu_context.h>
  45. #include <asm/proto.h>
  46. #include <asm/smp.h>
  47. #include <asm/sections.h>
  48. #include <asm/kdebug.h>
  49. #include <asm/numa.h>
  50. #include <asm/cacheflush.h>
  51. #include <asm/init.h>
  52. #include <linux/bootmem.h>
  53. static int __init parse_direct_gbpages_off(char *arg)
  54. {
  55. direct_gbpages = 0;
  56. return 0;
  57. }
  58. early_param("nogbpages", parse_direct_gbpages_off);
  59. static int __init parse_direct_gbpages_on(char *arg)
  60. {
  61. direct_gbpages = 1;
  62. return 0;
  63. }
  64. early_param("gbpages", parse_direct_gbpages_on);
  65. /*
  66. * NOTE: pagetable_init alloc all the fixmap pagetables contiguous on the
  67. * physical space so we can cache the place of the first one and move
  68. * around without checking the pgd every time.
  69. */
  70. pteval_t __supported_pte_mask __read_mostly = ~_PAGE_IOMAP;
  71. EXPORT_SYMBOL_GPL(__supported_pte_mask);
  72. int force_personality32;
  73. /*
  74. * noexec32=on|off
  75. * Control non executable heap for 32bit processes.
  76. * To control the stack too use noexec=off
  77. *
  78. * on PROT_READ does not imply PROT_EXEC for 32-bit processes (default)
  79. * off PROT_READ implies PROT_EXEC
  80. */
  81. static int __init nonx32_setup(char *str)
  82. {
  83. if (!strcmp(str, "on"))
  84. force_personality32 &= ~READ_IMPLIES_EXEC;
  85. else if (!strcmp(str, "off"))
  86. force_personality32 |= READ_IMPLIES_EXEC;
  87. return 1;
  88. }
  89. __setup("noexec32=", nonx32_setup);
  90. /*
  91. * When memory was added/removed make sure all the processes MM have
  92. * suitable PGD entries in the local PGD level page.
  93. */
  94. void sync_global_pgds(unsigned long start, unsigned long end)
  95. {
  96. unsigned long address;
  97. for (address = start; address <= end; address += PGDIR_SIZE) {
  98. const pgd_t *pgd_ref = pgd_offset_k(address);
  99. unsigned long flags;
  100. struct page *page;
  101. if (pgd_none(*pgd_ref))
  102. continue;
  103. spin_lock_irqsave(&pgd_lock, flags);
  104. list_for_each_entry(page, &pgd_list, lru) {
  105. pgd_t *pgd;
  106. spinlock_t *pgt_lock;
  107. pgd = (pgd_t *)page_address(page) + pgd_index(address);
  108. pgt_lock = &pgd_page_get_mm(page)->page_table_lock;
  109. spin_lock(pgt_lock);
  110. if (pgd_none(*pgd))
  111. set_pgd(pgd, *pgd_ref);
  112. else
  113. BUG_ON(pgd_page_vaddr(*pgd)
  114. != pgd_page_vaddr(*pgd_ref));
  115. spin_unlock(pgt_lock);
  116. }
  117. spin_unlock_irqrestore(&pgd_lock, flags);
  118. }
  119. }
  120. /*
  121. * NOTE: This function is marked __ref because it calls __init function
  122. * (alloc_bootmem_pages). It's safe to do it ONLY when after_bootmem == 0.
  123. */
  124. static __ref void *spp_getpage(void)
  125. {
  126. void *ptr;
  127. if (after_bootmem)
  128. ptr = (void *) get_zeroed_page(GFP_ATOMIC | __GFP_NOTRACK);
  129. else
  130. ptr = alloc_bootmem_pages(PAGE_SIZE);
  131. if (!ptr || ((unsigned long)ptr & ~PAGE_MASK)) {
  132. panic("set_pte_phys: cannot allocate page data %s\n",
  133. after_bootmem ? "after bootmem" : "");
  134. }
  135. pr_debug("spp_getpage %p\n", ptr);
  136. return ptr;
  137. }
  138. static pud_t *fill_pud(pgd_t *pgd, unsigned long vaddr)
  139. {
  140. if (pgd_none(*pgd)) {
  141. pud_t *pud = (pud_t *)spp_getpage();
  142. pgd_populate(&init_mm, pgd, pud);
  143. if (pud != pud_offset(pgd, 0))
  144. printk(KERN_ERR "PAGETABLE BUG #00! %p <-> %p\n",
  145. pud, pud_offset(pgd, 0));
  146. }
  147. return pud_offset(pgd, vaddr);
  148. }
  149. static pmd_t *fill_pmd(pud_t *pud, unsigned long vaddr)
  150. {
  151. if (pud_none(*pud)) {
  152. pmd_t *pmd = (pmd_t *) spp_getpage();
  153. pud_populate(&init_mm, pud, pmd);
  154. if (pmd != pmd_offset(pud, 0))
  155. printk(KERN_ERR "PAGETABLE BUG #01! %p <-> %p\n",
  156. pmd, pmd_offset(pud, 0));
  157. }
  158. return pmd_offset(pud, vaddr);
  159. }
  160. static pte_t *fill_pte(pmd_t *pmd, unsigned long vaddr)
  161. {
  162. if (pmd_none(*pmd)) {
  163. pte_t *pte = (pte_t *) spp_getpage();
  164. pmd_populate_kernel(&init_mm, pmd, pte);
  165. if (pte != pte_offset_kernel(pmd, 0))
  166. printk(KERN_ERR "PAGETABLE BUG #02!\n");
  167. }
  168. return pte_offset_kernel(pmd, vaddr);
  169. }
  170. void set_pte_vaddr_pud(pud_t *pud_page, unsigned long vaddr, pte_t new_pte)
  171. {
  172. pud_t *pud;
  173. pmd_t *pmd;
  174. pte_t *pte;
  175. pud = pud_page + pud_index(vaddr);
  176. pmd = fill_pmd(pud, vaddr);
  177. pte = fill_pte(pmd, vaddr);
  178. set_pte(pte, new_pte);
  179. /*
  180. * It's enough to flush this one mapping.
  181. * (PGE mappings get flushed as well)
  182. */
  183. __flush_tlb_one(vaddr);
  184. }
  185. void set_pte_vaddr(unsigned long vaddr, pte_t pteval)
  186. {
  187. pgd_t *pgd;
  188. pud_t *pud_page;
  189. pr_debug("set_pte_vaddr %lx to %lx\n", vaddr, native_pte_val(pteval));
  190. pgd = pgd_offset_k(vaddr);
  191. if (pgd_none(*pgd)) {
  192. printk(KERN_ERR
  193. "PGD FIXMAP MISSING, it should be setup in head.S!\n");
  194. return;
  195. }
  196. pud_page = (pud_t*)pgd_page_vaddr(*pgd);
  197. set_pte_vaddr_pud(pud_page, vaddr, pteval);
  198. }
  199. pmd_t * __init populate_extra_pmd(unsigned long vaddr)
  200. {
  201. pgd_t *pgd;
  202. pud_t *pud;
  203. pgd = pgd_offset_k(vaddr);
  204. pud = fill_pud(pgd, vaddr);
  205. return fill_pmd(pud, vaddr);
  206. }
  207. pte_t * __init populate_extra_pte(unsigned long vaddr)
  208. {
  209. pmd_t *pmd;
  210. pmd = populate_extra_pmd(vaddr);
  211. return fill_pte(pmd, vaddr);
  212. }
  213. /*
  214. * Create large page table mappings for a range of physical addresses.
  215. */
  216. static void __init __init_extra_mapping(unsigned long phys, unsigned long size,
  217. pgprot_t prot)
  218. {
  219. pgd_t *pgd;
  220. pud_t *pud;
  221. pmd_t *pmd;
  222. BUG_ON((phys & ~PMD_MASK) || (size & ~PMD_MASK));
  223. for (; size; phys += PMD_SIZE, size -= PMD_SIZE) {
  224. pgd = pgd_offset_k((unsigned long)__va(phys));
  225. if (pgd_none(*pgd)) {
  226. pud = (pud_t *) spp_getpage();
  227. set_pgd(pgd, __pgd(__pa(pud) | _KERNPG_TABLE |
  228. _PAGE_USER));
  229. }
  230. pud = pud_offset(pgd, (unsigned long)__va(phys));
  231. if (pud_none(*pud)) {
  232. pmd = (pmd_t *) spp_getpage();
  233. set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE |
  234. _PAGE_USER));
  235. }
  236. pmd = pmd_offset(pud, phys);
  237. BUG_ON(!pmd_none(*pmd));
  238. set_pmd(pmd, __pmd(phys | pgprot_val(prot)));
  239. }
  240. }
  241. void __init init_extra_mapping_wb(unsigned long phys, unsigned long size)
  242. {
  243. __init_extra_mapping(phys, size, PAGE_KERNEL_LARGE);
  244. }
  245. void __init init_extra_mapping_uc(unsigned long phys, unsigned long size)
  246. {
  247. __init_extra_mapping(phys, size, PAGE_KERNEL_LARGE_NOCACHE);
  248. }
  249. /*
  250. * The head.S code sets up the kernel high mapping:
  251. *
  252. * from __START_KERNEL_map to __START_KERNEL_map + size (== _end-_text)
  253. *
  254. * phys_addr holds the negative offset to the kernel, which is added
  255. * to the compile time generated pmds. This results in invalid pmds up
  256. * to the point where we hit the physaddr 0 mapping.
  257. *
  258. * We limit the mappings to the region from _text to _end. _end is
  259. * rounded up to the 2MB boundary. This catches the invalid pmds as
  260. * well, as they are located before _text:
  261. */
  262. void __init cleanup_highmap(void)
  263. {
  264. unsigned long vaddr = __START_KERNEL_map;
  265. unsigned long end = roundup((unsigned long)_end, PMD_SIZE) - 1;
  266. pmd_t *pmd = level2_kernel_pgt;
  267. pmd_t *last_pmd = pmd + PTRS_PER_PMD;
  268. for (; pmd < last_pmd; pmd++, vaddr += PMD_SIZE) {
  269. if (pmd_none(*pmd))
  270. continue;
  271. if (vaddr < (unsigned long) _text || vaddr > end)
  272. set_pmd(pmd, __pmd(0));
  273. }
  274. }
  275. static __ref void *alloc_low_page(unsigned long *phys)
  276. {
  277. unsigned long pfn = e820_table_end++;
  278. void *adr;
  279. if (after_bootmem) {
  280. adr = (void *)get_zeroed_page(GFP_ATOMIC | __GFP_NOTRACK);
  281. *phys = __pa(adr);
  282. return adr;
  283. }
  284. if (pfn >= e820_table_top)
  285. panic("alloc_low_page: ran out of memory");
  286. adr = early_memremap(pfn * PAGE_SIZE, PAGE_SIZE);
  287. clear_page(adr);
  288. *phys = pfn * PAGE_SIZE;
  289. return adr;
  290. }
  291. static __ref void unmap_low_page(void *adr)
  292. {
  293. if (after_bootmem)
  294. return;
  295. early_iounmap(adr, PAGE_SIZE);
  296. }
  297. static unsigned long __meminit
  298. phys_pte_init(pte_t *pte_page, unsigned long addr, unsigned long end,
  299. pgprot_t prot)
  300. {
  301. unsigned pages = 0;
  302. unsigned long last_map_addr = end;
  303. int i;
  304. pte_t *pte = pte_page + pte_index(addr);
  305. for(i = pte_index(addr); i < PTRS_PER_PTE; i++, addr += PAGE_SIZE, pte++) {
  306. if (addr >= end) {
  307. if (!after_bootmem) {
  308. for(; i < PTRS_PER_PTE; i++, pte++)
  309. set_pte(pte, __pte(0));
  310. }
  311. break;
  312. }
  313. /*
  314. * We will re-use the existing mapping.
  315. * Xen for example has some special requirements, like mapping
  316. * pagetable pages as RO. So assume someone who pre-setup
  317. * these mappings are more intelligent.
  318. */
  319. if (pte_val(*pte)) {
  320. pages++;
  321. continue;
  322. }
  323. if (0)
  324. printk(" pte=%p addr=%lx pte=%016lx\n",
  325. pte, addr, pfn_pte(addr >> PAGE_SHIFT, PAGE_KERNEL).pte);
  326. pages++;
  327. set_pte(pte, pfn_pte(addr >> PAGE_SHIFT, prot));
  328. last_map_addr = (addr & PAGE_MASK) + PAGE_SIZE;
  329. }
  330. update_page_count(PG_LEVEL_4K, pages);
  331. return last_map_addr;
  332. }
  333. static unsigned long __meminit
  334. phys_pte_update(pmd_t *pmd, unsigned long address, unsigned long end,
  335. pgprot_t prot)
  336. {
  337. pte_t *pte = (pte_t *)pmd_page_vaddr(*pmd);
  338. return phys_pte_init(pte, address, end, prot);
  339. }
  340. static unsigned long __meminit
  341. phys_pmd_init(pmd_t *pmd_page, unsigned long address, unsigned long end,
  342. unsigned long page_size_mask, pgprot_t prot)
  343. {
  344. unsigned long pages = 0;
  345. unsigned long last_map_addr = end;
  346. int i = pmd_index(address);
  347. for (; i < PTRS_PER_PMD; i++, address += PMD_SIZE) {
  348. unsigned long pte_phys;
  349. pmd_t *pmd = pmd_page + pmd_index(address);
  350. pte_t *pte;
  351. pgprot_t new_prot = prot;
  352. if (address >= end) {
  353. if (!after_bootmem) {
  354. for (; i < PTRS_PER_PMD; i++, pmd++)
  355. set_pmd(pmd, __pmd(0));
  356. }
  357. break;
  358. }
  359. if (pmd_val(*pmd)) {
  360. if (!pmd_large(*pmd)) {
  361. spin_lock(&init_mm.page_table_lock);
  362. last_map_addr = phys_pte_update(pmd, address,
  363. end, prot);
  364. spin_unlock(&init_mm.page_table_lock);
  365. continue;
  366. }
  367. /*
  368. * If we are ok with PG_LEVEL_2M mapping, then we will
  369. * use the existing mapping,
  370. *
  371. * Otherwise, we will split the large page mapping but
  372. * use the same existing protection bits except for
  373. * large page, so that we don't violate Intel's TLB
  374. * Application note (317080) which says, while changing
  375. * the page sizes, new and old translations should
  376. * not differ with respect to page frame and
  377. * attributes.
  378. */
  379. if (page_size_mask & (1 << PG_LEVEL_2M)) {
  380. pages++;
  381. continue;
  382. }
  383. new_prot = pte_pgprot(pte_clrhuge(*(pte_t *)pmd));
  384. }
  385. if (page_size_mask & (1<<PG_LEVEL_2M)) {
  386. pages++;
  387. spin_lock(&init_mm.page_table_lock);
  388. set_pte((pte_t *)pmd,
  389. pfn_pte(address >> PAGE_SHIFT,
  390. __pgprot(pgprot_val(prot) | _PAGE_PSE)));
  391. spin_unlock(&init_mm.page_table_lock);
  392. last_map_addr = (address & PMD_MASK) + PMD_SIZE;
  393. continue;
  394. }
  395. pte = alloc_low_page(&pte_phys);
  396. last_map_addr = phys_pte_init(pte, address, end, new_prot);
  397. unmap_low_page(pte);
  398. spin_lock(&init_mm.page_table_lock);
  399. pmd_populate_kernel(&init_mm, pmd, __va(pte_phys));
  400. spin_unlock(&init_mm.page_table_lock);
  401. }
  402. update_page_count(PG_LEVEL_2M, pages);
  403. return last_map_addr;
  404. }
  405. static unsigned long __meminit
  406. phys_pmd_update(pud_t *pud, unsigned long address, unsigned long end,
  407. unsigned long page_size_mask, pgprot_t prot)
  408. {
  409. pmd_t *pmd = pmd_offset(pud, 0);
  410. unsigned long last_map_addr;
  411. last_map_addr = phys_pmd_init(pmd, address, end, page_size_mask, prot);
  412. __flush_tlb_all();
  413. return last_map_addr;
  414. }
  415. static unsigned long __meminit
  416. phys_pud_init(pud_t *pud_page, unsigned long addr, unsigned long end,
  417. unsigned long page_size_mask)
  418. {
  419. unsigned long pages = 0;
  420. unsigned long last_map_addr = end;
  421. int i = pud_index(addr);
  422. for (; i < PTRS_PER_PUD; i++, addr = (addr & PUD_MASK) + PUD_SIZE) {
  423. unsigned long pmd_phys;
  424. pud_t *pud = pud_page + pud_index(addr);
  425. pmd_t *pmd;
  426. pgprot_t prot = PAGE_KERNEL;
  427. if (addr >= end)
  428. break;
  429. if (!after_bootmem &&
  430. !e820_any_mapped(addr, addr+PUD_SIZE, 0)) {
  431. set_pud(pud, __pud(0));
  432. continue;
  433. }
  434. if (pud_val(*pud)) {
  435. if (!pud_large(*pud)) {
  436. last_map_addr = phys_pmd_update(pud, addr, end,
  437. page_size_mask, prot);
  438. continue;
  439. }
  440. /*
  441. * If we are ok with PG_LEVEL_1G mapping, then we will
  442. * use the existing mapping.
  443. *
  444. * Otherwise, we will split the gbpage mapping but use
  445. * the same existing protection bits except for large
  446. * page, so that we don't violate Intel's TLB
  447. * Application note (317080) which says, while changing
  448. * the page sizes, new and old translations should
  449. * not differ with respect to page frame and
  450. * attributes.
  451. */
  452. if (page_size_mask & (1 << PG_LEVEL_1G)) {
  453. pages++;
  454. continue;
  455. }
  456. prot = pte_pgprot(pte_clrhuge(*(pte_t *)pud));
  457. }
  458. if (page_size_mask & (1<<PG_LEVEL_1G)) {
  459. pages++;
  460. spin_lock(&init_mm.page_table_lock);
  461. set_pte((pte_t *)pud,
  462. pfn_pte(addr >> PAGE_SHIFT, PAGE_KERNEL_LARGE));
  463. spin_unlock(&init_mm.page_table_lock);
  464. last_map_addr = (addr & PUD_MASK) + PUD_SIZE;
  465. continue;
  466. }
  467. pmd = alloc_low_page(&pmd_phys);
  468. last_map_addr = phys_pmd_init(pmd, addr, end, page_size_mask,
  469. prot);
  470. unmap_low_page(pmd);
  471. spin_lock(&init_mm.page_table_lock);
  472. pud_populate(&init_mm, pud, __va(pmd_phys));
  473. spin_unlock(&init_mm.page_table_lock);
  474. }
  475. __flush_tlb_all();
  476. update_page_count(PG_LEVEL_1G, pages);
  477. return last_map_addr;
  478. }
  479. static unsigned long __meminit
  480. phys_pud_update(pgd_t *pgd, unsigned long addr, unsigned long end,
  481. unsigned long page_size_mask)
  482. {
  483. pud_t *pud;
  484. pud = (pud_t *)pgd_page_vaddr(*pgd);
  485. return phys_pud_init(pud, addr, end, page_size_mask);
  486. }
  487. unsigned long __meminit
  488. kernel_physical_mapping_init(unsigned long start,
  489. unsigned long end,
  490. unsigned long page_size_mask)
  491. {
  492. bool pgd_changed = false;
  493. unsigned long next, last_map_addr = end;
  494. unsigned long addr;
  495. start = (unsigned long)__va(start);
  496. end = (unsigned long)__va(end);
  497. addr = start;
  498. for (; start < end; start = next) {
  499. pgd_t *pgd = pgd_offset_k(start);
  500. unsigned long pud_phys;
  501. pud_t *pud;
  502. next = (start + PGDIR_SIZE) & PGDIR_MASK;
  503. if (next > end)
  504. next = end;
  505. if (pgd_val(*pgd)) {
  506. last_map_addr = phys_pud_update(pgd, __pa(start),
  507. __pa(end), page_size_mask);
  508. continue;
  509. }
  510. pud = alloc_low_page(&pud_phys);
  511. last_map_addr = phys_pud_init(pud, __pa(start), __pa(next),
  512. page_size_mask);
  513. unmap_low_page(pud);
  514. spin_lock(&init_mm.page_table_lock);
  515. pgd_populate(&init_mm, pgd, __va(pud_phys));
  516. spin_unlock(&init_mm.page_table_lock);
  517. pgd_changed = true;
  518. }
  519. if (pgd_changed)
  520. sync_global_pgds(addr, end);
  521. __flush_tlb_all();
  522. return last_map_addr;
  523. }
  524. #ifndef CONFIG_NUMA
  525. void __init initmem_init(unsigned long start_pfn, unsigned long end_pfn,
  526. int acpi, int k8)
  527. {
  528. memblock_x86_register_active_regions(0, start_pfn, end_pfn);
  529. }
  530. #endif
  531. void __init paging_init(void)
  532. {
  533. unsigned long max_zone_pfns[MAX_NR_ZONES];
  534. memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
  535. max_zone_pfns[ZONE_DMA] = MAX_DMA_PFN;
  536. max_zone_pfns[ZONE_DMA32] = MAX_DMA32_PFN;
  537. max_zone_pfns[ZONE_NORMAL] = max_pfn;
  538. sparse_memory_present_with_active_regions(MAX_NUMNODES);
  539. sparse_init();
  540. /*
  541. * clear the default setting with node 0
  542. * note: don't use nodes_clear here, that is really clearing when
  543. * numa support is not compiled in, and later node_set_state
  544. * will not set it back.
  545. */
  546. node_clear_state(0, N_NORMAL_MEMORY);
  547. free_area_init_nodes(max_zone_pfns);
  548. }
  549. /*
  550. * Memory hotplug specific functions
  551. */
  552. #ifdef CONFIG_MEMORY_HOTPLUG
  553. /*
  554. * After memory hotplug the variables max_pfn, max_low_pfn and high_memory need
  555. * updating.
  556. */
  557. static void update_end_of_memory_vars(u64 start, u64 size)
  558. {
  559. unsigned long end_pfn = PFN_UP(start + size);
  560. if (end_pfn > max_pfn) {
  561. max_pfn = end_pfn;
  562. max_low_pfn = end_pfn;
  563. high_memory = (void *)__va(max_pfn * PAGE_SIZE - 1) + 1;
  564. }
  565. }
  566. /*
  567. * Memory is added always to NORMAL zone. This means you will never get
  568. * additional DMA/DMA32 memory.
  569. */
  570. int arch_add_memory(int nid, u64 start, u64 size)
  571. {
  572. struct pglist_data *pgdat = NODE_DATA(nid);
  573. struct zone *zone = pgdat->node_zones + ZONE_NORMAL;
  574. unsigned long last_mapped_pfn, start_pfn = start >> PAGE_SHIFT;
  575. unsigned long nr_pages = size >> PAGE_SHIFT;
  576. int ret;
  577. last_mapped_pfn = init_memory_mapping(start, start + size);
  578. if (last_mapped_pfn > max_pfn_mapped)
  579. max_pfn_mapped = last_mapped_pfn;
  580. ret = __add_pages(nid, zone, start_pfn, nr_pages);
  581. WARN_ON_ONCE(ret);
  582. /* update max_pfn, max_low_pfn and high_memory */
  583. update_end_of_memory_vars(start, size);
  584. return ret;
  585. }
  586. EXPORT_SYMBOL_GPL(arch_add_memory);
  587. #if !defined(CONFIG_ACPI_NUMA) && defined(CONFIG_NUMA)
  588. int memory_add_physaddr_to_nid(u64 start)
  589. {
  590. return 0;
  591. }
  592. EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid);
  593. #endif
  594. #endif /* CONFIG_MEMORY_HOTPLUG */
  595. static struct kcore_list kcore_vsyscall;
  596. void __init mem_init(void)
  597. {
  598. long codesize, reservedpages, datasize, initsize;
  599. unsigned long absent_pages;
  600. pci_iommu_alloc();
  601. /* clear_bss() already clear the empty_zero_page */
  602. reservedpages = 0;
  603. /* this will put all low memory onto the freelists */
  604. #ifdef CONFIG_NUMA
  605. totalram_pages = numa_free_all_bootmem();
  606. #else
  607. totalram_pages = free_all_bootmem();
  608. #endif
  609. absent_pages = absent_pages_in_range(0, max_pfn);
  610. reservedpages = max_pfn - totalram_pages - absent_pages;
  611. after_bootmem = 1;
  612. codesize = (unsigned long) &_etext - (unsigned long) &_text;
  613. datasize = (unsigned long) &_edata - (unsigned long) &_etext;
  614. initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
  615. /* Register memory areas for /proc/kcore */
  616. kclist_add(&kcore_vsyscall, (void *)VSYSCALL_START,
  617. VSYSCALL_END - VSYSCALL_START, KCORE_OTHER);
  618. printk(KERN_INFO "Memory: %luk/%luk available (%ldk kernel code, "
  619. "%ldk absent, %ldk reserved, %ldk data, %ldk init)\n",
  620. nr_free_pages() << (PAGE_SHIFT-10),
  621. max_pfn << (PAGE_SHIFT-10),
  622. codesize >> 10,
  623. absent_pages << (PAGE_SHIFT-10),
  624. reservedpages << (PAGE_SHIFT-10),
  625. datasize >> 10,
  626. initsize >> 10);
  627. }
  628. #ifdef CONFIG_DEBUG_RODATA
  629. const int rodata_test_data = 0xC3;
  630. EXPORT_SYMBOL_GPL(rodata_test_data);
  631. int kernel_set_to_readonly;
  632. void set_kernel_text_rw(void)
  633. {
  634. unsigned long start = PFN_ALIGN(_text);
  635. unsigned long end = PFN_ALIGN(__stop___ex_table);
  636. if (!kernel_set_to_readonly)
  637. return;
  638. pr_debug("Set kernel text: %lx - %lx for read write\n",
  639. start, end);
  640. /*
  641. * Make the kernel identity mapping for text RW. Kernel text
  642. * mapping will always be RO. Refer to the comment in
  643. * static_protections() in pageattr.c
  644. */
  645. set_memory_rw(start, (end - start) >> PAGE_SHIFT);
  646. }
  647. void set_kernel_text_ro(void)
  648. {
  649. unsigned long start = PFN_ALIGN(_text);
  650. unsigned long end = PFN_ALIGN(__stop___ex_table);
  651. if (!kernel_set_to_readonly)
  652. return;
  653. pr_debug("Set kernel text: %lx - %lx for read only\n",
  654. start, end);
  655. /*
  656. * Set the kernel identity mapping for text RO.
  657. */
  658. set_memory_ro(start, (end - start) >> PAGE_SHIFT);
  659. }
  660. void mark_rodata_ro(void)
  661. {
  662. unsigned long start = PFN_ALIGN(_text);
  663. unsigned long rodata_start =
  664. ((unsigned long)__start_rodata + PAGE_SIZE - 1) & PAGE_MASK;
  665. unsigned long end = (unsigned long) &__end_rodata_hpage_align;
  666. unsigned long text_end = PAGE_ALIGN((unsigned long) &__stop___ex_table);
  667. unsigned long rodata_end = PAGE_ALIGN((unsigned long) &__end_rodata);
  668. unsigned long data_start = (unsigned long) &_sdata;
  669. printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n",
  670. (end - start) >> 10);
  671. set_memory_ro(start, (end - start) >> PAGE_SHIFT);
  672. kernel_set_to_readonly = 1;
  673. /*
  674. * The rodata section (but not the kernel text!) should also be
  675. * not-executable.
  676. */
  677. set_memory_nx(rodata_start, (end - rodata_start) >> PAGE_SHIFT);
  678. rodata_test();
  679. #ifdef CONFIG_CPA_DEBUG
  680. printk(KERN_INFO "Testing CPA: undo %lx-%lx\n", start, end);
  681. set_memory_rw(start, (end-start) >> PAGE_SHIFT);
  682. printk(KERN_INFO "Testing CPA: again\n");
  683. set_memory_ro(start, (end-start) >> PAGE_SHIFT);
  684. #endif
  685. free_init_pages("unused kernel memory",
  686. (unsigned long) page_address(virt_to_page(text_end)),
  687. (unsigned long)
  688. page_address(virt_to_page(rodata_start)));
  689. free_init_pages("unused kernel memory",
  690. (unsigned long) page_address(virt_to_page(rodata_end)),
  691. (unsigned long) page_address(virt_to_page(data_start)));
  692. }
  693. #endif
  694. int kern_addr_valid(unsigned long addr)
  695. {
  696. unsigned long above = ((long)addr) >> __VIRTUAL_MASK_SHIFT;
  697. pgd_t *pgd;
  698. pud_t *pud;
  699. pmd_t *pmd;
  700. pte_t *pte;
  701. if (above != 0 && above != -1UL)
  702. return 0;
  703. pgd = pgd_offset_k(addr);
  704. if (pgd_none(*pgd))
  705. return 0;
  706. pud = pud_offset(pgd, addr);
  707. if (pud_none(*pud))
  708. return 0;
  709. pmd = pmd_offset(pud, addr);
  710. if (pmd_none(*pmd))
  711. return 0;
  712. if (pmd_large(*pmd))
  713. return pfn_valid(pmd_pfn(*pmd));
  714. pte = pte_offset_kernel(pmd, addr);
  715. if (pte_none(*pte))
  716. return 0;
  717. return pfn_valid(pte_pfn(*pte));
  718. }
  719. /*
  720. * A pseudo VMA to allow ptrace access for the vsyscall page. This only
  721. * covers the 64bit vsyscall page now. 32bit has a real VMA now and does
  722. * not need special handling anymore:
  723. */
  724. static struct vm_area_struct gate_vma = {
  725. .vm_start = VSYSCALL_START,
  726. .vm_end = VSYSCALL_START + (VSYSCALL_MAPPED_PAGES * PAGE_SIZE),
  727. .vm_page_prot = PAGE_READONLY_EXEC,
  728. .vm_flags = VM_READ | VM_EXEC
  729. };
  730. struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
  731. {
  732. #ifdef CONFIG_IA32_EMULATION
  733. if (test_tsk_thread_flag(tsk, TIF_IA32))
  734. return NULL;
  735. #endif
  736. return &gate_vma;
  737. }
  738. int in_gate_area(struct task_struct *task, unsigned long addr)
  739. {
  740. struct vm_area_struct *vma = get_gate_vma(task);
  741. if (!vma)
  742. return 0;
  743. return (addr >= vma->vm_start) && (addr < vma->vm_end);
  744. }
  745. /*
  746. * Use this when you have no reliable task/vma, typically from interrupt
  747. * context. It is less reliable than using the task's vma and may give
  748. * false positives:
  749. */
  750. int in_gate_area_no_task(unsigned long addr)
  751. {
  752. return (addr >= VSYSCALL_START) && (addr < VSYSCALL_END);
  753. }
  754. const char *arch_vma_name(struct vm_area_struct *vma)
  755. {
  756. if (vma->vm_mm && vma->vm_start == (long)vma->vm_mm->context.vdso)
  757. return "[vdso]";
  758. if (vma == &gate_vma)
  759. return "[vsyscall]";
  760. return NULL;
  761. }
  762. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  763. /*
  764. * Initialise the sparsemem vmemmap using huge-pages at the PMD level.
  765. */
  766. static long __meminitdata addr_start, addr_end;
  767. static void __meminitdata *p_start, *p_end;
  768. static int __meminitdata node_start;
  769. int __meminit
  770. vmemmap_populate(struct page *start_page, unsigned long size, int node)
  771. {
  772. unsigned long addr = (unsigned long)start_page;
  773. unsigned long end = (unsigned long)(start_page + size);
  774. unsigned long next;
  775. pgd_t *pgd;
  776. pud_t *pud;
  777. pmd_t *pmd;
  778. for (; addr < end; addr = next) {
  779. void *p = NULL;
  780. pgd = vmemmap_pgd_populate(addr, node);
  781. if (!pgd)
  782. return -ENOMEM;
  783. pud = vmemmap_pud_populate(pgd, addr, node);
  784. if (!pud)
  785. return -ENOMEM;
  786. if (!cpu_has_pse) {
  787. next = (addr + PAGE_SIZE) & PAGE_MASK;
  788. pmd = vmemmap_pmd_populate(pud, addr, node);
  789. if (!pmd)
  790. return -ENOMEM;
  791. p = vmemmap_pte_populate(pmd, addr, node);
  792. if (!p)
  793. return -ENOMEM;
  794. addr_end = addr + PAGE_SIZE;
  795. p_end = p + PAGE_SIZE;
  796. } else {
  797. next = pmd_addr_end(addr, end);
  798. pmd = pmd_offset(pud, addr);
  799. if (pmd_none(*pmd)) {
  800. pte_t entry;
  801. p = vmemmap_alloc_block_buf(PMD_SIZE, node);
  802. if (!p)
  803. return -ENOMEM;
  804. entry = pfn_pte(__pa(p) >> PAGE_SHIFT,
  805. PAGE_KERNEL_LARGE);
  806. set_pmd(pmd, __pmd(pte_val(entry)));
  807. /* check to see if we have contiguous blocks */
  808. if (p_end != p || node_start != node) {
  809. if (p_start)
  810. printk(KERN_DEBUG " [%lx-%lx] PMD -> [%p-%p] on node %d\n",
  811. addr_start, addr_end-1, p_start, p_end-1, node_start);
  812. addr_start = addr;
  813. node_start = node;
  814. p_start = p;
  815. }
  816. addr_end = addr + PMD_SIZE;
  817. p_end = p + PMD_SIZE;
  818. } else
  819. vmemmap_verify((pte_t *)pmd, node, addr, next);
  820. }
  821. }
  822. sync_global_pgds((unsigned long)start_page, end);
  823. return 0;
  824. }
  825. void __meminit vmemmap_populate_print_last(void)
  826. {
  827. if (p_start) {
  828. printk(KERN_DEBUG " [%lx-%lx] PMD -> [%p-%p] on node %d\n",
  829. addr_start, addr_end-1, p_start, p_end-1, node_start);
  830. p_start = NULL;
  831. p_end = NULL;
  832. node_start = 0;
  833. }
  834. }
  835. #endif