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
|
/*
* Mach Operating System
* Copyright (c) 1993,1992,1991,1990 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.
*/
/*
* Interface to new debugger.
*/
#if MACH_KDB
#include <sys/reboot.h>
#include <vm/pmap.h>
#include <i386/thread.h>
#include <i386/db_machdep.h>
#include <i386/seg.h>
#include <i386/trap.h>
#include <i386/setjmp.h>
#include <i386/pmap.h>
#include <i386/proc_reg.h>
#include "gdt.h"
#include "trap.h"
#include "vm_param.h"
#include <vm/vm_map.h>
#include <kern/cpu_number.h>
#include <kern/printf.h>
#include <kern/thread.h>
#include <kern/task.h>
#include <ddb/db_access.h>
#include <ddb/db_command.h>
#include <ddb/db_output.h>
#include <ddb/db_run.h>
#include <ddb/db_task_thread.h>
#include <ddb/db_trap.h>
#include <machine/db_interface.h>
#include <machine/machspl.h>
struct i386_saved_state *i386_last_saved_statep;
struct i386_saved_state i386_nested_saved_state;
unsigned i386_last_kdb_sp;
extern thread_t db_default_thread;
/*
* Print trap reason.
*/
void
kdbprinttrap(type, code)
int type, code;
{
printf("kernel: %s (%d), code=%x\n",
trap_name(type), type, code);
}
/*
* kdb_trap - field a TRACE or BPT trap
*/
extern jmp_buf_t *db_recover;
spl_t saved_ipl[NCPUS]; /* just to know what was IPL before trap */
boolean_t
kdb_trap(
int type,
int code,
register struct i386_saved_state *regs)
{
spl_t s;
s = splhigh();
saved_ipl[cpu_number()] = s;
switch (type) {
case T_DEBUG: /* single_step */
{
extern int dr_addr[];
int addr;
int status = dr6();
if (status & 0xf) { /* hmm hdw break */
addr = status & 0x8 ? dr_addr[3] :
status & 0x4 ? dr_addr[2] :
status & 0x2 ? dr_addr[1] :
dr_addr[0];
regs->efl |= EFL_RF;
db_single_step_cmd(addr, 0, 1, "p");
}
}
case T_INT3: /* breakpoint */
case T_WATCHPOINT: /* watchpoint */
case -1: /* keyboard interrupt */
break;
default:
if (db_recover) {
i386_nested_saved_state = *regs;
db_printf("Caught %s (%d), code = %x, pc = %x\n",
trap_name(type), type, code, regs->eip);
db_error("");
/*NOTREACHED*/
}
kdbprinttrap(type, code);
}
#if NCPUS > 1
if (db_enter())
#endif /* NCPUS > 1 */
{
i386_last_saved_statep = regs;
i386_last_kdb_sp = (unsigned) &type;
/* XXX Should switch to ddb`s own stack here. */
ddb_regs = *regs;
if ((regs->cs & 0x3) == KERNEL_RING) {
/*
* Kernel mode - esp and ss not saved
*/
ddb_regs.uesp = (int)®s->uesp; /* kernel stack pointer */
ddb_regs.ss = KERNEL_DS;
}
cnpollc(TRUE);
db_task_trap(type, code, (regs->cs & 0x3) != 0);
cnpollc(FALSE);
regs->eip = ddb_regs.eip;
regs->efl = ddb_regs.efl;
regs->eax = ddb_regs.eax;
regs->ecx = ddb_regs.ecx;
regs->edx = ddb_regs.edx;
regs->ebx = ddb_regs.ebx;
if ((regs->cs & 0x3) != KERNEL_RING) {
/*
* user mode - saved esp and ss valid
*/
regs->uesp = ddb_regs.uesp; /* user stack pointer */
regs->ss = ddb_regs.ss & 0xffff; /* user stack segment */
}
regs->ebp = ddb_regs.ebp;
regs->esi = ddb_regs.esi;
regs->edi = ddb_regs.edi;
regs->es = ddb_regs.es & 0xffff;
regs->cs = ddb_regs.cs & 0xffff;
regs->ds = ddb_regs.ds & 0xffff;
regs->fs = ddb_regs.fs & 0xffff;
regs->gs = ddb_regs.gs & 0xffff;
if ((type == T_INT3) &&
(db_get_task_value(regs->eip, BKPT_SIZE, FALSE, TASK_NULL)
== BKPT_INST))
regs->eip += BKPT_SIZE;
}
#if NCPUS > 1
db_leave();
#endif /* NCPUS > 1 */
splx(s);
return 1;
}
/*
* Enter KDB through a keyboard trap.
* We show the registers as of the keyboard 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
kdb_kentry(
struct int_regs *int_regs)
{
struct i386_interrupt_state *is = int_regs->is;
spl_t s = splhigh();
#if NCPUS > 1
if (db_enter())
#endif /* NCPUS > 1 */
{
if ((is->cs & 0x3) != KERNEL_RING) {
ddb_regs.uesp = ((int *)(is+1))[0];
ddb_regs.ss = ((int *)(is+1))[1];
}
else {
ddb_regs.ss = KERNEL_DS;
ddb_regs.uesp= (int)(is+1);
}
ddb_regs.efl = is->efl;
ddb_regs.cs = is->cs;
ddb_regs.eip = is->eip;
ddb_regs.eax = is->eax;
ddb_regs.ecx = is->ecx;
ddb_regs.edx = is->edx;
ddb_regs.ebx = int_regs->ebx;
ddb_regs.ebp = int_regs->ebp;
ddb_regs.esi = int_regs->esi;
ddb_regs.edi = int_regs->edi;
ddb_regs.ds = is->ds;
ddb_regs.es = is->es;
ddb_regs.fs = is->fs;
ddb_regs.gs = is->gs;
cnpollc(TRUE);
db_task_trap(-1, 0, (ddb_regs.cs & 0x3) != 0);
cnpollc(FALSE);
if ((ddb_regs.cs & 0x3) != KERNEL_RING) {
((int *)(is+1))[0] = ddb_regs.uesp;
((int *)(is+1))[1] = ddb_regs.ss & 0xffff;
}
is->efl = ddb_regs.efl;
is->cs = ddb_regs.cs & 0xffff;
is->eip = ddb_regs.eip;
is->eax = ddb_regs.eax;
is->ecx = ddb_regs.ecx;
is->edx = ddb_regs.edx;
int_regs->ebx = ddb_regs.ebx;
int_regs->ebp = ddb_regs.ebp;
int_regs->esi = ddb_regs.esi;
int_regs->edi = ddb_regs.edi;
is->ds = ddb_regs.ds & 0xffff;
is->es = ddb_regs.es & 0xffff;
is->fs = ddb_regs.fs & 0xffff;
is->gs = ddb_regs.gs & 0xffff;
}
#if NCPUS > 1
db_leave();
#endif /* NCPUS > 1 */
(void) splx(s);
}
boolean_t db_no_vm_fault = TRUE;
int
db_user_to_kernel_address(
task_t task,
vm_offset_t addr,
unsigned int *kaddr,
int flag)
{
register pt_entry_t *ptp;
boolean_t faulted = FALSE;
retry:
ptp = pmap_pte(task->map->pmap, addr);
if (ptp == PT_ENTRY_NULL || (*ptp & INTEL_PTE_VALID) == 0) {
if (!faulted && !db_no_vm_fault) {
kern_return_t err;
faulted = TRUE;
err = vm_fault( task->map,
trunc_page(addr),
VM_PROT_READ,
FALSE, FALSE, 0);
if (err == KERN_SUCCESS)
goto retry;
}
if (flag) {
db_printf("\nno memory is assigned to address %08x\n", addr);
db_error(0);
/* NOTREACHED */
}
return(-1);
}
*kaddr = (unsigned)ptetokv(*ptp) + (addr & (INTEL_PGBYTES-1));
return(0);
}
/*
* Read bytes from kernel address space for debugger.
*/
void
db_read_bytes(
vm_offset_t addr,
register int size,
register char *data,
task_t task)
{
register char *src;
register int n;
unsigned kern_addr;
src = (char *)addr;
if ((addr >= VM_MIN_KERNEL_ADDRESS && addr < VM_MAX_KERNEL_ADDRESS) || task == TASK_NULL) {
if (task == TASK_NULL)
task = db_current_task();
while (--size >= 0) {
if (addr < VM_MIN_KERNEL_ADDRESS && task == TASK_NULL) {
db_printf("\nbad address %x\n", addr);
db_error(0);
/* NOTREACHED */
}
addr++;
*data++ = *src++;
}
return;
}
while (size > 0) {
if (db_user_to_kernel_address(task, addr, &kern_addr, 1) < 0)
return;
src = (char *)kern_addr;
n = intel_trunc_page(addr+INTEL_PGBYTES) - addr;
if (n > size)
n = size;
size -= n;
addr += n;
while (--n >= 0)
*data++ = *src++;
}
}
/*
* Write bytes to kernel address space for debugger.
*/
void
db_write_bytes(
vm_offset_t addr,
register int size,
register char *data,
task_t task)
{
register char *dst;
register pt_entry_t *ptep0 = 0;
pt_entry_t oldmap0 = 0;
vm_offset_t addr1;
register pt_entry_t *ptep1 = 0;
pt_entry_t oldmap1 = 0;
extern char etext;
void db_write_bytes_user_space();
if ((addr < VM_MIN_KERNEL_ADDRESS) ^
((addr + size) <= VM_MIN_KERNEL_ADDRESS)) {
db_error("\ncannot write data into mixed space\n");
/* NOTREACHED */
}
if (addr < VM_MIN_KERNEL_ADDRESS) {
if (task) {
db_write_bytes_user_space(addr, size, data, task);
return;
} else if (db_current_task() == TASK_NULL) {
db_printf("\nbad address %x\n", addr);
db_error(0);
/* NOTREACHED */
}
}
if (addr >= VM_MIN_KERNEL_ADDRESS &&
addr <= (vm_offset_t)&etext)
{
ptep0 = pmap_pte(kernel_pmap, addr);
oldmap0 = *ptep0;
*ptep0 |= INTEL_PTE_WRITE;
addr1 = i386_trunc_page(addr + size - 1);
if (i386_trunc_page(addr) != addr1) {
/* data crosses a page boundary */
ptep1 = pmap_pte(kernel_pmap, addr1);
oldmap1 = *ptep1;
*ptep1 |= INTEL_PTE_WRITE;
}
flush_tlb();
}
dst = (char *)addr;
while (--size >= 0)
*dst++ = *data++;
if (ptep0) {
*ptep0 = oldmap0;
if (ptep1) {
*ptep1 = oldmap1;
}
flush_tlb();
}
}
void
db_write_bytes_user_space(
vm_offset_t addr,
register int size,
register char *data,
task_t task)
{
register char *dst;
register int n;
unsigned kern_addr;
while (size > 0) {
if (db_user_to_kernel_address(task, addr, &kern_addr, 1) < 0)
return;
dst = (char *)kern_addr;
n = intel_trunc_page(addr+INTEL_PGBYTES) - addr;
if (n > size)
n = size;
size -= n;
addr += n;
while (--n >= 0)
*dst++ = *data++;
}
}
boolean_t
db_check_access(
vm_offset_t addr,
register int size,
task_t task)
{
register n;
vm_offset_t kern_addr;
if (addr >= VM_MIN_KERNEL_ADDRESS) {
if (kernel_task == TASK_NULL)
return TRUE;
task = kernel_task;
} else if (task == TASK_NULL) {
if (current_thread() == THREAD_NULL)
return FALSE;
task = current_thread()->task;
}
while (size > 0) {
if (db_user_to_kernel_address(task, addr, &kern_addr, 0) < 0)
return FALSE;
n = intel_trunc_page(addr+INTEL_PGBYTES) - addr;
if (n > size)
n = size;
size -= n;
addr += n;
}
return TRUE;
}
boolean_t
db_phys_eq(
task_t task1,
vm_offset_t addr1,
task_t task2,
vm_offset_t addr2)
{
vm_offset_t kern_addr1, kern_addr2;
if (addr1 >= VM_MIN_KERNEL_ADDRESS || addr2 >= VM_MIN_KERNEL_ADDRESS)
return FALSE;
if ((addr1 & (INTEL_PGBYTES-1)) != (addr2 & (INTEL_PGBYTES-1)))
return FALSE;
if (task1 == TASK_NULL) {
if (current_thread() == THREAD_NULL)
return FALSE;
task1 = current_thread()->task;
}
if (db_user_to_kernel_address(task1, addr1, &kern_addr1, 0) < 0
|| db_user_to_kernel_address(task2, addr2, &kern_addr2, 0) < 0)
return FALSE;
return(kern_addr1 == kern_addr2);
}
#define DB_USER_STACK_ADDR (VM_MIN_KERNEL_ADDRESS)
#define DB_NAME_SEARCH_LIMIT (DB_USER_STACK_ADDR-(INTEL_PGBYTES*3))
static boolean_t
db_search_null(
task_t task,
vm_offset_t *svaddr,
vm_offset_t evaddr,
vm_offset_t *skaddr,
int flag)
{
register unsigned vaddr;
register unsigned *kaddr;
kaddr = (unsigned *)*skaddr;
for (vaddr = *svaddr; vaddr > evaddr; vaddr -= sizeof(unsigned)) {
if (vaddr % INTEL_PGBYTES == 0) {
vaddr -= sizeof(unsigned);
if (db_user_to_kernel_address(task, vaddr, skaddr, 0) < 0)
return FALSE;
kaddr = (vm_offset_t *)*skaddr;
} else {
vaddr -= sizeof(unsigned);
kaddr--;
}
if ((*kaddr == 0) ^ (flag == 0)) {
*svaddr = vaddr;
*skaddr = (unsigned)kaddr;
return TRUE;
}
}
return FALSE;
}
void
db_task_name(
task_t task)
{
register char *p;
register int n;
unsigned vaddr, kaddr;
vaddr = DB_USER_STACK_ADDR;
kaddr = 0;
/*
* skip nulls at the end
*/
if (!db_search_null(task, &vaddr, DB_NAME_SEARCH_LIMIT, &kaddr, 0)) {
db_printf(DB_NULL_TASK_NAME);
return;
}
/*
* search start of args
*/
if (!db_search_null(task, &vaddr, DB_NAME_SEARCH_LIMIT, &kaddr, 1)) {
db_printf(DB_NULL_TASK_NAME);
return;
}
n = DB_TASK_NAME_LEN-1;
p = (char *)kaddr + sizeof(unsigned);
for (vaddr += sizeof(int); vaddr < DB_USER_STACK_ADDR && n > 0;
vaddr++, p++, n--) {
if (vaddr % INTEL_PGBYTES == 0) {
(void)db_user_to_kernel_address(task, vaddr, &kaddr, 0);
p = (char*)kaddr;
}
db_printf("%c", (*p < ' ' || *p > '~')? ' ': *p);
}
while (n-- >= 0) /* compare with >= 0 for one more space */
db_printf(" ");
}
#endif /* MACH_KDB */
|