init_64.c 23 KB

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