Subversion Repositories HelenOS

Rev

Rev 746 | Rev 756 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
684 jermar 1
/*
2
 * Copyright (C) 2006 Jakub Jermar
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
29
#include <genarch/mm/page_ht.h>
30
#include <mm/page.h>
31
#include <mm/frame.h>
699 jermar 32
#include <mm/heap.h>
755 jermar 33
#include <mm/as.h>
687 jermar 34
#include <arch/mm/asid.h>
684 jermar 35
#include <arch/types.h>
36
#include <typedefs.h>
37
#include <arch/asm.h>
746 jermar 38
#include <synch/spinlock.h>
39
#include <arch.h>
699 jermar 40
#include <debug.h>
684 jermar 41
 
746 jermar 42
/**
43
 * This lock protects the page hash table. Note that software must
44
 * be still careful about ordering of writes to ensure consistent
45
 * view of the page hash table for hardware helpers such as VHPT
46
 * walker on ia64.
47
 */
48
SPINLOCK_INITIALIZE(page_ht_lock);
49
 
50
/**
51
 * Page hash table pointer.
52
 * The page hash table may be accessed only when page_ht_lock is held.
53
 */
54
pte_t *page_ht = NULL;
55
 
755 jermar 56
static void ht_mapping_insert(as_t *as, __address page, __address frame, int flags, __address root);
57
static pte_t *ht_mapping_find(as_t *as, __address page, __address root);
684 jermar 58
 
59
page_operations_t page_ht_operations = {
60
    .mapping_insert = ht_mapping_insert,
61
    .mapping_find = ht_mapping_find
62
};
63
 
64
/** Map page to frame using page hash table.
65
 *
66
 * Map virtual address 'page' to physical address 'frame'
746 jermar 67
 * using 'flags'. In order not to disturb hardware searching,
68
 * new mappings are appended to the end of the collision
69
 * chain.
684 jermar 70
 *
755 jermar 71
 * @param as Address space to which page belongs. Must be locked prior the call.
684 jermar 72
 * @param page Virtual address of the page to be mapped.
73
 * @param frame Physical address of memory frame to which the mapping is done.
74
 * @param flags Flags to be used for mapping.
699 jermar 75
 * @param root Ignored.
684 jermar 76
 */
755 jermar 77
void ht_mapping_insert(as_t *as, __address page, __address frame, int flags, __address root)
684 jermar 78
{
746 jermar 79
    pte_t *t, *u;
80
    ipl_t ipl;
699 jermar 81
 
746 jermar 82
    ipl = interrupts_disable();
83
    spinlock_lock(&page_ht_lock);
755 jermar 84
 
85
    t = HT_HASH(page, as->asid);
699 jermar 86
    if (!HT_SLOT_EMPTY(t)) {
746 jermar 87
 
88
        /*
89
         * The slot is occupied.
90
         * Walk through the collision chain and append the mapping to its end.
91
         */
92
 
93
        do {
94
            u = t;
755 jermar 95
            if (HT_COMPARE(page, as->asid, t)) {
746 jermar 96
                /*
97
                 * Nothing to do,
98
                 * the record is already there.
99
                 */
100
                spinlock_unlock(&page_ht_lock);
101
                interrupts_restore(ipl);
102
                return;
103
            }
104
        } while ((t = HT_GET_NEXT(t)));
105
 
106
        t = (pte_t *) malloc(sizeof(pte_t));    /* FIXME: use slab allocator for this */
107
        if (!t)
108
            panic("could not allocate memory\n");
109
 
110
        HT_SET_NEXT(u, t);
699 jermar 111
    }
746 jermar 112
 
755 jermar 113
    HT_SET_RECORD(t, page, as->asid, frame, flags);
746 jermar 114
    HT_SET_NEXT(t, NULL);
115
 
116
    spinlock_unlock(&page_ht_lock);
117
    interrupts_restore(ipl);
684 jermar 118
}
119
 
120
/** Find mapping for virtual page in page hash table.
121
 *
122
 * Find mapping for virtual page.
123
 *
746 jermar 124
 * Interrupts must be disabled.
125
 *
755 jermar 126
 * @param as Address space to wich page belongs. Must be locked prior the call.
684 jermar 127
 * @param page Virtual page.
699 jermar 128
 * @param root Ignored.
684 jermar 129
 *
699 jermar 130
 * @return NULL if there is no such mapping; requested mapping otherwise.
684 jermar 131
 */
755 jermar 132
pte_t *ht_mapping_find(as_t *as, __address page, __address root)
684 jermar 133
{
699 jermar 134
    pte_t *t;
135
 
746 jermar 136
    spinlock_lock(&page_ht_lock);
755 jermar 137
    t = HT_HASH(page, as->asid);
746 jermar 138
    if (!HT_SLOT_EMPTY(t)) {
755 jermar 139
        while (!HT_COMPARE(page, as->asid, t) && HT_GET_NEXT(t))
746 jermar 140
            t = HT_GET_NEXT(t);
755 jermar 141
        t = HT_COMPARE(page, as->asid, t) ? t : NULL;
746 jermar 142
    } else {
143
        t = NULL;
144
    }
145
    spinlock_unlock(&page_ht_lock);
146
    return t;
147
}
148
 
149
/** Invalidate page hash table.
150
 *
151
 * Interrupts must be disabled.
152
 */
153
void ht_invalidate_all(void)
154
{
155
    pte_t *t, *u;
156
    int i;
699 jermar 157
 
746 jermar 158
    spinlock_lock(&page_ht_lock);
159
    for (i = 0; i < HT_ENTRIES; i++) {
160
        if (!HT_SLOT_EMPTY(&page_ht[i])) {
161
            t = HT_GET_NEXT(&page_ht[i]);
162
            while (t) {
163
                u = t;
164
                t = HT_GET_NEXT(t);
165
                free(u);        /* FIXME: use slab allocator for this */
166
            }
167
            HT_INVALIDATE_SLOT(&page_ht[i]);
168
        }
169
    }
170
    spinlock_unlock(&page_ht_lock);
684 jermar 171
}