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