blob: f7fafb1741f4c6a78e89b239cd864936f6e5189c [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/*
21 * need to get a 16k page for level 1
22 */
Russell Kingb0d03742010-11-21 11:00:56 +000023pgd_t *pgd_alloc(struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -070024{
25 pgd_t *new_pgd, *init_pgd;
Russell King516295e2010-11-21 16:27:49 +000026 pud_t *new_pud, *init_pud;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 pmd_t *new_pmd, *init_pmd;
28 pte_t *new_pte, *init_pte;
29
30 new_pgd = (pgd_t *)__get_free_pages(GFP_KERNEL, 2);
31 if (!new_pgd)
32 goto no_pgd;
33
Russell Kinge926f442010-11-21 11:55:37 +000034 memset(new_pgd, 0, USER_PTRS_PER_PGD * sizeof(pgd_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Russell Kinga343e602005-06-27 14:08:56 +010036 /*
37 * Copy over the kernel and IO PGD entries
38 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 init_pgd = pgd_offset_k(0);
Russell Kinge926f442010-11-21 11:55:37 +000040 memcpy(new_pgd + USER_PTRS_PER_PGD, init_pgd + USER_PTRS_PER_PGD,
41 (PTRS_PER_PGD - USER_PTRS_PER_PGD) * sizeof(pgd_t));
Russell Kinga343e602005-06-27 14:08:56 +010042
43 clean_dcache_area(new_pgd, PTRS_PER_PGD * sizeof(pgd_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45 if (!vectors_high()) {
46 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 * On ARM, first page must always be allocated since it
48 * contains the machine vectors.
49 */
Russell King516295e2010-11-21 16:27:49 +000050 new_pud = pud_alloc(mm, new_pgd, 0);
51 if (!new_pud)
52 goto no_pud;
53
54 new_pmd = pmd_alloc(mm, new_pud, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 if (!new_pmd)
56 goto no_pmd;
57
58 new_pte = pte_alloc_map(mm, new_pmd, 0);
59 if (!new_pte)
60 goto no_pte;
61
Russell King516295e2010-11-21 16:27:49 +000062 init_pud = pud_offset(init_pgd, 0);
63 init_pmd = pmd_offset(init_pud, 0);
Peter Zijlstraece0e2b2010-10-26 14:21:52 -070064 init_pte = pte_offset_map(init_pmd, 0);
Russell Kingad1ae2f2006-12-13 14:34:43 +000065 set_pte_ext(new_pte, *init_pte, 0);
Peter Zijlstraece0e2b2010-10-26 14:21:52 -070066 pte_unmap(init_pte);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 pte_unmap(new_pte);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 }
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 return new_pgd;
71
72no_pte:
Benjamin Herrenschmidt5e541972008-02-04 22:29:14 -080073 pmd_free(mm, new_pmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074no_pmd:
Russell King516295e2010-11-21 16:27:49 +000075 pud_free(mm, new_pud);
76no_pud:
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 free_pages((unsigned long)new_pgd, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078no_pgd:
79 return NULL;
80}
81
Russell King6e4beb52010-11-21 23:48:55 +000082void pgd_free(struct mm_struct *mm, pgd_t *pgd_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
Russell King6e4beb52010-11-21 23:48:55 +000084 pgd_t *pgd;
Russell King516295e2010-11-21 16:27:49 +000085 pud_t *pud;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 pmd_t *pmd;
Uwe Kleine-König0c82d832008-02-27 13:44:59 +010087 pgtable_t pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Russell King6e4beb52010-11-21 23:48:55 +000089 if (!pgd_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 return;
91
Russell King6e4beb52010-11-21 23:48:55 +000092 pgd = pgd_base + pgd_index(0);
93 if (pgd_none_or_clear_bad(pgd))
94 goto no_pgd;
95
Russell King516295e2010-11-21 16:27:49 +000096 pud = pud_offset(pgd, 0);
97 if (pud_none_or_clear_bad(pud))
98 goto no_pud;
99
100 pmd = pmd_offset(pud, 0);
Russell King6e4beb52010-11-21 23:48:55 +0000101 if (pmd_none_or_clear_bad(pmd))
102 goto no_pmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Uwe Kleine-König0c82d832008-02-27 13:44:59 +0100104 pte = pmd_pgtable(*pmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 pmd_clear(pmd);
Benjamin Herrenschmidt5e541972008-02-04 22:29:14 -0800106 pte_free(mm, pte);
Russell King6e4beb52010-11-21 23:48:55 +0000107no_pmd:
Russell King516295e2010-11-21 16:27:49 +0000108 pud_clear(pud);
Benjamin Herrenschmidt5e541972008-02-04 22:29:14 -0800109 pmd_free(mm, pmd);
Russell King516295e2010-11-21 16:27:49 +0000110no_pud:
111 pgd_clear(pgd);
112 pud_free(mm, pud);
Russell King6e4beb52010-11-21 23:48:55 +0000113no_pgd:
114 free_pages((unsigned long) pgd_base, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}