upgrade to fedora-2.6.12-1.1398.FC4 + vserver 2.0.rc7
[linux-2.6.git] / arch / x86_64 / mm / init.c
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
9 #include <linux/config.h>
10 #include <linux/module.h>
11 #include <linux/signal.h>
12 #include <linux/sched.h>
13 #include <linux/kernel.h>
14 #include <linux/errno.h>
15 #include <linux/string.h>
16 #include <linux/types.h>
17 #include <linux/ptrace.h>
18 #include <linux/mman.h>
19 #include <linux/mm.h>
20 #include <linux/swap.h>
21 #include <linux/smp.h>
22 #include <linux/init.h>
23 #include <linux/pagemap.h>
24 #include <linux/bootmem.h>
25 #include <linux/proc_fs.h>
26 #include <linux/module.h>
27
28 #include <asm/processor.h>
29 #include <asm/system.h>
30 #include <asm/uaccess.h>
31 #include <asm/pgtable.h>
32 #include <asm/pgalloc.h>
33 #include <asm/dma.h>
34 #include <asm/fixmap.h>
35 #include <asm/e820.h>
36 #include <asm/apic.h>
37 #include <asm/tlb.h>
38 #include <asm/mmu_context.h>
39 #include <asm/proto.h>
40 #include <asm/smp.h>
41
42 #ifndef Dprintk
43 #define Dprintk(x...)
44 #endif
45
46 #ifdef CONFIG_GART_IOMMU
47 extern int swiotlb;
48 #endif
49
50 extern char _stext[];
51
52 DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
53
54 /*
55  * NOTE: pagetable_init alloc all the fixmap pagetables contiguous on the
56  * physical space so we can cache the place of the first one and move
57  * around without checking the pgd every time.
58  */
59
60 void show_mem(void)
61 {
62         int i, total = 0, reserved = 0;
63         int shared = 0, cached = 0;
64         pg_data_t *pgdat;
65         struct page *page;
66
67         printk("Mem-info:\n");
68         show_free_areas();
69         printk("Free swap:       %6ldkB\n", nr_swap_pages<<(PAGE_SHIFT-10));
70
71         for_each_pgdat(pgdat) {
72                for (i = 0; i < pgdat->node_spanned_pages; ++i) {
73                         page = pfn_to_page(pgdat->node_start_pfn + i);
74                         total++;
75                        if (PageReserved(page))
76                         reserved++;
77                        else if (PageSwapCache(page))
78                         cached++;
79                        else if (page_count(page))
80                                shared += page_count(page) - 1;
81                }
82         }
83         printk("%d pages of RAM\n", total);
84         printk("%d reserved pages\n",reserved);
85         printk("%d pages shared\n",shared);
86         printk("%d pages swap cached\n",cached);
87 }
88
89 EXPORT_SYMBOL_GPL(show_mem);
90
91 /* References to section boundaries */
92
93 extern char _text, _etext, _edata, __bss_start, _end[];
94 extern char __init_begin, __init_end;
95
96 int after_bootmem;
97
98 static void *spp_getpage(void)
99
100         void *ptr;
101         if (after_bootmem)
102                 ptr = (void *) get_zeroed_page(GFP_ATOMIC); 
103         else
104                 ptr = alloc_bootmem_pages(PAGE_SIZE);
105         if (!ptr || ((unsigned long)ptr & ~PAGE_MASK))
106                 panic("set_pte_phys: cannot allocate page data %s\n", after_bootmem?"after bootmem":"");
107
108         Dprintk("spp_getpage %p\n", ptr);
109         return ptr;
110
111
112 static void set_pte_phys(unsigned long vaddr,
113                          unsigned long phys, pgprot_t prot)
114 {
115         pgd_t *pgd;
116         pud_t *pud;
117         pmd_t *pmd;
118         pte_t *pte, new_pte;
119
120         Dprintk("set_pte_phys %lx to %lx\n", vaddr, phys);
121
122         pgd = pgd_offset_k(vaddr);
123         if (pgd_none(*pgd)) {
124                 printk("PGD FIXMAP MISSING, it should be setup in head.S!\n");
125                 return;
126         }
127         pud = pud_offset(pgd, vaddr);
128         if (pud_none(*pud)) {
129                 pmd = (pmd_t *) spp_getpage(); 
130                 set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE | _PAGE_USER));
131                 if (pmd != pmd_offset(pud, 0)) {
132                         printk("PAGETABLE BUG #01! %p <-> %p\n", pmd, pmd_offset(pud,0));
133                         return;
134                 }
135         }
136         pmd = pmd_offset(pud, vaddr);
137         if (pmd_none(*pmd)) {
138                 pte = (pte_t *) spp_getpage();
139                 set_pmd(pmd, __pmd(__pa(pte) | _KERNPG_TABLE | _PAGE_USER));
140                 if (pte != pte_offset_kernel(pmd, 0)) {
141                         printk("PAGETABLE BUG #02!\n");
142                         return;
143                 }
144         }
145         new_pte = pfn_pte(phys >> PAGE_SHIFT, prot);
146
147         pte = pte_offset_kernel(pmd, vaddr);
148         if (!pte_none(*pte) &&
149             pte_val(*pte) != (pte_val(new_pte) & __supported_pte_mask))
150                 pte_ERROR(*pte);
151         set_pte(pte, new_pte);
152
153         /*
154          * It's enough to flush this one mapping.
155          * (PGE mappings get flushed as well)
156          */
157         __flush_tlb_one(vaddr);
158 }
159
160 /* NOTE: this is meant to be run only at boot */
161 void __set_fixmap (enum fixed_addresses idx, unsigned long phys, pgprot_t prot)
162 {
163         unsigned long address = __fix_to_virt(idx);
164
165         if (idx >= __end_of_fixed_addresses) {
166                 printk("Invalid __set_fixmap\n");
167                 return;
168         }
169         set_pte_phys(address, phys, prot);
170 }
171
172 unsigned long __initdata table_start, table_end; 
173
174 extern pmd_t temp_boot_pmds[]; 
175
176 static  struct temp_map { 
177         pmd_t *pmd;
178         void  *address; 
179         int    allocated; 
180 } temp_mappings[] __initdata = { 
181         { &temp_boot_pmds[0], (void *)(40UL * 1024 * 1024) },
182         { &temp_boot_pmds[1], (void *)(42UL * 1024 * 1024) }, 
183         {}
184 }; 
185
186 static __init void *alloc_low_page(int *index, unsigned long *phys) 
187
188         struct temp_map *ti;
189         int i; 
190         unsigned long pfn = table_end++, paddr; 
191         void *adr;
192
193         if (pfn >= end_pfn) 
194                 panic("alloc_low_page: ran out of memory"); 
195         for (i = 0; temp_mappings[i].allocated; i++) {
196                 if (!temp_mappings[i].pmd) 
197                         panic("alloc_low_page: ran out of temp mappings"); 
198         } 
199         ti = &temp_mappings[i];
200         paddr = (pfn << PAGE_SHIFT) & PMD_MASK; 
201         set_pmd(ti->pmd, __pmd(paddr | _KERNPG_TABLE | _PAGE_PSE)); 
202         ti->allocated = 1; 
203         __flush_tlb();         
204         adr = ti->address + ((pfn << PAGE_SHIFT) & ~PMD_MASK); 
205         *index = i; 
206         *phys  = pfn * PAGE_SIZE;  
207         return adr; 
208
209
210 static __init void unmap_low_page(int i)
211
212         struct temp_map *ti = &temp_mappings[i];
213         set_pmd(ti->pmd, __pmd(0));
214         ti->allocated = 0; 
215
216
217 static void __init phys_pud_init(pud_t *pud, unsigned long address, unsigned long end)
218
219         long i, j; 
220
221         i = pud_index(address);
222         pud = pud + i;
223         for (; i < PTRS_PER_PUD; pud++, i++) {
224                 int map; 
225                 unsigned long paddr, pmd_phys;
226                 pmd_t *pmd;
227
228                 paddr = address + i*PUD_SIZE;
229                 if (paddr >= end) { 
230                         for (; i < PTRS_PER_PUD; i++, pud++) 
231                                 set_pud(pud, __pud(0)); 
232                         break;
233                 } 
234
235                 if (!e820_mapped(paddr, paddr+PUD_SIZE, 0)) { 
236                         set_pud(pud, __pud(0)); 
237                         continue;
238                 } 
239
240                 pmd = alloc_low_page(&map, &pmd_phys);
241                 set_pud(pud, __pud(pmd_phys | _KERNPG_TABLE));
242                 for (j = 0; j < PTRS_PER_PMD; pmd++, j++, paddr += PMD_SIZE) {
243                         unsigned long pe;
244
245                         if (paddr >= end) { 
246                                 for (; j < PTRS_PER_PMD; j++, pmd++)
247                                         set_pmd(pmd,  __pmd(0)); 
248                                 break;
249                 }
250                         pe = _PAGE_NX|_PAGE_PSE | _KERNPG_TABLE | _PAGE_GLOBAL | paddr;
251                         pe &= __supported_pte_mask;
252                         set_pmd(pmd, __pmd(pe));
253                 }
254                 unmap_low_page(map);
255         }
256         __flush_tlb();
257
258
259 static void __init find_early_table_space(unsigned long end)
260 {
261         unsigned long puds, pmds, tables;
262
263         puds = (end + PUD_SIZE - 1) >> PUD_SHIFT;
264         pmds = (end + PMD_SIZE - 1) >> PMD_SHIFT;
265         tables = round_up(puds * sizeof(pud_t), PAGE_SIZE) +
266                  round_up(pmds * sizeof(pmd_t), PAGE_SIZE);
267
268         table_start = find_e820_area(0x8000, __pa_symbol(&_text), tables);
269         if (table_start == -1UL)
270                 panic("Cannot find space for the kernel page tables");
271
272         table_start >>= PAGE_SHIFT;
273         table_end = table_start;
274 }
275
276 /* Setup the direct mapping of the physical memory at PAGE_OFFSET.
277    This runs before bootmem is initialized and gets pages directly from the 
278    physical memory. To access them they are temporarily mapped. */
279 void __init init_memory_mapping(unsigned long start, unsigned long end)
280
281         unsigned long next; 
282
283         Dprintk("init_memory_mapping\n");
284
285         /* 
286          * Find space for the kernel direct mapping tables.
287          * Later we should allocate these tables in the local node of the memory
288          * mapped.  Unfortunately this is done currently before the nodes are 
289          * discovered.
290          */
291         find_early_table_space(end);
292
293         start = (unsigned long)__va(start);
294         end = (unsigned long)__va(end);
295
296         for (; start < end; start = next) {
297                 int map;
298                 unsigned long pud_phys; 
299                 pud_t *pud = alloc_low_page(&map, &pud_phys);
300                 next = start + PGDIR_SIZE;
301                 if (next > end) 
302                         next = end; 
303                 phys_pud_init(pud, __pa(start), __pa(next));
304                 set_pgd(pgd_offset_k(start), mk_kernel_pgd(pud_phys));
305                 unmap_low_page(map);   
306         } 
307
308         asm volatile("movq %%cr4,%0" : "=r" (mmu_cr4_features));
309         __flush_tlb_all();
310         early_printk("kernel direct mapping tables upto %lx @ %lx-%lx\n", end, 
311                table_start<<PAGE_SHIFT, 
312                table_end<<PAGE_SHIFT);
313 }
314
315 extern struct x8664_pda cpu_pda[NR_CPUS];
316
317 /* Assumes all CPUs still execute in init_mm */
318 void zap_low_mappings(void)
319 {
320         pgd_t *pgd = pgd_offset_k(0UL);
321         pgd_clear(pgd);
322         flush_tlb_all();
323 }
324
325 #ifndef CONFIG_DISCONTIGMEM
326 void __init paging_init(void)
327 {
328         {
329                 unsigned long zones_size[MAX_NR_ZONES] = {0, 0, 0};
330                 unsigned int max_dma;
331
332                 max_dma = virt_to_phys((char *)MAX_DMA_ADDRESS) >> PAGE_SHIFT;
333
334                 if (end_pfn < max_dma)
335                         zones_size[ZONE_DMA] = end_pfn;
336                 else {
337                         zones_size[ZONE_DMA] = max_dma;
338                         zones_size[ZONE_NORMAL] = end_pfn - max_dma;
339                 }
340                 free_area_init(zones_size);
341         }
342         return;
343 }
344 #endif
345
346 /* Unmap a kernel mapping if it exists. This is useful to avoid prefetches
347    from the CPU leading to inconsistent cache lines. address and size
348    must be aligned to 2MB boundaries. 
349    Does nothing when the mapping doesn't exist. */
350 void __init clear_kernel_mapping(unsigned long address, unsigned long size) 
351 {
352         unsigned long end = address + size;
353
354         BUG_ON(address & ~LARGE_PAGE_MASK);
355         BUG_ON(size & ~LARGE_PAGE_MASK); 
356         
357         for (; address < end; address += LARGE_PAGE_SIZE) { 
358                 pgd_t *pgd = pgd_offset_k(address);
359                 pud_t *pud;
360                 pmd_t *pmd;
361                 if (pgd_none(*pgd))
362                         continue;
363                 pud = pud_offset(pgd, address);
364                 if (pud_none(*pud))
365                         continue; 
366                 pmd = pmd_offset(pud, address);
367                 if (!pmd || pmd_none(*pmd))
368                         continue; 
369                 if (0 == (pmd_val(*pmd) & _PAGE_PSE)) { 
370                         /* Could handle this, but it should not happen currently. */
371                         printk(KERN_ERR 
372                "clear_kernel_mapping: mapping has been split. will leak memory\n"); 
373                         pmd_ERROR(*pmd); 
374                 }
375                 set_pmd(pmd, __pmd(0));                 
376         }
377         __flush_tlb_all();
378
379
380 static inline int page_is_ram (unsigned long pagenr)
381 {
382         int i;
383
384         for (i = 0; i < e820.nr_map; i++) {
385                 unsigned long addr, end;
386
387                 if (e820.map[i].type != E820_RAM)       /* not usable memory */
388                         continue;
389                 /*
390                  *      !!!FIXME!!! Some BIOSen report areas as RAM that
391                  *      are not. Notably the 640->1Mb area. We need a sanity
392                  *      check here.
393                  */
394                 addr = (e820.map[i].addr+PAGE_SIZE-1) >> PAGE_SHIFT;
395                 end = (e820.map[i].addr+e820.map[i].size) >> PAGE_SHIFT;
396                 if  ((pagenr >= addr) && (pagenr < end))
397                         return 1;
398         }
399         return 0;
400 }
401
402 extern int swiotlb_force;
403
404 unsigned long next_ram_page (unsigned long pagenr)
405 {
406         int i;
407         unsigned long min_pageno = ULONG_MAX;
408
409         pagenr++;
410
411         for (i = 0; i < e820.nr_map; i++) {
412                 unsigned long addr, end;
413
414                 if (e820.map[i].type != E820_RAM)       /* not usable memory */
415                         continue;
416                 /*
417                  *      !!!FIXME!!! Some BIOSen report areas as RAM that
418                  *      are not. Notably the 640->1Mb area. We need a sanity
419                  *      check here.
420                  */
421                 addr = (e820.map[i].addr+PAGE_SIZE-1) >> PAGE_SHIFT;
422                 end = (e820.map[i].addr+e820.map[i].size) >> PAGE_SHIFT;
423                 if  ((pagenr >= addr) && (pagenr < end))
424                         return pagenr;
425                 if ((pagenr < addr) && (addr < min_pageno))
426                         min_pageno = addr;
427         }
428         return min_pageno;
429 }
430
431 EXPORT_SYMBOL_GPL(next_ram_page);
432
433 /*
434  * devmem_is_allowed() checks to see if /dev/mem access to a certain address is
435  * valid. The argument is a physical page number.
436  *
437  *
438  * On x86-64, access has to be given to the first megabyte of ram because that area
439  * contains bios code and data regions used by X and dosemu and similar apps.
440  * Access has to be given to non-kernel-ram areas as well, these contain the PCI
441  * mmio resources as well as potential bios/acpi data regions.
442  */
443 int devmem_is_allowed(unsigned long pagenr)
444 {
445         if (pagenr <= 256)
446                 return 1;
447         if (!page_is_ram(pagenr))
448                 return 1;
449         return 0;
450 }
451
452
453 EXPORT_SYMBOL_GPL(page_is_ram);
454
455 static struct kcore_list kcore_mem, kcore_vmalloc, kcore_kernel, kcore_modules,
456                          kcore_vsyscall;
457
458 void __init mem_init(void)
459 {
460         int codesize, reservedpages, datasize, initsize;
461         int tmp;
462
463 #ifdef CONFIG_SWIOTLB
464         if (!iommu_aperture &&
465             (end_pfn >= 0xffffffff>>PAGE_SHIFT || force_iommu))
466                swiotlb = 1;
467         if (swiotlb)
468                 swiotlb_init(); 
469 #endif
470
471         /* How many end-of-memory variables you have, grandma! */
472         max_low_pfn = end_pfn;
473         max_pfn = end_pfn;
474         num_physpages = end_pfn;
475         high_memory = (void *) __va(end_pfn * PAGE_SIZE);
476
477         /* clear the zero-page */
478         memset(empty_zero_page, 0, PAGE_SIZE);
479
480         reservedpages = 0;
481
482         /* this will put all low memory onto the freelists */
483 #ifdef CONFIG_DISCONTIGMEM
484         totalram_pages += numa_free_all_bootmem();
485         tmp = 0;
486         /* should count reserved pages here for all nodes */ 
487 #else
488         max_mapnr = end_pfn;
489         if (!mem_map) BUG();
490
491         totalram_pages += free_all_bootmem();
492
493         for (tmp = 0; tmp < end_pfn; tmp++)
494                 /*
495                  * Only count reserved RAM pages
496                  */
497                 if (page_is_ram(tmp) && PageReserved(pfn_to_page(tmp)))
498                         reservedpages++;
499 #endif
500
501         after_bootmem = 1;
502
503         codesize =  (unsigned long) &_etext - (unsigned long) &_text;
504         datasize =  (unsigned long) &_edata - (unsigned long) &_etext;
505         initsize =  (unsigned long) &__init_end - (unsigned long) &__init_begin;
506
507         /* Register memory areas for /proc/kcore */
508         kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT); 
509         kclist_add(&kcore_vmalloc, (void *)VMALLOC_START, 
510                    VMALLOC_END-VMALLOC_START);
511         kclist_add(&kcore_kernel, &_stext, _end - _stext);
512         kclist_add(&kcore_modules, (void *)MODULES_VADDR, MODULES_LEN);
513         kclist_add(&kcore_vsyscall, (void *)VSYSCALL_START, 
514                                  VSYSCALL_END - VSYSCALL_START);
515
516         printk("Memory: %luk/%luk available (%dk kernel code, %dk reserved, %dk data, %dk init)\n",
517                 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
518                 end_pfn << (PAGE_SHIFT-10),
519                 codesize >> 10,
520                 reservedpages << (PAGE_SHIFT-10),
521                 datasize >> 10,
522                 initsize >> 10);
523
524         /*
525          * Subtle. SMP is doing its boot stuff late (because it has to
526          * fork idle threads) - but it also needs low mappings for the
527          * protected-mode entry to work. We zap these entries only after
528          * the WP-bit has been tested.
529          */
530 #ifndef CONFIG_SMP
531         zap_low_mappings();
532 #endif
533 }
534
535 extern char __initdata_begin[], __initdata_end[];
536
537 void free_initmem(void)
538 {
539         unsigned long addr;
540
541         addr = (unsigned long)(&__init_begin);
542         for (; addr < (unsigned long)(&__init_end); addr += PAGE_SIZE) {
543                 ClearPageReserved(virt_to_page(addr));
544                 set_page_count(virt_to_page(addr), 1);
545                 memset((void *)(addr & ~(PAGE_SIZE-1)), 0xcc, PAGE_SIZE); 
546                 free_page(addr);
547                 totalram_pages++;
548         }
549         memset(__initdata_begin, 0xba, __initdata_end - __initdata_begin);
550         printk ("Freeing unused kernel memory: %luk freed\n", (&__init_end - &__init_begin) >> 10);
551 }
552
553 #ifdef CONFIG_BLK_DEV_INITRD
554 void free_initrd_mem(unsigned long start, unsigned long end)
555 {
556         if (start < (unsigned long)&_end)
557                 return;
558         printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
559         for (; start < end; start += PAGE_SIZE) {
560                 ClearPageReserved(virt_to_page(start));
561                 set_page_count(virt_to_page(start), 1);
562                 free_page(start);
563                 totalram_pages++;
564         }
565 }
566 #endif
567
568 void __init reserve_bootmem_generic(unsigned long phys, unsigned len) 
569
570         /* Should check here against the e820 map to avoid double free */ 
571 #ifdef CONFIG_DISCONTIGMEM
572         int nid = phys_to_nid(phys);
573         reserve_bootmem_node(NODE_DATA(nid), phys, len);
574 #else                   
575         reserve_bootmem(phys, len);    
576 #endif
577 }
578
579 int kern_addr_valid(unsigned long addr) 
580
581         unsigned long above = ((long)addr) >> __VIRTUAL_MASK_SHIFT;
582        pgd_t *pgd;
583        pud_t *pud;
584        pmd_t *pmd;
585        pte_t *pte;
586
587         if (above != 0 && above != -1UL)
588                 return 0; 
589         
590         pgd = pgd_offset_k(addr);
591         if (pgd_none(*pgd))
592                 return 0;
593
594         pud = pud_offset(pgd, addr);
595         if (pud_none(*pud))
596                 return 0; 
597
598         pmd = pmd_offset(pud, addr);
599         if (pmd_none(*pmd))
600                 return 0;
601         if (pmd_large(*pmd))
602                 return pfn_valid(pmd_pfn(*pmd));
603
604         pte = pte_offset_kernel(pmd, addr);
605         if (pte_none(*pte))
606                 return 0;
607         return pfn_valid(pte_pfn(*pte));
608 }
609 EXPORT_SYMBOL_GPL(kern_addr_valid);
610
611 #ifdef CONFIG_SYSCTL
612 #include <linux/sysctl.h>
613
614 extern int exception_trace, page_fault_trace;
615
616 static ctl_table debug_table2[] = {
617         { 99, "exception-trace", &exception_trace, sizeof(int), 0644, NULL,
618           proc_dointvec },
619 #ifdef CONFIG_CHECKING
620         { 100, "page-fault-trace", &page_fault_trace, sizeof(int), 0644, NULL,
621           proc_dointvec },
622 #endif
623         { 0, }
624 }; 
625
626 static ctl_table debug_root_table2[] = { 
627         { .ctl_name = CTL_DEBUG, .procname = "debug", .mode = 0555, 
628            .child = debug_table2 }, 
629         { 0 }, 
630 }; 
631
632 static __init int x8664_sysctl_init(void)
633
634         register_sysctl_table(debug_root_table2, 1);
635         return 0;
636 }
637 __initcall(x8664_sysctl_init);
638 #endif
639
640 /* A pseudo VMAs to allow ptrace access for the vsyscall page.   This only
641    covers the 64bit vsyscall page now. 32bit has a real VMA now and does
642    not need special handling anymore. */
643
644 static struct vm_area_struct gate_vma = {
645         .vm_start = VSYSCALL_START,
646         .vm_end = VSYSCALL_END,
647         .vm_page_prot = PAGE_READONLY
648 };
649
650 struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
651 {
652 #ifdef CONFIG_IA32_EMULATION
653         if (test_tsk_thread_flag(tsk, TIF_IA32))
654                 return NULL;
655 #endif
656         return &gate_vma;
657 }
658
659 int in_gate_area(struct task_struct *task, unsigned long addr)
660 {
661         struct vm_area_struct *vma = get_gate_vma(task);
662         if (!vma)
663                 return 0;
664         return (addr >= vma->vm_start) && (addr < vma->vm_end);
665 }
666
667 /* Use this when you have no reliable task/vma, typically from interrupt
668  * context.  It is less reliable than using the task's vma and may give
669  * false positives.
670  */
671 int in_gate_area_no_task(unsigned long addr)
672 {
673         return (addr >= VSYSCALL_START) && (addr < VSYSCALL_END);
674 }