Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Russell King | 0c66898 | 2006-09-27 15:40:28 +0100 | [diff] [blame] | 2 | * linux/arch/arm/mm/pgd.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
Russell King | 9007205 | 2005-10-28 14:48:37 +0100 | [diff] [blame] | 4 | * Copyright (C) 1998-2005 Russell King |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/highmem.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | |
| 13 | #include <asm/pgalloc.h> |
| 14 | #include <asm/page.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <asm/tlbflush.h> |
| 16 | |
Russell King | 1b2e2b7 | 2006-08-21 17:06:38 +0100 | [diff] [blame] | 17 | #include "mm.h" |
| 18 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #define FIRST_KERNEL_PGD_NR (FIRST_USER_PGD_NR + USER_PTRS_PER_PGD) |
| 20 | |
| 21 | /* |
| 22 | * need to get a 16k page for level 1 |
| 23 | */ |
| 24 | pgd_t *get_pgd_slow(struct mm_struct *mm) |
| 25 | { |
| 26 | pgd_t *new_pgd, *init_pgd; |
| 27 | 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 King | 59f0cb0 | 2008-10-27 11:24:09 +0000 | [diff] [blame] | 34 | memset(new_pgd, 0, FIRST_KERNEL_PGD_NR * sizeof(pgd_t)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
Russell King | a343e60 | 2005-06-27 14:08:56 +0100 | [diff] [blame] | 36 | /* |
| 37 | * Copy over the kernel and IO PGD entries |
| 38 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | init_pgd = pgd_offset_k(0); |
Russell King | a343e60 | 2005-06-27 14:08:56 +0100 | [diff] [blame] | 40 | memcpy(new_pgd + FIRST_KERNEL_PGD_NR, init_pgd + FIRST_KERNEL_PGD_NR, |
| 41 | (PTRS_PER_PGD - FIRST_KERNEL_PGD_NR) * sizeof(pgd_t)); |
| 42 | |
| 43 | clean_dcache_area(new_pgd, PTRS_PER_PGD * sizeof(pgd_t)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
| 45 | if (!vectors_high()) { |
| 46 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | * On ARM, first page must always be allocated since it |
| 48 | * contains the machine vectors. |
| 49 | */ |
| 50 | new_pmd = pmd_alloc(mm, new_pgd, 0); |
| 51 | if (!new_pmd) |
| 52 | goto no_pmd; |
| 53 | |
| 54 | new_pte = pte_alloc_map(mm, new_pmd, 0); |
| 55 | if (!new_pte) |
| 56 | goto no_pte; |
| 57 | |
| 58 | init_pmd = pmd_offset(init_pgd, 0); |
| 59 | init_pte = pte_offset_map_nested(init_pmd, 0); |
Russell King | ad1ae2f | 2006-12-13 14:34:43 +0000 | [diff] [blame] | 60 | set_pte_ext(new_pte, *init_pte, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | pte_unmap_nested(init_pte); |
| 62 | pte_unmap(new_pte); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | return new_pgd; |
| 66 | |
| 67 | no_pte: |
Benjamin Herrenschmidt | 5e54197 | 2008-02-04 22:29:14 -0800 | [diff] [blame] | 68 | pmd_free(mm, new_pmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | no_pmd: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | free_pages((unsigned long)new_pgd, 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | no_pgd: |
| 72 | return NULL; |
| 73 | } |
| 74 | |
Benjamin Herrenschmidt | 5e54197 | 2008-02-04 22:29:14 -0800 | [diff] [blame] | 75 | void free_pgd_slow(struct mm_struct *mm, pgd_t *pgd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | { |
| 77 | pmd_t *pmd; |
Uwe Kleine-König | 0c82d83 | 2008-02-27 13:44:59 +0100 | [diff] [blame] | 78 | pgtable_t pte; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
| 80 | if (!pgd) |
| 81 | return; |
| 82 | |
| 83 | /* pgd is always present and good */ |
Russell King | 155bb14 | 2005-05-09 20:52:51 +0100 | [diff] [blame] | 84 | pmd = pmd_off(pgd, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | if (pmd_none(*pmd)) |
| 86 | goto free; |
| 87 | if (pmd_bad(*pmd)) { |
| 88 | pmd_ERROR(*pmd); |
| 89 | pmd_clear(pmd); |
| 90 | goto free; |
| 91 | } |
| 92 | |
Uwe Kleine-König | 0c82d83 | 2008-02-27 13:44:59 +0100 | [diff] [blame] | 93 | pte = pmd_pgtable(*pmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | pmd_clear(pmd); |
Benjamin Herrenschmidt | 5e54197 | 2008-02-04 22:29:14 -0800 | [diff] [blame] | 95 | pte_free(mm, pte); |
| 96 | pmd_free(mm, pmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | free: |
| 98 | free_pages((unsigned long) pgd, 2); |
| 99 | } |