Subversion Repositories HelenOS-historic

Rev

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