10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
51991Sheppo * Common Development and Distribution License (the "License").
61991Sheppo * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
223446Smrj * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
270Sstevel@tonic-gate
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <sys/systm.h>
300Sstevel@tonic-gate #include <sys/bootconf.h>
310Sstevel@tonic-gate #include <sys/cpu_module.h>
320Sstevel@tonic-gate #include <sys/x_call.h>
330Sstevel@tonic-gate #include <sys/kdi_impl.h>
340Sstevel@tonic-gate #include <sys/mmu.h>
350Sstevel@tonic-gate #include <sys/cpuvar.h>
360Sstevel@tonic-gate #include <sys/kobj.h>
370Sstevel@tonic-gate #include <sys/kobj_impl.h>
381991Sheppo #ifdef sun4v
391991Sheppo #include <sys/ldoms.h>
401991Sheppo #include <sys/promif_impl.h>
411991Sheppo #include <kmdb/kmdb_kctl.h>
421991Sheppo #endif
430Sstevel@tonic-gate
440Sstevel@tonic-gate #include <kmdb/kctl/kctl.h>
450Sstevel@tonic-gate
460Sstevel@tonic-gate #define KCTL_TTABLE_SIZE 0x6000 /* trap table size */
470Sstevel@tonic-gate
480Sstevel@tonic-gate static uint32_t kctl_trap_brsav; /* saved ba,a from kmdb_trap */
490Sstevel@tonic-gate static uint32_t kctl_trap_tl1_brsav; /* saved ba,a from kmdb_trap_tl1 */
500Sstevel@tonic-gate
510Sstevel@tonic-gate extern struct scb trap_table;
520Sstevel@tonic-gate
530Sstevel@tonic-gate static void
kctl_patch_text(caddr_t addr,uint32_t data)540Sstevel@tonic-gate kctl_patch_text(caddr_t addr, uint32_t data)
550Sstevel@tonic-gate {
560Sstevel@tonic-gate if (kctl.kctl_boot_loaded) {
570Sstevel@tonic-gate /* LINTED - pointer alignment */
580Sstevel@tonic-gate *((uint32_t *)addr) = data;
590Sstevel@tonic-gate } else {
600Sstevel@tonic-gate hot_patch_kernel_text(addr, data, sizeof (data));
610Sstevel@tonic-gate }
620Sstevel@tonic-gate }
630Sstevel@tonic-gate
640Sstevel@tonic-gate /*
650Sstevel@tonic-gate * The traps that transfer control to kmdb (breakpoint, programmed entry, etc)
660Sstevel@tonic-gate * use kmdb_trap and kmdb_trap_tl1, which normally begin with a ba,a to
670Sstevel@tonic-gate * trap_table0 - a bad trap entry. When kmdb starts, it will use
680Sstevel@tonic-gate * kctl_ktrap_install to replace the ba with a jmp to the appropriate kmdb
690Sstevel@tonic-gate * entry points. Deactivation uses kctl_ktrap_restore to restore the ba
700Sstevel@tonic-gate * instructions.
710Sstevel@tonic-gate */
720Sstevel@tonic-gate static void
kctl_ktrap_install(int tl,void (* handler)(void))730Sstevel@tonic-gate kctl_ktrap_install(int tl, void (*handler)(void))
740Sstevel@tonic-gate {
750Sstevel@tonic-gate extern uint32_t kmdb_trap, kmdb_trap_tl1;
760Sstevel@tonic-gate uint32_t *entryp = tl ? &kmdb_trap_tl1 : &kmdb_trap;
770Sstevel@tonic-gate uint32_t *savp = tl ? &kctl_trap_brsav : &kctl_trap_tl1_brsav;
78436Sdmick uint32_t hi = (uint32_t)(uintptr_t)handler >> 10;
79436Sdmick uint32_t lo = (uint32_t)(uintptr_t)handler & 0x3ff;
800Sstevel@tonic-gate uint32_t inst;
810Sstevel@tonic-gate
820Sstevel@tonic-gate *savp = *entryp;
830Sstevel@tonic-gate
840Sstevel@tonic-gate inst = 0x81c06000 | lo; /* jmp %g1 + %lo(handler) */
850Sstevel@tonic-gate kctl_patch_text((caddr_t)(entryp + 1), inst);
860Sstevel@tonic-gate
870Sstevel@tonic-gate inst = 0x03000000 | hi; /* sethi %hi(handler), %g1 */
880Sstevel@tonic-gate kctl_patch_text((caddr_t)entryp, inst);
890Sstevel@tonic-gate }
900Sstevel@tonic-gate
910Sstevel@tonic-gate static void
kctl_ktrap_restore(void)920Sstevel@tonic-gate kctl_ktrap_restore(void)
930Sstevel@tonic-gate {
940Sstevel@tonic-gate extern uint32_t kmdb_trap, kmdb_trap_tl1;
950Sstevel@tonic-gate
960Sstevel@tonic-gate hot_patch_kernel_text((caddr_t)&kmdb_trap, kctl_trap_brsav, 4);
970Sstevel@tonic-gate hot_patch_kernel_text((caddr_t)&kmdb_trap_tl1, kctl_trap_tl1_brsav, 4);
980Sstevel@tonic-gate }
990Sstevel@tonic-gate
1000Sstevel@tonic-gate static void
kctl_ttable_tlb_modify(caddr_t tba,size_t sz,void (* func)(caddr_t,int))1010Sstevel@tonic-gate kctl_ttable_tlb_modify(caddr_t tba, size_t sz, void (*func)(caddr_t, int))
1020Sstevel@tonic-gate {
1030Sstevel@tonic-gate #if defined(KMDB_TRAPCOUNT)
1040Sstevel@tonic-gate int do_dtlb = 1;
1050Sstevel@tonic-gate #else
1060Sstevel@tonic-gate int do_dtlb = 0;
1070Sstevel@tonic-gate #endif
1080Sstevel@tonic-gate
1090Sstevel@tonic-gate caddr_t va;
1100Sstevel@tonic-gate
1110Sstevel@tonic-gate ASSERT((sz & MMU_PAGEOFFSET) == 0);
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate for (va = tba; sz > 0; sz -= MMU_PAGESIZE, va += MMU_PAGESIZE)
1140Sstevel@tonic-gate func(va, do_dtlb);
1150Sstevel@tonic-gate }
1160Sstevel@tonic-gate
1170Sstevel@tonic-gate static void
kctl_ttable_tlb_lock(caddr_t tba,size_t sz)1180Sstevel@tonic-gate kctl_ttable_tlb_lock(caddr_t tba, size_t sz)
1190Sstevel@tonic-gate {
1200Sstevel@tonic-gate kctl_ttable_tlb_modify(tba, sz, kdi_tlb_page_lock);
1210Sstevel@tonic-gate }
1220Sstevel@tonic-gate
1230Sstevel@tonic-gate static void
kctl_ttable_tlb_unlock(caddr_t tba,size_t sz)1240Sstevel@tonic-gate kctl_ttable_tlb_unlock(caddr_t tba, size_t sz)
1250Sstevel@tonic-gate {
1260Sstevel@tonic-gate kctl_ttable_tlb_modify(tba, sz, kdi_tlb_page_unlock);
1270Sstevel@tonic-gate }
1280Sstevel@tonic-gate
1290Sstevel@tonic-gate /*
1300Sstevel@tonic-gate * kmdb has its own trap table. Life is made considerably easier if
1310Sstevel@tonic-gate * we allocate and configure it here, passing it to the debugger for
1320Sstevel@tonic-gate * final tweaking.
1330Sstevel@tonic-gate *
1340Sstevel@tonic-gate * The debugger code, and data accessed by the handlers are either
1350Sstevel@tonic-gate * a) locked into the TLB or b) accessible by our tte-lookup code. As
1360Sstevel@tonic-gate * such, we need only lock the trap table itself into the TLBs. We'll
1370Sstevel@tonic-gate * get the memory for the table from the beginning of the debugger
1380Sstevel@tonic-gate * segment, which has already been allocated.
1390Sstevel@tonic-gate */
1400Sstevel@tonic-gate static void
kctl_ttable_init(void)1410Sstevel@tonic-gate kctl_ttable_init(void)
1420Sstevel@tonic-gate {
1430Sstevel@tonic-gate xc_all((xcfunc_t *)kctl_ttable_tlb_lock, (uint64_t)kctl.kctl_tba,
1440Sstevel@tonic-gate KCTL_TTABLE_SIZE);
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate static void
kctl_ttable_fini(void)1480Sstevel@tonic-gate kctl_ttable_fini(void)
1490Sstevel@tonic-gate {
1500Sstevel@tonic-gate xc_all((xcfunc_t *)kctl_ttable_tlb_unlock, (uint64_t)kctl.kctl_dseg,
1510Sstevel@tonic-gate KCTL_TTABLE_SIZE);
1520Sstevel@tonic-gate }
1530Sstevel@tonic-gate
1540Sstevel@tonic-gate static caddr_t
kctl_ttable_reserve(kmdb_auxv_t * kav,size_t * szp)1550Sstevel@tonic-gate kctl_ttable_reserve(kmdb_auxv_t *kav, size_t *szp)
1560Sstevel@tonic-gate {
1570Sstevel@tonic-gate caddr_t tba = kav->kav_dseg;
1580Sstevel@tonic-gate
1590Sstevel@tonic-gate ASSERT(kav->kav_dseg_size > KCTL_TTABLE_SIZE);
1600Sstevel@tonic-gate ASSERT(((uintptr_t)kav->kav_dseg & ((1 << 16) - 1)) == 0);
1610Sstevel@tonic-gate
1620Sstevel@tonic-gate kav->kav_dseg += KCTL_TTABLE_SIZE;
1630Sstevel@tonic-gate kav->kav_dseg_size -= KCTL_TTABLE_SIZE;
1640Sstevel@tonic-gate
1650Sstevel@tonic-gate *szp = KCTL_TTABLE_SIZE;
1660Sstevel@tonic-gate return (tba);
1670Sstevel@tonic-gate }
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate static void
kctl_cpu_init(void)1700Sstevel@tonic-gate kctl_cpu_init(void)
1710Sstevel@tonic-gate {
1720Sstevel@tonic-gate kctl_ttable_tlb_lock(kctl.kctl_tba, KCTL_TTABLE_SIZE);
1730Sstevel@tonic-gate }
1740Sstevel@tonic-gate
1750Sstevel@tonic-gate int
kctl_preactivate_isadep(void)1760Sstevel@tonic-gate kctl_preactivate_isadep(void)
1770Sstevel@tonic-gate {
1780Sstevel@tonic-gate if (!kctl.kctl_boot_loaded) {
1790Sstevel@tonic-gate if (kdi_watchdog_disable() != 0) {
1800Sstevel@tonic-gate cmn_err(CE_WARN, "hardware watchdog disabled while "
1810Sstevel@tonic-gate "debugger is activated");
1820Sstevel@tonic-gate }
1830Sstevel@tonic-gate
1840Sstevel@tonic-gate kctl_ttable_init();
1850Sstevel@tonic-gate }
1860Sstevel@tonic-gate
1870Sstevel@tonic-gate return (0);
1880Sstevel@tonic-gate }
1890Sstevel@tonic-gate
1900Sstevel@tonic-gate void
kctl_depreactivate_isadep(void)1910Sstevel@tonic-gate kctl_depreactivate_isadep(void)
1920Sstevel@tonic-gate {
1930Sstevel@tonic-gate kctl_ttable_fini();
1940Sstevel@tonic-gate
1950Sstevel@tonic-gate kdi_watchdog_restore();
1960Sstevel@tonic-gate }
1970Sstevel@tonic-gate
1983446Smrj void
kctl_activate_isadep(kdi_debugvec_t * dvec)1990Sstevel@tonic-gate kctl_activate_isadep(kdi_debugvec_t *dvec)
2000Sstevel@tonic-gate {
2010Sstevel@tonic-gate dvec->dv_kctl_cpu_init = kctl_cpu_init;
2020Sstevel@tonic-gate dvec->dv_kctl_vmready = kctl_ttable_init;
2030Sstevel@tonic-gate }
2040Sstevel@tonic-gate
2050Sstevel@tonic-gate void
kctl_auxv_init_isadep(kmdb_auxv_t * kav,void * romp)2060Sstevel@tonic-gate kctl_auxv_init_isadep(kmdb_auxv_t *kav, void *romp)
2070Sstevel@tonic-gate {
2080Sstevel@tonic-gate extern caddr_t boot_tba;
2090Sstevel@tonic-gate extern void *get_tba(void);
2100Sstevel@tonic-gate extern int (*cif_handler)(void *);
2110Sstevel@tonic-gate extern int prom_exit_enter_debugger;
2120Sstevel@tonic-gate
2130Sstevel@tonic-gate kctl.kctl_tba = kav->kav_tba_native = kctl_ttable_reserve(kav,
2140Sstevel@tonic-gate &kav->kav_tba_native_sz);
2150Sstevel@tonic-gate
2160Sstevel@tonic-gate kav->kav_tba_obp = (boot_tba == NULL ? get_tba() : boot_tba);
2170Sstevel@tonic-gate #ifdef sun4v
2180Sstevel@tonic-gate kav->kav_tba_kernel = (caddr_t)&trap_table;
2190Sstevel@tonic-gate #endif
2200Sstevel@tonic-gate kav->kav_tba_active = (kctl.kctl_boot_loaded ? kav->kav_tba_obp :
2210Sstevel@tonic-gate kav->kav_tba_native);
2220Sstevel@tonic-gate
2230Sstevel@tonic-gate kav->kav_promexitarmp = &prom_exit_enter_debugger;
2240Sstevel@tonic-gate
2250Sstevel@tonic-gate kav->kav_romp = (kctl.kctl_boot_loaded ? romp : (void *)cif_handler);
2260Sstevel@tonic-gate
2270Sstevel@tonic-gate kav->kav_ktrap_install = kctl_ktrap_install;
2280Sstevel@tonic-gate kav->kav_ktrap_restore = kctl_ktrap_restore;
2291991Sheppo #ifdef sun4v
2301991Sheppo if (kctl.kctl_boot_loaded) {
2311991Sheppo /*
2321991Sheppo * When booting kmdb, kmdb starts before domaining is
2331991Sheppo * enabled and before the cif handler is changed to the
2341991Sheppo * kernel cif handler. So we start kmdb with using the
2351991Sheppo * OBP and we will change this when the cif handler is
2361991Sheppo * installed.
2371991Sheppo */
2381991Sheppo kav->kav_domaining = 0;
2391991Sheppo } else {
2401991Sheppo kctl_auxv_set_promif(kav);
2411991Sheppo }
2421991Sheppo #endif
2430Sstevel@tonic-gate }
2440Sstevel@tonic-gate
2451991Sheppo #ifdef sun4v
2461991Sheppo
2471991Sheppo void
kctl_auxv_set_promif(kmdb_auxv_t * kav)2481991Sheppo kctl_auxv_set_promif(kmdb_auxv_t *kav)
2491991Sheppo {
250*4776Sjm22469 kav->kav_domaining = domaining_enabled();
2511991Sheppo kav->kav_promif_root = promif_stree_getroot();
2521991Sheppo kav->kav_promif_in = prom_stdin_ihandle();
2531991Sheppo kav->kav_promif_out = prom_stdout_ihandle();
2541991Sheppo kav->kav_promif_pin = prom_stdin_node();
2551991Sheppo kav->kav_promif_pout = prom_stdout_node();
2561991Sheppo kav->kav_promif_chosennode = prom_chosennode();
2571991Sheppo kav->kav_promif_optionsnode = prom_finddevice("/options");
2581991Sheppo }
2591991Sheppo
2601991Sheppo void
kctl_switch_promif(void)2611991Sheppo kctl_switch_promif(void)
2621991Sheppo {
2631991Sheppo kmdb_auxv_t kav;
2641991Sheppo
2651991Sheppo kctl_auxv_set_promif(&kav);
2661991Sheppo kmdb_init_promif(NULL, &kav);
2671991Sheppo }
2681991Sheppo
2691991Sheppo #endif
2701991Sheppo
2710Sstevel@tonic-gate /*ARGSUSED*/
2720Sstevel@tonic-gate void
kctl_auxv_fini_isadep(kmdb_auxv_t * auxv)2730Sstevel@tonic-gate kctl_auxv_fini_isadep(kmdb_auxv_t *auxv)
2740Sstevel@tonic-gate {
2750Sstevel@tonic-gate }
2760Sstevel@tonic-gate
2770Sstevel@tonic-gate void *
kctl_boot_tmpinit(void)2780Sstevel@tonic-gate kctl_boot_tmpinit(void)
2790Sstevel@tonic-gate {
2800Sstevel@tonic-gate kthread_t *kt0 = kobj_zalloc(sizeof (kthread_t), KM_TMP);
2810Sstevel@tonic-gate cpu_t *cpu = kobj_zalloc(sizeof (cpu_t), KM_TMP);
2820Sstevel@tonic-gate kt0->t_cpu = cpu;
2830Sstevel@tonic-gate
2840Sstevel@tonic-gate return (kctl_curthread_set(kt0));
2850Sstevel@tonic-gate }
2860Sstevel@tonic-gate
2870Sstevel@tonic-gate void
kctl_boot_tmpfini(void * old)2880Sstevel@tonic-gate kctl_boot_tmpfini(void *old)
2890Sstevel@tonic-gate {
2900Sstevel@tonic-gate (void) kctl_curthread_set(old);
2910Sstevel@tonic-gate }
292