home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.bin / gdb / config / hp300bsd-dep.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-12  |  27.3 KB  |  1,162 lines

  1. /*-
  2.  * This code is derived from software copyrighted by the Free Software
  3.  * Foundation.
  4.  *
  5.  * Modified 1991 by Donn Seeley at UUNET Technologies, Inc.
  6.  * Modified 1990 by Van Jacobson at Lawrence Berkeley Laboratory.
  7.  */
  8.  
  9. #ifndef lint
  10. static char sccsid[] = "@(#)hp300bsd-dep.c    6.10 (Berkeley) 5/12/91";
  11. #endif /* not lint */
  12.  
  13. /*
  14.  * Machine-dependent code for a Hewlett-Packard 9000/300, running bsd.
  15.  * Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc. 
  16.  *
  17.  * This file is part of GDB. 
  18.  *
  19.  * GDB is free software; you can redistribute it and/or modify it under the
  20.  * terms of the GNU General Public License as published by the Free Software
  21.  * Foundation; either version 1, or (at your option) any later version. 
  22.  *
  23.  * GDB is distributed in the hope that it will be useful, but WITHOUT ANY
  24.  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  25.  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  26.  * details. 
  27.  *
  28.  * You should have received a copy of the GNU General Public License along with
  29.  * GDB; see the file COPYING.  If not, write to the Free Software Foundation,
  30.  * 675 Mass Ave, Cambridge, MA 02139, USA.  
  31.  */
  32. #ifndef lint
  33. static char rcsid[] = "$Header: hp300bsd-dep.c,v 1.6 91/03/13 01:04:43 mccanne Exp $";
  34. #endif lint
  35.  
  36. #include <stdio.h>
  37. #include "defs.h"
  38. #include "param.h"
  39. #include "frame.h"
  40. #include "inferior.h"
  41. #include "value.h"
  42.  
  43.  
  44. #include <sys/param.h>
  45. #include <sys/dir.h>
  46. #include <signal.h>
  47. #include <sys/ioctl.h>
  48. /* #include <fcntl.h>  Can we live without this?  */
  49.  
  50. #include <a.out.h>
  51. #ifndef N_SET_MAGIC
  52. #define N_SET_MAGIC(exec, val) ((exec).a_magic = (val))
  53. #endif
  54.  
  55. #ifdef NEWVM
  56. #include <hp300/hp300/pte.h>
  57. #endif
  58.  
  59. #include <sys/time.h>
  60. #include <sys/resource.h>
  61. #include <sys/uio.h>
  62. #include <sys/user.h>        /* After a.out.h  */
  63. #include <sys/file.h>
  64. #include <sys/stat.h>
  65. #include <sys/ptrace.h>
  66.  
  67. CORE_ADDR       kernel_u_addr;
  68.  
  69. #ifdef KERNELDEBUG
  70. #ifndef NEWVM
  71. #include <sys/vmmac.h>
  72. #include <machine/pte.h>
  73. #endif
  74. #include <machine/vmparam.h>
  75. #include <machine/cpu.h>
  76. #include <ctype.h>
  77. #include "symtab.h"    /* XXX */
  78.  
  79. extern int kernel_debugging;
  80.  
  81. #define    KERNOFF        ((unsigned)KERNBASE)
  82. #define    LOWRAM        ((unsigned)0xfffffdce)
  83. /* actually you can't really distinguish user and kernel by address */
  84. #define    INKERNEL(x)    ((x) >= KERNOFF && (x) < KERNOFF + ctob(slr))
  85. #define INUDOT(x)    \
  86.     ((x) >= KERNEL_U_ADDR && (x) < KERNEL_U_ADDR + ctob(UPAGES))
  87.  
  88. #define    PT_ADDR_ANY    ((caddr_t) 1)
  89.  
  90. /*
  91.  * Convert from sysmap pte index to system virtual address & vice-versa.
  92.  * (why aren't these in one of the system vm macro files???)
  93.  */
  94. #define smxtob(a)       (sbr + (a) * sizeof(pte))
  95. #define btosmx(b)       (((b) - sbr) / sizeof(pte))
  96.  
  97. static int ok_to_cache();
  98. #ifdef NEWVM
  99. static int found_pcb;
  100. static CORE_ADDR curpcb;
  101. static CORE_ADDR kstack;
  102. #endif
  103. #endif
  104.  
  105. extern int      errno;
  106.  
  107. /*
  108.  * This function simply calls ptrace with the given arguments.  It exists so
  109.  * that all calls to ptrace are isolated in this machine-dependent file. 
  110.  */
  111. int
  112. call_ptrace(request, pid, arg3, arg4)
  113.     int request;
  114.     pid_t pid;
  115.     caddr_t arg3;
  116.     int arg4;
  117. {
  118.     return(ptrace(request, pid, arg3, arg4));
  119. }
  120.  
  121. kill_inferior()
  122. {
  123.     if (remote_debugging) {
  124. #ifdef KERNELDEBUG
  125.         if (kernel_debugging)
  126.             /*
  127.              * It's a very, very bad idea to go away leaving
  128.              * breakpoints in a remote kernel or to leave it
  129.              * stopped at a breakpoint. 
  130.              */
  131.             clear_breakpoints();
  132. #endif
  133.         remote_close(0);
  134.         inferior_died();
  135.     } else if (inferior_pid != 0) {
  136.         ptrace(PT_KILL, inferior_pid, 0, 0);
  137.         wait(0);
  138.         inferior_died();
  139.     }
  140. }
  141.  
  142. /*
  143.  * This is used when GDB is exiting.  It gives less chance of error.
  144.  */
  145. kill_inferior_fast()
  146. {
  147.     if (remote_debugging) {
  148. #ifdef KERNELDEBUG
  149.         if (kernel_debugging)
  150.             clear_breakpoints();
  151. #endif
  152.         remote_close(0);
  153.         return;
  154.     }
  155.     if (inferior_pid == 0)
  156.         return;
  157.  
  158.     ptrace(PT_KILL, inferior_pid, 0, 0);
  159.     wait(0);
  160. }
  161.  
  162. /*
  163.  * Resume execution of the inferior process. If STEP is nonzero, single-step
  164.  * it. If SIGNAL is nonzero, give it that signal.  
  165.  */
  166. void
  167. resume(step, signal)
  168.     int step;
  169.     int signal;
  170. {
  171.     errno = 0;
  172.     if (remote_debugging)
  173.         remote_resume(step, signal);
  174.     else {
  175.         ptrace(step ? PT_STEP : PT_CONTINUE, inferior_pid,
  176.                PT_ADDR_ANY, signal);
  177.         if (errno)
  178.             perror_with_name("ptrace");
  179.     }
  180. }
  181.  
  182. #ifdef ATTACH_DETACH
  183. extern int attach_flag;
  184.  
  185. /*
  186.  * Start debugging the process whose number is PID.
  187.  */
  188. attach(pid)
  189.     int pid;
  190. {
  191.     errno = 0;
  192.     ptrace(PT_ATTACH, pid, 0, 0);
  193.     if (errno)
  194.         perror_with_name("ptrace");
  195.     attach_flag = 1;
  196.     return pid;
  197. }
  198.  
  199. /*
  200.  * Stop debugging the process whose number is PID and continue it
  201.  * with signal number SIGNAL.  SIGNAL = 0 means just continue it.  
  202.  */
  203. void
  204. detach(signal)
  205.     int signal;
  206. {
  207.     errno = 0;
  208.     ptrace(PT_DETACH, inferior_pid, PT_ADDR_ANY, signal);
  209.     if (errno)
  210.         perror_with_name("ptrace");
  211.     attach_flag = 0;
  212. }
  213. #endif    /* ATTACH_DETACH */
  214.  
  215. static unsigned int
  216. get_register_offset()
  217. {
  218.     unsigned int offset;
  219.     struct user u;    /* XXX */
  220.  
  221. #ifdef NEWVM
  222.     offset = (char *) &u.u_kproc.kp_proc.p_regs - (char *) &u;
  223.     offset = ptrace(PT_READ_U, inferior_pid, (caddr_t)offset, 0) -
  224.         USRSTACK;
  225. #else
  226.     offset = (char *) &u.u_ar0 - (char *) &u;
  227.     offset = ptrace(PT_READ_U, inferior_pid, (caddr_t)offset, 0) -
  228.         KERNEL_U_ADDR;
  229. #endif
  230.     return offset;
  231. }
  232.  
  233. void
  234. fetch_inferior_registers()
  235. {
  236.     register int    regno;
  237.     register unsigned int regaddr;
  238.     char            buf[MAX_REGISTER_RAW_SIZE];
  239.     register int    i;
  240.     unsigned int    offset;
  241.  
  242.     if (remote_debugging) {
  243.         extern char     registers[];
  244.  
  245.         remote_fetch_registers(registers);
  246.         return;
  247.     }
  248.  
  249.     offset = get_register_offset();
  250.  
  251.     for (regno = 0; regno < NUM_REGS; regno++) {
  252.         regaddr = register_addr(regno, offset);
  253.         for (i = 0; i < REGISTER_RAW_SIZE(regno); i += sizeof(int)) {
  254.             *(int *)&buf[i] = ptrace(PT_READ_U, inferior_pid, 
  255.                          (caddr_t)regaddr, 0);
  256.             regaddr += sizeof(int);
  257.         }
  258.         supply_register(regno, buf);
  259.     }
  260. }
  261.  
  262. /*
  263.  * Store our register values back into the inferior. If REGNO is -1, do this
  264.  * for all registers. Otherwise, REGNO specifies which register (so we can
  265.  * save time).  
  266.  */
  267. store_inferior_registers(regno)
  268.     int             regno;
  269. {
  270.     register unsigned int regaddr;
  271.     char            buf[80];
  272.     extern char     registers[];
  273.     register int    i;
  274.     unsigned int    offset;
  275.  
  276.     if (remote_debugging) {
  277.         extern char     registers[];
  278.  
  279.         remote_store_registers(registers);
  280.         return;
  281.     }
  282.  
  283.     offset = get_register_offset();
  284.  
  285.     if (regno >= 0) {
  286.         regaddr = register_addr(regno, offset);
  287.         for (i = 0; i < REGISTER_RAW_SIZE(regno); i += sizeof(int)) {
  288.             errno = 0;
  289.             ptrace(PT_WRITE_U, inferior_pid, (caddr_t)regaddr,
  290.                  *(int *) ®isters[REGISTER_BYTE(regno) + i]);
  291.             if (errno != 0) {
  292.                 sprintf(buf, "writing register number %d(%d)",
  293.                     regno, i);
  294.                 perror_with_name(buf);
  295.             }
  296.             regaddr += sizeof(int);
  297.         }
  298.     } else
  299.         for (regno = 0; regno < NUM_REGS; regno++) {
  300.             regaddr = register_addr(regno, offset);
  301.             for (i = 0; i < REGISTER_RAW_SIZE(regno);
  302.                  i += sizeof(int)) {
  303.                 errno = 0;
  304.                 ptrace(PT_WRITE_U, inferior_pid,
  305.                        (caddr_t)regaddr,
  306.                        *(int *) ®isters[REGISTER_BYTE(regno) + i]);
  307.                 if (errno != 0) {
  308.                     sprintf(buf,
  309.                        "writing register number %d(%d)", 
  310.                         regno, i);
  311.                     perror_with_name(buf);
  312.                 }
  313.                 regaddr += sizeof(int);
  314.             }
  315.         }
  316. }
  317.  
  318. /*
  319.  * Copy LEN bytes from inferior's memory starting at MEMADDR to debugger
  320.  * memory starting at MYADDR. On failure (cannot read from inferior, usually
  321.  * because address is out of bounds) returns the value of errno. 
  322.  */
  323. int
  324. read_inferior_memory(memaddr, myaddr, len)
  325.     CORE_ADDR       memaddr;
  326.     char           *myaddr;
  327.     int             len;
  328. {
  329.     register int i;
  330.     /* Round starting address down to longword boundary.  */
  331.     register CORE_ADDR addr = memaddr & -sizeof(int);
  332.     /* Round ending address up; get number of longwords that makes.  */
  333.     register int count = (((memaddr + len) - addr) + sizeof(int) - 1) / 
  334.                 sizeof(int);
  335.     /* Allocate buffer of that many longwords.  */
  336.     register int *buffer = (int *) alloca(count * sizeof(int));
  337.     extern int errno;
  338.  
  339.     if (remote_debugging)
  340.         return (remote_read_inferior_memory(memaddr, myaddr, len));
  341.  
  342.     /* Read all the longwords */
  343.     errno = 0;
  344.     for (i = 0; i < count && errno == 0; i++, addr += sizeof(int)) 
  345.         buffer[i] = ptrace(PT_READ_I, inferior_pid, (caddr_t)addr, 0);
  346.  
  347.     /* Copy appropriate bytes out of the buffer.  */
  348.     bcopy((char *) buffer + (memaddr & (sizeof(int) - 1)), myaddr, len);
  349.     return(errno);
  350. }
  351.  
  352. /*
  353.  * Copy LEN bytes of data from debugger memory at MYADDR to inferior's memory
  354.  * at MEMADDR. On failure (cannot write the inferior) returns the value of
  355.  * errno.  
  356.  */
  357.  
  358. int
  359. write_inferior_memory(memaddr, myaddr, len)
  360.     CORE_ADDR       memaddr;
  361.     char           *myaddr;
  362.     int             len;
  363. {
  364.     register int    i;
  365.     /* Round starting address down to longword boundary.  */
  366.     register CORE_ADDR addr = memaddr & -sizeof(int);
  367.     /* Round ending address up; get number of longwords that makes.  */
  368.     register int count = (((memaddr + len) - addr) + sizeof(int) - 1) / 
  369.                 sizeof(int);
  370.     /* Allocate buffer of that many longwords.  */
  371.     register int *buffer = (int *) alloca(count * sizeof(int));
  372.     extern int errno;
  373.  
  374.     /*
  375.      * Fill start and end extra bytes of buffer with existing memory
  376.      * data.  
  377.      */
  378.     if (remote_debugging)
  379.         return (remote_write_inferior_memory(memaddr, myaddr, len));
  380.  
  381.     /*
  382.      * Fill start and end extra bytes of buffer with existing memory
  383.      * data.  
  384.      */
  385.     buffer[0] = ptrace(PT_READ_I, inferior_pid, (caddr_t)addr, 0);
  386.  
  387.     if (count > 1)
  388.         buffer[count - 1] = ptrace(PT_READ_I, inferior_pid,
  389.                  (caddr_t)addr + (count - 1) * sizeof(int), 0);
  390.  
  391.     /* Copy data to be written over corresponding part of buffer */
  392.  
  393.     bcopy(myaddr, (char *) buffer + (memaddr & (sizeof(int) - 1)), len);
  394.  
  395.     /* Write the entire buffer.  */
  396.  
  397.     errno = 0;
  398.     for (i = 0; i < count && errno == 0; i++, addr += sizeof(int))
  399.         ptrace(PT_WRITE_I, inferior_pid, (caddr_t)addr, buffer[i]);
  400.  
  401.     return(errno);
  402. }
  403.  
  404.  
  405. /*
  406.  * Work with core dump and executable files, for GDB. 
  407.  * This code would be in core.c if it weren't machine-dependent. 
  408.  */
  409.  
  410. #ifndef N_TXTADDR
  411. #define N_TXTADDR(hdr) 0
  412. #endif                /* no N_TXTADDR */
  413.  
  414. #ifndef N_DATADDR
  415. #define N_DATADDR(hdr) hdr.a_text
  416. #endif                /* no N_DATADDR */
  417.  
  418. /*
  419.  * Make COFF and non-COFF names for things a little more compatible to reduce
  420.  * conditionals later.  
  421.  */
  422.  
  423.  
  424. #ifndef AOUTHDR
  425. #define AOUTHDR struct exec
  426. #endif
  427.  
  428. extern char    *sys_siglist[];
  429.  
  430.  
  431. /* Hook for `exec_file_command' command to call.  */
  432.  
  433. extern void     (*exec_file_display_hook) ();
  434.  
  435. /* File names of core file and executable file.  */
  436.  
  437. extern char    *corefile;
  438. extern char    *execfile;
  439.  
  440. /* Descriptors on which core file and executable file are open.
  441.    Note that the execchan is closed when an inferior is created
  442.    and reopened if the inferior dies or is killed.  */
  443.  
  444. extern int      corechan;
  445. extern int      execchan;
  446.  
  447. /* Last modification time of executable file.
  448.    Also used in source.c to compare against mtime of a source file.  */
  449.  
  450. extern int      exec_mtime;
  451.  
  452. /* Virtual addresses of bounds of the two areas of memory in the core file.  */
  453.  
  454. extern CORE_ADDR data_start;
  455. extern CORE_ADDR data_end;
  456. extern CORE_ADDR stack_start;
  457. extern CORE_ADDR stack_end;
  458.  
  459. /* Virtual addresses of bounds of two areas of memory in the exec file.
  460.    Note that the data area in the exec file is used only when there is no core file.  */
  461.  
  462. extern CORE_ADDR text_start;
  463. extern CORE_ADDR text_end;
  464.  
  465. extern CORE_ADDR exec_data_start;
  466. extern CORE_ADDR exec_data_end;
  467.  
  468. /* Address in executable file of start of text area data.  */
  469.  
  470. extern int      text_offset;
  471.  
  472. /* Address in executable file of start of data area data.  */
  473.  
  474. extern int      exec_data_offset;
  475.  
  476. /* Address in core file of start of data area data.  */
  477.  
  478. extern int      data_offset;
  479.  
  480. /* Address in core file of start of stack area data.  */
  481.  
  482. extern int      stack_offset;
  483.  
  484.  
  485. /* a.out header saved in core file.  */
  486.  
  487. extern AOUTHDR  core_aouthdr;
  488.  
  489. /* a.out header of exec file.  */
  490.  
  491. extern AOUTHDR  exec_aouthdr;
  492.  
  493. extern void     validate_files();
  494.  
  495.  
  496. extern int (*core_file_hook)();
  497.  
  498. #ifdef KERNELDEBUG
  499. /*
  500.  * Kernel debugging routines.
  501.  */
  502.  
  503. static CORE_ADDR file_offset;
  504. static CORE_ADDR lowram;
  505. static CORE_ADDR sbr;
  506. static CORE_ADDR slr;
  507. static struct pcb pcb;
  508. static CORE_ADDR kernel_udot_va;
  509.  
  510. #ifndef CFSIZE
  511. #include <machine/frame.h>
  512. #endif
  513.  
  514. static CORE_ADDR
  515. ksym_lookup(name)
  516.     char *name;
  517. {
  518.     struct symbol *sym;
  519.     int i;
  520.  
  521.     if ((i = lookup_misc_func(name)) < 0)
  522.         error("kernel symbol `%s' not found.", name);
  523.  
  524.     return (misc_function_vector[i].address);
  525. }
  526.  
  527. /*
  528.  * return true if 'len' bytes starting at 'addr' can be read out as
  529.  * longwords and/or locally cached (this is mostly for memory mapped
  530.  * i/o register access when debugging remote kernels).
  531.  */
  532. static int
  533. ok_to_cache(addr, len)
  534. {
  535. #ifdef NEWVM
  536.     static CORE_ADDR intiobase, extiobase;
  537.  
  538.     if (! intiobase) {
  539.         intiobase = ksym_lookup("intiobase");
  540.         (void)remote_read_inferior_memory(intiobase, &intiobase,
  541.                           sizeof(intiobase));
  542.         extiobase = ksym_lookup("extiobase");
  543.         (void)remote_read_inferior_memory(extiobase, &extiobase,
  544.                           sizeof(extiobase));
  545.     }
  546.  
  547.     if (addr >= intiobase && addr < intiobase + ctob(IIOMAPSIZE))
  548.         return (0);
  549.     if (addr >= extiobase && addr < extiobase + ctob(EIOMAPSIZE))
  550.         return (0);
  551. #else
  552.     static CORE_ADDR IObase;
  553.  
  554.     if (! IObase)
  555.         IObase = ksym_lookup("IObase");
  556.  
  557.     if (addr >= IObase && addr < IObase + (IOTOP - IOBASE))
  558.         return (0);
  559. #endif
  560.  
  561.     return (1);
  562. }
  563.  
  564. static
  565. physrd(addr, dat, len)
  566.     u_int addr;
  567.     char *dat;
  568. {
  569.     if (lseek(corechan, addr - file_offset, L_SET) == -1)
  570.         return (-1);
  571.     if (read(corechan, dat, len) != len)
  572.         return (-1);
  573.  
  574.     return (0);
  575. }
  576.  
  577. /*
  578.  * When looking at kernel data space through /dev/mem or with a core file, do
  579.  * virtual memory mapping.
  580.  */
  581. #ifdef NEWVM
  582. static CORE_ADDR
  583. vtophys(addr)
  584.     CORE_ADDR addr;
  585. {
  586.     CORE_ADDR v;
  587.     struct pte pte;
  588.     CORE_ADDR stp;
  589.     CORE_ADDR oldaddr = addr;
  590.     static CORE_ADDR curstp = -1;
  591.  
  592.     /*
  593.      * If we're looking at the kernel stack,
  594.      * munge the address to refer to the user space mapping instead;
  595.      * that way we get the requested process's kstack, not the running one.
  596.      */
  597.     if (addr >= kstack && addr < kstack + ctob(UPAGES))
  598.         addr = (addr - kstack) + curpcb;
  599.  
  600.     /*
  601.      * Identify the current segment table.
  602.      * Since the given VA could come from either kernel
  603.      * or user space, the following heuristics don't always work.
  604.      */
  605.     if (INKERNEL(addr))
  606.         stp = sbr;
  607.     else if (found_pcb == 0) {
  608.         /* We have a pcb address, but haven't read it yet.  Cheat. */
  609.         if (curstp == -1) {
  610.             v = vtophys((CORE_ADDR)&((struct pcb *)curpcb)->pcb_ustp);
  611.             physrd(v, &curstp, sizeof curstp);
  612.         }
  613.         stp = curstp;
  614.     } else
  615.         stp = pcb.pcb_ustp;
  616.  
  617.     /*
  618.      * Read the current segment table.
  619.      */
  620.     v = stp + ((addr >> SG_ISHIFT) * sizeof pte);
  621.     if (physrd(v, (char *)&pte, sizeof(pte)))
  622.         return (~0);
  623.     if (*(int *)&pte == SG_NV)
  624.         return (~0);
  625.     v = hp300_btop(addr & SG_PMASK);
  626.     addr = (CORE_ADDR)(hp300_ptob(pte.pg_pfnum) + v*sizeof pte);
  627.  
  628.     /*
  629.      * Addr is now address of the pte of the page we are interested in;
  630.      * get the pte and paste up the physical address.
  631.      */
  632.     if (physrd(addr, (char *) &pte, sizeof(pte)))
  633.         return (~0);
  634.     if (pte.pg_v == 0 && pte.pg_pfnum == 0)
  635.         return (~0);
  636.     addr = (CORE_ADDR)hp300_ptob(pte.pg_pfnum) + (oldaddr & PGOFSET);
  637. #if 0
  638.     printf("vtophys(%x) -> %x\n", oldaddr, addr);
  639. #endif
  640.     return (addr);
  641. }
  642. #else
  643. static CORE_ADDR
  644. vtophys(addr)
  645.     CORE_ADDR addr;
  646. {
  647.     CORE_ADDR v;
  648.     struct pte pte;
  649.     CORE_ADDR oldaddr = addr;
  650.  
  651.     /* permit direct reference to physical memory */
  652.     if (addr >= lowram)
  653.         return (addr);
  654.  
  655.     if (kernel_udot_va && INUDOT(addr)) {
  656.         addr -= KERNEL_U_ADDR;
  657.         addr = kernel_udot_va + btop(addr) * sizeof (struct pte);
  658.         addr = vtophys(addr);
  659.     } else if (INKERNEL(addr)) {
  660.         /*
  661.          * In system space get system pte.  If valid or reclaimable
  662.          * then physical address is combination of its page number
  663.          * and the page offset of the original address.
  664.          */
  665.         v = smxtob(btop(addr - KERNOFF));
  666.         addr = v + lowram;
  667.     } else {
  668.         /* In p0 space must not be off end of region. */
  669.         v = btop(addr);
  670.         if (v >= pcb.pcb_p0lr)
  671.             /* address out of segment */
  672.             return (~0);
  673.  
  674.         addr = (CORE_ADDR)(pcb.pcb_p0br + v);
  675.         /*
  676.          * For p0/p1 address, user-level page table should be in
  677.          * kernel vm.  Do second-level indirect by recursing.
  678.          */
  679.         if (!INKERNEL(addr))
  680.             return (~0);
  681.  
  682.         addr = vtophys(addr);
  683.     }
  684.     /*
  685.      * Addr is now address of the pte of the page we are interested in;
  686.      * get the pte and paste up the physical address.
  687.      */
  688.     if (physrd(addr, (char *) &pte, sizeof(pte)))
  689.         return (~0);
  690.  
  691.     if (pte.pg_v == 0 && (pte.pg_fod || pte.pg_pfnum == 0))
  692.         return (~0);
  693.  
  694.     addr = (CORE_ADDR)ptob(pte.pg_pfnum) + (oldaddr & PGOFSET);
  695. #if 0
  696.     printf("vtophys(%x) -> %x\n", oldaddr, addr);
  697. #endif
  698.     return (addr);
  699. }
  700. #endif
  701.  
  702. static
  703. kvread(addr)
  704.     CORE_ADDR addr;
  705. {
  706.     CORE_ADDR paddr = vtophys(addr);
  707.  
  708.     if (paddr != ~0)
  709.         if (physrd(paddr, (char *)&addr, sizeof(addr)) == 0);
  710.             return (addr);
  711.  
  712.     return (~0);
  713. }
  714.  
  715. static void
  716. read_pcb(uaddr)
  717.      u_int uaddr;
  718. {
  719.     int i;
  720.  
  721. #ifdef NEWVM
  722.     if (physrd(uaddr, (char *)&pcb, sizeof pcb))
  723.         error("cannot read pcb at %x\n", uaddr);
  724.     printf("current pcb at %x\n", uaddr);
  725. #else
  726.     if (physrd (uaddr, (char *)&pcb, sizeof pcb))
  727.         error ("cannot read pcb at %x.\n", uaddr);
  728.     printf("p0br %x p0lr %x p1br %x p1lr %x\n",
  729.            pcb.pcb_p0br, pcb.pcb_p0lr, pcb.pcb_p1br, pcb.pcb_p1lr);
  730.  
  731.     kernel_udot_va = (CORE_ADDR) (pcb.pcb_p1br + BTOPUSRSTACK);
  732. #endif
  733.  
  734.     /*
  735.      * get the register values out of the sys pcb and
  736.      * store them where `read_register' will find them.
  737.      */
  738.     for (i = 2; i < 8; ++i)
  739.         supply_register(i, &pcb.pcb_regs[i-2]);
  740.     for (i = 10; i < 16; ++i)
  741.         supply_register(i, &pcb.pcb_regs[i-4]);
  742.     
  743.     /* fake 'scratch' regs d0, d1, a0, a1 */
  744.     i = 0;
  745.     supply_register(0, &i); supply_register(1, &i);
  746.     supply_register(8, &i); supply_register(9, &i);
  747.     
  748.     i = kvread(pcb.pcb_regs[10] + 4);
  749.     if (i != -1)
  750.         supply_register(PC_REGNUM, &i);
  751.     
  752.     supply_register(PS_REGNUM, &pcb.pcb_ps);
  753.     
  754.     for (i = FP0_REGNUM; i < NUM_REGS; ++i) {
  755.         int fpreg;
  756.  
  757.         REGISTER_U_ADDR(fpreg, 0, i);
  758.         supply_register(i, ((char *)&pcb) + fpreg);
  759.     }
  760. }
  761.  
  762. static void
  763. setup_kernel_debugging()
  764. {
  765.     struct stat stb;
  766.     int devmem = 0;
  767.     CORE_ADDR addr;
  768.  
  769.     fstat(corechan, &stb);
  770.     if ((stb.st_mode & S_IFMT) == S_IFCHR && stb.st_rdev == makedev(2, 0))
  771.         devmem = 1;
  772.  
  773.     /*
  774.      * Must get value of lowram before we can read PCB.
  775.      */
  776.     if (devmem)
  777.         /* /dev/mem == physical memory */
  778.         (void)physrd(LOWRAM, (char *)&lowram, sizeof(lowram));
  779.     else
  780.         /* normal file -- use standard offset */
  781.         (void)physrd(ksym_lookup("lowram"), (char *)&lowram,
  782.                  sizeof(lowram));
  783.     lowram = roundup(lowram, NBPG);
  784.     if (! devmem)
  785.         file_offset = lowram;
  786.  
  787.     /*
  788.      * Get system mapping information.
  789.      */
  790. #ifdef NEWVM
  791.     sbr = ksym_lookup("Sysseg") + lowram;
  792.     (void)physrd(sbr, (char *)&sbr, sizeof(sbr));
  793.     sbr += lowram;        /* sbr is a physical address for NEWVM */
  794.     slr = NPTEPG * (NPTEPG-1);
  795.     curpcb = ksym_lookup("curpcb") + lowram;
  796.     physrd(curpcb, &curpcb, sizeof curpcb);
  797.     kstack = ksym_lookup("kstack");
  798. #else
  799.     sbr = ksym_lookup("Sysmap");
  800.     slr = ksym_lookup("Syssize");
  801. #endif
  802.     printf("sbr %x slr %x\n", sbr, slr);
  803.  
  804.     /*
  805.      * pcb where "panic" saved registers in first thing in current
  806.      * u area.
  807.      */
  808. #ifdef NEWVM
  809.     read_pcb(vtophys(kstack));
  810.     found_pcb = 1;
  811. #else
  812.     read_pcb(vtophys(ksym_lookup("u")));
  813. #endif
  814.     if (!devmem) {
  815.         /* find stack frame */
  816.         CORE_ADDR panicstr;
  817.         char buf[256];
  818.         register char *cp;
  819.  
  820.         panicstr = kvread(ksym_lookup("panicstr"));
  821.         if (panicstr == ~0)
  822.             return;
  823.         (void) kernel_core_file_hook(panicstr, buf, sizeof(buf));
  824.         for (cp = buf; cp < &buf[sizeof(buf)] && *cp; cp++)
  825.             if (!isascii(*cp) || (!isprint(*cp) && !isspace(*cp)))
  826.                 *cp = '?';
  827.         if (*cp)
  828.             *cp = '\0';
  829.         printf("panic: %s\n", buf);
  830.     }
  831.  
  832.     stack_start = USRSTACK;
  833.     stack_end = USRSTACK + ctob(UPAGES);
  834. }
  835.  
  836. set_paddr_command(arg)
  837.     char *arg;
  838. {
  839.     u_int uaddr;
  840.  
  841.     if (!arg)
  842.         error_no_arg("ps-style address for new current process");
  843.     if (!kernel_debugging)
  844.         error("not debugging kernel");
  845.     if (lowram == 0)
  846.         error("need kernel core file");
  847.     uaddr = (u_int) parse_and_eval_address(arg);
  848. #ifndef NEWVM
  849.     read_pcb(ctob(uaddr));
  850. #else
  851.     /* p_addr is now a pcb virtual address */
  852.     read_pcb(vtophys(uaddr));
  853.     curpcb = uaddr;
  854. #endif
  855.  
  856.     flush_cached_frames();
  857.     set_current_frame(create_new_frame(read_register(FP_REGNUM), read_pc()));
  858.     select_frame(get_current_frame(), 0);
  859. }
  860.  
  861. /*
  862.  * read len bytes from kernel virtual address 'addr' into local 
  863.  * buffer 'buf'.  Return 0 if read ok, 1 otherwise.  On read
  864.  * errors, portion of buffer not read is zeroed.
  865.  */
  866. kernel_core_file_hook(addr, buf, len)
  867.     CORE_ADDR addr;
  868.     char *buf;
  869.     int len;
  870. {
  871.     int i;
  872.     CORE_ADDR paddr;
  873.  
  874.     while (len > 0) {
  875.         paddr = vtophys(addr);
  876.         if (paddr == ~0) {
  877.             bzero(buf, len);
  878.             return (1);
  879.         }
  880.         /* we can't read across a page boundary */
  881.         i = min(len, NBPG - (addr & PGOFSET));
  882.         if (physrd(paddr, buf, i)) {
  883.             bzero(buf, len);
  884.             return (1);
  885.         }
  886.         buf += i;
  887.         addr += i;
  888.         len -= i;
  889.     }
  890.     return (0);
  891. }
  892. #endif
  893.  
  894. core_file_command(filename, from_tty)
  895.     char           *filename;
  896.     int             from_tty;
  897. {
  898.     int             val;
  899.     extern char     registers[];
  900.     unsigned int    reg_offset;
  901. #ifdef KERNELDEBUG
  902.     struct stat stb;
  903. #endif
  904.     struct user     u;
  905.  
  906.     /*
  907.      * Discard all vestiges of any previous core file and mark data and
  908.      * stack spaces as empty.  
  909.      */
  910.     if (corefile)
  911.         free(corefile);
  912.     corefile = 0;
  913.     core_file_hook = 0;
  914.  
  915.     if (corechan >= 0)
  916.         close(corechan);
  917.     corechan = -1;
  918.  
  919.     /* Now, if a new core file was specified, open it and digest it.  */
  920.  
  921.     if (filename == 0) {
  922.         if (from_tty)
  923.             printf("No core file now.\n");
  924.         return;
  925.     }
  926.     filename = tilde_expand(filename);
  927.     make_cleanup(free, filename);
  928.     if (have_inferior_p())
  929.         error("To look at a core file, you must kill the inferior with \"kill\".");
  930.     corechan = open(filename, O_RDONLY, 0);
  931.     if (corechan < 0)
  932.         perror_with_name(filename);
  933.  
  934. #ifdef KERNELDEBUG
  935.     fstat(corechan, &stb);
  936.  
  937.     if (kernel_debugging) {
  938.         setup_kernel_debugging();
  939.         core_file_hook = kernel_core_file_hook;
  940.     } else if ((stb.st_mode & S_IFMT) == S_IFCHR &&
  941.            stb.st_rdev == makedev(2, 1)) {
  942.         /* looking at /dev/kmem */
  943.         data_offset = data_start = KERNOFF;
  944.         data_end = ~0; /* XXX */
  945.         stack_end = stack_start = data_end;
  946.     } else
  947. #endif
  948.     {
  949.         val = myread(corechan, &u, sizeof u);
  950.         if (val < 0)
  951.             perror_with_name("Not a core file: reading upage");
  952.         if (val != sizeof u)
  953.             error("Not a core file: could only read %d bytes", val);
  954.  
  955.         /*
  956.          * We are depending on exec_file_command having been
  957.          * called previously to set exec_data_start.  Since
  958.          * the executable and the core file share the same
  959.          * text segment, the address of the data segment will
  960.          * be the same in both.  
  961.          */
  962.         data_start = exec_data_start;
  963.  
  964. #ifndef NEWVM
  965.         data_end = data_start + NBPG * u.u_dsize;
  966.         stack_start = stack_end - NBPG * u.u_ssize;
  967.         data_offset = NBPG * UPAGES;
  968.         stack_offset = NBPG * (UPAGES + u.u_dsize);
  969.  
  970.         /*
  971.          * Some machines put an absolute address in here and
  972.          * some put the offset in the upage of the regs.  
  973.          */
  974.         reg_offset = (int) u.u_ar0 - KERNEL_U_ADDR;
  975. #else
  976.         data_end = data_start +
  977.             NBPG * u.u_kproc.kp_eproc.e_vm.vm_dsize;
  978.         stack_start = stack_end -
  979.             NBPG * u.u_kproc.kp_eproc.e_vm.vm_ssize;
  980.         data_offset = NBPG * UPAGES;
  981.         stack_offset = NBPG *
  982.             (UPAGES + u.u_kproc.kp_eproc.e_vm.vm_dsize);
  983.  
  984.         reg_offset = (int) u.u_kproc.kp_proc.p_regs - USRSTACK;
  985. #endif
  986.  
  987.         /*
  988.          * I don't know where to find this info. So, for now,
  989.          * mark it as not available.  
  990.          */
  991.         N_SET_MAGIC(core_aouthdr, 0);
  992.  
  993.         /*
  994.          * Read the register values out of the core file and
  995.          * store them where `read_register' will find them.  
  996.          */
  997.         {
  998.             register int    regno;
  999.  
  1000.             for (regno = 0; regno < NUM_REGS; regno++) {
  1001.                 char buf[MAX_REGISTER_RAW_SIZE];
  1002.  
  1003.                 val = lseek(corechan, register_addr(regno, reg_offset), 0);
  1004.                 if (val < 0
  1005.                     || (val = myread(corechan, buf, sizeof buf)) < 0) {
  1006.                     char *buffer = (char *) alloca(strlen(reg_names[regno]) + 30);
  1007.                     strcpy(buffer, "Reading register ");
  1008.                     strcat(buffer, reg_names[regno]);
  1009.                     perror_with_name(buffer);
  1010.                 }
  1011.                 supply_register(regno, buf);
  1012.             }
  1013.         }
  1014.     }
  1015.     if (filename[0] == '/')
  1016.         corefile = savestring(filename, strlen(filename));
  1017.     else
  1018.         corefile = concat(current_directory, "/", filename);
  1019.  
  1020.     set_current_frame(create_new_frame(read_register(FP_REGNUM),
  1021.                        read_pc()));
  1022.     select_frame(get_current_frame(), 0);
  1023.     validate_files();
  1024. }
  1025.  
  1026.  
  1027. exec_file_command(filename, from_tty)
  1028.     char           *filename;
  1029.     int             from_tty;
  1030. {
  1031.     int             val;
  1032.  
  1033.     /*
  1034.      * Eliminate all traces of old exec file. Mark text segment as empty.  
  1035.      */
  1036.  
  1037.     if (execfile)
  1038.         free(execfile);
  1039.     execfile = 0;
  1040.     data_start = 0;
  1041.     data_end -= exec_data_start;
  1042.     text_start = 0;
  1043.     text_end = 0;
  1044.     exec_data_start = 0;
  1045.     exec_data_end = 0;
  1046.     if (execchan >= 0)
  1047.         close(execchan);
  1048.     execchan = -1;
  1049.  
  1050.     /* Now open and digest the file the user requested, if any.  */
  1051.  
  1052.     if (filename) {
  1053.         filename = tilde_expand(filename);
  1054.         make_cleanup(free, filename);
  1055.  
  1056.         execchan = openp(getenv("PATH"), 1, filename, O_RDONLY, 0,
  1057.                  &execfile);
  1058.         if (execchan < 0)
  1059.             perror_with_name(filename);
  1060.  
  1061.         {
  1062.             struct stat     st_exec;
  1063.  
  1064. #ifdef HEADER_SEEK_FD
  1065.             HEADER_SEEK_FD(execchan);
  1066. #endif
  1067.  
  1068.             val = myread(execchan, &exec_aouthdr, sizeof(AOUTHDR));
  1069.  
  1070.             if (val < 0)
  1071.                 perror_with_name(filename);
  1072.  
  1073.             text_start = N_TXTADDR(exec_aouthdr);
  1074.             exec_data_start = N_DATADDR(exec_aouthdr);
  1075.  
  1076.             text_offset = N_TXTOFF(exec_aouthdr);
  1077.             exec_data_offset = N_TXTOFF(exec_aouthdr) + exec_aouthdr.a_text;
  1078.  
  1079.             text_end = text_start + exec_aouthdr.a_text;
  1080.             exec_data_end = exec_data_start + exec_aouthdr.a_data;
  1081.             data_start = exec_data_start;
  1082.             data_end += exec_data_start;
  1083.  
  1084.             fstat(execchan, &st_exec);
  1085.             exec_mtime = st_exec.st_mtime;
  1086.         }
  1087.  
  1088.         validate_files();
  1089.     } else if (from_tty)
  1090.         printf("No exec file now.\n");
  1091.  
  1092.     /* Tell display code (if any) about the changed file name.  */
  1093.     if (exec_file_display_hook)
  1094.         (*exec_file_display_hook) (filename);
  1095. }
  1096.  
  1097. int dummy_code[] = {
  1098.     0x4e714eb9,        /* nop, jsr @#32323232 */
  1099.     0x32323232,
  1100. #define DUMMY_CALL_INDEX 1
  1101.     0x4e424e71,        /* trap 2, nop */
  1102. };
  1103.  
  1104. /*
  1105.  * Build `dummy' call instructions on inferior's stack to cause
  1106.  * it to call a subroutine.
  1107.  *
  1108.  * N.B. - code in wait_for_inferior requires that sp < pc < fp when
  1109.  * we take the trap 2 above so it will recognize that we stopped
  1110.  * at a `dummy' call.  So, after the call sp is *not* decremented
  1111.  * to clean the arguments, code & other stuff we lay on the stack.
  1112.  * Since the regs are restored to saved values at the breakpoint,
  1113.  * sp will get reset correctly.  Also, this restore means we don't
  1114.  * have to construct frame linkage info to save pc & fp.  The lack
  1115.  * of frame linkage means we can't do a backtrace, etc., if the
  1116.  * called function gets a fault or hits a breakpoint but code in
  1117.  * run_stack_dummy makes this impossible anyway.
  1118.  */
  1119. CORE_ADDR
  1120. setup_dummy(sp, funaddr, nargs, args, struct_return_bytes, pushfn)
  1121.     CORE_ADDR sp;
  1122.     CORE_ADDR funaddr;
  1123.     int nargs;
  1124.     value *args;
  1125.     int struct_return_bytes;
  1126.     CORE_ADDR (*pushfn)();
  1127. {
  1128.     int padding, i;
  1129.     CORE_ADDR top = sp, struct_addr, pc;
  1130.  
  1131.     i = arg_stacklen(nargs, args) + struct_return_bytes
  1132.         + sizeof(dummy_code);
  1133.     if (i & 3)
  1134.         padding = 4 - (i & 3);
  1135.     else
  1136.         padding = 0;
  1137.     pc = sp - sizeof(dummy_code);
  1138.     sp = pc - padding - struct_return_bytes;
  1139.     struct_addr = sp;
  1140.     while (--nargs >= 0)
  1141.         sp = (*pushfn)(sp, *args++);
  1142.     if (struct_return_bytes)
  1143.         STORE_STRUCT_RETURN(struct_addr, sp);
  1144.     write_register(SP_REGNUM, sp);
  1145.  
  1146.     dummy_code[DUMMY_CALL_INDEX] = (int)funaddr;
  1147.     write_memory(pc, (char *)dummy_code, sizeof(dummy_code));
  1148.  
  1149.     return pc;
  1150. }
  1151.  
  1152. void
  1153. _initialize_hp300bsd_dep()
  1154. {
  1155. #ifdef KERNELDEBUG
  1156.     add_com ("process-address", class_obscure, set_paddr_command,
  1157.          "The process identified by (ps-style) ADDR becomes the\n\
  1158. \"current\" process context for kernel debugging.");
  1159.     add_com_alias ("paddr", "process-address", class_obscure, 0);
  1160. #endif
  1161. }
  1162.