17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 51ae08745Sheppo * Common Development and Distribution License (the "License"). 61ae08745Sheppo * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 211ae08745Sheppo 227c478bd9Sstevel@tonic-gate /* 23*ae115bc7Smrj * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate /* 307c478bd9Sstevel@tonic-gate * The debugger/PROM interface layer - debugger activation 317c478bd9Sstevel@tonic-gate */ 327c478bd9Sstevel@tonic-gate 337c478bd9Sstevel@tonic-gate #include <kmdb/kmdb_promif_isadep.h> 347c478bd9Sstevel@tonic-gate #include <kmdb/kmdb_start.h> 357c478bd9Sstevel@tonic-gate #include <kmdb/kmdb_kdi.h> 367c478bd9Sstevel@tonic-gate #include <kmdb/kmdb_asmutil.h> 377c478bd9Sstevel@tonic-gate #include <kmdb/kaif.h> 387c478bd9Sstevel@tonic-gate #include <mdb/mdb_debug.h> 397c478bd9Sstevel@tonic-gate #include <mdb/mdb_kreg.h> 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h> 427c478bd9Sstevel@tonic-gate #include <sys/kdi_impl.h> 437c478bd9Sstevel@tonic-gate #include <sys/machtrap.h> 447c478bd9Sstevel@tonic-gate 457c478bd9Sstevel@tonic-gate kaif_cpusave_t kaif_cb_save; 467c478bd9Sstevel@tonic-gate 477c478bd9Sstevel@tonic-gate static const char kaif_defer_word_tmpl[] = 487c478bd9Sstevel@tonic-gate /* 1 */ ": kmdb_callback " 497c478bd9Sstevel@tonic-gate 507c478bd9Sstevel@tonic-gate /* 517c478bd9Sstevel@tonic-gate * Don't hand control to the debugger if we're coming from OBP's text. 527c478bd9Sstevel@tonic-gate */ 537c478bd9Sstevel@tonic-gate 547c478bd9Sstevel@tonic-gate /* 2 */ " %%pc f000.0000 ffff.ffff between if exit then " 557c478bd9Sstevel@tonic-gate 567c478bd9Sstevel@tonic-gate /* 577c478bd9Sstevel@tonic-gate * Save registers 587c478bd9Sstevel@tonic-gate */ 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate /* 3 */ " %%pc h# %x x! " 617c478bd9Sstevel@tonic-gate /* 4 */ " %%npc h# %x x! " 627c478bd9Sstevel@tonic-gate /* 5 */ " %%g1 h# %x x! " 637c478bd9Sstevel@tonic-gate /* 6 */ " %%g2 h# %x x! " 647c478bd9Sstevel@tonic-gate /* 7 */ " %%g3 h# %x x! " 657c478bd9Sstevel@tonic-gate /* 8 */ " %%g4 h# %x x! " 667c478bd9Sstevel@tonic-gate /* 9 */ " %%g5 h# %x x! " 677c478bd9Sstevel@tonic-gate /* 10 */ " %%g6 h# %x x! " 687c478bd9Sstevel@tonic-gate /* 11 */ " %%g7 h# %x x! " 697c478bd9Sstevel@tonic-gate /* 12 */ " 1 %%tstate h# %x x! " 707c478bd9Sstevel@tonic-gate /* 13 */ " 1 %%tt h# %x x! " 717c478bd9Sstevel@tonic-gate /* 14 */ " %%tba h# %x x! " 727c478bd9Sstevel@tonic-gate /* 15 */ " h# %x set-pc " 737c478bd9Sstevel@tonic-gate /* 16 */ " go " 747c478bd9Sstevel@tonic-gate /* 17 */ "; "; 757c478bd9Sstevel@tonic-gate 767c478bd9Sstevel@tonic-gate /* 777c478bd9Sstevel@tonic-gate * Format the Forth word which tells the prom how to save state for 787c478bd9Sstevel@tonic-gate * giving control to us. 797c478bd9Sstevel@tonic-gate */ 807c478bd9Sstevel@tonic-gate static char * 817c478bd9Sstevel@tonic-gate kaif_format_word(void) 827c478bd9Sstevel@tonic-gate { 837c478bd9Sstevel@tonic-gate static char prom_str[550]; 847c478bd9Sstevel@tonic-gate kreg_t *kregs = kaif_cb_save.krs_gregs.kregs; 857c478bd9Sstevel@tonic-gate int len; 867c478bd9Sstevel@tonic-gate 877c478bd9Sstevel@tonic-gate len = mdb_snprintf(prom_str, sizeof (prom_str), kaif_defer_word_tmpl, 887c478bd9Sstevel@tonic-gate &kregs[KREG_PC], /* 3 */ 897c478bd9Sstevel@tonic-gate &kregs[KREG_NPC], /* 4 */ 907c478bd9Sstevel@tonic-gate &kregs[KREG_G1], /* 5 */ 917c478bd9Sstevel@tonic-gate &kregs[KREG_G2], /* 6 */ 927c478bd9Sstevel@tonic-gate &kregs[KREG_G3], /* 7 */ 937c478bd9Sstevel@tonic-gate &kregs[KREG_G4], /* 8 */ 947c478bd9Sstevel@tonic-gate &kregs[KREG_G5], /* 9 */ 957c478bd9Sstevel@tonic-gate &kregs[KREG_G6], /* 10 */ 967c478bd9Sstevel@tonic-gate &kregs[KREG_G7], /* 11 */ 977c478bd9Sstevel@tonic-gate &kaif_cb_save.krs_tstate, /* 12 */ 987c478bd9Sstevel@tonic-gate &kregs[KREG_TT], /* 13 */ 997c478bd9Sstevel@tonic-gate &kregs[KREG_TBA], /* 14 */ 1007c478bd9Sstevel@tonic-gate kaif_trap_obp); /* 15 */ 1017c478bd9Sstevel@tonic-gate 1027c478bd9Sstevel@tonic-gate ASSERT(len <= sizeof (prom_str)); 1037c478bd9Sstevel@tonic-gate 1047c478bd9Sstevel@tonic-gate return (prom_str); 1057c478bd9Sstevel@tonic-gate } 1067c478bd9Sstevel@tonic-gate 1077c478bd9Sstevel@tonic-gate static void 1087c478bd9Sstevel@tonic-gate kaif_prom_install(void) 1097c478bd9Sstevel@tonic-gate { 1107c478bd9Sstevel@tonic-gate kmdb_prom_interpret(kaif_format_word()); 1117c478bd9Sstevel@tonic-gate kmdb_prom_interpret(" ['] kmdb_callback init-debugger-hook "); 1127c478bd9Sstevel@tonic-gate } 1137c478bd9Sstevel@tonic-gate 1147c478bd9Sstevel@tonic-gate void 1157c478bd9Sstevel@tonic-gate kaif_prom_rearm(void) 1167c478bd9Sstevel@tonic-gate { 1177c478bd9Sstevel@tonic-gate kmdb_prom_interpret(" ['] kmdb_callback is debugger-hook "); 1187c478bd9Sstevel@tonic-gate } 1197c478bd9Sstevel@tonic-gate 1207c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 1217c478bd9Sstevel@tonic-gate static void 1227c478bd9Sstevel@tonic-gate kaif_cpu_init(cpu_t *cp) 1237c478bd9Sstevel@tonic-gate { 1247c478bd9Sstevel@tonic-gate kaif_wapt_set_regs(); 1257c478bd9Sstevel@tonic-gate } 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 1287c478bd9Sstevel@tonic-gate static void 1297c478bd9Sstevel@tonic-gate kaif_install_generic(caddr_t tgt, caddr_t arg) 1307c478bd9Sstevel@tonic-gate { 1317c478bd9Sstevel@tonic-gate bcopy((caddr_t)kaif_hdlr_generic, tgt, 32); 1327c478bd9Sstevel@tonic-gate } 1337c478bd9Sstevel@tonic-gate 1341ae08745Sheppo #ifdef sun4v 1351ae08745Sheppo 1361ae08745Sheppo /*ARGSUSED*/ 1371ae08745Sheppo static void 1381ae08745Sheppo kaif_install_goto_tt64(caddr_t tgt, caddr_t arg) 1391ae08745Sheppo { 1401ae08745Sheppo /* LINTED - pointer alignment */ 1411ae08745Sheppo uint32_t *hdlr = (uint32_t *)tgt; 1421ae08745Sheppo uint32_t disp = (T_FAST_INSTR_MMU_MISS - T_INSTR_MMU_MISS) * 0x20; 1431ae08745Sheppo 1441ae08745Sheppo *hdlr++ = 0x10480000 | (disp >> 2); /* ba,pt (to tt64) */ 1451ae08745Sheppo *hdlr++ = 0x01000000; /* nop */ 1461ae08745Sheppo } 1471ae08745Sheppo 1481ae08745Sheppo /*ARGSUSED*/ 1491ae08745Sheppo static void 1501ae08745Sheppo kaif_install_goto_tt68(caddr_t tgt, caddr_t arg) 1511ae08745Sheppo { 1521ae08745Sheppo /* LINTED - pointer alignment */ 1531ae08745Sheppo uint32_t *hdlr = (uint32_t *)tgt; 1541ae08745Sheppo uint32_t disp = (T_FAST_DATA_MMU_MISS - T_DATA_MMU_MISS) * 0x20; 1551ae08745Sheppo 1561ae08745Sheppo *hdlr++ = 0x10480000 | (disp >> 2); /* ba,pt (to tt68) */ 1571ae08745Sheppo *hdlr++ = 0x01000000; /* nop */ 1581ae08745Sheppo } 1591ae08745Sheppo 1601ae08745Sheppo #endif /* sun4v */ 1611ae08745Sheppo 1627c478bd9Sstevel@tonic-gate static void 1637c478bd9Sstevel@tonic-gate kaif_install_dmmumiss(caddr_t tgt, caddr_t vatotte) 1647c478bd9Sstevel@tonic-gate { 1657c478bd9Sstevel@tonic-gate uint32_t *patch; 1667c478bd9Sstevel@tonic-gate 1677c478bd9Sstevel@tonic-gate bcopy((caddr_t)kaif_hdlr_dmiss, tgt, 128); 1687c478bd9Sstevel@tonic-gate 1697c478bd9Sstevel@tonic-gate /* LINTED - pointer alignment */ 1707c478bd9Sstevel@tonic-gate patch = (uint32_t *)(tgt + ((uintptr_t)&kaif_hdlr_dmiss_patch - 1717c478bd9Sstevel@tonic-gate (uintptr_t)kaif_hdlr_dmiss)); 1727c478bd9Sstevel@tonic-gate *patch++ |= (uintptr_t)vatotte >> 10; 1737c478bd9Sstevel@tonic-gate *patch |= ((uintptr_t)vatotte) & 0x3ff; 1747c478bd9Sstevel@tonic-gate } 1757c478bd9Sstevel@tonic-gate 1767c478bd9Sstevel@tonic-gate static void 1777c478bd9Sstevel@tonic-gate kaif_install_immumiss(caddr_t tgt, caddr_t vatotte) 1787c478bd9Sstevel@tonic-gate { 1797c478bd9Sstevel@tonic-gate uint32_t *patch; 1807c478bd9Sstevel@tonic-gate 1817c478bd9Sstevel@tonic-gate bcopy((caddr_t)kaif_hdlr_imiss, tgt, 128); 1827c478bd9Sstevel@tonic-gate 1837c478bd9Sstevel@tonic-gate /* LINTED - pointer alignment */ 1847c478bd9Sstevel@tonic-gate patch = (uint32_t *)(tgt + ((uintptr_t)&kaif_hdlr_imiss_patch - 1857c478bd9Sstevel@tonic-gate (uintptr_t)kaif_hdlr_imiss)); 1867c478bd9Sstevel@tonic-gate *patch++ |= (uintptr_t)vatotte >> 10; 1877c478bd9Sstevel@tonic-gate *patch |= ((uintptr_t)vatotte) & 0x3ff; 1887c478bd9Sstevel@tonic-gate } 1897c478bd9Sstevel@tonic-gate 1907c478bd9Sstevel@tonic-gate static struct kaif_trap_handlers { 1917c478bd9Sstevel@tonic-gate uint_t th_tt; 1927c478bd9Sstevel@tonic-gate void (*th_install)(caddr_t, caddr_t); 1937c478bd9Sstevel@tonic-gate } kaif_trap_handlers[] = { 1947c478bd9Sstevel@tonic-gate { T_INSTR_EXCEPTION, kaif_install_generic }, 1951ae08745Sheppo #ifdef sun4v 1961ae08745Sheppo { T_INSTR_MMU_MISS, kaif_install_goto_tt64 }, 1971ae08745Sheppo #endif 1987c478bd9Sstevel@tonic-gate { T_IDIV0, kaif_install_generic }, 1997c478bd9Sstevel@tonic-gate { T_DATA_EXCEPTION, kaif_install_generic }, 2001ae08745Sheppo #ifdef sun4v 2011ae08745Sheppo { T_DATA_MMU_MISS, kaif_install_goto_tt68 }, 2021ae08745Sheppo #endif 2037c478bd9Sstevel@tonic-gate { T_DATA_ERROR, kaif_install_generic }, 2047c478bd9Sstevel@tonic-gate { T_ALIGNMENT, kaif_install_generic }, 2057c478bd9Sstevel@tonic-gate { T_FAST_INSTR_MMU_MISS, kaif_install_immumiss }, 2067c478bd9Sstevel@tonic-gate { T_FAST_DATA_MMU_MISS, kaif_install_dmmumiss }, 2077c478bd9Sstevel@tonic-gate { T_FAST_DATA_MMU_PROT, kaif_install_generic }, 2081ae08745Sheppo #ifdef sun4v 2091ae08745Sheppo { T_INSTR_MMU_MISS + T_TL1, kaif_install_goto_tt64 }, 2101ae08745Sheppo { T_DATA_MMU_MISS + T_TL1, kaif_install_goto_tt68 }, 2111ae08745Sheppo #endif 2127c478bd9Sstevel@tonic-gate { T_FAST_INSTR_MMU_MISS + T_TL1, kaif_install_immumiss }, 2137c478bd9Sstevel@tonic-gate { T_FAST_DATA_MMU_MISS + T_TL1, kaif_install_dmmumiss }, 2147c478bd9Sstevel@tonic-gate { 0 } 2157c478bd9Sstevel@tonic-gate }; 2167c478bd9Sstevel@tonic-gate 2177c478bd9Sstevel@tonic-gate static void 2187c478bd9Sstevel@tonic-gate kaif_trap_init(void) 2197c478bd9Sstevel@tonic-gate { 2207c478bd9Sstevel@tonic-gate caddr_t vatotte = kmdb_kdi_get_trap_vatotte(); 2217c478bd9Sstevel@tonic-gate uintptr_t brtgt; 2227c478bd9Sstevel@tonic-gate int i; 2237c478bd9Sstevel@tonic-gate 2247c478bd9Sstevel@tonic-gate /* 2251ae08745Sheppo * sun4u: 2267c478bd9Sstevel@tonic-gate * We rely upon OBP for the handling of a great many traps. As such, 2277c478bd9Sstevel@tonic-gate * we begin by populating our table with pointers to OBP's handlers. 2287c478bd9Sstevel@tonic-gate * We then copy in our own handlers where appropriate. At some point, 2297c478bd9Sstevel@tonic-gate * when we provide the bulk of the handlers, this process will be 2307c478bd9Sstevel@tonic-gate * reversed. 2311ae08745Sheppo * 2321ae08745Sheppo * sun4v: 2331ae08745Sheppo * The sun4v kernel dismisses OBP at boot. Both fast and slow TLB 2341ae08745Sheppo * misses are handled by KMDB. Breakpoint traps go directly KMDB. 2351ae08745Sheppo * All other trap entries are redirected to their respective 2361ae08745Sheppo * trap implemenation within the Solaris trap table. 2377c478bd9Sstevel@tonic-gate */ 2387c478bd9Sstevel@tonic-gate for (i = 0; i < kaif_tba_native_sz; i += 0x20) { 2397c478bd9Sstevel@tonic-gate /* LINTED - pointer alignment */ 2407c478bd9Sstevel@tonic-gate uint32_t *hdlr = (uint32_t *)(kaif_tba_native + i); 2417c478bd9Sstevel@tonic-gate #ifdef sun4v 2427c478bd9Sstevel@tonic-gate brtgt = (uintptr_t)(kaif_tba_kernel + i); 2431ae08745Sheppo #else 2447c478bd9Sstevel@tonic-gate brtgt = (uintptr_t)(kaif_tba_obp + i); 2451ae08745Sheppo #endif 2467c478bd9Sstevel@tonic-gate *hdlr++ = 0x03000000 | (brtgt >> 10); /* sethi brtgt, %g1 */ 2477c478bd9Sstevel@tonic-gate *hdlr++ = 0x81c06000 | (brtgt & 0x3ff); /* jmp %g1 + brtgt */ 2487c478bd9Sstevel@tonic-gate *hdlr++ = 0x01000000; /* nop */ 2497c478bd9Sstevel@tonic-gate } 2507c478bd9Sstevel@tonic-gate 2517c478bd9Sstevel@tonic-gate for (i = 0; kaif_trap_handlers[i].th_tt != 0; i++) { 2527c478bd9Sstevel@tonic-gate struct kaif_trap_handlers *th = &kaif_trap_handlers[i]; 2537c478bd9Sstevel@tonic-gate th->th_install(kaif_tba_native + th->th_tt * 0x20, vatotte); 2547c478bd9Sstevel@tonic-gate } 2557c478bd9Sstevel@tonic-gate membar_producer(); 2567c478bd9Sstevel@tonic-gate } 2577c478bd9Sstevel@tonic-gate 2587c478bd9Sstevel@tonic-gate /* 2597c478bd9Sstevel@tonic-gate * The kernel is ready for us to switch to our table (the HAT has been 2607c478bd9Sstevel@tonic-gate * initialized, the hments are walkable, and the trap table's pages 2617c478bd9Sstevel@tonic-gate * have been locked into the TLBs. 2627c478bd9Sstevel@tonic-gate */ 2637c478bd9Sstevel@tonic-gate static void 2647c478bd9Sstevel@tonic-gate kaif_vmready(void) 2657c478bd9Sstevel@tonic-gate { 2667c478bd9Sstevel@tonic-gate kaif_tba = kaif_tba_native; 2677c478bd9Sstevel@tonic-gate } 2687c478bd9Sstevel@tonic-gate 2697c478bd9Sstevel@tonic-gate /* 2707c478bd9Sstevel@tonic-gate * Called on the CPR master CPU. The driver has taken care of locking the 2717c478bd9Sstevel@tonic-gate * TLB entries. CPR restored the OBP image which contains kmdb_callback, 2727c478bd9Sstevel@tonic-gate * so there's nothing we need to do. This function should be removed entirely 2737c478bd9Sstevel@tonic-gate * in a future release. 2747c478bd9Sstevel@tonic-gate */ 2757c478bd9Sstevel@tonic-gate static void 2767c478bd9Sstevel@tonic-gate kaif_cpr_restart(void) 2777c478bd9Sstevel@tonic-gate { 2787c478bd9Sstevel@tonic-gate } 2797c478bd9Sstevel@tonic-gate 2807c478bd9Sstevel@tonic-gate static kdi_debugvec_t kaif_dvec = { 2817c478bd9Sstevel@tonic-gate NULL, /* dv_kctl_vmready */ 2827c478bd9Sstevel@tonic-gate NULL, /* dv_kctl_memavail */ 2837c478bd9Sstevel@tonic-gate NULL, /* dv_kctl_modavail */ 2847c478bd9Sstevel@tonic-gate NULL, /* dv_kctl_thravail */ 285*ae115bc7Smrj kaif_vmready, 286*ae115bc7Smrj NULL, /* dv_memavail */ 2877c478bd9Sstevel@tonic-gate kaif_mod_loaded, 288*ae115bc7Smrj kaif_mod_unloading, 289*ae115bc7Smrj NULL, /* dv_kctl_cpu_init */ 290*ae115bc7Smrj kaif_cpu_init, 291*ae115bc7Smrj kaif_cpr_restart 2927c478bd9Sstevel@tonic-gate }; 2937c478bd9Sstevel@tonic-gate 2947c478bd9Sstevel@tonic-gate /*ARGSUSED1*/ 2957c478bd9Sstevel@tonic-gate void 2967c478bd9Sstevel@tonic-gate kaif_activate(kdi_debugvec_t **dvecp, uint_t flags) 2977c478bd9Sstevel@tonic-gate { 2987c478bd9Sstevel@tonic-gate kaif_prom_install(); 2997c478bd9Sstevel@tonic-gate 3007c478bd9Sstevel@tonic-gate kaif_ktrap_install(0, kaif_ktrap); 3017c478bd9Sstevel@tonic-gate kaif_trap_init(); 3027c478bd9Sstevel@tonic-gate 3037c478bd9Sstevel@tonic-gate *dvecp = &kaif_dvec; 3047c478bd9Sstevel@tonic-gate } 3057c478bd9Sstevel@tonic-gate 3067c478bd9Sstevel@tonic-gate void 3077c478bd9Sstevel@tonic-gate kaif_deactivate(void) 3087c478bd9Sstevel@tonic-gate { 3097c478bd9Sstevel@tonic-gate kmdb_prom_interpret(" ['] noop is debugger-hook "); 3107c478bd9Sstevel@tonic-gate 3117c478bd9Sstevel@tonic-gate kaif_ktrap_restore(); 3127c478bd9Sstevel@tonic-gate } 313