home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.bin / gdb / config / i386bsd-dep.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-26  |  44.6 KB  |  1,862 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[] = "@(#)i386bsd-dep.c    6.10 (Berkeley) 6/26/91";
  11. #endif /* not lint */
  12.  
  13. /* Low level interface to ptrace, for GDB when running on the Intel 386.
  14.    Copyright (C) 1988, 1989 Free Software Foundation, Inc.
  15.  
  16. This file is part of GDB.
  17.  
  18. GDB is free software; you can redistribute it and/or modify
  19. it under the terms of the GNU General Public License as published by
  20. the Free Software Foundation; either version 1, or (at your option)
  21. any later version.
  22.  
  23. GDB is distributed in the hope that it will be useful,
  24. but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  26. GNU General Public License for more details.
  27.  
  28. You should have received a copy of the GNU General Public License
  29. along with GDB; see the file COPYING.  If not, write to
  30. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  31.  
  32. #include <stdio.h>
  33. #include "defs.h"
  34. #include "param.h"
  35. #include "frame.h"
  36. #include "inferior.h"
  37. #include "value.h"
  38.  
  39. #include <sys/param.h>
  40. #include <sys/dir.h>
  41. #include <signal.h>
  42. #include <sys/ioctl.h>
  43. #include <fcntl.h>
  44.  
  45. #include <a.out.h>
  46.  
  47. #ifndef N_SET_MAGIC
  48. #define N_SET_MAGIC(exec, val) ((exec).a_magic = (val))
  49. #endif
  50.  
  51. #include <sys/time.h>
  52. #include <sys/resource.h>
  53. #include <sys/uio.h>
  54. #define curpcb Xcurpcb    /* XXX avoid leaking declaration from pcb.h */
  55. #include <sys/user.h>
  56. #undef curpcb
  57. #include <sys/file.h>
  58. #include <sys/stat.h>
  59. #include <sys/ptrace.h>
  60.  
  61. #include <machine/reg.h>
  62.  
  63. #ifdef KERNELDEBUG
  64. #ifndef NEWVM
  65. #include <sys/vmmac.h>
  66. #include <machine/pte.h>
  67. #else
  68. #include <sys/proc.h>    /* for curproc */
  69. #endif
  70. #include <machine/vmparam.h>
  71. #include <machine/cpu.h>
  72. #include <ctype.h>
  73. #include "symtab.h"    /* XXX */
  74.  
  75. #undef    vtophys        /* XXX */
  76.  
  77. extern int kernel_debugging;
  78.  
  79. #define    KERNOFF        ((unsigned)KERNBASE)
  80. #ifndef NEWVM
  81. #define    INKERNEL(x)    ((x) >= KERNOFF && (x) < KERNOFF + ctob(slr))
  82. #define INUPAGE(x)    \
  83.     ((x) >= KERNEL_U_ADDR && (x) < KERNEL_U_ADDR + NBPG)
  84. #else
  85. #define    INKERNEL(x)    ((x) >= KERNOFF)
  86. #endif
  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. static int found_pcb;
  99. #ifdef NEWVM
  100. static CORE_ADDR curpcb;
  101. static CORE_ADDR kstack;
  102. #endif
  103.  
  104. static void setregmap();
  105.  
  106. extern int errno;
  107.  
  108. /*
  109.  * This function simply calls ptrace with the given arguments.  It exists so
  110.  * that all calls to ptrace are isolated in this machine-dependent file. 
  111.  */
  112. int
  113. call_ptrace(request, pid, arg3, arg4)
  114.     int request;
  115.     pid_t pid;
  116.     caddr_t arg3;
  117.     int arg4;
  118. {
  119.     return(ptrace(request, pid, arg3, arg4));
  120. }
  121.  
  122. kill_inferior()
  123. {
  124.     if (remote_debugging) {
  125. #ifdef KERNELDEBUG
  126.         if (kernel_debugging)
  127.             /*
  128.              * It's a very, very bad idea to go away leaving
  129.              * breakpoints in a remote kernel or to leave it
  130.              * stopped at a breakpoint. 
  131.              */
  132.             clear_breakpoints();
  133. #endif
  134.         remote_close(0);
  135.         inferior_died();
  136.     } else if (inferior_pid != 0) {
  137.         ptrace(PT_KILL, inferior_pid, 0, 0);
  138.         wait(0);
  139.         inferior_died();
  140.     }
  141. }
  142.  
  143. /*
  144.  * This is used when GDB is exiting.  It gives less chance of error.
  145.  */
  146. kill_inferior_fast()
  147. {
  148.     if (remote_debugging) {
  149. #ifdef KERNELDEBUG
  150.         if (kernel_debugging)
  151.             clear_breakpoints();
  152. #endif
  153.         remote_close(0);
  154.         return;
  155.     }
  156.     if (inferior_pid == 0)
  157.         return;
  158.  
  159.     ptrace(PT_KILL, inferior_pid, 0, 0);
  160.     wait(0);
  161. }
  162.  
  163. /*
  164.  * Resume execution of the inferior process. If STEP is nonzero, single-step
  165.  * it. If SIGNAL is nonzero, give it that signal.  
  166.  */
  167. void
  168. resume(step, signal)
  169.     int step;
  170.     int signal;
  171. {
  172.     errno = 0;
  173.     if (remote_debugging)
  174.         remote_resume(step, signal);
  175.     else {
  176.         ptrace(step ? PT_STEP : PT_CONTINUE, inferior_pid,
  177.                PT_ADDR_ANY, signal);
  178.         if (errno)
  179.             perror_with_name("ptrace");
  180.     }
  181. }
  182.  
  183. #ifdef ATTACH_DETACH
  184. extern int attach_flag;
  185.  
  186. /*
  187.  * Start debugging the process whose number is PID.
  188.  */
  189. attach(pid)
  190.     int pid;
  191. {
  192.     errno = 0;
  193.     ptrace(PT_ATTACH, pid, 0, 0);
  194.     if (errno)
  195.         perror_with_name("ptrace");
  196.     attach_flag = 1;
  197.     return pid;
  198. }
  199.  
  200. /*
  201.  * Stop debugging the process whose number is PID and continue it
  202.  * with signal number SIGNAL.  SIGNAL = 0 means just continue it.  
  203.  */
  204. void
  205. detach(signal)
  206.     int signal;
  207. {
  208.     errno = 0;
  209.     ptrace(PT_DETACH, inferior_pid, PT_ADDR_ANY, signal);
  210.     if (errno)
  211.         perror_with_name("ptrace");
  212.     attach_flag = 0;
  213. }
  214. #endif    /* ATTACH_DETACH */
  215.  
  216. static unsigned int
  217. get_register_offset()
  218. {
  219.     unsigned int offset;
  220.     struct user u;    /* XXX */
  221.     unsigned int    flags = (char *) &u.u_pcb.pcb_flags - (char *) &u;
  222.  
  223.     setregmap(ptrace(PT_READ_U, inferior_pid, (caddr_t)flags, 0));
  224.  
  225. #ifdef NEWVM
  226.     offset = (char *) &u.u_kproc.kp_proc.p_regs - (char *) &u;
  227.     offset = ptrace(PT_READ_U, inferior_pid, (caddr_t)offset, 0) -
  228.         USRSTACK;
  229. #else
  230.     offset = (char *) &u.u_ar0 - (char *) &u;
  231.     offset = ptrace(PT_READ_U, inferior_pid, (caddr_t)offset, 0) -
  232.         KERNEL_U_ADDR;
  233. #endif
  234.  
  235.     return offset;
  236. }
  237.  
  238. void
  239. fetch_inferior_registers()
  240. {
  241.     register int    regno;
  242.     register unsigned int regaddr;
  243.     char            buf[MAX_REGISTER_RAW_SIZE];
  244.     register int    i;
  245.     unsigned int    offset;
  246.  
  247.     if (remote_debugging) {
  248.         extern char     registers[];
  249.  
  250.         remote_fetch_registers(registers);
  251.         return;
  252.     }
  253.  
  254.     offset = get_register_offset();
  255.  
  256.     for (regno = 0; regno < NUM_REGS; regno++) {
  257.         regaddr = register_addr(regno, offset);
  258.         for (i = 0; i < REGISTER_RAW_SIZE(regno); i += sizeof(int)) {
  259.             *(int *)&buf[i] = ptrace(PT_READ_U, inferior_pid, 
  260.                          (caddr_t)regaddr, 0);
  261.             regaddr += sizeof(int);
  262.         }
  263.         supply_register(regno, buf);
  264.     }
  265. }
  266.  
  267. /*
  268.  * Store our register values back into the inferior. If REGNO is -1, do this
  269.  * for all registers. Otherwise, REGNO specifies which register (so we can
  270.  * save time).  
  271.  */
  272. store_inferior_registers(regno)
  273.     int             regno;
  274. {
  275.     register unsigned int regaddr;
  276.     char            buf[80];
  277.     extern char     registers[];
  278.     register int    i;
  279.     unsigned int    offset;
  280.  
  281.     if (remote_debugging) {
  282.         extern char     registers[];
  283.  
  284.         remote_store_registers(registers);
  285.         return;
  286.     }
  287.  
  288.     offset = get_register_offset();
  289.  
  290.     if (regno >= 0) {
  291.         regaddr = register_addr(regno, offset);
  292.         for (i = 0; i < REGISTER_RAW_SIZE(regno); i += sizeof(int)) {
  293.             errno = 0;
  294.             ptrace(PT_WRITE_U, inferior_pid, (caddr_t)regaddr,
  295.                  *(int *) ®isters[REGISTER_BYTE(regno) + i]);
  296.             if (errno != 0) {
  297.                 sprintf(buf, "writing register number %d(%d)",
  298.                     regno, i);
  299.                 perror_with_name(buf);
  300.             }
  301.             regaddr += sizeof(int);
  302.         }
  303.     } else
  304.         for (regno = 0; regno < NUM_REGS; regno++) {
  305.             regaddr = register_addr(regno, offset);
  306.             for (i = 0; i < REGISTER_RAW_SIZE(regno);
  307.                  i += sizeof(int)) {
  308.                 errno = 0;
  309.                 ptrace(PT_WRITE_U, inferior_pid,
  310.                        (caddr_t)regaddr,
  311.                        *(int *) ®isters[REGISTER_BYTE(regno) + i]);
  312.                 if (errno != 0) {
  313.                     sprintf(buf,
  314.                        "writing register number %d(%d)", 
  315.                         regno, i);
  316.                     perror_with_name(buf);
  317.                 }
  318.                 regaddr += sizeof(int);
  319.             }
  320.         }
  321. }
  322.  
  323. /*
  324.  * Copy LEN bytes from inferior's memory starting at MEMADDR to debugger
  325.  * memory starting at MYADDR. On failure (cannot read from inferior, usually
  326.  * because address is out of bounds) returns the value of errno. 
  327.  */
  328. int
  329. read_inferior_memory(memaddr, myaddr, len)
  330.     CORE_ADDR       memaddr;
  331.     char           *myaddr;
  332.     int             len;
  333. {
  334.     register int i;
  335.     /* Round starting address down to longword boundary.  */
  336.     register CORE_ADDR addr = memaddr & -sizeof(int);
  337.     /* Round ending address up; get number of longwords that makes.  */
  338.     register int count = (((memaddr + len) - addr) + sizeof(int) - 1) / 
  339.                 sizeof(int);
  340.     /* Allocate buffer of that many longwords.  */
  341.     register int *buffer = (int *) alloca(count * sizeof(int));
  342.     extern int errno;
  343.  
  344.     if (remote_debugging)
  345.         return (remote_read_inferior_memory(memaddr, myaddr, len));
  346.  
  347.     /* Read all the longwords */
  348.     errno = 0;
  349.     for (i = 0; i < count && errno == 0; i++, addr += sizeof(int)) 
  350.         buffer[i] = ptrace(PT_READ_I, inferior_pid, (caddr_t)addr, 0);
  351.  
  352.     /* Copy appropriate bytes out of the buffer.  */
  353.     bcopy((char *) buffer + (memaddr & (sizeof(int) - 1)), myaddr, len);
  354.     return(errno);
  355. }
  356.  
  357. /*
  358.  * Copy LEN bytes of data from debugger memory at MYADDR to inferior's memory
  359.  * at MEMADDR. On failure (cannot write the inferior) returns the value of
  360.  * errno.  
  361.  */
  362.  
  363. int
  364. write_inferior_memory(memaddr, myaddr, len)
  365.     CORE_ADDR       memaddr;
  366.     char           *myaddr;
  367.     int             len;
  368. {
  369.     register int    i;
  370.     /* Round starting address down to longword boundary.  */
  371.     register CORE_ADDR addr = memaddr & -sizeof(int);
  372.     /* Round ending address up; get number of longwords that makes.  */
  373.     register int count = (((memaddr + len) - addr) + sizeof(int) - 1) / 
  374.                 sizeof(int);
  375.     /* Allocate buffer of that many longwords.  */
  376.     register int *buffer = (int *) alloca(count * sizeof(int));
  377.     extern int errno;
  378.  
  379.     /*
  380.      * Fill start and end extra bytes of buffer with existing memory
  381.      * data.  
  382.      */
  383.     if (remote_debugging)
  384.         return (remote_write_inferior_memory(memaddr, myaddr, len));
  385.  
  386.     /*
  387.      * Fill start and end extra bytes of buffer with existing memory
  388.      * data.  
  389.      */
  390.     buffer[0] = ptrace(PT_READ_I, inferior_pid, (caddr_t)addr, 0);
  391.  
  392.     if (count > 1)
  393.         buffer[count - 1] = ptrace(PT_READ_I, inferior_pid,
  394.                  (caddr_t)addr + (count - 1) * sizeof(int), 0);
  395.  
  396.     /* Copy data to be written over corresponding part of buffer */
  397.  
  398.     bcopy(myaddr, (char *) buffer + (memaddr & (sizeof(int) - 1)), len);
  399.  
  400.     /* Write the entire buffer.  */
  401.  
  402.     errno = 0;
  403.     for (i = 0; i < count && errno == 0; i++, addr += sizeof(int))
  404.         ptrace(PT_WRITE_I, inferior_pid, (caddr_t)addr, buffer[i]);
  405.  
  406.     return(errno);
  407. }
  408.  
  409.  
  410. /*
  411.  * Work with core dump and executable files, for GDB. 
  412.  * This code would be in core.c if it weren't machine-dependent. 
  413.  */
  414.  
  415. #ifndef N_TXTADDR
  416. #define N_TXTADDR(hdr) 0
  417. #endif                /* no N_TXTADDR */
  418.  
  419. #ifndef N_DATADDR
  420. #define N_DATADDR(hdr) hdr.a_text
  421. #endif                /* no N_DATADDR */
  422.  
  423. /*
  424.  * Make COFF and non-COFF names for things a little more compatible to reduce
  425.  * conditionals later.  
  426.  */
  427.  
  428. #ifndef AOUTHDR
  429. #define AOUTHDR struct exec
  430. #endif
  431.  
  432. extern char *sys_siglist[];
  433.  
  434.  
  435. /* Hook for `exec_file_command' command to call.  */
  436.  
  437. extern void (*exec_file_display_hook) ();
  438.    
  439. /* File names of core file and executable file.  */
  440.  
  441. extern char *corefile;
  442. extern char *execfile;
  443.  
  444. /* Descriptors on which core file and executable file are open.
  445.    Note that the execchan is closed when an inferior is created
  446.    and reopened if the inferior dies or is killed.  */
  447.  
  448. extern int corechan;
  449. extern int execchan;
  450.  
  451. /* Last modification time of executable file.
  452.    Also used in source.c to compare against mtime of a source file.  */
  453.  
  454. extern int exec_mtime;
  455.  
  456. /* Virtual addresses of bounds of the two areas of memory in the core file.  */
  457.  
  458. extern CORE_ADDR data_start;
  459. extern CORE_ADDR data_end;
  460. extern CORE_ADDR stack_start;
  461. extern CORE_ADDR stack_end;
  462.  
  463. /* Virtual addresses of bounds of two areas of memory in the exec file.
  464.    Note that the data area in the exec file is used only when there is no core file.  */
  465.  
  466. extern CORE_ADDR text_start;
  467. extern CORE_ADDR text_end;
  468.  
  469. extern CORE_ADDR exec_data_start;
  470. extern CORE_ADDR exec_data_end;
  471.  
  472. /* Address in executable file of start of text area data.  */
  473.  
  474. extern int text_offset;
  475.  
  476. /* Address in executable file of start of data area data.  */
  477.  
  478. extern int exec_data_offset;
  479.  
  480. /* Address in core file of start of data area data.  */
  481.  
  482. extern int data_offset;
  483.  
  484. /* Address in core file of start of stack area data.  */
  485.  
  486. extern int stack_offset;
  487.  
  488. /* a.out header saved in core file.  */
  489.   
  490. extern AOUTHDR core_aouthdr;
  491.  
  492. /* a.out header of exec file.  */
  493.  
  494. extern AOUTHDR exec_aouthdr;
  495.  
  496. extern void validate_files ();
  497.  
  498. extern int (*core_file_hook)();
  499.  
  500. #ifdef KERNELDEBUG
  501. /*
  502.  * Kernel debugging routines.
  503.  */
  504.  
  505. #define    IOTOP    0x100000    /* XXX should get this from include file */
  506. #define    IOBASE     0xa0000    /* XXX should get this from include file */
  507.  
  508. static CORE_ADDR file_offset;
  509. static CORE_ADDR lowram;
  510. static CORE_ADDR sbr;
  511. static CORE_ADDR slr;
  512. static struct pcb pcb;
  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.  * XXX the HP code does this differently with NEWVM
  533.  */
  534. static int
  535. ok_to_cache(addr, len)
  536. {
  537.     static CORE_ADDR atdevbase;
  538.  
  539.     if (! atdevbase)
  540.         atdevbase = ksym_lookup("atdevbase");
  541.  
  542.     if (addr >= atdevbase && addr < atdevbase + (IOTOP - IOBASE))
  543.         return (0);
  544.  
  545.     return (1);
  546. }
  547.  
  548. static
  549. physrd(addr, dat, len)
  550.     u_int addr;
  551.     char *dat;
  552. {
  553.     if (lseek(corechan, addr - file_offset, L_SET) == -1)
  554.         return (-1);
  555.     if (read(corechan, dat, len) != len)
  556.         return (-1);
  557.  
  558.     return (0);
  559. }
  560.  
  561. /*
  562.  * When looking at kernel data space through /dev/mem or with a core file, do
  563.  * virtual memory mapping.
  564.  */
  565. #ifdef NEWVM
  566. static CORE_ADDR
  567. vtophys(addr)
  568.     CORE_ADDR addr;
  569. {
  570.     CORE_ADDR v;
  571.     struct pte pte;
  572.     static CORE_ADDR PTD = -1;
  573.     CORE_ADDR current_ptd;
  574.  
  575.     /*
  576.      * If we're looking at the kernel stack,
  577.      * munge the address to refer to the user space mapping instead;
  578.      * that way we get the requested process's kstack, not the running one.
  579.      */
  580.     if (addr >= kstack && addr < kstack + ctob(UPAGES))
  581.         addr = (addr - kstack) + curpcb;
  582.  
  583.     /*
  584.      * We may no longer have a linear system page table...
  585.      *
  586.      * Here's the scoop.  IdlePTD contains the physical address
  587.      * of a page table directory that always maps the kernel.
  588.      * IdlePTD is in memory that is mapped 1-to-1, so we can
  589.      * find it easily given its 'virtual' address from ksym_lookup().
  590.      * For hysterical reasons, the value of IdlePTD is stored in sbr.
  591.      *
  592.      * To look up a kernel address, we first convert it to a 1st-level
  593.      * address and look it up in IdlePTD.  This gives us the physical
  594.      * address of a page table page; we extract the 2nd-level part of
  595.      * VA and read the 2nd-level pte.  Finally, we add the offset part
  596.      * of the VA into the physical address from the pte and return it.
  597.      *
  598.      * User addresses are a little more complicated.  If we don't have
  599.      * a current PCB from read_pcb(), we use PTD, which is the (fixed)
  600.      * virtual address of the current ptd.  Since it's NOT in 1-to-1
  601.      * kernel space, we must look it up using IdlePTD.  If we do have
  602.      * a pcb, we get the ptd from pcb_ptd.
  603.      */
  604.  
  605.     if (INKERNEL(addr))
  606.         current_ptd = sbr;
  607.     else if (found_pcb == 0) {
  608.         if (PTD == -1)
  609.             PTD = vtophys(ksym_lookup("PTD"));
  610.         current_ptd = PTD;
  611.     } else
  612.         current_ptd = pcb.pcb_ptd;
  613.  
  614.     /*
  615.      * Read the first-level page table (ptd).
  616.      */
  617.     v = current_ptd + ((unsigned)addr >> PD_SHIFT) * sizeof pte;
  618.     if (physrd(v, (char *)&pte, sizeof pte) || pte.pg_v == 0)
  619.         return (~0);
  620.  
  621.     /*
  622.      * Read the second-level page table.
  623.      */
  624.     v = i386_ptob(pte.pg_pfnum) + ((addr&PT_MASK) >> PG_SHIFT) * sizeof pte;
  625.     if (physrd(v, (char *) &pte, sizeof(pte)) || pte.pg_v == 0)
  626.         return (~0);
  627.  
  628.     addr = i386_ptob(pte.pg_pfnum) + (addr & PGOFSET);
  629. #if 0
  630.     printf("vtophys(%x) -> %x\n", oldaddr, addr);
  631. #endif
  632.     return (addr);
  633. }
  634. #else
  635. static CORE_ADDR
  636. vtophys(addr)
  637.     CORE_ADDR addr;
  638. {
  639.     CORE_ADDR v;
  640.     struct pte pte;
  641.     CORE_ADDR oldaddr = addr;
  642.  
  643.     if (found_pcb == 0 && INUPAGE(addr)) {
  644.         static CORE_ADDR pSwtchmap;
  645.  
  646.         if (pSwtchmap == 0)
  647.             pSwtchmap = vtophys(ksym_lookup("Swtchmap"));
  648.         addr = pSwtchmap;
  649.     } else if (INKERNEL(addr)) {
  650.         /*
  651.          * In system space get system pte.  If valid or reclaimable
  652.          * then physical address is combination of its page number
  653.          * and the page offset of the original address.
  654.          */
  655.         addr = smxtob(btop(addr - KERNOFF)) - KERNOFF;
  656.     } else {
  657.         v = btop(addr);
  658.         if (v < pcb.pcb_p0lr)
  659.             addr = (CORE_ADDR) pcb.pcb_p0br +
  660.                 v * sizeof (struct pte);
  661.         else if (v >= pcb.pcb_p1lr && v < P1PAGES)
  662.             addr = (CORE_ADDR) pcb.pcb_p0br +
  663.                 ((pcb.pcb_szpt * NPTEPG - HIGHPAGES) -
  664.                  (BTOPUSRSTACK - v)) * sizeof (struct pte);
  665.         else
  666.             return (~0);
  667.  
  668.         /*
  669.          * For p0/p1 address, user-level page table should be in
  670.          * kernel vm.  Do second-level indirect by recursing.
  671.          */
  672.         if (!INKERNEL(addr))
  673.             return (~0);
  674.  
  675.         addr = vtophys(addr);
  676.     }
  677.     /*
  678.      * Addr is now address of the pte of the page we are interested in;
  679.      * get the pte and paste up the physical address.
  680.      */
  681.     if (physrd(addr, (char *) &pte, sizeof(pte)))
  682.         return (~0);
  683.  
  684.     if (pte.pg_v == 0 && (pte.pg_fod || pte.pg_pfnum == 0))
  685.         return (~0);
  686.  
  687.     addr = (CORE_ADDR)ptob(pte.pg_pfnum) + (oldaddr & PGOFSET);
  688. #if 0
  689.     printf("vtophys(%x) -> %x\n", oldaddr, addr);
  690. #endif
  691.     return (addr);
  692. }
  693. #endif
  694.  
  695. static
  696. kvread(addr)
  697.     CORE_ADDR addr;
  698. {
  699.     CORE_ADDR paddr = vtophys(addr);
  700.  
  701.     if (paddr != ~0)
  702.         if (physrd(paddr, (char *)&addr, sizeof(addr)) == 0);
  703.             return (addr);
  704.  
  705.     return (~0);
  706. }
  707.  
  708. static void
  709. read_pcb(uaddr)
  710.      u_int uaddr;
  711. {
  712.     int i;
  713.     int *pcb_regs = (int *)&pcb;
  714.  
  715. #ifdef NEWVM
  716.     if (physrd(uaddr, (char *)&pcb, sizeof pcb))
  717.         error("cannot read pcb at %x\n", uaddr);
  718.     printf("current pcb at %x\n", uaddr);
  719. #else
  720.     if (physrd(uaddr, (char *)&pcb, sizeof pcb))
  721.         error("cannot read pcb at %x\n", uaddr);
  722.     printf("p0br %x p0lr %x p1br %x p1lr %x\n",
  723.            pcb.pcb_p0br, pcb.pcb_p0lr, pcb.pcb_p1br, pcb.pcb_p1lr);
  724. #endif
  725.  
  726.     /*
  727.      * get the register values out of the sys pcb and
  728.      * store them where `read_register' will find them.
  729.      */
  730.     for (i = 0; i < 8; ++i)
  731.         supply_register(i, &pcb_regs[i+10]);
  732.     supply_register(8, &pcb_regs[8]);    /* eip */
  733.     supply_register(9, &pcb_regs[9]);    /* eflags */
  734.     for (i = 10; i < 13; ++i)        /* cs, ss, ds */
  735.         supply_register(i, &pcb_regs[i+9]);
  736.     supply_register(13, &pcb_regs[18]);    /* es */
  737.     for (i = 14; i < 16; ++i)        /* fs, gs */
  738.         supply_register(i, &pcb_regs[i+8]);
  739.  
  740.     /* XXX 80387 registers? */
  741. }
  742.  
  743. static void
  744. setup_kernel_debugging()
  745. {
  746.     struct stat stb;
  747.     int devmem = 0;
  748.     CORE_ADDR addr;
  749.  
  750.     fstat(corechan, &stb);
  751.     if ((stb.st_mode & S_IFMT) == S_IFCHR && stb.st_rdev == makedev(2, 0))
  752.         devmem = 1;
  753.  
  754. #ifdef NEWVM
  755.     physrd(ksym_lookup("IdlePTD") - KERNOFF, &sbr, sizeof sbr);
  756.     slr = 2 * NPTEPG;            /* XXX temporary */
  757.     printf("IdlePTD %x\n", sbr);
  758.     curpcb = ksym_lookup("curpcb") - KERNOFF;
  759.     physrd(curpcb, &curpcb, sizeof curpcb);
  760.     kstack = ksym_lookup("kstack");
  761. #else
  762.     sbr = ksym_lookup("Sysmap");
  763.     slr = ksym_lookup("Syssize");
  764.     printf("sbr %x slr %x\n", sbr, slr);
  765. #endif
  766.  
  767.     /*
  768.      * pcb where "panic" saved registers in first thing in current
  769.      * u area.
  770.      */
  771. #ifdef NEWVM
  772.     read_pcb(vtophys(kstack));
  773. #else
  774.     read_pcb(vtophys(ksym_lookup("u")));
  775. #endif
  776.     found_pcb = 1;
  777.     if (!devmem) {
  778.         /* find stack frame */
  779.         CORE_ADDR panicstr;
  780.         char buf[256];
  781.         register char *cp;
  782.  
  783.         panicstr = kvread(ksym_lookup("panicstr"));
  784.         if (panicstr == ~0)
  785.             return;
  786.         (void) kernel_core_file_hook(panicstr, buf, sizeof(buf));
  787.         for (cp = buf; cp < &buf[sizeof(buf)] && *cp; cp++)
  788.             if (!isascii(*cp) || (!isprint(*cp) && !isspace(*cp)))
  789.                 *cp = '?';
  790.         if (*cp)
  791.             *cp = '\0';
  792.         printf("panic: %s\n", buf);
  793.     }
  794.  
  795.     stack_start = USRSTACK;
  796.     stack_end = USRSTACK + ctob(UPAGES);
  797. }
  798.  
  799. set_paddr_command(arg)
  800.     char *arg;
  801. {
  802.     u_int uaddr;
  803.  
  804.     if (!arg)
  805.         error_no_arg("ps-style address for new current process");
  806.     if (!kernel_debugging)
  807.         error("not debugging kernel");
  808.     uaddr = (u_int) parse_and_eval_address(arg);
  809. #ifndef NEWVM
  810.     read_pcb(ctob(uaddr));
  811. #else
  812.     /* p_addr is now a pcb virtual address */
  813.     read_pcb(vtophys(uaddr));
  814.     curpcb = uaddr;
  815. #endif
  816.  
  817.     flush_cached_frames();
  818.     set_current_frame(create_new_frame(read_register(FP_REGNUM), read_pc()));
  819.     select_frame(get_current_frame(), 0);
  820. }
  821.  
  822. /*
  823.  * read len bytes from kernel virtual address 'addr' into local 
  824.  * buffer 'buf'.  Return 0 if read ok, 1 otherwise.  On read
  825.  * errors, portion of buffer not read is zeroed.
  826.  */
  827. kernel_core_file_hook(addr, buf, len)
  828.     CORE_ADDR addr;
  829.     char *buf;
  830.     int len;
  831. {
  832.     int i;
  833.     CORE_ADDR paddr;
  834.  
  835.     while (len > 0) {
  836.         paddr = vtophys(addr);
  837.         if (paddr == ~0) {
  838.             bzero(buf, len);
  839.             return (1);
  840.         }
  841.         /* we can't read across a page boundary */
  842.         i = min(len, NBPG - (addr & PGOFSET));
  843.         if (physrd(paddr, buf, i)) {
  844.             bzero(buf, len);
  845.             return (1);
  846.         }
  847.         buf += i;
  848.         addr += i;
  849.         len -= i;
  850.     }
  851.     return (0);
  852. }
  853. #endif
  854.  
  855. core_file_command(filename, from_tty)
  856.     char           *filename;
  857.     int             from_tty;
  858. {
  859.     int             val;
  860.     extern char     registers[];
  861. #ifdef KERNELDEBUG
  862.     struct stat stb;
  863. #endif
  864.  
  865.     /*
  866.      * Discard all vestiges of any previous core file and mark data and
  867.      * stack spaces as empty.  
  868.      */
  869.     if (corefile)
  870.         free(corefile);
  871.     corefile = 0;
  872.     core_file_hook = 0;
  873.  
  874.     if (corechan >= 0)
  875.         close(corechan);
  876.     corechan = -1;
  877.  
  878.     /* Now, if a new core file was specified, open it and digest it.  */
  879.  
  880.     if (filename == 0) {
  881.         if (from_tty)
  882.             printf("No core file now.\n");
  883.         return;
  884.     }
  885.     filename = tilde_expand(filename);
  886.     make_cleanup(free, filename);
  887.     if (have_inferior_p())
  888.         error("To look at a core file, you must kill the inferior with \"kill\".");
  889.     corechan = open(filename, O_RDONLY, 0);
  890.     if (corechan < 0)
  891.         perror_with_name(filename);
  892.  
  893. #ifdef KERNELDEBUG
  894.     fstat(corechan, &stb);
  895.  
  896.     if (kernel_debugging) {
  897.         setup_kernel_debugging();
  898.         core_file_hook = kernel_core_file_hook;
  899.     } else if ((stb.st_mode & S_IFMT) == S_IFCHR &&
  900.            stb.st_rdev == makedev(2, 1)) {
  901.         /* looking at /dev/kmem */
  902.         data_offset = data_start = KERNOFF;
  903.         data_end = ~0; /* XXX */
  904.         stack_end = stack_start = data_end;
  905.     } else
  906. #endif
  907.     {
  908.         /*
  909.          * 4.2-style core dump file.
  910.          */
  911.         struct user u;
  912.         unsigned int reg_offset;
  913.  
  914.         val = myread(corechan, &u, sizeof u);
  915.         if (val < 0)
  916.             perror_with_name("Not a core file: reading upage");
  917.         if (val != sizeof u)
  918.             error("Not a core file: could only read %d bytes", val);
  919.  
  920.         /*
  921.          * We are depending on exec_file_command having been
  922.          * called previously to set exec_data_start.  Since
  923.          * the executable and the core file share the same
  924.          * text segment, the address of the data segment will
  925.          * be the same in both.  
  926.          */
  927.         data_start = exec_data_start;
  928.  
  929. #ifndef NEWVM
  930.         data_end = data_start + NBPG * u.u_dsize;
  931.         stack_start = stack_end - NBPG * u.u_ssize;
  932.         data_offset = NBPG * UPAGES;
  933.         stack_offset = NBPG * (UPAGES + u.u_dsize);
  934.  
  935.         /*
  936.          * Some machines put an absolute address in here and
  937.          * some put the offset in the upage of the regs.  
  938.          */
  939.         reg_offset = (int) u.u_ar0 - KERNEL_U_ADDR;
  940. #else
  941.         data_end = data_start +
  942.             NBPG * u.u_kproc.kp_eproc.e_vm.vm_dsize;
  943.         stack_start = stack_end -
  944.             NBPG * u.u_kproc.kp_eproc.e_vm.vm_ssize;
  945.         data_offset = NBPG * UPAGES;
  946.         stack_offset = NBPG *
  947.             (UPAGES + u.u_kproc.kp_eproc.e_vm.vm_dsize);
  948.  
  949.         reg_offset = (int) u.u_kproc.kp_proc.p_regs - USRSTACK;
  950. #endif
  951.  
  952.         setregmap(u.u_pcb.pcb_flags);
  953.  
  954.         /*
  955.          * I don't know where to find this info. So, for now,
  956.          * mark it as not available.  
  957.          */
  958.     /*    N_SET_MAGIC (core_aouthdr, 0);  */
  959.         bzero ((char *) &core_aouthdr, sizeof core_aouthdr);
  960.  
  961.         /*
  962.          * Read the register values out of the core file and
  963.          * store them where `read_register' will find them.  
  964.          */
  965.         {
  966.             register int    regno;
  967.  
  968.             for (regno = 0; regno < NUM_REGS; regno++) {
  969.                 char buf[MAX_REGISTER_RAW_SIZE];
  970.  
  971.                 val = lseek(corechan, register_addr(regno, reg_offset), 0);
  972.                 if (val < 0
  973.                     || (val = myread(corechan, buf, sizeof buf)) < 0) {
  974.                     char *buffer = (char *) alloca(strlen(reg_names[regno]) + 30);
  975.                     strcpy(buffer, "Reading register ");
  976.                     strcat(buffer, reg_names[regno]);
  977.                     perror_with_name(buffer);
  978.                 }
  979.                 supply_register(regno, buf);
  980.             }
  981.         }
  982.     }
  983. #endif
  984.     if (filename[0] == '/')
  985.         corefile = savestring(filename, strlen(filename));
  986.     else
  987.         corefile = concat(current_directory, "/", filename);
  988.  
  989.     set_current_frame(create_new_frame(read_register(FP_REGNUM),
  990.                        read_pc()));
  991.     select_frame(get_current_frame(), 0);
  992.     validate_files();
  993. }
  994.  
  995. exec_file_command(filename, from_tty)
  996.     char           *filename;
  997.     int             from_tty;
  998. {
  999.     int             val;
  1000.  
  1001.     /*
  1002.      * Eliminate all traces of old exec file. Mark text segment as empty.  
  1003.      */
  1004.  
  1005.     if (execfile)
  1006.         free(execfile);
  1007.     execfile = 0;
  1008.     data_start = 0;
  1009.     data_end = 0;
  1010.     stack_start = 0;
  1011.     stack_end = 0;
  1012.     text_start = 0;
  1013.     text_end = 0;
  1014.     exec_data_start = 0;
  1015.     exec_data_end = 0;
  1016.     if (execchan >= 0)
  1017.         close(execchan);
  1018.     execchan = -1;
  1019.  
  1020.     /* Now open and digest the file the user requested, if any.  */
  1021.  
  1022.     if (filename) {
  1023.         filename = tilde_expand(filename);
  1024.         make_cleanup(free, filename);
  1025.  
  1026.         execchan = openp(getenv("PATH"), 1, filename, O_RDONLY, 0,
  1027.                  &execfile);
  1028.         if (execchan < 0)
  1029.             perror_with_name(filename);
  1030.  
  1031.         {
  1032.             struct stat     st_exec;
  1033.  
  1034. #ifdef HEADER_SEEK_FD
  1035.             HEADER_SEEK_FD(execchan);
  1036. #endif
  1037.  
  1038.             val = myread(execchan, &exec_aouthdr, sizeof(AOUTHDR));
  1039.  
  1040.             if (val < 0)
  1041.                 perror_with_name(filename);
  1042.  
  1043. #ifdef KERNELDEBUG
  1044.             if (kernel_debugging) {
  1045.                 /* Gross and disgusting XXX */
  1046.                 text_start = KERNTEXT_BASE;
  1047.                 exec_data_start = KERNTEXT_BASE +
  1048.                     (exec_aouthdr.a_text + 4095) & ~ 4095;
  1049.             } else {
  1050. #endif
  1051.                 text_start = N_TXTADDR(exec_aouthdr);
  1052.                 exec_data_start = N_DATADDR(exec_aouthdr);
  1053. #ifdef KERNELDEBUG
  1054.             }
  1055. #endif
  1056.  
  1057.             text_offset = N_TXTOFF(exec_aouthdr);
  1058.             exec_data_offset = N_TXTOFF(exec_aouthdr) + exec_aouthdr.a_text;
  1059.  
  1060.             text_end = text_start + exec_aouthdr.a_text;
  1061.             exec_data_end = exec_data_start + exec_aouthdr.a_data;
  1062.  
  1063.             fstat(execchan, &st_exec);
  1064.             exec_mtime = st_exec.st_mtime;
  1065.         }
  1066.  
  1067.         validate_files();
  1068.     } else if (from_tty)
  1069.         printf("No exec file now.\n");
  1070.  
  1071.     /* Tell display code (if any) about the changed file name.  */
  1072.     if (exec_file_display_hook)
  1073.         (*exec_file_display_hook) (filename);
  1074. }
  1075.  
  1076. int dummy_code[] = {
  1077.     0xb8909090,        /* nop; nop; nop; movl $0x32323232,%eax */
  1078.     0x32323232,
  1079. #define DUMMY_CALL_INDEX 1
  1080.     0x90ccd0ff,        /* call %eax; int3; nop */
  1081. };
  1082.  
  1083. /*
  1084.  * Build `dummy' call instructions on inferior's stack to cause
  1085.  * it to call a subroutine.
  1086.  *
  1087.  * N.B. - code in wait_for_inferior requires that sp < pc < fp when
  1088.  * we take the trap 2 above so it will recognize that we stopped
  1089.  * at a `dummy' call.  So, after the call sp is *not* decremented
  1090.  * to clean the arguments, code & other stuff we lay on the stack.
  1091.  * Since the regs are restored to saved values at the breakpoint,
  1092.  * sp will get reset correctly.  Also, this restore means we don't
  1093.  * have to construct frame linkage info to save pc & fp.  The lack
  1094.  * of frame linkage means we can't do a backtrace, etc., if the
  1095.  * called function gets a fault or hits a breakpoint but code in
  1096.  * run_stack_dummy makes this impossible anyway.
  1097.  */
  1098. CORE_ADDR
  1099. setup_dummy(sp, funaddr, nargs, args, struct_return_bytes, pushfn)
  1100.     CORE_ADDR sp;
  1101.     CORE_ADDR funaddr;
  1102.     int nargs;
  1103.     value *args;
  1104.     int struct_return_bytes;
  1105.     CORE_ADDR (*pushfn)();
  1106. {
  1107.     int padding, i;
  1108.     CORE_ADDR top = sp, struct_addr, pc;
  1109.  
  1110.     i = arg_stacklen(nargs, args) + struct_return_bytes
  1111.         + sizeof(dummy_code);
  1112.     if (i & 3)
  1113.         padding = 4 - (i & 3);
  1114.     else
  1115.         padding = 0;
  1116.     pc = sp - sizeof(dummy_code);
  1117.     sp = pc - padding - struct_return_bytes;
  1118.     struct_addr = sp;
  1119.     while (--nargs >= 0)
  1120.         sp = (*pushfn)(sp, *args++);
  1121.     if (struct_return_bytes)
  1122.         STORE_STRUCT_RETURN(struct_addr, sp);
  1123.     write_register(SP_REGNUM, sp);
  1124.  
  1125.     dummy_code[DUMMY_CALL_INDEX] = (int)funaddr;
  1126.     write_memory(pc, (char *)dummy_code, sizeof(dummy_code));
  1127.  
  1128.     return pc;
  1129. }
  1130.  
  1131. /* helper functions for m-i386.h */
  1132.  
  1133. /* stdio style buffering to minimize calls to ptrace */
  1134. static CORE_ADDR codestream_next_addr;
  1135. static CORE_ADDR codestream_addr;
  1136. static unsigned char codestream_buf[sizeof (int)];
  1137. static int codestream_off;
  1138. static int codestream_cnt;
  1139.  
  1140. #define codestream_tell() (codestream_addr + codestream_off)
  1141. #define codestream_peek() (codestream_cnt == 0 ? \
  1142.                codestream_fill(1): codestream_buf[codestream_off])
  1143. #define codestream_get() (codestream_cnt-- == 0 ? \
  1144.              codestream_fill(0) : codestream_buf[codestream_off++])
  1145.  
  1146. static unsigned char 
  1147. codestream_fill (peek_flag)
  1148. {
  1149.   codestream_addr = codestream_next_addr;
  1150.   codestream_next_addr += sizeof (int);
  1151.   codestream_off = 0;
  1152.   codestream_cnt = sizeof (int);
  1153.   read_memory (codestream_addr,
  1154.            (unsigned char *)codestream_buf,
  1155.            sizeof (int));
  1156.   
  1157.   if (peek_flag)
  1158.     return (codestream_peek());
  1159.   else
  1160.     return (codestream_get());
  1161. }
  1162.  
  1163. static void
  1164. codestream_seek (place)
  1165. {
  1166.   codestream_next_addr = place & -sizeof (int);
  1167.   codestream_cnt = 0;
  1168.   codestream_fill (1);
  1169.   while (codestream_tell() != place)
  1170.     codestream_get ();
  1171. }
  1172.  
  1173. static void
  1174. codestream_read (buf, count)
  1175.      unsigned char *buf;
  1176. {
  1177.   unsigned char *p;
  1178.   int i;
  1179.   p = buf;
  1180.   for (i = 0; i < count; i++)
  1181.     *p++ = codestream_get ();
  1182. }
  1183.  
  1184. /* next instruction is a jump, move to target */
  1185. static
  1186. i386_follow_jump ()
  1187. {
  1188.   int long_delta;
  1189.   short short_delta;
  1190.   char byte_delta;
  1191.   int data16;
  1192.   int pos;
  1193.   
  1194.   pos = codestream_tell ();
  1195.   
  1196.   data16 = 0;
  1197.   if (codestream_peek () == 0x66)
  1198.     {
  1199.       codestream_get ();
  1200.       data16 = 1;
  1201.     }
  1202.   
  1203.   switch (codestream_get ())
  1204.     {
  1205.     case 0xe9:
  1206.       /* relative jump: if data16 == 0, disp32, else disp16 */
  1207.       if (data16)
  1208.     {
  1209.       codestream_read ((unsigned char *)&short_delta, 2);
  1210.       pos += short_delta + 3; /* include size of jmp inst */
  1211.     }
  1212.       else
  1213.     {
  1214.       codestream_read ((unsigned char *)&long_delta, 4);
  1215.       pos += long_delta + 5;
  1216.     }
  1217.       break;
  1218.     case 0xeb:
  1219.       /* relative jump, disp8 (ignore data16) */
  1220.       codestream_read ((unsigned char *)&byte_delta, 1);
  1221.       pos += byte_delta + 2;
  1222.       break;
  1223.     }
  1224.   codestream_seek (pos + data16);
  1225. }
  1226.  
  1227. /*
  1228.  * find & return amound a local space allocated, and advance codestream to
  1229.  * first register push (if any)
  1230.  *
  1231.  * if entry sequence doesn't make sense, return -1, and leave 
  1232.  * codestream pointer random
  1233.  */
  1234. static long
  1235. i386_get_frame_setup (pc)
  1236. {
  1237.   unsigned char op;
  1238.   
  1239.   codestream_seek (pc);
  1240.   
  1241.   i386_follow_jump ();
  1242.   
  1243.   op = codestream_get ();
  1244.   
  1245.   if (op == 0x58)        /* popl %eax */
  1246.     {
  1247.       /*
  1248.        * this function must start with
  1249.        * 
  1250.        *    popl %eax          0x58
  1251.        *    xchgl %eax, (%esp)  0x87 0x04 0x24
  1252.        * or xchgl %eax, 0(%esp) 0x87 0x44 0x24 0x00
  1253.        *
  1254.        * (the system 5 compiler puts out the second xchg
  1255.        * inst, and the assembler doesn't try to optimize it,
  1256.        * so the 'sib' form gets generated)
  1257.        * 
  1258.        * this sequence is used to get the address of the return
  1259.        * buffer for a function that returns a structure
  1260.        */
  1261.       int pos;
  1262.       unsigned char buf[4];
  1263.       static unsigned char proto1[3] = { 0x87,0x04,0x24 };
  1264.       static unsigned char proto2[4] = { 0x87,0x44,0x24,0x00 };
  1265.       pos = codestream_tell ();
  1266.       codestream_read (buf, 4);
  1267.       if (bcmp (buf, proto1, 3) == 0)
  1268.     pos += 3;
  1269.       else if (bcmp (buf, proto2, 4) == 0)
  1270.     pos += 4;
  1271.       
  1272.       codestream_seek (pos);
  1273.       op = codestream_get (); /* update next opcode */
  1274.     }
  1275.   
  1276.   if (op == 0x55)        /* pushl %esp */
  1277.     {            
  1278.       /* check for movl %esp, %ebp - can be written two ways */
  1279.       switch (codestream_get ())
  1280.     {
  1281.     case 0x8b:
  1282.       if (codestream_get () != 0xec)
  1283.         return (-1);
  1284.       break;
  1285.     case 0x89:
  1286.       if (codestream_get () != 0xe5)
  1287.         return (-1);
  1288.       break;
  1289.     default:
  1290.       return (-1);
  1291.     }
  1292.       /* check for stack adjustment 
  1293.        *
  1294.        *  subl $XXX, %esp
  1295.        *
  1296.        * note: you can't subtract a 16 bit immediate
  1297.        * from a 32 bit reg, so we don't have to worry
  1298.        * about a data16 prefix 
  1299.        */
  1300.       op = codestream_peek ();
  1301.       if (op == 0x83)
  1302.     {
  1303.       /* subl with 8 bit immed */
  1304.       codestream_get ();
  1305.       if (codestream_get () != 0xec)
  1306.         return (-1);
  1307.       /* subl with signed byte immediate 
  1308.        * (though it wouldn't make sense to be negative)
  1309.        */
  1310.       return (codestream_get());
  1311.     }
  1312.       else if (op == 0x81)
  1313.     {
  1314.       /* subl with 32 bit immed */
  1315.       int locals;
  1316.       codestream_get();
  1317.       if (codestream_get () != 0xec)
  1318.         return (-1);
  1319.       /* subl with 32 bit immediate */
  1320.       codestream_read ((unsigned char *)&locals, 4);
  1321.       return (locals);
  1322.     }
  1323.       else
  1324.     {
  1325.       return (0);
  1326.     }
  1327.     }
  1328.   else if (op == 0xc8)
  1329.     {
  1330.       /* enter instruction: arg is 16 bit unsigned immed */
  1331.       unsigned short slocals;
  1332.       codestream_read ((unsigned char *)&slocals, 2);
  1333.       codestream_get (); /* flush final byte of enter instruction */
  1334.       return (slocals);
  1335.     }
  1336.   return (-1);
  1337. }
  1338.  
  1339. /* Return number of args passed to a frame.
  1340.    Can return -1, meaning no way to tell.  */
  1341.  
  1342. /* on the 386, the instruction following the call could be:
  1343.  *  popl %ecx        -  one arg
  1344.  *  addl $imm, %esp  -  imm/4 args; imm may be 8 or 32 bits
  1345.  *  anything else    -  zero args
  1346.  */
  1347.  
  1348. int
  1349. i386_frame_num_args (fi)
  1350.      struct frame_info fi;
  1351. {
  1352.   int retpc;                        
  1353.   unsigned char op;                    
  1354.   struct frame_info *pfi;
  1355.  
  1356.   pfi = get_prev_frame_info ((fi));            
  1357.   if (pfi == 0)
  1358.     {
  1359.       /* Note:  this can happen if we are looking at the frame for
  1360.      main, because FRAME_CHAIN_VALID won't let us go into
  1361.      start.  If we have debugging symbols, that's not really
  1362.      a big deal; it just means it will only show as many arguments
  1363.      to main as are declared.  */
  1364.       return -1;
  1365.     }
  1366.   else
  1367.     {
  1368.       retpc = pfi->pc;                    
  1369.       op = read_memory_integer (retpc, 1);            
  1370.       if (op == 0x59)                    
  1371.     /* pop %ecx */                   
  1372.     return 1;                
  1373.       else if (op == 0x83)
  1374.     {
  1375.       op = read_memory_integer (retpc+1, 1);    
  1376.       if (op == 0xc4)                
  1377.         /* addl $<signed imm 8 bits>, %esp */    
  1378.         return (read_memory_integer (retpc+2,1)&0xff)/4;
  1379.       else
  1380.         return 0;
  1381.     }
  1382.       else if (op == 0x81)
  1383.     { /* add with 32 bit immediate */
  1384.       op = read_memory_integer (retpc+1, 1);    
  1385.       if (op == 0xc4)                
  1386.         /* addl $<imm 32>, %esp */        
  1387.         return read_memory_integer (retpc+2, 4) / 4;
  1388.       else
  1389.         return 0;
  1390.     }
  1391.       else
  1392.     {
  1393.       return 0;
  1394.     }
  1395.     }
  1396. }
  1397.  
  1398. /*
  1399.  * parse the first few instructions of the function to see
  1400.  * what registers were stored.
  1401.  *
  1402.  * We handle these cases:
  1403.  *
  1404.  * The startup sequence can be at the start of the function,
  1405.  * or the function can start with a branch to startup code at the end.
  1406.  *
  1407.  * %ebp can be set up with either the 'enter' instruction, or 
  1408.  * 'pushl %ebp, movl %esp, %ebp' (enter is too slow to be useful,
  1409.  * but was once used in the sys5 compiler)
  1410.  *
  1411.  * Local space is allocated just below the saved %ebp by either the
  1412.  * 'enter' instruction, or by 'subl $<size>, %esp'.  'enter' has
  1413.  * a 16 bit unsigned argument for space to allocate, and the
  1414.  * 'addl' instruction could have either a signed byte, or
  1415.  * 32 bit immediate.
  1416.  *
  1417.  * Next, the registers used by this function are pushed.  In
  1418.  * the sys5 compiler they will always be in the order: %edi, %esi, %ebx
  1419.  * (and sometimes a harmless bug causes it to also save but not restore %eax);
  1420.  * however, the code below is willing to see the pushes in any order,
  1421.  * and will handle up to 8 of them.
  1422.  *
  1423.  * If the setup sequence is at the end of the function, then the
  1424.  * next instruction will be a branch back to the start.
  1425.  */
  1426.  
  1427. i386_frame_find_saved_regs (fip, fsrp)
  1428.      struct frame_info *fip;
  1429.      struct frame_saved_regs *fsrp;
  1430. {
  1431.   unsigned long locals;
  1432.   unsigned char *p;
  1433.   unsigned char op;
  1434.   CORE_ADDR dummy_bottom;
  1435.   CORE_ADDR adr;
  1436.   int i;
  1437.   
  1438.   bzero (fsrp, sizeof *fsrp);
  1439.   
  1440. #if 0
  1441.   /* if frame is the end of a dummy, compute where the
  1442.    * beginning would be
  1443.    */
  1444.   dummy_bottom = fip->frame - 4 - NUM_REGS*4 - CALL_DUMMY_LENGTH;
  1445.   
  1446.   /* check if the PC is in the stack, in a dummy frame */
  1447.   if (dummy_bottom <= fip->pc && fip->pc <= fip->frame) 
  1448.     {
  1449.       /* all regs were saved by push_call_dummy () */
  1450.       adr = fip->frame - 4;
  1451.       for (i = 0; i < NUM_REGS; i++) 
  1452.     {
  1453.       fsrp->regs[i] = adr;
  1454.       adr -= 4;
  1455.     }
  1456.       return;
  1457.     }
  1458. #endif
  1459.  
  1460.   locals = i386_get_frame_setup (get_pc_function_start (fip->pc));
  1461.   
  1462.   if (locals >= 0) 
  1463.     {
  1464.       adr = fip->frame - 4 - locals;
  1465.       for (i = 0; i < 8; i++) 
  1466.     {
  1467.       op = codestream_get ();
  1468.       if (op < 0x50 || op > 0x57)
  1469.         break;
  1470.       fsrp->regs[op - 0x50] = adr;
  1471.       adr -= 4;
  1472.     }
  1473.     }
  1474.   
  1475.   fsrp->regs[PC_REGNUM] = fip->frame + 4;
  1476.   fsrp->regs[FP_REGNUM] = fip->frame;
  1477. }
  1478.  
  1479. /* return pc of first real instruction */
  1480. i386_skip_prologue (pc)
  1481. {
  1482.   unsigned char op;
  1483.   int i;
  1484.   
  1485.   if (i386_get_frame_setup (pc) < 0)
  1486.     return (pc);
  1487.   
  1488.   /* found valid frame setup - codestream now points to 
  1489.    * start of push instructions for saving registers
  1490.    */
  1491.   
  1492.   /* skip over register saves */
  1493.   for (i = 0; i < 8; i++)
  1494.     {
  1495.       op = codestream_peek ();
  1496.       /* break if not pushl inst */
  1497.       if (op < 0x50 || op > 0x57) 
  1498.     break;
  1499.       codestream_get ();
  1500.     }
  1501.   
  1502.   i386_follow_jump ();
  1503.   
  1504.   return (codestream_tell ());
  1505. }
  1506.  
  1507. i386_pop_frame ()
  1508. {
  1509.   FRAME frame = get_current_frame ();
  1510.   CORE_ADDR fp;
  1511.   int regnum;
  1512.   struct frame_saved_regs fsr;
  1513.   struct frame_info *fi;
  1514.   
  1515.   fi = get_frame_info (frame);
  1516.   fp = fi->frame;
  1517.   get_frame_saved_regs (fi, &fsr);
  1518.   for (regnum = 0; regnum < NUM_REGS; regnum++) 
  1519.     {
  1520.       CORE_ADDR adr;
  1521.       adr = fsr.regs[regnum];
  1522.       if (adr)
  1523.     write_register (regnum, read_memory_integer (adr, 4));
  1524.     }
  1525.   write_register (FP_REGNUM, read_memory_integer (fp, 4));
  1526.   write_register (PC_REGNUM, read_memory_integer (fp + 4, 4));
  1527.   write_register (SP_REGNUM, fp + 8);
  1528.   flush_cached_frames ();
  1529.   set_current_frame ( create_new_frame (read_register (FP_REGNUM),
  1530.                     read_pc ()));
  1531. }
  1532.  
  1533. /* this table must line up with REGISTER_NAMES in m-i386.h */
  1534. /* symbols like 'EAX' come from <sys/reg.h> */
  1535. static int trapmap[] = 
  1536. {
  1537.     tEAX, tECX, tEDX, tEBX,
  1538.     tESP, tEBP, tESI, tEDI,
  1539.     tEIP, tEFLAGS, tCS, tSS,
  1540.     tDS, tES, tES, tES        /* lies: no fs or gs */
  1541. };
  1542. static int syscallmap[] = 
  1543. {
  1544.     sEAX, sECX, sEDX, sEBX,
  1545.     sESP, sEBP, sESI, sEDI,
  1546.     sEIP, sEFLAGS, sCS, sSS,
  1547.     sCS, sCS, sCS, sCS        /* lies: no ds, es, fs or gs */
  1548. };
  1549. static int *regmap;
  1550.  
  1551. static void
  1552. setregmap(flags)
  1553.     int flags;
  1554. {
  1555. #ifdef EX_TRAPSTK
  1556.     regmap = flags & EX_TRAPSTK ? trapmap : syscallmap;
  1557. #else
  1558.     regmap = trapmap;    /* the lesser evil */
  1559. #endif
  1560. }
  1561.  
  1562. /* blockend is the value of u.u_ar0, and points to the
  1563.  * place where GS is stored
  1564.  */
  1565. i386_register_u_addr (blockend, regnum)
  1566. {
  1567. #if 0
  1568.   /* this will be needed if fp registers are reinstated */
  1569.   /* for now, you can look at them with 'info float'
  1570.    * sys5 wont let you change them with ptrace anyway
  1571.    */
  1572.   if (regnum >= FP0_REGNUM && regnum <= FP7_REGNUM) 
  1573.     {
  1574.       int ubase, fpstate;
  1575.       struct user u;
  1576.       ubase = blockend + 4 * (SS + 1) - KSTKSZ;
  1577.       fpstate = ubase + ((char *)&u.u_fpstate - (char *)&u);
  1578.       return (fpstate + 0x1c + 10 * (regnum - FP0_REGNUM));
  1579.     } 
  1580.   else
  1581. #endif
  1582.     return (blockend + 4 * regmap[regnum]);
  1583. }
  1584.  
  1585. i387_to_double (from, to)
  1586.      char *from;
  1587.      char *to;
  1588. {
  1589.   long *lp;
  1590.   /* push extended mode on 387 stack, then pop in double mode
  1591.    *
  1592.    * first, set exception masks so no error is generated -
  1593.    * number will be rounded to inf or 0, if necessary 
  1594.    */
  1595.   asm ("pushl %eax");         /* grab a stack slot */
  1596.   asm ("fstcw (%esp)");        /* get 387 control word */
  1597.   asm ("movl (%esp),%eax");    /* save old value */
  1598.   asm ("orl $0x3f,%eax");        /* mask all exceptions */
  1599.   asm ("pushl %eax");
  1600.   asm ("fldcw (%esp)");        /* load new value into 387 */
  1601.   
  1602.   asm ("movl 8(%ebp),%eax");
  1603.   asm ("fldt (%eax)");        /* push extended number on 387 stack */
  1604.   asm ("fwait");
  1605.   asm ("movl 12(%ebp),%eax");
  1606.   asm ("fstpl (%eax)");        /* pop double */
  1607.   asm ("fwait");
  1608.   
  1609.   asm ("popl %eax");        /* flush modified control word */
  1610.   asm ("fnclex");            /* clear exceptions */
  1611.   asm ("fldcw (%esp)");        /* restore original control word */
  1612.   asm ("popl %eax");        /* flush saved copy */
  1613. }
  1614.  
  1615. double_to_i387 (from, to)
  1616.      char *from;
  1617.      char *to;
  1618. {
  1619.   /* push double mode on 387 stack, then pop in extended mode
  1620.    * no errors are possible because every 64-bit pattern
  1621.    * can be converted to an extended
  1622.    */
  1623.   asm ("movl 8(%ebp),%eax");
  1624.   asm ("fldl (%eax)");
  1625.   asm ("fwait");
  1626.   asm ("movl 12(%ebp),%eax");
  1627.   asm ("fstpt (%eax)");
  1628.   asm ("fwait");
  1629. }
  1630.  
  1631. struct env387 
  1632. {
  1633.   unsigned short control;
  1634.   unsigned short r0;
  1635.   unsigned short status;
  1636.   unsigned short r1;
  1637.   unsigned short tag;
  1638.   unsigned short r2;
  1639.   unsigned long eip;
  1640.   unsigned short code_seg;
  1641.   unsigned short opcode;
  1642.   unsigned long operand;
  1643.   unsigned short operand_seg;
  1644.   unsigned short r3;
  1645.   unsigned char regs[8][10];
  1646. };
  1647.  
  1648. static
  1649. print_387_control_word (control)
  1650. unsigned short control;
  1651. {
  1652.   printf ("control 0x%04x: ", control);
  1653.   printf ("compute to ");
  1654.   switch ((control >> 8) & 3) 
  1655.     {
  1656.     case 0: printf ("24 bits; "); break;
  1657.     case 1: printf ("(bad); "); break;
  1658.     case 2: printf ("53 bits; "); break;
  1659.     case 3: printf ("64 bits; "); break;
  1660.     }
  1661.   printf ("round ");
  1662.   switch ((control >> 10) & 3) 
  1663.     {
  1664.     case 0: printf ("NEAREST; "); break;
  1665.     case 1: printf ("DOWN; "); break;
  1666.     case 2: printf ("UP; "); break;
  1667.     case 3: printf ("CHOP; "); break;
  1668.     }
  1669.   if (control & 0x3f) 
  1670.     {
  1671.       printf ("mask:");
  1672.       if (control & 0x0001) printf (" INVALID");
  1673.       if (control & 0x0002) printf (" DENORM");
  1674.       if (control & 0x0004) printf (" DIVZ");
  1675.       if (control & 0x0008) printf (" OVERF");
  1676.       if (control & 0x0010) printf (" UNDERF");
  1677.       if (control & 0x0020) printf (" LOS");
  1678.       printf (";");
  1679.     }
  1680.   printf ("\n");
  1681.   if (control & 0xe080) printf ("warning: reserved bits on 0x%x\n",
  1682.                 control & 0xe080);
  1683. }
  1684.  
  1685. static
  1686. print_387_status_word (status)
  1687.      unsigned short status;
  1688. {
  1689.   printf ("status 0x%04x: ", status);
  1690.   if (status & 0xff) 
  1691.     {
  1692.       printf ("exceptions:");
  1693.       if (status & 0x0001) printf (" INVALID");
  1694.       if (status & 0x0002) printf (" DENORM");
  1695.       if (status & 0x0004) printf (" DIVZ");
  1696.       if (status & 0x0008) printf (" OVERF");
  1697.       if (status & 0x0010) printf (" UNDERF");
  1698.       if (status & 0x0020) printf (" LOS");
  1699.       if (status & 0x0040) printf (" FPSTACK");
  1700.       printf ("; ");
  1701.     }
  1702.   printf ("flags: %d%d%d%d; ",
  1703.       (status & 0x4000) != 0,
  1704.       (status & 0x0400) != 0,
  1705.       (status & 0x0200) != 0,
  1706.       (status & 0x0100) != 0);
  1707.   
  1708.   printf ("top %d\n", (status >> 11) & 7);
  1709. }
  1710.  
  1711. static
  1712. print_387_status (status, ep)
  1713.      unsigned short status;
  1714.      struct env387 *ep;
  1715. {
  1716.   int i;
  1717.   int bothstatus;
  1718.   int top;
  1719.   int fpreg;
  1720.   unsigned char *p;
  1721.   
  1722.   bothstatus = ((status != 0) && (ep->status != 0));
  1723.   if (status != 0) 
  1724.     {
  1725.       if (bothstatus)
  1726.     printf ("u: ");
  1727.       print_387_status_word (status);
  1728.     }
  1729.   
  1730.   if (ep->status != 0) 
  1731.     {
  1732.       if (bothstatus)
  1733.     printf ("e: ");
  1734.       print_387_status_word (ep->status);
  1735.     }
  1736.   
  1737.   print_387_control_word (ep->control);
  1738.   printf ("last exception: ");
  1739.   printf ("opcode 0x%x; ", ep->opcode);
  1740.   printf ("pc 0x%x:0x%x; ", ep->code_seg, ep->eip);
  1741.   printf ("operand 0x%x:0x%x\n", ep->operand_seg, ep->operand);
  1742.   
  1743.   top = (ep->status >> 11) & 7;
  1744.   
  1745.   printf ("regno  tag  msb              lsb  value\n");
  1746.   for (fpreg = 7; fpreg >= 0; fpreg--) 
  1747.     {
  1748.       double val;
  1749.       
  1750.       printf ("%s %d: ", fpreg == top ? "=>" : "  ", fpreg);
  1751.       
  1752.       switch ((ep->tag >> (fpreg * 2)) & 3) 
  1753.     {
  1754.     case 0: printf ("valid "); break;
  1755.     case 1: printf ("zero  "); break;
  1756.     case 2: printf ("trap  "); break;
  1757.     case 3: printf ("empty "); break;
  1758.     }
  1759.       for (i = 9; i >= 0; i--)
  1760.     printf ("%02x", ep->regs[fpreg][i]);
  1761.       
  1762.       i387_to_double (ep->regs[fpreg], (char *)&val);
  1763.       printf ("  %g\n", val);
  1764.     }
  1765.   if (ep->r0)
  1766.     printf ("warning: reserved0 is 0x%x\n", ep->r0);
  1767.   if (ep->r1)
  1768.     printf ("warning: reserved1 is 0x%x\n", ep->r1);
  1769.   if (ep->r2)
  1770.     printf ("warning: reserved2 is 0x%x\n", ep->r2);
  1771.   if (ep->r3)
  1772.     printf ("warning: reserved3 is 0x%x\n", ep->r3);
  1773. }
  1774.  
  1775. #ifndef U_FPSTATE
  1776. #define U_FPSTATE(u) u.u_fpstate
  1777. #endif
  1778.  
  1779. i386_float_info ()
  1780. {
  1781.   struct user u; /* just for address computations */
  1782.   int i;
  1783. #ifndef i386b
  1784.   /* fpstate defined in <sys/user.h> */
  1785.   struct fpstate *fpstatep;
  1786.   char buf[sizeof (struct fpstate) + 2 * sizeof (int)];
  1787.   unsigned int uaddr;
  1788.   char fpvalid;
  1789.   unsigned int rounded_addr;
  1790.   unsigned int rounded_size;
  1791.   extern int corechan;
  1792.   int skip;
  1793.   
  1794.   uaddr = (char *)&u.u_fpvalid - (char *)&u;
  1795.   if (have_inferior_p()) 
  1796.     {
  1797.       unsigned int data;
  1798.       unsigned int mask;
  1799.       
  1800.       rounded_addr = uaddr & -sizeof (int);
  1801.       data = ptrace (3, inferior_pid, rounded_addr, 0);
  1802.       mask = 0xff << ((uaddr - rounded_addr) * 8);
  1803.       
  1804.       fpvalid = ((data & mask) != 0);
  1805.     } 
  1806.   else 
  1807.     {
  1808.       if (lseek (corechan, uaddr, 0) < 0)
  1809.     perror ("seek on core file");
  1810.       if (myread (corechan, &fpvalid, 1) < 0) 
  1811.     perror ("read on core file");
  1812.       
  1813.     }
  1814.   
  1815.   if (fpvalid == 0) 
  1816.     {
  1817.       printf ("no floating point status saved\n");
  1818.       return;
  1819.     }
  1820.   
  1821.   uaddr = (char *)&U_FPSTATE(u) - (char *)&u;
  1822.   if (have_inferior_p ()) 
  1823.     {
  1824.       int *ip;
  1825.       
  1826.       rounded_addr = uaddr & -sizeof (int);
  1827.       rounded_size = (((uaddr + sizeof (struct fpstate)) - uaddr) +
  1828.               sizeof (int) - 1) / sizeof (int);
  1829.       skip = uaddr - rounded_addr;
  1830.       
  1831.       ip = (int *)buf;
  1832.       for (i = 0; i < rounded_size; i++) 
  1833.     {
  1834.       *ip++ = ptrace (3, inferior_pid, rounded_addr, 0);
  1835.       rounded_addr += sizeof (int);
  1836.     }
  1837.     } 
  1838.   else 
  1839.     {
  1840.       if (lseek (corechan, uaddr, 0) < 0)
  1841.     perror_with_name ("seek on core file");
  1842.       if (myread (corechan, buf, sizeof (struct fpstate)) < 0) 
  1843.     perror_with_name ("read from core file");
  1844.       skip = 0;
  1845.     }
  1846.   
  1847.   fpstatep = (struct fpstate *)(buf + skip);
  1848.   print_387_status (fpstatep->status, (struct env387 *)fpstatep->state);
  1849. #endif
  1850. }
  1851.  
  1852. void
  1853. _initialize_hp300bsd_dep()
  1854. {
  1855. #ifdef KERNELDEBUG
  1856.     add_com ("process-address", class_obscure, set_paddr_command,
  1857.          "The process identified by (ps-style) ADDR becomes the\n\
  1858. \"current\" process context for kernel debugging.");
  1859.     add_com_alias ("paddr", "process-address", class_obscure, 0);
  1860. #endif
  1861. }
  1862.