init_64.c 24 KB

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