blob: d15785eb73a70194527576d74195f35a58137758 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Russell King0c668982006-09-27 15:40:28 +01002 * linux/arch/arm/mm/pgd.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Russell King90072052005-10-28 14:48:37 +01004 * Copyright (C) 1998-2005 Russell King
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
14#include <asm/pgalloc.h>
15#include <asm/page.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <asm/tlbflush.h>
17
Russell King1b2e2b72006-08-21 17:06:38 +010018#include "mm.h"
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#define FIRST_KERNEL_PGD_NR (FIRST_USER_PGD_NR + USER_PTRS_PER_PGD)
21
22/*
23 * need to get a 16k page for level 1
24 */
Russell Kingb0d03742010-11-21 11:00:56 +000025pgd_t *pgd_alloc(struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -070026{
27 pgd_t *new_pgd, *init_pgd;
28 pmd_t *new_pmd, *init_pmd;
29 pte_t *new_pte, *init_pte;
30
31 new_pgd = (pgd_t *)__get_free_pages(GFP_KERNEL, 2);
32 if (!new_pgd)
33 goto no_pgd;
34
Russell King59f0cb02008-10-27 11:24:09 +000035 memset(new_pgd, 0, FIRST_KERNEL_PGD_NR * sizeof(pgd_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Russell Kinga343e602005-06-27 14:08:56 +010037 /*
38 * Copy over the kernel and IO PGD entries
39 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 init_pgd = pgd_offset_k(0);
Russell Kinga343e602005-06-27 14:08:56 +010041 memcpy(new_pgd + FIRST_KERNEL_PGD_NR, init_pgd + FIRST_KERNEL_PGD_NR,
42 (PTRS_PER_PGD - FIRST_KERNEL_PGD_NR) * sizeof(pgd_t));
43
44 clean_dcache_area(new_pgd, PTRS_PER_PGD * sizeof(pgd_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46 if (!vectors_high()) {
47 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 * On ARM, first page must always be allocated since it
49 * contains the machine vectors.
50 */
51 new_pmd = pmd_alloc(mm, new_pgd, 0);
52 if (!new_pmd)
53 goto no_pmd;
54
55 new_pte = pte_alloc_map(mm, new_pmd, 0);
56 if (!new_pte)
57 goto no_pte;
58
59 init_pmd = pmd_offset(init_pgd, 0);
Peter Zijlstraece0e2b2010-10-26 14:21:52 -070060 init_pte = pte_offset_map(init_pmd, 0);
Russell Kingad1ae2f2006-12-13 14:34:43 +000061 set_pte_ext(new_pte, *init_pte, 0);
Peter Zijlstraece0e2b2010-10-26 14:21:52 -070062 pte_unmap(init_pte);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 pte_unmap(new_pte);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 }
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 return new_pgd;
67
68no_pte:
Benjamin Herrenschmidt5e541972008-02-04 22:29:14 -080069 pmd_free(mm, new_pmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070no_pmd:
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 free_pages((unsigned long)new_pgd, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072no_pgd:
73 return NULL;
74}
75
Russell King6e4beb52010-11-21 23:48:55 +000076void pgd_free(struct mm_struct *mm, pgd_t *pgd_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
Russell King6e4beb52010-11-21 23:48:55 +000078 pgd_t *pgd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 pmd_t *pmd;
Uwe Kleine-König0c82d832008-02-27 13:44:59 +010080 pgtable_t pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Russell King6e4beb52010-11-21 23:48:55 +000082 if (!pgd_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 return;
84
Russell King6e4beb52010-11-21 23:48:55 +000085 pgd = pgd_base + pgd_index(0);
86 if (pgd_none_or_clear_bad(pgd))
87 goto no_pgd;
88
89 pmd = pmd_offset(pgd, 0);
90 if (pmd_none_or_clear_bad(pmd))
91 goto no_pmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Uwe Kleine-König0c82d832008-02-27 13:44:59 +010093 pte = pmd_pgtable(*pmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 pmd_clear(pmd);
Benjamin Herrenschmidt5e541972008-02-04 22:29:14 -080095 pte_free(mm, pte);
Russell King6e4beb52010-11-21 23:48:55 +000096no_pmd:
97 pgd_clear(pgd);
Benjamin Herrenschmidt5e541972008-02-04 22:29:14 -080098 pmd_free(mm, pmd);
Russell King6e4beb52010-11-21 23:48:55 +000099no_pgd:
100 free_pages((unsigned long) pgd_base, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}