init.c 16 KB

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