Pull bugzilla-7200 into release branch
[linux-2.6] / arch / um / kernel / mem.c
1 /*
2  * Copyright (C) 2000 - 2003 Jeff Dike (jdike@addtoit.com)
3  * Licensed under the GPL
4  */
5
6 #include "linux/stddef.h"
7 #include "linux/kernel.h"
8 #include "linux/mm.h"
9 #include "linux/bootmem.h"
10 #include "linux/swap.h"
11 #include "linux/highmem.h"
12 #include "linux/gfp.h"
13 #include "asm/page.h"
14 #include "asm/fixmap.h"
15 #include "asm/pgalloc.h"
16 #include "user_util.h"
17 #include "kern_util.h"
18 #include "kern.h"
19 #include "mem_user.h"
20 #include "uml_uaccess.h"
21 #include "os.h"
22 #include "linux/types.h"
23 #include "linux/string.h"
24 #include "init.h"
25 #include "kern_constants.h"
26
27 /* allocated in paging_init, zeroed in mem_init, and unchanged thereafter */
28 unsigned long *empty_zero_page = NULL;
29 /* allocated in paging_init and unchanged thereafter */
30 unsigned long *empty_bad_page = NULL;
31 pgd_t swapper_pg_dir[PTRS_PER_PGD];
32 unsigned long long highmem;
33 int kmalloc_ok = 0;
34
35 static unsigned long brk_end;
36
37 void unmap_physmem(void)
38 {
39         os_unmap_memory((void *) brk_end, uml_reserved - brk_end);
40 }
41
42 static void map_cb(void *unused)
43 {
44         map_memory(brk_end, __pa(brk_end), uml_reserved - brk_end, 1, 1, 0);
45 }
46
47 #ifdef CONFIG_HIGHMEM
48 static void setup_highmem(unsigned long highmem_start,
49                           unsigned long highmem_len)
50 {
51         struct page *page;
52         unsigned long highmem_pfn;
53         int i;
54
55         highmem_pfn = __pa(highmem_start) >> PAGE_SHIFT;
56         for(i = 0; i < highmem_len >> PAGE_SHIFT; i++){
57                 page = &mem_map[highmem_pfn + i];
58                 ClearPageReserved(page);
59                 init_page_count(page);
60                 __free_page(page);
61         }
62 }
63 #endif
64
65 void mem_init(void)
66 {
67         max_low_pfn = (high_physmem - uml_physmem) >> PAGE_SHIFT;
68
69         /* clear the zero-page */
70         memset((void *) empty_zero_page, 0, PAGE_SIZE);
71
72         /* Map in the area just after the brk now that kmalloc is about
73          * to be turned on.
74          */
75         brk_end = (unsigned long) UML_ROUND_UP(sbrk(0));
76         map_cb(NULL);
77         initial_thread_cb(map_cb, NULL);
78         free_bootmem(__pa(brk_end), uml_reserved - brk_end);
79         uml_reserved = brk_end;
80
81         /* this will put all low memory onto the freelists */
82         totalram_pages = free_all_bootmem();
83 #ifdef CONFIG_HIGHMEM
84         totalhigh_pages = highmem >> PAGE_SHIFT;
85         totalram_pages += totalhigh_pages;
86 #endif
87         num_physpages = totalram_pages;
88         max_pfn = totalram_pages;
89         printk(KERN_INFO "Memory: %luk available\n", 
90                (unsigned long) nr_free_pages() << (PAGE_SHIFT-10));
91         kmalloc_ok = 1;
92
93 #ifdef CONFIG_HIGHMEM
94         setup_highmem(end_iomem, highmem);
95 #endif
96 }
97
98 /*
99  * Create a page table and place a pointer to it in a middle page
100  * directory entry.
101  */
102 static void __init one_page_table_init(pmd_t *pmd)
103 {
104         if (pmd_none(*pmd)) {
105                 pte_t *pte = (pte_t *) alloc_bootmem_low_pages(PAGE_SIZE);
106                 set_pmd(pmd, __pmd(_KERNPG_TABLE +
107                                            (unsigned long) __pa(pte)));
108                 if (pte != pte_offset_kernel(pmd, 0))
109                         BUG();
110         }
111 }
112
113 static void __init one_md_table_init(pud_t *pud)
114 {
115 #ifdef CONFIG_3_LEVEL_PGTABLES
116         pmd_t *pmd_table = (pmd_t *) alloc_bootmem_low_pages(PAGE_SIZE);
117         set_pud(pud, __pud(_KERNPG_TABLE + (unsigned long) __pa(pmd_table)));
118         if (pmd_table != pmd_offset(pud, 0))
119                 BUG();
120 #endif
121 }
122
123 static void __init fixrange_init(unsigned long start, unsigned long end, 
124                                  pgd_t *pgd_base)
125 {
126         pgd_t *pgd;
127         pud_t *pud;
128         pmd_t *pmd;
129         int i, j;
130         unsigned long vaddr;
131
132         vaddr = start;
133         i = pgd_index(vaddr);
134         j = pmd_index(vaddr);
135         pgd = pgd_base + i;
136
137         for ( ; (i < PTRS_PER_PGD) && (vaddr < end); pgd++, i++) {
138                 pud = pud_offset(pgd, vaddr);
139                 if (pud_none(*pud))
140                         one_md_table_init(pud);
141                 pmd = pmd_offset(pud, vaddr);
142                 for (; (j < PTRS_PER_PMD) && (vaddr != end); pmd++, j++) {
143                         one_page_table_init(pmd);
144                         vaddr += PMD_SIZE;
145                 }
146                 j = 0;
147         }
148 }
149
150 #ifdef CONFIG_HIGHMEM
151 pte_t *kmap_pte;
152 pgprot_t kmap_prot;
153
154 #define kmap_get_fixmap_pte(vaddr)                                      \
155         pte_offset_kernel(pmd_offset(pud_offset(pgd_offset_k(vaddr), (vaddr)),\
156                           (vaddr)), (vaddr))
157
158 static void __init kmap_init(void)
159 {
160         unsigned long kmap_vstart;
161
162         /* cache the first kmap pte */
163         kmap_vstart = __fix_to_virt(FIX_KMAP_BEGIN);
164         kmap_pte = kmap_get_fixmap_pte(kmap_vstart);
165
166         kmap_prot = PAGE_KERNEL;
167 }
168
169 static void init_highmem(void)
170 {
171         pgd_t *pgd;
172         pud_t *pud;
173         pmd_t *pmd;
174         pte_t *pte;
175         unsigned long vaddr;
176
177         /*
178          * Permanent kmaps:
179          */
180         vaddr = PKMAP_BASE;
181         fixrange_init(vaddr, vaddr + PAGE_SIZE*LAST_PKMAP, swapper_pg_dir);
182
183         pgd = swapper_pg_dir + pgd_index(vaddr);
184         pud = pud_offset(pgd, vaddr);
185         pmd = pmd_offset(pud, vaddr);
186         pte = pte_offset_kernel(pmd, vaddr);
187         pkmap_page_table = pte;
188
189         kmap_init();
190 }
191 #endif /* CONFIG_HIGHMEM */
192
193 static void __init fixaddr_user_init( void)
194 {
195 #ifdef CONFIG_ARCH_REUSE_HOST_VSYSCALL_AREA
196         long size = FIXADDR_USER_END - FIXADDR_USER_START;
197         pgd_t *pgd;
198         pud_t *pud;
199         pmd_t *pmd;
200         pte_t *pte;
201         unsigned long paddr, vaddr = FIXADDR_USER_START;
202
203         if (  ! size )
204                 return;
205
206         fixrange_init( FIXADDR_USER_START, FIXADDR_USER_END, swapper_pg_dir);
207         paddr = (unsigned long)alloc_bootmem_low_pages( size);
208         memcpy( (void *)paddr, (void *)FIXADDR_USER_START, size);
209         paddr = __pa(paddr);
210         for ( ; size > 0; size-=PAGE_SIZE, vaddr+=PAGE_SIZE, paddr+=PAGE_SIZE){
211                 pgd = swapper_pg_dir + pgd_index(vaddr);
212                 pud = pud_offset(pgd, vaddr);
213                 pmd = pmd_offset(pud, vaddr);
214                 pte = pte_offset_kernel(pmd, vaddr);
215                 pte_set_val( (*pte), paddr, PAGE_READONLY);
216         }
217 #endif
218 }
219
220 void paging_init(void)
221 {
222         unsigned long zones_size[MAX_NR_ZONES], vaddr;
223         int i;
224
225         empty_zero_page = (unsigned long *) alloc_bootmem_low_pages(PAGE_SIZE);
226         empty_bad_page = (unsigned long *) alloc_bootmem_low_pages(PAGE_SIZE);
227         for(i = 0; i < ARRAY_SIZE(zones_size); i++)
228                 zones_size[i] = 0;
229
230         zones_size[ZONE_NORMAL] = (end_iomem >> PAGE_SHIFT) -
231                 (uml_physmem >> PAGE_SHIFT);
232 #ifdef CONFIG_HIGHMEM
233         zones_size[ZONE_HIGHMEM] = highmem >> PAGE_SHIFT;
234 #endif
235         free_area_init(zones_size);
236
237         /*
238          * Fixed mappings, only the page table structure has to be
239          * created - mappings will be set by set_fixmap():
240          */
241         vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK;
242         fixrange_init(vaddr, FIXADDR_TOP, swapper_pg_dir);
243
244         fixaddr_user_init();
245
246 #ifdef CONFIG_HIGHMEM
247         init_highmem();
248 #endif
249 }
250
251 struct page *arch_validate(struct page *page, gfp_t mask, int order)
252 {
253         unsigned long addr, zero = 0;
254         int i;
255
256  again:
257         if(page == NULL)
258                 return page;
259         if(PageHighMem(page))
260                 return page;
261
262         addr = (unsigned long) page_address(page);
263         for(i = 0; i < (1 << order); i++){
264                 current->thread.fault_addr = (void *) addr;
265                 if(__do_copy_to_user((void __user *) addr, &zero,
266                                      sizeof(zero),
267                                      &current->thread.fault_addr,
268                                      &current->thread.fault_catcher)){
269                         if(!(mask & __GFP_WAIT))
270                                 return NULL;
271                         else break;
272                 }
273                 addr += PAGE_SIZE;
274         }
275
276         if(i == (1 << order))
277                 return page;
278         page = alloc_pages(mask, order);
279         goto again;
280 }
281
282 /* This can't do anything because nothing in the kernel image can be freed
283  * since it's not in kernel physical memory.
284  */
285
286 void free_initmem(void)
287 {
288 }
289
290 #ifdef CONFIG_BLK_DEV_INITRD
291 void free_initrd_mem(unsigned long start, unsigned long end)
292 {
293         if (start < end)
294                 printk ("Freeing initrd memory: %ldk freed\n", 
295                         (end - start) >> 10);
296         for (; start < end; start += PAGE_SIZE) {
297                 ClearPageReserved(virt_to_page(start));
298                 init_page_count(virt_to_page(start));
299                 free_page(start);
300                 totalram_pages++;
301         }
302 }
303 #endif
304
305 void show_mem(void)
306 {
307         int pfn, total = 0, reserved = 0;
308         int shared = 0, cached = 0;
309         int highmem = 0;
310         struct page *page;
311
312         printk("Mem-info:\n");
313         show_free_areas();
314         printk("Free swap:       %6ldkB\n", nr_swap_pages<<(PAGE_SHIFT-10));
315         pfn = max_mapnr;
316         while(pfn-- > 0) {
317                 page = pfn_to_page(pfn);
318                 total++;
319                 if(PageHighMem(page))
320                         highmem++;
321                 if(PageReserved(page))
322                         reserved++;
323                 else if(PageSwapCache(page))
324                         cached++;
325                 else if(page_count(page))
326                         shared += page_count(page) - 1;
327         }
328         printk("%d pages of RAM\n", total);
329         printk("%d pages of HIGHMEM\n", highmem);
330         printk("%d reserved pages\n", reserved);
331         printk("%d pages shared\n", shared);
332         printk("%d pages swap cached\n", cached);
333 }
334
335 /*
336  * Allocate and free page tables.
337  */
338
339 pgd_t *pgd_alloc(struct mm_struct *mm)
340 {
341         pgd_t *pgd = (pgd_t *)__get_free_page(GFP_KERNEL);
342
343         if (pgd) {
344                 memset(pgd, 0, USER_PTRS_PER_PGD * sizeof(pgd_t));
345                 memcpy(pgd + USER_PTRS_PER_PGD, 
346                        swapper_pg_dir + USER_PTRS_PER_PGD, 
347                        (PTRS_PER_PGD - USER_PTRS_PER_PGD) * sizeof(pgd_t));
348         }
349         return pgd;
350 }
351
352 void pgd_free(pgd_t *pgd)
353 {
354         free_page((unsigned long) pgd);
355 }
356
357 pte_t *pte_alloc_one_kernel(struct mm_struct *mm, unsigned long address)
358 {
359         pte_t *pte;
360
361         pte = (pte_t *)__get_free_page(GFP_KERNEL|__GFP_REPEAT|__GFP_ZERO);
362         return pte;
363 }
364
365 struct page *pte_alloc_one(struct mm_struct *mm, unsigned long address)
366 {
367         struct page *pte;
368
369         pte = alloc_page(GFP_KERNEL|__GFP_REPEAT|__GFP_ZERO);
370         return pte;
371 }