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.5 skrll __KERNEL_RCSID(0, "$NetBSD: pmap_synci.c,v 1.5 2020/04/13 08:05:22 skrll 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.4 skrll 40 1.1 christos #include <sys/atomic.h> 41 1.1 christos #include <sys/cpu.h> 42 1.4 skrll #include <sys/mutex.h> 43 1.4 skrll #include <sys/systm.h> 44 1.5 skrll 45 1.1 christos #include <uvm/uvm.h> 46 1.1 christos 47 1.1 christos #if defined(MULTIPROCESSOR) 48 1.3 matt u_int pmap_tlb_synci_page_mask; 49 1.3 matt u_int pmap_tlb_synci_map_mask; 50 1.3 matt 51 1.1 christos void 52 1.3 matt pmap_tlb_syncicache_ast(struct cpu_info *ci) 53 1.1 christos { 54 1.2 matt struct pmap_tlb_info * const ti = cpu_tlb_info(ci); 55 1.1 christos 56 1.1 christos KASSERT(kpreempt_disabled()); 57 1.1 christos 58 1.1 christos uint32_t page_bitmap = atomic_swap_32(&ti->ti_synci_page_bitmap, 0); 59 1.1 christos #if 0 60 1.1 christos printf("%s: need to sync %#x\n", __func__, page_bitmap); 61 1.1 christos #endif 62 1.1 christos ti->ti_evcnt_synci_asts.ev_count++; 63 1.1 christos /* 64 1.1 christos * If every bit is set in the bitmap, sync the entire icache. 65 1.1 christos */ 66 1.1 christos if (page_bitmap == pmap_tlb_synci_map_mask) { 67 1.1 christos pmap_md_icache_sync_all(); 68 1.1 christos ti->ti_evcnt_synci_all.ev_count++; 69 1.1 christos ti->ti_evcnt_synci_pages.ev_count += pmap_tlb_synci_page_mask+1; 70 1.1 christos return; 71 1.1 christos } 72 1.1 christos 73 1.1 christos /* 74 1.1 christos * Loop through the bitmap clearing each set of indices for each page. 75 1.1 christos */ 76 1.1 christos for (vaddr_t va = 0; 77 1.1 christos page_bitmap != 0; 78 1.1 christos page_bitmap >>= 1, va += PAGE_SIZE) { 79 1.1 christos if (page_bitmap & 1) { 80 1.1 christos /* 81 1.1 christos * Each bit set represents a page index to be synced. 82 1.1 christos */ 83 1.1 christos pmap_md_icache_sync_range_index(va, PAGE_SIZE); 84 1.1 christos ti->ti_evcnt_synci_pages.ev_count++; 85 1.1 christos } 86 1.1 christos } 87 1.1 christos } 88 1.1 christos 89 1.1 christos void 90 1.3 matt pmap_tlb_syncicache(vaddr_t va, const kcpuset_t *page_onproc) 91 1.1 christos { 92 1.1 christos KASSERT(kpreempt_disabled()); 93 1.1 christos /* 94 1.1 christos * We don't sync the icache here but let ast do it for us just before 95 1.1 christos * returning to userspace. We do this because we don't really know 96 1.1 christos * on which CPU we will return to userspace and if we synch the icache 97 1.1 christos * now it might not be on the CPU we need it on. In addition, others 98 1.1 christos * threads might sync the icache before we get to return to userland 99 1.1 christos * so there's no reason for us to do it. 100 1.1 christos * 101 1.1 christos * Each TLB/cache keeps a synci sequence number which gets advanced 102 1.1 christos * each time that TLB/cache performs a pmap_md_sync_icache_all. When 103 1.1 christos * we return to userland, we check the pmap's corresponding synci 104 1.1 christos * sequence number for that TLB/cache. If they match, it means that 105 1.1 christos * no one has yet synched the icache so we much do it ourselves. If 106 1.1 christos * they don't match someone has already synced the icache for us. 107 1.1 christos * 108 1.1 christos * There is a small chance that the generation numbers will wrap and 109 1.1 christos * then become equal but that's a one in 4 billion cache and will 110 1.1 christos * just cause an extra sync of the icache. 111 1.1 christos */ 112 1.3 matt struct cpu_info * const ci = curcpu(); 113 1.3 matt kcpuset_t *onproc; 114 1.3 matt kcpuset_create(&onproc, true); 115 1.1 christos const uint32_t page_mask = 116 1.1 christos 1L << ((va >> PGSHIFT) & pmap_tlb_synci_page_mask); 117 1.1 christos for (size_t i = 0; i < pmap_ntlbs; i++) { 118 1.1 christos struct pmap_tlb_info * const ti = pmap_tlbs[i]; 119 1.1 christos TLBINFO_LOCK(ti); 120 1.1 christos for (;;) { 121 1.1 christos uint32_t old_page_bitmap = ti->ti_synci_page_bitmap; 122 1.1 christos if (old_page_bitmap & page_mask) { 123 1.1 christos ti->ti_evcnt_synci_duplicate.ev_count++; 124 1.1 christos break; 125 1.1 christos } 126 1.1 christos 127 1.1 christos uint32_t orig_page_bitmap = atomic_cas_32( 128 1.1 christos &ti->ti_synci_page_bitmap, old_page_bitmap, 129 1.1 christos old_page_bitmap | page_mask); 130 1.1 christos 131 1.1 christos if (orig_page_bitmap == old_page_bitmap) { 132 1.1 christos if (old_page_bitmap == 0) { 133 1.3 matt kcpuset_merge(onproc, ti->ti_kcpuset); 134 1.1 christos } else { 135 1.1 christos ti->ti_evcnt_synci_deferred.ev_count++; 136 1.1 christos } 137 1.1 christos ti->ti_evcnt_synci_desired.ev_count++; 138 1.1 christos break; 139 1.1 christos } 140 1.1 christos } 141 1.1 christos #if 0 142 1.1 christos printf("%s: %s: %x to %x on cpus %#x\n", __func__, 143 1.1 christos ti->ti_name, page_mask, ti->ti_synci_page_bitmap, 144 1.1 christos onproc & page_onproc & ti->ti_cpu_mask); 145 1.1 christos #endif 146 1.1 christos TLBINFO_UNLOCK(ti); 147 1.1 christos } 148 1.3 matt kcpuset_intersect(onproc, page_onproc); 149 1.3 matt if (__predict_false(!kcpuset_iszero(onproc))) { 150 1.1 christos /* 151 1.1 christos * If the cpu need to sync this page, tell the current lwp 152 1.1 christos * to sync the icache before it returns to userspace. 153 1.1 christos */ 154 1.3 matt if (kcpuset_isset(onproc, cpu_index(ci))) { 155 1.3 matt if (ci->ci_flags & CPUF_USERPMAP) { 156 1.1 christos curlwp->l_md.md_astpending = 1; /* force call to ast() */ 157 1.3 matt ci->ci_evcnt_synci_onproc_rqst.ev_count++; 158 1.1 christos } else { 159 1.3 matt ci->ci_evcnt_synci_deferred_rqst.ev_count++; 160 1.1 christos } 161 1.3 matt kcpuset_clear(onproc, cpu_index(ci)); 162 1.1 christos } 163 1.1 christos 164 1.1 christos /* 165 1.1 christos * For each cpu that is affect, send an IPI telling 166 1.1 christos * that CPU that the current thread needs to sync its icache. 167 1.1 christos * We might cause some spurious icache syncs but that's not 168 1.1 christos * going to break anything. 169 1.1 christos */ 170 1.3 matt for (cpuid_t n = kcpuset_ffs(onproc); 171 1.3 matt n-- > 0; 172 1.3 matt n = kcpuset_ffs(onproc)) { 173 1.3 matt kcpuset_clear(onproc, n); 174 1.3 matt cpu_send_ipi(cpu_lookup(n), IPI_SYNCICACHE); 175 1.1 christos } 176 1.1 christos } 177 1.3 matt kcpuset_destroy(onproc); 178 1.1 christos } 179 1.1 christos 180 1.1 christos void 181 1.1 christos pmap_tlb_syncicache_wanted(struct cpu_info *ci) 182 1.1 christos { 183 1.2 matt struct pmap_tlb_info * const ti = cpu_tlb_info(ci); 184 1.1 christos 185 1.1 christos KASSERT(cpu_intr_p()); 186 1.1 christos 187 1.1 christos TLBINFO_LOCK(ti); 188 1.1 christos 189 1.1 christos /* 190 1.1 christos * We might have been notified because another CPU changed an exec 191 1.1 christos * page and now needs us to sync the icache so tell the current lwp 192 1.1 christos * to do the next time it returns to userland (which should be very 193 1.1 christos * soon). 194 1.1 christos */ 195 1.1 christos if (ti->ti_synci_page_bitmap && (ci->ci_flags & CPUF_USERPMAP)) { 196 1.1 christos curlwp->l_md.md_astpending = 1; /* force call to ast() */ 197 1.1 christos ci->ci_evcnt_synci_ipi_rqst.ev_count++; 198 1.1 christos } 199 1.1 christos 200 1.1 christos TLBINFO_UNLOCK(ti); 201 1.1 christos 202 1.1 christos } 203 1.1 christos #endif /* MULTIPROCESSOR */ 204