blob: 8137ccc9635b5c98ae902fb8f6c5884faf67b8fe [file] [log] [blame]
Jeff Dike114069f2005-09-16 19:27:51 -07001/*
Jeff Dike009ec2a92008-02-04 22:30:53 -08002 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Licensed under the GPL
4 */
5
Jeff Dike009ec2a92008-02-04 22:30:53 -08006#include <linux/stddef.h>
7#include <linux/bootmem.h>
Jeff Dike009ec2a92008-02-04 22:30:53 -08008#include <linux/highmem.h>
9#include <linux/mm.h>
10#include <linux/swap.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/slab.h>
Jeff Dike009ec2a92008-02-04 22:30:53 -080012#include <asm/fixmap.h>
13#include <asm/page.h>
Jeff Dike4ff83ce2007-05-06 14:51:08 -070014#include "as-layout.h"
Jeff Dike114069f2005-09-16 19:27:51 -070015#include "init.h"
Jeff Dike009ec2a92008-02-04 22:30:53 -080016#include "kern.h"
17#include "kern_util.h"
18#include "mem_user.h"
19#include "os.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Jeff Dike6bf79482007-02-10 01:44:18 -080021/* allocated in paging_init, zeroed in mem_init, and unchanged thereafter */
Linus Torvalds1da177e2005-04-16 15:20:36 -070022unsigned long *empty_zero_page = NULL;
Jeff Dike6bf79482007-02-10 01:44:18 -080023/* allocated in paging_init and unchanged thereafter */
WANG Cong4a567582008-07-23 21:28:49 -070024static unsigned long *empty_bad_page = NULL;
Jeff Dike80e39312008-02-04 22:31:17 -080025
26/*
27 * Initialized during boot, and readonly for initializing page tables
28 * afterwards
29 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070030pgd_t swapper_pg_dir[PTRS_PER_PGD];
Jeff Dike80e39312008-02-04 22:31:17 -080031
32/* Initialized at boot time, and readonly after that */
Jeff Dike9902abd2006-03-31 02:30:09 -080033unsigned long long highmem;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034int kmalloc_ok = 0;
35
Jeff Dike80e39312008-02-04 22:31:17 -080036/* Used during early boot */
Linus Torvalds1da177e2005-04-16 15:20:36 -070037static unsigned long brk_end;
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#ifdef CONFIG_HIGHMEM
40static void setup_highmem(unsigned long highmem_start,
41 unsigned long highmem_len)
42{
43 struct page *page;
44 unsigned long highmem_pfn;
45 int i;
46
47 highmem_pfn = __pa(highmem_start) >> PAGE_SHIFT;
Jeff Dike009ec2a92008-02-04 22:30:53 -080048 for (i = 0; i < highmem_len >> PAGE_SHIFT; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 page = &mem_map[highmem_pfn + i];
50 ClearPageReserved(page);
Nick Piggin7835e982006-03-22 00:08:40 -080051 init_page_count(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 __free_page(page);
53 }
54}
55#endif
56
Jeff Dike97a1fcb2007-07-23 18:43:48 -070057void __init mem_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
Jeff Dike60678bb2007-02-10 01:44:10 -080059 /* clear the zero-page */
WANG Congc0a92902008-02-04 22:30:41 -080060 memset(empty_zero_page, 0, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62 /* Map in the area just after the brk now that kmalloc is about
63 * to be turned on.
64 */
65 brk_end = (unsigned long) UML_ROUND_UP(sbrk(0));
Jeff Dikeab26a522008-02-04 22:31:24 -080066 map_memory(brk_end, __pa(brk_end), uml_reserved - brk_end, 1, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 free_bootmem(__pa(brk_end), uml_reserved - brk_end);
68 uml_reserved = brk_end;
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 /* this will put all low memory onto the freelists */
71 totalram_pages = free_all_bootmem();
Jason Lunzaf84eab2007-03-29 01:20:31 -070072 max_low_pfn = totalram_pages;
Christoph Lameterc1f60a52006-09-25 23:31:11 -070073#ifdef CONFIG_HIGHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 totalhigh_pages = highmem >> PAGE_SHIFT;
75 totalram_pages += totalhigh_pages;
Christoph Lameterc1f60a52006-09-25 23:31:11 -070076#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 num_physpages = totalram_pages;
78 max_pfn = totalram_pages;
Jeff Dike009ec2a92008-02-04 22:30:53 -080079 printk(KERN_INFO "Memory: %luk available\n",
Geert Uytterhoevencc013a82009-09-21 17:02:36 -070080 nr_free_pages() << (PAGE_SHIFT-10));
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 kmalloc_ok = 1;
82
83#ifdef CONFIG_HIGHMEM
84 setup_highmem(end_iomem, highmem);
85#endif
86}
87
Jeff Dike12f49642005-05-20 13:59:12 -070088/*
89 * Create a page table and place a pointer to it in a middle page
90 * directory entry.
91 */
92static void __init one_page_table_init(pmd_t *pmd)
93{
94 if (pmd_none(*pmd)) {
95 pte_t *pte = (pte_t *) alloc_bootmem_low_pages(PAGE_SIZE);
96 set_pmd(pmd, __pmd(_KERNPG_TABLE +
97 (unsigned long) __pa(pte)));
98 if (pte != pte_offset_kernel(pmd, 0))
99 BUG();
100 }
101}
102
103static void __init one_md_table_init(pud_t *pud)
104{
105#ifdef CONFIG_3_LEVEL_PGTABLES
106 pmd_t *pmd_table = (pmd_t *) alloc_bootmem_low_pages(PAGE_SIZE);
107 set_pud(pud, __pud(_KERNPG_TABLE + (unsigned long) __pa(pmd_table)));
108 if (pmd_table != pmd_offset(pud, 0))
109 BUG();
110#endif
111}
112
Jeff Dike009ec2a92008-02-04 22:30:53 -0800113static void __init fixrange_init(unsigned long start, unsigned long end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 pgd_t *pgd_base)
115{
116 pgd_t *pgd;
Jeff Dike12f49642005-05-20 13:59:12 -0700117 pud_t *pud;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 pmd_t *pmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 int i, j;
120 unsigned long vaddr;
121
122 vaddr = start;
123 i = pgd_index(vaddr);
124 j = pmd_index(vaddr);
125 pgd = pgd_base + i;
126
127 for ( ; (i < PTRS_PER_PGD) && (vaddr < end); pgd++, i++) {
Jeff Dike12f49642005-05-20 13:59:12 -0700128 pud = pud_offset(pgd, vaddr);
129 if (pud_none(*pud))
130 one_md_table_init(pud);
131 pmd = pmd_offset(pud, vaddr);
Jeff Dike655e4ed2008-02-04 22:30:55 -0800132 for (; (j < PTRS_PER_PMD) && (vaddr < end); pmd++, j++) {
Jeff Dike12f49642005-05-20 13:59:12 -0700133 one_page_table_init(pmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 vaddr += PMD_SIZE;
135 }
136 j = 0;
137 }
138}
139
140#ifdef CONFIG_HIGHMEM
141pte_t *kmap_pte;
142pgprot_t kmap_prot;
143
144#define kmap_get_fixmap_pte(vaddr) \
145 pte_offset_kernel(pmd_offset(pud_offset(pgd_offset_k(vaddr), (vaddr)),\
Jeff Dike8192ab42008-02-04 22:30:53 -0800146 (vaddr)), (vaddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148static void __init kmap_init(void)
149{
150 unsigned long kmap_vstart;
151
152 /* cache the first kmap pte */
153 kmap_vstart = __fix_to_virt(FIX_KMAP_BEGIN);
154 kmap_pte = kmap_get_fixmap_pte(kmap_vstart);
155
156 kmap_prot = PAGE_KERNEL;
157}
158
Al Virob4a08a12007-10-29 04:36:10 +0000159static void __init init_highmem(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
161 pgd_t *pgd;
162 pud_t *pud;
163 pmd_t *pmd;
164 pte_t *pte;
165 unsigned long vaddr;
166
167 /*
168 * Permanent kmaps:
169 */
170 vaddr = PKMAP_BASE;
171 fixrange_init(vaddr, vaddr + PAGE_SIZE*LAST_PKMAP, swapper_pg_dir);
172
173 pgd = swapper_pg_dir + pgd_index(vaddr);
174 pud = pud_offset(pgd, vaddr);
175 pmd = pmd_offset(pud, vaddr);
176 pte = pte_offset_kernel(pmd, vaddr);
177 pkmap_page_table = pte;
178
179 kmap_init();
180}
181#endif /* CONFIG_HIGHMEM */
182
183static void __init fixaddr_user_init( void)
184{
viro@ZenIV.linux.org.uk9a0b3862005-09-07 23:21:11 +0100185#ifdef CONFIG_ARCH_REUSE_HOST_VSYSCALL_AREA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 long size = FIXADDR_USER_END - FIXADDR_USER_START;
187 pgd_t *pgd;
188 pud_t *pud;
189 pmd_t *pmd;
190 pte_t *pte;
Jeff Dike655e4ed2008-02-04 22:30:55 -0800191 phys_t p;
192 unsigned long v, vaddr = FIXADDR_USER_START;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Jeff Dike655e4ed2008-02-04 22:30:55 -0800194 if (!size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 return;
196
197 fixrange_init( FIXADDR_USER_START, FIXADDR_USER_END, swapper_pg_dir);
Jeff Dike655e4ed2008-02-04 22:30:55 -0800198 v = (unsigned long) alloc_bootmem_low_pages(size);
199 memcpy((void *) v , (void *) FIXADDR_USER_START, size);
200 p = __pa(v);
Jeff Dike009ec2a92008-02-04 22:30:53 -0800201 for ( ; size > 0; size -= PAGE_SIZE, vaddr += PAGE_SIZE,
Jeff Dike655e4ed2008-02-04 22:30:55 -0800202 p += PAGE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 pgd = swapper_pg_dir + pgd_index(vaddr);
204 pud = pud_offset(pgd, vaddr);
205 pmd = pmd_offset(pud, vaddr);
206 pte = pte_offset_kernel(pmd, vaddr);
Jeff Dike655e4ed2008-02-04 22:30:55 -0800207 pte_set_val(*pte, p, PAGE_READONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 }
209#endif
210}
211
Jeff Dike36e45462007-05-06 14:51:11 -0700212void __init paging_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
214 unsigned long zones_size[MAX_NR_ZONES], vaddr;
215 int i;
216
217 empty_zero_page = (unsigned long *) alloc_bootmem_low_pages(PAGE_SIZE);
218 empty_bad_page = (unsigned long *) alloc_bootmem_low_pages(PAGE_SIZE);
Jeff Dike009ec2a92008-02-04 22:30:53 -0800219 for (i = 0; i < ARRAY_SIZE(zones_size); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 zones_size[i] = 0;
Jeff Dike91b165c2006-09-25 23:33:00 -0700221
Jeff Dike07155012006-09-27 01:50:34 -0700222 zones_size[ZONE_NORMAL] = (end_iomem >> PAGE_SHIFT) -
223 (uml_physmem >> PAGE_SHIFT);
Christoph Lametere53ef382006-09-25 23:31:14 -0700224#ifdef CONFIG_HIGHMEM
Paolo 'Blaisorblade' Giarrusso353f8d12005-11-07 00:58:58 -0800225 zones_size[ZONE_HIGHMEM] = highmem >> PAGE_SHIFT;
Christoph Lametere53ef382006-09-25 23:31:14 -0700226#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 free_area_init(zones_size);
228
229 /*
230 * Fixed mappings, only the page table structure has to be
231 * created - mappings will be set by set_fixmap():
232 */
233 vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK;
234 fixrange_init(vaddr, FIXADDR_TOP, swapper_pg_dir);
235
236 fixaddr_user_init();
237
238#ifdef CONFIG_HIGHMEM
239 init_highmem();
240#endif
241}
242
Jeff Dike8192ab42008-02-04 22:30:53 -0800243/*
244 * This can't do anything because nothing in the kernel image can be freed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 * since it's not in kernel physical memory.
246 */
247
248void free_initmem(void)
249{
250}
251
252#ifdef CONFIG_BLK_DEV_INITRD
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253void free_initrd_mem(unsigned long start, unsigned long end)
254{
255 if (start < end)
Jeff Dike009ec2a92008-02-04 22:30:53 -0800256 printk(KERN_INFO "Freeing initrd memory: %ldk freed\n",
257 (end - start) >> 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 for (; start < end; start += PAGE_SIZE) {
259 ClearPageReserved(virt_to_page(start));
Nick Piggin7835e982006-03-22 00:08:40 -0800260 init_page_count(virt_to_page(start));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 free_page(start);
262 totalram_pages++;
263 }
264}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265#endif
266
Jeff Dike8192ab42008-02-04 22:30:53 -0800267/* Allocate and free page tables. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269pgd_t *pgd_alloc(struct mm_struct *mm)
270{
271 pgd_t *pgd = (pgd_t *)__get_free_page(GFP_KERNEL);
272
273 if (pgd) {
274 memset(pgd, 0, USER_PTRS_PER_PGD * sizeof(pgd_t));
Jeff Dike009ec2a92008-02-04 22:30:53 -0800275 memcpy(pgd + USER_PTRS_PER_PGD,
276 swapper_pg_dir + USER_PTRS_PER_PGD,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 (PTRS_PER_PGD - USER_PTRS_PER_PGD) * sizeof(pgd_t));
278 }
279 return pgd;
280}
281
Benjamin Herrenschmidt5e541972008-02-04 22:29:14 -0800282void pgd_free(struct mm_struct *mm, pgd_t *pgd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
284 free_page((unsigned long) pgd);
285}
286
287pte_t *pte_alloc_one_kernel(struct mm_struct *mm, unsigned long address)
288{
289 pte_t *pte;
290
291 pte = (pte_t *)__get_free_page(GFP_KERNEL|__GFP_REPEAT|__GFP_ZERO);
292 return pte;
293}
294
Martin Schwidefsky2f569af2008-02-08 04:22:04 -0800295pgtable_t pte_alloc_one(struct mm_struct *mm, unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
297 struct page *pte;
Jeff Dike60678bb2007-02-10 01:44:10 -0800298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 pte = alloc_page(GFP_KERNEL|__GFP_REPEAT|__GFP_ZERO);
Martin Schwidefsky2f569af2008-02-08 04:22:04 -0800300 if (pte)
301 pgtable_page_ctor(pte);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 return pte;
303}
Jeff Dike8192ab42008-02-04 22:30:53 -0800304
305#ifdef CONFIG_3_LEVEL_PGTABLES
306pmd_t *pmd_alloc_one(struct mm_struct *mm, unsigned long address)
307{
308 pmd_t *pmd = (pmd_t *) __get_free_page(GFP_KERNEL);
309
310 if (pmd)
311 memset(pmd, 0, PAGE_SIZE);
312
313 return pmd;
314}
315#endif
Jeff Dike43f5b302008-05-12 14:01:52 -0700316
317void *uml_kmalloc(int size, int flags)
318{
319 return kmalloc(size, flags);
320}