xref: /illumos-gate/usr/src/uts/i86pc/os/mach_kdi.c (revision 86ef0a63)
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
5ae115bc7Smrj  * Common Development and Distribution License (the "License").
6ae115bc7Smrj  * 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  */
217c478bd9Sstevel@tonic-gate /*
22ae115bc7Smrj  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
24*74ecdb51SJohn Levon  *
25*74ecdb51SJohn Levon  * Copyright 2018 Joyent, Inc.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * Kernel/Debugger Interface (KDI) routines.  Called during debugger under
307c478bd9Sstevel@tonic-gate  * various system states (boot, while running, while the debugger has control).
317c478bd9Sstevel@tonic-gate  * Functions intended for use while the debugger has control may not grab any
327c478bd9Sstevel@tonic-gate  * locks or perform any functions that assume the availability of other system
337c478bd9Sstevel@tonic-gate  * services.
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include <sys/systm.h>
377c478bd9Sstevel@tonic-gate #include <sys/x86_archext.h>
387c478bd9Sstevel@tonic-gate #include <sys/kdi_impl.h>
397c478bd9Sstevel@tonic-gate #include <sys/smp_impldefs.h>
407c478bd9Sstevel@tonic-gate #include <sys/psm_types.h>
417c478bd9Sstevel@tonic-gate #include <sys/segments.h>
42ae115bc7Smrj #include <sys/archsystm.h>
43ae115bc7Smrj #include <sys/controlregs.h>
44ae115bc7Smrj #include <sys/trap.h>
45ae115bc7Smrj #include <sys/kobj.h>
46ae115bc7Smrj #include <sys/kobj_impl.h>
47ae115bc7Smrj #include <sys/mach_mmu.h>
487c478bd9Sstevel@tonic-gate 
49ae115bc7Smrj void
kdi_idt_write(gate_desc_t * gate,uint_t vec)50ae115bc7Smrj kdi_idt_write(gate_desc_t *gate, uint_t vec)
517c478bd9Sstevel@tonic-gate {
52ae115bc7Smrj 	gate_desc_t *idt = CPU->cpu_m.mcpu_idt;
537c478bd9Sstevel@tonic-gate 
54ae115bc7Smrj 	/*
55ae115bc7Smrj 	 * See kdi_idtr_set().
56ae115bc7Smrj 	 */
57ae115bc7Smrj 	if (idt == NULL) {
58ae115bc7Smrj 		desctbr_t idtr;
59ae115bc7Smrj 		rd_idtr(&idtr);
60ae115bc7Smrj 		idt = (gate_desc_t *)idtr.dtr_base;
61ae115bc7Smrj 	}
627c478bd9Sstevel@tonic-gate 
63ae115bc7Smrj 	idt[vec] = *gate;
64ae115bc7Smrj }
657c478bd9Sstevel@tonic-gate 
66ae115bc7Smrj ulong_t
kdi_dreg_get(int reg)67ae115bc7Smrj kdi_dreg_get(int reg)
68ae115bc7Smrj {
69ae115bc7Smrj 	switch (reg) {
70ae115bc7Smrj 	case 0:
71ae115bc7Smrj 		return (kdi_getdr0());
72ae115bc7Smrj 	case 1:
73ae115bc7Smrj 		return (kdi_getdr1());
74ae115bc7Smrj 	case 2:
75ae115bc7Smrj 		return (kdi_getdr2());
76ae115bc7Smrj 	case 3:
77ae115bc7Smrj 		return (kdi_getdr3());
78ae115bc7Smrj 	case 6:
79ae115bc7Smrj 		return (kdi_getdr6());
80ae115bc7Smrj 	case 7:
81ae115bc7Smrj 		return (kdi_getdr7());
82ae115bc7Smrj 	default:
83ae115bc7Smrj 		panic("invalid debug register dr%d", reg);
84ae115bc7Smrj 		/*NOTREACHED*/
85ae115bc7Smrj 	}
867c478bd9Sstevel@tonic-gate }
877c478bd9Sstevel@tonic-gate 
88ae115bc7Smrj void
kdi_dreg_set(int reg,ulong_t value)89ae115bc7Smrj kdi_dreg_set(int reg, ulong_t value)
90ae115bc7Smrj {
91ae115bc7Smrj 	switch (reg) {
92ae115bc7Smrj 	case 0:
93ae115bc7Smrj 		kdi_setdr0(value);
94ae115bc7Smrj 		break;
95ae115bc7Smrj 	case 1:
96ae115bc7Smrj 		kdi_setdr1(value);
97ae115bc7Smrj 		break;
98ae115bc7Smrj 	case 2:
99ae115bc7Smrj 		kdi_setdr2(value);
100ae115bc7Smrj 		break;
101ae115bc7Smrj 	case 3:
102ae115bc7Smrj 		kdi_setdr3(value);
103ae115bc7Smrj 		break;
104ae115bc7Smrj 	case 6:
105ae115bc7Smrj 		kdi_setdr6(value);
106ae115bc7Smrj 		break;
107ae115bc7Smrj 	case 7:
108ae115bc7Smrj 		kdi_setdr7(value);
109ae115bc7Smrj 		break;
110ae115bc7Smrj 	default:
111ae115bc7Smrj 		panic("invalid debug register dr%d", reg);
112ae115bc7Smrj 		/*NOTREACHED*/
113ae115bc7Smrj 	}
1147c478bd9Sstevel@tonic-gate }
1157c478bd9Sstevel@tonic-gate 
116ae115bc7Smrj extern void kdi_slave_entry(void);
1177c478bd9Sstevel@tonic-gate 
118ae115bc7Smrj void
kdi_stop_slaves(int cpu,int doxc)119ae115bc7Smrj kdi_stop_slaves(int cpu, int doxc)
120ae115bc7Smrj {
121ae115bc7Smrj 	if (doxc)
122ae115bc7Smrj 		kdi_xc_others(cpu, kdi_slave_entry);
1237c478bd9Sstevel@tonic-gate }
1247c478bd9Sstevel@tonic-gate 
125ae115bc7Smrj /*
126ae115bc7Smrj  * On i86pc, slaves busy-loop, so we don't need to do anything here.
127ae115bc7Smrj  */
128ae115bc7Smrj void
kdi_start_slaves(void)129ae115bc7Smrj kdi_start_slaves(void)
1307c478bd9Sstevel@tonic-gate {
1317c478bd9Sstevel@tonic-gate }
1327c478bd9Sstevel@tonic-gate 
133ae115bc7Smrj void
kdi_slave_wait(void)134ae115bc7Smrj kdi_slave_wait(void)
1357c478bd9Sstevel@tonic-gate {
136ae115bc7Smrj }
137ae115bc7Smrj 
138ae115bc7Smrj /*
139ae115bc7Smrj  * Caution.
140ae115bc7Smrj  * These routines are called -extremely- early, during kmdb initialization.
141ae115bc7Smrj  *
142ae115bc7Smrj  * Many common kernel functions assume that %gs has been initialized,
143ae115bc7Smrj  * and fail horribly if it hasn't.  At this point, the boot code has
144ae115bc7Smrj  * reserved a descriptor for us (KMDBGS_SEL) in it's GDT; arrange for it
145ae115bc7Smrj  * to point at a dummy cpu_t, temporarily at least.
146ae115bc7Smrj  *
147ae115bc7Smrj  * Note that kmdb entry relies on the fake cpu_t having zero cpu_idt/cpu_id.
148ae115bc7Smrj  */
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 
151ae115bc7Smrj void *
boot_kdi_tmpinit(void)152ae115bc7Smrj boot_kdi_tmpinit(void)
1537c478bd9Sstevel@tonic-gate {
154ae115bc7Smrj 	cpu_t *cpu = kobj_zalloc(sizeof (*cpu), KM_TMP);
155ae115bc7Smrj 	uintptr_t old;
1567c478bd9Sstevel@tonic-gate 
157ae115bc7Smrj 	cpu->cpu_self = cpu;
1587c478bd9Sstevel@tonic-gate 
159ae115bc7Smrj 	old = (uintptr_t)rdmsr(MSR_AMD_GSBASE);
160ae115bc7Smrj 	wrmsr(MSR_AMD_GSBASE, (uint64_t)cpu);
161ae115bc7Smrj 	return ((void *)old);
1627c478bd9Sstevel@tonic-gate }
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate void
boot_kdi_tmpfini(void * old)165ae115bc7Smrj boot_kdi_tmpfini(void *old)
1667c478bd9Sstevel@tonic-gate {
167ae115bc7Smrj 	wrmsr(MSR_AMD_GSBASE, (uint64_t)old);
1687c478bd9Sstevel@tonic-gate }
169