Subversion Repositories HelenOS

Rev

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

Rev 3588 Rev 3674
1
/*
1
/*
2
 * Copyright (c) 2001-2004 Jakub Jermar
2
 * Copyright (c) 2001-2004 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
/** @addtogroup genericmm
29
/** @addtogroup genericmm
30
 * @{
30
 * @{
31
 */
31
 */
32
 
32
 
33
/**
33
/**
34
 * @file
34
 * @file
35
 * @brief   Generic TLB shootdown algorithm.
35
 * @brief   Generic TLB shootdown algorithm.
36
 *
36
 *
37
 * The algorithm implemented here is based on the CMU TLB shootdown
37
 * The algorithm implemented here is based on the CMU TLB shootdown
38
 * algorithm and is further simplified (e.g. all CPUs receive all TLB
38
 * algorithm and is further simplified (e.g. all CPUs receive all TLB
39
 * shootdown messages).
39
 * shootdown messages).
40
 */
40
 */
41
 
41
 
42
#include <mm/tlb.h>
42
#include <mm/tlb.h>
43
#include <mm/asid.h>
43
#include <mm/asid.h>
44
#include <arch/mm/tlb.h>
44
#include <arch/mm/tlb.h>
45
#include <smp/ipi.h>
45
#include <smp/ipi.h>
46
#include <synch/spinlock.h>
46
#include <synch/spinlock.h>
47
#include <atomic.h>
47
#include <atomic.h>
48
#include <arch/interrupt.h>
48
#include <arch/interrupt.h>
49
#include <config.h>
49
#include <config.h>
50
#include <arch.h>
50
#include <arch.h>
51
#include <panic.h>
51
#include <panic.h>
52
#include <debug.h>
52
#include <debug.h>
53
#include <cpu.h>
53
#include <cpu.h>
54
 
54
 
55
/**
55
/**
56
 * This lock is used for synchronisation between sender and
56
 * This lock is used for synchronisation between sender and
57
 * recipients of TLB shootdown message. It must be acquired
57
 * recipients of TLB shootdown message. It must be acquired
58
 * before CPU structure lock.
58
 * before CPU structure lock.
59
 */
59
 */
60
SPINLOCK_INITIALIZE(tlblock);
60
SPINLOCK_INITIALIZE(tlblock);
61
 
61
 
62
void tlb_init(void)
62
void tlb_init(void)
63
{
63
{
64
    tlb_arch_init();
64
    tlb_arch_init();
65
}
65
}
66
 
66
 
67
#ifdef CONFIG_SMP
67
#ifdef CONFIG_SMP
68
 
68
 
69
/** Send TLB shootdown message.
69
/** Send TLB shootdown message.
70
 *
70
 *
71
 * This function attempts to deliver TLB shootdown message
71
 * This function attempts to deliver TLB shootdown message
72
 * to all other processors.
72
 * to all other processors.
73
 *
73
 *
74
 * This function must be called with interrupts disabled.
74
 * This function must be called with interrupts disabled.
75
 *
75
 *
76
 * @param type Type describing scope of shootdown.
76
 * @param type Type describing scope of shootdown.
77
 * @param asid Address space, if required by type.
77
 * @param asid Address space, if required by type.
78
 * @param page Virtual page address, if required by type.
78
 * @param page Virtual page address, if required by type.
79
 * @param count Number of pages, if required by type.
79
 * @param count Number of pages, if required by type.
80
 */
80
 */
81
void tlb_shootdown_start(tlb_invalidate_type_t type, asid_t asid,
81
void tlb_shootdown_start(tlb_invalidate_type_t type, asid_t asid,
82
    uintptr_t page, count_t count)
82
    uintptr_t page, count_t count)
83
{
83
{
84
    unsigned int i;
84
    unsigned int i;
85
 
85
 
86
    CPU->tlb_active = 0;
86
    CPU->tlb_active = 0;
87
    spinlock_lock(&tlblock);
87
    spinlock_lock(&tlblock);
88
   
88
   
89
    for (i = 0; i < config.cpu_count; i++) {
89
    for (i = 0; i < config.cpu_count; i++) {
90
        cpu_t *cpu;
90
        cpu_t *cpu;
91
       
91
       
92
        if (i == CPU->id)
92
        if (i == CPU->id)
93
            continue;
93
            continue;
94
 
94
 
95
        cpu = &cpus[i];
95
        cpu = &cpus[i];
96
        spinlock_lock(&cpu->lock);
96
        spinlock_lock(&cpu->lock);
97
        if (cpu->tlb_messages_count == TLB_MESSAGE_QUEUE_LEN) {
97
        if (cpu->tlb_messages_count == TLB_MESSAGE_QUEUE_LEN) {
98
            /*
98
            /*
99
             * The message queue is full.
99
             * The message queue is full.
100
             * Erase the queue and store one TLB_INVL_ALL message.
100
             * Erase the queue and store one TLB_INVL_ALL message.
101
             */
101
             */
102
            cpu->tlb_messages_count = 1;
102
            cpu->tlb_messages_count = 1;
103
            cpu->tlb_messages[0].type = TLB_INVL_ALL;
103
            cpu->tlb_messages[0].type = TLB_INVL_ALL;
104
            cpu->tlb_messages[0].asid = ASID_INVALID;
104
            cpu->tlb_messages[0].asid = ASID_INVALID;
105
            cpu->tlb_messages[0].page = 0;
105
            cpu->tlb_messages[0].page = 0;
106
            cpu->tlb_messages[0].count = 0;
106
            cpu->tlb_messages[0].count = 0;
107
        } else {
107
        } else {
108
            /*
108
            /*
109
             * Enqueue the message.
109
             * Enqueue the message.
110
             */
110
             */
111
            index_t idx = cpu->tlb_messages_count++;
111
            index_t idx = cpu->tlb_messages_count++;
112
            cpu->tlb_messages[idx].type = type;
112
            cpu->tlb_messages[idx].type = type;
113
            cpu->tlb_messages[idx].asid = asid;
113
            cpu->tlb_messages[idx].asid = asid;
114
            cpu->tlb_messages[idx].page = page;
114
            cpu->tlb_messages[idx].page = page;
115
            cpu->tlb_messages[idx].count = count;
115
            cpu->tlb_messages[idx].count = count;
116
        }
116
        }
117
        spinlock_unlock(&cpu->lock);
117
        spinlock_unlock(&cpu->lock);
118
    }
118
    }
119
   
119
   
120
    tlb_shootdown_ipi_send();
120
    tlb_shootdown_ipi_send();
121
 
121
 
122
busy_wait: 
122
busy_wait: 
123
    for (i = 0; i < config.cpu_count; i++)
123
    for (i = 0; i < config.cpu_count; i++)
124
        if (cpus[i].tlb_active)
124
        if (cpus[i].tlb_active)
125
            goto busy_wait;
125
            goto busy_wait;
126
}
126
}
127
 
127
 
128
/** Finish TLB shootdown sequence. */
128
/** Finish TLB shootdown sequence. */
129
void tlb_shootdown_finalize(void)
129
void tlb_shootdown_finalize(void)
130
{
130
{
131
    spinlock_unlock(&tlblock);
131
    spinlock_unlock(&tlblock);
132
    CPU->tlb_active = 1;
132
    CPU->tlb_active = 1;
133
}
133
}
134
 
134
 
135
void tlb_shootdown_ipi_send(void)
135
void tlb_shootdown_ipi_send(void)
136
{
136
{
137
#ifndef ia64
-
 
138
    ipi_broadcast(VECTOR_TLB_SHOOTDOWN_IPI);
137
    ipi_broadcast(VECTOR_TLB_SHOOTDOWN_IPI);
139
#endif  
-
 
140
}
138
}
141
 
139
 
142
/** Receive TLB shootdown message. */
140
/** Receive TLB shootdown message. */
143
void tlb_shootdown_ipi_recv(void)
141
void tlb_shootdown_ipi_recv(void)
144
{
142
{
145
    tlb_invalidate_type_t type;
143
    tlb_invalidate_type_t type;
146
    asid_t asid;
144
    asid_t asid;
147
    uintptr_t page;
145
    uintptr_t page;
148
    count_t count;
146
    count_t count;
149
    unsigned int i;
147
    unsigned int i;
150
   
148
   
151
    ASSERT(CPU);
149
    ASSERT(CPU);
152
   
150
   
153
    CPU->tlb_active = 0;
151
    CPU->tlb_active = 0;
154
    spinlock_lock(&tlblock);
152
    spinlock_lock(&tlblock);
155
    spinlock_unlock(&tlblock);
153
    spinlock_unlock(&tlblock);
156
   
154
   
157
    spinlock_lock(&CPU->lock);
155
    spinlock_lock(&CPU->lock);
158
    ASSERT(CPU->tlb_messages_count <= TLB_MESSAGE_QUEUE_LEN);
156
    ASSERT(CPU->tlb_messages_count <= TLB_MESSAGE_QUEUE_LEN);
159
 
157
 
160
    for (i = 0; i < CPU->tlb_messages_count; CPU->tlb_messages_count--) {
158
    for (i = 0; i < CPU->tlb_messages_count; CPU->tlb_messages_count--) {
161
        type = CPU->tlb_messages[i].type;
159
        type = CPU->tlb_messages[i].type;
162
        asid = CPU->tlb_messages[i].asid;
160
        asid = CPU->tlb_messages[i].asid;
163
        page = CPU->tlb_messages[i].page;
161
        page = CPU->tlb_messages[i].page;
164
        count = CPU->tlb_messages[i].count;
162
        count = CPU->tlb_messages[i].count;
165
 
163
 
166
        switch (type) {
164
        switch (type) {
167
        case TLB_INVL_ALL:
165
        case TLB_INVL_ALL:
168
            tlb_invalidate_all();
166
            tlb_invalidate_all();
169
            break;
167
            break;
170
        case TLB_INVL_ASID:
168
        case TLB_INVL_ASID:
171
            tlb_invalidate_asid(asid);
169
            tlb_invalidate_asid(asid);
172
            break;
170
            break;
173
        case TLB_INVL_PAGES:
171
        case TLB_INVL_PAGES:
174
                ASSERT(count);
172
                ASSERT(count);
175
            tlb_invalidate_pages(asid, page, count);
173
            tlb_invalidate_pages(asid, page, count);
176
            break;
174
            break;
177
        default:
175
        default:
178
            panic("unknown type (%d)\n", type);
176
            panic("unknown type (%d)\n", type);
179
            break;
177
            break;
180
        }
178
        }
181
        if (type == TLB_INVL_ALL)
179
        if (type == TLB_INVL_ALL)
182
            break;
180
            break;
183
    }
181
    }
184
   
182
   
185
    spinlock_unlock(&CPU->lock);
183
    spinlock_unlock(&CPU->lock);
186
    CPU->tlb_active = 1;
184
    CPU->tlb_active = 1;
187
}
185
}
188
 
186
 
189
#endif /* CONFIG_SMP */
187
#endif /* CONFIG_SMP */
190
 
188
 
191
/** @}
189
/** @}
192
 */
190
 */
193
 
191