blob: 709244c66fa3148b3f144310216240fdadfac9a6 [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;
26 pmd_t *new_pmd, *init_pmd;
27 pte_t *new_pte, *init_pte;
28
29 new_pgd = (pgd_t *)__get_free_pages(GFP_KERNEL, 2);
30 if (!new_pgd)
31 goto no_pgd;
32
Russell Kinge926f442010-11-21 11:55:37 +000033 memset(new_pgd, 0, USER_PTRS_PER_PGD * sizeof(pgd_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Russell Kinga343e602005-06-27 14:08:56 +010035 /*
36 * Copy over the kernel and IO PGD entries
37 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 init_pgd = pgd_offset_k(0);
Russell Kinge926f442010-11-21 11:55:37 +000039 memcpy(new_pgd + USER_PTRS_PER_PGD, init_pgd + USER_PTRS_PER_PGD,
40 (PTRS_PER_PGD - USER_PTRS_PER_PGD) * sizeof(pgd_t));
Russell Kinga343e602005-06-27 14:08:56 +010041
42 clean_dcache_area(new_pgd, PTRS_PER_PGD * sizeof(pgd_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44 if (!vectors_high()) {
45 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * On ARM, first page must always be allocated since it
47 * contains the machine vectors.
48 */
49 new_pmd = pmd_alloc(mm, new_pgd, 0);
50 if (!new_pmd)
51 goto no_pmd;
52
Andrea Arcangeli8ac1f832011-01-13 15:46:43 -080053 new_pte = pte_alloc_map(mm, NULL, new_pmd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 if (!new_pte)
55 goto no_pte;
56
57 init_pmd = pmd_offset(init_pgd, 0);
Peter Zijlstraece0e2b2010-10-26 14:21:52 -070058 init_pte = pte_offset_map(init_pmd, 0);
Russell Kingad1ae2f2006-12-13 14:34:43 +000059 set_pte_ext(new_pte, *init_pte, 0);
Peter Zijlstraece0e2b2010-10-26 14:21:52 -070060 pte_unmap(init_pte);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 pte_unmap(new_pte);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 }
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 return new_pgd;
65
66no_pte:
Benjamin Herrenschmidt5e541972008-02-04 22:29:14 -080067 pmd_free(mm, new_pmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068no_pmd:
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 free_pages((unsigned long)new_pgd, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070no_pgd:
71 return NULL;
72}
73
Russell King6e4beb52010-11-21 23:48:55 +000074void pgd_free(struct mm_struct *mm, pgd_t *pgd_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
Russell King6e4beb52010-11-21 23:48:55 +000076 pgd_t *pgd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 pmd_t *pmd;
Uwe Kleine-König0c82d832008-02-27 13:44:59 +010078 pgtable_t pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Russell King6e4beb52010-11-21 23:48:55 +000080 if (!pgd_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 return;
82
Russell King6e4beb52010-11-21 23:48:55 +000083 pgd = pgd_base + pgd_index(0);
84 if (pgd_none_or_clear_bad(pgd))
85 goto no_pgd;
86
87 pmd = pmd_offset(pgd, 0);
88 if (pmd_none_or_clear_bad(pmd))
89 goto no_pmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Uwe Kleine-König0c82d832008-02-27 13:44:59 +010091 pte = pmd_pgtable(*pmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 pmd_clear(pmd);
Benjamin Herrenschmidt5e541972008-02-04 22:29:14 -080093 pte_free(mm, pte);
Russell King6e4beb52010-11-21 23:48:55 +000094no_pmd:
95 pgd_clear(pgd);
Benjamin Herrenschmidt5e541972008-02-04 22:29:14 -080096 pmd_free(mm, pmd);
Russell King6e4beb52010-11-21 23:48:55 +000097no_pgd:
98 free_pages((unsigned long) pgd_base, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}