2 * Copyright (C) 2000 - 2003 Jeff Dike (jdike@addtoit.com)
3 * Licensed under the GPL
6 #include "linux/stddef.h"
7 #include "linux/kernel.h"
9 #include "linux/bootmem.h"
10 #include "linux/swap.h"
11 #include "linux/highmem.h"
12 #include "linux/gfp.h"
14 #include "asm/fixmap.h"
15 #include "asm/pgalloc.h"
16 #include "user_util.h"
17 #include "kern_util.h"
20 #include "uml_uaccess.h"
23 extern char __binary_start
;
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
[1024];
29 unsigned long highmem
;
32 static unsigned long brk_end
;
34 void unmap_physmem(void)
36 os_unmap_memory((void *) brk_end
, uml_reserved
- brk_end
);
39 static void map_cb(void *unused
)
41 map_memory(brk_end
, __pa(brk_end
), uml_reserved
- brk_end
, 1, 1, 0);
45 static void setup_highmem(unsigned long highmem_start
,
46 unsigned long highmem_len
)
49 unsigned long highmem_pfn
;
52 highmem_start_page
= virt_to_page(highmem_start
);
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
);
58 set_bit(PG_highmem
, &page
->flags
);
59 set_page_count(page
, 1);
69 max_low_pfn
= (high_physmem
- uml_physmem
) >> PAGE_SHIFT
;
71 highmem_start_page
= phys_page(__pa(high_physmem
));
74 /* clear the zero-page */
75 memset((void *) empty_zero_page
, 0, PAGE_SIZE
);
77 /* Map in the area just after the brk now that kmalloc is about
80 brk_end
= (unsigned long) UML_ROUND_UP(sbrk(0));
82 initial_thread_cb(map_cb
, NULL
);
83 free_bootmem(__pa(brk_end
), uml_reserved
- brk_end
);
84 uml_reserved
= brk_end
;
86 /* Fill in any hole at the start of the binary */
87 start
= (unsigned long) &__binary_start
;
88 if(uml_physmem
!= start
){
89 map_memory(uml_physmem
, __pa(uml_physmem
), start
- uml_physmem
,
93 /* this will put all low memory onto the freelists */
94 totalram_pages
= free_all_bootmem();
95 totalhigh_pages
= highmem
>> PAGE_SHIFT
;
96 totalram_pages
+= totalhigh_pages
;
97 num_physpages
= totalram_pages
;
98 max_pfn
= totalram_pages
;
99 printk(KERN_INFO
"Memory: %luk available\n",
100 (unsigned long) nr_free_pages() << (PAGE_SHIFT
-10));
103 #ifdef CONFIG_HIGHMEM
104 setup_highmem(end_iomem
, highmem
);
108 static void __init
fixrange_init(unsigned long start
, unsigned long end
,
118 i
= pgd_index(vaddr
);
119 j
= pmd_index(vaddr
);
122 for ( ; (i
< PTRS_PER_PGD
) && (vaddr
< end
); pgd
++, i
++) {
124 for (; (j
< PTRS_PER_PMD
) && (vaddr
!= end
); pmd
++, j
++) {
125 if (pmd_none(*pmd
)) {
126 pte
= (pte_t
*) alloc_bootmem_low_pages(PAGE_SIZE
);
127 set_pmd(pmd
, __pmd(_KERNPG_TABLE
+
128 (unsigned long) __pa(pte
)));
129 if (pte
!= pte_offset_kernel(pmd
, 0))
142 #define kmap_get_fixmap_pte(vaddr) \
143 pte_offset(pmd_offset(pgd_offset_k(vaddr), (vaddr)), (vaddr))
145 void __init
kmap_init(void)
147 unsigned long kmap_vstart
;
149 /* cache the first kmap pte */
150 kmap_vstart
= __fix_to_virt(FIX_KMAP_BEGIN
);
151 kmap_pte
= kmap_get_fixmap_pte(kmap_vstart
);
153 kmap_prot
= PAGE_KERNEL
;
156 static void init_highmem(void)
167 fixrange_init(vaddr
, vaddr
+ PAGE_SIZE
*LAST_PKMAP
, swapper_pg_dir
);
169 pgd
= swapper_pg_dir
+ pgd_index(vaddr
);
170 pmd
= pmd_offset(pgd
, vaddr
);
171 pte
= pte_offset_kernel(pmd
, vaddr
);
172 pkmap_page_table
= pte
;
176 #endif /* CONFIG_HIGHMEM */
178 void paging_init(void)
180 unsigned long zones_size
[MAX_NR_ZONES
], vaddr
;
183 empty_zero_page
= (unsigned long *) alloc_bootmem_low_pages(PAGE_SIZE
);
184 empty_bad_page
= (unsigned long *) alloc_bootmem_low_pages(PAGE_SIZE
);
185 for(i
=0;i
<sizeof(zones_size
)/sizeof(zones_size
[0]);i
++)
187 zones_size
[0] = (end_iomem
>> PAGE_SHIFT
) - (uml_physmem
>> PAGE_SHIFT
);
188 zones_size
[2] = highmem
>> PAGE_SHIFT
;
189 free_area_init(zones_size
);
192 * Fixed mappings, only the page table structure has to be
193 * created - mappings will be set by set_fixmap():
195 vaddr
= __fix_to_virt(__end_of_fixed_addresses
- 1) & PMD_MASK
;
196 fixrange_init(vaddr
, FIXADDR_TOP
, swapper_pg_dir
);
198 #ifdef CONFIG_HIGHMEM
203 struct page
*arch_validate(struct page
*page
, int mask
, int order
)
205 unsigned long addr
, zero
= 0;
209 if(page
== NULL
) return(page
);
210 if(PageHighMem(page
)) return(page
);
212 addr
= (unsigned long) page_address(page
);
213 for(i
= 0; i
< (1 << order
); i
++){
214 current
->thread
.fault_addr
= (void *) addr
;
215 if(__do_copy_to_user((void *) addr
, &zero
,
217 ¤t
->thread
.fault_addr
,
218 ¤t
->thread
.fault_catcher
)){
219 if(!(mask
& __GFP_WAIT
)) return(NULL
);
224 if(i
== (1 << order
)) return(page
);
225 page
= alloc_pages(mask
, order
);
229 /* This can't do anything because nothing in the kernel image can be freed
230 * since it's not in kernel physical memory.
233 void free_initmem(void)
237 #ifdef CONFIG_BLK_DEV_INITRD
239 void free_initrd_mem(unsigned long start
, unsigned long end
)
242 printk ("Freeing initrd memory: %ldk freed\n",
243 (end
- start
) >> 10);
244 for (; start
< end
; start
+= PAGE_SIZE
) {
245 ClearPageReserved(virt_to_page(start
));
246 set_page_count(virt_to_page(start
), 1);
256 int pfn
, total
= 0, reserved
= 0;
257 int shared
= 0, cached
= 0;
261 printk("Mem-info:\n");
263 printk("Free swap: %6ldkB\n", nr_swap_pages
<<(PAGE_SHIFT
-10));
266 page
= pfn_to_page(pfn
);
268 if(PageHighMem(page
))
270 if(PageReserved(page
))
272 else if(PageSwapCache(page
))
274 else if(page_count(page
))
275 shared
+= page_count(page
) - 1;
277 printk("%d pages of RAM\n", total
);
278 printk("%d pages of HIGHMEM\n", highmem
);
279 printk("%d reserved pages\n", reserved
);
280 printk("%d pages shared\n", shared
);
281 printk("%d pages swap cached\n", cached
);
285 * Allocate and free page tables.
288 pgd_t
*pgd_alloc(struct mm_struct
*mm
)
290 pgd_t
*pgd
= (pgd_t
*)__get_free_page(GFP_KERNEL
);
293 memset(pgd
, 0, USER_PTRS_PER_PGD
* sizeof(pgd_t
));
294 memcpy(pgd
+ USER_PTRS_PER_PGD
,
295 swapper_pg_dir
+ USER_PTRS_PER_PGD
,
296 (PTRS_PER_PGD
- USER_PTRS_PER_PGD
) * sizeof(pgd_t
));
301 void pgd_free(pgd_t
*pgd
)
303 free_page((unsigned long) pgd
);
306 pte_t
*pte_alloc_one_kernel(struct mm_struct
*mm
, unsigned long address
)
310 pte
= (pte_t
*)__get_free_page(GFP_KERNEL
|__GFP_REPEAT
);
316 struct page
*pte_alloc_one(struct mm_struct
*mm
, unsigned long address
)
320 pte
= alloc_pages(GFP_KERNEL
|__GFP_REPEAT
, 0);
327 * Overrides for Emacs so that we follow Linus's tabbing style.
328 * Emacs will notice this stuff at the end of the file and automatically
329 * adjust the settings for this buffer only. This must remain at the end
331 * ---------------------------------------------------------------------------
333 * c-file-style: "linux"