1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
/* xen_dom0.c
*
* Copyright (C) 2015 David Anderson
* Copyright (C) 2015 Red Hat, Inc. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* Author: David Anderson
*/
#include "defs.h"
#include "xen_dom0.h"
static struct xen_kdump_data xen_kdump_data = { 0 };
struct xen_kdump_data *xkd = &xen_kdump_data;
void
dump_xen_kdump_data(FILE *fp)
{
int i, others;
fprintf(fp, " xen_kdump_data: %s\n",
XEN_CORE_DUMPFILE() ? " " : "(unused)");
if (!XEN_CORE_DUMPFILE())
return;
fprintf(fp, " flags: %lx (", xkd->flags);
others = 0;
if (xkd->flags & KDUMP_P2M_INIT)
fprintf(fp, "%sKDUMP_P2M_INIT", others++ ? "|" : "");
if (xkd->flags & KDUMP_CR3)
fprintf(fp, "%sKDUMP_CR3", others++ ? "|" : "");
if (xkd->flags & KDUMP_MFN_LIST)
fprintf(fp, "%sKDUMP_MFN_LIST", others++ ? "|" : "");
fprintf(fp, ")\n");
fprintf(fp, " p2m_mfn: %lx\n",
xkd->p2m_mfn);
fprintf(fp, " cr3: %lx\n",
xkd->cr3);
fprintf(fp, " last_mfn_read: %lx\n",
xkd->last_mfn_read);
fprintf(fp, " last_pmd_read: %lx\n",
xkd->last_pmd_read);
fprintf(fp, " page: %lx\n",
(ulong)xkd->page);
fprintf(fp, " accesses: %ld\n",
xkd->accesses);
fprintf(fp, " cache_hits: %ld ",
xkd->cache_hits);
if (xkd->accesses)
fprintf(fp, "(%ld%%)",
xkd->cache_hits * 100 / xkd->accesses);
fprintf(fp, "\n p2m_frames: %d\n",
xkd->p2m_frames);
fprintf(fp, " xen_phys_start: %lx\n",
xkd->xen_phys_start);
fprintf(fp, " xen_major_version: %d\n",
xkd->xen_major_version);
fprintf(fp, " xen_minor_version: %d\n",
xkd->xen_minor_version);
fprintf(fp, " p2m_mfn_frame_list: %lx\n",
(ulong)xkd->p2m_mfn_frame_list);
for (i = 0; i < xkd->p2m_frames; i++)
fprintf(fp, "%lx ", xkd->p2m_mfn_frame_list[i]);
if (i) fprintf(fp, "\n");
}
void
process_xen_note(ulong type, void *data, size_t sz)
{
ulong *up = (ulong*) data;
unsigned words = sz / sizeof(ulong);
pc->flags |= XEN_CORE;
xkd->last_mfn_read = UNINITIALIZED;
xkd->last_pmd_read = UNINITIALIZED;
if (type == NT_XEN_KDUMP_CR3)
error(WARNING,
"obsolete Xen n_type: %lx (NT_XEN_KDUMP_CR3)\n\n",
type);
if (type == NT_XEN_KDUMP_CR3 && words == 1) {
xkd->flags |= KDUMP_CR3;
/*
* Use the first cr3 found.
*/
if (!xkd->cr3)
xkd->cr3 = *up;
} else {
xkd->flags |= KDUMP_MFN_LIST;
/*
* If already set, overridden with --pfm_mfn
*/
if (!xkd->p2m_mfn)
xkd->p2m_mfn = up[words-1];
if (words > 9 && !xkd->xen_phys_start)
xkd->xen_phys_start = up[words-2];
xkd->xen_major_version = up[0];
xkd->xen_minor_version = up[1];
}
}
/*
* Override the dom0 p2m mfn in the XEN_ELFNOTE_CRASH_INFO note
* in order to initiate a crash session of a guest kernel.
*/
void
xen_kdump_p2m_mfn(char *arg)
{
ulong value;
int errflag;
errflag = 0;
value = htol(arg, RETURN_ON_ERROR|QUIET, &errflag);
if (!errflag) {
xen_kdump_data.p2m_mfn = value;
if (CRASHDEBUG(1))
error(INFO,
"xen_kdump_data.p2m_mfn override: %lx\n",
value);
} else
error(WARNING, "invalid p2m_mfn argument: %s\n", arg);
}
/*
* Fujitsu dom0/HV sadump-generated dumpfile, which requires
* the --p2m_mfn command line argument.
*/
int
is_sadump_xen(void)
{
if (xen_kdump_data.p2m_mfn) {
if (!XEN_CORE_DUMPFILE()) {
pc->flags |= XEN_CORE;
xkd->last_mfn_read = UNINITIALIZED;
xkd->last_pmd_read = UNINITIALIZED;
xkd->flags |= KDUMP_MFN_LIST;
}
return TRUE;
}
return FALSE;
}
void
set_xen_phys_start(char *arg)
{
ulong value;
int errflag = 0;
value = htol(arg, RETURN_ON_ERROR|QUIET, &errflag);
if (!errflag)
xen_kdump_data.xen_phys_start = value;
else
error(WARNING, "invalid xen_phys_start argument: %s\n", arg);
}
ulong
xen_phys_start(void)
{
return xkd->xen_phys_start;
}
int
xen_major_version(void)
{
return xkd->xen_major_version;
}
int
xen_minor_version(void)
{
return xkd->xen_minor_version;
}
struct xen_kdump_data *
get_xen_kdump_data(void)
{
return xkd;
}
/*
* Translate a xen domain's pseudo-physical address into the
* xen machine address. Since there's no compression involved,
* just the last phys_to_machine_mapping[] page read is cached,
* which essentially caches 1024 p2m translations.
*/
physaddr_t
xen_kdump_p2m(physaddr_t pseudo)
{
ulong pfn, mfn_frame;
ulong *mfnptr;
ulong mfn_idx, frame_idx;
physaddr_t paddr;
if (pc->curcmd_flags & XEN_MACHINE_ADDR)
return pseudo;
if (!(xkd->flags & KDUMP_P2M_INIT)) {
if (!machdep->xen_kdump_p2m_create)
error(FATAL,
"xen kdump dumpfiles not supported on this architecture\n");
if ((xkd->page =
(char *)malloc(PAGESIZE())) == NULL)
error(FATAL,
"cannot malloc xen kdump data page\n");
if (!machdep->xen_kdump_p2m_create(xkd))
error(FATAL,
"cannot create xen kdump pfn-to-mfn mapping\n");
xkd->flags |= KDUMP_P2M_INIT;
}
#ifdef IA64
return ia64_xen_kdump_p2m(xkd, pseudo);
#endif
xkd->accesses++;
pfn = (ulong)BTOP(pseudo);
mfn_idx = pfn / (PAGESIZE()/sizeof(ulong));
frame_idx = pfn % (PAGESIZE()/sizeof(ulong));
if (mfn_idx >= xkd->p2m_frames) {
if (CRASHDEBUG(8))
fprintf(fp, "xen_kdump_p2m: paddr/pfn: %llx/%lx: "
"mfn_idx nonexistent\n",
(ulonglong)pseudo, pfn);
return P2M_FAILURE;
}
mfn_frame = xkd->p2m_mfn_frame_list[mfn_idx];
if (mfn_frame == xkd->last_mfn_read)
xkd->cache_hits++;
else {
int res;
if (CRASHDEBUG(8))
fprintf(fp, "xen_kdump_p2m: paddr/pfn: %llx/%lx: "
"read mfn_frame: %llx\n",
(ulonglong)pseudo, pfn, PTOB(mfn_frame));
pc->curcmd_flags |= XEN_MACHINE_ADDR;
res = readmem((physaddr_t)PTOB(mfn_frame), PHYSADDR,
xkd->page, PAGESIZE(),
"xen_kdump_p2m mfn frame", RETURN_ON_ERROR);
pc->curcmd_flags &= ~XEN_MACHINE_ADDR;
if (!res)
return P2M_FAILURE;
}
xkd->last_mfn_read = mfn_frame;
mfnptr = ((ulong *)(xkd->page)) + frame_idx;
paddr = (physaddr_t)PTOB((ulonglong)(*mfnptr));
paddr |= PAGEOFFSET(pseudo);
if (CRASHDEBUG(7))
fprintf(fp,
"xen_kdump_p2m(%llx): mfn_idx: %ld frame_idx: %ld"
" mfn_frame: %lx mfn: %lx => %llx\n",
(ulonglong)pseudo, mfn_idx, frame_idx,
mfn_frame, *mfnptr, (ulonglong)paddr);
return paddr;
}
|