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