blob: c1c1a5c67da1418c8822fda4b282578175bf6ec3 [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>
Catalin Marinasda028772011-11-22 17:30:29 +000013#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
Russell King15d07dc2012-03-28 18:30:01 +010015#include <asm/cp15.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <asm/pgalloc.h>
17#include <asm/page.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <asm/tlbflush.h>
19
Russell King1b2e2b72006-08-21 17:06:38 +010020#include "mm.h"
21
Catalin Marinasda028772011-11-22 17:30:29 +000022#ifdef CONFIG_ARM_LPAE
23#define __pgd_alloc() kmalloc(PTRS_PER_PGD * sizeof(pgd_t), GFP_KERNEL)
24#define __pgd_free(pgd) kfree(pgd)
25#else
Michal Hocko397b0802016-07-26 15:20:59 -070026#define __pgd_alloc() (pgd_t *)__get_free_pages(GFP_KERNEL, 2)
Catalin Marinasda028772011-11-22 17:30:29 +000027#define __pgd_free(pgd) free_pages((unsigned long)pgd, 2)
28#endif
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030/*
31 * need to get a 16k page for level 1
32 */
Russell Kingb0d03742010-11-21 11:00:56 +000033pgd_t *pgd_alloc(struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
35 pgd_t *new_pgd, *init_pgd;
Russell King516295e2010-11-21 16:27:49 +000036 pud_t *new_pud, *init_pud;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 pmd_t *new_pmd, *init_pmd;
38 pte_t *new_pte, *init_pte;
39
Catalin Marinasda028772011-11-22 17:30:29 +000040 new_pgd = __pgd_alloc();
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 if (!new_pgd)
42 goto no_pgd;
43
Russell Kinge926f442010-11-21 11:55:37 +000044 memset(new_pgd, 0, USER_PTRS_PER_PGD * sizeof(pgd_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Russell Kinga343e602005-06-27 14:08:56 +010046 /*
47 * Copy over the kernel and IO PGD entries
48 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 init_pgd = pgd_offset_k(0);
Russell Kinge926f442010-11-21 11:55:37 +000050 memcpy(new_pgd + USER_PTRS_PER_PGD, init_pgd + USER_PTRS_PER_PGD,
51 (PTRS_PER_PGD - USER_PTRS_PER_PGD) * sizeof(pgd_t));
Russell Kinga343e602005-06-27 14:08:56 +010052
53 clean_dcache_area(new_pgd, PTRS_PER_PGD * sizeof(pgd_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Catalin Marinasda028772011-11-22 17:30:29 +000055#ifdef CONFIG_ARM_LPAE
56 /*
57 * Allocate PMD table for modules and pkmap mappings.
58 */
59 new_pud = pud_alloc(mm, new_pgd + pgd_index(MODULES_VADDR),
60 MODULES_VADDR);
61 if (!new_pud)
62 goto no_pud;
63
64 new_pmd = pmd_alloc(mm, new_pud, 0);
65 if (!new_pmd)
66 goto no_pmd;
67#endif
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 if (!vectors_high()) {
70 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 * On ARM, first page must always be allocated since it
Catalin Marinasda028772011-11-22 17:30:29 +000072 * contains the machine vectors. The vectors are always high
73 * with LPAE.
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 */
Russell King516295e2010-11-21 16:27:49 +000075 new_pud = pud_alloc(mm, new_pgd, 0);
76 if (!new_pud)
77 goto no_pud;
78
79 new_pmd = pmd_alloc(mm, new_pud, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 if (!new_pmd)
81 goto no_pmd;
82
Kirill A. Shutemov3ed3a4f2016-03-17 14:19:11 -070083 new_pte = pte_alloc_map(mm, new_pmd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 if (!new_pte)
85 goto no_pte;
86
Russell Kinga02d8df2015-08-21 09:38:31 +010087#ifndef CONFIG_ARM_LPAE
88 /*
89 * Modify the PTE pointer to have the correct domain. This
90 * needs to be the vectors domain to avoid the low vectors
91 * being unmapped.
92 */
93 pmd_val(*new_pmd) &= ~PMD_DOMAIN_MASK;
94 pmd_val(*new_pmd) |= PMD_DOMAIN(DOMAIN_VECTORS);
95#endif
96
Russell King516295e2010-11-21 16:27:49 +000097 init_pud = pud_offset(init_pgd, 0);
98 init_pmd = pmd_offset(init_pud, 0);
Peter Zijlstraece0e2b2010-10-26 14:21:52 -070099 init_pte = pte_offset_map(init_pmd, 0);
Russell Kingd8aa7122013-11-28 21:43:40 +0000100 set_pte_ext(new_pte + 0, init_pte[0], 0);
101 set_pte_ext(new_pte + 1, init_pte[1], 0);
Peter Zijlstraece0e2b2010-10-26 14:21:52 -0700102 pte_unmap(init_pte);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 pte_unmap(new_pte);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 }
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 return new_pgd;
107
108no_pte:
Benjamin Herrenschmidt5e541972008-02-04 22:29:14 -0800109 pmd_free(mm, new_pmd);
Kirill A. Shutemovb30fe6c2015-02-11 15:26:53 -0800110 mm_dec_nr_pmds(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111no_pmd:
Russell King516295e2010-11-21 16:27:49 +0000112 pud_free(mm, new_pud);
113no_pud:
Catalin Marinasda028772011-11-22 17:30:29 +0000114 __pgd_free(new_pgd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115no_pgd:
116 return NULL;
117}
118
Russell King6e4beb52010-11-21 23:48:55 +0000119void pgd_free(struct mm_struct *mm, pgd_t *pgd_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
Russell King6e4beb52010-11-21 23:48:55 +0000121 pgd_t *pgd;
Russell King516295e2010-11-21 16:27:49 +0000122 pud_t *pud;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 pmd_t *pmd;
Uwe Kleine-König0c82d832008-02-27 13:44:59 +0100124 pgtable_t pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Russell King6e4beb52010-11-21 23:48:55 +0000126 if (!pgd_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 return;
128
Russell King6e4beb52010-11-21 23:48:55 +0000129 pgd = pgd_base + pgd_index(0);
130 if (pgd_none_or_clear_bad(pgd))
131 goto no_pgd;
132
Russell King516295e2010-11-21 16:27:49 +0000133 pud = pud_offset(pgd, 0);
134 if (pud_none_or_clear_bad(pud))
135 goto no_pud;
136
137 pmd = pmd_offset(pud, 0);
Russell King6e4beb52010-11-21 23:48:55 +0000138 if (pmd_none_or_clear_bad(pmd))
139 goto no_pmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Uwe Kleine-König0c82d832008-02-27 13:44:59 +0100141 pte = pmd_pgtable(*pmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 pmd_clear(pmd);
Benjamin Herrenschmidt5e541972008-02-04 22:29:14 -0800143 pte_free(mm, pte);
Kirill A. Shutemovb30fe6c2015-02-11 15:26:53 -0800144 atomic_long_dec(&mm->nr_ptes);
Russell King6e4beb52010-11-21 23:48:55 +0000145no_pmd:
Russell King516295e2010-11-21 16:27:49 +0000146 pud_clear(pud);
Benjamin Herrenschmidt5e541972008-02-04 22:29:14 -0800147 pmd_free(mm, pmd);
Kirill A. Shutemovb30fe6c2015-02-11 15:26:53 -0800148 mm_dec_nr_pmds(mm);
Russell King516295e2010-11-21 16:27:49 +0000149no_pud:
150 pgd_clear(pgd);
151 pud_free(mm, pud);
Russell King6e4beb52010-11-21 23:48:55 +0000152no_pgd:
Catalin Marinasda028772011-11-22 17:30:29 +0000153#ifdef CONFIG_ARM_LPAE
154 /*
155 * Free modules/pkmap or identity pmd tables.
156 */
157 for (pgd = pgd_base; pgd < pgd_base + PTRS_PER_PGD; pgd++) {
158 if (pgd_none_or_clear_bad(pgd))
159 continue;
160 if (pgd_val(*pgd) & L_PGD_SWAPPER)
161 continue;
162 pud = pud_offset(pgd, 0);
163 if (pud_none_or_clear_bad(pud))
164 continue;
165 pmd = pmd_offset(pud, 0);
166 pud_clear(pud);
167 pmd_free(mm, pmd);
Kirill A. Shutemovb30fe6c2015-02-11 15:26:53 -0800168 mm_dec_nr_pmds(mm);
Catalin Marinasda028772011-11-22 17:30:29 +0000169 pgd_clear(pgd);
170 pud_free(mm, pud);
171 }
172#endif
173 __pgd_free(pgd_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}