How to intercept interrupts from guest domains
Hi!
I am writing my master thesis on virtualization with Xen. I am trying to
intercept the hypercalls coming from the guest domains. More
specific I am trying to determine where in memory a guest domain is writing. Does anyone have a hint on where in the code I should try to do this?
Rgds
Mads
Quoted 5 lines · Mads Bergdal
I am writing my master thesis on virtualization with Xen. I am trying to
intercept the hypercalls coming from the guest domains. More
specific I am trying to determine where in memory a guest domain is writing. Does anyone have a hint on where in the code I should try to do this?
If you want to know where a guest is writing you need to do more than intercept hypercalls. You want to intercept memory accesses to, which would liekly mean you need to run on shadow pagetables and manipulate access permissions to trap on first access to a page.
-- Keir
Quoted 8 lines · Keir Fraser
Quoted 5 lines · Mads Bergdal
I am writing my master thesis on virtualization with Xen. I am trying to
intercept the hypercalls coming from the guest domains. More
specific I am trying to determine where in memory a guest domain is writing. Does anyone have a hint on where in the code I should try to do this?
Quoted 6 lines
If you want to know where a guest is writing you need to do more than intercept hypercalls. You want to intercept memory accesses to, which would liekly mean you need to run on shadow pagetables and manipulate access permissions to trap on first access to a page.
-- Keir
Yes, that sounds reasonable. Do you know where in the code this could be achieved?
Mads
Quoted 6 lines · Mads Bergdal
Quoted 6 lines
Quoted 5 lines
I am writing my master thesis on virtualization with Xen. I am trying to
intercept the hypercalls coming from the guest domains. More
specific I am trying to determine where in memory a guest domain is writing. Does anyone have a hint on where in the code I should try to do this?
Quoted 8 lines
Quoted 6 lines
If you want to know where a guest is writing you need to do more than intercept hypercalls. You want to intercept memory accesses to, which would liekly mean you need to run on shadow pagetables and manipulate access permissions to trap on first access to a page.
-- Keir
Yes, that sounds reasonable. Do you know where in the code this could be achieved?
What's the intended purpose? You could perhaps look at the log-dirty shadow mode. This is used to track which pages have been modified by the guest -- a page which the guest maps writeable is not made writable in the shadow page tables until the time of the first write access (when that page is added to a 'dirty log' for further processing).
Be warned that modifying the shadow code is rather more difficult than a project that would simply involve adding a hook point to every hypercall!
-- Keir
Quoted 22 lines · Keir Fraser
Quoted 5 lines · Mads Bergdal
Quoted 5 lines
Quoted 5 lines
I am writing my master thesis on virtualization with Xen. I am trying to
intercept the hypercalls coming from the guest domains. More
specific I am trying to determine where in memory a guest domain is writing. Does anyone have a hint on where in the code I should try to do this?Quoted 8 lines
Quoted 6 lines
If you want to know where a guest is writing you need to do more than intercept hypercalls. You want to intercept memory accesses to, which would liekly mean you need to run on shadow pagetables and manipulate access permissions to trap on first access to a page.
-- Keir
Yes, that sounds reasonable. Do you know where in the code this could be achieved?
What's the intended purpose? You could perhaps look at the log-dirty shadow mode. This is used to track which pages have been modified by the guest -- a page which the guest maps writeable is not made writable in the shadow page tables until the time of the first write access (when that page is added to a 'dirty log' for further processing).
Quoted 4 lines
Be warned that modifying the shadow code is rather more difficult than a project that would simply involve adding a hook point to every hypercall!
-- Keir
Thanks for the hints. I really appreciate it.
My main purpose of this is to try to monitor when a guestdomain tries to write to a specific address in it's memory. (An address that it should not write to) And then get the Hypervisor to notify my userspace "surveillance" program about this. I have spent quite some time now reading the code. I must admit I am a bit lost. I am not sure where in the code I should be looking to get started.
From what you write above I take it that you think the easiest approach
is to hook into the hypercalls? I that case which hypercalls and where?
If not, where should I look to learn he internals of the shadow mode?
Hope I am not wasting your time...
Mads
Quoted 62 lines
-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Mads Bergdal
Sent: 21 September 2006 12:46
To: [email protected]
Subject: [Xen-devel] Re: How to intercept interrupts from guest domainsQuoted 3 lines · Keir Fraser
Quoted 1 line · Mads Bergdal
Quoted 1 line
Quoted 1 line
I am writing my master thesis on virtualization with
Xen. I am trying to
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
intercept the hypercalls coming from the guest domains. More
specific I am trying to determine where in memory aguest domain is
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
writing. Does anyone have a hint on where in the code I
should try to do
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 1 line
this?
If you want to know where a guest is writing you need to
do more than
Quoted 1 line
Quoted 1 line
Quoted 1 line
intercept hypercalls. You want to intercept memory
accesses to, which would
Quoted 1 line
Quoted 1 line
Quoted 1 line
liekly mean you need to run on shadow pagetables and
manipulate access
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
permissions to trap on first access to a page.
-- Keir
Yes, that sounds reasonable. Do you know where in the code
this could be
Quoted 3 lines
Quoted 1 line
achieved?
What's the intended purpose? You could perhaps look at the
log-dirty shadow
Quoted 1 line
mode. This is used to track which pages have been modified
by the guest -- a
Quoted 1 line
page which the guest maps writeable is not made writable in
the shadow page
Quoted 1 line
tables until the time of the first write access (when that
page is added to
Quoted 3 lines
a 'dirty log' for further processing).
Be warned that modifying the shadow code is rather more
difficult than a
Quoted 1 line
project that would simply involve adding a hook point to
every hypercall!
Quoted 2 lines
-- Keir
Thanks for the hints. I really appreciate it.
My main purpose of this is to try to monitor when a guestdomain tries to
write to a specific address in it's memory. (An address that it should
not write to) And then get the Hypervisor to notify my userspace "surveillance" program about this.
I have spent quite some time now reading the code. I must admit I am a
bit lost. I am not sure where in the code I should be looking to get started.
Quoted 6 lines
From what you write above I take it that you think the
easiest approach
is to hook into the hypercalls? I that case which
hypercalls and where?If not, where should I look to learn he internals of the shadow mode?
To catch a kernel writing to a specific address, you can't rely on hypercalls - there is no hypercall to indicate that a particular address (or any address) has been written to.
When the kernel writes to memory, the processor will read the page-table entry according to CR3 and the relevant page-table entries after that (depending on whether it's 32-bit, 32-bit+PAE or 64-bit and whether the pages are "large" 2MB/4MB or "small" 4KB). If the page indicated by the page-table is writable, then the processor will do nothing other than write the requested data to the memory. If the page indicated by the page-table is not writable, then the a page-fault is issued. In the case of Xen, this is trapped by Xen directly, and handled in the page-fault-handling routine. Depending on what the reason for page-fault is, Xen either changes the page to writable, or issues the page-fault back to the kernel (if Xen didn't think this page should be writable).
Xen uses non-writable pages to keep track of various things, so it's perfectly "normal" for Xen to take a page-fault for write to a non-writable page, and Xen "knows" whether a page SHOULD be written to or not. A typical case is the shadow page-table handling itself. Page-tables in the guest are write-protected, Xen takes the page-fault and figures out what the new value for the page-table-entry should be (remember that the Kernel doesn't actually know that memory mnay not be organized like the kernel expects) and the ACTUAL page-table (shadow version) used by the processor is updated accordingly, as well as writing the new value to the kernel's "visible" page-table [but this page-table is never used by the processor to address memory].
To make a memory address "trap" in the hypervisor, you would therefor have to make sure the page that contains that address is always write-protected, and when the page gets a hit, compare with your "magical" value, and if so, indicate it back to your "supervisor" application. Depending on the granularity, you could potentially just write-protect the page and never have to worry about write-enable/disable again.
[This gets even more tricky on SMP systems, I hope you're only planning on doing this for a single processor!]
Is the target address you're after a physical or a virtual address?
This is just an overview of how paging (and shadow paging) works... There's more to it than the above couple of paragraphs, as you can imagine.
--
Mats
Quoted 13 lines
Hope I am not wasting your time...
Mads
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
Quoted 96 lines · Mats
Quoted 49 lines
-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Mads Bergdal
Sent: 21 September 2006 12:46
To: [email protected]
Subject: [Xen-devel] Re: How to intercept interrupts from guest domainsQuoted 3 lines · Keir Fraser
Quoted 1 line · Mads Bergdal
Quoted 1 line
Quoted 1 line
I am writing my master thesis on virtualization with
Xen. I am trying to
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
intercept the hypercalls coming from the guest domains. More
specific I am trying to determine where in memory aguest domain is
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
writing. Does anyone have a hint on where in the code I
should try to do
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 1 line
this?
If you want to know where a guest is writing you need to
do more than
Quoted 1 line
Quoted 1 line
Quoted 1 line
intercept hypercalls. You want to intercept memory
accesses to, which would
Quoted 1 line
Quoted 1 line
Quoted 1 line
liekly mean you need to run on shadow pagetables and
manipulate access
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
permissions to trap on first access to a page.
-- Keir
Yes, that sounds reasonable. Do you know where in the code
this could be
Quoted 2 lines
Quoted 1 line
achieved?
What's the intended purpose? You could perhaps look at the
log-dirty shadow
Quoted 1 line
mode. This is used to track which pages have been modified
by the guest -- a
Quoted 1 line
page which the guest maps writeable is not made writable in
the shadow page
Quoted 1 line
tables until the time of the first write access (when that
page is added to
Quoted 3 lines
a 'dirty log' for further processing).
Be warned that modifying the shadow code is rather more
difficult than a
Quoted 1 line
project that would simply involve adding a hook point to
every hypercall!
Quoted 1 line
-- Keir
Thanks for the hints. I really appreciate it.
My main purpose of this is to try to monitor when a guestdomain tries to write to a specific address in it's memory. (An address that it should not write to) And then get the Hypervisor to notify my userspace "surveillance" program about this. I have spent quite some time now reading the code. I must admit I am a bit lost. I am not sure where in the code I should be looking to get started.
Quoted 6 lines
From what you write above I take it that you think the
easiest approach
is to hook into the hypercalls? I that case which
hypercalls and where?If not, where should I look to learn he internals of the shadow mode?
To catch a kernel writing to a specific address, you can't rely on hypercalls - there is no hypercall to indicate that a particular address (or any address) has been written to.
When the kernel writes to memory, the processor will read the page-table entry according to CR3 and the relevant page-table entries after that (depending on whether it's 32-bit, 32-bit+PAE or 64-bit and whether the pages are "large" 2MB/4MB or "small" 4KB). If the page indicated by the page-table is writable, then the processor will do nothing other than write the requested data to the memory. If the page indicated by the page-table is not writable, then the a page-fault is issued. In the case of Xen, this is trapped by Xen directly, and handled in the page-fault-handling routine. Depending on what the reason for page-fault is, Xen either changes the page to writable, or issues the page-fault back to the kernel (if Xen didn't think this page should be writable).
Xen uses non-writable pages to keep track of various things, so it's perfectly "normal" for Xen to take a page-fault for write to a non-writable page, and Xen "knows" whether a page SHOULD be written to or not. A typical case is the shadow page-table handling itself. Page-tables in the guest are write-protected, Xen takes the page-fault and figures out what the new value for the page-table-entry should be (remember that the Kernel doesn't actually know that memory mnay not be organized like the kernel expects) and the ACTUAL page-table (shadow version) used by the processor is updated accordingly, as well as writing the new value to the kernel's "visible" page-table [but this page-table is never used by the processor to address memory].
To make a memory address "trap" in the hypervisor, you would therefor have to make sure the page that contains that address is always write-protected, and when the page gets a hit, compare with your "magical" value, and if so, indicate it back to your "supervisor" application. Depending on the granularity, you could potentially just write-protect the page and never have to worry about write-enable/disable again.
[This gets even more tricky on SMP systems, I hope you're only planning on doing this for a single processor!]
Quoted 21 lines
Is the target address you're after a physical or a virtual address?
This is just an overview of how paging (and shadow paging) works... There's more to it than the above couple of paragraphs, as you can imagine.
--
MatsQuoted 12 lines
Hope I am not wasting your time...
Mads
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
Thanks a lot!
Well clearly this is a bit more complicated than I first excepted.... I'm only doing this for single 32-bit processors....
I guess I'm primarily interested in the virtual addresses. (I think...). If my 'supervisor' program first finds out which page the guest kernels syscall table (for instance...) maps to, the I would only need the hypervisor to send an message if this page is being accessed and let the 'supervisor' program take care of the rest?
The most promising thing to do then is to:
- find out which page my 'untouchable' address is on and mark this page as non-writable. (how?) Or are all pages marked as non-writable by Xen as default?
- This way the guest will always get a page fault, and trap to Xen. (Where in Xen is this trap handled?)
-I maintain (in Xen) an array of pages that is not supposed to be written to, and compare this to the page the guest kernel wants to write to. -If the page is in the array I send a message (how?) to my program saying that the guest is trying to access a marked page. -My program then decides if the guest kernel is allowed to proceed, and responds to the Hypervisor (how?)
Does this sound doable?
If so, where in the xen code is the xen page-fault-handling routine? And how can I send messages to and from the Hypervisor?
Mads
Quoted 5 lines
Thanks a lot!
Well clearly this is a bit more complicated than I first excepted.... I'm only doing this for single 32-bit processors....
Quoted 8 lines
I guess I'm primarily interested in the virtual addresses. (I think...).
If my 'supervisor' program first finds out which page the guest kernels
syscall table (for instance...) maps to, the I would only need the hypervisor to send an message if this page is being accessed and let the
'supervisor' program take care of the rest?
Ok, so you're trying to write something that detects writes to the syscall page. Isn't that write-protected already [I haven't checked, but I expect it to be]. In which case any kernel write to that page would be trapped with a kernel-oops...
But perhaps that's not really the page you're looking to see if it's being written to?
Quoted 6 lines
The most promising thing to do then is to:
- find out which page my 'untouchable' address is on and mark this
page as non-writable. (how?) Or are all pages marked as
non-writable by
Xen as default?
Yes. How? I don't know... It may be that you'd have to invent your own way to "fiddle" with the page-table.
Quoted 2 lines
- This way the guest will always get a page fault, and trap to Xen. (Where in Xen is this trap handled?)
You'd end up in "do_page_fault" in xen/arch/x86/traps.c - note that there is also x86_64 and x86_32/traps.c - these are additional architecture specific trap handlers. The function do_page_fault is 32/64 independent [obviously only to the x86 architecture, this code DOES NOT work on ia64 or ppc for example], so it lives at the upper level...
do_page_fault calls "fixup_page_fault" which calls "shadow_fault", which in the current shadow page-table code ends up in "sh_page_fault".
Any of these functions should be able to detect your fault. It depends on how you want to treat the handling, and what information you need to have available, which function is best for you.
Quoted 4 lines
-I maintain (in Xen) an array of pages that is not supposed to be written to, and compare this to the page the guest kernel wants to write
to.
Seems reasonable.
Quoted 2 lines
-If the page is in the array I send a message (how?) to my program saying that the guest is trying to access a marked page.
The usual method is by "event channel". Seexen/include/public/event_channel.h
Quoted 3 lines
-My program then decides if the guest kernel is allowed to proceed, and
responds to the Hypervisor (how?)
Event channel?
Quoted 2 lines
Does this sound doable?
Certainly possible. Non-trivial, but doable.
Quoted 16 lines
If so, where in the xen code is the xen page-fault-handling routine? And
how can I send messages to and from the Hypervisor?Mads
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel