* configure.in, dbxread.c, hppa-coredep.c, hppa-pinsn.c,
[deliverable/binutils-gdb.git] / gdb / hppahpux-tdep.c
1 /* Machine-dependent code which would otherwise be in inflow.c and core.c,
2 for GDB, the GNU debugger. This code is for the HP PA-RISC cpu.
3 Copyright (C) 1986, 1987, 1989, 1990, 1991 Free Software Foundation, Inc.
4
5 Contributed by the Center for Software Science at the
6 University of Utah (pa-gdb-bugs@cs.utah.edu).
7
8 This file is part of GDB.
9
10 GDB is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 1, or (at your option)
13 any later version.
14
15 GDB is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GDB; see the file COPYING. If not, write to
22 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
23
24 #include <stdio.h>
25 #include "defs.h"
26 #include "frame.h"
27 #include "inferior.h"
28 #include "value.h"
29
30 /* For argument passing to the inferior */
31 #include "symtab.h"
32
33 #ifdef USG
34 #include <sys/types.h>
35 #endif
36
37 #include <sys/param.h>
38 #include <sys/dir.h>
39 #include <signal.h>
40 #include <sys/ioctl.h>
41 /* #include <fcntl.h> Can we live without this? */
42
43 #ifdef COFF_ENCAPSULATE
44 #include "a.out.encap.h"
45 #else
46 #include <a.out.h>
47 #endif
48 #ifndef N_SET_MAGIC
49 #define N_SET_MAGIC(exec, val) ((exec).a_magic = (val))
50 #endif
51
52 /*#include <sys/user.h> After a.out.h */
53 #include <sys/file.h>
54 #include <sys/stat.h>
55 #include <sys/ptrace.h>
56 #include <machine/psl.h>
57
58 #ifdef KERNELDEBUG
59 #include <sys/vmmac.h>
60 #include <machine/machparam.h>
61 #include <machine/vmparam.h>
62 #include <machine/pde.h>
63 #include <machine/cpu.h>
64 #include <machine/iomod.h>
65 #include <machine/pcb.h>
66 #include <machine/rpb.h>
67 #include <ctype.h>
68
69 extern int kernel_debugging;
70 extern CORE_ADDR startup_file_start;
71 extern CORE_ADDR startup_file_end;
72
73 #define KERNOFF ((unsigned)KERNBASE)
74 #define INKERNEL(x) ((x) >= KERNOFF && (x) < KERNOFF + ctob(slr))
75
76 static int ok_to_cache();
77 static void set_kernel_boundaries();
78
79 int devmem = 0;
80 int vtophys_ready = 0;
81 int kerneltype;
82 #define OS_BSD 1
83 #define OS_MACH 2
84 #endif
85
86 #include "gdbcore.h"
87 #include "gdbcmd.h"
88
89 extern int errno;
90 \f
91
92
93
94
95
96 /* Last modification time of executable file.
97 Also used in source.c to compare against mtime of a source file. */
98
99 extern int exec_mtime;
100
101 /* Virtual addresses of bounds of the two areas of memory in the core file. */
102
103 /* extern CORE_ADDR data_start; */
104 extern CORE_ADDR data_end;
105 extern CORE_ADDR stack_start;
106 extern CORE_ADDR stack_end;
107
108 /* Virtual addresses of bounds of two areas of memory in the exec file.
109 Note that the data area in the exec file is used only when there is no core file. */
110
111 extern CORE_ADDR text_start;
112 extern CORE_ADDR text_end;
113
114 extern CORE_ADDR exec_data_start;
115 extern CORE_ADDR exec_data_end;
116
117 /* Address in executable file of start of text area data. */
118
119 extern int text_offset;
120
121 /* Address in executable file of start of data area data. */
122
123 extern int exec_data_offset;
124
125 /* Address in core file of start of data area data. */
126
127 extern int data_offset;
128
129 /* Address in core file of start of stack area data. */
130
131 extern int stack_offset;
132
133 struct header file_hdr;
134 struct som_exec_auxhdr exec_hdr;
135 \f
136 #ifdef KERNELDEBUG
137 /*
138 * Kernel debugging routines.
139 */
140
141 static struct pcb pcb;
142 static struct pde *pdir;
143 static struct hte *htbl;
144 static u_int npdir, nhtbl;
145
146 static CORE_ADDR
147 ksym_lookup(name)
148 char *name;
149 {
150 struct symbol *sym;
151 int i;
152
153 if ((i = lookup_misc_func(name)) < 0)
154 error("kernel symbol `%s' not found.", name);
155
156 return (misc_function_vector[i].address);
157 }
158
159 /*
160 * (re-)set the variables that tell "inside_entry_file" where to end
161 * a stack backtrace.
162 */
163 void
164 set_kernel_boundaries()
165 {
166 switch (kerneltype) {
167 case OS_MACH:
168 startup_file_start = ksym_lookup("$syscall");
169 startup_file_end = ksym_lookup("trap");
170 break;
171 case OS_BSD:
172 startup_file_start = ksym_lookup("syscallinit");
173 startup_file_end = ksym_lookup("$syscallexit");
174 break;
175 }
176 }
177
178 /*
179 * return true if 'len' bytes starting at 'addr' can be read out as
180 * longwords and/or locally cached (this is mostly for memory mapped
181 * i/o register access when debugging remote kernels).
182 */
183 static int
184 ok_to_cache(addr, len)
185 {
186 static CORE_ADDR ioptr;
187
188 if (! ioptr)
189 ioptr = ksym_lookup("ioptr");
190
191 if (addr >= ioptr && addr < SPA_HIGH)
192 return (0);
193
194 return (1);
195 }
196
197 static
198 physrd(addr, dat, len)
199 u_int addr;
200 char *dat;
201 {
202 if (lseek(corechan, addr, L_SET) == -1)
203 return (-1);
204 if (read(corechan, dat, len) != len)
205 return (-1);
206
207 return (0);
208 }
209
210 /*
211 * When looking at kernel data space through /dev/mem or with a core file, do
212 * virtual memory mapping.
213 */
214 static CORE_ADDR
215 vtophys(space, addr)
216 unsigned space;
217 CORE_ADDR addr;
218 {
219 struct pde *pptr;
220 u_int hindx, vpageno, ppageno;
221 CORE_ADDR phys = ~0;
222
223 if (!vtophys_ready) {
224 phys = addr; /* XXX for kvread */
225 } else if (kerneltype == OS_BSD) {
226 /* make offset into a virtual page no */
227 vpageno = btop(addr);
228 /*
229 * Determine index into hash table, initialize pptr to this
230 * entry (since first word of pte & hte are same), and set
231 * physical page number for first entry in chain.
232 */
233 hindx = pdirhash(space, addr) & (nhtbl-1);
234 pptr = (struct pde *) &htbl[hindx];
235 ppageno = pptr->pde_next;
236 while (1) {
237 if (pptr->pde_end)
238 break;
239 pptr = &pdir[ppageno];
240 /*
241 * If space id & virtual page number match, return
242 * "next PDIR entry of previous PDIR entry" as the
243 * physical page or'd with offset into page.
244 */
245 if (pptr->pde_space == space &&
246 pptr->pde_page == vpageno) {
247 phys = (CORE_ADDR) ((u_int)ptob(ppageno) |
248 (addr & PGOFSET));
249 break;
250 }
251 ppageno = pptr->pde_next;
252 }
253 }
254 #ifdef MACHKERNELDEBUG
255 else if (kerneltype == OS_MACH) {
256 (void) mach_vtophys(space, addr, &phys);
257 }
258 #endif
259 #if 0
260 printf("vtophys(%x.%x) -> %x\n", space, addr, phys);
261 #endif
262 return (phys);
263 }
264
265 static
266 kvread(addr)
267 CORE_ADDR addr;
268 {
269 CORE_ADDR paddr;
270
271 paddr = vtophys(0, addr);
272 if (paddr != ~0)
273 if (physrd(paddr, (char *)&addr, sizeof(addr)) == 0)
274 return (addr);
275
276 return (~0);
277 }
278
279 static void
280 read_pcb(addr)
281 u_int addr;
282 {
283 int i, off;
284 extern char registers[];
285 static int reg2pcb[] = {
286 /* RPB */
287 -1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
288 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
289 45, 52, 51, 75, 74, 49, 53, 54, 55, 56, -1, 70, 66, 67, 68, 69,
290 71, 72, 73, 34, 42, 43, 44, 46, 47, 58, 59, 60, -1, -1, -1, -1,
291 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
292 -1, -1, -1, -1,
293 /* BSD */
294 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
295 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
296 43, 64, 67, 68, 67, 47, 51, 52, 53, 54, -1, 35, 31, 32, 33, 34,
297 36, 37, 38, 39, 40, 41, 42, 44, 45, 56, 57, 58,102,103,104, -1,
298 70, 71, 72, 73, 74, 75, 76, 77, 78, 80, 82, 84, 86, 88, 90, 92,
299 94, 96, 98, 100,
300 /* Mach */
301 -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
302 14, 15, 16, -1, -1, -1, -1, -1, -1, -1, -1, 17, -1, -1, 18, -1,
303 25, -1, -1, -1, -1, 30, -1, -1, -1, -1, -1, 20, -1, -1, -1, 19,
304 21, 22, 23, 24, 26, 27, -1, 28, 29, -1, -1, -1, -1, -1, -1, -1,
305 34, 35, 36, 37, 38, 39, 40, 41, -1, -1, -1, -1, -1, -1, -1, -1,
306 42, 44, 46, 48
307 };
308 static struct rpb *rpbaddr = (struct rpb *) 0;
309 static u_int rpbpcbaddr = 0;
310
311 if (!remote_debugging) {
312 /*
313 * If we are debugging a post-mortem and this is the first
314 * call of read_pcb, read the RPB. Also assoicate the
315 * thread/proc running at the time with the RPB.
316 */
317 if (!devmem && rpbpcbaddr == 0) {
318 CORE_ADDR raddr = ksym_lookup("rpb");
319 int usepcb = 1;
320
321 if (raddr != ~0) {
322 rpbaddr = (struct rpb *) malloc(sizeof *rpbaddr);
323 if (!physrd(raddr, (char *)rpbaddr, sizeof *rpbaddr)) {
324 rpbpcbaddr = addr;
325 usepcb = 0;
326 }
327 }
328 if (usepcb) {
329 error("cannot read rpb, using pcb for registers\n");
330 if (rpbaddr)
331 free((char *)rpbaddr);
332 rpbpcbaddr = ~0;
333 }
334 }
335 if (physrd (addr, (char *)&pcb, sizeof pcb))
336 error ("cannot read pcb at %x.\n", addr);
337 } else {
338 if (remote_read_inferior_memory(addr, (char *)&pcb, sizeof pcb))
339 error ("cannot read pcb at %x.\n", addr);
340 }
341
342 if (kerneltype == OS_BSD) {
343 printf("p0br %lx p0lr %lx p1br %lx p1lr %lx\n",
344 pcb.pcb_p0br, pcb.pcb_p0lr, pcb.pcb_p1br, pcb.pcb_p1lr);
345 off = NUM_REGS;
346 } else {
347 printf("pcb %lx psw %lx ksp %lx\n",
348 addr, ((int *)&pcb)[31], ((int *)&pcb)[32]);
349 off = NUM_REGS * 2;
350 }
351 /*
352 * get the register values out of the sys pcb and
353 * store them where `read_register' will find them.
354 */
355 bzero(registers, REGISTER_BYTES);
356 for (i = 0; i < NUM_REGS; ++i)
357 if (reg2pcb[i+off] != -1)
358 supply_register(i, &((int *)&pcb)[reg2pcb[i+off]]);
359 /*
360 * If the RPB is valid for this thread/proc use the register values
361 * contained there.
362 */
363 if (addr == rpbpcbaddr) {
364 off = 0;
365 for (i = 0; i < NUM_REGS; ++i)
366 if (reg2pcb[i+off] != -1)
367 supply_register(i, &((int *)rpbaddr)[reg2pcb[i+off]]);
368 }
369 }
370
371 void
372 setup_kernel_debugging()
373 {
374 struct stat stb;
375 CORE_ADDR addr;
376
377 fstat(corechan, &stb);
378 devmem = 0;
379 if ((stb.st_mode & S_IFMT) == S_IFCHR && stb.st_rdev == makedev(2, 0))
380 devmem = 1;
381
382 /* XXX */
383 if (lookup_misc_func("Sysmap") < 0)
384 kerneltype = OS_MACH;
385 else
386 kerneltype = OS_BSD;
387
388 if (kerneltype == OS_BSD) {
389 int len, err = 0;
390
391 /*
392 * Hash table and PDIR are equivalently mapped
393 */
394 nhtbl = kvread(ksym_lookup("nhtbl"));
395 if (nhtbl != ~0) {
396 len = nhtbl * sizeof(*htbl);
397 htbl = (struct hte *) malloc(len);
398 if (htbl) {
399 addr = kvread(ksym_lookup("htbl"));
400 if (physrd(addr, (char *)htbl, len))
401 err++;
402 } else
403 err++;
404 } else
405 err++;
406 npdir = kvread(ksym_lookup("npdir"));
407 if (npdir != ~0) {
408 len = npdir * sizeof(*pdir);
409 pdir = (struct pde *) malloc(len);
410 if (pdir) {
411 addr = kvread(ksym_lookup("pdir"));
412 if (physrd(addr, (char *)pdir, len))
413 err++;
414 } else
415 err++;
416 } else
417 err++;
418 if (err) {
419 error("cannot read PDIR/HTBL");
420 return;
421 }
422 vtophys_ready = 1;
423
424 /*
425 * pcb where "panic" saved registers in first thing in
426 * current u-area. The current u-area is pointed to by
427 * "uptr".
428 */
429 addr = kvread(ksym_lookup("uptr"));
430 if (addr == ~0) {
431 error("cannot read current u-area address");
432 return;
433 }
434 read_pcb(vtophys(0, addr)); /* XXX space */
435 if (!devmem) {
436 /* find stack frame */
437 CORE_ADDR panicstr;
438 char buf[256];
439 register char *cp;
440
441 panicstr = kvread(ksym_lookup("panicstr"));
442 if (panicstr == ~0)
443 return;
444 (void) kernel_core_file_hook(panicstr, buf, sizeof(buf));
445 for (cp = buf; cp < &buf[sizeof(buf)] && *cp; cp++)
446 if (!isascii(*cp) || (!isprint(*cp) && !isspace(*cp)))
447 *cp = '?';
448 if (*cp)
449 *cp = '\0';
450 printf("panic: %s\n", buf);
451 }
452 }
453 #ifdef MACHKERNELDEBUG
454 else {
455 int *thread;
456
457 /*
458 * Set up address translation
459 */
460 if (mach_vtophys_init() == 0) {
461 error("cannot initialize vtophys for Mach");
462 return;
463 }
464 vtophys_ready = 1;
465
466 /*
467 * Locate active thread and read PCB
468 * XXX MAJOR HACK
469 * - assumes uni-processor
470 * - assumes position of pcb to avoid mach includes
471 */
472 thread = (int *)kvread(ksym_lookup("active_threads"));
473 addr = kvread(&thread[9]); /* XXX: pcb addr */
474 read_pcb(vtophys(0, addr));
475 }
476 #endif
477 }
478
479 vtop_command(arg)
480 char *arg;
481 {
482 u_int sp, off, pa;
483
484 if (!arg)
485 error_no_arg("kernel virtual address");
486 if (!kernel_debugging)
487 error("not debugging kernel");
488
489 sp = 0; /* XXX */
490 off = (u_int) parse_and_eval_address(arg);
491 pa = vtophys(sp, off);
492 printf("%lx.%lx -> ", sp, off);
493 if (pa == ~0)
494 printf("<invalid>\n");
495 else
496 printf("%lx\n", pa);
497 }
498
499 set_paddr_command(arg)
500 char *arg;
501 {
502 u_int addr;
503
504 if (!arg) {
505 if (kerneltype == OS_BSD)
506 error_no_arg("ps-style address for new process");
507 else
508 error_no_arg("thread structure virtual address");
509 }
510 if (!kernel_debugging)
511 error("not debugging kernel");
512
513 addr = (u_int) parse_and_eval_address(arg);
514 if (kerneltype == OS_BSD)
515 addr = ctob(addr);
516 else {
517 addr = kvread(&(((int *)addr)[9])); /* XXX: pcb addr */
518 addr = vtophys(0, addr); /* XXX space */
519 }
520 read_pcb(addr);
521
522 flush_cached_frames();
523 set_current_frame(create_new_frame(read_register(FP_REGNUM), read_pc()));
524 select_frame(get_current_frame(), 0);
525 }
526
527 /*
528 * read len bytes from kernel virtual address 'addr' into local
529 * buffer 'buf'. Return 0 if read ok, 1 otherwise. On read
530 * errors, portion of buffer not read is zeroed.
531 */
532 kernel_core_file_hook(addr, buf, len)
533 CORE_ADDR addr;
534 char *buf;
535 int len;
536 {
537 int i;
538 CORE_ADDR paddr;
539
540 while (len > 0) {
541 paddr = vtophys(0, addr); /* XXX space */
542 if (paddr == ~0) {
543 bzero(buf, len);
544 return (1);
545 }
546 /* we can't read across a page boundary */
547 i = min(len, NBPG - (addr & PGOFSET));
548 if (physrd(paddr, buf, i)) {
549 bzero(buf, len);
550 return (1);
551 }
552 buf += i;
553 addr += i;
554 len -= i;
555 }
556 return (0);
557 }
558 #endif
559
560
561 \f
562
563
564
565 /* Routines to extract various sized constants out of hppa
566 instructions. */
567
568 /* This assumes that no garbage lies outside of the lower bits of
569 value. */
570
571 int
572 sign_extend (val, bits)
573 unsigned val, bits;
574 {
575 return (int)(val >> bits - 1 ? (-1 << bits) | val : val);
576 }
577
578 /* For many immediate values the sign bit is the low bit! */
579
580 int
581 low_sign_extend (val, bits)
582 unsigned val, bits;
583 {
584 return (int)((val & 0x1 ? (-1 << (bits - 1)) : 0) | val >> 1);
585 }
586 /* extract the immediate field from a ld{bhw}s instruction */
587
588
589
590 unsigned
591 get_field (val, from, to)
592 unsigned val, from, to;
593 {
594 val = val >> 31 - to;
595 return val & ((1 << 32 - from) - 1);
596 }
597
598 unsigned
599 set_field (val, from, to, new_val)
600 unsigned *val, from, to;
601 {
602 unsigned mask = ~((1 << (to - from + 1)) << (31 - from));
603 return *val = *val & mask | (new_val << (31 - from));
604 }
605
606 /* extract a 3-bit space register number from a be, ble, mtsp or mfsp */
607
608 extract_3 (word)
609 unsigned word;
610 {
611 return GET_FIELD (word, 18, 18) << 2 | GET_FIELD (word, 16, 17);
612 }
613
614 extract_5_load (word)
615 unsigned word;
616 {
617 return low_sign_extend (word >> 16 & MASK_5, 5);
618 }
619
620 /* extract the immediate field from a st{bhw}s instruction */
621
622 int
623 extract_5_store (word)
624 unsigned word;
625 {
626 return low_sign_extend (word & MASK_5, 5);
627 }
628
629 /* extract an 11 bit immediate field */
630
631 int
632 extract_11 (word)
633 unsigned word;
634 {
635 return low_sign_extend (word & MASK_11, 11);
636 }
637
638 /* extract a 14 bit immediate field */
639
640 int
641 extract_14 (word)
642 unsigned word;
643 {
644 return low_sign_extend (word & MASK_14, 14);
645 }
646
647 /* deposit a 14 bit constant in a word */
648
649 unsigned
650 deposit_14 (opnd, word)
651 int opnd;
652 unsigned word;
653 {
654 unsigned sign = (opnd < 0 ? 1 : 0);
655
656 return word | ((unsigned)opnd << 1 & MASK_14) | sign;
657 }
658
659 /* extract a 21 bit constant */
660
661 int
662 extract_21 (word)
663 unsigned word;
664 {
665 int val;
666
667 word &= MASK_21;
668 word <<= 11;
669 val = GET_FIELD (word, 20, 20);
670 val <<= 11;
671 val |= GET_FIELD (word, 9, 19);
672 val <<= 2;
673 val |= GET_FIELD (word, 5, 6);
674 val <<= 5;
675 val |= GET_FIELD (word, 0, 4);
676 val <<= 2;
677 val |= GET_FIELD (word, 7, 8);
678 return sign_extend (val, 21) << 11;
679 }
680
681 /* deposit a 21 bit constant in a word. Although 21 bit constants are
682 usually the top 21 bits of a 32 bit constant, we assume that only
683 the low 21 bits of opnd are relevant */
684
685 unsigned
686 deposit_21 (opnd, word)
687 unsigned opnd, word;
688 {
689 unsigned val = 0;
690
691 val |= GET_FIELD (opnd, 11 + 14, 11 + 18);
692 val <<= 2;
693 val |= GET_FIELD (opnd, 11 + 12, 11 + 13);
694 val <<= 2;
695 val |= GET_FIELD (opnd, 11 + 19, 11 + 20);
696 val <<= 11;
697 val |= GET_FIELD (opnd, 11 + 1, 11 + 11);
698 val <<= 1;
699 val |= GET_FIELD (opnd, 11 + 0, 11 + 0);
700 return word | val;
701 }
702
703 /* extract a 12 bit constant from branch instructions */
704
705 int
706 extract_12 (word)
707 unsigned word;
708 {
709 return sign_extend (GET_FIELD (word, 19, 28) |
710 GET_FIELD (word, 29, 29) << 10 |
711 (word & 0x1) << 11, 12) << 2;
712 }
713
714 /* extract a 17 bit constant from branch instructions, returning the
715 19 bit signed value. */
716
717 int
718 extract_17 (word)
719 unsigned word;
720 {
721 return sign_extend (GET_FIELD (word, 19, 28) |
722 GET_FIELD (word, 29, 29) << 10 |
723 GET_FIELD (word, 11, 15) << 11 |
724 (word & 0x1) << 16, 17) << 2;
725 }
726
727
728 CORE_ADDR
729 frame_saved_pc (frame)
730 FRAME frame;
731 {
732 if (get_current_frame () == frame)
733 {
734 struct frame_saved_regs saved_regs;
735 CORE_ADDR pc = get_frame_pc (frame);
736
737 get_frame_saved_regs (frame, &saved_regs);
738 if (pc >= millicode_start && pc < millicode_end)
739 return read_register (31);
740 else if (saved_regs.regs[RP_REGNUM])
741 return read_memory_integer (saved_regs.regs[RP_REGNUM], 4);
742 else
743 return read_register (RP_REGNUM);
744 }
745 return read_memory_integer (frame->frame - 20, 4) & ~0x3;
746 }
747
748
749 /* To see if a frame chain is valid, see if the caller looks like it
750 was compiled with gcc. */
751
752 int frame_chain_valid (chain, thisframe)
753 FRAME_ADDR chain;
754 FRAME thisframe;
755 {
756 if (chain && (chain > 0x60000000
757 /* || remote_debugging -this is no longer used */
758 #ifdef KERNELDEBUG
759 || kernel_debugging
760 #endif
761 ))
762 {
763 CORE_ADDR pc = get_pc_function_start (FRAME_SAVED_PC (thisframe));
764
765 if (!inside_entry_file (pc))
766 return 0;
767 /* look for stw rp, -20(0,sp); copy 4,1; copy sp, 4 */
768 if (read_memory_integer (pc, 4) == 0x6BC23FD9)
769 pc = pc + 4;
770
771 if (read_memory_integer (pc, 4) == 0x8040241 &&
772 read_memory_integer (pc + 4, 4) == 0x81E0244)
773 return 1;
774 else
775 return 0;
776 }
777 else
778 return 0;
779 }
780
781 /* Some helper functions. gcc_p returns 1 if the function beginning at
782 pc appears to have been compiled with gcc. hpux_cc_p returns 1 if
783 fn was compiled with hpux cc. gcc functions look like :
784
785 stw rp,-0x14(sp) ; optional
786 or r4,r0,r1
787 or sp,r0,r4
788 stwm r1,framesize(sp)
789
790 hpux cc functions look like:
791
792 stw rp,-0x14(sp) ; optional.
793 stwm r3,framesiz(sp)
794 */
795
796 gcc_p (pc)
797 CORE_ADDR pc;
798 {
799 if (read_memory_integer (pc, 4) == 0x6BC23FD9)
800 pc = pc + 4;
801
802 if (read_memory_integer (pc, 4) == 0x8040241 &&
803 read_memory_integer (pc + 4, 4) == 0x81E0244)
804 return 1;
805 return 0;
806 }
807
808
809 find_dummy_frame_regs (frame, frame_saved_regs)
810 struct frame_info *frame;
811 struct frame_saved_regs *frame_saved_regs;
812 {
813 CORE_ADDR fp = frame->frame;
814 int i;
815
816 frame_saved_regs->regs[RP_REGNUM] = fp - 20 & ~0x3;
817 frame_saved_regs->regs[FP_REGNUM] = fp;
818 frame_saved_regs->regs[1] = fp + 8;
819 frame_saved_regs->regs[3] = fp + 12;
820 for (fp += 16, i = 3; i < 30; fp += 4, i++)
821 frame_saved_regs->regs[i] = fp;
822 frame_saved_regs->regs[31] = fp;
823 fp += 4;
824 for (i = FP0_REGNUM; i < NUM_REGS; i++, fp += 8)
825 frame_saved_regs->regs[i] = fp;
826 /* depend on last increment of fp */
827 frame_saved_regs->regs[IPSW_REGNUM] = fp - 4;
828 frame_saved_regs->regs[SAR_REGNUM] = fp;
829 fp += 4;
830 frame_saved_regs->regs[PCOQ_TAIL_REGNUM] = fp;
831 frame_saved_regs->regs[PCSQ_TAIL_REGNUM] = fp;
832 }
833
834 CORE_ADDR
835 hp_push_arguments (nargs, args, sp, struct_return, struct_addr)
836 int nargs;
837 value *args;
838 CORE_ADDR sp;
839 int struct_return;
840 CORE_ADDR struct_addr;
841 {
842 /* array of arguments' offsets */
843 int *offset = (int *)alloca(nargs);
844 int cum = 0;
845 int i, alignment;
846
847 for (i = 0; i < nargs; i++)
848 {
849 cum += TYPE_LENGTH (VALUE_TYPE (args[i]));
850 /* value must go at proper alignment. Assume alignment is a
851 power of two.*/
852 alignment = hp_alignof (VALUE_TYPE (args[i]));
853 if (cum % alignment)
854 cum = (cum + alignment) & -alignment;
855 offset[i] = -cum;
856 }
857 for (i == 0; i < nargs; i++)
858 {
859 write_memory (sp + offset[i], VALUE_CONTENTS (args[i]), sizeof(int));
860 }
861 sp += min ((cum + 7) & -8, 48);
862 if (struct_return)
863 write_register (28, struct_addr);
864 return sp + 48;
865 }
866
867 /* return the alignment of a type in bytes. Structures have the maximum
868 alignment required by their fields. */
869
870 int
871 hp_alignof (arg)
872 struct type *arg;
873 {
874 int max_align, align, i;
875 switch (TYPE_CODE (arg))
876 {
877 case TYPE_CODE_PTR:
878 case TYPE_CODE_INT:
879 case TYPE_CODE_FLT:
880 return TYPE_LENGTH (arg);
881 case TYPE_CODE_ARRAY:
882 return hp_alignof (TYPE_FIELD_TYPE (arg, 0));
883 case TYPE_CODE_STRUCT:
884 case TYPE_CODE_UNION:
885 max_align = 2;
886 for (i = 0; i < TYPE_NFIELDS (arg); i++)
887 {
888 /* Bit fields have no real alignment. */
889 if (!TYPE_FIELD_BITPOS (arg, i))
890 {
891 align = hp_alignof (TYPE_FIELD_TYPE (arg, i));
892 max_align = max (max_align, align);
893 }
894 }
895 return max_align;
896 default:
897 return 4;
898 }
899 }
900
901 /* Print the register regnum, or all registers if regnum is -1 */
902
903 pa_do_registers_info (regnum, fpregs)
904 int regnum;
905 int fpregs;
906 {
907 char raw_regs [REGISTER_BYTES];
908 int i;
909
910 for (i = 0; i < NUM_REGS; i++)
911 read_relative_register_raw_bytes (i, raw_regs + REGISTER_BYTE (i));
912 if (regnum = -1)
913 pa_print_registers (raw_regs, regnum);
914 else if (regnum < FP0_REGNUM)
915 {
916 printf ("%s %x\n", reg_names[regnum], *(long *)(raw_regs +
917 REGISTER_BYTE (regnum)));
918 }
919 else
920 pa_print_fp_reg (regnum);
921 }
922
923 pa_print_registers (raw_regs, regnum)
924 char *raw_regs;
925 int regnum;
926 {
927 int i;
928
929 for (i = 0; i < 18; i++)
930 printf ("%8.8s: %8x %8.8s: %8x %8.8s: %8x %8.8s: %8x\n",
931 reg_names[i],
932 *(int *)(raw_regs + REGISTER_BYTE (i)),
933 reg_names[i + 18],
934 *(int *)(raw_regs + REGISTER_BYTE (i + 18)),
935 reg_names[i + 36],
936 *(int *)(raw_regs + REGISTER_BYTE (i + 36)),
937 reg_names[i + 54],
938 *(int *)(raw_regs + REGISTER_BYTE (i + 54)));
939 for (i = 72; i < NUM_REGS; i++)
940 pa_print_fp_reg (i);
941 }
942
943 pa_print_fp_reg (i)
944 int i;
945 {
946 unsigned char raw_buffer[MAX_REGISTER_RAW_SIZE];
947 unsigned char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE];
948 REGISTER_TYPE val;
949
950 /* Get the data in raw format, then convert also to virtual format. */
951 read_relative_register_raw_bytes (i, raw_buffer);
952 REGISTER_CONVERT_TO_VIRTUAL (i, raw_buffer, virtual_buffer);
953
954 fputs_filtered (reg_names[i], stdout);
955 print_spaces_filtered (15 - strlen (reg_names[i]), stdout);
956
957 val_print (REGISTER_VIRTUAL_TYPE (i), virtual_buffer, 0, stdout, 0,
958 1, 0, Val_pretty_default);
959 printf_filtered ("\n");
960
961 }
962
963 /*
964 * Virtual to physical translation routines for Utah's Mach 3.0
965 */
966 #ifdef MACHKERNELDEBUG
967
968 #define STATIC
969
970 #if 0 /* too many includes to resolve, too much crap */
971 #include <kern/queue.h>
972 #include <vm/pmap.h>
973 #include <mach/vm_prot.h>
974 #else
975 /* queue.h */
976 struct queue_entry {
977 struct queue_entry *next; /* next element */
978 struct queue_entry *prev; /* previous element */
979 };
980
981 typedef struct queue_entry *queue_t;
982 typedef struct queue_entry queue_head_t;
983 typedef struct queue_entry queue_chain_t;
984 typedef struct queue_entry *queue_entry_t;
985
986 /* pmap.h */
987 #define HP800_HASHSIZE 1024
988 #define HP800_HASHSIZE_LOG2 10
989
990 #define pmap_hash(space, offset) \
991 (((unsigned) (space) << 5 ^ \
992 ((unsigned) (offset) >> 19 | (unsigned) (space) << 13) ^ \
993 (unsigned) (offset) >> 11) & (HP800_HASHSIZE-1))
994
995 struct mapping {
996 queue_head_t hash_link; /* hash table links */
997 queue_head_t phys_link; /* for mappings of a given PA */
998 space_t space; /* virtual space */
999 unsigned offset; /* virtual page number */
1000 unsigned tlbpage; /* physical page (for TLB load) */
1001 unsigned tlbprot; /* prot/access rights (for TLB load) */
1002 struct pmap *pmap; /* pmap mapping belongs to */
1003 };
1004
1005 struct phys_entry {
1006 queue_head_t phys_link; /* head of mappings of a given PA */
1007 struct mapping *writer; /* mapping with R/W access */
1008 unsigned tlbprot; /* TLB format protection */
1009 };
1010
1011 #endif
1012
1013 #define atop(a) ((unsigned)(a) >> 11)
1014 #define ptoa(p) ((unsigned)(p) << 11)
1015 #define trunc_page(a) ((unsigned)(a) & ~2047)
1016
1017 STATIC long equiv_end;
1018 STATIC queue_head_t *Ovtop_table, *vtop_table, *Ofree_mapping, free_mapping;
1019 STATIC struct phys_entry *Ophys_table, *phys_table;
1020 STATIC long vm_last_phys, vm_first_phys;
1021 STATIC struct mapping *firstmap, *lastmap, *Omap_table, *map_table;
1022 STATIC unsigned Omlow, Omhigh, Omhead, Ovlow, Ovhigh, Oplow, Ophigh;
1023 STATIC unsigned mlow, mhigh, mhead, vlow, vhigh, plow, phigh;
1024 STATIC int vtopsize, physsize, mapsize;
1025 STATIC int kmemfd;
1026
1027 #define IS_OVTOPPTR(p) ((unsigned)(p) >= Ovlow && (unsigned)(p) < Ovhigh)
1028 #define IS_OMAPPTR(p) ((unsigned)(p) >= Omlow && (unsigned)(p) < Omhigh)
1029 #define IS_OPHYSPTR(p) ((unsigned)(p) >= Oplow && (unsigned)(p) < Ophigh)
1030 #define IS_VTOPPTR(p) ((unsigned)(p) >= vlow && (unsigned)(p) < vhigh)
1031 #define IS_MAPPTR(p) ((unsigned)(p) >= mlow && (unsigned)(p) < mhigh)
1032 #define IS_PHYSPTR(p) ((unsigned)(p) >= plow && (unsigned)(p) < phigh)
1033
1034 struct mapstate {
1035 char unused;
1036 char flags;
1037 short hashix;
1038 short physix;
1039 } *mapstate;
1040
1041 /* flags */
1042 #define M_ISFREE 1
1043 #define M_ISHASH 2
1044 #define M_ISPHYS 4
1045
1046 mach_vtophys_init()
1047 {
1048 int errors = 0;
1049
1050 if (!readdata())
1051 errors++;
1052 if (!verifydata())
1053 errors++;
1054 if (!errors)
1055 return(1);
1056 fflush(stdout);
1057 fprintf(stderr,
1058 "translate: may not be able to translate all addresses\n");
1059 return(0);
1060 }
1061
1062 mach_vtophys(space, off, pa)
1063 unsigned space, off, *pa;
1064 {
1065 register int i;
1066 register queue_t qp;
1067 register struct mapping *mp;
1068 int poff;
1069
1070 /*
1071 * Kernel IO or equivilently mapped, one to one.
1072 */
1073 if (space == 0 && (long)off < equiv_end) {
1074 *pa = off;
1075 return(1);
1076 }
1077 /*
1078 * Else look it up in specified space
1079 */
1080 poff = off - trunc_page(off);
1081 off = trunc_page(off);
1082 qp = &vtop_table[pmap_hash(space, off)];
1083 for (mp = (struct mapping *)qp->next;
1084 qp != (queue_entry_t)mp;
1085 mp = (struct mapping *)mp->hash_link.next) {
1086 if (mp->space == space && mp->offset == off) {
1087 *pa = (mp->tlbpage << 7) | poff;
1088 return(1);
1089 }
1090 }
1091 return(0);
1092 }
1093
1094 STATIC
1095 readdata()
1096 {
1097 char *tmp, *mach_malloc();
1098 long size;
1099
1100 /* easy scalars */
1101 mach_read("equiv_end", ~0, (char *)&equiv_end, sizeof equiv_end);
1102 mach_read("vm_first_phys", ~0,
1103 (char *)&vm_first_phys, sizeof vm_first_phys);
1104 mach_read("vm_last_phys", ~0,
1105 (char *)&vm_last_phys, sizeof vm_last_phys);
1106 mach_read("firstmap", ~0, (char *)&firstmap, sizeof firstmap);
1107 mach_read("lastmap", ~0, (char *)&lastmap, sizeof lastmap);
1108
1109 /* virtual to physical hash table */
1110 vtopsize = HP800_HASHSIZE;
1111 size = vtopsize * sizeof(queue_head_t);
1112 tmp = mach_malloc("vtop table", size);
1113 mach_read("vtop_table", ~0, (char *)&Ovtop_table, sizeof Ovtop_table);
1114 mach_read("vtop table", (CORE_ADDR)Ovtop_table, tmp, size);
1115 vtop_table = (queue_head_t *) tmp;
1116
1117 /* inverted page table */
1118 physsize = atop(vm_last_phys - vm_first_phys);
1119 size = physsize * sizeof(struct phys_entry);
1120 tmp = mach_malloc("phys table", size);
1121 mach_read("phys_table", ~0, (char *)&Ophys_table, sizeof Ophys_table);
1122 mach_read("phys table", (CORE_ADDR)Ophys_table, tmp, size);
1123 phys_table = (struct phys_entry *) tmp;
1124
1125 /* mapping structures */
1126 Ofree_mapping = (queue_head_t *) ksym_lookup("free_mapping");
1127 mach_read("free mapping", (CORE_ADDR)Ofree_mapping,
1128 (char *) &free_mapping, sizeof free_mapping);
1129 Omap_table = firstmap;
1130 mapsize = lastmap - firstmap;
1131 size = mapsize * sizeof(struct mapping);
1132 tmp = mach_malloc("mapping table", size);
1133 mach_read("mapping table", (CORE_ADDR)Omap_table, tmp, size);
1134 map_table = (struct mapping *) tmp;
1135
1136 /* set limits */
1137 Ovlow = (unsigned) Ovtop_table;
1138 Ovhigh = (unsigned) &Ovtop_table[vtopsize];
1139 Oplow = (unsigned) Ophys_table;
1140 Ophigh = (unsigned) &Ophys_table[physsize];
1141 Omhead = (unsigned) Ofree_mapping;
1142 Omlow = (unsigned) firstmap;
1143 Omhigh = (unsigned) lastmap;
1144 mlow = (unsigned) map_table;
1145 mhigh = (unsigned) &map_table[mapsize];
1146 mhead = (unsigned) &free_mapping;
1147 vlow = (unsigned) vtop_table;
1148 vhigh = (unsigned) &vtop_table[vtopsize];
1149 plow = (unsigned) phys_table;
1150 phigh = (unsigned) &phys_table[physsize];
1151
1152 #if 0
1153 fprintf(stderr, "Ovtop [%#x-%#x) Ophys [%#x-%#x) Omap %#x [%#x-%#x)\n",
1154 Ovlow, Ovhigh, Oplow, Ophigh, Omhead, Omlow, Omhigh);
1155 fprintf(stderr, "vtop [%#x-%#x) phys [%#x-%#x) map %#x [%#x-%#x)\n",
1156 vlow, vhigh, plow, phigh, mhead, mlow, mhigh);
1157 #endif
1158 return(adjustdata());
1159 }
1160
1161 STATIC unsigned
1162 ptrcvt(ptr)
1163 unsigned ptr;
1164 {
1165 unsigned ret;
1166 char *str;
1167
1168 if (ptr == 0) {
1169 ret = ptr;
1170 str = "null";
1171 } else if (IS_OVTOPPTR(ptr)) {
1172 ret = vlow + (ptr - Ovlow);
1173 str = "vtop";
1174 } else if (IS_OPHYSPTR(ptr)) {
1175 ret = plow + (ptr - Oplow);
1176 str = "phys";
1177 } else if (IS_OMAPPTR(ptr)) {
1178 ret = mlow + (ptr - Omlow);
1179 str = "map";
1180 } else if (ptr == Omhead) {
1181 ret = mhead;
1182 str = "maphead";
1183 } else {
1184 error("bogus pointer %#x", ptr);
1185 str = "wild";
1186 ret = ptr;
1187 }
1188 #if 0
1189 fprintf(stderr, "%x (%s) -> %x\n", ptr, str, ret);
1190 #endif
1191 return(ret);
1192 }
1193
1194 STATIC int
1195 adjustdata()
1196 {
1197 register int i, lim;
1198 queue_head_t *nq;
1199 struct phys_entry *np;
1200 struct mapping *nm;
1201
1202 /* hash table */
1203 lim = vtopsize;
1204 for (nq = vtop_table; nq < &vtop_table[lim]; nq++) {
1205 nq->next = (queue_entry_t) ptrcvt((unsigned)nq->next);
1206 nq->prev = (queue_entry_t) ptrcvt((unsigned)nq->prev);
1207 }
1208
1209 /* IPT */
1210 lim = physsize;
1211 for (np = phys_table; np < &phys_table[lim]; np++) {
1212 np->phys_link.next = (queue_entry_t)
1213 ptrcvt((unsigned)np->phys_link.next);
1214 np->phys_link.prev = (queue_entry_t)
1215 ptrcvt((unsigned)np->phys_link.prev);
1216 np->writer = (struct mapping *) ptrcvt((unsigned)np->writer);
1217 }
1218
1219 /* mapping table */
1220 free_mapping.next = (queue_entry_t)ptrcvt((unsigned)free_mapping.next);
1221 free_mapping.prev = (queue_entry_t)ptrcvt((unsigned)free_mapping.prev);
1222 lim = mapsize;
1223 for (nm = map_table; nm < &map_table[lim]; nm++) {
1224 nm->hash_link.next = (queue_entry_t)
1225 ptrcvt((unsigned)nm->hash_link.next);
1226 nm->hash_link.prev = (queue_entry_t)
1227 ptrcvt((unsigned)nm->hash_link.prev);
1228 nm->phys_link.next = (queue_entry_t)
1229 ptrcvt((unsigned)nm->phys_link.next);
1230 nm->phys_link.prev = (queue_entry_t)
1231 ptrcvt((unsigned)nm->phys_link.prev);
1232 }
1233 return(1);
1234 }
1235
1236 /*
1237 * Consistency checks, make sure:
1238 *
1239 * 1. all mappings are accounted for
1240 * 2. no cycles
1241 * 3. no wild pointers
1242 * 4. consisent TLB state
1243 */
1244 STATIC int
1245 verifydata()
1246 {
1247 register struct mapstate *ms;
1248 register int i;
1249 int errors = 0;
1250
1251 mapstate = (struct mapstate *)
1252 mach_malloc("map state", mapsize * sizeof(struct mapstate));
1253 for (ms = mapstate; ms < &mapstate[mapsize]; ms++) {
1254 ms->flags = 0;
1255 ms->hashix = ms->physix = -2;
1256 }
1257
1258 /*
1259 * Check the free list
1260 */
1261 checkhashchain(&free_mapping, M_ISFREE, -1);
1262 /*
1263 * Check every hash chain
1264 */
1265 for (i = 0; i < vtopsize; i++)
1266 checkhashchain(&vtop_table[i], M_ISHASH, i);
1267 /*
1268 * Check every phys chain
1269 */
1270 for (i = 0; i < physsize; i++)
1271 checkphyschain(&phys_table[i].phys_link, M_ISPHYS, i);
1272 /*
1273 * Cycle through mapstate looking for anomolies
1274 */
1275 ms = mapstate;
1276 for (i = 0; i < mapsize; i++) {
1277 switch (ms->flags) {
1278 case M_ISFREE:
1279 case M_ISHASH|M_ISPHYS:
1280 break;
1281 case 0:
1282 merror(ms, "not found");
1283 errors++;
1284 break;
1285 case M_ISHASH:
1286 merror(ms, "in vtop but not phys");
1287 errors++;
1288 break;
1289 case M_ISPHYS:
1290 merror(ms, "in phys but not vtop");
1291 errors++;
1292 break;
1293 default:
1294 merror(ms, "totally bogus");
1295 errors++;
1296 break;
1297 }
1298 ms++;
1299 }
1300 return(errors ? 0 : 1);
1301 }
1302
1303 STATIC void
1304 checkhashchain(qhp, flag, ix)
1305 queue_entry_t qhp;
1306 {
1307 register queue_entry_t qp, pqp;
1308 register struct mapping *mp;
1309 struct mapstate *ms;
1310
1311 qp = qhp->next;
1312 /*
1313 * First element is not a mapping structure,
1314 * chain must be empty.
1315 */
1316 if (!IS_MAPPTR(qp)) {
1317 if (qp != qhp || qp != qhp->prev)
1318 fatal("bad vtop_table header pointer");
1319 } else {
1320 pqp = qhp;
1321 do {
1322 mp = (struct mapping *) qp;
1323 qp = &mp->hash_link;
1324 if (qp->prev != pqp)
1325 fatal("bad hash_link prev pointer");
1326 ms = &mapstate[mp-map_table];
1327 ms->flags |= flag;
1328 ms->hashix = ix;
1329 pqp = (queue_entry_t) mp;
1330 qp = qp->next;
1331 } while (IS_MAPPTR(qp));
1332 if (qp != qhp)
1333 fatal("bad hash_link next pointer");
1334 }
1335 }
1336
1337 STATIC void
1338 checkphyschain(qhp, flag, ix)
1339 queue_entry_t qhp;
1340 {
1341 register queue_entry_t qp, pqp;
1342 register struct mapping *mp;
1343 struct mapstate *ms;
1344
1345 qp = qhp->next;
1346 /*
1347 * First element is not a mapping structure,
1348 * chain must be empty.
1349 */
1350 if (!IS_MAPPTR(qp)) {
1351 if (qp != qhp || qp != qhp->prev)
1352 fatal("bad phys_table header pointer");
1353 } else {
1354 pqp = qhp;
1355 do {
1356 mp = (struct mapping *) qp;
1357 qp = &mp->phys_link;
1358 if (qp->prev != pqp)
1359 fatal("bad phys_link prev pointer");
1360 ms = &mapstate[mp-map_table];
1361 ms->flags |= flag;
1362 ms->physix = ix;
1363 pqp = (queue_entry_t) mp;
1364 qp = qp->next;
1365 } while (IS_MAPPTR(qp));
1366 if (qp != qhp)
1367 fatal("bad phys_link next pointer");
1368 }
1369 }
1370
1371 STATIC void
1372 merror(ms, str)
1373 struct mapstate *ms;
1374 char *str;
1375 {
1376 terminal_ours();
1377 fflush(stdout);
1378 fprintf(stderr,
1379 "vtophys: %s: %c%c%c, hashix %d, physix %d, mapping %x\n",
1380 str,
1381 (ms->flags & M_ISFREE) ? 'F' : '-',
1382 (ms->flags & M_ISHASH) ? 'H' : '-',
1383 (ms->flags & M_ISPHYS) ? 'P' : '-',
1384 ms->hashix, ms->physix, &map_table[ms-mapstate]);
1385 return_to_top_level();
1386 }
1387
1388 STATIC int
1389 mach_read(str, from, top, size)
1390 char *str;
1391 CORE_ADDR from;
1392 char *top;
1393 int size;
1394 {
1395 CORE_ADDR paddr;
1396
1397 if (from == ~0)
1398 from = ksym_lookup(str);
1399 paddr = vtophys(0, from);
1400 if (paddr == ~0 || physrd(paddr, top, size) != 0)
1401 fatal("cannot read %s", str);
1402 }
1403
1404 STATIC char *
1405 mach_malloc(str, size)
1406 char *str;
1407 int size;
1408 {
1409 char *ptr = (char *) malloc(size);
1410
1411 if (ptr == 0)
1412 fatal("no memory for %s", str);
1413 return(ptr);
1414 }
1415 #endif
1416
1417 #ifdef KERNELDEBUG
1418 void
1419 _initialize_hp9k8_dep()
1420 {
1421 add_com ("process-address", class_obscure, set_paddr_command,
1422 "The process identified by (ps-style) ADDR becomes the\n\
1423 \"current\" process context for kernel debugging.");
1424 add_com_alias ("paddr", "process-address", class_obscure, 0);
1425 add_com ("virtual-to-physical", class_obscure, vtop_command,
1426 "Translates the kernel virtual address ADDR into a physical address.");
1427 add_com_alias ("vtop", "virtual-to-physical", class_obscure, 0);
1428 }
1429 #endif
1430
1431
1432
This page took 0.058431 seconds and 5 git commands to generate.