Subversion Repositories HelenOS-historic

Rev

Rev 788 | Rev 1380 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 788 Rev 823
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
 * 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>
51
#include <mm/as.h>
55
#include <mm/as.h>
52
#include <mm/tlb.h>
56
#include <mm/tlb.h>
53
#include <arch/mm/asid.h>
57
#include <arch/mm/asid.h>
54
#include <synch/spinlock.h>
58
#include <synch/spinlock.h>
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.
101
         * Resort to stealing.
92
         * Resort to stealing.
102
         */
93
         */
103
       
94
       
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.
119
         */
110
         */
120
        asid = as->asid;
111
        asid = as->asid;
121
        ASSERT(asid != ASID_INVALID);
112
        ASSERT(asid != ASID_INVALID);
122
 
113
 
123
        /*
114
        /*
124
         * Notify the address space from wich the ASID
115
         * Notify the address space from wich the ASID
125
         * was stolen by invalidating its asid member.
116
         * was stolen by invalidating its asid member.
126
         */
117
         */
127
        as->asid = ASID_INVALID;
118
        as->asid = ASID_INVALID;
128
        spinlock_unlock(&as->lock);
119
        spinlock_unlock(&as->lock);
129
 
120
 
130
        /*
121
        /*
131
         * Get the system rid of the stolen ASID.
122
         * Get the system rid of the stolen ASID.
132
         */
123
         */
133
        tlb_shootdown_start(TLB_INVL_ASID, asid, 0, 0);
124
        tlb_shootdown_start(TLB_INVL_ASID, asid, 0, 0);
134
        tlb_shootdown_finalize();
125
        tlb_shootdown_finalize();
135
        tlb_invalidate_asid(asid);
126
        tlb_invalidate_asid(asid);
136
    } else {
127
    } else {
137
 
128
 
138
        /*
129
        /*
139
         * There is at least one unallocated ASID.
130
         * There is at least one unallocated ASID.
140
         * Find it and assign it.
131
         * Find it and assign it.
141
         */
132
         */
142
 
133
 
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.
154
 *
144
 *
155
 * This code relies on architecture
145
 * This code relies on architecture
156
 * dependent functionality.
146
 * dependent functionality.
157
 *
147
 *
158
 * @param asid ASID to be released.
148
 * @param asid ASID to be released.
159
 */
149
 */
160
void asid_put(asid_t asid)
150
void asid_put(asid_t asid)
161
{
151
{
162
    ipl_t ipl;
152
    ipl_t ipl;
163
 
153
 
164
    ipl = interrupts_disable();
154
    ipl = interrupts_disable();
165
    spinlock_lock(&asidlock);
155
    spinlock_lock(&asidlock);
166
 
156
 
167
    asids_allocated--;
157
    asids_allocated--;
168
    asid_put_arch(asid);
158
    asid_put_arch(asid);
169
   
159
   
170
    spinlock_unlock(&asidlock);
160
    spinlock_unlock(&asidlock);
171
    interrupts_restore(ipl);
-
 
172
}
-
 
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);
161
    interrupts_restore(ipl);
226
}
162
}
227
 
163