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