Linux 4.18.10
[linux/fpc-iii.git] / arch / um / kernel / mem.c
blob3c0e470ea6465872539a37ddab3229ab76eba868
1 /*
2 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
4 */
6 #include <linux/stddef.h>
7 #include <linux/module.h>
8 #include <linux/bootmem.h>
9 #include <linux/highmem.h>
10 #include <linux/mm.h>
11 #include <linux/swap.h>
12 #include <linux/slab.h>
13 #include <asm/fixmap.h>
14 #include <asm/page.h>
15 #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>
22 /* allocated in paging_init, zeroed in mem_init, and unchanged thereafter */
23 unsigned long *empty_zero_page = NULL;
24 EXPORT_SYMBOL(empty_zero_page);
27 * Initialized during boot, and readonly for initializing page tables
28 * afterwards
30 pgd_t swapper_pg_dir[PTRS_PER_PGD];
32 /* Initialized at boot time, and readonly after that */
33 unsigned long long highmem;
34 int kmalloc_ok = 0;
36 /* Used during early boot */
37 static unsigned long brk_end;
39 void __init mem_init(void)
41 /* clear the zero-page */
42 memset(empty_zero_page, 0, PAGE_SIZE);
44 /* Map in the area just after the brk now that kmalloc is about
45 * to be turned on.
47 brk_end = (unsigned long) UML_ROUND_UP(sbrk(0));
48 map_memory(brk_end, __pa(brk_end), uml_reserved - brk_end, 1, 1, 0);
49 free_bootmem(__pa(brk_end), uml_reserved - brk_end);
50 uml_reserved = brk_end;
52 /* this will put all low memory onto the freelists */
53 free_all_bootmem();
54 max_low_pfn = totalram_pages;
55 max_pfn = totalram_pages;
56 mem_init_print_info(NULL);
57 kmalloc_ok = 1;
61 * Create a page table and place a pointer to it in a middle page
62 * directory entry.
64 static void __init one_page_table_init(pmd_t *pmd)
66 if (pmd_none(*pmd)) {
67 pte_t *pte = (pte_t *) alloc_bootmem_low_pages(PAGE_SIZE);
68 set_pmd(pmd, __pmd(_KERNPG_TABLE +
69 (unsigned long) __pa(pte)));
70 if (pte != pte_offset_kernel(pmd, 0))
71 BUG();
75 static void __init one_md_table_init(pud_t *pud)
77 #ifdef CONFIG_3_LEVEL_PGTABLES
78 pmd_t *pmd_table = (pmd_t *) alloc_bootmem_low_pages(PAGE_SIZE);
79 set_pud(pud, __pud(_KERNPG_TABLE + (unsigned long) __pa(pmd_table)));
80 if (pmd_table != pmd_offset(pud, 0))
81 BUG();
82 #endif
85 static void __init fixrange_init(unsigned long start, unsigned long end,
86 pgd_t *pgd_base)
88 pgd_t *pgd;
89 pud_t *pud;
90 pmd_t *pmd;
91 int i, j;
92 unsigned long vaddr;
94 vaddr = start;
95 i = pgd_index(vaddr);
96 j = pmd_index(vaddr);
97 pgd = pgd_base + i;
99 for ( ; (i < PTRS_PER_PGD) && (vaddr < end); pgd++, i++) {
100 pud = pud_offset(pgd, vaddr);
101 if (pud_none(*pud))
102 one_md_table_init(pud);
103 pmd = pmd_offset(pud, vaddr);
104 for (; (j < PTRS_PER_PMD) && (vaddr < end); pmd++, j++) {
105 one_page_table_init(pmd);
106 vaddr += PMD_SIZE;
108 j = 0;
112 static void __init fixaddr_user_init( void)
114 #ifdef CONFIG_ARCH_REUSE_HOST_VSYSCALL_AREA
115 long size = FIXADDR_USER_END - FIXADDR_USER_START;
116 pgd_t *pgd;
117 pud_t *pud;
118 pmd_t *pmd;
119 pte_t *pte;
120 phys_t p;
121 unsigned long v, vaddr = FIXADDR_USER_START;
123 if (!size)
124 return;
126 fixrange_init( FIXADDR_USER_START, FIXADDR_USER_END, swapper_pg_dir);
127 v = (unsigned long) alloc_bootmem_low_pages(size);
128 memcpy((void *) v , (void *) FIXADDR_USER_START, size);
129 p = __pa(v);
130 for ( ; size > 0; size -= PAGE_SIZE, vaddr += PAGE_SIZE,
131 p += PAGE_SIZE) {
132 pgd = swapper_pg_dir + pgd_index(vaddr);
133 pud = pud_offset(pgd, vaddr);
134 pmd = pmd_offset(pud, vaddr);
135 pte = pte_offset_kernel(pmd, vaddr);
136 pte_set_val(*pte, p, PAGE_READONLY);
138 #endif
141 void __init paging_init(void)
143 unsigned long zones_size[MAX_NR_ZONES], vaddr;
144 int i;
146 empty_zero_page = (unsigned long *) alloc_bootmem_low_pages(PAGE_SIZE);
147 for (i = 0; i < ARRAY_SIZE(zones_size); i++)
148 zones_size[i] = 0;
150 zones_size[ZONE_NORMAL] = (end_iomem >> PAGE_SHIFT) -
151 (uml_physmem >> PAGE_SHIFT);
152 free_area_init(zones_size);
155 * Fixed mappings, only the page table structure has to be
156 * created - mappings will be set by set_fixmap():
158 vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK;
159 fixrange_init(vaddr, FIXADDR_TOP, swapper_pg_dir);
161 fixaddr_user_init();
165 * This can't do anything because nothing in the kernel image can be freed
166 * since it's not in kernel physical memory.
169 void free_initmem(void)
173 #ifdef CONFIG_BLK_DEV_INITRD
174 void free_initrd_mem(unsigned long start, unsigned long end)
176 free_reserved_area((void *)start, (void *)end, -1, "initrd");
178 #endif
180 /* Allocate and free page tables. */
182 pgd_t *pgd_alloc(struct mm_struct *mm)
184 pgd_t *pgd = (pgd_t *)__get_free_page(GFP_KERNEL);
186 if (pgd) {
187 memset(pgd, 0, USER_PTRS_PER_PGD * sizeof(pgd_t));
188 memcpy(pgd + USER_PTRS_PER_PGD,
189 swapper_pg_dir + USER_PTRS_PER_PGD,
190 (PTRS_PER_PGD - USER_PTRS_PER_PGD) * sizeof(pgd_t));
192 return pgd;
195 void pgd_free(struct mm_struct *mm, pgd_t *pgd)
197 free_page((unsigned long) pgd);
200 pte_t *pte_alloc_one_kernel(struct mm_struct *mm, unsigned long address)
202 pte_t *pte;
204 pte = (pte_t *)__get_free_page(GFP_KERNEL|__GFP_ZERO);
205 return pte;
208 pgtable_t pte_alloc_one(struct mm_struct *mm, unsigned long address)
210 struct page *pte;
212 pte = alloc_page(GFP_KERNEL|__GFP_ZERO);
213 if (!pte)
214 return NULL;
215 if (!pgtable_page_ctor(pte)) {
216 __free_page(pte);
217 return NULL;
219 return pte;
222 #ifdef CONFIG_3_LEVEL_PGTABLES
223 pmd_t *pmd_alloc_one(struct mm_struct *mm, unsigned long address)
225 pmd_t *pmd = (pmd_t *) __get_free_page(GFP_KERNEL);
227 if (pmd)
228 memset(pmd, 0, PAGE_SIZE);
230 return pmd;
232 #endif
234 void *uml_kmalloc(int size, int flags)
236 return kmalloc(size, flags);