blob: 93121c6d26e55594fe8bb505ebdd17ef20de145c [file] [log] [blame]
Jeff Dike114069f2005-09-16 19:27:51 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * 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"
Jeff Dike114069f2005-09-16 19:27:51 -070022#include "linux/types.h"
23#include "linux/string.h"
24#include "init.h"
25#include "kern_constants.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Linus Torvalds1da177e2005-04-16 15:20:36 -070027/* Changed during early boot */
28unsigned long *empty_zero_page = NULL;
29unsigned long *empty_bad_page = NULL;
30pgd_t swapper_pg_dir[PTRS_PER_PGD];
Jeff Dike9902abd2006-03-31 02:30:09 -080031unsigned long long highmem;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032int kmalloc_ok = 0;
33
34static unsigned long brk_end;
35
36void unmap_physmem(void)
37{
38 os_unmap_memory((void *) brk_end, uml_reserved - brk_end);
39}
40
41static void map_cb(void *unused)
42{
43 map_memory(brk_end, __pa(brk_end), uml_reserved - brk_end, 1, 1, 0);
44}
45
46#ifdef CONFIG_HIGHMEM
47static void setup_highmem(unsigned long highmem_start,
48 unsigned long highmem_len)
49{
50 struct page *page;
51 unsigned long highmem_pfn;
52 int i;
53
54 highmem_pfn = __pa(highmem_start) >> PAGE_SHIFT;
55 for(i = 0; i < highmem_len >> PAGE_SHIFT; i++){
56 page = &mem_map[highmem_pfn + i];
57 ClearPageReserved(page);
Nick Piggin7835e982006-03-22 00:08:40 -080058 init_page_count(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 __free_page(page);
60 }
61}
62#endif
63
64void mem_init(void)
65{
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 max_low_pfn = (high_physmem - uml_physmem) >> PAGE_SHIFT;
67
68 /* clear the zero-page */
69 memset((void *) empty_zero_page, 0, PAGE_SIZE);
70
71 /* Map in the area just after the brk now that kmalloc is about
72 * to be turned on.
73 */
74 brk_end = (unsigned long) UML_ROUND_UP(sbrk(0));
75 map_cb(NULL);
76 initial_thread_cb(map_cb, NULL);
77 free_bootmem(__pa(brk_end), uml_reserved - brk_end);
78 uml_reserved = brk_end;
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 /* this will put all low memory onto the freelists */
81 totalram_pages = free_all_bootmem();
Christoph Lameterc1f60a52006-09-25 23:31:11 -070082#ifdef CONFIG_HIGHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 totalhigh_pages = highmem >> PAGE_SHIFT;
84 totalram_pages += totalhigh_pages;
Christoph Lameterc1f60a52006-09-25 23:31:11 -070085#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 num_physpages = totalram_pages;
87 max_pfn = totalram_pages;
88 printk(KERN_INFO "Memory: %luk available\n",
89 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10));
90 kmalloc_ok = 1;
91
92#ifdef CONFIG_HIGHMEM
93 setup_highmem(end_iomem, highmem);
94#endif
95}
96
Jeff Dike12f49642005-05-20 13:59:12 -070097/*
98 * Create a page table and place a pointer to it in a middle page
99 * directory entry.
100 */
101static void __init one_page_table_init(pmd_t *pmd)
102{
103 if (pmd_none(*pmd)) {
104 pte_t *pte = (pte_t *) alloc_bootmem_low_pages(PAGE_SIZE);
105 set_pmd(pmd, __pmd(_KERNPG_TABLE +
106 (unsigned long) __pa(pte)));
107 if (pte != pte_offset_kernel(pmd, 0))
108 BUG();
109 }
110}
111
112static void __init one_md_table_init(pud_t *pud)
113{
114#ifdef CONFIG_3_LEVEL_PGTABLES
115 pmd_t *pmd_table = (pmd_t *) alloc_bootmem_low_pages(PAGE_SIZE);
116 set_pud(pud, __pud(_KERNPG_TABLE + (unsigned long) __pa(pmd_table)));
117 if (pmd_table != pmd_offset(pud, 0))
118 BUG();
119#endif
120}
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122static void __init fixrange_init(unsigned long start, unsigned long end,
123 pgd_t *pgd_base)
124{
125 pgd_t *pgd;
Jeff Dike12f49642005-05-20 13:59:12 -0700126 pud_t *pud;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 pmd_t *pmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 int i, j;
129 unsigned long vaddr;
130
131 vaddr = start;
132 i = pgd_index(vaddr);
133 j = pmd_index(vaddr);
134 pgd = pgd_base + i;
135
136 for ( ; (i < PTRS_PER_PGD) && (vaddr < end); pgd++, i++) {
Jeff Dike12f49642005-05-20 13:59:12 -0700137 pud = pud_offset(pgd, vaddr);
138 if (pud_none(*pud))
139 one_md_table_init(pud);
140 pmd = pmd_offset(pud, vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 for (; (j < PTRS_PER_PMD) && (vaddr != end); pmd++, j++) {
Jeff Dike12f49642005-05-20 13:59:12 -0700142 one_page_table_init(pmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 vaddr += PMD_SIZE;
144 }
145 j = 0;
146 }
147}
148
149#ifdef CONFIG_HIGHMEM
150pte_t *kmap_pte;
151pgprot_t kmap_prot;
152
153#define kmap_get_fixmap_pte(vaddr) \
154 pte_offset_kernel(pmd_offset(pud_offset(pgd_offset_k(vaddr), (vaddr)),\
155 (vaddr)), (vaddr))
156
157static void __init kmap_init(void)
158{
159 unsigned long kmap_vstart;
160
161 /* cache the first kmap pte */
162 kmap_vstart = __fix_to_virt(FIX_KMAP_BEGIN);
163 kmap_pte = kmap_get_fixmap_pte(kmap_vstart);
164
165 kmap_prot = PAGE_KERNEL;
166}
167
168static void init_highmem(void)
169{
170 pgd_t *pgd;
171 pud_t *pud;
172 pmd_t *pmd;
173 pte_t *pte;
174 unsigned long vaddr;
175
176 /*
177 * Permanent kmaps:
178 */
179 vaddr = PKMAP_BASE;
180 fixrange_init(vaddr, vaddr + PAGE_SIZE*LAST_PKMAP, swapper_pg_dir);
181
182 pgd = swapper_pg_dir + pgd_index(vaddr);
183 pud = pud_offset(pgd, vaddr);
184 pmd = pmd_offset(pud, vaddr);
185 pte = pte_offset_kernel(pmd, vaddr);
186 pkmap_page_table = pte;
187
188 kmap_init();
189}
190#endif /* CONFIG_HIGHMEM */
191
192static void __init fixaddr_user_init( void)
193{
viro@ZenIV.linux.org.uk9a0b3862005-09-07 23:21:11 +0100194#ifdef CONFIG_ARCH_REUSE_HOST_VSYSCALL_AREA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 long size = FIXADDR_USER_END - FIXADDR_USER_START;
196 pgd_t *pgd;
197 pud_t *pud;
198 pmd_t *pmd;
199 pte_t *pte;
200 unsigned long paddr, vaddr = FIXADDR_USER_START;
201
202 if ( ! size )
203 return;
204
205 fixrange_init( FIXADDR_USER_START, FIXADDR_USER_END, swapper_pg_dir);
206 paddr = (unsigned long)alloc_bootmem_low_pages( size);
207 memcpy( (void *)paddr, (void *)FIXADDR_USER_START, size);
208 paddr = __pa(paddr);
209 for ( ; size > 0; size-=PAGE_SIZE, vaddr+=PAGE_SIZE, paddr+=PAGE_SIZE){
210 pgd = swapper_pg_dir + pgd_index(vaddr);
211 pud = pud_offset(pgd, vaddr);
212 pmd = pmd_offset(pud, vaddr);
213 pte = pte_offset_kernel(pmd, vaddr);
214 pte_set_val( (*pte), paddr, PAGE_READONLY);
215 }
216#endif
217}
218
219void paging_init(void)
220{
221 unsigned long zones_size[MAX_NR_ZONES], vaddr;
222 int i;
223
224 empty_zero_page = (unsigned long *) alloc_bootmem_low_pages(PAGE_SIZE);
225 empty_bad_page = (unsigned long *) alloc_bootmem_low_pages(PAGE_SIZE);
Jeff Dike91b165c2006-09-25 23:33:00 -0700226 for(i = 0; i < ARRAY_SIZE(zones_size); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 zones_size[i] = 0;
Jeff Dike91b165c2006-09-25 23:33:00 -0700228
Paolo 'Blaisorblade' Giarrusso353f8d12005-11-07 00:58:58 -0800229 zones_size[ZONE_DMA] = (end_iomem >> PAGE_SHIFT) - (uml_physmem >> PAGE_SHIFT);
Christoph Lametere53ef382006-09-25 23:31:14 -0700230#ifdef CONFIG_HIGHMEM
Paolo 'Blaisorblade' Giarrusso353f8d12005-11-07 00:58:58 -0800231 zones_size[ZONE_HIGHMEM] = highmem >> PAGE_SHIFT;
Christoph Lametere53ef382006-09-25 23:31:14 -0700232#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 free_area_init(zones_size);
234
235 /*
236 * Fixed mappings, only the page table structure has to be
237 * created - mappings will be set by set_fixmap():
238 */
239 vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK;
240 fixrange_init(vaddr, FIXADDR_TOP, swapper_pg_dir);
241
242 fixaddr_user_init();
243
244#ifdef CONFIG_HIGHMEM
245 init_highmem();
246#endif
247}
248
Al Viro53f9fc92005-10-21 03:22:24 -0400249struct page *arch_validate(struct page *page, gfp_t mask, int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
251 unsigned long addr, zero = 0;
252 int i;
253
254 again:
255 if(page == NULL) return(page);
256 if(PageHighMem(page)) return(page);
257
258 addr = (unsigned long) page_address(page);
259 for(i = 0; i < (1 << order); i++){
260 current->thread.fault_addr = (void *) addr;
261 if(__do_copy_to_user((void __user *) addr, &zero,
262 sizeof(zero),
263 &current->thread.fault_addr,
264 &current->thread.fault_catcher)){
265 if(!(mask & __GFP_WAIT)) return(NULL);
266 else break;
267 }
268 addr += PAGE_SIZE;
269 }
270
271 if(i == (1 << order)) return(page);
272 page = alloc_pages(mask, order);
273 goto again;
274}
275
276/* This can't do anything because nothing in the kernel image can be freed
277 * since it's not in kernel physical memory.
278 */
279
280void free_initmem(void)
281{
282}
283
284#ifdef CONFIG_BLK_DEV_INITRD
285
286void free_initrd_mem(unsigned long start, unsigned long end)
287{
288 if (start < end)
289 printk ("Freeing initrd memory: %ldk freed\n",
290 (end - start) >> 10);
291 for (; start < end; start += PAGE_SIZE) {
292 ClearPageReserved(virt_to_page(start));
Nick Piggin7835e982006-03-22 00:08:40 -0800293 init_page_count(virt_to_page(start));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 free_page(start);
295 totalram_pages++;
296 }
297}
298
299#endif
300
301void show_mem(void)
302{
303 int pfn, total = 0, reserved = 0;
304 int shared = 0, cached = 0;
305 int highmem = 0;
306 struct page *page;
307
308 printk("Mem-info:\n");
309 show_free_areas();
310 printk("Free swap: %6ldkB\n", nr_swap_pages<<(PAGE_SHIFT-10));
311 pfn = max_mapnr;
312 while(pfn-- > 0) {
313 page = pfn_to_page(pfn);
314 total++;
315 if(PageHighMem(page))
316 highmem++;
317 if(PageReserved(page))
318 reserved++;
319 else if(PageSwapCache(page))
320 cached++;
321 else if(page_count(page))
322 shared += page_count(page) - 1;
323 }
324 printk("%d pages of RAM\n", total);
325 printk("%d pages of HIGHMEM\n", highmem);
326 printk("%d reserved pages\n", reserved);
327 printk("%d pages shared\n", shared);
328 printk("%d pages swap cached\n", cached);
329}
330
331/*
332 * Allocate and free page tables.
333 */
334
335pgd_t *pgd_alloc(struct mm_struct *mm)
336{
337 pgd_t *pgd = (pgd_t *)__get_free_page(GFP_KERNEL);
338
339 if (pgd) {
340 memset(pgd, 0, USER_PTRS_PER_PGD * sizeof(pgd_t));
341 memcpy(pgd + USER_PTRS_PER_PGD,
342 swapper_pg_dir + USER_PTRS_PER_PGD,
343 (PTRS_PER_PGD - USER_PTRS_PER_PGD) * sizeof(pgd_t));
344 }
345 return pgd;
346}
347
348void pgd_free(pgd_t *pgd)
349{
350 free_page((unsigned long) pgd);
351}
352
353pte_t *pte_alloc_one_kernel(struct mm_struct *mm, unsigned long address)
354{
355 pte_t *pte;
356
357 pte = (pte_t *)__get_free_page(GFP_KERNEL|__GFP_REPEAT|__GFP_ZERO);
358 return pte;
359}
360
361struct page *pte_alloc_one(struct mm_struct *mm, unsigned long address)
362{
363 struct page *pte;
364
365 pte = alloc_page(GFP_KERNEL|__GFP_REPEAT|__GFP_ZERO);
366 return pte;
367}
368
Jeff Dike114069f2005-09-16 19:27:51 -0700369struct iomem_region *iomem_regions = NULL;
370int iomem_size = 0;
371
372extern int parse_iomem(char *str, int *add) __init;
373
374__uml_setup("iomem=", parse_iomem,
375"iomem=<name>,<file>\n"
376" Configure <file> as an IO memory region named <name>.\n\n"
377);
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379/*
380 * Overrides for Emacs so that we follow Linus's tabbing style.
381 * Emacs will notice this stuff at the end of the file and automatically
382 * adjust the settings for this buffer only. This must remain at the end
383 * of the file.
384 * ---------------------------------------------------------------------------
385 * Local variables:
386 * c-file-style: "linux"
387 * End:
388 */