Subversion Repositories HelenOS

Rev

Rev 740 | Rev 753 | 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);
741 jermar 97
	if (ASID_STEALING_ENABLED && asids_allocated == ASIDS_ALLOCABLE) {
727 jermar 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
		/*
741 jermar 124
		 * Notify the address space from wich the ASID
125
		 * was stolen by invalidating its asid member.
126
		 */
127
		as->asid = ASID_INVALID;
128
		spinlock_unlock(&as->lock);
129
 
130
		/*
727 jermar 131
		 * Get the system rid of the stolen ASID.
132
		 */
133
		tlb_shootdown_start(TLB_INVL_ASID, asid, 0, 0);
134
		tlb_shootdown_finalize();
740 jermar 135
		tlb_invalidate_asid(asid);
727 jermar 136
	} else {
137
 
138
		/*
139
		 * There is at least one unallocated ASID.
140
		 * Find it and assign it.
141
		 */
142
 
143
		asid = asid_find_free();
144
		asids_allocated++;
145
	}
146
 
147
	spinlock_unlock(&asidlock);
148
	interrupts_restore(ipl);
149
 
150
	return asid;
151
}
152
 
153
/** Release address space identifier.
154
 *
155
 * This code relies on architecture
156
 * dependent functionality.
157
 *
158
 * @param asid ASID to be released.
159
 */
160
void asid_put(asid_t asid)
161
{
162
	ipl_t ipl;
163
 
164
	ipl = interrupts_disable();
165
	spinlock_lock(&asidlock);
166
 
167
	asids_allocated--;
168
	asid_put_arch(asid);
169
 
170
	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);
226
}