summaryrefslogtreecommitdiff
path: root/usr/src/uts/i86xpv/os/mach_kdi.c
blob: b12d30722d20ca13c87ddb93f9738833dd92adda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 * Kernel/Debugger Interface (KDI) routines.  Called during debugger under
 * various system states (boot, while running, while the debugger has control).
 * Functions intended for use while the debugger has control may not grab any
 * locks or perform any functions that assume the availability of other system
 * services.
 */

#include <sys/systm.h>
#include <sys/x86_archext.h>
#include <sys/kdi_impl.h>
#include <sys/smp_impldefs.h>
#include <sys/psm_types.h>
#include <sys/segments.h>
#include <sys/archsystm.h>
#include <sys/controlregs.h>
#include <sys/trap.h>
#include <sys/kobj.h>
#include <sys/kobj_impl.h>
#include <sys/hypervisor.h>
#include <sys/bootconf.h>
#include <sys/bootinfo.h>
#include <sys/promif.h>
#include <sys/evtchn_impl.h>
#include <sys/cpu.h>
#include <vm/kboot_mmu.h>
#include <vm/hat_pte.h>

static volatile int kdi_slaves_go;

/*
 * These are not safe against dropping into kmdb when fbt::: is active. This is
 * also broken on i86pc...
 */

void
kdi_idtr_write(desctbr_t *idtr)
{
	gate_desc_t *idt = (gate_desc_t *)idtr->dtr_base;
	uint_t nidt = (idtr->dtr_limit + 1) / sizeof (*idt);
	uint_t vec;

	for (vec = 0; vec < nidt; vec++, idt++)
		xen_idt_write(idt, vec);
}

void
kdi_idt_write(gate_desc_t *gate, uint_t vec)
{
	gate_desc_t *idt = CPU->cpu_m.mcpu_idt;

	/*
	 * See kdi_idtr_set().
	 */
	if (idt != NULL)
		idt[vec] = *gate;

	xen_idt_write(gate, vec);
}

ulong_t
kdi_dreg_get(int reg)
{
	return (__hypercall1(__HYPERVISOR_get_debugreg, (long)reg));
}

void
kdi_dreg_set(int reg, ulong_t value)
{
	(void) __hypercall2(__HYPERVISOR_set_debugreg, (long)reg, value);
}

void
kdi_flush_caches(void)
{
}

/*
 * To avoid domains sucking up CPU while sitting in kmdb, we make all the slave
 * CPUs wait for a wake-up evtchn.  The master CPU, meanwhile, sleeps for
 * console activity.
 */

extern void kdi_slave_entry(void);

void
kdi_stop_slaves(int cpu, int doxc)
{
	if (doxc)
		kdi_xc_others(cpu, kdi_slave_entry);
	kdi_slaves_go = 0;
}

void
kdi_start_slaves(void)
{
	int c;

	kdi_slaves_go = 1;

	for (c = 0; c < NCPU; c++) {
		if (cpu[c] == NULL || !(cpu[c]->cpu_flags & CPU_READY))
			continue;
		ec_try_ipi(XC_CPUPOKE_PIL, c);
	}
}

/*ARGSUSED*/
static int
check_slave(void *arg)
{
	return (kdi_slaves_go == 1);
}

void
kdi_slave_wait(void)
{
	if (!(cpu[CPU->cpu_id]->cpu_flags & CPU_READY))
		return;

	ec_wait_on_ipi(XC_CPUPOKE_PIL, check_slave, NULL);
}

/*
 * Caution.
 * These routines are called -extremely- early, during kmdb initialization.
 *
 * Many common kernel functions assume that %gs has been initialized,
 * and fail horribly if it hasn't.  At this point, the boot code has
 * reserved a descriptor for us (KMDBGS_SEL) in it's GDT; arrange for it
 * to point at a dummy cpu_t, temporarily at least.
 *
 * Note that kmdb entry relies on the fake cpu_t having zero cpu_idt/cpu_id.
 */

#if defined(__amd64)

void *
boot_kdi_tmpinit(void)
{
	cpu_t *cpu = kobj_zalloc(sizeof (*cpu), KM_TMP);
	user_desc_t *bgdt;
	uint64_t gdtpa;
	ulong_t ma[1];

	cpu->cpu_self = cpu;

	/*
	 * (Note that we had better switch to a -new- GDT before
	 * we discard the KM_TMP mappings, or disaster will ensue.)
	 */
	bgdt = kobj_zalloc(PAGESIZE, KM_TMP);
	ASSERT(((uintptr_t)bgdt & PAGEOFFSET) == 0);

	init_boot_gdt(bgdt);

	gdtpa = pfn_to_pa(va_to_pfn(bgdt));
	ma[0] = (ulong_t)(pa_to_ma(gdtpa) >> PAGESHIFT);
	kbm_read_only((uintptr_t)bgdt, gdtpa);
	if (HYPERVISOR_set_gdt(ma, PAGESIZE / sizeof (user_desc_t)))
		panic("boot_kdi_tmpinit:HYPERVISOR_set_gdt() failed");

	load_segment_registers(B64CODE_SEL, 0, 0, B32DATA_SEL);

	/*
	 * Now point %gsbase to our temp cpu structure.
	 */
	xen_set_segment_base(SEGBASE_GS_KERNEL, (ulong_t)cpu);
	return (0);
}

/*ARGSUSED*/
void
boot_kdi_tmpfini(void *old)
{
	/*
	 * This breaks, why do we need it anyway?
	 */
#if 0	/* XXPV */
	load_segment_registers(B64CODE_SEL, 0, KMDBGS_SEL, B32DATA_SEL);
#endif
}

#elif defined(__i386)

/*
 * Sigh.  We're called before we've initialized the kernels GDT, living
 * off the hypervisor's default GDT.  For kmdb's sake, we switch now to
 * a GDT that looks like dboot's GDT; very shortly we'll initialize and
 * switch to the kernel's GDT.
 */

void *
boot_kdi_tmpinit(void)
{
	cpu_t *cpu = kobj_zalloc(sizeof (*cpu), KM_TMP);
	user_desc_t *bgdt;
	uint64_t gdtpa;
	ulong_t ma[1];

	cpu->cpu_self = cpu;

	/*
	 * (Note that we had better switch to a -new- GDT before
	 * we discard the KM_TMP mappings, or disaster will ensue.)
	 */
	bgdt = kobj_zalloc(PAGESIZE, KM_TMP);

	ASSERT(((uintptr_t)bgdt & PAGEOFFSET) == 0);
	gdtpa = pfn_to_pa(va_to_pfn(bgdt));

	init_boot_gdt(bgdt);

	set_usegd(&bgdt[GDT_BGSTMP],
	    cpu, sizeof (*cpu), SDT_MEMRWA, SEL_KPL, SDP_BYTES, SDP_OP32);

	ma[0] = (ulong_t)(pa_to_ma(gdtpa) >> PAGESHIFT);
	kbm_read_only((uintptr_t)bgdt, gdtpa);
	if (HYPERVISOR_set_gdt(ma, PAGESIZE / sizeof (user_desc_t)))
		panic("boot_kdi_tmpinit:HYPERVISOR_set_gdt() failed");

	load_segment_registers(B32CODE_SEL, B32DATA_SEL, B32DATA_SEL, 0,
	    KMDBGS_SEL, B32DATA_SEL);
	return (0);
}

/*ARGSUSED*/
void
boot_kdi_tmpfini(void *old)
{
	load_segment_registers(B32CODE_SEL, B32DATA_SEL, B32DATA_SEL, 0,
	    0, B32DATA_SEL);
}

#endif	/* __i386 */