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 
  26 #pragma ident   "%Z%%M% %I%     %E% SMI"
  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 void
 117 kdi_flush_caches(void)
 118 {
 119         reload_cr3();
 120 }
 121 
 122 extern void kdi_slave_entry(void);
 123 
 124 void
 125 kdi_stop_slaves(int cpu, int doxc)
 126 {
 127         if (doxc)
 128                 kdi_xc_others(cpu, kdi_slave_entry);
 129 }
 130 
 131 /*
 132  * On i86pc, slaves busy-loop, so we don't need to do anything here.
 133  */
 134 void
 135 kdi_start_slaves(void)
 136 {
 137 }
 138 
 139 void
 140 kdi_slave_wait(void)
 141 {
 142 }
 143 
 144 /*
 145  * Caution.
 146  * These routines are called -extremely- early, during kmdb initialization.
 147  *
 148  * Many common kernel functions assume that %gs has been initialized,
 149  * and fail horribly if it hasn't.  At this point, the boot code has
 150  * reserved a descriptor for us (KMDBGS_SEL) in it's GDT; arrange for it
 151  * to point at a dummy cpu_t, temporarily at least.
 152  *
 153  * Note that kmdb entry relies on the fake cpu_t having zero cpu_idt/cpu_id.
 154  */
 155 
 156 #if defined(__amd64)
 157 
 158 void *
 159 boot_kdi_tmpinit(void)
 160 {
 161         cpu_t *cpu = kobj_zalloc(sizeof (*cpu), KM_TMP);
 162         uintptr_t old;
 163 
 164         cpu->cpu_self = cpu;
 165 
 166         old = (uintptr_t)rdmsr(MSR_AMD_GSBASE);
 167         wrmsr(MSR_AMD_GSBASE, (uint64_t)cpu);
 168         return ((void *)old);
 169 }
 170 
 171 void
 172 boot_kdi_tmpfini(void *old)
 173 {
 174         wrmsr(MSR_AMD_GSBASE, (uint64_t)old);
 175 }
 176 
 177 #elif defined(__i386)
 178 
 179 void *
 180 boot_kdi_tmpinit(void)
 181 {
 182         cpu_t *cpu = kobj_zalloc(sizeof (*cpu), KM_TMP);
 183         uintptr_t old;
 184         desctbr_t b_gdtr;
 185         user_desc_t *bgdt;
 186 
 187         cpu->cpu_self = cpu;
 188 
 189         rd_gdtr(&b_gdtr);
 190         bgdt = (user_desc_t *)(b_gdtr.dtr_base);
 191 
 192         set_usegd(&bgdt[GDT_BGSTMP],
 193             cpu, sizeof (*cpu), SDT_MEMRWA, SEL_KPL, SDP_BYTES, SDP_OP32);
 194 
 195         /*
 196          * Now switch %gs to point at it.
 197          */
 198         old = getgs();
 199         setgs(KMDBGS_SEL);
 200 
 201         return ((void *)old);
 202 }
 203 
 204 void
 205 boot_kdi_tmpfini(void *old)
 206 {
 207         setgs((uintptr_t)old);
 208 }
 209 
 210 #endif  /* __i386 */