pmap_synci.c revision 1.1 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.1 christos __KERNEL_RCSID(0, "$NetBSD: pmap_synci.c,v 1.1 2012/10/03 00:51:46 christos 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.1 christos void
48 1.1 christos pmap_syncicache_ast(struct cpu_info *ci)
49 1.1 christos {
50 1.1 christos struct pmap_tlb_info * const ti = ci->ci_tlb_info;
51 1.1 christos
52 1.1 christos KASSERT(kpreempt_disabled());
53 1.1 christos
54 1.1 christos uint32_t page_bitmap = atomic_swap_32(&ti->ti_synci_page_bitmap, 0);
55 1.1 christos #if 0
56 1.1 christos printf("%s: need to sync %#x\n", __func__, page_bitmap);
57 1.1 christos #endif
58 1.1 christos ti->ti_evcnt_synci_asts.ev_count++;
59 1.1 christos /*
60 1.1 christos * If every bit is set in the bitmap, sync the entire icache.
61 1.1 christos */
62 1.1 christos if (page_bitmap == pmap_tlb_synci_map_mask) {
63 1.1 christos pmap_md_icache_sync_all();
64 1.1 christos ti->ti_evcnt_synci_all.ev_count++;
65 1.1 christos ti->ti_evcnt_synci_pages.ev_count += pmap_tlb_synci_page_mask+1;
66 1.1 christos kpreempt_enable();
67 1.1 christos return;
68 1.1 christos }
69 1.1 christos
70 1.1 christos /*
71 1.1 christos * Loop through the bitmap clearing each set of indices for each page.
72 1.1 christos */
73 1.1 christos for (vaddr_t va = 0;
74 1.1 christos page_bitmap != 0;
75 1.1 christos page_bitmap >>= 1, va += PAGE_SIZE) {
76 1.1 christos if (page_bitmap & 1) {
77 1.1 christos /*
78 1.1 christos * Each bit set represents a page index to be synced.
79 1.1 christos */
80 1.1 christos pmap_md_icache_sync_range_index(va, PAGE_SIZE);
81 1.1 christos ti->ti_evcnt_synci_pages.ev_count++;
82 1.1 christos }
83 1.1 christos }
84 1.1 christos
85 1.1 christos kpreempt_enable();
86 1.1 christos }
87 1.1 christos
88 1.1 christos void
89 1.1 christos pmap_tlb_syncicache(vaddr_t va, uint32_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.1 christos const uint32_t cpu_mask = 1L << cpu_index(curcpu());
112 1.1 christos const uint32_t page_mask =
113 1.1 christos 1L << ((va >> PGSHIFT) & pmap_tlb_synci_page_mask);
114 1.1 christos uint32_t onproc = 0;
115 1.1 christos for (size_t i = 0; i < pmap_ntlbs; i++) {
116 1.1 christos struct pmap_tlb_info * const ti = pmap_tlbs[i];
117 1.1 christos TLBINFO_LOCK(ti);
118 1.1 christos for (;;) {
119 1.1 christos uint32_t old_page_bitmap = ti->ti_synci_page_bitmap;
120 1.1 christos if (old_page_bitmap & page_mask) {
121 1.1 christos ti->ti_evcnt_synci_duplicate.ev_count++;
122 1.1 christos break;
123 1.1 christos }
124 1.1 christos
125 1.1 christos uint32_t orig_page_bitmap = atomic_cas_32(
126 1.1 christos &ti->ti_synci_page_bitmap, old_page_bitmap,
127 1.1 christos old_page_bitmap | page_mask);
128 1.1 christos
129 1.1 christos if (orig_page_bitmap == old_page_bitmap) {
130 1.1 christos if (old_page_bitmap == 0) {
131 1.1 christos onproc |= ti->ti_cpu_mask;
132 1.1 christos } else {
133 1.1 christos ti->ti_evcnt_synci_deferred.ev_count++;
134 1.1 christos }
135 1.1 christos ti->ti_evcnt_synci_desired.ev_count++;
136 1.1 christos break;
137 1.1 christos }
138 1.1 christos }
139 1.1 christos #if 0
140 1.1 christos printf("%s: %s: %x to %x on cpus %#x\n", __func__,
141 1.1 christos ti->ti_name, page_mask, ti->ti_synci_page_bitmap,
142 1.1 christos onproc & page_onproc & ti->ti_cpu_mask);
143 1.1 christos #endif
144 1.1 christos TLBINFO_UNLOCK(ti);
145 1.1 christos }
146 1.1 christos onproc &= page_onproc;
147 1.1 christos if (__predict_false(onproc != 0)) {
148 1.1 christos /*
149 1.1 christos * If the cpu need to sync this page, tell the current lwp
150 1.1 christos * to sync the icache before it returns to userspace.
151 1.1 christos */
152 1.1 christos if (onproc & cpu_mask) {
153 1.1 christos if (curcpu()->ci_flags & CPUF_USERPMAP) {
154 1.1 christos curlwp->l_md.md_astpending = 1; /* force call to ast() */
155 1.1 christos curcpu()->ci_evcnt_synci_onproc_rqst.ev_count++;
156 1.1 christos } else {
157 1.1 christos curcpu()->ci_evcnt_synci_deferred_rqst.ev_count++;
158 1.1 christos }
159 1.1 christos onproc ^= cpu_mask;
160 1.1 christos }
161 1.1 christos
162 1.1 christos /*
163 1.1 christos * For each cpu that is affect, send an IPI telling
164 1.1 christos * that CPU that the current thread needs to sync its icache.
165 1.1 christos * We might cause some spurious icache syncs but that's not
166 1.1 christos * going to break anything.
167 1.1 christos */
168 1.1 christos for (u_int n = ffs(onproc);
169 1.1 christos onproc != 0;
170 1.1 christos onproc >>= n, onproc <<= n, n = ffs(onproc)) {
171 1.1 christos cpu_send_ipi(cpu_lookup(n-1), IPI_SYNCICACHE);
172 1.1 christos }
173 1.1 christos }
174 1.1 christos }
175 1.1 christos
176 1.1 christos void
177 1.1 christos pmap_tlb_syncicache_wanted(struct cpu_info *ci)
178 1.1 christos {
179 1.1 christos struct pmap_tlb_info * const ti = ci->ci_tlb_info;
180 1.1 christos
181 1.1 christos KASSERT(cpu_intr_p());
182 1.1 christos
183 1.1 christos TLBINFO_LOCK(ti);
184 1.1 christos
185 1.1 christos /*
186 1.1 christos * We might have been notified because another CPU changed an exec
187 1.1 christos * page and now needs us to sync the icache so tell the current lwp
188 1.1 christos * to do the next time it returns to userland (which should be very
189 1.1 christos * soon).
190 1.1 christos */
191 1.1 christos if (ti->ti_synci_page_bitmap && (ci->ci_flags & CPUF_USERPMAP)) {
192 1.1 christos curlwp->l_md.md_astpending = 1; /* force call to ast() */
193 1.1 christos ci->ci_evcnt_synci_ipi_rqst.ev_count++;
194 1.1 christos }
195 1.1 christos
196 1.1 christos TLBINFO_UNLOCK(ti);
197 1.1 christos
198 1.1 christos }
199 1.1 christos #endif /* MULTIPROCESSOR */
200