Subversion Repositories HelenOS-historic

Rev

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

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