Rev 1637 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1637 | Rev 1702 | ||
---|---|---|---|
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 | /** @addtogroup genarchmm |
|
- | 30 | * @{ |
|
- | 31 | */ |
|
- | 32 | ||
29 | /** |
33 | /** |
30 | * @file asid.c |
34 | * @file |
31 | * @brief ASID management. |
35 | * @brief ASID management. |
32 | * |
36 | * |
33 | * Modern processor architectures optimize TLB utilization |
37 | * Modern processor architectures optimize TLB utilization |
34 | * by using ASIDs (a.k.a. memory contexts on sparc64 and |
38 | * by using ASIDs (a.k.a. memory contexts on sparc64 and |
35 | * region identifiers on ia64). These ASIDs help to associate |
39 | * region identifiers on ia64). These ASIDs help to associate |
36 | * each TLB item with an address space, thus making |
40 | * each TLB item with an address space, thus making |
37 | * finer-grained TLB invalidation possible. |
41 | * finer-grained TLB invalidation possible. |
38 | * |
42 | * |
39 | * Unfortunatelly, there are usually less ASIDs available than |
43 | * Unfortunatelly, there are usually less ASIDs available than |
40 | * there can be unique as_t structures (i.e. address spaces |
44 | * there can be unique as_t structures (i.e. address spaces |
41 | * recognized by the kernel). |
45 | * recognized by the kernel). |
42 | * |
46 | * |
43 | * When system runs short of ASIDs, it will attempt to steal |
47 | * When system runs short of ASIDs, it will attempt to steal |
44 | * ASID from an address space that has not been active for |
48 | * ASID from an address space that has not been active for |
45 | * a while. |
49 | * a while. |
46 | * |
50 | * |
47 | * This code depends on the fact that ASIDS_ALLOCABLE |
51 | * This code depends on the fact that ASIDS_ALLOCABLE |
48 | * is greater than number of supported CPUs (i.e. the |
52 | * is greater than number of supported CPUs (i.e. the |
49 | * amount of concurently active address spaces). |
53 | * amount of concurently active address spaces). |
50 | * |
54 | * |
51 | * Architectures that don't have hardware support for address |
55 | * Architectures that don't have hardware support for address |
52 | * spaces do not compile with this file. |
56 | * spaces do not compile with this file. |
53 | */ |
57 | */ |
54 | 58 | ||
55 | #include <mm/asid.h> |
59 | #include <mm/asid.h> |
56 | #include <mm/as.h> |
60 | #include <mm/as.h> |
57 | #include <mm/tlb.h> |
61 | #include <mm/tlb.h> |
58 | #include <arch/mm/asid.h> |
62 | #include <arch/mm/asid.h> |
59 | #include <synch/spinlock.h> |
63 | #include <synch/spinlock.h> |
60 | #include <synch/mutex.h> |
64 | #include <synch/mutex.h> |
61 | #include <arch.h> |
65 | #include <arch.h> |
62 | #include <adt/list.h> |
66 | #include <adt/list.h> |
63 | #include <debug.h> |
67 | #include <debug.h> |
64 | 68 | ||
65 | /** |
69 | /** |
66 | * asidlock protects the asids_allocated counter. |
70 | * asidlock protects the asids_allocated counter. |
67 | */ |
71 | */ |
68 | SPINLOCK_INITIALIZE(asidlock); |
72 | SPINLOCK_INITIALIZE(asidlock); |
69 | 73 | ||
70 | static count_t asids_allocated = 0; |
74 | static count_t asids_allocated = 0; |
71 | 75 | ||
72 | /** Allocate free address space identifier. |
76 | /** Allocate free address space identifier. |
73 | * |
77 | * |
74 | * Interrupts must be disabled and inactive_as_with_asid_lock must be held |
78 | * Interrupts must be disabled and inactive_as_with_asid_lock must be held |
75 | * prior to this call |
79 | * prior to this call |
76 | * |
80 | * |
77 | * @return New ASID. |
81 | * @return New ASID. |
78 | */ |
82 | */ |
79 | asid_t asid_get(void) |
83 | asid_t asid_get(void) |
80 | { |
84 | { |
81 | asid_t asid; |
85 | asid_t asid; |
82 | link_t *tmp; |
86 | link_t *tmp; |
83 | as_t *as; |
87 | as_t *as; |
84 | 88 | ||
85 | /* |
89 | /* |
86 | * Check if there is an unallocated ASID. |
90 | * Check if there is an unallocated ASID. |
87 | */ |
91 | */ |
88 | 92 | ||
89 | spinlock_lock(&asidlock); |
93 | spinlock_lock(&asidlock); |
90 | if (asids_allocated == ASIDS_ALLOCABLE) { |
94 | if (asids_allocated == ASIDS_ALLOCABLE) { |
91 | 95 | ||
92 | /* |
96 | /* |
93 | * All ASIDs are already allocated. |
97 | * All ASIDs are already allocated. |
94 | * Resort to stealing. |
98 | * Resort to stealing. |
95 | */ |
99 | */ |
96 | 100 | ||
97 | /* |
101 | /* |
98 | * Remove the first item on the list. |
102 | * Remove the first item on the list. |
99 | * It is guaranteed to belong to an |
103 | * It is guaranteed to belong to an |
100 | * inactive address space. |
104 | * inactive address space. |
101 | */ |
105 | */ |
102 | ASSERT(!list_empty(&inactive_as_with_asid_head)); |
106 | ASSERT(!list_empty(&inactive_as_with_asid_head)); |
103 | tmp = inactive_as_with_asid_head.next; |
107 | tmp = inactive_as_with_asid_head.next; |
104 | list_remove(tmp); |
108 | list_remove(tmp); |
105 | 109 | ||
106 | as = list_get_instance(tmp, as_t, inactive_as_with_asid_link); |
110 | as = list_get_instance(tmp, as_t, inactive_as_with_asid_link); |
107 | mutex_lock_active(&as->lock); |
111 | mutex_lock_active(&as->lock); |
108 | 112 | ||
109 | /* |
113 | /* |
110 | * Steal the ASID. |
114 | * Steal the ASID. |
111 | * Note that the stolen ASID is not active. |
115 | * Note that the stolen ASID is not active. |
112 | */ |
116 | */ |
113 | asid = as->asid; |
117 | asid = as->asid; |
114 | ASSERT(asid != ASID_INVALID); |
118 | ASSERT(asid != ASID_INVALID); |
115 | 119 | ||
116 | /* |
120 | /* |
117 | * Notify the address space from wich the ASID |
121 | * Notify the address space from wich the ASID |
118 | * was stolen by invalidating its asid member. |
122 | * was stolen by invalidating its asid member. |
119 | */ |
123 | */ |
120 | as->asid = ASID_INVALID; |
124 | as->asid = ASID_INVALID; |
121 | mutex_unlock(&as->lock); |
125 | mutex_unlock(&as->lock); |
122 | 126 | ||
123 | /* |
127 | /* |
124 | * Get the system rid of the stolen ASID. |
128 | * Get the system rid of the stolen ASID. |
125 | */ |
129 | */ |
126 | tlb_shootdown_start(TLB_INVL_ASID, asid, 0, 0); |
130 | tlb_shootdown_start(TLB_INVL_ASID, asid, 0, 0); |
127 | tlb_invalidate_asid(asid); |
131 | tlb_invalidate_asid(asid); |
128 | tlb_shootdown_finalize(); |
132 | tlb_shootdown_finalize(); |
129 | } else { |
133 | } else { |
130 | 134 | ||
131 | /* |
135 | /* |
132 | * There is at least one unallocated ASID. |
136 | * There is at least one unallocated ASID. |
133 | * Find it and assign it. |
137 | * Find it and assign it. |
134 | */ |
138 | */ |
135 | 139 | ||
136 | asid = asid_find_free(); |
140 | asid = asid_find_free(); |
137 | asids_allocated++; |
141 | asids_allocated++; |
138 | 142 | ||
139 | /* |
143 | /* |
140 | * Purge the allocated rid from TLBs. |
144 | * Purge the allocated rid from TLBs. |
141 | */ |
145 | */ |
142 | tlb_shootdown_start(TLB_INVL_ASID, asid, 0, 0); |
146 | tlb_shootdown_start(TLB_INVL_ASID, asid, 0, 0); |
143 | tlb_invalidate_asid(asid); |
147 | tlb_invalidate_asid(asid); |
144 | tlb_shootdown_finalize(); |
148 | tlb_shootdown_finalize(); |
145 | } |
149 | } |
146 | 150 | ||
147 | spinlock_unlock(&asidlock); |
151 | spinlock_unlock(&asidlock); |
148 | 152 | ||
149 | return asid; |
153 | return asid; |
150 | } |
154 | } |
151 | 155 | ||
152 | /** Release address space identifier. |
156 | /** Release address space identifier. |
153 | * |
157 | * |
154 | * This code relies on architecture |
158 | * This code relies on architecture |
155 | * dependent functionality. |
159 | * dependent functionality. |
156 | * |
160 | * |
157 | * @param asid ASID to be released. |
161 | * @param asid ASID to be released. |
158 | */ |
162 | */ |
159 | void asid_put(asid_t asid) |
163 | void asid_put(asid_t asid) |
160 | { |
164 | { |
161 | ipl_t ipl; |
165 | ipl_t ipl; |
162 | 166 | ||
163 | ipl = interrupts_disable(); |
167 | ipl = interrupts_disable(); |
164 | spinlock_lock(&asidlock); |
168 | spinlock_lock(&asidlock); |
165 | 169 | ||
166 | asids_allocated--; |
170 | asids_allocated--; |
167 | asid_put_arch(asid); |
171 | asid_put_arch(asid); |
168 | 172 | ||
169 | spinlock_unlock(&asidlock); |
173 | spinlock_unlock(&asidlock); |
170 | interrupts_restore(ipl); |
174 | interrupts_restore(ipl); |
171 | } |
175 | } |
- | 176 | ||
- | 177 | /** @} |
|
- | 178 | */ |
|
- | 179 | ||
172 | 180 |