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