making bare metal simulation work (%rip sets to zero when starting to run)

Viewed 168

and thanks for your previous help in How to make "%bp.hap.run-until name = X86_HLT_Instr" work?

My next obstacle is that %rip magically turns zero when I start running.

My test program:

#include <simics/magic-instruction.h>
__attribute__((noinline))
void MagicBreakpoint() {
  MAGIC_BREAKPOINT;
  asm volatile ("hlt");
}
extern "C" void _start() {
  asm volatile ("mov $42, %rax");
  MagicBreakpoint();
}

0000000000401000 <_Z15MagicBreakpointv>:
  401000:   53                      push   %rbx
  401001:   b8 11 47 00 00          mov    $0x4711,%eax
  401006:   0f a2                   cpuid  
  401008:   f4                      hlt    
  401009:   5b                      pop    %rbx
  40100a:   c3                      retq   
  40100b:   0f 1f 44 00 00          nopl   0x0(%rax,%rax,1)

0000000000401010 <_start>:
  401010:   48 c7 c0 2a 00 00 00    mov    $0x2a,%rax
  401017:   e9 e4 ff ff ff          jmpq   401000 <_Z15MagicBreakpointv>


What I want to see is the execution starting from _start, setting %rax to 42, then hitting the magic instruction, then exiting. Instead, the execution starts from %rip=0.

my script:

run-command-file "%simics%/targets/qsp-x86/firststeps-no-network.simics"

$start = ($system.mb.cpu0.core[0][0].load-binary ./small)
$system.mb.cpu0.core[0][0].set-pc $start   ## Special command for the PC
$system.mb.cpu0.core[0][0].write-reg "rsp" 0x7fffffffdf50

enable-magic-breakpoint

print -x %rip
print -x %rsp

step-instruction
print -x %rip
quit

./simics -no-gui t2.simics 
Intel Simics 6 (build 6096 linux64) Copyright 2010-2021 Intel Corporation


Use of this software is subject to appropriate license.
Type 'copyright' for details on copyright and 'help' for on-line documentation.

[board.mb.cpu0.core[0][0] info] VMP disabled. Failed to open device.

WARNING: Simics failed to enable VMP. Enabling VMP substantially improves
         simulation performance. The problem is most likely caused by the
         vmxmon kernel module not being properly installed or updated.
         See the "Simics User's Guide", the "Performance" section,
         for instructions how to setup VMP.


Welcome to Simics!

An x86 target machine, referred to as a Quick Start Platform (QSP)
in the documentation, has been just created.
To start the simulation, enter the command "run" (or simply "r") at
the Simics prompt. This will boot Linux and automatically log you in.
You will see the login appear in the serial console window.

Note that during the boot Linux will emit a couple
of harmless warning messages related to ACPI errors.

To pause the simulation, use the command "stop". To resume simulation,
enter the command "run" again.

0x401010
0x7fffffffdf50
[board.mb.cpu0.core[0][0]] Exception: General_Protection_Exception
0x0

As you can see, before executing step-instruction, %rip is 0x401010, and right after step-instruction, %rip is zero.

1 Answers

Your problem is that the x86 cpu starts in 16-bit legacy mode, but your code in 64-bit code.

With this target you could try running it until it reaches 64-bit mode, before loading and executing the binary:

simics> bp.hap.run-until name = Core_Mode_Switch index = 5

When simulation stops you should be in 64-bit mode (which index 5 will specify). You can check current execution mode by running the pregs command. At this point it should likely work to run you code starting with "$start =".

Related