pgalloc_64.h 2.9 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#ifndef _X86_64_PGALLOC_H
#define _X86_64_PGALLOC_H

#include <asm/pda.h>
#include <linux/threads.h>
#include <linux/mm.h>

#define pmd_populate_kernel(mm, pmd, pte) \
		set_pmd(pmd, __pmd(_PAGE_TABLE | __pa(pte)))
#define pud_populate(mm, pud, pmd) \
		set_pud(pud, __pud(_PAGE_TABLE | __pa(pmd)))
#define pgd_populate(mm, pgd, pud) \
		set_pgd(pgd, __pgd(_PAGE_TABLE | __pa(pud)))

static inline void pmd_populate(struct mm_struct *mm, pmd_t *pmd, struct page *pte)
{
	set_pmd(pmd, __pmd(_PAGE_TABLE | (page_to_pfn(pte) << PAGE_SHIFT)));
}

20
static inline void pmd_free(pmd_t *pmd)
L
Linus Torvalds 已提交
21 22
{
	BUG_ON((unsigned long)pmd & (PAGE_SIZE-1));
23
	free_page((unsigned long)pmd);
L
Linus Torvalds 已提交
24 25 26 27
}

static inline pmd_t *pmd_alloc_one (struct mm_struct *mm, unsigned long addr)
{
28
	return (pmd_t *)get_zeroed_page(GFP_KERNEL|__GFP_REPEAT);
L
Linus Torvalds 已提交
29 30 31 32
}

static inline pud_t *pud_alloc_one(struct mm_struct *mm, unsigned long addr)
{
33
	return (pud_t *)get_zeroed_page(GFP_KERNEL|__GFP_REPEAT);
L
Linus Torvalds 已提交
34 35 36 37 38
}

static inline void pud_free (pud_t *pud)
{
	BUG_ON((unsigned long)pud & (PAGE_SIZE-1));
39
	free_page((unsigned long)pud);
L
Linus Torvalds 已提交
40 41
}

42 43 44 45 46
static inline void pgd_list_add(pgd_t *pgd)
{
	struct page *page = virt_to_page(pgd);

	spin_lock(&pgd_lock);
47
	list_add(&page->lru, &pgd_list);
48 49 50 51 52
	spin_unlock(&pgd_lock);
}

static inline void pgd_list_del(pgd_t *pgd)
{
53
	struct page *page = virt_to_page(pgd);
54 55

	spin_lock(&pgd_lock);
56
	list_del(&page->lru);
57 58 59
	spin_unlock(&pgd_lock);
}

60
static inline pgd_t *pgd_alloc(struct mm_struct *mm)
L
Linus Torvalds 已提交
61 62
{
	unsigned boundary;
63 64 65 66
	pgd_t *pgd = (pgd_t *)__get_free_page(GFP_KERNEL|__GFP_REPEAT);
	if (!pgd)
		return NULL;
	pgd_list_add(pgd);
L
Linus Torvalds 已提交
67 68
	/*
	 * Copy kernel pointers in from init.
69 70
	 * Could keep a freelist or slab cache of those because the kernel
	 * part never changes.
L
Linus Torvalds 已提交
71 72
	 */
	boundary = pgd_index(__PAGE_OFFSET);
73
	memset(pgd, 0, boundary * sizeof(pgd_t));
L
Linus Torvalds 已提交
74
	memcpy(pgd + boundary,
75 76
	       init_level4_pgt + boundary,
	       (PTRS_PER_PGD - boundary) * sizeof(pgd_t));
L
Linus Torvalds 已提交
77 78 79 80 81 82
	return pgd;
}

static inline void pgd_free(pgd_t *pgd)
{
	BUG_ON((unsigned long)pgd & (PAGE_SIZE-1));
83 84
	pgd_list_del(pgd);
	free_page((unsigned long)pgd);
L
Linus Torvalds 已提交
85 86 87 88
}

static inline pte_t *pte_alloc_one_kernel(struct mm_struct *mm, unsigned long address)
{
89
	return (pte_t *)get_zeroed_page(GFP_KERNEL|__GFP_REPEAT);
L
Linus Torvalds 已提交
90 91 92 93
}

static inline struct page *pte_alloc_one(struct mm_struct *mm, unsigned long address)
{
94
	void *p = (void *)get_zeroed_page(GFP_KERNEL|__GFP_REPEAT);
L
Linus Torvalds 已提交
95 96 97 98 99 100 101 102
	if (!p)
		return NULL;
	return virt_to_page(p);
}

/* Should really implement gc for free page table pages. This could be
   done with a reference count in struct page. */

103
static inline void pte_free_kernel(pte_t *pte)
L
Linus Torvalds 已提交
104 105
{
	BUG_ON((unsigned long)pte & (PAGE_SIZE-1));
106
	free_page((unsigned long)pte); 
L
Linus Torvalds 已提交
107 108
}

109
static inline void pte_free(struct page *pte)
L
Linus Torvalds 已提交
110
{
111 112
	__free_page(pte);
} 
L
Linus Torvalds 已提交
113

114
#define __pte_free_tlb(tlb,pte) tlb_remove_page((tlb),(pte))
L
Linus Torvalds 已提交
115

116 117
#define __pmd_free_tlb(tlb,x)   tlb_remove_page((tlb),virt_to_page(x))
#define __pud_free_tlb(tlb,x)   tlb_remove_page((tlb),virt_to_page(x))
L
Linus Torvalds 已提交
118 119

#endif /* _X86_64_PGALLOC_H */