blob: 2aa7a2448d5844237f7b0ae7bc51b367ee8650ff [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>
Al Viro73395a02011-08-18 20:14:10 +01007#include <linux/module.h>
Jeff Dike009ec2a92008-02-04 22:30:53 -08008#include <linux/bootmem.h>
Jeff Dike009ec2a92008-02-04 22:30:53 -08009#include <linux/highmem.h>
10#include <linux/mm.h>
11#include <linux/swap.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Jeff Dike009ec2a92008-02-04 22:30:53 -080013#include <asm/fixmap.h>
14#include <asm/page.h>
Al Viro37185b32012-10-08 03:27:32 +010015#include <as-layout.h>
16#include <init.h>
17#include <kern.h>
18#include <kern_util.h>
19#include <mem_user.h>
20#include <os.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Jeff Dike6bf79482007-02-10 01:44:18 -080022/* allocated in paging_init, zeroed in mem_init, and unchanged thereafter */
Linus Torvalds1da177e2005-04-16 15:20:36 -070023unsigned long *empty_zero_page = NULL;
Al Viro73395a02011-08-18 20:14:10 +010024EXPORT_SYMBOL(empty_zero_page);
Jeff Dike6bf79482007-02-10 01:44:18 -080025/* allocated in paging_init and unchanged thereafter */
WANG Cong4a567582008-07-23 21:28:49 -070026static unsigned long *empty_bad_page = NULL;
Jeff Dike80e39312008-02-04 22:31:17 -080027
28/*
29 * Initialized during boot, and readonly for initializing page tables
30 * afterwards
31 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070032pgd_t swapper_pg_dir[PTRS_PER_PGD];
Jeff Dike80e39312008-02-04 22:31:17 -080033
34/* Initialized at boot time, and readonly after that */
Jeff Dike9902abd2006-03-31 02:30:09 -080035unsigned long long highmem;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036int kmalloc_ok = 0;
37
Jeff Dike80e39312008-02-04 22:31:17 -080038/* Used during early boot */
Linus Torvalds1da177e2005-04-16 15:20:36 -070039static unsigned long brk_end;
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#ifdef CONFIG_HIGHMEM
42static void setup_highmem(unsigned long highmem_start,
43 unsigned long highmem_len)
44{
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 unsigned long highmem_pfn;
46 int i;
47
48 highmem_pfn = __pa(highmem_start) >> PAGE_SHIFT;
Jiang Liu5b472b22013-04-29 15:07:11 -070049 for (i = 0; i < highmem_len >> PAGE_SHIFT; i++)
50 free_highmem_page(&mem_map[highmem_pfn + i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051}
52#endif
53
Jeff Dike97a1fcb2007-07-23 18:43:48 -070054void __init mem_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
Jeff Dike60678bb2007-02-10 01:44:10 -080056 /* clear the zero-page */
WANG Congc0a92902008-02-04 22:30:41 -080057 memset(empty_zero_page, 0, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59 /* Map in the area just after the brk now that kmalloc is about
60 * to be turned on.
61 */
62 brk_end = (unsigned long) UML_ROUND_UP(sbrk(0));
Jeff Dikeab26a522008-02-04 22:31:24 -080063 map_memory(brk_end, __pa(brk_end), uml_reserved - brk_end, 1, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 free_bootmem(__pa(brk_end), uml_reserved - brk_end);
65 uml_reserved = brk_end;
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 /* this will put all low memory onto the freelists */
68 totalram_pages = free_all_bootmem();
Jason Lunzaf84eab2007-03-29 01:20:31 -070069 max_low_pfn = totalram_pages;
Christoph Lameterc1f60a52006-09-25 23:31:11 -070070#ifdef CONFIG_HIGHMEM
Jiang Liu5b472b22013-04-29 15:07:11 -070071 setup_highmem(end_iomem, highmem);
Christoph Lameterc1f60a52006-09-25 23:31:11 -070072#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 num_physpages = totalram_pages;
74 max_pfn = totalram_pages;
Jeff Dike009ec2a92008-02-04 22:30:53 -080075 printk(KERN_INFO "Memory: %luk available\n",
Geert Uytterhoevencc013a82009-09-21 17:02:36 -070076 nr_free_pages() << (PAGE_SHIFT-10));
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 kmalloc_ok = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79
Jeff Dike12f49642005-05-20 13:59:12 -070080/*
81 * Create a page table and place a pointer to it in a middle page
82 * directory entry.
83 */
84static void __init one_page_table_init(pmd_t *pmd)
85{
86 if (pmd_none(*pmd)) {
87 pte_t *pte = (pte_t *) alloc_bootmem_low_pages(PAGE_SIZE);
88 set_pmd(pmd, __pmd(_KERNPG_TABLE +
89 (unsigned long) __pa(pte)));
90 if (pte != pte_offset_kernel(pmd, 0))
91 BUG();
92 }
93}
94
95static void __init one_md_table_init(pud_t *pud)
96{
97#ifdef CONFIG_3_LEVEL_PGTABLES
98 pmd_t *pmd_table = (pmd_t *) alloc_bootmem_low_pages(PAGE_SIZE);
99 set_pud(pud, __pud(_KERNPG_TABLE + (unsigned long) __pa(pmd_table)));
100 if (pmd_table != pmd_offset(pud, 0))
101 BUG();
102#endif
103}
104
Jeff Dike009ec2a92008-02-04 22:30:53 -0800105static void __init fixrange_init(unsigned long start, unsigned long end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 pgd_t *pgd_base)
107{
108 pgd_t *pgd;
Jeff Dike12f49642005-05-20 13:59:12 -0700109 pud_t *pud;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 pmd_t *pmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 int i, j;
112 unsigned long vaddr;
113
114 vaddr = start;
115 i = pgd_index(vaddr);
116 j = pmd_index(vaddr);
117 pgd = pgd_base + i;
118
119 for ( ; (i < PTRS_PER_PGD) && (vaddr < end); pgd++, i++) {
Jeff Dike12f49642005-05-20 13:59:12 -0700120 pud = pud_offset(pgd, vaddr);
121 if (pud_none(*pud))
122 one_md_table_init(pud);
123 pmd = pmd_offset(pud, vaddr);
Jeff Dike655e4ed2008-02-04 22:30:55 -0800124 for (; (j < PTRS_PER_PMD) && (vaddr < end); pmd++, j++) {
Jeff Dike12f49642005-05-20 13:59:12 -0700125 one_page_table_init(pmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 vaddr += PMD_SIZE;
127 }
128 j = 0;
129 }
130}
131
132#ifdef CONFIG_HIGHMEM
133pte_t *kmap_pte;
134pgprot_t kmap_prot;
135
136#define kmap_get_fixmap_pte(vaddr) \
137 pte_offset_kernel(pmd_offset(pud_offset(pgd_offset_k(vaddr), (vaddr)),\
Jeff Dike8192ab42008-02-04 22:30:53 -0800138 (vaddr)), (vaddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140static void __init kmap_init(void)
141{
142 unsigned long kmap_vstart;
143
144 /* cache the first kmap pte */
145 kmap_vstart = __fix_to_virt(FIX_KMAP_BEGIN);
146 kmap_pte = kmap_get_fixmap_pte(kmap_vstart);
147
148 kmap_prot = PAGE_KERNEL;
149}
150
Al Virob4a08a12007-10-29 04:36:10 +0000151static void __init init_highmem(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
153 pgd_t *pgd;
154 pud_t *pud;
155 pmd_t *pmd;
156 pte_t *pte;
157 unsigned long vaddr;
158
159 /*
160 * Permanent kmaps:
161 */
162 vaddr = PKMAP_BASE;
163 fixrange_init(vaddr, vaddr + PAGE_SIZE*LAST_PKMAP, swapper_pg_dir);
164
165 pgd = swapper_pg_dir + pgd_index(vaddr);
166 pud = pud_offset(pgd, vaddr);
167 pmd = pmd_offset(pud, vaddr);
168 pte = pte_offset_kernel(pmd, vaddr);
169 pkmap_page_table = pte;
170
171 kmap_init();
172}
173#endif /* CONFIG_HIGHMEM */
174
175static void __init fixaddr_user_init( void)
176{
viro@ZenIV.linux.org.uk9a0b3862005-09-07 23:21:11 +0100177#ifdef CONFIG_ARCH_REUSE_HOST_VSYSCALL_AREA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 long size = FIXADDR_USER_END - FIXADDR_USER_START;
179 pgd_t *pgd;
180 pud_t *pud;
181 pmd_t *pmd;
182 pte_t *pte;
Jeff Dike655e4ed2008-02-04 22:30:55 -0800183 phys_t p;
184 unsigned long v, vaddr = FIXADDR_USER_START;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Jeff Dike655e4ed2008-02-04 22:30:55 -0800186 if (!size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 return;
188
189 fixrange_init( FIXADDR_USER_START, FIXADDR_USER_END, swapper_pg_dir);
Jeff Dike655e4ed2008-02-04 22:30:55 -0800190 v = (unsigned long) alloc_bootmem_low_pages(size);
191 memcpy((void *) v , (void *) FIXADDR_USER_START, size);
192 p = __pa(v);
Jeff Dike009ec2a92008-02-04 22:30:53 -0800193 for ( ; size > 0; size -= PAGE_SIZE, vaddr += PAGE_SIZE,
Jeff Dike655e4ed2008-02-04 22:30:55 -0800194 p += PAGE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 pgd = swapper_pg_dir + pgd_index(vaddr);
196 pud = pud_offset(pgd, vaddr);
197 pmd = pmd_offset(pud, vaddr);
198 pte = pte_offset_kernel(pmd, vaddr);
Jeff Dike655e4ed2008-02-04 22:30:55 -0800199 pte_set_val(*pte, p, PAGE_READONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
201#endif
202}
203
Jeff Dike36e45462007-05-06 14:51:11 -0700204void __init paging_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
206 unsigned long zones_size[MAX_NR_ZONES], vaddr;
207 int i;
208
209 empty_zero_page = (unsigned long *) alloc_bootmem_low_pages(PAGE_SIZE);
210 empty_bad_page = (unsigned long *) alloc_bootmem_low_pages(PAGE_SIZE);
Jeff Dike009ec2a92008-02-04 22:30:53 -0800211 for (i = 0; i < ARRAY_SIZE(zones_size); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 zones_size[i] = 0;
Jeff Dike91b165c2006-09-25 23:33:00 -0700213
Jeff Dike07155012006-09-27 01:50:34 -0700214 zones_size[ZONE_NORMAL] = (end_iomem >> PAGE_SHIFT) -
215 (uml_physmem >> PAGE_SHIFT);
Christoph Lametere53ef382006-09-25 23:31:14 -0700216#ifdef CONFIG_HIGHMEM
Paolo 'Blaisorblade' Giarrusso353f8d12005-11-07 00:58:58 -0800217 zones_size[ZONE_HIGHMEM] = highmem >> PAGE_SHIFT;
Christoph Lametere53ef382006-09-25 23:31:14 -0700218#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 free_area_init(zones_size);
220
221 /*
222 * Fixed mappings, only the page table structure has to be
223 * created - mappings will be set by set_fixmap():
224 */
225 vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK;
226 fixrange_init(vaddr, FIXADDR_TOP, swapper_pg_dir);
227
228 fixaddr_user_init();
229
230#ifdef CONFIG_HIGHMEM
231 init_highmem();
232#endif
233}
234
Jeff Dike8192ab42008-02-04 22:30:53 -0800235/*
236 * This can't do anything because nothing in the kernel image can be freed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 * since it's not in kernel physical memory.
238 */
239
240void free_initmem(void)
241{
242}
243
244#ifdef CONFIG_BLK_DEV_INITRD
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245void free_initrd_mem(unsigned long start, unsigned long end)
246{
Jiang Liu11199692013-07-03 15:02:48 -0700247 free_reserved_area((void *)start, (void *)end, 0, "initrd");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249#endif
250
Jeff Dike8192ab42008-02-04 22:30:53 -0800251/* Allocate and free page tables. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253pgd_t *pgd_alloc(struct mm_struct *mm)
254{
255 pgd_t *pgd = (pgd_t *)__get_free_page(GFP_KERNEL);
256
257 if (pgd) {
258 memset(pgd, 0, USER_PTRS_PER_PGD * sizeof(pgd_t));
Jeff Dike009ec2a92008-02-04 22:30:53 -0800259 memcpy(pgd + USER_PTRS_PER_PGD,
260 swapper_pg_dir + USER_PTRS_PER_PGD,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 (PTRS_PER_PGD - USER_PTRS_PER_PGD) * sizeof(pgd_t));
262 }
263 return pgd;
264}
265
Benjamin Herrenschmidt5e541972008-02-04 22:29:14 -0800266void pgd_free(struct mm_struct *mm, pgd_t *pgd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
268 free_page((unsigned long) pgd);
269}
270
271pte_t *pte_alloc_one_kernel(struct mm_struct *mm, unsigned long address)
272{
273 pte_t *pte;
274
275 pte = (pte_t *)__get_free_page(GFP_KERNEL|__GFP_REPEAT|__GFP_ZERO);
276 return pte;
277}
278
Martin Schwidefsky2f569af2008-02-08 04:22:04 -0800279pgtable_t pte_alloc_one(struct mm_struct *mm, unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
281 struct page *pte;
Jeff Dike60678bb2007-02-10 01:44:10 -0800282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 pte = alloc_page(GFP_KERNEL|__GFP_REPEAT|__GFP_ZERO);
Martin Schwidefsky2f569af2008-02-08 04:22:04 -0800284 if (pte)
285 pgtable_page_ctor(pte);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 return pte;
287}
Jeff Dike8192ab42008-02-04 22:30:53 -0800288
289#ifdef CONFIG_3_LEVEL_PGTABLES
290pmd_t *pmd_alloc_one(struct mm_struct *mm, unsigned long address)
291{
292 pmd_t *pmd = (pmd_t *) __get_free_page(GFP_KERNEL);
293
294 if (pmd)
295 memset(pmd, 0, PAGE_SIZE);
296
297 return pmd;
298}
299#endif
Jeff Dike43f5b302008-05-12 14:01:52 -0700300
301void *uml_kmalloc(int size, int flags)
302{
303 return kmalloc(size, flags);
304}