]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
21362eb3 | 2 | * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. |
1c79356b | 3 | * |
8f6c56a5 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 5 | * |
8f6c56a5 A |
6 | * This file contains Original Code and/or Modifications of Original Code |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
14 | * | |
15 | * Please obtain a copy of the License at | |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
17 | * | |
18 | * The Original Code and all software distributed under the License are | |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. | |
23 | * Please see the License for the specific language governing rights and | |
8ad349bb | 24 | * limitations under the License. |
8f6c56a5 A |
25 | * |
26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ | |
1c79356b A |
27 | */ |
28 | /* | |
29 | * @OSF_COPYRIGHT@ | |
30 | */ | |
31 | /* | |
32 | * Mach Operating System | |
33 | * Copyright (c) 1991,1990 Carnegie Mellon University | |
34 | * All Rights Reserved. | |
35 | * | |
36 | * Permission to use, copy, modify and distribute this software and its | |
37 | * documentation is hereby granted, provided that both the copyright | |
38 | * notice and this permission notice appear in all copies of the | |
39 | * software, derivative works or modified versions, and any portions | |
40 | * thereof, and that both notices appear in supporting documentation. | |
41 | * | |
42 | * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" | |
43 | * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR | |
44 | * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. | |
45 | * | |
46 | * Carnegie Mellon requests users of this software to return to | |
47 | * | |
48 | * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU | |
49 | * School of Computer Science | |
50 | * Carnegie Mellon University | |
51 | * Pittsburgh PA 15213-3890 | |
52 | * | |
53 | * any improvements or extensions that they make and grant Carnegie Mellon | |
54 | * the rights to redistribute these changes. | |
55 | */ | |
56 | /* | |
57 | */ | |
58 | ||
59 | /* | |
60 | * Interface to new debugger. | |
61 | */ | |
1c79356b A |
62 | #include <platforms.h> |
63 | #include <time_stamp.h> | |
64 | #include <mach_mp_debug.h> | |
65 | #include <mach_ldebug.h> | |
66 | #include <kern/spl.h> | |
67 | #include <kern/cpu_number.h> | |
68 | #include <kern/kern_types.h> | |
69 | #include <kern/misc_protos.h> | |
70 | #include <vm/pmap.h> | |
71 | ||
72 | #include <i386/thread.h> | |
73 | #include <i386/db_machdep.h> | |
74 | #include <i386/seg.h> | |
75 | #include <i386/trap.h> | |
76 | #include <i386/setjmp.h> | |
77 | #include <i386/pmap.h> | |
78 | #include <i386/misc_protos.h> | |
79 | ||
80 | #include <mach/vm_param.h> | |
81 | #include <vm/vm_map.h> | |
82 | #include <kern/thread.h> | |
83 | #include <kern/task.h> | |
84 | ||
85 | #include <ddb/db_command.h> | |
86 | #include <ddb/db_task_thread.h> | |
87 | #include <ddb/db_run.h> | |
88 | #include <ddb/db_trap.h> | |
89 | #include <ddb/db_output.h> | |
90 | #include <ddb/db_access.h> | |
91 | #include <ddb/db_sym.h> | |
92 | #include <ddb/db_break.h> | |
93 | #include <ddb/db_watch.h> | |
94 | ||
95 | int db_active = 0; | |
21362eb3 A |
96 | struct i386_saved_state *i386_last_saved_statep; |
97 | struct i386_saved_state i386_nested_saved_state; | |
1c79356b A |
98 | unsigned i386_last_kdb_sp; |
99 | ||
91447636 A |
100 | extern thread_t db_default_act; |
101 | extern pt_entry_t *DMAP1; | |
102 | extern caddr_t DADDR1; | |
1c79356b A |
103 | |
104 | #if MACH_MP_DEBUG | |
105 | extern int masked_state_cnt[]; | |
106 | #endif /* MACH_MP_DEBUG */ | |
107 | ||
108 | /* | |
109 | * Enter KDB through a keyboard trap. | |
110 | * We show the registers as of the keyboard interrupt | |
111 | * instead of those at its call to KDB. | |
112 | */ | |
113 | struct int_regs { | |
114 | int gs; | |
115 | int fs; | |
116 | int edi; | |
117 | int esi; | |
118 | int ebp; | |
119 | int ebx; | |
21362eb3 | 120 | struct i386_interrupt_state *is; |
1c79356b A |
121 | }; |
122 | ||
123 | extern char * trap_type[]; | |
124 | extern int TRAP_TYPES; | |
125 | ||
126 | /* Forward */ | |
127 | ||
128 | extern void kdbprinttrap( | |
129 | int type, | |
130 | int code, | |
131 | int *pc, | |
132 | int sp); | |
133 | extern void kdb_kentry( | |
134 | struct int_regs *int_regs); | |
135 | extern int db_user_to_kernel_address( | |
136 | task_t task, | |
137 | vm_offset_t addr, | |
138 | unsigned *kaddr, | |
139 | int flag); | |
140 | extern void db_write_bytes_user_space( | |
141 | vm_offset_t addr, | |
142 | int size, | |
143 | char *data, | |
144 | task_t task); | |
145 | extern int db_search_null( | |
146 | task_t task, | |
147 | unsigned *svaddr, | |
148 | unsigned evaddr, | |
149 | unsigned *skaddr, | |
150 | int flag); | |
151 | extern int kdb_enter(int); | |
152 | extern void kdb_leave(void); | |
153 | extern void lock_kdb(void); | |
154 | extern void unlock_kdb(void); | |
155 | ||
156 | /* | |
157 | * kdb_trap - field a TRACE or BPT trap | |
158 | */ | |
159 | ||
160 | ||
161 | extern jmp_buf_t *db_recover; | |
1c79356b A |
162 | |
163 | /* | |
164 | * Translate the state saved in a task state segment into an | |
165 | * exception frame. Since we "know" we always want the state | |
166 | * in a ktss, we hard-wire that in, rather than indexing the gdt | |
167 | * with tss_sel to derive a pointer to the desired tss. | |
168 | */ | |
169 | void | |
170 | db_tss_to_frame( | |
171 | int tss_sel, | |
21362eb3 | 172 | struct i386_saved_state *regs) |
1c79356b A |
173 | { |
174 | extern struct i386_tss ktss; | |
175 | int mycpu = cpu_number(); | |
176 | struct i386_tss *tss; | |
177 | ||
91447636 | 178 | tss = cpu_datap(mycpu)->cpu_desc_index.cdi_ktss; /* XXX */ |
1c79356b A |
179 | |
180 | /* | |
181 | * ddb will overwrite whatever's in esp, so put esp0 elsewhere, too. | |
182 | */ | |
21362eb3 | 183 | regs->esp = tss->esp0; |
1c79356b A |
184 | regs->efl = tss->eflags; |
185 | regs->eip = tss->eip; | |
186 | regs->trapno = tss->ss0; /* XXX */ | |
187 | regs->err = tss->esp0; /* XXX */ | |
188 | regs->eax = tss->eax; | |
189 | regs->ecx = tss->ecx; | |
190 | regs->edx = tss->edx; | |
191 | regs->ebx = tss->ebx; | |
192 | regs->uesp = tss->esp; | |
193 | regs->ebp = tss->ebp; | |
194 | regs->esi = tss->esi; | |
195 | regs->edi = tss->edi; | |
196 | regs->es = tss->es; | |
197 | regs->ss = tss->ss; | |
198 | regs->cs = tss->cs; | |
199 | regs->ds = tss->ds; | |
200 | regs->fs = tss->fs; | |
201 | regs->gs = tss->gs; | |
202 | } | |
203 | ||
204 | /* | |
205 | * Compose a call to the debugger from the saved state in regs. (No | |
206 | * reason not to do this in C.) | |
207 | */ | |
208 | boolean_t | |
209 | db_trap_from_asm( | |
21362eb3 | 210 | struct i386_saved_state *regs) |
1c79356b A |
211 | { |
212 | int code; | |
213 | int type; | |
214 | ||
215 | type = regs->trapno; | |
216 | code = regs->err; | |
217 | return (kdb_trap(type, code, regs)); | |
218 | } | |
219 | ||
220 | int | |
221 | kdb_trap( | |
222 | int type, | |
223 | int code, | |
21362eb3 | 224 | struct i386_saved_state *regs) |
1c79356b A |
225 | { |
226 | extern char etext; | |
227 | boolean_t trap_from_user; | |
21362eb3 | 228 | spl_t s = splhigh(); |
5d5c5d0d | 229 | |
1c79356b A |
230 | switch (type) { |
231 | case T_DEBUG: /* single_step */ | |
232 | { | |
233 | extern int dr_addr[]; | |
234 | int addr; | |
235 | int status = dr6(); | |
236 | ||
237 | if (status & 0xf) { /* hmm hdw break */ | |
238 | addr = status & 0x8 ? dr_addr[3] : | |
239 | status & 0x4 ? dr_addr[2] : | |
240 | status & 0x2 ? dr_addr[1] : | |
241 | dr_addr[0]; | |
242 | regs->efl |= EFL_RF; | |
243 | db_single_step_cmd(addr, 0, 1, "p"); | |
244 | } | |
245 | } | |
246 | case T_INT3: /* breakpoint */ | |
247 | case T_WATCHPOINT: /* watchpoint */ | |
248 | case -1: /* keyboard interrupt */ | |
249 | break; | |
250 | ||
251 | default: | |
252 | if (db_recover) { | |
253 | i386_nested_saved_state = *regs; | |
254 | db_printf("Caught "); | |
255 | if (type < 0 || type > TRAP_TYPES) | |
256 | db_printf("type %d", type); | |
257 | else | |
258 | db_printf("%s", trap_type[type]); | |
259 | db_printf(" trap, code = %x, pc = %x\n", | |
260 | code, regs->eip); | |
261 | splx(s); | |
262 | db_error(""); | |
263 | /*NOTREACHED*/ | |
264 | } | |
265 | kdbprinttrap(type, code, (int *)®s->eip, regs->uesp); | |
266 | } | |
267 | ||
1c79356b | 268 | disable_preemption(); |
1c79356b | 269 | |
91447636 A |
270 | current_cpu_datap()->cpu_kdb_saved_ipl = s; |
271 | current_cpu_datap()->cpu_kdb_saved_state = regs; | |
1c79356b A |
272 | |
273 | i386_last_saved_statep = regs; | |
274 | i386_last_kdb_sp = (unsigned) &type; | |
275 | ||
1c79356b A |
276 | if (!kdb_enter(regs->eip)) |
277 | goto kdb_exit; | |
1c79356b A |
278 | |
279 | /* Should switch to kdb's own stack here. */ | |
280 | ||
281 | if (!IS_USER_TRAP(regs, &etext)) { | |
282 | bzero((char *)&ddb_regs, sizeof (ddb_regs)); | |
21362eb3 A |
283 | *(struct i386_saved_state_from_kernel *)&ddb_regs = |
284 | *(struct i386_saved_state_from_kernel *)regs; | |
1c79356b A |
285 | trap_from_user = FALSE; |
286 | } | |
287 | else { | |
288 | ddb_regs = *regs; | |
289 | trap_from_user = TRUE; | |
290 | } | |
291 | if (!trap_from_user) { | |
292 | /* | |
293 | * Kernel mode - esp and ss not saved | |
294 | */ | |
295 | ddb_regs.uesp = (int)®s->uesp; /* kernel stack pointer */ | |
296 | ddb_regs.ss = KERNEL_DS; | |
297 | } | |
298 | ||
299 | db_active++; | |
300 | db_task_trap(type, code, trap_from_user); | |
301 | db_active--; | |
302 | ||
303 | regs->eip = ddb_regs.eip; | |
304 | regs->efl = ddb_regs.efl; | |
305 | regs->eax = ddb_regs.eax; | |
306 | regs->ecx = ddb_regs.ecx; | |
307 | regs->edx = ddb_regs.edx; | |
308 | regs->ebx = ddb_regs.ebx; | |
309 | if (trap_from_user) { | |
310 | /* | |
311 | * user mode - saved esp and ss valid | |
312 | */ | |
313 | regs->uesp = ddb_regs.uesp; /* user stack pointer */ | |
314 | regs->ss = ddb_regs.ss & 0xffff; /* user stack segment */ | |
315 | } | |
316 | regs->ebp = ddb_regs.ebp; | |
317 | regs->esi = ddb_regs.esi; | |
318 | regs->edi = ddb_regs.edi; | |
319 | regs->es = ddb_regs.es & 0xffff; | |
320 | regs->cs = ddb_regs.cs & 0xffff; | |
321 | regs->ds = ddb_regs.ds & 0xffff; | |
322 | regs->fs = ddb_regs.fs & 0xffff; | |
323 | regs->gs = ddb_regs.gs & 0xffff; | |
324 | ||
325 | if ((type == T_INT3) && | |
326 | (db_get_task_value(regs->eip, | |
327 | BKPT_SIZE, | |
328 | FALSE, | |
91447636 | 329 | db_target_space(current_thread(), |
1c79356b A |
330 | trap_from_user)) |
331 | == BKPT_INST)) | |
332 | regs->eip += BKPT_SIZE; | |
21362eb3 | 333 | |
1c79356b A |
334 | kdb_exit: |
335 | kdb_leave(); | |
1c79356b | 336 | |
91447636 | 337 | current_cpu_datap()->cpu_kdb_saved_state = 0; |
1c79356b | 338 | |
21362eb3 A |
339 | #if MACH_MP_DEBUG |
340 | current_cpu_datap()->cpu_masked_state_cnt = 0; | |
341 | #endif /* MACH_MP_DEBUG */ | |
342 | ||
1c79356b | 343 | enable_preemption(); |
1c79356b A |
344 | |
345 | splx(s); | |
346 | ||
347 | /* Allow continue to upper layers of exception handling if | |
348 | * trap was not a debugging trap. | |
349 | */ | |
350 | ||
351 | if (trap_from_user && type != T_DEBUG && type != T_INT3 | |
352 | && type != T_WATCHPOINT) | |
353 | return 0; | |
354 | else | |
355 | return (1); | |
356 | } | |
357 | ||
358 | /* | |
359 | * Enter KDB through a keyboard trap. | |
360 | * We show the registers as of the keyboard interrupt | |
361 | * instead of those at its call to KDB. | |
362 | */ | |
363 | ||
364 | spl_t kdb_oldspl; | |
365 | ||
366 | void | |
367 | kdb_kentry( | |
368 | struct int_regs *int_regs) | |
369 | { | |
370 | extern char etext; | |
371 | boolean_t trap_from_user; | |
21362eb3 A |
372 | struct i386_interrupt_state *is = int_regs->is; |
373 | struct i386_saved_state regs; | |
1c79356b A |
374 | spl_t s; |
375 | ||
376 | s = splhigh(); | |
377 | kdb_oldspl = s; | |
378 | ||
379 | if (IS_USER_TRAP(is, &etext)) | |
380 | { | |
381 | regs.uesp = ((int *)(is+1))[0]; | |
382 | regs.ss = ((int *)(is+1))[1]; | |
383 | } | |
384 | else { | |
385 | regs.ss = KERNEL_DS; | |
386 | regs.uesp= (int)(is+1); | |
387 | } | |
388 | regs.efl = is->efl; | |
389 | regs.cs = is->cs; | |
390 | regs.eip = is->eip; | |
391 | regs.eax = is->eax; | |
392 | regs.ecx = is->ecx; | |
393 | regs.edx = is->edx; | |
394 | regs.ebx = int_regs->ebx; | |
395 | regs.ebp = int_regs->ebp; | |
396 | regs.esi = int_regs->esi; | |
397 | regs.edi = int_regs->edi; | |
398 | regs.ds = is->ds; | |
399 | regs.es = is->es; | |
400 | regs.fs = int_regs->fs; | |
401 | regs.gs = int_regs->gs; | |
402 | ||
1c79356b | 403 | disable_preemption(); |
1c79356b | 404 | |
91447636 | 405 | current_cpu_datap()->cpu_kdb_saved_state = ®s; |
1c79356b | 406 | |
1c79356b A |
407 | if (!kdb_enter(regs.eip)) |
408 | goto kdb_exit; | |
1c79356b A |
409 | |
410 | bcopy((char *)®s, (char *)&ddb_regs, sizeof (ddb_regs)); | |
411 | trap_from_user = IS_USER_TRAP(&ddb_regs, &etext); | |
412 | ||
413 | db_active++; | |
414 | db_task_trap(-1, 0, trap_from_user); | |
415 | db_active--; | |
416 | ||
417 | if (trap_from_user) { | |
418 | ((int *)(is+1))[0] = ddb_regs.uesp; | |
419 | ((int *)(is+1))[1] = ddb_regs.ss & 0xffff; | |
420 | } | |
421 | is->efl = ddb_regs.efl; | |
422 | is->cs = ddb_regs.cs & 0xffff; | |
423 | is->eip = ddb_regs.eip; | |
424 | is->eax = ddb_regs.eax; | |
425 | is->ecx = ddb_regs.ecx; | |
426 | is->edx = ddb_regs.edx; | |
427 | int_regs->ebx = ddb_regs.ebx; | |
428 | int_regs->ebp = ddb_regs.ebp; | |
429 | int_regs->esi = ddb_regs.esi; | |
430 | int_regs->edi = ddb_regs.edi; | |
431 | is->ds = ddb_regs.ds & 0xffff; | |
432 | is->es = ddb_regs.es & 0xffff; | |
433 | int_regs->fs = ddb_regs.fs & 0xffff; | |
434 | int_regs->gs = ddb_regs.gs & 0xffff; | |
435 | ||
1c79356b A |
436 | kdb_exit: |
437 | kdb_leave(); | |
91447636 | 438 | current_cpu_datap()->cpu_kdb_saved_state = 0; |
1c79356b | 439 | |
1c79356b | 440 | enable_preemption(); |
1c79356b A |
441 | |
442 | splx(s); | |
443 | } | |
444 | ||
445 | /* | |
446 | * Print trap reason. | |
447 | */ | |
448 | ||
449 | void | |
450 | kdbprinttrap( | |
451 | int type, | |
452 | int code, | |
453 | int *pc, | |
454 | int sp) | |
455 | { | |
456 | printf("kernel: "); | |
457 | if (type < 0 || type > TRAP_TYPES) | |
458 | db_printf("type %d", type); | |
459 | else | |
460 | db_printf("%s", trap_type[type]); | |
461 | db_printf(" trap, code=%x eip@%x = %x esp=%x\n", | |
462 | code, pc, *(int *)pc, sp); | |
463 | db_run_mode = STEP_CONTINUE; | |
464 | } | |
465 | ||
466 | int | |
467 | db_user_to_kernel_address( | |
468 | task_t task, | |
469 | vm_offset_t addr, | |
470 | unsigned *kaddr, | |
471 | int flag) | |
472 | { | |
473 | register pt_entry_t *ptp; | |
474 | ||
21362eb3 | 475 | ptp = pmap_pte(task->map->pmap, addr); |
1c79356b A |
476 | if (ptp == PT_ENTRY_NULL || (*ptp & INTEL_PTE_VALID) == 0) { |
477 | if (flag) { | |
478 | db_printf("\nno memory is assigned to address %08x\n", addr); | |
479 | db_error(0); | |
480 | /* NOTREACHED */ | |
481 | } | |
482 | return(-1); | |
483 | } | |
89b3af67 | 484 | |
21362eb3 | 485 | src = (vm_offset_t)pte_to_pa(*ptp); |
91447636 A |
486 | *(int *) DMAP1 = INTEL_PTE_VALID | INTEL_PTE_RW | (src & PG_FRAME) | |
487 | INTEL_PTE_REF | INTEL_PTE_MOD; | |
488 | #if defined(I386_CPU) | |
489 | if (cpu_class == CPUCLASS_386) { | |
490 | invltlb(); | |
491 | } else | |
492 | #endif | |
493 | { | |
494 | invlpg((u_int)DADDR1); | |
495 | } | |
496 | ||
497 | *kaddr = (unsigned)DADDR1 + (addr & PAGE_MASK); | |
498 | ||
1c79356b A |
499 | return(0); |
500 | } | |
501 | ||
502 | /* | |
503 | * Read bytes from kernel address space for debugger. | |
504 | */ | |
505 | ||
506 | void | |
507 | db_read_bytes( | |
508 | vm_offset_t addr, | |
509 | int size, | |
510 | char *data, | |
511 | task_t task) | |
512 | { | |
513 | register char *src; | |
514 | register int n; | |
515 | unsigned kern_addr; | |
516 | ||
517 | src = (char *)addr; | |
518 | if (task == kernel_task || task == TASK_NULL) { | |
519 | while (--size >= 0) { | |
520 | if (addr++ > VM_MAX_KERNEL_ADDRESS) { | |
521 | db_printf("\nbad address %x\n", addr); | |
522 | db_error(0); | |
523 | /* NOTREACHED */ | |
524 | } | |
525 | *data++ = *src++; | |
526 | } | |
527 | return; | |
528 | } | |
529 | while (size > 0) { | |
530 | if (db_user_to_kernel_address(task, addr, &kern_addr, 1) < 0) | |
531 | return; | |
532 | src = (char *)kern_addr; | |
533 | n = intel_trunc_page(addr+INTEL_PGBYTES) - addr; | |
534 | if (n > size) | |
535 | n = size; | |
536 | size -= n; | |
537 | addr += n; | |
538 | while (--n >= 0) | |
539 | *data++ = *src++; | |
540 | } | |
541 | } | |
542 | ||
543 | /* | |
544 | * Write bytes to kernel address space for debugger. | |
545 | */ | |
546 | ||
547 | void | |
548 | db_write_bytes( | |
549 | vm_offset_t addr, | |
550 | int size, | |
551 | char *data, | |
552 | task_t task) | |
553 | { | |
554 | register char *dst; | |
555 | ||
556 | register pt_entry_t *ptep0 = 0; | |
557 | pt_entry_t oldmap0 = 0; | |
558 | vm_offset_t addr1; | |
559 | register pt_entry_t *ptep1 = 0; | |
560 | pt_entry_t oldmap1 = 0; | |
561 | extern char etext; | |
562 | ||
563 | if (task && task != kernel_task) { | |
564 | db_write_bytes_user_space(addr, size, data, task); | |
565 | return; | |
566 | } | |
567 | ||
568 | ||
569 | if (addr >= VM_MIN_KERNEL_LOADED_ADDRESS) { | |
570 | db_write_bytes_user_space(addr, size, data, kernel_task); | |
571 | return; | |
572 | } | |
573 | ||
574 | if (addr >= VM_MIN_KERNEL_ADDRESS && | |
575 | addr <= (vm_offset_t)&etext) | |
576 | { | |
21362eb3 | 577 | ptep0 = pmap_pte(kernel_pmap, addr); |
1c79356b A |
578 | oldmap0 = *ptep0; |
579 | *ptep0 |= INTEL_PTE_WRITE; | |
580 | ||
581 | addr1 = i386_trunc_page(addr + size - 1); | |
582 | if (i386_trunc_page(addr) != addr1) { | |
583 | /* data crosses a page boundary */ | |
584 | ||
21362eb3 | 585 | ptep1 = pmap_pte(kernel_pmap, addr1); |
1c79356b A |
586 | oldmap1 = *ptep1; |
587 | *ptep1 |= INTEL_PTE_WRITE; | |
588 | } | |
589 | flush_tlb(); | |
590 | } | |
591 | ||
592 | dst = (char *)addr; | |
593 | ||
594 | while (--size >= 0) { | |
595 | if (addr++ > VM_MAX_KERNEL_ADDRESS) { | |
596 | db_printf("\nbad address %x\n", addr); | |
597 | db_error(0); | |
598 | /* NOTREACHED */ | |
599 | } | |
600 | *dst++ = *data++; | |
601 | } | |
602 | ||
603 | if (ptep0) { | |
604 | *ptep0 = oldmap0; | |
605 | if (ptep1) { | |
606 | *ptep1 = oldmap1; | |
607 | } | |
608 | flush_tlb(); | |
609 | } | |
610 | } | |
611 | ||
612 | void | |
613 | db_write_bytes_user_space( | |
614 | vm_offset_t addr, | |
615 | int size, | |
616 | char *data, | |
617 | task_t task) | |
618 | { | |
619 | register char *dst; | |
620 | register int n; | |
621 | unsigned kern_addr; | |
622 | ||
623 | while (size > 0) { | |
624 | if (db_user_to_kernel_address(task, addr, &kern_addr, 1) < 0) | |
625 | return; | |
626 | dst = (char *)kern_addr; | |
627 | n = intel_trunc_page(addr+INTEL_PGBYTES) - addr; | |
628 | if (n > size) | |
629 | n = size; | |
630 | size -= n; | |
631 | addr += n; | |
632 | while (--n >= 0) | |
633 | *dst++ = *data++; | |
634 | } | |
635 | } | |
636 | ||
637 | boolean_t | |
638 | db_check_access( | |
639 | vm_offset_t addr, | |
640 | int size, | |
641 | task_t task) | |
642 | { | |
643 | register n; | |
644 | unsigned kern_addr; | |
645 | ||
646 | if (task == kernel_task || task == TASK_NULL) { | |
647 | if (kernel_task == TASK_NULL) | |
648 | return(TRUE); | |
649 | task = kernel_task; | |
650 | } else if (task == TASK_NULL) { | |
91447636 | 651 | if (current_thread() == THREAD_NULL) |
1c79356b | 652 | return(FALSE); |
91447636 | 653 | task = current_thread()->task; |
1c79356b A |
654 | } |
655 | while (size > 0) { | |
656 | if (db_user_to_kernel_address(task, addr, &kern_addr, 0) < 0) | |
657 | return(FALSE); | |
658 | n = intel_trunc_page(addr+INTEL_PGBYTES) - addr; | |
659 | if (n > size) | |
660 | n = size; | |
661 | size -= n; | |
662 | addr += n; | |
663 | } | |
664 | return(TRUE); | |
665 | } | |
666 | ||
667 | boolean_t | |
668 | db_phys_eq( | |
669 | task_t task1, | |
670 | vm_offset_t addr1, | |
671 | task_t task2, | |
672 | vm_offset_t addr2) | |
673 | { | |
674 | unsigned kern_addr1, kern_addr2; | |
675 | ||
676 | if ((addr1 & (INTEL_PGBYTES-1)) != (addr2 & (INTEL_PGBYTES-1))) | |
677 | return(FALSE); | |
678 | if (task1 == TASK_NULL) { | |
91447636 | 679 | if (current_thread() == THREAD_NULL) |
1c79356b | 680 | return(FALSE); |
91447636 | 681 | task1 = current_thread()->task; |
1c79356b A |
682 | } |
683 | if (db_user_to_kernel_address(task1, addr1, &kern_addr1, 0) < 0 || | |
684 | db_user_to_kernel_address(task2, addr2, &kern_addr2, 0) < 0) | |
685 | return(FALSE); | |
686 | return(kern_addr1 == kern_addr2); | |
687 | } | |
688 | ||
689 | #define DB_USER_STACK_ADDR (VM_MIN_KERNEL_ADDRESS) | |
690 | #define DB_NAME_SEARCH_LIMIT (DB_USER_STACK_ADDR-(INTEL_PGBYTES*3)) | |
691 | ||
692 | int | |
693 | db_search_null( | |
694 | task_t task, | |
695 | unsigned *svaddr, | |
696 | unsigned evaddr, | |
697 | unsigned *skaddr, | |
698 | int flag) | |
699 | { | |
700 | register unsigned vaddr; | |
701 | register unsigned *kaddr; | |
702 | ||
703 | kaddr = (unsigned *)*skaddr; | |
704 | for (vaddr = *svaddr; vaddr > evaddr; vaddr -= sizeof(unsigned)) { | |
705 | if (vaddr % INTEL_PGBYTES == 0) { | |
706 | vaddr -= sizeof(unsigned); | |
707 | if (db_user_to_kernel_address(task, vaddr, skaddr, 0) < 0) | |
708 | return(-1); | |
709 | kaddr = (unsigned *)*skaddr; | |
710 | } else { | |
711 | vaddr -= sizeof(unsigned); | |
712 | kaddr--; | |
713 | } | |
714 | if ((*kaddr == 0) ^ (flag == 0)) { | |
715 | *svaddr = vaddr; | |
716 | *skaddr = (unsigned)kaddr; | |
717 | return(0); | |
718 | } | |
719 | } | |
720 | return(-1); | |
721 | } | |
722 | ||
723 | void | |
724 | db_task_name( | |
725 | task_t task) | |
726 | { | |
727 | register char *p; | |
728 | register n; | |
729 | unsigned vaddr, kaddr; | |
730 | ||
731 | vaddr = DB_USER_STACK_ADDR; | |
732 | kaddr = 0; | |
733 | ||
734 | /* | |
735 | * skip nulls at the end | |
736 | */ | |
737 | if (db_search_null(task, &vaddr, DB_NAME_SEARCH_LIMIT, &kaddr, 0) < 0) { | |
738 | db_printf(DB_NULL_TASK_NAME); | |
739 | return; | |
740 | } | |
741 | /* | |
742 | * search start of args | |
743 | */ | |
744 | if (db_search_null(task, &vaddr, DB_NAME_SEARCH_LIMIT, &kaddr, 1) < 0) { | |
745 | db_printf(DB_NULL_TASK_NAME); | |
746 | return; | |
747 | } | |
748 | ||
749 | n = DB_TASK_NAME_LEN-1; | |
750 | p = (char *)kaddr + sizeof(unsigned); | |
751 | for (vaddr += sizeof(int); vaddr < DB_USER_STACK_ADDR && n > 0; | |
752 | vaddr++, p++, n--) { | |
753 | if (vaddr % INTEL_PGBYTES == 0) { | |
754 | (void)db_user_to_kernel_address(task, vaddr, &kaddr, 0); | |
755 | p = (char*)kaddr; | |
756 | } | |
757 | db_printf("%c", (*p < ' ' || *p > '~')? ' ': *p); | |
758 | } | |
759 | while (n-- >= 0) /* compare with >= 0 for one more space */ | |
760 | db_printf(" "); | |
761 | } | |
762 | ||
21362eb3 A |
763 | /* |
764 | * Code used to synchronize kdb among all cpus, one active at a time, switch | |
765 | * from on to another using kdb_on! #cpu or cpu #cpu | |
766 | */ | |
767 | ||
768 | decl_simple_lock_data(, kdb_lock) /* kdb lock */ | |
769 | ||
770 | #define db_simple_lock_init(l, e) hw_lock_init(&((l)->interlock)) | |
771 | #define db_simple_lock_try(l) hw_lock_try(&((l)->interlock)) | |
772 | #define db_simple_unlock(l) hw_lock_unlock(&((l)->interlock)) | |
773 | ||
774 | int kdb_cpu = -1; /* current cpu running kdb */ | |
775 | int kdb_debug = 0; | |
776 | volatile unsigned int cpus_holding_bkpts; /* counter for number of cpus holding | |
777 | breakpoints (ie: cpus that did not | |
778 | insert back breakpoints) */ | |
779 | extern boolean_t db_breakpoints_inserted; | |
780 | ||
1c79356b A |
781 | void |
782 | db_machdep_init(void) | |
783 | { | |
784 | int c; | |
785 | ||
91447636 A |
786 | db_simple_lock_init(&kdb_lock, 0); |
787 | for (c = 0; c < real_ncpus; ++c) { | |
21362eb3 A |
788 | db_stacks[c] = (vm_offset_t) (db_stack_store + |
789 | (INTSTACK_SIZE * (c + 1)) - sizeof (natural_t)); | |
1c79356b | 790 | if (c == master_cpu) { |
21362eb3 | 791 | dbtss.esp0 = (int)(db_task_stack_store + |
1c79356b | 792 | (INTSTACK_SIZE * (c + 1)) - sizeof (natural_t)); |
21362eb3 A |
793 | dbtss.esp = dbtss.esp0; |
794 | dbtss.eip = (int)&db_task_start; | |
1c79356b A |
795 | /* |
796 | * The TSS for the debugging task on each slave CPU | |
21362eb3 | 797 | * is set up in mp_desc_init(). |
1c79356b A |
798 | */ |
799 | } | |
800 | } | |
801 | } | |
802 | ||
803 | /* | |
804 | * Called when entering kdb: | |
805 | * Takes kdb lock. If if we were called remotely (slave state) we just | |
806 | * wait for kdb_cpu to be equal to cpu_number(). Otherwise enter kdb if | |
807 | * not active on another cpu. | |
808 | * If db_pass_thru[cpu_number()] > 0, then kdb can't stop now. | |
809 | */ | |
810 | ||
811 | int | |
812 | kdb_enter(int pc) | |
813 | { | |
21362eb3 | 814 | int mycpu; |
1c79356b A |
815 | int retval; |
816 | ||
1c79356b | 817 | disable_preemption(); |
1c79356b | 818 | |
21362eb3 | 819 | mycpu = cpu_number(); |
1c79356b | 820 | |
91447636 | 821 | if (current_cpu_datap()->cpu_db_pass_thru) { |
1c79356b A |
822 | retval = 0; |
823 | goto kdb_exit; | |
824 | } | |
825 | ||
91447636 | 826 | current_cpu_datap()->cpu_kdb_active++; |
1c79356b A |
827 | lock_kdb(); |
828 | ||
21362eb3 A |
829 | if (kdb_debug) |
830 | db_printf("kdb_enter: cpu %d, is_slave %d, kdb_cpu %d, run mode %d pc %x (%x) holds %d\n", | |
831 | my_cpu, current_cpu_datap()->cpu_kdb_is_slave, kdb_cpu, | |
832 | db_run_mode, pc, *(int *)pc, cpus_holding_bkpts); | |
1c79356b A |
833 | if (db_breakpoints_inserted) |
834 | cpus_holding_bkpts++; | |
91447636 | 835 | if (kdb_cpu == -1 && !current_cpu_datap()->cpu_kdb_is_slave) { |
1c79356b A |
836 | kdb_cpu = my_cpu; |
837 | remote_kdb(); /* stop other cpus */ | |
838 | retval = 1; | |
839 | } else if (kdb_cpu == my_cpu) | |
840 | retval = 1; | |
841 | else | |
842 | retval = 0; | |
843 | ||
844 | kdb_exit: | |
1c79356b | 845 | enable_preemption(); |
1c79356b A |
846 | |
847 | return (retval); | |
848 | } | |
849 | ||
850 | void | |
851 | kdb_leave(void) | |
852 | { | |
853 | int my_cpu; | |
854 | boolean_t wait = FALSE; | |
855 | ||
1c79356b | 856 | disable_preemption(); |
1c79356b A |
857 | |
858 | my_cpu = cpu_number(); | |
859 | ||
860 | if (db_run_mode == STEP_CONTINUE) { | |
861 | wait = TRUE; | |
862 | kdb_cpu = -1; | |
863 | } | |
864 | if (db_breakpoints_inserted) | |
865 | cpus_holding_bkpts--; | |
91447636 A |
866 | if (current_cpu_datap()->cpu_kdb_is_slave) |
867 | current_cpu_datap()->cpu_kdb_is_slave--; | |
1c79356b A |
868 | if (kdb_debug) |
869 | db_printf("kdb_leave: cpu %d, kdb_cpu %d, run_mode %d pc %x (%x) holds %d\n", | |
870 | my_cpu, kdb_cpu, db_run_mode, | |
871 | ddb_regs.eip, *(int *)ddb_regs.eip, | |
872 | cpus_holding_bkpts); | |
873 | clear_kdb_intr(); | |
874 | unlock_kdb(); | |
91447636 | 875 | current_cpu_datap()->cpu_kdb_active--; |
1c79356b | 876 | |
1c79356b | 877 | enable_preemption(); |
1c79356b A |
878 | |
879 | if (wait) { | |
880 | while(cpus_holding_bkpts); | |
881 | } | |
882 | } | |
883 | ||
884 | void | |
885 | lock_kdb(void) | |
886 | { | |
887 | int my_cpu; | |
888 | register i; | |
21362eb3 | 889 | extern void kdb_console(void); |
1c79356b | 890 | |
1c79356b | 891 | disable_preemption(); |
1c79356b A |
892 | |
893 | my_cpu = cpu_number(); | |
894 | ||
895 | for(;;) { | |
21362eb3 | 896 | kdb_console(); |
1c79356b A |
897 | if (kdb_cpu != -1 && kdb_cpu != my_cpu) { |
898 | continue; | |
899 | } | |
900 | if (db_simple_lock_try(&kdb_lock)) { | |
901 | if (kdb_cpu == -1 || kdb_cpu == my_cpu) | |
902 | break; | |
903 | db_simple_unlock(&kdb_lock); | |
904 | } | |
905 | } | |
906 | ||
1c79356b | 907 | enable_preemption(); |
1c79356b A |
908 | } |
909 | ||
910 | #if TIME_STAMP | |
911 | extern unsigned old_time_stamp; | |
912 | #endif /* TIME_STAMP */ | |
913 | ||
914 | void | |
915 | unlock_kdb(void) | |
916 | { | |
917 | db_simple_unlock(&kdb_lock); | |
918 | #if TIME_STAMP | |
919 | old_time_stamp = 0; | |
920 | #endif /* TIME_STAMP */ | |
921 | } | |
922 | ||
923 | ||
924 | #ifdef __STDC__ | |
925 | #define KDB_SAVE(type, name) extern type name; type name##_save = name | |
926 | #define KDB_RESTORE(name) name = name##_save | |
927 | #else /* __STDC__ */ | |
928 | #define KDB_SAVE(type, name) extern type name; type name/**/_save = name | |
929 | #define KDB_RESTORE(name) name = name/**/_save | |
930 | #endif /* __STDC__ */ | |
931 | ||
932 | #define KDB_SAVE_CTXT() \ | |
933 | KDB_SAVE(int, db_run_mode); \ | |
934 | KDB_SAVE(boolean_t, db_sstep_print); \ | |
935 | KDB_SAVE(int, db_loop_count); \ | |
936 | KDB_SAVE(int, db_call_depth); \ | |
937 | KDB_SAVE(int, db_inst_count); \ | |
938 | KDB_SAVE(int, db_last_inst_count); \ | |
939 | KDB_SAVE(int, db_load_count); \ | |
940 | KDB_SAVE(int, db_store_count); \ | |
941 | KDB_SAVE(boolean_t, db_cmd_loop_done); \ | |
942 | KDB_SAVE(jmp_buf_t *, db_recover); \ | |
943 | KDB_SAVE(db_addr_t, db_dot); \ | |
944 | KDB_SAVE(db_addr_t, db_last_addr); \ | |
945 | KDB_SAVE(db_addr_t, db_prev); \ | |
946 | KDB_SAVE(db_addr_t, db_next); \ | |
947 | KDB_SAVE(db_regs_t, ddb_regs); | |
948 | ||
949 | #define KDB_RESTORE_CTXT() \ | |
950 | KDB_RESTORE(db_run_mode); \ | |
951 | KDB_RESTORE(db_sstep_print); \ | |
952 | KDB_RESTORE(db_loop_count); \ | |
953 | KDB_RESTORE(db_call_depth); \ | |
954 | KDB_RESTORE(db_inst_count); \ | |
955 | KDB_RESTORE(db_last_inst_count); \ | |
956 | KDB_RESTORE(db_load_count); \ | |
957 | KDB_RESTORE(db_store_count); \ | |
958 | KDB_RESTORE(db_cmd_loop_done); \ | |
959 | KDB_RESTORE(db_recover); \ | |
960 | KDB_RESTORE(db_dot); \ | |
961 | KDB_RESTORE(db_last_addr); \ | |
962 | KDB_RESTORE(db_prev); \ | |
963 | KDB_RESTORE(db_next); \ | |
964 | KDB_RESTORE(ddb_regs); | |
965 | ||
966 | /* | |
967 | * switch to another cpu | |
968 | */ | |
969 | ||
970 | void | |
971 | kdb_on( | |
972 | int cpu) | |
973 | { | |
974 | KDB_SAVE_CTXT(); | |
91447636 | 975 | if (cpu < 0 || cpu >= real_ncpus || !cpu_datap(cpu)->cpu_kdb_active) |
1c79356b A |
976 | return; |
977 | db_set_breakpoints(); | |
978 | db_set_watchpoints(); | |
979 | kdb_cpu = cpu; | |
980 | unlock_kdb(); | |
981 | lock_kdb(); | |
982 | db_clear_breakpoints(); | |
983 | db_clear_watchpoints(); | |
984 | KDB_RESTORE_CTXT(); | |
985 | if (kdb_cpu == -1) {/* someone continued */ | |
986 | kdb_cpu = cpu_number(); | |
987 | db_continue_cmd(0, 0, 0, ""); | |
988 | } | |
989 | } | |
990 | ||
1c79356b A |
991 | void db_reboot( |
992 | db_expr_t addr, | |
993 | boolean_t have_addr, | |
994 | db_expr_t count, | |
995 | char *modif) | |
996 | { | |
21362eb3 A |
997 | boolean_t reboot = TRUE; |
998 | char *cp, c; | |
999 | ||
1000 | cp = modif; | |
1001 | while ((c = *cp++) != 0) { | |
1002 | if (c == 'r') /* reboot */ | |
1003 | reboot = TRUE; | |
1004 | if (c == 'h') /* halt */ | |
1005 | reboot = FALSE; | |
1006 | } | |
1007 | halt_all_cpus(reboot); | |
1c79356b | 1008 | } |