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