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
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2015-01-11 23:50:26 +03:00
|
|
|
// Copyright (c) 2006-2015 Stanislav Shwartsman
|
2009-10-15 00:45:29 +04:00
|
|
|
// Written by Stanislav Shwartsman [sshwarts at sourceforge net]
|
2001-04-10 05:04:59 +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-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
|
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"
|
2009-03-22 12:52:48 +03:00
|
|
|
#include "disasm/disasm.h"
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
// maximum size of an instruction
|
2011-07-23 23:58:38 +04:00
|
|
|
#define MAX_OPCODE_LENGTH 16
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
// 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 {
|
2011-07-23 23:58:38 +04:00
|
|
|
bx_bool ready; // is current instruction ready to be printed
|
|
|
|
unsigned opcode_length;
|
|
|
|
Bit8u opcode[MAX_OPCODE_LENGTH];
|
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
|
2012-04-11 23:01:25 +04:00
|
|
|
unsigned rw; // BX_READ, BX_WRITE or BX_RW
|
2011-07-23 23:58:38 +04:00
|
|
|
unsigned size; // 1 .. 32
|
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->
|
|
|
|
|
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 (instruction == NULL)
|
|
|
|
instruction = new struct instruction_t[BX_SMP_PROCESSORS];
|
|
|
|
|
2015-01-26 00:24:13 +03:00
|
|
|
fprintf(stderr, "Initialize cpu %u\n", cpu);
|
2006-01-16 22:47:18 +03:00
|
|
|
}
|
2001-06-28 23:43:14 +04:00
|
|
|
|
2008-11-18 23:55:59 +03:00
|
|
|
void bx_instr_reset(unsigned cpu, unsigned type)
|
2001-06-28 23:43:14 +04:00
|
|
|
{
|
2011-07-23 23:58:38 +04:00
|
|
|
instruction[cpu].ready = 0;
|
2002-09-29 20:05:13 +04:00
|
|
|
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
|
|
|
|
2011-07-23 23:58:38 +04:00
|
|
|
void bx_print_instruction(unsigned cpu, const instruction_t *i)
|
2001-04-10 05:04:59 +04:00
|
|
|
{
|
2011-07-23 23:58:38 +04:00
|
|
|
char disasm_tbuf[512]; // buffer for instruction disassembly
|
|
|
|
unsigned length = i->opcode_length, n;
|
|
|
|
bx_disassembler.disasm(i->is32, i->is64, 0, 0, i->opcode, disasm_tbuf);
|
2002-09-28 21:09:04 +04:00
|
|
|
|
2011-07-23 23:58:38 +04:00
|
|
|
if(length != 0)
|
2002-09-28 21:09:04 +04:00
|
|
|
{
|
2011-07-23 23:58:38 +04:00
|
|
|
fprintf(stderr, "----------------------------------------------------------\n");
|
2015-01-26 00:24:13 +03:00
|
|
|
fprintf(stderr, "CPU %u: %s\n", cpu, disasm_tbuf);
|
|
|
|
fprintf(stderr, "LEN %u\tBYTES: ", length);
|
2011-07-23 23:58:38 +04:00
|
|
|
for(n=0;n < length;n++) fprintf(stderr, "%02x", i->opcode[n]);
|
|
|
|
if(i->is_branch)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "\tBRANCH ");
|
2008-02-06 01:57:43 +03:00
|
|
|
|
2011-07-23 23:58:38 +04:00
|
|
|
if(i->is_taken)
|
|
|
|
fprintf(stderr, "TARGET " FMT_ADDRX " (TAKEN)", i->target_linear);
|
|
|
|
else
|
|
|
|
fprintf(stderr, "(NOT TAKEN)");
|
|
|
|
}
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
for(n=0;n < i->num_data_accesses;n++)
|
2002-09-28 21:09:04 +04:00
|
|
|
{
|
2011-07-23 23:58:38 +04:00
|
|
|
fprintf(stderr, "MEM ACCESS[%u]: 0x" FMT_ADDRX " (linear) 0x" FMT_PHY_ADDRX " (physical) %s SIZE: %d\n", n,
|
|
|
|
i->data_access[n].laddr,
|
|
|
|
i->data_access[n].paddr,
|
2012-04-11 23:01:25 +04:00
|
|
|
i->data_access[n].rw == BX_READ ? "RD":"WR",
|
2011-07-23 23:58:38 +04:00
|
|
|
i->data_access[n].size);
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
2011-07-23 23:58:38 +04:00
|
|
|
fprintf(stderr, "\n");
|
2002-09-28 21:09:04 +04:00
|
|
|
}
|
2011-07-23 23:58:38 +04:00
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2011-07-23 23:58:38 +04:00
|
|
|
void bx_instr_before_execution(unsigned cpu, bxInstruction_c *bx_instr)
|
|
|
|
{
|
|
|
|
if (!active) return;
|
|
|
|
|
|
|
|
instruction_t *i = &instruction[cpu];
|
|
|
|
|
|
|
|
if (i->ready) bx_print_instruction(cpu, i);
|
|
|
|
|
|
|
|
// prepare instruction_t structure for new instruction
|
|
|
|
i->ready = 1;
|
|
|
|
i->num_data_accesses = 0;
|
|
|
|
i->is_branch = 0;
|
|
|
|
|
|
|
|
i->is32 = BX_CPU(cpu)->sregs[BX_SEG_REG_CS].cache.u.segment.d_b;
|
|
|
|
i->is64 = BX_CPU(cpu)->long64_mode();
|
|
|
|
i->opcode_length = bx_instr->ilen();
|
|
|
|
memcpy(i->opcode, bx_instr->get_opcode_bytes(), i->opcode_length);
|
|
|
|
}
|
|
|
|
|
|
|
|
void bx_instr_after_execution(unsigned cpu, bxInstruction_c *bx_instr)
|
|
|
|
{
|
|
|
|
if (!active) return;
|
|
|
|
|
|
|
|
instruction_t *i = &instruction[cpu];
|
|
|
|
if (i->ready) {
|
|
|
|
bx_print_instruction(cpu, i);
|
|
|
|
i->ready = 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
|
|
|
{
|
2011-07-23 23:58:38 +04:00
|
|
|
if (!active || !instruction[cpu].ready) return;
|
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;
|
2012-07-24 19:32:55 +04:00
|
|
|
|
|
|
|
// find linear address
|
|
|
|
instruction[cpu].target_linear = BX_CPU(cpu)->get_laddr(BX_SEG_REG_CS, new_eip);
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
|
2012-07-24 19:32:55 +04:00
|
|
|
void bx_instr_cnear_branch_taken(unsigned cpu, bx_address branch_eip, 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
|
|
|
}
|
|
|
|
|
2012-07-24 19:32:55 +04:00
|
|
|
void bx_instr_cnear_branch_not_taken(unsigned cpu, bx_address branch_eip)
|
2001-04-10 05:04:59 +04:00
|
|
|
{
|
2011-07-23 23:58:38 +04:00
|
|
|
if (!active || !instruction[cpu].ready) 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
|
|
|
}
|
|
|
|
|
2012-07-24 19:32:55 +04:00
|
|
|
void bx_instr_ucnear_branch(unsigned cpu, unsigned what, bx_address branch_eip, bx_address new_eip)
|
2006-01-16 22:47:18 +03:00
|
|
|
{
|
2002-09-28 21:09:04 +04:00
|
|
|
branch_taken(cpu, new_eip);
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
|
2015-01-11 23:45:39 +03:00
|
|
|
void bx_instr_far_branch(unsigned cpu, unsigned what, Bit16u prev_cs, bx_address prev_eip, Bit16u new_cs, bx_address new_eip)
|
2006-01-16 22:47:18 +03: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_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
|
|
|
}
|
|
|
|
|
2009-01-20 22:34:16 +03:00
|
|
|
void bx_instr_exception(unsigned cpu, unsigned vector, unsigned error_code)
|
2001-04-10 05:04:59 +04:00
|
|
|
{
|
2002-09-28 21:09:04 +04:00
|
|
|
if(active)
|
|
|
|
{
|
2009-01-20 22:34:16 +03:00
|
|
|
fprintf(stderr, "CPU %u: exception %02xh, error_code = %x\n", cpu, vector, error_code);
|
2002-09-28 21:09:04 +04:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2012-04-11 23:01:25 +04:00
|
|
|
void bx_instr_lin_access(unsigned cpu, bx_address lin, bx_phy_address phy, unsigned len, unsigned rw)
|
2001-04-10 05:04:59 +04:00
|
|
|
{
|
2011-07-23 23:58:38 +04:00
|
|
|
if(!active || !instruction[cpu].ready) return;
|
2002-09-28 21:09:04 +04:00
|
|
|
|
2012-04-11 23:01:25 +04:00
|
|
|
unsigned index = instruction[cpu].num_data_accesses;
|
2002-09-28 21:09:04 +04:00
|
|
|
|
2012-04-11 23:01:25 +04:00
|
|
|
if (index < MAX_DATA_ACCESSES) {
|
|
|
|
instruction[cpu].data_access[index].laddr = lin;
|
|
|
|
instruction[cpu].data_access[index].paddr = phy;
|
|
|
|
instruction[cpu].data_access[index].rw = rw;
|
|
|
|
instruction[cpu].data_access[index].size = len;
|
|
|
|
instruction[cpu].num_data_accesses++;
|
|
|
|
index++;
|
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|