Add 'info pg' command to monitor
Hi all,
I am a CS professor (and, newly, a second-time contributor). I have been using qemu in my courses for over a decade, especially a course that asks students to write major pieces of an OS kernel from starter code.
I have some patches, originally from Austin Clements at MIT, that I have found useful over the years and that may be useful to others. It would also be nice not to have to build a custom qemu each semester. I have cleared upstreaming these with Austin, the original author.
This patch set adds an 'info pg' command to the monitor, which prints a nicer view of the page tables. A project in my graduate OS course involves implementing x86 page table support, and my students have found this helpful for debugging.
Thank you in advance for your time,
Don Porter
PATCH 1/2 monitor: Implement a generic x86 page table iterator
From: Austin Clements <[email protected]>
This iterator provides a way to traverse 32-bit, PAE, and 64-bit page tables by abstracting them as n-ary trees. A struct describes the full range of x86 page table layouts and the iterator builds on this to provide a "successor" function for efficiently traversing the page table tree.
This code is currently unused, but provides the groundwork for the following "info pg" patch. It could also be used to unify and simplify the implementations of "info mem" and "info tlb".
--- target/i386/monitor.c | 149 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 149 insertions(+)
target/i386/monitor.c +149 -0
@@ -34,6 +34,155 @@#include "qapi/qapi-commands-misc-target.h" #include "qapi/qapi-commands-misc.h" +/** +* PTLayout describes the layout of an x86 page table in enough detail +* to fully decode up to a 4-level 64-bit page table tree. +*/ +typedef struct PTLayout { +int levels, entsize; +int entries[4 ];/* Entries in each table level */ +int shift[4 ];/* VA bit shift each each level */ +bool pse[4 ];/* Whether PSE bit is valid */ +const char *names[4 ]; +int vaw, paw;/* VA and PA width in characters */ +}PTLayout ; + +/** +* PTIter provides a generic way to traverse and decode an x86 page +* table tree. +*/ +typedef struct PTIter { +const PTLayout *layout; +bool pse;/* PSE enabled */ +int level;/* Current level */ +int i[4 ];/* Index at each level */ +hwaddr base[4 ];/* Physical base pointer */ + +uint64_t ent;/* Current entry */ +bool present, leaf; +target_ulong va; +hwaddr pa; +target_ulong size; +}PTIter ; + +static bool ptiter_succ (PTIter *it); + +/** +* Initialize a PTIter to point to the first entry of the page table's +* top level. On failure, prints a message to mon and returns false. +*/ +static bool +__attribute__ ((unused)) +ptiter_init (Monitor *mon,PTIter *it) +{ +static const PTLayout l32 = { +2 ,4 , {1024 ,1024 }, {22 ,12 }, {1 ,0 }, {"PDE" ,"PTE" },8 ,8 + }; +static const PTLayout lpae = { +3 ,8 , {4 ,512 ,512 }, {30 ,21 ,12 }, {0 ,1 ,0 }, + {"PDP" ,"PDE" ,"PTE" },8 ,13 + }; +#ifdef TARGET_X86_64 +static const PTLayout l64 = { +4 ,8 , {512 ,512 ,512 ,512 }, {39 ,30 ,21 ,12 }, {0 ,1 ,1 ,0 }, + {"PML4" ,"PDP" ,"PDE" ,"PTE" },12 ,13 + }; +#endif +CPUArchState *env; + + env =mon_get_cpu_env (mon); + +if (!(env->cr[0 ] &CR0_PG_MASK )) { +monitor_printf (mon,"PG disabled\n" ); +return false; + } + +memset (it,0 ,sizeof (*it)); +if (env->cr[4 ] &CR4_PAE_MASK ) { +#ifdef TARGET_X86_64 +if (env->hflags &HF_LMA_MASK ) { + it->layout = &l64; + it->base[0 ] = env->cr[3 ] &0x3fffffffff000ULL ; + } else +#endif + { + it->layout = &lpae; + it->base[0 ] = env->cr[3 ] & ~0x1f ; + } + it->pse = true; + }else { + it->layout = &l32; + it->base[0 ] = env->cr[3 ] & ~0xfff ; + it->pse = (env->cr[4 ] &CR4_PSE_MASK ); + } + +/* Trick ptiter_succ into doing the hard initialization. */ + it->i[0 ] =-1 ; + it->leaf = true; +ptiter_succ (it); +return true; +} + +/** +* Move a PTIter to the successor of the current entry. Specifically: +* if the iterator points to a leaf, move to its next sibling, or to +* the next sibling of a parent if it has no more siblings. If the +* iterator points to a non-leaf, move to its first child. If there +* is no successor, return false. +* +* Note that the resulting entry may not be marked present, though +* non-present entries are always leafs (within a page +* table/directory/etc, this will always visit all entries). +*/ +static bool ptiter_succ (PTIter *it) +{ +int i, l, entsize; +uint64_t ent64; +uint32_t ent32; +bool large; + +if (it->level <0 ) { +return false; + }else if (!it->leaf) { +/* Move to this entry's first child */ + it->level++; + it->base[it->level] = it->pa; + it->i[it->level] =0 ; + }else { +/* Move forward and, if we hit the end of this level, up */ +while (++it->i[it->level] == it->layout->entries[it->level]) { +if (it->level-- ==0 ) { +/* We're out of page table */ +return false; + } + } + } + +/* Read this entry */ + l = it->level; + entsize = it->layout->entsize; +cpu_physical_memory_read (it->base[l] + it->i[l] * entsize, + entsize ==4 ? (void *)&ent32 : (void *)&ent64, + entsize); +if (entsize ==4 ) { + it->ent =le32_to_cpu (ent32); + }else { + it->ent =le64_to_cpu (ent64); + } + +/* Decode the entry */ + large = (it->pse && it->layout->pse[l] && (it->ent &PG_PSE_MASK )); + it->present = it->ent &PG_PRESENT_MASK ; + it->leaf = (large || !it->present || ((l +1 ) == it->layout->levels)); + it->va =0 ; +for (i =0 ; i <= l; i++) { + it->va |= (uint64_t )it->i[i] << it->layout->shift[i]; + } + it->pa = it->ent & (large ?0x3ffffffffc000ULL :0x3fffffffff000ULL ); + it->size =1 << it->layout->shift[l]; +return true; +} +/* Perform linear address sign extension */ static hwaddr addr_canonical (CPUArchState *env,hwaddr addr) {
Signature · 1 line
2.25.1
PATCH 2/2 monitor: Add an "info pg" command that prints the current page tables
In reply to Don PorterFrom: Austin Clements <[email protected]>
The new "info pg" monitor command prints the current page table, including virtual address ranges, flag bits, and snippets of physical page numbers. Completely filled regions of the page table with compatible flags are "folded", with the result that the complete output for a freshly booted x86-64 Linux VM can fit in a single terminal window. The output looks like this:
VPN range Entry Flags Physical page
[7f0000000-7f0000000] PML4[0fe] ---DA--UWP
[7f28c0000-7f28fffff] PDP[0a3] ---DA--UWP
[7f28c4600-7f28c47ff] PDE[023] ---DA--UWP
[7f28c4655-7f28c4656] PTE[055-056] X--D---U-P 0000007f14-0000007f15
[7f28c465b-7f28c465b] PTE[05b] ----A--U-P 0000001cfc
...
[ff8000000-ff8000000] PML4[1ff] ---DA--UWP
[ffff80000-ffffbffff] PDP[1fe] ---DA---WP
[ffff81000-ffff81dff] PDE[008-00e] -GSDA---WP 0000001000-0000001dff
[ffffc0000-fffffffff] PDP[1ff] ---DA--UWP
[ffffff400-ffffff5ff] PDE[1fa] ---DA--UWP
[ffffff5fb-ffffff5fc] PTE[1fb-1fc] XG-DACT-WP 00000fec00 00000fee00
[ffffff600-ffffff7ff] PDE[1fb] ---DA--UWP
[ffffff600-ffffff600] PTE[000] -G-DA--U-P 0000001467--- hmp-commands-info.hx | 15 +++ include/monitor/hmp-target.h | 2 + target/i386/monitor.c | 179 ++++++++++++++++++++++++++++++++++- 3 files changed, 195 insertions(+), 1 deletion(-)
hmp-commands-info.hx +15 -0
@@ -239,6 +239,21 @@ SRST Show the active virtual memory mappings. ERST +#if defined(TARGET_I386) + { + .name = "pg", + .args_type = "", + .params = "", + .help = "show the page table", + .cmd = hmp_info_pg, + }, +#endif + +SRST | + ``info pg`` | + Show the active page table. | +ERST + { .name = "mtree", .args_type = "flatview:-f,dispatch_tree:-d,owner:-o,disabled:-D",
include/monitor/hmp-target.h +2 -0
@@ -48,6 +48,8 @@ void hmp_info_mem(Monitor *mon, const QDict *qdict);void hmp_info_tlb (Monitor *mon,const QDict *qdict);void hmp_mce (Monitor *mon,const QDict *qdict);void hmp_info_local_apic (Monitor *mon,const QDict *qdict); +void hmp_info_io_apic (Monitor *mon,const QDict *qdict); +void hmp_info_pg (Monitor *mon,const QDict *qdict);void hmp_info_sev (Monitor *mon,const QDict *qdict);void hmp_info_sgx (Monitor *mon,const QDict *qdict);void hmp_info_via (Monitor *mon,const QDict *qdict);
target/i386/monitor.c +178 -1
201 lines not on this page · raw message
Signature · 1 line
2.25.1
Quoted 10 lines · Don Porter
I am a CS professor (and, newly, a second-time contributor). I have been using qemu in my courses for over a decade, especially a course that asks students to write major pieces of an OS kernel from starter code.
I have some patches, originally from Austin Clements at MIT, that I have found useful over the years and that may be useful to others. It would also be nice not to have to build a custom qemu each semester. I have cleared upstreaming these with Austin, the original author.
Quoted 4 lines
This patch set adds an 'info pg' command to the monitor, which prints a nicer view of the page tables. A project in my graduate OS course involves implementing x86 page table support, and my students have found this helpful for debugging.
So, my issue with this is that it's x86 specific, and it adds yet another monitor command that is doing "show some kind of debug info related to the guest page tables", along with "info mem" and "info tlb". Plus it is yet another lump of code that's walking the guest page tables and interpreting them.
What I'd really like to see is some infrastructure that is at least somewhat guest-architecture-agnostic, so we can minimise what a guest architecture needs to implement (and then make providing that mandatory).
The other thing I'd like to see is perhaps some investigation of whether there's any way to implement something useful by using/extending the existing get_phys_page_attrs_debug() and similar functions, so that you don't have to write one lot of page-table-walking code for QEMU to use when it's executing guest code and a separate lot (that's bound to get out of sync or not support new functionality/changes) that's only handling these monitor debug commands. There's a lot of complexity in figuring out things like permissions in a modern architecture...
thanks
-- PMM
Quoted 28 lines · Peter Maydell
On Mon, 15 Apr 2024 at 17:09, Don Porter <[email protected]> wrote:
This patch set adds an 'info pg' command to the monitor, which prints a nicer view of the page tables. A project in my graduate OS course involves implementing x86 page table support, and my students have found this helpful for debugging.
So, my issue with this is that it's x86 specific, and it adds yet another monitor command that is doing "show some kind of debug info related to the guest page tables", along with "info mem" and "info tlb". Plus it is yet another lump of code that's walking the guest page tables and interpreting them.What I'd really like to see is some infrastructure that is at least somewhat guest-architecture-agnostic, so we can minimise what a guest architecture needs to implement (and then make providing that mandatory).
The other thing I'd like to see is perhaps some investigation of whether there's any way to implement something useful by using/extending the existing
get_phys_page_attrs_debug()and similar functions, so that you don't have to write one lot of page-table-walking code for QEMU to use when it's executing guest code and a separate lot (that's bound to get out of sync or not support new functionality/changes) that's only handling these monitor debug commands. There's a lot of complexity in figuring out things like permissions in a modern architecture...
Quoted 3 lines
thanks
-- PMM
Thank you for the feedback!
There is still a lot I am learning about the code base, but it seems that qemu_get_guest_memory_mapping() does most of what one would need. It currently only returns the "leaves" of the page table tree in a list.
What if I extend this function with an optional argument to either
1) return the interior nodes of the page table in additional lists (and then parse+print in the monitor code), or
2) inline the monitor printing in the arch-specific hook, and pass a flag to get_guest_memory_mapping() that turns on/off the statements that pretty print the page tables?
It looks like most CPUs implement this function as part of checkpointing.
Thoughts?
Don
Quoted 4 lines · Don Porter
There is still a lot I am learning about the code base, but it seems that
qemu_get_guest_memory_mapping()does most of what one would need. It currently only returns the "leaves" of the page table tree in a list.
Quoted 8 lines
What if I extend this function with an optional argument to either
1) return the interior nodes of the page table in additional lists (and then parse+print in the monitor code), or
2) inline the monitor printing in the arch-specific hook, and pass a flag toget_guest_memory_mapping()that turns on/off the statements that pretty print the page tables?It looks like most CPUs implement this function as part of checkpointing.
As far as I can see only x86 implements the get_memory_mapping function, so once again somebody has added some bit of functionality that does a walk of the page tables that is x86 only and that shares no code with any of the other page table walking code :-(
thanks
-- PMM
Quoted 12 lines · Peter Maydell
Quoted 11 lines · Don Porter
There is still a lot I am learning about the code base, but it seems that
qemu_get_guest_memory_mapping()does most of what one would need. It currently only returns the "leaves" of the page table tree in a list.What if I extend this function with an optional argument to either
1) return the interior nodes of the page table in additional lists (and then parse+print in the monitor code), or
2) inline the monitor printing in the arch-specific hook, and pass a flag toget_guest_memory_mapping()that turns on/off the statements that pretty print the page tables?
Quoted 6 lines
Quoted 1 line
It looks like most CPUs implement this function as part of checkpointing.
As far as I can see only x86 implements the
get_memory_mappingfunction, so once again somebody has added some bit of functionality that does a walk of the page tables that is x86 only and that shares no code with any of the other page table walking code :-(
My mistake - get_memory_mappings() is only implemented in x86.
In doing some searching of the code, many architectures implement mmu_translate() and get_physical_address() functions, but they are not standardized. I also see your larger point about replicating page walking code in x86.
I imagine you have something in mind that abstracts things like the height of the radix tree, entries per node, checking permissions, printing the contents, etc.
Perhaps I should start by trying to merge the x86 page walking code into one set of common helper functions, get more feedback (perhaps on a new patch thread?), and then consider how to abstract across architectures after getting feedback on this?
In looking at x86 code, I see the following places where there is page table walking code to potentially merge:
* target/i386/monitor.c - existing info commands
* target/i386/helper.c - get_phys_page_attrs_debug
* target/i386/arch_memory_mapping.c - implements get_memory_mapping
* tcg/sysemu/excp_helper.c: implements mmu_translate() and get_physical_address()
Thanks,
Don
Quoted 17 lines · Don Porter
Quoted 12 lines · Peter Maydell
Quoted 11 lines · Don Porter
There is still a lot I am learning about the code base, but it seems that
qemu_get_guest_memory_mapping()does most of what one would need. It currently only returns the "leaves" of the page table tree in a list.What if I extend this function with an optional argument to either
1) return the interior nodes of the page table in additional lists (and then parse+print in the monitor code), or
2) inline the monitor printing in the arch-specific hook, and pass a flag toget_guest_memory_mapping()that turns on/off the statements that pretty print the page tables?
Quoted 9 lines
Quoted 6 lines
Quoted 1 line
It looks like most CPUs implement this function as part of checkpointing.
As far as I can see only x86 implements the
get_memory_mappingfunction, so once again somebody has added some bit of functionality that does a walk of the page tables that is x86 only and that shares no code with any of the other page table walking code :-(My mistake -
get_memory_mappings()is only implemented in x86.
Hi Don,
Your email popped up, so I just drop my 2 cents.
If you are only interested in x86, you can just build a gdb script that would parse the page tables and present the data. It might be less efficient, but easier to maintain.
Regards,
Nadav
Quoted 31 lines · Nadav Amit
Quoted 18 lines · Don Porter
Quoted 12 lines · Peter Maydell
Quoted 11 lines · Don Porter
There is still a lot I am learning about the code base, but it seems that
qemu_get_guest_memory_mapping()does most of what one would need. It currently only returns the "leaves" of the page table tree in a list.What if I extend this function with an optional argument to either
1) return the interior nodes of the page table in additional lists (and then parse+print in the monitor code), or
2) inline the monitor printing in the arch-specific hook, and pass a flag toget_guest_memory_mapping()that turns on/off the statements that pretty print the page tables?Quoted 9 lines
Quoted 6 lines
Quoted 1 line
It looks like most CPUs implement this function as part of checkpointing.
As far as I can see only x86 implements the
get_memory_mappingfunction, so once again somebody has added some bit of functionality that does a walk of the page tables that is x86 only and that shares no code with any of the other page table walking code :-(My mistake -
get_memory_mappings()is only implemented in x86.
Quoted 7 lines
Hi Don,
Your email popped up, so I just drop my 2 cents.
If you are only interested in x86, you can just build a gdb script that would parse the page tables and present the data. It might be less efficient, but easier to maintain.
Actually, I think I was wrong. It might not be easily done with GDB, but I did something similar with QMP.
You can have a look at some related code I once wrote for similar purpose:
https://nadav.amit.zone/downloads/pti_test.py
Quoted 42 lines · * Don Porter ([email protected]) wrote:
Quoted 12 lines · Peter Maydell
Quoted 11 lines · Don Porter
There is still a lot I am learning about the code base, but it seems that
qemu_get_guest_memory_mapping()does most of what one would need. It currently only returns the "leaves" of the page table tree in a list.What if I extend this function with an optional argument to either
1) return the interior nodes of the page table in additional lists (and then parse+print in the monitor code), or
2) inline the monitor printing in the arch-specific hook, and pass a flag toget_guest_memory_mapping()that turns on/off the statements that pretty print the page tables?Quoted 6 lines
Quoted 1 line
It looks like most CPUs implement this function as part of checkpointing.
As far as I can see only x86 implements the
get_memory_mappingfunction, so once again somebody has added some bit of functionality that does a walk of the page tables that is x86 only and that shares no code with any of the other page table walking code :-(My mistake -
get_memory_mappings()is only implemented in x86.In doing some searching of the code, many architectures implement
mmu_translate()andget_physical_address()functions, but they are not standardized. I also see your larger point
about replicating page walking code in x86.I imagine you have something in mind that abstracts things like the height of the radix tree,
entries per node, checking permissions, printing the contents, etc.Perhaps I should start by trying to merge the x86 page walking code into one set of common
helper functions, get more feedback (perhaps on a new patch thread?), and then consider
how to abstract across architectures after getting feedback on this?In looking at x86 code, I see the following places where there is page table walking code to
potentially merge:
Quoted 8 lines
*
target/i386/monitor.c- existing info commands*
target/i386/helper.c-get_phys_page_attrs_debug*
target/i386/arch_memory_mapping.c- implementsget_memory_mapping*
tcg/sysemu/excp_helper.c: implementsmmu_translate()andget_physical_address()
One thing to keep in mind (although I don't know the x86 code) is that you want the monitor command not to change any state, nor to fail if there's a particularly screwy page table; so no flagging exceptions or flagging accessed bits or changing the state of the tcg.
Dave
Quoted 4 lines
Thanks,
Don
Signature · 4 lines
-----Open up your eyes, open up your mind, open up your code ------- / Dr. David Alan Gilbert | Running GNU/Linux | Happy \ \ dave @ treblig.org | | In Hex / \ _________________________|_____ http://www.treblig.org |_______/
Quoted 10 lines · Dr. David Alan Gilbert
Quoted 10 lines
In looking at x86 code, I see the following places where there is page table walking code to
potentially merge:*
target/i386/monitor.c- existing info commands*
target/i386/helper.c-get_phys_page_attrs_debug*
target/i386/arch_memory_mapping.c- implementsget_memory_mapping
Quoted 7 lines
Quoted 2 lines
*
tcg/sysemu/excp_helper.c: implementsmmu_translate()andget_physical_address()One thing to keep in mind (although I don't know the x86 code) is that you want the monitor command not to change any state, nor to fail if there's a particularly screwy page table; so no flagging exceptions or flagging accessed bits or changing the state of the tcg.
Indeed, the only one of the 4 that *is* allowed to change state is excp_helper.c.
r~
Quoted 33 lines · Don Porter
Quoted 12 lines · Peter Maydell
Quoted 11 lines · Don Porter
There is still a lot I am learning about the code base, but it seems that
qemu_get_guest_memory_mapping()does most of what one would need. It currently only returns the "leaves" of the page table tree in a list.What if I extend this function with an optional argument to either
1) return the interior nodes of the page table in additional lists (and then parse+print in the monitor code), or
2) inline the monitor printing in the arch-specific hook, and pass a flag toget_guest_memory_mapping()that turns on/off the statements that pretty print the page tables?Quoted 6 lines
Quoted 1 line
It looks like most CPUs implement this function as part of checkpointing.
As far as I can see only x86 implements the
get_memory_mappingfunction, so once again somebody has added some bit of functionality that does a walk of the page tables that is x86 only and that shares no code with any of the other page table walking code :-(My mistake -
get_memory_mappings()is only implemented in x86.In doing some searching of the code, many architectures implement
mmu_translate()andget_physical_address()functions, but they are not standardized. I also see your larger point
about replicating page walking code in x86.I imagine you have something in mind that abstracts things like the height of the radix tree,
entries per node, checking permissions, printing the contents, etc.
Quoted 5 lines
Perhaps I should start by trying to merge the x86 page walking code into one set of common
helper functions, get more feedback (perhaps on a new patch thread?), and then consider
how to abstract across architectures after getting feedback on this?
I think the cross-architecture abstraction is probably the trickiest part. I would actually be happy for us to drop 'info tlb' and 'info mem' entirely if we have a cross-arch command that gives basically the same information -- we don't IMHO need more than one command for this, and we only have multiple commands for basically legacy reasons. And for the human monitor (HMP) we don't need to keep things around for backwards compatibility.
thanks
-- PMM
Quoted 40 lines · * Peter Maydell ([email protected]) wrote:
Quoted 33 lines · Don Porter
Quoted 12 lines · Peter Maydell
Quoted 11 lines · Don Porter
There is still a lot I am learning about the code base, but it seems that
qemu_get_guest_memory_mapping()does most of what one would need. It currently only returns the "leaves" of the page table tree in a list.What if I extend this function with an optional argument to either
1) return the interior nodes of the page table in additional lists (and then parse+print in the monitor code), or
2) inline the monitor printing in the arch-specific hook, and pass a flag toget_guest_memory_mapping()that turns on/off the statements that pretty print the page tables?Quoted 6 lines
Quoted 1 line
It looks like most CPUs implement this function as part of checkpointing.
As far as I can see only x86 implements the
get_memory_mappingfunction, so once again somebody has added some bit of functionality that does a walk of the page tables that is x86 only and that shares no code with any of the other page table walking code :-(My mistake -
get_memory_mappings()is only implemented in x86.In doing some searching of the code, many architectures implement
mmu_translate()andget_physical_address()functions, but they are not standardized. I also see your larger point
about replicating page walking code in x86.I imagine you have something in mind that abstracts things like the height of the radix tree,
entries per node, checking permissions, printing the contents, etc.Quoted 5 lines
Perhaps I should start by trying to merge the x86 page walking code into one set of common
helper functions, get more feedback (perhaps on a new patch thread?), and then consider
how to abstract across architectures after getting feedback on this?
Quoted 8 lines
I think the cross-architecture abstraction is probably the trickiest part. I would actually be happy for us to drop 'info tlb' and 'info mem' entirely if we have a cross-arch command that gives basically the same information -- we don't IMHO need more than one command for this, and we only have multiple commands for basically legacy reasons. And for the human monitor (HMP) we don't need to keep things around for backwards compatibility.
I'm not sure what happens for the (MIPS/SPARC ?) where it's not a traditional table hierarchy.
The other thing you might want (and I'm not sure how it interacts with any of this) is to specify the root of the MMU tree (i.e. CR3 value for those in Intel thinking) to dump different processes etc.
Dave
Quoted 2 lines
thanks
-- PMM
Signature · 4 lines
-----Open up your eyes, open up your mind, open up your code ------- / Dr. David Alan Gilbert | Running GNU/Linux | Happy \ \ dave @ treblig.org | | In Hex / \ _________________________|_____ http://www.treblig.org |_______/
v2 cover Rework x86 page table walks
This version of the 'info pg' command adopts Peter Maydell's request to write some guest-agnostic page table iterator and accessor code, along with architecture-specific hooks. The first patch in this series contributes a generic page table iterator and an x86 instantiation. As a client, we first introduce an 'info pg' monitor command, as well as a compressing callback hook for creating succinct page table representations.
After this, each successive patch replaces an exisitng x86 page table walker with a use of common iterator code.
I could use advice on how to ensure this is sufficiently well tested. I used 'make check' and 'make check-avocado', which both pass; what is the typical standard for testing something like a page table related change?
As far as generality, I have only attempted this on x86, but I expect the design would work for any similar radix-tree style page table.
I am still new enough to the code base that I wasn't certain about where to put the generic code, as well as naming conventions.
Per David Gilbert's suggestion, I was careful to ensure that monitor calls do not perturb TLB state (see the read-only flag in some functions).
I appreciate Nadav's suggestion about other ways to pursue the same
goal: I ended up deciding I would like to try my hand at consolidating the x86 page table code.
Don Porter (6):
Add an "info pg" command that prints the current page tables Convert 'info tlb' to use generic iterator
Convert 'info mem' to use generic iterator
Convert x86_cpu_get_memory_mapping() to use generic iterators Move tcg implementation of x86 get_physical_address into common helper
code.
Convert x86_mmu_translate() to use common code.
hmp-commands-info.hx | 26 + include/monitor/hmp-target.h | 1 + target/i386/arch_memory_mapping.c | 735 +++++++++++++++------- target/i386/cpu.h | 66 ++ target/i386/helper.c | 518 ++++++++++++---- target/i386/monitor.c | 883 +++++++++++++-------------- target/i386/tcg/sysemu/excp_helper.c | 555 +---------------- 7 files changed, 1439 insertions(+), 1345 deletions(-)
--
2.34.1
PATCH v2 1/6 Add an "info pg" command that prints the current page tables
The new "info pg" monitor command prints the current page table, including virtual address ranges, flag bits, and snippets of physical page numbers. Completely filled regions of the page table with compatible flags are "folded", with the result that the complete output for a freshly booted x86-64 Linux VM can fit in a single terminal window. The output looks like this:
VPN range Entry Flags Physical page
[7f0000000-7f0000000] PML4[0fe] ---DA--UWP
[7f28c0000-7f28fffff] PDP[0a3] ---DA--UWP
[7f28c4600-7f28c47ff] PDE[023] ---DA--UWP
[7f28c4655-7f28c4656] PTE[055-056] X--D---U-P 0000007f14-0000007f15
[7f28c465b-7f28c465b] PTE[05b] ----A--U-P 0000001cfc
...
[ff8000000-ff8000000] PML4[1ff] ---DA--UWP
[ffff80000-ffffbffff] PDP[1fe] ---DA---WP
[ffff81000-ffff81dff] PDE[008-00e] -GSDA---WP 0000001000-0000001dff
[ffffc0000-fffffffff] PDP[1ff] ---DA--UWP
[ffffff400-ffffff5ff] PDE[1fa] ---DA--UWP
[ffffff5fb-ffffff5fc] PTE[1fb-1fc] XG-DACT-WP 00000fec00 00000fee00
[ffffff600-ffffff7ff] PDE[1fb] ---DA--UWP
[ffffff600-ffffff600] PTE[000] -G-DA--U-P 0000001467This draws heavy inspiration from Austin Clements' original patch.
This also adds a generic page table walker, which other monitor and execution commands will be migrated to in subsequent patches.
--- hmp-commands-info.hx | 26 ++ include/monitor/hmp-target.h | 1 + target/i386/arch_memory_mapping.c | 486 +++++++++++++++++++++++++++++- target/i386/cpu.h | 16 + target/i386/monitor.c | 380 +++++++++++++++++++++++ 5 files changed, 908 insertions(+), 1 deletion(-)
hmp-commands-info.hx +26 -0
@@ -237,6 +237,32 @@ ERST .cmd = hmp_info_mtree, }, +#if defined(TARGET_I386) + { + .name = "pg", + .args_type = "", + .params = "", + .help = "show the page table", + .cmd = hmp_info_pg, + }, +#endif + +SRST | + ``info pg`` | + Show the active page table. | +ERST + + { + .name = "mtree", + .args_type = "flatview:-f,dispatch_tree:-d,owner:-o,disabled:-D", + .params = "[-f][-d][-o][-D]", + .help = "show memory tree (-f: dump flat view for address spaces;" + "-d: dump dispatch tree, valid with -f only);" + "-o: dump region owners/parents;" + "-D: dump disabled regions", + .cmd = hmp_info_mtree, + }, + SRST ``info mtree`` Show memory tree.
include/monitor/hmp-target.h +1 -0
@@ -50,6 +50,7 @@ CPUState *mon_get_cpu(Monitor *mon);void hmp_info_mem (Monitor *mon,const QDict *qdict);void hmp_info_tlb (Monitor *mon,const QDict *qdict);void hmp_mce (Monitor *mon,const QDict *qdict); +void hmp_info_pg (Monitor *mon,const QDict *qdict);void hmp_info_local_apic (Monitor *mon,const QDict *qdict);void hmp_info_sev (Monitor *mon,const QDict *qdict);void hmp_info_sgx (Monitor *mon,const QDict *qdict);
target/i386/arch_memory_mapping.c +485 -1
495 lines not on this page · raw message
target/i386/cpu.h +16 -0
@@ -2094,6 +2094,12 @@ struct X86CPUClass {ResettablePhases parent_phases; }; +/* Intended to become a generic PTE type */ +typedef union PTE { +uint64_t pte64_t; +uint32_t pte32_t; +}PTE_t ; +#ifndef CONFIG_USER_ONLY extern const VMStateDescription vmstate_x86_cpu;#endif
@@ -2109,6 +2115,16 @@ int x86_cpu_write_elf64_qemunote(WriteCoreDumpFunction f, CPUState *cpu,int x86_cpu_write_elf32_qemunote (WriteCoreDumpFunction f,CPUState *cpu,DumpState *s); +bool mmu_pte_leaf (CPUState *cs,int height,PTE_t *pte); +target_ulong mmu_pte_leaf_page_size (CPUState *cs,int height); +hwaddr mmu_pte_child (CPUState *cs,PTE_t *pte,int height); +int mmu_page_table_entries_per_node (CPUState *cs,int height); +bool for_each_pte (CPUState *cs, +int (*fn)(CPUState *cs,void *data,PTE_t *pte, +target_ulong vaddr,int height,int offset), +void *data,bool visit_interior_nodes, +bool visit_not_present); +bool x86_cpu_get_memory_mapping (CPUState *cpu,MemoryMappingList *list,Error **errp);
target/i386/monitor.c +380 -0
392 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH v2 1/6] Add an "info pg" command that prints the current page tables
Quoted 44 lines · * Don Porter ([email protected]) wrote:
The new "info pg" monitor command prints the current page table, including virtual address ranges, flag bits, and snippets of physical page numbers. Completely filled regions of the page table with compatible flags are "folded", with the result that the complete output for a freshly booted x86-64 Linux VM can fit in a single terminal window. The output looks like this:
VPN range Entry Flags Physical page
[7f0000000-7f0000000] PML4[0fe] ---DA--UWP
[7f28c0000-7f28fffff] PDP[0a3] ---DA--UWP[7f28c4600-7f28c47ff] PDE[023] ---DA--UWP [7f28c4655-7f28c4656] PTE[055-056] X--D---U-P 0000007f14-0000007f15 [7f28c465b-7f28c465b] PTE[05b] ----A--U-P 0000001cfc ... [ff8000000-ff8000000] PML4[1ff] ---DA--UWP [ffff80000-ffffbffff] PDP[1fe] ---DA---WP [ffff81000-ffff81dff] PDE[008-00e] -GSDA---WP 0000001000-0000001dff [ffffc0000-fffffffff] PDP[1ff] ---DA--UWP [ffffff400-ffffff5ff] PDE[1fa] ---DA--UWP [ffffff5fb-ffffff5fc] PTE[1fb-1fc] XG-DACT-WP 00000fec00 00000fee00 [ffffff600-ffffff7ff] PDE[1fb] ---DA--UWP [ffffff600-ffffff600] PTE[000] -G-DA--U-P 0000001467This draws heavy inspiration from Austin Clements' original patch.
This also adds a generic page table walker, which other monitor and execution commands will be migrated to in subsequent patches.
Signed-off-by Don Porter <[email protected]>--- hmp-commands-info.hx | 26 ++ include/monitor/hmp-target.h | 1 + target/i386/arch_memory_mapping.c | 486 +++++++++++++++++++++++++++++- target/i386/cpu.h | 16 + target/i386/monitor.c | 380 +++++++++++++++++++++++ 5 files changed, 908 insertions(+), 1 deletion(-)hmp-commands-info.hx +0
-0@@ -237,6 +237,32 @@ ERST .cmd = hmp_info_mtree, },
hmp-commands-info.hx ERST · line 248
Quoted 10 lines
hmp-commands-info.hx +9
-0+#if defined(TARGET_I386) + { + .name = "pg", + .args_type = "", + .params = "", + .help = "show the page table", + .cmd = hmp_info_pg, + }, +#endif
So that looks OK
hmp-commands-info.hx ERST · line 253
Quoted 5 lines
hmp-commands-info.hx +5
-0+ +SRST | + ``info pg`` | + Show the active page table. | +ERST
Are those |'s over -----> in the actual patch?
hmp-commands-info.hx ERST · line 264
Quoted 10 lines
hmp-commands-info.hx +10
-0+ { + .name = "mtree", + .args_type = "flatview:-f,dispatch_tree:-d,owner:-o,disabled:-D", + .params = "[-f][-d][-o][-D]", + .help = "show memory tree (-f: dump flat view for address spaces;" + "-d: dump dispatch tree, valid with -f only);" + "-o: dump region owners/parents;" + "-D: dump disabled regions", + .cmd = hmp_info_mtree, + },
Hmm this looks like a copy-pasteism ?
Dave
Quoted 951 lines
hmp-commands-info.hx +0
-0SRST ``info mtree`` Show memory tree.include/monitor/hmp-target.h +1
-0@@ -50,6 +50,7 @@ CPUState *mon_get_cpu(Monitor *mon);void hmp_info_mem (Monitor *mon,const QDict *qdict);void hmp_info_tlb (Monitor *mon,const QDict *qdict);void hmp_mce (Monitor *mon,const QDict *qdict); +void hmp_info_pg (Monitor *mon,const QDict *qdict);void hmp_info_local_apic (Monitor *mon,const QDict *qdict);void hmp_info_sev (Monitor *mon,const QDict *qdict);void hmp_info_sgx (Monitor *mon,const QDict *qdict);target/i386/arch_memory_mapping.c +485
-1@@ -15,6 +15,491 @@#include "cpu.h" #include "sysemu/memory_mapping.h" +/** +************** code hook implementations for x86 *********** +*/ + +#define PML4_ADDR_MASK 0xffffffffff000ULL/* selects bits 51:12 */ + +/** +* mmu_page_table_root - Given a CPUState, return the physical address +* of the current page table root, as well as +* write the height of the tree into *height. +* +* @cs - CPU state +* @height - a pointer to an integer, to store the page table tree height +* +* Returns a hardware address on success. Should not fail (i.e., caller is +* responsible to ensure that a page table is actually present). +*/ +static hwaddr mmu_page_table_root (CPUState *cs,int *height) +{ +X86CPU *cpu =X86_CPU (cs); +CPUX86State *env = &cpu->env; +/* +* DEP 5/15/24: Some original page table walking code sets the a20 +* mask as a 32 bit integer and checks it on each level of hte +* page table walk; some only checks it against the final result. +* For 64 bits, I think we need to sign extend in the common case +* it is not set (and returns -1), or we will lose bits. +*/ +int64_t a20_mask; + +assert (cpu_paging_enabled (cs)); + a20_mask =x86_get_a20_mask (env); + +if (env->cr[4 ] &CR4_PAE_MASK ) { +#ifdef TARGET_X86_64 +if (env->hflags &HF_LMA_MASK ) { +if (env->cr[4 ] &CR4_LA57_MASK ) { + *height =5 ; + }else { + *height =4 ; + } +return (env->cr[3 ] &PML4_ADDR_MASK ) & a20_mask; + } else +#endif + { + *height =3 ; +return (env->cr[3 ] & ~0x1f ) & a20_mask; + } + }else { + *height =2 ; +return (env->cr[3 ] & ~0xfff ) & a20_mask; + } +} + + +/** +* mmu_page_table_entries_per_node - Return the number of +* entries in a page table +* node for the CPU at a given +* height. +* +* @cs - CPU state +* @height - height of the page table tree to query, where the leaves +* are 1. +* +* Returns a value greater than zero on success, -1 on error. +*/ +int mmu_page_table_entries_per_node (CPUState *cs,int height) +{ +X86CPU *cpu =X86_CPU (cs); +CPUX86State *env = &cpu->env; +bool pae_enabled = env->cr[4 ] &CR4_PAE_MASK ; + +assert (height <6 ); +assert (height >0 ); + +switch (height) { +#ifdef TARGET_X86_64 +case 5 : +assert (env->cr[4 ] &CR4_LA57_MASK ); +case 4 : +assert (env->hflags &HF_LMA_MASK ); +assert (pae_enabled); +return 512 ; +#endif +case 3 : +assert (pae_enabled); +#ifdef TARGET_X86_64 +if (env->hflags &HF_LMA_MASK ) { +return 512 ; + } else +#endif + { +return 4 ; + } +case 2 : +case 1 : +return pae_enabled ?512 :1024 ; +default : +g_assert_not_reached (); + } +return -1 ; +} + +/** +* mmu_pte_leaf_page_size - Return the page size of a leaf entry, +* given the height and CPU state +* +* @cs - CPU state +* @height - height of the page table tree to query, where the leaves +* are 1. +* +* Returns a value greater than zero on success, -1 on error. +*/ +target_ulong mmu_pte_leaf_page_size (CPUState *cs,int height) +{ +X86CPU *cpu =X86_CPU (cs); +CPUX86State *env = &cpu->env; +bool pae_enabled = env->cr[4 ] &CR4_PAE_MASK ; + +assert (height <6 ); +assert (height >0 ); + +switch (height) { +#ifdef TARGET_X86_64 +case 5 : +assert (pae_enabled); +assert (env->cr[4 ] &CR4_LA57_MASK ); +assert (env->hflags &HF_LMA_MASK ); +return 1ULL <<48 ; +case 4 : +assert (pae_enabled); +assert (env->hflags &HF_LMA_MASK ); +return 1ULL <<39 ; +#endif +case 3 : +assert (pae_enabled); +return 1 <<30 ; +case 2 : +if (pae_enabled) { +return 1 <<21 ; + }else { +return 1 <<22 ; + } +case 1 : +return 4096 ; +default : +g_assert_not_reached (); + } +return -1 ; +} + +/* +* Given a CPU state and height, return the number of bits +* to shift right/left in going from virtual to PTE index +* and vice versa, the number of useful bits. +*/ +static void _mmu_decode_va_parameters (CPUState *cs,int height, +int *shift,int *width) +{ +X86CPU *cpu =X86_CPU (cs); +CPUX86State *env = &cpu->env; +int _shift =0 ; +int _width =0 ; +bool pae_enabled = env->cr[4 ] &CR4_PAE_MASK ; + +switch (height) { +case 5 : + _shift =48 ; + _width =9 ; +break ; +case 4 : + _shift =39 ; + _width =9 ; +break ; +case 3 : + _shift =30 ; + _width =9 ; +break ; +case 2 : +/* 64 bit page tables shift from 30->21 bits here */ +if (pae_enabled) { + _shift =21 ; + _width =9 ; + }else { +/* 32 bit page tables shift from 32->22 bits */ + _shift =22 ; + _width =10 ; + } +break ; +case 1 : + _shift =12 ; +if (pae_enabled) { + _width =9 ; + }else { + _width =10 ; + } + +break ; +default : +g_assert_not_reached (); + } + +if (shift) { + *shift = _shift; + } + +if (width) { + *width = _width; + } +} + +/** +* get_pte - Copy the contents of the page table entry at node[i] into pt_entry. +* Optionally, add the relevant bits to the virtual address in +* vaddr_pte. +* +* @cs - CPU state +* @node - physical address of the current page table node +* @i - index (in page table entries, not bytes) of the page table +* entry, within node +* @height - height of node within the tree (leaves are 1, not 0) +* @pt_entry - Poiter to a PTE_t, stores the contents of the page table entry +* @vaddr_parent - The virtual address bits already translated in walking the +* page table to node. Optional: only used if vaddr_pte is set. +* @vaddr_pte - Optional pointer to a variable storing the virtual address bits +* translated by node[i]. +* @pte_paddr - Pointer to the physical address of the PTE within node. +* Optional parameter. +*/ + +static void +get_pte (CPUState *cs,hwaddr node,int i,int height, +PTE_t *pt_entry,target_ulong vaddr_parent,target_ulong *vaddr_pte, +hwaddr *pte_paddr) + +{ +X86CPU *cpu =X86_CPU (cs); +CPUX86State *env = &cpu->env; +int32_t a20_mask =x86_get_a20_mask (env); +hwaddr pte; + +if (env->hflags &HF_LMA_MASK ) { +/* 64 bit */ +int pte_width =8 ; + pte = (node + (i * pte_width)) & a20_mask; + pt_entry->pte64_t =address_space_ldq (cs->as, pte, +MEMTXATTRS_UNSPECIFIED ,NULL ); + }else { +/* 32 bit */ +int pte_width =4 ; + pte = (node + (i * pte_width)) & a20_mask; + pt_entry->pte32_t =address_space_ldl (cs->as, pte, +MEMTXATTRS_UNSPECIFIED ,NULL ); + } + +if (vaddr_pte) { +int shift =0 ; +_mmu_decode_va_parameters (cs, height, &shift,NULL ); + *vaddr_pte = vaddr_parent | ((i &0x1ffULL ) << shift); + } + +if (pte_paddr) { + *pte_paddr = pte; + } +} + + +static bool +mmu_pte_check_bits (CPUState *cs,PTE_t *pte,int64_t mask) +{ +X86CPU *cpu =X86_CPU (cs); +CPUX86State *env = &cpu->env; +if (env->hflags &HF_LMA_MASK ) { +return pte->pte64_t & mask; + }else { +return pte->pte32_t & mask; + } +} + +/** +* mmu_pte_presetn - Return true if the pte is +* marked 'present' +*/ +static bool +mmu_pte_present (CPUState *cs,PTE_t *pte) +{ +return mmu_pte_check_bits (cs, pte,PG_PRESENT_MASK ); +} + +/** +* mmu_pte_leaf - Return true if the pte is +* a page table leaf, false if +* the pte points to another +* node in the radix tree. +*/ +bool +mmu_pte_leaf (CPUState *cs,int height,PTE_t *pte) +{ +return height ==1 ||mmu_pte_check_bits (cs, pte,PG_PSE_MASK ); +} + +/** +* mmu_pte_child - Returns the physical address +* of a radix tree node pointed to by pte. +* +* @cs - CPU state +* @pte - The page table entry +* @height - The height in the tree of pte +* +* Returns the physical address stored in pte on success, +* -1 on error. +*/ +hwaddr +mmu_pte_child (CPUState *cs,PTE_t *pte,int height) +{ +X86CPU *cpu =X86_CPU (cs); +CPUX86State *env = &cpu->env; +bool pae_enabled = env->cr[4 ] &CR4_PAE_MASK ; +int32_t a20_mask =x86_get_a20_mask (env); + +switch (height) { +#ifdef TARGET_X86_64 +case 5 : +assert (env->cr[4 ] &CR4_LA57_MASK ); +case 4 : +assert (env->hflags &HF_LMA_MASK ); +/* assert(pae_enabled); */ +/* Fall through */ +#endif +case 3 : +assert (pae_enabled); +#ifdef TARGET_X86_64 +if (env->hflags &HF_LMA_MASK ) { +return (pte->pte64_t &PG_ADDRESS_MASK ) & a20_mask; + } else +#endif + { +return (pte->pte64_t & ~0xfff ) & a20_mask; + } +case 2 : +case 1 : +if (pae_enabled) { +return (pte->pte64_t &PG_ADDRESS_MASK ) & a20_mask; + }else { +return (pte->pte32_t & ~0xfff ) & a20_mask; + } +default : +g_assert_not_reached (); + } +return -1 ; +} + + +/** +************** generic page table code *********** +*/ + +/** +* _for_each_pte - recursive helper function +* +* @cs - CPU state +* @fn(cs, data, pte, vaddr, height) - User-provided function to call on each +* pte. +* * @cs - pass through cs +* * @data - user-provided, opaque pointer +* * @pte - current pte +* * @vaddr - virtual address translated by pte +* * @height - height in the tree of pte +* @data - user-provided, opaque pointer, passed to fn() +* @visit_interior_nodes - if true, call fn() on page table entries in +* interior nodes. If false, only call fn() on page +* table entries in leaves. +* @visit_not_present - if true, call fn() on entries that are not present. +* if false, visit only present entries. +* @node - The physical address of the current page table radix tree node +* @vaddr - The virtual address bits translated in walking the page table to +* node +* @height - The height of node in the radix tree +* +* height starts at the max and counts down. +* In a 4 level x86 page table, pml4e is level 4, pdpe is level 3, +* pde is level 2, and pte is level 1 +* +* Returns true on success, false on error. +*/ +static bool +_for_each_pte (CPUState *cs, +int (*fn)(CPUState *cs,void *data,PTE_t *pte, +target_ulong vaddr,int height,int offset), +void *data,bool visit_interior_nodes, +bool visit_not_present,hwaddr node, +target_ulong vaddr,int height) +{ +int ptes_per_node; +int i; + +assert (height >0 ); + + ptes_per_node =mmu_page_table_entries_per_node (cs, height); + +for (i =0 ; i < ptes_per_node; i++) { +PTE_t pt_entry; +target_ulong vaddr_i; +bool pte_present; + +get_pte (cs, node, i, height, &pt_entry, vaddr, &vaddr_i,NULL ); + pte_present =mmu_pte_present (cs, &pt_entry); + +if (pte_present || visit_not_present) { +if ((!pte_present) ||mmu_pte_leaf (cs, height, &pt_entry)) { +if (fn (cs, data, &pt_entry, vaddr_i, height, i)) { +/* Error */ +return false; + } + }else {/* Non-leaf */ +if (visit_interior_nodes) { +if (fn (cs, data, &pt_entry, vaddr_i, height, i)) { +/* Error */ +return false; + } + } +hwaddr child =mmu_pte_child (cs, &pt_entry, height); +assert (height >1 ); +if (!_for_each_pte (cs, fn, data, visit_interior_nodes, + visit_not_present, child, vaddr_i, + height -1 )) { +return false; + } + } + } + } + +return true; +} + +/** +* for_each_pte - iterate over a page table, and +* call fn on each entry +* +* @cs - CPU state +* @fn(cs, data, pte, vaddr, height) - User-provided function to call on each +* pte. +* * @cs - pass through cs +* * @data - user-provided, opaque pointer +* * @pte - current pte +* * @vaddr - virtual address translated by pte +* * @height - height in the tree of pte +* @data - opaque pointer; passed through to fn +* @visit_interior_nodes - if true, call fn() on interior entries in +* page table; if false, visit only leaf entries. +* @visit_not_present - if true, call fn() on entries that are not present. +* if false, visit only present entries. +* +* Returns true on success, false on error. +* +*/ +bool for_each_pte (CPUState *cs, +int (*fn)(CPUState *cs,void *data,PTE_t *pte, +target_ulong vaddr,int height,int offset), +void *data,bool visit_interior_nodes, +bool visit_not_present) +{ +int height; +target_ulong vaddr =0 ; +hwaddr root; + +if (!cpu_paging_enabled (cs)) { +/* paging is disabled */ +return true; + } + + root =mmu_page_table_root (cs, &height); + +assert (height >1 ); + +/* Recursively call a helper to walk the page table */ +return _for_each_pte (cs, fn, data, visit_interior_nodes, visit_not_present, + root, vaddr, height); +} + +/** +* Back to x86 hooks +*/ +/* PAE Paging or IA-32e Paging */ static void walk_pte(MemoryMappingList *list, AddressSpace *as, hwaddr pte_start_addr,@@ -313,4 +798,3 @@ bool x86_cpu_get_memory_mapping(CPUState *cs, MemoryMappingList *list,return true; } -target/i386/cpu.h +16
-0@@ -2094,6 +2094,12 @@ struct X86CPUClass {ResettablePhases parent_phases; }; +/* Intended to become a generic PTE type */ +typedef union PTE { +uint64_t pte64_t; +uint32_t pte32_t; +}PTE_t ; +#ifndef CONFIG_USER_ONLY extern const VMStateDescription vmstate_x86_cpu;#endif @@ -2109,6 +2115,16 @@ int x86_cpu_write_elf64_qemunote(WriteCoreDumpFunction f, CPUState *cpu,int x86_cpu_write_elf32_qemunote (WriteCoreDumpFunction f,CPUState *cpu,DumpState *s); +bool mmu_pte_leaf (CPUState *cs,int height,PTE_t *pte); +target_ulong mmu_pte_leaf_page_size (CPUState *cs,int height); +hwaddr mmu_pte_child (CPUState *cs,PTE_t *pte,int height); +int mmu_page_table_entries_per_node (CPUState *cs,int height); +bool for_each_pte (CPUState *cs, +int (*fn)(CPUState *cs,void *data,PTE_t *pte, +target_ulong vaddr,int height,int offset), +void *data,bool visit_interior_nodes, +bool visit_not_present); +bool x86_cpu_get_memory_mapping (CPUState *cpu,MemoryMappingList *list,Error **errp);target/i386/monitor.c +380
-0@@ -32,6 +32,201 @@#include "qapi/qapi-commands-misc-target.h" #include "qapi/qapi-commands-misc.h" +/* Maximum x86 height */ +#define MAX_HEIGHT 5 + +struct mem_print_state { +Monitor *mon; +CPUArchState *env; +int vaw, paw;/* VA and PA width in characters */ +int max_height; +bool (*flusher)(CPUState *cs,struct mem_print_state *state); +bool flush_interior;/* If false, only call flusher() on leaves */ +bool require_physical_contiguity; +/* +* The height at which we started accumulating ranges, i.e., the +* next height we need to print once we hit the end of a +* contiguous range. +*/ +int start_height; +/* +* For compressing contiguous ranges, track the +* start and end of the range +*/ +hwaddr vstart[MAX_HEIGHT +1 ];/* Starting virt. addr. of open pte range */ +hwaddr vend[MAX_HEIGHT +1 ];/* Ending virtual address of open pte range */ +hwaddr pstart;/* Starting physical address of open pte range */ +hwaddr pend;/* Ending physical address of open pte range */ +int64_t ent[MAX_HEIGHT +1 ];/* PTE contents on current root->leaf path */ +int offset[MAX_HEIGHT +1 ];/* PTE range starting offsets */ +int last_offset[MAX_HEIGHT +1 ];/* PTE range ending offsets */ +}; + +/********************* x86 specific hooks for printing page table stuff ****/ + +const char *names[7 ] = {(char *)NULL ,"PTE" ,"PDE" ,"PDP" ,"PML4" ,"Pml5" , + (char *)NULL }; +static char *pg_bits (hwaddr ent) +{ +static char buf[32 ]; +sprintf (buf,"%c%c%c%c%c%c%c%c%c%c" , + ent &PG_NX_MASK ?'X' :'-' , + ent &PG_GLOBAL_MASK ?'G' :'-' , + ent &PG_PSE_MASK ?'S' :'-' , + ent &PG_DIRTY_MASK ?'D' :'-' , + ent &PG_ACCESSED_MASK ?'A' :'-' , + ent &PG_PCD_MASK ?'C' :'-' , + ent &PG_PWT_MASK ?'T' :'-' , + ent &PG_USER_MASK ?'U' :'-' , + ent &PG_RW_MASK ?'W' :'-' , + ent &PG_PRESENT_MASK ?'P' :'-' ); +return buf; +} + +static bool init_iterator (Monitor *mon,struct mem_print_state *state) +{ +CPUArchState *env; + state->mon = mon; + state->flush_interior = false; + state->require_physical_contiguity = false; + +for (int i =0 ; i <MAX_HEIGHT ; i++) { + state->vstart[i] =-1 ; + state->last_offset[i] =0 ; + } + state->start_height =0 ; + + env =mon_get_cpu_env (mon); +if (!env) { +monitor_printf (mon,"No CPU available\n" ); +return false; + } + state->env = env; + +if (!(env->cr[0 ] &CR0_PG_MASK )) { +monitor_printf (mon,"PG disabled\n" ); +return false; + } + +/* set va and pa width */ +if (env->cr[4 ] &CR4_PAE_MASK ) { + state->paw =13 ; +#ifdef TARGET_X86_64 +if (env->hflags &HF_LMA_MASK ) { +if (env->cr[4 ] &CR4_LA57_MASK ) { + state->vaw =15 ; + state->max_height =5 ; + }else { + state->vaw =12 ; + state->max_height =4 ; + } + } else +#endif + { + state->vaw =8 ; + state->max_height =3 ; + } + }else { + state->max_height =2 ; + state->vaw =8 ; + state->paw =8 ; + } + +return true; +} + +static void pg_print_header (Monitor *mon,struct mem_print_state *state) +{ +/* Header line */ +monitor_printf (mon,"%-*s %-13s %-10s %*s%s\n" , +3 +2 * (state->vaw -3 ),"VPN range" , +"Entry" ,"Flags" , +2 * (state->max_height -1 ),"" ,"Physical page(s)" ); +} + + +static void pg_print (CPUState *cs,Monitor *mon,uint64_t pt_ent, +target_ulong vaddr_s,target_ulong vaddr_l, +hwaddr paddr_s,hwaddr paddr_l, +int offset_s,int offset_l, +int height,int max_height,int vaw,int paw, +bool is_leaf) + +{ +char buf[128 ]; +char *pos = buf, *end = buf +sizeof (buf); +target_ulong size =mmu_pte_leaf_page_size (cs, height); + +/* VFN range */ + pos +=sprintf (pos,"%*s[%0*" PRIx64"-%0*" PRIx64"] " , + (max_height - height) *2 ,"" , + vaw -3 , (uint64_t )vaddr_s >>12 , + vaw -3 , ((uint64_t )vaddr_l + size -1 ) >>12 ); + +/* Slot */ +if (vaddr_s == vaddr_l) { + pos +=sprintf (pos,"%4s[%03x] " , + names[height], offset_s); + }else { + pos +=sprintf (pos,"%4s[%03x-%03x]" , + names[height], offset_s, offset_l); + } + +/* Flags */ + pos +=sprintf (pos," %s" ,pg_bits (pt_ent)); + + +/* Range-compressed PFN's */ +if (is_leaf) { +if (vaddr_s == vaddr_l) { + pos +=snprintf (pos, end - pos," %0*" PRIx64, + paw -3 , (uint64_t )paddr_s >>12 ); + }else { + pos +=snprintf (pos, end - pos," %0*" PRIx64"-%0*" PRIx64, + paw -3 , (uint64_t )paddr_s >>12 , + paw -3 , (uint64_t )paddr_l >>12 ); + } + pos =MIN (pos, end); + } + +/* Trim line to fit screen */ +if (pos - buf >79 ) { +strcpy (buf +77 ,".." ); + } + +monitor_printf (mon,"%s\n" , buf); +} + +static inline +int ent2prot (uint64_t prot) +{ +return prot & (PG_USER_MASK |PG_RW_MASK | +PG_PRESENT_MASK ); +} + +/* Returns true if it emitted anything */ +static +bool flush_print_pg_state (CPUState *cs,struct mem_print_state *state) +{ +bool ret = false; +for (int i = state->start_height; i >0 ; i--) { +if (state->vstart[i] ==-1 ) { +break ; + } +PTE_t my_pte; + my_pte.pte64_t = state->ent[i]; + ret = true; +pg_print (cs, state->mon, state->ent[i], + state->vstart[i], state->vend[i], + state->pstart, state->pend, + state->offset[i], state->last_offset[i], + i, state->max_height, state->vaw, state->paw, +mmu_pte_leaf (cs, i, &my_pte)); + } + +return ret; +} +/* Perform linear address sign extension */ static hwaddr addr_canonical (CPUArchState *env,hwaddr addr) {@@ -49,6 +244,191 @@ static hwaddr addr_canonical(CPUArchState *env, hwaddr addr)return addr; } + + +/*************************** Start generic page table monitor code *********/ + +/* Assume only called on present entries */ +static +int compressing_iterator (CPUState *cs,void *data,PTE_t *pte, +target_ulong vaddr,int height,int offset) +{ +struct mem_print_state *state = (struct mem_print_state *) data; +hwaddr paddr =mmu_pte_child (cs, pte, height); +target_ulong size =mmu_pte_leaf_page_size (cs, height); +bool start_new_run = false, flush = false; +bool is_leaf =mmu_pte_leaf (cs, height, pte); + +int entries_per_node =mmu_page_table_entries_per_node (cs, height); + +/* Prot of current pte */ +int prot =ent2prot (pte->pte64_t); + + +/* If there is a prior run, first try to extend it. */ +if (state->start_height !=0 ) { + +/* +* If we aren't flushing interior nodes, raise the start height. +* We don't need to detect non-compressible interior nodes. +*/ +if ((!state->flush_interior) && state->start_height < height) { + state->start_height = height; + state->vstart[height] = vaddr; + state->vend[height] = vaddr; + state->ent[height] = pte->pte64_t; +if (offset ==0 ) { + state->last_offset[height] = entries_per_node -1 ; + }else { + state->last_offset[height] = offset -1 ; + } + } + +/* Detect when we are walking down the "left edge" of a range */ +if (state->vstart[height] ==-1 + && (height +1 ) <= state->start_height + && state->vstart[height +1 ] == vaddr) { + + state->vstart[height] = vaddr; + state->vend[height] = vaddr; + state->ent[height] = pte->pte64_t; + state->offset[height] = offset; + state->last_offset[height] = offset; + +if (is_leaf) { + state->pstart = paddr; + state->pend = paddr; + } + +/* Detect contiguous entries at same level */ + }else if ((state->vstart[height] !=-1 ) + && (state->start_height >= height) + &&ent2prot (state->ent[height]) == prot + && (((state->last_offset[height] +1 ) % entries_per_node) + == offset) + && ((!is_leaf) + || (!state->require_physical_contiguity) + || (state->pend + size == paddr))) { + + +/* +* If there are entries at the levels below, make sure we +* completed them. We only compress interior nodes +* without holes in the mappings. +*/ +if (height !=1 ) { +for (int i = height -1 ; i >=1 ; i--) { +int entries =mmu_page_table_entries_per_node (cs, i); + +/* Stop if we hit large pages before level 1 */ +if (state->vstart[i] ==-1 ) { +break ; + } + +if ((state->last_offset[i] +1 ) != entries) { + flush = true; + start_new_run = true; +break ; + } + } + } + + +if (!flush) { + +/* We can compress these entries */ + state->ent[height] = pte->pte64_t; + state->vend[height] = vaddr; + state->last_offset[height] = offset; + +/* Only update the physical range on leaves */ +if (is_leaf) { + state->pend = paddr; + } + } +/* Let PTEs accumulate... */ + }else { + flush = true; + } + +if (flush) { +/* +* We hit dicontiguous permissions or pages. +* Print the old entries, then start accumulating again +* +* Some clients only want the flusher called on a leaf. +* Check that too. +* +* We can infer whether the accumulated range includes a +* leaf based on whether pstart is -1. +*/ +if (state->flush_interior || (state->pstart !=-1 )) { +if (state->flusher (cs, state)) { + start_new_run = true; + } + }else { + start_new_run = true; + } + } + }else { + start_new_run = true; + } + +if (start_new_run) { +/* start a new run with this PTE */ +for (int i = state->start_height; i >0 ; i--) { +if (state->vstart[i] !=-1 ) { + state->ent[i] =0 ; + state->last_offset[i] =0 ; + state->vstart[i] =-1 ; + } + } + state->pstart =-1 ; + state->vstart[height] = vaddr; + state->vend[height] = vaddr; + state->ent[height] = pte->pte64_t; + state->offset[height] = offset; + state->last_offset[height] = offset; +if (is_leaf) { + state->pstart = paddr; + state->pend = paddr; + } + state->start_height = height; + } + +return 0 ; +} + + +void hmp_info_pg (Monitor *mon,const QDict *qdict) +{ +struct mem_print_state state; + +CPUState *cs =mon_get_cpu (mon); +if (!cs) { +monitor_printf (mon,"Unable to get CPUState. Internal error\n" ); +return ; + } + +if (!init_iterator (mon, &state)) { +return ; + } + state.flush_interior = true; + state.require_physical_contiguity = true; + state.flusher = &flush_print_pg_state; + +pg_print_header (mon, &state); + +/* +* We must visit interior entries to get the hierarchy, but +* can skip not present mappings +*/ +for_each_pte (cs, &compressing_iterator, &state, true, false); + +/* Print last entry, if one present */ +flush_print_pg_state (cs, &state); +} +static void print_pte (Monitor *mon,CPUArchState *env,hwaddr addr,hwaddr pte,hwaddr mask) {Signature · 1 line
2.34.1
Signature · 4 lines
-----Open up your eyes, open up your mind, open up your code ------- / Dr. David Alan Gilbert | Running GNU/Linux | Happy \ \ dave @ treblig.org | | In Hex / \ _________________________|_____ http://www.treblig.org |_______/
Re: [PATCH v2 1/6] Add an "info pg" command that prints the current page tables
Quoted 46 lines · Dr. David Alan Gilbert
Quoted 45 lines · * Don Porter ([email protected]) wrote:
The new "info pg" monitor command prints the current page table, including virtual address ranges, flag bits, and snippets of physical page numbers. Completely filled regions of the page table with compatible flags are "folded", with the result that the complete output for a freshly booted x86-64 Linux VM can fit in a single terminal window. The output looks like this:
VPN range Entry Flags Physical page
[7f0000000-7f0000000] PML4[0fe] ---DA--UWP
[7f28c0000-7f28fffff] PDP[0a3] ---DA--UWP[7f28c4600-7f28c47ff] PDE[023] ---DA--UWP [7f28c4655-7f28c4656] PTE[055-056] X--D---U-P 0000007f14-0000007f15 [7f28c465b-7f28c465b] PTE[05b] ----A--U-P 0000001cfc ... [ff8000000-ff8000000] PML4[1ff] ---DA--UWP [ffff80000-ffffbffff] PDP[1fe] ---DA---WP [ffff81000-ffff81dff] PDE[008-00e] -GSDA---WP 0000001000-0000001dff [ffffc0000-fffffffff] PDP[1ff] ---DA--UWP [ffffff400-ffffff5ff] PDE[1fa] ---DA--UWP [ffffff5fb-ffffff5fc] PTE[1fb-1fc] XG-DACT-WP 00000fec00 00000fee00 [ffffff600-ffffff7ff] PDE[1fb] ---DA--UWP [ffffff600-ffffff600] PTE[000] -G-DA--U-P 0000001467This draws heavy inspiration from Austin Clements' original patch.
This also adds a generic page table walker, which other monitor and execution commands will be migrated to in subsequent patches.
Signed-off-by Don Porter <[email protected]>--- hmp-commands-info.hx | 26 ++ include/monitor/hmp-target.h | 1 + target/i386/arch_memory_mapping.c | 486 +++++++++++++++++++++++++++++- target/i386/cpu.h | 16 + target/i386/monitor.c | 380 +++++++++++++++++++++++ 5 files changed, 908 insertions(+), 1 deletion(-)hmp-commands-info.hx +0
-0@@ -237,6 +237,32 @@ ERST .cmd = hmp_info_mtree, },
Quoted 1 line
hmp-commands-info.hx ERST · line 240
Quoted 1 line
hmp-commands-info.hx +1
-0+#if defined(TARGET_I386)
FYI in order to unify all QEMU system binaries as a single one, we are trying to remove target-specific bits in monitor.
How 'info pg' should work in a binary supporting heterogeneous emulation?
Quoted 10 lines
hmp-commands-info.hx ERST · line 248
Quoted 8 lines
hmp-commands-info.hx +8
-0+ { + .name = "pg", + .args_type = "", + .params = "", + .help = "show the page table", + .cmd = hmp_info_pg, + }, +#endifSo that looks OK
Re: [PATCH v2 1/6] Add an "info pg" command that prints the current page tables
Quoted 48 lines · Philippe Mathieu-Daudé
Quoted 46 lines · Dr. David Alan Gilbert
Quoted 45 lines · * Don Porter ([email protected]) wrote:
The new "info pg" monitor command prints the current page table, including virtual address ranges, flag bits, and snippets of physical page numbers. Completely filled regions of the page table with compatible flags are "folded", with the result that the complete output for a freshly booted x86-64 Linux VM can fit in a single terminal window. The output looks like this:
VPN range Entry Flags Physical page
[7f0000000-7f0000000] PML4[0fe] ---DA--UWP
[7f28c0000-7f28fffff] PDP[0a3] ---DA--UWP[7f28c4600-7f28c47ff] PDE[023] ---DA--UWP [7f28c4655-7f28c4656] PTE[055-056] X--D---U-P 0000007f14-0000007f15 [7f28c465b-7f28c465b] PTE[05b] ----A--U-P 0000001cfc ... [ff8000000-ff8000000] PML4[1ff] ---DA--UWP [ffff80000-ffffbffff] PDP[1fe] ---DA---WP [ffff81000-ffff81dff] PDE[008-00e] -GSDA---WP 0000001000-0000001dff [ffffc0000-fffffffff] PDP[1ff] ---DA--UWP [ffffff400-ffffff5ff] PDE[1fa] ---DA--UWP [ffffff5fb-ffffff5fc] PTE[1fb-1fc] XG-DACT-WP 00000fec00 00000fee00 [ffffff600-ffffff7ff] PDE[1fb] ---DA--UWP [ffffff600-ffffff600] PTE[000] -G-DA--U-P 0000001467This draws heavy inspiration from Austin Clements' original patch.
This also adds a generic page table walker, which other monitor and execution commands will be migrated to in subsequent patches.
Signed-off-by Don Porter <[email protected]>--- hmp-commands-info.hx | 26 ++ include/monitor/hmp-target.h | 1 + target/i386/arch_memory_mapping.c | 486 +++++++++++++++++++++++++++++- target/i386/cpu.h | 16 + target/i386/monitor.c | 380 +++++++++++++++++++++++ 5 files changed, 908 insertions(+), 1 deletion(-)hmp-commands-info.hx +0
-0@@ -237,6 +237,32 @@ ERST .cmd = hmp_info_mtree, },
Quoted 7 lines
Quoted 1 line
hmp-commands-info.hx ERST · line 240
Quoted 1 line
hmp-commands-info.hx +1
-0+#if defined(TARGET_I386)FYI in order to unify all QEMU system binaries as a single one, we are trying to remove target-specific bits in monitor.
How 'info pg' should work in a binary supporting heterogeneous emulation?
Like the existing "print page table" monitor commands, this will need to change to be an indirect via a CPU method, rather than a command directly implemented as a command in the target/i386 code.
-- PMM
Re: [PATCH v2 1/6] Add an "info pg" command that prints the current page tables
In reply to Philippe Mathieu-DaudéQuoted 52 lines · Philippe Mathieu-Daudé
Quoted 39 lines · Dr. David Alan Gilbert
Quoted 38 lines · * Don Porter ([email protected]) wrote:
The new "info pg" monitor command prints the current page table, including virtual address ranges, flag bits, and snippets of physical page numbers. Completely filled regions of the page table with compatible flags are "folded", with the result that the complete output for a freshly booted x86-64 Linux VM can fit in a single terminal window. The output looks like this:
VPN range Entry Flags Physical page [7f0000000-7f0000000] PML4[0fe] ---DA--UWP [7f28c0000-7f28fffff] PDP[0a3] ---DA--UWP [7f28c4600-7f28c47ff] PDE[023] ---DA--UWP [7f28c4655-7f28c4656] PTE[055-056] X--D---U-P 0000007f14-0000007f15 [7f28c465b-7f28c465b] PTE[05b] ----A--U-P
0000001cfc
...
[ff8000000-ff8000000] PML4[1ff] ---DA--UWP [ffff80000-ffffbffff] PDP[1fe] ---DA---WP [ffff81000-ffff81dff] PDE[008-00e] -GSDA---WP 0000001000-0000001dff [ffffc0000-fffffffff] PDP[1ff] ---DA--UWP [ffffff400-ffffff5ff] PDE[1fa] ---DA--UWP [ffffff5fb-ffffff5fc] PTE[1fb-1fc] XG-DACT-WP00000fec0000000fee00[ffffff600-ffffff7ff] PDE[1fb] ---DA--UWP [ffffff600-ffffff600] PTE[000] -G-DA--U-P 0000001467This draws heavy inspiration from Austin Clements' original patch.
This also adds a generic page table walker, which other monitor and execution commands will be migrated to in subsequent patches.
Signed-off-by Don Porter <[email protected]>---
hmp-commands-info.hx | 26 ++ include/monitor/hmp-target.h | 1 +target/i386/arch_memory_mapping.c| 486
+++++++++++++++++++++++++++++-
target/i386/cpu.h | 16 + target/i386/monitor.c | 380 +++++++++++++++++++++++5 files changed, 908 insertions(+), 1 deletion(-)Quoted 8 lines
hmp-commands-info.hx ERST · line 239
Quoted 8 lines
hmp-commands-info.hx +0
-0@@ -237,6 +237,32 @@ ERST .cmd = hmp_info_mtree, }, +#if defined(TARGET_I386)FYI in order to unify all QEMU system binaries as a single one, we are trying to remove target-specific bits in monitor.
Quoted 2 lines
How 'info pg' should work in a binary supporting heterogeneous emulation?
In the discussion of v1 of this patch, it was suggested that we rework this mmu related code into generic hooks that need to be implemented on each ISA. The hooks need to be migrated, but in the monitor.c code there are several functions (pg_print, pg_print_header, etc), and in arch_memory_mapping.c (mmu_page_table_root, mmu_page_table_entries_per_node, etc) that each ISA would need to implement to interpret its particular page table structures.
I suppose one can also push the check for whether a given ISA supports the command down one level in the code, and call a wrapper that may do nothing on some ISAs.
Does that answer the question?
Thanks,
Don
Re: [PATCH v2 1/6] Add an "info pg" command that prints the current page tables
In reply to Dr. David Alan GilbertQuoted 18 lines · Dr. David Alan Gilbert
hmp-commands-info.hx ERST · line 253
Quoted 5 lines · * Don Porter ([email protected]) wrote:
hmp-commands-info.hx +5
-0+ +SRST | + ``info pg`` | + Show the active page table. | +ERSTAre those |'s over -----> in the actual patch?
hmp-commands-info.hx ERST · line 264
Quoted 10 lines
hmp-commands-info.hx +10
-0+ { + .name = "mtree", + .args_type = "flatview:-f,dispatch_tree:-d,owner:-o,disabled:-D", + .params = "[-f][-d][-o][-D]", + .help = "show memory tree (-f: dump flat view for address spaces;" + "-d: dump dispatch tree, valid with -f only);" + "-o: dump region owners/parents;" + "-D: dump disabled regions", + .cmd = hmp_info_mtree, + },
Quoted 1 line
Hmm this looks like a copy-pasteism ?
Yep, my bad. Sorry about that - will be addressed in v3.
PATCH v2 2/6 Convert 'info tlb' to use generic iterator
In reply to Don Porter--- target/i386/monitor.c | 203 ++++++------------------------------------ 1 file changed, 28 insertions(+), 175 deletions(-)
target/i386/monitor.c +28 -175
@@ -430,201 +430,54 @@ void hmp_info_pg(Monitor *mon, const QDict *qdict) }static void print_pte (Monitor *mon,CPUArchState *env,hwaddr addr, -hwaddr pte,hwaddr mask) +hwaddr pte) { - addr =addr_canonical (env, addr); - -monitor_printf (mon, HWADDR_FMT_plx": " HWADDR_FMT_plx -" %c%c%c%c%c%c%c%c%c\n" , - addr, - pte & mask, - pte &PG_NX_MASK ?'X' :'-' , - pte &PG_GLOBAL_MASK ?'G' :'-' , - pte &PG_PSE_MASK ?'P' :'-' , - pte &PG_DIRTY_MASK ?'D' :'-' , - pte &PG_ACCESSED_MASK ?'A' :'-' , - pte &PG_PCD_MASK ?'C' :'-' , - pte &PG_PWT_MASK ?'T' :'-' , - pte &PG_USER_MASK ?'U' :'-' , - pte &PG_RW_MASK ?'W' :'-' ); -} +char buf[128 ]; +char *pos = buf; -static void tlb_info_32 (Monitor *mon,CPUArchState *env) -{ -unsigned int l1, l2; -uint32_t pgd, pde, pte; + addr =addr_canonical (env, addr); - pgd = env->cr[3 ] & ~0xfff ; -for (l1 =0 ; l1 <1024 ; l1++) { -cpu_physical_memory_read (pgd + l1 *4 , &pde,4 ); - pde =le32_to_cpu (pde); -if (pde &PG_PRESENT_MASK ) { -if ((pde &PG_PSE_MASK ) && (env->cr[4 ] &CR4_PSE_MASK )) { -/* 4M pages */ -print_pte (mon, env, (l1 <<22 ), pde, ~((1 <<21 ) -1 )); - }else { -for (l2 =0 ; l2 <1024 ; l2++) { -cpu_physical_memory_read ((pde & ~0xfff ) + l2 *4 , &pte,4 ); - pte =le32_to_cpu (pte); -if (pte &PG_PRESENT_MASK ) { -print_pte (mon, env, (l1 <<22 ) + (l2 <<12 ), - pte & ~PG_PSE_MASK , - ~0xfff ); - } - } - } - } - } -} + pos +=sprintf (pos, HWADDR_FMT_plx": " HWADDR_FMT_plx" " , addr, + (hwaddr) (pte &PG_ADDRESS_MASK )); -static void tlb_info_pae32 (Monitor *mon,CPUArchState *env) -{ -unsigned int l1, l2, l3; -uint64_t pdpe, pde, pte; -uint64_t pdp_addr, pd_addr, pt_addr; + pos +=sprintf (pos," %s" ,pg_bits (pte)); - pdp_addr = env->cr[3 ] & ~0x1f ; -for (l1 =0 ; l1 <4 ; l1++) { -cpu_physical_memory_read (pdp_addr + l1 *8 , &pdpe,8 ); - pdpe =le64_to_cpu (pdpe); -if (pdpe &PG_PRESENT_MASK ) { - pd_addr = pdpe &0x3fffffffff000ULL ; -for (l2 =0 ; l2 <512 ; l2++) { -cpu_physical_memory_read (pd_addr + l2 *8 , &pde,8 ); - pde =le64_to_cpu (pde); -if (pde &PG_PRESENT_MASK ) { -if (pde &PG_PSE_MASK ) { -/* 2M pages with PAE, CR4.PSE is ignored */ -print_pte (mon, env, (l1 <<30 ) + (l2 <<21 ), pde, - ~((hwaddr)(1 <<20 ) -1 )); - }else { - pt_addr = pde &0x3fffffffff000ULL ; -for (l3 =0 ; l3 <512 ; l3++) { -cpu_physical_memory_read (pt_addr + l3 *8 , &pte,8 ); - pte =le64_to_cpu (pte); -if (pte &PG_PRESENT_MASK ) { -print_pte (mon, env, (l1 <<30 ) + (l2 <<21 ) - + (l3 <<12 ), - pte & ~PG_PSE_MASK , - ~(hwaddr )0xfff ); - } - } - } - } - } - } +/* Trim line to fit screen */ +if (pos - buf >79 ) { +strcpy (buf +77 ,".." ); } -} -#ifdef TARGET_X86_64 -static void tlb_info_la48 (Monitor *mon,CPUArchState *env, -uint64_t l0,uint64_t pml4_addr) -{ -uint64_t l1, l2, l3, l4; -uint64_t pml4e, pdpe, pde, pte; -uint64_t pdp_addr, pd_addr, pt_addr; - -for (l1 =0 ; l1 <512 ; l1++) { -cpu_physical_memory_read (pml4_addr + l1 *8 , &pml4e,8 ); - pml4e =le64_to_cpu (pml4e); -if (!(pml4e &PG_PRESENT_MASK )) { -continue ; - } - - pdp_addr = pml4e &0x3fffffffff000ULL ; -for (l2 =0 ; l2 <512 ; l2++) { -cpu_physical_memory_read (pdp_addr + l2 *8 , &pdpe,8 ); - pdpe =le64_to_cpu (pdpe); -if (!(pdpe &PG_PRESENT_MASK )) { -continue ; - } - -if (pdpe &PG_PSE_MASK ) { -/* 1G pages, CR4.PSE is ignored */ -print_pte (mon, env, (l0 <<48 ) + (l1 <<39 ) + (l2 <<30 ), - pdpe,0x3ffffc0000000ULL ); -continue ; - } - - pd_addr = pdpe &0x3fffffffff000ULL ; -for (l3 =0 ; l3 <512 ; l3++) { -cpu_physical_memory_read (pd_addr + l3 *8 , &pde,8 ); - pde =le64_to_cpu (pde); -if (!(pde &PG_PRESENT_MASK )) { -continue ; - } - -if (pde &PG_PSE_MASK ) { -/* 2M pages, CR4.PSE is ignored */ -print_pte (mon, env, (l0 <<48 ) + (l1 <<39 ) + (l2 <<30 ) + - (l3 <<21 ), pde,0x3ffffffe00000ULL ); -continue ; - } - - pt_addr = pde &0x3fffffffff000ULL ; -for (l4 =0 ; l4 <512 ; l4++) { -cpu_physical_memory_read (pt_addr - + l4 *8 , - &pte,8 ); - pte =le64_to_cpu (pte); -if (pte &PG_PRESENT_MASK ) { -print_pte (mon, env, (l0 <<48 ) + (l1 <<39 ) + - (l2 <<30 ) + (l3 <<21 ) + (l4 <<12 ), - pte & ~PG_PSE_MASK ,0x3fffffffff000ULL ); - } - } - } - } - } +monitor_printf (mon,"%s\n" , buf); } -static void tlb_info_la57 (Monitor *mon,CPUArchState *env) +static +int mem_print_tlb (CPUState *cs,void *data,PTE_t *pte, +target_ulong vaddr,int height,int offset) { -uint64_t l0; -uint64_t pml5e; -uint64_t pml5_addr; - - pml5_addr = env->cr[3 ] &0x3fffffffff000ULL ; -for (l0 =0 ; l0 <512 ; l0++) { -cpu_physical_memory_read (pml5_addr + l0 *8 , &pml5e,8 ); - pml5e =le64_to_cpu (pml5e); -if (pml5e &PG_PRESENT_MASK ) { -tlb_info_la48 (mon, env, l0, pml5e &0x3fffffffff000ULL ); - } - } +struct mem_print_state *state = (struct mem_print_state *) data; +print_pte (state->mon, state->env, vaddr, pte->pte64_t); +return 0 ; } -#endif /* TARGET_X86_64 */ void hmp_info_tlb (Monitor *mon,const QDict *qdict) { -CPUArchState *env; +struct mem_print_state state; - env =mon_get_cpu_env (mon); -if (!env) { -monitor_printf (mon,"No CPU available\n" ); +CPUState *cs =mon_get_cpu (mon); +if (!cs) { +monitor_printf (mon,"Unable to get CPUState. Internal error\n" );return ; } -if (!(env->cr[0 ] &CR0_PG_MASK )) { -monitor_printf (mon,"PG disabled\n" ); +if (!init_iterator (mon, &state)) {return ; } -if (env->cr[4 ] &CR4_PAE_MASK ) { -#ifdef TARGET_X86_64 -if (env->hflags &HF_LMA_MASK ) { -if (env->cr[4 ] &CR4_LA57_MASK ) { -tlb_info_la57 (mon, env); - }else { -tlb_info_la48 (mon, env,0 , env->cr[3 ] &0x3fffffffff000ULL ); - } - } else -#endif - { -tlb_info_pae32 (mon, env); - } - }else { -tlb_info_32 (mon, env); - } + +/** +* 'info tlb' visits only leaf PTEs marked present. +* It does not check other protection bits. +*/ +for_each_pte (cs, &mem_print_tlb, &state, false, false); } static void mem_print(Monitor *mon, CPUArchState *env,
Signature · 1 line
2.34.1
Re: [PATCH v2 2/6] Convert 'info tlb' to use generic iterator
Quoted 1 line · * Don Porter ([email protected]) wrote:
Signed-off-by Don Porter <[email protected]>
If this changes the output of 'info tlb' could you add a before/after to the commit message please.
Also, have a look at glib's g_printf and friends, you might find they're easier;
https://www.manpagez.com/html/glib/glib-2.52.3/glib-String-Utility-Functions.php#g-printf
Dave
Quoted 241 lines
--- target/i386/monitor.c | 203 ++++++------------------------------------ 1 file changed, 28 insertions(+), 175 deletions(-)target/i386/monitor.c +28
-175@@ -430,201 +430,54 @@ void hmp_info_pg(Monitor *mon, const QDict *qdict) }static void print_pte (Monitor *mon,CPUArchState *env,hwaddr addr, -hwaddr pte,hwaddr mask) +hwaddr pte) { - addr =addr_canonical (env, addr); - -monitor_printf (mon, HWADDR_FMT_plx": " HWADDR_FMT_plx -" %c%c%c%c%c%c%c%c%c\n" , - addr, - pte & mask, - pte &PG_NX_MASK ?'X' :'-' , - pte &PG_GLOBAL_MASK ?'G' :'-' , - pte &PG_PSE_MASK ?'P' :'-' , - pte &PG_DIRTY_MASK ?'D' :'-' , - pte &PG_ACCESSED_MASK ?'A' :'-' , - pte &PG_PCD_MASK ?'C' :'-' , - pte &PG_PWT_MASK ?'T' :'-' , - pte &PG_USER_MASK ?'U' :'-' , - pte &PG_RW_MASK ?'W' :'-' ); -} +char buf[128 ]; +char *pos = buf; -static void tlb_info_32 (Monitor *mon,CPUArchState *env) -{ -unsigned int l1, l2; -uint32_t pgd, pde, pte; + addr =addr_canonical (env, addr); - pgd = env->cr[3 ] & ~0xfff ; -for (l1 =0 ; l1 <1024 ; l1++) { -cpu_physical_memory_read (pgd + l1 *4 , &pde,4 ); - pde =le32_to_cpu (pde); -if (pde &PG_PRESENT_MASK ) { -if ((pde &PG_PSE_MASK ) && (env->cr[4 ] &CR4_PSE_MASK )) { -/* 4M pages */ -print_pte (mon, env, (l1 <<22 ), pde, ~((1 <<21 ) -1 )); - }else { -for (l2 =0 ; l2 <1024 ; l2++) { -cpu_physical_memory_read ((pde & ~0xfff ) + l2 *4 , &pte,4 ); - pte =le32_to_cpu (pte); -if (pte &PG_PRESENT_MASK ) { -print_pte (mon, env, (l1 <<22 ) + (l2 <<12 ), - pte & ~PG_PSE_MASK , - ~0xfff ); - } - } - } - } - } -} + pos +=sprintf (pos, HWADDR_FMT_plx": " HWADDR_FMT_plx" " , addr, + (hwaddr) (pte &PG_ADDRESS_MASK )); -static void tlb_info_pae32 (Monitor *mon,CPUArchState *env) -{ -unsigned int l1, l2, l3; -uint64_t pdpe, pde, pte; -uint64_t pdp_addr, pd_addr, pt_addr; + pos +=sprintf (pos," %s" ,pg_bits (pte)); - pdp_addr = env->cr[3 ] & ~0x1f ; -for (l1 =0 ; l1 <4 ; l1++) { -cpu_physical_memory_read (pdp_addr + l1 *8 , &pdpe,8 ); - pdpe =le64_to_cpu (pdpe); -if (pdpe &PG_PRESENT_MASK ) { - pd_addr = pdpe &0x3fffffffff000ULL ; -for (l2 =0 ; l2 <512 ; l2++) { -cpu_physical_memory_read (pd_addr + l2 *8 , &pde,8 ); - pde =le64_to_cpu (pde); -if (pde &PG_PRESENT_MASK ) { -if (pde &PG_PSE_MASK ) { -/* 2M pages with PAE, CR4.PSE is ignored */ -print_pte (mon, env, (l1 <<30 ) + (l2 <<21 ), pde, - ~((hwaddr)(1 <<20 ) -1 )); - }else { - pt_addr = pde &0x3fffffffff000ULL ; -for (l3 =0 ; l3 <512 ; l3++) { -cpu_physical_memory_read (pt_addr + l3 *8 , &pte,8 ); - pte =le64_to_cpu (pte); -if (pte &PG_PRESENT_MASK ) { -print_pte (mon, env, (l1 <<30 ) + (l2 <<21 ) - + (l3 <<12 ), - pte & ~PG_PSE_MASK , - ~(hwaddr )0xfff ); - } - } - } - } - } - } +/* Trim line to fit screen */ +if (pos - buf >79 ) { +strcpy (buf +77 ,".." ); } -} -#ifdef TARGET_X86_64 -static void tlb_info_la48 (Monitor *mon,CPUArchState *env, -uint64_t l0,uint64_t pml4_addr) -{ -uint64_t l1, l2, l3, l4; -uint64_t pml4e, pdpe, pde, pte; -uint64_t pdp_addr, pd_addr, pt_addr; - -for (l1 =0 ; l1 <512 ; l1++) { -cpu_physical_memory_read (pml4_addr + l1 *8 , &pml4e,8 ); - pml4e =le64_to_cpu (pml4e); -if (!(pml4e &PG_PRESENT_MASK )) { -continue ; - } - - pdp_addr = pml4e &0x3fffffffff000ULL ; -for (l2 =0 ; l2 <512 ; l2++) { -cpu_physical_memory_read (pdp_addr + l2 *8 , &pdpe,8 ); - pdpe =le64_to_cpu (pdpe); -if (!(pdpe &PG_PRESENT_MASK )) { -continue ; - } - -if (pdpe &PG_PSE_MASK ) { -/* 1G pages, CR4.PSE is ignored */ -print_pte (mon, env, (l0 <<48 ) + (l1 <<39 ) + (l2 <<30 ), - pdpe,0x3ffffc0000000ULL ); -continue ; - } - - pd_addr = pdpe &0x3fffffffff000ULL ; -for (l3 =0 ; l3 <512 ; l3++) { -cpu_physical_memory_read (pd_addr + l3 *8 , &pde,8 ); - pde =le64_to_cpu (pde); -if (!(pde &PG_PRESENT_MASK )) { -continue ; - } - -if (pde &PG_PSE_MASK ) { -/* 2M pages, CR4.PSE is ignored */ -print_pte (mon, env, (l0 <<48 ) + (l1 <<39 ) + (l2 <<30 ) + - (l3 <<21 ), pde,0x3ffffffe00000ULL ); -continue ; - } - - pt_addr = pde &0x3fffffffff000ULL ; -for (l4 =0 ; l4 <512 ; l4++) { -cpu_physical_memory_read (pt_addr - + l4 *8 , - &pte,8 ); - pte =le64_to_cpu (pte); -if (pte &PG_PRESENT_MASK ) { -print_pte (mon, env, (l0 <<48 ) + (l1 <<39 ) + - (l2 <<30 ) + (l3 <<21 ) + (l4 <<12 ), - pte & ~PG_PSE_MASK ,0x3fffffffff000ULL ); - } - } - } - } - } +monitor_printf (mon,"%s\n" , buf); } -static void tlb_info_la57 (Monitor *mon,CPUArchState *env) +static +int mem_print_tlb (CPUState *cs,void *data,PTE_t *pte, +target_ulong vaddr,int height,int offset) { -uint64_t l0; -uint64_t pml5e; -uint64_t pml5_addr; - - pml5_addr = env->cr[3 ] &0x3fffffffff000ULL ; -for (l0 =0 ; l0 <512 ; l0++) { -cpu_physical_memory_read (pml5_addr + l0 *8 , &pml5e,8 ); - pml5e =le64_to_cpu (pml5e); -if (pml5e &PG_PRESENT_MASK ) { -tlb_info_la48 (mon, env, l0, pml5e &0x3fffffffff000ULL ); - } - } +struct mem_print_state *state = (struct mem_print_state *) data; +print_pte (state->mon, state->env, vaddr, pte->pte64_t); +return 0 ; } -#endif /* TARGET_X86_64 */ void hmp_info_tlb (Monitor *mon,const QDict *qdict) { -CPUArchState *env; +struct mem_print_state state; - env =mon_get_cpu_env (mon); -if (!env) { -monitor_printf (mon,"No CPU available\n" ); +CPUState *cs =mon_get_cpu (mon); +if (!cs) { +monitor_printf (mon,"Unable to get CPUState. Internal error\n" );return ; } -if (!(env->cr[0 ] &CR0_PG_MASK )) { -monitor_printf (mon,"PG disabled\n" ); +if (!init_iterator (mon, &state)) {return ; } -if (env->cr[4 ] &CR4_PAE_MASK ) { -#ifdef TARGET_X86_64 -if (env->hflags &HF_LMA_MASK ) { -if (env->cr[4 ] &CR4_LA57_MASK ) { -tlb_info_la57 (mon, env); - }else { -tlb_info_la48 (mon, env,0 , env->cr[3 ] &0x3fffffffff000ULL ); - } - } else -#endif - { -tlb_info_pae32 (mon, env); - } - }else { -tlb_info_32 (mon, env); - } + +/** +* 'info tlb' visits only leaf PTEs marked present. +* It does not check other protection bits. +*/ +for_each_pte (cs, &mem_print_tlb, &state, false, false); } static void mem_print(Monitor *mon, CPUArchState *env,Signature · 1 line
2.34.1
Signature · 4 lines
-----Open up your eyes, open up your mind, open up your code ------- / Dr. David Alan Gilbert | Running GNU/Linux | Happy \ \ dave @ treblig.org | | In Hex / \ _________________________|_____ http://www.treblig.org |_______/
Re: [PATCH v2 2/6] Convert 'info tlb' to use generic iterator
Quoted 4 lines · Dr. David Alan Gilbert
Quoted 1 line · * Don Porter ([email protected]) wrote:
Signed-off-by Don Porter<[email protected]>If this changes the output of 'info tlb' could you add a before/after to the commit message please.
Thanks for the advice. It should not change the output of info tlb.
Quoted 3 lines
Also, have a look at glib's
g_printfand friends, you might find they're easier;
https://www.manpagez.com/html/glib/glib-2.52.3/glib-String-Utility-Functions.php#g-printf
Thanks for this tip too! It isn't clear to me that converting will substantially simplify the patch at this point, but I'm open to it if I'm missing something or this is the preferred project style.
-Don
Re: [PATCH v2 2/6] Convert 'info tlb' to use generic iterator
Quoted 9 lines · Don Porter
Quoted 4 lines · Dr. David Alan Gilbert
Quoted 1 line · * Don Porter ([email protected]) wrote:
Signed-off-by Don Porter<[email protected]>If this changes the output of 'info tlb' could you add a before/after to the commit message please.
Thanks for the advice. It should not change the output of info tlb.
Quoted 7 lines
Quoted 3 lines
Also, have a look at glib's
g_printfand friends, you might find they're easier;
https://www.manpagez.com/html/glib/glib-2.52.3/glib-String-Utility-Functions.php#g-printfThanks for this tip too! It isn't clear to me that converting will substantially simplify the patch at this point, but I'm open to it if I'm missing something or this is the preferred project style.
sprintf is deprecated in the current MacOS version, so using it produces warnings. Once we convert our existing usage, we will want to poison new uses entirely.
r~
Re: [PATCH v2 2/6] Convert 'info tlb' to use generic iterator
Quoted 15 lines · Richard Henderson
Quoted 14 lines · Don Porter
Quoted 4 lines · Dr. David Alan Gilbert
Quoted 1 line · * Don Porter ([email protected]) wrote:
Signed-off-by Don Porter<[email protected]>If this changes the output of 'info tlb' could you add a before/after to the commit message please.
Thanks for the advice. It should not change the output of info tlb.
Quoted 4 lines
Also, have a look at glib's
g_printfand friends, you might find they're easier;
https://www.manpagez.com/html/glib/glib-2.52.3/glib-String-Utility-Functions.php#g-printf
Quoted 7 lines
Quoted 3 lines
Thanks for this tip too! It isn't clear to me that converting will substantially simplify the patch at this point, but I'm open to it if I'm missing something or this is the preferred project style.
sprintf is deprecated in the current MacOS version, so using it produces warnings. Once we convert our existing usage, we will want to poison new uses entirely.
Gotcha, makes sense.
Thank you,
Don
PATCH v2 3/6 Convert 'info mem' to use generic iterator
In reply to Don Porter--- target/i386/monitor.c | 344 +++++------------------------------------- 1 file changed, 35 insertions(+), 309 deletions(-)
target/i386/monitor.c +35 -309
@@ -480,332 +480,58 @@ void hmp_info_tlb(Monitor *mon, const QDict *qdict)for_each_pte (cs, &mem_print_tlb, &state, false, false); } -static void mem_print (Monitor *mon,CPUArchState *env, -hwaddr *pstart,int *plast_prot, -hwaddr end,int prot) -{ -int prot1; - prot1 = *plast_prot; -if (prot != prot1) { -if (*pstart !=-1 ) { -monitor_printf (mon, HWADDR_FMT_plx"-" HWADDR_FMT_plx" " - HWADDR_FMT_plx" %c%c%c\n" , -addr_canonical (env, *pstart), -addr_canonical (env, end), -addr_canonical (env, end - *pstart), - prot1 &PG_USER_MASK ?'u' :'-' , -'r' , - prot1 &PG_RW_MASK ?'w' :'-' ); - } -if (prot !=0 ) - *pstart = end; -else - *pstart =-1 ; - *plast_prot = prot; - } -} - -static void mem_info_32 (Monitor *mon,CPUArchState *env) +static +bool mem_print (CPUState *cs,struct mem_print_state *state) { -unsigned int l1, l2; -int prot, last_prot; -uint32_t pgd, pde, pte; -hwaddr start, end; - - pgd = env->cr[3 ] & ~0xfff ; - last_prot =0 ; - start =-1 ; -for (l1 =0 ; l1 <1024 ; l1++) { -cpu_physical_memory_read (pgd + l1 *4 , &pde,4 ); - pde =le32_to_cpu (pde); - end = l1 <<22 ; -if (pde &PG_PRESENT_MASK ) { -if ((pde &PG_PSE_MASK ) && (env->cr[4 ] &CR4_PSE_MASK )) { - prot = pde & (PG_USER_MASK |PG_RW_MASK |PG_PRESENT_MASK ); -mem_print (mon, env, &start, &last_prot, end, prot); - }else { -for (l2 =0 ; l2 <1024 ; l2++) { -cpu_physical_memory_read ((pde & ~0xfff ) + l2 *4 , &pte,4 ); - pte =le32_to_cpu (pte); - end = (l1 <<22 ) + (l2 <<12 ); -if (pte &PG_PRESENT_MASK ) { - prot = pte & pde & - (PG_USER_MASK |PG_RW_MASK |PG_PRESENT_MASK ); - }else { - prot =0 ; - } -mem_print (mon, env, &start, &last_prot, end, prot); - } - } - }else { - prot =0 ; -mem_print (mon, env, &start, &last_prot, end, prot); - } - } -/* Flush last range */ -mem_print (mon, env, &start, &last_prot, (hwaddr )1 <<32 ,0 ); -} +CPUArchState *env = state->env; +int i =0 ; -static void mem_info_pae32 (Monitor *mon,CPUArchState *env) -{ -unsigned int l1, l2, l3; -int prot, last_prot; -uint64_t pdpe, pde, pte; -uint64_t pdp_addr, pd_addr, pt_addr; -hwaddr start, end; - - pdp_addr = env->cr[3 ] & ~0x1f ; - last_prot =0 ; - start =-1 ; -for (l1 =0 ; l1 <4 ; l1++) { -cpu_physical_memory_read (pdp_addr + l1 *8 , &pdpe,8 ); - pdpe =le64_to_cpu (pdpe); - end = l1 <<30 ; -if (pdpe &PG_PRESENT_MASK ) { - pd_addr = pdpe &0x3fffffffff000ULL ; -for (l2 =0 ; l2 <512 ; l2++) { -cpu_physical_memory_read (pd_addr + l2 *8 , &pde,8 ); - pde =le64_to_cpu (pde); - end = (l1 <<30 ) + (l2 <<21 ); -if (pde &PG_PRESENT_MASK ) { -if (pde &PG_PSE_MASK ) { - prot = pde & (PG_USER_MASK |PG_RW_MASK | -PG_PRESENT_MASK ); -mem_print (mon, env, &start, &last_prot, end, prot); - }else { - pt_addr = pde &0x3fffffffff000ULL ; -for (l3 =0 ; l3 <512 ; l3++) { -cpu_physical_memory_read (pt_addr + l3 *8 , &pte,8 ); - pte =le64_to_cpu (pte); - end = (l1 <<30 ) + (l2 <<21 ) + (l3 <<12 ); -if (pte &PG_PRESENT_MASK ) { - prot = pte & pde & (PG_USER_MASK |PG_RW_MASK | -PG_PRESENT_MASK ); - }else { - prot =0 ; - } -mem_print (mon, env, &start, &last_prot, end, prot); - } - } - }else { - prot =0 ; -mem_print (mon, env, &start, &last_prot, end, prot); - } - } - }else { - prot =0 ; -mem_print (mon, env, &start, &last_prot, end, prot); - } - } -/* Flush last range */ -mem_print (mon, env, &start, &last_prot, (hwaddr )1 <<32 ,0 ); -} - - -#ifdef TARGET_X86_64 -static void mem_info_la48 (Monitor *mon,CPUArchState *env) -{ -int prot, last_prot; -uint64_t l1, l2, l3, l4; -uint64_t pml4e, pdpe, pde, pte; -uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr, start, end; - - pml4_addr = env->cr[3 ] &0x3fffffffff000ULL ; - last_prot =0 ; - start =-1 ; -for (l1 =0 ; l1 <512 ; l1++) { -cpu_physical_memory_read (pml4_addr + l1 *8 , &pml4e,8 ); - pml4e =le64_to_cpu (pml4e); - end = l1 <<39 ; -if (pml4e &PG_PRESENT_MASK ) { - pdp_addr = pml4e &0x3fffffffff000ULL ; -for (l2 =0 ; l2 <512 ; l2++) { -cpu_physical_memory_read (pdp_addr + l2 *8 , &pdpe,8 ); - pdpe =le64_to_cpu (pdpe); - end = (l1 <<39 ) + (l2 <<30 ); -if (pdpe &PG_PRESENT_MASK ) { -if (pdpe &PG_PSE_MASK ) { - prot = pdpe & (PG_USER_MASK |PG_RW_MASK | -PG_PRESENT_MASK ); - prot &= pml4e; -mem_print (mon, env, &start, &last_prot, end, prot); - }else { - pd_addr = pdpe &0x3fffffffff000ULL ; -for (l3 =0 ; l3 <512 ; l3++) { -cpu_physical_memory_read (pd_addr + l3 *8 , &pde,8 ); - pde =le64_to_cpu (pde); - end = (l1 <<39 ) + (l2 <<30 ) + (l3 <<21 ); -if (pde &PG_PRESENT_MASK ) { -if (pde &PG_PSE_MASK ) { - prot = pde & (PG_USER_MASK |PG_RW_MASK | -PG_PRESENT_MASK ); - prot &= pml4e & pdpe; -mem_print (mon, env, &start, - &last_prot, end, prot); - }else { - pt_addr = pde &0x3fffffffff000ULL ; -for (l4 =0 ; l4 <512 ; l4++) { -cpu_physical_memory_read (pt_addr - + l4 *8 , - &pte,8 ); - pte =le64_to_cpu (pte); - end = (l1 <<39 ) + (l2 <<30 ) + - (l3 <<21 ) + (l4 <<12 ); -if (pte &PG_PRESENT_MASK ) { - prot = pte & (PG_USER_MASK |PG_RW_MASK | -PG_PRESENT_MASK ); - prot &= pml4e & pdpe & pde; - }else { - prot =0 ; - } -mem_print (mon, env, &start, - &last_prot, end, prot); - } - } - }else { - prot =0 ; -mem_print (mon, env, &start, - &last_prot, end, prot); - } - } - } - }else { - prot =0 ; -mem_print (mon, env, &start, &last_prot, end, prot); - } - } - }else { - prot =0 ; -mem_print (mon, env, &start, &last_prot, end, prot); +/* We need to figure out the lowest populated level */ +for ( ; i < state->max_height; i++) { +if (state->vstart[i] !=-1 ) { +break ; } } -/* Flush last range */ -mem_print (mon, env, &start, &last_prot, (hwaddr )1 <<48 ,0 ); -} -static void mem_info_la57 (Monitor *mon,CPUArchState *env) -{ -int prot, last_prot; -uint64_t l0, l1, l2, l3, l4; -uint64_t pml5e, pml4e, pdpe, pde, pte; -uint64_t pml5_addr, pml4_addr, pdp_addr, pd_addr, pt_addr, start, end; - - pml5_addr = env->cr[3 ] &0x3fffffffff000ULL ; - last_prot =0 ; - start =-1 ; -for (l0 =0 ; l0 <512 ; l0++) { -cpu_physical_memory_read (pml5_addr + l0 *8 , &pml5e,8 ); - pml5e =le64_to_cpu (pml5e); - end = l0 <<48 ; -if (!(pml5e &PG_PRESENT_MASK )) { - prot =0 ; -mem_print (mon, env, &start, &last_prot, end, prot); -continue ; - } +hwaddr vstart = state->vstart[i]; +hwaddr end = state->vend[i] +mmu_pte_leaf_page_size (cs, i); +int prot =ent2prot (state->ent[i]); - pml4_addr = pml5e &0x3fffffffff000ULL ; -for (l1 =0 ; l1 <512 ; l1++) { -cpu_physical_memory_read (pml4_addr + l1 *8 , &pml4e,8 ); - pml4e =le64_to_cpu (pml4e); - end = (l0 <<48 ) + (l1 <<39 ); -if (!(pml4e &PG_PRESENT_MASK )) { - prot =0 ; -mem_print (mon, env, &start, &last_prot, end, prot); -continue ; - } - - pdp_addr = pml4e &0x3fffffffff000ULL ; -for (l2 =0 ; l2 <512 ; l2++) { -cpu_physical_memory_read (pdp_addr + l2 *8 , &pdpe,8 ); - pdpe =le64_to_cpu (pdpe); - end = (l0 <<48 ) + (l1 <<39 ) + (l2 <<30 ); -if (pdpe &PG_PRESENT_MASK ) { - prot =0 ; -mem_print (mon, env, &start, &last_prot, end, prot); -continue ; - } - -if (pdpe &PG_PSE_MASK ) { - prot = pdpe & (PG_USER_MASK |PG_RW_MASK | -PG_PRESENT_MASK ); - prot &= pml5e & pml4e; -mem_print (mon, env, &start, &last_prot, end, prot); -continue ; - } - pd_addr = pdpe &0x3fffffffff000ULL ; -for (l3 =0 ; l3 <512 ; l3++) { -cpu_physical_memory_read (pd_addr + l3 *8 , &pde,8 ); - pde =le64_to_cpu (pde); - end = (l0 <<48 ) + (l1 <<39 ) + (l2 <<30 ) + (l3 <<21 ); -if (pde &PG_PRESENT_MASK ) { - prot =0 ; -mem_print (mon, env, &start, &last_prot, end, prot); -continue ; - } - -if (pde &PG_PSE_MASK ) { - prot = pde & (PG_USER_MASK |PG_RW_MASK | -PG_PRESENT_MASK ); - prot &= pml5e & pml4e & pdpe; -mem_print (mon, env, &start, &last_prot, end, prot); -continue ; - } - - pt_addr = pde &0x3fffffffff000ULL ; -for (l4 =0 ; l4 <512 ; l4++) { -cpu_physical_memory_read (pt_addr + l4 *8 , &pte,8 ); - pte =le64_to_cpu (pte); - end = (l0 <<48 ) + (l1 <<39 ) + (l2 <<30 ) + - (l3 <<21 ) + (l4 <<12 ); -if (pte &PG_PRESENT_MASK ) { - prot = pte & (PG_USER_MASK |PG_RW_MASK | -PG_PRESENT_MASK ); - prot &= pml5e & pml4e & pdpe & pde; - }else { - prot =0 ; - } -mem_print (mon, env, &start, &last_prot, end, prot); - } - } - } - } - } -/* Flush last range */ -mem_print (mon, env, &start, &last_prot, (hwaddr )1 <<57 ,0 ); +monitor_printf (state->mon, HWADDR_FMT_plx"-" HWADDR_FMT_plx" " + HWADDR_FMT_plx" %c%c%c\n" , +addr_canonical (env, vstart), +addr_canonical (env, end), +addr_canonical (env, end - vstart), + prot &PG_USER_MASK ?'u' :'-' , +'r' , + prot &PG_RW_MASK ?'w' :'-' ); +return true; } -#endif /* TARGET_X86_64 */ void hmp_info_mem (Monitor *mon,const QDict *qdict) { -CPUArchState *env; +CPUState *cs; +struct mem_print_state state; - env =mon_get_cpu_env (mon); -if (!env) { -monitor_printf (mon,"No CPU available\n" ); +if (!init_iterator (mon, &state)) {return ; } + state.flusher = mem_print; -if (!(env->cr[0 ] &CR0_PG_MASK )) { -monitor_printf (mon,"PG disabled\n" ); + cs =mon_get_cpu (mon); +if (!cs) { +monitor_printf (mon,"Unable to get CPUState. Internal error\n" );return ; } -if (env->cr[4 ] &CR4_PAE_MASK ) { -#ifdef TARGET_X86_64 -if (env->hflags &HF_LMA_MASK ) { -if (env->cr[4 ] &CR4_LA57_MASK ) { -mem_info_la57 (mon, env); - }else { -mem_info_la48 (mon, env); - } - } else -#endif - { -mem_info_pae32 (mon, env); - } - }else { -mem_info_32 (mon, env); - } + +/** +* We must visit interior entries to update prot +*/ +for_each_pte (cs, &compressing_iterator, &state, true, false); + +/* Flush the last entry, if needed */ +mem_print (cs, &state); }void hmp_mce (Monitor *mon,const QDict *qdict)
Signature · 1 line
2.34.1
PATCH v2 4/6 Convert x86_cpu_get_memory_mapping() to use generic iterators
In reply to Don Porter--- target/i386/arch_memory_mapping.c | 318 ++++-------------------------- 1 file changed, 40 insertions(+), 278 deletions(-)
target/i386/arch_memory_mapping.c +40 -278
@@ -19,6 +19,7 @@************** code hook implementations for x86 *********** */ +/* PAE Paging or IA-32e Paging */ #define PML4_ADDR_MASK 0xffffffffff000ULL/* selects bits 51:12 */ /**
@@ -499,302 +500,63 @@ bool for_each_pte(CPUState *cs,/** * Back to x86 hooks */ +struct memory_mapping_data { +MemoryMappingList *list; +}; -/* PAE Paging or IA-32e Paging */ -static void walk_pte (MemoryMappingList *list,AddressSpace *as, -hwaddr pte_start_addr, -int32_t a20_mask,target_ulong start_line_addr) -{ -hwaddr pte_addr, start_paddr; -uint64_t pte; -target_ulong start_vaddr; -int i; - -for (i =0 ; i <512 ; i++) { - pte_addr = (pte_start_addr + i *8 ) & a20_mask; - pte =address_space_ldq (as, pte_addr,MEMTXATTRS_UNSPECIFIED ,NULL ); -if (!(pte &PG_PRESENT_MASK )) { -/* not present */ -continue ; - } - - start_paddr = (pte & ~0xfff ) & ~(0x1ULL <<63 ); -if (cpu_physical_memory_is_io (start_paddr)) { -/* I/O region */ -continue ; - } - - start_vaddr = start_line_addr | ((i &0x1ff ) <<12 ); -memory_mapping_list_add_merge_sorted (list, start_paddr, - start_vaddr,1 <<12 ); - } -} - -/* 32-bit Paging */ -static void walk_pte2 (MemoryMappingList *list,AddressSpace *as, -hwaddr pte_start_addr,int32_t a20_mask, -target_ulong start_line_addr) -{ -hwaddr pte_addr, start_paddr; -uint32_t pte; -target_ulong start_vaddr; -int i; - -for (i =0 ; i <1024 ; i++) { - pte_addr = (pte_start_addr + i *4 ) & a20_mask; - pte =address_space_ldl (as, pte_addr,MEMTXATTRS_UNSPECIFIED ,NULL ); -if (!(pte &PG_PRESENT_MASK )) { -/* not present */ -continue ; - } - - start_paddr = pte & ~0xfff ; -if (cpu_physical_memory_is_io (start_paddr)) { -/* I/O region */ -continue ; - } - - start_vaddr = start_line_addr | ((i &0x3ff ) <<12 ); -memory_mapping_list_add_merge_sorted (list, start_paddr, - start_vaddr,1 <<12 ); - } -} - -/* PAE Paging or IA-32e Paging */ -#define PLM4_ADDR_MASK 0xffffffffff000ULL/* selects bits 51:12 */ - -static void walk_pde (MemoryMappingList *list,AddressSpace *as, -hwaddr pde_start_addr, -int32_t a20_mask,target_ulong start_line_addr) +static int add_memory_mapping_to_list (CPUState *cs,void *data,PTE_t *pte, +target_ulong vaddr,int height, +int offset) { -hwaddr pde_addr, pte_start_addr, start_paddr; -uint64_t pde; -target_ulong line_addr, start_vaddr; -int i; - -for (i =0 ; i <512 ; i++) { - pde_addr = (pde_start_addr + i *8 ) & a20_mask; - pde =address_space_ldq (as, pde_addr,MEMTXATTRS_UNSPECIFIED ,NULL ); -if (!(pde &PG_PRESENT_MASK )) { -/* not present */ -continue ; - } - - line_addr = start_line_addr | ((i &0x1ff ) <<21 ); -if (pde &PG_PSE_MASK ) { -/* 2 MB page */ - start_paddr = (pde & ~0x1fffff ) & ~(0x1ULL <<63 ); -if (cpu_physical_memory_is_io (start_paddr)) { -/* I/O region */ -continue ; - } - start_vaddr = line_addr; -memory_mapping_list_add_merge_sorted (list, start_paddr, - start_vaddr,1 <<21 ); -continue ; - } - - pte_start_addr = (pde &PLM4_ADDR_MASK ) & a20_mask; -walk_pte (list, as, pte_start_addr, a20_mask, line_addr); - } -} +X86CPU *cpu =X86_CPU (cs); +CPUX86State *env = &cpu->env; -/* 32-bit Paging */ -static void walk_pde2 (MemoryMappingList *list,AddressSpace *as, -hwaddr pde_start_addr,int32_t a20_mask, -bool pse) -{ -hwaddr pde_addr, pte_start_addr, start_paddr, high_paddr; -uint32_t pde; -target_ulong line_addr, start_vaddr; -int i; +struct memory_mapping_data *mm_data = (struct memory_mapping_data *) data; -for (i =0 ; i <1024 ; i++) { - pde_addr = (pde_start_addr + i *4 ) & a20_mask; - pde =address_space_ldl (as, pde_addr,MEMTXATTRS_UNSPECIFIED ,NULL ); -if (!(pde &PG_PRESENT_MASK )) { -/* not present */ -continue ; +hwaddr start_paddr =0 ; +size_t pg_size =mmu_pte_leaf_page_size (cs, height); +switch (height) { +case 1 : + start_paddr = pte->pte64_t & ~0xfff ; +if (env->cr[4 ] &CR4_PAE_MASK ) { + start_paddr &= ~(0x1ULL <<63 ); } - - line_addr = (((unsigned int )i &0x3ff ) <<22 ); -if ((pde &PG_PSE_MASK ) && pse) { +break ; +case 2 : +if (env->cr[4 ] &CR4_PAE_MASK ) { + start_paddr = (pte->pte64_t & ~0x1fffff ) & ~(0x1ULL <<63 ); + }else { +assert (!!(env->cr[4 ] &CR4_PSE_MASK ));/* * 4 MB page: * bits 39:32 are bits 20:13 of the PDE * bit3 31:22 are bits 31:22 of the PDE */ - high_paddr = ((hwaddr)(pde &0x1fe000 ) <<19 ); - start_paddr = (pde & ~0x3fffff ) | high_paddr; -if (cpu_physical_memory_is_io (start_paddr)) { -/* I/O region */ -continue ; - } - start_vaddr = line_addr; -memory_mapping_list_add_merge_sorted (list, start_paddr, - start_vaddr,1 <<22 ); -continue ; - } - - pte_start_addr = (pde & ~0xfff ) & a20_mask; -walk_pte2 (list, as, pte_start_addr, a20_mask, line_addr); - } -} - -/* PAE Paging */ -static void walk_pdpe2 (MemoryMappingList *list,AddressSpace *as, -hwaddr pdpe_start_addr,int32_t a20_mask) -{ -hwaddr pdpe_addr, pde_start_addr; -uint64_t pdpe; -target_ulong line_addr; -int i; - -for (i =0 ; i <4 ; i++) { - pdpe_addr = (pdpe_start_addr + i *8 ) & a20_mask; - pdpe =address_space_ldq (as, pdpe_addr,MEMTXATTRS_UNSPECIFIED ,NULL ); -if (!(pdpe &PG_PRESENT_MASK )) { -/* not present */ -continue ; +hwaddr high_paddr = ((hwaddr)(pte->pte64_t &0x1fe000 ) <<19 ); + start_paddr = (pte->pte64_t & ~0x3fffff ) | high_paddr; } - - line_addr = (((unsigned int )i &0x3 ) <<30 ); - pde_start_addr = (pdpe & ~0xfff ) & a20_mask; -walk_pde (list, as, pde_start_addr, a20_mask, line_addr); - } -} - -#ifdef TARGET_X86_64 -/* IA-32e Paging */ -static void walk_pdpe (MemoryMappingList *list,AddressSpace *as, -hwaddr pdpe_start_addr,int32_t a20_mask, -target_ulong start_line_addr) -{ -hwaddr pdpe_addr, pde_start_addr, start_paddr; -uint64_t pdpe; -target_ulong line_addr, start_vaddr; -int i; - -for (i =0 ; i <512 ; i++) { - pdpe_addr = (pdpe_start_addr + i *8 ) & a20_mask; - pdpe =address_space_ldq (as, pdpe_addr,MEMTXATTRS_UNSPECIFIED ,NULL ); -if (!(pdpe &PG_PRESENT_MASK )) { -/* not present */ -continue ; - } - - line_addr = start_line_addr | ((i &0x1ffULL ) <<30 ); -if (pdpe &PG_PSE_MASK ) { -/* 1 GB page */ - start_paddr = (pdpe & ~0x3fffffff ) & ~(0x1ULL <<63 ); -if (cpu_physical_memory_is_io (start_paddr)) { -/* I/O region */ -continue ; - } - start_vaddr = line_addr; -memory_mapping_list_add_merge_sorted (list, start_paddr, - start_vaddr,1 <<30 ); -continue ; - } - - pde_start_addr = (pdpe &PLM4_ADDR_MASK ) & a20_mask; -walk_pde (list, as, pde_start_addr, a20_mask, line_addr); +break ; +case 3 : +/* Select bits 30--51 */ + start_paddr = (pte->pte64_t &0xfffffc0000000 ); +break ; +default : +g_assert_not_reached (); } -} - -/* IA-32e Paging */ -static void walk_pml4e (MemoryMappingList *list,AddressSpace *as, -hwaddr pml4e_start_addr,int32_t a20_mask, -target_ulong start_line_addr) -{ -hwaddr pml4e_addr, pdpe_start_addr; -uint64_t pml4e; -target_ulong line_addr; -int i; -for (i =0 ; i <512 ; i++) { - pml4e_addr = (pml4e_start_addr + i *8 ) & a20_mask; - pml4e =address_space_ldq (as, pml4e_addr,MEMTXATTRS_UNSPECIFIED , -NULL ); -if (!(pml4e &PG_PRESENT_MASK )) { -/* not present */ -continue ; - } - - line_addr = start_line_addr | ((i &0x1ffULL ) <<39 ); - pdpe_start_addr = (pml4e &PLM4_ADDR_MASK ) & a20_mask; -walk_pdpe (list, as, pdpe_start_addr, a20_mask, line_addr); +/* This hook skips mappings for the I/O region */ +if (cpu_physical_memory_is_io (start_paddr)) { +/* I/O region */ +return 0 ; } -} -static void walk_pml5e (MemoryMappingList *list,AddressSpace *as, -hwaddr pml5e_start_addr,int32_t a20_mask) -{ -hwaddr pml5e_addr, pml4e_start_addr; -uint64_t pml5e; -target_ulong line_addr; -int i; - -for (i =0 ; i <512 ; i++) { - pml5e_addr = (pml5e_start_addr + i *8 ) & a20_mask; - pml5e =address_space_ldq (as, pml5e_addr,MEMTXATTRS_UNSPECIFIED , -NULL ); -if (!(pml5e &PG_PRESENT_MASK )) { -/* not present */ -continue ; - } - - line_addr = (0x7fULL <<57 ) | ((i &0x1ffULL ) <<48 ); - pml4e_start_addr = (pml5e &PLM4_ADDR_MASK ) & a20_mask; -walk_pml4e (list, as, pml4e_start_addr, a20_mask, line_addr); - } +memory_mapping_list_add_merge_sorted (mm_data->list, start_paddr, + vaddr, pg_size); +return 0 ; } -#endif bool x86_cpu_get_memory_mapping (CPUState *cs,MemoryMappingList *list,Error **errp) { -X86CPU *cpu =X86_CPU (cs); -CPUX86State *env = &cpu->env; -int32_t a20_mask; - -if (!cpu_paging_enabled (cs)) { -/* paging is disabled */ -return true; - } - - a20_mask =x86_get_a20_mask (env); -if (env->cr[4 ] &CR4_PAE_MASK ) { -#ifdef TARGET_X86_64 -if (env->hflags &HF_LMA_MASK ) { -if (env->cr[4 ] &CR4_LA57_MASK ) { -hwaddr pml5e_addr; - - pml5e_addr = (env->cr[3 ] &PLM4_ADDR_MASK ) & a20_mask; -walk_pml5e (list, cs->as, pml5e_addr, a20_mask); - }else { -hwaddr pml4e_addr; - - pml4e_addr = (env->cr[3 ] &PLM4_ADDR_MASK ) & a20_mask; -walk_pml4e (list, cs->as, pml4e_addr, a20_mask, -0xffffULL <<48 ); - } - } else -#endif - { -hwaddr pdpe_addr; - - pdpe_addr = (env->cr[3 ] & ~0x1f ) & a20_mask; -walk_pdpe2 (list, cs->as, pdpe_addr, a20_mask); - } - }else { -hwaddr pde_addr; -bool pse; - - pde_addr = (env->cr[3 ] & ~0xfff ) & a20_mask; - pse = !!(env->cr[4 ] &CR4_PSE_MASK ); -walk_pde2 (list, cs->as, pde_addr, a20_mask, pse); - } - -return true; +return for_each_pte (cs, &add_memory_mapping_to_list, list, false, false); }
Signature · 1 line
2.34.1
PATCH v2 5/6 Move tcg implementation of x86 get_physical_address into common helper code.
In reply to Don Porter--- target/i386/cpu.h | 41 ++ target/i386/helper.c | 515 +++++++++++++++++++++++++ target/i386/tcg/sysemu/excp_helper.c | 555 +-------------------------- 3 files changed, 561 insertions(+), 550 deletions(-)
target/i386/cpu.h +41 -0
@@ -2094,6 +2094,42 @@ struct X86CPUClass {ResettablePhases parent_phases; }; +typedef struct X86TranslateParams { +target_ulong addr; +target_ulong cr3; +int pg_mode; +int mmu_idx; +int ptw_idx; +MMUAccessType access_type; +}X86TranslateParams ; + +typedef struct X86TranslateResult { +hwaddr paddr; +int prot; +int page_size; +}X86TranslateResult ; + +typedef enum X86TranslateFaultStage2 { +S2_NONE , +S2_GPA , +S2_GPT , +}X86TranslateFaultStage2 ; + +typedef struct X86TranslateFault { +int exception_index; +int error_code; +target_ulong cr2; +X86TranslateFaultStage2 stage2; +}X86TranslateFault ; + +typedef struct X86PTETranslate { +CPUX86State *env; +X86TranslateFault *err; +int ptw_idx; +void *haddr; +hwaddr gaddr; +}X86PTETranslate ; +/* Intended to become a generic PTE type */ typedef union PTE { uint64_t pte64_t;
@@ -2137,6 +2173,11 @@ void x86_cpu_list(void);int cpu_x86_support_mca_broadcast (CPUX86State *env);#ifndef CONFIG_USER_ONLY +bool x86_cpu_get_physical_address (CPUX86State *env,vaddr addr, +MMUAccessType access_type,int mmu_idx, +X86TranslateResult *out, +X86TranslateFault *err,uint64_t ra); +hwaddr x86_cpu_get_phys_page_attrs_debug (CPUState *cpu,vaddr addr,MemTxAttrs *attrs);int cpu_get_pic_interrupt (CPUX86State *s);
target/i386/helper.c +515 -0
527 lines not on this page · raw message
target/i386/tcg/sysemu/excp_helper.c +5 -550
574 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v2 6/6 Convert x86_mmu_translate() to use common code.
In reply to Don Porter--- target/i386/arch_memory_mapping.c | 37 ++- target/i386/cpu.h | 11 +- target/i386/helper.c | 371 ++++++--------------------- target/i386/tcg/sysemu/excp_helper.c | 2 +- 4 files changed, 128 insertions(+), 293 deletions(-)
target/i386/arch_memory_mapping.c +33 -4
@@ -33,7 +33,7 @@* Returns a hardware address on success. Should not fail (i.e., caller is * responsible to ensure that a page table is actually present). */ -static hwaddr mmu_page_table_root (CPUState *cs,int *height) +hwaddr mmu_page_table_root (CPUState *cs,int *height) {X86CPU *cpu =X86_CPU (cs);CPUX86State *env = &cpu->env;
@@ -228,6 +228,35 @@ static void _mmu_decode_va_parameters(CPUState *cs, int height, } } +/** +* mmu_virtual_to_pte_index - Given a virtual address and height in the +* page table radix tree, return the index that should be used +* to look up the next page table entry (pte) in translating an +* address. +* +* @cs - CPU state +* @vaddr - The virtual address to translate +* @height - height of node within the tree (leaves are 1, not 0). +* +* Example: In 32-bit x86 page tables, the virtual address is split +* into 10 bits at height 2, 10 bits at height 1, and 12 offset bits. +* So a call with VA and height 2 would return the first 10 bits of va, +* right shifted by 22. +*/ + +int mmu_virtual_to_pte_index (CPUState *cs,target_ulong vaddr,int height) +{ +int shift =0 ; +int width =0 ; +int mask =0 ; + +_mmu_decode_va_parameters (cs, height, &shift, &width); + + mask = (1 << width) -1 ; + +return (vaddr >> shift) & mask; +} +/** * get_pte - Copy the contents of the page table entry at node[i] into pt_entry. * Optionally, add the relevant bits to the virtual address in
@@ -247,7 +276,7 @@ static void _mmu_decode_va_parameters(CPUState *cs, int height,* Optional parameter. */ -static void +void get_pte (CPUState *cs,hwaddr node,int i,int height,PTE_t *pt_entry,target_ulong vaddr_parent,target_ulong *vaddr_pte,hwaddr *pte_paddr)
@@ -284,7 +313,7 @@ get_pte(CPUState *cs, hwaddr node, int i, int height, } -static bool +bool mmu_pte_check_bits (CPUState *cs,PTE_t *pte,int64_t mask) {X86CPU *cpu =X86_CPU (cs);
@@ -300,7 +329,7 @@ mmu_pte_check_bits(CPUState *cs, PTE_t *pte, int64_t mask)* mmu_pte_presetn - Return true if the pte is * marked 'present' */ -static bool +bool mmu_pte_present (CPUState *cs,PTE_t *pte) {return mmu_pte_check_bits (cs, pte,PG_PRESENT_MASK );
target/i386/cpu.h +10 -1
@@ -2151,15 +2151,23 @@ int x86_cpu_write_elf64_qemunote(WriteCoreDumpFunction f, CPUState *cpu,int x86_cpu_write_elf32_qemunote (WriteCoreDumpFunction f,CPUState *cpu,DumpState *s); +hwaddr mmu_page_table_root (CPUState *cs,int *height); +bool mmu_pte_check_bits (CPUState *cs,PTE_t *pte,int64_t mask); +bool mmu_pte_present (CPUState *cs,PTE_t *pte);bool mmu_pte_leaf (CPUState *cs,int height,PTE_t *pte);target_ulong mmu_pte_leaf_page_size (CPUState *cs,int height);hwaddr mmu_pte_child (CPUState *cs,PTE_t *pte,int height);int mmu_page_table_entries_per_node (CPUState *cs,int height); +int mmu_virtual_to_pte_index (CPUState *cs,target_ulong vaddr,int height);bool for_each_pte (CPUState *cs,int (*fn)(CPUState *cs,void *data,PTE_t *pte,target_ulong vaddr,int height,int offset),void *data,bool visit_interior_nodes,bool visit_not_present); +void get_pte (CPUState *cs,hwaddr node,int i,int height,PTE_t *pt_entry, +target_ulong vaddr_parent,target_ulong *vaddr_pte, +hwaddr *pte_paddr); +bool x86_cpu_get_memory_mapping (CPUState *cpu,MemoryMappingList *list,Error **errp);
@@ -2176,7 +2184,8 @@ int cpu_x86_support_mca_broadcast(CPUX86State *env);bool x86_cpu_get_physical_address (CPUX86State *env,vaddr addr,MMUAccessType access_type,int mmu_idx,X86TranslateResult *out, -X86TranslateFault *err,uint64_t ra); +X86TranslateFault *err,uint64_t ra, +bool read_only);hwaddr x86_cpu_get_phys_page_attrs_debug (CPUState *cpu,vaddr addr,MemTxAttrs *attrs);
target/i386/helper.c +84 -287
438 lines not on this page · raw message
target/i386/tcg/sysemu/excp_helper.c +1 -1
@@ -56,7 +56,7 @@ bool x86_cpu_tlb_fill(CPUState *cs, vaddr addr, int size,X86TranslateFault err;if (x86_cpu_get_physical_address (env, addr, access_type, mmu_idx, &out, - &err, retaddr)) { + &err, retaddr, false)) {/* * Even if 4MB pages, we map only one 4KB page in the cache to * avoid filling it too fast.
Signature · 1 line
2.34.1
Re: [PATCH v2 0/6] Rework x86 page table walks
In reply to Don PorterQuoted 31 lines · Don Porter
This version of the 'info pg' command adopts Peter Maydell's request to write some guest-agnostic page table iterator and accessor code, along with architecture-specific hooks. The first patch in this series contributes a generic page table iterator and an x86 instantiation. As a client, we first introduce an 'info pg' monitor command, as well as a compressing callback hook for creating succinct page table representations.
After this, each successive patch replaces an exisitng x86 page table walker with a use of common iterator code.
I could use advice on how to ensure this is sufficiently well tested. I used 'make check' and 'make check-avocado', which both pass; what is the typical standard for testing something like a page table related change?
As far as generality, I have only attempted this on x86, but I expect the design would work for any similar radix-tree style page table.
I am still new enough to the code base that I wasn't certain about where to put the generic code, as well as naming conventions.
Per David Gilbert's suggestion, I was careful to ensure that monitor calls do not perturb TLB state (see the read-only flag in some functions).
I appreciate Nadav's suggestion about other ways to pursue the same
goal: I ended up deciding I would like to try my hand at consolidating the x86 page table code.
Quoted 8 lines
Don Porter (6):
Add an "info pg" command that prints the current page tables Convert 'info tlb' to use generic iterator
Convert 'info mem' to use generic iterator
Convertx86_cpu_get_memory_mapping()to use generic iterators Move tcg implementation of x86get_physical_addressinto common helper
code.
Convertx86_mmu_translate()to use common code.
Thanks for doing this work -- I like the diffstats for patches 2 and 3 a lot :-)
I have been digging out from under a big backlog of unreviewed patches, but in an ideal world I might get to this next week. Hopefully the x86 maintainers will have a look at it too.
thanks
-- PMM
Re: [PATCH v2 0/6] Rework x86 page table walks
Quoted 42 lines · Peter Maydell
Quoted 31 lines · Don Porter
This version of the 'info pg' command adopts Peter Maydell's request to write some guest-agnostic page table iterator and accessor code, along with architecture-specific hooks. The first patch in this series contributes a generic page table iterator and an x86 instantiation. As a client, we first introduce an 'info pg' monitor command, as well as a compressing callback hook for creating succinct page table representations.
After this, each successive patch replaces an exisitng x86 page table walker with a use of common iterator code.
I could use advice on how to ensure this is sufficiently well tested. I used 'make check' and 'make check-avocado', which both pass; what is the typical standard for testing something like a page table related change?
As far as generality, I have only attempted this on x86, but I expect the design would work for any similar radix-tree style page table.
I am still new enough to the code base that I wasn't certain about where to put the generic code, as well as naming conventions.
Per David Gilbert's suggestion, I was careful to ensure that monitor calls do not perturb TLB state (see the read-only flag in some functions).
I appreciate Nadav's suggestion about other ways to pursue the same
goal: I ended up deciding I would like to try my hand at consolidating the x86 page table code.Quoted 8 lines
Don Porter (6):
Add an "info pg" command that prints the current page tables Convert 'info tlb' to use generic iterator
Convert 'info mem' to use generic iterator
Convertx86_cpu_get_memory_mapping()to use generic iterators Move tcg implementation of x86get_physical_addressinto common helper
code.
Convertx86_mmu_translate()to use common code.
Quoted 6 lines
Thanks for doing this work -- I like the diffstats for patches 2 and 3 a lot :-)
I have been digging out from under a big backlog of unreviewed patches, but in an ideal world I might get to this next week. Hopefully the x86 maintainers will have a look at it too.
PS: forgot to mention, but if you eventually send a v2 of this series, could you send it as its own fresh email, not as a reply to an existing email like this series? Patches and patchsets that are replies to existing threads tend to get overlooked, and our automated patch tooling doesn't spot them either.
thanks
-- PMM