1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
/*-
* Copyright (c) 2011 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Matt Thomas of 3am Software Foundry.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: pmap_synci.c,v 1.5 2020/04/13 08:05:22 skrll Exp $");
#define __PMAP_PRIVATE
#include "opt_multiprocessor.h"
#include <sys/param.h>
#include <sys/atomic.h>
#include <sys/cpu.h>
#include <sys/mutex.h>
#include <sys/systm.h>
#include <uvm/uvm.h>
#if defined(MULTIPROCESSOR)
u_int pmap_tlb_synci_page_mask;
u_int pmap_tlb_synci_map_mask;
void
pmap_tlb_syncicache_ast(struct cpu_info *ci)
{
struct pmap_tlb_info * const ti = cpu_tlb_info(ci);
KASSERT(kpreempt_disabled());
uint32_t page_bitmap = atomic_swap_32(&ti->ti_synci_page_bitmap, 0);
#if 0
printf("%s: need to sync %#x\n", __func__, page_bitmap);
#endif
ti->ti_evcnt_synci_asts.ev_count++;
/*
* If every bit is set in the bitmap, sync the entire icache.
*/
if (page_bitmap == pmap_tlb_synci_map_mask) {
pmap_md_icache_sync_all();
ti->ti_evcnt_synci_all.ev_count++;
ti->ti_evcnt_synci_pages.ev_count += pmap_tlb_synci_page_mask+1;
return;
}
/*
* Loop through the bitmap clearing each set of indices for each page.
*/
for (vaddr_t va = 0;
page_bitmap != 0;
page_bitmap >>= 1, va += PAGE_SIZE) {
if (page_bitmap & 1) {
/*
* Each bit set represents a page index to be synced.
*/
pmap_md_icache_sync_range_index(va, PAGE_SIZE);
ti->ti_evcnt_synci_pages.ev_count++;
}
}
}
void
pmap_tlb_syncicache(vaddr_t va, const kcpuset_t *page_onproc)
{
KASSERT(kpreempt_disabled());
/*
* We don't sync the icache here but let ast do it for us just before
* returning to userspace. We do this because we don't really know
* on which CPU we will return to userspace and if we synch the icache
* now it might not be on the CPU we need it on. In addition, others
* threads might sync the icache before we get to return to userland
* so there's no reason for us to do it.
*
* Each TLB/cache keeps a synci sequence number which gets advanced
* each time that TLB/cache performs a pmap_md_sync_icache_all. When
* we return to userland, we check the pmap's corresponding synci
* sequence number for that TLB/cache. If they match, it means that
* no one has yet synched the icache so we much do it ourselves. If
* they don't match someone has already synced the icache for us.
*
* There is a small chance that the generation numbers will wrap and
* then become equal but that's a one in 4 billion cache and will
* just cause an extra sync of the icache.
*/
struct cpu_info * const ci = curcpu();
kcpuset_t *onproc;
kcpuset_create(&onproc, true);
const uint32_t page_mask =
1L << ((va >> PGSHIFT) & pmap_tlb_synci_page_mask);
for (size_t i = 0; i < pmap_ntlbs; i++) {
struct pmap_tlb_info * const ti = pmap_tlbs[i];
TLBINFO_LOCK(ti);
for (;;) {
uint32_t old_page_bitmap = ti->ti_synci_page_bitmap;
if (old_page_bitmap & page_mask) {
ti->ti_evcnt_synci_duplicate.ev_count++;
break;
}
uint32_t orig_page_bitmap = atomic_cas_32(
&ti->ti_synci_page_bitmap, old_page_bitmap,
old_page_bitmap | page_mask);
if (orig_page_bitmap == old_page_bitmap) {
if (old_page_bitmap == 0) {
kcpuset_merge(onproc, ti->ti_kcpuset);
} else {
ti->ti_evcnt_synci_deferred.ev_count++;
}
ti->ti_evcnt_synci_desired.ev_count++;
break;
}
}
#if 0
printf("%s: %s: %x to %x on cpus %#x\n", __func__,
ti->ti_name, page_mask, ti->ti_synci_page_bitmap,
onproc & page_onproc & ti->ti_cpu_mask);
#endif
TLBINFO_UNLOCK(ti);
}
kcpuset_intersect(onproc, page_onproc);
if (__predict_false(!kcpuset_iszero(onproc))) {
/*
* If the cpu need to sync this page, tell the current lwp
* to sync the icache before it returns to userspace.
*/
if (kcpuset_isset(onproc, cpu_index(ci))) {
if (ci->ci_flags & CPUF_USERPMAP) {
curlwp->l_md.md_astpending = 1; /* force call to ast() */
ci->ci_evcnt_synci_onproc_rqst.ev_count++;
} else {
ci->ci_evcnt_synci_deferred_rqst.ev_count++;
}
kcpuset_clear(onproc, cpu_index(ci));
}
/*
* For each cpu that is affect, send an IPI telling
* that CPU that the current thread needs to sync its icache.
* We might cause some spurious icache syncs but that's not
* going to break anything.
*/
for (cpuid_t n = kcpuset_ffs(onproc);
n-- > 0;
n = kcpuset_ffs(onproc)) {
kcpuset_clear(onproc, n);
cpu_send_ipi(cpu_lookup(n), IPI_SYNCICACHE);
}
}
kcpuset_destroy(onproc);
}
void
pmap_tlb_syncicache_wanted(struct cpu_info *ci)
{
struct pmap_tlb_info * const ti = cpu_tlb_info(ci);
KASSERT(cpu_intr_p());
TLBINFO_LOCK(ti);
/*
* We might have been notified because another CPU changed an exec
* page and now needs us to sync the icache so tell the current lwp
* to do the next time it returns to userland (which should be very
* soon).
*/
if (ti->ti_synci_page_bitmap && (ci->ci_flags & CPUF_USERPMAP)) {
curlwp->l_md.md_astpending = 1; /* force call to ast() */
ci->ci_evcnt_synci_ipi_rqst.ev_count++;
}
TLBINFO_UNLOCK(ti);
}
#endif /* MULTIPROCESSOR */
|