Subversion Repositories HelenOS

Rev

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

Rev 1266 Rev 1380
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 <arch.h>
61
#include <arch.h>
61
#include <adt/list.h>
62
#include <adt/list.h>
62
#include <debug.h>
63
#include <debug.h>
63
 
64
 
64
/**
65
/**
65
 * asidlock protects the asids_allocated counter.
66
 * asidlock protects the asids_allocated counter.
66
 */
67
 */
67
SPINLOCK_INITIALIZE(asidlock);
68
SPINLOCK_INITIALIZE(asidlock);
68
 
69
 
69
static count_t asids_allocated = 0;
70
static count_t asids_allocated = 0;
70
 
71
 
71
/** Allocate free address space identifier.
72
/** Allocate free address space identifier.
72
 *
73
 *
73
 * Interrupts must be disabled and as_lock must be held
74
 * Interrupts must be disabled and as_lock must be held
74
 * prior to this call
75
 * prior to this call
75
 *
76
 *
76
 * @return New ASID.
77
 * @return New ASID.
77
 */
78
 */
78
asid_t asid_get(void)
79
asid_t asid_get(void)
79
{
80
{
80
    asid_t asid;
81
    asid_t asid;
81
    link_t *tmp;
82
    link_t *tmp;
82
    as_t *as;
83
    as_t *as;
83
 
84
 
84
    /*
85
    /*
85
     * Check if there is an unallocated ASID.
86
     * Check if there is an unallocated ASID.
86
     */
87
     */
87
   
88
   
88
    spinlock_lock(&asidlock);
89
    spinlock_lock(&asidlock);
89
    if (asids_allocated == ASIDS_ALLOCABLE) {
90
    if (asids_allocated == ASIDS_ALLOCABLE) {
90
 
91
 
91
        /*
92
        /*
92
         * All ASIDs are already allocated.
93
         * All ASIDs are already allocated.
93
         * Resort to stealing.
94
         * Resort to stealing.
94
         */
95
         */
95
       
96
       
96
        /*
97
        /*
97
         * Remove the first item on the list.
98
         * Remove the first item on the list.
98
         * It is guaranteed to belong to an
99
         * It is guaranteed to belong to an
99
         * inactive address space.
100
         * inactive address space.
100
         */
101
         */
101
        ASSERT(!list_empty(&inactive_as_with_asid_head));
102
        ASSERT(!list_empty(&inactive_as_with_asid_head));
102
        tmp = inactive_as_with_asid_head.next;
103
        tmp = inactive_as_with_asid_head.next;
103
        list_remove(tmp);
104
        list_remove(tmp);
104
       
105
       
105
        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);
106
        spinlock_lock(&as->lock);
107
        mutex_lock_active(&as->lock);
107
 
108
 
108
        /*
109
        /*
109
         * Steal the ASID.
110
         * Steal the ASID.
110
         * Note that the stolen ASID is not active.
111
         * Note that the stolen ASID is not active.
111
         */
112
         */
112
        asid = as->asid;
113
        asid = as->asid;
113
        ASSERT(asid != ASID_INVALID);
114
        ASSERT(asid != ASID_INVALID);
114
 
115
 
115
        /*
116
        /*
116
         * Notify the address space from wich the ASID
117
         * Notify the address space from wich the ASID
117
         * was stolen by invalidating its asid member.
118
         * was stolen by invalidating its asid member.
118
         */
119
         */
119
        as->asid = ASID_INVALID;
120
        as->asid = ASID_INVALID;
120
        spinlock_unlock(&as->lock);
121
        mutex_unlock(&as->lock);
121
 
122
 
122
        /*
123
        /*
123
         * Get the system rid of the stolen ASID.
124
         * Get the system rid of the stolen ASID.
124
         */
125
         */
125
        tlb_shootdown_start(TLB_INVL_ASID, asid, 0, 0);
126
        tlb_shootdown_start(TLB_INVL_ASID, asid, 0, 0);
126
        tlb_shootdown_finalize();
127
        tlb_shootdown_finalize();
127
        tlb_invalidate_asid(asid);
128
        tlb_invalidate_asid(asid);
128
    } else {
129
    } else {
129
 
130
 
130
        /*
131
        /*
131
         * There is at least one unallocated ASID.
132
         * There is at least one unallocated ASID.
132
         * Find it and assign it.
133
         * Find it and assign it.
133
         */
134
         */
134
 
135
 
135
        asid = asid_find_free();
136
        asid = asid_find_free();
136
        asids_allocated++;
137
        asids_allocated++;
137
    }
138
    }
138
   
139
   
139
    spinlock_unlock(&asidlock);
140
    spinlock_unlock(&asidlock);
140
   
141
   
141
    return asid;
142
    return asid;
142
}
143
}
143
 
144
 
144
/** Release address space identifier.
145
/** Release address space identifier.
145
 *
146
 *
146
 * This code relies on architecture
147
 * This code relies on architecture
147
 * dependent functionality.
148
 * dependent functionality.
148
 *
149
 *
149
 * @param asid ASID to be released.
150
 * @param asid ASID to be released.
150
 */
151
 */
151
void asid_put(asid_t asid)
152
void asid_put(asid_t asid)
152
{
153
{
153
    ipl_t ipl;
154
    ipl_t ipl;
154
 
155
 
155
    ipl = interrupts_disable();
156
    ipl = interrupts_disable();
156
    spinlock_lock(&asidlock);
157
    spinlock_lock(&asidlock);
157
 
158
 
158
    asids_allocated--;
159
    asids_allocated--;
159
    asid_put_arch(asid);
160
    asid_put_arch(asid);
160
   
161
   
161
    spinlock_unlock(&asidlock);
162
    spinlock_unlock(&asidlock);
162
    interrupts_restore(ipl);
163
    interrupts_restore(ipl);
163
}
164
}
164
 
165