Fix memory leak in discard case of sctp_sf_abort_violation()
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / um / kernel / mem.c
blob59822dee438adfe53f373569f9b74d675726b56d
1 /*
2 * Copyright (C) 2000 - 2003 Jeff Dike (jdike@addtoit.com)
3 * Licensed under the GPL
4 */
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 "kern_util.h"
17 #include "as-layout.h"
18 #include "kern.h"
19 #include "mem_user.h"
20 #include "um_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"
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;
35 static unsigned long brk_end;
37 void unmap_physmem(void)
39 os_unmap_memory((void *) brk_end, uml_reserved - brk_end);
42 static void map_cb(void *unused)
44 map_memory(brk_end, __pa(brk_end), uml_reserved - brk_end, 1, 1, 0);
47 #ifdef CONFIG_HIGHMEM
48 static void setup_highmem(unsigned long highmem_start,
49 unsigned long highmem_len)
51 struct page *page;
52 unsigned long highmem_pfn;
53 int i;
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);
63 #endif
65 void __init mem_init(void)
67 /* clear the zero-page */
68 memset((void *) empty_zero_page, 0, PAGE_SIZE);
70 /* Map in the area just after the brk now that kmalloc is about
71 * to be turned on.
73 brk_end = (unsigned long) UML_ROUND_UP(sbrk(0));
74 map_cb(NULL);
75 initial_thread_cb(map_cb, NULL);
76 free_bootmem(__pa(brk_end), uml_reserved - brk_end);
77 uml_reserved = brk_end;
79 /* this will put all low memory onto the freelists */
80 totalram_pages = free_all_bootmem();
81 max_low_pfn = totalram_pages;
82 #ifdef CONFIG_HIGHMEM
83 totalhigh_pages = highmem >> PAGE_SHIFT;
84 totalram_pages += totalhigh_pages;
85 #endif
86 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;
92 #ifdef CONFIG_HIGHMEM
93 setup_highmem(end_iomem, highmem);
94 #endif
98 * Create a page table and place a pointer to it in a middle page
99 * directory entry.
101 static void __init one_page_table_init(pmd_t *pmd)
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();
112 static void __init one_md_table_init(pud_t *pud)
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
122 static void __init fixrange_init(unsigned long start, unsigned long end,
123 pgd_t *pgd_base)
125 pgd_t *pgd;
126 pud_t *pud;
127 pmd_t *pmd;
128 int i, j;
129 unsigned long vaddr;
131 vaddr = start;
132 i = pgd_index(vaddr);
133 j = pmd_index(vaddr);
134 pgd = pgd_base + i;
136 for ( ; (i < PTRS_PER_PGD) && (vaddr < end); pgd++, i++) {
137 pud = pud_offset(pgd, vaddr);
138 if (pud_none(*pud))
139 one_md_table_init(pud);
140 pmd = pmd_offset(pud, vaddr);
141 for (; (j < PTRS_PER_PMD) && (vaddr != end); pmd++, j++) {
142 one_page_table_init(pmd);
143 vaddr += PMD_SIZE;
145 j = 0;
149 #ifdef CONFIG_HIGHMEM
150 pte_t *kmap_pte;
151 pgprot_t kmap_prot;
153 #define kmap_get_fixmap_pte(vaddr) \
154 pte_offset_kernel(pmd_offset(pud_offset(pgd_offset_k(vaddr), (vaddr)),\
155 (vaddr)), (vaddr))
157 static void __init kmap_init(void)
159 unsigned long kmap_vstart;
161 /* cache the first kmap pte */
162 kmap_vstart = __fix_to_virt(FIX_KMAP_BEGIN);
163 kmap_pte = kmap_get_fixmap_pte(kmap_vstart);
165 kmap_prot = PAGE_KERNEL;
168 static void __init init_highmem(void)
170 pgd_t *pgd;
171 pud_t *pud;
172 pmd_t *pmd;
173 pte_t *pte;
174 unsigned long vaddr;
177 * Permanent kmaps:
179 vaddr = PKMAP_BASE;
180 fixrange_init(vaddr, vaddr + PAGE_SIZE*LAST_PKMAP, swapper_pg_dir);
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;
188 kmap_init();
190 #endif /* CONFIG_HIGHMEM */
192 static void __init fixaddr_user_init( void)
194 #ifdef CONFIG_ARCH_REUSE_HOST_VSYSCALL_AREA
195 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;
202 if ( ! size )
203 return;
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);
216 #endif
219 void __init paging_init(void)
221 unsigned long zones_size[MAX_NR_ZONES], vaddr;
222 int i;
224 empty_zero_page = (unsigned long *) alloc_bootmem_low_pages(PAGE_SIZE);
225 empty_bad_page = (unsigned long *) alloc_bootmem_low_pages(PAGE_SIZE);
226 for(i = 0; i < ARRAY_SIZE(zones_size); i++)
227 zones_size[i] = 0;
229 zones_size[ZONE_NORMAL] = (end_iomem >> PAGE_SHIFT) -
230 (uml_physmem >> PAGE_SHIFT);
231 #ifdef CONFIG_HIGHMEM
232 zones_size[ZONE_HIGHMEM] = highmem >> PAGE_SHIFT;
233 #endif
234 free_area_init(zones_size);
237 * Fixed mappings, only the page table structure has to be
238 * created - mappings will be set by set_fixmap():
240 vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK;
241 fixrange_init(vaddr, FIXADDR_TOP, swapper_pg_dir);
243 fixaddr_user_init();
245 #ifdef CONFIG_HIGHMEM
246 init_highmem();
247 #endif
250 struct page *arch_validate(struct page *page, gfp_t mask, int order)
252 unsigned long addr, zero = 0;
253 int i;
255 again:
256 if(page == NULL)
257 return page;
258 if(PageHighMem(page))
259 return page;
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))
269 return NULL;
270 else break;
272 addr += PAGE_SIZE;
275 if(i == (1 << order))
276 return page;
277 page = alloc_pages(mask, order);
278 goto again;
281 /* This can't do anything because nothing in the kernel image can be freed
282 * since it's not in kernel physical memory.
285 void free_initmem(void)
289 #ifdef CONFIG_BLK_DEV_INITRD
290 void free_initrd_mem(unsigned long start, unsigned long end)
292 if (start < end)
293 printk ("Freeing initrd memory: %ldk freed\n",
294 (end - start) >> 10);
295 for (; start < end; start += PAGE_SIZE) {
296 ClearPageReserved(virt_to_page(start));
297 init_page_count(virt_to_page(start));
298 free_page(start);
299 totalram_pages++;
302 #endif
304 void show_mem(void)
306 int pfn, total = 0, reserved = 0;
307 int shared = 0, cached = 0;
308 int highmem = 0;
309 struct page *page;
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;
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);
335 * Allocate and free page tables.
338 pgd_t *pgd_alloc(struct mm_struct *mm)
340 pgd_t *pgd = (pgd_t *)__get_free_page(GFP_KERNEL);
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));
348 return pgd;
351 void pgd_free(pgd_t *pgd)
353 free_page((unsigned long) pgd);
356 pte_t *pte_alloc_one_kernel(struct mm_struct *mm, unsigned long address)
358 pte_t *pte;
360 pte = (pte_t *)__get_free_page(GFP_KERNEL|__GFP_REPEAT|__GFP_ZERO);
361 return pte;
364 struct page *pte_alloc_one(struct mm_struct *mm, unsigned long address)
366 struct page *pte;
368 pte = alloc_page(GFP_KERNEL|__GFP_REPEAT|__GFP_ZERO);
369 return pte;