* Makefile.in (VERSION): Bump to 4.5.6.
[deliverable/binutils-gdb.git] / gdb / hppabsd-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 /* Routines to extract various sized constants out of hppa
565 instructions. */
566
567 /* This assumes that no garbage lies outside of the lower bits of
568 value. */
569
570 int
571 sign_extend (val, bits)
572 unsigned val, bits;
573 {
574 return (int)(val >> bits - 1 ? (-1 << bits) | val : val);
575 }
576
577 /* For many immediate values the sign bit is the low bit! */
578
579 int
580 low_sign_extend (val, bits)
581 unsigned val, bits;
582 {
583 return (int)((val & 0x1 ? (-1 << (bits - 1)) : 0) | val >> 1);
584 }
585 /* extract the immediate field from a ld{bhw}s instruction */
586
587
588
589 unsigned
590 get_field (val, from, to)
591 unsigned val, from, to;
592 {
593 val = val >> 31 - to;
594 return val & ((1 << 32 - from) - 1);
595 }
596
597 unsigned
598 set_field (val, from, to, new_val)
599 unsigned *val, from, to;
600 {
601 unsigned mask = ~((1 << (to - from + 1)) << (31 - from));
602 return *val = *val & mask | (new_val << (31 - from));
603 }
604
605 /* extract a 3-bit space register number from a be, ble, mtsp or mfsp */
606
607 extract_3 (word)
608 unsigned word;
609 {
610 return GET_FIELD (word, 18, 18) << 2 | GET_FIELD (word, 16, 17);
611 }
612
613 extract_5_load (word)
614 unsigned word;
615 {
616 return low_sign_extend (word >> 16 & MASK_5, 5);
617 }
618
619 /* extract the immediate field from a st{bhw}s instruction */
620
621 int
622 extract_5_store (word)
623 unsigned word;
624 {
625 return low_sign_extend (word & MASK_5, 5);
626 }
627
628 /* extract an 11 bit immediate field */
629
630 int
631 extract_11 (word)
632 unsigned word;
633 {
634 return low_sign_extend (word & MASK_11, 11);
635 }
636
637 /* extract a 14 bit immediate field */
638
639 int
640 extract_14 (word)
641 unsigned word;
642 {
643 return low_sign_extend (word & MASK_14, 14);
644 }
645
646 /* deposit a 14 bit constant in a word */
647
648 unsigned
649 deposit_14 (opnd, word)
650 int opnd;
651 unsigned word;
652 {
653 unsigned sign = (opnd < 0 ? 1 : 0);
654
655 return word | ((unsigned)opnd << 1 & MASK_14) | sign;
656 }
657
658 /* extract a 21 bit constant */
659
660 int
661 extract_21 (word)
662 unsigned word;
663 {
664 int val;
665
666 word &= MASK_21;
667 word <<= 11;
668 val = GET_FIELD (word, 20, 20);
669 val <<= 11;
670 val |= GET_FIELD (word, 9, 19);
671 val <<= 2;
672 val |= GET_FIELD (word, 5, 6);
673 val <<= 5;
674 val |= GET_FIELD (word, 0, 4);
675 val <<= 2;
676 val |= GET_FIELD (word, 7, 8);
677 return sign_extend (val, 21) << 11;
678 }
679
680 /* deposit a 21 bit constant in a word. Although 21 bit constants are
681 usually the top 21 bits of a 32 bit constant, we assume that only
682 the low 21 bits of opnd are relevant */
683
684 unsigned
685 deposit_21 (opnd, word)
686 unsigned opnd, word;
687 {
688 unsigned val = 0;
689
690 val |= GET_FIELD (opnd, 11 + 14, 11 + 18);
691 val <<= 2;
692 val |= GET_FIELD (opnd, 11 + 12, 11 + 13);
693 val <<= 2;
694 val |= GET_FIELD (opnd, 11 + 19, 11 + 20);
695 val <<= 11;
696 val |= GET_FIELD (opnd, 11 + 1, 11 + 11);
697 val <<= 1;
698 val |= GET_FIELD (opnd, 11 + 0, 11 + 0);
699 return word | val;
700 }
701
702 /* extract a 12 bit constant from branch instructions */
703
704 int
705 extract_12 (word)
706 unsigned word;
707 {
708 return sign_extend (GET_FIELD (word, 19, 28) |
709 GET_FIELD (word, 29, 29) << 10 |
710 (word & 0x1) << 11, 12) << 2;
711 }
712
713 /* extract a 17 bit constant from branch instructions, returning the
714 19 bit signed value. */
715
716 int
717 extract_17 (word)
718 unsigned word;
719 {
720 return sign_extend (GET_FIELD (word, 19, 28) |
721 GET_FIELD (word, 29, 29) << 10 |
722 GET_FIELD (word, 11, 15) << 11 |
723 (word & 0x1) << 16, 17) << 2;
724 }
725
726
727 CORE_ADDR
728 frame_saved_pc (frame)
729 FRAME frame;
730 {
731 if (get_current_frame () == frame)
732 {
733 struct frame_saved_regs saved_regs;
734
735 get_frame_saved_regs (frame, &saved_regs);
736 if (saved_regs.regs[RP_REGNUM])
737 return read_memory_integer (saved_regs.regs[RP_REGNUM], 4);
738 else
739 return read_register (RP_REGNUM);
740 }
741 return read_memory_integer (frame->frame - 20, 4) & ~0x3;
742 }
743
744 /* To see if a frame chain is valid, see if the caller looks like it
745 was compiled with gcc. */
746
747 int frame_chain_valid (chain, thisframe)
748 FRAME_ADDR chain;
749 FRAME thisframe;
750 {
751 if (chain && (chain > 0x60000000
752 /* || remote_debugging -this is no longer used */
753 #ifdef KERNELDEBUG
754 || kernel_debugging
755 #endif
756 ))
757 {
758 CORE_ADDR pc = get_pc_function_start (FRAME_SAVED_PC (thisframe));
759
760 if (!inside_entry_file (pc))
761 return 0;
762 /* look for stw rp, -20(0,sp); copy 4,1; copy sp, 4 */
763 if (read_memory_integer (pc, 4) == 0x6BC23FD9)
764 pc = pc + 4;
765
766 if (read_memory_integer (pc, 4) == 0x8040241 &&
767 read_memory_integer (pc + 4, 4) == 0x81E0244)
768 return 1;
769 else
770 return 0;
771 }
772 else
773 return 0;
774 }
775
776 /* Some helper functions. gcc_p returns 1 if the function beginning at
777 pc appears to have been compiled with gcc. hpux_cc_p returns 1 if
778 fn was compiled with hpux cc. gcc functions look like :
779
780 stw rp,-0x14(sp) ; optional
781 or r4,r0,r1
782 or sp,r0,r4
783 stwm r1,framesize(sp)
784
785 hpux cc functions look like:
786
787 stw rp,-0x14(sp) ; optional.
788 stwm r3,framesiz(sp)
789 */
790
791 gcc_p (pc)
792 CORE_ADDR pc;
793 {
794 if (read_memory_integer (pc, 4) == 0x6BC23FD9)
795 pc = pc + 4;
796
797 if (read_memory_integer (pc, 4) == 0x8040241 &&
798 read_memory_integer (pc + 4, 4) == 0x81E0244)
799 return 1;
800 return 0;
801 }
802
803
804 find_dummy_frame_regs (frame, frame_saved_regs)
805 struct frame_info *frame;
806 struct frame_saved_regs *frame_saved_regs;
807 {
808 CORE_ADDR fp = frame->frame;
809 int i;
810
811 frame_saved_regs->regs[RP_REGNUM] = fp - 20 & ~0x3;
812 frame_saved_regs->regs[FP_REGNUM] = fp;
813 frame_saved_regs->regs[1] = fp + 8;
814 frame_saved_regs->regs[3] = fp + 12;
815 for (fp += 16, i = 3; i < 30; fp += 4, i++)
816 frame_saved_regs->regs[i] = fp;
817 frame_saved_regs->regs[31] = fp;
818 fp += 4;
819 for (i = FP0_REGNUM; i < NUM_REGS; i++, fp += 8)
820 frame_saved_regs->regs[i] = fp;
821 /* depend on last increment of fp */
822 frame_saved_regs->regs[IPSW_REGNUM] = fp - 4;
823 frame_saved_regs->regs[SAR_REGNUM] = fp;
824 fp += 4;
825 frame_saved_regs->regs[PCOQ_TAIL_REGNUM] = fp;
826 frame_saved_regs->regs[PCSQ_TAIL_REGNUM] = fp;
827 }
828
829 CORE_ADDR
830 hp_push_arguments (nargs, args, sp, struct_return, struct_addr)
831 int nargs;
832 value *args;
833 CORE_ADDR sp;
834 int struct_return;
835 CORE_ADDR struct_addr;
836 {
837 /* array of arguments' offsets */
838 int *offset = (int *)alloca(nargs);
839 int cum = 0;
840 int i, alignment;
841
842 for (i = 0; i < nargs; i++)
843 {
844 cum += TYPE_LENGTH (VALUE_TYPE (args[i]));
845 /* value must go at proper alignment. Assume alignment is a
846 power of two.*/
847 alignment = hp_alignof (VALUE_TYPE (args[i]));
848 if (cum % alignment)
849 cum = (cum + alignment) & -alignment;
850 offset[i] = -cum;
851 }
852 for (i == 0; i < nargs; i++)
853 {
854 write_memory (sp + offset[i], VALUE_CONTENTS (args[i]), sizeof(int));
855 }
856 sp += min ((cum + 7) & -8, 48);
857 if (struct_return)
858 write_register (28, struct_addr);
859 return sp + 48;
860 }
861
862 /* return the alignment of a type in bytes. Structures have the maximum
863 alignment required by their fields. */
864
865 int
866 hp_alignof (arg)
867 struct type *arg;
868 {
869 int max_align, align, i;
870 switch (TYPE_CODE (arg))
871 {
872 case TYPE_CODE_PTR:
873 case TYPE_CODE_INT:
874 case TYPE_CODE_FLT:
875 return TYPE_LENGTH (arg);
876 case TYPE_CODE_ARRAY:
877 return hp_alignof (TYPE_FIELD_TYPE (arg, 0));
878 case TYPE_CODE_STRUCT:
879 case TYPE_CODE_UNION:
880 max_align = 2;
881 for (i = 0; i < TYPE_NFIELDS (arg); i++)
882 {
883 /* Bit fields have no real alignment. */
884 if (!TYPE_FIELD_BITPOS (arg, i))
885 {
886 align = hp_alignof (TYPE_FIELD_TYPE (arg, i));
887 max_align = max (max_align, align);
888 }
889 }
890 return max_align;
891 default:
892 return 4;
893 }
894 }
895
896 /* Print the register regnum, or all registers if regnum is -1 */
897
898 pa_do_registers_info (regnum, fpregs)
899 int regnum;
900 int fpregs;
901 {
902 char raw_regs [REGISTER_BYTES];
903 int i;
904
905 for (i = 0; i < NUM_REGS; i++)
906 read_relative_register_raw_bytes (i, raw_regs + REGISTER_BYTE (i));
907 if (regnum = -1)
908 pa_print_registers (raw_regs, regnum);
909 else if (regnum < FP0_REGNUM)
910 {
911 printf ("%s %x\n", reg_names[regnum], *(long *)(raw_regs +
912 REGISTER_BYTE (regnum)));
913 }
914 else
915 pa_print_fp_reg (regnum);
916 }
917
918 pa_print_registers (raw_regs, regnum)
919 char *raw_regs;
920 int regnum;
921 {
922 int i;
923
924 for (i = 0; i < 18; i++)
925 printf ("%8.8s: %8x %8.8s: %8x %8.8s: %8x %8.8s: %8x\n",
926 reg_names[i],
927 *(int *)(raw_regs + REGISTER_BYTE (i)),
928 reg_names[i + 18],
929 *(int *)(raw_regs + REGISTER_BYTE (i + 18)),
930 reg_names[i + 36],
931 *(int *)(raw_regs + REGISTER_BYTE (i + 36)),
932 reg_names[i + 54],
933 *(int *)(raw_regs + REGISTER_BYTE (i + 54)));
934 for (i = 72; i < NUM_REGS; i++)
935 pa_print_fp_reg (i);
936 }
937
938 pa_print_fp_reg (i)
939 int i;
940 {
941 unsigned char raw_buffer[MAX_REGISTER_RAW_SIZE];
942 unsigned char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE];
943 REGISTER_TYPE val;
944
945 /* Get the data in raw format, then convert also to virtual format. */
946 read_relative_register_raw_bytes (i, raw_buffer);
947 REGISTER_CONVERT_TO_VIRTUAL (i, raw_buffer, virtual_buffer);
948
949 fputs_filtered (reg_names[i], stdout);
950 print_spaces_filtered (15 - strlen (reg_names[i]), stdout);
951
952 val_print (REGISTER_VIRTUAL_TYPE (i), virtual_buffer, 0, stdout, 0,
953 1, 0, Val_pretty_default);
954 printf_filtered ("\n");
955
956 }
957
958 /*
959 * Virtual to physical translation routines for Utah's Mach 3.0
960 */
961 #ifdef MACHKERNELDEBUG
962
963 #define STATIC
964
965 #if 0 /* too many includes to resolve, too much crap */
966 #include <kern/queue.h>
967 #include <vm/pmap.h>
968 #include <mach/vm_prot.h>
969 #else
970 /* queue.h */
971 struct queue_entry {
972 struct queue_entry *next; /* next element */
973 struct queue_entry *prev; /* previous element */
974 };
975
976 typedef struct queue_entry *queue_t;
977 typedef struct queue_entry queue_head_t;
978 typedef struct queue_entry queue_chain_t;
979 typedef struct queue_entry *queue_entry_t;
980
981 /* pmap.h */
982 #define HP800_HASHSIZE 1024
983 #define HP800_HASHSIZE_LOG2 10
984
985 #define pmap_hash(space, offset) \
986 (((unsigned) (space) << 5 ^ \
987 ((unsigned) (offset) >> 19 | (unsigned) (space) << 13) ^ \
988 (unsigned) (offset) >> 11) & (HP800_HASHSIZE-1))
989
990 struct mapping {
991 queue_head_t hash_link; /* hash table links */
992 queue_head_t phys_link; /* for mappings of a given PA */
993 space_t space; /* virtual space */
994 unsigned offset; /* virtual page number */
995 unsigned tlbpage; /* physical page (for TLB load) */
996 unsigned tlbprot; /* prot/access rights (for TLB load) */
997 struct pmap *pmap; /* pmap mapping belongs to */
998 };
999
1000 struct phys_entry {
1001 queue_head_t phys_link; /* head of mappings of a given PA */
1002 struct mapping *writer; /* mapping with R/W access */
1003 unsigned tlbprot; /* TLB format protection */
1004 };
1005
1006 #endif
1007
1008 #define atop(a) ((unsigned)(a) >> 11)
1009 #define ptoa(p) ((unsigned)(p) << 11)
1010 #define trunc_page(a) ((unsigned)(a) & ~2047)
1011
1012 STATIC long equiv_end;
1013 STATIC queue_head_t *Ovtop_table, *vtop_table, *Ofree_mapping, free_mapping;
1014 STATIC struct phys_entry *Ophys_table, *phys_table;
1015 STATIC long vm_last_phys, vm_first_phys;
1016 STATIC struct mapping *firstmap, *lastmap, *Omap_table, *map_table;
1017 STATIC unsigned Omlow, Omhigh, Omhead, Ovlow, Ovhigh, Oplow, Ophigh;
1018 STATIC unsigned mlow, mhigh, mhead, vlow, vhigh, plow, phigh;
1019 STATIC int vtopsize, physsize, mapsize;
1020 STATIC int kmemfd;
1021
1022 #define IS_OVTOPPTR(p) ((unsigned)(p) >= Ovlow && (unsigned)(p) < Ovhigh)
1023 #define IS_OMAPPTR(p) ((unsigned)(p) >= Omlow && (unsigned)(p) < Omhigh)
1024 #define IS_OPHYSPTR(p) ((unsigned)(p) >= Oplow && (unsigned)(p) < Ophigh)
1025 #define IS_VTOPPTR(p) ((unsigned)(p) >= vlow && (unsigned)(p) < vhigh)
1026 #define IS_MAPPTR(p) ((unsigned)(p) >= mlow && (unsigned)(p) < mhigh)
1027 #define IS_PHYSPTR(p) ((unsigned)(p) >= plow && (unsigned)(p) < phigh)
1028
1029 struct mapstate {
1030 char unused;
1031 char flags;
1032 short hashix;
1033 short physix;
1034 } *mapstate;
1035
1036 /* flags */
1037 #define M_ISFREE 1
1038 #define M_ISHASH 2
1039 #define M_ISPHYS 4
1040
1041 mach_vtophys_init()
1042 {
1043 int errors = 0;
1044
1045 if (!readdata())
1046 errors++;
1047 if (!verifydata())
1048 errors++;
1049 if (!errors)
1050 return(1);
1051 fflush(stdout);
1052 fprintf(stderr,
1053 "translate: may not be able to translate all addresses\n");
1054 return(0);
1055 }
1056
1057 mach_vtophys(space, off, pa)
1058 unsigned space, off, *pa;
1059 {
1060 register int i;
1061 register queue_t qp;
1062 register struct mapping *mp;
1063 int poff;
1064
1065 /*
1066 * Kernel IO or equivilently mapped, one to one.
1067 */
1068 if (space == 0 && (long)off < equiv_end) {
1069 *pa = off;
1070 return(1);
1071 }
1072 /*
1073 * Else look it up in specified space
1074 */
1075 poff = off - trunc_page(off);
1076 off = trunc_page(off);
1077 qp = &vtop_table[pmap_hash(space, off)];
1078 for (mp = (struct mapping *)qp->next;
1079 qp != (queue_entry_t)mp;
1080 mp = (struct mapping *)mp->hash_link.next) {
1081 if (mp->space == space && mp->offset == off) {
1082 *pa = (mp->tlbpage << 7) | poff;
1083 return(1);
1084 }
1085 }
1086 return(0);
1087 }
1088
1089 STATIC
1090 readdata()
1091 {
1092 char *tmp, *mach_malloc();
1093 long size;
1094
1095 /* easy scalars */
1096 mach_read("equiv_end", ~0, (char *)&equiv_end, sizeof equiv_end);
1097 mach_read("vm_first_phys", ~0,
1098 (char *)&vm_first_phys, sizeof vm_first_phys);
1099 mach_read("vm_last_phys", ~0,
1100 (char *)&vm_last_phys, sizeof vm_last_phys);
1101 mach_read("firstmap", ~0, (char *)&firstmap, sizeof firstmap);
1102 mach_read("lastmap", ~0, (char *)&lastmap, sizeof lastmap);
1103
1104 /* virtual to physical hash table */
1105 vtopsize = HP800_HASHSIZE;
1106 size = vtopsize * sizeof(queue_head_t);
1107 tmp = mach_malloc("vtop table", size);
1108 mach_read("vtop_table", ~0, (char *)&Ovtop_table, sizeof Ovtop_table);
1109 mach_read("vtop table", (CORE_ADDR)Ovtop_table, tmp, size);
1110 vtop_table = (queue_head_t *) tmp;
1111
1112 /* inverted page table */
1113 physsize = atop(vm_last_phys - vm_first_phys);
1114 size = physsize * sizeof(struct phys_entry);
1115 tmp = mach_malloc("phys table", size);
1116 mach_read("phys_table", ~0, (char *)&Ophys_table, sizeof Ophys_table);
1117 mach_read("phys table", (CORE_ADDR)Ophys_table, tmp, size);
1118 phys_table = (struct phys_entry *) tmp;
1119
1120 /* mapping structures */
1121 Ofree_mapping = (queue_head_t *) ksym_lookup("free_mapping");
1122 mach_read("free mapping", (CORE_ADDR)Ofree_mapping,
1123 (char *) &free_mapping, sizeof free_mapping);
1124 Omap_table = firstmap;
1125 mapsize = lastmap - firstmap;
1126 size = mapsize * sizeof(struct mapping);
1127 tmp = mach_malloc("mapping table", size);
1128 mach_read("mapping table", (CORE_ADDR)Omap_table, tmp, size);
1129 map_table = (struct mapping *) tmp;
1130
1131 /* set limits */
1132 Ovlow = (unsigned) Ovtop_table;
1133 Ovhigh = (unsigned) &Ovtop_table[vtopsize];
1134 Oplow = (unsigned) Ophys_table;
1135 Ophigh = (unsigned) &Ophys_table[physsize];
1136 Omhead = (unsigned) Ofree_mapping;
1137 Omlow = (unsigned) firstmap;
1138 Omhigh = (unsigned) lastmap;
1139 mlow = (unsigned) map_table;
1140 mhigh = (unsigned) &map_table[mapsize];
1141 mhead = (unsigned) &free_mapping;
1142 vlow = (unsigned) vtop_table;
1143 vhigh = (unsigned) &vtop_table[vtopsize];
1144 plow = (unsigned) phys_table;
1145 phigh = (unsigned) &phys_table[physsize];
1146
1147 #if 0
1148 fprintf(stderr, "Ovtop [%#x-%#x) Ophys [%#x-%#x) Omap %#x [%#x-%#x)\n",
1149 Ovlow, Ovhigh, Oplow, Ophigh, Omhead, Omlow, Omhigh);
1150 fprintf(stderr, "vtop [%#x-%#x) phys [%#x-%#x) map %#x [%#x-%#x)\n",
1151 vlow, vhigh, plow, phigh, mhead, mlow, mhigh);
1152 #endif
1153 return(adjustdata());
1154 }
1155
1156 STATIC unsigned
1157 ptrcvt(ptr)
1158 unsigned ptr;
1159 {
1160 unsigned ret;
1161 char *str;
1162
1163 if (ptr == 0) {
1164 ret = ptr;
1165 str = "null";
1166 } else if (IS_OVTOPPTR(ptr)) {
1167 ret = vlow + (ptr - Ovlow);
1168 str = "vtop";
1169 } else if (IS_OPHYSPTR(ptr)) {
1170 ret = plow + (ptr - Oplow);
1171 str = "phys";
1172 } else if (IS_OMAPPTR(ptr)) {
1173 ret = mlow + (ptr - Omlow);
1174 str = "map";
1175 } else if (ptr == Omhead) {
1176 ret = mhead;
1177 str = "maphead";
1178 } else {
1179 error("bogus pointer %#x", ptr);
1180 str = "wild";
1181 ret = ptr;
1182 }
1183 #if 0
1184 fprintf(stderr, "%x (%s) -> %x\n", ptr, str, ret);
1185 #endif
1186 return(ret);
1187 }
1188
1189 STATIC int
1190 adjustdata()
1191 {
1192 register int i, lim;
1193 queue_head_t *nq;
1194 struct phys_entry *np;
1195 struct mapping *nm;
1196
1197 /* hash table */
1198 lim = vtopsize;
1199 for (nq = vtop_table; nq < &vtop_table[lim]; nq++) {
1200 nq->next = (queue_entry_t) ptrcvt((unsigned)nq->next);
1201 nq->prev = (queue_entry_t) ptrcvt((unsigned)nq->prev);
1202 }
1203
1204 /* IPT */
1205 lim = physsize;
1206 for (np = phys_table; np < &phys_table[lim]; np++) {
1207 np->phys_link.next = (queue_entry_t)
1208 ptrcvt((unsigned)np->phys_link.next);
1209 np->phys_link.prev = (queue_entry_t)
1210 ptrcvt((unsigned)np->phys_link.prev);
1211 np->writer = (struct mapping *) ptrcvt((unsigned)np->writer);
1212 }
1213
1214 /* mapping table */
1215 free_mapping.next = (queue_entry_t)ptrcvt((unsigned)free_mapping.next);
1216 free_mapping.prev = (queue_entry_t)ptrcvt((unsigned)free_mapping.prev);
1217 lim = mapsize;
1218 for (nm = map_table; nm < &map_table[lim]; nm++) {
1219 nm->hash_link.next = (queue_entry_t)
1220 ptrcvt((unsigned)nm->hash_link.next);
1221 nm->hash_link.prev = (queue_entry_t)
1222 ptrcvt((unsigned)nm->hash_link.prev);
1223 nm->phys_link.next = (queue_entry_t)
1224 ptrcvt((unsigned)nm->phys_link.next);
1225 nm->phys_link.prev = (queue_entry_t)
1226 ptrcvt((unsigned)nm->phys_link.prev);
1227 }
1228 return(1);
1229 }
1230
1231 /*
1232 * Consistency checks, make sure:
1233 *
1234 * 1. all mappings are accounted for
1235 * 2. no cycles
1236 * 3. no wild pointers
1237 * 4. consisent TLB state
1238 */
1239 STATIC int
1240 verifydata()
1241 {
1242 register struct mapstate *ms;
1243 register int i;
1244 int errors = 0;
1245
1246 mapstate = (struct mapstate *)
1247 mach_malloc("map state", mapsize * sizeof(struct mapstate));
1248 for (ms = mapstate; ms < &mapstate[mapsize]; ms++) {
1249 ms->flags = 0;
1250 ms->hashix = ms->physix = -2;
1251 }
1252
1253 /*
1254 * Check the free list
1255 */
1256 checkhashchain(&free_mapping, M_ISFREE, -1);
1257 /*
1258 * Check every hash chain
1259 */
1260 for (i = 0; i < vtopsize; i++)
1261 checkhashchain(&vtop_table[i], M_ISHASH, i);
1262 /*
1263 * Check every phys chain
1264 */
1265 for (i = 0; i < physsize; i++)
1266 checkphyschain(&phys_table[i].phys_link, M_ISPHYS, i);
1267 /*
1268 * Cycle through mapstate looking for anomolies
1269 */
1270 ms = mapstate;
1271 for (i = 0; i < mapsize; i++) {
1272 switch (ms->flags) {
1273 case M_ISFREE:
1274 case M_ISHASH|M_ISPHYS:
1275 break;
1276 case 0:
1277 merror(ms, "not found");
1278 errors++;
1279 break;
1280 case M_ISHASH:
1281 merror(ms, "in vtop but not phys");
1282 errors++;
1283 break;
1284 case M_ISPHYS:
1285 merror(ms, "in phys but not vtop");
1286 errors++;
1287 break;
1288 default:
1289 merror(ms, "totally bogus");
1290 errors++;
1291 break;
1292 }
1293 ms++;
1294 }
1295 return(errors ? 0 : 1);
1296 }
1297
1298 STATIC void
1299 checkhashchain(qhp, flag, ix)
1300 queue_entry_t qhp;
1301 {
1302 register queue_entry_t qp, pqp;
1303 register struct mapping *mp;
1304 struct mapstate *ms;
1305
1306 qp = qhp->next;
1307 /*
1308 * First element is not a mapping structure,
1309 * chain must be empty.
1310 */
1311 if (!IS_MAPPTR(qp)) {
1312 if (qp != qhp || qp != qhp->prev)
1313 fatal("bad vtop_table header pointer");
1314 } else {
1315 pqp = qhp;
1316 do {
1317 mp = (struct mapping *) qp;
1318 qp = &mp->hash_link;
1319 if (qp->prev != pqp)
1320 fatal("bad hash_link prev pointer");
1321 ms = &mapstate[mp-map_table];
1322 ms->flags |= flag;
1323 ms->hashix = ix;
1324 pqp = (queue_entry_t) mp;
1325 qp = qp->next;
1326 } while (IS_MAPPTR(qp));
1327 if (qp != qhp)
1328 fatal("bad hash_link next pointer");
1329 }
1330 }
1331
1332 STATIC void
1333 checkphyschain(qhp, flag, ix)
1334 queue_entry_t qhp;
1335 {
1336 register queue_entry_t qp, pqp;
1337 register struct mapping *mp;
1338 struct mapstate *ms;
1339
1340 qp = qhp->next;
1341 /*
1342 * First element is not a mapping structure,
1343 * chain must be empty.
1344 */
1345 if (!IS_MAPPTR(qp)) {
1346 if (qp != qhp || qp != qhp->prev)
1347 fatal("bad phys_table header pointer");
1348 } else {
1349 pqp = qhp;
1350 do {
1351 mp = (struct mapping *) qp;
1352 qp = &mp->phys_link;
1353 if (qp->prev != pqp)
1354 fatal("bad phys_link prev pointer");
1355 ms = &mapstate[mp-map_table];
1356 ms->flags |= flag;
1357 ms->physix = ix;
1358 pqp = (queue_entry_t) mp;
1359 qp = qp->next;
1360 } while (IS_MAPPTR(qp));
1361 if (qp != qhp)
1362 fatal("bad phys_link next pointer");
1363 }
1364 }
1365
1366 STATIC void
1367 merror(ms, str)
1368 struct mapstate *ms;
1369 char *str;
1370 {
1371 terminal_ours();
1372 fflush(stdout);
1373 fprintf(stderr,
1374 "vtophys: %s: %c%c%c, hashix %d, physix %d, mapping %x\n",
1375 str,
1376 (ms->flags & M_ISFREE) ? 'F' : '-',
1377 (ms->flags & M_ISHASH) ? 'H' : '-',
1378 (ms->flags & M_ISPHYS) ? 'P' : '-',
1379 ms->hashix, ms->physix, &map_table[ms-mapstate]);
1380 return_to_top_level();
1381 }
1382
1383 STATIC int
1384 mach_read(str, from, top, size)
1385 char *str;
1386 CORE_ADDR from;
1387 char *top;
1388 int size;
1389 {
1390 CORE_ADDR paddr;
1391
1392 if (from == ~0)
1393 from = ksym_lookup(str);
1394 paddr = vtophys(0, from);
1395 if (paddr == ~0 || physrd(paddr, top, size) != 0)
1396 fatal("cannot read %s", str);
1397 }
1398
1399 STATIC char *
1400 mach_malloc(str, size)
1401 char *str;
1402 int size;
1403 {
1404 char *ptr = (char *) malloc(size);
1405
1406 if (ptr == 0)
1407 fatal("no memory for %s", str);
1408 return(ptr);
1409 }
1410 #endif
1411
1412 #ifdef KERNELDEBUG
1413 void
1414 _initialize_hp9k8_dep()
1415 {
1416 add_com ("process-address", class_obscure, set_paddr_command,
1417 "The process identified by (ps-style) ADDR becomes the\n\
1418 \"current\" process context for kernel debugging.");
1419 add_com_alias ("paddr", "process-address", class_obscure, 0);
1420 add_com ("virtual-to-physical", class_obscure, vtop_command,
1421 "Translates the kernel virtual address ADDR into a physical address.");
1422 add_com_alias ("vtop", "virtual-to-physical", class_obscure, 0);
1423 }
1424 #endif
1425
1426
1427
1428
This page took 0.077372 seconds and 4 git commands to generate.