Subversion Repositories HelenOS-historic

Rev

Rev 788 | Rev 1266 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 788 Rev 823
Line 41... Line 41...
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
 * This code depends on the fact that ASIDS_ALLOCABLE
-
 
47
 * is greater than number of supported CPUs (i.e. the
-
 
48
 * amount of concurently active address spaces).
-
 
49
 *
46
 * Architectures that don't have hardware support for address
50
 * Architectures that don't have hardware support for address
47
 * spaces do not compile with this file.
51
 * spaces do not compile with this file.
48
 */
52
 */
49
 
53
 
50
#include <mm/asid.h>
54
#include <mm/asid.h>
Line 55... Line 59...
55
#include <arch.h>
59
#include <arch.h>
56
#include <adt/list.h>
60
#include <adt/list.h>
57
#include <debug.h>
61
#include <debug.h>
58
 
62
 
59
/**
63
/**
60
 * asidlock protects both the asids_allocated counter
64
 * asidlock protects the asids_allocated counter.
61
 * and the list of address spaces that were already
-
 
62
 * assigned ASID.
-
 
63
 */
65
 */
64
SPINLOCK_INITIALIZE(asidlock);
66
SPINLOCK_INITIALIZE(asidlock);
65
 
67
 
66
static count_t asids_allocated = 0;
68
static count_t asids_allocated = 0;
67
 
69
 
68
/**
-
 
69
 * List of address spaces with assigned ASID.
-
 
70
 * When the system runs short of allocable
-
 
71
 * ASIDS, inactive address spaces are guaranteed
-
 
72
 * to be at the beginning of the list.
-
 
73
 */
-
 
74
LIST_INITIALIZE(as_with_asid_head);
-
 
75
 
-
 
76
 
-
 
77
/** Allocate free address space identifier.
70
/** Allocate free address space identifier.
78
 *
71
 *
79
 * This code depends on the fact that ASIDS_ALLOCABLE
72
 * Interrupts must be disabled and as_lock must be held
80
 * is greater than number of supported CPUs.
73
 * prior to this call
81
 *
74
 *
82
 * @return New ASID.
75
 * @return New ASID.
83
 */
76
 */
84
asid_t asid_get(void)
77
asid_t asid_get(void)
85
{
78
{
86
    ipl_t ipl;
-
 
87
    asid_t asid;
79
    asid_t asid;
88
    link_t *tmp;
80
    link_t *tmp;
89
    as_t *as;
81
    as_t *as;
90
 
82
 
91
    /*
83
    /*
92
     * Check if there is an unallocated ASID.
84
     * Check if there is an unallocated ASID.
93
     */
85
     */
94
   
86
   
95
    ipl = interrupts_disable();
-
 
96
    spinlock_lock(&asidlock);
87
    spinlock_lock(&asidlock);
97
    if (asids_allocated == ASIDS_ALLOCABLE) {
88
    if (asids_allocated == ASIDS_ALLOCABLE) {
98
 
89
 
99
        /*
90
        /*
100
         * All ASIDs are already allocated.
91
         * All ASIDs are already allocated.
Line 104... Line 95...
104
        /*
95
        /*
105
         * Remove the first item on the list.
96
         * Remove the first item on the list.
106
         * It is guaranteed to belong to an
97
         * It is guaranteed to belong to an
107
         * inactive address space.
98
         * inactive address space.
108
         */
99
         */
109
        tmp = as_with_asid_head.next;
100
        ASSERT(!list_empty(&inactive_as_with_asid_head));
110
        ASSERT(tmp != &as_with_asid_head);
101
        tmp = inactive_as_with_asid_head.next;
111
        list_remove(tmp);
102
        list_remove(tmp);
112
       
103
       
113
        as = list_get_instance(tmp, as_t, as_with_asid_link);
104
        as = list_get_instance(tmp, as_t, inactive_as_with_asid_link);
114
        spinlock_lock(&as->lock);
105
        spinlock_lock(&as->lock);
115
 
106
 
116
        /*
107
        /*
117
         * Steal the ASID.
108
         * Steal the ASID.
118
         * Note that the stolen ASID is not active.
109
         * Note that the stolen ASID is not active.
Line 143... Line 134...
143
        asid = asid_find_free();
134
        asid = asid_find_free();
144
        asids_allocated++;
135
        asids_allocated++;
145
    }
136
    }
146
   
137
   
147
    spinlock_unlock(&asidlock);
138
    spinlock_unlock(&asidlock);
148
    interrupts_restore(ipl);
-
 
149
   
139
   
150
    return asid;
140
    return asid;
151
}
141
}
152
 
142
 
153
/** Release address space identifier.
143
/** Release address space identifier.
Line 168... Line 158...
168
    asid_put_arch(asid);
158
    asid_put_arch(asid);
169
   
159
   
170
    spinlock_unlock(&asidlock);
160
    spinlock_unlock(&asidlock);
171
    interrupts_restore(ipl);
161
    interrupts_restore(ipl);
172
}
162
}
173
 
-
 
174
/** Install ASID.
-
 
175
 *
-
 
176
 * This function is to be executed on each address space switch.
-
 
177
 *
-
 
178
 * @param as Address space.
-
 
179
 */
-
 
180
void asid_install(as_t *as)
-
 
181
{
-
 
182
    ipl_t ipl;
-
 
183
   
-
 
184
    ipl = interrupts_disable();
-
 
185
    spinlock_lock(&asidlock);
-
 
186
    spinlock_lock(&as->lock);
-
 
187
   
-
 
188
    if (as->asid != ASID_KERNEL) {
-
 
189
        if (as->asid != ASID_INVALID) {
-
 
190
            /*
-
 
191
             * This address space has valid ASID.
-
 
192
             * Remove 'as' from the list of address spaces
-
 
193
             * with assigned ASID, so that it can be later
-
 
194
             * appended to the tail of the same list.
-
 
195
             * This is to prevent stealing of ASIDs from
-
 
196
             * recently installed address spaces.
-
 
197
             */
-
 
198
            list_remove(&as->as_with_asid_link);
-
 
199
        } else {
-
 
200
            spinlock_unlock(&as->lock);
-
 
201
            spinlock_unlock(&asidlock);
-
 
202
   
-
 
203
            /*
-
 
204
             * This address space doesn't have ASID assigned.
-
 
205
             * It was stolen or the address space is being
-
 
206
             * installed for the first time.
-
 
207
             * Allocate new ASID for it.
-
 
208
             */
-
 
209
            as->asid = asid_get();
-
 
210
            spinlock_lock(&asidlock);
-
 
211
            spinlock_lock(&as->lock);
-
 
212
        }
-
 
213
   
-
 
214
        /*
-
 
215
         * Now it is sure that 'as' has ASID.
-
 
216
         * It is therefore appended to the list
-
 
217
         * of address spaces from which it can
-
 
218
         * be stolen.
-
 
219
         */
-
 
220
        list_append(&as->as_with_asid_link, &as_with_asid_head);
-
 
221
    }
-
 
222
   
-
 
223
    spinlock_unlock(&as->lock);
-
 
224
    spinlock_unlock(&asidlock);
-
 
225
    interrupts_restore(ipl);
-
 
226
}
-