Re: hvm_emulate_one() usage

Tim Deegan
2013-01-10T13:16:50Z

Hi,

At 16:34 +0200 on 28 Dec (1356712445), Razvan Cojocaru
wrote:
> Hello,
>
> I have a dom0 userspace application that receives mem_events. Mem_events
> are being received if a page fault occurs, and until I clear the page
> access rights I keep receiving the event in a loop. If I do clear the
> page access rights, I will no longer receive mem_events for said page.
>
> What I thought I''d do was to add a new flag to the mem_event
response
> (MEM_EVENT_FLAG_EMULATE_WRITE), and have this code execute in
> p2m_mem_access_resume() in xen/arch/x86/mm/p2m.c:
>
> mem_event_get_response(d, &rsp);
>
> if ( rsp.flags & MEM_EVENT_FLAG_EMULATE_WRITE )
> {
>     struct hvm_emulate_ctxt ctx[1] = {};
>     struct vcpu *current_vcpu = current;
>
>     set_current(d->vcpu[rsp.vcpu_id]);

This won''t work -- as Andrew pointed out, set_current() can only happen
safely as part of a proper context switch.  If you want to cause the
vcpu to single-step, I think it''s better to follow the existing
debugger
code, which marks the vcpu for single-stepping and the schedules it as
usual.

How about (from your user-space tool):
 - use XEN_DOMCTL_DEBUG_OP_SINGLE_STEP_ON to enable single-stepping.
 - respond to the mem_event that you''re handling, causing the vcpu to
be
   unpaused.
Then when the vcpu is scheduled, it will single-step in its own context,
and you''ll get another mem_event (assuming you''ve set
HVM_PARAM_MEMORY_EVENT_SINGLE_STEP).  Once that happens:
 - use XEN_DOMCTL_DEBUG_OP_SINGLE_STEP_OFF to disable single-stepping.
 - respond to that event to unpause the vcpu.

I guess if you''re tyring to have some special case in the single-step
handler that allows it to write to a page that it normally coudn''t you
might need to add an interface for controlling that.

Cheers,

Tim.