init_64.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  1. /*
  2. * linux/arch/x86_64/mm/init.c
  3. *
  4. * Copyright (C) 1995 Linus Torvalds
  5. * Copyright (C) 2000 Pavel Machek <pavel@suse.cz>
  6. * Copyright (C) 2002,2003 Andi Kleen <ak@suse.de>
  7. */
  8. #include <linux/signal.h>
  9. #include <linux/sched.h>
  10. #include <linux/kernel.h>
  11. #include <linux/errno.h>
  12. #include <linux/string.h>
  13. #include <linux/types.h>
  14. #include <linux/ptrace.h>
  15. #include <linux/mman.h>
  16. #include <linux/mm.h>
  17. #include <linux/swap.h>
  18. #include <linux/smp.h>
  19. #include <linux/init.h>
  20. #include <linux/initrd.h>
  21. #include <linux/pagemap.h>
  22. #include <linux/bootmem.h>
  23. #include <linux/proc_fs.h>
  24. #include <linux/pci.h>
  25. #include <linux/pfn.h>
  26. #include <linux/poison.h>
  27. #include <linux/dma-mapping.h>
  28. #include <linux/module.h>
  29. #include <linux/memory_hotplug.h>
  30. #include <linux/nmi.h>
  31. #include <asm/processor.h>
  32. #include <asm/system.h>
  33. #include <asm/uaccess.h>
  34. #include <asm/pgtable.h>
  35. #include <asm/pgalloc.h>
  36. #include <asm/dma.h>
  37. #include <asm/fixmap.h>
  38. #include <asm/e820.h>
  39. #include <asm/apic.h>
  40. #include <asm/tlb.h>
  41. #include <asm/mmu_context.h>
  42. #include <asm/proto.h>
  43. #include <asm/smp.h>
  44. #include <asm/sections.h>
  45. #include <asm/kdebug.h>
  46. #include <asm/numa.h>
  47. #include <asm/cacheflush.h>
  48. /*
  49. * PFN of last memory page.
  50. */
  51. unsigned long end_pfn;
  52. /*
  53. * end_pfn only includes RAM, while max_pfn_mapped includes all e820 entries.
  54. * The direct mapping extends to max_pfn_mapped, so that we can directly access
  55. * apertures, ACPI and other tables without having to play with fixmaps.
  56. */
  57. unsigned long max_pfn_mapped;
  58. static unsigned long dma_reserve __initdata;
  59. DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
  60. int direct_gbpages __meminitdata
  61. #ifdef CONFIG_DIRECT_GBPAGES
  62. = 1
  63. #endif
  64. ;
  65. static int __init parse_direct_gbpages_off(char *arg)
  66. {
  67. direct_gbpages = 0;
  68. return 0;
  69. }
  70. early_param("nogbpages", parse_direct_gbpages_off);
  71. static int __init parse_direct_gbpages_on(char *arg)
  72. {
  73. direct_gbpages = 1;
  74. return 0;
  75. }
  76. early_param("gbpages", parse_direct_gbpages_on);
  77. /*
  78. * NOTE: pagetable_init alloc all the fixmap pagetables contiguous on the
  79. * physical space so we can cache the place of the first one and move
  80. * around without checking the pgd every time.
  81. */
  82. void show_mem(void)
  83. {
  84. long i, total = 0, reserved = 0;
  85. long shared = 0, cached = 0;
  86. struct page *page;
  87. pg_data_t *pgdat;
  88. printk(KERN_INFO "Mem-info:\n");
  89. show_free_areas();
  90. for_each_online_pgdat(pgdat) {
  91. for (i = 0; i < pgdat->node_spanned_pages; ++i) {
  92. /*
  93. * This loop can take a while with 256 GB and
  94. * 4k pages so defer the NMI watchdog:
  95. */
  96. if (unlikely(i % MAX_ORDER_NR_PAGES == 0))
  97. touch_nmi_watchdog();
  98. if (!pfn_valid(pgdat->node_start_pfn + i))
  99. continue;
  100. page = pfn_to_page(pgdat->node_start_pfn + i);
  101. total++;
  102. if (PageReserved(page))
  103. reserved++;
  104. else if (PageSwapCache(page))
  105. cached++;
  106. else if (page_count(page))
  107. shared += page_count(page) - 1;
  108. }
  109. }
  110. printk(KERN_INFO "%lu pages of RAM\n", total);
  111. printk(KERN_INFO "%lu reserved pages\n", reserved);
  112. printk(KERN_INFO "%lu pages shared\n", shared);
  113. printk(KERN_INFO "%lu pages swap cached\n", cached);
  114. }
  115. int after_bootmem;
  116. static __init void *spp_getpage(void)
  117. {
  118. void *ptr;
  119. if (after_bootmem)
  120. ptr = (void *) get_zeroed_page(GFP_ATOMIC);
  121. else
  122. ptr = alloc_bootmem_pages(PAGE_SIZE);
  123. if (!ptr || ((unsigned long)ptr & ~PAGE_MASK)) {
  124. panic("set_pte_phys: cannot allocate page data %s\n",
  125. after_bootmem ? "after bootmem" : "");
  126. }
  127. pr_debug("spp_getpage %p\n", ptr);
  128. return ptr;
  129. }
  130. void
  131. set_pte_vaddr(unsigned long vaddr, pte_t new_pte)
  132. {
  133. pgd_t *pgd;
  134. pud_t *pud;
  135. pmd_t *pmd;
  136. pte_t *pte;
  137. pr_debug("set_pte_vaddr %lx to %lx\n", vaddr, native_pte_val(new_pte));
  138. pgd = pgd_offset_k(vaddr);
  139. if (pgd_none(*pgd)) {
  140. printk(KERN_ERR
  141. "PGD FIXMAP MISSING, it should be setup in head.S!\n");
  142. return;
  143. }
  144. pud = pud_offset(pgd, vaddr);
  145. if (pud_none(*pud)) {
  146. pmd = (pmd_t *) spp_getpage();
  147. pud_populate(&init_mm, pud, pmd);
  148. if (pmd != pmd_offset(pud, 0)) {
  149. printk(KERN_ERR "PAGETABLE BUG #01! %p <-> %p\n",
  150. pmd, pmd_offset(pud, 0));
  151. return;
  152. }
  153. }
  154. pmd = pmd_offset(pud, vaddr);
  155. if (pmd_none(*pmd)) {
  156. pte = (pte_t *) spp_getpage();
  157. pmd_populate_kernel(&init_mm, pmd, pte);
  158. if (pte != pte_offset_kernel(pmd, 0)) {
  159. printk(KERN_ERR "PAGETABLE BUG #02!\n");
  160. return;
  161. }
  162. }
  163. pte = pte_offset_kernel(pmd, vaddr);
  164. if (!pte_none(*pte) && pte_val(new_pte) &&
  165. pte_val(*pte) != (pte_val(new_pte) & __supported_pte_mask))
  166. pte_ERROR(*pte);
  167. set_pte(pte, new_pte);
  168. /*
  169. * It's enough to flush this one mapping.
  170. * (PGE mappings get flushed as well)
  171. */
  172. __flush_tlb_one(vaddr);
  173. }
  174. /*
  175. * The head.S code sets up the kernel high mapping:
  176. *
  177. * from __START_KERNEL_map to __START_KERNEL_map + size (== _end-_text)
  178. *
  179. * phys_addr holds the negative offset to the kernel, which is added
  180. * to the compile time generated pmds. This results in invalid pmds up
  181. * to the point where we hit the physaddr 0 mapping.
  182. *
  183. * We limit the mappings to the region from _text to _end. _end is
  184. * rounded up to the 2MB boundary. This catches the invalid pmds as
  185. * well, as they are located before _text:
  186. */
  187. void __init cleanup_highmap(void)
  188. {
  189. unsigned long vaddr = __START_KERNEL_map;
  190. unsigned long end = round_up((unsigned long)_end, PMD_SIZE) - 1;
  191. pmd_t *pmd = level2_kernel_pgt;
  192. pmd_t *last_pmd = pmd + PTRS_PER_PMD;
  193. for (; pmd < last_pmd; pmd++, vaddr += PMD_SIZE) {
  194. if (pmd_none(*pmd))
  195. continue;
  196. if (vaddr < (unsigned long) _text || vaddr > end)
  197. set_pmd(pmd, __pmd(0));
  198. }
  199. }
  200. static unsigned long __initdata table_start;
  201. static unsigned long __meminitdata table_end;
  202. static __meminit void *alloc_low_page(unsigned long *phys)
  203. {
  204. unsigned long pfn = table_end++;
  205. void *adr;
  206. if (after_bootmem) {
  207. adr = (void *)get_zeroed_page(GFP_ATOMIC);
  208. *phys = __pa(adr);
  209. return adr;
  210. }
  211. if (pfn >= end_pfn)
  212. panic("alloc_low_page: ran out of memory");
  213. adr = early_ioremap(pfn * PAGE_SIZE, PAGE_SIZE);
  214. memset(adr, 0, PAGE_SIZE);
  215. *phys = pfn * PAGE_SIZE;
  216. return adr;
  217. }
  218. static __meminit void unmap_low_page(void *adr)
  219. {
  220. if (after_bootmem)
  221. return;
  222. early_iounmap(adr, PAGE_SIZE);
  223. }
  224. static unsigned long __meminit
  225. phys_pmd_init(pmd_t *pmd_page, unsigned long address, unsigned long end)
  226. {
  227. unsigned long pages = 0;
  228. int i = pmd_index(address);
  229. for (; i < PTRS_PER_PMD; i++, address += PMD_SIZE) {
  230. pmd_t *pmd = pmd_page + pmd_index(address);
  231. if (address >= end) {
  232. if (!after_bootmem) {
  233. for (; i < PTRS_PER_PMD; i++, pmd++)
  234. set_pmd(pmd, __pmd(0));
  235. }
  236. break;
  237. }
  238. if (pmd_val(*pmd))
  239. continue;
  240. pages++;
  241. set_pte((pte_t *)pmd,
  242. pfn_pte(address >> PAGE_SHIFT, PAGE_KERNEL_LARGE));
  243. }
  244. update_page_count(PG_LEVEL_2M, pages);
  245. return address;
  246. }
  247. static unsigned long __meminit
  248. phys_pmd_update(pud_t *pud, unsigned long address, unsigned long end)
  249. {
  250. pmd_t *pmd = pmd_offset(pud, 0);
  251. unsigned long last_map_addr;
  252. spin_lock(&init_mm.page_table_lock);
  253. last_map_addr = phys_pmd_init(pmd, address, end);
  254. spin_unlock(&init_mm.page_table_lock);
  255. __flush_tlb_all();
  256. return last_map_addr;
  257. }
  258. static unsigned long __meminit
  259. phys_pud_init(pud_t *pud_page, unsigned long addr, unsigned long end)
  260. {
  261. unsigned long pages = 0;
  262. unsigned long last_map_addr = end;
  263. int i = pud_index(addr);
  264. for (; i < PTRS_PER_PUD; i++, addr = (addr & PUD_MASK) + PUD_SIZE) {
  265. unsigned long pmd_phys;
  266. pud_t *pud = pud_page + pud_index(addr);
  267. pmd_t *pmd;
  268. if (addr >= end)
  269. break;
  270. if (!after_bootmem &&
  271. !e820_any_mapped(addr, addr+PUD_SIZE, 0)) {
  272. set_pud(pud, __pud(0));
  273. continue;
  274. }
  275. if (pud_val(*pud)) {
  276. if (!pud_large(*pud))
  277. last_map_addr = phys_pmd_update(pud, addr, end);
  278. continue;
  279. }
  280. if (direct_gbpages) {
  281. pages++;
  282. set_pte((pte_t *)pud,
  283. pfn_pte(addr >> PAGE_SHIFT, PAGE_KERNEL_LARGE));
  284. last_map_addr = (addr & PUD_MASK) + PUD_SIZE;
  285. continue;
  286. }
  287. pmd = alloc_low_page(&pmd_phys);
  288. spin_lock(&init_mm.page_table_lock);
  289. pud_populate(&init_mm, pud, __va(pmd_phys));
  290. last_map_addr = phys_pmd_init(pmd, addr, end);
  291. spin_unlock(&init_mm.page_table_lock);
  292. unmap_low_page(pmd);
  293. }
  294. __flush_tlb_all();
  295. update_page_count(PG_LEVEL_1G, pages);
  296. return last_map_addr >> PAGE_SHIFT;
  297. }
  298. static void __init find_early_table_space(unsigned long end)
  299. {
  300. unsigned long puds, pmds, tables, start;
  301. puds = (end + PUD_SIZE - 1) >> PUD_SHIFT;
  302. tables = round_up(puds * sizeof(pud_t), PAGE_SIZE);
  303. if (!direct_gbpages) {
  304. pmds = (end + PMD_SIZE - 1) >> PMD_SHIFT;
  305. tables += round_up(pmds * sizeof(pmd_t), PAGE_SIZE);
  306. }
  307. /*
  308. * RED-PEN putting page tables only on node 0 could
  309. * cause a hotspot and fill up ZONE_DMA. The page tables
  310. * need roughly 0.5KB per GB.
  311. */
  312. start = 0x8000;
  313. table_start = find_e820_area(start, end, tables, PAGE_SIZE);
  314. if (table_start == -1UL)
  315. panic("Cannot find space for the kernel page tables");
  316. table_start >>= PAGE_SHIFT;
  317. table_end = table_start;
  318. early_printk("kernel direct mapping tables up to %lx @ %lx-%lx\n",
  319. end, table_start << PAGE_SHIFT,
  320. (table_start << PAGE_SHIFT) + tables);
  321. }
  322. static void __init init_gbpages(void)
  323. {
  324. if (direct_gbpages && cpu_has_gbpages)
  325. printk(KERN_INFO "Using GB pages for direct mapping\n");
  326. else
  327. direct_gbpages = 0;
  328. }
  329. #ifdef CONFIG_MEMTEST
  330. static void __init memtest(unsigned long start_phys, unsigned long size,
  331. unsigned pattern)
  332. {
  333. unsigned long i;
  334. unsigned long *start;
  335. unsigned long start_bad;
  336. unsigned long last_bad;
  337. unsigned long val;
  338. unsigned long start_phys_aligned;
  339. unsigned long count;
  340. unsigned long incr;
  341. switch (pattern) {
  342. case 0:
  343. val = 0UL;
  344. break;
  345. case 1:
  346. val = -1UL;
  347. break;
  348. case 2:
  349. val = 0x5555555555555555UL;
  350. break;
  351. case 3:
  352. val = 0xaaaaaaaaaaaaaaaaUL;
  353. break;
  354. default:
  355. return;
  356. }
  357. incr = sizeof(unsigned long);
  358. start_phys_aligned = ALIGN(start_phys, incr);
  359. count = (size - (start_phys_aligned - start_phys))/incr;
  360. start = __va(start_phys_aligned);
  361. start_bad = 0;
  362. last_bad = 0;
  363. for (i = 0; i < count; i++)
  364. start[i] = val;
  365. for (i = 0; i < count; i++, start++, start_phys_aligned += incr) {
  366. if (*start != val) {
  367. if (start_phys_aligned == last_bad + incr) {
  368. last_bad += incr;
  369. } else {
  370. if (start_bad) {
  371. printk(KERN_CONT "\n %016lx bad mem addr %016lx - %016lx reserved",
  372. val, start_bad, last_bad + incr);
  373. reserve_early(start_bad, last_bad - start_bad, "BAD RAM");
  374. }
  375. start_bad = last_bad = start_phys_aligned;
  376. }
  377. }
  378. }
  379. if (start_bad) {
  380. printk(KERN_CONT "\n %016lx bad mem addr %016lx - %016lx reserved",
  381. val, start_bad, last_bad + incr);
  382. reserve_early(start_bad, last_bad - start_bad, "BAD RAM");
  383. }
  384. }
  385. /* default is disabled */
  386. static int memtest_pattern __initdata;
  387. static int __init parse_memtest(char *arg)
  388. {
  389. if (arg)
  390. memtest_pattern = simple_strtoul(arg, NULL, 0);
  391. return 0;
  392. }
  393. early_param("memtest", parse_memtest);
  394. static void __init early_memtest(unsigned long start, unsigned long end)
  395. {
  396. u64 t_start, t_size;
  397. unsigned pattern;
  398. if (!memtest_pattern)
  399. return;
  400. printk(KERN_INFO "early_memtest: pattern num %d", memtest_pattern);
  401. for (pattern = 0; pattern < memtest_pattern; pattern++) {
  402. t_start = start;
  403. t_size = 0;
  404. while (t_start < end) {
  405. t_start = find_e820_area_size(t_start, &t_size, 1);
  406. /* done ? */
  407. if (t_start >= end)
  408. break;
  409. if (t_start + t_size > end)
  410. t_size = end - t_start;
  411. printk(KERN_CONT "\n %016llx - %016llx pattern %d",
  412. (unsigned long long)t_start,
  413. (unsigned long long)t_start + t_size, pattern);
  414. memtest(t_start, t_size, pattern);
  415. t_start += t_size;
  416. }
  417. }
  418. printk(KERN_CONT "\n");
  419. }
  420. #else
  421. static void __init early_memtest(unsigned long start, unsigned long end)
  422. {
  423. }
  424. #endif
  425. /*
  426. * Setup the direct mapping of the physical memory at PAGE_OFFSET.
  427. * This runs before bootmem is initialized and gets pages directly from
  428. * the physical memory. To access them they are temporarily mapped.
  429. */
  430. unsigned long __init_refok init_memory_mapping(unsigned long start, unsigned long end)
  431. {
  432. unsigned long next, last_map_addr = end;
  433. unsigned long start_phys = start, end_phys = end;
  434. printk(KERN_INFO "init_memory_mapping\n");
  435. /*
  436. * Find space for the kernel direct mapping tables.
  437. *
  438. * Later we should allocate these tables in the local node of the
  439. * memory mapped. Unfortunately this is done currently before the
  440. * nodes are discovered.
  441. */
  442. if (!after_bootmem) {
  443. init_gbpages();
  444. find_early_table_space(end);
  445. }
  446. start = (unsigned long)__va(start);
  447. end = (unsigned long)__va(end);
  448. for (; start < end; start = next) {
  449. pgd_t *pgd = pgd_offset_k(start);
  450. unsigned long pud_phys;
  451. pud_t *pud;
  452. if (after_bootmem)
  453. pud = pud_offset(pgd, start & PGDIR_MASK);
  454. else
  455. pud = alloc_low_page(&pud_phys);
  456. next = start + PGDIR_SIZE;
  457. if (next > end)
  458. next = end;
  459. last_map_addr = phys_pud_init(pud, __pa(start), __pa(next));
  460. if (!after_bootmem)
  461. pgd_populate(&init_mm, pgd_offset_k(start),
  462. __va(pud_phys));
  463. unmap_low_page(pud);
  464. }
  465. if (!after_bootmem)
  466. mmu_cr4_features = read_cr4();
  467. __flush_tlb_all();
  468. if (!after_bootmem)
  469. reserve_early(table_start << PAGE_SHIFT,
  470. table_end << PAGE_SHIFT, "PGTABLE");
  471. if (!after_bootmem)
  472. early_memtest(start_phys, end_phys);
  473. return last_map_addr;
  474. }
  475. #ifndef CONFIG_NUMA
  476. void __init initmem_init(unsigned long start_pfn, unsigned long end_pfn)
  477. {
  478. unsigned long bootmap_size, bootmap;
  479. bootmap_size = bootmem_bootmap_pages(end_pfn)<<PAGE_SHIFT;
  480. bootmap = find_e820_area(0, end_pfn<<PAGE_SHIFT, bootmap_size,
  481. PAGE_SIZE);
  482. if (bootmap == -1L)
  483. panic("Cannot find bootmem map of size %ld\n", bootmap_size);
  484. /* don't touch min_low_pfn */
  485. bootmap_size = init_bootmem_node(NODE_DATA(0), bootmap >> PAGE_SHIFT,
  486. 0, end_pfn);
  487. e820_register_active_regions(0, start_pfn, end_pfn);
  488. free_bootmem_with_active_regions(0, end_pfn);
  489. early_res_to_bootmem(0, end_pfn<<PAGE_SHIFT);
  490. reserve_bootmem(bootmap, bootmap_size, BOOTMEM_DEFAULT);
  491. }
  492. void __init paging_init(void)
  493. {
  494. unsigned long max_zone_pfns[MAX_NR_ZONES];
  495. memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
  496. max_zone_pfns[ZONE_DMA] = MAX_DMA_PFN;
  497. max_zone_pfns[ZONE_DMA32] = MAX_DMA32_PFN;
  498. max_zone_pfns[ZONE_NORMAL] = end_pfn;
  499. memory_present(0, 0, end_pfn);
  500. sparse_init();
  501. free_area_init_nodes(max_zone_pfns);
  502. }
  503. #endif
  504. /*
  505. * Memory hotplug specific functions
  506. */
  507. #ifdef CONFIG_MEMORY_HOTPLUG
  508. /*
  509. * Memory is added always to NORMAL zone. This means you will never get
  510. * additional DMA/DMA32 memory.
  511. */
  512. int arch_add_memory(int nid, u64 start, u64 size)
  513. {
  514. struct pglist_data *pgdat = NODE_DATA(nid);
  515. struct zone *zone = pgdat->node_zones + ZONE_NORMAL;
  516. unsigned long last_mapped_pfn, start_pfn = start >> PAGE_SHIFT;
  517. unsigned long nr_pages = size >> PAGE_SHIFT;
  518. int ret;
  519. last_mapped_pfn = init_memory_mapping(start, start + size-1);
  520. if (last_mapped_pfn > max_pfn_mapped)
  521. max_pfn_mapped = last_mapped_pfn;
  522. ret = __add_pages(zone, start_pfn, nr_pages);
  523. WARN_ON(1);
  524. return ret;
  525. }
  526. EXPORT_SYMBOL_GPL(arch_add_memory);
  527. #if !defined(CONFIG_ACPI_NUMA) && defined(CONFIG_NUMA)
  528. int memory_add_physaddr_to_nid(u64 start)
  529. {
  530. return 0;
  531. }
  532. EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid);
  533. #endif
  534. #endif /* CONFIG_MEMORY_HOTPLUG */
  535. /*
  536. * devmem_is_allowed() checks to see if /dev/mem access to a certain address
  537. * is valid. The argument is a physical page number.
  538. *
  539. *
  540. * On x86, access has to be given to the first megabyte of ram because that area
  541. * contains bios code and data regions used by X and dosemu and similar apps.
  542. * Access has to be given to non-kernel-ram areas as well, these contain the PCI
  543. * mmio resources as well as potential bios/acpi data regions.
  544. */
  545. int devmem_is_allowed(unsigned long pagenr)
  546. {
  547. if (pagenr <= 256)
  548. return 1;
  549. if (!page_is_ram(pagenr))
  550. return 1;
  551. return 0;
  552. }
  553. static struct kcore_list kcore_mem, kcore_vmalloc, kcore_kernel,
  554. kcore_modules, kcore_vsyscall;
  555. void __init mem_init(void)
  556. {
  557. long codesize, reservedpages, datasize, initsize;
  558. pci_iommu_alloc();
  559. /* clear_bss() already clear the empty_zero_page */
  560. reservedpages = 0;
  561. /* this will put all low memory onto the freelists */
  562. #ifdef CONFIG_NUMA
  563. totalram_pages = numa_free_all_bootmem();
  564. #else
  565. totalram_pages = free_all_bootmem();
  566. #endif
  567. reservedpages = end_pfn - totalram_pages -
  568. absent_pages_in_range(0, end_pfn);
  569. after_bootmem = 1;
  570. codesize = (unsigned long) &_etext - (unsigned long) &_text;
  571. datasize = (unsigned long) &_edata - (unsigned long) &_etext;
  572. initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
  573. /* Register memory areas for /proc/kcore */
  574. kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT);
  575. kclist_add(&kcore_vmalloc, (void *)VMALLOC_START,
  576. VMALLOC_END-VMALLOC_START);
  577. kclist_add(&kcore_kernel, &_stext, _end - _stext);
  578. kclist_add(&kcore_modules, (void *)MODULES_VADDR, MODULES_LEN);
  579. kclist_add(&kcore_vsyscall, (void *)VSYSCALL_START,
  580. VSYSCALL_END - VSYSCALL_START);
  581. printk(KERN_INFO "Memory: %luk/%luk available (%ldk kernel code, "
  582. "%ldk reserved, %ldk data, %ldk init)\n",
  583. (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
  584. end_pfn << (PAGE_SHIFT-10),
  585. codesize >> 10,
  586. reservedpages << (PAGE_SHIFT-10),
  587. datasize >> 10,
  588. initsize >> 10);
  589. cpa_init();
  590. }
  591. void free_init_pages(char *what, unsigned long begin, unsigned long end)
  592. {
  593. unsigned long addr = begin;
  594. if (addr >= end)
  595. return;
  596. /*
  597. * If debugging page accesses then do not free this memory but
  598. * mark them not present - any buggy init-section access will
  599. * create a kernel page fault:
  600. */
  601. #ifdef CONFIG_DEBUG_PAGEALLOC
  602. printk(KERN_INFO "debug: unmapping init memory %08lx..%08lx\n",
  603. begin, PAGE_ALIGN(end));
  604. set_memory_np(begin, (end - begin) >> PAGE_SHIFT);
  605. #else
  606. printk(KERN_INFO "Freeing %s: %luk freed\n", what, (end - begin) >> 10);
  607. for (; addr < end; addr += PAGE_SIZE) {
  608. ClearPageReserved(virt_to_page(addr));
  609. init_page_count(virt_to_page(addr));
  610. memset((void *)(addr & ~(PAGE_SIZE-1)),
  611. POISON_FREE_INITMEM, PAGE_SIZE);
  612. free_page(addr);
  613. totalram_pages++;
  614. }
  615. #endif
  616. }
  617. void free_initmem(void)
  618. {
  619. free_init_pages("unused kernel memory",
  620. (unsigned long)(&__init_begin),
  621. (unsigned long)(&__init_end));
  622. }
  623. #ifdef CONFIG_DEBUG_RODATA
  624. const int rodata_test_data = 0xC3;
  625. EXPORT_SYMBOL_GPL(rodata_test_data);
  626. void mark_rodata_ro(void)
  627. {
  628. unsigned long start = PFN_ALIGN(_stext), end = PFN_ALIGN(__end_rodata);
  629. printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n",
  630. (end - start) >> 10);
  631. set_memory_ro(start, (end - start) >> PAGE_SHIFT);
  632. /*
  633. * The rodata section (but not the kernel text!) should also be
  634. * not-executable.
  635. */
  636. start = ((unsigned long)__start_rodata + PAGE_SIZE - 1) & PAGE_MASK;
  637. set_memory_nx(start, (end - start) >> PAGE_SHIFT);
  638. rodata_test();
  639. #ifdef CONFIG_CPA_DEBUG
  640. printk(KERN_INFO "Testing CPA: undo %lx-%lx\n", start, end);
  641. set_memory_rw(start, (end-start) >> PAGE_SHIFT);
  642. printk(KERN_INFO "Testing CPA: again\n");
  643. set_memory_ro(start, (end-start) >> PAGE_SHIFT);
  644. #endif
  645. }
  646. #endif
  647. #ifdef CONFIG_BLK_DEV_INITRD
  648. void free_initrd_mem(unsigned long start, unsigned long end)
  649. {
  650. free_init_pages("initrd memory", start, end);
  651. }
  652. #endif
  653. int __init reserve_bootmem_generic(unsigned long phys, unsigned long len,
  654. int flags)
  655. {
  656. #ifdef CONFIG_NUMA
  657. int nid, next_nid;
  658. int ret;
  659. #endif
  660. unsigned long pfn = phys >> PAGE_SHIFT;
  661. if (pfn >= end_pfn) {
  662. /*
  663. * This can happen with kdump kernels when accessing
  664. * firmware tables:
  665. */
  666. if (pfn < max_pfn_mapped)
  667. return -EFAULT;
  668. printk(KERN_ERR "reserve_bootmem: illegal reserve %lx %lu\n",
  669. phys, len);
  670. return -EFAULT;
  671. }
  672. /* Should check here against the e820 map to avoid double free */
  673. #ifdef CONFIG_NUMA
  674. nid = phys_to_nid(phys);
  675. next_nid = phys_to_nid(phys + len - 1);
  676. if (nid == next_nid)
  677. ret = reserve_bootmem_node(NODE_DATA(nid), phys, len, flags);
  678. else
  679. ret = reserve_bootmem(phys, len, flags);
  680. if (ret != 0)
  681. return ret;
  682. #else
  683. reserve_bootmem(phys, len, BOOTMEM_DEFAULT);
  684. #endif
  685. if (phys+len <= MAX_DMA_PFN*PAGE_SIZE) {
  686. dma_reserve += len / PAGE_SIZE;
  687. set_dma_reserve(dma_reserve);
  688. }
  689. return 0;
  690. }
  691. int kern_addr_valid(unsigned long addr)
  692. {
  693. unsigned long above = ((long)addr) >> __VIRTUAL_MASK_SHIFT;
  694. pgd_t *pgd;
  695. pud_t *pud;
  696. pmd_t *pmd;
  697. pte_t *pte;
  698. if (above != 0 && above != -1UL)
  699. return 0;
  700. pgd = pgd_offset_k(addr);
  701. if (pgd_none(*pgd))
  702. return 0;
  703. pud = pud_offset(pgd, addr);
  704. if (pud_none(*pud))
  705. return 0;
  706. pmd = pmd_offset(pud, addr);
  707. if (pmd_none(*pmd))
  708. return 0;
  709. if (pmd_large(*pmd))
  710. return pfn_valid(pmd_pfn(*pmd));
  711. pte = pte_offset_kernel(pmd, addr);
  712. if (pte_none(*pte))
  713. return 0;
  714. return pfn_valid(pte_pfn(*pte));
  715. }
  716. /*
  717. * A pseudo VMA to allow ptrace access for the vsyscall page. This only
  718. * covers the 64bit vsyscall page now. 32bit has a real VMA now and does
  719. * not need special handling anymore:
  720. */
  721. static struct vm_area_struct gate_vma = {
  722. .vm_start = VSYSCALL_START,
  723. .vm_end = VSYSCALL_START + (VSYSCALL_MAPPED_PAGES * PAGE_SIZE),
  724. .vm_page_prot = PAGE_READONLY_EXEC,
  725. .vm_flags = VM_READ | VM_EXEC
  726. };
  727. struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
  728. {
  729. #ifdef CONFIG_IA32_EMULATION
  730. if (test_tsk_thread_flag(tsk, TIF_IA32))
  731. return NULL;
  732. #endif
  733. return &gate_vma;
  734. }
  735. int in_gate_area(struct task_struct *task, unsigned long addr)
  736. {
  737. struct vm_area_struct *vma = get_gate_vma(task);
  738. if (!vma)
  739. return 0;
  740. return (addr >= vma->vm_start) && (addr < vma->vm_end);
  741. }
  742. /*
  743. * Use this when you have no reliable task/vma, typically from interrupt
  744. * context. It is less reliable than using the task's vma and may give
  745. * false positives:
  746. */
  747. int in_gate_area_no_task(unsigned long addr)
  748. {
  749. return (addr >= VSYSCALL_START) && (addr < VSYSCALL_END);
  750. }
  751. const char *arch_vma_name(struct vm_area_struct *vma)
  752. {
  753. if (vma->vm_mm && vma->vm_start == (long)vma->vm_mm->context.vdso)
  754. return "[vdso]";
  755. if (vma == &gate_vma)
  756. return "[vsyscall]";
  757. return NULL;
  758. }
  759. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  760. /*
  761. * Initialise the sparsemem vmemmap using huge-pages at the PMD level.
  762. */
  763. static long __meminitdata addr_start, addr_end;
  764. static void __meminitdata *p_start, *p_end;
  765. static int __meminitdata node_start;
  766. int __meminit
  767. vmemmap_populate(struct page *start_page, unsigned long size, int node)
  768. {
  769. unsigned long addr = (unsigned long)start_page;
  770. unsigned long end = (unsigned long)(start_page + size);
  771. unsigned long next;
  772. pgd_t *pgd;
  773. pud_t *pud;
  774. pmd_t *pmd;
  775. for (; addr < end; addr = next) {
  776. next = pmd_addr_end(addr, end);
  777. pgd = vmemmap_pgd_populate(addr, node);
  778. if (!pgd)
  779. return -ENOMEM;
  780. pud = vmemmap_pud_populate(pgd, addr, node);
  781. if (!pud)
  782. return -ENOMEM;
  783. pmd = pmd_offset(pud, addr);
  784. if (pmd_none(*pmd)) {
  785. pte_t entry;
  786. void *p;
  787. p = vmemmap_alloc_block(PMD_SIZE, node);
  788. if (!p)
  789. return -ENOMEM;
  790. entry = pfn_pte(__pa(p) >> PAGE_SHIFT,
  791. PAGE_KERNEL_LARGE);
  792. set_pmd(pmd, __pmd(pte_val(entry)));
  793. /* check to see if we have contiguous blocks */
  794. if (p_end != p || node_start != node) {
  795. if (p_start)
  796. printk(KERN_DEBUG " [%lx-%lx] PMD -> [%p-%p] on node %d\n",
  797. addr_start, addr_end-1, p_start, p_end-1, node_start);
  798. addr_start = addr;
  799. node_start = node;
  800. p_start = p;
  801. }
  802. addr_end = addr + PMD_SIZE;
  803. p_end = p + PMD_SIZE;
  804. } else {
  805. vmemmap_verify((pte_t *)pmd, node, addr, next);
  806. }
  807. }
  808. return 0;
  809. }
  810. void __meminit vmemmap_populate_print_last(void)
  811. {
  812. if (p_start) {
  813. printk(KERN_DEBUG " [%lx-%lx] PMD -> [%p-%p] on node %d\n",
  814. addr_start, addr_end-1, p_start, p_end-1, node_start);
  815. p_start = NULL;
  816. p_end = NULL;
  817. node_start = 0;
  818. }
  819. }
  820. #endif