Subversion Repositories HelenOS-historic

Rev

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

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