Debug.tips revision 1.1
11.1SmycroftNOTE: this description applies to the hp300 system with the old BSD
21.1Smycroftvirtual memory system.  It has not been updated to reflect the new,
31.1SmycroftMach-derived VM system, but should still be useful.
41.1SmycroftThe new system has no fixed-address "u.", but has a fixed mapping
51.1Smycroftfor the kernel stack at 0xfff00000.
61.1Smycroft
71.1Smycroft--------------------------------------------------------------------------
81.1Smycroft
91.1SmycroftSome quick notes on the HPBSD VM layout and kernel debugging.
101.1Smycroft
111.1SmycroftPhysical memory:
121.1Smycroft
131.1SmycroftPhysical memory always ends at the top of the 32 bit address space; i.e. the
141.1Smycroftlast addressible byte is at 0xFFFFFFFF.  Hence, the start of physical memory
151.1Smycroftvaries depending on how much memory is installed.  The kernel variable "lowram"
161.1Smycroftcontains the starting locatation of memory as provided by the ROM.
171.1Smycroft
181.1SmycroftThe low 128k (I think) of the physical address space is occupied by the ROM.
191.1SmycroftThis is accessible via /dev/mem *only* if the kernel is compiled with DEBUG.
201.1Smycroft[ Maybe it should always be accessible? ]
211.1Smycroft
221.1SmycroftVirtual address spaces:
231.1Smycroft
241.1SmycroftThe hardware page size is 4096 bytes.  The hardware uses a two-level lookup.
251.1SmycroftAt the highest level is a one page segment table which maps a page table which
261.1Smycroftmaps the address space.  Each 4 byte segment table entry (described in
271.1Smycrofthp300/pte.h) contains the page number of a single page of 4 byte page table
281.1Smycroftentries.  Each PTE maps a single page of address space.  Hence, each STE maps
291.1Smycroft4Mb of address space and one page containing 1024 STEs is adequate to map the
301.1Smycroftentire 4Gb address space.
311.1Smycroft
321.1SmycroftBoth page and segment table entries look similar.  Both have the page frame
331.1Smycroftin the upper part and control bits in the lower.  This is the opposite of
341.1Smycroftthe VAX.  It is easy to convert the page frame number in an STE/PTE to a
351.1Smycroftphysical address, simply mentally mask out the low 12 bits.  For example
361.1Smycroftif a PTE contains 0xFF880019, the physical memory location mapped starts at
371.1Smycroft0xFF880000.
381.1Smycroft
391.1SmycroftKernel address space:
401.1Smycroft
411.1SmycroftThe kernel resides in its own virtual address space independent of all user
421.1Smycroftprocesses.  When the processor is in supervisor mode (i.e. interrupt or 
431.1Smycroftexception handling) it uses the kernel virtual mapping.  The kernel segment
441.1Smycrofttable is called Sysseg and is allocated statically in hp300/locore.s.  The
451.1Smycroftkernel page table is called Systab is also allocated statically in
461.1Smycrofthp300/locore.s and consists of the usual assortment of SYSMAPs.
471.1SmycroftThe size of Systab (Syssize) depends on the configured size of the various
481.1Smycroftmaps but as currently configured is 9216 PTEs.  Both segment and page tables
491.1Smycroftare initialized at bootup in hp300/locore.s.  The segment table never changes
501.1Smycroft(except for bits maintained by the hardware).  Portions of the page table
511.1Smycroftchange as needed.  The kernel is mapped into the address space starting at 0.
521.1Smycroft
531.1SmycroftTheoretically, any address in the range 0 to Syssize * 4096 (0x2400000 as
541.1Smycroftcurrently configured) is valid.  However, certain addresses are more common
551.1Smycroftin dumps than others.  Those are (for the current configuration):
561.1Smycroft
571.1Smycroft	0         - 0x800000	kernel text and permanent data structures
581.1Smycroft	0x917000  - 0x91a000	u-area; 1st page is user struct, last k-stack
591.1Smycroft	0x1b1b000 - 0x2400000	user page tables, also kmem_alloc()ed data
601.1Smycroft
611.1SmycroftUser address space:
621.1Smycroft
631.1SmycroftThe user text and data are loaded starting at VA 0.  The user's stack starts
641.1Smycroftat 0xFFF00000 and grows toward lower addresses.  The pages above the user
651.1Smycroftstack are used by the kernel.  From 0xFFF00000 to 0xFFF03000 is the u-area.
661.1SmycroftThe 3 PTEs for this range map (read-only) the same memory as does 0x917000
671.1Smycroftto 0x91a000 in the kernel address space.  This address range is never used
681.1Smycroftby the kernel, but exists for utilities that assume that the u-area sits
691.1Smycroftabove the user stack.  The pages from FFF03000 up are not used.  They
701.1Smycroftexist so that the user stack is in the same location as in HPUX.
711.1Smycroft
721.1SmycroftThe user segment table is allocated along with the page tables from Usrptmap.
731.1SmycroftThey are contiguous in kernel VA space with the page tables coming before
741.1Smycroftthe segment table.  Hence, a process has p_szpt+1 pages allocated starting
751.1Smycroftat kernel VA p_p0br.
761.1Smycroft
771.1SmycroftThe user segment table is typically very sparse since each entry maps 4Mb.
781.1SmycroftThere are usually only two valid STEs, one at the start mapping the text/data
791.1Smycroftpotion of the page table, and one at the end mapping the stack/u-area.  For
801.1Smycroftexample if the segment table was at 0xFFFFA000 there would be valid entries
811.1Smycroftat 0xFFFFA000 and 0xFFFFAFFC.
821.1Smycroft
831.1SmycroftRandom notes:
841.1Smycroft
851.1SmycroftAn important thing to note is that there are no hardware length registers
861.1Smycrofton the HP.  This implies that we cannot "pack" data and stack PTEs into the
871.1Smycroftsame page table page.  Hence, every user page table has at least 2 pages
881.1Smycroft(3 if you count the segment table).
891.1Smycroft
901.1SmycroftThe HP maintains the p0br/p0lr and p1br/p1lr PCB fields the same as the
911.1SmycroftVAX even though they have no meaning to the hardware.  This also keeps many
921.1Smycroftutilities happy.
931.1Smycroft
941.1SmycroftThere is no seperate interrupt stack (right now) on the HPs.  Interrupt
951.1Smycroftprocessing is handled on the kernel stack of the "current" process.
961.1Smycroft
971.1SmycroftFollowing is a list of things you might want to be able to do with a kernel
981.1Smycroftcore dump.  One thing you should always have is a ps listing from the core
991.1Smycroftfile.  Just do:
1001.1Smycroft
1011.1Smycroft	ps klaw vmunix.? vmcore.?
1021.1Smycroft
1031.1SmycroftException related panics (i.e. those detected in hp300/trap.c) will dump
1041.1Smycroftout various useful information before panicing.  If available, you should
1051.1Smycroftget this out of the /usr/adm/messages file.  Finally, you should be in adb:
1061.1Smycroft
1071.1Smycroft	adb -k vmunix.? vmcore.?
1081.1Smycroft
1091.1SmycroftAdb -k will allow you to examine the kernel address space more easily.
1101.1SmycroftIt automatically maps kernel VAs in the range 0 to 0x2400000 to physical
1111.1Smycroftaddresses.  Since the kernel and user address spaces overlap (i.e. both
1121.1Smycroftstart at 0), adb can't let you examine the address space of the "current"
1131.1Smycroftprocess as it does on the VAX.
1141.1Smycroft--------
1151.1Smycroft
1161.1Smycroft1. Find out what the current process was at the time of the crash:
1171.1Smycroft
1181.1SmycroftIf you have the dump info from /usr/adm/messages, it should contain the
1191.1SmycroftPID of the active process.  If you don't have this info you can just look
1201.1Smycroftat location "Umap".  This is the PTE for the first page of the u-area; i.e.
1211.1Smycroftthe user structure.  Forget about the last 3 hex digits and compare the top
1221.1Smycroft5 to the ADDR column in the ps listing.
1231.1Smycroft
1241.1Smycroft2. Locating a process' user structure:
1251.1Smycroft
1261.1SmycroftGet the ADDR field of the desired process from the ps listing.  This is the
1271.1Smycroftpage frame number of the process' user structure.  Tack 3 zeros on to the
1281.1Smycroftend to get the physical address.  Note that this doesn't give you the kernel
1291.1Smycroftstack since it is in a different page than the user-structure and pages of
1301.1Smycroftthe u-area are not physically contiguous.
1311.1Smycroft
1321.1Smycroft3. Locating a process' proc structure:
1331.1Smycroft
1341.1SmycroftFirst find the process' user structure as described above.  Find the u_procp
1351.1Smycroftfield at offset 0x200 from the beginning.  This gives you the kernel VA of
1361.1Smycroftthe proc structure.
1371.1Smycroft
1381.1Smycroft4. Locating a process' page table:
1391.1Smycroft
1401.1SmycroftFirst find the process' user structure as described above.  The first part
1411.1Smycroftof the user structure is the PCB.  The second longword (third field) of the
1421.1SmycroftPCB is pcb_ustp, a pointer to the user segment table.  This pointer is
1431.1Smycroftactually the page frame number.  Again adding 3 zeros yields the physical
1441.1Smycroftaddress.  You can now use the values in the segment table to locate the
1451.1Smycroftpage tables.  For example, to locate the first page of the text/data part
1461.1Smycroftof the page table, use the first STE (longword) in the segment table.
1471.1Smycroft
1481.1Smycroft5. Locating a process' kernel stack:
1491.1Smycroft
1501.1SmycroftFirst find the process' page table as described above.  The kernel stack
1511.1Smycroftis near the end of the user address space.  So, locate the last entry in the
1521.1Smycroftuser segment table (base+0xFFC) and use that entry to find the last page of
1531.1Smycroftthe user page table.  Look at the last 256 entries of this page
1541.1Smycroft(pagebase+0xFE0)  The first is the PTE for the user-structure.  The second
1551.1Smycroftwas intended to be a read-only page to protect the user structure from the
1561.1Smycroftkernel stack.  Currently it is read/write and actually allocated.  Hence
1571.1Smycroftit can wind up being a second page for the kernel stack.  The third is the
1581.1Smycroftkernel stack.  The last 253 should be zero.  Hence, indirecing through the
1591.1Smycroftthird of these last 256 PTEs will give you the kernel stack page.
1601.1Smycroft
1611.1SmycroftAn alternate way to do this is to use the p_addr field of the proc structure
1621.1Smycroftwhich is found as described above.  The p_addr field is at offset 0x10 in the
1631.1Smycroftproc structure and points to the first of the PTEs mentioned above (i.e. the
1641.1Smycroftuser structure PTE).
1651.1Smycroft
1661.1Smycroft6. Interpreting the info in a "trap type N..." panic:
1671.1Smycroft
1681.1SmycroftAs mentioned, when the kernel crashes out of hp300/trap.c it will dump some
1691.1Smycroftuseful information.  This dates back to the days when I was debugging the
1701.1Smycroftexception handling code and had no kernel adb or even kernel crash dump code.
1711.1Smycroft"trap type" (decimal) is as defined in hp300/trap.h, it doesn't really
1721.1Smycroftcorrelate with anything useful.  "code" (hex) is only useful for MMU
1731.1Smycroft(trap type 8) errors.  It is the concatination of the MMU status register
1741.1Smycroft(see hp300/cpu.h) in the high 16 bits and the 68020 special status word
1751.1Smycroft(see the 020 manual page 6-17) in the low 16.  "v" (hex) is the virtual
1761.1Smycroftaddress which caused the fault.  "pid" (decimal) is the ID of the process
1771.1Smycroftrunning at the time of the exception.  Note that if we panic in an interrupt
1781.1Smycroftroutine, this process may not be related to the panic.  "ps" (hex) is the
1791.1Smycroftvalue of the 68020 status register (see page 1-4 of 020 manual) at the time
1801.1Smycroftof the crash.  If the 0x2000 bit is on, we were in supervisor (kernel) mode
1811.1Smycroftat the time, otherwise we were in user mode.  "pc" (hex) is the value of the
1821.1SmycroftPC saved on the hardware exception frame.  It may *not* be the PC of the
1831.1Smycroftinstruction causing the fault (see the 020 manual for details).  The 0x2000
1841.1Smycroftbit of "ps" dictates whether this is a kernel or user VA.  "sfc" and "dfc"
1851.1Smycroftare the 68020 source/destination function codes.  They should always be one.
1861.1Smycroft"p0" and "p1" are the VAX-like region registers.  They are of the form:
1871.1Smycroft
1881.1Smycroft	<length> '@' <kernel VA>
1891.1Smycroft
1901.1Smycroftwhere both are in hex.  Following these values are a dump of the processor
1911.1Smycroftregisters (hex).  Check the address registers for values close to "v", the
1921.1Smycroftfault address.  Most faults are causes by dereferences of bogus pointers.
1931.1SmycroftMost such dereferences are the result of 020 instructions using the:
1941.1Smycroft
1951.1Smycroft	<address-register> '@' '(' offset ')'
1961.1Smycroft
1971.1Smycroftaddressing mode.  This can help you track down the faulting instruction (since
1981.1Smycroftthe PC may not point to it).  Note that the value of a7 (the stack pointer) is
1991.1SmycroftALWAYS the user SP.  This is brain-dead I know.  Finally, is a dump of the
2001.1Smycroftstack (user/kernel) at the time of the offense.  Before kernel crash dumps,
2011.1Smycroftthis was very useful.
2021.1Smycroft
2031.1Smycroft7. Converting kernel virtual address to a physical address.
2041.1Smycroft
2051.1SmycroftAdb -k already does this for you, but sometimes you want to know what the
2061.1Smycroftresulting physical address is rather than what is there.  Doing this is
2071.1Smycroftsimply a matter of indexing into the kernel page table.  In theory we would
2081.1Smycroftfirst have to do a lookup in the kernel segment table, but we know that the
2091.1Smycroftkernel page table is physically contiguous so this isn't necessary.  The
2101.1Smycroftbase of the system page table is "Sysmap", so to convert an address V just
2111.1Smycroftdivide the address by 4096 to get the page number, multiply that by 4 (the
2121.1Smycroftsize of a PTE in bytes) to get a byte offset, and add that to "Sysmap".
2131.1SmycroftThis gives you the address of the PTE mapping V.  You can then get the
2141.1Smycroftphysical address by masking out the low 12 bits of the contents of that PTE.
2151.1SmycroftTo wit:
2161.1Smycroft
2171.1Smycroft	*(Sysmap+(VA%1000*4))&fffff000
2181.1Smycroft
2191.1Smycroftwhere VA is the virtual address in question.
2201.1Smycroft
2211.1SmycroftThis technique should also work for user virtual addresses if you replace
2221.1Smycroft"Sysmap" with the value of the appropriate processes' P0BR.  This works
2231.1Smycroftbecause a user's page table is *virtually* contiguous in the kernel
2241.1Smycroftstarting at P0BR, and adb will handle translating the kernel virtual addresses
2251.1Smycroftfor you.
226