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 /*
  23  * Copyright (c) 2012 Gary Mills
  24  *
  25  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
  26  * Use is subject to license terms.
  27  */
  28 
  29 #include <kmdb/kmdb_auxv.h>
  30 #include <kmdb/kctl/kctl.h>
  31 
  32 #include <sys/bootconf.h>
  33 #include <sys/kobj.h>
  34 #include <sys/kobj_impl.h>
  35 #include <sys/cpuvar.h>
  36 #include <sys/kdi_impl.h>
  37 #include <sys/x86_archext.h>
  38 #include <sys/controlregs.h>
  39 #include <sys/archsystm.h>
  40 
  41 static int
  42 kctl_boot_prop_read(char *pname, char *prop_buf, int buf_len)
  43 {
  44         struct bootops *ops = kctl.kctl_boot_ops;
  45         int len;
  46 
  47         len = BOP_GETPROPLEN(ops, pname);
  48         if (len > 0 && len <= buf_len) {
  49                 (void) BOP_GETPROP(ops, pname, (void *)prop_buf);
  50                 return (1);
  51         }
  52 
  53         return (0);
  54 }
  55 
  56 static int
  57 kctl_ddi_prop_read(char *pname, char *prop_buf, int buf_len)
  58 {
  59         dev_info_t *dip = ddi_root_node();
  60         char *val;
  61         int ret = 0;
  62 
  63         if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip,
  64             DDI_PROP_DONTPASS, pname, &val) != DDI_SUCCESS)
  65                 return (0);
  66 
  67         if (strlen(val) < buf_len) {
  68                 (void) strcpy(prop_buf, val);
  69                 ret = 1;
  70         }
  71 
  72         ddi_prop_free(val);
  73         return (ret);
  74 }
  75 
  76 /*
  77  * We don't have any property-walking routines, so we have to specifically
  78  * query and thus have guilty knowledge of the properties that the
  79  * debugger wants to see.
  80  *
  81  * Here actually we only support eight console properties:
  82  *     input-device, output-device, tty[a-d]-mode, screen-#rows, screen-#cols.
  83  */
  84 #define KCTL_PROPNV_NIODEV      2
  85 #define KCTL_PROPNV_NTTYMD      4
  86 #define KCTL_PROPNV_NSCREEN     2
  87 #define KCTL_PROPNV_NENT        (KCTL_PROPNV_NIODEV + KCTL_PROPNV_NTTYMD + \
  88         KCTL_PROPNV_NSCREEN)
  89 
  90 static kmdb_auxv_nv_t *
  91 kctl_pcache_create(int *nprops)
  92 {
  93         int (*preader)(char *, char *, int);
  94         kmdb_auxv_nv_t *pnv;
  95         size_t psz = sizeof (kmdb_auxv_nv_t) * KCTL_PROPNV_NENT;
  96         char *inputdev, *outputdev;
  97         int i, j;
  98         char ttymode[] = "ttyX-mode";
  99 
 100         if (kctl.kctl_boot_loaded) {
 101                 preader = kctl_boot_prop_read;
 102         } else {
 103                 preader = kctl_ddi_prop_read;
 104         }
 105 
 106         pnv = kobj_alloc(psz, KM_WAIT);
 107         inputdev = (&pnv[0])->kanv_val;
 108         outputdev = (&pnv[1])->kanv_val;
 109 
 110         /* Set the property names. */
 111         (void) strcpy((&pnv[0])->kanv_name, "input-device");
 112         (void) strcpy((&pnv[1])->kanv_name, "output-device");
 113         for (i = 0; i < KCTL_PROPNV_NTTYMD; i++) {
 114                 ttymode[3] = 'a' + i;
 115                 (void) strcpy((&pnv[i + KCTL_PROPNV_NIODEV])->kanv_name,
 116                     ttymode);
 117         }
 118 
 119         /*
 120          * console is defined by "console" property, with
 121          * fallback on the old "input-device" property.
 122          */
 123         (void) strcpy(inputdev, "text");        /* default to screen */
 124         if (!preader("diag-device", inputdev, sizeof ((&pnv[0])->kanv_val)) &&
 125             !preader("console", inputdev, sizeof ((&pnv[0])->kanv_val)))
 126                 (void) preader("input-device", inputdev,
 127                     sizeof ((&pnv[0])->kanv_val));
 128 
 129         if (strncmp(inputdev, "tty", 3) == 0 &&
 130             inputdev[4] == '\0' &&
 131             inputdev[3] >= 'a' &&
 132             inputdev[3] < 'a' + KCTL_PROPNV_NTTYMD) {
 133                 (void) strcpy(outputdev, inputdev);
 134         } else {
 135                 (void) strcpy(inputdev, "keyboard");
 136                 (void) strcpy(outputdev, "screen");
 137         }
 138 
 139         /* Set tty modes or defaults. */
 140         j = KCTL_PROPNV_NIODEV + KCTL_PROPNV_NTTYMD;
 141         for (i = KCTL_PROPNV_NIODEV; i < j; i++) {
 142                 if (!preader((&pnv[i])->kanv_name, (&pnv[i])->kanv_val,
 143                     sizeof ((&pnv[0])->kanv_val)))
 144                         (void) strcpy((&pnv[i])->kanv_val, "9600,8,n,1,-");
 145         }
 146 
 147         (void) strcpy((&pnv[j])->kanv_name, "screen-#rows");
 148         (void) strcpy((&pnv[j + 1])->kanv_name, "screen-#cols");
 149         (void) strcpy((&pnv[j])->kanv_val, "0");
 150         (void) strcpy((&pnv[j + 1])->kanv_val, "0");
 151         (void) preader((&pnv[j])->kanv_name, (&pnv[j])->kanv_val,
 152             sizeof ((&pnv[j])->kanv_val));
 153         (void) preader((&pnv[j + 1])->kanv_name, (&pnv[j + 1])->kanv_val,
 154             sizeof ((&pnv[j + 1])->kanv_val));
 155 
 156         *nprops = KCTL_PROPNV_NENT;
 157         return (pnv);
 158 }
 159 
 160 static void
 161 kctl_pcache_destroy(kmdb_auxv_nv_t *pnv)
 162 {
 163         kobj_free(pnv, sizeof (kmdb_auxv_nv_t) * KCTL_PROPNV_NENT);
 164 }
 165 
 166 void
 167 kctl_auxv_init_isadep(kmdb_auxv_t *kav, void *romp)
 168 {
 169         kav->kav_pcache = kctl_pcache_create(&kav->kav_nprops);
 170         kav->kav_romp = romp;
 171 }
 172 
 173 void
 174 kctl_auxv_fini_isadep(kmdb_auxv_t *kav)
 175 {
 176         if (kav->kav_pcache != NULL)
 177                 kctl_pcache_destroy(kav->kav_pcache);
 178 }
 179 
 180 int
 181 kctl_preactivate_isadep(void)
 182 {
 183         return (0);
 184 }
 185 
 186 /*ARGSUSED*/
 187 void
 188 kctl_activate_isadep(kdi_debugvec_t *dvec)
 189 {
 190         dvec->dv_kctl_vmready = hat_kdi_init;
 191 
 192         if (!kctl.kctl_boot_loaded)
 193                 hat_kdi_init();
 194 }
 195 
 196 void
 197 kctl_depreactivate_isadep(void)
 198 {
 199 }
 200 
 201 /*
 202  * Many common kernel functions assume that %gs can be deferenced, and
 203  * fail horribly if it cannot.  Ask the kernel to set up a temporary
 204  * mapping to a fake cpu_t so that we can call such functions during
 205  * initialization.
 206  */
 207 void *
 208 kctl_boot_tmpinit(void)
 209 {
 210         return (boot_kdi_tmpinit());
 211 }
 212 
 213 void
 214 kctl_boot_tmpfini(void *old)
 215 {
 216         boot_kdi_tmpfini(old);
 217 }