Rev 753 | Rev 823 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 753 | Rev 788 | ||
---|---|---|---|
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 | /* |
29 | /* |
30 | * ASID management. |
30 | * ASID management. |
31 | * |
31 | * |
32 | * Modern processor architectures optimize TLB utilization |
32 | * Modern processor architectures optimize TLB utilization |
33 | * by using ASIDs (a.k.a. memory contexts on sparc64 and |
33 | * by using ASIDs (a.k.a. memory contexts on sparc64 and |
34 | * region identifiers on ia64). These ASIDs help to associate |
34 | * region identifiers on ia64). These ASIDs help to associate |
35 | * each TLB item with an address space, thus making |
35 | * each TLB item with an address space, thus making |
36 | * finer-grained TLB invalidation possible. |
36 | * finer-grained TLB invalidation possible. |
37 | * |
37 | * |
38 | * Unfortunatelly, there are usually less ASIDs available than |
38 | * Unfortunatelly, there are usually less ASIDs available than |
39 | * there can be unique as_t structures (i.e. address spaces |
39 | * there can be unique as_t structures (i.e. address spaces |
40 | * recognized by the kernel). |
40 | * recognized by the kernel). |
41 | * |
41 | * |
42 | * When system runs short of ASIDs, it will attempt to steal |
42 | * When system runs short of ASIDs, it will attempt to steal |
43 | * ASID from an address space that has not been active for |
43 | * ASID from an address space that has not been active for |
44 | * a while. |
44 | * a while. |
45 | * |
45 | * |
46 | * Architectures that don't have hardware support for address |
46 | * Architectures that don't have hardware support for address |
47 | * spaces do not compile with this file. |
47 | * spaces do not compile with this file. |
48 | */ |
48 | */ |
49 | 49 | ||
50 | #include <mm/asid.h> |
50 | #include <mm/asid.h> |
51 | #include <mm/as.h> |
51 | #include <mm/as.h> |
52 | #include <mm/tlb.h> |
52 | #include <mm/tlb.h> |
53 | #include <arch/mm/asid.h> |
53 | #include <arch/mm/asid.h> |
54 | #include <synch/spinlock.h> |
54 | #include <synch/spinlock.h> |
55 | #include <arch.h> |
55 | #include <arch.h> |
56 | #include <list.h> |
56 | #include <adt/list.h> |
57 | #include <debug.h> |
57 | #include <debug.h> |
58 | 58 | ||
59 | /** |
59 | /** |
60 | * asidlock protects both the asids_allocated counter |
60 | * asidlock protects both the asids_allocated counter |
61 | * and the list of address spaces that were already |
61 | * and the list of address spaces that were already |
62 | * assigned ASID. |
62 | * assigned ASID. |
63 | */ |
63 | */ |
64 | SPINLOCK_INITIALIZE(asidlock); |
64 | SPINLOCK_INITIALIZE(asidlock); |
65 | 65 | ||
66 | static count_t asids_allocated = 0; |
66 | static count_t asids_allocated = 0; |
67 | 67 | ||
68 | /** |
68 | /** |
69 | * List of address spaces with assigned ASID. |
69 | * List of address spaces with assigned ASID. |
70 | * When the system runs short of allocable |
70 | * When the system runs short of allocable |
71 | * ASIDS, inactive address spaces are guaranteed |
71 | * ASIDS, inactive address spaces are guaranteed |
72 | * to be at the beginning of the list. |
72 | * to be at the beginning of the list. |
73 | */ |
73 | */ |
74 | LIST_INITIALIZE(as_with_asid_head); |
74 | LIST_INITIALIZE(as_with_asid_head); |
75 | 75 | ||
76 | 76 | ||
77 | /** Allocate free address space identifier. |
77 | /** Allocate free address space identifier. |
78 | * |
78 | * |
79 | * This code depends on the fact that ASIDS_ALLOCABLE |
79 | * This code depends on the fact that ASIDS_ALLOCABLE |
80 | * is greater than number of supported CPUs. |
80 | * is greater than number of supported CPUs. |
81 | * |
81 | * |
82 | * @return New ASID. |
82 | * @return New ASID. |
83 | */ |
83 | */ |
84 | asid_t asid_get(void) |
84 | asid_t asid_get(void) |
85 | { |
85 | { |
86 | ipl_t ipl; |
86 | ipl_t ipl; |
87 | asid_t asid; |
87 | asid_t asid; |
88 | link_t *tmp; |
88 | link_t *tmp; |
89 | as_t *as; |
89 | as_t *as; |
90 | 90 | ||
91 | /* |
91 | /* |
92 | * Check if there is an unallocated ASID. |
92 | * Check if there is an unallocated ASID. |
93 | */ |
93 | */ |
94 | 94 | ||
95 | ipl = interrupts_disable(); |
95 | ipl = interrupts_disable(); |
96 | spinlock_lock(&asidlock); |
96 | spinlock_lock(&asidlock); |
97 | if (asids_allocated == ASIDS_ALLOCABLE) { |
97 | if (asids_allocated == ASIDS_ALLOCABLE) { |
98 | 98 | ||
99 | /* |
99 | /* |
100 | * All ASIDs are already allocated. |
100 | * All ASIDs are already allocated. |
101 | * Resort to stealing. |
101 | * Resort to stealing. |
102 | */ |
102 | */ |
103 | 103 | ||
104 | /* |
104 | /* |
105 | * Remove the first item on the list. |
105 | * Remove the first item on the list. |
106 | * It is guaranteed to belong to an |
106 | * It is guaranteed to belong to an |
107 | * inactive address space. |
107 | * inactive address space. |
108 | */ |
108 | */ |
109 | tmp = as_with_asid_head.next; |
109 | tmp = as_with_asid_head.next; |
110 | ASSERT(tmp != &as_with_asid_head); |
110 | ASSERT(tmp != &as_with_asid_head); |
111 | list_remove(tmp); |
111 | list_remove(tmp); |
112 | 112 | ||
113 | as = list_get_instance(tmp, as_t, as_with_asid_link); |
113 | as = list_get_instance(tmp, as_t, as_with_asid_link); |
114 | spinlock_lock(&as->lock); |
114 | spinlock_lock(&as->lock); |
115 | 115 | ||
116 | /* |
116 | /* |
117 | * Steal the ASID. |
117 | * Steal the ASID. |
118 | * Note that the stolen ASID is not active. |
118 | * Note that the stolen ASID is not active. |
119 | */ |
119 | */ |
120 | asid = as->asid; |
120 | asid = as->asid; |
121 | ASSERT(asid != ASID_INVALID); |
121 | ASSERT(asid != ASID_INVALID); |
122 | 122 | ||
123 | /* |
123 | /* |
124 | * Notify the address space from wich the ASID |
124 | * Notify the address space from wich the ASID |
125 | * was stolen by invalidating its asid member. |
125 | * was stolen by invalidating its asid member. |
126 | */ |
126 | */ |
127 | as->asid = ASID_INVALID; |
127 | as->asid = ASID_INVALID; |
128 | spinlock_unlock(&as->lock); |
128 | spinlock_unlock(&as->lock); |
129 | 129 | ||
130 | /* |
130 | /* |
131 | * Get the system rid of the stolen ASID. |
131 | * Get the system rid of the stolen ASID. |
132 | */ |
132 | */ |
133 | tlb_shootdown_start(TLB_INVL_ASID, asid, 0, 0); |
133 | tlb_shootdown_start(TLB_INVL_ASID, asid, 0, 0); |
134 | tlb_shootdown_finalize(); |
134 | tlb_shootdown_finalize(); |
135 | tlb_invalidate_asid(asid); |
135 | tlb_invalidate_asid(asid); |
136 | } else { |
136 | } else { |
137 | 137 | ||
138 | /* |
138 | /* |
139 | * There is at least one unallocated ASID. |
139 | * There is at least one unallocated ASID. |
140 | * Find it and assign it. |
140 | * Find it and assign it. |
141 | */ |
141 | */ |
142 | 142 | ||
143 | asid = asid_find_free(); |
143 | asid = asid_find_free(); |
144 | asids_allocated++; |
144 | asids_allocated++; |
145 | } |
145 | } |
146 | 146 | ||
147 | spinlock_unlock(&asidlock); |
147 | spinlock_unlock(&asidlock); |
148 | interrupts_restore(ipl); |
148 | interrupts_restore(ipl); |
149 | 149 | ||
150 | return asid; |
150 | return asid; |
151 | } |
151 | } |
152 | 152 | ||
153 | /** Release address space identifier. |
153 | /** Release address space identifier. |
154 | * |
154 | * |
155 | * This code relies on architecture |
155 | * This code relies on architecture |
156 | * dependent functionality. |
156 | * dependent functionality. |
157 | * |
157 | * |
158 | * @param asid ASID to be released. |
158 | * @param asid ASID to be released. |
159 | */ |
159 | */ |
160 | void asid_put(asid_t asid) |
160 | void asid_put(asid_t asid) |
161 | { |
161 | { |
162 | ipl_t ipl; |
162 | ipl_t ipl; |
163 | 163 | ||
164 | ipl = interrupts_disable(); |
164 | ipl = interrupts_disable(); |
165 | spinlock_lock(&asidlock); |
165 | spinlock_lock(&asidlock); |
166 | 166 | ||
167 | asids_allocated--; |
167 | asids_allocated--; |
168 | asid_put_arch(asid); |
168 | asid_put_arch(asid); |
169 | 169 | ||
170 | spinlock_unlock(&asidlock); |
170 | spinlock_unlock(&asidlock); |
171 | interrupts_restore(ipl); |
171 | interrupts_restore(ipl); |
172 | } |
172 | } |
173 | 173 | ||
174 | /** Install ASID. |
174 | /** Install ASID. |
175 | * |
175 | * |
176 | * This function is to be executed on each address space switch. |
176 | * This function is to be executed on each address space switch. |
177 | * |
177 | * |
178 | * @param as Address space. |
178 | * @param as Address space. |
179 | */ |
179 | */ |
180 | void asid_install(as_t *as) |
180 | void asid_install(as_t *as) |
181 | { |
181 | { |
182 | ipl_t ipl; |
182 | ipl_t ipl; |
183 | 183 | ||
184 | ipl = interrupts_disable(); |
184 | ipl = interrupts_disable(); |
185 | spinlock_lock(&asidlock); |
185 | spinlock_lock(&asidlock); |
186 | spinlock_lock(&as->lock); |
186 | spinlock_lock(&as->lock); |
187 | 187 | ||
188 | if (as->asid != ASID_KERNEL) { |
188 | if (as->asid != ASID_KERNEL) { |
189 | if (as->asid != ASID_INVALID) { |
189 | if (as->asid != ASID_INVALID) { |
190 | /* |
190 | /* |
191 | * This address space has valid ASID. |
191 | * This address space has valid ASID. |
192 | * Remove 'as' from the list of address spaces |
192 | * Remove 'as' from the list of address spaces |
193 | * with assigned ASID, so that it can be later |
193 | * with assigned ASID, so that it can be later |
194 | * appended to the tail of the same list. |
194 | * appended to the tail of the same list. |
195 | * This is to prevent stealing of ASIDs from |
195 | * This is to prevent stealing of ASIDs from |
196 | * recently installed address spaces. |
196 | * recently installed address spaces. |
197 | */ |
197 | */ |
198 | list_remove(&as->as_with_asid_link); |
198 | list_remove(&as->as_with_asid_link); |
199 | } else { |
199 | } else { |
200 | spinlock_unlock(&as->lock); |
200 | spinlock_unlock(&as->lock); |
201 | spinlock_unlock(&asidlock); |
201 | spinlock_unlock(&asidlock); |
202 | 202 | ||
203 | /* |
203 | /* |
204 | * This address space doesn't have ASID assigned. |
204 | * This address space doesn't have ASID assigned. |
205 | * It was stolen or the address space is being |
205 | * It was stolen or the address space is being |
206 | * installed for the first time. |
206 | * installed for the first time. |
207 | * Allocate new ASID for it. |
207 | * Allocate new ASID for it. |
208 | */ |
208 | */ |
209 | as->asid = asid_get(); |
209 | as->asid = asid_get(); |
210 | spinlock_lock(&asidlock); |
210 | spinlock_lock(&asidlock); |
211 | spinlock_lock(&as->lock); |
211 | spinlock_lock(&as->lock); |
212 | } |
212 | } |
213 | 213 | ||
214 | /* |
214 | /* |
215 | * Now it is sure that 'as' has ASID. |
215 | * Now it is sure that 'as' has ASID. |
216 | * It is therefore appended to the list |
216 | * It is therefore appended to the list |
217 | * of address spaces from which it can |
217 | * of address spaces from which it can |
218 | * be stolen. |
218 | * be stolen. |
219 | */ |
219 | */ |
220 | list_append(&as->as_with_asid_link, &as_with_asid_head); |
220 | list_append(&as->as_with_asid_link, &as_with_asid_head); |
221 | } |
221 | } |
222 | 222 | ||
223 | spinlock_unlock(&as->lock); |
223 | spinlock_unlock(&as->lock); |
224 | spinlock_unlock(&asidlock); |
224 | spinlock_unlock(&asidlock); |
225 | interrupts_restore(ipl); |
225 | interrupts_restore(ipl); |
226 | } |
226 | } |
227 | 227 |