Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #ifndef _X86_64_PGALLOC_H |
| 2 | #define _X86_64_PGALLOC_H |
| 3 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | #include <asm/pda.h> |
| 5 | #include <linux/threads.h> |
| 6 | #include <linux/mm.h> |
| 7 | |
| 8 | #define pmd_populate_kernel(mm, pmd, pte) \ |
| 9 | set_pmd(pmd, __pmd(_PAGE_TABLE | __pa(pte))) |
| 10 | #define pud_populate(mm, pud, pmd) \ |
| 11 | set_pud(pud, __pud(_PAGE_TABLE | __pa(pmd))) |
| 12 | #define pgd_populate(mm, pgd, pud) \ |
| 13 | set_pgd(pgd, __pgd(_PAGE_TABLE | __pa(pud))) |
| 14 | |
| 15 | static inline void pmd_populate(struct mm_struct *mm, pmd_t *pmd, struct page *pte) |
| 16 | { |
| 17 | set_pmd(pmd, __pmd(_PAGE_TABLE | (page_to_pfn(pte) << PAGE_SHIFT))); |
| 18 | } |
| 19 | |
Adrian Bunk | 9c0aa0f | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 20 | static inline void pmd_free(pmd_t *pmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | { |
| 22 | BUG_ON((unsigned long)pmd & (PAGE_SIZE-1)); |
| 23 | free_page((unsigned long)pmd); |
| 24 | } |
| 25 | |
| 26 | static inline pmd_t *pmd_alloc_one (struct mm_struct *mm, unsigned long addr) |
| 27 | { |
| 28 | return (pmd_t *)get_zeroed_page(GFP_KERNEL|__GFP_REPEAT); |
| 29 | } |
| 30 | |
| 31 | static inline pud_t *pud_alloc_one(struct mm_struct *mm, unsigned long addr) |
| 32 | { |
| 33 | return (pud_t *)get_zeroed_page(GFP_KERNEL|__GFP_REPEAT); |
| 34 | } |
| 35 | |
| 36 | static inline void pud_free (pud_t *pud) |
| 37 | { |
| 38 | BUG_ON((unsigned long)pud & (PAGE_SIZE-1)); |
| 39 | free_page((unsigned long)pud); |
| 40 | } |
| 41 | |
Jan Beulich | 8c914cb | 2006-03-25 16:29:40 +0100 | [diff] [blame] | 42 | static inline void pgd_list_add(pgd_t *pgd) |
| 43 | { |
| 44 | struct page *page = virt_to_page(pgd); |
| 45 | |
| 46 | spin_lock(&pgd_lock); |
| 47 | page->index = (pgoff_t)pgd_list; |
| 48 | if (pgd_list) |
| 49 | pgd_list->private = (unsigned long)&page->index; |
| 50 | pgd_list = page; |
| 51 | page->private = (unsigned long)&pgd_list; |
| 52 | spin_unlock(&pgd_lock); |
| 53 | } |
| 54 | |
| 55 | static inline void pgd_list_del(pgd_t *pgd) |
| 56 | { |
| 57 | struct page *next, **pprev, *page = virt_to_page(pgd); |
| 58 | |
| 59 | spin_lock(&pgd_lock); |
| 60 | next = (struct page *)page->index; |
| 61 | pprev = (struct page **)page->private; |
| 62 | *pprev = next; |
| 63 | if (next) |
| 64 | next->private = (unsigned long)pprev; |
| 65 | spin_unlock(&pgd_lock); |
| 66 | } |
| 67 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | static inline pgd_t *pgd_alloc(struct mm_struct *mm) |
| 69 | { |
| 70 | unsigned boundary; |
| 71 | pgd_t *pgd = (pgd_t *)__get_free_page(GFP_KERNEL|__GFP_REPEAT); |
| 72 | if (!pgd) |
| 73 | return NULL; |
Jan Beulich | 8c914cb | 2006-03-25 16:29:40 +0100 | [diff] [blame] | 74 | pgd_list_add(pgd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | /* |
| 76 | * Copy kernel pointers in from init. |
| 77 | * Could keep a freelist or slab cache of those because the kernel |
| 78 | * part never changes. |
| 79 | */ |
| 80 | boundary = pgd_index(__PAGE_OFFSET); |
| 81 | memset(pgd, 0, boundary * sizeof(pgd_t)); |
| 82 | memcpy(pgd + boundary, |
| 83 | init_level4_pgt + boundary, |
| 84 | (PTRS_PER_PGD - boundary) * sizeof(pgd_t)); |
| 85 | return pgd; |
| 86 | } |
| 87 | |
| 88 | static inline void pgd_free(pgd_t *pgd) |
| 89 | { |
| 90 | BUG_ON((unsigned long)pgd & (PAGE_SIZE-1)); |
Jan Beulich | 8c914cb | 2006-03-25 16:29:40 +0100 | [diff] [blame] | 91 | pgd_list_del(pgd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | free_page((unsigned long)pgd); |
| 93 | } |
| 94 | |
| 95 | static inline pte_t *pte_alloc_one_kernel(struct mm_struct *mm, unsigned long address) |
| 96 | { |
| 97 | return (pte_t *)get_zeroed_page(GFP_KERNEL|__GFP_REPEAT); |
| 98 | } |
| 99 | |
| 100 | static inline struct page *pte_alloc_one(struct mm_struct *mm, unsigned long address) |
| 101 | { |
| 102 | void *p = (void *)get_zeroed_page(GFP_KERNEL|__GFP_REPEAT); |
| 103 | if (!p) |
| 104 | return NULL; |
| 105 | return virt_to_page(p); |
| 106 | } |
| 107 | |
| 108 | /* Should really implement gc for free page table pages. This could be |
| 109 | done with a reference count in struct page. */ |
| 110 | |
Adrian Bunk | 9c0aa0f | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 111 | static inline void pte_free_kernel(pte_t *pte) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | { |
| 113 | BUG_ON((unsigned long)pte & (PAGE_SIZE-1)); |
| 114 | free_page((unsigned long)pte); |
| 115 | } |
| 116 | |
Adrian Bunk | 9c0aa0f | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 117 | static inline void pte_free(struct page *pte) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | { |
| 119 | __free_page(pte); |
| 120 | } |
| 121 | |
| 122 | #define __pte_free_tlb(tlb,pte) tlb_remove_page((tlb),(pte)) |
| 123 | |
| 124 | #define __pmd_free_tlb(tlb,x) tlb_remove_page((tlb),virt_to_page(x)) |
| 125 | #define __pud_free_tlb(tlb,x) tlb_remove_page((tlb),virt_to_page(x)) |
| 126 | |
| 127 | #endif /* _X86_64_PGALLOC_H */ |