Expand description
§The MacSim Format
MacSim is a cycle-level simulator for
CPUs and GPUs. Its x86 front end reads traces produced by a Pin tool
(tools/x86_trace_generator), and kvmtrace writes the same files, so
MacSim can consume them without changes. This chapter explains the files,
then each field and where its value comes from, including the places
where a whole-system trace does not fit a format designed for one user
process.
§The files
For a trace directory trace/:
trace/trace.txt "x86 1.3\n<threads>\n<tid> <start>\n..."
trace/trace_0.raw gzip of 80-byte records, vCPU 0
trace/trace_0.side gzip sideband: one entry per record of trace_0.raw
trace/trace_1.raw ... vCPU 1
trace/trace_1.sidetrace.txt is what you point MacSim at. Its first line names the format
and the generator version (1.3, which MacSim’s reader checks). Then come
the number of threads and one line per thread: its id and its start
instruction. Each vCPU becomes one MacSim thread.
§One record
A record is trace_info_cpu_s from MacSim’s trace_read.h without its
last field (m_instruction_next_addr, which the Pin tool does not write
either), 80 bytes with C layout. The test
macsim_record_layout_matches_the_c_struct compiles a C program with the
real header and checks every offset against
MacsimRecord::to_bytes.
| Field | Filled from |
|---|---|
m_num_read_regs, m_src[9] | registers the instruction reads, as Pin 3.13 register numbers (macsim::pin_reg), sorted, deduplicated, at most 8 |
m_num_dest_regs, m_dst[6] | registers it writes, at most 5 |
m_cf_type | from the decoder’s flow control: NOT_CF, BR, CBR, CALL, IBR, ICALL, ICO, RET, SYS, ICBR |
m_has_immediate | always 0, as in the Pin tool |
m_opcode | a CPU_OPCODE_ENUM value: see below |
m_has_st, m_num_ld | the static counts, as Pin reports them: a gather counts as one load |
m_is_fp | uses x87/MMX/SSE/AVX registers |
m_write_flg | writes RFLAGS |
m_size | instruction length |
m_ld_vaddr1, m_ld_vaddr2, m_mem_read_size | linear addresses of the first two loads, size of the first |
m_st_vaddr, m_mem_write_size | linear address and size of the store |
m_instruction_addr | linear address of the instruction (CS base + RIP) |
m_branch_target | taken target, or the conditional branch’s target |
m_rep_dir | RFLAGS.DF |
m_actually_taken | the branch was taken |
§Opcodes
MacSim does not decode x86 itself; it relies on the trace’s opcode
category. The Pin tool takes it from Intel XED’s instruction category.
kvmtrace uses a table generated from XED’s own datafiles
(tools/gen_xed_categories.py: 2 046 instruction classes), then applies
the Pin tool’s special cases by name: NOP → TR_NOP, MUL/IMUL →
TR_MUL, DIV/IDIV → TR_DIV, the floating-point versions →
TR_FMUL/TR_FDIV, prefetch hints → PREFETCH_*. If an instruction is
missing from the table, a heuristic picks a category and the session
counts it in Stats::heuristic_opcodes;
on the Linux workloads in chapter 9 that count was zero.
§Where a whole-system trace does not fit, and the sideband
The format was designed for one user-space process traced by Pin. A full-system trace needs more per instruction than the 80-byte record holds:
- Privilege level and page table. There is no field for CS or CR3, yet a full-system simulator must know whether an instruction ran in the kernel (CS bits 1:0 = 0) or in user space (= 3), and which process’s page table (CR3) its addresses belong to.
- Physical addresses. None in the record.
- The exact instruction.
m_opcodeis a category (BINARY,IO, …), not the opcode. - Two loads and one store. Instructions that touch more (
movs,push [mem], gathers with up to 16 lanes,outreading the TSS I/O bitmap) do not fit.
Changing the record would break MacSim’s reader, so kvmtrace keeps
trace_<v>.raw exactly as MacSim expects and writes the rest to
trace_<v>.side: entry k describes record k. Each entry has CS,
CPL, CR3, the physical address of the instruction, its bytes, the vCPU,
both sequence numbers, and every memory access with linear and physical
addresses, kind, size and outcome. The accesses that are also in the
.raw record are marked. The layout is in crate::side, and in C in
macsim/kvmtrace_side.h for use inside MacSim. A test compiles that header
and checks every offset against the writer, and kvmtrace side DIR --check verifies a trace’s sideband against its .raw files:
$ kvmtrace side trace --check
vCPU 0: 821473 entries = 821473 .raw records; raw_index mismatches 0; MacSim address mismatches 0
CPL 0: 719717, CPL 3: 101756; CS.RPL != CPL: 0; 1 distinct CR3; 1117828 accesses, 16 without a physical address
RESULT : sideband consistent with the MacSim records
$ kvmtrace side trace --count 1
0 g0 cpu0 cs=0x0033 (rpl 3) cpl3 cr3=0x517a000 pc=0x401209 pa=0x3b15209 op=IO bytes=[ef] X1@0x401209(pa 0x3b15209) R2@0xfffffe0000003066(pa 0x1f405066) R2@0xfffffe00000030c7(pa 0x1f4050c7)(The 16 accesses without a physical address are the 16 attempts that
page-faulted: their pages were not mapped yet. The first record is the
user-mode out that opened the trace window, with its two implicit reads
of the TSS I/O-permission bitmap.)
Two smaller mismatches remain, and are counted rather than hidden:
- Address-space aliasing. MacSim caches decoded instructions by PC, per
thread. With several processes on one vCPU, two different instructions
can share a PC. The session counts these in
Stats::pc_aliases. - Events. Interrupt frames and IDT reads have no place in the record.
They are in
.kvt; the handlers’ instructions are in both.
Faulting instructions are kept: Pin instruments before execution, so a
faulting instruction appears in a Pin trace too, and kvmtrace matches
that by default (macsim_include_faulted).
§Checking a file
kvmtrace macsim DIR --vcpu N prints the records; from Rust, use
MacsimReader and
SideReader side by side.