summaryrefslogtreecommitdiff
path: root/i386/i386/kttd_interface.c
blob: f48fe8ebde19e379d7d61a0f74e0a08082785f05 (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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/*
 * Mach Operating System
 * Copyright (c) 1993,1992 Carnegie Mellon University
 * All Rights Reserved.
 *
 * Permission to use, copy, modify and distribute this software and its
 * documentation is hereby granted, provided that both the copyright
 * notice and this permission notice appear in all copies of the
 * software, derivative works or modified versions, and any portions
 * thereof, and that both notices appear in supporting documentation.
 *
 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
 *
 * Carnegie Mellon requests users of this software to return to
 *
 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
 *  School of Computer Science
 *  Carnegie Mellon University
 *  Pittsburgh PA 15213-3890
 *
 * any improvements or extensions that they make and grant Carnegie Mellon
 * the rights to redistribute these changes.
 */

#if MACH_TTD

#include <sys/types.h>
#include <kern/printf.h>

#include <mach/machine/eflags.h>

#include <kern/thread.h>
#include <kern/processor.h>
#include <mach/thread_status.h>
#include <mach/vm_param.h>
#include <i386/seg.h>

#include <ttd/ttd_types.h>
#include <ttd/ttd_stub.h>
#include <machine/kttd_machdep.h>

/*
 * Shamelessly copied from the ddb sources:
 */
struct	 i386_saved_state *kttd_last_saved_statep;
struct	 i386_saved_state kttd_nested_saved_state;
unsigned last_kttd_sp;

struct i386_saved_state kttd_regs;	/* was ddb_regs */

extern int		kttd_debug;
extern boolean_t	kttd_enabled;
extern vm_offset_t	virtual_end;

#define	I386_BREAKPOINT	0xcc

/*
 *	kernel map
 */
extern vm_map_t	kernel_map;

boolean_t kttd_console_init(void)
{
	/*
	 * Get local machine's IP address via bootp.
	 */
	return(ttd_ip_bootp());
}

/*
 *	Execute a break instruction that will invoke ttd
 */
void kttd_break(void)
{
	if (!kttd_enabled)
		return;
	asm("int3");
}

/*
 * Halt all processors on the 386at (not really applicable).
 */
void kttd_halt_processors(void)
{
	/* XXX Fix for Sequent!!! */
	/* Only one on AT386, so ignore for now... */
}

/*
 * Determine whether or not the ehternet device driver supports
 * ttd.
 */
boolean_t kttd_supported(void)
{
	return ((int)ttd_get_packet != NULL);
}

/*
 * Return the ttd machine type for the i386at
 */
ttd_machine_type get_ttd_machine_type(void)
{
	return TTD_AT386;
}

void kttd_machine_getregs(struct i386_gdb_register_state *ttd_state)
{
	ttd_state->gs = kttd_regs.gs;
	ttd_state->fs = kttd_regs.fs;
	ttd_state->es = kttd_regs.es;
	ttd_state->ds = kttd_regs.ds;
	ttd_state->edi = kttd_regs.edi;
	ttd_state->esi = kttd_regs.esi;
	ttd_state->ebp = kttd_regs.ebp;

	/*
	 * This is set up to point to the right place in
	 * kttd_trap and .
	 */
	ttd_state->esp = kttd_regs.uesp;

	ttd_state->ebx = kttd_regs.ebx;
	ttd_state->edx = kttd_regs.edx;
	ttd_state->ecx = kttd_regs.ecx;
	ttd_state->eax = kttd_regs.eax;
	ttd_state->eip = kttd_regs.eip;
	ttd_state->cs = kttd_regs.cs;
	ttd_state->efl = kttd_regs.efl;
	ttd_state->ss = kttd_regs.ss;
}

void kttd_machine_setregs(struct i386_gdb_register_state *ttd_state)
{
	if (kttd_regs.gs != ttd_state->gs) {
		if (kttd_debug)
			printf("gs 0x%x:0x%x, ", kttd_regs.gs, ttd_state->gs);
		kttd_regs.gs = ttd_state->gs;
	}
	if (kttd_regs.fs != ttd_state->fs) {
		if (kttd_debug)
			printf("fs 0x%x:0x%x, ", kttd_regs.fs, ttd_state->fs);
		kttd_regs.fs = ttd_state->fs;
	}
	if (kttd_regs.es != ttd_state->es) {
		if (kttd_debug)
			printf("es 0x%x:0x%x, ", kttd_regs.es, ttd_state->es);
		kttd_regs.es = ttd_state->es;
	}
	if (kttd_regs.ds != ttd_state->ds) {
		if (kttd_debug)
			printf("ds 0x%x:0x%x, ", kttd_regs.ds, ttd_state->ds);
		kttd_regs.ds = ttd_state->ds;
	}
	if (kttd_regs.edi != ttd_state->edi) {
		if (kttd_debug)
			printf("edi 0x%x:0x%x, ", kttd_regs.edi, ttd_state->edi);
		kttd_regs.edi = ttd_state->edi;
	}
	if (kttd_regs.esi != ttd_state->esi) {
		if (kttd_debug)
			printf("esi 0x%x:0x%x, ", kttd_regs.esi, ttd_state->esi);
		kttd_regs.esi = ttd_state->esi;
	}
	if (kttd_regs.ebp != ttd_state->ebp) {
		if (kttd_debug)
			printf("ebp 0x%x:0x%x, ", kttd_regs.ebp, ttd_state->ebp);
		kttd_regs.ebp = ttd_state->ebp;
	}
	if (kttd_regs.ebx != ttd_state->ebx) {
		if (kttd_debug)
			printf("ebx 0x%x:0x%x, ", kttd_regs.ebx, ttd_state->ebx);
		kttd_regs.ebx = ttd_state->ebx;
	}
	if (kttd_regs.edx != ttd_state->edx) {
		if (kttd_debug)
			printf("edx 0x%x:0x%x, ", kttd_regs.edx, ttd_state->edx);
		kttd_regs.edx = ttd_state->edx;
	}
	if (kttd_regs.ecx != ttd_state->ecx) {
		if (kttd_debug)
			printf("ecx 0x%x:0x%x, ", kttd_regs.ecx, ttd_state->ecx);
		kttd_regs.ecx = ttd_state->ecx;
	}
	if (kttd_regs.eax != ttd_state->eax) {
		if (kttd_debug)
			printf("eax 0x%x:0x%x, ", kttd_regs.eax, ttd_state->eax);
		kttd_regs.eax = ttd_state->eax;
	}
	if (kttd_regs.eip != ttd_state->eip) {
		if (kttd_debug)
			printf("eip 0x%x:0x%x, ", kttd_regs.eip, ttd_state->eip);
		kttd_regs.eip = ttd_state->eip;
	}
	if (kttd_regs.cs != ttd_state->cs) {
		if (kttd_debug)
			printf("cs 0x%x:0x%x, ", kttd_regs.cs, ttd_state->cs);
		kttd_regs.cs = ttd_state->cs;
	}
	if (kttd_regs.efl != ttd_state->efl) {
		if (kttd_debug)
			printf("efl 0x%x:0x%x, ", kttd_regs.efl, ttd_state->efl);
		kttd_regs.efl = ttd_state->efl;
	}
#if	0
	/*
	 * We probably shouldn't mess with the uesp or the ss? XXX
	 */
	if (kttd_regs.ss != ttd_state->ss) {
		if (kttd_debug)
			printf("ss 0x%x:0x%x, ", kttd_regs.ss, ttd_state->ss);
		kttd_regs.ss = ttd_state->ss;
	}
#endif	/* 0 */

}

/*
 *	Enable a page for access, faulting it in if necessary
 */
boolean_t kttd_mem_access(vm_offset_t offset, vm_prot_t access)
{
	kern_return_t	code;

	/*
	 *	VM_MIN_KERNEL_ADDRESS if the beginning of equiv
	 *	mapped kernel memory.  virtual_end is the end.
	 *	If it's in between it's always accessible
	 */
	if (offset >= VM_MIN_KERNEL_ADDRESS && offset < virtual_end)
		return TRUE;

	if (offset >= virtual_end) {
		/*
		 *    	fault in the memory just to make sure we can access it
		 */
		if (kttd_debug)
			printf(">>>>>>>>>>Faulting in memory: 0x%x, 0x%x\n",
			       trunc_page(offset), access);
		code = vm_fault(kernel_map, trunc_page(offset), access, FALSE,
				FALSE, (void (*)()) 0);
	} else {
		/*
		 * Check for user thread
		 */
#if	1
		if ((current_thread() != THREAD_NULL) &&
		    (current_thread()->task->map->pmap != kernel_pmap) &&
		    (current_thread()->task->map->pmap != PMAP_NULL)) {
			code = vm_fault(current_thread()->task->map,
					trunc_page(offset), access, FALSE,
					FALSE, (void (*)()) 0);
		}else{
			/*
			 * Invalid kernel address (below VM_MIN_KERNEL_ADDRESS)
			 */
			return FALSE;
		}
#else
		if (kttd_debug)
			printf("==========Would've tried to map in user area 0x%x\n",
			       trunc_page(offset));
		return FALSE;
#endif	/* 0 */
	}

	return (code == KERN_SUCCESS);
}

/*
 *	See if we modified the kernel text and if so flush the caches.
 *	This routine is never called with a range that crosses a page
 *	boundary.
 */
void kttd_flush_cache(vm_offset_t offset, vm_size_t length)
{
	/* 386 doesn't need this */
	return;
}

/*
 * Insert a breakpoint into memory.
 */
boolean_t kttd_insert_breakpoint(vm_address_t address,
				 ttd_saved_inst *saved_inst)
{
	/*
	 * Saved old memory data:
	 */
	*saved_inst = *(unsigned char *)address;

	/*
	 * Put in a Breakpoint:
	 */
	*(unsigned char *)address = I386_BREAKPOINT;

	return TRUE;
}

/*
 * Remove breakpoint from memory.
 */
boolean_t kttd_remove_breakpoint(vm_address_t address,
				 ttd_saved_inst saved_inst)
{
	/*
	 * replace it:
	 */
	*(unsigned char *)address = (saved_inst & 0xff);

	return TRUE;
}

/*
 * Set single stepping mode.  Assumes that program counter is set
 * to the location where single stepping is to begin.  The 386 is
 * an easy single stepping machine, ie. built into the processor.
 */
boolean_t kttd_set_machine_single_step(void)
{
	/* Turn on Single Stepping */
	kttd_regs.efl |= EFL_TF;

	return TRUE;
}

/*
 * Clear single stepping mode.
 */
boolean_t kttd_clear_machine_single_step(void)
{
	/* Turn off the trace flag */
	kttd_regs.efl &= ~EFL_TF;

	return TRUE;
}


/*
 * kttd_type_to_ttdtrap:
 *
 * Fills in the task and thread info structures with the reason
 * for entering the Teledebugger (bp, single step, pg flt, etc.)
 *
 */
void kttd_type_to_ttdtrap(int type)
{
	/* XXX Fill this in sometime for i386 */
}

/*
 * kttd_trap:
 *
 *  This routine is called from the trap or interrupt handler when a
 * breakpoint instruction is encountered or a single step operation
 * completes. The argument is a pointer to a machine dependent
 * saved_state structure that was built on the interrupt or kernel stack.
 *
 */
boolean_t kttd_trap(int	type, int code, struct i386_saved_state *regs)
{
	int s;

	if (kttd_debug)
		printf("kttd_TRAP, before splhigh()\n");

	/*
	 * TTD isn't supported by the driver.
	 *
	 * Try to switch off to kdb if it is resident.
	 * Otherwise just hang (this might be panic).
	 *
	 * Check to make sure that TTD is supported.
	 * (Both by the machine's driver's, and bootp if using ether).
	 */
	if (!kttd_supported()) {
		kttd_enabled = FALSE;
		return FALSE;
	}

	s = splhigh();

	/*
	 * We are already in TTD!
	 */
	if (++kttd_active > MAX_KTTD_ACTIVE) {
		printf("kttd_trap: RE-ENTERED!!!\n");
	}

	if (kttd_debug)
		printf("kttd_TRAP, after splhigh()\n");

	/*  Should switch to kttd's own stack here. */

	kttd_regs = *regs;

	if ((regs->cs & 0x3) == KERNEL_RING) {
	    /*
	     * Kernel mode - esp and ss not saved
	     */
	    kttd_regs.uesp = (int)&regs->uesp;	/* kernel stack pointer */
	    kttd_regs.ss   = KERNEL_DS;
	}

	/*
	 * If this was not entered via an interrupt (type != -1)
	 * then we've entered via a bpt, single, etc. and must
	 * set the globals.
	 *
	 * Setup the kttd globals for entry....
	 */
	if (type != -1) {
		kttd_current_request = NULL;
		kttd_current_length = 0;
		kttd_current_kmsg = NULL;
		kttd_run_status = FULL_STOP;
	}else{
		/*
		 * We know that we can only get here if we did a kttd_intr
		 * since it's the way that we are called with type -1 (via
		 * the trampoline), so we don't have to worry about entering
		 * from Cntl-Alt-D like the mips does.
		 */
		/*
		 * Perform sanity check!
		 */
		if ((kttd_current_request == NULL) ||
		    (kttd_current_length == 0) ||
		    (kttd_current_kmsg == NULL) ||
		    (kttd_run_status != ONE_STOP)) {

			printf("kttd_trap: INSANITY!!!\n");
		}
	}

	kttd_task_trap(type, code, (regs->cs & 0x3) != 0);

	regs->eip    = kttd_regs.eip;
	regs->efl    = kttd_regs.efl;
	regs->eax    = kttd_regs.eax;
	regs->ecx    = kttd_regs.ecx;
	regs->edx    = kttd_regs.edx;
	regs->ebx    = kttd_regs.ebx;
	if ((regs->cs & 0x3) != KERNEL_RING) {
	    /*
	     * user mode - saved esp and ss valid
	     */
	    regs->uesp = kttd_regs.uesp;		/* user stack pointer */
	    regs->ss   = kttd_regs.ss & 0xffff;	/* user stack segment */
	}
	regs->ebp    = kttd_regs.ebp;
	regs->esi    = kttd_regs.esi;
	regs->edi    = kttd_regs.edi;
	regs->es     = kttd_regs.es & 0xffff;
	regs->cs     = kttd_regs.cs & 0xffff;
	regs->ds     = kttd_regs.ds & 0xffff;
	regs->fs     = kttd_regs.fs & 0xffff;
	regs->gs     = kttd_regs.gs & 0xffff;

	if (--kttd_active < MIN_KTTD_ACTIVE)
		printf("ttd_trap: kttd_active < 0\n");

	if (kttd_debug) {
		printf("Leaving kttd_trap, kttd_active = %d\n", kttd_active);
	}

	/*
	 * Only reset this if we entered kttd_trap via an async trampoline.
	 */
	if (type == -1) {
		if (kttd_run_status == RUNNING)
			printf("kttd_trap: $$$$$ run_status already RUNNING! $$$$$\n");
		kttd_run_status = RUNNING;
	}

	/* Is this right? XXX */
	kttd_run_status = RUNNING;

	(void) splx(s);

	/*
	 * Return true, that yes we handled the trap.
	 */
	return TRUE;
}

/*
 *	Enter KTTD through a network packet trap.
 *	We show the registers as of the network interrupt
 *	instead of those at its call to KDB.
 */
struct int_regs {
	int	edi;
	int	esi;
	int	ebp;
	int	ebx;
	struct i386_interrupt_state *is;
};

void
kttd_netentry(struct int_regs *int_regs)
{
	struct i386_interrupt_state *is = int_regs->is;
	int	s;

	if (kttd_debug)
		printf("kttd_NETENTRY before slphigh()\n");

	s = splhigh();

	if (kttd_debug)
		printf("kttd_NETENTRY after slphigh()\n");

	if ((is->cs & 0x3) != KERNEL_RING) {
	    /*
	     * Interrupted from User Space
	     */
	    kttd_regs.uesp = ((int *)(is+1))[0];
	    kttd_regs.ss   = ((int *)(is+1))[1];
	}
	else {
	    /*
	     * Interrupted from Kernel Space
	     */
	    kttd_regs.ss  = KERNEL_DS;
	    kttd_regs.uesp= (int)(is+1);
	}
	kttd_regs.efl = is->efl;
	kttd_regs.cs  = is->cs;
	kttd_regs.eip = is->eip;
	kttd_regs.eax = is->eax;
	kttd_regs.ecx = is->ecx;
	kttd_regs.edx = is->edx;
	kttd_regs.ebx = int_regs->ebx;
	kttd_regs.ebp = int_regs->ebp;
	kttd_regs.esi = int_regs->esi;
	kttd_regs.edi = int_regs->edi;
	kttd_regs.ds  = is->ds;
	kttd_regs.es  = is->es;
	kttd_regs.fs  = is->fs;
	kttd_regs.gs  = is->gs;

	kttd_active++;
	kttd_task_trap(-1, 0, (kttd_regs.cs & 0x3) != 0);
	kttd_active--;

	if ((kttd_regs.cs & 0x3) != KERNEL_RING) {
	    ((int *)(is+1))[0] = kttd_regs.uesp;
	    ((int *)(is+1))[1] = kttd_regs.ss & 0xffff;
	}
	is->efl = kttd_regs.efl;
	is->cs  = kttd_regs.cs & 0xffff;
	is->eip = kttd_regs.eip;
	is->eax = kttd_regs.eax;
	is->ecx = kttd_regs.ecx;
	is->edx = kttd_regs.edx;
	int_regs->ebx = kttd_regs.ebx;
	int_regs->ebp = kttd_regs.ebp;
	int_regs->esi = kttd_regs.esi;
	int_regs->edi = kttd_regs.edi;
	is->ds  = kttd_regs.ds & 0xffff;
	is->es  = kttd_regs.es & 0xffff;
	is->fs  = kttd_regs.fs & 0xffff;
	is->gs  = kttd_regs.gs & 0xffff;

	if (kttd_run_status == RUNNING)
		printf("kttd_netentry: %%%%% run_status already RUNNING! %%%%%\n");
	kttd_run_status = RUNNING;

	(void) splx(s);
}

#endif /* MACH_TTD */