1  | 
     | 
     | 
    /*	$OpenBSD: kvm_amd64.c,v 1.13 2015/12/19 18:40:30 mmcc Exp $	*/  | 
    
    
    2  | 
     | 
     | 
    /*	$NetBSD: kvm_x86_64.c,v 1.3 2002/06/05 22:01:55 fvdl Exp $	*/  | 
    
    
    3  | 
     | 
     | 
     | 
    
    
    4  | 
     | 
     | 
    /*-  | 
    
    
    5  | 
     | 
     | 
     * Copyright (c) 1989, 1992, 1993  | 
    
    
    6  | 
     | 
     | 
     *	The Regents of the University of California.  All rights reserved.  | 
    
    
    7  | 
     | 
     | 
     *  | 
    
    
    8  | 
     | 
     | 
     * This code is derived from software developed by the Computer Systems  | 
    
    
    9  | 
     | 
     | 
     * Engineering group at Lawrence Berkeley Laboratory under DARPA contract  | 
    
    
    10  | 
     | 
     | 
     * BG 91-66 and contributed to Berkeley.  | 
    
    
    11  | 
     | 
     | 
     *  | 
    
    
    12  | 
     | 
     | 
     * Redistribution and use in source and binary forms, with or without  | 
    
    
    13  | 
     | 
     | 
     * modification, are permitted provided that the following conditions  | 
    
    
    14  | 
     | 
     | 
     * are met:  | 
    
    
    15  | 
     | 
     | 
     * 1. Redistributions of source code must retain the above copyright  | 
    
    
    16  | 
     | 
     | 
     *    notice, this list of conditions and the following disclaimer.  | 
    
    
    17  | 
     | 
     | 
     * 2. Redistributions in binary form must reproduce the above copyright  | 
    
    
    18  | 
     | 
     | 
     *    notice, this list of conditions and the following disclaimer in the  | 
    
    
    19  | 
     | 
     | 
     *    documentation and/or other materials provided with the distribution.  | 
    
    
    20  | 
     | 
     | 
     * 3. Neither the name of the University nor the names of its contributors  | 
    
    
    21  | 
     | 
     | 
     *    may be used to endorse or promote products derived from this software  | 
    
    
    22  | 
     | 
     | 
     *    without specific prior written permission.  | 
    
    
    23  | 
     | 
     | 
     *  | 
    
    
    24  | 
     | 
     | 
     * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND  | 
    
    
    25  | 
     | 
     | 
     * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE  | 
    
    
    26  | 
     | 
     | 
     * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE  | 
    
    
    27  | 
     | 
     | 
     * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE  | 
    
    
    28  | 
     | 
     | 
     * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL  | 
    
    
    29  | 
     | 
     | 
     * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS  | 
    
    
    30  | 
     | 
     | 
     * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)  | 
    
    
    31  | 
     | 
     | 
     * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT  | 
    
    
    32  | 
     | 
     | 
     * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY  | 
    
    
    33  | 
     | 
     | 
     * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF  | 
    
    
    34  | 
     | 
     | 
     * SUCH DAMAGE.  | 
    
    
    35  | 
     | 
     | 
     */  | 
    
    
    36  | 
     | 
     | 
     | 
    
    
    37  | 
     | 
     | 
    /*  | 
    
    
    38  | 
     | 
     | 
     * x86-64 machine dependent routines for kvm.  | 
    
    
    39  | 
     | 
     | 
     */  | 
    
    
    40  | 
     | 
     | 
     | 
    
    
    41  | 
     | 
     | 
    #include <sys/param.h>	/* MAXCOMLEN */  | 
    
    
    42  | 
     | 
     | 
    #include <sys/proc.h>  | 
    
    
    43  | 
     | 
     | 
    #include <sys/stat.h>  | 
    
    
    44  | 
     | 
     | 
    #include <sys/kcore.h>  | 
    
    
    45  | 
     | 
     | 
    #include <machine/kcore.h>  | 
    
    
    46  | 
     | 
     | 
    #include <stdlib.h>  | 
    
    
    47  | 
     | 
     | 
    #include <unistd.h>  | 
    
    
    48  | 
     | 
     | 
    #include <nlist.h>  | 
    
    
    49  | 
     | 
     | 
    #include <kvm.h>  | 
    
    
    50  | 
     | 
     | 
     | 
    
    
    51  | 
     | 
     | 
    #include <uvm/uvm_extern.h>  | 
    
    
    52  | 
     | 
     | 
     | 
    
    
    53  | 
     | 
     | 
    #include <limits.h>  | 
    
    
    54  | 
     | 
     | 
    #include <db.h>  | 
    
    
    55  | 
     | 
     | 
     | 
    
    
    56  | 
     | 
     | 
    #include "kvm_private.h"  | 
    
    
    57  | 
     | 
     | 
     | 
    
    
    58  | 
     | 
     | 
    #include <machine/pmap.h>  | 
    
    
    59  | 
     | 
     | 
    #include <machine/pte.h>  | 
    
    
    60  | 
     | 
     | 
    #include <machine/vmparam.h>  | 
    
    
    61  | 
     | 
     | 
     | 
    
    
    62  | 
     | 
     | 
    void  | 
    
    
    63  | 
     | 
     | 
    _kvm_freevtop(kvm_t *kd)  | 
    
    
    64  | 
     | 
     | 
    { | 
    
    
    65  | 
     | 
     | 
    	/* Not actually used for anything right now, but safe. */  | 
    
    
    66  | 
     | 
     | 
    	free(kd->vmst);  | 
    
    
    67  | 
     | 
     | 
    	kd->vmst = NULL;  | 
    
    
    68  | 
     | 
     | 
    }  | 
    
    
    69  | 
     | 
     | 
     | 
    
    
    70  | 
     | 
     | 
    /*ARGSUSED*/  | 
    
    
    71  | 
     | 
     | 
    int  | 
    
    
    72  | 
     | 
     | 
    _kvm_initvtop(kvm_t *kd)  | 
    
    
    73  | 
     | 
     | 
    { | 
    
    
    74  | 
     | 
     | 
     | 
    
    
    75  | 
     | 
     | 
    	return (0);  | 
    
    
    76  | 
     | 
     | 
    }  | 
    
    
    77  | 
     | 
     | 
     | 
    
    
    78  | 
     | 
     | 
    /*  | 
    
    
    79  | 
     | 
     | 
     * Translate a kernel virtual address to a physical address.  | 
    
    
    80  | 
     | 
     | 
     */  | 
    
    
    81  | 
     | 
     | 
    int  | 
    
    
    82  | 
     | 
     | 
    _kvm_kvatop(kvm_t *kd, u_long va, paddr_t *pa)  | 
    
    
    83  | 
     | 
     | 
    { | 
    
    
    84  | 
     | 
     | 
    	cpu_kcore_hdr_t *cpu_kh;  | 
    
    
    85  | 
     | 
     | 
    	paddr_t pde_pa, pte_pa;  | 
    
    
    86  | 
     | 
     | 
    	u_long page_off;  | 
    
    
    87  | 
     | 
     | 
    	pd_entry_t pde;  | 
    
    
    88  | 
     | 
     | 
    	pt_entry_t pte;  | 
    
    
    89  | 
     | 
     | 
     | 
    
    
    90  | 
     | 
     | 
    	if (ISALIVE(kd)) { | 
    
    
    91  | 
     | 
     | 
    		_kvm_err(kd, 0, "vatop called in live kernel!");  | 
    
    
    92  | 
     | 
     | 
    		return (0);  | 
    
    
    93  | 
     | 
     | 
    	}  | 
    
    
    94  | 
     | 
     | 
     | 
    
    
    95  | 
     | 
     | 
    	page_off = va & (kd->nbpg - 1);  | 
    
    
    96  | 
     | 
     | 
     | 
    
    
    97  | 
     | 
     | 
    	if (va >= PMAP_DIRECT_BASE && va <= PMAP_DIRECT_END) { | 
    
    
    98  | 
     | 
     | 
    		*pa = va - PMAP_DIRECT_BASE;  | 
    
    
    99  | 
     | 
     | 
    		return (int)(kd->nbpg - page_off);  | 
    
    
    100  | 
     | 
     | 
    	}  | 
    
    
    101  | 
     | 
     | 
     | 
    
    
    102  | 
     | 
     | 
    	cpu_kh = kd->cpu_data;  | 
    
    
    103  | 
     | 
     | 
     | 
    
    
    104  | 
     | 
     | 
    	/*  | 
    
    
    105  | 
     | 
     | 
    	 * Find and read all entries to get to the pa.  | 
    
    
    106  | 
     | 
     | 
    	 */  | 
    
    
    107  | 
     | 
     | 
     | 
    
    
    108  | 
     | 
     | 
    	/*  | 
    
    
    109  | 
     | 
     | 
    	 * Level 4.  | 
    
    
    110  | 
     | 
     | 
    	 */  | 
    
    
    111  | 
     | 
     | 
    	pde_pa = cpu_kh->ptdpaddr + (pl4_pi(va) * sizeof(pd_entry_t));  | 
    
    
    112  | 
     | 
     | 
    	if (pread(kd->pmfd, (void *)&pde, sizeof(pde),  | 
    
    
    113  | 
     | 
     | 
    	    _kvm_pa2off(kd, pde_pa)) != sizeof(pde)) { | 
    
    
    114  | 
     | 
     | 
    		_kvm_syserr(kd, 0, "could not read PT level 4 entry");  | 
    
    
    115  | 
     | 
     | 
    		goto lose;  | 
    
    
    116  | 
     | 
     | 
    	}  | 
    
    
    117  | 
     | 
     | 
    	if ((pde & PG_V) == 0) { | 
    
    
    118  | 
     | 
     | 
    		_kvm_err(kd, 0, "invalid translation (invalid level 4 PDE)");  | 
    
    
    119  | 
     | 
     | 
    		goto lose;  | 
    
    
    120  | 
     | 
     | 
    	}  | 
    
    
    121  | 
     | 
     | 
     | 
    
    
    122  | 
     | 
     | 
    	/*  | 
    
    
    123  | 
     | 
     | 
    	 * Level 3.  | 
    
    
    124  | 
     | 
     | 
    	 */  | 
    
    
    125  | 
     | 
     | 
    	pde_pa = (pde & PG_FRAME) + (pl3_pi(va) * sizeof(pd_entry_t));  | 
    
    
    126  | 
     | 
     | 
    	if (pread(kd->pmfd, (void *)&pde, sizeof(pde),  | 
    
    
    127  | 
     | 
     | 
    	    _kvm_pa2off(kd, pde_pa)) != sizeof(pde)) { | 
    
    
    128  | 
     | 
     | 
    		_kvm_syserr(kd, 0, "could not read PT level 3 entry");  | 
    
    
    129  | 
     | 
     | 
    		goto lose;  | 
    
    
    130  | 
     | 
     | 
    	}  | 
    
    
    131  | 
     | 
     | 
    	if ((pde & PG_V) == 0) { | 
    
    
    132  | 
     | 
     | 
    		_kvm_err(kd, 0, "invalid translation (invalid level 3 PDE)");  | 
    
    
    133  | 
     | 
     | 
    		goto lose;  | 
    
    
    134  | 
     | 
     | 
    	}  | 
    
    
    135  | 
     | 
     | 
     | 
    
    
    136  | 
     | 
     | 
    	/*  | 
    
    
    137  | 
     | 
     | 
    	 * Level 2.  | 
    
    
    138  | 
     | 
     | 
    	 */  | 
    
    
    139  | 
     | 
     | 
    	pde_pa = (pde & PG_FRAME) + (pl2_pi(va) * sizeof(pd_entry_t));  | 
    
    
    140  | 
     | 
     | 
    	if (pread(kd->pmfd, (void *)&pde, sizeof(pde),  | 
    
    
    141  | 
     | 
     | 
    	    _kvm_pa2off(kd, pde_pa)) != sizeof(pde)) { | 
    
    
    142  | 
     | 
     | 
    		_kvm_syserr(kd, 0, "could not read PT level 2 entry");  | 
    
    
    143  | 
     | 
     | 
    		goto lose;  | 
    
    
    144  | 
     | 
     | 
    	}  | 
    
    
    145  | 
     | 
     | 
    	if ((pde & PG_V) == 0) { | 
    
    
    146  | 
     | 
     | 
    		_kvm_err(kd, 0, "invalid translation (invalid level 2 PDE)");  | 
    
    
    147  | 
     | 
     | 
    		goto lose;  | 
    
    
    148  | 
     | 
     | 
    	}  | 
    
    
    149  | 
     | 
     | 
     | 
    
    
    150  | 
     | 
     | 
    	/*  | 
    
    
    151  | 
     | 
     | 
    	 * Might be a large page.  | 
    
    
    152  | 
     | 
     | 
    	 */  | 
    
    
    153  | 
     | 
     | 
    	if ((pde & PG_PS) != 0) { | 
    
    
    154  | 
     | 
     | 
    		page_off = va & (NBPD_L2 - 1);  | 
    
    
    155  | 
     | 
     | 
    		*pa = (pde & PG_LGFRAME) | page_off;  | 
    
    
    156  | 
     | 
     | 
    		return (int)(NBPD_L2 - page_off);  | 
    
    
    157  | 
     | 
     | 
    	}  | 
    
    
    158  | 
     | 
     | 
     | 
    
    
    159  | 
     | 
     | 
    	/*  | 
    
    
    160  | 
     | 
     | 
    	 * Level 1.  | 
    
    
    161  | 
     | 
     | 
    	 */  | 
    
    
    162  | 
     | 
     | 
    	pte_pa = (pde & PG_FRAME) + (pl1_pi(va) * sizeof(pt_entry_t));  | 
    
    
    163  | 
     | 
     | 
    	if (pread(kd->pmfd, (void *) &pte, sizeof(pte),  | 
    
    
    164  | 
     | 
     | 
    	    _kvm_pa2off(kd, pte_pa)) != sizeof(pte)) { | 
    
    
    165  | 
     | 
     | 
    		_kvm_syserr(kd, 0, "could not read PTE");  | 
    
    
    166  | 
     | 
     | 
    		goto lose;  | 
    
    
    167  | 
     | 
     | 
    	}  | 
    
    
    168  | 
     | 
     | 
    	/*  | 
    
    
    169  | 
     | 
     | 
    	 * Validate the PTE and return the physical address.  | 
    
    
    170  | 
     | 
     | 
    	 */  | 
    
    
    171  | 
     | 
     | 
    	if ((pte & PG_V) == 0) { | 
    
    
    172  | 
     | 
     | 
    		_kvm_err(kd, 0, "invalid translation (invalid PTE)");  | 
    
    
    173  | 
     | 
     | 
    		goto lose;  | 
    
    
    174  | 
     | 
     | 
    	}  | 
    
    
    175  | 
     | 
     | 
    	*pa = (pte & PG_FRAME) + page_off;  | 
    
    
    176  | 
     | 
     | 
    	return (int)(kd->nbpg - page_off);  | 
    
    
    177  | 
     | 
     | 
     | 
    
    
    178  | 
     | 
     | 
     lose:  | 
    
    
    179  | 
     | 
     | 
    	*pa = (u_long)~0L;  | 
    
    
    180  | 
     | 
     | 
    	return (0);  | 
    
    
    181  | 
     | 
     | 
    }  | 
    
    
    182  | 
     | 
     | 
     | 
    
    
    183  | 
     | 
     | 
    /*  | 
    
    
    184  | 
     | 
     | 
     * Translate a physical address to a file-offset in the crash dump.  | 
    
    
    185  | 
     | 
     | 
     */  | 
    
    
    186  | 
     | 
     | 
    off_t  | 
    
    
    187  | 
     | 
     | 
    _kvm_pa2off(kvm_t *kd, paddr_t pa)  | 
    
    
    188  | 
     | 
     | 
    { | 
    
    
    189  | 
     | 
     | 
    	cpu_kcore_hdr_t *cpu_kh;  | 
    
    
    190  | 
     | 
     | 
    	phys_ram_seg_t *ramsegs;  | 
    
    
    191  | 
     | 
     | 
    	off_t off;  | 
    
    
    192  | 
     | 
     | 
    	int i;  | 
    
    
    193  | 
     | 
     | 
     | 
    
    
    194  | 
     | 
     | 
    	cpu_kh = kd->cpu_data;  | 
    
    
    195  | 
     | 
     | 
    	ramsegs = (void *)((char *)(void *)cpu_kh + ALIGN(sizeof *cpu_kh));  | 
    
    
    196  | 
     | 
     | 
     | 
    
    
    197  | 
     | 
     | 
    	off = 0;  | 
    
    
    198  | 
     | 
     | 
    	for (i = 0; i < cpu_kh->nmemsegs; i++) { | 
    
    
    199  | 
     | 
     | 
    		if (pa >= ramsegs[i].start &&  | 
    
    
    200  | 
     | 
     | 
    		    (pa - ramsegs[i].start) < ramsegs[i].size) { | 
    
    
    201  | 
     | 
     | 
    			off += (pa - ramsegs[i].start);  | 
    
    
    202  | 
     | 
     | 
    			break;  | 
    
    
    203  | 
     | 
     | 
    		}  | 
    
    
    204  | 
     | 
     | 
    		off += ramsegs[i].size;  | 
    
    
    205  | 
     | 
     | 
    	}  | 
    
    
    206  | 
     | 
     | 
     | 
    
    
    207  | 
     | 
     | 
    	if (i == cpu_kh->nmemsegs)  | 
    
    
    208  | 
     | 
     | 
    		_kvm_err(kd, 0, "pa %lx not in dump", pa);  | 
    
    
    209  | 
     | 
     | 
     | 
    
    
    210  | 
     | 
     | 
    	return (kd->dump_off + off);  | 
    
    
    211  | 
     | 
     | 
    }  |