Rev 699 | Rev 755 | 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> |
687 | jermar | 33 | #include <arch/mm/asid.h> |
684 | jermar | 34 | #include <arch/types.h> |
35 | #include <typedefs.h> |
||
36 | #include <arch/asm.h> |
||
746 | jermar | 37 | #include <synch/spinlock.h> |
38 | #include <arch.h> |
||
699 | jermar | 39 | #include <debug.h> |
684 | jermar | 40 | |
746 | jermar | 41 | /** |
42 | * This lock protects the page hash table. Note that software must |
||
43 | * be still careful about ordering of writes to ensure consistent |
||
44 | * view of the page hash table for hardware helpers such as VHPT |
||
45 | * walker on ia64. |
||
46 | */ |
||
47 | SPINLOCK_INITIALIZE(page_ht_lock); |
||
48 | |||
49 | /** |
||
50 | * Page hash table pointer. |
||
51 | * The page hash table may be accessed only when page_ht_lock is held. |
||
52 | */ |
||
53 | pte_t *page_ht = NULL; |
||
54 | |||
687 | jermar | 55 | static void ht_mapping_insert(__address page, asid_t asid, __address frame, int flags, __address root); |
56 | static pte_t *ht_mapping_find(__address page, asid_t asid, __address root); |
||
684 | jermar | 57 | |
58 | page_operations_t page_ht_operations = { |
||
59 | .mapping_insert = ht_mapping_insert, |
||
60 | .mapping_find = ht_mapping_find |
||
61 | }; |
||
62 | |||
63 | /** Map page to frame using page hash table. |
||
64 | * |
||
65 | * Map virtual address 'page' to physical address 'frame' |
||
746 | jermar | 66 | * using 'flags'. In order not to disturb hardware searching, |
67 | * new mappings are appended to the end of the collision |
||
68 | * chain. |
||
684 | jermar | 69 | * |
70 | * @param page Virtual address of the page to be mapped. |
||
687 | jermar | 71 | * @param asid Address space to which page belongs. |
684 | jermar | 72 | * @param frame Physical address of memory frame to which the mapping is done. |
73 | * @param flags Flags to be used for mapping. |
||
699 | jermar | 74 | * @param root Ignored. |
684 | jermar | 75 | */ |
699 | jermar | 76 | void ht_mapping_insert(__address page, asid_t asid, __address frame, int flags, __address root) |
684 | jermar | 77 | { |
746 | jermar | 78 | pte_t *t, *u; |
79 | ipl_t ipl; |
||
699 | jermar | 80 | |
746 | jermar | 81 | ipl = interrupts_disable(); |
82 | spinlock_lock(&page_ht_lock); |
||
83 | |||
699 | jermar | 84 | t = HT_HASH(page, asid); |
85 | if (!HT_SLOT_EMPTY(t)) { |
||
746 | jermar | 86 | |
87 | /* |
||
88 | * The slot is occupied. |
||
89 | * Walk through the collision chain and append the mapping to its end. |
||
90 | */ |
||
91 | |||
92 | do { |
||
93 | u = t; |
||
94 | if (HT_COMPARE(page, asid, t)) { |
||
95 | /* |
||
96 | * Nothing to do, |
||
97 | * the record is already there. |
||
98 | */ |
||
99 | spinlock_unlock(&page_ht_lock); |
||
100 | interrupts_restore(ipl); |
||
101 | return; |
||
102 | } |
||
103 | } while ((t = HT_GET_NEXT(t))); |
||
104 | |||
105 | t = (pte_t *) malloc(sizeof(pte_t)); /* FIXME: use slab allocator for this */ |
||
106 | if (!t) |
||
107 | panic("could not allocate memory\n"); |
||
108 | |||
109 | HT_SET_NEXT(u, t); |
||
699 | jermar | 110 | } |
746 | jermar | 111 | |
699 | jermar | 112 | HT_SET_RECORD(t, page, asid, frame, flags); |
746 | jermar | 113 | HT_SET_NEXT(t, NULL); |
114 | |||
115 | spinlock_unlock(&page_ht_lock); |
||
116 | interrupts_restore(ipl); |
||
684 | jermar | 117 | } |
118 | |||
119 | /** Find mapping for virtual page in page hash table. |
||
120 | * |
||
121 | * Find mapping for virtual page. |
||
122 | * |
||
746 | jermar | 123 | * Interrupts must be disabled. |
124 | * |
||
684 | jermar | 125 | * @param page Virtual page. |
687 | jermar | 126 | * @param asid Address space to wich page belongs. |
699 | jermar | 127 | * @param root Ignored. |
684 | jermar | 128 | * |
699 | jermar | 129 | * @return NULL if there is no such mapping; requested mapping otherwise. |
684 | jermar | 130 | */ |
687 | jermar | 131 | pte_t *ht_mapping_find(__address page, asid_t asid, __address root) |
684 | jermar | 132 | { |
699 | jermar | 133 | pte_t *t; |
134 | |||
746 | jermar | 135 | spinlock_lock(&page_ht_lock); |
699 | jermar | 136 | t = HT_HASH(page, asid); |
746 | jermar | 137 | if (!HT_SLOT_EMPTY(t)) { |
138 | while (!HT_COMPARE(page, asid, t) && HT_GET_NEXT(t)) |
||
139 | t = HT_GET_NEXT(t); |
||
140 | t = HT_COMPARE(page, asid, t) ? t : NULL; |
||
141 | } else { |
||
142 | t = NULL; |
||
143 | } |
||
144 | spinlock_unlock(&page_ht_lock); |
||
145 | return t; |
||
146 | } |
||
147 | |||
148 | /** Invalidate page hash table. |
||
149 | * |
||
150 | * Interrupts must be disabled. |
||
151 | */ |
||
152 | void ht_invalidate_all(void) |
||
153 | { |
||
154 | pte_t *t, *u; |
||
155 | int i; |
||
699 | jermar | 156 | |
746 | jermar | 157 | spinlock_lock(&page_ht_lock); |
158 | for (i = 0; i < HT_ENTRIES; i++) { |
||
159 | if (!HT_SLOT_EMPTY(&page_ht[i])) { |
||
160 | t = HT_GET_NEXT(&page_ht[i]); |
||
161 | while (t) { |
||
162 | u = t; |
||
163 | t = HT_GET_NEXT(t); |
||
164 | free(u); /* FIXME: use slab allocator for this */ |
||
165 | } |
||
166 | HT_INVALIDATE_SLOT(&page_ht[i]); |
||
167 | } |
||
168 | } |
||
169 | spinlock_unlock(&page_ht_lock); |
||
684 | jermar | 170 | } |