1 /* $NetBSD: patch.c,v 1.8 2023/11/21 22:19:12 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 2007, 2021 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Andrew Doran and Jason R. Thorpe. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * Patch kernel code at boot time, depending on available CPU features 34 * and configuration. 35 */ 36 37 #include <sys/cdefs.h> 38 __KERNEL_RCSID(0, "$NetBSD: patch.c,v 1.8 2023/11/21 22:19:12 thorpej Exp $"); 39 40 #include "opt_multiprocessor.h" 41 42 #include <sys/types.h> 43 #include <sys/systm.h> 44 #include <sys/lwp.h> 45 46 #include <machine/cpu.h> 47 #include <machine/alpha.h> 48 #include <machine/intr.h> 49 50 #include <machine/alpha_instruction.h> 51 52 void _membar_producer(void); 53 void _membar_producer_end(void); 54 void _membar_producer_mp(void); 55 void _membar_producer_mp_end(void); 56 57 void _membar_sync(void); 58 void _membar_sync_end(void); 59 void _membar_sync_mp(void); 60 void _membar_sync_mp_end(void); 61 62 extern char alpha_copystr_bwx[], alpha_copystr_bwx_end[]; 63 extern char alpha_copystr[], alpha_copystr_end[]; 64 65 #if defined(MULTIPROCESSOR) 66 extern uint32_t *lock_stub_patch_table[]; 67 68 static void 69 patch_lock_stubs(void) 70 { 71 alpha_instruction insn = { 72 .mem_format = { 73 .opcode = op_special, 74 .displacement = op_mb, 75 }, 76 }; 77 int i, s; 78 79 s = splhigh(); 80 81 for (i = 0; lock_stub_patch_table[i] != NULL; i++) { 82 *lock_stub_patch_table[i] = insn.bits; 83 } 84 alpha_pal_imb(); 85 86 splx(s); 87 } 88 #endif /* MULTIPROCESSOR */ 89 90 static void 91 patchfunc(void *from_s, void *from_e, void *to_s, void *to_e) 92 { 93 int s; 94 95 s = splhigh(); 96 97 if ((uintptr_t)from_e - (uintptr_t)from_s != 98 (uintptr_t)to_e - (uintptr_t)to_s) 99 panic("patchfunc: sizes do not match (from=%p)", from_s); 100 101 memcpy(to_s, from_s, (uintptr_t)to_e - (uintptr_t)to_s); 102 alpha_pal_imb(); 103 104 splx(s); 105 } 106 107 void 108 alpha_patch(bool is_mp) 109 { 110 111 /* 112 * We allow this function to be called multiple times 113 * (there is no harm in doing so), so long as other 114 * CPUs have not yet actually hatched to start running 115 * kernel code. 116 */ 117 118 if (cpu_amask & ALPHA_AMASK_BWX) { 119 patchfunc(alpha_copystr_bwx, alpha_copystr_bwx_end, 120 alpha_copystr, alpha_copystr_end); 121 } 122 123 #if defined(MULTIPROCESSOR) 124 if (is_mp) { 125 KASSERT(curcpu()->ci_flags & CPUF_PRIMARY); 126 KASSERT((cpus_running & ~(1UL << cpu_number())) == 0); 127 128 patch_lock_stubs(); 129 130 patchfunc(_membar_producer_mp, _membar_producer_mp_end, 131 _membar_producer, _membar_producer_end); 132 patchfunc(_membar_sync_mp, _membar_sync_mp_end, 133 _membar_sync, _membar_sync_end); 134 } 135 #else 136 KASSERT(is_mp == false); 137 #endif /* MULTIPROCESSOR */ 138 } 139