Subversion Repositories HelenOS-historic

Rev

Rev 727 | Rev 741 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
727 jermar 1
/*
2
 * Copyright (C) 2006 Jakub Jermar
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
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
15
 *   derived from this software without specific prior written permission.
16
 *
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
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
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
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
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
29
/*
30
 * ASID management.
31
 *
32
 * Modern processor architectures optimize TLB utilization
33
 * by using ASIDs (a.k.a. memory contexts on sparc64 and
34
 * region identifiers on ia64). These ASIDs help to associate
35
 * each TLB item with an address space, thus making
36
 * finer-grained TLB invalidation possible.
37
 *
38
 * Unfortunatelly, there are usually less ASIDs available than
39
 * there can be unique as_t structures (i.e. address spaces
40
 * recognized by the kernel).
41
 *
42
 * When system runs short of ASIDs, it will attempt to steal
43
 * ASID from an address space that has not been active for
44
 * a while.
45
 *
46
 * Architectures that don't have hardware support for address
47
 * spaces do not compile with this file.
48
 */
49
 
50
#include <mm/asid.h>
51
#include <mm/as.h>
52
#include <mm/tlb.h>
53
#include <arch/mm/asid.h>
54
#include <synch/spinlock.h>
55
#include <arch.h>
56
#include <list.h>
57
#include <debug.h>
58
 
59
/**
60
 * asidlock protects both the asids_allocated counter
61
 * and the list of address spaces that were already
62
 * assigned ASID.
63
 */
64
SPINLOCK_INITIALIZE(asidlock);
65
 
66
static count_t asids_allocated = 0;
67
 
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.
78
 *
79
 * This code depends on the fact that ASIDS_ALLOCABLE
80
 * is greater than number of supported CPUs.
81
 *
82
 * @return New ASID.
83
 */
84
asid_t asid_get(void)
85
{
86
    ipl_t ipl;
87
    asid_t asid;
88
    link_t *tmp;
89
    as_t *as;
90
 
91
    /*
92
     * Check if there is an unallocated ASID.
93
     */
94
 
95
    ipl = interrupts_disable();
96
    spinlock_lock(&asidlock);
97
    if (asids_allocated == ASIDS_ALLOCABLE) {
98
 
99
        /*
100
         * All ASIDs are already allocated.
101
         * Resort to stealing.
102
         */
103
 
104
        /*
105
         * Remove the first item on the list.
106
         * It is guaranteed to belong to an
107
         * inactive address space.
108
         */
109
        tmp = as_with_asid_head.next;
110
        ASSERT(tmp != &as_with_asid_head);
111
        list_remove(tmp);
112
 
113
        as = list_get_instance(tmp, as_t, as_with_asid_link);
114
        spinlock_lock(&as->lock);
115
 
116
        /*
117
         * Steal the ASID.
118
         * Note that the stolen ASID is not active.
119
         */
120
        asid = as->asid;
121
        ASSERT(asid != ASID_INVALID);
122
 
123
        /*
124
         * Get the system rid of the stolen ASID.
125
         */
126
        tlb_shootdown_start(TLB_INVL_ASID, asid, 0, 0);
127
        tlb_shootdown_finalize();
740 jermar 128
        tlb_invalidate_asid(asid);
727 jermar 129
        as->asid = ASID_INVALID;
130
 
131
        spinlock_unlock(&as->lock);
132
    } else {
133
 
134
        /*
135
         * There is at least one unallocated ASID.
136
         * Find it and assign it.
137
         */
138
 
139
        asid = asid_find_free();
140
        asids_allocated++;
141
    }
142
 
143
    spinlock_unlock(&asidlock);
144
    interrupts_restore(ipl);
145
 
146
    return asid;
147
}
148
 
149
/** Release address space identifier.
150
 *
151
 * This code relies on architecture
152
 * dependent functionality.
153
 *
154
 * @param asid ASID to be released.
155
 */
156
void asid_put(asid_t asid)
157
{
158
    ipl_t ipl;
159
 
160
    ipl = interrupts_disable();
161
    spinlock_lock(&asidlock);
162
 
163
    asids_allocated--;
164
    asid_put_arch(asid);
165
 
166
    spinlock_unlock(&asidlock);
167
    interrupts_restore(ipl);
168
}
169
 
170
/** Install ASID.
171
 *
172
 * This function is to be executed on each address space switch.
173
 *
174
 * @param as Address space.
175
 */
176
void asid_install(as_t *as)
177
{
178
    ipl_t ipl;
179
 
180
    ipl = interrupts_disable();
181
    spinlock_lock(&asidlock);
182
    spinlock_lock(&as->lock);
183
 
184
    if (as->asid != ASID_KERNEL) {
185
        if (as->asid != ASID_INVALID) {
186
            /*
187
             * This address space has valid ASID.
188
             * Remove 'as' from the list of address spaces
189
             * with assigned ASID, so that it can be later
190
             * appended to the tail of the same list.
191
             * This is to prevent stealing of ASIDs from
192
             * recently installed address spaces.
193
             */
194
            list_remove(&as->as_with_asid_link);
195
        } else {
196
            spinlock_unlock(&as->lock);
197
            spinlock_unlock(&asidlock);
198
 
199
            /*
200
             * This address space doesn't have ASID assigned.
201
             * It was stolen or the address space is being
202
             * installed for the first time.
203
             * Allocate new ASID for it.
204
             */
205
            as->asid = asid_get();
206
            spinlock_lock(&asidlock);
207
            spinlock_lock(&as->lock);
208
        }
209
 
210
        /*
211
         * Now it is sure that 'as' has ASID.
212
         * It is therefore appended to the list
213
         * of address spaces from which it can
214
         * be stolen.
215
         */
216
        list_append(&as->as_with_asid_link, &as_with_asid_head);
217
    }
218
 
219
    spinlock_unlock(&as->lock);
220
    spinlock_unlock(&asidlock);
221
    interrupts_restore(ipl);
222
}