Merge branch 'upstream-linus' of master.kernel.org:/pub/scm/linux/kernel/git/jgarzik...
[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 extern char __binary_start;
28
29 /* Changed during early boot */
30 unsigned long *empty_zero_page = NULL;
31 unsigned long *empty_bad_page = NULL;
32 pgd_t swapper_pg_dir[PTRS_PER_PGD];
33 unsigned long long highmem;
34 int kmalloc_ok = 0;
35
36 static unsigned long brk_end;
37
38 void unmap_physmem(void)
39 {
40         os_unmap_memory((void *) brk_end, uml_reserved - brk_end);
41 }
42
43 static void map_cb(void *unused)
44 {
45         map_memory(brk_end, __pa(brk_end), uml_reserved - brk_end, 1, 1, 0);
46 }
47
48 #ifdef CONFIG_HIGHMEM
49 static void setup_highmem(unsigned long highmem_start,
50                           unsigned long highmem_len)
51 {
52         struct page *page;
53         unsigned long highmem_pfn;
54         int i;
55
56         highmem_pfn = __pa(highmem_start) >> PAGE_SHIFT;
57         for(i = 0; i < highmem_len >> PAGE_SHIFT; i++){
58                 page = &mem_map[highmem_pfn + i];
59                 ClearPageReserved(page);
60                 init_page_count(page);
61                 __free_page(page);
62         }
63 }
64 #endif
65
66 void mem_init(void)
67 {
68         unsigned long start;
69
70         max_low_pfn = (high_physmem - uml_physmem) >> PAGE_SHIFT;
71
72         /* clear the zero-page */
73         memset((void *) empty_zero_page, 0, PAGE_SIZE);
74
75         /* Map in the area just after the brk now that kmalloc is about
76          * to be turned on.
77          */
78         brk_end = (unsigned long) UML_ROUND_UP(sbrk(0));
79         map_cb(NULL);
80         initial_thread_cb(map_cb, NULL);
81         free_bootmem(__pa(brk_end), uml_reserved - brk_end);
82         uml_reserved = brk_end;
83
84         /* Fill in any hole at the start of the binary */
85         start = (unsigned long) &__binary_start & PAGE_MASK;
86         if(uml_physmem != start){
87                 map_memory(uml_physmem, __pa(uml_physmem), start - uml_physmem,
88                            1, 1, 0);
89         }
90
91         /* this will put all low memory onto the freelists */
92         totalram_pages = free_all_bootmem();
93         totalhigh_pages = highmem >> PAGE_SHIFT;
94         totalram_pages += totalhigh_pages;
95         num_physpages = totalram_pages;
96         max_pfn = totalram_pages;
97         printk(KERN_INFO "Memory: %luk available\n", 
98                (unsigned long) nr_free_pages() << (PAGE_SHIFT-10));
99         kmalloc_ok = 1;
100
101 #ifdef CONFIG_HIGHMEM
102         setup_highmem(end_iomem, highmem);
103 #endif
104 }
105
106 /*
107  * Create a page table and place a pointer to it in a middle page
108  * directory entry.
109  */
110 static void __init one_page_table_init(pmd_t *pmd)
111 {
112         if (pmd_none(*pmd)) {
113                 pte_t *pte = (pte_t *) alloc_bootmem_low_pages(PAGE_SIZE);
114                 set_pmd(pmd, __pmd(_KERNPG_TABLE +
115                                            (unsigned long) __pa(pte)));
116                 if (pte != pte_offset_kernel(pmd, 0))
117                         BUG();
118         }
119 }
120
121 static void __init one_md_table_init(pud_t *pud)
122 {
123 #ifdef CONFIG_3_LEVEL_PGTABLES
124         pmd_t *pmd_table = (pmd_t *) alloc_bootmem_low_pages(PAGE_SIZE);
125         set_pud(pud, __pud(_KERNPG_TABLE + (unsigned long) __pa(pmd_table)));
126         if (pmd_table != pmd_offset(pud, 0))
127                 BUG();
128 #endif
129 }
130
131 static void __init fixrange_init(unsigned long start, unsigned long end, 
132                                  pgd_t *pgd_base)
133 {
134         pgd_t *pgd;
135         pud_t *pud;
136         pmd_t *pmd;
137         int i, j;
138         unsigned long vaddr;
139
140         vaddr = start;
141         i = pgd_index(vaddr);
142         j = pmd_index(vaddr);
143         pgd = pgd_base + i;
144
145         for ( ; (i < PTRS_PER_PGD) && (vaddr < end); pgd++, i++) {
146                 pud = pud_offset(pgd, vaddr);
147                 if (pud_none(*pud))
148                         one_md_table_init(pud);
149                 pmd = pmd_offset(pud, vaddr);
150                 for (; (j < PTRS_PER_PMD) && (vaddr != end); pmd++, j++) {
151                         one_page_table_init(pmd);
152                         vaddr += PMD_SIZE;
153                 }
154                 j = 0;
155         }
156 }
157
158 #ifdef CONFIG_HIGHMEM
159 pte_t *kmap_pte;
160 pgprot_t kmap_prot;
161
162 #define kmap_get_fixmap_pte(vaddr)                                      \
163         pte_offset_kernel(pmd_offset(pud_offset(pgd_offset_k(vaddr), (vaddr)),\
164                           (vaddr)), (vaddr))
165
166 static void __init kmap_init(void)
167 {
168         unsigned long kmap_vstart;
169
170         /* cache the first kmap pte */
171         kmap_vstart = __fix_to_virt(FIX_KMAP_BEGIN);
172         kmap_pte = kmap_get_fixmap_pte(kmap_vstart);
173
174         kmap_prot = PAGE_KERNEL;
175 }
176
177 static void init_highmem(void)
178 {
179         pgd_t *pgd;
180         pud_t *pud;
181         pmd_t *pmd;
182         pte_t *pte;
183         unsigned long vaddr;
184
185         /*
186          * Permanent kmaps:
187          */
188         vaddr = PKMAP_BASE;
189         fixrange_init(vaddr, vaddr + PAGE_SIZE*LAST_PKMAP, swapper_pg_dir);
190
191         pgd = swapper_pg_dir + pgd_index(vaddr);
192         pud = pud_offset(pgd, vaddr);
193         pmd = pmd_offset(pud, vaddr);
194         pte = pte_offset_kernel(pmd, vaddr);
195         pkmap_page_table = pte;
196
197         kmap_init();
198 }
199 #endif /* CONFIG_HIGHMEM */
200
201 static void __init fixaddr_user_init( void)
202 {
203 #ifdef CONFIG_ARCH_REUSE_HOST_VSYSCALL_AREA
204         long size = FIXADDR_USER_END - FIXADDR_USER_START;
205         pgd_t *pgd;
206         pud_t *pud;
207         pmd_t *pmd;
208         pte_t *pte;
209         unsigned long paddr, vaddr = FIXADDR_USER_START;
210
211         if (  ! size )
212                 return;
213
214         fixrange_init( FIXADDR_USER_START, FIXADDR_USER_END, swapper_pg_dir);
215         paddr = (unsigned long)alloc_bootmem_low_pages( size);
216         memcpy( (void *)paddr, (void *)FIXADDR_USER_START, size);
217         paddr = __pa(paddr);
218         for ( ; size > 0; size-=PAGE_SIZE, vaddr+=PAGE_SIZE, paddr+=PAGE_SIZE){
219                 pgd = swapper_pg_dir + pgd_index(vaddr);
220                 pud = pud_offset(pgd, vaddr);
221                 pmd = pmd_offset(pud, vaddr);
222                 pte = pte_offset_kernel(pmd, vaddr);
223                 pte_set_val( (*pte), paddr, PAGE_READONLY);
224         }
225 #endif
226 }
227
228 void paging_init(void)
229 {
230         unsigned long zones_size[MAX_NR_ZONES], vaddr;
231         int i;
232
233         empty_zero_page = (unsigned long *) alloc_bootmem_low_pages(PAGE_SIZE);
234         empty_bad_page = (unsigned long *) alloc_bootmem_low_pages(PAGE_SIZE);
235         for(i=0;i<sizeof(zones_size)/sizeof(zones_size[0]);i++) 
236                 zones_size[i] = 0;
237         zones_size[ZONE_DMA] = (end_iomem >> PAGE_SHIFT) - (uml_physmem >> PAGE_SHIFT);
238         zones_size[ZONE_HIGHMEM] = highmem >> PAGE_SHIFT;
239         free_area_init(zones_size);
240
241         /*
242          * Fixed mappings, only the page table structure has to be
243          * created - mappings will be set by set_fixmap():
244          */
245         vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK;
246         fixrange_init(vaddr, FIXADDR_TOP, swapper_pg_dir);
247
248         fixaddr_user_init();
249
250 #ifdef CONFIG_HIGHMEM
251         init_highmem();
252 #endif
253 }
254
255 struct page *arch_validate(struct page *page, gfp_t mask, int order)
256 {
257         unsigned long addr, zero = 0;
258         int i;
259
260  again:
261         if(page == NULL) return(page);
262         if(PageHighMem(page)) return(page);
263
264         addr = (unsigned long) page_address(page);
265         for(i = 0; i < (1 << order); i++){
266                 current->thread.fault_addr = (void *) addr;
267                 if(__do_copy_to_user((void __user *) addr, &zero,
268                                      sizeof(zero),
269                                      &current->thread.fault_addr,
270                                      &current->thread.fault_catcher)){
271                         if(!(mask & __GFP_WAIT)) return(NULL);
272                         else break;
273                 }
274                 addr += PAGE_SIZE;
275         }
276
277         if(i == (1 << order)) 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
292 void free_initrd_mem(unsigned long start, unsigned long end)
293 {
294         if (start < end)
295                 printk ("Freeing initrd memory: %ldk freed\n", 
296                         (end - start) >> 10);
297         for (; start < end; start += PAGE_SIZE) {
298                 ClearPageReserved(virt_to_page(start));
299                 init_page_count(virt_to_page(start));
300                 free_page(start);
301                 totalram_pages++;
302         }
303 }
304         
305 #endif
306
307 void show_mem(void)
308 {
309         int pfn, total = 0, reserved = 0;
310         int shared = 0, cached = 0;
311         int highmem = 0;
312         struct page *page;
313
314         printk("Mem-info:\n");
315         show_free_areas();
316         printk("Free swap:       %6ldkB\n", nr_swap_pages<<(PAGE_SHIFT-10));
317         pfn = max_mapnr;
318         while(pfn-- > 0) {
319                 page = pfn_to_page(pfn);
320                 total++;
321                 if(PageHighMem(page))
322                         highmem++;
323                 if(PageReserved(page))
324                         reserved++;
325                 else if(PageSwapCache(page))
326                         cached++;
327                 else if(page_count(page))
328                         shared += page_count(page) - 1;
329         }
330         printk("%d pages of RAM\n", total);
331         printk("%d pages of HIGHMEM\n", highmem);
332         printk("%d reserved pages\n", reserved);
333         printk("%d pages shared\n", shared);
334         printk("%d pages swap cached\n", cached);
335 }
336
337 /*
338  * Allocate and free page tables.
339  */
340
341 pgd_t *pgd_alloc(struct mm_struct *mm)
342 {
343         pgd_t *pgd = (pgd_t *)__get_free_page(GFP_KERNEL);
344
345         if (pgd) {
346                 memset(pgd, 0, USER_PTRS_PER_PGD * sizeof(pgd_t));
347                 memcpy(pgd + USER_PTRS_PER_PGD, 
348                        swapper_pg_dir + USER_PTRS_PER_PGD, 
349                        (PTRS_PER_PGD - USER_PTRS_PER_PGD) * sizeof(pgd_t));
350         }
351         return pgd;
352 }
353
354 void pgd_free(pgd_t *pgd)
355 {
356         free_page((unsigned long) pgd);
357 }
358
359 pte_t *pte_alloc_one_kernel(struct mm_struct *mm, unsigned long address)
360 {
361         pte_t *pte;
362
363         pte = (pte_t *)__get_free_page(GFP_KERNEL|__GFP_REPEAT|__GFP_ZERO);
364         return pte;
365 }
366
367 struct page *pte_alloc_one(struct mm_struct *mm, unsigned long address)
368 {
369         struct page *pte;
370    
371         pte = alloc_page(GFP_KERNEL|__GFP_REPEAT|__GFP_ZERO);
372         return pte;
373 }
374
375 struct iomem_region *iomem_regions = NULL;
376 int iomem_size = 0;
377
378 extern int parse_iomem(char *str, int *add) __init;
379
380 __uml_setup("iomem=", parse_iomem,
381 "iomem=<name>,<file>\n"
382 "    Configure <file> as an IO memory region named <name>.\n\n"
383 );
384
385 /*
386  * Overrides for Emacs so that we follow Linus's tabbing style.
387  * Emacs will notice this stuff at the end of the file and automatically
388  * adjust the settings for this buffer only.  This must remain at the end
389  * of the file.
390  * ---------------------------------------------------------------------------
391  * Local variables:
392  * c-file-style: "linux"
393  * End:
394  */