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