Subversion Repositories HelenOS-historic

Rev

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

Rev 727 Rev 740
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 <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
         * Get the system rid of the stolen ASID.
124
         * Get the system rid of the stolen ASID.
125
         */
125
         */
126
        tlb_shootdown_start(TLB_INVL_ASID, asid, 0, 0);
126
        tlb_shootdown_start(TLB_INVL_ASID, asid, 0, 0);
127
        tlb_shootdown_finalize();
127
        tlb_shootdown_finalize();
-
 
128
        tlb_invalidate_asid(asid);
128
        as->asid = ASID_INVALID;
129
        as->asid = ASID_INVALID;
129
       
130
       
130
        spinlock_unlock(&as->lock);
131
        spinlock_unlock(&as->lock);
131
    } else {
132
    } else {
132
 
133
 
133
        /*
134
        /*
134
         * There is at least one unallocated ASID.
135
         * There is at least one unallocated ASID.
135
         * Find it and assign it.
136
         * Find it and assign it.
136
         */
137
         */
137
 
138
 
138
        asid = asid_find_free();
139
        asid = asid_find_free();
139
        asids_allocated++;
140
        asids_allocated++;
140
    }
141
    }
141
   
142
   
142
    spinlock_unlock(&asidlock);
143
    spinlock_unlock(&asidlock);
143
    interrupts_restore(ipl);
144
    interrupts_restore(ipl);
144
   
145
   
145
    return asid;
146
    return asid;
146
}
147
}
147
 
148
 
148
/** Release address space identifier.
149
/** Release address space identifier.
149
 *
150
 *
150
 * This code relies on architecture
151
 * This code relies on architecture
151
 * dependent functionality.
152
 * dependent functionality.
152
 *
153
 *
153
 * @param asid ASID to be released.
154
 * @param asid ASID to be released.
154
 */
155
 */
155
void asid_put(asid_t asid)
156
void asid_put(asid_t asid)
156
{
157
{
157
    ipl_t ipl;
158
    ipl_t ipl;
158
 
159
 
159
    ipl = interrupts_disable();
160
    ipl = interrupts_disable();
160
    spinlock_lock(&asidlock);
161
    spinlock_lock(&asidlock);
161
 
162
 
162
    asids_allocated--;
163
    asids_allocated--;
163
    asid_put_arch(asid);
164
    asid_put_arch(asid);
164
   
165
   
165
    spinlock_unlock(&asidlock);
166
    spinlock_unlock(&asidlock);
166
    interrupts_restore(ipl);
167
    interrupts_restore(ipl);
167
}
168
}
168
 
169
 
169
/** Install ASID.
170
/** Install ASID.
170
 *
171
 *
171
 * This function is to be executed on each address space switch.
172
 * This function is to be executed on each address space switch.
172
 *
173
 *
173
 * @param as Address space.
174
 * @param as Address space.
174
 */
175
 */
175
void asid_install(as_t *as)
176
void asid_install(as_t *as)
176
{
177
{
177
    ipl_t ipl;
178
    ipl_t ipl;
178
   
179
   
179
    ipl = interrupts_disable();
180
    ipl = interrupts_disable();
180
    spinlock_lock(&asidlock);
181
    spinlock_lock(&asidlock);
181
    spinlock_lock(&as->lock);
182
    spinlock_lock(&as->lock);
182
   
183
   
183
    if (as->asid != ASID_KERNEL) {
184
    if (as->asid != ASID_KERNEL) {
184
        if (as->asid != ASID_INVALID) {
185
        if (as->asid != ASID_INVALID) {
185
            /*
186
            /*
186
             * This address space has valid ASID.
187
             * This address space has valid ASID.
187
             * Remove 'as' from the list of address spaces
188
             * Remove 'as' from the list of address spaces
188
             * with assigned ASID, so that it can be later
189
             * with assigned ASID, so that it can be later
189
             * appended to the tail of the same list.
190
             * appended to the tail of the same list.
190
             * This is to prevent stealing of ASIDs from
191
             * This is to prevent stealing of ASIDs from
191
             * recently installed address spaces.
192
             * recently installed address spaces.
192
             */
193
             */
193
            list_remove(&as->as_with_asid_link);
194
            list_remove(&as->as_with_asid_link);
194
        } else {
195
        } else {
195
            spinlock_unlock(&as->lock);
196
            spinlock_unlock(&as->lock);
196
            spinlock_unlock(&asidlock);
197
            spinlock_unlock(&asidlock);
197
   
198
   
198
            /*
199
            /*
199
             * This address space doesn't have ASID assigned.
200
             * This address space doesn't have ASID assigned.
200
             * It was stolen or the address space is being
201
             * It was stolen or the address space is being
201
             * installed for the first time.
202
             * installed for the first time.
202
             * Allocate new ASID for it.
203
             * Allocate new ASID for it.
203
             */
204
             */
204
            as->asid = asid_get();
205
            as->asid = asid_get();
205
            spinlock_lock(&asidlock);
206
            spinlock_lock(&asidlock);
206
            spinlock_lock(&as->lock);
207
            spinlock_lock(&as->lock);
207
        }
208
        }
208
   
209
   
209
        /*
210
        /*
210
         * Now it is sure that 'as' has ASID.
211
         * Now it is sure that 'as' has ASID.
211
         * It is therefore appended to the list
212
         * It is therefore appended to the list
212
         * of address spaces from which it can
213
         * of address spaces from which it can
213
         * be stolen.
214
         * be stolen.
214
         */
215
         */
215
        list_append(&as->as_with_asid_link, &as_with_asid_head);
216
        list_append(&as->as_with_asid_link, &as_with_asid_head);
216
    }
217
    }
217
   
218
   
218
    spinlock_unlock(&as->lock);
219
    spinlock_unlock(&as->lock);
219
    spinlock_unlock(&asidlock);
220
    spinlock_unlock(&asidlock);
220
    interrupts_restore(ipl);
221
    interrupts_restore(ipl);
221
}
222
}
222
 
223