init_64.c 23 KB

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