2001-10-03 17:10:38 +04:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
2008-03-03 18:09:30 +03:00
|
|
|
// $Id: instrument.cc,v 1.22 2008-03-03 15:09:30 sshwarts Exp $
|
2001-10-03 17:10:38 +04:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2001-04-10 06:20:02 +04:00
|
|
|
// Copyright (C) 2001 MandrakeSoft S.A.
|
2001-04-10 05:04:59 +04:00
|
|
|
//
|
|
|
|
// MandrakeSoft S.A.
|
|
|
|
// 43, rue d'Aboukir
|
|
|
|
// 75002 Paris - France
|
|
|
|
// http://www.linux-mandrake.com/
|
|
|
|
// http://www.mandrakesoft.com/
|
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
// version 2 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This library 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
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
|
|
|
|
2006-01-16 22:47:18 +03:00
|
|
|
#include <assert.h>
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2006-01-16 22:47:18 +03:00
|
|
|
#include "bochs.h"
|
2006-03-07 01:03:16 +03:00
|
|
|
#include "cpu/cpu.h"
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
// maximum size of an instruction
|
|
|
|
#define MAX_OPCODE_SIZE 16
|
|
|
|
|
|
|
|
// maximum physical addresses an instruction can generate
|
|
|
|
#define MAX_DATA_ACCESSES 1024
|
|
|
|
|
|
|
|
// Use this variable to turn on/off collection of instrumentation data
|
|
|
|
// If you are not using the debugger to turn this on/off, then possibly
|
|
|
|
// start this at 1 instead of 0.
|
2002-10-25 15:44:41 +04:00
|
|
|
static bx_bool active = 1;
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2005-11-14 21:25:41 +03:00
|
|
|
static disassembler bx_disassembler;
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2002-09-28 21:09:04 +04:00
|
|
|
static struct instruction_t {
|
2002-10-25 15:44:41 +04:00
|
|
|
bx_bool valid; // is current instruction valid
|
2001-04-10 05:04:59 +04:00
|
|
|
unsigned opcode_size;
|
2002-09-28 21:09:04 +04:00
|
|
|
unsigned nprefixes;
|
2001-04-10 05:04:59 +04:00
|
|
|
Bit8u opcode[MAX_OPCODE_SIZE];
|
2005-11-14 21:25:41 +03:00
|
|
|
bx_bool is32, is64;
|
2001-04-10 05:04:59 +04:00
|
|
|
unsigned num_data_accesses;
|
|
|
|
struct {
|
2007-12-14 00:53:55 +03:00
|
|
|
bx_address laddr; // linear address
|
|
|
|
bx_phy_address paddr; // physical address
|
|
|
|
unsigned op; // BX_READ, BX_WRITE or BX_RW
|
|
|
|
unsigned size; // 1 .. 8
|
2002-09-28 21:09:04 +04:00
|
|
|
} data_access[MAX_DATA_ACCESSES];
|
2002-10-25 15:44:41 +04:00
|
|
|
bx_bool is_branch;
|
|
|
|
bx_bool is_taken;
|
2002-09-28 21:09:04 +04:00
|
|
|
bx_address target_linear;
|
2006-01-16 22:47:18 +03:00
|
|
|
} *instruction;
|
2002-09-28 21:09:04 +04:00
|
|
|
|
|
|
|
static logfunctions *instrument_log = new logfunctions ();
|
2001-06-28 23:43:14 +04:00
|
|
|
#define LOG_THIS instrument_log->
|
|
|
|
|
2006-01-16 22:47:18 +03:00
|
|
|
void bx_instr_init(unsigned cpu)
|
|
|
|
{
|
|
|
|
assert(cpu < BX_SMP_PROCESSORS);
|
|
|
|
|
|
|
|
if (instruction == NULL)
|
|
|
|
instruction = new struct instruction_t[BX_SMP_PROCESSORS];
|
|
|
|
|
|
|
|
fprintf(stderr, "Initialize cpu %d\n", cpu);
|
|
|
|
}
|
2001-06-28 23:43:14 +04:00
|
|
|
|
2002-09-28 21:09:04 +04:00
|
|
|
void bx_instr_reset(unsigned cpu)
|
2001-06-28 23:43:14 +04:00
|
|
|
{
|
2002-09-29 20:05:13 +04:00
|
|
|
instruction[cpu].valid = 0;
|
|
|
|
instruction[cpu].nprefixes = 0;
|
|
|
|
instruction[cpu].num_data_accesses = 0;
|
|
|
|
instruction[cpu].is_branch = 0;
|
2001-06-28 23:43:14 +04:00
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2002-09-28 21:09:04 +04:00
|
|
|
void bx_instr_new_instruction(unsigned cpu)
|
2001-04-10 05:04:59 +04:00
|
|
|
{
|
2006-01-16 22:47:18 +03:00
|
|
|
if (!active) return;
|
2002-09-28 21:09:04 +04:00
|
|
|
|
|
|
|
instruction_t *i = &instruction[cpu];
|
|
|
|
|
|
|
|
if (i->valid)
|
|
|
|
{
|
|
|
|
char disasm_tbuf[512]; // buffer for instruction disassembly
|
|
|
|
unsigned length = i->opcode_size, n;
|
|
|
|
|
2005-11-14 21:25:41 +03:00
|
|
|
bx_disassembler.disasm(i->is32, i->is64, 0, 0, i->opcode, disasm_tbuf);
|
2008-02-06 01:57:43 +03:00
|
|
|
|
|
|
|
if(length != 0)
|
2002-09-28 21:09:04 +04:00
|
|
|
{
|
|
|
|
fprintf(stderr, "----------------------------------------------------------\n");
|
|
|
|
fprintf(stderr, "CPU: %d: %s\n", cpu, disasm_tbuf);
|
2006-01-17 21:17:01 +03:00
|
|
|
fprintf(stderr, "LEN: %d\tPREFIXES: %d\tBYTES: ", length, i->nprefixes);
|
2002-09-28 21:09:04 +04:00
|
|
|
for(n=0;n<length;n++) fprintf(stderr, "%02x", i->opcode[n]);
|
2008-02-06 01:57:43 +03:00
|
|
|
if(i->is_branch)
|
2002-09-28 21:09:04 +04:00
|
|
|
{
|
|
|
|
fprintf(stderr, "\tBRANCH ");
|
|
|
|
|
2008-02-06 01:57:43 +03:00
|
|
|
if(i->is_taken)
|
2006-01-17 21:17:01 +03:00
|
|
|
fprintf(stderr, "TARGET " FMT_ADDRX " (TAKEN)", i->target_linear);
|
2002-09-28 21:09:04 +04:00
|
|
|
else
|
|
|
|
fprintf(stderr, "(NOT TAKEN)");
|
2008-02-06 01:57:43 +03:00
|
|
|
}
|
2006-01-17 21:17:01 +03:00
|
|
|
fprintf(stderr, "\n");
|
2002-09-28 21:09:04 +04:00
|
|
|
for(n=0;n<i->num_data_accesses;n++)
|
|
|
|
{
|
2006-01-17 21:17:01 +03:00
|
|
|
fprintf(stderr, "MEM ACCESS[%u]: " FMT_ADDRX " (linear) 0x%08x (physical) %s SIZE: %d\n", n,
|
2008-02-06 01:57:43 +03:00
|
|
|
i->data_access[n].laddr,
|
2002-09-28 21:09:04 +04:00
|
|
|
i->data_access[n].paddr,
|
|
|
|
i->data_access[n].op == BX_READ ? "RD":"WR",
|
|
|
|
i->data_access[n].size);
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
2002-09-28 21:09:04 +04:00
|
|
|
fprintf(stderr, "\n");
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
2002-09-28 21:09:04 +04:00
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2002-09-28 21:09:04 +04:00
|
|
|
instruction[cpu].valid = 0;
|
|
|
|
instruction[cpu].nprefixes = 0;
|
|
|
|
instruction[cpu].num_data_accesses = 0;
|
|
|
|
instruction[cpu].is_branch = 0;
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
|
2002-09-28 21:09:04 +04:00
|
|
|
static void branch_taken(unsigned cpu, bx_address new_eip)
|
2001-04-10 05:04:59 +04:00
|
|
|
{
|
2006-01-16 22:47:18 +03:00
|
|
|
if (!active || !instruction[cpu].valid) return;
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2002-09-28 21:09:04 +04:00
|
|
|
// find linear address
|
2006-01-16 22:53:11 +03:00
|
|
|
bx_address laddr = BX_CPU(cpu)->get_segment_base(BX_SEG_REG_CS) + new_eip;
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2002-09-28 21:09:04 +04:00
|
|
|
instruction[cpu].is_branch = 1;
|
|
|
|
instruction[cpu].is_taken = 1;
|
|
|
|
instruction[cpu].target_linear = laddr;
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
|
2008-02-06 01:57:43 +03:00
|
|
|
void bx_instr_cnear_branch_taken(unsigned cpu, bx_address new_eip)
|
2001-04-10 05:04:59 +04:00
|
|
|
{
|
2002-09-28 21:09:04 +04:00
|
|
|
branch_taken(cpu, new_eip);
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
|
2002-09-28 21:09:04 +04:00
|
|
|
void bx_instr_cnear_branch_not_taken(unsigned cpu)
|
2001-04-10 05:04:59 +04:00
|
|
|
{
|
2006-01-16 22:47:18 +03:00
|
|
|
if (!active || !instruction[cpu].valid) return;
|
2002-09-28 21:09:04 +04:00
|
|
|
|
|
|
|
instruction[cpu].is_branch = 1;
|
|
|
|
instruction[cpu].is_taken = 0;
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
|
2006-01-16 22:47:18 +03:00
|
|
|
void bx_instr_ucnear_branch(unsigned cpu, unsigned what, bx_address new_eip)
|
|
|
|
{
|
2002-09-28 21:09:04 +04:00
|
|
|
branch_taken(cpu, new_eip);
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
|
2006-01-16 22:47:18 +03:00
|
|
|
void bx_instr_far_branch(unsigned cpu, unsigned what, Bit16u new_cs, bx_address new_eip)
|
|
|
|
{
|
2002-09-28 21:09:04 +04:00
|
|
|
branch_taken(cpu, new_eip);
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
|
2008-03-03 18:09:30 +03:00
|
|
|
void bx_instr_opcode(unsigned cpu, const Bit8u *opcode, unsigned len, bx_bool is32, bx_bool is64)
|
2001-04-10 05:04:59 +04:00
|
|
|
{
|
2006-01-16 22:47:18 +03:00
|
|
|
if (!active) return;
|
2002-09-28 21:09:04 +04:00
|
|
|
|
2008-02-06 01:57:43 +03:00
|
|
|
for(unsigned i=0;i<len;i++)
|
2002-09-28 21:09:04 +04:00
|
|
|
{
|
|
|
|
instruction[cpu].opcode[i] = opcode[i];
|
|
|
|
}
|
2008-02-06 01:57:43 +03:00
|
|
|
|
2002-09-28 21:09:04 +04:00
|
|
|
instruction[cpu].is32 = is32;
|
2005-11-14 21:25:41 +03:00
|
|
|
instruction[cpu].is64 = is64;
|
2002-09-28 21:09:04 +04:00
|
|
|
instruction[cpu].opcode_size = len;
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
|
2008-01-18 00:35:21 +03:00
|
|
|
void bx_instr_fetch_decode_completed(unsigned cpu, bxInstruction_c *i)
|
2001-04-10 05:04:59 +04:00
|
|
|
{
|
2006-01-16 22:47:18 +03:00
|
|
|
if(active) instruction[cpu].valid = 1;
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
|
2005-04-30 01:28:59 +04:00
|
|
|
void bx_instr_prefix(unsigned cpu, Bit8u prefix)
|
|
|
|
{
|
|
|
|
if(active) instruction[cpu].nprefixes++;
|
|
|
|
}
|
2002-09-28 21:09:04 +04:00
|
|
|
|
|
|
|
void bx_instr_interrupt(unsigned cpu, unsigned vector)
|
2001-04-10 05:04:59 +04:00
|
|
|
{
|
2002-09-28 21:09:04 +04:00
|
|
|
if(active)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "CPU %u: interrupt %02xh\n", cpu, vector);
|
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
|
2002-09-28 21:09:04 +04:00
|
|
|
void bx_instr_exception(unsigned cpu, unsigned vector)
|
2001-04-10 05:04:59 +04:00
|
|
|
{
|
2002-09-28 21:09:04 +04:00
|
|
|
if(active)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "CPU %u: exception %02xh\n", cpu, vector);
|
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
|
2002-09-28 21:09:04 +04:00
|
|
|
void bx_instr_hwinterrupt(unsigned cpu, unsigned vector, Bit16u cs, bx_address eip)
|
2001-04-10 05:04:59 +04:00
|
|
|
{
|
2002-09-28 21:09:04 +04:00
|
|
|
if(active)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "CPU %u: hardware interrupt %02xh\n", cpu, vector);
|
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
|
2002-09-29 20:05:13 +04:00
|
|
|
void bx_instr_mem_data(unsigned cpu, bx_address lin, unsigned size, unsigned rw)
|
2001-04-10 05:04:59 +04:00
|
|
|
{
|
|
|
|
unsigned index;
|
2006-06-17 16:09:55 +04:00
|
|
|
bx_phy_address phy;
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2006-01-16 22:47:18 +03:00
|
|
|
if(!active || !instruction[cpu].valid) return;
|
2002-09-28 21:09:04 +04:00
|
|
|
|
2008-02-06 01:57:43 +03:00
|
|
|
if (instruction[cpu].num_data_accesses >= MAX_DATA_ACCESSES)
|
2002-09-28 21:09:04 +04:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-06-17 16:09:55 +04:00
|
|
|
bx_bool page_valid = BX_CPU(cpu)->dbg_xlate_linear2phy(lin, &phy);
|
2002-09-29 20:05:13 +04:00
|
|
|
phy = A20ADDR(phy);
|
2002-09-28 21:09:04 +04:00
|
|
|
|
2002-09-29 20:05:13 +04:00
|
|
|
// If linear translation doesn't exist, a paging exception will occur.
|
|
|
|
// Invalidate physical address data for now.
|
2008-02-06 01:57:43 +03:00
|
|
|
if (!page_valid)
|
2002-09-28 21:09:04 +04:00
|
|
|
{
|
2002-09-29 20:05:13 +04:00
|
|
|
phy = 0;
|
2002-09-28 21:09:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
index = instruction[cpu].num_data_accesses;
|
|
|
|
instruction[cpu].data_access[index].laddr = lin;
|
2002-09-29 20:05:13 +04:00
|
|
|
instruction[cpu].data_access[index].paddr = phy;
|
|
|
|
instruction[cpu].data_access[index].op = rw;
|
|
|
|
instruction[cpu].data_access[index].size = size;
|
2002-09-28 21:09:04 +04:00
|
|
|
instruction[cpu].num_data_accesses++;
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|