init.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. /*
  2. * Initialize MMU support.
  3. *
  4. * Copyright (C) 1998-2003 Hewlett-Packard Co
  5. * David Mosberger-Tang <davidm@hpl.hp.com>
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/init.h>
  9. #include <linux/bootmem.h>
  10. #include <linux/efi.h>
  11. #include <linux/elf.h>
  12. #include <linux/mm.h>
  13. #include <linux/mmzone.h>
  14. #include <linux/module.h>
  15. #include <linux/personality.h>
  16. #include <linux/reboot.h>
  17. #include <linux/slab.h>
  18. #include <linux/swap.h>
  19. #include <linux/proc_fs.h>
  20. #include <linux/bitops.h>
  21. #include <asm/a.out.h>
  22. #include <asm/dma.h>
  23. #include <asm/ia32.h>
  24. #include <asm/io.h>
  25. #include <asm/machvec.h>
  26. #include <asm/numa.h>
  27. #include <asm/patch.h>
  28. #include <asm/pgalloc.h>
  29. #include <asm/sal.h>
  30. #include <asm/sections.h>
  31. #include <asm/system.h>
  32. #include <asm/tlb.h>
  33. #include <asm/uaccess.h>
  34. #include <asm/unistd.h>
  35. #include <asm/mca.h>
  36. DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
  37. DEFINE_PER_CPU(unsigned long *, __pgtable_quicklist);
  38. DEFINE_PER_CPU(long, __pgtable_quicklist_size);
  39. extern void ia64_tlb_init (void);
  40. unsigned long MAX_DMA_ADDRESS = PAGE_OFFSET + 0x100000000UL;
  41. #ifdef CONFIG_VIRTUAL_MEM_MAP
  42. unsigned long vmalloc_end = VMALLOC_END_INIT;
  43. EXPORT_SYMBOL(vmalloc_end);
  44. struct page *vmem_map;
  45. EXPORT_SYMBOL(vmem_map);
  46. #endif
  47. struct page *zero_page_memmap_ptr; /* map entry for zero page */
  48. EXPORT_SYMBOL(zero_page_memmap_ptr);
  49. #define MIN_PGT_PAGES 25UL
  50. #define MAX_PGT_FREES_PER_PASS 16L
  51. #define PGT_FRACTION_OF_NODE_MEM 16
  52. static inline long
  53. max_pgt_pages(void)
  54. {
  55. u64 node_free_pages, max_pgt_pages;
  56. #ifndef CONFIG_NUMA
  57. node_free_pages = nr_free_pages();
  58. #else
  59. node_free_pages = nr_free_pages_pgdat(NODE_DATA(numa_node_id()));
  60. #endif
  61. max_pgt_pages = node_free_pages / PGT_FRACTION_OF_NODE_MEM;
  62. max_pgt_pages = max(max_pgt_pages, MIN_PGT_PAGES);
  63. return max_pgt_pages;
  64. }
  65. static inline long
  66. min_pages_to_free(void)
  67. {
  68. long pages_to_free;
  69. pages_to_free = pgtable_quicklist_size - max_pgt_pages();
  70. pages_to_free = min(pages_to_free, MAX_PGT_FREES_PER_PASS);
  71. return pages_to_free;
  72. }
  73. void
  74. check_pgt_cache(void)
  75. {
  76. long pages_to_free;
  77. if (unlikely(pgtable_quicklist_size <= MIN_PGT_PAGES))
  78. return;
  79. preempt_disable();
  80. while (unlikely((pages_to_free = min_pages_to_free()) > 0)) {
  81. while (pages_to_free--) {
  82. free_page((unsigned long)pgtable_quicklist_alloc());
  83. }
  84. preempt_enable();
  85. preempt_disable();
  86. }
  87. preempt_enable();
  88. }
  89. void
  90. lazy_mmu_prot_update (pte_t pte)
  91. {
  92. unsigned long addr;
  93. struct page *page;
  94. unsigned long order;
  95. if (!pte_exec(pte))
  96. return; /* not an executable page... */
  97. page = pte_page(pte);
  98. addr = (unsigned long) page_address(page);
  99. if (test_bit(PG_arch_1, &page->flags))
  100. return; /* i-cache is already coherent with d-cache */
  101. if (PageCompound(page)) {
  102. order = (unsigned long) (page[1].lru.prev);
  103. flush_icache_range(addr, addr + (1UL << order << PAGE_SHIFT));
  104. }
  105. else
  106. flush_icache_range(addr, addr + PAGE_SIZE);
  107. set_bit(PG_arch_1, &page->flags); /* mark page as clean */
  108. }
  109. inline void
  110. ia64_set_rbs_bot (void)
  111. {
  112. unsigned long stack_size = current->signal->rlim[RLIMIT_STACK].rlim_max & -16;
  113. if (stack_size > MAX_USER_STACK_SIZE)
  114. stack_size = MAX_USER_STACK_SIZE;
  115. current->thread.rbs_bot = STACK_TOP - stack_size;
  116. }
  117. /*
  118. * This performs some platform-dependent address space initialization.
  119. * On IA-64, we want to setup the VM area for the register backing
  120. * store (which grows upwards) and install the gateway page which is
  121. * used for signal trampolines, etc.
  122. */
  123. void
  124. ia64_init_addr_space (void)
  125. {
  126. struct vm_area_struct *vma;
  127. ia64_set_rbs_bot();
  128. /*
  129. * If we're out of memory and kmem_cache_alloc() returns NULL, we simply ignore
  130. * the problem. When the process attempts to write to the register backing store
  131. * for the first time, it will get a SEGFAULT in this case.
  132. */
  133. vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
  134. if (vma) {
  135. memset(vma, 0, sizeof(*vma));
  136. vma->vm_mm = current->mm;
  137. vma->vm_start = current->thread.rbs_bot & PAGE_MASK;
  138. vma->vm_end = vma->vm_start + PAGE_SIZE;
  139. vma->vm_page_prot = protection_map[VM_DATA_DEFAULT_FLAGS & 0x7];
  140. vma->vm_flags = VM_DATA_DEFAULT_FLAGS|VM_GROWSUP|VM_ACCOUNT;
  141. down_write(&current->mm->mmap_sem);
  142. if (insert_vm_struct(current->mm, vma)) {
  143. up_write(&current->mm->mmap_sem);
  144. kmem_cache_free(vm_area_cachep, vma);
  145. return;
  146. }
  147. up_write(&current->mm->mmap_sem);
  148. }
  149. /* map NaT-page at address zero to speed up speculative dereferencing of NULL: */
  150. if (!(current->personality & MMAP_PAGE_ZERO)) {
  151. vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
  152. if (vma) {
  153. memset(vma, 0, sizeof(*vma));
  154. vma->vm_mm = current->mm;
  155. vma->vm_end = PAGE_SIZE;
  156. vma->vm_page_prot = __pgprot(pgprot_val(PAGE_READONLY) | _PAGE_MA_NAT);
  157. vma->vm_flags = VM_READ | VM_MAYREAD | VM_IO | VM_RESERVED;
  158. down_write(&current->mm->mmap_sem);
  159. if (insert_vm_struct(current->mm, vma)) {
  160. up_write(&current->mm->mmap_sem);
  161. kmem_cache_free(vm_area_cachep, vma);
  162. return;
  163. }
  164. up_write(&current->mm->mmap_sem);
  165. }
  166. }
  167. }
  168. void
  169. free_initmem (void)
  170. {
  171. unsigned long addr, eaddr;
  172. addr = (unsigned long) ia64_imva(__init_begin);
  173. eaddr = (unsigned long) ia64_imva(__init_end);
  174. while (addr < eaddr) {
  175. ClearPageReserved(virt_to_page(addr));
  176. init_page_count(virt_to_page(addr));
  177. free_page(addr);
  178. ++totalram_pages;
  179. addr += PAGE_SIZE;
  180. }
  181. printk(KERN_INFO "Freeing unused kernel memory: %ldkB freed\n",
  182. (__init_end - __init_begin) >> 10);
  183. }
  184. void __init
  185. free_initrd_mem (unsigned long start, unsigned long end)
  186. {
  187. struct page *page;
  188. /*
  189. * EFI uses 4KB pages while the kernel can use 4KB or bigger.
  190. * Thus EFI and the kernel may have different page sizes. It is
  191. * therefore possible to have the initrd share the same page as
  192. * the end of the kernel (given current setup).
  193. *
  194. * To avoid freeing/using the wrong page (kernel sized) we:
  195. * - align up the beginning of initrd
  196. * - align down the end of initrd
  197. *
  198. * | |
  199. * |=============| a000
  200. * | |
  201. * | |
  202. * | | 9000
  203. * |/////////////|
  204. * |/////////////|
  205. * |=============| 8000
  206. * |///INITRD////|
  207. * |/////////////|
  208. * |/////////////| 7000
  209. * | |
  210. * |KKKKKKKKKKKKK|
  211. * |=============| 6000
  212. * |KKKKKKKKKKKKK|
  213. * |KKKKKKKKKKKKK|
  214. * K=kernel using 8KB pages
  215. *
  216. * In this example, we must free page 8000 ONLY. So we must align up
  217. * initrd_start and keep initrd_end as is.
  218. */
  219. start = PAGE_ALIGN(start);
  220. end = end & PAGE_MASK;
  221. if (start < end)
  222. printk(KERN_INFO "Freeing initrd memory: %ldkB freed\n", (end - start) >> 10);
  223. for (; start < end; start += PAGE_SIZE) {
  224. if (!virt_addr_valid(start))
  225. continue;
  226. page = virt_to_page(start);
  227. ClearPageReserved(page);
  228. init_page_count(page);
  229. free_page(start);
  230. ++totalram_pages;
  231. }
  232. }
  233. /*
  234. * This installs a clean page in the kernel's page table.
  235. */
  236. static struct page * __init
  237. put_kernel_page (struct page *page, unsigned long address, pgprot_t pgprot)
  238. {
  239. pgd_t *pgd;
  240. pud_t *pud;
  241. pmd_t *pmd;
  242. pte_t *pte;
  243. if (!PageReserved(page))
  244. printk(KERN_ERR "put_kernel_page: page at 0x%p not in reserved memory\n",
  245. page_address(page));
  246. pgd = pgd_offset_k(address); /* note: this is NOT pgd_offset()! */
  247. {
  248. pud = pud_alloc(&init_mm, pgd, address);
  249. if (!pud)
  250. goto out;
  251. pmd = pmd_alloc(&init_mm, pud, address);
  252. if (!pmd)
  253. goto out;
  254. pte = pte_alloc_kernel(pmd, address);
  255. if (!pte)
  256. goto out;
  257. if (!pte_none(*pte))
  258. goto out;
  259. set_pte(pte, mk_pte(page, pgprot));
  260. }
  261. out:
  262. /* no need for flush_tlb */
  263. return page;
  264. }
  265. static void __init
  266. setup_gate (void)
  267. {
  268. struct page *page;
  269. /*
  270. * Map the gate page twice: once read-only to export the ELF
  271. * headers etc. and once execute-only page to enable
  272. * privilege-promotion via "epc":
  273. */
  274. page = virt_to_page(ia64_imva(__start_gate_section));
  275. put_kernel_page(page, GATE_ADDR, PAGE_READONLY);
  276. #ifdef HAVE_BUGGY_SEGREL
  277. page = virt_to_page(ia64_imva(__start_gate_section + PAGE_SIZE));
  278. put_kernel_page(page, GATE_ADDR + PAGE_SIZE, PAGE_GATE);
  279. #else
  280. put_kernel_page(page, GATE_ADDR + PERCPU_PAGE_SIZE, PAGE_GATE);
  281. /* Fill in the holes (if any) with read-only zero pages: */
  282. {
  283. unsigned long addr;
  284. for (addr = GATE_ADDR + PAGE_SIZE;
  285. addr < GATE_ADDR + PERCPU_PAGE_SIZE;
  286. addr += PAGE_SIZE)
  287. {
  288. put_kernel_page(ZERO_PAGE(0), addr,
  289. PAGE_READONLY);
  290. put_kernel_page(ZERO_PAGE(0), addr + PERCPU_PAGE_SIZE,
  291. PAGE_READONLY);
  292. }
  293. }
  294. #endif
  295. ia64_patch_gate();
  296. }
  297. void __devinit
  298. ia64_mmu_init (void *my_cpu_data)
  299. {
  300. unsigned long psr, pta, impl_va_bits;
  301. extern void __devinit tlb_init (void);
  302. #ifdef CONFIG_DISABLE_VHPT
  303. # define VHPT_ENABLE_BIT 0
  304. #else
  305. # define VHPT_ENABLE_BIT 1
  306. #endif
  307. /* Pin mapping for percpu area into TLB */
  308. psr = ia64_clear_ic();
  309. ia64_itr(0x2, IA64_TR_PERCPU_DATA, PERCPU_ADDR,
  310. pte_val(pfn_pte(__pa(my_cpu_data) >> PAGE_SHIFT, PAGE_KERNEL)),
  311. PERCPU_PAGE_SHIFT);
  312. ia64_set_psr(psr);
  313. ia64_srlz_i();
  314. /*
  315. * Check if the virtually mapped linear page table (VMLPT) overlaps with a mapped
  316. * address space. The IA-64 architecture guarantees that at least 50 bits of
  317. * virtual address space are implemented but if we pick a large enough page size
  318. * (e.g., 64KB), the mapped address space is big enough that it will overlap with
  319. * VMLPT. I assume that once we run on machines big enough to warrant 64KB pages,
  320. * IMPL_VA_MSB will be significantly bigger, so this is unlikely to become a
  321. * problem in practice. Alternatively, we could truncate the top of the mapped
  322. * address space to not permit mappings that would overlap with the VMLPT.
  323. * --davidm 00/12/06
  324. */
  325. # define pte_bits 3
  326. # define mapped_space_bits (3*(PAGE_SHIFT - pte_bits) + PAGE_SHIFT)
  327. /*
  328. * The virtual page table has to cover the entire implemented address space within
  329. * a region even though not all of this space may be mappable. The reason for
  330. * this is that the Access bit and Dirty bit fault handlers perform
  331. * non-speculative accesses to the virtual page table, so the address range of the
  332. * virtual page table itself needs to be covered by virtual page table.
  333. */
  334. # define vmlpt_bits (impl_va_bits - PAGE_SHIFT + pte_bits)
  335. # define POW2(n) (1ULL << (n))
  336. impl_va_bits = ffz(~(local_cpu_data->unimpl_va_mask | (7UL << 61)));
  337. if (impl_va_bits < 51 || impl_va_bits > 61)
  338. panic("CPU has bogus IMPL_VA_MSB value of %lu!\n", impl_va_bits - 1);
  339. /*
  340. * mapped_space_bits - PAGE_SHIFT is the total number of ptes we need,
  341. * which must fit into "vmlpt_bits - pte_bits" slots. Second half of
  342. * the test makes sure that our mapped space doesn't overlap the
  343. * unimplemented hole in the middle of the region.
  344. */
  345. if ((mapped_space_bits - PAGE_SHIFT > vmlpt_bits - pte_bits) ||
  346. (mapped_space_bits > impl_va_bits - 1))
  347. panic("Cannot build a big enough virtual-linear page table"
  348. " to cover mapped address space.\n"
  349. " Try using a smaller page size.\n");
  350. /* place the VMLPT at the end of each page-table mapped region: */
  351. pta = POW2(61) - POW2(vmlpt_bits);
  352. /*
  353. * Set the (virtually mapped linear) page table address. Bit
  354. * 8 selects between the short and long format, bits 2-7 the
  355. * size of the table, and bit 0 whether the VHPT walker is
  356. * enabled.
  357. */
  358. ia64_set_pta(pta | (0 << 8) | (vmlpt_bits << 2) | VHPT_ENABLE_BIT);
  359. ia64_tlb_init();
  360. #ifdef CONFIG_HUGETLB_PAGE
  361. ia64_set_rr(HPAGE_REGION_BASE, HPAGE_SHIFT << 2);
  362. ia64_srlz_d();
  363. #endif
  364. }
  365. #ifdef CONFIG_VIRTUAL_MEM_MAP
  366. int vmemmap_find_next_valid_pfn(int node, int i)
  367. {
  368. unsigned long end_address, hole_next_pfn;
  369. unsigned long stop_address;
  370. pg_data_t *pgdat = NODE_DATA(node);
  371. end_address = (unsigned long) &vmem_map[pgdat->node_start_pfn + i];
  372. end_address = PAGE_ALIGN(end_address);
  373. stop_address = (unsigned long) &vmem_map[
  374. pgdat->node_start_pfn + pgdat->node_spanned_pages];
  375. do {
  376. pgd_t *pgd;
  377. pud_t *pud;
  378. pmd_t *pmd;
  379. pte_t *pte;
  380. pgd = pgd_offset_k(end_address);
  381. if (pgd_none(*pgd)) {
  382. end_address += PGDIR_SIZE;
  383. continue;
  384. }
  385. pud = pud_offset(pgd, end_address);
  386. if (pud_none(*pud)) {
  387. end_address += PUD_SIZE;
  388. continue;
  389. }
  390. pmd = pmd_offset(pud, end_address);
  391. if (pmd_none(*pmd)) {
  392. end_address += PMD_SIZE;
  393. continue;
  394. }
  395. pte = pte_offset_kernel(pmd, end_address);
  396. retry_pte:
  397. if (pte_none(*pte)) {
  398. end_address += PAGE_SIZE;
  399. pte++;
  400. if ((end_address < stop_address) &&
  401. (end_address != ALIGN(end_address, 1UL << PMD_SHIFT)))
  402. goto retry_pte;
  403. continue;
  404. }
  405. /* Found next valid vmem_map page */
  406. break;
  407. } while (end_address < stop_address);
  408. end_address = min(end_address, stop_address);
  409. end_address = end_address - (unsigned long) vmem_map + sizeof(struct page) - 1;
  410. hole_next_pfn = end_address / sizeof(struct page);
  411. return hole_next_pfn - pgdat->node_start_pfn;
  412. }
  413. int __init
  414. create_mem_map_page_table (u64 start, u64 end, void *arg)
  415. {
  416. unsigned long address, start_page, end_page;
  417. struct page *map_start, *map_end;
  418. int node;
  419. pgd_t *pgd;
  420. pud_t *pud;
  421. pmd_t *pmd;
  422. pte_t *pte;
  423. map_start = vmem_map + (__pa(start) >> PAGE_SHIFT);
  424. map_end = vmem_map + (__pa(end) >> PAGE_SHIFT);
  425. start_page = (unsigned long) map_start & PAGE_MASK;
  426. end_page = PAGE_ALIGN((unsigned long) map_end);
  427. node = paddr_to_nid(__pa(start));
  428. for (address = start_page; address < end_page; address += PAGE_SIZE) {
  429. pgd = pgd_offset_k(address);
  430. if (pgd_none(*pgd))
  431. pgd_populate(&init_mm, pgd, alloc_bootmem_pages_node(NODE_DATA(node), PAGE_SIZE));
  432. pud = pud_offset(pgd, address);
  433. if (pud_none(*pud))
  434. pud_populate(&init_mm, pud, alloc_bootmem_pages_node(NODE_DATA(node), PAGE_SIZE));
  435. pmd = pmd_offset(pud, address);
  436. if (pmd_none(*pmd))
  437. pmd_populate_kernel(&init_mm, pmd, alloc_bootmem_pages_node(NODE_DATA(node), PAGE_SIZE));
  438. pte = pte_offset_kernel(pmd, address);
  439. if (pte_none(*pte))
  440. set_pte(pte, pfn_pte(__pa(alloc_bootmem_pages_node(NODE_DATA(node), PAGE_SIZE)) >> PAGE_SHIFT,
  441. PAGE_KERNEL));
  442. }
  443. return 0;
  444. }
  445. struct memmap_init_callback_data {
  446. struct page *start;
  447. struct page *end;
  448. int nid;
  449. unsigned long zone;
  450. };
  451. static int
  452. virtual_memmap_init (u64 start, u64 end, void *arg)
  453. {
  454. struct memmap_init_callback_data *args;
  455. struct page *map_start, *map_end;
  456. args = (struct memmap_init_callback_data *) arg;
  457. map_start = vmem_map + (__pa(start) >> PAGE_SHIFT);
  458. map_end = vmem_map + (__pa(end) >> PAGE_SHIFT);
  459. if (map_start < args->start)
  460. map_start = args->start;
  461. if (map_end > args->end)
  462. map_end = args->end;
  463. /*
  464. * We have to initialize "out of bounds" struct page elements that fit completely
  465. * on the same pages that were allocated for the "in bounds" elements because they
  466. * may be referenced later (and found to be "reserved").
  467. */
  468. map_start -= ((unsigned long) map_start & (PAGE_SIZE - 1)) / sizeof(struct page);
  469. map_end += ((PAGE_ALIGN((unsigned long) map_end) - (unsigned long) map_end)
  470. / sizeof(struct page));
  471. if (map_start < map_end)
  472. memmap_init_zone((unsigned long)(map_end - map_start),
  473. args->nid, args->zone, page_to_pfn(map_start));
  474. return 0;
  475. }
  476. void
  477. memmap_init (unsigned long size, int nid, unsigned long zone,
  478. unsigned long start_pfn)
  479. {
  480. if (!vmem_map)
  481. memmap_init_zone(size, nid, zone, start_pfn);
  482. else {
  483. struct page *start;
  484. struct memmap_init_callback_data args;
  485. start = pfn_to_page(start_pfn);
  486. args.start = start;
  487. args.end = start + size;
  488. args.nid = nid;
  489. args.zone = zone;
  490. efi_memmap_walk(virtual_memmap_init, &args);
  491. }
  492. }
  493. int
  494. ia64_pfn_valid (unsigned long pfn)
  495. {
  496. char byte;
  497. struct page *pg = pfn_to_page(pfn);
  498. return (__get_user(byte, (char __user *) pg) == 0)
  499. && ((((u64)pg & PAGE_MASK) == (((u64)(pg + 1) - 1) & PAGE_MASK))
  500. || (__get_user(byte, (char __user *) (pg + 1) - 1) == 0));
  501. }
  502. EXPORT_SYMBOL(ia64_pfn_valid);
  503. int __init
  504. find_largest_hole (u64 start, u64 end, void *arg)
  505. {
  506. u64 *max_gap = arg;
  507. static u64 last_end = PAGE_OFFSET;
  508. /* NOTE: this algorithm assumes efi memmap table is ordered */
  509. if (*max_gap < (start - last_end))
  510. *max_gap = start - last_end;
  511. last_end = end;
  512. return 0;
  513. }
  514. int __init
  515. register_active_ranges(u64 start, u64 end, void *nid)
  516. {
  517. BUG_ON(nid == NULL);
  518. BUG_ON(*(unsigned long *)nid >= MAX_NUMNODES);
  519. add_active_range(*(unsigned long *)nid,
  520. __pa(start) >> PAGE_SHIFT,
  521. __pa(end) >> PAGE_SHIFT);
  522. return 0;
  523. }
  524. #endif /* CONFIG_VIRTUAL_MEM_MAP */
  525. static int __init
  526. count_reserved_pages (u64 start, u64 end, void *arg)
  527. {
  528. unsigned long num_reserved = 0;
  529. unsigned long *count = arg;
  530. for (; start < end; start += PAGE_SIZE)
  531. if (PageReserved(virt_to_page(start)))
  532. ++num_reserved;
  533. *count += num_reserved;
  534. return 0;
  535. }
  536. /*
  537. * Boot command-line option "nolwsys" can be used to disable the use of any light-weight
  538. * system call handler. When this option is in effect, all fsyscalls will end up bubbling
  539. * down into the kernel and calling the normal (heavy-weight) syscall handler. This is
  540. * useful for performance testing, but conceivably could also come in handy for debugging
  541. * purposes.
  542. */
  543. static int nolwsys __initdata;
  544. static int __init
  545. nolwsys_setup (char *s)
  546. {
  547. nolwsys = 1;
  548. return 1;
  549. }
  550. __setup("nolwsys", nolwsys_setup);
  551. void __init
  552. mem_init (void)
  553. {
  554. long reserved_pages, codesize, datasize, initsize;
  555. pg_data_t *pgdat;
  556. int i;
  557. static struct kcore_list kcore_mem, kcore_vmem, kcore_kernel;
  558. BUG_ON(PTRS_PER_PGD * sizeof(pgd_t) != PAGE_SIZE);
  559. BUG_ON(PTRS_PER_PMD * sizeof(pmd_t) != PAGE_SIZE);
  560. BUG_ON(PTRS_PER_PTE * sizeof(pte_t) != PAGE_SIZE);
  561. #ifdef CONFIG_PCI
  562. /*
  563. * This needs to be called _after_ the command line has been parsed but _before_
  564. * any drivers that may need the PCI DMA interface are initialized or bootmem has
  565. * been freed.
  566. */
  567. platform_dma_init();
  568. #endif
  569. #ifdef CONFIG_FLATMEM
  570. if (!mem_map)
  571. BUG();
  572. max_mapnr = max_low_pfn;
  573. #endif
  574. high_memory = __va(max_low_pfn * PAGE_SIZE);
  575. kclist_add(&kcore_mem, __va(0), max_low_pfn * PAGE_SIZE);
  576. kclist_add(&kcore_vmem, (void *)VMALLOC_START, VMALLOC_END-VMALLOC_START);
  577. kclist_add(&kcore_kernel, _stext, _end - _stext);
  578. for_each_online_pgdat(pgdat)
  579. if (pgdat->bdata->node_bootmem_map)
  580. totalram_pages += free_all_bootmem_node(pgdat);
  581. reserved_pages = 0;
  582. efi_memmap_walk(count_reserved_pages, &reserved_pages);
  583. codesize = (unsigned long) _etext - (unsigned long) _stext;
  584. datasize = (unsigned long) _edata - (unsigned long) _etext;
  585. initsize = (unsigned long) __init_end - (unsigned long) __init_begin;
  586. printk(KERN_INFO "Memory: %luk/%luk available (%luk code, %luk reserved, "
  587. "%luk data, %luk init)\n", (unsigned long) nr_free_pages() << (PAGE_SHIFT - 10),
  588. num_physpages << (PAGE_SHIFT - 10), codesize >> 10,
  589. reserved_pages << (PAGE_SHIFT - 10), datasize >> 10, initsize >> 10);
  590. /*
  591. * For fsyscall entrpoints with no light-weight handler, use the ordinary
  592. * (heavy-weight) handler, but mark it by setting bit 0, so the fsyscall entry
  593. * code can tell them apart.
  594. */
  595. for (i = 0; i < NR_syscalls; ++i) {
  596. extern unsigned long fsyscall_table[NR_syscalls];
  597. extern unsigned long sys_call_table[NR_syscalls];
  598. if (!fsyscall_table[i] || nolwsys)
  599. fsyscall_table[i] = sys_call_table[i] | 1;
  600. }
  601. setup_gate();
  602. #ifdef CONFIG_IA32_SUPPORT
  603. ia32_mem_init();
  604. #endif
  605. }
  606. #ifdef CONFIG_MEMORY_HOTPLUG
  607. void online_page(struct page *page)
  608. {
  609. ClearPageReserved(page);
  610. init_page_count(page);
  611. __free_page(page);
  612. totalram_pages++;
  613. num_physpages++;
  614. }
  615. int arch_add_memory(int nid, u64 start, u64 size)
  616. {
  617. pg_data_t *pgdat;
  618. struct zone *zone;
  619. unsigned long start_pfn = start >> PAGE_SHIFT;
  620. unsigned long nr_pages = size >> PAGE_SHIFT;
  621. int ret;
  622. pgdat = NODE_DATA(nid);
  623. zone = pgdat->node_zones + ZONE_NORMAL;
  624. ret = __add_pages(zone, start_pfn, nr_pages);
  625. if (ret)
  626. printk("%s: Problem encountered in __add_pages() as ret=%d\n",
  627. __FUNCTION__, ret);
  628. return ret;
  629. }
  630. int remove_memory(u64 start, u64 size)
  631. {
  632. return -EINVAL;
  633. }
  634. EXPORT_SYMBOL_GPL(remove_memory);
  635. #endif