1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 * 25 * Copyright 2018 Joyent, Inc. 26 */ 27 28 /* 29 * Kernel/Debugger Interface (KDI) routines. Called during debugger under 30 * various system states (boot, while running, while the debugger has control). 31 * Functions intended for use while the debugger has control may not grab any 32 * locks or perform any functions that assume the availability of other system 33 * services. 34 */ 35 36 #include <sys/systm.h> 37 #include <sys/x86_archext.h> 38 #include <sys/kdi_impl.h> 39 #include <sys/smp_impldefs.h> 40 #include <sys/psm_types.h> 41 #include <sys/segments.h> 42 #include <sys/archsystm.h> 43 #include <sys/controlregs.h> 44 #include <sys/trap.h> 45 #include <sys/kobj.h> 46 #include <sys/kobj_impl.h> 47 #include <sys/mach_mmu.h> 48 49 void 50 kdi_idt_write(gate_desc_t *gate, uint_t vec) 51 { 52 gate_desc_t *idt = CPU->cpu_m.mcpu_idt; 53 54 /* 55 * See kdi_idtr_set(). 56 */ 57 if (idt == NULL) { 58 desctbr_t idtr; 59 rd_idtr(&idtr); 60 idt = (gate_desc_t *)idtr.dtr_base; 61 } 62 63 idt[vec] = *gate; 64 } 65 66 ulong_t 67 kdi_dreg_get(int reg) 68 { 69 switch (reg) { 70 case 0: 71 return (kdi_getdr0()); 72 case 1: 73 return (kdi_getdr1()); 74 case 2: 75 return (kdi_getdr2()); 76 case 3: 77 return (kdi_getdr3()); 78 case 6: 79 return (kdi_getdr6()); 80 case 7: 81 return (kdi_getdr7()); 82 default: 83 panic("invalid debug register dr%d", reg); 84 /*NOTREACHED*/ 85 } 86 } 87 88 void 89 kdi_dreg_set(int reg, ulong_t value) 90 { 91 switch (reg) { 92 case 0: 93 kdi_setdr0(value); 94 break; 95 case 1: 96 kdi_setdr1(value); 97 break; 98 case 2: 99 kdi_setdr2(value); 100 break; 101 case 3: 102 kdi_setdr3(value); 103 break; 104 case 6: 105 kdi_setdr6(value); 106 break; 107 case 7: 108 kdi_setdr7(value); 109 break; 110 default: 111 panic("invalid debug register dr%d", reg); 112 /*NOTREACHED*/ 113 } 114 } 115 116 extern void kdi_slave_entry(void); 117 118 void 119 kdi_stop_slaves(int cpu, int doxc) 120 { 121 if (doxc) 122 kdi_xc_others(cpu, kdi_slave_entry); 123 } 124 125 /* 126 * On i86pc, slaves busy-loop, so we don't need to do anything here. 127 */ 128 void 129 kdi_start_slaves(void) 130 { 131 } 132 133 void 134 kdi_slave_wait(void) 135 { 136 } 137 138 /* 139 * Caution. 140 * These routines are called -extremely- early, during kmdb initialization. 141 * 142 * Many common kernel functions assume that %gs has been initialized, 143 * and fail horribly if it hasn't. At this point, the boot code has 144 * reserved a descriptor for us (KMDBGS_SEL) in it's GDT; arrange for it 145 * to point at a dummy cpu_t, temporarily at least. 146 * 147 * Note that kmdb entry relies on the fake cpu_t having zero cpu_idt/cpu_id. 148 */ 149 150 #if defined(__amd64) 151 152 void * 153 boot_kdi_tmpinit(void) 154 { 155 cpu_t *cpu = kobj_zalloc(sizeof (*cpu), KM_TMP); 156 uintptr_t old; 157 158 cpu->cpu_self = cpu; 159 160 old = (uintptr_t)rdmsr(MSR_AMD_GSBASE); 161 wrmsr(MSR_AMD_GSBASE, (uint64_t)cpu); 162 return ((void *)old); 163 } 164 165 void 166 boot_kdi_tmpfini(void *old) 167 { 168 wrmsr(MSR_AMD_GSBASE, (uint64_t)old); 169 } 170 171 #elif defined(__i386) 172 173 void * 174 boot_kdi_tmpinit(void) 175 { 176 cpu_t *cpu = kobj_zalloc(sizeof (*cpu), KM_TMP); 177 uintptr_t old; 178 desctbr_t b_gdtr; 179 user_desc_t *bgdt; 180 181 cpu->cpu_self = cpu; 182 183 rd_gdtr(&b_gdtr); 184 bgdt = (user_desc_t *)(b_gdtr.dtr_base); 185 186 set_usegd(&bgdt[GDT_BGSTMP], 187 cpu, sizeof (*cpu), SDT_MEMRWA, SEL_KPL, SDP_BYTES, SDP_OP32); 188 189 /* 190 * Now switch %gs to point at it. 191 */ 192 old = getgs(); 193 setgs(KMDBGS_SEL); 194 195 return ((void *)old); 196 } 197 198 void 199 boot_kdi_tmpfini(void *old) 200 { 201 setgs((uintptr_t)old); 202 } 203 204 #endif /* __i386 */