2001-10-03 17:10:38 +04:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
2011-03-23 01:18:40 +03:00
|
|
|
// $Id$
|
2001-10-03 17:10:38 +04:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2012-07-24 19:32:55 +04:00
|
|
|
// Copyright (c) 2006-2012 Stanislav Shwartsman
|
2009-10-15 00:45:29 +04:00
|
|
|
// Written by Stanislav Shwartsman [sshwarts at sourceforge net]
|
2001-06-28 23:48:37 +04:00
|
|
|
//
|
|
|
|
// 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
|
2009-02-09 13:35:55 +03:00
|
|
|
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
2001-06-28 23:48:37 +04:00
|
|
|
|
2006-01-16 22:47:18 +03:00
|
|
|
#include <assert.h>
|
2001-06-28 23:48:37 +04:00
|
|
|
|
|
|
|
#include "bochs.h"
|
2006-03-07 01:03:16 +03:00
|
|
|
#include "cpu/cpu.h"
|
2009-03-22 12:52:48 +03:00
|
|
|
#include "disasm/disasm.h"
|
2001-06-28 23:48:37 +04:00
|
|
|
|
2006-01-16 22:47:18 +03:00
|
|
|
bxInstrumentation *icpu = NULL;
|
2001-06-28 23:48:37 +04:00
|
|
|
|
2005-11-14 21:25:41 +03:00
|
|
|
static disassembler bx_disassembler;
|
2001-06-28 23:48:37 +04:00
|
|
|
|
2008-10-01 15:36:04 +04:00
|
|
|
void bx_instr_init_env(void) {}
|
|
|
|
void bx_instr_exit_env(void) {}
|
|
|
|
|
2008-11-18 23:55:59 +03:00
|
|
|
void bx_instr_initialize(unsigned cpu)
|
2006-01-16 22:47:18 +03:00
|
|
|
{
|
|
|
|
assert(cpu < BX_SMP_PROCESSORS);
|
|
|
|
|
|
|
|
if (icpu == NULL)
|
|
|
|
icpu = new bxInstrumentation[BX_SMP_PROCESSORS];
|
|
|
|
|
|
|
|
icpu[cpu].set_cpu_id(cpu);
|
|
|
|
|
|
|
|
fprintf(stderr, "Initialize cpu %d\n", cpu);
|
|
|
|
}
|
|
|
|
|
2008-11-18 23:55:59 +03:00
|
|
|
void bxInstrumentation::bx_instr_reset(unsigned type)
|
2001-06-28 23:48:37 +04:00
|
|
|
{
|
2011-07-23 23:58:38 +04:00
|
|
|
ready = is_branch = 0;
|
2008-06-23 06:56:31 +04:00
|
|
|
num_data_accesses = 0;
|
2002-09-29 20:50:29 +04:00
|
|
|
active = 1;
|
2001-06-28 23:48:37 +04:00
|
|
|
}
|
|
|
|
|
2011-07-23 23:58:38 +04:00
|
|
|
void bxInstrumentation::bx_print_instruction(void)
|
2001-06-28 23:48:37 +04:00
|
|
|
{
|
2011-07-23 23:58:38 +04:00
|
|
|
char disasm_tbuf[512]; // buffer for instruction disassembly
|
|
|
|
bx_disassembler.disasm(is32, is64, 0, 0, opcode, disasm_tbuf);
|
2001-06-28 23:48:37 +04:00
|
|
|
|
2011-07-23 23:58:38 +04:00
|
|
|
if(opcode_length != 0)
|
2002-09-29 20:50:29 +04:00
|
|
|
{
|
2011-07-23 23:58:38 +04:00
|
|
|
unsigned n;
|
|
|
|
|
|
|
|
fprintf(stderr, "----------------------------------------------------------\n");
|
|
|
|
fprintf(stderr, "CPU: %d: %s\n", cpu_id, disasm_tbuf);
|
|
|
|
fprintf(stderr, "LEN: %d\tBYTES: ", opcode_length);
|
|
|
|
for(n=0;n < opcode_length;n++) fprintf(stderr, "%02x", opcode[n]);
|
|
|
|
if(is_branch)
|
2002-09-29 20:50:29 +04:00
|
|
|
{
|
2011-07-23 23:58:38 +04:00
|
|
|
fprintf(stderr, "\tBRANCH ");
|
|
|
|
|
|
|
|
if(is_taken)
|
|
|
|
fprintf(stderr, "TARGET " FMT_ADDRX " (TAKEN)", target_linear);
|
|
|
|
else
|
|
|
|
fprintf(stderr, "(NOT TAKEN)");
|
2002-09-29 20:50:29 +04:00
|
|
|
}
|
2011-07-23 23:58:38 +04:00
|
|
|
fprintf(stderr, "\n");
|
|
|
|
for(n=0;n < num_data_accesses;n++)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "MEM ACCESS[%u]: 0x" FMT_ADDRX " (linear) 0x" FMT_PHY_ADDRX " (physical) %s SIZE: %d\n", n,
|
|
|
|
data_access[n].laddr,
|
|
|
|
data_access[n].paddr,
|
2012-04-11 23:01:25 +04:00
|
|
|
data_access[n].rw == BX_READ ? "RD":"WR",
|
2011-07-23 23:58:38 +04:00
|
|
|
data_access[n].size);
|
|
|
|
}
|
|
|
|
fprintf(stderr, "\n");
|
2002-09-29 20:50:29 +04:00
|
|
|
}
|
2011-07-23 23:58:38 +04:00
|
|
|
}
|
2001-06-28 23:48:37 +04:00
|
|
|
|
2011-07-23 23:58:38 +04:00
|
|
|
void bxInstrumentation::bx_instr_before_execution(bxInstruction_c *i)
|
|
|
|
{
|
|
|
|
if (!active) return;
|
|
|
|
|
|
|
|
if (ready) bx_print_instruction();
|
|
|
|
|
|
|
|
// prepare instruction_t structure for new instruction
|
|
|
|
ready = 1;
|
2008-06-23 06:56:31 +04:00
|
|
|
num_data_accesses = 0;
|
2011-07-23 23:58:38 +04:00
|
|
|
is_branch = 0;
|
|
|
|
|
|
|
|
is32 = BX_CPU(cpu_id)->sregs[BX_SEG_REG_CS].cache.u.segment.d_b;
|
|
|
|
is64 = BX_CPU(cpu_id)->long64_mode();
|
|
|
|
opcode_length = i->ilen();
|
|
|
|
memcpy(opcode, i->get_opcode_bytes(), opcode_length);
|
|
|
|
}
|
|
|
|
|
|
|
|
void bxInstrumentation::bx_instr_after_execution(bxInstruction_c *i)
|
|
|
|
{
|
|
|
|
if (!active) return;
|
|
|
|
|
|
|
|
if (ready) {
|
|
|
|
bx_print_instruction();
|
|
|
|
ready = 0;
|
|
|
|
}
|
2001-06-28 23:48:37 +04:00
|
|
|
}
|
|
|
|
|
2002-09-29 20:50:29 +04:00
|
|
|
void bxInstrumentation::branch_taken(bx_address new_eip)
|
2001-06-28 23:48:37 +04:00
|
|
|
{
|
2011-07-23 23:58:38 +04:00
|
|
|
if (!active || !ready) return;
|
2001-06-28 23:48:37 +04:00
|
|
|
|
2002-09-29 20:50:29 +04:00
|
|
|
is_branch = 1;
|
|
|
|
is_taken = 1;
|
2012-07-24 19:32:55 +04:00
|
|
|
|
|
|
|
// find linear address
|
|
|
|
target_linear = BX_CPU(cpu_id)->get_laddr(BX_SEG_REG_CS, new_eip);
|
2001-06-28 23:48:37 +04:00
|
|
|
}
|
|
|
|
|
2012-07-24 19:32:55 +04:00
|
|
|
void bxInstrumentation::bx_instr_cnear_branch_taken(bx_address branch_eip, bx_address new_eip)
|
2001-06-28 23:48:37 +04:00
|
|
|
{
|
2002-09-29 20:50:29 +04:00
|
|
|
branch_taken(new_eip);
|
2001-06-28 23:48:37 +04:00
|
|
|
}
|
|
|
|
|
2012-07-24 19:32:55 +04:00
|
|
|
void bxInstrumentation::bx_instr_cnear_branch_not_taken(bx_address branch_eip)
|
2001-06-28 23:48:37 +04:00
|
|
|
{
|
2011-07-23 23:58:38 +04:00
|
|
|
if (!active || !ready) return;
|
2001-06-28 23:48:37 +04:00
|
|
|
|
2002-09-29 20:50:29 +04:00
|
|
|
is_branch = 1;
|
|
|
|
is_taken = 0;
|
2001-06-28 23:48:37 +04:00
|
|
|
}
|
|
|
|
|
2012-07-24 19:32:55 +04:00
|
|
|
void bxInstrumentation::bx_instr_ucnear_branch(unsigned what, bx_address branch_eip, bx_address new_eip)
|
2006-01-16 22:47:18 +03:00
|
|
|
{
|
2002-09-29 20:50:29 +04:00
|
|
|
branch_taken(new_eip);
|
2001-06-28 23:48:37 +04:00
|
|
|
}
|
|
|
|
|
2006-01-16 22:47:18 +03:00
|
|
|
void bxInstrumentation::bx_instr_far_branch(unsigned what, Bit16u new_cs, bx_address new_eip)
|
|
|
|
{
|
2002-09-29 20:50:29 +04:00
|
|
|
branch_taken(new_eip);
|
2001-06-28 23:48:37 +04:00
|
|
|
}
|
|
|
|
|
2002-09-29 20:50:29 +04:00
|
|
|
void bxInstrumentation::bx_instr_interrupt(unsigned vector)
|
|
|
|
{
|
|
|
|
if(active)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "CPU %u: interrupt %02xh\n", cpu_id, vector);
|
|
|
|
}
|
2001-06-28 23:48:37 +04:00
|
|
|
}
|
|
|
|
|
2009-01-20 22:34:16 +03:00
|
|
|
void bxInstrumentation::bx_instr_exception(unsigned vector, unsigned error_code)
|
2001-06-28 23:48:37 +04:00
|
|
|
{
|
2002-09-29 20:50:29 +04:00
|
|
|
if(active)
|
|
|
|
{
|
2009-01-20 22:34:16 +03:00
|
|
|
fprintf(stderr, "CPU %u: exception %02xh error_code=%x\n", cpu_id, vector, error_code);
|
2002-09-29 20:50:29 +04:00
|
|
|
}
|
2001-06-28 23:48:37 +04:00
|
|
|
}
|
|
|
|
|
2002-09-29 20:50:29 +04:00
|
|
|
void bxInstrumentation::bx_instr_hwinterrupt(unsigned vector, Bit16u cs, bx_address eip)
|
2001-06-28 23:48:37 +04:00
|
|
|
{
|
2002-09-29 20:50:29 +04:00
|
|
|
if(active)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "CPU %u: hardware interrupt %02xh\n", cpu_id, vector);
|
|
|
|
}
|
2001-06-28 23:48:37 +04:00
|
|
|
}
|
|
|
|
|
2012-04-11 23:01:25 +04:00
|
|
|
void bxInstrumentation::bx_instr_lin_access(bx_address lin, bx_phy_adress phy, unsigned len, unsigned rw)
|
2001-06-28 23:48:37 +04:00
|
|
|
{
|
2011-07-23 23:58:38 +04:00
|
|
|
if(!active || !ready) return;
|
2001-06-28 23:48:37 +04:00
|
|
|
|
2012-04-11 23:01:25 +04:00
|
|
|
if (num_data_accesses < MAX_DATA_ACCESSES) {
|
|
|
|
data_access[num_data_accesses].laddr = lin;
|
|
|
|
data_access[num_data_accesses].paddr = phy;
|
|
|
|
data_access[num_data_accesses].rw = rw;
|
|
|
|
data_access[num_data_accesses].size = len;
|
|
|
|
num_data_accesses++;
|
2002-09-29 20:50:29 +04:00
|
|
|
}
|
2001-06-28 23:48:37 +04:00
|
|
|
}
|