linux/arch/um/kernel/mem.c
<<
>>
Prefs
   1/*
   2 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
   3 * Licensed under the GPL
   4 */
   5
   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>
  21
  22/* allocated in paging_init, zeroed in mem_init, and unchanged thereafter */
  23unsigned long *empty_zero_page = NULL;
  24EXPORT_SYMBOL(empty_zero_page);
  25/* allocated in paging_init and unchanged thereafter */
  26static unsigned long *empty_bad_page = NULL;
  27
  28/*
  29 * Initialized during boot, and readonly for initializing page tables
  30 * afterwards
  31 */
  32pgd_t swapper_pg_dir[PTRS_PER_PGD];
  33
  34/* Initialized at boot time, and readonly after that */
  35unsigned long long highmem;
  36int kmalloc_ok = 0;
  37
  38/* Used during early boot */
  39static unsigned long brk_end;
  40
  41#ifdef CONFIG_HIGHMEM
  42static void setup_highmem(unsigned long highmem_start,
  43                          unsigned long highmem_len)
  44{
  45        unsigned long highmem_pfn;
  46        int i;
  47
  48        highmem_pfn = __pa(highmem_start) >> PAGE_SHIFT;
  49        for (i = 0; i < highmem_len >> PAGE_SHIFT; i++)
  50                free_highmem_page(&mem_map[highmem_pfn + i]);
  51}
  52#endif
  53
  54void __init mem_init(void)
  55{
  56        /* clear the zero-page */
  57        memset(empty_zero_page, 0, PAGE_SIZE);
  58
  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));
  63        map_memory(brk_end, __pa(brk_end), uml_reserved - brk_end, 1, 1, 0);
  64        free_bootmem(__pa(brk_end), uml_reserved - brk_end);
  65        uml_reserved = brk_end;
  66
  67        /* this will put all low memory onto the freelists */
  68        totalram_pages = free_all_bootmem();
  69        max_low_pfn = totalram_pages;
  70#ifdef CONFIG_HIGHMEM
  71        setup_highmem(end_iomem, highmem);
  72#endif
  73        num_physpages = totalram_pages;
  74        max_pfn = totalram_pages;
  75        printk(KERN_INFO "Memory: %luk available\n",
  76               nr_free_pages() << (PAGE_SHIFT-10));
  77        kmalloc_ok = 1;
  78}
  79
  80/*
  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
 105static void __init fixrange_init(unsigned long start, unsigned long end,
 106                                 pgd_t *pgd_base)
 107{
 108        pgd_t *pgd;
 109        pud_t *pud;
 110        pmd_t *pmd;
 111        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++) {
 120                pud = pud_offset(pgd, vaddr);
 121                if (pud_none(*pud))
 122                        one_md_table_init(pud);
 123                pmd = pmd_offset(pud, vaddr);
 124                for (; (j < PTRS_PER_PMD) && (vaddr < end); pmd++, j++) {
 125                        one_page_table_init(pmd);
 126                        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)),\
 138                                     (vaddr)), (vaddr))
 139
 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
 151static void __init init_highmem(void)
 152{
 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{
 177#ifdef CONFIG_ARCH_REUSE_HOST_VSYSCALL_AREA
 178        long size = FIXADDR_USER_END - FIXADDR_USER_START;
 179        pgd_t *pgd;
 180        pud_t *pud;
 181        pmd_t *pmd;
 182        pte_t *pte;
 183        phys_t p;
 184        unsigned long v, vaddr = FIXADDR_USER_START;
 185
 186        if (!size)
 187                return;
 188
 189        fixrange_init( FIXADDR_USER_START, FIXADDR_USER_END, swapper_pg_dir);
 190        v = (unsigned long) alloc_bootmem_low_pages(size);
 191        memcpy((void *) v , (void *) FIXADDR_USER_START, size);
 192        p = __pa(v);
 193        for ( ; size > 0; size -= PAGE_SIZE, vaddr += PAGE_SIZE,
 194                      p += PAGE_SIZE) {
 195                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);
 199                pte_set_val(*pte, p, PAGE_READONLY);
 200        }
 201#endif
 202}
 203
 204void __init paging_init(void)
 205{
 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);
 211        for (i = 0; i < ARRAY_SIZE(zones_size); i++)
 212                zones_size[i] = 0;
 213
 214        zones_size[ZONE_NORMAL] = (end_iomem >> PAGE_SHIFT) -
 215                (uml_physmem >> PAGE_SHIFT);
 216#ifdef CONFIG_HIGHMEM
 217        zones_size[ZONE_HIGHMEM] = highmem >> PAGE_SHIFT;
 218#endif
 219        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
 235/*
 236 * This can't do anything because nothing in the kernel image can be freed
 237 * since it's not in kernel physical memory.
 238 */
 239
 240void free_initmem(void)
 241{
 242}
 243
 244#ifdef CONFIG_BLK_DEV_INITRD
 245void free_initrd_mem(unsigned long start, unsigned long end)
 246{
 247        free_reserved_area(start, end, 0, "initrd");
 248}
 249#endif
 250
 251/* Allocate and free page tables. */
 252
 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));
 259                memcpy(pgd + USER_PTRS_PER_PGD,
 260                       swapper_pg_dir + USER_PTRS_PER_PGD,
 261                       (PTRS_PER_PGD - USER_PTRS_PER_PGD) * sizeof(pgd_t));
 262        }
 263        return pgd;
 264}
 265
 266void pgd_free(struct mm_struct *mm, pgd_t *pgd)
 267{
 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
 279pgtable_t pte_alloc_one(struct mm_struct *mm, unsigned long address)
 280{
 281        struct page *pte;
 282
 283        pte = alloc_page(GFP_KERNEL|__GFP_REPEAT|__GFP_ZERO);
 284        if (pte)
 285                pgtable_page_ctor(pte);
 286        return pte;
 287}
 288
 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
 300
 301void *uml_kmalloc(int size, int flags)
 302{
 303        return kmalloc(size, flags);
 304}
 305