Previous - Up - Next

21.1   Switching Contexts Manually

Changing the current context of a processor is very simple. Every processor starts out with primary-context as current context, so for this to be interesting we will need to create another context first:

simics> new-context my-little-context

Then, simply set the current-context attribute of the processor, either directly, or via the set-context command:

simics> cpu0.set-context my-little-context

And we are all done.

The tricky thing is to know when to change the context. This generally involves monitoring the state of the simulated machine, and looking for more or less subtle signs of relevant change. For example, assume that we are interested in the kernel. What we would like to do then is to change to the kernel context when the processor enters supervisor mode, and change back to primary-context when it enters user mode.

Conveniently, Simics triggers a Core_Mode_Change hap whenever the privilege level changes. The following Python script listens for that hap, and changes context accordingly.

def set_context(user_arg, cpu, old_mode, new_mode):
    if new_mode == Sim_CPU_Mode_Supervisor:
        cpu.current_context = conf.my_little_context
        cpu.current_context = conf.primary_context
SIM_hap_add_callback_obj("Core_Mode_Change", conf.cpu0,
                         0, set_context, None)

Note that, in this example, all of userspace is covered by a single context (primary-context). Distinguishing between different userspace processes is, as mentioned earlier, not something that Simics can do by itself. It needs a process tracker for that; this is the topic of the next section.

Previous - Up - Next